Path: utzoo!mnetor!uunet!husc6!bloom-beacon!athena.mit.edu!wesommer From: wesommer@athena.mit.edu (William E. Sommerfeld) Newsgroups: comp.emacs Subject: Re: Is there a cleaner way to check for a narrowed buffer? Message-ID: <2342@bloom-beacon.MIT.EDU> Date: 18 Jan 88 15:31:34 GMT References: <138@axcess.UUCP> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: wesommer@athena.mit.edu (William E. Sommerfeld) Organization: Massachusetts Institute of Technology Lines: 46 Keywords: GNU Emacs narrow-to-region In article <138@axcess.UUCP> allbery@axcess.UUCP (Brandon S. Allbery) writes: >I have a function which wants to narrow the current buffer to a region only if >it is not already narrowed. ... If the buffer is already narrowed, I >want the function to signal an error. > >The only way I have figured out so far to do this is the following piece of >ugliness: > (gross use of unwind-protect deleted in the interest of ) Rob Krawitz will probably also respond to this, but... The other suggestions on how to detect a current restriction makes sense, but there is there is a special form, save-restriction, which could be used to cleanly implement constructs similar to what you have above (which you're probably want to use in the yacc/bison mode): (save-restriction form1 form2 ...) is sort of equivalent to (let ((old-min (point-min)) (old-max (point-max))) (unwind-protect (progn form1 form2 ...) (widen) (narrow-to-region old-min old-max))) (the difference being that old-min and old-max won't be visible inside the execution of the internal forms). Another form related to this which is real useful is save-excursion, which saves `point' and `mark' on entry, and _always_ restores them when the form is exited. The two are sort of like gsave and grestore in postscript, except that they also interact well with exception handling. Look in rmail.el and info.el to see a lot of use of both of these forms. - Bill