(xpm_format, pbm_format, png_format, jpeg_format, tiff_format)
[bpt/emacs.git] / lisp / register.el
CommitLineData
55535639 1;;; register.el --- register commands for Emacs
c88ab9ce 2
8f1204db 3;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
9750e079 4
4821e2af 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: internal
4821e2af 7
efeae993
RS
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
efeae993
RS
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
b578f267
EN
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.
efeae993 24
d9ecc911
ER
25;;; Commentary:
26
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.
31
4821e2af 32;;; Code:
efeae993
RS
33
34(defvar register-alist nil
35 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
070c2506 36NAME is a character (a number). CONTENTS is a string, number, marker or list.
22073dda 37A list of strings represents a rectangle.
28cbd14d
RS
38A list of the form (file . NAME) represents the file named NAME.
39A list of the form (file-query NAME POSITION) represents position POSITION
070c2506
KH
40 in the file named NAME, but query before visiting it.
41A list of the form (WINDOW-CONFIGURATION POSITION)
42 represents a saved window configuration plus a saved value of point.
43A list of the form (FRAME-CONFIGURATION POSITION)
44 represents a saved frame configuration plus a saved value of point.")
efeae993 45
1b8cac5d
RS
46(defun get-register (reg)
47 "Return contents of Emacs register named REG, or nil if none."
48 (cdr (assq reg register-alist)))
efeae993 49
1b8cac5d
RS
50(defun set-register (register value)
51 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
22073dda 52See the documentation of the variable `register-alist' for possible VALUE."
1b8cac5d 53 (let ((aelt (assq register register-alist)))
efeae993
RS
54 (if aelt
55 (setcdr aelt value)
ddbb3cc7 56 (push (cons register value) register-alist))
efeae993
RS
57 value))
58
1b8cac5d 59(defun point-to-register (register &optional arg)
b42e6156 60 "Store current location of point in register REGISTER.
0cc89026 61With prefix argument, store current frame configuration.
b42e6156 62Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 63Argument is a character, naming the register."
b42e6156 64 (interactive "cPoint to register: \nP")
ddbb3cc7
SM
65 ;; Turn the marker into a file-ref if the buffer is killed.
66 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 67 (set-register register
070c2506
KH
68 (if arg (list (current-frame-configuration) (point-marker))
69 (point-marker))))
efeae993 70
1b8cac5d 71(defun window-configuration-to-register (register &optional arg)
83b5d757
RS
72 "Store the window configuration of the selected frame in register REGISTER.
73Use \\[jump-to-register] to restore the configuration.
74Argument is a character, naming the register."
5f517806 75 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
76 ;; current-window-configuration does not include the value
77 ;; of point in the current buffer, so record that separately.
070c2506 78 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 79
1b8cac5d 80(defun frame-configuration-to-register (register &optional arg)
83b5d757
RS
81 "Store the window configuration of all frames in register REGISTER.
82Use \\[jump-to-register] to restore the configuration.
83Argument is a character, naming the register."
5f517806 84 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
85 ;; current-frame-configuration does not include the value
86 ;; of point in the current buffer, so record that separately.
070c2506 87 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 88
31e1d920 89(defalias 'register-to-point 'jump-to-register)
1b8cac5d 90(defun jump-to-register (register &optional delete)
efeae993 91 "Move point to location stored in a register.
22073dda
RS
92If the register contains a file name, find that file.
93 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
94If the register contains a window configuration (one frame) or a frame
95configuration (all frames), restore that frame or all frames accordingly.
e7683fff 96First argument is a character, naming the register.
1542ad37
RS
97Optional second arg non-nil (interactively, prefix argument) says to
98delete any existing frames that the frame configuration doesn't mention.
a21d94f9 99\(Otherwise, these frames are iconified.)"
e7683fff 100 (interactive "cJump to register: \nP")
1b8cac5d 101 (let ((val (get-register register)))
376a7584 102 (cond
b4a91f43
KH
103 ((and (consp val) (frame-configuration-p (car val)))
104 (set-frame-configuration (car val) (not delete))
105 (goto-char (cadr val)))
106 ((and (consp val) (window-configuration-p (car val)))
107 (set-window-configuration (car val))
108 (goto-char (cadr val)))
376a7584 109 ((markerp val)
8c4ca60c
KH
110 (or (marker-buffer val)
111 (error "That register's buffer no longer exists"))
376a7584
JB
112 (switch-to-buffer (marker-buffer val))
113 (goto-char val))
22073dda
RS
114 ((and (consp val) (eq (car val) 'file))
115 (find-file (cdr val)))
28cbd14d
RS
116 ((and (consp val) (eq (car val) 'file-query))
117 (or (find-buffer-visiting (nth 1 val))
118 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
119 (error "Register access aborted"))
120 (find-file (nth 1 val))
121 (goto-char (nth 2 val)))
376a7584
JB
122 (t
123 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 124
28cbd14d 125(defun register-swap-out ()
ddbb3cc7 126 "Turn markers into file-query references when a buffer is killed."
28cbd14d 127 (and buffer-file-name
ddbb3cc7
SM
128 (dolist (elem register-alist)
129 (and (markerp (cdr elem))
130 (eq (marker-buffer (cdr elem)) (current-buffer))
131 (setcdr elem
132 (list 'file-query
133 buffer-file-name
134 (marker-position (cdr elem))))))))
28cbd14d 135
0e07a458 136(defun number-to-register (number register)
070c2506
KH
137 "Store a number in a register.
138Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
139If NUMBER is nil, a decimal number is read from the buffer starting
140at point, and point moves to the end of that number.
070c2506
KH
141Interactively, NUMBER is the prefix arg (none means nil)."
142 (interactive "P\ncNumber to register: ")
0e07a458
KH
143 (set-register register
144 (if number
145 (prefix-numeric-value number)
4d2caa07
KH
146 (if (looking-at "\\s-*-?[0-9]+")
147 (progn
148 (goto-char (match-end 0))
149 (string-to-int (match-string 0)))
070c2506
KH
150 0))))
151
0e07a458 152(defun increment-register (number register)
070c2506 153 "Add NUMBER to the contents of register REGISTER.
0e07a458 154Interactively, NUMBER is the prefix arg."
070c2506 155 (interactive "p\ncIncrement register: ")
0e07a458 156 (or (numberp (get-register register))
070c2506 157 (error "Register does not contain a number"))
0e07a458 158 (set-register register (+ number (get-register register))))
efeae993 159
1b8cac5d 160(defun view-register (register)
efeae993 161 "Display what is contained in register named REGISTER.
1b8cac5d 162The Lisp value REGISTER is a character."
efeae993 163 (interactive "cView register: ")
1b8cac5d 164 (let ((val (get-register register)))
efeae993 165 (if (null val)
1b8cac5d 166 (message "Register %s is empty" (single-key-description register))
efeae993 167 (with-output-to-temp-buffer "*Output*"
92840a31
RS
168 (describe-register-1 register t)))))
169
170(defun list-registers ()
171 "Display a list of nonempty registers saying briefly what they contain."
172 (interactive)
173 (let ((list (copy-sequence register-alist)))
174 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
175 (with-output-to-temp-buffer "*Output*"
176 (dolist (elt list)
177 (when (get-register (car elt))
178 (describe-register-1 (car elt))
179 (terpri))))))
180
181(defun describe-register-1 (register &optional verbose)
182 (princ "Register ")
183 (princ (single-key-description register))
184 (princ " contains ")
6ed21409
RS
185 (let ((val (get-register register)))
186 (cond
187 ((numberp val)
188 (princ val))
189
190 ((markerp val)
191 (let ((buf (marker-buffer val)))
192 (if (null buf)
193 (princ "a marker in no buffer")
194 (princ "a buffer position:\n buffer ")
195 (princ (buffer-name buf))
196 (princ ", position ")
197 (princ (marker-position val)))))
198
199 ((and (consp val) (window-configuration-p (car val)))
200 (princ "a window configuration."))
201
202 ((and (consp val) (frame-configuration-p (car val)))
203 (princ "a frame configuration."))
204
205 ((and (consp val) (eq (car val) 'file))
206 (princ "the file ")
207 (prin1 (cdr val))
208 (princ "."))
209
210 ((and (consp val) (eq (car val) 'file-query))
211 (princ "a file-query reference:\n file ")
212 (prin1 (car (cdr val)))
213 (princ ",\n position ")
214 (princ (car (cdr (cdr val))))
215 (princ "."))
216
217 ((consp val)
218 (if verbose
219 (progn
220 (princ "the rectangle:\n")
221 (while val
222 (princ " ")
223 (princ (car val))
224 (terpri)
225 (setq val (cdr val))))
226 (princ "a rectangle starting with ")
227 (princ (car val))))
228
229 ((stringp val)
e7c765c3
RS
230 (setq val
231 (remove-list-of-text-properties 0 (length val)
232 yank-excluded-properties val))
6ed21409
RS
233 (if verbose
234 (progn
235 (princ "the text:\n")
236 (princ val))
237 (princ "text starting with\n ")
238 (string-match "[^ \t\n].\\{,20\\}" val)
239 (princ (match-string 0 val))))
240 (t
241 (princ "Garbage:\n")
242 (if verbose (prin1 val))))))
efeae993 243
1b8cac5d
RS
244(defun insert-register (register &optional arg)
245 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
246Normally puts point before and mark after the inserted text.
247If optional second arg is non-nil, puts mark before and point after.
248Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 249 (interactive "*cInsert register: \nP")
efeae993 250 (push-mark)
1b8cac5d 251 (let ((val (get-register register)))
cbd4993c
KH
252 (cond
253 ((consp val)
254 (insert-rectangle val))
255 ((stringp val)
e7c765c3 256 (insert-for-yank val))
070c2506 257 ((numberp val)
cbd4993c
KH
258 (princ val (current-buffer)))
259 ((and (markerp val) (marker-position val))
260 (princ (marker-position val) (current-buffer)))
261 (t
262 (error "Register does not contain text"))))
efeae993
RS
263 (if (not arg) (exchange-point-and-mark)))
264
1b8cac5d
RS
265(defun copy-to-register (register start end &optional delete-flag)
266 "Copy region into register REGISTER. With prefix arg, delete as well.
267Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
268START and END are buffer positions indicating what to copy."
269 (interactive "cCopy to register: \nr\nP")
1b8cac5d 270 (set-register register (buffer-substring start end))
efeae993
RS
271 (if delete-flag (delete-region start end)))
272
1b8cac5d
RS
273(defun append-to-register (register start end &optional delete-flag)
274 "Append region to text in register REGISTER.
275With prefix arg, delete as well.
276Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
277START and END are buffer positions indicating what to append."
278 (interactive "cAppend to register: \nr\nP")
1b8cac5d 279 (or (stringp (get-register register))
efeae993 280 (error "Register does not contain text"))
1b8cac5d
RS
281 (set-register register (concat (get-register register)
282 (buffer-substring start end)))
efeae993
RS
283 (if delete-flag (delete-region start end)))
284
1b8cac5d
RS
285(defun prepend-to-register (register start end &optional delete-flag)
286 "Prepend region to text in register REGISTER.
287With prefix arg, delete as well.
288Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
289START and END are buffer positions indicating what to prepend."
290 (interactive "cPrepend to register: \nr\nP")
1b8cac5d 291 (or (stringp (get-register register))
efeae993 292 (error "Register does not contain text"))
1b8cac5d
RS
293 (set-register register (concat (buffer-substring start end)
294 (get-register register)))
efeae993
RS
295 (if delete-flag (delete-region start end)))
296
1b8cac5d
RS
297(defun copy-rectangle-to-register (register start end &optional delete-flag)
298 "Copy rectangular region into register REGISTER.
299With prefix arg, delete as well.
300Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
301START and END are buffer positions giving two corners of rectangle."
302 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 303 (set-register register
efeae993
RS
304 (if delete-flag
305 (delete-extract-rectangle start end)
306 (extract-rectangle start end))))
c88ab9ce
ER
307
308;;; register.el ends here