summary refs log tree commit diff stats
path: root/lisp/acdw.el
diff options
context:
space:
mode:
authorCase Duckworth2021-05-22 16:40:18 -0500
committerCase Duckworth2021-05-22 16:40:18 -0500
commitec19fb5f78036a5fe5f36eab713d79164f087a5a (patch)
tree5647443b2a8d221437babbcb2c9bc8abc9d14d14 /lisp/acdw.el
parentMerge branch 'main' of https://tildegit.org/acdw/emacs (diff)
downloademacs-ec19fb5f78036a5fe5f36eab713d79164f087a5a.tar.gz
emacs-ec19fb5f78036a5fe5f36eab713d79164f087a5a.zip
Add `sort-sexps'
from unpackaged.el
Diffstat (limited to 'lisp/acdw.el')
-rw-r--r--lisp/acdw.el49
1 files changed, 49 insertions, 0 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index 8b87dd5..f607695 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el
@@ -196,6 +196,55 @@ With a prefix argument N, (un)comment that many sexps."
196 (comment-sexp--raw)))) 196 (comment-sexp--raw))))
197 197
198 198
199;;; Sort sexps
200;; from https://github.com/alphapapa/unpackaged.el#sort-sexps
201
202(defun sort-sexps (beg end)
203 "Sort sexps in region.
204Comments stay with the code below."
205 (interactive "r")
206 (cl-flet ((skip-whitespace () (while (looking-at (rx (1+ (or space "\n"))))
207 (goto-char (match-end 0))))
208 (skip-both () (while (cond ((or (nth 4 (syntax-ppss))
209 (ignore-errors
210 (save-excursion
211 (forward-char 1)
212 (nth 4 (syntax-ppss)))))
213 (forward-line 1))
214 ((looking-at (rx (1+ (or space "\n"))))
215 (goto-char (match-end 0)))))))
216 (save-excursion
217 (save-restriction
218 (narrow-to-region beg end)
219 (goto-char beg)
220 (skip-both)
221 (cl-destructuring-bind (sexps markers)
222 (cl-loop do (skip-whitespace)
223 for start = (point-marker)
224 for sexp = (ignore-errors
225 (read (current-buffer)))
226 for end = (point-marker)
227 while sexp
228 ;; Collect the real string, then one used for sorting.
229 collect (cons (buffer-substring (marker-position start) (marker-position end))
230 (save-excursion
231 (goto-char (marker-position start))
232 (skip-both)
233 (buffer-substring (point) (marker-position end))))
234 into sexps
235 collect (cons start end)
236 into markers
237 finally return (list sexps markers))
238 (setq sexps (sort sexps (lambda (a b)
239 (string< (cdr a) (cdr b)))))
240 (cl-loop for (real . sort) in sexps
241 for (start . end) in markers
242 do (progn
243 (goto-char (marker-position start))
244 (insert-before-markers real)
245 (delete-region (point) (marker-position end)))))))))
246
247
199;;; Emacs configuration functions 248;;; Emacs configuration functions
200 249
201(defun emacs-git-pull-config (&optional remote branch) 250(defun emacs-git-pull-config (&optional remote branch)