window启动时拆分,如何打开右边的文件?
When window is split on startup, how to open the file on the right side?
如何打开文件 foo,与主要模式相关联(在本例中为 python,因此它是 foo.py),位于拆分 window 的右侧(并且 window 仅针对此主要模式拆分)?以下代码根本不起作用(它在两侧显示 scratch 缓冲区)。
(defun my-eval-after-load-python()
(split-window-horizontally)
)
(eval-after-load "python" '(my-eval-after-load-python))
要设置与文件名或文件扩展名匹配的特定 major-mode
,请参阅变量 auto-mode-alist
。 Emacs 25 支持 python-mode
.py
开箱即用的文件扩展名。
启动注意事项:内置(硬编码)库 startup.el
包含启动时显示的一些 buffers/windows 选项。在启动 Emacs 时自定义特定的 window 布局总是有点挑战,而且该方法很可能是针对特定用户自定义的。为了获得更好的结果,用户可能希望将 window 组织函数放在 .emacs
或 init.el
的最底部,或者使用 emacs-startup-hook
(运行到启动过程)。某些库,如 desktop.el
(桌面还原)将使启动完成时选择哪个缓冲区应该具有焦点变得复杂。每个用户都需要投入一些时间以适合 his/her 需要的方式组织初创公司。例如,可能有两个 windows 并且用户可能希望关注另一个 - 定制文件底部的诸如 (other-window 1)
之类的低技术可能就是所需要的。在下面使用 my-display-buffer
的示例中,返回的值是 window —— 用户可能希望将最后一行 window
像这样 (select-window window)
换行以便它被选中;或者,用户可以在使用函数时添加 (select-window (my-display-buffer BUFFER ALIST DIRECTION))
而不是修改 my-display-buffer
而无需在内部修改它。原始发布者也可能对使用 find-file
(针对当前 window)或 find-file-other-window
(针对 create/target 另一个 window)感兴趣——例如, (find-file-other-window "~/foo.py")
.
如果焦点已经在所需的 window 中(例如,右侧的 window 已被选中),则只需使用 set-window-buffer
或 switch-to-buffer
之类的东西.
要控制缓冲区在上方、下方、左侧或右侧的显示,请参阅以下示例,该示例使用了一个名为 my-display-buffer
的自定义函数,如下所示:
用法示例:my-display-buffer
的函数定义需要出现在 .emacs
或 init.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))
如何打开文件 foo,与主要模式相关联(在本例中为 python,因此它是 foo.py),位于拆分 window 的右侧(并且 window 仅针对此主要模式拆分)?以下代码根本不起作用(它在两侧显示 scratch 缓冲区)。
(defun my-eval-after-load-python()
(split-window-horizontally)
)
(eval-after-load "python" '(my-eval-after-load-python))
要设置与文件名或文件扩展名匹配的特定 major-mode
,请参阅变量 auto-mode-alist
。 Emacs 25 支持 python-mode
.py
开箱即用的文件扩展名。
启动注意事项:内置(硬编码)库 startup.el
包含启动时显示的一些 buffers/windows 选项。在启动 Emacs 时自定义特定的 window 布局总是有点挑战,而且该方法很可能是针对特定用户自定义的。为了获得更好的结果,用户可能希望将 window 组织函数放在 .emacs
或 init.el
的最底部,或者使用 emacs-startup-hook
(运行到启动过程)。某些库,如 desktop.el
(桌面还原)将使启动完成时选择哪个缓冲区应该具有焦点变得复杂。每个用户都需要投入一些时间以适合 his/her 需要的方式组织初创公司。例如,可能有两个 windows 并且用户可能希望关注另一个 - 定制文件底部的诸如 (other-window 1)
之类的低技术可能就是所需要的。在下面使用 my-display-buffer
的示例中,返回的值是 window —— 用户可能希望将最后一行 window
像这样 (select-window window)
换行以便它被选中;或者,用户可以在使用函数时添加 (select-window (my-display-buffer BUFFER ALIST DIRECTION))
而不是修改 my-display-buffer
而无需在内部修改它。原始发布者也可能对使用 find-file
(针对当前 window)或 find-file-other-window
(针对 create/target 另一个 window)感兴趣——例如, (find-file-other-window "~/foo.py")
.
如果焦点已经在所需的 window 中(例如,右侧的 window 已被选中),则只需使用 set-window-buffer
或 switch-to-buffer
之类的东西.
要控制缓冲区在上方、下方、左侧或右侧的显示,请参阅以下示例,该示例使用了一个名为 my-display-buffer
的自定义函数,如下所示:
用法示例:my-display-buffer
的函数定义需要出现在 .emacs
或 init.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))