From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tim X Newsgroups: gmane.emacs.help Subject: Re: (save-excursion (other-window 1)) leaves me in the other window Date: Mon, 08 Mar 2010 18:26:22 +1100 Organization: Unlimited download news at news.astraweb.com Message-ID: <87mxyjs28x.fsf@rapttech.com.au> References: <87y6i4sjsn.fsf@rapttech.com.au> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1272990789 7780 80.91.229.12 (4 May 2010 16:33:09 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 4 May 2010 16:33:09 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 04 18:33:08 2010 connect(): No such file or directory Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1O9L3T-0002jY-4P for geh-help-gnu-emacs@m.gmane.org; Tue, 04 May 2010 18:33:07 +0200 Original-Received: from localhost ([127.0.0.1]:42384 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O9L3S-0006AT-Dh for geh-help-gnu-emacs@m.gmane.org; Tue, 04 May 2010 12:33:06 -0400 Original-Path: usenet.stanford.edu!news.kjsl.com!news.alt.net!news.astraweb.com!border1.newsrouter.astraweb.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.93 (gnu/linux) Cancel-Lock: sha1:yGWONrufbVQW6Z1XL+cKSn9qnFc= Original-Lines: 98 Original-NNTP-Posting-Host: d6a51597.news.astraweb.com Original-X-Trace: DXC=D>Va:=KH:DWD:=ciL5QPHRL?0kYOcDh@Z3E5>gaGjiF^k^lO5Gk8OVZaCb3`j`aPhYe_A3@kGf@?Y Original-Xref: usenet.stanford.edu gnu.emacs.help:177401 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:72933 Archived-At: Sean McAfee writes: > Tim X writes: > >> Sean McAfee writes: >>> A native reimplementation of scroll-other-window doesn't work as I'd >>> expect: >>> >>> (save-excursion >>> (other-window 1) >>> (scroll-up)) >>> >>> The problem is that the current window isn't restored, which surprised >>> me considerably. Why doesn't this work, and how would I write a >>> function to go do some stuff in the other window and then come back? >> >> As emacs already has the command to scroll the other window, I'm >> assuming your example is a simplification of what you really want to do. > > True. The situation is this: > > I have a frame, split horizontally into two windows. One window shows > text that came from an OCR process; the other window, in image-mode, > shows the (large) image that was the input to that OCR process. What I > want to do is work in the text window, shifting the image in the other > window around as I check it against the text. I assumed I could do > something like this: > > (defmacro in-other-window (&rest body) > `(save-excursion (other-window 1) ,@body)) > > And then: > > (global-set-key [(shift down)] > (lambda () (interactive) (in-other-window (image-next-line)))) > > ...and similarly for the other three directions. > > Although the documentation for save-excursion says that it saves and > restores the current buffer, it doesn't in this case. I still don't > really know why. I tried using save-window-excursion instead as Joe > Fineman suggested, but while that worked for image-next-line and > image-previous-line, it doesn't for image-forward-hscroll, which I need > for scrolling horizontally. I guess the horizontal scroll amount is > something that's saved and restored by save-window-excursion. So I > finally settled on this: Note that the docs as you noted say that they restore the buffer, not the window. This was part of the point I was tyring to make. Buffers and windows are not the same thing. For example, you might use save-excursion in a command that needs to jump to another point in the buffer, perform some calculation, maybe put the results in a temp buffer or variable and then return to where things were at before the command executed. Essentially, your saving state of the buffer (not the window!) going off and doing something else and then once done, returning to where you were. In many cases, the user won't even realise this has occured. If on the other hand, you wanted to do something like display another window with some data, maybe a status message and then after the user hits a particular key, restore the window as it was, then save-window-excursion is probably what you want. In your current situation, you want to switch to the window displaying the buffer with the image in it, scroll it up/down or left/right and then return to where you were in the text buffer? I think you could either just use an unwind-protect or a save-excursion, but I'm a little confused regarding what the issue is with restoration. Strongly suggest you have a look at the sources to simple.el as it contains some examples of convenience commands related to scrolling. I also did an apropos-command with the search term 'scroll' and found the following which you might find useful - image-backward-hscroll M-x ... RET Scroll image in current window to the right by N character widths. image-forward-hscroll M-x ... RET Scroll image in current window to the left by N character widths. image-scroll-down M-x ... RET Scroll image in current window downward by N lines. image-scroll-up M-x ... RET Scroll image in current window upward by N lines. It would be fairly trivial to write two commands that would scroll the image up/down or left/right. It could take a prefix argument to determine the distance with positive arguments meaning donw/right and negative meaning up/left etc. I don't see there is any need to make it a macro unless you want to be able to execute arbitrarily complex forms. Maybe just write functions and later do a macro if justified. Tim -- tcross (at) rapttech dot com dot au