all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* does with-temp-buffer respect case-replace?
@ 2008-10-29 13:41 Xah
  2008-10-29 14:49 ` Seweryn Kokot
  0 siblings, 1 reply; 2+ messages in thread
From: Xah @ 2008-10-29 13:41 UTC (permalink / raw)
  To: help-gnu-emacs

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?

Thanks.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: does with-temp-buffer respect case-replace?
  2008-10-29 13:41 does with-temp-buffer respect case-replace? Xah
@ 2008-10-29 14:49 ` Seweryn Kokot
  0 siblings, 0 replies; 2+ messages in thread
From: Seweryn Kokot @ 2008-10-29 14:49 UTC (permalink / raw)
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-10-29 14:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-29 13:41 does with-temp-buffer respect case-replace? Xah
2008-10-29 14:49 ` Seweryn Kokot

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.