Writing a ELisp function to make man open in another window

97 Views Asked by At

So I know that Man-notify-method controls this, but I like to have the option to open it in the same window. By default, I have it to pushy. Currently, I have:

(global-set-key (kbd "H-m s") 'man)

and I would like to have

(global-set-key (kbd "H-m o") 'man-other-window)

I am very new to Elisp, but I think it should be something as simple as:

(defun man-other-window ()
  "open a `man` in a new window."
  (interactive)
  (let ((Man-notify-method 'friendly))
      (man)))

but I get an error to do with wrong number of arguments, but when I pass an argument to the function, it doesn't work the way I like it. How can I get the behavior so that it would behave as:

M-x man-other-window RET relevant-man-page RET

just as M-x man does?

1

There are 1 best solutions below

1
Y. E. On

If you're using >= Emacs 28.1, as the simplest solution, try using other-window-prefix command, which is bound to C-x 4 4 by default.

Namely:

  • Hit C-x 4 4.
  • See Display next command buffer in a new window... message.
  • Hit M-x man RET <command-name>.
  • Voilà.

The conveniency of other-window-prefix is that it could be used on-the-fly in many situations, without a need to write functions for all the cases you might encounter.

C-h f other-window-prefix now outputs the following docstring:

Display the buffer of the next command in a new window.
The next buffer is the buffer displayed by the next command invoked
immediately after this command (ignoring reading from the minibuffer).
Creates a new window before displaying the buffer.
When ‘switch-to-buffer-obey-display-actions’ is non-nil,
‘switch-to-buffer’ commands are also supported.

For the similar conveniency bindings consult M-: (info "(emacs) Pop Up Window"). Notably, same-window-prefix is bound to C-x 4 1.