Make emacs next-buffer skip *Messages* buffer

后端 未结 4 977
盖世英雄少女心
盖世英雄少女心 2021-01-02 06:36

I\'d like to make a simple change to Emacs so that the next-buffer and previous-buffer commands (which I have bound to C-x

4条回答
  •  庸人自扰
    2021-01-02 06:47

    The simplest I can think of is defining an advice for both functions. Here it is for next-buffer. Similarly would be for previous-buffer. You can also define a configuration variable to enable/disable the behavior (or activating/deactivating the advice):

    (defadvice next-buffer (after avoid-messages-buffer-in-next-buffer)
      "Advice around `next-buffer' to avoid going into the *Messages* buffer."
      (when (string= "*Messages*" (buffer-name))
        (next-buffer)))
    
    ;; activate the advice
    (ad-activate 'next-buffer)
    

    Maybe you can compare buffers in some other way instead of its string name, but that will work. The code for previous buffer is almost the same. I don't know either if there is a way of calling the original function without triggering the advice once inside the advice itself, but again, the code will work even if the name of the buffer is tested afterwards (will fail if you just have one buffer, and it is the messages buffer; some code can check if there is just one buffer and don't call next-buffer again).

    If you want to use a standalone function that does the same thing:

    (defun my-next-buffer ()
      "next-buffer, only skip *Messages*"
      (interactive)
      (next-buffer)
      (when (string= "*Messages*" (buffer-name))
          (next-buffer)))
    
    (global-set-key [remap next-buffer] 'my-next-buffer)
    (global-set-key [remap previous-buffer] 'my-next-buffer)
    

提交回复
热议问题