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.
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.
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)
来源:https://stackoverflow.com/questions/11239201/can-i-limit-the-length-of-the-compilation-buffer-in-emacs