是否可以限制Emacs编译缓冲区存储的行数?如果没有错误,则我们的构建系统可以在整个产品构建过程中生成约10,000行输出。由于我的编译缓冲区还解析ANSI颜色,这可能会变得非常缓慢。我想只有2000行输出被缓存。
看起来comint-truncate-buffer
对于编译缓冲区和shell缓冲区同样适用:
(add-hook 'compilation-filter-hook 'comint-truncate-buffer)
(setq comint-buffer-maximum-size 2000)
我通过使用命令perl -le 'print for 1..10000'
运行compile
进行了测试。当完成后,编译缓冲区中的第一行是8001
。
shell-mode
,其中默认情况下不包含截断钩子。 - Sean好的,我坐下来编写了自己的函数,并将其插入到编译过滤钩子中。这可能不是最高效的解决方案,但目前看来它似乎可以正常工作。
(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)
delete-region
删除的文本会在Emacs的撤销历史中堆积起来。 - Peter.O(buffer-disable-undo)
和(buffer-enable-undo)
就可以解决问题。当然,肯定有更好的方法可以做到这一点。 - Randy Morris