Answers:
多年前,在#emacs IRC频道上,我得到了我一直使用过的这段代码。值“ 2秒”设置成功编译窗口保持可见的时间。
; from enberg on #emacs
(setq compilation-finish-function
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
;;no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"2 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(message "No Compilation Errors!")))))
快速查看代码(在库中compile.el
),您应该能够通过使用hook函数来杀死或隐藏显示的缓冲区compilation-finish-functions
。为此,请使用以下方法:
(add-hook 'compilation-finish-functions (lambda (buf strg) (kill-buffer buf))
如果您不想杀死缓冲区,请使用以下命令:
(add-hook 'compilation-finish-functions
(lambda (buf strg)
(let ((win (get-buffer-window buf 'visible)))
(when win (delete-window win)))))
通常,您可以想象对于这样的事情,可能已经提供了一个挂钩,因此您可以轻松地在处理中如此重要的位置附加代码。稍微浏览一下代码或使用它们M-x apropos
通常会很快使您知道。挂钩的名称通常以-hook
或结尾-functions
。
如果有人感兴趣,此线程也很重要:
/programming/11043004/emacs-compile-buffer-auto-close
学分归原始作者jpkotta所爱。这是他的答案:
我使用以下内容进行编译。如果有警告或错误,它将保留编译缓冲区,否则将其埋入(1秒后)。
(defun bury-compile-buffer-if-successful (buffer string) "Bury a compilation buffer if succeeded without warnings " (if (and (string-match "compilation" (buffer-name buffer)) (string-match "finished" string) (not (with-current-buffer buffer (search-forward "warning" nil t)))) (run-with-timer 1 nil (lambda (buf) (bury-buffer buf) (switch-to-prev-buffer (get-buffer-window buf) 'kill)) buffer))) (add-hook 'compilation-finish-functions 'bury-compile-buffer-if-successful)
我有这样的片段在我的.emacs.d
:
(defcustom compilation-auto-quit-window-delay 1
"Time in seconds before auto closing the window."
:group 'compilation
:type 'number)
(defun compilation-auto-quit-window-finish-function (buffer status)
"Quit the *compilation* window if it went well."
(let ((window (get-buffer-window buffer)))
(when (and (equal status "finished\n")
(compilation-went-super-p))
(run-with-timer
(or compilation-auto-quit-window-delay 0) nil
(lambda nil
(when (and (window-live-p window)
(eq (window-buffer window)
buffer)
(not (eq (selected-window)
window)))
(save-selected-window
(quit-window nil window))))))))
(define-minor-mode compilation-auto-quit-window
"Automatically close the *compilation* window if it went well."
:global t
(cond (compilation-auto-quit-window
(add-hook 'compilation-finish-functions
'compilation-auto-quit-window-finish-function))
(t
(remove-hook 'compilation-finish-functions
'compilation-auto-quit-window-finish-function))))
(defun compilation-went-super-p (&optional buffer)
"Return t, if no gotoable output appeared."
(with-current-buffer (or buffer (current-buffer))
(save-excursion
(goto-char (point-min))
(let (;; (compilation-skip-threshold 1)
)
(not (ignore-errors
(compilation-next-error 1)
t))))))