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

时间:2014-12-13 15:37:38

标签: 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,但是,原始缓冲区从makefile切换到暂存缓冲区,原因不明。这是为什么?

另外,如果我省略了eshell调用和最后一个"其他窗口",我仍然会得到暂存缓冲区。

2 个答案:

答案 0 :(得分:1)

这是因为makefile-mode-hook在窗口中打开实际文件之前运行,并且通过将原始窗口切换到新打开的缓冲区来进行窗口切换。

因此,您可以通过在空闲的回调中一切都解决之后进行窗口改组来解决此问题:

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

更新:我不喜欢这种有状态的“选择下一个窗口” /“选择上一个窗口”胡说八道,并认为必须有更好的方法。原来那里! save-selected-window允许游览式的临时窗口更改,而with-selected-window建立在其之上,允许将临时的窗口更改为新窗口:

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

答案 1 :(得分:0)

试试这个我刚刚做过的代码:

(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)))))))

切换到a时,应始终打开适当的eshell 生成文件。

用它来阻止疯狂:

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