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