From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Stephen J. Turnbull" Newsgroups: gmane.emacs.devel Subject: Re: save-excursion again Date: Sat, 19 Jun 2010 23:50:05 +0900 Message-ID: <87pqzncc0y.fsf@uwakimon.sk.tsukuba.ac.jp> References: <19483.43525.253000.115910@gargle.gargle.HOWL> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1276959204 17116 80.91.229.12 (19 Jun 2010 14:53:24 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 19 Jun 2010 14:53:24 +0000 (UTC) Cc: Stefan Monnier , emacs-devel@gnu.org To: Uday S Reddy Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jun 19 16:53:23 2010 connect(): No such file or directory Return-path: Envelope-to: ged-emacs-devel@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 1OPzQ6-0005rO-Oi for ged-emacs-devel@m.gmane.org; Sat, 19 Jun 2010 16:53:19 +0200 Original-Received: from localhost ([127.0.0.1]:38561 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPzQ5-0006n7-Tn for ged-emacs-devel@m.gmane.org; Sat, 19 Jun 2010 10:53:17 -0400 Original-Received: from [140.186.70.92] (port=52281 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPzPz-0006mr-P3 for emacs-devel@gnu.org; Sat, 19 Jun 2010 10:53:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OPzPy-0006Ys-IL for emacs-devel@gnu.org; Sat, 19 Jun 2010 10:53:11 -0400 Original-Received: from mtps01.sk.tsukuba.ac.jp ([130.158.97.223]:55500) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OPzPy-0006YA-2f for emacs-devel@gnu.org; Sat, 19 Jun 2010 10:53:10 -0400 Original-Received: from uwakimon.sk.tsukuba.ac.jp (uwakimon.sk.tsukuba.ac.jp [130.158.99.156]) by mtps01.sk.tsukuba.ac.jp (Postfix) with ESMTP id A44D31537B2; Sat, 19 Jun 2010 23:53:00 +0900 (JST) Original-Received: by uwakimon.sk.tsukuba.ac.jp (Postfix, from userid 1000) id 4E2701A3E22; Sat, 19 Jun 2010 23:50:05 +0900 (JST) In-Reply-To: <19483.43525.253000.115910@gargle.gargle.HOWL> X-Mailer: VM 8.0.12-devo-585 under 21.5 (beta29) "garbanzo" a03421eb562b XEmacs Lucid (x86_64-unknown-linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:126223 Archived-At: Uday S Reddy writes: > When I first got involved with VM, there were a host of problems that > were called "jumping cursor" issues. You do something and the cursor > ends up at a different place. The point is that if ;; current buffer is "bar" (save-excursion (set-buffer "foo") (frob-current-buffer)) protects "bar" from jumping cursor, then `frob-current-buffer' must switch back to "bar" and move the cursor at some point. If your code is doing that kind of thing, then you end up with > In fact, I would have rather liked a compiler that asked, do you > really want to do set-buffer without a save-excursion first? But that's *not good enough*. Suppose a new feature requires this code: ;; current buffer is "bar" (frob-current-buffer) Now you're going to see jumping cursor without a set-buffer to warn you. The obvious try is ;; current buffer is "bar" (save-excursion (frob-current-buffer)) but since `frob-current-buffer' is used in situations where jumping cursor is bad, why not (fset 'frob-current-buffer-internal (symbol-function 'frob-current-buffer)) (defun frob-current-buffer (&rest args) (save-excursion (apply #'frob-current-buffer-internal args))) and save yourself future aggravation by using `frob-current-buffer' except when point motion is the intent? > That is, set-buffer without save-excursion is dangerous, because it > might lead to the "jumping cursor" problem. No. It's the code *after* the set-buffer that's dangerous, not the set-buffer. That fact that you consider set-buffer per se dangerous is a strong suggestion that your program is rotten with code that does set-buffer and then moves point without a save-excursion. > I will assume you are joking. But, why bother with any of this at > all? What is wrong with the original code in the first place? Nothing, assuming that the whole project is already using functions that have nasty side-effects of moving point in buffers they aren't called from. In that case it might be cheaper for you to wrap every set-buffer and following code in a save-excursion, but it's not 100% reliable. And it's *much* better to write new code to avoid side effects except in functions designed to produce side effects. It's true that using the set-buffer heuristic you'll probably catch *most* such issues, because a lot of the functions of the form (defun find-something (quux) (set-buffer "bar") ;; I should have put a save-excursion *here* but didn't. Oops. (if (search-forward quux nil t) (extract-info-from-line-in-bar) (error "No quux here, boss!"))) are mostly called from other buffers (in a context like VM, if you're in "bar", you're probably already on the appropriate line, so you call `extract-info-from-line-in-bar' directly, not via `find-something'). But you can't guarantee that.