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