From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: `save-excursion' defeated by `set-buffer' Date: Tue, 15 Mar 2011 13:55:37 -0700 Message-ID: <5820D67316764FEBB3FFC175B4CBBCA1@us.oracle.com> References: <4D792D16.1080900@easy-emacs.de><87pqpwr8ay.fsf@fencepost.gnu.org><87d3lwr56j.fsf@fencepost.gnu.org><4d7b8ea3$0$23760$14726298@news.sunsite.dk><87k4g4pdi3.fsf@fencepost.gnu.org><4d7c2ea3$0$23757$14726298@news.sunsite.dk> <87tyf4i4vc.fsf@notengoamigos.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1300222608 28494 80.91.229.12 (15 Mar 2011 20:56:48 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 15 Mar 2011 20:56:48 +0000 (UTC) To: "'Jason Earl'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 15 21:56:43 2011 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 1PzbIJ-0001Ac-AG for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Mar 2011 21:56:43 +0100 Original-Received: from localhost ([127.0.0.1]:46188 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PzbII-0006W4-Q4 for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Mar 2011 16:56:42 -0400 Original-Received: from [140.186.70.92] (port=41154 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PzbHO-0006U6-Vd for help-gnu-emacs@gnu.org; Tue, 15 Mar 2011 16:55:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PzbHM-0000NE-IB for help-gnu-emacs@gnu.org; Tue, 15 Mar 2011 16:55:46 -0400 Original-Received: from rcsinet10.oracle.com ([148.87.113.121]:21978) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PzbHM-0000Ml-D0 for help-gnu-emacs@gnu.org; Tue, 15 Mar 2011 16:55:44 -0400 Original-Received: from rcsinet15.oracle.com (rcsinet15.oracle.com [148.87.113.117]) by rcsinet10.oracle.com (Switch-3.4.2/Switch-3.4.2) with ESMTP id p2FKtfdt003076 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 15 Mar 2011 20:55:42 GMT Original-Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by rcsinet15.oracle.com (Switch-3.4.2/Switch-3.4.1) with ESMTP id p2FKte5a013676 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 15 Mar 2011 20:55:41 GMT Original-Received: from abhmt014.oracle.com (abhmt014.oracle.com [141.146.116.23]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id p2FKtbVd018591; Tue, 15 Mar 2011 15:55:38 -0500 Original-Received: from dradamslap1 (/130.35.178.194) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 15 Mar 2011 13:55:36 -0700 X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <87tyf4i4vc.fsf@notengoamigos.org> Thread-Index: AcvjSNAt3WUrmVm/SyG79wttiXGYmgAAPNWQ X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5994 X-Source-IP: acsmt357.oracle.com [141.146.40.157] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A090204.4D7FD24D.006B,ss=1,fgs=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 148.87.113.121 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:80150 Archived-At: > >> People will not naturally use `save-excursion' nowadays > >> just to save the current buffer. > > > > Maybe not naturally, no. But thanks to the pressure of years of old > > code flying around the web, they will do it nowadays on a regular > > basis. But indeed, thanks to threads like this one and to > > the warning I added, I hope I can reverse the trend. > > Precisely, these warnings are not for people like you, Drew, or David. > You guys have the experience necessary to make the correct choice. > Warnings like this are *very* helpful to people like me, however, that > have no experience with Emacs Lisp, but would like to write Elisp > anyhow. Actually, it is precisely people such as yourself who are done the greatest disservice by this warning, IMHO. > To be honest, for the purpose of teaching newbies it almost doesn't > matter what the warning says either. As long as it is clear that > save-excursion and set-buffer are not to be used together then it is a > win. Et voila the damage done. It is NOT true that "`save-excursion' and `set-buffer' are not to be used together." Even Stefan acknowledges that. But that's not the message that you (_clearly_) received. > And yes, I realize that what I should do is read the manual umpteen > million times, and memorize a pile of stuff. Actually, in this case > this might not be such a good idea. Just read the doc string of `save-excursion' and you should be good to go. It tells you all you will ever need to know about it. It tells you what `save-excursion' _does_. The warning was supposedly designed to hint at what `save-excursion' does _not_ do, e.g., to prevent you from having unreasonable expectations that it will restore more than it does. > I assumed that I had picked up the save-excursion + set-buffer idiom > while I was perusing the Gnus code, but in checking the manual I > think that I might have picked it up from the /Introduction to > Emacs Lisp/. Using save-excursion and set-buffer together is covered in: > > 4.4.3 `save-excursion' in `append-to-buffer' Yes, please file a (doc) bug: the example in that section does not reflect the latest definition of `append-to-buffer', which now uses `with-current-buffer' instead of `set-buffer'. That said, the code shown in that doc, which is just the Emacs 22 version of `append-to-buffer', is not bad code. It uses `set-buffer' instead of `with-current-buffer', AND there is no harm in it (IMHO): Emacs 22: (save-excursion (set-buffer append-to) ; <====== (setq point (point)) (barf-if-buffer-read-only) (insert-buffer-substring oldbuf start end) (dolist (window windows) (when (= (window-point window) point) (set-window-point window (point)))))) Emacs 23+: (save-excursion (with-current-buffer append-to ; <====== (setq point (point)) (barf-if-buffer-read-only) (insert-buffer-substring oldbuf start end) (dolist (window windows) (when (= (window-point window) point) (set-window-point window (point)))))) Do I prefer the latter (Emacs 23+)? Yes. Is the former dangerous or abominable? No. Does it even matter whether the BUFFER arg to `append-to-buffer' (which corresponds to APPEND-TO above) happens to be the same as the `current-buffer'? No. Did this code _really need_ to be "fixed" to use `with-current-buffer' instead of `set-buffer'? No. No danger, but it's still better with `with-current-buffer'.