7

是否可以限制 Emacs 编译缓冲区存储的行数?如果没有遇到错误,我们的构建系统可以在整个产品构建中产生大约 10,000 行输出。因为我的编译缓冲区也解析 ANSI 颜色,所以这会变得非常非常慢。我只想缓冲 2,000 行输出。

4

2 回答 2

10

看来comint-truncate-buffer编译缓冲区和 shell 缓冲区一样有效:

(add-hook 'compilation-filter-hook 'comint-truncate-buffer)
(setq comint-buffer-maximum-size 2000)

我通过运行compile命令对此进行了测试perl -le 'print for 1..10000'。完成后,编译缓冲区中的第一行是8001.

于 2012-06-29T02:41:28.543 回答
4

好的,我坐下来编写了自己的函数,该函数被插入到编译过滤器挂钩中。它可能不是性能最好的解决方案,但到目前为止它似乎工作正常。

(defcustom my-compilation-buffer-length 2500 
  "The maximum number of lines that the compilation buffer is allowed to store")
(defun my-limit-compilation-buffer ()
  "This function limits the length of the compilation buffer.
It uses the variable my-compilation-buffer-length to determine
the maximum allowed number of lines. It will then delete the first 
N+50 lines of the buffer, where N is the number of lines that the 
buffer is longer than the above mentioned variable allows."
  (toggle-read-only)
  (buffer-disable-undo)
  (let ((num-lines (count-lines (point-min) (point-max))))
    (if (> num-lines my-compilation-buffer-length)
        (let ((beg (point)))
          (goto-char (point-min))
          (forward-line (+ (- num-lines my-compilation-buffer-length) 250))
          (delete-region (point-min) (point))
          (goto-char beg)
          )
      )
    )
  (buffer-enable-undo)
  (toggle-read-only)
  )
(add-hook 'compilation-filter-hook 'my-limit-compilation-buffer)
于 2012-06-28T07:28:20.970 回答