Skip to content

Instantly share code, notes, and snippets.

@jimblandy
Last active February 10, 2016 05:57
Show Gist options
  • Save jimblandy/951b9a19f8b6a00bba20 to your computer and use it in GitHub Desktop.
Save jimblandy/951b9a19f8b6a00bba20 to your computer and use it in GitHub Desktop.
(defun jimb-rust-clean-error ()
"Clean up an error message from rustc to fit nicely in the book.
Run this command with point on the first line of an error message
copied from rustc and pasted into a Markdown file; we'll clean it
up and indent it as a code block."
(interactive)
(save-excursion
(forward-line 0)
(let ((top (point)))
(let ((line-was-interesting t)
(code-indentation 100000)
last-filename-length)
(while line-was-interesting
(cond
((looking-at "[^ \t:]*:[0-9]+:[0-9]+: [0-9]+:[0-9]+ ")
(delete-region (match-beginning 0) (match-end 0)))
((looking-at "[^ \t:]*:[0-9]+ ")
(setq last-filename-length (- (match-end 0) (match-beginning 0)))
(delete-region (match-beginning 0) (match-end 0)))
((looking-at "\\s-*^")
(delete-region (point) (+ (point) last-filename-length)))
((looking-at "note: ") t)
(t (setq line-was-interesting nil)))
(when line-was-interesting
(unless (looking-at "\\(error\\|warning\\|note\\): ")
(skip-chars-forward " \t")
(setq code-indentation (min code-indentation (current-column))))
(forward-line 1)))
;; Remove common indentation from quoted code.
(let ((bottom (point-marker)))
(goto-char top)
(while (< (point) bottom)
;; Does this line consist of only whitespace for the next
;; common-indentation columns?
(let ((start (point)))
(skip-chars-forward " \t")
(when (>= (current-column) code-indentation)
(move-to-column code-indentation)
(delete-region start (point))))
(forward-line 1))
;; Now indent the whole shebang to turn it into a Markdown code block.
(goto-char top)
(while (< (point) bottom)
(insert " ")
(forward-line 1)))))))
(provide 'rust-book-md)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment