From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: fabrice bauzac Newsgroups: gmane.emacs.help Subject: Re: problem with save-excursion Date: Mon, 29 Jul 2002 21:47:21 +0200 Sender: help-gnu-emacs-admin@gnu.org Message-ID: <20020729194721.GA23089@noon.dnsalias.net> References: <20020729155659.59139.qmail@web20209.mail.yahoo.com> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1027972085 14138 127.0.0.1 (29 Jul 2002 19:48:05 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 29 Jul 2002 19:48:05 +0000 (UTC) Return-path: Original-Received: from fencepost.gnu.org ([199.232.76.164]) by main.gmane.org with esmtp (Exim 3.33 #1 (Debian)) id 17ZGUu-0003fv-00 for ; Mon, 29 Jul 2002 21:48:04 +0200 Original-Received: from localhost ([127.0.0.1] helo=fencepost.gnu.org) by fencepost.gnu.org with esmtp (Exim 3.35 #1 (Debian)) id 17ZGV1-00032x-00; Mon, 29 Jul 2002 15:48:11 -0400 Original-Received: from atoulouse-104-1-3-250.abo.wanadoo.fr ([80.11.125.250] helo=noon.dnsalias.net) by fencepost.gnu.org with esmtp (Exim 3.35 #1 (Debian)) id 17ZGUR-00031w-00 for ; Mon, 29 Jul 2002 15:47:36 -0400 Original-Received: from noon by noon.dnsalias.net with local (Exim 3.35 #1 (Debian)) id 17ZGUD-00064f-00 for ; Mon, 29 Jul 2002 21:47:21 +0200 Original-To: help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <20020729155659.59139.qmail@web20209.mail.yahoo.com> User-Agent: Mutt/1.4i Errors-To: help-gnu-emacs-admin@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.help:734 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:734 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