The other-buffer
function actually provides a buffer when it is
used as an argument to a function that requires one. We can see this
by using other-buffer
and switch-to-buffer
to switch to a
different buffer.
But first, a brief introduction to the switch-to-buffer
function.
When you switched back and forth from Info to the `*scratch*'
buffer to evaluate (buffer-name)
, you most likely typed C-x
b and then typed `*scratch*' when prompted in the minibuffer for
the name of the buffer to which you wanted to switch. The keystrokes,
C-x b, cause the Lisp interpreter to evaluate the interactive
Emacs Lisp function switch-to-buffer
. As we said before, this is
how Emacs works: different keystrokes call or run different functions.
For example, C-f calls forward-char
, M-e calls
forward-sentence
, and so on.
By writing switch-to-buffer
in an expression, and giving it a
buffer to switch to, we can switch buffers just the way C-x b
does.
Here is the Lisp expression:
(switch-to-buffer (other-buffer))
The symbol switch-to-buffer
is the first element of the list, so
the Lisp interpreter will treat it as a function and carry out the
instructions that are attached to it. But before doing that, the
interpreter will note that other-buffer
is inside parentheses and
work on that symbol first. other-buffer
is the first (and in
this case, the only) element of this list, so the Lisp interpreter calls
or runs the function. It returns another buffer. Next, the interpreter
runs switch-to-buffer
, passing to it, as an argument, the other
buffer, which is what Emacs will switch to. If you are reading this in
Info, try this now. Evaluate the expression. (To get back, type
C-x b RET.)
In the programming examples in later sections of this document, you will
see the function set-buffer
more often than
switch-to-buffer
. This is because of a difference between
computer programs and humans: humans have eyes and expect to see the
buffer on which they are working on their computer terminals. This is
so obvious, it almost goes without saying. However, programs do not
have eyes. When a computer program works on a buffer, that buffer does
not need to be visible on the screen.
switch-to-buffer
is designed for humans and does two different
things: it switches the buffer to which Emacs attention is directed; and
it switches the buffer displayed in the window to the new buffer.
set-buffer
, on the other hand, does only one thing: it switches
the attention of the computer program to a different buffer. The buffer
on the screen remains unchanged (of course, normally nothing happens
there until the command finishes running).
Also, we have just introduced another jargon term, the word call. When you evaluate a list in which the first symbol is a function, you are calling that function. The use of the term comes from the notion of the function as an entity that can do something for you if you `call' it--just as a plumber is an entity who can fix a leak if you call him or her.
Go to the first, previous, next, last section, table of contents.