emacs编译缓冲区自动关闭?

emacs编译缓冲区自动关闭?,emacs,buffer,compile-mode,Emacs,Buffer,Compile Mode,我想在没有错误和警告时自动关闭编译缓冲区,但在有警告时显示它。有人能帮我吗?此代码仅满足第一个要求。如何改变它 ;; 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

我想在没有错误和警告时自动关闭编译缓冲区,但在有警告时显示它。有人能帮我吗?此代码仅满足第一个要求。如何改变它

  ;; 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秒后)


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)

@Thomas这不是关键问题了解正在运行的编译器可能很有用,因为您可以使用
msg
参数检查是否存在错误或警告。您可以尝试向and子句添加另一个条件,以在编译缓冲区中查找字符串“warning”。或者编译器用来表示警告的任何其他字符串。@vpit3833我刚试过,但不起作用。这很酷,但为什么在编译缓冲区关闭后它会保持窗口打开?然后,此窗口保持打开状态,直到我移动光标,然后它突然关闭。是什么导致了这种行为?@johnbakers:因为它所做的只是切换窗口中的缓冲区,保持窗口布局不变。我通常不喜欢Emacs改变我的窗口布局。尝试在上玩
删除窗口,而不是
切换到prev buffer
。这是一个非常好的功能,但是我想知道我是否可以让它总是在特定的窗口中弹出编译,而不是在我的屏幕底部自动创建一个新窗口?@johnbakers:我回答中的代码不会弹出窗口或创建缓冲区,这是由
编译
完成的;一旦编译完成,我的代码就会运行(这应该很明显,因为它被添加到
编译完成函数中
)。如果您想更多地控制窗口的创建方式,请查看
shackle
包。
(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)