Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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