eintr: Switching Buffers

 
 2.3 Switching Buffers
 =====================
 
 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*’(1) 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 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:
 
      (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>’.)(2)
 
    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’s 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.
 
    ---------- Footnotes ----------
 
    (1) Or rather, to save typing, you probably only typed ‘RET’ if the
 default buffer was ‘*scratch*’, or if it was different, then you typed
 just part of the name, such as ‘*sc’, pressed your ‘TAB’ key to cause it
 to expand to the full name, and then typed ‘RET’.
 
    (2) Remember, this expression will move you to your most recent other
 buffer that you cannot see.  If you really want to go to your most
 recently selected buffer, even if you can still see it, you need to
 evaluate the following more complex expression:
 
      (switch-to-buffer (other-buffer (current-buffer) t))
 
    In this case, the first argument to ‘other-buffer’ tells it which
 buffer to skip—the current one—and the second argument tells
 ‘other-buffer’ it is OK to switch to a visible buffer.  In regular use,
 ‘switch-to-buffer’ takes you to a buffer not visible in windows since
 you would most likely use ‘C-x o’ (‘other-window’) to go to another
 visible buffer.