Skip to content

Instantly share code, notes, and snippets.

@spacebat
Created November 29, 2016 13:14
Show Gist options
  • Save spacebat/994f42291352397bfb48945b3a48f4ac to your computer and use it in GitHub Desktop.
Save spacebat/994f42291352397bfb48945b3a48f4ac to your computer and use it in GitHub Desktop.
An Emacs global minor mode that prevents lines in compilation buffers from growing too long
(defvar truncated-compilation-line-limit 70)
(defvar truncated-compilation-line-trailer "…")
;; TODO: convert this from a post filter hook to advice on a
;; configured set of filter functions to prevent the insertion of text
;; constituting overlong lines in the first place
;;;###autoload
(defun truncate-compilation-long-lines ()
"Emacs doesn't cope well with extremely long
lines. Unfortunately some processes like grep, ack, ag, rg are
prone to matching minified files or otherwise extremely long
lines. Once Added to compilation-filter-hook, this function
truncates lines returned by the compilation process."
(cl-flet ((truncate-line (pos)
(let* ((beginning (progn (beginning-of-line) (point)))
(ending (progn (end-of-line) (point)))
(length (- ending beginning))
(excess (max (- length truncated-compilation-line-limit))))
(when (plusp excess)
(delete-region (- ending excess) ending)
(when truncated-compilation-line-trailer
(insert truncated-compilation-line-trailer))))))
(goto-char compilation-filter-start)
(cl-loop do (truncate-line (point))
(forward-line 1)
(end-of-line)
(when (= (point) (point-max))
(return)))))
;;;###autoload
(define-minor-mode truncated-compilation-mode
"Limit the length of lines in compilation buffers"
:global t
(cond
(truncated-compilation-mode
(add-hook 'compilation-filter-hook 'truncate-compilation-long-lines))
(t
(remove-hook 'compilation-filter-hook 'truncate-compilation-long-lines))))
(provide 'truncated-compilation-mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment