【问题标题】:emacs compile buffer auto close?emacs编译缓冲区自动关闭?
【发布时间】:2012-06-18 01:42:08
【问题描述】:

我想在没有错误和警告时自动关闭编译缓冲区,但我想在有警告时显示它。任何人都可以帮助我吗?来自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 这不是关键问题
  • 了解您正在运行的编译器可能很有用,因为您可以使用msg 参数来检查是否有错误或警告。
  • 您可以尝试在 and 子句中添加另一个条件以在 compilation 缓冲区中查找字符串 'warning'。或者您的编译器用来指示警告的任何其他字符串。
  • @vpit3833 我刚试过了,但是没用。

标签: emacs buffer compile-mode


【解决方案1】:

我使用以下代码进行编译。如果有警告或错误,它将保留编译缓冲区,否则将其掩埋(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:因为它所做的只是切换窗口中的缓冲区,保持窗口布局不变。我一般不喜欢 Emacs 改变我的窗口布局。尝试使用delete-windows-on 而不是switch-to-prev-buffer
  • 这是一个非常简洁的功能,但我想知道我是否可以让它总是在特定窗口中弹出编译,而不是在我的屏幕底部自动创建一个新窗口?跨度>
  • @johnbakers:我的答案中的代码不会弹出窗口或创建缓冲区,这是由compile 完成的;我的代码在编译完成后运行(这应该很明显,因为它已添加到compilation-finish-functions)。如果您想更好地控制窗口的创建方式,请查看 shackle 包。
【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多