From 5380cfb76277c64dd264d5312f5146d6cac96f97 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 15 Sep 2021 23:32:16 -0500 Subject: Re-sort init.el --- lisp/acdw.el | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'lisp/acdw.el') 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." ;; from https://github.com/alphapapa/unpackaged.el#sort-sexps ;; and https://github.com/alphapapa/unpackaged.el/issues/20 -(defun sort-sexps (beg end &optional key) +(defun sort-sexps (beg end &optional key-fn sort-fn) "Sort sexps between BEG and END. -Comments stay with the code below. KEY is a function to sort by, -e.g. (lambda (sexp) (symbol-name (car sexp)))" +Comments stay with the code below. + +Optional argument KEY-FN will determine where in each sexp to +start sorting. e.g. (lambda (sexp) (symbol-name (car sexp))) + +Optional argument SORT-FN will determine how to sort two sexps' +strings. It's passed to `sort'. By default, it sorts the sexps +with `string<' starting with the key determined by KEY-FN." (interactive "r") (cl-flet ((skip-whitespace () (while (looking-at (rx (1+ (or space "\n")))) (goto-char (match-end 0)))) @@ -298,8 +304,8 @@ e.g. (lambda (sexp) (symbol-name (car sexp)))" (save-excursion (goto-char (marker-position start)) (skip-both) - (if key - (funcall key sexp) + (if key-fn + (funcall key-fn sexp) (buffer-substring (point) (marker-position end))))) @@ -307,8 +313,9 @@ e.g. (lambda (sexp) (symbol-name (car sexp)))" collect (cons start end) into markers finally return (list sexps markers)) - (setq sexps (sort sexps (lambda (a b) - (string< (cdr a) (cdr b))))) + (setq sexps (sort sexps (if sort-fn sort-fn + (lambda (a b) + (string< (cdr a) (cdr b)))))) (cl-loop for (real . sort) in sexps for (start . end) in markers do (progn -- cgit 1.4.1-21-gabe81