* disp-table.el (standard-display-default): New function.
[bpt/emacs.git] / lisp / register.el
CommitLineData
c88ab9ce
ER
1;;; register.el --- register commands for Emacs.
2
9750e079
ER
3;; Copyright (C) 1985 Free Software Foundation, Inc.
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."
66 (interactive "cPoint to register: \nP")
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."
73 (interactive "cPoint to register: \nP")
74 (set-register char (current-frame-configuration)))
75
31e1d920 76(defalias 'register-to-point 'jump-to-register)
efeae993
RS
77(defun jump-to-register (char)
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.
efeae993
RS
83Argument is a character, naming the register."
84 (interactive "cJump to register: ")
85 (let ((val (get-register char)))
376a7584
JB
86 (cond
87 ((frame-configuration-p val)
88 (set-frame-configuration val))
89 ((window-configuration-p val)
90 (set-window-configuration val))
91 ((markerp val)
92 (switch-to-buffer (marker-buffer val))
93 (goto-char val))
22073dda
RS
94 ((and (consp val) (eq (car val) 'file))
95 (find-file (cdr val)))
376a7584
JB
96 (t
97 (error "Register doesn't contain a buffer position or configuration")))))
efeae993
RS
98
99;(defun number-to-register (arg char)
100; "Store a number in a register.
101;Two args, NUMBER and REGISTER (a character, naming the register).
102;If NUMBER is nil, digits in the buffer following point are read
103;to get the number to store.
104;Interactively, NUMBER is the prefix arg (none means nil)."
105; (interactive "P\ncNumber to register: ")
106; (set-register char
107; (if arg
108; (prefix-numeric-value arg)
109; (if (looking-at "[0-9][0-9]*")
110; (save-excursion
111; (save-restriction
112; (narrow-to-region (point)
113; (progn (skip-chars-forward "0-9")
114; (point)))
115; (goto-char (point-min))
116; (read (current-buffer))))
117; 0))))
118
119;(defun increment-register (arg char)
120; "Add NUMBER to the contents of register REGISTER.
121;Interactively, NUMBER is the prefix arg (none means nil)."
122; (interactive "p\ncNumber to register: ")
123; (or (integerp (get-register char))
124; (error "Register does not contain a number"))
125; (set-register char (+ arg (get-register char))))
126
127(defun view-register (char)
128 "Display what is contained in register named REGISTER.
129REGISTER is a character."
130 (interactive "cView register: ")
131 (let ((val (get-register char)))
132 (if (null val)
133 (message "Register %s is empty" (single-key-description char))
134 (with-output-to-temp-buffer "*Output*"
135 (princ "Register ")
136 (princ (single-key-description char))
137 (princ " contains ")
ed83b192
JB
138 (cond
139 ((integerp val)
140 (princ val))
141
142 ((markerp val)
143 (princ "a buffer position:\nbuffer ")
144 (princ (buffer-name (marker-buffer val)))
145 (princ ", position ")
146 (princ (+ 0 val)))
147
148 ((window-configuration-p val)
149 (princ "a window configuration."))
150
151 ((frame-configuration-p val)
152 (princ "a frame configuration."))
153
154 ((consp val)
155 (princ "the rectangle:\n")
156 (while val
157 (princ (car val))
158 (terpri)
159 (setq val (cdr val))))
160
161 ((stringp val)
162 (princ "the text:\n")
163 (princ val))
164
165 (t
166 (princ "Garbage:\n")
167 (prin1 val)))))))
efeae993
RS
168
169(defun insert-register (char &optional arg)
170 "Insert contents of register REG. REG is a character.
171Normally puts point before and mark after the inserted text.
172If optional second arg is non-nil, puts mark before and point after.
173Interactively, second arg is non-nil if prefix arg is supplied."
174 (interactive "cInsert register: \nP")
175 (push-mark)
176 (let ((val (get-register char)))
177 (if (consp val)
178 (insert-rectangle val)
179 (if (stringp val)
180 (insert val)
181 (if (or (integerp val) (markerp val))
182 (princ (+ 0 val) (current-buffer))
183 (error "Register does not contain text")))))
184 (if (not arg) (exchange-point-and-mark)))
185
186(defun copy-to-register (char start end &optional delete-flag)
14c5b721
JB
187 "Copy region into register REG. With prefix arg, delete as well.
188Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
189START and END are buffer positions indicating what to copy."
190 (interactive "cCopy to register: \nr\nP")
191 (set-register char (buffer-substring start end))
192 (if delete-flag (delete-region start end)))
193
194(defun append-to-register (char start end &optional delete-flag)
14c5b721
JB
195 "Append region to text in register REG. With prefix arg, delete as well.
196Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
197START and END are buffer positions indicating what to append."
198 (interactive "cAppend to register: \nr\nP")
199 (or (stringp (get-register char))
200 (error "Register does not contain text"))
201 (set-register char (concat (get-register char)
202 (buffer-substring start end)))
203 (if delete-flag (delete-region start end)))
204
205(defun prepend-to-register (char start end &optional delete-flag)
14c5b721
JB
206 "Prepend region to text in register REG. With prefix arg, delete as well.
207Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
208START and END are buffer positions indicating what to prepend."
209 (interactive "cPrepend to register: \nr\nP")
210 (or (stringp (get-register char))
211 (error "Register does not contain text"))
212 (set-register char (concat (buffer-substring start end)
213 (get-register char)))
214 (if delete-flag (delete-region start end)))
215
216(defun copy-rectangle-to-register (char start end &optional delete-flag)
14c5b721
JB
217 "Copy rectangular region into register REG. With prefix arg, delete as well.
218Called from program, takes four args: REG, START, END and DELETE-FLAG.
efeae993
RS
219START and END are buffer positions giving two corners of rectangle."
220 (interactive "cCopy rectangle to register: \nr\nP")
221 (set-register char
222 (if delete-flag
223 (delete-extract-rectangle start end)
224 (extract-rectangle start end))))
c88ab9ce
ER
225
226;;; register.el ends here