Node:Buffer Names, Next:Getting Buffers, Previous:How to Evaluate, Up:Practicing Evaluation
The two functions, buffer-name
and buffer-file-name
, show
the difference between a file and a buffer. When you evaluate the
following expression, (buffer-name)
, the name of the buffer
appears in the echo area. When you evaluate (buffer-file-name)
,
the name of the file to which the buffer refers appears in the echo
area. Usually, the name returned by (buffer-name)
is the same as
the name of the file to which it refers, and the name returned by
(buffer-file-name)
is the full path-name of the file.
A file and a buffer are two different entities. A file is information recorded permanently in the computer (unless you delete it). A buffer, on the other hand, is information inside of Emacs that will vanish at the end of the editing session (or when you kill the buffer). Usually, a buffer contains information that you have copied from a file; we say the buffer is visiting that file. This copy is what you work on and modify. Changes to the buffer do not change the file, until you save the buffer. When you save the buffer, the buffer is copied to the file and is thus saved permanently.
If you are reading this in Info inside of GNU Emacs, you can evaluate each of the following expressions by positioning the cursor after it and typing C-x C-e.
(buffer-name) (buffer-file-name)
When I do this, "introduction.texinfo"
is the value returned by
evaluating (buffer-name)
, and
"/gnu/work/intro/introduction.texinfo"
is the value returned by
evaluating (buffer-file-name)
. The former is the name of the
buffer and the latter is the name of the file. (In the expressions, the
parentheses tell the Lisp interpreter to treat buffer-name
and
buffer-file-name
as functions; without the parentheses, the
interpreter would attempt to evaluate the symbols as variables.
See Variables.)
In spite of the distinction between files and buffers, you will often find that people refer to a file when they mean a buffer and vice-versa. Indeed, most people say, "I am editing a file," rather than saying, "I am editing a buffer which I will soon save to a file." It is almost always clear from context what people mean. When dealing with computer programs, however, it is important to keep the distinction in mind, since the computer is not as smart as a person.
The word `buffer', by the way, comes from the meaning of the word as a cushion that deadens the force of a collision. In early computers, a buffer cushioned the interaction between files and the computer's central processing unit. The drums or tapes that held a file and the central processing unit were pieces of equipment that were very different from each other, working at their own speeds, in spurts. The buffer made it possible for them to work together effectively. Eventually, the buffer grew from being an intermediary, a temporary holding place, to being the place where work is done. This transformation is rather like that of a small seaport that grew into a great city: once it was merely the place where cargo was warehoused temporarily before being loaded onto ships; then it became a business and cultural center in its own right.
Not all buffers are associated with files. For example, when you start
an Emacs session by typing the command emacs
alone, without
naming any files, Emacs will start with the *scratch*
buffer on
the screen. This buffer is not visiting any file. Similarly, a
*Help*
buffer is not associated with any file.
If you switch to the *scratch*
buffer, type (buffer-name)
,
position the cursor after it, and type C-x C-e to evaluate the
expression, the name "*scratch*"
is returned and will appear in
the echo area. "*scratch*"
is the name of the buffer. However,
if you type (buffer-file-name)
in the *scratch*
buffer and
evaluate that, nil
will appear in the echo area. nil
is
from the Latin word for `nothing'; in this case, it means that the
*scratch*
buffer is not associated with any file. (In Lisp,
nil
is also used to mean `false' and is a synonym for the empty
list, ()
.)
Incidentally, if you are in the *scratch*
buffer and want the
value returned by an expression to appear in the *scratch*
buffer itself rather than in the echo area, type C-u C-x C-e
instead of C-x C-e. This causes the value returned to appear
after the expression. The buffer will look like this:
(buffer-name)"*scratch*"
You cannot do this in Info since Info is read-only and it will not allow you to change the contents of the buffer. But you can do this in any buffer you can edit; and when you write code or documentation (such as this book), this feature is very useful.