-
-
Save linktohack/28105f209a391b6f7bc8 to your computer and use it in GitHub Desktop.
Regions - multiple selections
This file contains hidden or 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
(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