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