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