如何在Emacs中使编译日志在新窗口中创建?

4
如果我在emacs中只有一个窗口,并使用M-x compile,则窗口会分成两个,我可以轻松地查看编译缓冲区。然而,如果我有更多的窗口正在显示,编译日志会接管其中一个窗口,这让我感到烦恼。我如何使emacs始终拆分一个新窗口以显示编译日志?
编辑:从我所做的阅读中获取了更多信息。看起来compile.el调用display-buffer,仅在当前完整宽度时才分割窗口。有没有什么方法可以避免这种行为?
4个回答

4
你可以修改Trey Jackson提供的解决方案以适应你的需求。
下面的代码段将*compilation*缓冲区标记为特殊,并设置一个自定义函数作为其显示函数,即使已经在分割窗口中也会分割当前窗口。
(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)))

3
如果你想要一个专用的顶层窗口(Emacs将其称为frames),那么这个代码片段可以帮到你。此片段包括放置指令,但自定义'special-display-buffer-names变量将帮助你获得所需内容。
(setq special-display-buffer-names
      `(("*compilation*" . ((name . "*compilation*")
                            ,@default-frame-alist
                            (left . (- 1))
                            (top . 0)))))

我了解这个术语,实际上我正在寻找一个逻辑的emacs窗口,而不是物理窗口。 - Josh Matthews

2

将来自如何防止emacs为编译输出打开新窗口http://www.emacswiki.org/emacs/CompilationMode的代码合并,这是我所有的compile代码,它提供了以下4个功能:

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

2). compile会分割当前窗口,不会影响此框架中的其他窗口。

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

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

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

0

在 Emacs 中安装 smart-compile 包。

将此添加到您的 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!")))))

使用 C-x C-m 编译您的源代码,如果您是第一次执行 C-x C-m,它将要求您更改默认命令(通常足够),否则它将直接执行您刚才用来编译的命令,并且如果您想要更改命令,则必须使用 C-u C-x C-m。如果当前目录中有一个 Makefile,它会注意到并提示您是否要使用它。

也许这个答案对于你的问题来说有点过多,请尝试一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接