我能限制Emacs中编译缓冲区的长度吗?

9

是否可以限制Emacs编译缓冲区存储的行数?如果没有错误,则我们的构建系统可以在整个产品构建过程中生成约10,000行输出。由于我的编译缓冲区还解析ANSI颜色,这可能会变得非常缓慢。我想只有2000行输出被缓存。


要不就禁用语法高亮?也许可以添加一个钩子,在缓冲区增长时禁用它。 - tripleee
2个回答

11

看起来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


我知道Emacs里面肯定有这个函数。 :) - Arne
顺便问一下:你知道这个函数是否也在其他comint模式的钩子中吗? - Arne
我经常使用的唯一 comint 类型模式是 shell-mode,其中默认情况下不包含截断钩子。 - Sean

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)

1
只是一个想法……我不确定细节,或者这对你是否重要,但被delete-region删除的文本会在Emacs的撤销历史中堆积起来。 - Peter.O
啊,看起来不是这样。kill-region 会添加到 kill-ring,但 delete-region 不会,正如这里讨论的那样。 - Arne
我指的是“撤销历史记录”,它在“delete-region”操作时被存储...但仔细想想,这可能对你来说是一个功能;以防你实际上需要参考已删除部分中的某些内容... - Peter.O
1
在你切换只读模式时,可能只需运行(buffer-disable-undo)(buffer-enable-undo)就可以解决问题。当然,肯定有更好的方法可以做到这一点。 - Randy Morris
@RandyMorris 谢谢提示。我已将其纳入上述发布的答案中。 - Arne
显示剩余3条评论

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