如何使Emacs中的编译窗口始终保持一定的大小?

时间:2012-03-15 17:29:12

标签: emacs

我想让Emacs中的编辑窗口始终显示在窗口的底部,并且始终是一定的高度。到目前为止,我在.emacs文件中添加了以下行:

(setq split-height-threshold 0)
(setq compilation-window-height 10)

...当我只有一个窗口打开时,它确实有效,但只要我将屏幕水平分割成两个窗口(即中间的分界线从顶部到底部),编辑窗口停止尊重高度变量,并在中间右侧分割窗口。

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:14)

我会使用这样的东西,可以从EmacsWiki

中自由改编
(defun my-compilation-hook ()
  (when (not (get-buffer-window "*compilation*"))
    (save-selected-window
      (save-excursion
        (let* ((w (split-window-vertically))
               (h (window-height w)))
          (select-window w)
          (switch-to-buffer "*compilation*")
          (shrink-window (- h compilation-window-height)))))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)

如果*compilation*缓冲区不可见,则会垂直分割窗口,将新打开的窗口的大小调整为10行高,然后打开其中的*compilation*缓冲区。

答案 1 :(得分:1)

您可以自定义变量compilation-window-height

答案 2 :(得分:1)

结合How can I prevent emacs from opening new window for compilation output?的代码和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)

答案 3 :(得分:0)

针对这些情况有一个出色的软件包,名为Shackle。 https://github.com/wasamasa/shackle

易于设置并可以在几乎任何类型的缓冲区上工作