如何防止emacs为编译输出打开新窗口?

如何防止emacs为编译输出打开新窗口?,emacs,Emacs,调用compile命令时,如何防止emacs打开新窗口? 我想将其绑定到一个特定的现有窗口。您可以通过将编译缓冲区名称函数设置为一个函数来选择编译缓冲区的名称,该函数采用主模式的名称并返回缓冲区名称: (setq compilation-buffer-name-function '(lambda (maj-mode) "existing-buffer-name")) 基于您对Luke的评论,我建议您检查一下我使用的这个函数。我喜欢它,因为如果没有错误,它会隐藏编译缓冲区,

调用compile命令时,如何防止emacs打开新窗口?
我想将其绑定到一个特定的现有窗口。

您可以通过将
编译缓冲区名称函数设置为一个函数来选择编译缓冲区的名称,该函数采用主模式的名称并返回缓冲区名称:

(setq compilation-buffer-name-function '(lambda (maj-mode) "existing-buffer-name"))
基于您对Luke的评论,我建议您检查一下我使用的这个函数。我喜欢它,因为如果没有错误,它会隐藏编译缓冲区,否则它会保留它以便您可以看到它们

您可以在emacs wiki上查看该页面,但代码如下:

;; Helper for compilation. Close the compilation window if
;; there was no error at all. (emacs wiki)
(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)

您始终可以切换回编译缓冲区以查看任何警告。

不太确定您的要求,但如果您希望缓冲区“编译”显示在当前窗口中,而不是显示在其他窗口中,则:

(add-to-list 'same-window-buffer-names "*compilation*")

结合@zdav的anwser和来自的代码,这是我编译的所有代码,它为您提供了4个功能:

1) 。使用
再次编译
自动运行与上次相同的编译,无提示。如果没有最后一次,或者有一个前缀参数,则它的行为类似于M-x编译

2) <代码>编译
将拆分当前窗口,它不会影响此框架中的其他窗口

3) 。如果没有错误,它将自动关闭
*编译*
缓冲区(窗口),如果存在错误,则保留它

4) 。它将突出显示
*编译*
缓冲区中源代码的错误行和行号,使用
M-n/p
导航
*编译*
缓冲区中的每个错误,
在错误行中输入
,跳转到代码中的行

