2012-06-15 169 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