From 29a160f087a0e530e415a9f2676abb390c54f910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barra=20=C3=93=20Cath=C3=A1in?= Date: Tue, 17 Sep 2024 00:11:11 +0100 Subject: [PATCH] Add file-to-register and buffer-to-register. * lisp/bindings.el (ctl-x-r-map): Add bindings to the register keymap for the two new functions. 'F' for file-to-register and 'B' for buffer-to-register. * lisp/register.el (register-command-info): Registers the two new functions, file-to-register and buffer-to-register as commands. (file-to-register): New function, stores a file into a register. Used interactively, prompts for REGISTER using `register-read-with-preview'. With a prefix-argument, prompts for FILE-NAME using `read-file-name', otherwise, uses the currently visited file or directory for FILE-NAME. (buffer-to-register): New function, stores a buffer into a register. Used interactively, prompts for REGISTER using `register-read-with-preview'. With a prefix-argument, prompts for BUFFER-NAME using `read-buffer', otherwise, uses the current buffer. * doc/emacs/regs.texi (File and Buffer Registers): Added documentation for the new file-to-register and buffer-to-register commands, replacing the elisp snippet that was used previously. --- doc/emacs/regs.texi | 55 ++++++++++++++++++++++++--------------------- lisp/bindings.el | 4 +++- lisp/register.el | 41 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/doc/emacs/regs.texi b/doc/emacs/regs.texi index 734d704a272..446b42395ba 100644 --- a/doc/emacs/regs.texi +++ b/doc/emacs/regs.texi @@ -289,41 +289,46 @@ File and Buffer Registers @section Keeping File and Buffer Names in Registers @cindex saving file name in a register @cindex saving buffer name in a register +@kindex C-x r F +@kindex C-x r B 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}: +conveniently if you put their names in registers. Here's how you can +put a file into a buffer. -@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}. +@table @kbd +@item C-x r F @var{r} +@kindex C-x r F +@findex file-to-register +Store the currently visited file or directory into register @var{r} (@code{file-to-register}). +@item C-u C-x r F @var{r} +@kindex C-x r F +@findex file-to-register +Prompt for a file, and store into register @var{r} (@code{file-to-register}). +@end table 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: + Similarly, if there are certain buffers you visit frequently, you can +put their names in registers. You can use the following command to put a +buffer into a register. -@smallexample -(set-register ?m '(buffer . "*Messages*")) -@end smallexample +@table @kbd +@item C-x r B @var{r} +@kindex C-x r B +@findex buffer-to-register +Store the current buffer into register @var{r} (@code{buffer-to-register}). +@item C-u C-x r B @var{r} +@kindex C-x r B +@findex buffer-to-register +Prompt for a buffer, and store into 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}}. + To visit the buffer 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.) @node Keyboard Macro Registers @section Keyboard Macro Registers 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 @@ ctl-x-r-map "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..01be41314c5 100644 --- a/lisp/register.el +++ b/lisp/register.el @@ -300,6 +300,18 @@ register-command-info :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. @@ -688,6 +700,35 @@ jump-to-register (let ((val (get-register register))) (register-val-jump-to val delete))) +(defun file-to-register (file-name register) + "Inserts FILE-NAME in REGISTER. +To visit the file, use \\[jump-to-register]. + +Interactively, prompts for REGISTER using `register-read-with-preview'. +With a prefix-argument, prompts for FILE-NAME using `read-file-name', +otherwise, uses 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) + "Inserts BUFFER in REGISTER. +To visit the buffer, use \\[jump-to-register]. + +Interactively, prompts for REGISTER using `register-read-with-preview'. +With a prefix-argument, prompts for BUFFER-NAME using `read-buffer', +otherwise, uses the current buffer." + (interactive (list (if (eq current-prefix-arg nil) + (current-buffer) + (read-buffer "Buffer: ")) + (register-read-with-preview "Buffer to register: "))) + (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'. -- 2.46.1