summary refs log tree commit diff stats
path: root/lisp/+lisp.el
diff options
context:
space:
mode:
authorCase Duckworth2021-12-29 22:55:55 -0600
committerCase Duckworth2021-12-29 22:55:55 -0600
commit038e5de1adf2de6cdf28a428a44b0753813b928c (patch)
treebeb71cc3157ac2af3dc586fc02b74b906605e338 /lisp/+lisp.el
parentRe-install (new and improved!) filldent.el (diff)
downloademacs-038e5de1adf2de6cdf28a428a44b0753813b928c.tar.gz
emacs-038e5de1adf2de6cdf28a428a44b0753813b928c.zip
Lots and lots of changes, oh jeez
Diffstat (limited to 'lisp/+lisp.el')
-rw-r--r--lisp/+lisp.el89
1 files changed, 89 insertions, 0 deletions
diff --git a/lisp/+lisp.el b/lisp/+lisp.el index 3267fd9..07dfcbd 100644 --- a/lisp/+lisp.el +++ b/lisp/+lisp.el
@@ -67,5 +67,94 @@ with `string<' starting with the key determined by KEY-FN."
67 (insert-before-markers real) 67 (insert-before-markers real)
68 (delete-region (point) (marker-position end)))))))) 68 (delete-region (point) (marker-position end))))))))
69 69
70;;; Comment-or-uncomment-sexp
71;; from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html
72
73(defun +lisp-uncomment-sexp (&optional n)
74 "Uncomment N sexps around point."
75 (interactive "P")
76 (let* ((initial-point (point-marker))
77 (inhibit-field-text-motion t)
78 (p)
79 (end (save-excursion
80 (when (elt (syntax-ppss) 4)
81 (re-search-backward comment-start-skip
82 (line-beginning-position)
83 t))
84 (setq p (point-marker))
85 (comment-forward (point-max))
86 (point-marker)))
87 (beg (save-excursion
88 (forward-line 0)
89 (while (and (not (bobp))
90 (= end (save-excursion
91 (comment-forward (point-max))
92 (point))))
93 (forward-line -1))
94 (goto-char (line-end-position))
95 (re-search-backward comment-start-skip
96 (line-beginning-position)
97 t)
98 (ignore-errors
99 (while (looking-at-p comment-start-skip)
100 (forward-char -1)))
101 (point-marker))))
102 (unless (= beg end)
103 (uncomment-region beg end)
104 (goto-char p)
105 ;; Indentify the "top-level" sexp inside the comment.
106 (while (and (ignore-errors (backward-up-list) t)
107 (>= (point) beg))
108 (skip-chars-backward (rx (syntax expression-prefix)))
109 (setq p (point-marker)))
110 ;; Re-comment everything before it.
111 (ignore-errors
112 (comment-region beg p))
113 ;; And everything after it.
114 (goto-char p)
115 (forward-sexp (or n 1))
116 (skip-chars-forward "\r\n[:blank:]")
117 (if (< (point) end)
118 (ignore-errors
119 (comment-region (point) end))
120 ;; If this is a closing delimiter, pull it up.
121 (goto-char end)
122 (skip-chars-forward "\r\n[:blank:]")
123 (when (eq 5 (car (syntax-after (point))))
124 (delete-indentation))))
125 ;; Without a prefix, it's more useful to leave point where
126 ;; it was.
127 (unless n
128 (goto-char initial-point))))
129
130(defun +lisp-comment-sexp--raw ()
131 "Comment the sexp at point or ahead of point."
132 (pcase (or (bounds-of-thing-at-point 'sexp)
133 (save-excursion
134 (skip-chars-forward "\r\n[:blank:]")
135 (bounds-of-thing-at-point 'sexp)))
136 (`(,l . ,r)
137 (goto-char r)
138 (skip-chars-forward "\r\n[:blank:]")
139 (save-excursion
140 (comment-region l r))
141 (skip-chars-forward "\r\n[:blank:]"))))
142
143(defun +lisp-comment-or-uncomment-sexp (&optional n)
144 "Comment the sexp at point and move past it.
145If already inside (or before) a comment, uncomment instead.
146With a prefix argument N, (un)comment that many sexps."
147 (interactive "P")
148 (if (or (elt (syntax-ppss) 4)
149 (< (save-excursion
150 (skip-chars-forward "\r\n[:blank:]")
151 (point))
152 (save-excursion
153 (comment-forward 1)
154 (point))))
155 (+lisp-uncomment-sexp n)
156 (dotimes (_ (or n 1))
157 (+lisp-comment-sexp--raw))))
158
70(provide '+lisp) 159(provide '+lisp)
71;;; +lisp.el ends here 160;;; +lisp.el ends here