Skip to content

Instantly share code, notes, and snippets.

@jarohen
Last active August 29, 2015 14:16
Show Gist options
  • Save jarohen/6f8adf4df28d0fef3b4e to your computer and use it in GitHub Desktop.
Save jarohen/6f8adf4df28d0fef3b4e to your computer and use it in GitHub Desktop.
In Clojure, I'd like to transpose *pairs* of s-exps (i.e. KVs in a map)
;; {:foo bar|
;; :baz quux}
;;
;; =>
;;
;; {:baz quux
;; :foo bar|}
;; This is my first attempt at ELisp of a 'bigger-than-hacking-around-in-init.el' size - please be kind!
;; At the same time, any feedback would be really valuable!
;; I've essentially tried to automate what I'd do with the usual Paredit functions - not sure if this
;; is the right way to go about things?
(defun transpose-sexp-pairs ()
(interactive)
(let ((mid-point (point)))
(paredit-forward 1)
(paredit-backward 1)
(let ((second-start (point)))
(paredit-forward 2)
(let ((second-end (point)))
(paredit-backward 4)
(let ((first-start (point)))
(paredit-forward 2)
(let ((first-end (point)))
(let ((first-pair (buffer-substring first-start first-end))
(second-pair (buffer-substring second-start second-end)))
(goto-char second-start)
(delete-region second-start second-end)
(insert first-pair)
(goto-char first-start)
(delete-region first-start first-end)
(insert second-pair)
(paredit-forward 2))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment