Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function to add pretty comments in Emacs
(defun insert-comment-with-description (comment-syntax comment)
"Inserts a comment with '―' (Unicode character: U+2015) on each side."
(let* ((comment-length (length comment))
(current-column-pos (current-column))
(space-on-each-side (/ (- fill-column
current-column-pos
comment-length
(length comment-syntax)
;; Single space on each side of comment
(if (> comment-length 0) 2 0)
;; Single space after comment syntax sting
1)
2)))
(if (< space-on-each-side 2)
(message "Comment string is too big to fit in one line")
(progn
(insert comment-syntax)
(insert " ")
(dotimes (_ space-on-each-side) (insert ""))
(when (> comment-length 0) (insert " "))
(insert comment)
(when (> comment-length 0) (insert " "))
(dotimes (_ (if (= (% comment-length 2) 0)
space-on-each-side
(- space-on-each-side 1)))
(insert ""))))))
;; For Clojure
(defun clj-insert-comment-with-description ()
"Inserts a pretty Clojure comment."
(interactive)
(insert-comment-with-description ";;" (read-from-minibuffer "Comment: ")))
;; For C
(defun c-insert-comment-with-description ()
"Inserts a pretty C comment."
(interactive)
(insert-comment-with-description "//" (read-from-minibuffer "Comment: ")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.