我想在没有错误和警告时自动关闭编译缓冲区,但我想在有警告时显示它。有人可以帮助我吗?来自 emacswiki 的代码仅满足第一个要求。怎么改?
;; 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)
我使用以下内容进行编译。如果有警告或错误,它会保留编译缓冲区,否则(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)
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)
我用更好的逻辑调整了上面的答案并对其进行了测试,工作完美:
(add-hook 'compilation-start-hook 'compilation-started)
(add-hook 'compilation-finish-functions 'hide-compile-buffer-if-successful)
M-x customize-variable RET auto-hide-compile-buffer-delay
): (defcustom auto-hide-compile-buffer-delay 0
"Time in seconds before auto hiding compile buffer."
:group 'compilation
:type 'number
)
compilation-num-*
来获取编译缓冲区中的警告和错误计数: (defun hide-compile-buffer-if-successful (buffer string)
(setq compilation-total-time (time-subtract nil compilation-start-time))
(setq time-str (concat " (Time: " (format-time-string "%s.%3N" compilation-total-time) "s)"))
(if
(with-current-buffer buffer
(setq warnings (eval compilation-num-warnings-found))
(setq warnings-str (concat " (Warnings: " (number-to-string warnings) ")"))
(setq errors (eval compilation-num-errors-found))
(if (eq errors 0) nil t)
)
;;If Errors then
(message (concat "Compiled with Errors" warnings-str time-str))
;;If Compiled Successfully or with Warnings then
(progn
(bury-buffer buffer)
(run-with-timer auto-hide-compile-buffer-delay nil 'delete-window (get-buffer-window buffer 'visible))
(message (concat "Compiled Successfully" warnings-str time-str))
)
)
)
(make-variable-buffer-local 'compilation-start-time)
(defun compilation-started (proc)
(setq compilation-start-time (current-time))
)
以下仅显示错误时的编译缓冲区。如果显示缓冲区并且新编译完成且没有错误,则缓冲区将消失。
;; Prevent the compilation buffer from being displayed until the compilation
;; process has finished. After that, ensure that this buffer is visible if and
;; only if the compilation failed.
;; (compile.el displays the buffer with (allow-no-window . t))
(add-to-list 'display-buffer-alist
'("\\*compilation\\*" (display-buffer-no-window)))
;; After the compilation process is finished:
(setq compilation-exit-message-function
(lambda (status code msg)
(let ((compilation-window
(get-buffer-window "*compilation*")))
(cond
;; If compilation failed and compilation buffer is not visible,
((and (eq status 'exit)
(not (zerop code))
(not compilation-window))
;; temporarily allow displaying the buffer,
(let ((display-buffer-overriding-action
'((display-buffer-use-least-recent-window))))
;; and display that buffer.
(display-buffer "*compilation*")))
;; If compilation succeeded and compilation buffer is visible,
((and (eq status 'exit)
(zerop code)
compilation-window)
;; bury that buffer.
(with-selected-window compilation-window
(bury-buffer)))))
(cons msg code))) ;; default return value