Subject: [PATCH] Add buffer-local register commands to doc-view

From: Visuwesh <[email-masked]>
Date: Mon, 16 Sep 2024 17:40:18 +0530

Attached patch adds buffer-local registers to doc-view-mode.  I have
opted to make the registers buffer-local since I personally use the same
mnemonic to save pages to the register in multiple buffers.  It is
something like folding a page or placing a pen in between to quickly go
to-and-fro between those pages.

In GNU Emacs 31.0.50 (build 7, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.18.0, Xaw scroll bars) of 2024-09-09 built on astatine
Repository revision: 7d7aa65f63db78c5732f1580213fc3767b767a4a
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101011
System Description: Debian GNU/Linux trixie/sid

Configured using:
 'configure --with-sound=alsa --with-x-toolkit=lucid --without-xaw3d
 --without-gconf --without-libsystemd --with-cairo'

>From 415f3ba0ad07f113858eaa45b60b9bd66b09c7da Mon Sep 17 00:00:00 2001
From: Visuwesh <[email-masked]>
Date: Sun, 15 Sep 2024 13:56:21 +0530
Subject: [PATCH] Add buffer-local register commands to doc-view

* lisp/doc-view.el (doc-view-register-alist): New defvar to keep
track of buffer-local register-alist.
(doc-view-page-to-register, doc-view-jump-to-register): Add new
commands to set and jump to buffer-local registers.
(register-val-insert, register-val-describe)
(register-val-jump-to): Register defmethod to save and restore
doc-view registers.
(doc-view-mode-map): Bind the new commands.
* doc/emacs/misc.texi (DocView Navigation): Document the new
commands.
* etc/NEWS: Announce the change.
---
 doc/emacs/misc.texi | 10 +++++++++
 etc/NEWS            |  6 +++++
 lisp/doc-view.el    | 54 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi
index eb157c146e7..e19e554fb26 100644
--- a/doc/emacs/misc.texi
+++ b/doc/emacs/misc.texi
@@ -593,6 +593,16 @@ DocView Navigation
 displayed using the variables @code{doc-view-imenu-format} and
 @code{doc-view-imenu-flatten}.
 
+@findex doc-view-page-to-register
+@findex doc-view-jump-to-register
+@kindex m @r{(DocView mode)}
+@kindex ' @r{(DocView mode)}
+  You can save the current page to a register with @kbd{m}
+(@code{doc-view-page-to-register}) (@pxref{Registers}).  However, these
+registers are not shared across buffers and stay local to the DocView
+buffer.  You can later jump to the register with @kbd{'}
+(@code{doc-view-jump-to-register}).
+
 @node DocView Searching
 @subsection DocView Searching
 
diff --git a/etc/NEWS b/etc/NEWS
index c6f8b0062e4..607665a71bb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -262,6 +262,12 @@ When switching to the plain text contents with 'doc-view-open-text',
 DocView now creates a dedicated buffer to display it.  'C-c C-c' gets you
 back to real DocView buffer if it still exists.
 
++++
+*** New commands to save and restore pages in buffer local registers.
+Docview can store current page to buffer local registers with the new
+command 'doc-view-page-to-register' (bound to 'm'), and later can be
+restored with 'doc-view-jump-to-register' (bound to ''').
+
 ** Tramp
 
 +++
diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index 0d89d63e03e..395993e6263 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -556,7 +556,10 @@ doc-view-mode-map
   "C-c C-c" #'doc-view-toggle-display
   ;; Open a new buffer with doc's text contents
   "C-c C-t" #'doc-view-open-text
-  "r"       #'revert-buffer)
+  "r"       #'revert-buffer
+  ;; Registers
+  "m"       #'doc-view-page-to-register
+  "'"       #'doc-view-jump-to-register)
 
 (define-obsolete-function-alias 'doc-view-revert-buffer #'revert-buffer "27.1")
 (defvar revert-buffer-preserve-modes)
@@ -2468,6 +2471,55 @@ doc-view-bookmark-jump
 
 (put 'doc-view-bookmark-jump 'bookmark-handler-type "DocView")
 
+;;; Register integration
+
+(defvar-local doc-view-register-alist nil
+  "Register alist containing only doc-view registers for current buffer.
+Each doc-view register entry is of the form (doc-view . ALIST) where
+ALIST has the keys `buffer', `file', and `page'.  `buffer' is the buffer
+the `file' is visiting.  `page' is the page number to be show.")
+
+(defun doc-view-page-to-register (register)
+  "Store the current page to the register REGISTER."
+  (interactive
+   (let ((register-alist doc-view-register-alist))
+     (list (register-read-with-preview "Page to register: "))))
+  (let ((register-alist doc-view-register-alist))
+    (set-register register
+                  `(doc-view
+                    (buffer . ,(current-buffer))
+                    (file . ,(buffer-file-name))
+                    (page . ,(doc-view-current-page))))
+    (setq doc-view-register-alist register-alist)))
+
+(defun doc-view-jump-to-register (register)
+  "Jump to the register REGISTER."
+  (interactive
+   (let ((register-alist doc-view-register-alist))
+     (list (register-read-with-preview "Jump to register: "))))
+  (let ((register-alist doc-view-register-alist))
+    (jump-to-register register)))
+
+(cl-defmethod register-val-insert ((val (head doc-view)))
+  (prin1 val))
+
+(cl-defmethod register-val-describe ((val (head doc-view)) _verbose)
+  (let* ((alist (cdr val))
+         (name (or (file-name-nondirectory (alist-get 'file alist))
+                   (buffer-name (alist-get 'buffer alist)))))
+    (princ name)
+    (princ " p. ")
+    (princ (alist-get 'page alist))))
+
+(cl-defmethod register-val-jump-to ((val (head doc-view)) _arg)
+  (let* ((alist (cdr val))
+         (buffer (or (alist-get 'buffer alist)
+                     (find-buffer-visiting (alist-get 'file alist)))))
+    (unless buffer
+      (user-error "Cannot find the doc-view buffer to jump to"))
+    (switch-to-buffer buffer)
+    (doc-view-goto-page (alist-get 'page alist))))
+
 ;; Obsolete.
 
 (defun doc-view-intersection (l1 l2)
-- 
2.45.2