Fix last change of grow_mini_window.
[bpt/emacs.git] / lisp / register.el
CommitLineData
85698d63 1;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
c88ab9ce 2
bb098075 3;; Copyright (C) 1985, 1993-1994, 2001-2013 Free Software Foundation, Inc.
9750e079 4
4821e2af 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: internal
bd78fa1d 7;; Package: emacs
4821e2af 8
efeae993
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
efeae993 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
efeae993
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
efeae993 23
d9ecc911
ER
24;;; Commentary:
25
26;; This package of functions emulates and somewhat extends the venerable
27;; TECO's `register' feature, which permits you to save various useful
28;; pieces of buffer state to named variables. The entry points are
29;; documented in the Emacs user's manual.
30
f58e0fd5 31(eval-when-compile (require 'cl-lib))
6302e0d3 32
4821e2af 33;;; Code:
efeae993 34
f58e0fd5 35(cl-defstruct
6302e0d3
LL
36 (registerv (:constructor nil)
37 (:constructor registerv--make (&optional data print-func
38 jump-func insert-func))
39 (:copier nil)
40 (:type vector)
41 :named)
42 (data nil :read-only t)
43 (print-func nil :read-only t)
44 (jump-func nil :read-only t)
45 (insert-func nil :read-only t))
46
f58e0fd5 47(cl-defun registerv-make (data &key print-func jump-func insert-func)
6302e0d3
LL
48 "Create a register value object.
49
50DATA can be any value.
51PRINT-FUNC if provided controls how `list-registers' and
52`view-register' print the register. It should be a function
9173deec 53receiving one argument DATA and print text that completes
6302e0d3
LL
54this sentence:
55 Register X contains [TEXT PRINTED BY PRINT-FUNC]
56JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
57INSERT-FUNC if provided, controls how `insert-register' insert the register.
58They both receive DATA as argument."
59 (registerv--make data print-func jump-func insert-func))
60
efeae993
RS
61(defvar register-alist nil
62 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
6302e0d3
LL
63NAME is a character (a number). CONTENTS is a string, number, marker, list
64or a struct returned by `registerv-make'.
22073dda 65A list of strings represents a rectangle.
5858bcc4
CY
66A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
67A list of the form (file-query FILE-NAME POSITION) represents
68 position POSITION in the file named FILE-NAME, but query before
69 visiting it.
070c2506
KH
70A list of the form (WINDOW-CONFIGURATION POSITION)
71 represents a saved window configuration plus a saved value of point.
72A list of the form (FRAME-CONFIGURATION POSITION)
77187e6f 73 represents a saved frame configuration plus a saved value of point.")
efeae993 74
0979429b
J
75(defgroup register nil
76 "Register commands."
77 :group 'convenience
bfabf70a 78 :version "24.3")
0979429b 79
bfabf70a
AS
80(defcustom register-separator nil
81 "Register containing the text to put between collected texts, or nil if none.
0979429b
J
82
83When collecting text with
84`append-to-register' (resp. `prepend-to-register') contents of
85this register is added to the beginning (resp. end) of the marked
86text."
87 :group 'register
88 :type '(choice (const :tag "None" nil)
89 (character :tag "Use register" :value ?+)))
90
85698d63
LL
91(defcustom register-preview-delay 1
92 "If non-nil delay in seconds to pop up the preview window."
bb098075 93 :version "24.4"
85698d63
LL
94 :type '(choice number (const :tag "Indefinitely" nil))
95 :group 'register)
96
1a86cc81
JB
97(defun get-register (register)
98 "Return contents of Emacs register named REGISTER, or nil if none."
99 (cdr (assq register register-alist)))
efeae993 100
1b8cac5d
RS
101(defun set-register (register value)
102 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
1a86cc81 103See the documentation of the variable `register-alist' for possible VALUEs."
1b8cac5d 104 (let ((aelt (assq register register-alist)))
efeae993
RS
105 (if aelt
106 (setcdr aelt value)
ddbb3cc7 107 (push (cons register value) register-alist))
efeae993
RS
108 value))
109
85698d63
LL
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(defvar register-preview-functions nil)
120
121(defun register-preview (buffer &optional show-empty)
122 "Pop up a window to show register preview in BUFFER.
123If SHOW-EMPTY is non-nil show the window even if no registers."
124 (when (or show-empty (consp register-alist))
cf2b7efc
MR
125 (with-temp-buffer-window
126 buffer
127 (cons 'display-buffer-below-selected
128 '((window-height . fit-window-to-buffer)))
129 nil
130 (with-current-buffer standard-output
131 (setq cursor-in-non-selected-windows nil)
132 (mapc
133 (lambda (r)
134 (insert (or (run-hook-with-args-until-success
135 'register-preview-functions r)
136 (format "%s %s\n"
137 (concat (single-key-description (car r)) ":")
138 (register-describe-oneline (car r))))))
139 register-alist)))))
85698d63
LL
140
141(defun register-read-with-preview (prompt)
142 "Read an event with register preview using PROMPT.
143Pop up a register preview window if the input is a help char but
144is not a register. Alternatively if `register-preview-delay' is a
145number the preview window is popped up after some delay."
146 (let* ((buffer "*Register Preview*")
147 (timer (when (numberp register-preview-delay)
148 (run-with-timer register-preview-delay nil
149 (lambda ()
150 (unless (get-buffer-window buffer)
151 (register-preview buffer))))))
152 (help-chars (cl-loop for c in (cons help-char help-event-list)
153 when (not (get-register c))
154 collect c)))
155 (unwind-protect
156 (progn
157 (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt))
158 help-chars)
159 (unless (get-buffer-window buffer)
160 (register-preview buffer 'show-empty)))
161 last-input-event)
162 (and (timerp timer) (cancel-timer timer))
163 (let ((w (get-buffer-window buffer)))
164 (and (window-live-p w) (delete-window w)))
165 (and (get-buffer buffer) (kill-buffer buffer)))))
166
1b8cac5d 167(defun point-to-register (register &optional arg)
b42e6156 168 "Store current location of point in register REGISTER.
0cc89026 169With prefix argument, store current frame configuration.
b42e6156 170Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 171Argument is a character, naming the register."
85698d63
LL
172 (interactive (list (register-read-with-preview "Point to register: ")
173 current-prefix-arg))
ddbb3cc7
SM
174 ;; Turn the marker into a file-ref if the buffer is killed.
175 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 176 (set-register register
070c2506
KH
177 (if arg (list (current-frame-configuration) (point-marker))
178 (point-marker))))
efeae993 179
06b60517 180(defun window-configuration-to-register (register &optional _arg)
83b5d757
RS
181 "Store the window configuration of the selected frame in register REGISTER.
182Use \\[jump-to-register] to restore the configuration.
183Argument is a character, naming the register."
85698d63
LL
184 (interactive (list (register-read-with-preview
185 "Window configuration to register: ")
186 current-prefix-arg))
b4a91f43
KH
187 ;; current-window-configuration does not include the value
188 ;; of point in the current buffer, so record that separately.
070c2506 189 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 190
06b60517 191(defun frame-configuration-to-register (register &optional _arg)
83b5d757
RS
192 "Store the window configuration of all frames in register REGISTER.
193Use \\[jump-to-register] to restore the configuration.
194Argument is a character, naming the register."
85698d63
LL
195 (interactive (list (register-read-with-preview
196 "Frame configuration to register: ")
197 current-prefix-arg))
b4a91f43
KH
198 ;; current-frame-configuration does not include the value
199 ;; of point in the current buffer, so record that separately.
070c2506 200 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 201
31e1d920 202(defalias 'register-to-point 'jump-to-register)
1b8cac5d 203(defun jump-to-register (register &optional delete)
efeae993 204 "Move point to location stored in a register.
22073dda 205If the register contains a file name, find that file.
1a86cc81 206\(To put a file name in a register, you must use `set-register'.)
2805a651
JB
207If the register contains a window configuration (one frame) or a frameset
208\(all frames), restore that frame or all frames accordingly.
e7683fff 209First argument is a character, naming the register.
1542ad37 210Optional second arg non-nil (interactively, prefix argument) says to
2805a651 211delete any existing frames that the frameset doesn't mention.
a21d94f9 212\(Otherwise, these frames are iconified.)"
85698d63
LL
213 (interactive (list (register-read-with-preview "Jump to register: ")
214 current-prefix-arg))
1b8cac5d 215 (let ((val (get-register register)))
376a7584 216 (cond
6302e0d3 217 ((registerv-p val)
f58e0fd5 218 (cl-assert (registerv-jump-func val) nil
6302e0d3
LL
219 "Don't know how to jump to register %s"
220 (single-key-description register))
221 (funcall (registerv-jump-func val) (registerv-data val)))
b4a91f43
KH
222 ((and (consp val) (frame-configuration-p (car val)))
223 (set-frame-configuration (car val) (not delete))
224 (goto-char (cadr val)))
225 ((and (consp val) (window-configuration-p (car val)))
226 (set-window-configuration (car val))
227 (goto-char (cadr val)))
376a7584 228 ((markerp val)
8c4ca60c
KH
229 (or (marker-buffer val)
230 (error "That register's buffer no longer exists"))
376a7584
JB
231 (switch-to-buffer (marker-buffer val))
232 (goto-char val))
22073dda
RS
233 ((and (consp val) (eq (car val) 'file))
234 (find-file (cdr val)))
28cbd14d
RS
235 ((and (consp val) (eq (car val) 'file-query))
236 (or (find-buffer-visiting (nth 1 val))
237 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
238 (error "Register access aborted"))
239 (find-file (nth 1 val))
240 (goto-char (nth 2 val)))
376a7584
JB
241 (t
242 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 243
28cbd14d 244(defun register-swap-out ()
ddbb3cc7 245 "Turn markers into file-query references when a buffer is killed."
28cbd14d 246 (and buffer-file-name
ddbb3cc7
SM
247 (dolist (elem register-alist)
248 (and (markerp (cdr elem))
249 (eq (marker-buffer (cdr elem)) (current-buffer))
250 (setcdr elem
251 (list 'file-query
252 buffer-file-name
253 (marker-position (cdr elem))))))))
28cbd14d 254
0e07a458 255(defun number-to-register (number register)
070c2506
KH
256 "Store a number in a register.
257Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
258If NUMBER is nil, a decimal number is read from the buffer starting
259at point, and point moves to the end of that number.
070c2506 260Interactively, NUMBER is the prefix arg (none means nil)."
85698d63
LL
261 (interactive (list current-prefix-arg
262 (register-read-with-preview "Number to register: ")))
4f23d31c 263 (set-register register
0e07a458
KH
264 (if number
265 (prefix-numeric-value number)
4d2caa07
KH
266 (if (looking-at "\\s-*-?[0-9]+")
267 (progn
268 (goto-char (match-end 0))
fe22eed0 269 (string-to-number (match-string 0)))
070c2506
KH
270 0))))
271
0979429b
J
272(defun increment-register (prefix register)
273 "Augment contents of REGISTER.
274Interactively, PREFIX is in raw form.
275
276If REGISTER contains a number, add `prefix-numeric-value' of
277PREFIX to it.
278
279If REGISTER is empty or if it contains text, call
280`append-to-register' with `delete-flag' set to PREFIX."
281 (interactive "P\ncIncrement register: ")
282 (let ((register-val (get-register register)))
283 (cond
284 ((numberp register-val)
285 (let ((number (prefix-numeric-value prefix)))
286 (set-register register (+ number register-val))))
287 ((or (not register-val) (stringp register-val))
288 (append-to-register register (region-beginning) (region-end) prefix))
289 (t (error "Register does not contain a number or text")))))
efeae993 290
1b8cac5d 291(defun view-register (register)
efeae993 292 "Display what is contained in register named REGISTER.
1b8cac5d 293The Lisp value REGISTER is a character."
85698d63 294 (interactive (list (register-read-with-preview "View register: ")))
1b8cac5d 295 (let ((val (get-register register)))
efeae993 296 (if (null val)
1b8cac5d 297 (message "Register %s is empty" (single-key-description register))
efeae993 298 (with-output-to-temp-buffer "*Output*"
92840a31
RS
299 (describe-register-1 register t)))))
300
301(defun list-registers ()
302 "Display a list of nonempty registers saying briefly what they contain."
303 (interactive)
304 (let ((list (copy-sequence register-alist)))
305 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
306 (with-output-to-temp-buffer "*Output*"
307 (dolist (elt list)
308 (when (get-register (car elt))
309 (describe-register-1 (car elt))
310 (terpri))))))
311
312(defun describe-register-1 (register &optional verbose)
313 (princ "Register ")
314 (princ (single-key-description register))
315 (princ " contains ")
6ed21409
RS
316 (let ((val (get-register register)))
317 (cond
6302e0d3
LL
318 ((registerv-p val)
319 (if (registerv-print-func val)
320 (funcall (registerv-print-func val) (registerv-data val))
321 (princ "[UNPRINTABLE CONTENTS].")))
322
6ed21409
RS
323 ((numberp val)
324 (princ val))
325
326 ((markerp val)
327 (let ((buf (marker-buffer val)))
328 (if (null buf)
329 (princ "a marker in no buffer")
330 (princ "a buffer position:\n buffer ")
331 (princ (buffer-name buf))
332 (princ ", position ")
333 (princ (marker-position val)))))
334
335 ((and (consp val) (window-configuration-p (car val)))
336 (princ "a window configuration."))
337
338 ((and (consp val) (frame-configuration-p (car val)))
339 (princ "a frame configuration."))
340
341 ((and (consp val) (eq (car val) 'file))
342 (princ "the file ")
343 (prin1 (cdr val))
344 (princ "."))
345
346 ((and (consp val) (eq (car val) 'file-query))
347 (princ "a file-query reference:\n file ")
348 (prin1 (car (cdr val)))
349 (princ ",\n position ")
350 (princ (car (cdr (cdr val))))
351 (princ "."))
352
353 ((consp val)
354 (if verbose
355 (progn
356 (princ "the rectangle:\n")
357 (while val
358 (princ " ")
359 (princ (car val))
360 (terpri)
361 (setq val (cdr val))))
362 (princ "a rectangle starting with ")
363 (princ (car val))))
364
365 ((stringp val)
00a2b823 366 (setq val (copy-sequence val))
9c7cc04b
RS
367 (if (eq yank-excluded-properties t)
368 (set-text-properties 0 (length val) nil val)
369 (remove-list-of-text-properties 0 (length val)
370 yank-excluded-properties val))
6ed21409
RS
371 (if verbose
372 (progn
373 (princ "the text:\n")
374 (princ val))
f1180544 375 (cond
13d6f302
RS
376 ;; Extract first N characters starting with first non-whitespace.
377 ((string-match (format "[^ \t\n].\\{,%d\\}"
378 ;; Deduct 6 for the spaces inserted below.
379 (min 20 (max 0 (- (window-width) 6))))
380 val)
381 (princ "text starting with\n ")
382 (princ (match-string 0 val)))
383 ((string-match "^[ \t\n]+$" val)
384 (princ "whitespace"))
385 (t
386 (princ "the empty string")))))
6ed21409
RS
387 (t
388 (princ "Garbage:\n")
389 (if verbose (prin1 val))))))
efeae993 390
1b8cac5d
RS
391(defun insert-register (register &optional arg)
392 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
393Normally puts point before and mark after the inserted text.
394If optional second arg is non-nil, puts mark before and point after.
395Interactively, second arg is non-nil if prefix arg is supplied."
85698d63
LL
396 (interactive (progn
397 (barf-if-buffer-read-only)
5ea75d23
BG
398 (list (register-read-with-preview "Insert register: ")
399 current-prefix-arg)))
efeae993 400 (push-mark)
1b8cac5d 401 (let ((val (get-register register)))
cbd4993c 402 (cond
6302e0d3 403 ((registerv-p val)
f58e0fd5 404 (cl-assert (registerv-insert-func val) nil
6302e0d3
LL
405 "Don't know how to insert register %s"
406 (single-key-description register))
407 (funcall (registerv-insert-func val) (registerv-data val)))
2d43b8c9
LL
408 ((consp val)
409 (insert-rectangle val))
cbd4993c 410 ((stringp val)
e7c765c3 411 (insert-for-yank val))
070c2506 412 ((numberp val)
cbd4993c
KH
413 (princ val (current-buffer)))
414 ((and (markerp val) (marker-position val))
415 (princ (marker-position val) (current-buffer)))
416 (t
417 (error "Register does not contain text"))))
efeae993
RS
418 (if (not arg) (exchange-point-and-mark)))
419
00a2b823 420(defun copy-to-register (register start end &optional delete-flag region)
1a86cc81
JB
421 "Copy region into register REGISTER.
422With prefix arg, delete as well.
1b8cac5d 423Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
00a2b823
SM
424START and END are buffer positions indicating what to copy.
425The optional argument REGION if non-nil, indicates that we're not just copying
426some text between START and END, but we're copying the region."
85698d63
LL
427 (interactive (list (register-read-with-preview "Copy to register: ")
428 (region-beginning)
429 (region-end)
00a2b823
SM
430 current-prefix-arg
431 t))
432 (set-register register (if region
433 (funcall region-extract-function delete-flag)
434 (prog1 (filter-buffer-substring start end)
435 (if delete-flag (delete-region start end)))))
2549c068 436 (setq deactivate-mark t)
00a2b823 437 (cond (delete-flag)
2549c068
CY
438 ((called-interactively-p 'interactive)
439 (indicate-copied-region))))
efeae993 440
1b8cac5d
RS
441(defun append-to-register (register start end &optional delete-flag)
442 "Append region to text in register REGISTER.
443With prefix arg, delete as well.
444Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 445START and END are buffer positions indicating what to append."
85698d63
LL
446 (interactive (list (register-read-with-preview "Append to register: ")
447 (region-beginning)
448 (region-end)
449 current-prefix-arg))
c81f72ce 450 (let ((reg (get-register register))
0979429b 451 (text (filter-buffer-substring start end))
bfabf70a 452 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
453 (set-register
454 register (cond ((not reg) text)
0979429b 455 ((stringp reg) (concat reg separator text))
c81f72ce 456 (t (error "Register does not contain text")))))
5694896d 457 (setq deactivate-mark t)
2549c068
CY
458 (cond (delete-flag
459 (delete-region start end))
460 ((called-interactively-p 'interactive)
461 (indicate-copied-region))))
efeae993 462
1b8cac5d
RS
463(defun prepend-to-register (register start end &optional delete-flag)
464 "Prepend region to text in register REGISTER.
465With prefix arg, delete as well.
466Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 467START and END are buffer positions indicating what to prepend."
85698d63
LL
468 (interactive (list (register-read-with-preview "Prepend to register: ")
469 (region-beginning)
470 (region-end)
471 current-prefix-arg))
c81f72ce 472 (let ((reg (get-register register))
0979429b 473 (text (filter-buffer-substring start end))
bfabf70a 474 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
475 (set-register
476 register (cond ((not reg) text)
0979429b 477 ((stringp reg) (concat text separator reg))
c81f72ce 478 (t (error "Register does not contain text")))))
5694896d 479 (setq deactivate-mark t)
2549c068
CY
480 (cond (delete-flag
481 (delete-region start end))
482 ((called-interactively-p 'interactive)
483 (indicate-copied-region))))
efeae993 484
1b8cac5d
RS
485(defun copy-rectangle-to-register (register start end &optional delete-flag)
486 "Copy rectangular region into register REGISTER.
1a86cc81
JB
487With prefix arg, delete as well.
488To insert this register in the buffer, use \\[insert-register].
5ef5d6ce
RS
489
490Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 491START and END are buffer positions giving two corners of rectangle."
85698d63
LL
492 (interactive (list (register-read-with-preview
493 "Copy rectangle to register: ")
494 (region-beginning)
495 (region-end)
496 current-prefix-arg))
2549c068
CY
497 (let ((rectangle (if delete-flag
498 (delete-extract-rectangle start end)
499 (extract-rectangle start end))))
500 (set-register register rectangle)
501 (when (and (null delete-flag)
502 (called-interactively-p 'interactive))
503 (setq deactivate-mark t)
504 (indicate-copied-region (length (car rectangle))))))
505
0f214cdf 506(provide 'register)
c88ab9ce 507;;; register.el ends here