【问题标题】:When window is split on startup, how to open the file on the right side?启动时拆分窗口时,如何打开右侧的文件?
【发布时间】:2017-04-26 23:58:00
【问题描述】:

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

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

【问题讨论】:

    标签: emacs window startup


    【解决方案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) 以便它被选中;或者,用户可以在使用该功能时添加(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-bufferswitch-to-buffer 之类的内容。

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

    示例用法my-display-buffer 的函数定义需要出现在.emacsinit.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))
    

    【讨论】:

    • 谢谢,看来它工作得很好。我希望将焦点放在新创建的窗口上,因此将使用选择窗口的想法。只有一件事我不清楚。该文件最初是从终端打开/创建的,例如:emacs foo.py,因此代码中没有明确说明其名称(.emacs),这是如何完成的?
    • 以下示例假定 my-display-buffer 位于 .emacs 文件中的某个位置,并且从终端调用以下命令行:emacs -nw --eval="(let ((buffer (find-file-noselect \"~/foo.py\"))) (my-display-buffer buffer nil 'right))" 如果我要做很多这些,我将在.emacs 文件中设置函数来简单地解决问题。
    • 太好了,它按预期工作!会做很多,那个功能怎么定义? defun 中的某些函数会读取 shell 给出的名称吗?我想我可以简单地输入“emacs file.py”,我希望它是可能的;((另外,我很好奇你设置终端模式的原因-nw)
    • 我使用的Emacs版本是GUI版和终端版(合二为一);并且,-nw 告诉 Emacs 不启动 GUI 版本——即,不使用 -nw,启动 GUI 版本。在我的脑海中,我不知道如何从终端将示例简化为emacs file.py。我可以将其简化为emacs -nw --eval="(my-func \"foo.py\")",但根据我目前的 Emacs-fu 水平,我只能做到这一点。
    • 要研究的文件是startup.el——例如,传入的命令行参数被记录/设置为变量command-line-args,加载的.emacs可以访问这些参数。命令行参数有很多变化/可能性,以及用户在他/她的用户配置文件中可能拥有的无限可能性,很难/不可能提出一个有保证的解决方案所有情况。最后,无论您决定什么,都可能是您自己的设置所独有的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2014-05-08
    • 2012-02-25
    相关资源
    最近更新 更多