about summary refs log tree commit diff stats
path: root/lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lisp')
-rw-r--r--lisp/acdw.el21
1 files changed, 14 insertions, 7 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index 0790f2e..472b4ab 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el
@@ -265,10 +265,16 @@ With a prefix argument N, (un)comment that many sexps."
265;; from https://github.com/alphapapa/unpackaged.el#sort-sexps 265;; from https://github.com/alphapapa/unpackaged.el#sort-sexps
266;; and https://github.com/alphapapa/unpackaged.el/issues/20 266;; and https://github.com/alphapapa/unpackaged.el/issues/20
267 267
268(defun sort-sexps (beg end &optional key) 268(defun sort-sexps (beg end &optional key-fn sort-fn)
269 "Sort sexps between BEG and END. 269 "Sort sexps between BEG and END.
270Comments stay with the code below. KEY is a function to sort by, 270Comments stay with the code below.
271e.g. (lambda (sexp) (symbol-name (car sexp)))" 271
272Optional argument KEY-FN will determine where in each sexp to
273start sorting. e.g. (lambda (sexp) (symbol-name (car sexp)))
274
275Optional argument SORT-FN will determine how to sort two sexps'
276strings. It's passed to `sort'. By default, it sorts the sexps
277with `string<' starting with the key determined by KEY-FN."
272 (interactive "r") 278 (interactive "r")
273 (cl-flet ((skip-whitespace () (while (looking-at (rx (1+ (or space "\n")))) 279 (cl-flet ((skip-whitespace () (while (looking-at (rx (1+ (or space "\n"))))
274 (goto-char (match-end 0)))) 280 (goto-char (match-end 0))))
@@ -298,8 +304,8 @@ e.g. (lambda (sexp) (symbol-name (car sexp)))"
298 (save-excursion 304 (save-excursion
299 (goto-char (marker-position start)) 305 (goto-char (marker-position start))
300 (skip-both) 306 (skip-both)
301 (if key 307 (if key-fn
302 (funcall key sexp) 308 (funcall key-fn sexp)
303 (buffer-substring 309 (buffer-substring
304 (point) 310 (point)
305 (marker-position end))))) 311 (marker-position end)))))
@@ -307,8 +313,9 @@ e.g. (lambda (sexp) (symbol-name (car sexp)))"
307 collect (cons start end) 313 collect (cons start end)
308 into markers 314 into markers
309 finally return (list sexps markers)) 315 finally return (list sexps markers))
310 (setq sexps (sort sexps (lambda (a b) 316 (setq sexps (sort sexps (if sort-fn sort-fn
311 (string< (cdr a) (cdr b))))) 317 (lambda (a b)
318 (string< (cdr a) (cdr b))))))
312 (cl-loop for (real . sort) in sexps 319 (cl-loop for (real . sort) in sexps
313 for (start . end) in markers 320 for (start . end) in markers
314 do (progn 321 do (progn