Attempt to solve bug #16615 by using a more accurate function prototype.
[bpt/emacs.git] / lisp / register.el
CommitLineData
85698d63 1;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
c88ab9ce 2
ba318903
PE
3;; Copyright (C) 1985, 1993-1994, 2001-2014 Free Software Foundation,
4;; Inc.
9750e079 5
4821e2af 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: internal
bd78fa1d 8;; Package: emacs
4821e2af 9
efeae993
RS
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
efeae993 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
efeae993
RS
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
efeae993 24
d9ecc911
ER
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
f58e0fd5 32(eval-when-compile (require 'cl-lib))
6302e0d3 33
4821e2af 34;;; Code:
efeae993 35
f58e0fd5 36(cl-defstruct
6302e0d3
LL
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
f58e0fd5 48(cl-defun registerv-make (data &key print-func jump-func insert-func)
6302e0d3
LL
49 "Create a register value object.
50
51DATA can be any value.
52PRINT-FUNC if provided controls how `list-registers' and
53`view-register' print the register. It should be a function
9173deec 54receiving one argument DATA and print text that completes
6302e0d3
LL
55this sentence:
56 Register X contains [TEXT PRINTED BY PRINT-FUNC]
57JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
58INSERT-FUNC if provided, controls how `insert-register' insert the register.
59They both receive DATA as argument."
60 (registerv--make data print-func jump-func insert-func))
61
efeae993
RS
62(defvar register-alist nil
63 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
6302e0d3
LL
64NAME is a character (a number). CONTENTS is a string, number, marker, list
65or a struct returned by `registerv-make'.
22073dda 66A list of strings represents a rectangle.
5858bcc4
CY
67A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
68A 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.
070c2506
KH
71A list of the form (WINDOW-CONFIGURATION POSITION)
72 represents a saved window configuration plus a saved value of point.
73A list of the form (FRAME-CONFIGURATION POSITION)
77187e6f 74 represents a saved frame configuration plus a saved value of point.")
efeae993 75
0979429b
J
76(defgroup register nil
77 "Register commands."
78 :group 'convenience
bfabf70a 79 :version "24.3")
0979429b 80
bfabf70a
AS
81(defcustom register-separator nil
82 "Register containing the text to put between collected texts, or nil if none.
0979429b
J
83
84When collecting text with
85`append-to-register' (resp. `prepend-to-register') contents of
86this register is added to the beginning (resp. end) of the marked
87text."
88 :group 'register
89 :type '(choice (const :tag "None" nil)
90 (character :tag "Use register" :value ?+)))
91
85698d63 92(defcustom register-preview-delay 1
da942af1
GM
93 "If non-nil, time to wait in seconds before popping up a preview window.
94If nil, do not show register previews, unless `help-char' (or a member of
95`help-event-list') is pressed."
bb098075 96 :version "24.4"
da942af1 97 :type '(choice number (const :tag "No preview unless requested" nil))
85698d63
LL
98 :group 'register)
99
1a86cc81
JB
100(defun get-register (register)
101 "Return contents of Emacs register named REGISTER, or nil if none."
102 (cdr (assq register register-alist)))
efeae993 103
1b8cac5d
RS
104(defun set-register (register value)
105 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
1a86cc81 106See the documentation of the variable `register-alist' for possible VALUEs."
1b8cac5d 107 (let ((aelt (assq register register-alist)))
efeae993
RS
108 (if aelt
109 (setcdr aelt value)
ddbb3cc7 110 (push (cons register value) register-alist))
efeae993
RS
111 value))
112
85698d63
LL
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.
126If SHOW-EMPTY is non-nil show the window even if no registers."
127 (when (or show-empty (consp register-alist))
cf2b7efc
MR
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)))))
85698d63
LL
143
144(defun register-read-with-preview (prompt)
da942af1
GM
145 "Read and return an event, prompting with PROMPT, possibly showing a preview.
146If `register-alist' and `register-preview-delay' are both non-nil,
147display a window listing registers after `register-preview-delay' seconds.
148If `help-char' (or a member of `help-event-list') is pressed, display
149such a window regardless."
85698d63
LL
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
1b8cac5d 171(defun point-to-register (register &optional arg)
b42e6156 172 "Store current location of point in register REGISTER.
0cc89026 173With prefix argument, store current frame configuration.
b42e6156 174Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 175Argument is a character, naming the register."
85698d63
LL
176 (interactive (list (register-read-with-preview "Point to register: ")
177 current-prefix-arg))
ddbb3cc7
SM
178 ;; Turn the marker into a file-ref if the buffer is killed.
179 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 180 (set-register register
070c2506
KH
181 (if arg (list (current-frame-configuration) (point-marker))
182 (point-marker))))
efeae993 183
06b60517 184(defun window-configuration-to-register (register &optional _arg)
83b5d757
RS
185 "Store the window configuration of the selected frame in register REGISTER.
186Use \\[jump-to-register] to restore the configuration.
187Argument is a character, naming the register."
85698d63
LL
188 (interactive (list (register-read-with-preview
189 "Window configuration to register: ")
190 current-prefix-arg))
b4a91f43
KH
191 ;; current-window-configuration does not include the value
192 ;; of point in the current buffer, so record that separately.
070c2506 193 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 194
06b60517 195(defun frame-configuration-to-register (register &optional _arg)
83b5d757
RS
196 "Store the window configuration of all frames in register REGISTER.
197Use \\[jump-to-register] to restore the configuration.
198Argument is a character, naming the register."
85698d63
LL
199 (interactive (list (register-read-with-preview
200 "Frame configuration to register: ")
201 current-prefix-arg))
b4a91f43
KH
202 ;; current-frame-configuration does not include the value
203 ;; of point in the current buffer, so record that separately.
070c2506 204 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 205
31e1d920 206(defalias 'register-to-point 'jump-to-register)
1b8cac5d 207(defun jump-to-register (register &optional delete)
efeae993 208 "Move point to location stored in a register.
22073dda 209If the register contains a file name, find that file.
1a86cc81 210\(To put a file name in a register, you must use `set-register'.)
2805a651
JB
211If the register contains a window configuration (one frame) or a frameset
212\(all frames), restore that frame or all frames accordingly.
e7683fff 213First argument is a character, naming the register.
1542ad37 214Optional second arg non-nil (interactively, prefix argument) says to
2805a651 215delete any existing frames that the frameset doesn't mention.
a21d94f9 216\(Otherwise, these frames are iconified.)"
85698d63
LL
217 (interactive (list (register-read-with-preview "Jump to register: ")
218 current-prefix-arg))
1b8cac5d 219 (let ((val (get-register register)))
376a7584 220 (cond
6302e0d3 221 ((registerv-p val)
f58e0fd5 222 (cl-assert (registerv-jump-func val) nil
6302e0d3
LL
223 "Don't know how to jump to register %s"
224 (single-key-description register))
225 (funcall (registerv-jump-func val) (registerv-data val)))
b4a91f43
KH
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)))
376a7584 232 ((markerp val)
8c4ca60c
KH
233 (or (marker-buffer val)
234 (error "That register's buffer no longer exists"))
376a7584
JB
235 (switch-to-buffer (marker-buffer val))
236 (goto-char val))
22073dda
RS
237 ((and (consp val) (eq (car val) 'file))
238 (find-file (cdr val)))
28cbd14d
RS
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)))
376a7584
JB
245 (t
246 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 247
28cbd14d 248(defun register-swap-out ()
ddbb3cc7 249 "Turn markers into file-query references when a buffer is killed."
28cbd14d 250 (and buffer-file-name
ddbb3cc7
SM
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))))))))
28cbd14d 258
0e07a458 259(defun number-to-register (number register)
070c2506
KH
260 "Store a number in a register.
261Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
262If NUMBER is nil, a decimal number is read from the buffer starting
263at point, and point moves to the end of that number.
070c2506 264Interactively, NUMBER is the prefix arg (none means nil)."
85698d63
LL
265 (interactive (list current-prefix-arg
266 (register-read-with-preview "Number to register: ")))
4f23d31c 267 (set-register register
0e07a458
KH
268 (if number
269 (prefix-numeric-value number)
4d2caa07
KH
270 (if (looking-at "\\s-*-?[0-9]+")
271 (progn
272 (goto-char (match-end 0))
fe22eed0 273 (string-to-number (match-string 0)))
070c2506
KH
274 0))))
275
0979429b
J
276(defun increment-register (prefix register)
277 "Augment contents of REGISTER.
278Interactively, PREFIX is in raw form.
279
280If REGISTER contains a number, add `prefix-numeric-value' of
281PREFIX to it.
282
283If 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")))))
efeae993 294
1b8cac5d 295(defun view-register (register)
efeae993 296 "Display what is contained in register named REGISTER.
1b8cac5d 297The Lisp value REGISTER is a character."
85698d63 298 (interactive (list (register-read-with-preview "View register: ")))
1b8cac5d 299 (let ((val (get-register register)))
efeae993 300 (if (null val)
1b8cac5d 301 (message "Register %s is empty" (single-key-description register))
efeae993 302 (with-output-to-temp-buffer "*Output*"
92840a31
RS
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 ")
6ed21409
RS
320 (let ((val (get-register register)))
321 (cond
6302e0d3
LL
322 ((registerv-p val)
323 (if (registerv-print-func val)
324 (funcall (registerv-print-func val) (registerv-data val))
325 (princ "[UNPRINTABLE CONTENTS].")))
326
6ed21409
RS
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)
00a2b823 370 (setq val (copy-sequence val))
9c7cc04b
RS
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))
6ed21409
RS
375 (if verbose
376 (progn
377 (princ "the text:\n")
378 (princ val))
f1180544 379 (cond
13d6f302
RS
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")))))
6ed21409
RS
391 (t
392 (princ "Garbage:\n")
393 (if verbose (prin1 val))))))
efeae993 394
1b8cac5d
RS
395(defun insert-register (register &optional arg)
396 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
397Normally puts point before and mark after the inserted text.
398If optional second arg is non-nil, puts mark before and point after.
399Interactively, second arg is non-nil if prefix arg is supplied."
85698d63
LL
400 (interactive (progn
401 (barf-if-buffer-read-only)
5ea75d23
BG
402 (list (register-read-with-preview "Insert register: ")
403 current-prefix-arg)))
efeae993 404 (push-mark)
1b8cac5d 405 (let ((val (get-register register)))
cbd4993c 406 (cond
6302e0d3 407 ((registerv-p val)
f58e0fd5 408 (cl-assert (registerv-insert-func val) nil
6302e0d3
LL
409 "Don't know how to insert register %s"
410 (single-key-description register))
411 (funcall (registerv-insert-func val) (registerv-data val)))
2d43b8c9
LL
412 ((consp val)
413 (insert-rectangle val))
cbd4993c 414 ((stringp val)
e7c765c3 415 (insert-for-yank val))
070c2506 416 ((numberp val)
cbd4993c
KH
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"))))
efeae993
RS
422 (if (not arg) (exchange-point-and-mark)))
423
00a2b823 424(defun copy-to-register (register start end &optional delete-flag region)
1a86cc81
JB
425 "Copy region into register REGISTER.
426With prefix arg, delete as well.
1b8cac5d 427Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
00a2b823
SM
428START and END are buffer positions indicating what to copy.
429The optional argument REGION if non-nil, indicates that we're not just copying
430some text between START and END, but we're copying the region."
85698d63
LL
431 (interactive (list (register-read-with-preview "Copy to register: ")
432 (region-beginning)
433 (region-end)
00a2b823
SM
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)))))
2549c068 440 (setq deactivate-mark t)
00a2b823 441 (cond (delete-flag)
2549c068
CY
442 ((called-interactively-p 'interactive)
443 (indicate-copied-region))))
efeae993 444
1b8cac5d
RS
445(defun append-to-register (register start end &optional delete-flag)
446 "Append region to text in register REGISTER.
447With prefix arg, delete as well.
448Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 449START and END are buffer positions indicating what to append."
85698d63
LL
450 (interactive (list (register-read-with-preview "Append to register: ")
451 (region-beginning)
452 (region-end)
453 current-prefix-arg))
c81f72ce 454 (let ((reg (get-register register))
0979429b 455 (text (filter-buffer-substring start end))
bfabf70a 456 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
457 (set-register
458 register (cond ((not reg) text)
0979429b 459 ((stringp reg) (concat reg separator text))
c81f72ce 460 (t (error "Register does not contain text")))))
5694896d 461 (setq deactivate-mark t)
2549c068
CY
462 (cond (delete-flag
463 (delete-region start end))
464 ((called-interactively-p 'interactive)
465 (indicate-copied-region))))
efeae993 466
1b8cac5d
RS
467(defun prepend-to-register (register start end &optional delete-flag)
468 "Prepend region to text in register REGISTER.
469With prefix arg, delete as well.
470Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 471START and END are buffer positions indicating what to prepend."
85698d63
LL
472 (interactive (list (register-read-with-preview "Prepend to register: ")
473 (region-beginning)
474 (region-end)
475 current-prefix-arg))
c81f72ce 476 (let ((reg (get-register register))
0979429b 477 (text (filter-buffer-substring start end))
bfabf70a 478 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
479 (set-register
480 register (cond ((not reg) text)
0979429b 481 ((stringp reg) (concat text separator reg))
c81f72ce 482 (t (error "Register does not contain text")))))
5694896d 483 (setq deactivate-mark t)
2549c068
CY
484 (cond (delete-flag
485 (delete-region start end))
486 ((called-interactively-p 'interactive)
487 (indicate-copied-region))))
efeae993 488
1b8cac5d
RS
489(defun copy-rectangle-to-register (register start end &optional delete-flag)
490 "Copy rectangular region into register REGISTER.
1a86cc81
JB
491With prefix arg, delete as well.
492To insert this register in the buffer, use \\[insert-register].
5ef5d6ce
RS
493
494Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 495START and END are buffer positions giving two corners of rectangle."
85698d63
LL
496 (interactive (list (register-read-with-preview
497 "Copy rectangle to register: ")
498 (region-beginning)
499 (region-end)
500 current-prefix-arg))
2549c068
CY
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
0f214cdf 510(provide 'register)
c88ab9ce 511;;; register.el ends here