本文介绍了启动时分割窗口时,如何在右侧打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何在拆分窗口的右侧打开与主要模式关联的文件foo(在本例中为python,所以它是foo.py)(并且仅针对该主要模式对窗口进行了拆分) ?下面的代码根本不起作用(它在两侧都显示 scratch 缓冲区)。 (defun my-eval-after-load-python()(水平分割窗口))(eval-after-load python'(my-eval-after -load-python)) 解决方案用于设置特定的主要模式与文件名或文件扩展名匹配,请参见变量 auto-mode-alist 。 Emacs 25开箱即用地为 .py 文件扩展名提供 python-mode 。 重新启动提示:内置的(硬编码)库 startup.el 包含一些选择启动时显示的缓冲区/窗口数量。在启动Emacs时自定义特定的窗口布局总是有些困难,并且该方法很可能是针对特定用户定制的。为了获得更好的结果,用户可能希望将窗口组织功能放在 .emacs 或 init.el 或使用 emacs-startup-hook (在启动过程结束时运行)。某些库,例如 desktop.el (桌面还原),将使启动完成后哪个缓冲区应具有焦点的选择复杂化。每个用户都需要花费一些时间来组织适合自己需求的启动公司。例如,可能有两个窗口,并且用户可能希望将焦点集中在另一个窗口上-诸如(other-window 1)之类的高科技定制文件可能就足够了。在下面带有 my-display-buffer 的示例中,返回的值是一个窗口-用户可能希望包装最后一行 window 像这样的(选择窗口窗口),以便将其选中;或者,用户可以添加(select-window(my-display-buffer BUFFER ALIST DIRECTION))而不是修改 my-display-buffer / code>使用该函数而不在内部对其进行修改。原始张贴者也可能对使用查找文件(以当前窗口为目标)或 find-file-other-window (用于创建/定位另一个窗口)-例如,(查找文件-其他窗口〜/ foo.py)。 如果焦点已位于所需的窗口中(例如,右侧的窗口已被选中),则只需使用 set-window-buffer 或切换到缓冲区。 要控制缓冲区的显示,在上方,下方,左或右,请参见下面的示例,该示例在下文中使用名为 my-display-buffer 的自定义函数: 示例用法: my-display-buffer 的功能定义需要出现在 .emacs 或 init.el 文件在之前使用这四个示例代码片段中的任何一个。 (let(((buffer(find-file-noselect〜/ foo.py))))(with -current-buffer buffer (消息 major-mode:%s major-mode))(my-display-buffer buffer nil'left)) 或 (让(((buffer(find-file-noselect〜/ foo.py)))))(with-current-buffer buffer (message major-mode :%s主要模式))(我的显示缓冲区缓冲区nil'右)) 或 (let((buffer(find -file-noselect〜/ foo.py)))(带有当前缓冲区缓冲区(消息 major-mode:%s major-mode))(我-display-buffer buffer nil'上面)) 或 (let((buffer(find-file-noselect〜/ foo.py)))(带当前缓冲区缓冲区(消息主要模式:%s主要模式))(我的显示缓冲区缓冲区nil'在下面)) 示例函数: (defun my-display-buffer(缓冲区alist方向和可选大小,像素方向) BUFFER:The将显示的缓冲区。 ALIST:有关更多信息,请参见 display-buffer的文档字符串。 方向:必须使用以下符号之一:尺寸上方的左,右,以下:请参阅文档字符串中的分割窗口。 PIXELWISE:请参阅拆分窗口的文档字符串。 有三种可能性:-(1)如果框架上的一个窗口已经显示了目标缓冲区,则只需重用同一窗口。 -(2)如果已经存在一个沿指定方向的窗口,该窗口与所选窗口的关系为,则在该窗口中显示目标缓冲区。 -(3)如果在指定方向上没有窗口,则在该方向上创建一个,并在所述窗口中显示目标缓冲区。 (let((window (cond ((get-buffer-window缓冲区(selected-frame)))((window-in-direction方向))(t (分割窗口(selected-window)大小方向按像素方向)))))(window--display-buffer缓冲窗口'window alist display-buffer-mark-dicated) window)) How do I open a file foo, associated with a major mode (in this case python, so it's a foo.py), on the right side of a split window (and the window is split only for this major mode)? The following code doesn't work at all (it displays the scratch buffer on both sides).(defun my-eval-after-load-python() (split-window-horizontally))(eval-after-load "python" '(my-eval-after-load-python)) 解决方案 For setting up a particular major-mode that matches a file name or file extension, see the variable auto-mode-alist. Emacs 25 supports python-mode for .py file extensions out-of-the-box.Caveat re Startup: The built-in (hard-coded) library startup.el contains some selection of buffers/windows that are displayed on startup. Customizing a particular window layout upon starting Emacs is always somewhat of a challenge, and the method will most likely be custom to a particular user. For better results, the user may wish to place the window organization functions at the very bottom of the .emacs or init.el or use the emacs-startup-hook (which runs towards the end of the startup process). Certain libraries such as desktop.el (desktop restore) will complicate the selection of which buffer should have focus when startup finishes. Each user will need to invest some time organizing the startup in a way that suits his/her needs. For example, there may be two windows and the user may want to have focus in the other -- something lo-tech like (other-window 1) at the bottom of the customization file might be all that is needed. In the example below with my-display-buffer, the value returned is a window -- the user may wish to wrap the last line window like this (select-window window) so that it gets selected; or, instead of modifying my-display-buffer, the user could add (select-window (my-display-buffer BUFFER ALIST DIRECTION)) when using the function without modifying it internally. The original poster may also be interested in using find-file (to target the current window), or find-file-other-window (to create/target another window) -- e.g., (find-file-other-window "~/foo.py").If focus is already in the desired window (e.g., the window on the right is already selected), then just use something like set-window-buffer or switch-to-buffer.To control display of a buffer above, below, left or right, see the following example that uses a custom function called my-display-buffer hereinbelow:Sample Usage: The function definition of my-display-buffer would need to appear in the .emacs or init.el file prior to using any of these four sample snippets.(let ((buffer (find-file-noselect "~/foo.py"))) (with-current-buffer buffer (message "major-mode: %s" major-mode)) (my-display-buffer buffer nil 'left))or(let ((buffer (find-file-noselect "~/foo.py"))) (with-current-buffer buffer (message "major-mode: %s" major-mode)) (my-display-buffer buffer nil 'right))or(let ((buffer (find-file-noselect "~/foo.py"))) (with-current-buffer buffer (message "major-mode: %s" major-mode)) (my-display-buffer buffer nil 'above))or(let ((buffer (find-file-noselect "~/foo.py"))) (with-current-buffer buffer (message "major-mode: %s" major-mode)) (my-display-buffer buffer nil 'below))Example Function:(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 'aboveSIZE: 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 relationto the selected window, then display the target buffer in said window.- (3) If there is no window in the specified direction, then create onein 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)) 这篇关于启动时分割窗口时,如何在右侧打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 20:07