From ff420e32f0e8b40c35eeb2b3133984fcb20824c7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 19 Feb 2020 01:15:22 +0100 Subject: [PATCH] Emacs: Add next-register and prev-register. --- init.d/text/common.el | 47 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/init.d/text/common.el b/init.d/text/common.el index 601a0e9..9066f8d 100644 --- a/init.d/text/common.el +++ b/init.d/text/common.el @@ -1,6 +1,6 @@ ;;; common.el --- Common settings for text files. -*- lexical-binding: t; -*- -;; Time-stamp: <2020-02-18T19:27:15+0100> +;; Time-stamp: <2020-02-19T01:15:14+0100> ;;; Commentary: @@ -16,9 +16,54 @@ (fill-column 80) ; Documents are 80 chars wide by default. (word-wrap t) ; Wrap at word for continuation lines. + :config + (defvar my/current-marker-register 0 + "An index to the current marker register.") + + (defun my/sort-marker-register-elements (a b) + "Returns `t' if the file name is the same and A < B." + (and (string= (buffer-file-name (marker-buffer (cdr a))) + (buffer-file-name (marker-buffer (cdr b)))) + (< (marker-position (cdr a)) + (marker-position (cdr b))))) + + (defun my/marker-registers () + "Returns all registers which markers." + (let (result) + (dolist (item register-alist) + (when (markerp (cdr item)) + (setq result (cons item result)))) + (sort result 'my/sort-marker-register-elements))) + + (defun my/next-register () + "Jump to next register." + (interactive) + (setq my/current-marker-register (+ my/current-marker-register 1)) + (when (>= my/current-marker-register (length (my/marker-registers))) + (setq my/current-marker-register 0)) + (let (register-element register-name) + (setq register-element + (car (nthcdr my/current-marker-register (my/marker-registers)))) + (setq register-name (car register-element)) + (register-to-point register-name))) + + (defun my/prev-register () + "Jump to previous register." + (interactive) + (setq my/current-marker-register (- my/current-marker-register 1)) + (when (< my/current-marker-register 0) + (setq my/current-marker-register (- (length (my/marker-registers)) 1))) + (let (register-element register-name) + (setq register-element + (car (nthcdr my/current-marker-register (my/marker-registers)))) + (setq register-name (car register-element)) + (register-to-point register-name))) + :bind ("C-r" . jump-to-register) ; Move point to location in register. ("C-S-r" . point-to-register) ; Store location of point in register. + ("C-" . my/prev-register) ; Jump to previous marker register. + ("C-" . my/next-register) ; Jump to next marker register. :hook (text-mode . auto-fill-mode) ; Enable word-wrapping at fill-column.