* lisp/net/tramp-cmds.el, lisp/allout.el: Avoid custom-set-variables
[emacs.git] / lisp / register.el
blob23eefd08b884bbae7d8e29f7cfcb0c2cc04c72ea
1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
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))
34 ;;; Code:
36 ;; FIXME: Clean up namespace usage!
38 (cl-defstruct
39 (registerv (:constructor nil)
40 (:constructor registerv--make (&optional data print-func
41 jump-func insert-func))
42 (:copier nil)
43 (:type vector)
44 :named)
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
57 this sentence:
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
72 visiting it.
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
79 "Register commands."
80 :group 'convenience
81 :version "24.3")
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)
88 of the marked text."
89 :group 'register
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."
97 :version "24.4"
98 :type '(choice number (const :tag "No preview unless requested" nil))
99 :group 'register)
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
113 "\n[ \t]*" " "
114 (with-output-to-string (describe-register-1 c)))))
115 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d)
116 (substring d (match-end 0))
117 d)))
119 (defun register-preview-default (r)
120 "Default function for the variable `register-preview-function'."
121 (format "%s: %s\n"
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'.
128 Returns a string.")
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
136 buffer
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
155 (lambda ()
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))
160 collect c)))
161 (unwind-protect
162 (progn
163 (while (memq (read-key (propertize prompt 'face 'minibuffer-prompt))
164 help-chars)
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))
170 (keyboard-quit))
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
186 (if current-prefix-arg
187 "Frame configuration to register: "
188 "Point to register: "))
189 current-prefix-arg))
190 ;; Turn the marker into a file-ref if the buffer is killed.
191 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
192 (set-register register
193 (if arg (list (current-frame-configuration) (point-marker))
194 (point-marker))))
196 (defun window-configuration-to-register (register &optional _arg)
197 "Store the window configuration of the selected frame in register REGISTER.
198 Use \\[jump-to-register] to restore the configuration.
199 Argument is a character, naming the register.
201 Interactively, reads the register using `register-read-with-preview'."
202 (interactive (list (register-read-with-preview
203 "Window configuration to register: ")
204 current-prefix-arg))
205 ;; current-window-configuration does not include the value
206 ;; of point in the current buffer, so record that separately.
207 (set-register register (list (current-window-configuration) (point-marker))))
209 ;; It has had the optional arg for ages, but never used it.
210 (set-advertised-calling-convention 'window-configuration-to-register
211 '(register) "24.4")
213 (defun frame-configuration-to-register (register &optional _arg)
214 "Store the window configuration of all frames in register REGISTER.
215 Use \\[jump-to-register] to restore the configuration.
216 Argument is a character, naming the register.
218 Interactively, reads the register using `register-read-with-preview'."
219 (interactive (list (register-read-with-preview
220 "Frame configuration to register: ")
221 current-prefix-arg))
222 ;; current-frame-configuration does not include the value
223 ;; of point in the current buffer, so record that separately.
224 (set-register register (list (current-frame-configuration) (point-marker))))
226 ;; It has had the optional arg for ages, but never used it.
227 (set-advertised-calling-convention 'frame-configuration-to-register
228 '(register) "24.4")
230 (make-obsolete 'frame-configuration-to-register 'frameset-to-register "24.4")
232 (defalias 'register-to-point 'jump-to-register)
233 (defun jump-to-register (register &optional delete)
234 "Move point to location stored in a register.
235 If the register contains a file name, find that file.
236 \(To put a file name in a register, you must use `set-register'.)
237 If the register contains a window configuration (one frame) or a frameset
238 \(all frames), restore that frame or all frames accordingly.
239 First argument is a character, naming the register.
240 Optional second arg non-nil (interactively, prefix argument) says to
241 delete any existing frames that the frameset doesn't mention.
242 \(Otherwise, these frames are iconified.)
244 Interactively, reads the register using `register-read-with-preview'."
245 (interactive (list (register-read-with-preview "Jump to register: ")
246 current-prefix-arg))
247 (let ((val (get-register register)))
248 (cond
249 ((registerv-p val)
250 (cl-assert (registerv-jump-func val) nil
251 "Don't know how to jump to register %s"
252 (single-key-description register))
253 (funcall (registerv-jump-func val) (registerv-data val)))
254 ((and (consp val) (frame-configuration-p (car val)))
255 (set-frame-configuration (car val) (not delete))
256 (goto-char (cadr val)))
257 ((and (consp val) (window-configuration-p (car val)))
258 (set-window-configuration (car val))
259 (goto-char (cadr val)))
260 ((markerp val)
261 (or (marker-buffer val)
262 (user-error "That register's buffer no longer exists"))
263 (switch-to-buffer (marker-buffer val))
264 (unless (or (= (point) (marker-position val))
265 (eq last-command 'jump-to-register))
266 (push-mark))
267 (goto-char val))
268 ((and (consp val) (eq (car val) 'file))
269 (find-file (cdr val)))
270 ((and (consp val) (eq (car val) 'file-query))
271 (or (find-buffer-visiting (nth 1 val))
272 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
273 (user-error "Register access aborted"))
274 (find-file (nth 1 val))
275 (goto-char (nth 2 val)))
277 (user-error "Register doesn't contain a buffer position or configuration")))))
279 (defun register-swap-out ()
280 "Turn markers into file-query references when a buffer is killed."
281 (and buffer-file-name
282 (dolist (elem register-alist)
283 (and (markerp (cdr elem))
284 (eq (marker-buffer (cdr elem)) (current-buffer))
285 (setcdr elem
286 (list 'file-query
287 buffer-file-name
288 (marker-position (cdr elem))))))))
290 (defun number-to-register (number register)
291 "Store a number in a register.
292 Two args, NUMBER and REGISTER (a character, naming the register).
293 If NUMBER is nil, a decimal number is read from the buffer starting
294 at point, and point moves to the end of that number.
295 Interactively, NUMBER is the prefix arg (none means nil).
297 Interactively, reads the register using `register-read-with-preview'."
298 (interactive (list current-prefix-arg
299 (register-read-with-preview "Number to register: ")))
300 (set-register register
301 (if number
302 (prefix-numeric-value number)
303 (if (looking-at "\\s-*-?[0-9]+")
304 (progn
305 (goto-char (match-end 0))
306 (string-to-number (match-string 0)))
307 0))))
309 (defun increment-register (prefix register)
310 "Augment contents of REGISTER.
311 Interactively, PREFIX is in raw form.
313 If REGISTER contains a number, add `prefix-numeric-value' of
314 PREFIX to it.
316 If REGISTER is empty or if it contains text, call
317 `append-to-register' with `delete-flag' set to PREFIX.
319 Interactively, reads the register using `register-read-with-preview'."
320 (interactive (list current-prefix-arg
321 (register-read-with-preview "Increment register: ")))
322 (let ((register-val (get-register register)))
323 (cond
324 ((numberp register-val)
325 (let ((number (prefix-numeric-value prefix)))
326 (set-register register (+ number register-val))))
327 ((or (not register-val) (stringp register-val))
328 (append-to-register register (region-beginning) (region-end) prefix))
329 (t (user-error "Register does not contain a number or text")))))
331 (defun view-register (register)
332 "Display what is contained in register named REGISTER.
333 The Lisp value REGISTER is a character.
335 Interactively, reads the register using `register-read-with-preview'."
336 (interactive (list (register-read-with-preview "View register: ")))
337 (let ((val (get-register register)))
338 (if (null val)
339 (message "Register %s is empty" (single-key-description register))
340 (with-output-to-temp-buffer "*Output*"
341 (describe-register-1 register t)))))
343 (defun list-registers ()
344 "Display a list of nonempty registers saying briefly what they contain."
345 (interactive)
346 (let ((list (copy-sequence register-alist)))
347 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
348 (with-output-to-temp-buffer "*Output*"
349 (dolist (elt list)
350 (when (get-register (car elt))
351 (describe-register-1 (car elt))
352 (terpri))))))
354 (defun describe-register-1 (register &optional verbose)
355 (princ "Register ")
356 (princ (single-key-description register))
357 (princ " contains ")
358 (let ((val (get-register register)))
359 (cond
360 ((registerv-p val)
361 (if (registerv-print-func val)
362 (funcall (registerv-print-func val) (registerv-data val))
363 (princ "[UNPRINTABLE CONTENTS].")))
365 ((numberp val)
366 (princ val))
368 ((markerp val)
369 (let ((buf (marker-buffer val)))
370 (if (null buf)
371 (princ "a marker in no buffer")
372 (princ "a buffer position:\n buffer ")
373 (princ (buffer-name buf))
374 (princ ", position ")
375 (princ (marker-position val)))))
377 ((and (consp val) (window-configuration-p (car val)))
378 (princ "a window configuration."))
380 ((and (consp val) (frame-configuration-p (car val)))
381 (princ "a frame configuration."))
383 ((and (consp val) (eq (car val) 'file))
384 (princ "the file ")
385 (prin1 (cdr val))
386 (princ "."))
388 ((and (consp val) (eq (car val) 'file-query))
389 (princ "a file-query reference:\n file ")
390 (prin1 (car (cdr val)))
391 (princ ",\n position ")
392 (princ (car (cdr (cdr val))))
393 (princ "."))
395 ((consp val)
396 (if verbose
397 (progn
398 (princ "the rectangle:\n")
399 (while val
400 (princ " ")
401 (princ (car val))
402 (terpri)
403 (setq val (cdr val))))
404 (princ "a rectangle starting with ")
405 (princ (car val))))
407 ((stringp val)
408 (setq val (copy-sequence val))
409 (if (eq yank-excluded-properties t)
410 (set-text-properties 0 (length val) nil val)
411 (remove-list-of-text-properties 0 (length val)
412 yank-excluded-properties val))
413 (if verbose
414 (progn
415 (princ "the text:\n")
416 (princ val))
417 (cond
418 ;; Extract first N characters starting with first non-whitespace.
419 ((string-match (format "[^ \t\n].\\{,%d\\}"
420 ;; Deduct 6 for the spaces inserted below.
421 (min 20 (max 0 (- (window-width) 6))))
422 val)
423 (princ "text starting with\n ")
424 (princ (match-string 0 val)))
425 ((string-match "^[ \t\n]+$" val)
426 (princ "whitespace"))
428 (princ "the empty string")))))
430 (princ "Garbage:\n")
431 (if verbose (prin1 val))))))
433 (defun insert-register (register &optional arg)
434 "Insert contents of register REGISTER. (REGISTER is a character.)
435 Normally puts point before and mark after the inserted text.
436 If optional second arg is non-nil, puts mark before and point after.
437 Interactively, second arg is nil if prefix arg is supplied and t
438 otherwise.
440 Interactively, reads the register using `register-read-with-preview'."
441 (interactive (progn
442 (barf-if-buffer-read-only)
443 (list (register-read-with-preview "Insert register: ")
444 (not current-prefix-arg))))
445 (push-mark)
446 (let ((val (get-register register)))
447 (cond
448 ((registerv-p val)
449 (cl-assert (registerv-insert-func val) nil
450 "Don't know how to insert register %s"
451 (single-key-description register))
452 (funcall (registerv-insert-func val) (registerv-data val)))
453 ((consp val)
454 (insert-rectangle val))
455 ((stringp val)
456 (insert-for-yank val))
457 ((numberp val)
458 (princ val (current-buffer)))
459 ((and (markerp val) (marker-position val))
460 (princ (marker-position val) (current-buffer)))
462 (user-error "Register does not contain text"))))
463 (if (not arg) (exchange-point-and-mark)))
465 (defun copy-to-register (register start end &optional delete-flag region)
466 "Copy region into register REGISTER.
467 With prefix arg, delete as well.
468 Called from program, takes five args: REGISTER, START, END, DELETE-FLAG,
469 and REGION. START and END are buffer positions indicating what to copy.
470 The optional argument REGION if non-nil, indicates that we're not just
471 copying some text between START and END, but we're copying the region.
473 Interactively, reads the register using `register-read-with-preview'."
474 (interactive (list (register-read-with-preview "Copy to register: ")
475 (region-beginning)
476 (region-end)
477 current-prefix-arg
479 (set-register register (if region
480 (funcall region-extract-function delete-flag)
481 (prog1 (filter-buffer-substring start end)
482 (if delete-flag (delete-region start end)))))
483 (setq deactivate-mark t)
484 (cond (delete-flag)
485 ((called-interactively-p 'interactive)
486 (indicate-copied-region))))
488 (defun append-to-register (register start end &optional delete-flag)
489 "Append region to text in register REGISTER.
490 With prefix arg, delete as well.
491 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
492 START and END are buffer positions indicating what to append.
494 Interactively, reads the register using `register-read-with-preview'."
495 (interactive (list (register-read-with-preview "Append to register: ")
496 (region-beginning)
497 (region-end)
498 current-prefix-arg))
499 (let ((reg (get-register register))
500 (text (filter-buffer-substring start end))
501 (separator (and register-separator (get-register register-separator))))
502 (set-register
503 register (cond ((not reg) text)
504 ((stringp reg) (concat reg separator text))
505 (t (user-error "Register does not contain text")))))
506 (setq deactivate-mark t)
507 (cond (delete-flag
508 (delete-region start end))
509 ((called-interactively-p 'interactive)
510 (indicate-copied-region))))
512 (defun prepend-to-register (register start end &optional delete-flag)
513 "Prepend region to text in register REGISTER.
514 With prefix arg, delete as well.
515 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
516 START and END are buffer positions indicating what to prepend.
518 Interactively, reads the register using `register-read-with-preview'."
519 (interactive (list (register-read-with-preview "Prepend to register: ")
520 (region-beginning)
521 (region-end)
522 current-prefix-arg))
523 (let ((reg (get-register register))
524 (text (filter-buffer-substring start end))
525 (separator (and register-separator (get-register register-separator))))
526 (set-register
527 register (cond ((not reg) text)
528 ((stringp reg) (concat text separator reg))
529 (t (user-error "Register does not contain text")))))
530 (setq deactivate-mark t)
531 (cond (delete-flag
532 (delete-region start end))
533 ((called-interactively-p 'interactive)
534 (indicate-copied-region))))
536 (defun copy-rectangle-to-register (register start end &optional delete-flag)
537 "Copy rectangular region into register REGISTER.
538 With prefix arg, delete as well.
539 To insert this register in the buffer, use \\[insert-register].
541 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
542 START and END are buffer positions giving two corners of rectangle.
544 Interactively, reads the register using `register-read-with-preview'."
545 (interactive (list (register-read-with-preview
546 "Copy rectangle to register: ")
547 (region-beginning)
548 (region-end)
549 current-prefix-arg))
550 (let ((rectangle (if delete-flag
551 (delete-extract-rectangle start end)
552 (extract-rectangle start end))))
553 (set-register register rectangle)
554 (when (and (null delete-flag)
555 (called-interactively-p 'interactive))
556 (setq deactivate-mark t)
557 (indicate-copied-region (length (car rectangle))))))
559 (provide 'register)
560 ;;; register.el ends here