Fix typos.
[bpt/emacs.git] / lisp / register.el
CommitLineData
55535639 1;;; register.el --- register commands for Emacs
c88ab9ce 2
c90f2757 3;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
9750e079 5
4821e2af 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: internal
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
8daffab7
JL
31;;; Global key bindings
32
33;;;###autoload (define-key ctl-x-r-map "\C-@" 'point-to-register)
34;;;###autoload (define-key ctl-x-r-map [?\C-\ ] 'point-to-register)
35;;;###autoload (define-key ctl-x-r-map " " 'point-to-register)
36;;;###autoload (define-key ctl-x-r-map "j" 'jump-to-register)
37;;;###autoload (define-key ctl-x-r-map "s" 'copy-to-register)
38;;;###autoload (define-key ctl-x-r-map "x" 'copy-to-register)
39;;;###autoload (define-key ctl-x-r-map "i" 'insert-register)
40;;;###autoload (define-key ctl-x-r-map "g" 'insert-register)
41;;;###autoload (define-key ctl-x-r-map "r" 'copy-rectangle-to-register)
42;;;###autoload (define-key ctl-x-r-map "n" 'number-to-register)
43;;;###autoload (define-key ctl-x-r-map "+" 'increment-register)
44;;;###autoload (define-key ctl-x-r-map "w" 'window-configuration-to-register)
45;;;###autoload (define-key ctl-x-r-map "f" 'frame-configuration-to-register)
46
4821e2af 47;;; Code:
efeae993
RS
48
49(defvar register-alist nil
50 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
070c2506 51NAME is a character (a number). CONTENTS is a string, number, marker or list.
22073dda 52A list of strings represents a rectangle.
28cbd14d
RS
53A list of the form (file . NAME) represents the file named NAME.
54A list of the form (file-query NAME POSITION) represents position POSITION
070c2506
KH
55 in the file named NAME, but query before visiting it.
56A list of the form (WINDOW-CONFIGURATION POSITION)
57 represents a saved window configuration plus a saved value of point.
58A list of the form (FRAME-CONFIGURATION POSITION)
59 represents a saved frame configuration plus a saved value of point.")
efeae993 60
1b8cac5d
RS
61(defun get-register (reg)
62 "Return contents of Emacs register named REG, or nil if none."
63 (cdr (assq reg register-alist)))
efeae993 64
1b8cac5d
RS
65(defun set-register (register value)
66 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
22073dda 67See the documentation of the variable `register-alist' for possible VALUE."
1b8cac5d 68 (let ((aelt (assq register register-alist)))
efeae993
RS
69 (if aelt
70 (setcdr aelt value)
ddbb3cc7 71 (push (cons register value) register-alist))
efeae993
RS
72 value))
73
1b8cac5d 74(defun point-to-register (register &optional arg)
b42e6156 75 "Store current location of point in register REGISTER.
0cc89026 76With prefix argument, store current frame configuration.
b42e6156 77Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 78Argument is a character, naming the register."
b42e6156 79 (interactive "cPoint to register: \nP")
ddbb3cc7
SM
80 ;; Turn the marker into a file-ref if the buffer is killed.
81 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 82 (set-register register
070c2506
KH
83 (if arg (list (current-frame-configuration) (point-marker))
84 (point-marker))))
efeae993 85
1b8cac5d 86(defun window-configuration-to-register (register &optional arg)
83b5d757
RS
87 "Store the window configuration of the selected frame in register REGISTER.
88Use \\[jump-to-register] to restore the configuration.
89Argument is a character, naming the register."
5f517806 90 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
91 ;; current-window-configuration does not include the value
92 ;; of point in the current buffer, so record that separately.
070c2506 93 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 94
1b8cac5d 95(defun frame-configuration-to-register (register &optional arg)
83b5d757
RS
96 "Store the window configuration of all frames in register REGISTER.
97Use \\[jump-to-register] to restore the configuration.
98Argument is a character, naming the register."
5f517806 99 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
100 ;; current-frame-configuration does not include the value
101 ;; of point in the current buffer, so record that separately.
070c2506 102 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 103
31e1d920 104(defalias 'register-to-point 'jump-to-register)
1b8cac5d 105(defun jump-to-register (register &optional delete)
efeae993 106 "Move point to location stored in a register.
22073dda
RS
107If the register contains a file name, find that file.
108 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
109If the register contains a window configuration (one frame) or a frame
110configuration (all frames), restore that frame or all frames accordingly.
e7683fff 111First argument is a character, naming the register.
1542ad37
RS
112Optional second arg non-nil (interactively, prefix argument) says to
113delete any existing frames that the frame configuration doesn't mention.
a21d94f9 114\(Otherwise, these frames are iconified.)"
e7683fff 115 (interactive "cJump to register: \nP")
1b8cac5d 116 (let ((val (get-register register)))
376a7584 117 (cond
b4a91f43
KH
118 ((and (consp val) (frame-configuration-p (car val)))
119 (set-frame-configuration (car val) (not delete))
120 (goto-char (cadr val)))
121 ((and (consp val) (window-configuration-p (car val)))
122 (set-window-configuration (car val))
123 (goto-char (cadr val)))
376a7584 124 ((markerp val)
8c4ca60c
KH
125 (or (marker-buffer val)
126 (error "That register's buffer no longer exists"))
376a7584
JB
127 (switch-to-buffer (marker-buffer val))
128 (goto-char val))
22073dda
RS
129 ((and (consp val) (eq (car val) 'file))
130 (find-file (cdr val)))
28cbd14d
RS
131 ((and (consp val) (eq (car val) 'file-query))
132 (or (find-buffer-visiting (nth 1 val))
133 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
134 (error "Register access aborted"))
135 (find-file (nth 1 val))
136 (goto-char (nth 2 val)))
376a7584
JB
137 (t
138 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 139
28cbd14d 140(defun register-swap-out ()
ddbb3cc7 141 "Turn markers into file-query references when a buffer is killed."
28cbd14d 142 (and buffer-file-name
ddbb3cc7
SM
143 (dolist (elem register-alist)
144 (and (markerp (cdr elem))
145 (eq (marker-buffer (cdr elem)) (current-buffer))
146 (setcdr elem
147 (list 'file-query
148 buffer-file-name
149 (marker-position (cdr elem))))))))
28cbd14d 150
0e07a458 151(defun number-to-register (number register)
070c2506
KH
152 "Store a number in a register.
153Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
154If NUMBER is nil, a decimal number is read from the buffer starting
155at point, and point moves to the end of that number.
070c2506
KH
156Interactively, NUMBER is the prefix arg (none means nil)."
157 (interactive "P\ncNumber to register: ")
4f23d31c 158 (set-register register
0e07a458
KH
159 (if number
160 (prefix-numeric-value number)
4d2caa07
KH
161 (if (looking-at "\\s-*-?[0-9]+")
162 (progn
163 (goto-char (match-end 0))
fe22eed0 164 (string-to-number (match-string 0)))
070c2506
KH
165 0))))
166
0e07a458 167(defun increment-register (number register)
070c2506 168 "Add NUMBER to the contents of register REGISTER.
0e07a458 169Interactively, NUMBER is the prefix arg."
070c2506 170 (interactive "p\ncIncrement register: ")
0e07a458 171 (or (numberp (get-register register))
070c2506 172 (error "Register does not contain a number"))
0e07a458 173 (set-register register (+ number (get-register register))))
efeae993 174
1b8cac5d 175(defun view-register (register)
efeae993 176 "Display what is contained in register named REGISTER.
1b8cac5d 177The Lisp value REGISTER is a character."
efeae993 178 (interactive "cView register: ")
1b8cac5d 179 (let ((val (get-register register)))
efeae993 180 (if (null val)
1b8cac5d 181 (message "Register %s is empty" (single-key-description register))
efeae993 182 (with-output-to-temp-buffer "*Output*"
92840a31
RS
183 (describe-register-1 register t)))))
184
185(defun list-registers ()
186 "Display a list of nonempty registers saying briefly what they contain."
187 (interactive)
188 (let ((list (copy-sequence register-alist)))
189 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
190 (with-output-to-temp-buffer "*Output*"
191 (dolist (elt list)
192 (when (get-register (car elt))
193 (describe-register-1 (car elt))
194 (terpri))))))
195
196(defun describe-register-1 (register &optional verbose)
197 (princ "Register ")
198 (princ (single-key-description register))
199 (princ " contains ")
6ed21409
RS
200 (let ((val (get-register register)))
201 (cond
202 ((numberp val)
203 (princ val))
204
205 ((markerp val)
206 (let ((buf (marker-buffer val)))
207 (if (null buf)
208 (princ "a marker in no buffer")
209 (princ "a buffer position:\n buffer ")
210 (princ (buffer-name buf))
211 (princ ", position ")
212 (princ (marker-position val)))))
213
214 ((and (consp val) (window-configuration-p (car val)))
215 (princ "a window configuration."))
216
217 ((and (consp val) (frame-configuration-p (car val)))
218 (princ "a frame configuration."))
219
220 ((and (consp val) (eq (car val) 'file))
221 (princ "the file ")
222 (prin1 (cdr val))
223 (princ "."))
224
225 ((and (consp val) (eq (car val) 'file-query))
226 (princ "a file-query reference:\n file ")
227 (prin1 (car (cdr val)))
228 (princ ",\n position ")
229 (princ (car (cdr (cdr val))))
230 (princ "."))
231
232 ((consp val)
233 (if verbose
234 (progn
235 (princ "the rectangle:\n")
236 (while val
237 (princ " ")
238 (princ (car val))
239 (terpri)
240 (setq val (cdr val))))
241 (princ "a rectangle starting with ")
242 (princ (car val))))
243
244 ((stringp val)
9c7cc04b
RS
245 (if (eq yank-excluded-properties t)
246 (set-text-properties 0 (length val) nil val)
247 (remove-list-of-text-properties 0 (length val)
248 yank-excluded-properties val))
6ed21409
RS
249 (if verbose
250 (progn
251 (princ "the text:\n")
252 (princ val))
f1180544 253 (cond
13d6f302
RS
254 ;; Extract first N characters starting with first non-whitespace.
255 ((string-match (format "[^ \t\n].\\{,%d\\}"
256 ;; Deduct 6 for the spaces inserted below.
257 (min 20 (max 0 (- (window-width) 6))))
258 val)
259 (princ "text starting with\n ")
260 (princ (match-string 0 val)))
261 ((string-match "^[ \t\n]+$" val)
262 (princ "whitespace"))
263 (t
264 (princ "the empty string")))))
6ed21409
RS
265 (t
266 (princ "Garbage:\n")
267 (if verbose (prin1 val))))))
efeae993 268
1b8cac5d
RS
269(defun insert-register (register &optional arg)
270 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
271Normally puts point before and mark after the inserted text.
272If optional second arg is non-nil, puts mark before and point after.
273Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 274 (interactive "*cInsert register: \nP")
efeae993 275 (push-mark)
1b8cac5d 276 (let ((val (get-register register)))
cbd4993c
KH
277 (cond
278 ((consp val)
279 (insert-rectangle val))
280 ((stringp val)
e7c765c3 281 (insert-for-yank val))
070c2506 282 ((numberp val)
cbd4993c
KH
283 (princ val (current-buffer)))
284 ((and (markerp val) (marker-position val))
285 (princ (marker-position val) (current-buffer)))
286 (t
287 (error "Register does not contain text"))))
efeae993
RS
288 (if (not arg) (exchange-point-and-mark)))
289
1b8cac5d
RS
290(defun copy-to-register (register start end &optional delete-flag)
291 "Copy region into register REGISTER. With prefix arg, delete as well.
292Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
293START and END are buffer positions indicating what to copy."
294 (interactive "cCopy to register: \nr\nP")
13191e32 295 (set-register register (filter-buffer-substring start end))
efeae993
RS
296 (if delete-flag (delete-region start end)))
297
1b8cac5d
RS
298(defun append-to-register (register start end &optional delete-flag)
299 "Append region to text in register REGISTER.
300With prefix arg, delete as well.
301Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
302START and END are buffer positions indicating what to append."
303 (interactive "cAppend to register: \nr\nP")
c81f72ce
TTN
304 (let ((reg (get-register register))
305 (text (filter-buffer-substring start end)))
306 (set-register
307 register (cond ((not reg) text)
308 ((stringp reg) (concat reg text))
309 (t (error "Register does not contain text")))))
efeae993
RS
310 (if delete-flag (delete-region start end)))
311
1b8cac5d
RS
312(defun prepend-to-register (register start end &optional delete-flag)
313 "Prepend region to text in register REGISTER.
314With prefix arg, delete as well.
315Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
316START and END are buffer positions indicating what to prepend."
317 (interactive "cPrepend to register: \nr\nP")
c81f72ce
TTN
318 (let ((reg (get-register register))
319 (text (filter-buffer-substring start end)))
320 (set-register
321 register (cond ((not reg) text)
322 ((stringp reg) (concat text reg))
323 (t (error "Register does not contain text")))))
efeae993
RS
324 (if delete-flag (delete-region start end)))
325
1b8cac5d
RS
326(defun copy-rectangle-to-register (register start end &optional delete-flag)
327 "Copy rectangular region into register REGISTER.
5ef5d6ce
RS
328With prefix arg, delete as well. To insert this register
329in the buffer, use \\[insert-register].
330
331Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
332START and END are buffer positions giving two corners of rectangle."
333 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 334 (set-register register
efeae993
RS
335 (if delete-flag
336 (delete-extract-rectangle start end)
337 (extract-rectangle start end))))
c88ab9ce 338
0f214cdf 339(provide 'register)
cbee283d 340;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
c88ab9ce 341;;; register.el ends here