From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: eval-last-sexp in other window Date: Wed, 01 Apr 2009 11:09:10 +0200 Organization: Anevia SAS Message-ID: <7c1vscpwp5.fsf@pbourguignon.anevia.com> References: <87d4bzbfky.fsf@galatea.local> <87bpri9lhd.fsf@galatea.local> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1238578894 9347 80.91.229.12 (1 Apr 2009 09:41:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 1 Apr 2009 09:41:34 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Apr 01 11:42:52 2009 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.50) id 1Lowy7-0004fq-Qh for geh-help-gnu-emacs@m.gmane.org; Wed, 01 Apr 2009 11:42:48 +0200 Original-Received: from localhost ([127.0.0.1]:56482 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Lowwk-0003IF-3F for geh-help-gnu-emacs@m.gmane.org; Wed, 01 Apr 2009 05:41:22 -0400 Original-Newsgroups: gnu.emacs.help Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:YTlkMmMwMzA4NTFhZTE0MjdkMTQyN2RlMGUzZTM2Y2M5ZWJhOTc0Ng== Original-Lines: 81 Original-NNTP-Posting-Date: 01 Apr 2009 11:09:11 MEST Original-NNTP-Posting-Host: 88.170.236.224 Original-X-Trace: 1238576951 news-2.free.fr 29005 88.170.236.224:49945 Original-X-Complaints-To: abuse@proxad.net Original-Path: news.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!feed.ac-versailles.fr!proxad.net!feeder1-2.proxad.net!cleanfeed3-a.proxad.net!nnrp11-2.free.fr!not-for-mail Original-Xref: news.stanford.edu gnu.emacs.help:168148 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:63433 Archived-At: Bob Babcock writes: > pjb@informatimago.com (Pascal J. Bourguignon) wrote in > news:87bpri9lhd.fsf@galatea.local: > >>> (defun last-sexp-other-window() >>> "Get last sexp and run it in other window. >>> If there isn't another window, use current window." >>> (interactive) >>> (setq ow-sexp (preceding-sexp)) >>> (other-window 1) (eval ow-sexp) (other-window -1) ) >> >> At the very least, use let, not setq! >> >> (defun last-sexp-other-window() >> "Get last sexp and run it in other window. >> If there isn't another window, use current window." >> (interactive) >> (let ((ow-sexp (preceding-sexp))) >> (other-window 1) >> (eval ow-sexp) >> (other-window -1))) > > I will admit that I was surprised when setq worked in this context. > Perhaps there are cases where setq would fail? For example in this case: (defun evaluate-sexps-in-other-windows () (let ((ow-sexp 0)) ; count the sexps we evaluate. (while (< (point) (max-point)) (incf ow-sexp) (forward-sexp) (last-sexp-other-window)) ow-sexp)) Remember that in emacs lisp, all the bindings are special (dynamic). When you use setq on a variable for which you don't provide a local binding, you are actually destroying the binding of the caller (or the caller of the caller...), therefore introducing bugs in unrelated code. In some occasions it might be easier to use setq/setf, but do it only on a locally rebound variable: (let ((var init-value)) (setf var (f1 var)) (loop while (condp var) do (setf var (f2 var))) (setf var (f3 var)) var) [ which, in the case of loop can also be rewritten as: (loop with var = init-value initially (setf var (f1 var)) while (condp var) do (setf var (f2 var)) finally (return (f3 var))) ]. But it may be clearer to write it as: (f3 (fix (function f2) (function condp) (f1 init-value))) with: (defun fix (fun predicate value) (if (funcall predicate value) (fix fun predicate (funcall fun value)) value)) -- __Pascal Bourguignon__