Last active
December 2, 2018 13:33
-
-
Save ksqsf/b9b28f539332ba46d1a5bbb7107ea96b to your computer and use it in GitHub Desktop.
Region ring
This file contains 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
(defvar region-ring nil) | |
(defvar region-ring-max 60) | |
(defun push-region () | |
"Push the current region into the global region ring. The | |
current mark will not be popped off the mark ring." | |
(interactive) | |
(let ((buffer (current-buffer)) | |
(mark (copy-marker (mark-marker))) | |
(point (point-marker))) | |
(if (or (null mark) | |
(null point)) | |
(error "Mark not set; no region available") | |
(save-excursion | |
(let ((tail (if (= (length region-ring) region-ring-max) | |
(cdr region-ring) | |
region-ring))) | |
(setq region-ring (cons (list buffer mark point) tail))))))) | |
(defun pop-region () | |
"Pop off region ring and go to the actual position." | |
(interactive) | |
(if (null region-ring) | |
(error "Region ring is empty!") | |
(let* ((top (car region-ring)) | |
(buffer (car top)) | |
(mark (cadr top)) | |
(point (caddr top))) | |
(switch-to-buffer buffer) | |
(push-mark) | |
(set-mark mark) | |
(goto-char point) | |
(setq region-ring (cdr region-ring))))) | |
;;; whatever key binding you want | |
;; (global-set-key (kbd "<f5>") #'push-region) | |
;; (global-set-key (kbd "<f6>") #'pop-region) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: I wasn't aware of the existence of
ring
in Emacs, which is a generic ring data structure.