diff options
| -rw-r--r-- | doc/lispref/ChangeLog | 8 | ||||
| -rw-r--r-- | doc/lispref/buffers.texi | 159 | ||||
| -rw-r--r-- | doc/lispref/positions.texi | 99 |
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 @@ | |||
| 1 | 2011-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 | |||
| 1 | 2011-03-16 Stefan Monnier <monnier@iro.umontreal.ca> | 9 | 2011-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 |
| 89 | one of them is designated as the @dfn{current buffer}. This is the | 89 | time, one of them is designated the @dfn{current buffer}---the buffer |
| 90 | buffer in which most editing takes place, because most of the primitives | 90 | in which most editing takes place. Most of the primitives for |
| 91 | for examining or changing text in a buffer operate implicitly on the | 91 | examining or changing text operate implicitly on the current buffer |
| 92 | current buffer (@pxref{Text}). Normally the buffer that is displayed on | 92 | (@pxref{Text}). |
| 93 | the screen in the selected window is the current buffer, but this is not | 93 | |
| 94 | always so: a Lisp program can temporarily designate any buffer as | 94 | Normally, the buffer displayed in the selected window is the current |
| 95 | current in order to operate on its contents, without changing what is | 95 | buffer, but this is not always so: a Lisp program can temporarily |
| 96 | displayed on the screen. | 96 | designate any buffer as current in order to operate on its contents, |
| 97 | 97 | without 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 | 98 | function for designating a current buffer is @code{set-buffer}. |
| 99 | @code{set-buffer}. The specified buffer remains current until a new one | 99 | |
| 100 | is designated. | 100 | @defun current-buffer |
| 101 | 101 | This function returns the current buffer. | |
| 102 | When an editing command returns to the editor command loop, the | 102 | |
| 103 | command loop designates the buffer displayed in the selected window as | 103 | @example |
| 104 | current, to prevent confusion: the buffer that the cursor is in when | 104 | @group |
| 105 | Emacs 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> |
| 107 | switch visibly to a different buffer so that the user can edit it. For | 107 | @end group |
| 108 | that, 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 | |
| 111 | should not depend on the command loop to set it back afterwards. | 111 | @defun set-buffer buffer-or-name |
| 112 | Editing commands written in Emacs Lisp can be called from other programs | 112 | This function makes @var{buffer-or-name} the current buffer. |
| 113 | as 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 |
| 114 | the subroutine does not change which buffer is current (unless, of | 114 | existing buffer. The return value is the buffer made current. |
| 115 | course, that is the subroutine's purpose). Therefore, you should | 115 | |
| 116 | normally use @code{set-buffer} within a @code{save-current-buffer} or | 116 | This function does not display the buffer in any window, so the user |
| 117 | @code{save-excursion} (@pxref{Excursions}) form that will restore the | 117 | cannot necessarily see the buffer. But Lisp programs will now operate |
| 118 | current buffer when your function is done. Here, as an example, is a | 118 | on it. |
| 119 | @end defun | ||
| 120 | |||
| 121 | When an editing command returns to the editor command loop, Emacs | ||
| 122 | automatically calls @code{set-buffer} on the buffer shown in the | ||
| 123 | selected window. This is to prevent confusion: it ensures that the | ||
| 124 | buffer that the cursor is in, when Emacs reads a command, is the | ||
| 125 | buffer to which that command applies (@pxref{Command Loop}). Thus, | ||
| 126 | you should not use @code{set-buffer} to switch visibly to a different | ||
| 127 | buffer; for that, use the functions described in @ref{Displaying | ||
| 128 | Buffers}. | ||
| 129 | |||
| 130 | When writing a Lisp function, do @emph{not} rely on this behavior of | ||
| 131 | the command loop to restore the current buffer after an operation. | ||
| 132 | Editing commands can also be called as Lisp functions by other | ||
| 133 | programs, not just from the command loop; it is convenient for the | ||
| 134 | caller 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} | ||
| 138 | within a @code{save-current-buffer} form. Here, as an example, is a | ||
| 119 | simplified version of the command @code{append-to-buffer}: | 139 | simplified 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 |
| 134 | This function binds a local variable to record the current buffer, and | 154 | Here, we bind a local variable to record the current buffer, and then |
| 135 | then @code{save-current-buffer} arranges to make it current again. | 155 | @code{save-current-buffer} arranges to make it current again later. |
| 136 | Next, @code{set-buffer} makes the specified buffer current. Finally, | 156 | Next, @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 |
| 138 | current buffer to the specified (and now current) buffer. | 158 | buffer 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: |
| 141 | the next redisplay will show how its text has changed. Otherwise, you | 161 | |
| 142 | will not see the change immediately on the screen. The buffer becomes | 162 | @example |
| 143 | current temporarily during the execution of the command, but this does | 163 | @group |
| 144 | not 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") |
| 147 | a variable that may also have buffer-local bindings, make sure that the | 167 | (let ((oldbuf (current-buffer))) |
| 148 | same buffer is current at the beginning and at the end of the local | 168 | (with-current-buffer (get-buffer-create buffer) |
| 149 | binding's scope. Otherwise you might bind it in one buffer and unbind | 169 | (insert-buffer-substring oldbuf start end)))) |
| 150 | it in another! There are two ways to do this. In simple cases, you may | 170 | @end group |
| 151 | see that nothing ever changes the current buffer within the scope of the | 171 | @end example |
| 152 | binding. 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 |
| 154 | beginning is current again whenever the variable is unbound. | 174 | some window, the next redisplay will show how its text has changed. |
| 175 | If it is not displayed in any window, you will not see the change | ||
| 176 | immediately on the screen. The command causes the buffer to become | ||
| 177 | current temporarily, but does not cause it to be displayed. | ||
| 178 | |||
| 179 | If you make local bindings (with @code{let} or function arguments) | ||
| 180 | for a variable that may also have buffer-local bindings, make sure | ||
| 181 | that the same buffer is current at the beginning and at the end of the | ||
| 182 | local binding's scope. Otherwise you might bind it in one buffer and | ||
| 183 | unbind 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 |
| 157 | back, because that won't do the job if a quit happens while the wrong | 186 | back, 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 |
| 171 | Using @code{save-current-buffer}, as we did, handles quitting, errors, | 200 | Using @code{save-current-buffer} or @code{with-current-buffer}, as we |
| 172 | and @code{throw}, as well as ordinary evaluation. | 201 | did, correctly handles quitting, errors, and @code{throw}, as well as |
| 173 | 202 | ordinary evaluation. | |
| 174 | @defun current-buffer | ||
| 175 | This 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 | ||
| 186 | This function makes @var{buffer-or-name} the current buffer. | ||
| 187 | @var{buffer-or-name} must be an existing buffer or the name of an | ||
| 188 | existing buffer. The return value is the buffer made current. | ||
| 189 | |||
| 190 | This function does not display the buffer in any window, so the user | ||
| 191 | cannot necessarily see the buffer. But Lisp programs will now operate | ||
| 192 | on it. | ||
| 193 | @end defun | ||
| 194 | 203 | ||
| 195 | @defspec save-current-buffer body@dots{} | 204 | @defspec save-current-buffer body@dots{} |
| 196 | The @code{save-current-buffer} special form saves the identity of the | 205 | The @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 |
| 801 | portion of the program, or to switch buffers temporarily. This is | 801 | portion of the program. This is called an @dfn{excursion}, and it is |
| 802 | called an @dfn{excursion}, and it is done with the @code{save-excursion} | 802 | done with the @code{save-excursion} special form. This construct |
| 803 | special form. This construct initially remembers the identity of the | 803 | remembers the initial identity of the current buffer, and its values |
| 804 | current buffer, and its values of point and the mark, and restores them | 804 | of point and the mark, and restores them after the excursion |
| 805 | after the completion of the excursion. | 805 | completes. It is the standard way to move point within one part of a |
| 806 | 806 | program and avoid affecting the rest of the program, and is used | |
| 807 | The forms for saving and restoring the configuration of windows are | 807 | thousands of times in the Lisp sources of Emacs. |
| 808 | described elsewhere (see @ref{Window Configurations}, and @pxref{Frame | 808 | |
| 809 | Configurations}). When only the identity of the current buffer needs | 809 | If you only need to save and restore the identity of the current |
| 810 | to be saved and restored, it is preferable to use | 810 | buffer, use @code{save-current-buffer} or @code{with-current-buffer} |
| 811 | @code{save-current-buffer} instead. | 811 | instead (@pxref{Current Buffer}). If you need to save or restore |
| 812 | window configurations, see the forms described in @ref{Window | ||
| 813 | Configurations} 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 |
| 816 | The @code{save-excursion} special form saves the identity of the current | 818 | This special form saves the identity of the current buffer and the |
| 817 | buffer and the values of point and the mark in it, evaluates | 819 | values 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 | 820 | restores the buffer and its saved values of point and the mark. All |
| 819 | point and the mark. All three saved values are restored even in case of | 821 | three saved values are restored even in case of an abnormal exit via |
| 820 | an abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}). | 822 | @code{throw} or error (@pxref{Nonlocal Exits}). |
| 821 | 823 | ||
| 822 | The @code{save-excursion} special form is the standard way to move | 824 | The value returned by @code{save-excursion} is the result of the last |
| 823 | point within one part of a program and avoid affecting the rest of the | 825 | form in @var{body}, or @code{nil} if no body forms were given. |
| 824 | program. It is used more than 4000 times in the Lisp sources | 826 | @end defspec |
| 825 | of 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 |
| 828 | other buffers, so changes in other buffers remain in effect after | 829 | buffer that was current at the start of the excursion, any changes |
| 829 | @code{save-excursion} exits. | 830 | made to point and/or mark in other buffers, during the excursion, will |
| 831 | remain in effect afterward. This frequently leads to unintended | ||
| 832 | consequences, so the byte compiler warns if you call @code{set-buffer} | ||
| 833 | during an excursion: | ||
| 830 | 834 | ||
| 831 | @cindex window excursions | 835 | @example |
| 832 | Likewise, @code{save-excursion} does not restore window-buffer | 836 | Warning: @code{save-excursion} defeated by @code{set-buffer} |
| 833 | correspondences altered by functions such as @code{switch-to-buffer}. | 837 | @end example |
| 834 | One way to restore these correspondences, and the selected window, is to | ||
| 835 | use @code{save-window-excursion} inside @code{save-excursion} | ||
| 836 | (@pxref{Window Configurations}). | ||
| 837 | 838 | ||
| 838 | The value returned by @code{save-excursion} is the result of the last | 839 | @noindent |
| 839 | form in @var{body}, or @code{nil} if no body forms were given. | 840 | To avoid such problems, you should call @code{save-excursion} only |
| 841 | after 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 | ||
| 856 | correspondences altered by functions such as @code{switch-to-buffer}. | ||
| 857 | One way to restore these correspondences, and the selected window, is to | ||
| 858 | use @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 |
| 860 | point value relocates the saved value, just as it relocates all markers. | 862 | point value relocates the saved value, just as it relocates all |
| 861 | More precisely, the saved value is a marker with insertion type | 863 | markers. More precisely, the saved value is a marker with insertion |
| 862 | @code{nil}. @xref{Marker Insertion Types}. Therefore, when the saved | 864 | type @code{nil}. @xref{Marker Insertion Types}. Therefore, when the |
| 863 | point value is restored, it normally comes before the inserted text. | 865 | saved point value is restored, it normally comes before the inserted |
| 866 | text. | ||
| 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 |
| 866 | not prevent functions which modify the buffer from setting | 869 | not prevent functions which modify the buffer from setting |