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