[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: display-buffer-alist simplifications

From: Chong Yidong
Subject: Re: display-buffer-alist simplifications
Date: Wed, 31 Aug 2011 22:04:57 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

martin rudalics <address@hidden> writes:

> I don't know.  If we do so, then the action list should be probably also
> made an argument of the switch-to-* family of functions which, IIUC,
> according to Stefan should remain callable from programs.
> I wouldn't have included the pop-to-* functions in the first place but
> there are already too many callers around.  So please rewrite them the
> way you consider most suitable.

OK, I understand better now.

The pop-to-* function should NOT be "action functions" (in the sense of
display-buffer-alist), because they select the window in addition to
displaying it.  But, they should make use of action functions.

TRT is to define two functions, `display-buffer-same-window' and
`display-buffer-other-window', which serve as action functions, which
handle the window selection/display part.  Then the implementation of
the pop-to-* functions is very simple: it consists choosing a particular
order for two action functions, or omitting one of them; and then
selecting the window.

This involves a simple re-factoring of your code:

(defun display-buffer-same-window (buffer alist)
  "Display BUFFER in the selected window, and return the window.
If BUFFER cannot be displayed in the selected window (usually
because it is dedicated to another buffer), return nil."
  (setq buffer (window-normalize-buffer-to-display buffer))
  (let ((norecord (cadr (assq 'norecord alist))))
     ((eq buffer (window-buffer))
     ((not (or (window-minibuffer-p) (window-dedicated-p)))
      (set-window-buffer nil buffer)

(defun display-buffer-other-window (buffer alist)
  "Display BUFFER in another window, and return BUFFER.
If BUFFER cannot be displayed in another window, just return nil."
  (display-buffer-default buffer t))

(defun pop-to-buffer (buffer-or-name &optional other-window norecord)
  (interactive "BPop to buffer:\nP")
  (if other-window
      (let ((same-window-buffer-names nil)
            (same-window-regexps nil))
        (pop-to-buffer-1 buffer-or-name
    (pop-to-buffer-1 buffer-or-name

(defun pop-to-buffer-same-window (&optional buffer-or-name norecord)
  (interactive "BPop to buffer in selected window:\nP")
  (pop-to-buffer-1 buffer-or-name

(defun pop-to-buffer-other-window (&optional buffer-or-name norecord)
  (interactive "BPop to buffer in another window:\nP")
  (let ((pop-up-windows t)
        (same-window-buffer-names nil)
        (same-window-regexps nil))
    (pop-to-buffer-1 buffer-or-name

(defun pop-to-buffer-other-frame (&optional buffer-or-name norecord)
  (interactive "BPop to buffer on another frame:\nP")
  (let ((pop-up-frames t)
        (same-window-buffer-names nil)
        (same-window-regexps nil))
    (pop-to-buffer-1 buffer-or-name

(defun pop-to-buffer-1 (buffer-or-name action norecord)
  (let ((buffer (window-normalize-buffer-to-display
                 ;; BUFFER-OR-NAME nil means another buffer.
                 (or buffer-or-name
                     (other-buffer (current-buffer))))))
    (set-buffer buffer)
    (let* ((old-window (selected-window))
           (old-frame (selected-frame))
           (window (display-buffer buffer action))
           (frame (window-frame window)))
      (if (eq frame old-frame)
          ;; Make sure new window gets selected (Bug#8615), (Bug#6954).
          (select-window window norecord)
        ;; `display-buffer' has chosen another frame, make sure it gets
        ;; input focus and is risen.
        (select-frame-set-input-focus frame norecord))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]