Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / register.el
blobf3c18a86fb5089fcab49266a4b4f78ce1c19f591
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: FSF
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 delay in seconds to pop up the preview window."
94 :version "24.4"
95 :type '(choice number (const :tag "Indefinitely" nil))
96 :group 'register)
98 (defun get-register (register)
99 "Return contents of Emacs register named REGISTER, or nil if none."
100 (cdr (assq register register-alist)))
102 (defun set-register (register value)
103 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
104 See the documentation of the variable `register-alist' for possible VALUEs."
105 (let ((aelt (assq register register-alist)))
106 (if aelt
107 (setcdr aelt value)
108 (push (cons register value) register-alist))
109 value))
111 (defun register-describe-oneline (c)
112 "One-line description of register C."
113 (let ((d (replace-regexp-in-string
114 "\n[ \t]*" " "
115 (with-output-to-string (describe-register-1 c)))))
116 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d)
117 (substring d (match-end 0))
118 d)))
120 (defvar register-preview-functions nil)
122 (defun register-preview (buffer &optional show-empty)
123 "Pop up a window to show register preview in BUFFER.
124 If SHOW-EMPTY is non-nil show the window even if no registers."
125 (when (or show-empty (consp register-alist))
126 (with-temp-buffer-window
127 buffer
128 (cons 'display-buffer-below-selected
129 '((window-height . fit-window-to-buffer)))
131 (with-current-buffer standard-output
132 (setq cursor-in-non-selected-windows nil)
133 (mapc
134 (lambda (r)
135 (insert (or (run-hook-with-args-until-success
136 'register-preview-functions r)
137 (format "%s %s\n"
138 (concat (single-key-description (car r)) ":")
139 (register-describe-oneline (car r))))))
140 register-alist)))))
142 (defun register-read-with-preview (prompt)
143 "Read an event with register preview using PROMPT.
144 Pop up a register preview window if the input is a help char but
145 is not a register. Alternatively if `register-preview-delay' is a
146 number the preview window is popped up after some delay."
147 (let* ((buffer "*Register Preview*")
148 (timer (when (numberp register-preview-delay)
149 (run-with-timer register-preview-delay nil
150 (lambda ()
151 (unless (get-buffer-window buffer)
152 (register-preview buffer))))))
153 (help-chars (cl-loop for c in (cons help-char help-event-list)
154 when (not (get-register c))
155 collect c)))
156 (unwind-protect
157 (progn
158 (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt))
159 help-chars)
160 (unless (get-buffer-window buffer)
161 (register-preview buffer 'show-empty)))
162 last-input-event)
163 (and (timerp timer) (cancel-timer timer))
164 (let ((w (get-buffer-window buffer)))
165 (and (window-live-p w) (delete-window w)))
166 (and (get-buffer buffer) (kill-buffer buffer)))))
168 (defun point-to-register (register &optional arg)
169 "Store current location of point in register REGISTER.
170 With prefix argument, store current frame configuration.
171 Use \\[jump-to-register] to go to that location or restore that configuration.
172 Argument is a character, naming the register."
173 (interactive (list (register-read-with-preview "Point to register: ")
174 current-prefix-arg))
175 ;; Turn the marker into a file-ref if the buffer is killed.
176 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
177 (set-register register
178 (if arg (list (current-frame-configuration) (point-marker))
179 (point-marker))))
181 (defun window-configuration-to-register (register &optional _arg)
182 "Store the window configuration of the selected frame in register REGISTER.
183 Use \\[jump-to-register] to restore the configuration.
184 Argument is a character, naming the register."
185 (interactive (list (register-read-with-preview
186 "Window configuration to register: ")
187 current-prefix-arg))
188 ;; current-window-configuration does not include the value
189 ;; of point in the current buffer, so record that separately.
190 (set-register register (list (current-window-configuration) (point-marker))))
192 (defun frame-configuration-to-register (register &optional _arg)
193 "Store the window configuration of all frames in register REGISTER.
194 Use \\[jump-to-register] to restore the configuration.
195 Argument is a character, naming the register."
196 (interactive (list (register-read-with-preview
197 "Frame configuration to register: ")
198 current-prefix-arg))
199 ;; current-frame-configuration does not include the value
200 ;; of point in the current buffer, so record that separately.
201 (set-register register (list (current-frame-configuration) (point-marker))))
203 (defalias 'register-to-point 'jump-to-register)
204 (defun jump-to-register (register &optional delete)
205 "Move point to location stored in a register.
206 If the register contains a file name, find that file.
207 \(To put a file name in a register, you must use `set-register'.)
208 If the register contains a window configuration (one frame) or a frameset
209 \(all frames), restore that frame or all frames accordingly.
210 First argument is a character, naming the register.
211 Optional second arg non-nil (interactively, prefix argument) says to
212 delete any existing frames that the frameset doesn't mention.
213 \(Otherwise, these frames are iconified.)"
214 (interactive (list (register-read-with-preview "Jump to register: ")
215 current-prefix-arg))
216 (let ((val (get-register register)))
217 (cond
218 ((registerv-p val)
219 (cl-assert (registerv-jump-func val) nil
220 "Don't know how to jump to register %s"
221 (single-key-description register))
222 (funcall (registerv-jump-func val) (registerv-data val)))
223 ((and (consp val) (frame-configuration-p (car val)))
224 (set-frame-configuration (car val) (not delete))
225 (goto-char (cadr val)))
226 ((and (consp val) (window-configuration-p (car val)))
227 (set-window-configuration (car val))
228 (goto-char (cadr val)))
229 ((markerp val)
230 (or (marker-buffer val)
231 (error "That register's buffer no longer exists"))
232 (switch-to-buffer (marker-buffer val))
233 (goto-char val))
234 ((and (consp val) (eq (car val) 'file))
235 (find-file (cdr val)))
236 ((and (consp val) (eq (car val) 'file-query))
237 (or (find-buffer-visiting (nth 1 val))
238 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
239 (error "Register access aborted"))
240 (find-file (nth 1 val))
241 (goto-char (nth 2 val)))
243 (error "Register doesn't contain a buffer position or configuration")))))
245 (defun register-swap-out ()
246 "Turn markers into file-query references when a buffer is killed."
247 (and buffer-file-name
248 (dolist (elem register-alist)
249 (and (markerp (cdr elem))
250 (eq (marker-buffer (cdr elem)) (current-buffer))
251 (setcdr elem
252 (list 'file-query
253 buffer-file-name
254 (marker-position (cdr elem))))))))
256 (defun number-to-register (number register)
257 "Store a number in a register.
258 Two args, NUMBER and REGISTER (a character, naming the register).
259 If NUMBER is nil, a decimal number is read from the buffer starting
260 at point, and point moves to the end of that number.
261 Interactively, NUMBER is the prefix arg (none means nil)."
262 (interactive (list current-prefix-arg
263 (register-read-with-preview "Number to register: ")))
264 (set-register register
265 (if number
266 (prefix-numeric-value number)
267 (if (looking-at "\\s-*-?[0-9]+")
268 (progn
269 (goto-char (match-end 0))
270 (string-to-number (match-string 0)))
271 0))))
273 (defun increment-register (prefix register)
274 "Augment contents of REGISTER.
275 Interactively, PREFIX is in raw form.
277 If REGISTER contains a number, add `prefix-numeric-value' of
278 PREFIX to it.
280 If REGISTER is empty or if it contains text, call
281 `append-to-register' with `delete-flag' set to PREFIX."
282 (interactive "P\ncIncrement register: ")
283 (let ((register-val (get-register register)))
284 (cond
285 ((numberp register-val)
286 (let ((number (prefix-numeric-value prefix)))
287 (set-register register (+ number register-val))))
288 ((or (not register-val) (stringp register-val))
289 (append-to-register register (region-beginning) (region-end) prefix))
290 (t (error "Register does not contain a number or text")))))
292 (defun view-register (register)
293 "Display what is contained in register named REGISTER.
294 The Lisp value REGISTER is a character."
295 (interactive (list (register-read-with-preview "View register: ")))
296 (let ((val (get-register register)))
297 (if (null val)
298 (message "Register %s is empty" (single-key-description register))
299 (with-output-to-temp-buffer "*Output*"
300 (describe-register-1 register t)))))
302 (defun list-registers ()
303 "Display a list of nonempty registers saying briefly what they contain."
304 (interactive)
305 (let ((list (copy-sequence register-alist)))
306 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
307 (with-output-to-temp-buffer "*Output*"
308 (dolist (elt list)
309 (when (get-register (car elt))
310 (describe-register-1 (car elt))
311 (terpri))))))
313 (defun describe-register-1 (register &optional verbose)
314 (princ "Register ")
315 (princ (single-key-description register))
316 (princ " contains ")
317 (let ((val (get-register register)))
318 (cond
319 ((registerv-p val)
320 (if (registerv-print-func val)
321 (funcall (registerv-print-func val) (registerv-data val))
322 (princ "[UNPRINTABLE CONTENTS].")))
324 ((numberp val)
325 (princ val))
327 ((markerp val)
328 (let ((buf (marker-buffer val)))
329 (if (null buf)
330 (princ "a marker in no buffer")
331 (princ "a buffer position:\n buffer ")
332 (princ (buffer-name buf))
333 (princ ", position ")
334 (princ (marker-position val)))))
336 ((and (consp val) (window-configuration-p (car val)))
337 (princ "a window configuration."))
339 ((and (consp val) (frame-configuration-p (car val)))
340 (princ "a frame configuration."))
342 ((and (consp val) (eq (car val) 'file))
343 (princ "the file ")
344 (prin1 (cdr val))
345 (princ "."))
347 ((and (consp val) (eq (car val) 'file-query))
348 (princ "a file-query reference:\n file ")
349 (prin1 (car (cdr val)))
350 (princ ",\n position ")
351 (princ (car (cdr (cdr val))))
352 (princ "."))
354 ((consp val)
355 (if verbose
356 (progn
357 (princ "the rectangle:\n")
358 (while val
359 (princ " ")
360 (princ (car val))
361 (terpri)
362 (setq val (cdr val))))
363 (princ "a rectangle starting with ")
364 (princ (car val))))
366 ((stringp val)
367 (setq val (copy-sequence val))
368 (if (eq yank-excluded-properties t)
369 (set-text-properties 0 (length val) nil val)
370 (remove-list-of-text-properties 0 (length val)
371 yank-excluded-properties val))
372 (if verbose
373 (progn
374 (princ "the text:\n")
375 (princ val))
376 (cond
377 ;; Extract first N characters starting with first non-whitespace.
378 ((string-match (format "[^ \t\n].\\{,%d\\}"
379 ;; Deduct 6 for the spaces inserted below.
380 (min 20 (max 0 (- (window-width) 6))))
381 val)
382 (princ "text starting with\n ")
383 (princ (match-string 0 val)))
384 ((string-match "^[ \t\n]+$" val)
385 (princ "whitespace"))
387 (princ "the empty string")))))
389 (princ "Garbage:\n")
390 (if verbose (prin1 val))))))
392 (defun insert-register (register &optional arg)
393 "Insert contents of register REGISTER. (REGISTER is a character.)
394 Normally puts point before and mark after the inserted text.
395 If optional second arg is non-nil, puts mark before and point after.
396 Interactively, second arg is non-nil if prefix arg is supplied."
397 (interactive (progn
398 (barf-if-buffer-read-only)
399 (list (register-read-with-preview "Insert register: ")
400 current-prefix-arg)))
401 (push-mark)
402 (let ((val (get-register register)))
403 (cond
404 ((registerv-p val)
405 (cl-assert (registerv-insert-func val) nil
406 "Don't know how to insert register %s"
407 (single-key-description register))
408 (funcall (registerv-insert-func val) (registerv-data val)))
409 ((consp val)
410 (insert-rectangle val))
411 ((stringp val)
412 (insert-for-yank val))
413 ((numberp val)
414 (princ val (current-buffer)))
415 ((and (markerp val) (marker-position val))
416 (princ (marker-position val) (current-buffer)))
418 (error "Register does not contain text"))))
419 (if (not arg) (exchange-point-and-mark)))
421 (defun copy-to-register (register start end &optional delete-flag region)
422 "Copy region into register REGISTER.
423 With prefix arg, delete as well.
424 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
425 START and END are buffer positions indicating what to copy.
426 The optional argument REGION if non-nil, indicates that we're not just copying
427 some text between START and END, but we're copying the region."
428 (interactive (list (register-read-with-preview "Copy to register: ")
429 (region-beginning)
430 (region-end)
431 current-prefix-arg
433 (set-register register (if region
434 (funcall region-extract-function delete-flag)
435 (prog1 (filter-buffer-substring start end)
436 (if delete-flag (delete-region start end)))))
437 (setq deactivate-mark t)
438 (cond (delete-flag)
439 ((called-interactively-p 'interactive)
440 (indicate-copied-region))))
442 (defun append-to-register (register start end &optional delete-flag)
443 "Append region to text in register REGISTER.
444 With prefix arg, delete as well.
445 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
446 START and END are buffer positions indicating what to append."
447 (interactive (list (register-read-with-preview "Append to register: ")
448 (region-beginning)
449 (region-end)
450 current-prefix-arg))
451 (let ((reg (get-register register))
452 (text (filter-buffer-substring start end))
453 (separator (and register-separator (get-register register-separator))))
454 (set-register
455 register (cond ((not reg) text)
456 ((stringp reg) (concat reg separator text))
457 (t (error "Register does not contain text")))))
458 (setq deactivate-mark t)
459 (cond (delete-flag
460 (delete-region start end))
461 ((called-interactively-p 'interactive)
462 (indicate-copied-region))))
464 (defun prepend-to-register (register start end &optional delete-flag)
465 "Prepend region to text in register REGISTER.
466 With prefix arg, delete as well.
467 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
468 START and END are buffer positions indicating what to prepend."
469 (interactive (list (register-read-with-preview "Prepend to register: ")
470 (region-beginning)
471 (region-end)
472 current-prefix-arg))
473 (let ((reg (get-register register))
474 (text (filter-buffer-substring start end))
475 (separator (and register-separator (get-register register-separator))))
476 (set-register
477 register (cond ((not reg) text)
478 ((stringp reg) (concat text separator reg))
479 (t (error "Register does not contain text")))))
480 (setq deactivate-mark t)
481 (cond (delete-flag
482 (delete-region start end))
483 ((called-interactively-p 'interactive)
484 (indicate-copied-region))))
486 (defun copy-rectangle-to-register (register start end &optional delete-flag)
487 "Copy rectangular region into register REGISTER.
488 With prefix arg, delete as well.
489 To insert this register in the buffer, use \\[insert-register].
491 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
492 START and END are buffer positions giving two corners of rectangle."
493 (interactive (list (register-read-with-preview
494 "Copy rectangle to register: ")
495 (region-beginning)
496 (region-end)
497 current-prefix-arg))
498 (let ((rectangle (if delete-flag
499 (delete-extract-rectangle start end)
500 (extract-rectangle start end))))
501 (set-register register rectangle)
502 (when (and (null delete-flag)
503 (called-interactively-p 'interactive))
504 (setq deactivate-mark t)
505 (indicate-copied-region (length (car rectangle))))))
507 (provide 'register)
508 ;;; register.el ends here