(require 'compile)
(setq compilation-last-buffer nil)
(defun compile-again (ARG)
  "Run the same compile as the last time.

If there is no last time, or there is a prefix argument, this acts like M-x compile."
  (interactive "p")
  (if (and (eq ARG 1)
           compilation-last-buffer)
      (progn
        (set-buffer compilation-last-buffer)
        (revert-buffer t t))
    (progn
      (call-interactively 'compile)
      (setq cur (selected-window))
      (setq w (get-buffer-window "*compilation*"))
      (select-window w)
      (setq h (window-height w))
      (shrink-window (- h 10))
      (select-window cur))))
(global-set-key (kbd "C-x C-m") 'compile-again)
(defun my-compilation-hook ()
  "Make sure that the compile window is splitting vertically."
  (progn
    (if (not (get-buffer-window "*compilation*"))
        (progn
          (split-window-vertically)))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)
(defun compilation-exit-autoclose (STATUS code msg)
  "Close the compilation window if there was no error at all."
  ;; 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))
(setq compilation-exit-message-function 'compilation-exit-autoclose)
(defvar all-overlays ())
(defun delete-this-overlay(overlay is-after begin end &optional len)
  (delete-overlay overlay)
  )
(defun highlight-current-line ()
"Highlight current line."
  (interactive)
  (setq current-point (point))
  (beginning-of-line)
  (setq beg (point))
  (forward-line 1)
  (setq end (point))
  ;; Create and place the overlay
  (setq error-line-overlay (make-overlay 1 1))

  ;; Append to list of all overlays
  (setq all-overlays (cons error-line-overlay all-overlays))

  (overlay-put error-line-overlay
               'face '(background-color . "red"))
  (overlay-put error-line-overlay
               'modification-hooks (list 'delete-this-overlay))
  (move-overlay error-line-overlay beg end)
  (goto-char current-point))
(defun delete-all-overlays ()
  "Delete all overlays"
  (while all-overlays
    (delete-overlay (car all-overlays))
    (setq all-overlays (cdr all-overlays))))
(defun highlight-error-lines(compilation-buffer process-result)
  (interactive)
  (delete-all-overlays)
  (condition-case nil
      (while t
        (next-error)
        (highlight-current-line))
    (error nil)))
(setq compilation-finish-functions 'highlight-error-lines)

我对另一个案例(人工模式)提出了类似的问题,但可能代码在这里也有用:
(特别是,但请检查上面的问题)

我对编译名没意见。我只是不同意在编译不可见时“编译”打开新窗口。好吧,那么你不想自动弹出*compilation*缓冲区?查看我编辑的答案。在默认的emacs lisp文件中注释内容不好。您更应该
defadvice
it.
(defadvice编译开始(在inhibit display(命令和可选模式名称函数突出显示regexp)附近)(flet((显示缓冲区))(fset'display buffer'忽略)ad do it))(ad activate'编译开始)(ad deactivate'编译开始)
如果愿意,您也可以将函数设置为“忽略”(ignore)。这将需要
显示缓冲区
参数。OP要求在另一个现有窗口中打开它,而不是在同一个窗口中。嗯,谢谢,但是这种方法有一个小问题。想象你有两扇窗户并排打开。您可以在左侧窗口中开始编译—它将在右侧窗口中打开。编译结束后——您希望将窗口和代码一起返回,而此脚本只会将其关闭(这也很烦人)。不管怎样,我应该提高我的ELisp,也许做正确的事情。@KonstantineRybnikov
(require 'compile)
(setq compilation-last-buffer nil)
(defun compile-again (ARG)
  "Run the same compile as the last time.

If there is no last time, or there is a prefix argument, this acts like M-x compile."
  (interactive "p")
  (if (and (eq ARG 1)
           compilation-last-buffer)
      (progn
        (set-buffer compilation-last-buffer)
        (revert-buffer t t))
    (progn
      (call-interactively 'compile)
      (setq cur (selected-window))
      (setq w (get-buffer-window "*compilation*"))
      (select-window w)
      (setq h (window-height w))
      (shrink-window (- h 10))
      (select-window cur))))
(global-set-key (kbd "C-x C-m") 'compile-again)
(defun my-compilation-hook ()
  "Make sure that the compile window is splitting vertically."
  (progn
    (if (not (get-buffer-window "*compilation*"))
        (progn
          (split-window-vertically)))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)
(defun compilation-exit-autoclose (STATUS code msg)
  "Close the compilation window if there was no error at all."
  ;; 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))
(setq compilation-exit-message-function 'compilation-exit-autoclose)
(defvar all-overlays ())
(defun delete-this-overlay(overlay is-after begin end &optional len)
  (delete-overlay overlay)
  )
(defun highlight-current-line ()
"Highlight current line."
  (interactive)
  (setq current-point (point))
  (beginning-of-line)
  (setq beg (point))
  (forward-line 1)
  (setq end (point))
  ;; Create and place the overlay
  (setq error-line-overlay (make-overlay 1 1))

  ;; Append to list of all overlays
  (setq all-overlays (cons error-line-overlay all-overlays))

  (overlay-put error-line-overlay
               'face '(background-color . "red"))
  (overlay-put error-line-overlay
               'modification-hooks (list 'delete-this-overlay))
  (move-overlay error-line-overlay beg end)
  (goto-char current-point))
(defun delete-all-overlays ()
  "Delete all overlays"
  (while all-overlays
    (delete-overlay (car all-overlays))
    (setq all-overlays (cdr all-overlays))))
(defun highlight-error-lines(compilation-buffer process-result)
  (interactive)
  (delete-all-overlays)
  (condition-case nil
      (while t
        (next-error)
        (highlight-current-line))
    (error nil)))
(setq compilation-finish-functions 'highlight-error-lines)