(notice_overwritten_cursor): Fix an off by 1 error.
[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)
1b8cac5d 56 (setq aelt (cons register value))
efeae993
RS
57 (setq register-alist (cons aelt register-alist)))
58 value))
59
1b8cac5d 60(defun point-to-register (register &optional arg)
b42e6156 61 "Store current location of point in register REGISTER.
0cc89026 62With prefix argument, store current frame configuration.
b42e6156 63Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 64Argument is a character, naming the register."
b42e6156 65 (interactive "cPoint to register: \nP")
1b8cac5d 66 (set-register register
070c2506
KH
67 (if arg (list (current-frame-configuration) (point-marker))
68 (point-marker))))
efeae993 69
1b8cac5d 70(defun window-configuration-to-register (register &optional arg)
83b5d757
RS
71 "Store the window configuration of the selected frame in register REGISTER.
72Use \\[jump-to-register] to restore the configuration.
73Argument is a character, naming the register."
5f517806 74 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
75 ;; current-window-configuration does not include the value
76 ;; of point in the current buffer, so record that separately.
070c2506 77 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 78
1b8cac5d 79(defun frame-configuration-to-register (register &optional arg)
83b5d757
RS
80 "Store the window configuration of all frames in register REGISTER.
81Use \\[jump-to-register] to restore the configuration.
82Argument is a character, naming the register."
5f517806 83 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
84 ;; current-frame-configuration does not include the value
85 ;; of point in the current buffer, so record that separately.
070c2506 86 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 87
31e1d920 88(defalias 'register-to-point 'jump-to-register)
1b8cac5d 89(defun jump-to-register (register &optional delete)
efeae993 90 "Move point to location stored in a register.
22073dda
RS
91If the register contains a file name, find that file.
92 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
93If the register contains a window configuration (one frame) or a frame
94configuration (all frames), restore that frame or all frames accordingly.
e7683fff 95First argument is a character, naming the register.
1542ad37
RS
96Optional second arg non-nil (interactively, prefix argument) says to
97delete any existing frames that the frame configuration doesn't mention.
a21d94f9 98\(Otherwise, these frames are iconified.)"
e7683fff 99 (interactive "cJump to register: \nP")
1b8cac5d 100 (let ((val (get-register register)))
376a7584 101 (cond
b4a91f43
KH
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)))
376a7584 108 ((markerp val)
8c4ca60c
KH
109 (or (marker-buffer val)
110 (error "That register's buffer no longer exists"))
376a7584
JB
111 (switch-to-buffer (marker-buffer val))
112 (goto-char val))
22073dda
RS
113 ((and (consp val) (eq (car val) 'file))
114 (find-file (cdr val)))
28cbd14d
RS
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)))
376a7584
JB
121 (t
122 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 123
28cbd14d
RS
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))
128 (while tail
129 (and (markerp (cdr (car tail)))
130 (eq (marker-buffer (cdr (car tail))) (current-buffer))
131 (setcdr (car tail)
132 (list 'file-query
133 buffer-file-name
134 (marker-position (cdr (car tail))))))
135 (setq tail (cdr tail))))))
136
137(add-hook 'kill-buffer-hook 'register-swap-out)
138
0e07a458 139(defun number-to-register (number register)
070c2506
KH
140 "Store a number in a register.
141Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
142If NUMBER is nil, a decimal number is read from the buffer starting
143at point, and point moves to the end of that number.
070c2506
KH
144Interactively, NUMBER is the prefix arg (none means nil)."
145 (interactive "P\ncNumber to register: ")
0e07a458
KH
146 (set-register register
147 (if number
148 (prefix-numeric-value number)
4d2caa07
KH
149 (if (looking-at "\\s-*-?[0-9]+")
150 (progn
151 (goto-char (match-end 0))
152 (string-to-int (match-string 0)))
070c2506
KH
153 0))))
154
0e07a458 155(defun increment-register (number register)
070c2506 156 "Add NUMBER to the contents of register REGISTER.
0e07a458 157Interactively, NUMBER is the prefix arg."
070c2506 158 (interactive "p\ncIncrement register: ")
0e07a458 159 (or (numberp (get-register register))
070c2506 160 (error "Register does not contain a number"))
0e07a458 161 (set-register register (+ number (get-register register))))
efeae993 162
1b8cac5d 163(defun view-register (register)
efeae993 164 "Display what is contained in register named REGISTER.
1b8cac5d 165The Lisp value REGISTER is a character."
efeae993 166 (interactive "cView register: ")
1b8cac5d 167 (let ((val (get-register register)))
efeae993 168 (if (null val)
1b8cac5d 169 (message "Register %s is empty" (single-key-description register))
efeae993 170 (with-output-to-temp-buffer "*Output*"
92840a31
RS
171 (describe-register-1 register t)))))
172
173(defun list-registers ()
174 "Display a list of nonempty registers saying briefly what they contain."
175 (interactive)
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*"
179 (dolist (elt list)
180 (when (get-register (car elt))
181 (describe-register-1 (car elt))
182 (terpri))))))
183
184(defun describe-register-1 (register &optional verbose)
185 (princ "Register ")
186 (princ (single-key-description register))
187 (princ " contains ")
6ed21409
RS
188 (let ((val (get-register register)))
189 (cond
190 ((numberp val)
191 (princ val))
192
193 ((markerp val)
194 (let ((buf (marker-buffer val)))
195 (if (null buf)
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)))))
201
202 ((and (consp val) (window-configuration-p (car val)))
203 (princ "a window configuration."))
204
205 ((and (consp val) (frame-configuration-p (car val)))
206 (princ "a frame configuration."))
207
208 ((and (consp val) (eq (car val) 'file))
209 (princ "the file ")
210 (prin1 (cdr val))
211 (princ "."))
212
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))))
218 (princ "."))
219
220 ((consp val)
221 (if verbose
222 (progn
223 (princ "the rectangle:\n")
224 (while val
225 (princ " ")
226 (princ (car val))
227 (terpri)
228 (setq val (cdr val))))
229 (princ "a rectangle starting with ")
230 (princ (car val))))
231
232 ((stringp val)
e7c765c3
RS
233 (setq val
234 (remove-list-of-text-properties 0 (length val)
235 yank-excluded-properties val))
6ed21409
RS
236 (if verbose
237 (progn
238 (princ "the text:\n")
239 (princ val))
240 (princ "text starting with\n ")
241 (string-match "[^ \t\n].\\{,20\\}" val)
242 (princ (match-string 0 val))))
243 (t
244 (princ "Garbage:\n")
245 (if verbose (prin1 val))))))
efeae993 246
1b8cac5d
RS
247(defun insert-register (register &optional arg)
248 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
249Normally puts point before and mark after the inserted text.
250If optional second arg is non-nil, puts mark before and point after.
251Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 252 (interactive "*cInsert register: \nP")
efeae993 253 (push-mark)
1b8cac5d 254 (let ((val (get-register register)))
cbd4993c
KH
255 (cond
256 ((consp val)
257 (insert-rectangle val))
258 ((stringp val)
e7c765c3 259 (insert-for-yank val))
070c2506 260 ((numberp val)
cbd4993c
KH
261 (princ val (current-buffer)))
262 ((and (markerp val) (marker-position val))
263 (princ (marker-position val) (current-buffer)))
264 (t
265 (error "Register does not contain text"))))
efeae993
RS
266 (if (not arg) (exchange-point-and-mark)))
267
1b8cac5d
RS
268(defun copy-to-register (register start end &optional delete-flag)
269 "Copy region into register REGISTER. With prefix arg, delete as well.
270Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
271START and END are buffer positions indicating what to copy."
272 (interactive "cCopy to register: \nr\nP")
1b8cac5d 273 (set-register register (buffer-substring start end))
efeae993
RS
274 (if delete-flag (delete-region start end)))
275
1b8cac5d
RS
276(defun append-to-register (register start end &optional delete-flag)
277 "Append region to text in register REGISTER.
278With prefix arg, delete as well.
279Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
280START and END are buffer positions indicating what to append."
281 (interactive "cAppend to register: \nr\nP")
1b8cac5d 282 (or (stringp (get-register register))
efeae993 283 (error "Register does not contain text"))
1b8cac5d
RS
284 (set-register register (concat (get-register register)
285 (buffer-substring start end)))
efeae993
RS
286 (if delete-flag (delete-region start end)))
287
1b8cac5d
RS
288(defun prepend-to-register (register start end &optional delete-flag)
289 "Prepend region to text in register REGISTER.
290With prefix arg, delete as well.
291Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
292START and END are buffer positions indicating what to prepend."
293 (interactive "cPrepend to register: \nr\nP")
1b8cac5d 294 (or (stringp (get-register register))
efeae993 295 (error "Register does not contain text"))
1b8cac5d
RS
296 (set-register register (concat (buffer-substring start end)
297 (get-register register)))
efeae993
RS
298 (if delete-flag (delete-region start end)))
299
1b8cac5d
RS
300(defun copy-rectangle-to-register (register start end &optional delete-flag)
301 "Copy rectangular region into register REGISTER.
302With prefix arg, delete as well.
303Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
304START and END are buffer positions giving two corners of rectangle."
305 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 306 (set-register register
efeae993
RS
307 (if delete-flag
308 (delete-extract-rectangle start end)
309 (extract-rectangle start end))))
c88ab9ce
ER
310
311;;; register.el ends here