Skip to content

Instantly share code, notes, and snippets.

@linktohack
Last active January 4, 2016 23:38
Show Gist options
  • Save linktohack/28105f209a391b6f7bc8 to your computer and use it in GitHub Desktop.
Save linktohack/28105f209a391b6f7bc8 to your computer and use it in GitHub Desktop.
Regions - multiple selections
(provide 'regions)
(defvar regions nil
"Regions in buffer.")
(defun regions-maybe-push-selection ()
"Push mark and point if point is not in regions."
(let ((point (point))
exists)
(dolist (sel regions)
(when (= point (cdr sel))
(setq exists t)))
(unless exists
(push (cons (mark) (point)) regions))))
(defun regions-maybe-merge ()
"Sort and merge overlap selections."
(unless (>= 1 (length regions))
(setq regions
(sort regions #'(lambda (a b) (< (car a) (car b)))))
;; I'm pretty sure there should be a more lipsy way
(let ((i 0)
(j 1)
(sel (nth 0 regions))
new-regions
a b
(len (length regions))
(cont t))
(while cont
(setq a (cdr sel)
b (car (nth j regions)))
(if (<= a b)
(progn
(push sel new-regions)
(setq i j
j (1+ i)
sel (nth i regions)))
(setcdr sel (cdr (nth j regions)))
(incf j))
(when (>= j len)
(incf i)
(setq j (1+ i)))
(when (>= i (1- len))
(push sel new-regions)
(setq cont nil)))
(setq regions new-regions))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment