There's no need to advise functions. M-x info-apropos buffer-list
gives us a link to this info node: The Buffer List.
This paragraph in particular offers a solution:
To change the order or value of a specific frame’s buffer list, set
that frame’s ‘buffer-list’ parameter with ‘modify-frame-parameters’.
There isn't a with-frame-parameters
as far as I know, but we can monkey-wrench one pretty easily:
(defmacro clean-buffer-list (&rest body)
"Remove all entries from the current frame's buffer-list parameter
that match the regex, \" ?\\\\\\*\"; update the parameter, and then
execute BODY in a `progn'. Note that this permanently modifies the
frame's buffer-list as a side effect."
(declare (indent defun)
(debug (body)))
`(let ((buffers ',(cl-loop for x in (frame-parameter nil 'buffer-list)
unless (string-match (rx (: (? " ") "*")) (buffer-name x))
collect x into rs
finally return rs)))
(set-frame-parameter nil 'buffer-list buffers)
(progn ,@body)))
I tested it like this:
M-x buffer-menu <RET>
(other-buffer) ; returns "*Buffer List*"
(clean-buffer-list (other-buffer)) ; returns "a.el"
The *-buffers still live on in the fundamental buffer-list, but I'm not sure how to access them, and I think that's beyond the scope of this question. The linked info page should give you some pointers if that's what you need. The macro right now might also cause some wonky behaviour if you're calling it from within a *-buffer; I haven't tested that extensively.
next-buffer
, for example. And you say, "in many commands". Please clarify what you mean.buffer-list
returns.buffer-list
, and this is what I mean by "many commands". I am trying to avoid advising each and every function which utilizesbuffer-list
. And it doesn't seem likebuffer-list
itself can be advised. This is the reason for my asking my question here.kill-buffer
. After killing, it tries to present the value of(other-buffer)
. However, that function is defined in the emacs source file,buffer.c
, and it doesn't seem to be overridable via advice.