I’ve tried to write a simple test (kill-buffer-test.el):
:; exec emacs -Q --script "$0" -- "$@"
(setq debug-on-error t)
(setq n 0)
(dolist (idx (buffer-list))
(message "%d '%s' %s" n idx (buffer-list))
(setq n (+ 1 n))
(message "result: %s cb='%s'\n" (kill-buffer idx) (current-buffer)))
And run it:
$ ./kill-buffer-test.el
0 '*scratch*' (*scratch* *Minibuf-0* *Messages* *code-conversion-work* *load*)
result: t cb='*Messages*'
1 ' *Minibuf-0*' ( *Minibuf-0* *Messages* *code-conversion-work* *load*)
result: nil cb='*Messages*'
2 '*Messages*' ( *Minibuf-0* *Messages* *code-conversion-work* *load*)
result: t cb='*scratch*'
3 ' *code-conversion-work*' ( *Minibuf-0* *code-conversion-work* *load* *scratch* *Messages*)
result: t cb='*scratch*'
4 ' *load*' ( *Minibuf-0* *load* *scratch* *Messages*)
result: t cb='*scratch*'
Selecting deleted buffer
$ echo $?
255
Notice that *Minibuf-0* buffer wasn’t killed (why?), *scratch* was resurrected, and emacs exited with a strange error message.
So, should I worry that (current-buffer) may fail? And if I should, how to test that? (current-buffer) will raise an error or will just return nil?
M-x version
GNU Emacs 24.3.50.1 (i686-pc-linux-gnu, X toolkit) of 2013-02-08 on linux.9bf016
No need to worry:
(current-buffer)can’t fail (Emacs would crash and burn if it ever did).*scratch*as well as a few other internal buffers get re-generated as/when needed.