Skip to content

Instantly share code, notes, and snippets.

@satyr
Created July 17, 2010 14:55
Show Gist options
  • Save satyr/479555 to your computer and use it in GitHub Desktop.
Save satyr/479555 to your computer and use it in GitHub Desktop.
enclose-sexp-or-selection
(defun enclose-sexp-or-selection (ncls)
(interactive "sEnclosure (split with #\\RET): ")
(if (equal ncls "") (setq ncls "()"))
(let ((type (get-selection-type))
ps bg nd mp pp bp)
(if (setq ps (position #\RET ncls))
(setf bg (substring ncls 0 ps)
nd (substring ncls (1+ ps)))
(setf ps (ceiling (/ (length ncls) 2))
bg (substring ncls 0 ps)
nd (substring ncls (- ps))))
(cond
((eq type 3)
(setf mp (selection-mark)
pp (selection-point)
bp (min mp pp)
ps (pre-selection-p))
(kill-rectangle-selection)
(setq *rectangle-kill-buffer*
(map 'list (lambda (x) (concat bg x nd)) *rectangle-kill-buffer*))
(goto-char bp)
(yank-rectangle)
(if (< mp pp) (setq bp (prog1 (point) (goto-char bp))))
(start-selection 3 ps)
(goto-char bp))
(type
(ed::map-selection
(lambda (b n)
(insert bg (prog1 (buffer-substring b n) (delete-region b n)) nd))))
(t (backward-sexp) (insert bg)
(forward-sexp) (insert nd))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment