I wrote a rather clunky elisp function to sort the SYM VAL args in a setq expression alphabetically by SYM. For example:
(setq c "blah" (setq a 9 b '(more stuff) --> b '(more stuff) a 9) c "blah")
After reading about sort-subr, I thought I might be able to build a cleaner implementation. I thought I had it, but something's not right. What am I doing wrong here?
(defun sort-setq () (interactive) (save-excursion (save-restriction (let ((sort-end (progn (end-of-defun) (backward-char) (point-marker))) (sort-beg (progn (beginning-of-defun) (re-search-forward "[ \\t]*(" (point-at-eol)) (forward-sexp) (re-search-forward "\\<" (point-at-eol)) (point-marker)))) (narrow-to-region (1- sort-beg) (1+ sort-end)) (sort-subr nil #'sort-setq-next-record #'sort-setq-end-record))))) (defun sort-setq-next-record () (condition-case nil (progn (forward-sexp 3) (backward-sexp)) ('scan-error (end-of-buffer)))) (defun sort-setq-end-record () (condition-case nil (forward-sexp 2) ('scan-error (end-of-buffer))))
It seems to work for the 3-pair example above, but if I start adding more pairs it fails to sort them all.