Skip to content

Instantly share code, notes, and snippets.

@ksqsf
Last active December 2, 2018 13:33
Show Gist options
  • Save ksqsf/b9b28f539332ba46d1a5bbb7107ea96b to your computer and use it in GitHub Desktop.
Save ksqsf/b9b28f539332ba46d1a5bbb7107ea96b to your computer and use it in GitHub Desktop.
Region ring
(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)
@ksqsf
Copy link
Author

ksqsf commented Dec 2, 2018

NOTE: I wasn't aware of the existence of ring in Emacs, which is a generic ring data structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment