Emacs: how to display a buffer without switching windows and without raising frames?

I want to display something in a specific emacs buffer. If the buffer does not appear in any window, I want to display it in a new window in the current frame. If it is already displayed somewhere, I do not want to bring this window or frame or focus. Things I tried: (pop-to-buffer buf nil 'norecord) This will not work, because when I am in another frame that displays "buf", it will open a new window. (display-buffer buf nil t) It almost works, but it raises a frame showing the buffer. I want the frame to stay in the background.

How to act? Maybe there is a function to check whether the buffer will already be displayed in some frame in some window?

+4
source share
3 answers

If you understood correctly that the next function will do what you want

(defun my-switch-to-buffer (buffer)
  (interactive
   (list (ido-read-buffer "Switch to buffer: ")))
  ;; get-buffer-window (with second argument t) will return
  ;; nil if the buffer is not open in any window across any
  ;; frame
  (unless (get-buffer-window buffer 0)
    (pop-to-buffer buffer nil t)))
+2
source

What version of Emacs are you using? display-bufferhas been slightly modified in Emacs 24, offering an almost unlimited number of display options. But as a result, the behavior is generally non-trivial, as is the document.

(And pop-to-bufferalways chooses a buffer that you say you don’t want.)

Here is a document for display-bufferfrom a recent Emacs build. See the Elisp manual for more information. Regardless of whether the short answer here gives you enough, you may want to know how to tame the beast display-buffer. (Good luck.)

,----
| display-buffer is an interactive compiled Lisp function in
| `window.el'.
| 
| It is bound to C-x 4 C-o.
| 
| (display-buffer BUFFER-OR-NAME &optional ACTION FRAME)
| 
| Display BUFFER-OR-NAME in some window, without selecting it.
| BUFFER-OR-NAME must be a buffer or the name of an existing
| buffer.  Return the window chosen for displaying BUFFER-OR-NAME,
| or nil if no such window is found.
| 
| Optional argument ACTION, if non-nil, should specify a display
| action.  Its form is described below.
| 
| Optional argument FRAME, if non-nil, acts like an additional
| ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
| specifying the frame(s) to search for a window that is already
| displaying the buffer.  See `display-buffer-reuse-window'
| 
| If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
| where FUNCTION is either a function or a list of functions, and
| ALIST is an arbitrary association list (alist).
| 
| Each such FUNCTION should accept two arguments: the buffer to
| display and an alist.  Based on those arguments, it should
| display the buffer and return the window.  If the caller is
| prepared to handle the case of not displaying the buffer
| and returning nil from `display-buffer' it should pass
| (allow-no-window . t) as an element of the ALIST.
| 
| The `display-buffer' function builds a function list and an alist
| by combining the functions and alists specified in
| `display-buffer-overriding-action', `display-buffer-alist', the
| ACTION argument, `display-buffer-base-action', and
| `display-buffer-fallback-action' (in order).  Then it calls each
| function in the combined function list in turn, passing the
| buffer as the first argument and the combined alist as the second
| argument, until one of the functions returns non-nil.
| 
| If ACTION is nil, the function list and the alist are built using
| only the other variables mentioned above.
| 
| Available action functions include:
|  `display-buffer-same-window'
|  `display-buffer-reuse-window'
|  `display-buffer-pop-up-frame'
|  `display-buffer-pop-up-window'
|  `display-buffer-in-previous-window'
|  `display-buffer-use-some-window'
| 
| Recognized alist entries include:
| 
|  `inhibit-same-window' -- A non-nil value prevents the same
|                           window from being used for display.
| 
|  `inhibit-switch-frame' -- A non-nil value prevents any other
|                            frame from being raised or selected,
|                            even if the window is displayed there.
| 
|  `reusable-frames' -- Value specifies frame(s) to search for a
|                       window that already displays the buffer.
|                       See `display-buffer-reuse-window'.
| 
|  `pop-up-frame-parameters' -- Value specifies an alist of frame
|                               parameters to give a new frame, if
|                               one is created.
| 
|  `window-height' -- Value specifies either an integer (the number
|     of lines of a new window), a floating point number (the
|     fraction of a new window with respect to the height of the
|     frame root window) or a function to be called with one
|     argument - a new window.  The function is supposed to adjust
|     the height of the window; its return value is ignored.
|     Suitable functions are `shrink-window-if-larger-than-buffer'
|     and `fit-window-to-buffer'.
| 
|  `window-width' -- Value specifies either an integer (the number
|     of columns of a new window), a floating point number (the
|     fraction of a new window with respect to the width of the
|     frame root window) or a function to be called with one
|     argument - a new window.  The function is supposed to adjust
|     the width of the window; its return value is ignored.
| 
|  `allow-no-window' -- A non-nil value indicates readiness for the case
|     of not displaying the buffer and FUNCTION can safely return
|     a non-window value to suppress displaying.
| 
| The ACTION argument to `display-buffer' can also have a non-nil
| and non-list value.  This means to display the buffer in a window
| other than the selected one, even if it is already displayed in
| the selected window.  If called interactively with a prefix
| argument, ACTION is t.
`----
+1
source

(: http://www.chemie.fu-berlin.de/chemnet/use/info/emacs/emacs_19.html):

C-x b buffer RET 

To select or create a buffer called buffer (switch-to-buffer).

C-x 4 b buffer RET 

Similarly, but select a buffer in another window (switch-to-buffer-other-window).

C-x 5 b buffer RET 

Similar, but select the buffer in a separate frame (switch-to-buffer-other-frame).

Also interesting about several windows / frames: http://www.chemie.fu-berlin.de/chemnet/use/info/emacs/emacs_20.html#SEC157

0
source

All Articles