Skip to content

Instantly share code, notes, and snippets.

@suvratapte
Last active May 14, 2019 11:44
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 suvratapte/fc3f5bfbc9c9d649e3ef080ec66bf9d5 to your computer and use it in GitHub Desktop.
Save suvratapte/fc3f5bfbc9c9d649e3ef080ec66bf9d5 to your computer and use it in GitHub Desktop.
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