Spacemacs Useful Buffers Gotcha¶
I’ve ran into a little spacemacs annoyance that took me a while to resolve, and it seems like it could pop up under a variety of different contexts. I figured that a few words about it here might save others some time.
Spacemacs tries to be a better clever when switching buffers, you can observe this cleverness by the following sequence:
SPC b m SPC tab SPC tab
Looks like spacemacs is shy about bringing to the *Messages*
buffer as you
would expect. What gives?
Spacemacs has this concept of useful buffers. And it tries really hard to hide buffers it considers useless out of your sight.
How does it know how to characterize buffers this way? Roughly, a useful buffer is one of the following:
A buffer that inherits from
comint-mode
A buffer whose name matches any of the regexp in
spacemacs-useful-buffers-regexp
A buffer whose name does not match any of the regexp
spacemacs-useful-buffers-regexp
For me, the values of these 2 regexps lists is as follows
ELISP> spacemacs-useful-buffers-regexp
("\\*\\(ansi-term\\|eshell\\|shell\\|terminal.+\\)\\(-[0-9]+\\)?\\*" "\\*scratch\\*")
ELISP> spacemacs-useless-buffers-regexp
("*.+")
In practice this means that any buffer name that is surrounded by *
is
considered useless, unless its in the list of exceptions.
Simple enough, but ends up being too blunt in some situations. In my situation,
this ended up marking all my notmuch buffers as useless. Notmuch buffers are
surrounded by *
but don’t otherwise have a pattern to their name that would
make them recognizable with a regexp. What we’d like to do instead, is to mark
useful buffers by mode. Although spacemacs doesn’t support this, we can hack it
in with a little advice:
(defun spacemacs/notmuch-buffer-p (buffer)
(member (buffer-local-value 'major-mode buffer)
'(notmuch-hello-mode
notmuch-search-mode
notmuch-tree-mode
notmuch-message-mode
notmuch-show-mode)))
(defun spacemacs/notmuch-buffer-p-advice (f &rest args)
(or (apply f args) (spacemacs/notmuch-buffer-p (car args))))
(advice-add 'spacemacs/useful-buffer-p :around
#'spacemacs/notmuch-buffer-p-advice)
Hopefully, in the future, spacemacs will support this use case better. In particular, I’ve suggested changing the list of regexps to a list of predicates. This way we can characterize useful buffers with more than just buffer names.