irrelephant
irrelephant

Reputation: 376

Make buffer-menu open in a new, temporary window

Is there a way to customize the behavior of buffer-menu such that it mimics the list-buffers command?

What I'm really after is making buffer-menu open in a new Emacs window, and after selecting the desired buffer, the buffer-menu window closes, and the previous window switches to the selected buffer.

I've looked at various solutions, such as ibuffer, and I've tried writing Elisp to do it, but I'm having trouble getting the user selection from the menu buffer. Is there a way to get that value? Or, is there a way to wait until the user has made a selection in menu-buffer, either via the keyboard or a mouse click?

Upvotes: 0

Views: 1855

Answers (2)

[The text of this answer was originally written by irrelephant]

I was able to use buffer-menu-other-window to split the frame and get the desired behavior by using the following bindings:

(define-key Buffer-menu-mode-map (kbd "RET") 'Buffer-menu-1-window)
(define-key Buffer-menu-mode-map [mouse-2] 'Buffer-menu-1-window)

Although the above works fine if you're only using one window, it breaks when you try it and you're already using a split window. In that case, it takes some further tweaking:

(defun my-buffer-menu (arg)
  (interactive "P")
  (split-window-below)
  (other-window 1)
  (buffer-menu))

(defun my-buffer-menu-1-window ()
  (interactive)
  (let ((target-buffer (Buffer-menu-buffer t)))
    (delete-window)
    (switch-to-buffer target-buffer)))

(define-key Buffer-menu-mode-map (kbd "RET") 'my-buffer-menu-1-window)
(define-key Buffer-menu-mode-map [mouse-2] 'my-buffer-menu-1-window)

Upvotes: 1

Jérôme Radix
Jérôme Radix

Reputation: 10533

Is there a way to customize 'buffer-menu'

All Emacs extension and customization mechanisms could be used. For example you could define a function called buffer-menu-other-frame on the same basis as it exists a buffer-menu-other-window :

(defun buffer-menu-other-frame (&optional arg)
  "Display the Buffer Menu in another frame.
See `buffer-menu' for a description of the Buffer Menu.

By default, all buffers are listed except those whose names start
with a space (which are for internal use).  With prefix argument
ARG, show only buffers that are visiting files."
  (interactive "P")
  (switch-to-buffer-other-frame (list-buffers-noselect arg))
  (message
   "Commands: d, s, x, u; f, o, 1, 2, m, v; ~, %%; q to quit; ? for help."))

Then you could define a macro to do what you want when you press RET. Then associate this macro to RET in buffer-menu-mode.

Upvotes: 0

Related Questions