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