[Top][All Lists]

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

restore region after replace-string

From: Paul Madden
Subject: restore region after replace-string
Date: Fri, 14 Jan 2011 15:57:17 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv: Gecko/20101207 Thunderbird/3.1.7

I'm not sure whether to ask the general or the specific question, so I'll ask 

Generally, I'd like the region I've defined (transient-mark-mode on) to be
restored after a command like replace-string. I often want to run many
replacements on the same region, and don't want to keep recreating it. I thought
I'd mention the real goal, in case there's a better method than what follows.

Specifically, here's what I've tried:

(defun rs1 ()
  (save-excursion (call-interactively 'replace-string)))

If I create a region and M-x rs1, the replacement works, point is restored, and
then I can use C-x C-x C-x C-x to get what I want: The same region highlighted
with point at its end.

But I don't want the extra keystrokes, so try to make the function do it for me:

(defun rs2 ()
  (save-excursion (call-interactively 'replace-string))

But no region is highlighted. And if I do M-x replace-string after rs2, the
replacement occurs only below point in the buffer, so it seems no region is 

Interestingly, though:

(defun rs3 ()
  (save-excursion (call-interactively 'replace-string))

After rs3, my region is highlighted with point at its end, and there's a
"definition is void" message (for the bogus (asdf) call) in the minibuffer. But
I can run replace-string now and it operates on the highlighted region.

So it seems like what I want is happening in rs3 (plus an unwanted error)... but
is undone when the function returns.

Can anybody shed any light on this, or point me in a better direction?

Incidentally, this is GNU Emacs, built from source, but I just tried
21.4.1 and it behaves the same.


reply via email to

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