隐藏*编译*窗口


20

看到编译窗口成功时很烦人。如何自动删除?

我想看看它是否成功。

Answers:


11

多年前,在#emacs IRC频道上,我得到了我一直使用过的这段代码。值“ 2秒”设置成功编译窗口保持可见的时间。

; from enberg on #emacs
(setq compilation-finish-function
  (lambda (buf str)
    (if (null (string-match ".*exited abnormally.*" str))
        ;;no errors, make the compilation window go away in a few seconds
        (progn
          (run-at-time
           "2 sec" nil 'delete-windows-on
           (get-buffer-create "*compilation*"))
          (message "No Compilation Errors!")))))

14

快速查看代码(在库中compile.el),您应该能够通过使用hook函数来杀死或隐藏显示的缓冲区compilation-finish-functions。为此,请使用以下方法:

 (add-hook 'compilation-finish-functions (lambda (buf strg) (kill-buffer buf))

如果您不想杀死缓冲区,请使用以下命令:

 (add-hook 'compilation-finish-functions
           (lambda (buf strg)
             (let ((win  (get-buffer-window buf 'visible)))
               (when win (delete-window win)))))

通常,您可以想象对于这样的事情,可能已经提供了一个挂钩,因此您可以轻松地在处理中如此重要的位置附加代码。稍微浏览一下代码或使用它们M-x apropos通常会很快使您知道。挂钩的名称通常以-hook或结尾-functions


2

如果有人感兴趣,此线程也很重要:

/programming/11043004/emacs-compile-buffer-auto-close

学分归原始作者jpkotta所爱。这是他的答案:

我使用以下内容进行编译。如果有警告或错误,它将保留编译缓冲区,否则将其埋入(1秒后)。

(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
          (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
不要发布仅包含链接的答案。在您的帖子中包括答案的基本部分,以便您的帖子回答问题。提供该链接作为来源和更多信息。有关更多信息,请阅读我们有关编写答案指南
吉尔(Gilles)'所以

更新了我的答案。
Swarnendu Biswas


1

我有这样的片段在我的.emacs.d

    (defcustom compilation-auto-quit-window-delay 1
      "Time in seconds before auto closing the window."
      :group 'compilation
      :type 'number)  

    (defun compilation-auto-quit-window-finish-function (buffer status)
      "Quit the *compilation* window if it went well."
      (let ((window (get-buffer-window buffer)))
        (when (and (equal status "finished\n")
                   (compilation-went-super-p))
          (run-with-timer
              (or compilation-auto-quit-window-delay 0) nil
            (lambda nil
              (when (and (window-live-p window)
                         (eq (window-buffer window)
                             buffer)
                         (not (eq (selected-window)
                                  window)))
                (save-selected-window
                  (quit-window nil window))))))))

    (define-minor-mode compilation-auto-quit-window
      "Automatically close the *compilation* window if it went well."
      :global t
      (cond (compilation-auto-quit-window
             (add-hook 'compilation-finish-functions
                       'compilation-auto-quit-window-finish-function))
            (t
             (remove-hook 'compilation-finish-functions
                          'compilation-auto-quit-window-finish-function))))

    (defun compilation-went-super-p (&optional buffer)
      "Return t, if no gotoable output appeared."
      (with-current-buffer (or buffer (current-buffer))
        (save-excursion
          (goto-char (point-min))
          (let (;; (compilation-skip-threshold 1)
                )
            (not (ignore-errors
                   (compilation-next-error 1)
                   t))))))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.