Add 2009 to copyright years.
[bpt/emacs.git] / lisp / register.el
1 ;;; register.el --- register commands for Emacs
2
3 ;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
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
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
47 ;;; Code:
48
49 (defvar register-alist nil
50 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
51 NAME is a character (a number). CONTENTS is a string, number, marker or list.
52 A list of strings represents a rectangle.
53 A list of the form (file . NAME) represents the file named NAME.
54 A list of the form (file-query NAME POSITION) represents position POSITION
55 in the file named NAME, but query before visiting it.
56 A list of the form (WINDOW-CONFIGURATION POSITION)
57 represents a saved window configuration plus a saved value of point.
58 A list of the form (FRAME-CONFIGURATION POSITION)
59 represents a saved frame configuration plus a saved value of point.")
60
61 (defun get-register (reg)
62 "Return contents of Emacs register named REG, or nil if none."
63 (cdr (assq reg register-alist)))
64
65 (defun set-register (register value)
66 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
67 See the documentation of the variable `register-alist' for possible VALUE."
68 (let ((aelt (assq register register-alist)))
69 (if aelt
70 (setcdr aelt value)
71 (push (cons register value) register-alist))
72 value))
73
74 (defun point-to-register (register &optional arg)
75 "Store current location of point in register REGISTER.
76 With prefix argument, store current frame configuration.
77 Use \\[jump-to-register] to go to that location or restore that configuration.
78 Argument is a character, naming the register."
79 (interactive "cPoint to register: \nP")
80 ;; Turn the marker into a file-ref if the buffer is killed.
81 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
82 (set-register register
83 (if arg (list (current-frame-configuration) (point-marker))
84 (point-marker))))
85
86 (defun window-configuration-to-register (register &optional arg)
87 "Store the window configuration of the selected frame in register REGISTER.
88 Use \\[jump-to-register] to restore the configuration.
89 Argument is a character, naming the register."
90 (interactive "cWindow configuration to register: \nP")
91 ;; current-window-configuration does not include the value
92 ;; of point in the current buffer, so record that separately.
93 (set-register register (list (current-window-configuration) (point-marker))))
94
95 (defun frame-configuration-to-register (register &optional arg)
96 "Store the window configuration of all frames in register REGISTER.
97 Use \\[jump-to-register] to restore the configuration.
98 Argument is a character, naming the register."
99 (interactive "cFrame configuration to register: \nP")
100 ;; current-frame-configuration does not include the value
101 ;; of point in the current buffer, so record that separately.
102 (set-register register (list (current-frame-configuration) (point-marker))))
103
104 (defalias 'register-to-point 'jump-to-register)
105 (defun jump-to-register (register &optional delete)
106 "Move point to location stored in a register.
107 If the register contains a file name, find that file.
108 \(To put a file name in a register, you must use `set-register'.)
109 If the register contains a window configuration (one frame) or a frame
110 configuration (all frames), restore that frame or all frames accordingly.
111 First argument is a character, naming the register.
112 Optional second arg non-nil (interactively, prefix argument) says to
113 delete any existing frames that the frame configuration doesn't mention.
114 \(Otherwise, these frames are iconified.)"
115 (interactive "cJump to register: \nP")
116 (let ((val (get-register register)))
117 (cond
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)))
124 ((markerp val)
125 (or (marker-buffer val)
126 (error "That register's buffer no longer exists"))
127 (switch-to-buffer (marker-buffer val))
128 (goto-char val))
129 ((and (consp val) (eq (car val) 'file))
130 (find-file (cdr val)))
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)))
137 (t
138 (error "Register doesn't contain a buffer position or configuration")))))
139
140 (defun register-swap-out ()
141 "Turn markers into file-query references when a buffer is killed."
142 (and buffer-file-name
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))))))))
150
151 (defun number-to-register (number register)
152 "Store a number in a register.
153 Two args, NUMBER and REGISTER (a character, naming the register).
154 If NUMBER is nil, a decimal number is read from the buffer starting
155 at point, and point moves to the end of that number.
156 Interactively, NUMBER is the prefix arg (none means nil)."
157 (interactive "P\ncNumber to register: ")
158 (set-register register
159 (if number
160 (prefix-numeric-value number)
161 (if (looking-at "\\s-*-?[0-9]+")
162 (progn
163 (goto-char (match-end 0))
164 (string-to-number (match-string 0)))
165 0))))
166
167 (defun increment-register (number register)
168 "Add NUMBER to the contents of register REGISTER.
169 Interactively, NUMBER is the prefix arg."
170 (interactive "p\ncIncrement register: ")
171 (or (numberp (get-register register))
172 (error "Register does not contain a number"))
173 (set-register register (+ number (get-register register))))
174
175 (defun view-register (register)
176 "Display what is contained in register named REGISTER.
177 The Lisp value REGISTER is a character."
178 (interactive "cView register: ")
179 (let ((val (get-register register)))
180 (if (null val)
181 (message "Register %s is empty" (single-key-description register))
182 (with-output-to-temp-buffer "*Output*"
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 ")
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)
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))
249 (if verbose
250 (progn
251 (princ "the text:\n")
252 (princ val))
253 (cond
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")))))
265 (t
266 (princ "Garbage:\n")
267 (if verbose (prin1 val))))))
268
269 (defun insert-register (register &optional arg)
270 "Insert contents of register REGISTER. (REGISTER is a character.)
271 Normally puts point before and mark after the inserted text.
272 If optional second arg is non-nil, puts mark before and point after.
273 Interactively, second arg is non-nil if prefix arg is supplied."
274 (interactive "*cInsert register: \nP")
275 (push-mark)
276 (let ((val (get-register register)))
277 (cond
278 ((consp val)
279 (insert-rectangle val))
280 ((stringp val)
281 (insert-for-yank val))
282 ((numberp val)
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"))))
288 (if (not arg) (exchange-point-and-mark)))
289
290 (defun copy-to-register (register start end &optional delete-flag)
291 "Copy region into register REGISTER. With prefix arg, delete as well.
292 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
293 START and END are buffer positions indicating what to copy."
294 (interactive "cCopy to register: \nr\nP")
295 (set-register register (filter-buffer-substring start end))
296 (if delete-flag (delete-region start end)))
297
298 (defun append-to-register (register start end &optional delete-flag)
299 "Append region to text in register REGISTER.
300 With prefix arg, delete as well.
301 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
302 START and END are buffer positions indicating what to append."
303 (interactive "cAppend to register: \nr\nP")
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")))))
310 (if delete-flag (delete-region start end)))
311
312 (defun prepend-to-register (register start end &optional delete-flag)
313 "Prepend region to text in register REGISTER.
314 With prefix arg, delete as well.
315 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
316 START and END are buffer positions indicating what to prepend."
317 (interactive "cPrepend to register: \nr\nP")
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")))))
324 (if delete-flag (delete-region start end)))
325
326 (defun copy-rectangle-to-register (register start end &optional delete-flag)
327 "Copy rectangular region into register REGISTER.
328 With prefix arg, delete as well. To insert this register
329 in the buffer, use \\[insert-register].
330
331 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
332 START and END are buffer positions giving two corners of rectangle."
333 (interactive "cCopy rectangle to register: \nr\nP")
334 (set-register register
335 (if delete-flag
336 (delete-extract-rectangle start end)
337 (extract-rectangle start end))))
338
339 (provide 'register)
340 ;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
341 ;;; register.el ends here