1

Is it possible to ADD commands to the "Buffers" menu? I'd like to add menu entries for:

  • Open new window below current buffer
  • Split window
  • Close current buffer

...and maybe more. I'd want to add them after the existing:

  • Next Buffer
  • Previous Buffer
  • Select Named Buffer...
  • List All Buffers

Is that possible? If so, how?

2

Even if the automatic generation of the buffer menu is a bit complicated the method to add menu items to the buffer menu is quite simple. Just add them with easy-menu-add-item after menu-mode is loaded. The general scheme is:

(eval-after-load "menu-bar"
  (lambda ()
    (easy-menu-add-item nil '("Buffers") ITEM)
    ...))

where ITEM stands for the item you want to add (see the doc for easy-menu-define for the syntax of ITEM).

The interpretation of the menu entries you specified in your question is a bit difficult. I interpret these entries as follows:

  • Open new window below current buffer: Split the selected window, select the lower window of the split, and find a file in that window.
  • Split window: This is pretty clear: split-window
  • Close current buffer: kill-buffer

The following lisp code demonstrates adding menu items to the buffer menu according to your specification (as I interpret it).

You can copy-paste the code into your init file.

(require 'easymenu)

(declare-function find-file-window-below-selected "ext: ")

(eval-after-load "menu-bar"
  (lambda ()
    (easy-menu-add-item nil '("Buffers") ["Find file below selected window" find-file-window-below-selected t])
    (easy-menu-add-item nil '("Buffers") ["Split window" split-window t])
    (easy-menu-add-item nil '("Buffers") ["Kill buffer" kill-buffer t])))

(defun find-file-window-below-selected (filename &optional wildcards)
  "Like `find-file' with args FILENAME and WILDCARDS.
Split window before vertically and display the new file buffer in the
lower window."
  (interactive (find-file-read-args "Find file in new window: "
                    (confirm-nonexistent-file-or-buffer)))
  (select-window (split-window-below))
  (find-file filename wildcards))

Last but not least a short motivation why this method works:

The buffer menu commands are listed in the variable menu-bar-buffers-menu-command-entries. That list is populated by the function menu-bar-update-buffers if it is not set yet. Afterwards menu-bar-buffers-menu-command-entries is appended to global-buffers-menu-map by menu-bar-update-buffers.

Thereby menu-bar-buffers-menu-command-entries is not copied but just appended. Therefore items which you define via easy-menu-add-item at the end of global-buffers-menu-map end up in menu-bar-buffers-menu-command-entries. The value of menu-bar-buffers-menu-command-entries is set when menu-bar-update-buffers is called at loading menu-bar.el[c]?. It is not reset afterwards and always appended to the automatically generated buffer menu.

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.