From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: `save-excursion' defeated by `set-buffer' Date: Fri, 11 Mar 2011 10:52:14 -0500 Organization: A noiseless patient Spider Message-ID: References: <4D792D16.1080900@easy-emacs.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1299883249 5715 80.91.229.12 (11 Mar 2011 22:40:49 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 11 Mar 2011 22:40:49 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Mar 11 23:40:45 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 1PyB0k-00035R-MM for geh-help-gnu-emacs@m.gmane.org; Fri, 11 Mar 2011 23:40:42 +0100 Original-Received: from localhost ([127.0.0.1]:53621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PyB0k-0001x9-0v for geh-help-gnu-emacs@m.gmane.org; Fri, 11 Mar 2011 17:40:42 -0500 Original-Path: usenet.stanford.edu!news-transit.tcx.org.uk!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 28 Injection-Info: mx03.eternal-september.org; posting-host="p1HHT3UJiEXQirt0aLXAiQ"; logging-data="21197"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SsUoSTyDbxmKhWhbwoIo/" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:XfvRFbrogBMN7dXJRbY2ZLabYqg= sha1:glkpiB6hubQVVrGmizmU5pKHGGo= Original-Xref: usenet.stanford.edu gnu.emacs.help:185860 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:80008 Archived-At: > is there an example, where save-excursion will fail, ie not restore the > buffer due to a set-buffer afterwards? `save-excursion' is not the form to use to save the current buffer: that's what save-current-buffer is for. What `save-excursion' does is preserve point (and mark), and some people tend to think of it as "undoes all point movement", which is sadly only true for the current buffer but not all buffers. So (save-excursion (goto-char BAR)) is pretty much a no-op. But (save-excursion (set-buffer FOO) (goto-char BAR)) is either: - the same as (save-excursion (goto-char BAR)), in case FOO is already current. - an inefficient form of (with-current-buffer FOO (goto-char BAR)). I still haven't found any code out there where this behavior is what is actually wanted and expected by the programmer. In more than 90% of the cases, the intended meaning is (with-current-buffer FOO (goto-char BAR)) and the behavior if FOO is current (to additionally preserve point) is harmless, so the warning simply points out an inefficiency. In the remaining cases, FOO is almost always current, but when it's not the resulting behavior is a bug. I.e. the intended code is (with-current-buffer FOO (save-excursion (goto-char BAR))). Stefan