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