1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2014 Free Software Foundation,
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 <http://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.
32 (eval-when-compile (require 'cl-lib))
37 (registerv (:constructor nil)
38 (:constructor registerv--make (&optional data print-func
39 jump-func insert-func))
43 (data nil :read-only t)
44 (print-func nil :read-only t)
45 (jump-func nil :read-only t)
46 (insert-func nil :read-only t))
48 (cl-defun registerv-make (data &key print-func jump-func insert-func)
49 "Create a register value object.
51 DATA can be any value.
52 PRINT-FUNC if provided controls how `list-registers' and
53 `view-register' print the register. It should be a function
54 receiving one argument DATA and print text that completes
56 Register X contains [TEXT PRINTED BY PRINT-FUNC]
57 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
58 INSERT-FUNC if provided, controls how `insert-register' insert the register.
59 They both receive DATA as argument."
60 (registerv--make data print-func jump-func insert-func))
62 (defvar register-alist nil
63 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
64 NAME is a character (a number). CONTENTS is a string, number, marker, list
65 or a struct returned by `registerv-make'.
66 A list of strings represents a rectangle.
67 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
68 A list of the form (file-query FILE-NAME POSITION) represents
69 position POSITION in the file named FILE-NAME, but query before
71 A list of the form (WINDOW-CONFIGURATION POSITION)
72 represents a saved window configuration plus a saved value of point.
73 A list of the form (FRAME-CONFIGURATION POSITION)
74 represents a saved frame configuration plus a saved value of point.")
76 (defgroup register nil
81 (defcustom register-separator nil
82 "Register containing the text to put between collected texts, or nil if none.
84 When collecting text with
85 `append-to-register' (resp. `prepend-to-register') contents of
86 this register is added to the beginning (resp. end) of the marked
89 :type '(choice (const :tag "None" nil)
90 (character :tag "Use register" :value ?+)))
92 (defcustom register-preview-delay 1
93 "If non-nil, time to wait in seconds before popping up a preview window.
94 If nil, do not show register previews, unless `help-char' (or a member of
95 `help-event-list') is pressed."
97 :type '(choice number (const :tag "No preview unless requested" nil))
100 (defun get-register (register)
101 "Return contents of Emacs register named REGISTER, or nil if none."
102 (cdr (assq register register-alist)))
104 (defun set-register (register value)
105 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
106 See the documentation of the variable `register-alist' for possible VALUEs."
107 (let ((aelt (assq register register-alist)))
110 (push (cons register value) register-alist))
113 (defun register-describe-oneline (c)
114 "One-line description of register C."
115 (let ((d (replace-regexp-in-string
117 (with-output-to-string (describe-register-1 c)))))
118 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d)
119 (substring d (match-end 0))
122 (defun register-preview-default (r)
123 "Default function for the variable `register-preview-function'."
125 (concat (single-key-description (car r)) ":")
126 (register-describe-oneline (car r))))
128 (defvar register-preview-function #'register-preview-default
129 "Function to format a register for previewing.
130 Takes one argument, a cons (NAME . CONTENTS) as found in `register-alist'.
133 (defun register-preview (buffer &optional show-empty)
134 "Pop up a window to show register preview in BUFFER.
135 If SHOW-EMPTY is non-nil show the window even if no registers.
136 Format of each entry is controlled by the variable `register-preview-function'."
137 (when (or show-empty (consp register-alist))
138 (with-temp-buffer-window
140 (cons 'display-buffer-below-selected
141 '((window-height . fit-window-to-buffer)))
143 (with-current-buffer standard-output
144 (setq cursor-in-non-selected-windows nil)
145 (insert (mapconcat register-preview-function register-alist ""))))))
147 (defun register-read-with-preview (prompt)
148 "Read and return an event, prompting with PROMPT, possibly showing a preview.
149 If `register-alist' and `register-preview-delay' are both non-nil,
150 display a window listing registers after `register-preview-delay' seconds.
151 If `help-char' (or a member of `help-event-list') is pressed, display
152 such a window regardless."
153 (let* ((buffer "*Register Preview*")
154 (timer (when (numberp register-preview-delay)
155 (run-with-timer register-preview-delay nil
157 (unless (get-buffer-window buffer)
158 (register-preview buffer))))))
159 (help-chars (cl-loop for c in (cons help-char help-event-list)
160 when (not (get-register c))
164 (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt))
166 (unless (get-buffer-window buffer)
167 (register-preview buffer 'show-empty)))
168 (if (characterp last-input-event) last-input-event
169 (error "Non-character input-event")))
170 (and (timerp timer) (cancel-timer timer))
171 (let ((w (get-buffer-window buffer)))
172 (and (window-live-p w) (delete-window w)))
173 (and (get-buffer buffer) (kill-buffer buffer)))))
175 (defun point-to-register (register &optional arg)
176 "Store current location of point in register REGISTER.
177 With prefix argument, store current frame configuration.
178 Use \\[jump-to-register] to go to that location or restore that configuration.
179 Argument is a character, naming the register."
180 (interactive (list (register-read-with-preview "Point to register: ")
182 ;; Turn the marker into a file-ref if the buffer is killed.
183 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
184 (set-register register
185 (if arg (list (current-frame-configuration) (point-marker))
188 (defun window-configuration-to-register (register &optional _arg)
189 "Store the window configuration of the selected frame in register REGISTER.
190 Use \\[jump-to-register] to restore the configuration.
191 Argument is a character, naming the register."
192 (interactive (list (register-read-with-preview
193 "Window configuration to register: ")
195 ;; current-window-configuration does not include the value
196 ;; of point in the current buffer, so record that separately.
197 (set-register register (list (current-window-configuration) (point-marker))))
199 (defun frame-configuration-to-register (register &optional _arg)
200 "Store the window configuration of all frames in register REGISTER.
201 Use \\[jump-to-register] to restore the configuration.
202 Argument is a character, naming the register."
203 (interactive (list (register-read-with-preview
204 "Frame configuration to register: ")
206 ;; current-frame-configuration does not include the value
207 ;; of point in the current buffer, so record that separately.
208 (set-register register (list (current-frame-configuration) (point-marker))))
210 (defalias 'register-to-point 'jump-to-register)
211 (defun jump-to-register (register &optional delete)
212 "Move point to location stored in a register.
213 If the register contains a file name, find that file.
214 \(To put a file name in a register, you must use `set-register'.)
215 If the register contains a window configuration (one frame) or a frameset
216 \(all frames), restore that frame or all frames accordingly.
217 First argument is a character, naming the register.
218 Optional second arg non-nil (interactively, prefix argument) says to
219 delete any existing frames that the frameset doesn't mention.
220 \(Otherwise, these frames are iconified.)"
221 (interactive (list (register-read-with-preview "Jump to register: ")
223 (let ((val (get-register register)))
226 (cl-assert (registerv-jump-func val) nil
227 "Don't know how to jump to register %s"
228 (single-key-description register))
229 (funcall (registerv-jump-func val) (registerv-data val)))
230 ((and (consp val) (frame-configuration-p (car val)))
231 (set-frame-configuration (car val) (not delete))
232 (goto-char (cadr val)))
233 ((and (consp val) (window-configuration-p (car val)))
234 (set-window-configuration (car val))
235 (goto-char (cadr val)))
237 (or (marker-buffer val)
238 (error "That register's buffer no longer exists"))
239 (switch-to-buffer (marker-buffer val))
241 ((and (consp val) (eq (car val) 'file))
242 (find-file (cdr val)))
243 ((and (consp val) (eq (car val) 'file-query))
244 (or (find-buffer-visiting (nth 1 val))
245 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
246 (error "Register access aborted"))
247 (find-file (nth 1 val))
248 (goto-char (nth 2 val)))
250 (error "Register doesn't contain a buffer position or configuration")))))
252 (defun register-swap-out ()
253 "Turn markers into file-query references when a buffer is killed."
254 (and buffer-file-name
255 (dolist (elem register-alist)
256 (and (markerp (cdr elem))
257 (eq (marker-buffer (cdr elem)) (current-buffer))
261 (marker-position (cdr elem))))))))
263 (defun number-to-register (number register)
264 "Store a number in a register.
265 Two args, NUMBER and REGISTER (a character, naming the register).
266 If NUMBER is nil, a decimal number is read from the buffer starting
267 at point, and point moves to the end of that number.
268 Interactively, NUMBER is the prefix arg (none means nil)."
269 (interactive (list current-prefix-arg
270 (register-read-with-preview "Number to register: ")))
271 (set-register register
273 (prefix-numeric-value number)
274 (if (looking-at "\\s-*-?[0-9]+")
276 (goto-char (match-end 0))
277 (string-to-number (match-string 0)))
280 (defun increment-register (prefix register)
281 "Augment contents of REGISTER.
282 Interactively, PREFIX is in raw form.
284 If REGISTER contains a number, add `prefix-numeric-value' of
287 If REGISTER is empty or if it contains text, call
288 `append-to-register' with `delete-flag' set to PREFIX."
289 (interactive "P\ncIncrement register: ")
290 (let ((register-val (get-register register)))
292 ((numberp register-val)
293 (let ((number (prefix-numeric-value prefix)))
294 (set-register register (+ number register-val))))
295 ((or (not register-val) (stringp register-val))
296 (append-to-register register (region-beginning) (region-end) prefix))
297 (t (error "Register does not contain a number or text")))))
299 (defun view-register (register)
300 "Display what is contained in register named REGISTER.
301 The Lisp value REGISTER is a character."
302 (interactive (list (register-read-with-preview "View register: ")))
303 (let ((val (get-register register)))
305 (message "Register %s is empty" (single-key-description register))
306 (with-output-to-temp-buffer "*Output*"
307 (describe-register-1 register t)))))
309 (defun list-registers ()
310 "Display a list of nonempty registers saying briefly what they contain."
312 (let ((list (copy-sequence register-alist)))
313 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
314 (with-output-to-temp-buffer "*Output*"
316 (when (get-register (car elt))
317 (describe-register-1 (car elt))
320 (defun describe-register-1 (register &optional verbose)
322 (princ (single-key-description register))
324 (let ((val (get-register register)))
327 (if (registerv-print-func val)
328 (funcall (registerv-print-func val) (registerv-data val))
329 (princ "[UNPRINTABLE CONTENTS].")))
335 (let ((buf (marker-buffer val)))
337 (princ "a marker in no buffer")
338 (princ "a buffer position:\n buffer ")
339 (princ (buffer-name buf))
340 (princ ", position ")
341 (princ (marker-position val)))))
343 ((and (consp val) (window-configuration-p (car val)))
344 (princ "a window configuration."))
346 ((and (consp val) (frame-configuration-p (car val)))
347 (princ "a frame configuration."))
349 ((and (consp val) (eq (car val) 'file))
354 ((and (consp val) (eq (car val) 'file-query))
355 (princ "a file-query reference:\n file ")
356 (prin1 (car (cdr val)))
357 (princ ",\n position ")
358 (princ (car (cdr (cdr val))))
364 (princ "the rectangle:\n")
369 (setq val (cdr val))))
370 (princ "a rectangle starting with ")
374 (setq val (copy-sequence val))
375 (if (eq yank-excluded-properties t)
376 (set-text-properties 0 (length val) nil val)
377 (remove-list-of-text-properties 0 (length val)
378 yank-excluded-properties val))
381 (princ "the text:\n")
384 ;; Extract first N characters starting with first non-whitespace.
385 ((string-match (format "[^ \t\n].\\{,%d\\}"
386 ;; Deduct 6 for the spaces inserted below.
387 (min 20 (max 0 (- (window-width) 6))))
389 (princ "text starting with\n ")
390 (princ (match-string 0 val)))
391 ((string-match "^[ \t\n]+$" val)
392 (princ "whitespace"))
394 (princ "the empty string")))))
397 (if verbose (prin1 val))))))
399 (defun insert-register (register &optional arg)
400 "Insert contents of register REGISTER. (REGISTER is a character.)
401 Normally puts point before and mark after the inserted text.
402 If optional second arg is non-nil, puts mark before and point after.
403 Interactively, second arg is non-nil if prefix arg is supplied."
405 (barf-if-buffer-read-only)
406 (list (register-read-with-preview "Insert register: ")
407 current-prefix-arg)))
409 (let ((val (get-register register)))
412 (cl-assert (registerv-insert-func val) nil
413 "Don't know how to insert register %s"
414 (single-key-description register))
415 (funcall (registerv-insert-func val) (registerv-data val)))
417 (insert-rectangle val))
419 (insert-for-yank val))
421 (princ val (current-buffer)))
422 ((and (markerp val) (marker-position val))
423 (princ (marker-position val) (current-buffer)))
425 (error "Register does not contain text"))))
426 (if (not arg) (exchange-point-and-mark)))
428 (defun copy-to-register (register start end &optional delete-flag region)
429 "Copy region into register REGISTER.
430 With prefix arg, delete as well.
431 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
432 START and END are buffer positions indicating what to copy.
433 The optional argument REGION if non-nil, indicates that we're not just copying
434 some text between START and END, but we're copying the region."
435 (interactive (list (register-read-with-preview "Copy to register: ")
440 (set-register register (if region
441 (funcall region-extract-function delete-flag)
442 (prog1 (filter-buffer-substring start end)
443 (if delete-flag (delete-region start end)))))
444 (setq deactivate-mark t)
446 ((called-interactively-p 'interactive)
447 (indicate-copied-region))))
449 (defun append-to-register (register start end &optional delete-flag)
450 "Append region to text in register REGISTER.
451 With prefix arg, delete as well.
452 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
453 START and END are buffer positions indicating what to append."
454 (interactive (list (register-read-with-preview "Append to register: ")
458 (let ((reg (get-register register))
459 (text (filter-buffer-substring start end))
460 (separator (and register-separator (get-register register-separator))))
462 register (cond ((not reg) text)
463 ((stringp reg) (concat reg separator text))
464 (t (error "Register does not contain text")))))
465 (setq deactivate-mark t)
467 (delete-region start end))
468 ((called-interactively-p 'interactive)
469 (indicate-copied-region))))
471 (defun prepend-to-register (register start end &optional delete-flag)
472 "Prepend region to text in register REGISTER.
473 With prefix arg, delete as well.
474 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
475 START and END are buffer positions indicating what to prepend."
476 (interactive (list (register-read-with-preview "Prepend to register: ")
480 (let ((reg (get-register register))
481 (text (filter-buffer-substring start end))
482 (separator (and register-separator (get-register register-separator))))
484 register (cond ((not reg) text)
485 ((stringp reg) (concat text separator reg))
486 (t (error "Register does not contain text")))))
487 (setq deactivate-mark t)
489 (delete-region start end))
490 ((called-interactively-p 'interactive)
491 (indicate-copied-region))))
493 (defun copy-rectangle-to-register (register start end &optional delete-flag)
494 "Copy rectangular region into register REGISTER.
495 With prefix arg, delete as well.
496 To insert this register in the buffer, use \\[insert-register].
498 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
499 START and END are buffer positions giving two corners of rectangle."
500 (interactive (list (register-read-with-preview
501 "Copy rectangle to register: ")
505 (let ((rectangle (if delete-flag
506 (delete-extract-rectangle start end)
507 (extract-rectangle start end))))
508 (set-register register rectangle)
509 (when (and (null delete-flag)
510 (called-interactively-p 'interactive))
511 (setq deactivate-mark t)
512 (indicate-copied-region (length (car rectangle))))))
515 ;;; register.el ends here