new version
[emacs.git] / lisp / register.el
blob32cd981bdcac978baf9263b735edd94031c672c9
1 ;;; register.el --- register commands for Emacs.
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
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.
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.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
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.
32 ;;; Code:
34 (defvar register-alist nil
35 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
36 NAME is a character (a number). CONTENTS is a string, number,
37 frame configuration, mark or list.
38 A list of strings represents a rectangle.
39 A list of the form (file . NAME) represents the file named NAME.
40 A list of the form (file-query NAME POSITION) represents position POSITION
41 in the file named NAME, but query before visiting it.")
43 (defun get-register (reg)
44 "Return contents of Emacs register named REG, or nil if none."
45 (cdr (assq reg register-alist)))
47 (defun set-register (register value)
48 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
49 See the documentation of the variable `register-alist' for possible VALUE."
50 (let ((aelt (assq register register-alist)))
51 (if aelt
52 (setcdr aelt value)
53 (setq aelt (cons register value))
54 (setq register-alist (cons aelt register-alist)))
55 value))
57 (defun point-to-register (register &optional arg)
58 "Store current location of point in register REGISTER.
59 With prefix argument, store current frame configuration.
60 Use \\[jump-to-register] to go to that location or restore that configuration.
61 Argument is a character, naming the register."
62 (interactive "cPoint to register: \nP")
63 (set-register register
64 (if arg (current-frame-configuration) (point-marker))))
66 (defun window-configuration-to-register (register &optional arg)
67 "Store the window configuration of the selected frame in register REGISTER.
68 Use \\[jump-to-register] to restore the configuration.
69 Argument is a character, naming the register."
70 (interactive "cWindow configuration to register: \nP")
71 (set-register register (current-window-configuration)))
73 (defun frame-configuration-to-register (register &optional arg)
74 "Store the window configuration of all frames in register REGISTER.
75 Use \\[jump-to-register] to restore the configuration.
76 Argument is a character, naming the register."
77 (interactive "cFrame configuration to register: \nP")
78 (set-register register (current-frame-configuration)))
80 (defalias 'register-to-point 'jump-to-register)
81 (defun jump-to-register (register &optional delete)
82 "Move point to location stored in a register.
83 If the register contains a file name, find that file.
84 \(To put a file name in a register, you must use `set-register'.)
85 If the register contains a window configuration (one frame) or a frame
86 configuration (all frames), restore that frame or all frames accordingly.
87 First argument is a character, naming the register.
88 Optional second arg non-nil (interactively, prefix argument) says to
89 delete any existing frames that the frame configuration doesn't mention.
90 \(Otherwise, these frames are iconified.)"
91 (interactive "cJump to register: \nP")
92 (let ((val (get-register register)))
93 (cond
94 ((and (fboundp 'frame-configuration-p)
95 (frame-configuration-p val))
96 (set-frame-configuration val (not delete)))
97 ((window-configuration-p val)
98 (set-window-configuration val))
99 ((markerp val)
100 (or (marker-buffer val)
101 (error "That register's buffer no longer exists"))
102 (switch-to-buffer (marker-buffer val))
103 (goto-char val))
104 ((and (consp val) (eq (car val) 'file))
105 (find-file (cdr val)))
106 ((and (consp val) (eq (car val) 'file-query))
107 (or (find-buffer-visiting (nth 1 val))
108 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
109 (error "Register access aborted"))
110 (find-file (nth 1 val))
111 (goto-char (nth 2 val)))
113 (error "Register doesn't contain a buffer position or configuration")))))
115 ;; Turn markers into file-query references when a buffer is killed.
116 (defun register-swap-out ()
117 (and buffer-file-name
118 (let ((tail register-alist))
119 (while tail
120 (and (markerp (cdr (car tail)))
121 (eq (marker-buffer (cdr (car tail))) (current-buffer))
122 (setcdr (car tail)
123 (list 'file-query
124 buffer-file-name
125 (marker-position (cdr (car tail))))))
126 (setq tail (cdr tail))))))
128 (add-hook 'kill-buffer-hook 'register-swap-out)
130 ;(defun number-to-register (arg char)
131 ; "Store a number in a register.
132 ;Two args, NUMBER and REGISTER (a character, naming the register).
133 ;If NUMBER is nil, digits in the buffer following point are read
134 ;to get the number to store.
135 ;Interactively, NUMBER is the prefix arg (none means nil)."
136 ; (interactive "P\ncNumber to register: ")
137 ; (set-register char
138 ; (if arg
139 ; (prefix-numeric-value arg)
140 ; (if (looking-at "[0-9][0-9]*")
141 ; (save-excursion
142 ; (save-restriction
143 ; (narrow-to-region (point)
144 ; (progn (skip-chars-forward "0-9")
145 ; (point)))
146 ; (goto-char (point-min))
147 ; (read (current-buffer))))
148 ; 0))))
150 ;(defun increment-register (arg char)
151 ; "Add NUMBER to the contents of register REGISTER.
152 ;Interactively, NUMBER is the prefix arg (none means nil)."
153 ; (interactive "p\ncNumber to register: ")
154 ; (or (integerp (get-register char))
155 ; (error "Register does not contain a number"))
156 ; (set-register char (+ arg (get-register char))))
158 (defun view-register (register)
159 "Display what is contained in register named REGISTER.
160 The Lisp value REGISTER is a character."
161 (interactive "cView register: ")
162 (let ((val (get-register register)))
163 (if (null val)
164 (message "Register %s is empty" (single-key-description register))
165 (with-output-to-temp-buffer "*Output*"
166 (princ "Register ")
167 (princ (single-key-description register))
168 (princ " contains ")
169 (cond
170 ((integerp val)
171 (princ val))
173 ((markerp val)
174 (let ((buf (marker-buffer val)))
175 (if (null buf)
176 (princ "a marker in no buffer")
177 (princ "a buffer position:\nbuffer ")
178 (princ (buffer-name buf))
179 (princ ", position ")
180 (princ (marker-position val)))))
182 ((window-configuration-p val)
183 (princ "a window configuration."))
185 ((frame-configuration-p val)
186 (princ "a frame configuration."))
188 ((and (consp val) (eq (car val) 'file))
189 (princ "the file ")
190 (prin1 (cdr val))
191 (princ "."))
193 ((consp val)
194 (princ "the rectangle:\n")
195 (while val
196 (princ (car val))
197 (terpri)
198 (setq val (cdr val))))
200 ((stringp val)
201 (princ "the text:\n")
202 (princ val))
205 (princ "Garbage:\n")
206 (prin1 val)))))))
208 (defun insert-register (register &optional arg)
209 "Insert contents of register REGISTER. (REGISTER is a character.)
210 Normally puts point before and mark after the inserted text.
211 If optional second arg is non-nil, puts mark before and point after.
212 Interactively, second arg is non-nil if prefix arg is supplied."
213 (interactive "*cInsert register: \nP")
214 (push-mark)
215 (let ((val (get-register register)))
216 (cond
217 ((consp val)
218 (insert-rectangle val))
219 ((stringp val)
220 (insert val))
221 ((integerp val)
222 (princ val (current-buffer)))
223 ((and (markerp val) (marker-position val))
224 (princ (marker-position val) (current-buffer)))
226 (error "Register does not contain text"))))
227 (if (not arg) (exchange-point-and-mark)))
229 (defun copy-to-register (register start end &optional delete-flag)
230 "Copy region into register REGISTER. With prefix arg, delete as well.
231 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
232 START and END are buffer positions indicating what to copy."
233 (interactive "cCopy to register: \nr\nP")
234 (set-register register (buffer-substring start end))
235 (if delete-flag (delete-region start end)))
237 (defun append-to-register (register start end &optional delete-flag)
238 "Append region to text in register REGISTER.
239 With prefix arg, delete as well.
240 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
241 START and END are buffer positions indicating what to append."
242 (interactive "cAppend to register: \nr\nP")
243 (or (stringp (get-register register))
244 (error "Register does not contain text"))
245 (set-register register (concat (get-register register)
246 (buffer-substring start end)))
247 (if delete-flag (delete-region start end)))
249 (defun prepend-to-register (register start end &optional delete-flag)
250 "Prepend region to text in register REGISTER.
251 With prefix arg, delete as well.
252 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
253 START and END are buffer positions indicating what to prepend."
254 (interactive "cPrepend to register: \nr\nP")
255 (or (stringp (get-register register))
256 (error "Register does not contain text"))
257 (set-register register (concat (buffer-substring start end)
258 (get-register register)))
259 (if delete-flag (delete-region start end)))
261 (defun copy-rectangle-to-register (register start end &optional delete-flag)
262 "Copy rectangular region into register REGISTER.
263 With prefix arg, delete as well.
264 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
265 START and END are buffer positions giving two corners of rectangle."
266 (interactive "cCopy rectangle to register: \nr\nP")
267 (set-register register
268 (if delete-flag
269 (delete-extract-rectangle start end)
270 (extract-rectangle start end))))
272 ;;; register.el ends here