2020-02-20 13:51:00 +01:00
|
|
|
;;; register-quicknav.el --- Quickly jump to next/previous register -*- lexical-binding: t; -*-
|
2020-02-19 15:58:45 +01:00
|
|
|
|
|
|
|
;; Copyright (C) 2020 tastytea
|
|
|
|
|
|
|
|
;; Author: tastytea <tastytea@tastytea.de>
|
2020-03-07 15:14:32 +01:00
|
|
|
;; Version: 0.2.0
|
2020-03-02 10:16:13 +01:00
|
|
|
;; Package-Requires: ((emacs "24.3"))
|
2020-02-20 01:50:04 +01:00
|
|
|
;; 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
|
|
|
|
|
|
|
;; Installation:
|
|
|
|
;;
|
2020-03-01 06:42:50 +01:00
|
|
|
;; **Note:** The function and variable names were previously separated by “/”
|
|
|
|
;; from the namespace. To conform with MELPA rules the separator has been
|
|
|
|
;; changed to “-”.
|
|
|
|
;;
|
2020-02-19 15:58:45 +01:00
|
|
|
;; 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-03-01 06:42:50 +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-03-01 06:42:50 +01:00
|
|
|
;; :bind (("C-<f5>" . register-quicknav-prev-register)
|
|
|
|
;; ("C-<f6>" . register-quicknav-next-register)
|
|
|
|
;; ("M-r" . register-quicknav-clear-current-register)))
|
2020-02-24 22:12:09 +01:00
|
|
|
;;
|
|
|
|
;; Instead of manually copying `register-quicknav.el' into your load-path, you
|
|
|
|
;; can use [quelpa](https://github.com/quelpa/quelpa):
|
|
|
|
;;
|
|
|
|
;; (quelpa '(register-quicknav
|
|
|
|
;; :fetcher git
|
|
|
|
;; :url "https://schlomp.space/tastytea/register-quicknav.git"))
|
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-03-01 06:42:50 +01:00
|
|
|
;; * `register-quicknav-buffer-only': Cycle only through position registers in
|
2020-02-20 03:25:36 +01:00
|
|
|
;; current buffer.
|
2020-02-19 19:00:30 +01:00
|
|
|
|
2020-02-19 15:58:45 +01:00
|
|
|
;;; Code:
|
|
|
|
|
2020-03-02 10:16:13 +01:00
|
|
|
(require 'cl-lib)
|
|
|
|
|
2020-03-01 06:42:50 +01:00
|
|
|
(define-obsolete-variable-alias 'register-quicknav/buffer-only
|
|
|
|
'register-quicknav-buffer-only "0.1.1")
|
2020-03-02 09:41:22 +01:00
|
|
|
(define-obsolete-function-alias 'register-quicknav/next-register
|
2020-03-01 06:42:50 +01:00
|
|
|
#'register-quicknav-next-register "0.1.1")
|
2020-03-02 09:41:22 +01:00
|
|
|
(define-obsolete-function-alias 'register-quicknav/prev-register
|
2020-03-01 06:42:50 +01:00
|
|
|
#'register-quicknav-prev-register "0.1.1")
|
2020-03-02 09:41:22 +01:00
|
|
|
(define-obsolete-function-alias 'register-quicknav/clear-current-register
|
2020-03-01 06:42:50 +01:00
|
|
|
#'register-quicknav-clear-current-register "0.1.1")
|
|
|
|
|
2020-02-20 03:25:36 +01:00
|
|
|
(defgroup register-quicknav nil
|
|
|
|
"Variables for register-quicknav."
|
|
|
|
:group 'editing)
|
|
|
|
|
2020-03-01 06:42:50 +01:00
|
|
|
(defcustom register-quicknav-buffer-only nil
|
2020-02-20 03:25:36 +01:00
|
|
|
"Cycle only through position registers in current buffer."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'register-quicknav)
|
|
|
|
|
2020-03-08 04:12:00 +01:00
|
|
|
(defvar-local register-quicknav--last-register-v nil
|
2020-03-07 19:56:54 +01:00
|
|
|
"The last jumped-to position register.")
|
|
|
|
|
2020-03-08 04:12:00 +01:00
|
|
|
(defun register-quicknav--last-register (&optional val)
|
|
|
|
"Return `register-quicknav--last-register-v' or set it if VAL is non-nil.
|
|
|
|
Use the local value if `register-quicknav-buffer-only' is set and
|
|
|
|
the global value otherwise."
|
|
|
|
(if val
|
|
|
|
(if register-quicknav-buffer-only
|
|
|
|
(setq register-quicknav--last-register-v val)
|
|
|
|
(setq-default register-quicknav--last-register-v val))
|
|
|
|
(if register-quicknav-buffer-only
|
|
|
|
register-quicknav--last-register-v
|
|
|
|
(default-value 'register-quicknav--last-register-v))))
|
|
|
|
|
2020-03-07 19:56:54 +01:00
|
|
|
(defun register-quicknav--item-file-name (item)
|
|
|
|
"Return file-name of ITEM.
|
|
|
|
Works on markers and file-queries."
|
|
|
|
(if (markerp (cdr item))
|
|
|
|
(buffer-file-name (marker-buffer (cdr item)))
|
|
|
|
(nth 2 item)))
|
|
|
|
|
|
|
|
(defun register-quicknav--is-current-buffer? (item)
|
|
|
|
"Return t if ITEM is in current buffer.
|
|
|
|
Works on markers and file-queries."
|
|
|
|
(if (markerp (cdr item))
|
|
|
|
(eq (current-buffer) (marker-buffer (cdr item)))
|
|
|
|
(string= (buffer-file-name (current-buffer))
|
|
|
|
(register-quicknav--item-file-name item))))
|
|
|
|
|
|
|
|
(defun register-quicknav--item-position (item)
|
|
|
|
"Return position of ITEM.
|
|
|
|
Works on markers and file-queries."
|
|
|
|
(if (markerp (cdr item))
|
|
|
|
(marker-position (cdr item))
|
|
|
|
(nth 3 item)))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-03-01 06:42:50 +01:00
|
|
|
(defun register-quicknav--registers ()
|
2020-02-20 03:30:51 +01:00
|
|
|
"Return all position registers, sorted by file name and position.
|
2020-03-01 06:42:50 +01:00
|
|
|
If `register-quicknav-buffer-only' is t, return only registers in
|
2020-02-20 03:30:51 +01:00
|
|
|
current buffer."
|
2020-03-07 19:56:54 +01:00
|
|
|
(cl-flet ((sort-registers
|
|
|
|
(lambda (a b)
|
|
|
|
"Return t if position of A is < B.
|
2020-03-07 15:14:32 +01:00
|
|
|
Works on markers and file-queries."
|
2020-03-07 19:56:54 +01:00
|
|
|
(and (string= (register-quicknav--item-file-name a)
|
|
|
|
(register-quicknav--item-file-name b))
|
|
|
|
(< (register-quicknav--item-position a)
|
|
|
|
(register-quicknav--item-position b))))))
|
2020-03-02 10:32:01 +01:00
|
|
|
(let ((result))
|
|
|
|
(dolist (item register-alist)
|
2020-03-07 15:14:32 +01:00
|
|
|
(if (or (markerp (cdr item))
|
|
|
|
(eq (nth 1 item) 'file-query))
|
|
|
|
(if register-quicknav-buffer-only
|
2020-03-07 19:56:54 +01:00
|
|
|
(when (register-quicknav--is-current-buffer? item)
|
2020-03-07 15:14:32 +01:00
|
|
|
(push item result))
|
|
|
|
(push item result))))
|
2020-03-02 10:32:01 +01:00
|
|
|
(sort result #'sort-registers))))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-03-07 19:56:54 +01:00
|
|
|
(defun register-quicknav--jump-to-register (next)
|
|
|
|
"Jump to next position register if NEXT is t, to previous otherwise."
|
|
|
|
(let* ((registers (register-quicknav--registers))
|
2020-03-08 04:12:00 +01:00
|
|
|
(index (cl-position (register-quicknav--last-register) registers))
|
2020-03-07 19:56:54 +01:00
|
|
|
(stop-searching))
|
|
|
|
(unless (eq index nil)
|
|
|
|
(if next
|
|
|
|
(cl-decf index)
|
|
|
|
(cl-incf index)))
|
|
|
|
|
|
|
|
;; Try to find the position register closest to point.
|
|
|
|
(dolist (item registers)
|
|
|
|
(when (register-quicknav--is-current-buffer? item)
|
|
|
|
(let ((item-pos (register-quicknav--item-position item)))
|
|
|
|
(if next
|
|
|
|
(when (<= item-pos (point))
|
|
|
|
(setq index (cl-position item registers)))
|
|
|
|
(when (and (not stop-searching) (>= item-pos (point)))
|
|
|
|
(setq index (cl-position item registers)
|
|
|
|
stop-searching t))))))
|
|
|
|
|
|
|
|
;; If an index was found, set it to the next/previous register. If not, set
|
|
|
|
;; it to the first/last.
|
|
|
|
(if index
|
|
|
|
(progn
|
|
|
|
(when (> index (- (length registers) 1))
|
|
|
|
(setq index nil))
|
|
|
|
(if next
|
|
|
|
(progn
|
|
|
|
(if (or (eq index nil) (eq index (- (length registers) 1)))
|
|
|
|
(setq index 0)
|
|
|
|
(cl-incf index)))
|
|
|
|
(if (or (eq index nil) (eq index 0))
|
|
|
|
(setq index (- (length registers) 1))
|
|
|
|
(cl-decf index)))
|
|
|
|
(register-to-point (car (nth index registers)))
|
2020-03-08 04:12:00 +01:00
|
|
|
(register-quicknav--last-register (nth index registers)))
|
2020-03-07 19:56:54 +01:00
|
|
|
(register-to-point (car (car registers)))
|
2020-03-08 04:12:00 +01:00
|
|
|
(register-quicknav--last-register (car registers)))))
|
2020-03-07 19:56:54 +01:00
|
|
|
|
2020-02-28 06:17:18 +01:00
|
|
|
;;;###autoload
|
2020-03-01 06:42:50 +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-03-07 19:56:54 +01:00
|
|
|
(register-quicknav--jump-to-register t))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-02-28 06:17:18 +01:00
|
|
|
;;;###autoload
|
2020-03-01 06:42:50 +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-03-07 19:56:54 +01:00
|
|
|
(register-quicknav--jump-to-register nil))
|
2020-02-19 15:58:45 +01:00
|
|
|
|
2020-02-28 06:17:18 +01:00
|
|
|
;;;###autoload
|
2020-03-01 06:42:50 +01:00
|
|
|
(defun register-quicknav-clear-current-register ()
|
2020-03-07 19:56:54 +01:00
|
|
|
"Clear last jumped-to position register from `register-alist'."
|
2020-02-19 16:48:18 +01:00
|
|
|
(interactive)
|
2020-03-08 04:12:00 +01:00
|
|
|
(setq register-alist
|
|
|
|
(delq (register-quicknav--last-register) 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
|