summaryrefslogtreecommitdiff
path: root/modules/custom-comments.el
diff options
context:
space:
mode:
Diffstat (limited to 'modules/custom-comments.el')
-rw-r--r--modules/custom-comments.el219
1 files changed, 110 insertions, 109 deletions
diff --git a/modules/custom-comments.el b/modules/custom-comments.el
index 16f10235..101ba092 100644
--- a/modules/custom-comments.el
+++ b/modules/custom-comments.el
@@ -13,7 +13,6 @@
;; These utilities help create consistent, well-formatted code comments and section headers.
;; Bound to keymap prefix: C-; C
;;
-;;
;;; Code:
(eval-when-compile (defvar cj/custom-keymap)) ;; cj/custom-keymap defined in keybindings.el
@@ -26,7 +25,7 @@
(interactive)
(goto-char (point-min))
(let (kill-ring)
- (comment-kill (count-lines (point-min) (point-max)))))
+ (comment-kill (count-lines (point-min) (point-max)))))
;; ------------------------------ Comment Reformat -----------------------------
@@ -34,14 +33,14 @@
"Reformat commented text into a single paragraph."
(interactive)
(if mark-active
- (let ((beg (region-beginning))
- (end (copy-marker (region-end)))
- (orig-fill-column fill-column))
- (uncomment-region beg end)
- (setq fill-column (- fill-column 3))
- (cj/join-line-or-region beg end)
- (comment-region beg end)
- (setq fill-column orig-fill-column )))
+ (let ((beg (region-beginning))
+ (end (copy-marker (region-end)))
+ (orig-fill-column fill-column))
+ (uncomment-region beg end)
+ (setq fill-column (- fill-column 3))
+ (cj/join-line-or-region beg end)
+ (comment-region beg end)
+ (setq fill-column orig-fill-column )))
;; if no region
(message "No region was selected. Select the comment lines to reformat."))
@@ -54,42 +53,42 @@ Use the lesser of `fill-column' or 80 to calculate the comment length.
Begin and end line with the appropriate comment symbols for the current mode."
(interactive)
(if (not (char-or-string-p comment-char))
- (setq comment-char "#"))
+ (setq comment-char "#"))
(let* ((comment (capitalize (string-trim (read-from-minibuffer "Comment: "))))
- (fill-column (min fill-column 80))
- (comment-length (length comment))
- ;; (comment-start-length (length comment-start))
- ;; (comment-end-length (length comment-end))
- (current-column-pos (current-column))
- (space-on-each-side (/ (- fill-column
- current-column-pos
- comment-length
- (length comment-start)
- (length comment-end)
- ;; 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-start)
- (when (equal comment-start ";") ;; emacs-lisp line comments are ';;'
- (insert comment-start)) ;; so insert comment-char again
- (insert " ")
- (dotimes (_ space-on-each-side) (insert comment-char))
- (when (> comment-length 0) (insert " "))
- (insert comment)
- (when (> comment-length 0) (insert " "))
- (dotimes (_ (if (= (% comment-length 2) 0)
- (- space-on-each-side 1)
- space-on-each-side))
- (insert comment-char))
- ;; Only insert trailing space and comment-end if comment-end is not empty
- (when (not (string-empty-p comment-end))
- (insert " ")
- (insert comment-end))))))
+ (fill-column (min fill-column 80))
+ (comment-length (length comment))
+ ;; (comment-start-length (length comment-start))
+ ;; (comment-end-length (length comment-end))
+ (current-column-pos (current-column))
+ (space-on-each-side (/ (- fill-column
+ current-column-pos
+ comment-length
+ (length comment-start)
+ (length comment-end)
+ ;; 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-start)
+ (when (equal comment-start ";") ;; emacs-lisp line comments are ';;'
+ (insert comment-start)) ;; so insert comment-char again
+ (insert " ")
+ (dotimes (_ space-on-each-side) (insert comment-char))
+ (when (> comment-length 0) (insert " "))
+ (insert comment)
+ (when (> comment-length 0) (insert " "))
+ (dotimes (_ (if (= (% comment-length 2) 0)
+ (- space-on-each-side 1)
+ space-on-each-side))
+ (insert comment-char))
+ ;; Only insert trailing space and comment-end if comment-end is not empty
+ (when (not (string-empty-p comment-end))
+ (insert " ")
+ (insert comment-end))))))
;; -------------------------------- Comment Box --------------------------------
@@ -100,81 +99,80 @@ mode's comment syntax at both the beginning and end of each line. The box
respects the current indentation level and avoids trailing whitespace."
(interactive)
(let* ((comment-char (if (equal comment-start ";") ";;"
- (string-trim comment-start)))
- (comment-end-char (if (string-empty-p comment-end)
- comment-char
- (string-trim comment-end)))
- (line-char (if (equal comment-char ";;") "-" "#"))
- (comment (capitalize (string-trim (read-from-minibuffer "Comment: "))))
- (comment-length (length comment))
- (current-column-pos (current-column))
- (max-width (min fill-column 80))
- ;; Calculate available width between comment markers
- (available-width (- max-width
- current-column-pos
- (length comment-char)
- (length comment-end-char)))
- ;; Inner width is the width without the spaces after comment start and before comment end
- (inner-width (- available-width 2))
- ;; Calculate padding for each side of the centered text
- (padding-each-side (max 1 (/ (- inner-width comment-length) 2)))
- ;; Adjust for odd-length comments
- (right-padding (if (= (% (- inner-width comment-length) 2) 0)
- padding-each-side
- (1+ padding-each-side))))
-
- ;; Check if we have enough space
- (if (< inner-width (+ comment-length 4)) ; minimum sensible width
- (message "Comment string is too big to fit in one line")
- (progn
- ;; Top line - fill entirely with line characters except for space after comment start
- (insert comment-char)
- (insert " ")
- (insert (make-string inner-width (string-to-char line-char)))
- (insert " ")
- (insert comment-end-char)
- (newline)
-
- ;; Add indentation on the new line to match current column
- (dotimes (_ current-column-pos) (insert " "))
-
- ;; Middle line with centered text
- (insert comment-char)
- (insert " ")
- ;; Left padding
- (dotimes (_ padding-each-side) (insert " "))
- ;; The comment text
- (insert comment)
- ;; Right padding
- (dotimes (_ right-padding) (insert " "))
- (insert " ")
- (insert comment-end-char)
- (newline)
-
- ;; Add indentation on the new line to match current column
- (dotimes (_ current-column-pos) (insert " "))
-
- ;; Bottom line - same as top line
- (insert comment-char)
- (insert " ")
- (dotimes (_ inner-width) (insert line-char))
- (insert " ")
- (insert comment-end-char)
- (newline)))))
+ (string-trim comment-start)))
+ (comment-end-char (if (string-empty-p comment-end)
+ comment-char
+ (string-trim comment-end)))
+ (line-char (if (equal comment-char ";;") "-" "#"))
+ (comment (capitalize (string-trim (read-from-minibuffer "Comment: "))))
+ (comment-length (length comment))
+ (current-column-pos (current-column))
+ (max-width (min fill-column 80))
+ ;; Calculate available width between comment markers
+ (available-width (- max-width
+ current-column-pos
+ (length comment-char)
+ (length comment-end-char)))
+ ;; Inner width is the width without the spaces after comment start and before comment end
+ (inner-width (- available-width 2))
+ ;; Calculate padding for each side of the centered text
+ (padding-each-side (max 1 (/ (- inner-width comment-length) 2)))
+ ;; Adjust for odd-length comments
+ (right-padding (if (= (% (- inner-width comment-length) 2) 0)
+ padding-each-side
+ (1+ padding-each-side))))
+
+ ;; Check if we have enough space
+ (if (< inner-width (+ comment-length 4)) ; minimum sensible width
+ (message "Comment string is too big to fit in one line")
+ (progn
+ ;; Top line - fill entirely with line characters except for space after comment start
+ (insert comment-char)
+ (insert " ")
+ (insert (make-string inner-width (string-to-char line-char)))
+ (insert " ")
+ (insert comment-end-char)
+ (newline)
+
+ ;; Add indentation on the new line to match current column
+ (dotimes (_ current-column-pos) (insert " "))
+
+ ;; Middle line with centered text
+ (insert comment-char)
+ (insert " ")
+ ;; Left padding
+ (dotimes (_ padding-each-side) (insert " "))
+ ;; The comment text
+ (insert comment)
+ ;; Right padding
+ (dotimes (_ right-padding) (insert " "))
+ (insert " ")
+ (insert comment-end-char)
+ (newline)
+
+ ;; Add indentation on the new line to match current column
+ (dotimes (_ current-column-pos) (insert " "))
+
+ ;; Bottom line - same as top line
+ (insert comment-char)
+ (insert " ")
+ (dotimes (_ inner-width) (insert line-char))
+ (insert " ")
+ (insert comment-end-char)
+ (newline)))))
;; ------------------------------- Comment Hyphen ------------------------------
(defun cj/comment-hyphen()
- "Insert a centered comment with '-' (hyphens) on each side.
+ "Insert a centered comment with `-' (hyphens) on each side.
Leverages cj/comment-centered."
(interactive)
(cj/comment-centered "-"))
;; ------------------------------- Comment Keymap ------------------------------
-;; Comment styles & comment removal keymap.
(defvar-keymap cj/comment-map
- :doc "Keymap for code comment operations."
+ :doc "Keymap for code comment operations"
"r" #'cj/comment-reformat
"c" #'cj/comment-centered
"-" #'cj/comment-hyphen
@@ -182,5 +180,8 @@ Leverages cj/comment-centered."
"D" #'cj/delete-buffer-comments)
(keymap-set cj/custom-keymap "C" cj/comment-map)
+(with-eval-after-load 'which-key
+ (which-key-add-key-based-replacements "C-; C" "code comment menu"))
+
(provide 'custom-comments)
;;; custom-comments.el ends here.