* lisp/progmodes/ruby-mode.el (ruby-mode): Add `ruby-mode-set-encoding'
[bpt/emacs.git] / lisp / register.el
CommitLineData
85698d63 1;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
c88ab9ce 2
ab422c4d
PE
3;; Copyright (C) 1985, 1993-1994, 2001-2013 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
LL
92(defcustom register-preview-delay 1
93 "If non-nil delay in seconds to pop up the preview window."
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))
125 (let ((split-height-threshold 0))
85698d63
LL
126 (with-temp-buffer-window
127 buffer
128 (cons 'display-buffer-below-selected
129 '((window-height . fit-window-to-buffer)))
130 nil
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))))))
141
142(defun register-read-with-preview (prompt)
143 "Read an event with register preview using PROMPT.
144Pop up a register preview window if the input is a help char but
145is not a register. Alternatively if `register-preview-delay' is a
146number 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)))))
167
1b8cac5d 168(defun point-to-register (register &optional arg)
b42e6156 169 "Store current location of point in register REGISTER.
0cc89026 170With prefix argument, store current frame configuration.
b42e6156 171Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 172Argument is a character, naming the register."
85698d63
LL
173 (interactive (list (register-read-with-preview "Point to register: ")
174 current-prefix-arg))
ddbb3cc7
SM
175 ;; Turn the marker into a file-ref if the buffer is killed.
176 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 177 (set-register register
070c2506
KH
178 (if arg (list (current-frame-configuration) (point-marker))
179 (point-marker))))
efeae993 180
06b60517 181(defun window-configuration-to-register (register &optional _arg)
83b5d757
RS
182 "Store the window configuration of the selected frame in register REGISTER.
183Use \\[jump-to-register] to restore the configuration.
184Argument is a character, naming the register."
85698d63
LL
185 (interactive (list (register-read-with-preview
186 "Window configuration to register: ")
187 current-prefix-arg))
b4a91f43
KH
188 ;; current-window-configuration does not include the value
189 ;; of point in the current buffer, so record that separately.
070c2506 190 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 191
06b60517 192(defun frame-configuration-to-register (register &optional _arg)
83b5d757
RS
193 "Store the window configuration of all frames in register REGISTER.
194Use \\[jump-to-register] to restore the configuration.
195Argument is a character, naming the register."
85698d63
LL
196 (interactive (list (register-read-with-preview
197 "Frame configuration to register: ")
198 current-prefix-arg))
b4a91f43
KH
199 ;; current-frame-configuration does not include the value
200 ;; of point in the current buffer, so record that separately.
070c2506 201 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 202
31e1d920 203(defalias 'register-to-point 'jump-to-register)
1b8cac5d 204(defun jump-to-register (register &optional delete)
efeae993 205 "Move point to location stored in a register.
22073dda 206If the register contains a file name, find that file.
1a86cc81 207\(To put a file name in a register, you must use `set-register'.)
2805a651
JB
208If the register contains a window configuration (one frame) or a frameset
209\(all frames), restore that frame or all frames accordingly.
e7683fff 210First argument is a character, naming the register.
1542ad37 211Optional second arg non-nil (interactively, prefix argument) says to
2805a651 212delete any existing frames that the frameset doesn't mention.
a21d94f9 213\(Otherwise, these frames are iconified.)"
85698d63
LL
214 (interactive (list (register-read-with-preview "Jump to register: ")
215 current-prefix-arg))
1b8cac5d 216 (let ((val (get-register register)))
376a7584 217 (cond
6302e0d3 218 ((registerv-p val)
f58e0fd5 219 (cl-assert (registerv-jump-func val) nil
6302e0d3
LL
220 "Don't know how to jump to register %s"
221 (single-key-description register))
222 (funcall (registerv-jump-func val) (registerv-data val)))
b4a91f43
KH
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)))
376a7584 229 ((markerp val)
8c4ca60c
KH
230 (or (marker-buffer val)
231 (error "That register's buffer no longer exists"))
376a7584
JB
232 (switch-to-buffer (marker-buffer val))
233 (goto-char val))
22073dda
RS
234 ((and (consp val) (eq (car val) 'file))
235 (find-file (cdr val)))
28cbd14d
RS
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)))
376a7584
JB
242 (t
243 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 244
28cbd14d 245(defun register-swap-out ()
ddbb3cc7 246 "Turn markers into file-query references when a buffer is killed."
28cbd14d 247 (and buffer-file-name
ddbb3cc7
SM
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))))))))
28cbd14d 255
0e07a458 256(defun number-to-register (number register)
070c2506
KH
257 "Store a number in a register.
258Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
259If NUMBER is nil, a decimal number is read from the buffer starting
260at point, and point moves to the end of that number.
070c2506 261Interactively, NUMBER is the prefix arg (none means nil)."
85698d63
LL
262 (interactive (list current-prefix-arg
263 (register-read-with-preview "Number to register: ")))
4f23d31c 264 (set-register register
0e07a458
KH
265 (if number
266 (prefix-numeric-value number)
4d2caa07
KH
267 (if (looking-at "\\s-*-?[0-9]+")
268 (progn
269 (goto-char (match-end 0))
fe22eed0 270 (string-to-number (match-string 0)))
070c2506
KH
271 0))))
272
0979429b
J
273(defun increment-register (prefix register)
274 "Augment contents of REGISTER.
275Interactively, PREFIX is in raw form.
276
277If REGISTER contains a number, add `prefix-numeric-value' of
278PREFIX to it.
279
280If 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")))))
efeae993 291
1b8cac5d 292(defun view-register (register)
efeae993 293 "Display what is contained in register named REGISTER.
1b8cac5d 294The Lisp value REGISTER is a character."
85698d63 295 (interactive (list (register-read-with-preview "View register: ")))
1b8cac5d 296 (let ((val (get-register register)))
efeae993 297 (if (null val)
1b8cac5d 298 (message "Register %s is empty" (single-key-description register))
efeae993 299 (with-output-to-temp-buffer "*Output*"
92840a31
RS
300 (describe-register-1 register t)))))
301
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))))))
312
313(defun describe-register-1 (register &optional verbose)
314 (princ "Register ")
315 (princ (single-key-description register))
316 (princ " contains ")
6ed21409
RS
317 (let ((val (get-register register)))
318 (cond
6302e0d3
LL
319 ((registerv-p val)
320 (if (registerv-print-func val)
321 (funcall (registerv-print-func val) (registerv-data val))
322 (princ "[UNPRINTABLE CONTENTS].")))
323
6ed21409
RS
324 ((numberp val)
325 (princ val))
326
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)))))
335
336 ((and (consp val) (window-configuration-p (car val)))
337 (princ "a window configuration."))
338
339 ((and (consp val) (frame-configuration-p (car val)))
340 (princ "a frame configuration."))
341
342 ((and (consp val) (eq (car val) 'file))
343 (princ "the file ")
344 (prin1 (cdr val))
345 (princ "."))
346
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 "."))
353
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))))
365
366 ((stringp 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
1b8cac5d 420(defun copy-to-register (register start end &optional delete-flag)
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.
efeae993 424START and END are buffer positions indicating what to copy."
85698d63
LL
425 (interactive (list (register-read-with-preview "Copy to register: ")
426 (region-beginning)
427 (region-end)
428 current-prefix-arg))
13191e32 429 (set-register register (filter-buffer-substring start end))
2549c068
CY
430 (setq deactivate-mark t)
431 (cond (delete-flag
432 (delete-region start end))
433 ((called-interactively-p 'interactive)
434 (indicate-copied-region))))
efeae993 435
1b8cac5d
RS
436(defun append-to-register (register start end &optional delete-flag)
437 "Append region to text in register REGISTER.
438With prefix arg, delete as well.
439Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 440START and END are buffer positions indicating what to append."
85698d63
LL
441 (interactive (list (register-read-with-preview "Append to register: ")
442 (region-beginning)
443 (region-end)
444 current-prefix-arg))
c81f72ce 445 (let ((reg (get-register register))
0979429b 446 (text (filter-buffer-substring start end))
bfabf70a 447 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
448 (set-register
449 register (cond ((not reg) text)
0979429b 450 ((stringp reg) (concat reg separator text))
c81f72ce 451 (t (error "Register does not contain text")))))
5694896d 452 (setq deactivate-mark t)
2549c068
CY
453 (cond (delete-flag
454 (delete-region start end))
455 ((called-interactively-p 'interactive)
456 (indicate-copied-region))))
efeae993 457
1b8cac5d
RS
458(defun prepend-to-register (register start end &optional delete-flag)
459 "Prepend region to text in register REGISTER.
460With prefix arg, delete as well.
461Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 462START and END are buffer positions indicating what to prepend."
85698d63
LL
463 (interactive (list (register-read-with-preview "Prepend to register: ")
464 (region-beginning)
465 (region-end)
466 current-prefix-arg))
c81f72ce 467 (let ((reg (get-register register))
0979429b 468 (text (filter-buffer-substring start end))
bfabf70a 469 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
470 (set-register
471 register (cond ((not reg) text)
0979429b 472 ((stringp reg) (concat text separator reg))
c81f72ce 473 (t (error "Register does not contain text")))))
5694896d 474 (setq deactivate-mark t)
2549c068
CY
475 (cond (delete-flag
476 (delete-region start end))
477 ((called-interactively-p 'interactive)
478 (indicate-copied-region))))
efeae993 479
1b8cac5d
RS
480(defun copy-rectangle-to-register (register start end &optional delete-flag)
481 "Copy rectangular region into register REGISTER.
1a86cc81
JB
482With prefix arg, delete as well.
483To insert this register in the buffer, use \\[insert-register].
5ef5d6ce
RS
484
485Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993 486START and END are buffer positions giving two corners of rectangle."
85698d63
LL
487 (interactive (list (register-read-with-preview
488 "Copy rectangle to register: ")
489 (region-beginning)
490 (region-end)
491 current-prefix-arg))
2549c068
CY
492 (let ((rectangle (if delete-flag
493 (delete-extract-rectangle start end)
494 (extract-rectangle start end))))
495 (set-register register rectangle)
496 (when (and (null delete-flag)
497 (called-interactively-p 'interactive))
498 (setq deactivate-mark t)
499 (indicate-copied-region (length (car rectangle))))))
500
0f214cdf 501(provide 'register)
c88ab9ce 502;;; register.el ends here