aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2010-01-04 13:18:38 -0500
committerStefan Monnier2010-01-04 13:18:38 -0500
commitc57008f6ef0b368e7beb820d83ab8be09c48736b (patch)
tree2c09192eee806ade83c84a7d4ab45bedd87147ec
parente3eb1dae0dbedaa4623e248be0e3db8c9495df02 (diff)
downloademacs-c57008f6ef0b368e7beb820d83ab8be09c48736b.tar.gz
emacs-c57008f6ef0b368e7beb820d83ab8be09c48736b.zip
Avoid dubious uses of save-excursions.
* doc/lispref/positions.texi (Excursions): Recommend the use of save-current-buffer if applicable. * doc/lispref/text.texi (Clickable Text): Fix the example code which used save-excursion in a naive way which sometimes preserves point and sometimes not. * doc/lispref/variables.texi (Creating Buffer-Local): * doc/lispref/os.texi (Session Management): * doc/lispref/display.texi (GIF Images): * doc/lispref/control.texi (Cleanups): Use (save|with)-current-buffer. * doc/misc/gnus.texi (Posting Styles): Use with-current-buffer. * doc/misc/calc.texi (Defining Simple Commands): Prefer save-current-buffer.
-rw-r--r--doc/lispref/ChangeLog15
-rw-r--r--doc/lispref/control.texi7
-rw-r--r--doc/lispref/display.texi3
-rw-r--r--doc/lispref/os.texi2
-rw-r--r--doc/lispref/positions.texi12
-rw-r--r--doc/lispref/text.texi13
-rw-r--r--doc/lispref/variables.texi3
-rw-r--r--doc/misc/ChangeLog7
-rw-r--r--doc/misc/calc.texi2
-rw-r--r--doc/misc/gnus.texi5
10 files changed, 42 insertions, 27 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index e7467c20d25..6dfc203f638 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,16 @@
12010-01-04 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 Avoid dubious uses of save-excursions.
4 * positions.texi (Excursions): Recommend the use of
5 save-current-buffer if applicable.
6 * text.texi (Clickable Text): Fix the example code which used
7 save-excursion in a naive way which sometimes preserves point and
8 sometimes not.
9 * variables.texi (Creating Buffer-Local):
10 * os.texi (Session Management):
11 * display.texi (GIF Images):
12 * control.texi (Cleanups): Use (save|with)-current-buffer.
13
12010-01-02 Eli Zaretskii <eliz@gnu.org> 142010-01-02 Eli Zaretskii <eliz@gnu.org>
2 15
3 * modes.texi (Example Major Modes): Fix indentation. (Bug#5195) 16 * modes.texi (Example Major Modes): Fix indentation. (Bug#5195)
@@ -8375,7 +8388,7 @@
8375;; End: 8388;; End:
8376 8389
8377 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 8390 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
8378 2007, 2008, 2009 Free Software Foundation, Inc. 8391 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
8379 8392
8380 This file is part of GNU Emacs. 8393 This file is part of GNU Emacs.
8381 8394
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi
index 41f844b4e21..6d7c01d354b 100644
--- a/doc/lispref/control.texi
+++ b/doc/lispref/control.texi
@@ -1255,9 +1255,8 @@ make sure to kill it before finishing:
1255 1255
1256@smallexample 1256@smallexample
1257@group 1257@group
1258(save-excursion 1258(let ((buffer (get-buffer-create " *temp*")))
1259 (let ((buffer (get-buffer-create " *temp*"))) 1259 (with-current-buffer buffer
1260 (set-buffer buffer)
1261 (unwind-protect 1260 (unwind-protect
1262 @var{body-form} 1261 @var{body-form}
1263 (kill-buffer buffer)))) 1262 (kill-buffer buffer))))
@@ -1269,7 +1268,7 @@ You might think that we could just as well write @code{(kill-buffer
1269(current-buffer))} and dispense with the variable @code{buffer}. 1268(current-buffer))} and dispense with the variable @code{buffer}.
1270However, the way shown above is safer, if @var{body-form} happens to 1269However, the way shown above is safer, if @var{body-form} happens to
1271get an error after switching to a different buffer! (Alternatively, 1270get an error after switching to a different buffer! (Alternatively,
1272you could write another @code{save-excursion} around @var{body-form}, 1271you could write a @code{save-current-buffer} around @var{body-form},
1273to ensure that the temporary buffer becomes current again in time to 1272to ensure that the temporary buffer becomes current again in time to
1274kill it.) 1273kill it.)
1275 1274
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index 8feb8ea8c81..512d7d53019 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -4394,8 +4394,7 @@ every 0.1 seconds.
4394 (when (= idx max) 4394 (when (= idx max)
4395 (setq idx 0)) 4395 (setq idx 0))
4396 (let ((img (create-image file nil :image idx))) 4396 (let ((img (create-image file nil :image idx)))
4397 (save-excursion 4397 (with-current-buffer buffer
4398 (set-buffer buffer)
4399 (goto-char (point-min)) 4398 (goto-char (point-min))
4400 (unless first-time (delete-char 1)) 4399 (unless first-time (delete-char 1))
4401 (insert-image img)) 4400 (insert-image img))
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index ded70f4927b..8d62ab87499 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -2182,7 +2182,7 @@ Emacs is restarted by the session manager.
2182 2182
2183@group 2183@group
2184(defun save-yourself-test () 2184(defun save-yourself-test ()
2185 (insert "(save-excursion 2185 (insert "(save-current-buffer
2186 (switch-to-buffer \"*scratch*\") 2186 (switch-to-buffer \"*scratch*\")
2187 (insert \"I am restored\"))") 2187 (insert \"I am restored\"))")
2188 nil) 2188 nil)
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi
index deded596f81..3897efc6f2b 100644
--- a/doc/lispref/positions.texi
+++ b/doc/lispref/positions.texi
@@ -806,7 +806,9 @@ after the completion of the excursion.
806 806
807 The forms for saving and restoring the configuration of windows are 807 The forms for saving and restoring the configuration of windows are
808described elsewhere (see @ref{Window Configurations}, and @pxref{Frame 808described elsewhere (see @ref{Window Configurations}, and @pxref{Frame
809Configurations}). 809Configurations}). When only the identity of the current buffer needs
810to be saved and restored, it is preferable to use
811@code{save-current-buffer} instead.
810 812
811@defspec save-excursion body@dots{} 813@defspec save-excursion body@dots{}
812@cindex mark excursion 814@cindex mark excursion
@@ -817,10 +819,10 @@ buffer and the values of point and the mark in it, evaluates
817point and the mark. All three saved values are restored even in case of 819point and the mark. All three saved values are restored even in case of
818an abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}). 820an abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}).
819 821
820The @code{save-excursion} special form is the standard way to switch 822The @code{save-excursion} special form is the standard way to move
821buffers or move point within one part of a program and avoid affecting 823point within one part of a program and avoid affecting the rest of the
822the rest of the program. It is used more than 4000 times in the Lisp 824program. It is used more than 4000 times in the Lisp sources
823sources of Emacs. 825of Emacs.
824 826
825@code{save-excursion} does not save the values of point and the mark for 827@code{save-excursion} does not save the values of point and the mark for
826other buffers, so changes in other buffers remain in effect after 828other buffers, so changes in other buffers remain in effect after
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index 91b65017754..7c3f91c3fa8 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -3524,13 +3524,12 @@ following command:
3524(defun dired-mouse-find-file-other-window (event) 3524(defun dired-mouse-find-file-other-window (event)
3525 "In Dired, visit the file or directory name you click on." 3525 "In Dired, visit the file or directory name you click on."
3526 (interactive "e") 3526 (interactive "e")
3527 (let (window pos file) 3527 (let ((window (posn-window (event-end event)))
3528 (save-excursion 3528 (pos (posn-point (event-end event)))
3529 (setq window (posn-window (event-end event)) 3529 file)
3530 pos (posn-point (event-end event))) 3530 (if (not (windowp window))
3531 (if (not (windowp window)) 3531 (error "No file chosen"))
3532 (error "No file chosen")) 3532 (with-current-buffer (window-buffer window)
3533 (set-buffer (window-buffer window))
3534 (goto-char pos) 3533 (goto-char pos)
3535 (setq file (dired-get-file-for-visit))) 3534 (setq file (dired-get-file-for-visit)))
3536 (if (file-directory-p file) 3535 (if (file-directory-p file)
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index 4f9f9c17369..d8ab347eebf 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -1240,8 +1240,7 @@ foo
1240 1240
1241@group 1241@group
1242;; @r{In buffer @samp{b2}, the value hasn't changed.} 1242;; @r{In buffer @samp{b2}, the value hasn't changed.}
1243(save-excursion 1243(with-current-buffer "b2"
1244 (set-buffer "b2")
1245 foo) 1244 foo)
1246 @result{} 5 1245 @result{} 5
1247@end group 1246@end group
diff --git a/doc/misc/ChangeLog b/doc/misc/ChangeLog
index 27c376f764c..cabec8f7fb1 100644
--- a/doc/misc/ChangeLog
+++ b/doc/misc/ChangeLog
@@ -1,3 +1,8 @@
12010-01-04 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * gnus.texi (Posting Styles): Use with-current-buffer.
4 * calc.texi (Defining Simple Commands): Prefer save-current-buffer.
5
12010-01-02 Kevin Ryde <user42@zip.com.au> 62010-01-02 Kevin Ryde <user42@zip.com.au>
2 7
3 * eieio.texi (Naming Conventions): Correction to xref on elisp 8 * eieio.texi (Naming Conventions): Correction to xref on elisp
@@ -6512,7 +6517,7 @@
6512;; End: 6517;; End:
6513 6518
6514 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, 6519 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
6515 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 6520 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6516 6521
6517 This file is part of GNU Emacs. 6522 This file is part of GNU Emacs.
6518 6523
diff --git a/doc/misc/calc.texi b/doc/misc/calc.texi
index c88bb3e9ab8..e7c03197704 100644
--- a/doc/misc/calc.texi
+++ b/doc/misc/calc.texi
@@ -31968,7 +31968,7 @@ the function with code that looks roughly like this:
31968@smallexample 31968@smallexample
31969(let ((calc-command-flags nil)) 31969(let ((calc-command-flags nil))
31970 (unwind-protect 31970 (unwind-protect
31971 (save-excursion 31971 (save-current-buffer
31972 (calc-select-buffer) 31972 (calc-select-buffer)
31973 @emph{body of function} 31973 @emph{body of function}
31974 @emph{renumber stack} 31974 @emph{renumber stack}
diff --git a/doc/misc/gnus.texi b/doc/misc/gnus.texi
index e90789d2494..a33a91ba6f1 100644
--- a/doc/misc/gnus.texi
+++ b/doc/misc/gnus.texi
@@ -10,7 +10,7 @@
10 10
11@copying 11@copying
12Copyright @copyright{} 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 12Copyright @copyright{} 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
132003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 132003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
14 14
15@quotation 15@quotation
16Permission is granted to copy, distribute and/or modify this document 16Permission is granted to copy, distribute and/or modify this document
@@ -13449,8 +13449,7 @@ So here's a new example:
13449 (body "You are fired.\n\nSincerely, your boss.") 13449 (body "You are fired.\n\nSincerely, your boss.")
13450 (organization "Important Work, Inc")) 13450 (organization "Important Work, Inc"))
13451 ("nnml:.*" 13451 ("nnml:.*"
13452 (From (save-excursion 13452 (From (with-current-buffer gnus-article-buffer
13453 (set-buffer gnus-article-buffer)
13454 (message-fetch-field "to")))) 13453 (message-fetch-field "to"))))
13455 ("^nn.+:" 13454 ("^nn.+:"
13456 (signature-file "~/.mail-signature")))) 13455 (signature-file "~/.mail-signature"))))