Replace "Maintainer: FSF" with the emacs-devel mailing address
[emacs.git] / lisp / register.el
blobb0afa2d41f0c89747458a4b27ebe553a604a4728
1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2014 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 <http://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.
32 (eval-when-compile (require 'cl-lib))
34 ;;; Code:
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))
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
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))
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.")
76 (defgroup register nil
77 "Register commands."
78 :group 'convenience
79 :version "24.3")
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
87 text."
88 :group 'register
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."
96 :version "24.4"
97 :type '(choice number (const :tag "No preview unless requested" nil))
98 :group 'register)
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)))
108 (if aelt
109 (setcdr aelt value)
110 (push (cons register value) register-alist))
111 value))
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)))
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))))
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.")
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)))
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 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)))))
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.
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))))
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.
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))))
204 ;; It has had the optional arg for ages, but never used it.
205 (set-advertised-calling-convention 'window-configuration-to-register
206 '(register) "24.4")
208 (defun frame-configuration-to-register (register &optional _arg)
209 "Store the window configuration of all frames in register REGISTER.
210 Use \\[jump-to-register] to restore the configuration.
211 Argument is a character, naming the register.
213 Interactively, reads the register using `register-read-with-preview'."
214 (interactive (list (register-read-with-preview
215 "Frame configuration to register: ")
216 current-prefix-arg))
217 ;; current-frame-configuration does not include the value
218 ;; of point in the current buffer, so record that separately.
219 (set-register register (list (current-frame-configuration) (point-marker))))
221 ;; It has had the optional arg for ages, but never used it.
222 (set-advertised-calling-convention 'frame-configuration-to-register
223 '(register) "24.4")
225 (defalias 'register-to-point 'jump-to-register)
226 (defun jump-to-register (register &optional delete)
227 "Move point to location stored in a register.
228 If the register contains a file name, find that file.
229 \(To put a file name in a register, you must use `set-register'.)
230 If the register contains a window configuration (one frame) or a frameset
231 \(all frames), restore that frame or all frames accordingly.
232 First argument is a character, naming the register.
233 Optional second arg non-nil (interactively, prefix argument) says to
234 delete any existing frames that the frameset doesn't mention.
235 \(Otherwise, these frames are iconified.)
237 Interactively, reads the register using `register-read-with-preview'."
238 (interactive (list (register-read-with-preview "Jump to register: ")
239 current-prefix-arg))
240 (let ((val (get-register register)))
241 (cond
242 ((registerv-p val)
243 (cl-assert (registerv-jump-func val) nil
244 "Don't know how to jump to register %s"
245 (single-key-description register))
246 (funcall (registerv-jump-func val) (registerv-data val)))
247 ((and (consp val) (frame-configuration-p (car val)))
248 (set-frame-configuration (car val) (not delete))
249 (goto-char (cadr val)))
250 ((and (consp val) (window-configuration-p (car val)))
251 (set-window-configuration (car val))
252 (goto-char (cadr val)))
253 ((markerp val)
254 (or (marker-buffer val)
255 (error "That register's buffer no longer exists"))
256 (switch-to-buffer (marker-buffer val))
257 (goto-char val))
258 ((and (consp val) (eq (car val) 'file))
259 (find-file (cdr val)))
260 ((and (consp val) (eq (car val) 'file-query))
261 (or (find-buffer-visiting (nth 1 val))
262 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
263 (error "Register access aborted"))
264 (find-file (nth 1 val))
265 (goto-char (nth 2 val)))
267 (error "Register doesn't contain a buffer position or configuration")))))
269 (defun register-swap-out ()
270 "Turn markers into file-query references when a buffer is killed."
271 (and buffer-file-name
272 (dolist (elem register-alist)
273 (and (markerp (cdr elem))
274 (eq (marker-buffer (cdr elem)) (current-buffer))
275 (setcdr elem
276 (list 'file-query
277 buffer-file-name
278 (marker-position (cdr elem))))))))
280 (defun number-to-register (number register)
281 "Store a number in a register.
282 Two args, NUMBER and REGISTER (a character, naming the register).
283 If NUMBER is nil, a decimal number is read from the buffer starting
284 at point, and point moves to the end of that number.
285 Interactively, NUMBER is the prefix arg (none means nil).
287 Interactively, reads the register using `register-read-with-preview'."
288 (interactive (list current-prefix-arg
289 (register-read-with-preview "Number to register: ")))
290 (set-register register
291 (if number
292 (prefix-numeric-value number)
293 (if (looking-at "\\s-*-?[0-9]+")
294 (progn
295 (goto-char (match-end 0))
296 (string-to-number (match-string 0)))
297 0))))
299 (defun increment-register (prefix register)
300 "Augment contents of REGISTER.
301 Interactively, PREFIX is in raw form.
303 If REGISTER contains a number, add `prefix-numeric-value' of
304 PREFIX to it.
306 If REGISTER is empty or if it contains text, call
307 `append-to-register' with `delete-flag' set to PREFIX.
309 Interactively, reads the register using `register-read-with-preview'."
310 (interactive (list current-prefix-arg
311 (register-read-with-preview "Increment register: ")))
312 (let ((register-val (get-register register)))
313 (cond
314 ((numberp register-val)
315 (let ((number (prefix-numeric-value prefix)))
316 (set-register register (+ number register-val))))
317 ((or (not register-val) (stringp register-val))
318 (append-to-register register (region-beginning) (region-end) prefix))
319 (t (error "Register does not contain a number or text")))))
321 (defun view-register (register)
322 "Display what is contained in register named REGISTER.
323 The Lisp value REGISTER is a character.
325 Interactively, reads the register using `register-read-with-preview'."
326 (interactive (list (register-read-with-preview "View register: ")))
327 (let ((val (get-register register)))
328 (if (null val)
329 (message "Register %s is empty" (single-key-description register))
330 (with-output-to-temp-buffer "*Output*"
331 (describe-register-1 register t)))))
333 (defun list-registers ()
334 "Display a list of nonempty registers saying briefly what they contain."
335 (interactive)
336 (let ((list (copy-sequence register-alist)))
337 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
338 (with-output-to-temp-buffer "*Output*"
339 (dolist (elt list)
340 (when (get-register (car elt))
341 (describe-register-1 (car elt))
342 (terpri))))))
344 (defun describe-register-1 (register &optional verbose)
345 (princ "Register ")
346 (princ (single-key-description register))
347 (princ " contains ")
348 (let ((val (get-register register)))
349 (cond
350 ((registerv-p val)
351 (if (registerv-print-func val)
352 (funcall (registerv-print-func val) (registerv-data val))
353 (princ "[UNPRINTABLE CONTENTS].")))
355 ((numberp val)
356 (princ val))
358 ((markerp val)
359 (let ((buf (marker-buffer val)))
360 (if (null buf)
361 (princ "a marker in no buffer")
362 (princ "a buffer position:\n buffer ")
363 (princ (buffer-name buf))
364 (princ ", position ")
365 (princ (marker-position val)))))
367 ((and (consp val) (window-configuration-p (car val)))
368 (princ "a window configuration."))
370 ((and (consp val) (frame-configuration-p (car val)))
371 (princ "a frame configuration."))
373 ((and (consp val) (eq (car val) 'file))
374 (princ "the file ")
375 (prin1 (cdr val))
376 (princ "."))
378 ((and (consp val) (eq (car val) 'file-query))
379 (princ "a file-query reference:\n file ")
380 (prin1 (car (cdr val)))
381 (princ ",\n position ")
382 (princ (car (cdr (cdr val))))
383 (princ "."))
385 ((consp val)
386 (if verbose
387 (progn
388 (princ "the rectangle:\n")
389 (while val
390 (princ " ")
391 (princ (car val))
392 (terpri)
393 (setq val (cdr val))))
394 (princ "a rectangle starting with ")
395 (princ (car val))))
397 ((stringp val)
398 (setq val (copy-sequence val))
399 (if (eq yank-excluded-properties t)
400 (set-text-properties 0 (length val) nil val)
401 (remove-list-of-text-properties 0 (length val)
402 yank-excluded-properties val))
403 (if verbose
404 (progn
405 (princ "the text:\n")
406 (princ val))
407 (cond
408 ;; Extract first N characters starting with first non-whitespace.
409 ((string-match (format "[^ \t\n].\\{,%d\\}"
410 ;; Deduct 6 for the spaces inserted below.
411 (min 20 (max 0 (- (window-width) 6))))
412 val)
413 (princ "text starting with\n ")
414 (princ (match-string 0 val)))
415 ((string-match "^[ \t\n]+$" val)
416 (princ "whitespace"))
418 (princ "the empty string")))))
420 (princ "Garbage:\n")
421 (if verbose (prin1 val))))))
423 (defun insert-register (register &optional arg)
424 "Insert contents of register REGISTER. (REGISTER is a character.)
425 Normally puts point before and mark after the inserted text.
426 If optional second arg is non-nil, puts mark before and point after.
427 Interactively, second arg is non-nil if prefix arg is supplied.
429 Interactively, reads the register using `register-read-with-preview'."
430 (interactive (progn
431 (barf-if-buffer-read-only)
432 (list (register-read-with-preview "Insert register: ")
433 current-prefix-arg)))
434 (push-mark)
435 (let ((val (get-register register)))
436 (cond
437 ((registerv-p val)
438 (cl-assert (registerv-insert-func val) nil
439 "Don't know how to insert register %s"
440 (single-key-description register))
441 (funcall (registerv-insert-func val) (registerv-data val)))
442 ((consp val)
443 (insert-rectangle val))
444 ((stringp val)
445 (insert-for-yank val))
446 ((numberp val)
447 (princ val (current-buffer)))
448 ((and (markerp val) (marker-position val))
449 (princ (marker-position val) (current-buffer)))
451 (error "Register does not contain text"))))
452 (if (not arg) (exchange-point-and-mark)))
454 (defun copy-to-register (register start end &optional delete-flag region)
455 "Copy region into register REGISTER.
456 With prefix arg, delete as well.
457 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
458 START and END are buffer positions indicating what to copy.
459 The optional argument REGION if non-nil, indicates that we're not just copying
460 some text between START and END, but we're copying the region.
462 Interactively, reads the register using `register-read-with-preview'."
463 (interactive (list (register-read-with-preview "Copy to register: ")
464 (region-beginning)
465 (region-end)
466 current-prefix-arg
468 (set-register register (if region
469 (funcall region-extract-function delete-flag)
470 (prog1 (filter-buffer-substring start end)
471 (if delete-flag (delete-region start end)))))
472 (setq deactivate-mark t)
473 (cond (delete-flag)
474 ((called-interactively-p 'interactive)
475 (indicate-copied-region))))
477 (defun append-to-register (register start end &optional delete-flag)
478 "Append region to text in register REGISTER.
479 With prefix arg, delete as well.
480 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
481 START and END are buffer positions indicating what to append.
483 Interactively, reads the register using `register-read-with-preview'."
484 (interactive (list (register-read-with-preview "Append to register: ")
485 (region-beginning)
486 (region-end)
487 current-prefix-arg))
488 (let ((reg (get-register register))
489 (text (filter-buffer-substring start end))
490 (separator (and register-separator (get-register register-separator))))
491 (set-register
492 register (cond ((not reg) text)
493 ((stringp reg) (concat reg separator text))
494 (t (error "Register does not contain text")))))
495 (setq deactivate-mark t)
496 (cond (delete-flag
497 (delete-region start end))
498 ((called-interactively-p 'interactive)
499 (indicate-copied-region))))
501 (defun prepend-to-register (register start end &optional delete-flag)
502 "Prepend region to text in register REGISTER.
503 With prefix arg, delete as well.
504 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
505 START and END are buffer positions indicating what to prepend.
507 Interactively, reads the register using `register-read-with-preview'."
508 (interactive (list (register-read-with-preview "Prepend to register: ")
509 (region-beginning)
510 (region-end)
511 current-prefix-arg))
512 (let ((reg (get-register register))
513 (text (filter-buffer-substring start end))
514 (separator (and register-separator (get-register register-separator))))
515 (set-register
516 register (cond ((not reg) text)
517 ((stringp reg) (concat text separator reg))
518 (t (error "Register does not contain text")))))
519 (setq deactivate-mark t)
520 (cond (delete-flag
521 (delete-region start end))
522 ((called-interactively-p 'interactive)
523 (indicate-copied-region))))
525 (defun copy-rectangle-to-register (register start end &optional delete-flag)
526 "Copy rectangular region into register REGISTER.
527 With prefix arg, delete as well.
528 To insert this register in the buffer, use \\[insert-register].
530 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
531 START and END are buffer positions giving two corners of rectangle.
533 Interactively, reads the register using `register-read-with-preview'."
534 (interactive (list (register-read-with-preview
535 "Copy rectangle to register: ")
536 (region-beginning)
537 (region-end)
538 current-prefix-arg))
539 (let ((rectangle (if delete-flag
540 (delete-extract-rectangle start end)
541 (extract-rectangle start end))))
542 (set-register register rectangle)
543 (when (and (null delete-flag)
544 (called-interactively-p 'interactive))
545 (setq deactivate-mark t)
546 (indicate-copied-region (length (car rectangle))))))
548 (provide 'register)
549 ;;; register.el ends here