2

I have some code in my .emacs that prevents it from showing some pre-defined special buffers when I'm using previous-buffer and next-buffer:

(defadvice next-buffer (after avoid-messages-buffer-in-next-buffer)
  (when (string= "*Messages*" (buffer-name)) (next-buffer))
  (when (string= "*Completions*" (buffer-name)) (next-buffer))
  (when (string-match "\*tramp.*\*" (buffer-name)) (previous-buffer))
   ... and so on)

This works great, but I'd like to have the same functionality for C-x k. So far, I've tried writing the same kind of function (defadvice (after kill-buffer) ...), but that doesn't seem to work.

How do I go about doing this?

2

This should work:

(defvar no-kill-buffers
  '("*Messages*"
    ;; ...
    ))

(defadvice kill-buffer (around no-kill-some-buffers activate)
  (unless (member (buffer-name) no-kill-buffers)
    ad-do-it))

The reason your advice didn't work is that it was "after" advice, meaning it didn't run until after the normal kill-buffer logic had completed. (That's the after in (after avoid-message-buffer-in-next-buffer).

around advice let's you put custom logic either before or after the advised command and even control whether it runs at all. The ad-do-it symbol is what tells it if and when to run the normal kill-buffer routine.

Edit:

Having re-read your question I think I may have misunderstood it. If you're looking to skip a special buffer that would have been displayed after killing a buffer then your approach is basically correct. Have you activated the advice?

You can either evaluate (ad-activate 'avoid-messages-buffer-in-next-buffer) or include activate at the end of the argument list to defadvice as I did in my example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.