help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: does with-temp-buffer respect case-replace?


From: Seweryn Kokot
Subject: Re: does with-temp-buffer respect case-replace?
Date: Wed, 29 Oct 2008 15:49:24 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)

Xah <xahlee@gmail.com> writes:

> It seems that “with-temp-buffer” does not respect the var case-
> replace?
>
> here's a test code that replace some string in a region. It does this
> by first grabing the region's text, then create temp buffer, insert
> text, do replacement, grab the text back, then replace it in the
> original buffer.
>
> (defun tt (p1 p2)
>   "Do a bunch of find/replace on current text selection."
>   (interactive "r")
>
>   (let (mystr)
>     (setq mystr (buffer-substring p1 p2))
>     (setq mystr
>           (with-temp-buffer
>             (setq case-fold-search nil)
>             (setq case-replace t)
>             (insert mystr)
>
>             ;; Alt+t
>             (goto-char (point-min))
>             (while
>                 (search-forward-regexp "Alt\\+\\([[:graph:]]\\)" nil
> t)
>               (replace-match "<span class=\"kbd\"><span class=\"key
> \">Alt</span>+<span class=\"key\">\\1</span></span>"))
>
>             (buffer-string)
>             )
>           )
>
>     (delete-region p1 p2)
>     (insert mystr)
>     )
>   )
>
> Here's the text to test on:
>
> ... Alt+T ...
>
> doesn't matter what i set case-replace to, the result is:
>
> ... <Span Class="Kbd"><Span Class="Key">Alt</Span>+<Span
> Class="Key">T</Span></Span> ...
>
> Note the cap in “Span”, but i really want “span”.
> I spent a hour debugging this.
> Am i missing something?

just set the second argument as t for replace-match (see the docs for
replace-match). The following function works for me.

(defun tt (p1 p2)
  "Do a bunch of find/replace on current text selection."
  (interactive "r")

  (let (mystr)
    (setq mystr (buffer-substring p1 p2))
    (setq mystr
          (with-temp-buffer
            (insert mystr)
            ;; Alt+t
            (goto-char (point-min))
            (while
                (re-search-forward "Alt\\+\\([[:graph:]]\\)" nil t)
              (replace-match "<span class=\"kbd\"><span 
class=\"key\">Alt</span>+<span class=\"key\">\\1</span></span>" t))
            (buffer-string)
            )
          )
    (delete-region p1 p2)
    (insert mystr)
    )
  )

-- 
regards,
Seweryn Kokot





reply via email to

[Prev in Thread] Current Thread [Next in Thread]