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