emacs init - 拆分 window 并打开彼此相邻的文件

emacs init - splitting window and opening files next to each other

我正在尝试自定义我的 Emacs init 文件,使 Emacs 打开时有两个 windows 拆分,在一侧打开 ansi-term,在另一侧打开我的 init 文件。现在,如果 Emacs 已经打开,我编写的函数 (switch-to-next-window) 可以完美运行。

我希望将光标切换到另一个 window 然后在那里打开我的 init 文件。但是,如果我在启动时尝试 运行 这个(实际上是在启动之后,至少这是我的想法),我会收到以下错误:window-live-p, nil

我猜没有 "next window"。但是我只是不知道这里有什么工作,因为我确实认为我只是在 Emacs 完全启动后才调用我的函数?如果有人能指出我的逻辑哪里出了问题,那就太好了!

(split-window-horizontally)
(setq initial-buffer-choice "*ansi-term*")

(defun switch-to-next-window ()
  (interactive)
  (let* ((next-window (get-buffer-window (other-buffer (current-buffer) t))))
    (select-window next-window)))

(add-hook 'emacs-startup-hook (lambda ()(ansi-term "/bin/bash")))
(with-eval-after-load "~/.emacs.d/init.el"
  (switch-to-next-window)
  (setq initial-buffer-choice "~/.emacs.d/init.el"))

在初始缓冲区打开后更改 initial-buffer-choice 不会有任何效果。

有用的是将所有内容放入 emacs-startup-hook 并使用 find-file-other-window 函数:

(add-hook 'emacs-startup-hook
          (lambda ()
            (ansi-term "/bin/bash")
            (split-window-horizontally)
            (find-file-other-window "~/.emacs.d/init.el")))