Skip to content

Instantly share code, notes, and snippets.

@sachac
Last active August 29, 2015 14:12
Show Gist options
  • Save sachac/c8e27a543d9d422023dc to your computer and use it in GitHub Desktop.
Save sachac/c8e27a543d9d422023dc to your computer and use it in GitHub Desktop.
Sorting sexps alphabetically. May be handy for finding accidental duplicates.
(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