register.el doc fixes
[bpt/emacs.git] / lisp / register.el
1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 1985, 1993-1994, 2001-2014 Free Software Foundation,
4 ;; Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
31
32 (eval-when-compile (require 'cl-lib))
33
34 ;;; Code:
35
36 (cl-defstruct
37 (registerv (:constructor nil)
38 (:constructor registerv--make (&optional data print-func
39 jump-func insert-func))
40 (:copier nil)
41 (:type vector)
42 :named)
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))
47
48 (cl-defun registerv-make (data &key print-func jump-func insert-func)
49 "Create a register value object.
50
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
55 this sentence:
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))
61
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
70 visiting it.
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.")
75
76 (defgroup register nil
77 "Register commands."
78 :group 'convenience
79 :version "24.3")
80
81 (defcustom register-separator nil
82 "Register containing the text to put between collected texts, or nil if none.
83
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
87 text."
88 :group 'register
89 :type '(choice (const :tag "None" nil)
90 (character :tag "Use register" :value ?+)))
91
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."
96 :version "24.4"
97 :type '(choice number (const :tag "No preview unless requested" nil))
98 :group 'register)
99
100 (defun get-register (register)
101 "Return contents of Emacs register named REGISTER, or nil if none."
102 (cdr (assq register register-alist)))
103
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)))
108 (if aelt
109 (setcdr aelt value)
110 (push (cons register value) register-alist))
111 value))
112
113 (defun register-describe-oneline (c)
114 "One-line description of register C."
115 (let ((d (replace-regexp-in-string
116 "\n[ \t]*" " "
117 (with-output-to-string (describe-register-1 c)))))
118 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d)
119 (substring d (match-end 0))
120 d)))
121
122 (defun register-preview-default (r)
123 "Default function for the variable `register-preview-function'."
124 (format "%s %s\n"
125 (concat (single-key-description (car r)) ":")
126 (register-describe-oneline (car r))))
127
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'.
131 Returns a string.")
132
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
139 buffer
140 (cons 'display-buffer-below-selected
141 '((window-height . fit-window-to-buffer)))
142 nil
143 (with-current-buffer standard-output
144 (setq cursor-in-non-selected-windows nil)
145 (insert (mapconcat register-preview-function register-alist ""))))))
146
147 (defun register-read-with-preview (prompt)
148 "Read and return a register name, possibly showing existing registers.
149 Prompt with the string PROMPT. If `register-alist' and
150 `register-preview-delay' are both non-nil, display a window
151 listing existing registers after `register-preview-delay' seconds.
152 If `help-char' (or a member of `help-event-list') is pressed,
153 display such a window regardless."
154 (let* ((buffer "*Register Preview*")
155 (timer (when (numberp register-preview-delay)
156 (run-with-timer register-preview-delay nil
157 (lambda ()
158 (unless (get-buffer-window buffer)
159 (register-preview buffer))))))
160 (help-chars (cl-loop for c in (cons help-char help-event-list)
161 when (not (get-register c))
162 collect c)))
163 (unwind-protect
164 (progn
165 (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt))
166 help-chars)
167 (unless (get-buffer-window buffer)
168 (register-preview buffer 'show-empty)))
169 (if (characterp last-input-event) last-input-event
170 (error "Non-character input-event")))
171 (and (timerp timer) (cancel-timer timer))
172 (let ((w (get-buffer-window buffer)))
173 (and (window-live-p w) (delete-window w)))
174 (and (get-buffer buffer) (kill-buffer buffer)))))
175
176 (defun point-to-register (register &optional arg)
177 "Store current location of point in register REGISTER.
178 With prefix argument, store current frame configuration.
179 Use \\[jump-to-register] to go to that location or restore that configuration.
180 Argument is a character, naming the register.
181
182 Interactively, reads the register using `register-read-with-preview'."
183 (interactive (list (register-read-with-preview "Point to register: ")
184 current-prefix-arg))
185 ;; Turn the marker into a file-ref if the buffer is killed.
186 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
187 (set-register register
188 (if arg (list (current-frame-configuration) (point-marker))
189 (point-marker))))
190
191 (defun window-configuration-to-register (register &optional _arg)
192 "Store the window configuration of the selected frame in register REGISTER.
193 Use \\[jump-to-register] to restore the configuration.
194 Argument is a character, naming the register.
195
196 Interactively, reads the register using `register-read-with-preview'."
197 (interactive (list (register-read-with-preview
198 "Window configuration to register: ")
199 current-prefix-arg))
200 ;; current-window-configuration does not include the value
201 ;; of point in the current buffer, so record that separately.
202 (set-register register (list (current-window-configuration) (point-marker))))
203
204 (defun frame-configuration-to-register (register &optional _arg)
205 "Store the window configuration of all frames in register REGISTER.
206 Use \\[jump-to-register] to restore the configuration.
207 Argument is a character, naming the register.
208
209 Interactively, reads the register using `register-read-with-preview'."
210 (interactive (list (register-read-with-preview
211 "Frame configuration to register: ")
212 current-prefix-arg))
213 ;; current-frame-configuration does not include the value
214 ;; of point in the current buffer, so record that separately.
215 (set-register register (list (current-frame-configuration) (point-marker))))
216
217 (defalias 'register-to-point 'jump-to-register)
218 (defun jump-to-register (register &optional delete)
219 "Move point to location stored in a register.
220 If the register contains a file name, find that file.
221 \(To put a file name in a register, you must use `set-register'.)
222 If the register contains a window configuration (one frame) or a frameset
223 \(all frames), restore that frame or all frames accordingly.
224 First argument is a character, naming the register.
225 Optional second arg non-nil (interactively, prefix argument) says to
226 delete any existing frames that the frameset doesn't mention.
227 \(Otherwise, these frames are iconified.)
228
229 Interactively, reads the register using `register-read-with-preview'."
230 (interactive (list (register-read-with-preview "Jump to register: ")
231 current-prefix-arg))
232 (let ((val (get-register register)))
233 (cond
234 ((registerv-p val)
235 (cl-assert (registerv-jump-func val) nil
236 "Don't know how to jump to register %s"
237 (single-key-description register))
238 (funcall (registerv-jump-func val) (registerv-data val)))
239 ((and (consp val) (frame-configuration-p (car val)))
240 (set-frame-configuration (car val) (not delete))
241 (goto-char (cadr val)))
242 ((and (consp val) (window-configuration-p (car val)))
243 (set-window-configuration (car val))
244 (goto-char (cadr val)))
245 ((markerp val)
246 (or (marker-buffer val)
247 (error "That register's buffer no longer exists"))
248 (switch-to-buffer (marker-buffer val))
249 (goto-char val))
250 ((and (consp val) (eq (car val) 'file))
251 (find-file (cdr val)))
252 ((and (consp val) (eq (car val) 'file-query))
253 (or (find-buffer-visiting (nth 1 val))
254 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
255 (error "Register access aborted"))
256 (find-file (nth 1 val))
257 (goto-char (nth 2 val)))
258 (t
259 (error "Register doesn't contain a buffer position or configuration")))))
260
261 (defun register-swap-out ()
262 "Turn markers into file-query references when a buffer is killed."
263 (and buffer-file-name
264 (dolist (elem register-alist)
265 (and (markerp (cdr elem))
266 (eq (marker-buffer (cdr elem)) (current-buffer))
267 (setcdr elem
268 (list 'file-query
269 buffer-file-name
270 (marker-position (cdr elem))))))))
271
272 (defun number-to-register (number register)
273 "Store a number in a register.
274 Two args, NUMBER and REGISTER (a character, naming the register).
275 If NUMBER is nil, a decimal number is read from the buffer starting
276 at point, and point moves to the end of that number.
277 Interactively, NUMBER is the prefix arg (none means nil).
278
279 Interactively, reads the register using `register-read-with-preview'."
280 (interactive (list current-prefix-arg
281 (register-read-with-preview "Number to register: ")))
282 (set-register register
283 (if number
284 (prefix-numeric-value number)
285 (if (looking-at "\\s-*-?[0-9]+")
286 (progn
287 (goto-char (match-end 0))
288 (string-to-number (match-string 0)))
289 0))))
290
291 (defun increment-register (prefix register)
292 "Augment contents of REGISTER.
293 Interactively, PREFIX is in raw form.
294
295 If REGISTER contains a number, add `prefix-numeric-value' of
296 PREFIX to it.
297
298 If REGISTER is empty or if it contains text, call
299 `append-to-register' with `delete-flag' set to PREFIX."
300 (interactive "P\ncIncrement register: ")
301 (let ((register-val (get-register register)))
302 (cond
303 ((numberp register-val)
304 (let ((number (prefix-numeric-value prefix)))
305 (set-register register (+ number register-val))))
306 ((or (not register-val) (stringp register-val))
307 (append-to-register register (region-beginning) (region-end) prefix))
308 (t (error "Register does not contain a number or text")))))
309
310 (defun view-register (register)
311 "Display what is contained in register named REGISTER.
312 The Lisp value REGISTER is a character.
313
314 Interactively, reads the register using `register-read-with-preview'."
315 (interactive (list (register-read-with-preview "View register: ")))
316 (let ((val (get-register register)))
317 (if (null val)
318 (message "Register %s is empty" (single-key-description register))
319 (with-output-to-temp-buffer "*Output*"
320 (describe-register-1 register t)))))
321
322 (defun list-registers ()
323 "Display a list of nonempty registers saying briefly what they contain."
324 (interactive)
325 (let ((list (copy-sequence register-alist)))
326 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
327 (with-output-to-temp-buffer "*Output*"
328 (dolist (elt list)
329 (when (get-register (car elt))
330 (describe-register-1 (car elt))
331 (terpri))))))
332
333 (defun describe-register-1 (register &optional verbose)
334 (princ "Register ")
335 (princ (single-key-description register))
336 (princ " contains ")
337 (let ((val (get-register register)))
338 (cond
339 ((registerv-p val)
340 (if (registerv-print-func val)
341 (funcall (registerv-print-func val) (registerv-data val))
342 (princ "[UNPRINTABLE CONTENTS].")))
343
344 ((numberp val)
345 (princ val))
346
347 ((markerp val)
348 (let ((buf (marker-buffer val)))
349 (if (null buf)
350 (princ "a marker in no buffer")
351 (princ "a buffer position:\n buffer ")
352 (princ (buffer-name buf))
353 (princ ", position ")
354 (princ (marker-position val)))))
355
356 ((and (consp val) (window-configuration-p (car val)))
357 (princ "a window configuration."))
358
359 ((and (consp val) (frame-configuration-p (car val)))
360 (princ "a frame configuration."))
361
362 ((and (consp val) (eq (car val) 'file))
363 (princ "the file ")
364 (prin1 (cdr val))
365 (princ "."))
366
367 ((and (consp val) (eq (car val) 'file-query))
368 (princ "a file-query reference:\n file ")
369 (prin1 (car (cdr val)))
370 (princ ",\n position ")
371 (princ (car (cdr (cdr val))))
372 (princ "."))
373
374 ((consp val)
375 (if verbose
376 (progn
377 (princ "the rectangle:\n")
378 (while val
379 (princ " ")
380 (princ (car val))
381 (terpri)
382 (setq val (cdr val))))
383 (princ "a rectangle starting with ")
384 (princ (car val))))
385
386 ((stringp val)
387 (setq val (copy-sequence val))
388 (if (eq yank-excluded-properties t)
389 (set-text-properties 0 (length val) nil val)
390 (remove-list-of-text-properties 0 (length val)
391 yank-excluded-properties val))
392 (if verbose
393 (progn
394 (princ "the text:\n")
395 (princ val))
396 (cond
397 ;; Extract first N characters starting with first non-whitespace.
398 ((string-match (format "[^ \t\n].\\{,%d\\}"
399 ;; Deduct 6 for the spaces inserted below.
400 (min 20 (max 0 (- (window-width) 6))))
401 val)
402 (princ "text starting with\n ")
403 (princ (match-string 0 val)))
404 ((string-match "^[ \t\n]+$" val)
405 (princ "whitespace"))
406 (t
407 (princ "the empty string")))))
408 (t
409 (princ "Garbage:\n")
410 (if verbose (prin1 val))))))
411
412 (defun insert-register (register &optional arg)
413 "Insert contents of register REGISTER. (REGISTER is a character.)
414 Normally puts point before and mark after the inserted text.
415 If optional second arg is non-nil, puts mark before and point after.
416 Interactively, second arg is non-nil if prefix arg is supplied.
417
418 Interactively, reads the register using `register-read-with-preview'."
419 (interactive (progn
420 (barf-if-buffer-read-only)
421 (list (register-read-with-preview "Insert register: ")
422 current-prefix-arg)))
423 (push-mark)
424 (let ((val (get-register register)))
425 (cond
426 ((registerv-p val)
427 (cl-assert (registerv-insert-func val) nil
428 "Don't know how to insert register %s"
429 (single-key-description register))
430 (funcall (registerv-insert-func val) (registerv-data val)))
431 ((consp val)
432 (insert-rectangle val))
433 ((stringp val)
434 (insert-for-yank val))
435 ((numberp val)
436 (princ val (current-buffer)))
437 ((and (markerp val) (marker-position val))
438 (princ (marker-position val) (current-buffer)))
439 (t
440 (error "Register does not contain text"))))
441 (if (not arg) (exchange-point-and-mark)))
442
443 (defun copy-to-register (register start end &optional delete-flag region)
444 "Copy region into register REGISTER.
445 With prefix arg, delete as well.
446 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
447 START and END are buffer positions indicating what to copy.
448 The optional argument REGION if non-nil, indicates that we're not just copying
449 some text between START and END, but we're copying the region.
450
451 Interactively, reads the register using `register-read-with-preview'."
452 (interactive (list (register-read-with-preview "Copy to register: ")
453 (region-beginning)
454 (region-end)
455 current-prefix-arg
456 t))
457 (set-register register (if region
458 (funcall region-extract-function delete-flag)
459 (prog1 (filter-buffer-substring start end)
460 (if delete-flag (delete-region start end)))))
461 (setq deactivate-mark t)
462 (cond (delete-flag)
463 ((called-interactively-p 'interactive)
464 (indicate-copied-region))))
465
466 (defun append-to-register (register start end &optional delete-flag)
467 "Append region to text in register REGISTER.
468 With prefix arg, delete as well.
469 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
470 START and END are buffer positions indicating what to append.
471
472 Interactively, reads the register using `register-read-with-preview'."
473 (interactive (list (register-read-with-preview "Append to register: ")
474 (region-beginning)
475 (region-end)
476 current-prefix-arg))
477 (let ((reg (get-register register))
478 (text (filter-buffer-substring start end))
479 (separator (and register-separator (get-register register-separator))))
480 (set-register
481 register (cond ((not reg) text)
482 ((stringp reg) (concat reg separator text))
483 (t (error "Register does not contain text")))))
484 (setq deactivate-mark t)
485 (cond (delete-flag
486 (delete-region start end))
487 ((called-interactively-p 'interactive)
488 (indicate-copied-region))))
489
490 (defun prepend-to-register (register start end &optional delete-flag)
491 "Prepend region to text in register REGISTER.
492 With prefix arg, delete as well.
493 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
494 START and END are buffer positions indicating what to prepend.
495
496 Interactively, reads the register using `register-read-with-preview'."
497 (interactive (list (register-read-with-preview "Prepend to register: ")
498 (region-beginning)
499 (region-end)
500 current-prefix-arg))
501 (let ((reg (get-register register))
502 (text (filter-buffer-substring start end))
503 (separator (and register-separator (get-register register-separator))))
504 (set-register
505 register (cond ((not reg) text)
506 ((stringp reg) (concat text separator reg))
507 (t (error "Register does not contain text")))))
508 (setq deactivate-mark t)
509 (cond (delete-flag
510 (delete-region start end))
511 ((called-interactively-p 'interactive)
512 (indicate-copied-region))))
513
514 (defun copy-rectangle-to-register (register start end &optional delete-flag)
515 "Copy rectangular region into register REGISTER.
516 With prefix arg, delete as well.
517 To insert this register in the buffer, use \\[insert-register].
518
519 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
520 START and END are buffer positions giving two corners of rectangle.
521
522 Interactively, reads the register using `register-read-with-preview'."
523 (interactive (list (register-read-with-preview
524 "Copy rectangle to register: ")
525 (region-beginning)
526 (region-end)
527 current-prefix-arg))
528 (let ((rectangle (if delete-flag
529 (delete-extract-rectangle start end)
530 (extract-rectangle start end))))
531 (set-register register rectangle)
532 (when (and (null delete-flag)
533 (called-interactively-p 'interactive))
534 (setq deactivate-mark t)
535 (indicate-copied-region (length (car rectangle))))))
536
537 (provide 'register)
538 ;;; register.el ends here