如何让编译日志在Emacs中创建一个新窗口?

时间:2023-01-22 00:06:33

If I only have one window showing in emacs and use M-x compile, the window splits in two and I can watch the compile buffer easily. However, if I have more windows showing, the compilation log takes over one of the others, which I find irritating. How can I make emacs always split a new window to show the compilation log?

如果我只在emacs中显示一个窗口并使用M-x编译,则窗口分为两部分,我可以轻松地查看编译缓冲区。但是,如果我有更多的窗口显示,编译日志将接管其中一个,我觉得很烦人。如何使emacs始终拆分新窗口以显示编译日志?

Edit: A bit more information from my reading that I've been doing. It looks like compile.el calls display-buffer, which only splits a window if it is current full width. Is there some way to avoid this behaviour?

编辑:我读过的一些信息,我一直在做的。看起来compile.el调用display-buffer,如果它是当前的全宽度,它只会拆分一个窗口。有没有办法避免这种行为?

4 个解决方案

#1


You may modify the solution provided by Trey Jackson to fit your needs.

您可以修改Trey Jackson提供的解决方案以满足您的需求。

The following snippet marks buffer *compilation* as special, and sets a customized function as its display function to split current window even if already in a split window.

以下片段将缓冲区*编译*标记为特殊,并将自定义函数设置为其显示功能,即使已经在拆分窗口中也可以拆分当前窗口。

(setq special-display-buffer-names
      '("*compilation*"))

(setq special-display-function
      (lambda (buffer &optional args)
        (split-window)
        (switch-to-buffer buffer)
        (get-buffer-window buffer 0)))

#2


If what you're wanting is a dedicated top-level window (Emacs calls these frames), then this will do the trick for you. This snippet includes placement directives, but customizing the 'special-display-buffer-names variable will get you what you want.

如果您想要的是一个专用的*窗口(Emacs称这些帧),那么这将为您提供帮助。此代码段包含放置指令,但自定义'special-display-buffer-names变量将为您提供所需的内容。

(setq special-display-buffer-names
      `(("*compilation*" . ((name . "*compilation*")
                            ,@default-frame-alist
                            (left . (- 1))
                            (top . 0)))))

#3


Combining code from How can I prevent emacs from opening new window for compilation output? and code from http://www.emacswiki.org/emacs/CompilationMode, this is all my code for compile, it provides you 4 features:

组合代码如何防止emacs打开编辑输出的新窗口?和来自http://www.emacswiki.org/emacs/CompilationMode的代码,这是我编译的全部代码,它为您提供了4个功能:

1). Use compile-again to run the same compile as the last time automatically, no prompt. If there is no last time, or there is a prefix argument, it acts like M-x compile.

1)。使用compile-again自动运行与上次相同的编译,没有提示。如果没有最后一次,或者有一个前缀参数,它就像M-x编译一样。

2). compile will split the current window, it will not affect the other windows in this frame.

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

3). it will auto-close the *compilation* buffer (window) if there is no error, keep it if error exists.

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

4). it will highlight the error line and line number of the source code in the *compilation* buffer, use M-n/p to navigate every error in *compilation* buffer, Enter in the error line to jump to the line in your code code.

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

(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)

#4


Install smart-compile package inside Emacs.

在Emacs中安装smart-compile包。

add this into your init.el or .emacs

将其添加到init.el或.emacs中

(require 'compile)
(setq compilation-last-buffer nil)
;; save all modified buffers without asking before compilation
(setq compilation-ask-about-save nil)
(defun compile-again (ARG)
  "Run the same compile as the last time.

With a prefix argument or no last time, this acts like M-x compile,
and you can reconfigure the compile args."
  (interactive "p")
  ;; the following two lines create bug: split a new window every time
  ;; (if (not (get-buffer-window "*compilation*"))
  ;;      (split-window-below))
  (if (and (eq ARG 1) compilation-last-buffer)
      (recompile)
    (call-interactively 'smart-compile)))
(bind-key* "C-x C-m" 'compile-again)
;; create a new small frame to show the compilation info
;; will be auto closed if no error
(setq special-display-buffer-names
      `(("*compilation*" . ((name . "*compilation*")
                            ,@default-frame-alist
                            (left . (- 1))
                            (top . 0)))))
(setq compilation-finish-functions
      (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
               "1 sec" nil 'delete-windows-on
               (get-buffer-create "*compilation*"))
              (message "No Compilation Errors!")))))

Use C-x C-m to compile your source code, If you are the first time to execute C-x C-m, it will ask you to change the default command (which is usually enough), otherwise it will execute the command you just used to compile directly and you have to use C-u C-x C-m to change the command if you want. If you got a Makefile inside the current directory, it will notice and prompt to ask whether you want to use it.

