Is it possible to limit the number of lines that the Emacs compilation buffer stores? Our build system can produce some 10,000 lines of output on whole product builds, if no errors are encountered. Since my compilation buffer also parses ANSI colors, this can get very, very slow. I would like to have only e.g. 2,000 lines of output buffered.
Asked
Active
Viewed 1,452 times
2 Answers
11
It appears that comint-truncate-buffer
works just as well for compilation buffers as it does for shell buffers:
(add-hook 'compilation-filter-hook 'comint-truncate-buffer)
(setq comint-buffer-maximum-size 2000)
I tested this by running compile
with the command perl -le 'print for 1..10000'
. When it was done, the first line in the compilation buffer was 8001
.

Sean
- 29,130
- 4
- 80
- 105
-
-
Btw: do you know if this function is also in the hooks for the other comint modes? – Arne Jun 29 '12 at 07:17
-
The only comint-type mode I use often is `shell-mode`, where the truncate hook is not present by default. – Sean Jun 29 '12 at 18:16
4
Ok, I sat down and wrote my own function that gets plugged into the compilation-filter-hook. It might not be the best performing solution, but so far it seems to work fine.
(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)

Arne
- 2,624
- 3
- 24
- 45
-
1Just a thought... I'm not certain about the details, or if it matters to you, but won't the text deleted by `delete-region` be building up in emac's undo history. – Peter.O Jun 28 '12 at 09:12
-
Yes, that's quite possible. I am glad to take another function. Any ideas? I am still pretty much an elisp newbie. – Arne Jun 28 '12 at 09:14
-
-
Ah, it seems no, it does not. kill-region adds to the kill-ring, but delete-region doesn't, as has been [discussed here](http://stackoverflow.com/questions/1257365/how-to-delete-a-region-of-text-in-emacs) as well. – Arne Jun 28 '12 at 09:15
-
I was referring to the `undo history`, which is stored upon `delete-region`... but on second thoughts, that may be a feature for you; in case you actually need to refer back to something in the deleted sections... – Peter.O Jun 28 '12 at 09:27
-
Good point. I guess this will be another question: How to delete a region in emacs without kill-ring and undo history? – Arne Jun 28 '12 at 11:55
-
1You could probably get away with just running `(buffer-disable-undo)` and `(buffer-enable-undo)` where you are toggling read-only. There is probably a much better way to do that. – Randy Morris Jun 28 '12 at 14:12
-
@RandyMorris Thanks for the hint. I incorporated it into the above posted answer. – Arne Jun 28 '12 at 16:48