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