在启动时拆分窗口时,如何在右侧打开文件?

时间:2016-12-12 06:43:59

标签: emacs window startup

如何在分割窗口的右侧打开与主要模式(在本例中为python,因此它为foo.py)相关联的文件foo(并且窗口仅为此分割主要模式)?以下代码根本不起作用(它在两侧显示 scratch 缓冲区。)

(defun my-eval-after-load-python()
  (split-window-horizontally)
)
(eval-after-load "python" '(my-eval-after-load-python))

1 个答案:

答案 0 :(得分:1)

要设置与文件名或文件扩展名匹配的特定major-mode,请参阅变量auto-mode-alist。 Emacs 25支持python-mode .py文件扩展名为开箱即用。

警告重新启动:内置(硬编码)库startup.el包含一些启动时显示的缓冲区/窗口选择。在启动Emacs时自定义特定窗口布局总是有些挑战,并且该方法很可能是针对特定用户的自定义。为了获得更好的结果,用户可能希望将窗口组织功能放在.emacsinit.el的最底部,或使用emacs-startup-hook(在启动过程结束时运行) 。某些库(例如desktop.el(桌面还原)将使启动完成时哪个缓冲区应该具有焦点的选择变得复杂。每个用户都需要花一些时间以适合他/她需要的方式组织创业。例如,可能有两个窗口,用户可能希望将焦点放在另一个窗口中 - 自定义文件底部的(other-window 1)等技术可能就是所需要的。在下面带有my-display-buffer的示例中,返回的值是一个窗口 - 用户可能希望将最后一行window包裹起来(select-window window),以便它被选中;或者,用户可以在使用函数时添加my-display-buffer而不是修改(select-window (my-display-buffer BUFFER ALIST DIRECTION)),而无需在内部修改它。原始海报可能也有兴趣使用find-file(定位当前窗口)或find-file-other-window(创建/定位其他窗口) - 例如(find-file-other-window "~/foo.py")

如果焦点已经在所需的窗口中(例如,已经选择了右侧的窗口),那么只需使用set-window-bufferswitch-to-buffer之类的内容。

要控制上方,下方,左侧或右侧缓冲区的显示,请参阅以下示例,该示例使用下文中名为my-display-buffer的自定义函数:

示例使用my-display-buffer的功能定义需要在之前的.emacsinit.el文件中显示这四个样本片段中的任何一个。

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