(dired-sort-set-modeline): Use force-mode-line-update.
[bpt/emacs.git] / lispref / buffers.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/buffers
6 @node Buffers, Windows, Backups and Auto-Saving, Top
7 @chapter Buffers
8 @cindex buffer
9
10 A @dfn{buffer} is a Lisp object containing text to be edited. Buffers
11 are used to hold the contents of files that are being visited; there may
12 also be buffers that are not visiting files. While several buffers may
13 exist at one time, exactly one buffer is designated the @dfn{current
14 buffer} at any time. Most editing commands act on the contents of the
15 current buffer. Each buffer, including the current buffer, may or may
16 not be displayed in any windows.
17
18 @menu
19 * Buffer Basics:: What is a buffer?
20 * Buffer Names:: Accessing and changing buffer names.
21 * Buffer File Name:: The buffer file name indicates which file is visited.
22 * Buffer Modification:: A buffer is @dfn{modified} if it needs to be saved.
23 * Modification Time:: Determining whether the visited file was changed
24 ``behind Emacs's back''.
25 * Read Only Buffers:: Modifying text is not allowed in a read-only buffer.
26 * The Buffer List:: How to look at all the existing buffers.
27 * Creating Buffers:: Functions that create buffers.
28 * Killing Buffers:: Buffers exist until explicitly killed.
29 * Current Buffer:: Designating a buffer as current
30 so primitives will access its contents.
31 @end menu
32
33 @node Buffer Basics
34 @comment node-name, next, previous, up
35 @section Buffer Basics
36
37 @ifinfo
38 A @dfn{buffer} is a Lisp object containing text to be edited. Buffers
39 are used to hold the contents of files that are being visited; there may
40 also be buffers that are not visiting files. While several buffers may
41 exist at one time, exactly one buffer is designated the @dfn{current
42 buffer} at any time. Most editing commands act on the contents of the
43 current buffer. Each buffer, including the current buffer, may or may
44 not be displayed in any windows.
45 @end ifinfo
46
47 Buffers in Emacs editing are objects that have distinct names and
48 hold text that can be edited. Buffers appear to Lisp programs as a
49 special data type. The contents of a buffer may be viewed as an
50 extendable string; insertions and deletions may occur in any part of the
51 buffer. @xref{Text}.
52
53 A Lisp buffer object contains numerous pieces of information. Some of
54 this information is directly accessible to the programmer through
55 variables, while other information is accessible only through
56 special-purpose functions. For example, the visited file name is
57 directly accessible through a variable, while the value of point is
58 accessible only through a primitive function.
59
60 Buffer-specific information that is directly accessible is stored in
61 @dfn{buffer-local} variable bindings, which are variable values that are
62 effective only in a particular buffer. This feature allows each buffer
63 to override the values of certain variables. Most major modes override
64 variables such as @code{fill-column} or @code{comment-column} in this
65 way. For more information about buffer-local variables and functions
66 related to them, see @ref{Buffer-Local Variables}.
67
68 For functions and variables related to visiting files in buffers, see
69 @ref{Visiting Files} and @ref{Saving Buffers}. For functions and
70 variables related to the display of buffers in windows, see
71 @ref{Buffers and Windows}.
72
73 @defun bufferp object
74 This function returns @code{t} if @var{object} is a buffer,
75 @code{nil} otherwise.
76 @end defun
77
78 @node Buffer Names
79 @section Buffer Names
80 @cindex buffer names
81
82 Each buffer has a unique name, which is a string. Many of the
83 functions that work on buffers accept either a buffer or a buffer name
84 as an argument. Any argument called @var{buffer-or-name} is of this
85 sort, and an error is signaled if it is neither a string nor a buffer.
86 Any argument called @var{buffer} must be an actual buffer
87 object, not a name.
88
89 Buffers that are ephemeral and generally uninteresting to the user
90 have names starting with a space, so that the @code{list-buffers} or
91 @code{buffer-menu} commands don't mention them. A name starting with
92 space also initially disables recording undo information; see
93 @ref{Undo}.
94
95 @defun buffer-name &optional buffer
96 This function returns the name of @var{buffer} as a string. If
97 @var{buffer} is not supplied, it defaults to the current buffer.
98
99 If @code{buffer-name} returns @code{nil}, it means that @var{buffer}
100 has been killed. @xref{Killing Buffers}.
101
102 @example
103 @group
104 (buffer-name)
105 @result{} "buffers.texi"
106 @end group
107
108 @group
109 (setq foo (get-buffer "temp"))
110 @result{} #<buffer temp>
111 @end group
112 @group
113 (kill-buffer foo)
114 @result{} nil
115 @end group
116 @group
117 (buffer-name foo)
118 @result{} nil
119 @end group
120 @group
121 foo
122 @result{} #<killed buffer>
123 @end group
124 @end example
125 @end defun
126
127 @deffn Command rename-buffer newname &optional unique
128 This function renames the current buffer to @var{newname}. An error
129 is signaled if @var{newname} is not a string, or if there is already a
130 buffer with that name. The function returns @code{nil}.
131
132 @c Emacs 19 feature
133 Ordinarily, @code{rename-buffer} signals an error if @var{newname} is
134 already in use. However, if @var{unique} is non-@code{nil}, it modifies
135 @var{newname} to make a name that is not in use. Interactively, you can
136 make @var{unique} non-@code{nil} with a numeric prefix argument.
137
138 One application of this command is to rename the @samp{*shell*} buffer
139 to some other name, thus making it possible to create a second shell
140 buffer under the name @samp{*shell*}.
141 @end deffn
142
143 @defun get-buffer buffer-or-name
144 This function returns the buffer specified by @var{buffer-or-name}.
145 If @var{buffer-or-name} is a string and there is no buffer with that
146 name, the value is @code{nil}. If @var{buffer-or-name} is a buffer, it
147 is returned as given. (That is not very useful, so the argument is usually
148 a name.) For example:
149
150 @example
151 @group
152 (setq b (get-buffer "lewis"))
153 @result{} #<buffer lewis>
154 @end group
155 @group
156 (get-buffer b)
157 @result{} #<buffer lewis>
158 @end group
159 @group
160 (get-buffer "Frazzle-nots")
161 @result{} nil
162 @end group
163 @end example
164
165 See also the function @code{get-buffer-create} in @ref{Creating Buffers}.
166 @end defun
167
168 @c Emacs 19 feature
169 @defun generate-new-buffer-name starting-name
170 This function returns a name that would be unique for a new buffer---but
171 does not create the buffer. It starts with @var{starting-name}, and
172 produces a name not currently in use for any buffer by appending a
173 number inside of @samp{<@dots{}>}.
174
175 See the related function @code{generate-new-buffer} in @ref{Creating
176 Buffers}.
177 @end defun
178
179 @node Buffer File Name
180 @section Buffer File Name
181 @cindex visited file
182 @cindex buffer file name
183 @cindex file name of buffer
184
185 The @dfn{buffer file name} is the name of the file that is visited in
186 that buffer. When a buffer is not visiting a file, its buffer file name
187 is @code{nil}. Most of the time, the buffer name is the same as the
188 nondirectory part of the buffer file name, but the buffer file name and
189 the buffer name are distinct and can be set independently.
190 @xref{Visiting Files}.
191
192 @defun buffer-file-name &optional buffer
193 This function returns the absolute file name of the file that
194 @var{buffer} is visiting. If @var{buffer} is not visiting any file,
195 @code{buffer-file-name} returns @code{nil}. If @var{buffer} is not
196 supplied, it defaults to the current buffer.
197
198 @example
199 @group
200 (buffer-file-name (other-buffer))
201 @result{} "/usr/user/lewis/manual/files.texi"
202 @end group
203 @end example
204 @end defun
205
206 @defvar buffer-file-name
207 This buffer-local variable contains the name of the file being visited
208 in the current buffer, or @code{nil} if it is not visiting a file. It
209 is a permanent local, unaffected by @code{kill-local-variables}.
210
211 @example
212 @group
213 buffer-file-name
214 @result{} "/usr/user/lewis/manual/buffers.texi"
215 @end group
216 @end example
217
218 It is risky to change this variable's value without doing various other
219 things. See the definition of @code{set-visited-file-name} in
220 @file{files.el}; some of the things done there, such as changing the
221 buffer name, are not strictly necessary, but others are essential to
222 avoid confusing Emacs.
223 @end defvar
224
225 @defvar buffer-file-truename
226 This buffer-local variable holds the truename of the file visited in the
227 current buffer, or @code{nil} if no file is visited. It is a permanent
228 local, unaffected by @code{kill-local-variables}. @xref{Truenames}.
229 @end defvar
230
231 @defvar buffer-file-number
232 This buffer-local variable holds the file number and directory device
233 number of the file visited in the current buffer, or @code{nil} if no
234 file or a nonexistent file is visited. It is a permanent local,
235 unaffected by @code{kill-local-variables}. @xref{Truenames}.
236
237 The value is normally a list of the form @code{(@var{filenum}
238 @var{devnum})}. This pair of numbers uniquely identifies the file among
239 all files accessible on the system. See the function
240 @code{file-attributes}, in @ref{File Attributes}, for more information
241 about them.
242 @end defvar
243
244 @defun get-file-buffer filename
245 This function returns the buffer visiting file @var{filename}. If
246 there is no such buffer, it returns @code{nil}. The argument
247 @var{filename}, which must be a string, is expanded (@pxref{File Name
248 Expansion}), then compared against the visited file names of all live
249 buffers.
250
251 @example
252 @group
253 (get-file-buffer "buffers.texi")
254 @result{} #<buffer buffers.texi>
255 @end group
256 @end example
257
258 In unusual circumstances, there can be more than one buffer visiting
259 the same file name. In such cases, this function returns the first
260 such buffer in the buffer list.
261 @end defun
262
263 @deffn Command set-visited-file-name filename
264 If @var{filename} is a non-empty string, this function changes the
265 name of the file visited in current buffer to @var{filename}. (If the
266 buffer had no visited file, this gives it one.) The @emph{next time}
267 the buffer is saved it will go in the newly-specified file. This
268 command marks the buffer as modified, since it does not (as far as Emacs
269 knows) match the contents of @var{filename}, even if it matched the
270 former visited file.
271
272 If @var{filename} is @code{nil} or the empty string, that stands for
273 ``no visited file''. In this case, @code{set-visited-file-name} marks
274 the buffer as having no visited file.
275
276 @c Wordy to avoid overfull hbox. --rjc 16mar92
277 When the function @code{set-visited-file-name} is called interactively, it
278 prompts for @var{filename} in the minibuffer.
279
280 See also @code{clear-visited-file-modtime} and
281 @code{verify-visited-file-modtime} in @ref{Buffer Modification}.
282 @end deffn
283
284 @defvar list-buffers-directory
285 This buffer-local variable records a string to display in a buffer
286 listing in place of the visited file name, for buffers that don't have a
287 visited file name. Dired buffers use this variable.
288 @end defvar
289
290 @node Buffer Modification
291 @section Buffer Modification
292 @cindex buffer modification
293 @cindex modification flag (of buffer)
294
295 Emacs keeps a flag called the @dfn{modified flag} for each buffer, to
296 record whether you have changed the text of the buffer. This flag is
297 set to @code{t} whenever you alter the contents of the buffer, and
298 cleared to @code{nil} when you save it. Thus, the flag shows whether
299 there are unsaved changes. The flag value is normally shown in the mode
300 line (@pxref{Mode Line Variables}), and controls saving (@pxref{Saving
301 Buffers}) and auto-saving (@pxref{Auto-Saving}).
302
303 Some Lisp programs set the flag explicitly. For example, the function
304 @code{set-visited-file-name} sets the flag to @code{t}, because the text
305 does not match the newly-visited file, even if it is unchanged from the
306 file formerly visited.
307
308 The functions that modify the contents of buffers are described in
309 @ref{Text}.
310
311 @defun buffer-modified-p &optional buffer
312 This function returns @code{t} if the buffer @var{buffer} has been modified
313 since it was last read in from a file or saved, or @code{nil}
314 otherwise. If @var{buffer} is not supplied, the current buffer
315 is tested.
316 @end defun
317
318 @defun set-buffer-modified-p flag
319 This function marks the current buffer as modified if @var{flag} is
320 non-@code{nil}, or as unmodified if the flag is @code{nil}.
321
322 Another effect of calling this function is to cause unconditional
323 redisplay of the mode line for the current buffer. In fact, the
324 function @code{force-mode-line-update} works by doing this:
325
326 @example
327 @group
328 (set-buffer-modified-p (buffer-modified-p))
329 @end group
330 @end example
331 @end defun
332
333 @deffn Command not-modified
334 This command marks the current buffer as unmodified, and not needing
335 to be saved. Don't use this function in programs, since it prints a
336 message in the echo area; use @code{set-buffer-modified-p} (above) instead.
337 @end deffn
338
339 @c Emacs 19 feature
340 @defun buffer-modified-tick &optional buffer
341 This function returns @var{buffer}`s modification-count. This is a
342 counter that increments every time the buffer is modified. If
343 @var{buffer} is @code{nil} (or omitted), the current buffer is used.
344 @end defun
345
346 @node Modification Time
347 @comment node-name, next, previous, up
348 @section Comparison of Modification Time
349 @cindex comparison of modification time
350 @cindex modification time, comparison of
351
352 Suppose that you visit a file and make changes in its buffer, and
353 meanwhile the file itself is changed on disk. At this point, saving the
354 buffer would overwrite the changes in the file. Occasionally this may
355 be what you want, but usually it would lose valuable information. Emacs
356 therefore checks the file's modification time using the functions
357 described below before saving the file.
358
359 @defun verify-visited-file-modtime buffer
360 This function compares what @var{buffer} has recorded for the
361 modification time of its visited file against the actual modification
362 time of the file as recorded by the operating system. The two should be
363 the same unless some other process has written the file since Emacs
364 visited or saved it.
365
366 The function returns @code{t} if the last actual modification time and
367 Emacs's recorded modification time are the same, @code{nil} otherwise.
368 @end defun
369
370 @defun clear-visited-file-modtime
371 This function clears out the record of the last modification time of
372 the file being visited by the current buffer. As a result, the next
373 attempt to save this buffer will not complain of a discrepancy in
374 file modification times.
375
376 This function is called in @code{set-visited-file-name} and other
377 exceptional places where the usual test to avoid overwriting a changed
378 file should not be done.
379 @end defun
380
381 @c Emacs 19 feature
382 @defun visited-file-modtime
383 This function returns the buffer's recorded last file modification time,
384 as a list of the form @code{(@var{high} . @var{low})}. (This is the
385 same format that @code{file-attributes} uses to return time values; see
386 @ref{File Attributes}.)
387 @end defun
388
389 @c Emacs 19 feature
390 @defun set-visited-file-modtime &optional time
391 This function updates the buffer's record of the last modification time
392 of the visited file, to the value specified by @var{time} if @var{time}
393 is not @code{nil}, and otherwise to the last modification time of the
394 visited file.
395
396 If @var{time} is not @code{nil}, it should have the form
397 @code{(@var{high} . @var{low})} or @code{(@var{high} @var{low})}, in
398 either case containing two integers, each of which holds 16 bits of the
399 time.
400
401 This function is useful if the buffer was not read from the file
402 normally, or if the file itself has been changed for some known benign
403 reason.
404 @end defun
405
406 @defun ask-user-about-supersession-threat filename
407 @cindex obsolete buffer
408 This function is used to ask a user how to proceed after an attempt to
409 modify an obsolete buffer visiting file @var{filename}. An
410 @dfn{obsolete buffer} is an unmodified buffer for which the associated
411 file on disk is newer than the last save-time of the buffer. This means
412 some other program has probably altered the file.
413
414 @kindex file-supersession
415 Depending on the user's answer, the function may return normally, in
416 which case the modification of the buffer proceeds, or it may signal a
417 @code{file-supersession} error with data @code{(@var{filename})}, in which
418 case the proposed buffer modification is not allowed.
419
420 This function is called automatically by Emacs on the proper
421 occasions. It exists so you can customize Emacs by redefining it.
422 See the file @file{userlock.el} for the standard definition.
423
424 See also the file locking mechanism in @ref{File Locks}.
425 @end defun
426
427 @node Read Only Buffers
428 @section Read-Only Buffers
429 @cindex read-only buffer
430 @cindex buffer, read-only
431
432 If a buffer is @dfn{read-only}, then you cannot change its contents,
433 although you may change your view of the contents by scrolling and
434 narrowing.
435
436 Read-only buffers are used in two kinds of situations:
437
438 @itemize @bullet
439 @item
440 A buffer visiting a write-protected file is normally read-only.
441
442 Here, the purpose is to show the user that editing the buffer with the
443 aim of saving it in the file may be futile or undesirable. The user who
444 wants to change the buffer text despite this can do so after clearing
445 the read-only flag with @kbd{C-M-q}.
446
447 @item
448 Modes such as Dired and Rmail make buffers read-only when altering the
449 contents with the usual editing commands is probably a mistake.
450
451 The special commands of these modes bind @code{buffer-read-only} to
452 @code{nil} (with @code{let}) or bind @code{inhibit-read-only} to
453 @code{t} around the places where they change the text.
454 @end itemize
455
456 @defvar buffer-read-only
457 This buffer-local variable specifies whether the buffer is read-only.
458 The buffer is read-only if this variable is non-@code{nil}.
459 @end defvar
460
461 @defvar inhibit-read-only
462 If this variable is non-@code{nil}, then read-only buffers and read-only
463 characters may be modified. Read-only characters in a buffer are those
464 that have non-@code{nil} @code{read-only} properties (either text
465 properties or overlay properties). @xref{Special Properties}, for more
466 information about text properties. @xref{Overlays}, for more
467 information about overlays and their properties.
468
469 If @code{inhibit-read-only} is @code{t}, all @code{read-only} character
470 properties have no effect. If @code{inhibit-read-only} is a list, then
471 @code{read-only} character properties have no effect if they are members
472 of the list (comparison is done with @code{eq}).
473 @end defvar
474
475 @deffn Command toggle-read-only
476 This command changes whether the current buffer is read-only. It is
477 intended for interactive use; don't use it in programs. At any given
478 point in a program, you should know whether you want the read-only flag
479 on or off; so you can set @code{buffer-read-only} explicitly to the
480 proper value, @code{t} or @code{nil}.
481 @end deffn
482
483 @defun barf-if-buffer-read-only
484 This function signals a @code{buffer-read-only} error if the current
485 buffer is read-only. @xref{Interactive Call}, for another way to
486 signal an error if the current buffer is read-only.
487 @end defun
488
489 @node The Buffer List
490 @section The Buffer List
491 @cindex buffer list
492
493 The @dfn{buffer list} is a list of all live buffers. Creating a
494 buffer adds it to this list, and killing a buffer deletes it. The order
495 of the buffers in the list is based primarily on how recently each
496 buffer has been displayed in the selected window. Buffers move to the
497 front of the list when they are selected and to the end when they are
498 buried. Several functions, notably @code{other-buffer}, use this
499 ordering. A buffer list displayed for the user also follows this order.
500
501 @defun buffer-list
502 This function returns a list of all buffers, including those whose names
503 begin with a space. The elements are actual buffers, not their names.
504
505 @example
506 @group
507 (buffer-list)
508 @result{} (#<buffer buffers.texi>
509 #<buffer *Minibuf-1*> #<buffer buffer.c>
510 #<buffer *Help*> #<buffer TAGS>)
511 @end group
512
513 @group
514 ;; @r{Note that the name of the minibuffer}
515 ;; @r{begins with a space!}
516 (mapcar (function buffer-name) (buffer-list))
517 @result{} ("buffers.texi" " *Minibuf-1*"
518 "buffer.c" "*Help*" "TAGS")
519 @end group
520 @end example
521
522 This list is a copy of a list used inside Emacs; modifying it has no
523 effect on the ordering of buffers.
524 @end defun
525
526 @defun other-buffer &optional buffer-or-name visible-ok
527 This function returns the first buffer in the buffer list other than
528 @var{buffer-or-name}. Usually this is the buffer most recently shown in
529 the selected window, aside from @var{buffer-or-name}. Buffers whose
530 names start with a space are not considered.
531
532 If @var{buffer-or-name} is not supplied (or if it is not a buffer),
533 then @code{other-buffer} returns the first buffer on the buffer list
534 that is not visible in any window in a visible frame.
535
536 @c Emacs 19 feature
537 If @var{visible-ok} is @code{nil}, @code{other-buffer} avoids returning
538 a buffer visible in any window on any visible frame, except as a last
539 resort. If @var{visible-ok} is non-@code{nil}, then it does not matter
540 whether a buffer is displayed somewhere or not.
541
542 If no suitable buffer exists, the buffer @samp{*scratch*} is returned
543 (and created, if necessary).
544 @end defun
545
546 @deffn Command bury-buffer &optional buffer-or-name
547 This function puts @var{buffer-or-name} at the end of the buffer list
548 without changing the order of any of the other buffers on the list.
549 This buffer therefore becomes the least desirable candidate for
550 @code{other-buffer} to return.
551
552 If @var{buffer-or-name} is @code{nil} or omitted, this means to bury the
553 current buffer. In addition, if the buffer is displayed in the selected
554 window, this switches to some other buffer (obtained using
555 @code{other-buffer}) in the selected window. But if the buffer is
556 displayed in some other window, it remains displayed there.
557
558 If you wish to replace a buffer in all the windows that display it, use
559 @code{replace-buffer-in-windows}. @xref{Buffers and Windows}.
560 @end deffn
561
562 @node Creating Buffers
563 @section Creating Buffers
564 @cindex creating buffers
565 @cindex buffers, creating
566
567 This section describes the two primitives for creating buffers.
568 @code{get-buffer-create} creates a buffer if it finds no existing buffer
569 with the specified name; @code{generate-new-buffer} always creates a new
570 buffer and gives it a unique name.
571
572 Other functions you can use to create buffers include
573 @code{with-output-to-temp-buffer} (@pxref{Temporary Displays}) and
574 @code{create-file-buffer} (@pxref{Visiting Files}). Starting a
575 subprocess can also create a buffer (@pxref{Processes}).
576
577 @defun get-buffer-create name
578 This function returns a buffer named @var{name}. It returns an existing
579 buffer with that name, if one exists; otherwise, it creates a new
580 buffer. The buffer does not become the current buffer---this function
581 does not change which buffer is current.
582
583 An error is signaled if @var{name} is not a string.
584
585 @example
586 @group
587 (get-buffer-create "foo")
588 @result{} #<buffer foo>
589 @end group
590 @end example
591
592 The major mode for the new buffer is set according to the variable
593 @code{default-major-mode}. @xref{Auto Major Mode}.
594 @end defun
595
596 @defun generate-new-buffer name
597 This function returns a newly created, empty buffer, but does not make
598 it current. If there is no buffer named @var{name}, then that is the
599 name of the new buffer. If that name is in use, this function adds
600 suffixes of the form @samp{<@var{n}>} to @var{name}, where @var{n} is an
601 integer. It tries successive integers starting with 2 until it finds an
602 available name.
603
604 An error is signaled if @var{name} is not a string.
605
606 @example
607 @group
608 (generate-new-buffer "bar")
609 @result{} #<buffer bar>
610 @end group
611 @group
612 (generate-new-buffer "bar")
613 @result{} #<buffer bar<2>>
614 @end group
615 @group
616 (generate-new-buffer "bar")
617 @result{} #<buffer bar<3>>
618 @end group
619 @end example
620
621 The major mode for the new buffer is set by the value of
622 @code{default-major-mode}. @xref{Auto Major Mode}.
623
624 See the related function @code{generate-new-buffer-name} in @ref{Buffer
625 Names}.
626 @end defun
627
628 @node Killing Buffers
629 @section Killing Buffers
630 @cindex killing buffers
631 @cindex buffers, killing
632
633 @dfn{Killing a buffer} makes its name unknown to Emacs and makes its
634 text space available for other use.
635
636 The buffer object for the buffer that has been killed remains in
637 existence as long as anything refers to it, but it is specially marked
638 so that you cannot make it current or display it. Killed buffers retain
639 their identity, however; two distinct buffers, when killed, remain
640 distinct according to @code{eq}.
641
642 If you kill a buffer that is current or displayed in a window, Emacs
643 automatically selects or displays some other buffer instead. This means
644 that killing a buffer can in general change the current buffer.
645 Therefore, when you kill a buffer, you should also take the precautions
646 associated with changing the current buffer (unless you happen to know
647 that the buffer being killed isn't current). @xref{Current Buffer}.
648
649 The @code{buffer-name} of a killed buffer is @code{nil}. You can use
650 this feature to test whether a buffer has been killed:
651
652 @example
653 @group
654 (defun buffer-killed-p (buffer)
655 "Return t if BUFFER is killed."
656 (not (buffer-name buffer)))
657 @end group
658 @end example
659
660 @deffn Command kill-buffer buffer-or-name
661 This function kills the buffer @var{buffer-or-name}, freeing all its
662 memory for use as space for other buffers. (Emacs version 18 and older
663 was unable to return the memory to the operating system.) It returns
664 @code{nil}.
665
666 Any processes that have this buffer as the @code{process-buffer} are
667 sent the @code{SIGHUP} signal, which normally causes them to terminate.
668 (The basic meaning of @code{SIGHUP} is that a dialup line has been
669 disconnected.) @xref{Deleting Processes}.
670
671 If the buffer is visiting a file and contains unsaved changes,
672 @code{kill-buffer} asks the user to confirm before the buffer is killed.
673 It does this even if not called interactively. To prevent the request
674 for confirmation, clear the modified flag before calling
675 @code{kill-buffer}. @xref{Buffer Modification}.
676
677 Killing a buffer that is already dead has no effect.
678
679 @smallexample
680 (kill-buffer "foo.unchanged")
681 @result{} nil
682 (kill-buffer "foo.changed")
683
684 ---------- Buffer: Minibuffer ----------
685 Buffer foo.changed modified; kill anyway? (yes or no) @kbd{yes}
686 ---------- Buffer: Minibuffer ----------
687
688 @result{} nil
689 @end smallexample
690 @end deffn
691
692 @defvar kill-buffer-query-functions
693 After confirming unsaved changes, @code{kill-buffer} calls the functions
694 in the list @code{kill-buffer-query-functions}, in order of appearance,
695 with no arguments. The buffer being killed is the current buffer when
696 they are called. The idea is that these functions ask for confirmation
697 from the user for various nonstandard reasons. If any of them returns
698 @code{nil}, @code{kill-buffer} spares the buffer's life.
699 @end defvar
700
701 @defvar kill-buffer-hook
702 This is a normal hook run by @code{kill-buffer} after asking all the
703 questions it is going to ask, just before actually killing the buffer.
704 The buffer to be killed is current when the hook functions run.
705 @xref{Hooks}.
706 @end defvar
707
708 @defvar buffer-offer-save
709 This variable, if non-@code{nil} in a particular buffer, tells
710 @code{save-buffers-kill-emacs} and @code{save-some-buffers} to offer to
711 save that buffer, just as they offer to save file-visiting buffers. The
712 variable @code{buffer-offer-save} automatically becomes buffer-local
713 when set for any reason. @xref{Buffer-Local Variables}.
714 @end defvar
715
716 @node Current Buffer
717 @section The Current Buffer
718 @cindex selecting a buffer
719 @cindex changing to another buffer
720 @cindex current buffer
721
722 There are, in general, many buffers in an Emacs session. At any time,
723 one of them is designated as the @dfn{current buffer}. This is the
724 buffer in which most editing takes place, because most of the primitives
725 for examining or changing text in a buffer operate implicitly on the
726 current buffer (@pxref{Text}). Normally the buffer that is displayed on
727 the screen in the selected window is the current buffer, but this is not
728 always so: a Lisp program can designate any buffer as current
729 temporarily in order to operate on its contents, without changing what
730 is displayed on the screen.
731
732 The way to designate a current buffer in a Lisp program is by calling
733 @code{set-buffer}. The specified buffer remains current until a new one
734 is designated.
735
736 When an editing command returns to the editor command loop, the
737 command loop designates the buffer displayed in the selected window as
738 current, to prevent confusion: the buffer that the cursor is in when
739 Emacs reads a command is the buffer that the command will apply to.
740 (@xref{Command Loop}.) Therefore, @code{set-buffer} is not the way to
741 switch visibly to a different buffer so that the user can edit it. For
742 this, you must use the functions described in @ref{Displaying Buffers}.
743
744 However, Lisp functions that change to a different current buffer
745 should not depend on the command loop to set it back afterwards.
746 Editing commands written in Emacs Lisp can be called from other programs
747 as well as from the command loop. It is convenient for the caller if
748 the subroutine does not change which buffer is current (unless, of
749 course, that is the subroutine's purpose). Therefore, you should
750 normally use @code{set-buffer} within a @code{save-excursion} that will
751 restore the current buffer when your function is done
752 (@pxref{Excursions}). Here is an example, the code for the command
753 @code{append-to-buffer} (with the documentation string abridged):
754
755 @example
756 @group
757 (defun append-to-buffer (buffer start end)
758 "Append to specified buffer the text of the region.
759 @dots{}"
760 (interactive "BAppend to buffer: \nr")
761 (let ((oldbuf (current-buffer)))
762 (save-excursion
763 (set-buffer (get-buffer-create buffer))
764 (insert-buffer-substring oldbuf start end))))
765 @end group
766 @end example
767
768 @noindent
769 This function binds a local variable to the current buffer, and then
770 @code{save-excursion} records the values of point, the mark, and the
771 original buffer. Next, @code{set-buffer} makes another buffer current.
772 Finally, @code{insert-buffer-substring} copies the string from the
773 original current buffer to the new current buffer.
774
775 If the buffer appended to happens to be displayed in some window,
776 the next redisplay will show how its text has changed. Otherwise, you
777 will not see the change immediately on the screen. The buffer becomes
778 current temporarily during the execution of the command, but this does
779 not cause it to be displayed.
780
781 If you make local bindings (with @code{let} or function arguments) for
782 a variable that may also have buffer-local bindings, make sure that the
783 same buffer is current at the beginning and at the end of the local
784 binding's scope. Otherwise you might bind it in one buffer and unbind
785 it in another! There are two ways to do this. In simple cases, you may
786 see that nothing ever changes the current buffer within the scope of the
787 binding. Otherwise, use @code{save-excursion} to make sure that the
788 buffer current at the beginning is current again whenever the variable
789 is unbound.
790
791 It is not reliable to change the current buffer back with
792 @code{set-buffer}, because that won't do the job if a quit happens while
793 the wrong buffer is current. Here is what @emph{not} to do:
794
795 @example
796 @group
797 (let (buffer-read-only
798 (obuf (current-buffer)))
799 (set-buffer @dots{})
800 @dots{}
801 (set-buffer obuf))
802 @end group
803 @end example
804
805 @noindent
806 Using @code{save-excursion}, as shown below, handles quitting, errors,
807 and @code{throw}, as well as ordinary evaluation.
808
809 @example
810 @group
811 (let (buffer-read-only)
812 (save-excursion
813 (set-buffer @dots{})
814 @dots{}))
815 @end group
816 @end example
817
818 @defun current-buffer
819 This function returns the current buffer.
820
821 @example
822 @group
823 (current-buffer)
824 @result{} #<buffer buffers.texi>
825 @end group
826 @end example
827 @end defun
828
829 @defun set-buffer buffer-or-name
830 This function makes @var{buffer-or-name} the current buffer. It does
831 not display the buffer in the currently selected window or in any other
832 window, so the user cannot necessarily see the buffer. But Lisp
833 programs can in any case work on it.
834
835 This function returns the buffer identified by @var{buffer-or-name}.
836 An error is signaled if @var{buffer-or-name} does not identify an
837 existing buffer.
838 @end defun