Update copyright year to 2015
[emacs.git] / lisp / register.el
bloba60181e43c6f39828c38c6e4f4e1884848dd6bae
1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2015 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: (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 (if (characterp last-input-event) last-input-event
168 (error "Non-character input-event")))
169 (and (timerp timer) (cancel-timer timer))
170 (let ((w (get-buffer-window buffer)))
171 (and (window-live-p w) (delete-window w)))
172 (and (get-buffer buffer) (kill-buffer buffer)))))
174 (defun point-to-register (register &optional arg)
175 "Store current location of point in register REGISTER.
176 With prefix argument, store current frame configuration.
177 Use \\[jump-to-register] to go to that location or restore that configuration.
178 Argument is a character, naming the register.
180 Interactively, reads the register using `register-read-with-preview'."
181 (interactive (list (register-read-with-preview "Point to register: ")
182 current-prefix-arg))
183 ;; Turn the marker into a file-ref if the buffer is killed.
184 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
185 (set-register register
186 (if arg (list (current-frame-configuration) (point-marker))
187 (point-marker))))
189 (defun window-configuration-to-register (register &optional _arg)
190 "Store the window configuration of the selected frame in register REGISTER.
191 Use \\[jump-to-register] to restore the configuration.
192 Argument is a character, naming the register.
194 Interactively, reads the register using `register-read-with-preview'."
195 (interactive (list (register-read-with-preview
196 "Window configuration to register: ")
197 current-prefix-arg))
198 ;; current-window-configuration does not include the value
199 ;; of point in the current buffer, so record that separately.
200 (set-register register (list (current-window-configuration) (point-marker))))
202 ;; It has had the optional arg for ages, but never used it.
203 (set-advertised-calling-convention 'window-configuration-to-register
204 '(register) "24.4")
206 (defun frame-configuration-to-register (register &optional _arg)
207 "Store the window configuration of all frames in register REGISTER.
208 Use \\[jump-to-register] to restore the configuration.
209 Argument is a character, naming the register.
211 Interactively, reads the register using `register-read-with-preview'."
212 (interactive (list (register-read-with-preview
213 "Frame configuration to register: ")
214 current-prefix-arg))
215 ;; current-frame-configuration does not include the value
216 ;; of point in the current buffer, so record that separately.
217 (set-register register (list (current-frame-configuration) (point-marker))))
219 ;; It has had the optional arg for ages, but never used it.
220 (set-advertised-calling-convention 'frame-configuration-to-register
221 '(register) "24.4")
223 (make-obsolete 'frame-configuration-to-register 'frameset-to-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 nil if prefix arg is supplied and t
428 otherwise.
430 Interactively, reads the register using `register-read-with-preview'."
431 (interactive (progn
432 (barf-if-buffer-read-only)
433 (list (register-read-with-preview "Insert register: ")
434 (not current-prefix-arg))))
435 (push-mark)
436 (let ((val (get-register register)))
437 (cond
438 ((registerv-p val)
439 (cl-assert (registerv-insert-func val) nil
440 "Don't know how to insert register %s"
441 (single-key-description register))
442 (funcall (registerv-insert-func val) (registerv-data val)))
443 ((consp val)
444 (insert-rectangle val))
445 ((stringp val)
446 (insert-for-yank val))
447 ((numberp val)
448 (princ val (current-buffer)))
449 ((and (markerp val) (marker-position val))
450 (princ (marker-position val) (current-buffer)))
452 (error "Register does not contain text"))))
453 (if (not arg) (exchange-point-and-mark)))
455 (defun copy-to-register (register start end &optional delete-flag region)
456 "Copy region into register REGISTER.
457 With prefix arg, delete as well.
458 Called from program, takes five args: REGISTER, START, END, DELETE-FLAG,
459 and REGION. START and END are buffer positions indicating what to copy.
460 The optional argument REGION if non-nil, indicates that we're not just
461 copying some text between START and END, but we're copying the region.
463 Interactively, reads the register using `register-read-with-preview'."
464 (interactive (list (register-read-with-preview "Copy to register: ")
465 (region-beginning)
466 (region-end)
467 current-prefix-arg
469 (set-register register (if region
470 (funcall region-extract-function delete-flag)
471 (prog1 (filter-buffer-substring start end)
472 (if delete-flag (delete-region start end)))))
473 (setq deactivate-mark t)
474 (cond (delete-flag)
475 ((called-interactively-p 'interactive)
476 (indicate-copied-region))))
478 (defun append-to-register (register start end &optional delete-flag)
479 "Append region to text in register REGISTER.
480 With prefix arg, delete as well.
481 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
482 START and END are buffer positions indicating what to append.
484 Interactively, reads the register using `register-read-with-preview'."
485 (interactive (list (register-read-with-preview "Append to register: ")
486 (region-beginning)
487 (region-end)
488 current-prefix-arg))
489 (let ((reg (get-register register))
490 (text (filter-buffer-substring start end))
491 (separator (and register-separator (get-register register-separator))))
492 (set-register
493 register (cond ((not reg) text)
494 ((stringp reg) (concat reg separator text))
495 (t (error "Register does not contain text")))))
496 (setq deactivate-mark t)
497 (cond (delete-flag
498 (delete-region start end))
499 ((called-interactively-p 'interactive)
500 (indicate-copied-region))))
502 (defun prepend-to-register (register start end &optional delete-flag)
503 "Prepend region to text in register REGISTER.
504 With prefix arg, delete as well.
505 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
506 START and END are buffer positions indicating what to prepend.
508 Interactively, reads the register using `register-read-with-preview'."
509 (interactive (list (register-read-with-preview "Prepend to register: ")
510 (region-beginning)
511 (region-end)
512 current-prefix-arg))
513 (let ((reg (get-register register))
514 (text (filter-buffer-substring start end))
515 (separator (and register-separator (get-register register-separator))))
516 (set-register
517 register (cond ((not reg) text)
518 ((stringp reg) (concat text separator reg))
519 (t (error "Register does not contain text")))))
520 (setq deactivate-mark t)
521 (cond (delete-flag
522 (delete-region start end))
523 ((called-interactively-p 'interactive)
524 (indicate-copied-region))))
526 (defun copy-rectangle-to-register (register start end &optional delete-flag)
527 "Copy rectangular region into register REGISTER.
528 With prefix arg, delete as well.
529 To insert this register in the buffer, use \\[insert-register].
531 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
532 START and END are buffer positions giving two corners of rectangle.
534 Interactively, reads the register using `register-read-with-preview'."
535 (interactive (list (register-read-with-preview
536 "Copy rectangle to register: ")
537 (region-beginning)
538 (region-end)
539 current-prefix-arg))
540 (let ((rectangle (if delete-flag
541 (delete-extract-rectangle start end)
542 (extract-rectangle start end))))
543 (set-register register rectangle)
544 (when (and (null delete-flag)
545 (called-interactively-p 'interactive))
546 (setq deactivate-mark t)
547 (indicate-copied-region (length (car rectangle))))))
549 (provide 'register)
550 ;;; register.el ends here