summary refs log tree commit diff stats
path: root/lisp
diff options
context:
space:
mode:
authorCase Duckworth2021-05-21 11:08:41 -0500
committerCase Duckworth2021-05-21 11:08:41 -0500
commit4e945fa7bc9e86fc0616930195eb3fbea74c4f7e (patch)
tree2ded2169a74abd84e74f7006fdb398b4083242d0 /lisp
parentFix arity of consult-sensible-* functions (diff)
downloademacs-4e945fa7bc9e86fc0616930195eb3fbea74c4f7e.tar.gz
emacs-4e945fa7bc9e86fc0616930195eb3fbea74c4f7e.zip
Add `comment-or-uncomment-sexp'
from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html
Diffstat (limited to 'lisp')
-rw-r--r--lisp/acdw.el90
1 files changed, 90 insertions, 0 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index c48c4e3..8b87dd5 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el
@@ -106,6 +106,96 @@ is unfocused."
106 (message "%s... Done." ,message))) 106 (message "%s... Done." ,message)))
107 107
108 108
109;;; Comment-or-uncomment-sexp
110;; from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html
111
112(defun uncomment-sexp (&optional n)
113 "Uncomment a sexp around point."
114 (interactive "P")
115 (let* ((initial-point (point-marker))
116 (inhibit-field-text-motion t)
117 (p)
118 (end (save-excursion
119 (when (elt (syntax-ppss) 4)
120 (re-search-backward comment-start-skip
121 (line-beginning-position)
122 t))
123 (setq p (point-marker))
124 (comment-forward (point-max))
125 (point-marker)))
126 (beg (save-excursion
127 (forward-line 0)
128 (while (and (not (bobp))
129 (= end (save-excursion
130 (comment-forward (point-max))
131 (point))))
132 (forward-line -1))
133 (goto-char (line-end-position))
134 (re-search-backward comment-start-skip
135 (line-beginning-position)
136 t)
137 (ignore-errors
138 (while (looking-at-p comment-start-skip)
139 (forward-char -1)))
140 (point-marker))))
141 (unless (= beg end)
142 (uncomment-region beg end)
143 (goto-char p)
144 ;; Indentify the "top-level" sexp inside the comment.
145 (while (and (ignore-errors (backward-up-list) t)
146 (>= (point) beg))
147 (skip-chars-backward (rx (syntax expression-prefix)))
148 (setq p (point-marker)))
149 ;; Re-comment everything before it.
150 (ignore-errors
151 (comment-region beg p))
152 ;; And everything after it.
153 (goto-char p)
154 (forward-sexp (or n 1))
155 (skip-chars-forward "\r\n[:blank:]")
156 (if (< (point) end)
157 (ignore-errors
158 (comment-region (point) end))
159 ;; If this is a closing delimiter, pull it up.
160 (goto-char end)
161 (skip-chars-forward "\r\n[:blank:]")
162 (when (eq 5 (car (syntax-after (point))))
163 (delete-indentation))))
164 ;; Without a prefix, it's more useful to leave point where
165 ;; it was.
166 (unless n
167 (goto-char initial-point))))
168
169(defun comment-sexp--raw ()
170 "Comment the sexp at point or ahead of point."
171 (pcase (or (bounds-of-thing-at-point 'sexp)
172 (save-excursion
173 (skip-chars-forward "\r\n[:blank:]")
174 (bounds-of-thing-at-point 'sexp)))
175 (`(,l . ,r)
176 (goto-char r)
177 (skip-chars-forward "\r\n[:blank:]")
178 (save-excursion
179 (comment-region l r))
180 (skip-chars-forward "\r\n[:blank:]"))))
181
182(defun comment-or-uncomment-sexp (&optional n)
183 "Comment the sexp at point and move past it.
184If already inside (or before) a comment, uncomment instead.
185With a prefix argument N, (un)comment that many sexps."
186 (interactive "P")
187 (if (or (elt (syntax-ppss) 4)
188 (< (save-excursion
189 (skip-chars-forward "\r\n[:blank:]")
190 (point))
191 (save-excursion
192 (comment-forward 1)
193 (point))))
194 (uncomment-sexp n)
195 (dotimes (_ (or n 1))
196 (comment-sexp--raw))))
197
198
109;;; Emacs configuration functions 199;;; Emacs configuration functions
110 200
111(defun emacs-git-pull-config (&optional remote branch) 201(defun emacs-git-pull-config (&optional remote branch)