* indent/ruby.rb: Fix a factual error.
[bpt/emacs.git] / lisp / register.el
CommitLineData
55535639 1;;; register.el --- register commands for Emacs
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
1a86cc81
JB
92(defun get-register (register)
93 "Return contents of Emacs register named REGISTER, or nil if none."
94 (cdr (assq register register-alist)))
efeae993 95
1b8cac5d
RS
96(defun set-register (register value)
97 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
1a86cc81 98See the documentation of the variable `register-alist' for possible VALUEs."
1b8cac5d 99 (let ((aelt (assq register register-alist)))
efeae993
RS
100 (if aelt
101 (setcdr aelt value)
ddbb3cc7 102 (push (cons register value) register-alist))
efeae993
RS
103 value))
104
1b8cac5d 105(defun point-to-register (register &optional arg)
b42e6156 106 "Store current location of point in register REGISTER.
0cc89026 107With prefix argument, store current frame configuration.
b42e6156 108Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 109Argument is a character, naming the register."
b42e6156 110 (interactive "cPoint to register: \nP")
ddbb3cc7
SM
111 ;; Turn the marker into a file-ref if the buffer is killed.
112 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 113 (set-register register
070c2506
KH
114 (if arg (list (current-frame-configuration) (point-marker))
115 (point-marker))))
efeae993 116
06b60517 117(defun window-configuration-to-register (register &optional _arg)
83b5d757
RS
118 "Store the window configuration of the selected frame in register REGISTER.
119Use \\[jump-to-register] to restore the configuration.
120Argument is a character, naming the register."
5f517806 121 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
122 ;; current-window-configuration does not include the value
123 ;; of point in the current buffer, so record that separately.
070c2506 124 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 125
06b60517 126(defun frame-configuration-to-register (register &optional _arg)
83b5d757
RS
127 "Store the window configuration of all frames in register REGISTER.
128Use \\[jump-to-register] to restore the configuration.
129Argument is a character, naming the register."
5f517806 130 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
131 ;; current-frame-configuration does not include the value
132 ;; of point in the current buffer, so record that separately.
070c2506 133 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 134
31e1d920 135(defalias 'register-to-point 'jump-to-register)
1b8cac5d 136(defun jump-to-register (register &optional delete)
efeae993 137 "Move point to location stored in a register.
22073dda 138If the register contains a file name, find that file.
1a86cc81 139\(To put a file name in a register, you must use `set-register'.)
2805a651
JB
140If the register contains a window configuration (one frame) or a frameset
141\(all frames), restore that frame or all frames accordingly.
e7683fff 142First argument is a character, naming the register.
1542ad37 143Optional second arg non-nil (interactively, prefix argument) says to
2805a651 144delete any existing frames that the frameset doesn't mention.
a21d94f9 145\(Otherwise, these frames are iconified.)"
e7683fff 146 (interactive "cJump to register: \nP")
1b8cac5d 147 (let ((val (get-register register)))
376a7584 148 (cond
6302e0d3 149 ((registerv-p val)
f58e0fd5 150 (cl-assert (registerv-jump-func val) nil
6302e0d3
LL
151 "Don't know how to jump to register %s"
152 (single-key-description register))
153 (funcall (registerv-jump-func val) (registerv-data val)))
b4a91f43
KH
154 ((and (consp val) (frame-configuration-p (car val)))
155 (set-frame-configuration (car val) (not delete))
156 (goto-char (cadr val)))
157 ((and (consp val) (window-configuration-p (car val)))
158 (set-window-configuration (car val))
159 (goto-char (cadr val)))
376a7584 160 ((markerp val)
8c4ca60c
KH
161 (or (marker-buffer val)
162 (error "That register's buffer no longer exists"))
376a7584
JB
163 (switch-to-buffer (marker-buffer val))
164 (goto-char val))
22073dda
RS
165 ((and (consp val) (eq (car val) 'file))
166 (find-file (cdr val)))
28cbd14d
RS
167 ((and (consp val) (eq (car val) 'file-query))
168 (or (find-buffer-visiting (nth 1 val))
169 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
170 (error "Register access aborted"))
171 (find-file (nth 1 val))
172 (goto-char (nth 2 val)))
376a7584
JB
173 (t
174 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 175
28cbd14d 176(defun register-swap-out ()
ddbb3cc7 177 "Turn markers into file-query references when a buffer is killed."
28cbd14d 178 (and buffer-file-name
ddbb3cc7
SM
179 (dolist (elem register-alist)
180 (and (markerp (cdr elem))
181 (eq (marker-buffer (cdr elem)) (current-buffer))
182 (setcdr elem
183 (list 'file-query
184 buffer-file-name
185 (marker-position (cdr elem))))))))
28cbd14d 186
0e07a458 187(defun number-to-register (number register)
070c2506
KH
188 "Store a number in a register.
189Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
190If NUMBER is nil, a decimal number is read from the buffer starting
191at point, and point moves to the end of that number.
070c2506
KH
192Interactively, NUMBER is the prefix arg (none means nil)."
193 (interactive "P\ncNumber to register: ")
4f23d31c 194 (set-register register
0e07a458
KH
195 (if number
196 (prefix-numeric-value number)
4d2caa07
KH
197 (if (looking-at "\\s-*-?[0-9]+")
198 (progn
199 (goto-char (match-end 0))
fe22eed0 200 (string-to-number (match-string 0)))
070c2506
KH
201 0))))
202
0979429b
J
203(defun increment-register (prefix register)
204 "Augment contents of REGISTER.
205Interactively, PREFIX is in raw form.
206
207If REGISTER contains a number, add `prefix-numeric-value' of
208PREFIX to it.
209
210If REGISTER is empty or if it contains text, call
211`append-to-register' with `delete-flag' set to PREFIX."
212 (interactive "P\ncIncrement register: ")
213 (let ((register-val (get-register register)))
214 (cond
215 ((numberp register-val)
216 (let ((number (prefix-numeric-value prefix)))
217 (set-register register (+ number register-val))))
218 ((or (not register-val) (stringp register-val))
219 (append-to-register register (region-beginning) (region-end) prefix))
220 (t (error "Register does not contain a number or text")))))
efeae993 221
1b8cac5d 222(defun view-register (register)
efeae993 223 "Display what is contained in register named REGISTER.
1b8cac5d 224The Lisp value REGISTER is a character."
efeae993 225 (interactive "cView register: ")
1b8cac5d 226 (let ((val (get-register register)))
efeae993 227 (if (null val)
1b8cac5d 228 (message "Register %s is empty" (single-key-description register))
efeae993 229 (with-output-to-temp-buffer "*Output*"
92840a31
RS
230 (describe-register-1 register t)))))
231
232(defun list-registers ()
233 "Display a list of nonempty registers saying briefly what they contain."
234 (interactive)
235 (let ((list (copy-sequence register-alist)))
236 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
237 (with-output-to-temp-buffer "*Output*"
238 (dolist (elt list)
239 (when (get-register (car elt))
240 (describe-register-1 (car elt))
241 (terpri))))))
242
243(defun describe-register-1 (register &optional verbose)
244 (princ "Register ")
245 (princ (single-key-description register))
246 (princ " contains ")
6ed21409
RS
247 (let ((val (get-register register)))
248 (cond
6302e0d3
LL
249 ((registerv-p val)
250 (if (registerv-print-func val)
251 (funcall (registerv-print-func val) (registerv-data val))
252 (princ "[UNPRINTABLE CONTENTS].")))
253
6ed21409
RS
254 ((numberp val)
255 (princ val))
256
257 ((markerp val)
258 (let ((buf (marker-buffer val)))
259 (if (null buf)
260 (princ "a marker in no buffer")
261 (princ "a buffer position:\n buffer ")
262 (princ (buffer-name buf))
263 (princ ", position ")
264 (princ (marker-position val)))))
265
266 ((and (consp val) (window-configuration-p (car val)))
267 (princ "a window configuration."))
268
269 ((and (consp val) (frame-configuration-p (car val)))
270 (princ "a frame configuration."))
271
272 ((and (consp val) (eq (car val) 'file))
273 (princ "the file ")
274 (prin1 (cdr val))
275 (princ "."))
276
277 ((and (consp val) (eq (car val) 'file-query))
278 (princ "a file-query reference:\n file ")
279 (prin1 (car (cdr val)))
280 (princ ",\n position ")
281 (princ (car (cdr (cdr val))))
282 (princ "."))
283
284 ((consp val)
285 (if verbose
286 (progn
287 (princ "the rectangle:\n")
288 (while val
289 (princ " ")
290 (princ (car val))
291 (terpri)
292 (setq val (cdr val))))
293 (princ "a rectangle starting with ")
294 (princ (car val))))
295
296 ((stringp val)
9c7cc04b
RS
297 (if (eq yank-excluded-properties t)
298 (set-text-properties 0 (length val) nil val)
299 (remove-list-of-text-properties 0 (length val)
300 yank-excluded-properties val))
6ed21409
RS
301 (if verbose
302 (progn
303 (princ "the text:\n")
304 (princ val))
f1180544 305 (cond
13d6f302
RS
306 ;; Extract first N characters starting with first non-whitespace.
307 ((string-match (format "[^ \t\n].\\{,%d\\}"
308 ;; Deduct 6 for the spaces inserted below.
309 (min 20 (max 0 (- (window-width) 6))))
310 val)
311 (princ "text starting with\n ")
312 (princ (match-string 0 val)))
313 ((string-match "^[ \t\n]+$" val)
314 (princ "whitespace"))
315 (t
316 (princ "the empty string")))))
6ed21409
RS
317 (t
318 (princ "Garbage:\n")
319 (if verbose (prin1 val))))))
efeae993 320
1b8cac5d
RS
321(defun insert-register (register &optional arg)
322 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
323Normally puts point before and mark after the inserted text.
324If optional second arg is non-nil, puts mark before and point after.
325Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 326 (interactive "*cInsert register: \nP")
efeae993 327 (push-mark)
1b8cac5d 328 (let ((val (get-register register)))
cbd4993c 329 (cond
6302e0d3 330 ((registerv-p val)
f58e0fd5 331 (cl-assert (registerv-insert-func val) nil
6302e0d3
LL
332 "Don't know how to insert register %s"
333 (single-key-description register))
334 (funcall (registerv-insert-func val) (registerv-data val)))
2d43b8c9
LL
335 ((consp val)
336 (insert-rectangle val))
cbd4993c 337 ((stringp val)
e7c765c3 338 (insert-for-yank val))
070c2506 339 ((numberp val)
cbd4993c
KH
340 (princ val (current-buffer)))
341 ((and (markerp val) (marker-position val))
342 (princ (marker-position val) (current-buffer)))
343 (t
344 (error "Register does not contain text"))))
efeae993
RS
345 (if (not arg) (exchange-point-and-mark)))
346
1b8cac5d 347(defun copy-to-register (register start end &optional delete-flag)
1a86cc81
JB
348 "Copy region into register REGISTER.
349With prefix arg, delete as well.
1b8cac5d 350Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
351START and END are buffer positions indicating what to copy."
352 (interactive "cCopy to register: \nr\nP")
13191e32 353 (set-register register (filter-buffer-substring start end))
2549c068
CY
354 (setq deactivate-mark t)
355 (cond (delete-flag
356 (delete-region start end))
357 ((called-interactively-p 'interactive)
358 (indicate-copied-region))))
efeae993 359
1b8cac5d
RS
360(defun append-to-register (register start end &optional delete-flag)
361 "Append region to text in register REGISTER.
362With prefix arg, delete as well.
363Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
364START and END are buffer positions indicating what to append."
365 (interactive "cAppend to register: \nr\nP")
c81f72ce 366 (let ((reg (get-register register))
0979429b 367 (text (filter-buffer-substring start end))
bfabf70a 368 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
369 (set-register
370 register (cond ((not reg) text)
0979429b 371 ((stringp reg) (concat reg separator text))
c81f72ce 372 (t (error "Register does not contain text")))))
5694896d 373 (setq deactivate-mark t)
2549c068
CY
374 (cond (delete-flag
375 (delete-region start end))
376 ((called-interactively-p 'interactive)
377 (indicate-copied-region))))
efeae993 378
1b8cac5d
RS
379(defun prepend-to-register (register start end &optional delete-flag)
380 "Prepend region to text in register REGISTER.
381With prefix arg, delete as well.
382Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
383START and END are buffer positions indicating what to prepend."
384 (interactive "cPrepend to register: \nr\nP")
c81f72ce 385 (let ((reg (get-register register))
0979429b 386 (text (filter-buffer-substring start end))
bfabf70a 387 (separator (and register-separator (get-register register-separator))))
c81f72ce
TTN
388 (set-register
389 register (cond ((not reg) text)
0979429b 390 ((stringp reg) (concat text separator reg))
c81f72ce 391 (t (error "Register does not contain text")))))
5694896d 392 (setq deactivate-mark t)
2549c068
CY
393 (cond (delete-flag
394 (delete-region start end))
395 ((called-interactively-p 'interactive)
396 (indicate-copied-region))))
efeae993 397
1b8cac5d
RS
398(defun copy-rectangle-to-register (register start end &optional delete-flag)
399 "Copy rectangular region into register REGISTER.
1a86cc81
JB
400With prefix arg, delete as well.
401To insert this register in the buffer, use \\[insert-register].
5ef5d6ce
RS
402
403Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
404START and END are buffer positions giving two corners of rectangle."
405 (interactive "cCopy rectangle to register: \nr\nP")
2549c068
CY
406 (let ((rectangle (if delete-flag
407 (delete-extract-rectangle start end)
408 (extract-rectangle start end))))
409 (set-register register rectangle)
410 (when (and (null delete-flag)
411 (called-interactively-p 'interactive))
412 (setq deactivate-mark t)
413 (indicate-copied-region (length (car rectangle))))))
414
c88ab9ce 415
0f214cdf 416(provide 'register)
c88ab9ce 417;;; register.el ends here