使用Cx Cm编译源代码,如果您是第一次执行Cx Cm,它会要求您更改默认命令(这通常就足够了),否则它将执行您刚才用于直接编译的命令如果你愿意,必须使用Cu Cx Cm来改变命令。如果你在当前目录中有一个Makefile,它会注意并提示询问你是否要使用它。

Maybe this answer is too much for your question, but please try it.

也许这个答案对你的问题来说太过分了,但请试一试。

#1


You may modify the solution provided by Trey Jackson to fit your needs.

您可以修改Trey Jackson提供的解决方案以满足您的需求。

The following snippet marks buffer *compilation* as special, and sets a customized function as its display function to split current window even if already in a split window.

以下片段将缓冲区*编译*标记为特殊,并将自定义函数设置为其显示功能,即使已经在拆分窗口中也可以拆分当前窗口。

(setq special-display-buffer-names
      '("*compilation*"))

(setq special-display-function
      (lambda (buffer &optional args)
        (split-window)
        (switch-to-buffer buffer)
        (get-buffer-window buffer 0)))

#2


If what you're wanting is a dedicated top-level window (Emacs calls these frames), then this will do the trick for you. This snippet includes placement directives, but customizing the 'special-display-buffer-names variable will get you what you want.

如果您想要的是一个专用的*窗口(Emacs称这些帧),那么这将为您提供帮助。此代码段包含放置指令,但自定义'special-display-buffer-names变量将为您提供所需的内容。

(setq special-display-buffer-names
      `(("*compilation*" . ((name . "*compilation*")
                            ,@default-frame-alist
                            (left . (- 1))
                            (top . 0)))))

#3


Combining code from How can I prevent emacs from opening new window for compilation output? and code from http://www.emacswiki.org/emacs/CompilationMode, this is all my code for compile, it provides you 4 features:

组合代码如何防止emacs打开编辑输出的新窗口?和来自http://www.emacswiki.org/emacs/CompilationMode的代码,这是我编译的全部代码,它为您提供了4个功能:

1). Use compile-again to run the same compile as the last time automatically, no prompt. If there is no last time, or there is a prefix argument, it acts like M-x compile.

1)。使用compile-again自动运行与上次相同的编译,没有提示。如果没有最后一次,或者有一个前缀参数,它就像M-x编译一样。

2). compile will split the current window, it will not affect the other windows in this frame.

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

3). it will auto-close the *compilation* buffer (window) if there is no error, keep it if error exists.

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

4). it will highlight the error line and line number of the source code in the *compilation* buffer, use M-n/p to navigate every error in *compilation* buffer, Enter in the error line to jump to the line in your code code.

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

(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)

#4


Install smart-compile package inside Emacs.

在Emacs中安装smart-compile包。

add this into your init.el or .emacs

将其添加到init.el或.emacs中

(require 'compile)
(setq compilation-last-buffer nil)
;; save all modified buffers without asking before compilation
(setq compilation-ask-about-save nil)
(defun compile-again (ARG)
  "Run the same compile as the last time.

With a prefix argument or no last time, this acts like M-x compile,
and you can reconfigure the compile args."
  (interactive "p")
  ;; the following two lines create bug: split a new window every time
  ;; (if (not (get-buffer-window "*compilation*"))
  ;;      (split-window-below))
  (if (and (eq ARG 1) compilation-last-buffer)
      (recompile)
    (call-interactively 'smart-compile)))
(bind-key* "C-x C-m" 'compile-again)
;; create a new small frame to show the compilation info
;; will be auto closed if no error
(setq special-display-buffer-names
      `(("*compilation*" . ((name . "*compilation*")
                            ,@default-frame-alist
                            (left . (- 1))
                            (top . 0)))))
(setq compilation-finish-functions
      (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
               "1 sec" nil 'delete-windows-on
               (get-buffer-create "*compilation*"))
              (message "No Compilation Errors!")))))

Use C-x C-m to compile your source code, If you are the first time to execute C-x C-m, it will ask you to change the default command (which is usually enough), otherwise it will execute the command you just used to compile directly and you have to use C-u C-x C-m to change the command if you want. If you got a Makefile inside the current directory, it will notice and prompt to ask whether you want to use it.

使用Cx Cm编译源代码,如果您是第一次执行Cx Cm,它会要求您更改默认命令(这通常就足够了),否则它将执行您刚才用于直接编译的命令如果你愿意,必须使用Cu Cx Cm来改变命令。如果你在当前目录中有一个Makefile,它会注意并提示询问你是否要使用它。

Maybe this answer is too much for your question, but please try it.

也许这个答案对你的问题来说太过分了,但请试一试。