Emacs意外地将缓冲区切换到临时缓冲区

Emacs意外地将缓冲区切换到临时缓冲区,emacs,window,Emacs,Window,这里是Emacs的新手。我想在打开makefile时在拆分窗口中启动eshell。因此,我在我的.emacs中添加了以下内容: (add-hook 'makefile-mode-hook (lambda () (progn (split-window-right 110) (other-window 1) (eshell) (other-window 1)))) 我按计划得到了eshell,但是,出于未知的原因,我的原始缓

这里是Emacs的新手。我想在打开makefile时在拆分窗口中启动eshell。因此,我在我的
.emacs
中添加了以下内容:

(add-hook 
  'makefile-mode-hook 
  (lambda () 
    (progn 
      (split-window-right 110)
      (other-window 1)
      (eshell)
      (other-window 1))))
我按计划得到了eshell,但是,出于未知的原因,我的原始缓冲区从makefile切换到scratch缓冲区。为什么会这样


此外,如果我省略了
eshell
调用和最后一个“其他窗口”,我仍然会得到临时缓冲区

试试我刚刚编写的代码:

(add-hook 'window-configuration-change-hook
          'makefile-open-eshell)

(defvar makefile-eshell-in-progress nil)

(defun makefile-open-eshell ()
  (interactive)
  (unless makefile-eshell-in-progress
    (when (memq major-mode
                '(makefile-mode makefile-gmake-mode))
      (let ((dir (file-name-directory (buffer-file-name)))
            (window (cl-find-if
                     (lambda (window)
                       (with-current-buffer (window-buffer window)
                         (eq major-mode 'eshell-mode)))
                     (window-list))))
        (if window
            (with-current-buffer (window-buffer window)
              (unless (file-equal-p dir (eshell/pwd))
                (eshell/cd dir)
                (eshell-send-input)))
          (let ((makefile-eshell-in-progress t))
            ;; (delete-other-windows)
            (split-window-right)
            (other-window 1)
            (eshell)
            (eshell/cd dir)
            (eshell-send-input)
            (other-window -1)))))))
当您切换到某个应用程序时,应始终打开相应的eshell Makefile

用这个来停止疯狂:

(remove-hook 'window-configuration-change-hook
             'makefile-open-eshell)

这是因为
makefile模式钩子
在窗口中打开实际文件之前运行,并且窗口切换在将原始窗口切换到新打开的缓冲区时出错

因此,您可以通过在所有事情都解决后,在空闲回调中执行窗口洗牌来解决此问题:

(add-hook 
 'makefile-mode-hook 
 (lambda ()
   (run-with-idle-timer 0 nil
     (lambda () 
       (split-window-right 110)
       (other-window 1)
       (eshell)
       (other-window -1)))))
更新:我不喜欢这种有状态的“选择下一个窗口”/“选择上一个窗口”的废话,我认为应该有更好的方法。结果是有<代码>保存所选窗口允许短途风格的临时窗口更改,而构建在该窗口之上的所选窗口允许临时窗口更改为新窗口:

(add-hook
 'makefile-mode-hook
 (lambda ()
   (run-with-idle-timer 0 nil
     (lambda ()
       (with-selected-window (split-window-right 110)
         (eshell))))))

这在启动时起作用,但当我退出eshell或尝试终止其缓冲区(一种不受欢迎的行为)时,会分割eshell窗口,更重要的是,我仍然不知道为什么会这样做,或者为什么我的原始版本不会这样做。您不应该在生成此代码的Makefile之前终止eshell。无论如何,您的问题的答案是:
(其他窗口-1)
,可能。
其他窗口-1
没有帮助。为了澄清这一点,我只希望能够打开一个文件,并将eshell放在一个拆分窗口中,我不需要比这更华丽的东西。