(Qfixed_window_size): New.
[bpt/emacs.git] / lispref / buffers.texi
index 6f777d7..f05d242 100644 (file)
@@ -1,6 +1,6 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 
+@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. 
 @c See the file elisp.texi for copying conditions.
 @setfilename ../info/buffers
 @node Buffers, Windows, Backups and Auto-Saving, Top
@@ -9,7 +9,7 @@
 
   A @dfn{buffer} is a Lisp object containing text to be edited.  Buffers
 are used to hold the contents of files that are being visited; there may
-also be buffers which are not visiting files.  While several buffers may
+also be buffers that are not visiting files.  While several buffers may
 exist at one time, exactly one buffer is designated the @dfn{current
 buffer} at any time.  Most editing commands act on the contents of the
 current buffer.  Each buffer, including the current buffer, may or may
@@ -17,6 +17,8 @@ not be displayed in any windows.
 
 @menu
 * Buffer Basics::       What is a buffer?
+* Current Buffer::      Designating a buffer as current
+                          so primitives will access its contents.
 * Buffer Names::        Accessing and changing buffer names.
 * Buffer File Name::    The buffer file name indicates which file is visited.
 * Buffer Modification:: A buffer is @dfn{modified} if it needs to be saved.
@@ -26,8 +28,8 @@ not be displayed in any windows.
 * The Buffer List::     How to look at all the existing buffers.
 * Creating Buffers::    Functions that create buffers.
 * Killing Buffers::     Buffers exist until explicitly killed.
-* Current Buffer::      Designating a buffer as current
-                          so primitives will access its contents.
+* Indirect Buffers::    An indirect buffer shares text with some other buffer.
+* Buffer Gap::          The gap in the buffer.
 @end menu
 
 @node Buffer Basics
@@ -37,22 +39,22 @@ not be displayed in any windows.
 @ifinfo
   A @dfn{buffer} is a Lisp object containing text to be edited.  Buffers
 are used to hold the contents of files that are being visited; there may
-also be buffers which are not visiting files.  While several buffers may
+also be buffers that are not visiting files.  While several buffers may
 exist at one time, exactly one buffer is designated the @dfn{current
 buffer} at any time.  Most editing commands act on the contents of the
 current buffer.  Each buffer, including the current buffer, may or may
 not be displayed in any windows.
 @end ifinfo
 
-  Buffers in Emacs editing are objects which have distinct names and
-hold text that can be edited.  Buffers appear to Lisp programs as a
-special data type.  The contents of a buffer may be viewed as an
-extendable string; insertions and deletions may occur in any part of the
+  Buffers in Emacs editing are objects that have distinct names and hold
+text that can be edited.  Buffers appear to Lisp programs as a special
+data type.  You can think of the contents of a buffer as a string that
+you can extend; insertions and deletions may occur in any part of the
 buffer.  @xref{Text}.
 
   A Lisp buffer object contains numerous pieces of information.  Some of
 this information is directly accessible to the programmer through
-variables, while other information is only accessible through
+variables, while other information is accessible only through
 special-purpose functions.  For example, the visited file name is
 directly accessible through a variable, while the value of point is
 accessible only through a primitive function.
@@ -75,6 +77,172 @@ This function returns @code{t} if @var{object} is a buffer,
 @code{nil} otherwise.
 @end defun
 
