Skip to content

Instantly share code, notes, and snippets.

@seh
Created October 20, 2013 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seh/7073195 to your computer and use it in GitHub Desktop.
Save seh/7073195 to your computer and use it in GitHub Desktop.
Emacs Lisp function to "underline" the current line.
;; a la Graham's "On Lisp"
(defmacro nonzero-bind (var exp &rest body)
`(let ((,var ,exp))
(unless (zerop ,var)
,@body)))
(defun underline-current (&optional str)
"Insert multiple copies of STR under and of same width as current line.
The default underline string is =.
With a prefix argument, prompt for the underline string."
(interactive (list (if current-prefix-arg
(read-string "Underline string: "
"="))))
(save-excursion
(end-of-line)
(nonzero-bind len (current-column)
(newline)
(let* ((str (or str "="))
(slen (length str)))
(cond
((zerop slen)
(error "Underline string must be at least one character."))
((= 1 slen)
(insert-char (aref str 0) len))
(t
(dotimes (i (/ len slen))
(insert str))
(nonzero-bind rem (mod len slen)
(insert-string (substring str 0 rem)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment