【问题标题】:Can I limit the length of the compilation buffer in Emacs?我可以限制 Emacs 中编译缓冲区的长度吗?
【发布时间】:2012-06-28 06:33:23
【问题描述】:

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

【问题讨论】:

  • 禁用语法高亮怎么样?也许添加一个钩子,当缓冲区变大时禁用它。

标签: emacs elisp


【解决方案1】:

看来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 在某个地方有一个函数。 :)
  • 顺便说一句:你知道这个函数是否也在其他comint模式的钩子中?
  • 我经常使用的唯一comint-type模式是shell-mode,默认情况下不存在截断钩子。
【解决方案2】:

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

(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 删除的文本不会在 emac 的撤消历史中建立。
  • 是的,这很有可能。我很高兴接受另一个功能。有任何想法吗?我仍然是一个 elisp 新手。
  • 没有更多的想法;我和你属于同一类;)
  • 啊,好像没有,没有。 kill-region 添加到 kill-ring,但 delete-region 没有,discussed here 也是如此。
  • 你可能只需要运行(buffer-disable-undo)(buffer-enable-undo) 就可以了,你在只读状态下切换。可能有更好的方法来做到这一点。
猜你喜欢
  • 1970-01-01
  • 2014-10-28
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多