emacs:如何显示缓冲区而不切换窗口而不提升帧?

时间:2014-02-22 13:47:54

标签: emacs elisp

我想在特定的emacs缓冲区中显示某些内容。如果缓冲区尚未显示在某个窗口中,我想在当前帧的新窗口中显示它。如果它已经显示在某个地方,我不想把这个窗口或框架抬起或聚焦。我尝试过的事情: (pop-to-buffer buf nil 'norecord) 这不起作用,因为当我与显示“buf”的框架不同时,它将打开一个新窗口。 (display-buffer buf nil t) 这几乎可以工作,但它会提升显示缓冲区的帧。我希望框架保持在后台。

如何进行?也许有一个函数来检查缓冲区是否已经在某些窗口的某些框架中显示了?

3 个答案:

答案 0 :(得分:3)

如果我理解你的问题,那么以下功能就能满足您的需求

(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)))

答案 1 :(得分:1)

您使用的是什么Emacs版本?在Emacs 24中,display-buffer发生了很大的变化,提供了几乎无限的显示可能性。但结果是行为通常是非平凡的,同样也是文档。

pop-to-buffer总是选择你不想要的缓冲区。)

以下是最近Emacs版本中display-buffer的文档。有关更多信息,请参阅Elisp手册。无论某人是否在这里给你一个简短的答案,你可能想学习如何驯服display-buffer野兽。 (祝你好运。)

,----
| 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's 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's 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.
`----

答案 2 :(得分:0)

您可以尝试(来源:http://www.chemie.fu-berlin.de/chemnet/use/info/emacs/emacs_19.html):

C-x b buffer RET 

选择或创建名为buffer(switch-to-buffer)的缓冲区。

C-x 4 b buffer RET 

类似,但在另一个窗口中选择缓冲区(切换到缓冲区 - 其他窗口)。

C-x 5 b buffer RET 

类似,但在单独的帧中选择缓冲区(switch-to-buffer-other-frame)。

多个窗口/框架也很有趣:http://www.chemie.fu-berlin.de/chemnet/use/info/emacs/emacs_20.html#SEC157