+@node Current Buffer
+@section The Current Buffer
+@cindex selecting a buffer
+@cindex changing to another buffer
+@cindex current buffer
+
+  There are, in general, many buffers in an Emacs session.  At any time,
+one of them is designated as the @dfn{current buffer}.  This is the
+buffer in which most editing takes place, because most of the primitives
+for examining or changing text in a buffer operate implicitly on the
+current buffer (@pxref{Text}).  Normally the buffer that is displayed on
+the screen in the selected window is the current buffer, but this is not
+always so: a Lisp program can temporarily designate any buffer as
+current in order to operate on its contents, without changing what is
+displayed on the screen.
+
+  The way to designate a current buffer in a Lisp program is by calling
+@code{set-buffer}.  The specified buffer remains current until a new one
+is designated.
+
+  When an editing command returns to the editor command loop, the
+command loop designates the buffer displayed in the selected window as
+current, to prevent confusion: the buffer that the cursor is in when
+Emacs reads a command is the buffer that the command will apply to.
+(@xref{Command Loop}.)  Therefore, @code{set-buffer} is not the way to
+switch visibly to a different buffer so that the user can edit it.  For
+this, you must use the functions described in @ref{Displaying Buffers}.
+
+  However, Lisp functions that change to a different current buffer
+should not depend on the command loop to set it back afterwards.
+Editing commands written in Emacs Lisp can be called from other programs
+as well as from the command loop.  It is convenient for the caller if
+the subroutine does not change which buffer is current (unless, of
+course, that is the subroutine's purpose).  Therefore, you should
+normally use @code{set-buffer} within a @code{save-current-buffer} or
+@code{save-excursion} (@pxref{Excursions}) form that will restore the
+current buffer when your function is done.  Here is an example, the
+code for the command @code{append-to-buffer} (with the documentation
+string abridged):
+
+@example
+@group
+(defun append-to-buffer (buffer start end)
+  "Append to specified buffer the text of the region.
+@dots{}"
+  (interactive "BAppend to buffer: \nr")
+  (let ((oldbuf (current-buffer)))
+    (save-current-buffer
+      (set-buffer (get-buffer-create buffer))
+      (insert-buffer-substring oldbuf start end))))
+@end group
+@end example
+
+@noindent
+This function binds a local variable to record the current buffer, and
+then @code{save-current-buffer} arranges to make it current again.
+Next, @code{set-buffer} makes the specified buffer current.  Finally,
+@code{insert-buffer-substring} copies the string from the original
+current buffer to the specified (and now current) buffer.
+
+  If the buffer appended to happens to be displayed in some window, 
+the next redisplay will show how its text has changed.  Otherwise, you
+will not see the change immediately on the screen.  The buffer becomes
+current temporarily during the execution of the command, but this does
+not cause it to be displayed.
+
+  If you make local bindings (with @code{let} or function arguments) for
+a variable that may also have buffer-local bindings, make sure that the
+same buffer is current at the beginning and at the end of the local
+binding's scope.  Otherwise you might bind it in one buffer and unbind
+it in another!  There are two ways to do this.  In simple cases, you may
+see that nothing ever changes the current buffer within the scope of the
+binding.  Otherwise, use @code{save-current-buffer} or
+@code{save-excursion} to make sure that the buffer current at the
+beginning is current again whenever the variable is unbound.
+
+  It is not reliable to change the current buffer back with
+@code{set-buffer}, because that won't do the job if a quit happens while
+the wrong buffer is current.  Here is what @emph{not} to do:
+
+@example
+@group
+(let (buffer-read-only
+      (obuf (current-buffer)))
+  (set-buffer @dots{})
+  @dots{}
+  (set-buffer obuf))
+@end group
+@end example
+
+@noindent
+Using @code{save-current-buffer}, as shown here, handles quitting,
+errors, and @code{throw}, as well as ordinary evaluation.
+
+@example
+@group
+(let (buffer-read-only)
+  (save-current-buffer
+    (set-buffer @dots{})
+    @dots{}))
+@end group
+@end example
+
+@defun current-buffer
+This function returns the current buffer.
+
+@example
+@group
+(current-buffer)
+     @result{} #<buffer buffers.texi>
+@end group
+@end example
+@end defun
+
+@defun set-buffer buffer-or-name
+This function makes @var{buffer-or-name} the current buffer.  It does
+not display the buffer in the currently selected window or in any other
+window, so the user cannot necessarily see the buffer.  But Lisp
+programs can in any case work on it.
+
+This function returns the buffer identified by @var{buffer-or-name}.
+An error is signaled if @var{buffer-or-name} does not identify an
+existing buffer.
+@end defun
+
+@defspec save-current-buffer body...
+@tindex save-current-buffer
+The @code{save-current-buffer} macro saves the identity of the current
+buffer, evaluates the @var{body} forms, and finally restores that buffer
+as current.  The return value is the value of the last form in
+@var{body}.  The current buffer is restored even in case of an abnormal
+exit via @code{throw} or error (@pxref{Nonlocal Exits}).
+
+If the buffer that used to be current has been killed by the time of
+exit from @code{save-current-buffer}, then it is not made current again,
+of course.  Instead, whichever buffer was current just before exit
+remains current.
+@end defspec
+
+@defmac with-current-buffer buffer body...
+@tindex with-current-buffer
+The @code{with-current-buffer} macro saves the identity of the current
+buffer, makes @var{buffer} current, evaluates the @var{body} forms, and
+finally restores the buffer.  The return value is the value of the last
+form in @var{body}.  The current buffer is restored even in case of an
+abnormal exit via @code{throw} or error (@pxref{Nonlocal Exits}).
+@end defmac
+
+@defmac with-temp-buffer body...
+@tindex with-temp-buffer
+The @code{with-temp-buffer} macro evaluates the @var{body} forms
+with a temporary buffer as the current buffer.  It saves the identity of
+the current buffer, creates a temporary buffer and makes it current,
+evaluates the @var{body} forms, and finally restores the previous
+current buffer while killing the temporary buffer.
+
+The return value is the value of the last form in @var{body}.  You can
+return the contents of the temporary buffer by using
+@code{(buffer-string)} as the last form.
+
+The current buffer is restored even in case of an abnormal exit via
+@code{throw} or error (@pxref{Nonlocal Exits}).
+@end defmac
+
+See also @code{with-temp-file} in @ref{Writing to Files}.
+
 @node Buffer Names
 @section Buffer Names
 @cindex buffer names
@@ -87,7 +255,7 @@ Any argument called @var{buffer} must be an actual buffer
 object, not a name.
 
   Buffers that are ephemeral and generally uninteresting to the user
-have names starting with a space, so that the @code{list-buffers} or
+have names starting with a space, so that the @code{list-buffers} and
 @code{buffer-menu} commands don't mention them.  A name starting with
 space also initially disables recording undo information; see
 @ref{Undo}.
@@ -127,7 +295,7 @@ foo
 @deffn Command rename-buffer newname &optional unique
 This function renames the current buffer to @var{newname}.  An error
 is signaled if @var{newname} is not a string, or if there is already a
-buffer with that name.  The function returns @code{nil}.
+buffer with that name.  The function returns @var{newname}.
 
 @c Emacs 19 feature
 Ordinarily, @code{rename-buffer} signals an error if @var{newname} is
@@ -216,10 +384,10 @@ buffer-file-name
 @end example
 
 It is risky to change this variable's value without doing various other
-things.  See the definition of @code{set-visited-file-name} in
-@file{files.el}; some of the things done there, such as changing the
-buffer name, are not strictly necessary, but others are essential to
-avoid confusing Emacs.
+things.  Normally it is better to use @code{set-visited-file-name} (see
+below); some of the things done there, such as changing the buffer name,
+are not strictly necessary, but others are essential to avoid confusing
+Emacs.
 @end defvar
 
 @defvar buffer-file-truename
@@ -232,7 +400,7 @@ local, unaffected by @code{kill-local-variables}.  @xref{Truenames}.
 This buffer-local variable holds the file number and directory device
 number of the file visited in the current buffer, or @code{nil} if no
 file or a nonexistent file is visited.  It is a permanent local,
-unaffected by @code{kill-local-variables}.  @xref{Truenames}.
+unaffected by @code{kill-local-variables}.
 
 The value is normally a list of the form @code{(@var{filenum}
 @var{devnum})}.  This pair of numbers uniquely identifies the file among
@@ -260,7 +428,7 @@ the same file name.  In such cases, this function returns the first
 such buffer in the buffer list.
 @end defun
 
-@deffn Command set-visited-file-name filename
+@deffn Command set-visited-file-name filename &optional no-query along-with-file
 If @var{filename} is a non-empty string, this function changes the
 name of the file visited in current buffer to @var{filename}.  (If the
 buffer had no visited file, this gives it one.)  The @emph{next time}
@@ -273,18 +441,22 @@ If @var{filename} is @code{nil} or the empty string, that stands for
 ``no visited file''.  In this case, @code{set-visited-file-name} marks
 the buffer as having no visited file.
 
+Normally, this function asks the user for confirmation if the specified
+file already exists.  If @var{no-query} is non-@code{nil}, that prevents
+asking this question.
+
+If @var{along-with-file} is non-@code{nil}, that means to assume that the
+former visited file has been renamed to @var{filename}.
+
 @c Wordy to avoid overfull hbox.  --rjc 16mar92
 When the function @code{set-visited-file-name} is called interactively, it
 prompts for @var{filename} in the minibuffer.
-
-See also @code{clear-visited-file-modtime} and
-@code{verify-visited-file-modtime} in @ref{Buffer Modification}.
 @end deffn
 
 @defvar list-buffers-directory
-This buffer-local variable records a string to display in a buffer
-listing in place of the visited file name, for buffers that don't have a
-visited file name.  Dired buffers use this variable.
+This buffer-local variable specifies a string to display in a buffer
+listing where the visited file name would go, for buffers that don't
+have a visited file name.  Dired buffers use this variable.
 @end defvar
 
 @node Buffer Modification
@@ -331,14 +503,17 @@ function @code{force-mode-line-update} works by doing this:
 @end defun
 
 @deffn Command not-modified
-This command marks the current buffer as unmodified, and not needing
-to be saved.  Don't use this function in programs, since it prints a
-message in the echo area; use @code{set-buffer-modified-p} (above) instead.
+This command marks the current buffer as unmodified, and not needing to
+be saved.  With prefix arg, it marks the buffer as modified, so that it
+will be saved at the next suitable occasion.
+
+Don't use this function in programs, since it prints a message in the
+echo area; use @code{set-buffer-modified-p} (above) instead.
 @end deffn
 
 @c Emacs 19 feature
 @defun buffer-modified-tick &optional buffer
-This function returns @var{buffer}`s modification-count.  This is a
+This function returns @var{buffer}'s modification-count.  This is a
 counter that increments every time the buffer is modified.  If
 @var{buffer} is @code{nil} (or omitted), the current buffer is used.
 @end defun
@@ -403,24 +578,24 @@ normally, or if the file itself has been changed for some known benign
 reason.
 @end defun
 
-@defun ask-user-about-supersession-threat fn
+@defun ask-user-about-supersession-threat filename
 @cindex obsolete buffer
 This function is used to ask a user how to proceed after an attempt to
-modify an obsolete buffer.  An @dfn{obsolete buffer} is an unmodified
-buffer for which the associated file on disk is newer than the last
-save-time of the buffer.  This means some other program has probably
-altered the file.
-
-This function is called automatically by Emacs on the proper
-occasions.  It exists so you can customize Emacs by redefining it.
-See the file @file{userlock.el} for the standard definition.
+modify an obsolete buffer visiting file @var{filename}.  An
+@dfn{obsolete buffer} is an unmodified buffer for which the associated
+file on disk is newer than the last save-time of the buffer.  This means
+some other program has probably altered the file.
 
 @kindex file-supersession
 Depending on the user's answer, the function may return normally, in
 which case the modification of the buffer proceeds, or it may signal a
-@code{file-supersession} error with data @code{(@var{fn})}, in which
+@code{file-supersession} error with data @code{(@var{filename})}, in which
 case the proposed buffer modification is not allowed.  
 
+This function is called automatically by Emacs on the proper
+occasions.  It exists so you can customize Emacs by redefining it.
+See the file @file{userlock.el} for the standard definition.
+
 See also the file locking mechanism in @ref{File Locks}.
 @end defun
 
@@ -439,10 +614,10 @@ narrowing.
 @item
 A buffer visiting a write-protected file is normally read-only.
 
-Here, the purpose is to show the user that editing the buffer with the
+Here, the purpose is to inform the user that editing the buffer with the
 aim of saving it in the file may be futile or undesirable.  The user who
 wants to change the buffer text despite this can do so after clearing
-the read-only flag with @kbd{C-M-q}.
+the read-only flag with @kbd{C-x C-q}.
 
 @item
 Modes such as Dired and Rmail make buffers read-only when altering the
@@ -450,7 +625,7 @@ contents with the usual editing commands is probably a mistake.
 
 The special commands of these modes bind @code{buffer-read-only} to
 @code{nil} (with @code{let}) or bind @code{inhibit-read-only} to
-@code{t} around the places where they change the text.
+@code{t} around the places where they themselves change the text.
 @end itemize
 
 @defvar buffer-read-only
@@ -460,14 +635,16 @@ The buffer is read-only if this variable is non-@code{nil}.
 
 @defvar inhibit-read-only
 If this variable is non-@code{nil}, then read-only buffers and read-only
-characters may be modified.  The value of @code{buffer-read-only} does
-not matter when @code{inhibit-read-only} is non-@code{nil}.
-
-If @code{inhibit-read-only} is @code{t}, all @code{read-only} text
-properties have no effect (@pxref{Special Properties}).  If
-@code{inhibit-read-only} is a list, then @code{read-only} text
-properties are ignored if they are members of the list (comparison is
-done with @code{eq}).
+characters may be modified.  Read-only characters in a buffer are those
+that have non-@code{nil} @code{read-only} properties (either text
+properties or overlay properties).  @xref{Special Properties}, for more
+information about text properties.  @xref{Overlays}, for more
+information about overlays and their properties.
+
+If @code{inhibit-read-only} is @code{t}, all @code{read-only} character
+properties have no effect.  If @code{inhibit-read-only} is a list, then
+@code{read-only} character properties have no effect if they are members
+of the list (comparison is done with @code{eq}).
 @end defvar
 
 @deffn Command toggle-read-only
@@ -489,16 +666,31 @@ signal an error if the current buffer is read-only.
 @cindex buffer list
 
   The @dfn{buffer list} is a list of all live buffers.  Creating a
-buffer adds it to this list, and killing a buffer deletes it.  The order
+buffer adds it to this list, and killing a buffer excises it.  The order
 of the buffers in the list is based primarily on how recently each
 buffer has been displayed in the selected window.  Buffers move to the
 front of the list when they are selected and to the end when they are
-buried.  Several functions, notably @code{other-buffer}, use this
-ordering.  A buffer list displayed for the user also follows this order.
-
-@defun buffer-list
-This function returns a list of all buffers, including those whose names
-begin with a space.  The elements are actual buffers, not their names.
+buried (see @code{bury-buffer}, below).  Several functions, notably
+@code{other-buffer}, use this ordering.  A buffer list displayed for the
+user also follows this order.
+
+  In addition to the fundamental Emacs buffer list, each frame has its
+own version of the buffer list, in which the buffers that have been
+selected in that frame come first, starting with the buffers most
+recently selected @emph{in that frame}.  (This order is recorded in
+@var{frame}'s @code{buffer-list} frame parameter; see @ref{Window Frame
+Parameters}.)  The buffers that were never selected in @var{frame} come
+afterward, ordered according to the fundamental Emacs buffer list.
+
+@defun buffer-list &optional frame
+This function returns the buffer list, including all buffers, even those
+whose names begin with a space.  The elements are actual buffers, not
+their names.
+
+If @var{frame} is a frame, this returns @var{frame}'s buffer list.  If
+@var{frame} is @code{nil}, the fundamental Emacs buffer list is used:
+all the buffers appear in order of most recent selection, regardless of
+which frames they were selected in.
 
 @example
 @group
@@ -516,20 +708,44 @@ begin with a space.  The elements are actual buffers, not their names.
         "buffer.c" "*Help*" "TAGS")
 @end group
 @end example
-
-This list is a copy of a list used inside Emacs; modifying it has no
-effect on the ordering of buffers.
 @end defun
 
-@defun other-buffer &optional buffer-or-name visible-ok
+  The list that @code{buffer-list} returns is constructed specifically
+by @code{buffer-list}; it is not an internal Emacs data structure, and
+modifying it has no effect on the order of buffers.  If you want to
+change the order of buffers in the frame-independent buffer list, here
+is an easy way:
+
+@example
+(defun reorder-buffer-list (new-list)
+  (while new-list
+    (bury-buffer (car new-list))
+    (setq new-list (cdr new-list))))
+@end example
+
+  With this method, you can specify any order for the list, but there is
+no danger of losing a buffer or adding something that is not a valid
+live buffer.
+
+  To change the order or value of a frame's buffer list, set the frame's
+@code{buffer-list} frame parameter with @code{modify-frame-parameters}
+(@pxref{Parameter Access}).
+
+@defun other-buffer &optional buffer visible-ok frame
 This function returns the first buffer in the buffer list other than
-@var{buffer-or-name}.  Usually this is the buffer most recently shown in
-the selected window, aside from @var{buffer-or-name}.  Buffers whose
-names start with a space are not considered.
+@var{buffer}.  Usually this is the buffer selected most recently (in
+frame @var{frame} or else the currently selected frame), aside from
+@var{buffer}.  Buffers whose names start with a space are not considered
+at all.
 
-If @var{buffer-or-name} is not supplied (or if it is not a buffer),
-then @code{other-buffer} returns the first buffer on the buffer list
-that is not visible in any window in a visible frame.
+If @var{buffer} is not supplied (or if it is not a buffer), then
+@code{other-buffer} returns the first buffer in the selected frame's
+buffer list that is not now visible in any window in a visible frame.
+
+If @var{frame} has a non-@code{nil} @code{buffer-predicate} parameter,
+then @code{other-buffer} uses that predicate to decide which buffers to
+consider.  It calls the predicate once for each buffer, and if the value
+is @code{nil}, that buffer is ignored.  @xref{Window Frame Parameters}.
 
 @c Emacs 19 feature
 If @var{visible-ok} is @code{nil}, @code{other-buffer} avoids returning
@@ -542,18 +758,23 @@ If no suitable buffer exists, the buffer @samp{*scratch*} is returned
 @end defun
 
 @deffn Command bury-buffer &optional buffer-or-name
-This function puts @var{buffer-or-name} at the end of the buffer list
+This function puts @var{buffer-or-name} at the end of the buffer list,
 without changing the order of any of the other buffers on the list.
 This buffer therefore becomes the least desirable candidate for
 @code{other-buffer} to return.
 
-If @var{buffer-or-name} is @code{nil} or omitted, this means to bury
-the current buffer.  In addition, this switches to some other buffer
-(obtained using @code{other-buffer}) in the selected window.  If the
-buffer is displayed in a window other than the selected one, it remains
-there.
+@code{bury-buffer} operates on each frame's @code{buffer-list} parameter
+as well as the frame-independent Emacs buffer list; therefore, the
+buffer that you bury will come last in the value of @code{(buffer-list
+@var{frame})} and in the value of @code{(buffer-list nil)}.
+
+If @var{buffer-or-name} is @code{nil} or omitted, this means to bury the
+current buffer.  In addition, if the buffer is displayed in the selected
+window, this switches to some other buffer (obtained using
+@code{other-buffer}) in the selected window.  But if the buffer is
+displayed in some other window, it remains displayed there.
 
-If you wish to replace a buffer in all the windows that display it, use
+To replace a buffer in all the windows that display it, use
 @code{replace-buffer-in-windows}.  @xref{Buffers and Windows}.
 @end deffn
 
@@ -563,9 +784,9 @@ If you wish to replace a buffer in all the windows that display it, use
 @cindex buffers, creating
 
   This section describes the two primitives for creating buffers.
-@code{get-buffer-create} creates a buffer if it finds no existing
-buffer; @code{generate-new-buffer} always creates a new buffer, and
-gives it a unique name.
+@code{get-buffer-create} creates a buffer if it finds no existing buffer
+with the specified name; @code{generate-new-buffer} always creates a new
+buffer and gives it a unique name.
 
   Other functions you can use to create buffers include
 @code{with-output-to-temp-buffer} (@pxref{Temporary Displays}) and
@@ -587,17 +808,18 @@ An error is signaled if @var{name} is not a string.
 @end group
 @end example
 
-The major mode for the new buffer is set according to the variable
-@code{default-major-mode}.  @xref{Auto Major Mode}.
+The major mode for the new buffer is set to Fundamental mode.  The
+variable @code{default-major-mode} is handled at a higher level.
+@xref{Auto Major Mode}.
 @end defun
 
 @defun generate-new-buffer name
 This function returns a newly created, empty buffer, but does not make
 it current.  If there is no buffer named @var{name}, then that is the
 name of the new buffer.  If that name is in use, this function adds
-suffixes of the form @samp{<@var{n}>} are added to @var{name}, where
-@var{n} is an integer.  It tries successive integers starting with 2
-until it finds an available name.
+suffixes of the form @samp{<@var{n}>} to @var{name}, where @var{n} is an
+integer.  It tries successive integers starting with 2 until it finds an
+available name.
 
 An error is signaled if @var{name} is not a string.
 
@@ -616,8 +838,9 @@ An error is signaled if @var{name} is not a string.
 @end group
 @end example
 
-The major mode for the new buffer is set by the value of
-@code{default-major-mode}.  @xref{Auto Major Mode}.
+The major mode for the new buffer is set to Fundamental mode.  The
+variable @code{default-major-mode} is handled at a higher level.
+@xref{Auto Major Mode}.
 
 See the related function @code{generate-new-buffer-name} in @ref{Buffer
 Names}.
@@ -629,9 +852,9 @@ Names}.
 @cindex buffers, killing
 
   @dfn{Killing a buffer} makes its name unknown to Emacs and makes its
-space available for other use.
+text space available for other use.
 
-  The buffer object for the buffer which has been killed remains in
+  The buffer object for the buffer that has been killed remains in
 existence as long as anything refers to it, but it is specially marked
 so that you cannot make it current or display it.  Killed buffers retain
 their identity, however; two distinct buffers, when killed, remain
@@ -644,6 +867,9 @@ Therefore, when you kill a buffer, you should also take the precautions
 associated with changing the current buffer (unless you happen to know
 that the buffer being killed isn't current).  @xref{Current Buffer}.
 
+  If you kill a buffer that is the base buffer of one or more indirect
+buffers, the indirect buffers are automatically killed as well.
+
   The @code{buffer-name} of a killed buffer is @code{nil}.  You can use
 this feature to test whether a buffer has been killed:
 
@@ -657,9 +883,8 @@ this feature to test whether a buffer has been killed:
 
 @deffn Command kill-buffer buffer-or-name
 This function kills the buffer @var{buffer-or-name}, freeing all its
-memory for use as space for other buffers.  (Emacs version 18 and older
-was unable to return the memory to the operating system.)  It returns
-@code{nil}.
+memory for other uses or to be returned to the operating system.  It
+returns @code{nil}.
 
 Any processes that have this buffer as the @code{process-buffer} are
 sent the @code{SIGHUP} signal, which normally causes them to terminate.
@@ -672,22 +897,6 @@ It does this even if not called interactively.  To prevent the request
 for confirmation, clear the modified flag before calling
 @code{kill-buffer}.  @xref{Buffer Modification}.
 
-@vindex kill-buffer-query-functions
-You can program additional requests for confirmation.  After confirming
-unsaved changes, @code{kill-buffer} calls the functions in the list
-@code{kill-buffer-query-functions}, in order of appearance, with no
-arguments.  The buffer being killed is the current buffer when they are
-called.  The idea is that these functions ask for confirmation from the
-user for various nonstandard reasons.  If any of them returns
-non-@code{nil}, the buffer is not killed.
-
-@c Emacs 19 feature
-@vindex kill-buffer-hook
-Just before actually killing the buffer, after asking all questions,
-@code{kill-buffer} runs the normal hook @code{kill-buffer-hook}.  The
-buffer to be killed is current when the hook functions run.
-@xref{Hooks}.
-
 Killing a buffer that is already dead has no effect.
 
 @smallexample
@@ -703,126 +912,95 @@ Buffer foo.changed modified; kill anyway? (yes or no) @kbd{yes}
 @end smallexample
 @end deffn
 
-@node Current Buffer
-@section The Current Buffer
-@cindex selecting a buffer
-@cindex changing to another buffer
-@cindex current buffer
-
-  There are, in general, many buffers in an Emacs session.  At any time,
-one of them is designated as the @dfn{current buffer}.  This is the
-buffer in which most editing takes place, because most of the primitives
-for examining or changing text in a buffer operate implicitly on the
-current buffer (@pxref{Text}).  Normally the buffer that is displayed on
-the screen in the selected window is the current buffer, but this is not
-always so: a Lisp program can designate any buffer as current
-temporarily in order to operate on its contents, without changing what
-is displayed on the screen.
-
-  The way to designate a current buffer in a Lisp program is by calling
-@code{set-buffer}.  The specified buffer remains current until a new one
-is designated.
-
-  When an editing command returns to the editor command loop, the
-command loop designates the buffer displayed in the selected window as
-current, to prevent confusion: the buffer that the cursor is in, when
-Emacs reads a command, is the one to which the command will apply.
-(@xref{Command Loop}.)  Therefore, @code{set-buffer} is not the way to
-switch visibly to a different buffer so that the user can edit it.  For
-this, you must use the functions described in @ref{Displaying Buffers}.
-
-  However, Lisp functions that change to a different current buffer
-should not depend on the command loop to set it back afterwards.
-Editing commands written in Emacs Lisp can be called from other programs
-as well as from the command loop.  It is convenient for the caller if
-the subroutine does not change which buffer is current (unless, of
-course, that is the subroutine's purpose).  Therefore, you should
-normally use @code{set-buffer} within a @code{save-excursion} that will
-restore the current buffer when your function is done
-(@pxref{Excursions}).  Here is an example, the code for the command
-@code{append-to-buffer} (with the documentation string abridged):
-
-@example
-@group
-(defun append-to-buffer (buffer start end)
-  "Append to specified buffer the text of the region.
-@dots{}"
-  (interactive "BAppend to buffer: \nr")
-  (let ((oldbuf (current-buffer)))
-    (save-excursion
-      (set-buffer (get-buffer-create buffer))
-      (insert-buffer-substring oldbuf start end))))
-@end group
-@end example
-
-@noindent
-This function binds a local variable to the current buffer, and then
-@code{save-excursion} records the values of point, the mark, and the
-original buffer.  Next, @code{set-buffer} makes another buffer current.
-Finally, @code{insert-buffer-substring} copies the string from the
-original current buffer to the new current buffer.
+@defvar kill-buffer-query-functions
+After confirming unsaved changes, @code{kill-buffer} calls the functions
+in the list @code{kill-buffer-query-functions}, in order of appearance,
+with no arguments.  The buffer being killed is the current buffer when
+they are called.  The idea is that these functions ask for confirmation
+from the user for various nonstandard reasons.  If any of them returns
+@code{nil}, @code{kill-buffer} spares the buffer's life.
+@end defvar
 
-  If the buffer appended to happens to be displayed in some window, 
-the next redisplay will show how its text has changed.  Otherwise, you
-will not see the change immediately on the screen.  The buffer becomes
-current temporarily during the execution of the command, but this does
-not cause it to be displayed.
+@defvar kill-buffer-hook
+This is a normal hook run by @code{kill-buffer} after asking all the
+questions it is going to ask, just before actually killing the buffer.
+The buffer to be killed is current when the hook functions run.
+@xref{Hooks}.
+@end defvar
 
-  If you make local bindings (with @code{let} or function arguments) for
-a variable that may also have buffer-local bindings, make sure that the
-same buffer is current at the beginning and at the end of the local
-binding's scope.  Otherwise you might bind it in one buffer and unbind
-it in another!  There are two ways to do this.  In simple cases, you may
-see that nothing ever changes the current buffer within the scope of the
-binding.  Otherwise, use @code{save-excursion} to make sure that the
-buffer current at the beginning is current again whenever the variable
-is unbound.
+@defvar buffer-offer-save
+This variable, if non-@code{nil} in a particular buffer, tells
+@code{save-buffers-kill-emacs} and @code{save-some-buffers} to offer to
+save that buffer, just as they offer to save file-visiting buffers.  The
+variable @code{buffer-offer-save} automatically becomes buffer-local
+when set for any reason.  @xref{Buffer-Local Variables}.
+@end defvar
 
-  It is not reliable to change the current buffer back with
-@code{set-buffer}, because that won't do the job if a quit happens while
-the wrong buffer is current.  Here is what not to do:
+@node Indirect Buffers
+@section Indirect Buffers
+@cindex indirect buffers
+@cindex base buffer
+
+  An @dfn{indirect buffer} shares the text of some other buffer, which
+is called the @dfn{base buffer} of the indirect buffer.  In some ways it
+is the analogue, for buffers, of a symbolic link among files.  The base
+buffer may not itself be an indirect buffer.
+
+  The text of the indirect buffer is always identical to the text of its
+base buffer; changes made by editing either one are visible immediately
+in the other.  This includes the text properties as well as the characters
+themselves.
+
+  But in all other respects, the indirect buffer and its base buffer are
+completely separate.  They have different names, different values of
+point, different narrowing, different markers and overlays (though
+inserting or deleting text in either buffer relocates the markers and
+overlays for both), different major modes, and different buffer-local
+variables.
+
+  An indirect buffer cannot visit a file, but its base buffer can.  If
+you try to save the indirect buffer, that actually works by saving the
+base buffer.
+
+  Killing an indirect buffer has no effect on its base buffer.  Killing
+the base buffer effectively kills the indirect buffer in that it cannot
+ever again be the current buffer.
+
+@deffn Command make-indirect-buffer base-buffer name
+This creates an indirect buffer named @var{name} whose base buffer
+is @var{base-buffer}.  The argument @var{base-buffer} may be a buffer
+or a string.
+If @var{base-buffer} is an indirect buffer, its base buffer is used as
+the base for the new buffer.
+@end deffn
 
-@example
-@group
-(let (buffer-read-only
-      (obuf (current-buffer)))
-  (set-buffer @dots{})
-  @dots{}
-  (set-buffer obuf))
-@end group
-@end example
+@defun buffer-base-buffer buffer
+This function returns the base buffer of @var{buffer}.  If @var{buffer}
+is not indirect, the value is @code{nil}.  Otherwise, the value is
+another buffer, which is never an indirect buffer.
+@end defun
 
-@noindent
-Using  @code{save-excursion}, as shown below, handles quitting, errors
-and @code{throw} as well as ordinary evaluation.
+@node Buffer Gap
+@section The Buffer Gap
 
-@example
-@group
-(let (buffer-read-only)
-  (save-excursion
-    (set-buffer @dots{})
-    @dots{}))
-@end group
-@end example
+  Emacs buffers are implemented using an invisible @dfn{gap} to make
+insertion and deletion faster.  Insertion works by filling in part of
+the gap, and deletion adds to the gap.  Of course, this means that the
+gap must first be moved to the locus of the insertion or deletion.
+Emacs moves the gap only when you try to insert or delete.  This is why
+your first editing command in one part of a large buffer, after
+previously editing in another far-away part, sometimes involves a
+noticeable delay.
 
-@defun current-buffer
-This function returns the current buffer.
+  This mechanism works invisibly, and Lisp code should never be affected
+by the gap's current location, but these functions are available for
+getting information about the gap status.
 
-@example
-@group
-(current-buffer)
-     @result{} #<buffer buffers.texi>
-@end group
-@end example
+@defun gap-position
+This function returns the current gap position in the current buffer.
 @end defun
 
-@defun set-buffer buffer-or-name
-This function makes @var{buffer-or-name} the current buffer.  It does
-not display the buffer in the currently selected window or in any other
-window, so the user cannot necessarily see the buffer.  But Lisp
-programs can in any case work on it.
-
-This function returns the buffer identified by @var{buffer-or-name}.
-An error is signaled if @var{buffer-or-name} does not identify an
-existing buffer.
+@defun gap-size
+This function returns the current gap size of the current buffer.
 @end defun