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

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

Re: problem with save-excursion


From: fabrice bauzac
Subject: Re: problem with save-excursion
Date: Mon, 29 Jul 2002 21:47:21 +0200
User-agent: Mutt/1.4i

On Mon, Jul 29, 2002 at 08:56:59AM -0700, Mike Krell wrote:

> I've written a little elisp function to run the current buffer 
> through perl (or any other command).  However, the current region is 
> lost as a result of running the code, even though I'm using save-
> excursion.  Can anyone help with this?

> defun perl-replace-buffer ()
>   "Apply perl command to buffer"
>   (interactive)
>   (save-excursion
>     (mark-whole-buffer)
>     (let ((start (region-beginning))
>           (end   (region-end)))
>       (shell-command-on-region start end 
>         (read-from-minibuffer "Command: " '("perl -pe \"\"" . 11 ))
>           t
>           t
>           )
>       )
>     )
>   )

What happens is that shell-command-on-region first shrinks the
start->end region, and then inserts the result of the command.  But
when it shrinks the marked text, the point and mark move at the same
time.  They hold a byte-count from the beginning of the buffer:
i.e. "point is just before byte number (point)".  They "move", that is
the numbers change.  And since the buffer shrinks to emptyness, both
have the value 0 after the shrinking.

When you have two similar texts and a position (point, mark, ...) in
one of them, there is no easy way of finding the matching position in
the other text.  Maybe you could try saving the line number and the
column number of point and mark, and restoring them after the
operation, but anyway there will be cases where it doesn't work well.

However, if you use Emacs's functions to edit the text, it will be
able to keep track of all the modifications and your point and mark
will be set up correctly.  For example, you may use replace-regexp for
substitution.  But you may absolutely need Perl, in which case I don't
think we can do anything about that, apart having Perl tell Emacs
about the changes it makes to the text.

-- 
fabrice bauzac
Software should be free.  http://www.gnu.org/philosophy/why-free.html



reply via email to

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