all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* problem with save-excursion
@ 2002-07-29 15:56 Mike Krell
  2002-07-29 19:47 ` fabrice bauzac
  0 siblings, 1 reply; 2+ messages in thread
From: Mike Krell @ 2002-07-29 15:56 UTC (permalink / raw)


Hello,

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?

   Thanks,
   Mike

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
          )
      )
    )
  )




__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

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

* Re: problem with save-excursion
  2002-07-29 15:56 problem with save-excursion Mike Krell
@ 2002-07-29 19:47 ` fabrice bauzac
  0 siblings, 0 replies; 2+ messages in thread
From: fabrice bauzac @ 2002-07-29 19:47 UTC (permalink / raw)


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

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

end of thread, other threads:[~2002-07-29 19:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-29 15:56 problem with save-excursion Mike Krell
2002-07-29 19:47 ` fabrice bauzac

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.