2012-06-15 132 views
12

我想在没有错误和没有警告时自动关闭编译缓冲区,但我想在出现警告时显示它。任何人都可以帮助我? emacswiki这个代码只做第一个要求。如何改变它?emacs编译缓冲区自动关闭?

;; Helper for compilation. Close the compilation window if 
    ;; there was no error at all. 
    (defun compilation-exit-autoclose (status code msg) 
    ;; If M-x compile exists with a 0 
    (when (and (eq status 'exit) (zerop code)) 
     ;; then bury the *compilation* buffer, so that C-x b doesn't go there 
     (bury-buffer) 
     ;; and delete the *compilation* window 
     (delete-window (get-buffer-window (get-buffer "*compilation*")))) 
    ;; Always return the anticipated result of compilation-exit-message-function 
    (cons msg code)) 
    ;; Specify my function (maybe I should have done a lambda function) 
    (setq compilation-exit-message-function 'compilation-exit-autoclose) 
+0

你在编译什么? – Thomas

+0

@Thomas这不是关键问题 – Iceman

+1

知道你正在运行哪个编译器可能很有用,因为你可以使用'msg'参数来检查是否有错误或警告。 – Thomas

回答

15

我使用以下代码进行编译。如果存在警告或错误,它将保留编译缓冲区,否则将其嵌入(1秒后)。

(defun bury-compile-buffer-if-successful (buffer string) 
"Bury a compilation buffer if succeeded without warnings " 
(when (and 
     (buffer-live-p buffer) 
     (string-match "compilation" (buffer-name buffer)) 
     (string-match "finished" string) 
     (not 
      (with-current-buffer buffer 
      (goto-char (point-min)) 
      (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) 
+0

好,它的工作原理,也许我会删除计时器。 – Iceman

+0

这很酷,但为什么它会在编译缓冲区关闭后打开窗口?这个窗口保持打开,直到我移动光标,然后它突然关闭。什么导致这种行为? – johnbakers

+0

@johnbakers:因为它所做的只是切换窗口中的缓冲区,而不改变窗口布局。我通常不喜欢Emacs改变我的窗口布局。尝试使用'delete-windows-on'而不是'switch-to-prev-buffer'进行播放。 – jpkotta

2

jpkotta,它确实工作的大部分时间。有时,即使有警告,它也不会切换到编译缓冲区。所以我改变了你的表格&现在确实有效:

(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 
      **(goto-char 1)** 
      (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) 
+0

谢谢,我已经更新了我的答案。 – jpkotta