Search code examples
emacselisp

&optional BUFFER for other-buffer function does not work?


When I need to switch to another buffer I have a key binding that will create a buffer called "*Buffer List*" from which I select the new buffer.

I need a command to switch between current buffer and the previous one. Except I don't want "*Buffer List*". I looked up function definition for other-buffer:

(other-buffer &optional BUFFER VISIBLE-OK FRAME)

Return most recently selected buffer other than BUFFER...

so I tried using:

(other-buffer "*Buffer List*")

Now if in the new buffer I execute above code with C-x C-e it will echo "*Buffer List*", instead of the initial buffer that I called "*Buffer List*" from. So &optional BUFFER does not seem to work the way I do it. Can anyone explain why?


Solution

  • BUFFER must be a buffer object. To use the buffer name you would need to use:

    (other-buffer (get-buffer "*Buffer List*"))
    

    Notice that when you can use a buffer's name as the argument, the documentation tends to name that argument BUFFER-OR-NAME (e.g. see the docstring for get-buffer itself), and to be explicit about the fact in the text.

    If you see BUFFER as an argument, it likely requires the actual buffer object (as in this instance).