blob: 101ba092d8f0d8b3000ea4a7b835f96974a58190 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
;;; custom-comments.el --- Custom Comment Operations -*- coding: utf-8; lexical-binding: t; -*-
;;
;;; Commentary:
;; This module provides custom comment formatting and manipulation utilities for code editing.
;;
;; Functions include:
;; - deleting all comments in a buffer,
;; - reformatting commented text into single-line paragraphs,
;; - creating centered comment headers with customizable separator characters,
;; - creating comment boxes around text
;; - inserting hyphen-style centered comments.
;;
;; 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
(autoload 'cj/join-line-or-region "custom-line-paragraph" nil t)
;; --------------------------- Delete Buffer Comments --------------------------
(defun cj/delete-buffer-comments ()
"Delete all comments within the current buffer."
(interactive)
(goto-char (point-min))
(let (kill-ring)
(comment-kill (count-lines (point-min) (point-max)))))
;; ------------------------------ Comment Reformat -----------------------------
(defun cj/comment-reformat ()
"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 )))
;; if no region
(message "No region was selected. Select the comment lines to reformat."))
;; ------------------------------ Comment Centered -----------------------------
(defun cj/comment-centered (&optional comment-char)
"Insert comment text centered around the COMMENT-CHAR character.
Default to the hash character when COMMENT-CHAR is nil.
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 "#"))
(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))))))
;; -------------------------------- Comment Box --------------------------------
(defun cj/comment-box ()
"Insert a comment box around text that the user inputs.
The box extends to the fill column, centers the text, and uses the current
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)))))
;; ------------------------------- Comment Hyphen ------------------------------
(defun cj/comment-hyphen()
"Insert a centered comment with `-' (hyphens) on each side.
Leverages cj/comment-centered."
(interactive)
(cj/comment-centered "-"))
;; ------------------------------- Comment Keymap ------------------------------
(defvar-keymap cj/comment-map
:doc "Keymap for code comment operations"
"r" #'cj/comment-reformat
"c" #'cj/comment-centered
"-" #'cj/comment-hyphen
"b" #'cj/comment-box
"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.
|