1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
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)
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.
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.
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, marker or list.
37 A list of strings represents a rectangle.
38 A list of the form (file . NAME) represents the file named NAME.
39 A list of the form (file-query NAME POSITION) represents position POSITION
40 in the file named NAME, but query before visiting it.
41 A list of the form (WINDOW-CONFIGURATION POSITION)
42 represents a saved window configuration plus a saved value of point.
43 A list of the form (FRAME-CONFIGURATION POSITION)
44 represents a saved frame configuration plus a saved value of point.")
46 (defun get-register (reg)
47 "Return contents of Emacs register named REG, or nil if none."
48 (cdr (assq reg register-alist
)))
50 (defun set-register (register value
)
51 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
52 See the documentation of the variable `register-alist' for possible VALUE."
53 (let ((aelt (assq register register-alist
)))
56 (setq aelt
(cons register value
))
57 (setq register-alist
(cons aelt register-alist
)))
60 (defun point-to-register (register &optional arg
)
61 "Store current location of point in register REGISTER.
62 With prefix argument, store current frame configuration.
63 Use \\[jump-to-register] to go to that location or restore that configuration.
64 Argument is a character, naming the register."
65 (interactive "cPoint to register: \nP")
66 (set-register register
67 (if arg
(list (current-frame-configuration) (point-marker))
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 ;; current-window-configuration does not include the value
76 ;; of point in the current buffer, so record that separately.
77 (set-register register
(list (current-window-configuration) (point-marker))))
79 (defun frame-configuration-to-register (register &optional arg
)
80 "Store the window configuration of all frames in register REGISTER.
81 Use \\[jump-to-register] to restore the configuration.
82 Argument is a character, naming the register."
83 (interactive "cFrame configuration to register: \nP")
84 ;; current-frame-configuration does not include the value
85 ;; of point in the current buffer, so record that separately.
86 (set-register register
(list (current-frame-configuration) (point-marker))))
88 (defalias 'register-to-point
'jump-to-register
)
89 (defun jump-to-register (register &optional delete
)
90 "Move point to location stored in a register.
91 If the register contains a file name, find that file.
92 \(To put a file name in a register, you must use `set-register'.)
93 If the register contains a window configuration (one frame) or a frame
94 configuration (all frames), restore that frame or all frames accordingly.
95 First argument is a character, naming the register.
96 Optional second arg non-nil (interactively, prefix argument) says to
97 delete any existing frames that the frame configuration doesn't mention.
98 \(Otherwise, these frames are iconified.)"
99 (interactive "cJump to register: \nP")
100 (let ((val (get-register register
)))
102 ((and (consp val
) (frame-configuration-p (car val
)))
103 (set-frame-configuration (car val
) (not delete
))
104 (goto-char (cadr val
)))
105 ((and (consp val
) (window-configuration-p (car val
)))
106 (set-window-configuration (car val
))
107 (goto-char (cadr val
)))
109 (or (marker-buffer val
)
110 (error "That register's buffer no longer exists"))
111 (switch-to-buffer (marker-buffer val
))
113 ((and (consp val
) (eq (car val
) 'file
))
114 (find-file (cdr val
)))
115 ((and (consp val
) (eq (car val
) 'file-query
))
116 (or (find-buffer-visiting (nth 1 val
))
117 (y-or-n-p (format "Visit file %s again? " (nth 1 val
)))
118 (error "Register access aborted"))
119 (find-file (nth 1 val
))
120 (goto-char (nth 2 val
)))
122 (error "Register doesn't contain a buffer position or configuration")))))
124 ;; Turn markers into file-query references when a buffer is killed.
125 (defun register-swap-out ()
126 (and buffer-file-name
127 (let ((tail register-alist
))
129 (and (markerp (cdr (car tail
)))
130 (eq (marker-buffer (cdr (car tail
))) (current-buffer))
134 (marker-position (cdr (car tail
))))))
135 (setq tail
(cdr tail
))))))
137 (add-hook 'kill-buffer-hook
'register-swap-out
)
139 (defun number-to-register (number register
)
140 "Store a number in a register.
141 Two args, NUMBER and REGISTER (a character, naming the register).
142 If NUMBER is nil, a decimal number is read from the buffer starting
143 at point, and point moves to the end of that number.
144 Interactively, NUMBER is the prefix arg (none means nil)."
145 (interactive "P\ncNumber to register: ")
146 (set-register register
148 (prefix-numeric-value number
)
149 (if (looking-at "\\s-*-?[0-9]+")
151 (goto-char (match-end 0))
152 (string-to-int (match-string 0)))
155 (defun increment-register (number register
)
156 "Add NUMBER to the contents of register REGISTER.
157 Interactively, NUMBER is the prefix arg."
158 (interactive "p\ncIncrement register: ")
159 (or (numberp (get-register register
))
160 (error "Register does not contain a number"))
161 (set-register register
(+ number
(get-register register
))))
163 (defun view-register (register)
164 "Display what is contained in register named REGISTER.
165 The Lisp value REGISTER is a character."
166 (interactive "cView register: ")
167 (let ((val (get-register register
)))
169 (message "Register %s is empty" (single-key-description register
))
170 (with-output-to-temp-buffer "*Output*"
171 (describe-register-1 register t
)))))
173 (defun list-registers ()
174 "Display a list of nonempty registers saying briefly what they contain."
176 (let ((list (copy-sequence register-alist
)))
177 (setq list
(sort list
(lambda (a b
) (< (car a
) (car b
)))))
178 (with-output-to-temp-buffer "*Output*"
180 (when (get-register (car elt
))
181 (describe-register-1 (car elt
))
184 (defun describe-register-1 (register &optional verbose
)
186 (princ (single-key-description register
))
188 (let ((val (get-register register
)))
194 (let ((buf (marker-buffer val
)))
196 (princ "a marker in no buffer")
197 (princ "a buffer position:\n buffer ")
198 (princ (buffer-name buf
))
199 (princ ", position ")
200 (princ (marker-position val
)))))
202 ((and (consp val
) (window-configuration-p (car val
)))
203 (princ "a window configuration."))
205 ((and (consp val
) (frame-configuration-p (car val
)))
206 (princ "a frame configuration."))
208 ((and (consp val
) (eq (car val
) 'file
))
213 ((and (consp val
) (eq (car val
) 'file-query
))
214 (princ "a file-query reference:\n file ")
215 (prin1 (car (cdr val
)))
216 (princ ",\n position ")
217 (princ (car (cdr (cdr val
))))
223 (princ "the rectangle:\n")
228 (setq val
(cdr val
))))
229 (princ "a rectangle starting with ")
235 (princ "the text:\n")
237 (princ "text starting with\n ")
238 (string-match "[^ \t\n].\\{,20\\}" val
)
239 (princ (match-string 0 val
))))
242 (if verbose
(prin1 val
))))))
244 (defun insert-register (register &optional arg
)
245 "Insert contents of register REGISTER. (REGISTER is a character.)
246 Normally puts point before and mark after the inserted text.
247 If optional second arg is non-nil, puts mark before and point after.
248 Interactively, second arg is non-nil if prefix arg is supplied."
249 (interactive "*cInsert register: \nP")
251 (let ((val (get-register register
)))
254 (insert-rectangle val
))
258 (princ val
(current-buffer)))
259 ((and (markerp val
) (marker-position val
))
260 (princ (marker-position val
) (current-buffer)))
262 (error "Register does not contain text"))))
263 (if (not arg
) (exchange-point-and-mark)))
265 (defun copy-to-register (register start end
&optional delete-flag
)
266 "Copy region into register REGISTER. With prefix arg, delete as well.
267 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
268 START and END are buffer positions indicating what to copy."
269 (interactive "cCopy to register: \nr\nP")
270 (set-register register
(buffer-substring start end
))
271 (if delete-flag
(delete-region start end
)))
273 (defun append-to-register (register start end
&optional delete-flag
)
274 "Append region to text in register REGISTER.
275 With prefix arg, delete as well.
276 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
277 START and END are buffer positions indicating what to append."
278 (interactive "cAppend to register: \nr\nP")
279 (or (stringp (get-register register
))
280 (error "Register does not contain text"))
281 (set-register register
(concat (get-register register
)
282 (buffer-substring start end
)))
283 (if delete-flag
(delete-region start end
)))
285 (defun prepend-to-register (register start end
&optional delete-flag
)
286 "Prepend region to text in register REGISTER.
287 With prefix arg, delete as well.
288 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
289 START and END are buffer positions indicating what to prepend."
290 (interactive "cPrepend to register: \nr\nP")
291 (or (stringp (get-register register
))
292 (error "Register does not contain text"))
293 (set-register register
(concat (buffer-substring start end
)
294 (get-register register
)))
295 (if delete-flag
(delete-region start end
)))
297 (defun copy-rectangle-to-register (register start end
&optional delete-flag
)
298 "Copy rectangular region into register REGISTER.
299 With prefix arg, delete as well.
300 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
301 START and END are buffer positions giving two corners of rectangle."
302 (interactive "cCopy rectangle to register: \nr\nP")
303 (set-register register
305 (delete-extract-rectangle start end
)
306 (extract-rectangle start end
))))
308 ;;; register.el ends here