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