Last active
August 29, 2015 14:12
-
-
Save sachac/c8e27a543d9d422023dc to your computer and use it in GitHub Desktop.
Sorting sexps alphabetically. May be handy for finding accidental duplicates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun sacha/sort-sexps-in-region (beg end) | |
"Can be handy for sorting out duplicates. | |
Sorts the sexps from BEG to END. Leaves the point at where it | |
couldn't figure things out (ex: syntax errors)." | |
(interactive "r") | |
(let ((input (buffer-substring beg end)) | |
list last-point form result) | |
(save-restriction | |
(save-excursion | |
(narrow-to-region beg end) | |
(goto-char (point-min)) | |
(setq last-point (point-min)) | |
(setq form t) | |
(while (and form (not (eobp))) | |
(setq form (ignore-errors (read (current-buffer)))) | |
(when form | |
(add-to-list | |
'list | |
(cons | |
(prin1-to-string form) | |
(buffer-substring last-point (point)))) | |
(setq last-point (point)))) | |
(setq list (sort list (lambda (a b) (string< (car a) (car b))))) | |
(delete-region (point-min) (point)) | |
(insert (mapconcat 'cdr list "\n")))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment