要设置与文件名或文件扩展名匹配的特定major-mode,请参阅变量auto-mode-alist。 Emacs 25 支持python-mode 开箱即用的.py 文件扩展名。
启动警告:内置(硬编码)库startup.el 包含一些在启动时显示的缓冲区/窗口的选择。在启动 Emacs 时自定义特定的窗口布局总是有些挑战,并且该方法很可能是为特定用户自定义的。为了获得更好的效果,用户可能希望将窗口组织功能放在.emacs 或init.el 的最底部或使用emacs-startup-hook(在启动过程结束时运行)。某些库,例如desktop.el(桌面恢复)会使启动完成时选择哪个缓冲区的焦点变得复杂。每个用户都需要花一些时间以适合他/她需求的方式组织创业公司。例如,可能有两个窗口,而用户可能希望将焦点放在另一个窗口上——可能只需要自定义文件底部的(other-window 1) 之类的低技术。在下面的my-display-buffer 示例中,返回的值是一个窗口——用户可能希望将最后一行window 包装成(select-window window) 以便它被选中;或者,用户可以在使用该功能时添加(select-window (my-display-buffer BUFFER ALIST DIRECTION)),而不是修改my-display-buffer,而无需在内部修改它。原始发布者也可能有兴趣使用find-file(定位当前窗口)或find-file-other-window(创建/定位另一个窗口)——例如(find-file-other-window "~/foo.py")。
如果焦点已经在所需的窗口中(例如,右侧的窗口已被选中),则只需使用 set-window-buffer 或 switch-to-buffer 之类的内容。
要控制缓冲区在上方、下方、左侧或右侧的显示,请参见以下示例,该示例使用名为 my-display-buffer 的自定义函数,如下所示:
示例用法:my-display-buffer 的函数定义需要出现在.emacs 或init.el 文件中在使用这四个示例 sn 之前 -ps。
(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))