Emacs编译缓冲区自动关闭?

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

@Thomas,这不是关键问题。 - Iceman
1
知道你正在使用哪个编译器可能会很有用,因为你可以使用msg参数来检查是否有错误或警告。 - Thomas
你可以尝试在 and 子句中添加另一个条件,以查找“warning”字符串在编译缓冲区中的出现。或者查找编译器用于指示警告的其他字符串。 - vpit3833
@vpit3833,我刚试了一下,但是它不起作用。 - Iceman
3个回答

22
我使用以下方式进行编译。如果有警告或错误,则保留编译缓冲区;否则,将其隐藏(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)

这很酷,但为什么编译缓冲区关闭后它会保留窗口打开?然后这个窗口一直保持打开状态,直到我移动光标,然后它突然关闭。是什么导致了这种行为? - johnbakers
@johnbakers:因为它所做的只是在窗口中切换缓冲区,而不改变窗口布局。我通常不喜欢Emacs改变我的窗口布局。尝试使用delete-windows-on而不是switch-to-prev-buffer - jpkotta
这是一个非常棒的函数,但我想知道是否可以让它总是在特定窗口中弹出编译,而不是自动在屏幕底部创建新窗口? - johnbakers
@johnbakers:我的回答中的代码并不会弹出窗口或创建缓冲区,这是由compile完成的;我的代码在编译完成后运行(这应该非常明显,因为它被添加到compilation-finish-functions)。如果您想更精细地控制窗口的创建方式,请查看shackle包。 - 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

我已经优化了上面的答案,并进行了测试,完美地工作:

  1. 将Hooks添加到函数中:
  (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
  )

获取编译时间并在编译缓冲区中使用`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))
  )

演示


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接