Emacs在右侧窗口中自动显示事物

时间:2018-06-14 12:01:36

标签: emacs

我希望在Emacs光标提示某个单词时,在右侧窗口中显示一个显示相关代码段的函数。

我们假设我有自己的日志格式,并且我有相应的数据库,我可以找到日志消息来自哪里,然后我将指针放在某个日志消息上。如果是这样,那么我想让Emacs在该日志缓冲区的右侧窗口中打开相应的源文件。 现在,我可以通过自己的数据库和emacs查询并获取源文件的位置。但我仍然不知道如何控制正确的窗口。

如果我打开一个右侧窗口,那么Emacs会再次打开另一个窗口,我不想让它做,但是想让它使用之前的现有窗口。 我怎么能实现这个?请告诉我,或分享你可能有的例子。

感谢。

1 个答案:

答案 0 :(得分:1)

除了使用自定义函数my-display-buffer的以下示例之外,请记住BUFFER可以通过find-file-noselect以外的方式获得;例如,current-buffer如果需要的话。在其他窗口中查找您的位置方面,您可能会发现它对select-windowwith-selected-windowset-window-point等有帮助。如果选择了窗口,目标缓冲区在所述窗口中可见然后,像goto-char这样的简单事物就足以直观地转到特定的位置。示例my-display-buffer函数有一个doc-string,它通常描述了它的设计目的;即,"有三种可能性......"。

向左显示缓冲区

  
(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'left))

向右显示缓冲区:

  
(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'right))

以上显示缓冲区:

  
(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'above))

以下显示缓冲区:

  
(let ((buffer (find-file-noselect "~/foo.py")))
  (with-current-buffer buffer
    (message "major-mode:  %s" major-mode))
  (my-display-buffer buffer nil 'below))
  
(defun my-display-buffer (buffer-or-name alist direction &optional size pixelwise)
"BUFFER:  The buffer that will be displayed.
ALIST:  See the doc-string of `display-buffer' for more information.
DIRECTION:  Must use one of these symbols:  'left 'right 'below 'above
SIZE:  See the doc-string for `split-window'.
PIXELWISE:  See the doc-string for `split-window'.
There are three possibilities:
-  (1) If a window on the frame already displays the target buffer,
then just reuse the same window.
-  (2) If there is already a window in the specified direction in relation
to the selected window, then display the target buffer in said window.
-  (3) If there is no window in the specified direction, then create one
in that direction and display the target buffer in said window."
  (let* ((buffer
           (if (bufferp buffer-or-name)
             buffer-or-name
             (get-buffer buffer-or-name)))
         (window
           (cond
             ((get-buffer-window buffer (selected-frame)))
             ((window-in-direction direction))
             (t
               (split-window (selected-window) size direction pixelwise)))))
    (window--display-buffer buffer window 'window alist display-buffer-mark-dedicated)
    window))