-
Notifications
You must be signed in to change notification settings - Fork 0
/
pel-comment.el
176 lines (152 loc) · 6.48 KB
/
pel-comment.el
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
;;; pel-comment.el --- PEL Comments Utilities -*-lexical-binding: t-*-
;; Copyright (C) 2020, 2021, 2023, 2024 Pierre Rouleau
;; Author: Pierre Rouleau <[email protected]>
;; This file is part of the PEL package
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; --------------------------------------------------------------------------
;;; Commentary:
;;
;; This file holds a collection of functions used to manipulate,insert
;; comments in the current buffer and provide information about the way
;; comments are handled.
;;
;; The file holds the following commands:
;; * pel-comment-start
;; * pel-comment-middle
;; * pel-comment-end
;; * pel-toggle-comment-auto-fill-only-comments
;; * pel-delete-all-comments
;; * pel-kill-all-comments
;;; --------------------------------------------------------------------------
;;; Dependencies:
;;
;; Elisp functions taken from files always loaded
;; - from: newcomment ; use: comment-kill
;; - from: simple ; use kill-ring
(require 'pel--base) ; use: pel-toggle,
;; ; pel-print-in-buffer
(require 'pel-ccp) ; use: pel-delete-all-empty-lines
(eval-when-compile (require 'subr-x)) ; use: dolist, with-current-buffer
;;; --------------------------------------------------------------------------
;;; Code:
;;-pel-autoload
(defun pel-comment-start (string)
"Show and set comment start STRING for current mode."
(interactive
(let ((string
(read-string
(format "Comment start string 「%s」: " comment-start)
comment-start)))
(list string)))
(setq comment-start string))
;;-pel-autoload
(defun pel-comment-middle (string)
"Show and set comment continue/middle STRING for current mode."
(interactive
(let ((string
(read-string
(format "Comment middle/continue string 「%s」: " comment-continue)
comment-continue)))
(list string)))
(setq comment-continue string))
;;-pel-autoload
(defun pel-comment-end (string)
"Show and set comment end STRING for current mode."
(interactive
(let ((string
(read-string
(format "Comment end string 「%s」: " comment-end)
comment-end)))
(list string)))
(setq comment-end string))
;;-pel-autoload
(defun pel-toggle-comment-auto-fill-only-comments ()
"Toggle the variable `comment-auto-fill-only-comments'.
Activate/de-activate automatic filling in source code comments only."
(interactive)
(pel-toggle 'comment-auto-fill-only-comments))
;;-pel-autoload
(defun pel-kill-all-comments ()
"Kill all comments in current (possibly narrowed) buffer or marked region.
Retain them in the variable `kill-ring'.
Each killed comment group is retained in the kill ring, as a separate kill
ring entry."
(interactive "*")
(let ((start-point (if (use-region-p) (region-beginning) (point-min)))
(end-point (if (use-region-p) (region-end) (point-max))))
(save-excursion
(goto-char start-point)
(comment-kill (count-lines start-point end-point)))))
;;-pel-autoload
(defun pel-delete-all-comments (&optional keep-empty-lines)
"Delete all comments in current (possibly narrowed) buffer or marked region."
(interactive "*P")
(let (kill-ring)
(pel-kill-all-comments)
(unless keep-empty-lines
(let ((start-point (if (use-region-p) (region-beginning) (point-min)))
(end-point (if (use-region-p) (region-end) (point-max))))
(pel-delete-all-empty-lines start-point end-point )))))
;;-pel-autoload
(defun pel-comment-show-variables (&optional only-user-options)
"Show the names and values of the comment related variables.
Open a *comment-vars* buffer and insert the information at the end of the
buffer. Move last line of text to the last line of window.
If the ONLY-USER-OPTIONS argument is non-nil, include only user option
variables."
(interactive "P")
(let ((pel-insert-symbol-content-context-buffer (current-buffer))
(comment-symbols '())
(max-length 0)
(local-values-alist '())) ; maps symbol to it's buffer local value
;; Accumulate names of `comment-' variables and their values.
(mapatoms (lambda (symbol)
(when (and (boundp symbol)
(or (not only-user-options)
(get symbol 'custom-type))
(pel-string-starts-with-p
(symbol-name symbol) "comment-"))
(push symbol comment-symbols)
(push (cons symbol (symbol-value symbol))
local-values-alist))))
;; Sort their names.
(setq comment-symbols
(sort comment-symbols (lambda (s1 s2)
(let ((n1 (symbol-name s1)))
(when (> (length n1) max-length)
(setq max-length (length n1)))
(string< n1
(symbol-name s2))))))
;; prepend several other symbols that are related to
;; commenting to the comment-symbols list
(push 'auto-fill-function comment-symbols)
(push 'normal-auto-fill-function comment-symbols)
(push 'fill-column comment-symbols)
(push 'major-mode comment-symbols)
;; Print a report inside a specialized buffer.
(pel-print-in-buffer
"*comment-vars*"
"Comment controlling variables"
(lambda ()
"Print comment control data."
(insert (format "----- List of comment %s:\n"
(if only-user-options
"user options"
"variables")))
(dolist (symbol comment-symbols)
(pel-insert-symbol-content-line symbol))))))
;;; --------------------------------------------------------------------------
(provide 'pel-comment)
;;; pel-comment.el ends here