]> code.delx.au - gnu-emacs/blob - lisp/register.el
Merge from emacs-24; up to 117689
[gnu-emacs] / 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: emacs-devel@gnu.org
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: (info "(emacs) Registers").
31
32 (eval-when-compile (require 'cl-lib))
33
34 ;;; Code:
35
36 ;; FIXME: Clean up namespace usage!
37
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))
49
50 (cl-defun registerv-make (data &key print-func jump-func insert-func)
51 "Create a register value object.
52
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))
63
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.")
77
78 (defgroup register nil
79 "Register commands."
80 :group 'convenience
81 :version "24.3")
82
83 (defcustom register-separator nil
84 "Register containing the text to put between collected texts, or nil if none.
85
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 ?+)))
92
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)
100
101 (defun get-register (register)
102 "Return contents of Emacs register named REGISTER, or nil if none."
103 (alist-get register register-alist))
104
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))
109
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)))
118
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))))
124
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.")
129
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 nil
140 (with-current-buffer standard-output
141 (setq cursor-in-non-selected-windows nil)
142 (insert (mapconcat register-preview-function register-alist ""))))))
143
144 (defun register-read-with-preview (prompt)
145 "Read and return a register name, possibly showing existing registers.
146 Prompt with the string PROMPT. If `register-alist' and
147 `register-preview-delay' are both non-nil, display a window
148 listing existing registers after `register-preview-delay' seconds.
149 If `help-char' (or a member of `help-event-list') is pressed,
150 display such a window regardless."
151 (let* ((buffer "*Register Preview*")
152 (timer (when (numberp register-preview-delay)
153 (run-with-timer register-preview-delay nil
154 (lambda ()
155 (unless (get-buffer-window buffer)
156 (register-preview buffer))))))
157 (help-chars (cl-loop for c in (cons help-char help-event-list)
158 when (not (get-register c))
159 collect c)))
160 (unwind-protect
161 (progn
162 (while (memq (read-key (propertize prompt 'face 'minibuffer-prompt))
163 help-chars)
164 (unless (get-buffer-window buffer)
165 (register-preview buffer 'show-empty)))
166 (if (characterp last-input-event) last-input-event
167 (error "Non-character input-event")))
168 (and (timerp timer) (cancel-timer timer))
169 (let ((w (get-buffer-window buffer)))
170 (and (window-live-p w) (delete-window w)))
171 (and (get-buffer buffer) (kill-buffer buffer)))))
172
173 (defun point-to-register (register &optional arg)
174 "Store current location of point in register REGISTER.
175 With prefix argument, store current frame configuration.
176 Use \\[jump-to-register] to go to that location or restore that configuration.
177 Argument is a character, naming the register.
178
179 Interactively, reads the register using `register-read-with-preview'."
180 (interactive (list (register-read-with-preview "Point to register: ")
181 current-prefix-arg))
182 ;; Turn the marker into a file-ref if the buffer is killed.
183 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
184 (set-register register
185 (if arg (list (current-frame-configuration) (point-marker))
186 (point-marker))))
187
188 (defun window-configuration-to-register (register &optional _arg)
189 "Store the window configuration of the selected frame in register REGISTER.
190 Use \\[jump-to-register] to restore the configuration.
191 Argument is a character, naming the register.
192
193 Interactively, reads the register using `register-read-with-preview'."
194 (interactive (list (register-read-with-preview
195 "Window configuration to register: ")
196 current-prefix-arg))
197 ;; current-window-configuration does not include the value
198 ;; of point in the current buffer, so record that separately.
199 (set-register register (list (current-window-configuration) (point-marker))))
200
201 ;; It has had the optional arg for ages, but never used it.
202 (set-advertised-calling-convention 'window-configuration-to-register
203 '(register) "24.4")
204
205 (defun frame-configuration-to-register (register &optional _arg)
206 "Store the window configuration of all frames in register REGISTER.
207 Use \\[jump-to-register] to restore the configuration.
208 Argument is a character, naming the register.
209
210 Interactively, reads the register using `register-read-with-preview'."
211 (interactive (list (register-read-with-preview
212 "Frame configuration to register: ")
213 current-prefix-arg))
214 ;; current-frame-configuration does not include the value
215 ;; of point in the current buffer, so record that separately.
216 (set-register register (list (current-frame-configuration) (point-marker))))
217
218 ;; It has had the optional arg for ages, but never used it.
219 (set-advertised-calling-convention 'frame-configuration-to-register
220 '(register) "24.4")
221
222 (make-obsolete 'frame-configuration-to-register 'frameset-to-register' "24.4")
223
224 (defalias 'register-to-point 'jump-to-register)
225 (defun jump-to-register (register &optional delete)
226 "Move point to location stored in a register.
227 If the register contains a file name, find that file.
228 \(To put a file name in a register, you must use `set-register'.)
229 If the register contains a window configuration (one frame) or a frameset
230 \(all frames), restore that frame or all frames accordingly.
231 First argument is a character, naming the register.
232 Optional second arg non-nil (interactively, prefix argument) says to
233 delete any existing frames that the frameset doesn't mention.
234 \(Otherwise, these frames are iconified.)
235
236 Interactively, reads the register using `register-read-with-preview'."
237 (interactive (list (register-read-with-preview "Jump to register: ")
238 current-prefix-arg))
239 (let ((val (get-register register)))
240 (cond
241 ((registerv-p val)
242 (cl-assert (registerv-jump-func val) nil
243 "Don't know how to jump to register %s"
244 (single-key-description register))
245 (funcall (registerv-jump-func val) (registerv-data val)))
246 ((and (consp val) (frame-configuration-p (car val)))
247 (set-frame-configuration (car val) (not delete))
248 (goto-char (cadr val)))
249 ((and (consp val) (window-configuration-p (car val)))
250 (set-window-configuration (car val))
251 (goto-char (cadr val)))
252 ((markerp val)
253 (or (marker-buffer val)
254 (error "That register's buffer no longer exists"))
255 (switch-to-buffer (marker-buffer val))
256 (goto-char val))
257 ((and (consp val) (eq (car val) 'file))
258 (find-file (cdr val)))
259 ((and (consp val) (eq (car val) 'file-query))
260 (or (find-buffer-visiting (nth 1 val))
261 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
262 (error "Register access aborted"))
263 (find-file (nth 1 val))
264 (goto-char (nth 2 val)))
265 (t
266 (error "Register doesn't contain a buffer position or configuration")))))
267
268 (defun register-swap-out ()
269 "Turn markers into file-query references when a buffer is killed."
270 (and buffer-file-name
271 (dolist (elem register-alist)
272 (and (markerp (cdr elem))
273 (eq (marker-buffer (cdr elem)) (current-buffer))
274 (setcdr elem
275 (list 'file-query
276 buffer-file-name
277 (marker-position (cdr elem))))))))
278
279 (defun number-to-register (number register)
280 "Store a number in a register.
281 Two args, NUMBER and REGISTER (a character, naming the register).
282 If NUMBER is nil, a decimal number is read from the buffer starting
283 at point, and point moves to the end of that number.
284 Interactively, NUMBER is the prefix arg (none means nil).
285
286 Interactively, reads the register using `register-read-with-preview'."
287 (interactive (list current-prefix-arg
288 (register-read-with-preview "Number to register: ")))
289 (set-register register
290 (if number
291 (prefix-numeric-value number)
292 (if (looking-at "\\s-*-?[0-9]+")
293 (progn
294 (goto-char (match-end 0))
295 (string-to-number (match-string 0)))
296 0))))
297
298 (defun increment-register (prefix register)
299 "Augment contents of REGISTER.
300 Interactively, PREFIX is in raw form.
301
302 If REGISTER contains a number, add `prefix-numeric-value' of
303 PREFIX to it.
304
305 If REGISTER is empty or if it contains text, call
306 `append-to-register' with `delete-flag' set to PREFIX.
307
308 Interactively, reads the register using `register-read-with-preview'."
309 (interactive (list current-prefix-arg
310 (register-read-with-preview "Increment register: ")))
311 (let ((register-val (get-register register)))
312 (cond
313 ((numberp register-val)
314 (let ((number (prefix-numeric-value prefix)))
315 (set-register register (+ number register-val))))
316 ((or (not register-val) (stringp register-val))
317 (append-to-register register (region-beginning) (region-end) prefix))
318 (t (error "Register does not contain a number or text")))))
319
320 (defun view-register (register)
321 "Display what is contained in register named REGISTER.
322 The Lisp value REGISTER is a character.
323
324 Interactively, reads the register using `register-read-with-preview'."
325 (interactive (list (register-read-with-preview "View register: ")))
326 (let ((val (get-register register)))
327 (if (null val)
328 (message "Register %s is empty" (single-key-description register))
329 (with-output-to-temp-buffer "*Output*"
330 (describe-register-1 register t)))))
331
332 (defun list-registers ()
333 "Display a list of nonempty registers saying briefly what they contain."
334 (interactive)
335 (let ((list (copy-sequence register-alist)))
336 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
337 (with-output-to-temp-buffer "*Output*"
338 (dolist (elt list)
339 (when (get-register (car elt))
340 (describe-register-1 (car elt))
341 (terpri))))))
342
343 (defun describe-register-1 (register &optional verbose)
344 (princ "Register ")
345 (princ (single-key-description register))
346 (princ " contains ")
347 (let ((val (get-register register)))
348 (cond
349 ((registerv-p val)
350 (if (registerv-print-func val)
351 (funcall (registerv-print-func val) (registerv-data val))
352 (princ "[UNPRINTABLE CONTENTS].")))
353
354 ((numberp val)
355 (princ val))
356
357 ((markerp val)
358 (let ((buf (marker-buffer val)))
359 (if (null buf)
360 (princ "a marker in no buffer")
361 (princ "a buffer position:\n buffer ")
362 (princ (buffer-name buf))
363 (princ ", position ")
364 (princ (marker-position val)))))
365
366 ((and (consp val) (window-configuration-p (car val)))
367 (princ "a window configuration."))
368
369 ((and (consp val) (frame-configuration-p (car val)))
370 (princ "a frame configuration."))
371
372 ((and (consp val) (eq (car val) 'file))
373 (princ "the file ")
374 (prin1 (cdr val))
375 (princ "."))
376
377 ((and (consp val) (eq (car val) 'file-query))
378 (princ "a file-query reference:\n file ")
379 (prin1 (car (cdr val)))
380 (princ ",\n position ")
381 (princ (car (cdr (cdr val))))
382 (princ "."))
383
384 ((consp val)
385 (if verbose
386 (progn
387 (princ "the rectangle:\n")
388 (while val
389 (princ " ")
390 (princ (car val))
391 (terpri)
392 (setq val (cdr val))))
393 (princ "a rectangle starting with ")
394 (princ (car val))))
395
396 ((stringp val)
397 (setq val (copy-sequence val))
398 (if (eq yank-excluded-properties t)
399 (set-text-properties 0 (length val) nil val)
400 (remove-list-of-text-properties 0 (length val)
401 yank-excluded-properties val))
402 (if verbose
403 (progn
404 (princ "the text:\n")
405 (princ val))
406 (cond
407 ;; Extract first N characters starting with first non-whitespace.
408 ((string-match (format "[^ \t\n].\\{,%d\\}"
409 ;; Deduct 6 for the spaces inserted below.
410 (min 20 (max 0 (- (window-width) 6))))
411 val)
412 (princ "text starting with\n ")
413 (princ (match-string 0 val)))
414 ((string-match "^[ \t\n]+$" val)
415 (princ "whitespace"))
416 (t
417 (princ "the empty string")))))
418 (t
419 (princ "Garbage:\n")
420 (if verbose (prin1 val))))))
421
422 (defun insert-register (register &optional arg)
423 "Insert contents of register REGISTER. (REGISTER is a character.)
424 Normally puts point before and mark after the inserted text.
425 If optional second arg is non-nil, puts mark before and point after.
426 Interactively, second arg is nil if prefix arg is supplied and t
427 otherwise.
428
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 (not 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)))
450 (t
451 (error "Register does not contain text"))))
452 (if (not arg) (exchange-point-and-mark)))
453
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 five args: REGISTER, START, END, DELETE-FLAG,
458 and REGION. START and END are buffer positions indicating what to copy.
459 The optional argument REGION if non-nil, indicates that we're not just
460 copying some text between START and END, but we're copying the region.
461
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
467 t))
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))))
476
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.
482
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))))
500
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.
506
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))))
524
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].
529
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.
532
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))))))
547
548 (provide 'register)
549 ;;; register.el ends here