aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/lispref/ChangeLog8
-rw-r--r--doc/lispref/buffers.texi159
-rw-r--r--doc/lispref/positions.texi99
3 files changed, 143 insertions, 123 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index 90a1d24ce04..26bb5888df5 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,11 @@
12011-03-19 Chong Yidong <cyd@stupidchicken.com>
2
3 * positions.texi (Excursions): Explain the "save-excursion
4 defeated by set-buffer" warning.
5
6 * buffers.texi (Current Buffer): Copyedits. Don't recommend using
7 save-excursion. Suggested by Uday S Reddy.
8
12011-03-16 Stefan Monnier <monnier@iro.umontreal.ca> 92011-03-16 Stefan Monnier <monnier@iro.umontreal.ca>
2 10
3 * strings.texi (String Conversion): Don't mention 11 * strings.texi (String Conversion): Don't mention
diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index f097ef4f25a..98fb748cd36 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -85,43 +85,63 @@ This function returns @code{t} if @var{object} is a buffer,
85@cindex changing to another buffer 85@cindex changing to another buffer
86@cindex current buffer 86@cindex current buffer
87 87
88 There are, in general, many buffers in an Emacs session. At any time, 88 There are, in general, many buffers in an Emacs session. At any
89one of them is designated as the @dfn{current buffer}. This is the 89time, one of them is designated the @dfn{current buffer}---the buffer
90buffer in which most editing takes place, because most of the primitives 90in which most editing takes place. Most of the primitives for
91for examining or changing text in a buffer operate implicitly on the 91examining or changing text operate implicitly on the current buffer
92current buffer (@pxref{Text}). Normally the buffer that is displayed on 92(@pxref{Text}).
93the screen in the selected window is the current buffer, but this is not 93
94always so: a Lisp program can temporarily designate any buffer as 94 Normally, the buffer displayed in the selected window is the current
95current in order to operate on its contents, without changing what is 95buffer, but this is not always so: a Lisp program can temporarily
96displayed on the screen. 96designate any buffer as current in order to operate on its contents,
97 97without changing what is displayed on the screen. The most basic
98 The way to designate a current buffer in a Lisp program is by calling 98function for designating a current buffer is @code{set-buffer}.
99@code{set-buffer}. The specified buffer remains current until a new one 99
100is designated. 100@defun current-buffer
101 101This function returns the current buffer.
102 When an editing command returns to the editor command loop, the 102
103command loop designates the buffer displayed in the selected window as 103@example
104current, to prevent confusion: the buffer that the cursor is in when 104@group
105Emacs reads a command is the buffer that the command will apply to. 105(current-buffer)
106(@xref{Command Loop}.) Therefore, @code{set-buffer} is not the way to 106 @result{} #<buffer buffers.texi>
107switch visibly to a different buffer so that the user can edit it. For 107@end group
108that, you must use the functions described in @ref{Displaying Buffers}. 108@end example
109 109@end defun
110 @strong{Warning:} Lisp functions that change to a different current buffer 110
111should not depend on the command loop to set it back afterwards. 111@defun set-buffer buffer-or-name
112Editing commands written in Emacs Lisp can be called from other programs 112This function makes @var{buffer-or-name} the current buffer.
113as well as from the command loop; it is convenient for the caller if 113@var{buffer-or-name} must be an existing buffer or the name of an
114the subroutine does not change which buffer is current (unless, of 114existing buffer. The return value is the buffer made current.
115course, that is the subroutine's purpose). Therefore, you should 115
116normally use @code{set-buffer} within a @code{save-current-buffer} or 116This function does not display the buffer in any window, so the user
117@code{save-excursion} (@pxref{Excursions}) form that will restore the 117cannot necessarily see the buffer. But Lisp programs will now operate
118current buffer when your function is done. Here, as an example, is a 118on it.
119@end defun
120
121 When an editing command returns to the editor command loop, Emacs
122automatically calls @code{set-buffer} on the buffer shown in the
123selected window. This is to prevent confusion: it ensures that the
124buffer that the cursor is in, when Emacs reads a command, is the
125buffer to which that command applies (@pxref{Command Loop}). Thus,
126you should not use @code{set-buffer} to switch visibly to a different
127buffer; for that, use the functions described in @ref{Displaying
128Buffers}.
129
130 When writing a Lisp function, do @emph{not} rely on this behavior of
131the command loop to restore the current buffer after an operation.
132Editing commands can also be called as Lisp functions by other
133programs, not just from the command loop; it is convenient for the
134caller if the subroutine does not change which buffer is current
135(unless, of course, that is the subroutine's purpose).
136
137 To operate temporarily on another buffer, put the @code{set-buffer}
138within a @code{save-current-buffer} form. Here, as an example, is a
119simplified version of the command @code{append-to-buffer}: 139simplified version of the command @code{append-to-buffer}:
120 140
121@example 141@example
122@group 142@group
123(defun append-to-buffer (buffer start end) 143(defun append-to-buffer (buffer start end)
124 "Append to specified buffer the text of the region." 144 "Append the text of the region to BUFFER."
125 (interactive "BAppend to buffer: \nr") 145 (interactive "BAppend to buffer: \nr")
126 (let ((oldbuf (current-buffer))) 146 (let ((oldbuf (current-buffer)))
127 (save-current-buffer 147 (save-current-buffer
@@ -131,27 +151,36 @@ simplified version of the command @code{append-to-buffer}:
131@end example 151@end example
132 152
133@noindent 153@noindent
134This function binds a local variable to record the current buffer, and 154Here, we bind a local variable to record the current buffer, and then
135then @code{save-current-buffer} arranges to make it current again. 155@code{save-current-buffer} arranges to make it current again later.
136Next, @code{set-buffer} makes the specified buffer current. Finally, 156Next, @code{set-buffer} makes the specified buffer current, and
137@code{insert-buffer-substring} copies the string from the original 157@code{insert-buffer-substring} copies the string from the original
138current buffer to the specified (and now current) buffer. 158buffer to the specified (and now current) buffer.
139 159
140 If the buffer appended to happens to be displayed in some window, 160 Alternatively, we can use the @code{with-current-buffer} macro:
141the next redisplay will show how its text has changed. Otherwise, you 161
142will not see the change immediately on the screen. The buffer becomes 162@example
143current temporarily during the execution of the command, but this does 163@group
144not cause it to be displayed. 164(defun append-to-buffer (buffer start end)
145 165 "Append the text of the region to BUFFER."
146 If you make local bindings (with @code{let} or function arguments) for 166 (interactive "BAppend to buffer: \nr")
147a variable that may also have buffer-local bindings, make sure that the 167 (let ((oldbuf (current-buffer)))
148same buffer is current at the beginning and at the end of the local 168 (with-current-buffer (get-buffer-create buffer)
149binding's scope. Otherwise you might bind it in one buffer and unbind 169 (insert-buffer-substring oldbuf start end))))
150it in another! There are two ways to do this. In simple cases, you may 170@end group
151see that nothing ever changes the current buffer within the scope of the 171@end example
152binding. Otherwise, use @code{save-current-buffer} or 172
153@code{save-excursion} to make sure that the buffer current at the 173 In either case, if the buffer appended to happens to be displayed in
154beginning is current again whenever the variable is unbound. 174some window, the next redisplay will show how its text has changed.
175If it is not displayed in any window, you will not see the change
176immediately on the screen. The command causes the buffer to become
177current temporarily, but does not cause it to be displayed.
178
179 If you make local bindings (with @code{let} or function arguments)
180for a variable that may also have buffer-local bindings, make sure
181that the same buffer is current at the beginning and at the end of the
182local binding's scope. Otherwise you might bind it in one buffer and
183unbind it in another!
155 184
156 Do not rely on using @code{set-buffer} to change the current buffer 185 Do not rely on using @code{set-buffer} to change the current buffer
157back, because that won't do the job if a quit happens while the wrong 186back, because that won't do the job if a quit happens while the wrong
@@ -168,29 +197,9 @@ have been wrong to do this:
168@end example 197@end example
169 198
170@noindent 199@noindent
171Using @code{save-current-buffer}, as we did, handles quitting, errors, 200Using @code{save-current-buffer} or @code{with-current-buffer}, as we
172and @code{throw}, as well as ordinary evaluation. 201did, correctly handles quitting, errors, and @code{throw}, as well as
173 202ordinary evaluation.
174@defun current-buffer
175This function returns the current buffer.
176
177@example
178@group
179(current-buffer)
180 @result{} #<buffer buffers.texi>
181@end group
182@end example
183@end defun
184
185@defun set-buffer buffer-or-name
186This function makes @var{buffer-or-name} the current buffer.
187@var{buffer-or-name} must be an existing buffer or the name of an
188existing buffer. The return value is the buffer made current.
189
190This function does not display the buffer in any window, so the user
191cannot necessarily see the buffer. But Lisp programs will now operate
192on it.
193@end defun
194 203
195@defspec save-current-buffer body@dots{} 204@defspec save-current-buffer body@dots{}
196The @code{save-current-buffer} special form saves the identity of the 205The @code{save-current-buffer} special form saves the identity of the
diff --git a/doc/lispref/positions.texi b/doc/lispref/positions.texi
index 0b5dcd44dd8..92846bf6d56 100644
--- a/doc/lispref/positions.texi
+++ b/doc/lispref/positions.texi
@@ -798,69 +798,72 @@ is zero or less.
798@cindex excursion 798@cindex excursion
799 799
800 It is often useful to move point ``temporarily'' within a localized 800 It is often useful to move point ``temporarily'' within a localized
801portion of the program, or to switch buffers temporarily. This is 801portion of the program. This is called an @dfn{excursion}, and it is
802called an @dfn{excursion}, and it is done with the @code{save-excursion} 802done with the @code{save-excursion} special form. This construct
803special form. This construct initially remembers the identity of the 803remembers the initial identity of the current buffer, and its values
804current buffer, and its values of point and the mark, and restores them 804of point and the mark, and restores them after the excursion
805after the completion of the excursion. 805completes. It is the standard way to move point within one part of a
806 806program and avoid affecting the rest of the program, and is used
807 The forms for saving and restoring the configuration of windows are 807thousands of times in the Lisp sources of Emacs.
808described elsewhere (see @ref{Window Configurations}, and @pxref{Frame 808
809Configurations}). When only the identity of the current buffer needs 809 If you only need to save and restore the identity of the current
810to be saved and restored, it is preferable to use 810buffer, use @code{save-current-buffer} or @code{with-current-buffer}
811@code{save-current-buffer} instead. 811instead (@pxref{Current Buffer}). If you need to save or restore
812window configurations, see the forms described in @ref{Window
813Configurations} and in @ref{Frame Configurations}.
812 814
813@defspec save-excursion body@dots{} 815@defspec save-excursion body@dots{}
814@cindex mark excursion 816@cindex mark excursion
815@cindex point excursion 817@cindex point excursion
816The @code{save-excursion} special form saves the identity of the current 818This special form saves the identity of the current buffer and the
817buffer and the values of point and the mark in it, evaluates 819values of point and the mark in it, evaluates @var{body}, and finally
818@var{body}, and finally restores the buffer and its saved values of 820restores the buffer and its saved values of point and the mark. All
819point and the mark. All three saved values are restored even in case of 821three saved values are restored even in case of an abnormal exit via
820an abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}). 822@code{throw} or error (@pxref{Nonlocal Exits}).
821 823
822The @code{save-excursion} special form is the standard way to move 824The value returned by @code{save-excursion} is the result of the last
823point within one part of a program and avoid affecting the rest of the 825form in @var{body}, or @code{nil} if no body forms were given.
824program. It is used more than 4000 times in the Lisp sources 826@end defspec
825of Emacs.
826 827
827@code{save-excursion} does not save the values of point and the mark for 828 Because @code{save-excursion} only saves point and mark for the
828other buffers, so changes in other buffers remain in effect after 829buffer that was current at the start of the excursion, any changes
829@code{save-excursion} exits. 830made to point and/or mark in other buffers, during the excursion, will
831remain in effect afterward. This frequently leads to unintended
832consequences, so the byte compiler warns if you call @code{set-buffer}
833during an excursion:
830 834
831@cindex window excursions 835@example
832Likewise, @code{save-excursion} does not restore window-buffer 836Warning: @code{save-excursion} defeated by @code{set-buffer}
833correspondences altered by functions such as @code{switch-to-buffer}. 837@end example
834One way to restore these correspondences, and the selected window, is to
835use @code{save-window-excursion} inside @code{save-excursion}
836(@pxref{Window Configurations}).
837 838
838The value returned by @code{save-excursion} is the result of the last 839@noindent
839form in @var{body}, or @code{nil} if no body forms were given. 840To avoid such problems, you should call @code{save-excursion} only
841after setting the desired current buffer, as in the following example:
840 842
841@example 843@example
842@group 844@group
843(save-excursion @var{forms}) 845(defun append-string-to-buffer (string buffer)
844@equiv{} 846 "Append STRING to the end of BUFFER."
845(let ((old-buf (current-buffer)) 847 (with-current-buffer buffer
846 (old-pnt (point-marker)) 848 (save-excursion
847@end group 849 (goto-char (point-max))
848 (old-mark (copy-marker (mark-marker)))) 850 (insert string))))
849 (unwind-protect
850 (progn @var{forms})
851 (set-buffer old-buf)
852@group
853 (goto-char old-pnt)
854 (set-marker (mark-marker) old-mark)))
855@end group 851@end group
856@end example 852@end example
857@end defspec 853
854@cindex window excursions
855 Likewise, @code{save-excursion} does not restore window-buffer
856correspondences altered by functions such as @code{switch-to-buffer}.
857One way to restore these correspondences, and the selected window, is to
858use @code{save-window-excursion} inside @code{save-excursion}
859(@pxref{Window Configurations}).
858 860
859 @strong{Warning:} Ordinary insertion of text adjacent to the saved 861 @strong{Warning:} Ordinary insertion of text adjacent to the saved
860point value relocates the saved value, just as it relocates all markers. 862point value relocates the saved value, just as it relocates all
861More precisely, the saved value is a marker with insertion type 863markers. More precisely, the saved value is a marker with insertion
862@code{nil}. @xref{Marker Insertion Types}. Therefore, when the saved 864type @code{nil}. @xref{Marker Insertion Types}. Therefore, when the
863point value is restored, it normally comes before the inserted text. 865saved point value is restored, it normally comes before the inserted
866text.
864 867
865 Although @code{save-excursion} saves the location of the mark, it does 868 Although @code{save-excursion} saves the location of the mark, it does
866not prevent functions which modify the buffer from setting 869not prevent functions which modify the buffer from setting