1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2018 Free Software Foundation,
6 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
27 ;; This package of functions emulates and somewhat extends the venerable
28 ;; TECO's `register' feature, which permits you to save various useful
29 ;; pieces of buffer state to named variables. The entry points are
30 ;; documented in the Emacs user's manual: (info "(emacs) Registers").
32 (eval-when-compile (require 'cl-lib
))
36 ;; FIXME: Clean up namespace usage!
39 (registerv (:constructor nil
)
40 (:constructor registerv--make
(&optional data print-func
41 jump-func insert-func
))
45 (data nil
:read-only t
)
46 (print-func nil
:read-only t
)
47 (jump-func nil
:read-only t
)
48 (insert-func nil
:read-only t
))
50 (cl-defun registerv-make (data &key print-func jump-func insert-func
)
51 "Create a register value object.
53 DATA can be any value.
54 PRINT-FUNC if provided controls how `list-registers' and
55 `view-register' print the register. It should be a function
56 receiving one argument DATA and print text that completes
58 Register X contains [TEXT PRINTED BY PRINT-FUNC]
59 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
60 INSERT-FUNC if provided, controls how `insert-register' insert the register.
61 They both receive DATA as argument."
62 (registerv--make data print-func jump-func insert-func
))
64 (defvar register-alist nil
65 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
66 NAME is a character (a number). CONTENTS is a string, number, marker, list
67 or a struct returned by `registerv-make'.
68 A list of strings represents a rectangle.
69 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
70 A list of the form (file-query FILE-NAME POSITION) represents
71 position POSITION in the file named FILE-NAME, but query before
73 A list of the form (WINDOW-CONFIGURATION POSITION)
74 represents a saved window configuration plus a saved value of point.
75 A list of the form (FRAME-CONFIGURATION POSITION)
76 represents a saved frame configuration plus a saved value of point.")
78 (defgroup register nil
83 (defcustom register-separator nil
84 "Register containing the text to put between collected texts, or nil if none.
86 When collecting text with \\[append-to-register] (or \\[prepend-to-register]),
87 contents of this register is added to the beginning (or end, respectively)
90 :type
'(choice (const :tag
"None" nil
)
91 (character :tag
"Use register" :value ?
+)))
93 (defcustom register-preview-delay
1
94 "If non-nil, time to wait in seconds before popping up a preview window.
95 If nil, do not show register previews, unless `help-char' (or a member of
96 `help-event-list') is pressed."
98 :type
'(choice number
(const :tag
"No preview unless requested" nil
))
101 (defun get-register (register)
102 "Return contents of Emacs register named REGISTER, or nil if none."
103 (alist-get register register-alist
))
105 (defun set-register (register value
)
106 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
107 See the documentation of the variable `register-alist' for possible VALUEs."
108 (setf (alist-get register register-alist
) value
))
110 (defun register-describe-oneline (c)
111 "One-line description of register C."
112 (let ((d (replace-regexp-in-string
114 (with-output-to-string (describe-register-1 c
)))))
115 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d
)
116 (substring d
(match-end 0))
119 (defun register-preview-default (r)
120 "Default function for the variable `register-preview-function'."
122 (single-key-description (car r
))
123 (register-describe-oneline (car r
))))
125 (defvar register-preview-function
#'register-preview-default
126 "Function to format a register for previewing.
127 Takes one argument, a cons (NAME . CONTENTS) as found in `register-alist'.
130 (defun register-preview (buffer &optional show-empty
)
131 "Pop up a window to show register preview in BUFFER.
132 If SHOW-EMPTY is non-nil show the window even if no registers.
133 Format of each entry is controlled by the variable `register-preview-function'."
134 (when (or show-empty
(consp register-alist
))
135 (with-current-buffer-window
137 (cons 'display-buffer-below-selected
138 '((window-height . fit-window-to-buffer
)
139 (preserve-size .
(nil . t
))))
141 (with-current-buffer standard-output
142 (setq cursor-in-non-selected-windows nil
)
143 (insert (mapconcat register-preview-function register-alist
""))))))
145 (defun register-read-with-preview (prompt)
146 "Read and return a register name, possibly showing existing registers.
147 Prompt with the string PROMPT. If `register-alist' and
148 `register-preview-delay' are both non-nil, display a window
149 listing existing registers after `register-preview-delay' seconds.
150 If `help-char' (or a member of `help-event-list') is pressed,
151 display such a window regardless."
152 (let* ((buffer "*Register Preview*")
153 (timer (when (numberp register-preview-delay
)
154 (run-with-timer register-preview-delay nil
156 (unless (get-buffer-window buffer
)
157 (register-preview buffer
))))))
158 (help-chars (cl-loop for c in
(cons help-char help-event-list
)
159 when
(not (get-register c
))
163 (while (memq (read-key (propertize prompt
'face
'minibuffer-prompt
))
165 (unless (get-buffer-window buffer
)
166 (register-preview buffer
'show-empty
)))
167 (when (or (eq ?\C-g last-input-event
)
168 (eq 'escape last-input-event
)
169 (eq ?\C-\
[ last-input-event
))
171 (if (characterp last-input-event
) last-input-event
172 (error "Non-character input-event")))
173 (and (timerp timer
) (cancel-timer timer
))
174 (let ((w (get-buffer-window buffer
)))
175 (and (window-live-p w
) (delete-window w
)))
176 (and (get-buffer buffer
) (kill-buffer buffer
)))))
178 (defun point-to-register (register &optional arg
)
179 "Store current location of point in register REGISTER.
180 With prefix argument, store current frame configuration.
181 Use \\[jump-to-register] to go to that location or restore that configuration.
182 Argument is a character, naming the register.
184 Interactively, reads the register using `register-read-with-preview'."
185 (interactive (list (register-read-with-preview "Point to register: ")
187 ;; Turn the marker into a file-ref if the buffer is killed.
188 (add-hook 'kill-buffer-hook
'register-swap-out nil t
)
189 (set-register register
190 (if arg
(list (current-frame-configuration) (point-marker))
193 (defun window-configuration-to-register (register &optional _arg
)
194 "Store the window configuration of the selected frame in register REGISTER.
195 Use \\[jump-to-register] to restore the configuration.
196 Argument is a character, naming the register.
198 Interactively, reads the register using `register-read-with-preview'."
199 (interactive (list (register-read-with-preview
200 "Window configuration to register: ")
202 ;; current-window-configuration does not include the value
203 ;; of point in the current buffer, so record that separately.
204 (set-register register
(list (current-window-configuration) (point-marker))))
206 ;; It has had the optional arg for ages, but never used it.
207 (set-advertised-calling-convention 'window-configuration-to-register
210 (defun frame-configuration-to-register (register &optional _arg
)
211 "Store the window configuration of all frames in register REGISTER.
212 Use \\[jump-to-register] to restore the configuration.
213 Argument is a character, naming the register.
215 Interactively, reads the register using `register-read-with-preview'."
216 (interactive (list (register-read-with-preview
217 "Frame configuration to register: ")
219 ;; current-frame-configuration does not include the value
220 ;; of point in the current buffer, so record that separately.
221 (set-register register
(list (current-frame-configuration) (point-marker))))
223 ;; It has had the optional arg for ages, but never used it.
224 (set-advertised-calling-convention 'frame-configuration-to-register
227 (make-obsolete 'frame-configuration-to-register
'frameset-to-register
"24.4")
229 (defalias 'register-to-point
'jump-to-register
)
230 (defun jump-to-register (register &optional delete
)
231 "Move point to location stored in a register.
232 If the register contains a file name, find that file.
233 \(To put a file name in a register, you must use `set-register'.)
234 If the register contains a window configuration (one frame) or a frameset
235 \(all frames), restore that frame or all frames accordingly.
236 First argument is a character, naming the register.
237 Optional second arg non-nil (interactively, prefix argument) says to
238 delete any existing frames that the frameset doesn't mention.
239 \(Otherwise, these frames are iconified.)
241 Interactively, reads the register using `register-read-with-preview'."
242 (interactive (list (register-read-with-preview "Jump to register: ")
244 (let ((val (get-register register
)))
247 (cl-assert (registerv-jump-func val
) nil
248 "Don't know how to jump to register %s"
249 (single-key-description register
))
250 (funcall (registerv-jump-func val
) (registerv-data val
)))
251 ((and (consp val
) (frame-configuration-p (car val
)))
252 (set-frame-configuration (car val
) (not delete
))
253 (goto-char (cadr val
)))
254 ((and (consp val
) (window-configuration-p (car val
)))
255 (set-window-configuration (car val
))
256 (goto-char (cadr val
)))
258 (or (marker-buffer val
)
259 (user-error "That register's buffer no longer exists"))
260 (switch-to-buffer (marker-buffer val
))
261 (unless (or (= (point) (marker-position val
))
262 (eq last-command
'jump-to-register
))
265 ((and (consp val
) (eq (car val
) 'file
))
266 (find-file (cdr val
)))
267 ((and (consp val
) (eq (car val
) 'file-query
))
268 (or (find-buffer-visiting (nth 1 val
))
269 (y-or-n-p (format "Visit file %s again? " (nth 1 val
)))
270 (user-error "Register access aborted"))
271 (find-file (nth 1 val
))
272 (goto-char (nth 2 val
)))
274 (user-error "Register doesn't contain a buffer position or configuration")))))
276 (defun register-swap-out ()
277 "Turn markers into file-query references when a buffer is killed."
278 (and buffer-file-name
279 (dolist (elem register-alist
)
280 (and (markerp (cdr elem
))
281 (eq (marker-buffer (cdr elem
)) (current-buffer))
285 (marker-position (cdr elem
))))))))
287 (defun number-to-register (number register
)
288 "Store a number in a register.
289 Two args, NUMBER and REGISTER (a character, naming the register).
290 If NUMBER is nil, a decimal number is read from the buffer starting
291 at point, and point moves to the end of that number.
292 Interactively, NUMBER is the prefix arg (none means nil).
294 Interactively, reads the register using `register-read-with-preview'."
295 (interactive (list current-prefix-arg
296 (register-read-with-preview "Number to register: ")))
297 (set-register register
299 (prefix-numeric-value number
)
300 (if (looking-at "\\s-*-?[0-9]+")
302 (goto-char (match-end 0))
303 (string-to-number (match-string 0)))
306 (defun increment-register (prefix register
)
307 "Augment contents of REGISTER.
308 Interactively, PREFIX is in raw form.
310 If REGISTER contains a number, add `prefix-numeric-value' of
313 If REGISTER is empty or if it contains text, call
314 `append-to-register' with `delete-flag' set to PREFIX.
316 Interactively, reads the register using `register-read-with-preview'."
317 (interactive (list current-prefix-arg
318 (register-read-with-preview "Increment register: ")))
319 (let ((register-val (get-register register
)))
321 ((numberp register-val
)
322 (let ((number (prefix-numeric-value prefix
)))
323 (set-register register
(+ number register-val
))))
324 ((or (not register-val
) (stringp register-val
))
325 (append-to-register register
(region-beginning) (region-end) prefix
))
326 (t (user-error "Register does not contain a number or text")))))
328 (defun view-register (register)
329 "Display what is contained in register named REGISTER.
330 The Lisp value REGISTER is a character.
332 Interactively, reads the register using `register-read-with-preview'."
333 (interactive (list (register-read-with-preview "View register: ")))
334 (let ((val (get-register register
)))
336 (message "Register %s is empty" (single-key-description register
))
337 (with-output-to-temp-buffer "*Output*"
338 (describe-register-1 register t
)))))
340 (defun list-registers ()
341 "Display a list of nonempty registers saying briefly what they contain."
343 (let ((list (copy-sequence register-alist
)))
344 (setq list
(sort list
(lambda (a b
) (< (car a
) (car b
)))))
345 (with-output-to-temp-buffer "*Output*"
347 (when (get-register (car elt
))
348 (describe-register-1 (car elt
))
351 (defun describe-register-1 (register &optional verbose
)
353 (princ (single-key-description register
))
355 (let ((val (get-register register
)))
358 (if (registerv-print-func val
)
359 (funcall (registerv-print-func val
) (registerv-data val
))
360 (princ "[UNPRINTABLE CONTENTS].")))
366 (let ((buf (marker-buffer val
)))
368 (princ "a marker in no buffer")
369 (princ "a buffer position:\n buffer ")
370 (princ (buffer-name buf
))
371 (princ ", position ")
372 (princ (marker-position val
)))))
374 ((and (consp val
) (window-configuration-p (car val
)))
375 (princ "a window configuration."))
377 ((and (consp val
) (frame-configuration-p (car val
)))
378 (princ "a frame configuration."))
380 ((and (consp val
) (eq (car val
) 'file
))
385 ((and (consp val
) (eq (car val
) 'file-query
))
386 (princ "a file-query reference:\n file ")
387 (prin1 (car (cdr val
)))
388 (princ ",\n position ")
389 (princ (car (cdr (cdr val
))))
395 (princ "the rectangle:\n")
400 (setq val
(cdr val
))))
401 (princ "a rectangle starting with ")
405 (setq val
(copy-sequence val
))
406 (if (eq yank-excluded-properties t
)
407 (set-text-properties 0 (length val
) nil val
)
408 (remove-list-of-text-properties 0 (length val
)
409 yank-excluded-properties val
))
412 (princ "the text:\n")
415 ;; Extract first N characters starting with first non-whitespace.
416 ((string-match (format "[^ \t\n].\\{,%d\\}"
417 ;; Deduct 6 for the spaces inserted below.
418 (min 20 (max 0 (- (window-width) 6))))
420 (princ "text starting with\n ")
421 (princ (match-string 0 val
)))
422 ((string-match "^[ \t\n]+$" val
)
423 (princ "whitespace"))
425 (princ "the empty string")))))
428 (if verbose
(prin1 val
))))))
430 (defun insert-register (register &optional arg
)
431 "Insert contents of register REGISTER. (REGISTER is a character.)
432 Normally puts point before and mark after the inserted text.
433 If optional second arg is non-nil, puts mark before and point after.
434 Interactively, second arg is nil if prefix arg is supplied and t
437 Interactively, reads the register using `register-read-with-preview'."
439 (barf-if-buffer-read-only)
440 (list (register-read-with-preview "Insert register: ")
441 (not current-prefix-arg
))))
443 (let ((val (get-register register
)))
446 (cl-assert (registerv-insert-func val
) nil
447 "Don't know how to insert register %s"
448 (single-key-description register
))
449 (funcall (registerv-insert-func val
) (registerv-data val
)))
451 (insert-rectangle val
))
453 (insert-for-yank val
))
455 (princ val
(current-buffer)))
456 ((and (markerp val
) (marker-position val
))
457 (princ (marker-position val
) (current-buffer)))
459 (user-error "Register does not contain text"))))
460 (if (not arg
) (exchange-point-and-mark)))
462 (defun copy-to-register (register start end
&optional delete-flag region
)
463 "Copy region into register REGISTER.
464 With prefix arg, delete as well.
465 Called from program, takes five args: REGISTER, START, END, DELETE-FLAG,
466 and REGION. START and END are buffer positions indicating what to copy.
467 The optional argument REGION if non-nil, indicates that we're not just
468 copying some text between START and END, but we're copying the region.
470 Interactively, reads the register using `register-read-with-preview'."
471 (interactive (list (register-read-with-preview "Copy to register: ")
476 (set-register register
(if region
477 (funcall region-extract-function delete-flag
)
478 (prog1 (filter-buffer-substring start end
)
479 (if delete-flag
(delete-region start end
)))))
480 (setq deactivate-mark t
)
482 ((called-interactively-p 'interactive
)
483 (indicate-copied-region))))
485 (defun append-to-register (register start end
&optional delete-flag
)
486 "Append region to text in register REGISTER.
487 With prefix arg, delete as well.
488 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
489 START and END are buffer positions indicating what to append.
491 Interactively, reads the register using `register-read-with-preview'."
492 (interactive (list (register-read-with-preview "Append to register: ")
496 (let ((reg (get-register register
))
497 (text (filter-buffer-substring start end
))
498 (separator (and register-separator
(get-register register-separator
))))
500 register
(cond ((not reg
) text
)
501 ((stringp reg
) (concat reg separator text
))
502 (t (user-error "Register does not contain text")))))
503 (setq deactivate-mark t
)
505 (delete-region start end
))
506 ((called-interactively-p 'interactive
)
507 (indicate-copied-region))))
509 (defun prepend-to-register (register start end
&optional delete-flag
)
510 "Prepend region to text in register REGISTER.
511 With prefix arg, delete as well.
512 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
513 START and END are buffer positions indicating what to prepend.
515 Interactively, reads the register using `register-read-with-preview'."
516 (interactive (list (register-read-with-preview "Prepend to register: ")
520 (let ((reg (get-register register
))
521 (text (filter-buffer-substring start end
))
522 (separator (and register-separator
(get-register register-separator
))))
524 register
(cond ((not reg
) text
)
525 ((stringp reg
) (concat text separator reg
))
526 (t (user-error "Register does not contain text")))))
527 (setq deactivate-mark t
)
529 (delete-region start end
))
530 ((called-interactively-p 'interactive
)
531 (indicate-copied-region))))
533 (defun copy-rectangle-to-register (register start end
&optional delete-flag
)
534 "Copy rectangular region into register REGISTER.
535 With prefix arg, delete as well.
536 To insert this register in the buffer, use \\[insert-register].
538 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
539 START and END are buffer positions giving two corners of rectangle.
541 Interactively, reads the register using `register-read-with-preview'."
542 (interactive (list (register-read-with-preview
543 "Copy rectangle to register: ")
547 (let ((rectangle (if delete-flag
548 (delete-extract-rectangle start end
)
549 (extract-rectangle start end
))))
550 (set-register register rectangle
)
551 (when (and (null delete-flag
)
552 (called-interactively-p 'interactive
))
553 (setq deactivate-mark t
)
554 (indicate-copied-region (length (car rectangle
))))))
557 ;;; register.el ends here