emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 048e72a403e 1/2: Add file/buffer-to-register (Bug#73308)


From: Eli Zaretskii
Subject: master 048e72a403e 1/2: Add file/buffer-to-register (Bug#73308)
Date: Sat, 5 Oct 2024 06:07:43 -0400 (EDT)

branch: master
commit 048e72a403e22dc394b265c6ff290b8d40e806a5
Author: Barra Ó Catháin <barra@ocathain.ie>
Commit: Eli Zaretskii <eliz@gnu.org>

    Add file/buffer-to-register (Bug#73308)
    
    * doc/emacs/regs.texi (File and Buffer Registers): Update
    documentation to refer to 'file-to-register' and
    'buffer-to-register'.
    * etc/NEWS: Announce the new commands.
    * lisp/bindings.el (ctl-x-r-map): Map new commands into the
    register keymap.
    * lisp/register.el (register-command-info): Register new commands.
    (jump-to-register): Remove docstring line referring to using
    set-register instead of new commands.
    (file-to-register): Add function for storing files in registers.
    (buffer-to-register): Add function for storing buffers in
    registers.
    (register-buffer-to-file-query): Add function for converting
    buffer registers to file-query registers on killing a buffer.
---
 doc/emacs/regs.texi | 65 ++++++++++++++++++++++++++---------------------------
 etc/NEWS            |  7 ++++++
 lisp/bindings.el    |  4 +++-
 lisp/register.el    | 55 ++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 96 insertions(+), 35 deletions(-)

diff --git a/doc/emacs/regs.texi b/doc/emacs/regs.texi
index 734d704a272..df3c22742b6 100644
--- a/doc/emacs/regs.texi
+++ b/doc/emacs/regs.texi
@@ -290,40 +290,39 @@ numeric argument stores zero in the register.
 @cindex saving file name in a register
 @cindex saving buffer name in a register
 
-  If you visit certain file names frequently, you can visit them more
-conveniently if you put their names in registers.  Here's the Lisp code
-used to put a file @var{name} into register @var{r}:
-
-@smallexample
-(set-register @var{r} '(file . @var{name}))
-@end smallexample
-
-@need 3000
-@noindent
-For example,
-
-@smallexample
-(set-register ?z '(file . "/gd/gnu/emacs/19.0/src/ChangeLog"))
-@end smallexample
-
-@noindent
-puts the file name shown in register @samp{z}.
-
-  To visit the file whose name is in register @var{r}, type @kbd{C-x r j
-@var{r}}.  (This is the same command used to jump to a position or
-restore a frame configuration.)
-
-  Similarly, if there are certain buffers you visit frequently, you
-can put their names in registers.  For instance, if you visit the
-@samp{*Messages*} buffer often, you can use the following snippet to
-put that buffer into the @samp{m} register:
-
-@smallexample
-(set-register ?m '(buffer . "*Messages*"))
-@end smallexample
+@table @kbd
+@item C-x r F @var{r}
+Store the currently visited file or directory in register
+@var{r} (@code{file-to-register}).
+@item C-x r B @var{r}
+Store the currently visited buffer in register
+@var{r} (@code{buffer-to-register}).
+@end table
 
-  To switch to the buffer whose name is in register @var{r}, type
-@kbd{C-x r j @var{r}}.
+If you visit certain files or buffers frequently, you can visit them
+more conveniently if you put their names in registers.
+
+@kindex C-x r F
+@findex file-to-register
+        Typing @kbd{C-x r F} (@code{file-to-register}), followed by a
+character @kbd{@var{r}}, saves a reference to the currently visited file
+in register @var{r}.  You can then visit the file using
+@code{jump-to-register} by typing @kbd{C-x r j} @var{r}. With a prefix
+argument, @code{file-to-register} prompts for a file name to store in
+the register.
+
+@kindex C-x r B
+@findex buffer-to-register
+        Typing @kbd{C-x r B} (@code{buffer-to-register}), followed by a
+character @kbd{@var{r}}, saves a reference to the currently visited buffer
+in register @var{r}.  You can then revisit the buffer using
+@code{jump-to-register}. With a prefix argument, @code{buffer-to-register}
+prompts for a buffer name to store in the register.
+
+        If you store a buffer name which is visiting a file in a
+register, and the buffer is then closed, the register is automatically
+converted to a file reference, allowing you to quickly re-open the
+closed file.
 
 @node Keyboard Macro Registers
 @section Keyboard Macro Registers
diff --git a/etc/NEWS b/etc/NEWS
index 24fd8ae0e8d..44c618b824d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -124,6 +124,13 @@ This hook allows you to control which tab-bar tabs are 
auto-resized.
 *** New command 'project-root-find-file'.
 It is equivalent to running ‘project-any-command’ with ‘find-file’.
 
+** Registers
+
+*** New functions 'buffer-to-register' and 'file-to-register'.
+These allow users to interactively store file and buffers in registers.
+Killed buffers stored in a register using buffer-to-register are
+automatically converted to a file-query value if the buffer was visiting
+a file.
 
 * Editing Changes in Emacs 31.1
 ** Commands for keyboard translation
diff --git a/lisp/bindings.el b/lisp/bindings.el
index 6b34c5750c1..06a488fa9fa 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -1574,7 +1574,9 @@ if `inhibit-field-text-motion' is non-nil."
   "n"     #'number-to-register
   "+"     #'increment-register
   "w"     #'window-configuration-to-register
-  "f"     #'frameset-to-register)
+  "f"     #'frameset-to-register
+  "F"     #'file-to-register
+  "B"     #'buffer-to-register)
 (define-key ctl-x-map "r" ctl-x-r-map)
 
 (define-key esc-map "q" 'fill-paragraph)
diff --git a/lisp/register.el b/lisp/register.el
index 497848ded1e..ed7b4402a3f 100644
--- a/lisp/register.el
+++ b/lisp/register.el
@@ -300,6 +300,18 @@ If NOCONFIRM is non-nil, request confirmation of register 
name by RET."
    :act 'set
    :noconfirm (memq register-use-preview '(nil never))
    :smatch t))
+(cl-defmethod register-command-info ((_command (eql file-to-register)))
+  (make-register-preview-info
+   :types '(all)
+   :msg "File to register `%s'"
+   :act 'set
+   :noconfirm (memq register-use-preview '(nil never))))
+(cl-defmethod register-command-info ((_command (eql buffer-to-register)))
+  (make-register-preview-info
+   :types '(all)
+   :msg "Buffer to register `%s'"
+   :act 'set
+   :noconfirm (memq register-use-preview '(nil never))))
 
 (defun register-preview-forward-line (arg)
   "Move to next or previous line in register preview buffer.
@@ -672,7 +684,6 @@ Interactively, prompt for REGISTER using 
`register-read-with-preview'."
 Push the mark if going to the location moves point, unless called in 
succession.
 If the register contains a file name, find that file.
 If the register contains a buffer name, switch to that buffer.
-\(To put a file or buffer name in a register, you must use `set-register'.)
 If the register contains a window configuration (one frame) or a frameset
 \(all frames), restore the configuration of that frame or of all frames
 accordingly.
@@ -688,6 +699,36 @@ Interactively, prompt for REGISTER using 
`register-read-with-preview'."
   (let ((val (get-register register)))
     (register-val-jump-to val delete)))
 
+(defun file-to-register (file-name register)
+  "Insert FILE-NAME into REGISTER.
+To visit the file, use \\[jump-to-register].
+
+Interactively, prompt for REGISTER using `register-read-with-preview'.
+With a prefix-argument, prompt for FILE-NAME using `read-file-name',
+With no prefix-argument, use the currently visited file or directory for 
FILE-NAME."
+  (interactive (list (if (eq current-prefix-arg nil)
+                         (if (eq major-mode 'dired-mode)
+                             (dired-current-directory)
+                           (buffer-file-name))
+                       (read-file-name "File: "))
+                (register-read-with-preview "File to register: ")))
+  (unless (eq file-name nil)
+    (set-register register (cons 'file file-name))))
+
+(defun buffer-to-register (buffer register)
+  "Insert BUFFER into REGISTER.
+To visit the buffer, use \\[jump-to-register].
+
+Interactively, prompt for REGISTER using `register-read-with-preview'.
+With a prefix-argument, prompt for BUFFER-NAME using `read-buffer',
+With no prefix-argument, use the current buffer for BUFFER."
+  (interactive (list (if (eq current-prefix-arg nil)
+                         (current-buffer)
+                       (read-buffer "Buffer: "))
+                     (register-read-with-preview "Buffer to register: ")))
+  (add-hook 'kill-buffer-hook 'register-buffer-to-file-query nil t)
+  (set-register register (cons 'buffer buffer)))
+
 (cl-defgeneric register-val-jump-to (_val _arg)
   "Execute the \"jump\" operation of VAL.
 VAL is the contents of a register as returned by `get-register'.
@@ -739,6 +780,18 @@ ARG is the value of the prefix argument or nil."
                            buffer-file-name
                            (marker-position (cdr elem))))))))
 
+(defun register-buffer-to-file-query ()
+  "Turn buffer registers into file-query references when a buffer is killed."
+  (and buffer-file-name
+       (dolist (elem register-alist)
+         (and (consp (cdr elem))
+              (eq (current-buffer) (cddr elem))
+              (setcdr elem
+                      (list 'file-query
+                            buffer-file-name
+                            (point)))))))
+
+
 (defun number-to-register (number register)
   "Store NUMBER in REGISTER.
 REGISTER is a character, the name of the register.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]