(add_command_key, command_loop_1, read_char, kbd_buffer_store_event,
[bpt/emacs.git] / lisp / register.el
CommitLineData
c88ab9ce
ER
1;;; register.el --- register commands for Emacs.
2
6bfb7922 3;; Copyright (C) 1985, 1993 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
RS
39
40(defun get-register (char)
41 "Return contents of Emacs register named CHAR, or nil if none."
42 (cdr (assq char register-alist)))
43
44(defun set-register (char value)
22073dda
RS
45 "Set contents of Emacs register named CHAR to VALUE. Returns VALUE.
46See the documentation of the variable `register-alist' for possible VALUE."
efeae993
RS
47 (let ((aelt (assq char register-alist)))
48 (if aelt
49 (setcdr aelt value)
50 (setq aelt (cons char value))
51 (setq register-alist (cons aelt register-alist)))
52 value))
53
a8a6776b 54(defun point-to-register (char &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")
0cc89026 60 (set-register char (if arg (current-frame-configuration) (point-marker))))
efeae993 61
a8a6776b 62(defun window-configuration-to-register (char &optional arg)
83b5d757
RS
63 "Store the window configuration of the selected frame in register REGISTER.
64Use \\[jump-to-register] to restore the configuration.
65Argument is a character, naming the register."
5f517806 66 (interactive "cWindow configuration to register: \nP")
83b5d757
RS
67 (set-register char (current-window-configuration)))
68
a8a6776b 69(defun frame-configuration-to-register (char &optional arg)
83b5d757
RS
70 "Store the window configuration of all frames in register REGISTER.
71Use \\[jump-to-register] to restore the configuration.
72Argument is a character, naming the register."
5f517806 73 (interactive "cFrame configuration to register: \nP")
83b5d757
RS
74 (set-register char (current-frame-configuration)))
75
31e1d920 76(defalias 'register-to-point 'jump-to-register)
1542ad37 77(defun jump-to-register (char &optional delete)
efeae993 78 "Move point to location stored in a register.
22073dda
RS
79If the register contains a file name, find that file.
80 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
81If the register contains a window configuration (one frame) or a frame
82configuration (all frames), restore that frame or all frames accordingly.
e7683fff 83First argument is a character, naming the register.
1542ad37
RS
84Optional second arg non-nil (interactively, prefix argument) says to
85delete any existing frames that the frame configuration doesn't mention.
a21d94f9 86\(Otherwise, these frames are iconified.)"
e7683fff 87 (interactive "cJump to register: \nP")
efeae993 88 (let ((val (get-register char)))
376a7584 89 (cond
bf77ce53
RS
90 ((and (fboundp 'frame-configuration-p)
91 (frame-configuration-p val))
1542ad37 92 (set-frame-configuration val (not delete)))
376a7584
JB
93 ((window-configuration-p val)
94 (set-window-configuration val))
95 ((markerp val)
96 (switch-to-buffer (marker-buffer val))
97 (goto-char val))
22073dda
RS
98 ((and (consp val) (eq (car val) 'file))
99 (find-file (cdr val)))
376a7584
JB
100 (t
101 (error "Register doesn't contain a buffer position or configuration")))))
efeae993
RS
102
103;(defun number-to-register (arg char)
104; "Store a number in a register.
105;Two args, NUMBER and REGISTER (a character, naming the register).
106;If NUMBER is nil, digits in the buffer following point are read
107;to get the number to store.
108;Interactively, NUMBER is the prefix arg (none means nil)."
109; (interactive "P\ncNumber to register: ")
110; (set-register char
111; (if arg
112; (prefix-numeric-value arg)
113; (if (looking-at "[0-9][0-9]*")
114; (save-excursion
115; (save-restriction
116; (narrow-to-region (point)
117; (progn (skip-chars-forward "0-9")
118; (point)))
119; (goto-char (point-min))
120; (read (current-buffer))))
121; 0))))
122
123;(defun increment-register (arg char)
124; "Add NUMBER to the contents of register REGISTER.
125;Interactively, NUMBER is the prefix arg (none means nil)."
126; (interactive "p\ncNumber to register: ")
127; (or (integerp (get-register char))
128; (error "Register does not contain a number"))
129; (set-register char (+ arg (get-register char))))
130
131(defun view-register (char)
132 "Display what is contained in register named REGISTER.
133REGISTER is a character."
134 (interactive "cView register: ")
135 (let ((val (get-register char)))
136 (if (null val)
137 (message "Register %s is empty" (single-key-description char))
138 (with-output-to-temp-buffer "*Output*"
139 (princ "Register ")
140 (princ (single-key-description char))
141 (princ " contains ")
ed83b192
JB
142 (cond
143 ((integerp val)
144 (princ val))
145
146 ((markerp val)
cbd4993c
KH
147 (let ((buf (marker-buffer val)))
148 (if (null buf)
149 (princ "a marker in no buffer")
150 (princ "a buffer position:\nbuffer ")
151 (princ (buffer-name buf))
152 (princ ", position ")
153 (princ (marker-position val)))))
ed83b192
JB
154
155 ((window-configuration-p val)
156 (princ "a window configuration."))
157
158 ((frame-configuration-p val)
159 (princ "a frame configuration."))
160
6bfb7922
RS
161 ((and (consp val) (eq (car val) 'file))
162 (princ "the file ")
163 (prin1 (cdr val))
164 (princ "."))
165
ed83b192
JB
166 ((consp val)
167 (princ "the rectangle:\n")
168 (while val
169 (princ (car val))
170 (terpri)
171 (setq val (cdr val))))
172
173 ((stringp val)
174 (princ "the text:\n")
175 (princ val))
176
177 (t
178 (princ "Garbage:\n")
179 (prin1 val)))))))
efeae993
RS
180
181(defun insert-register (char &optional arg)
182 "Insert contents of register REG. REG is a character.
183Normally puts point before and mark after the inserted text.
184If optional second arg is non-nil, puts mark before and point after.
185Interactively, second arg is non-nil if prefix arg is supplied."
186 (interactive "cInsert register: \nP")
187 (push-mark)
188 (let ((val (get-register char)))
cbd4993c
KH
189 (cond
190 ((consp val)
191 (insert-rectangle val))
192 ((stringp val)
193 (insert val))
194 ((integerp val)
195 (princ val (current-buffer)))
196 ((and (markerp val) (marker-position val))
197 (princ (marker-position val) (current-buffer)))
198 (t
199 (error "Register does not contain text"))))
efeae993
RS
200 (if (not arg) (exchange-point-and-mark)))
201
202(defun copy-to-register (char start end &optional delete-flag)
14c5b721
JB
203 "Copy region into register REG. With prefix arg, delete as well.
204Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
205START and END are buffer positions indicating what to copy."
206 (interactive "cCopy to register: \nr\nP")
207 (set-register char (buffer-substring start end))
208 (if delete-flag (delete-region start end)))
209
210(defun append-to-register (char start end &optional delete-flag)
14c5b721
JB
211 "Append region to text in register REG. With prefix arg, delete as well.
212Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
213START and END are buffer positions indicating what to append."
214 (interactive "cAppend to register: \nr\nP")
215 (or (stringp (get-register char))
216 (error "Register does not contain text"))
217 (set-register char (concat (get-register char)
218 (buffer-substring start end)))
219 (if delete-flag (delete-region start end)))
220
221(defun prepend-to-register (char start end &optional delete-flag)
14c5b721
JB
222 "Prepend region to text in register REG. With prefix arg, delete as well.
223Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
224START and END are buffer positions indicating what to prepend."
225 (interactive "cPrepend to register: \nr\nP")
226 (or (stringp (get-register char))
227 (error "Register does not contain text"))
228 (set-register char (concat (buffer-substring start end)
229 (get-register char)))
230 (if delete-flag (delete-region start end)))
231
232(defun copy-rectangle-to-register (char start end &optional delete-flag)
14c5b721
JB
233 "Copy rectangular region into register REG. With prefix arg, delete as well.
234Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
235START and END are buffer positions giving two corners of rectangle."
236 (interactive "cCopy rectangle to register: \nr\nP")
237 (set-register char
238 (if delete-flag
239 (delete-extract-rectangle start end)
240 (extract-rectangle start end))))
c88ab9ce
ER
241
242;;; register.el ends here