I want to achieve the following when a buffer is displayed in GNU Emacs:

  1. it should never split my windows;

  2. if the buffer is already opened in a window it should reuse it, otherwise it should use the current window.


Partial solution

  • setting pop-up-windows to nil will solve the point 1;

  • setting same-window-regexps to '("^\*") will solve the second part of the point 2, but it's in contrast with the former.

link|improve this question

46% accept rate
feedback

2 Answers

up vote 2 down vote accepted

I found that is possible to customize the behavior of the function display-buffer by setting the variable display-buffer-function.

This function seems to do what I want (surely it needs some cleanup but it works):

(defun my-display-buffer (BUFFER-OR-NAME &optional NOT-THIS-WINDOW)
  (catch 'return
    ;; search the buffer in the existing windows
    (let ((windows (window-list)))
      (while (first windows)
        ;; found
        (if (eq (window-buffer (first windows)) BUFFER-OR-NAME)
            (throw 'return
                   ;; if minibuffer doesn't select the window
                   (if (eq (selected-window) (minibuffer-window))
                       (first windows)
                     (select-window (first windows)))))
        ;; next window
        (setq windows (rest windows))))
    ;; otherwise use current
    (if (eq (selected-window) (minibuffer-window))
        ;; if minubuffer
        (progn
          (select-window (minibuffer-selected-window))
          (switch-to-buffer BUFFER-OR-NAME)
          (select-window (minibuffer-window)))
      ;; if regular window
      (progn
        (switch-to-buffer BUFFER-OR-NAME)
        (selected-window)))))
link|improve this answer
feedback
  1. (setq pop-up-windows nil) seems to do what you want.
  2. I don't think you should have to do much here. At least, the help commands already exhibit this behavior.
link|improve this answer
You're right, it seems that switch-to-buffer-other-window is used pretty deep in the internals of the help code. You could try advising switch-to-buffer-other-window to use switch-to-buffer for certain contexts. That should give you a start. – Joseph Gay Feb 11 '11 at 20:07
Thanks for the tip, I'll look in that direction, but note *Help* it's just an example. – cYrus Feb 11 '11 at 20:45
That's true, but since switch-to-buffer-other-window is likely used in most cases where you see the undesired behavior, advising it will modify all those cases, conditionally if you like. – Joseph Gay Feb 13 '11 at 2:12
It seems that customizing the variable display-buffer-function will do the trick, see my own answer. – cYrus Feb 13 '11 at 17:36
Good find there. That is definitely preferable to my suggestion to use advise. – Joseph Gay Feb 13 '11 at 23:35
feedback

Your Answer

 
or
required, but never shown

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