2020-02-19 15:58:45 +01:00
|
|
|
;;; register-quicknav.el --- Quickly jump to next/previous register. -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2020 tastytea
|
|
|
|
|
|
|
|
;; Author: tastytea <tastytea@tastytea.de>
|
|
|
|
;; Version: 0.1.0
|
2020-02-20 01:50:04 +01:00
|
|
|
;; Package-Requires: ((emacs "24"))
|
|
|
|
;; Keywords: convenience
|
2020-02-19 19:23:16 +01:00
|
|
|
;; URL: https://schlomp.space/tastytea/register-quicknav
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2020-02-19 17:02:54 +01:00
|
|
|
;; This package is built on top of `register.el' and allows you to quickly jump
|
|
|
|
;; to the next/previous position register. If you reach the end, the search
|
|
|
|
;; wraps around and continues with the first (or last) register.
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
;; Features:
|
|
|
|
;;
|
|
|
|
;; * Cycle through all position registers in both directions.
|
2020-02-19 16:48:18 +01:00
|
|
|
;; * Clear current register.
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
;; Known limitations:
|
|
|
|
;;
|
|
|
|
;; Works only for as long as the buffer containing the registers is open. If
|
|
|
|
;; you close and reopen it, it won't work anymore.
|
|
|
|
|
|
|
|
;; Installation:
|
|
|
|
;;
|
|
|
|
;; To use `register-quicknav.el', put it in your load-path and add the following
|
2020-02-19 18:54:11 +01:00
|
|
|
;; to your init.el:
|
2020-02-19 15:58:45 +01:00
|
|
|
;;
|
|
|
|
;; (require 'register-quicknav)
|
2020-02-20 01:51:44 +01:00
|
|
|
;; (global-set-key (kbd "<C-f5>") #'register-quicknav/prev-register)
|
|
|
|
;; (global-set-key (kbd "<C-f6>") #'register-quicknav/next-register)
|
|
|
|
;; (global-set-key (kbd "M-r") #'register-quicknav/clear-current-register)
|
2020-02-19 15:58:45 +01:00
|
|
|
;;
|
|
|
|
;; Or, with use-package:
|
|
|
|
;;
|
|
|
|
;; (use-package register-quicknav
|
2020-02-19 16:48:18 +01:00
|
|
|
;; :bind (("C-<f5>" . register-quicknav/prev-register)
|
|
|
|
;; ("C-<f6>" . register-quicknav/next-register)
|
|
|
|
;; ("M-r" . register-quicknav/clear-current-register)))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-02-20 03:25:36 +01:00
|
|
|
;; Variables:
|
2020-02-19 19:00:30 +01:00
|
|
|
;;
|
2020-02-20 03:25:36 +01:00
|
|
|
;; * `register-quicknav/buffer-only': Cycle only through position registers in
|
|
|
|
;; current buffer.
|
2020-02-19 19:00:30 +01:00
|
|
|
|
2020-02-19 15:58:45 +01:00
|
|
|
;;; Code:
|
|
|
|
|
2020-02-20 03:25:36 +01:00
|
|
|
(defgroup register-quicknav nil
|
|
|
|
"Variables for register-quicknav."
|
|
|
|
:group 'editing)
|
|
|
|
|
|
|
|
(defcustom register-quicknav/buffer-only nil
|
|
|
|
"Cycle only through position registers in current buffer."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'register-quicknav)
|
|
|
|
|
2020-02-19 15:58:45 +01:00
|
|
|
(defvar register-quicknav//current-position-register 0
|
|
|
|
"An index to the current position register.")
|
|
|
|
|
|
|
|
(defun register-quicknav//sort-position-register-elements (a b)
|
|
|
|
"Return t if the file name is the same and A < B."
|
|
|
|
(let ((marker-a (cdr a))
|
|
|
|
(marker-b (cdr b)))
|
|
|
|
(and (string= (buffer-file-name (marker-buffer marker-a))
|
|
|
|
(buffer-file-name (marker-buffer marker-b)))
|
|
|
|
(< (marker-position marker-a)
|
|
|
|
(marker-position marker-b)))))
|
|
|
|
|
|
|
|
(defun register-quicknav//registers ()
|
|
|
|
"Return all position registers, sorted by file name and position."
|
|
|
|
(let (result)
|
|
|
|
(dolist (item register-alist)
|
|
|
|
(when (markerp (cdr item))
|
2020-02-20 03:25:36 +01:00
|
|
|
(if register-quicknav/buffer-only
|
|
|
|
(when (eq (current-buffer) (marker-buffer (cdr item)))
|
|
|
|
(setq result (cons item result)))
|
|
|
|
(setq result (cons item result)))))
|
2020-02-20 01:51:44 +01:00
|
|
|
(sort result #'register-quicknav//sort-position-register-elements)))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
(defun register-quicknav/next-register ()
|
2020-02-19 20:01:33 +01:00
|
|
|
"Jump to next position register."
|
2020-02-19 15:58:45 +01:00
|
|
|
(interactive)
|
2020-02-19 16:27:57 +01:00
|
|
|
(let ((pos register-quicknav//current-position-register)
|
|
|
|
(registers (register-quicknav//registers)))
|
|
|
|
(setq pos (+ pos 1))
|
|
|
|
(when (>= pos (length registers))
|
|
|
|
(setq pos 0))
|
|
|
|
(setq register-quicknav//current-position-register pos)
|
|
|
|
(register-to-point (car (nth pos registers)))))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
(defun register-quicknav/prev-register ()
|
2020-02-19 20:01:33 +01:00
|
|
|
"Jump to previous position register."
|
2020-02-19 15:58:45 +01:00
|
|
|
(interactive)
|
2020-02-19 16:27:57 +01:00
|
|
|
(let ((pos register-quicknav//current-position-register)
|
|
|
|
(registers (register-quicknav//registers)))
|
|
|
|
(setq pos (- pos 1))
|
|
|
|
(when (< pos 0)
|
|
|
|
(setq pos (- (length registers) 1)))
|
|
|
|
(setq register-quicknav//current-position-register pos)
|
|
|
|
(register-to-point (car (nth pos registers)))))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-02-19 16:48:18 +01:00
|
|
|
(defun register-quicknav/clear-current-register ()
|
2020-02-19 18:50:04 +01:00
|
|
|
"Clear currently selected position register.
|
|
|
|
To be more precise, it deletes the
|
|
|
|
`register-quicknav//current-position-register'th position
|
|
|
|
register, as reported by `register-quicknav//registers', from
|
|
|
|
`register-alist'."
|
2020-02-19 16:48:18 +01:00
|
|
|
(interactive)
|
|
|
|
(let ((pos register-quicknav//current-position-register)
|
|
|
|
(registers (register-quicknav//registers)))
|
2020-02-20 02:32:39 +01:00
|
|
|
(setq register-alist (delq (nth pos registers) register-alist))))
|
2020-02-19 16:48:18 +01:00
|
|
|
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
(provide 'register-quicknav)
|
|
|
|
;;; register-quicknav.el ends here
|