(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / lisp / register.el
CommitLineData
c88ab9ce
ER
1;;; register.el --- register commands for Emacs.
2
8f1204db 3;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
9750e079 4
4821e2af 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: internal
4821e2af 7
efeae993
RS
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
efeae993
RS
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
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
4821e2af 31;;; Code:
efeae993
RS
32
33(defvar register-alist nil
34 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
35NAME is a character (a number). CONTENTS is a string, number,
0cc89026 36frame configuration, mark or list.
22073dda
RS
37A list of strings represents a rectangle.
38A list of the form (file . NAME) represents the file named NAME.")
efeae993 39
1b8cac5d
RS
40(defun get-register (reg)
41 "Return contents of Emacs register named REG, or nil if none."
42 (cdr (assq reg register-alist)))
efeae993 43
1b8cac5d
RS
44(defun set-register (register value)
45 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
22073dda 46See the documentation of the variable `register-alist' for possible VALUE."
1b8cac5d 47 (let ((aelt (assq register register-alist)))
efeae993
RS
48 (if aelt
49 (setcdr aelt value)
1b8cac5d 50 (setq aelt (cons register value))
efeae993
RS
51 (setq register-alist (cons aelt register-alist)))
52 value))
53
1b8cac5d 54(defun point-to-register (register &optional arg)
b42e6156 55 "Store current location of point in register REGISTER.
0cc89026 56With prefix argument, store current frame configuration.
b42e6156 57Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 58Argument is a character, naming the register."
b42e6156 59 (interactive "cPoint to register: \nP")
1b8cac5d
RS
60 (set-register register
61 (if arg (current-frame-configuration) (point-marker))))
efeae993 62
1b8cac5d 63(defun window-configuration-to-register (register &optional arg)
83b5d757
RS
64 "Store the window configuration of the selected frame in register REGISTER.
65Use \\[jump-to-register] to restore the configuration.
66Argument is a character, naming the register."
5f517806 67 (interactive "cWindow configuration to register: \nP")
1b8cac5d 68 (set-register register (current-window-configuration)))
83b5d757 69
1b8cac5d 70(defun frame-configuration-to-register (register &optional arg)
83b5d757
RS
71 "Store the window configuration of all frames in register REGISTER.
72Use \\[jump-to-register] to restore the configuration.
73Argument is a character, naming the register."
5f517806 74 (interactive "cFrame configuration to register: \nP")
1b8cac5d 75 (set-register register (current-frame-configuration)))
83b5d757 76
31e1d920 77(defalias 'register-to-point 'jump-to-register)
1b8cac5d 78(defun jump-to-register (register &optional delete)
efeae993 79 "Move point to location stored in a register.
22073dda
RS
80If the register contains a file name, find that file.
81 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
82If the register contains a window configuration (one frame) or a frame
83configuration (all frames), restore that frame or all frames accordingly.
e7683fff 84First argument is a character, naming the register.
1542ad37
RS
85Optional second arg non-nil (interactively, prefix argument) says to
86delete any existing frames that the frame configuration doesn't mention.
a21d94f9 87\(Otherwise, these frames are iconified.)"
e7683fff 88 (interactive "cJump to register: \nP")
1b8cac5d 89 (let ((val (get-register register)))
376a7584 90 (cond
bf77ce53
RS
91 ((and (fboundp 'frame-configuration-p)
92 (frame-configuration-p val))
1542ad37 93 (set-frame-configuration val (not delete)))
376a7584
JB
94 ((window-configuration-p val)
95 (set-window-configuration val))
96 ((markerp val)
8c4ca60c
KH
97 (or (marker-buffer val)
98 (error "That register's buffer no longer exists"))
376a7584
JB
99 (switch-to-buffer (marker-buffer val))
100 (goto-char val))
22073dda
RS
101 ((and (consp val) (eq (car val) 'file))
102 (find-file (cdr val)))
376a7584
JB
103 (t
104 (error "Register doesn't contain a buffer position or configuration")))))
efeae993
RS
105
106;(defun number-to-register (arg char)
107; "Store a number in a register.
108;Two args, NUMBER and REGISTER (a character, naming the register).
109;If NUMBER is nil, digits in the buffer following point are read
110;to get the number to store.
111;Interactively, NUMBER is the prefix arg (none means nil)."
112; (interactive "P\ncNumber to register: ")
113; (set-register char
114; (if arg
115; (prefix-numeric-value arg)
116; (if (looking-at "[0-9][0-9]*")
117; (save-excursion
118; (save-restriction
119; (narrow-to-region (point)
120; (progn (skip-chars-forward "0-9")
121; (point)))
122; (goto-char (point-min))
123; (read (current-buffer))))
124; 0))))
125
126;(defun increment-register (arg char)
127; "Add NUMBER to the contents of register REGISTER.
128;Interactively, NUMBER is the prefix arg (none means nil)."
129; (interactive "p\ncNumber to register: ")
130; (or (integerp (get-register char))
131; (error "Register does not contain a number"))
132; (set-register char (+ arg (get-register char))))
133
1b8cac5d 134(defun view-register (register)
efeae993 135 "Display what is contained in register named REGISTER.
1b8cac5d 136The Lisp value REGISTER is a character."
efeae993 137 (interactive "cView register: ")
1b8cac5d 138 (let ((val (get-register register)))
efeae993 139 (if (null val)
1b8cac5d 140 (message "Register %s is empty" (single-key-description register))
efeae993
RS
141 (with-output-to-temp-buffer "*Output*"
142 (princ "Register ")
1b8cac5d 143 (princ (single-key-description register))
efeae993 144 (princ " contains ")
ed83b192
JB
145 (cond
146 ((integerp val)
147 (princ val))
148
149 ((markerp val)
cbd4993c
KH
150 (let ((buf (marker-buffer val)))
151 (if (null buf)
152 (princ "a marker in no buffer")
153 (princ "a buffer position:\nbuffer ")
154 (princ (buffer-name buf))
155 (princ ", position ")
156 (princ (marker-position val)))))
ed83b192
JB
157
158 ((window-configuration-p val)
159 (princ "a window configuration."))
160
161 ((frame-configuration-p val)
162 (princ "a frame configuration."))
163
6bfb7922
RS
164 ((and (consp val) (eq (car val) 'file))
165 (princ "the file ")
166 (prin1 (cdr val))
167 (princ "."))
168
ed83b192
JB
169 ((consp val)
170 (princ "the rectangle:\n")
171 (while val
172 (princ (car val))
173 (terpri)
174 (setq val (cdr val))))
175
176 ((stringp val)
177 (princ "the text:\n")
178 (princ val))
179
180 (t
181 (princ "Garbage:\n")
182 (prin1 val)))))))
efeae993 183
1b8cac5d
RS
184(defun insert-register (register &optional arg)
185 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
186Normally puts point before and mark after the inserted text.
187If optional second arg is non-nil, puts mark before and point after.
188Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 189 (interactive "*cInsert register: \nP")
efeae993 190 (push-mark)
1b8cac5d 191 (let ((val (get-register register)))
cbd4993c
KH
192 (cond
193 ((consp val)
194 (insert-rectangle val))
195 ((stringp val)
196 (insert val))
197 ((integerp val)
198 (princ val (current-buffer)))
199 ((and (markerp val) (marker-position val))
200 (princ (marker-position val) (current-buffer)))
201 (t
202 (error "Register does not contain text"))))
efeae993
RS
203 (if (not arg) (exchange-point-and-mark)))
204
1b8cac5d
RS
205(defun copy-to-register (register start end &optional delete-flag)
206 "Copy region into register REGISTER. With prefix arg, delete as well.
207Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
208START and END are buffer positions indicating what to copy."
209 (interactive "cCopy to register: \nr\nP")
1b8cac5d 210 (set-register register (buffer-substring start end))
efeae993
RS
211 (if delete-flag (delete-region start end)))
212
1b8cac5d
RS
213(defun append-to-register (register start end &optional delete-flag)
214 "Append region to text in register REGISTER.
215With prefix arg, delete as well.
216Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
217START and END are buffer positions indicating what to append."
218 (interactive "cAppend to register: \nr\nP")
1b8cac5d 219 (or (stringp (get-register register))
efeae993 220 (error "Register does not contain text"))
1b8cac5d
RS
221 (set-register register (concat (get-register register)
222 (buffer-substring start end)))
efeae993
RS
223 (if delete-flag (delete-region start end)))
224
1b8cac5d
RS
225(defun prepend-to-register (register start end &optional delete-flag)
226 "Prepend region to text in register REGISTER.
227With prefix arg, delete as well.
228Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
229START and END are buffer positions indicating what to prepend."
230 (interactive "cPrepend to register: \nr\nP")
1b8cac5d 231 (or (stringp (get-register register))
efeae993 232 (error "Register does not contain text"))
1b8cac5d
RS
233 (set-register register (concat (buffer-substring start end)
234 (get-register register)))
efeae993
RS
235 (if delete-flag (delete-region start end)))
236
1b8cac5d
RS
237(defun copy-rectangle-to-register (register start end &optional delete-flag)
238 "Copy rectangular region into register REGISTER.
239With prefix arg, delete as well.
240Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
241START and END are buffer positions giving two corners of rectangle."
242 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 243 (set-register register
efeae993
RS
244 (if delete-flag
245 (delete-extract-rectangle start end)
246 (extract-rectangle start end))))
c88ab9ce
ER
247
248;;; register.el ends here