Emacs 编译缓冲区自动关闭



我想在没有错误和警告时自动关闭编译缓冲区,但我想在有警告时显示它。有人可以帮助我吗?来自 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)

我用更好的逻辑调整了上面的答案并对其进行了测试,工作完美:

  1. 向函数添加钩子:
  (add-hook 'compilation-start-hook 'compilation-started)
  (add-hook 'compilation-finish-functions 'hide-compile-buffer-if-successful)
  1. 自动隐藏编译缓冲区延迟可自定义变量(通过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
  )
  1. 获取编译所花费的时间,并使用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))
  )

演示

最新更新