XEmacs 21.2-b1
[chise/xemacs-chise.git.1] / lisp / register.el
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.3
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, marker or list.
41 A list of strings represents a rectangle.
42 A list of the form (file . NAME) represents the file named NAME.
43 A list of the form (file-query NAME POSITION) represents position POSITION
44  in the file named NAME, but query before visiting it.
45 A list of the form (WINDOW-CONFIGURATION POSITION)
46  represents a saved window configuration plus a saved value of point.
47 A list of the form (FRAME-CONFIGURATION POSITION)
48  represents a saved frame configuration plus a saved value of point.")
49
50 (defun get-register (reg)
51   "Return contents of Emacs register named REG, or nil if none."
52   (cdr (assq reg register-alist)))
53
54 (defun set-register (register value)
55   "Set contents of Emacs register named REGISTER to VALUE.  Returns VALUE.
56 See the documentation of the variable `register-alist' for possible VALUE."
57   (let ((aelt (assq register register-alist)))
58     (if aelt
59         (setcdr aelt value)
60       (setq aelt (cons register value))
61       (setq register-alist (cons aelt register-alist)))
62     value))
63
64 (defun point-to-register (register &optional arg)
65   "Store current location of point in register REGISTER.
66 With prefix argument, store current frame configuration.
67 Use \\[jump-to-register] to go to that location or restore that configuration.
68 Argument is a character, naming the register."
69   (interactive "cPoint to register: \nP")
70   (set-register register
71                 (if arg (list (current-frame-configuration) (point-marker))
72                   (point-marker))))
73
74 (defun window-configuration-to-register (register &optional arg)
75   "Store the window configuration of the selected frame in register REGISTER.
76 Use \\[jump-to-register] to restore the configuration.
77 Argument is a character, naming the register."
78   (interactive "cWindow configuration to register: \nP")
79   ;; current-window-configuration does not include the value
80   ;; of point in the current buffer, so record that separately.
81   (set-register register (list (current-window-configuration) (point-marker))))
82
83 (defun frame-configuration-to-register (register &optional arg)
84   "Store the window configuration of all frames in register REGISTER.
85 Use \\[jump-to-register] to restore the configuration.
86 Argument is a character, naming the register."
87   (interactive "cFrame configuration to register: \nP")
88   ;; current-frame-configuration does not include the value
89   ;; of point in the current buffer, so record that separately.
90   (set-register register (list (current-frame-configuration) (point-marker))))
91
92 (defalias 'register-to-point 'jump-to-register)
93 (defun jump-to-register (register &optional delete)
94   "Move point to location stored in a register.
95 If the register contains a file name, find that file.
96  \(To put a file name in a register, you must use `set-register'.)
97 If the register contains a window configuration (one frame) or a frame
98 configuration (all frames), restore that frame or all frames accordingly.
99 First argument is a character, naming the register.
100 Optional second arg non-nil (interactively, prefix argument) says to
101 delete any existing frames that the frame configuration doesn't mention.
102 \(Otherwise, these frames are iconified.)"
103   (interactive "cJump to register: \nP")
104   (let ((val (get-register register)))
105     (cond
106      ((and (consp val) (frame-configuration-p (car val)))
107       (set-frame-configuration (car val) (not delete))
108       (goto-char (cadr val)))
109      ((and (consp val) (window-configuration-p (car val)))
110       (set-window-configuration (car val))
111       (goto-char (cadr val)))
112      ((markerp val)
113       (or (marker-buffer val)
114           (error "That register's buffer no longer exists"))
115       (switch-to-buffer (marker-buffer val))
116       (goto-char val))
117      ((and (consp val) (eq (car val) 'file))
118       (find-file (cdr val)))
119      ((and (consp val) (eq (car val) 'file-query))
120       (or (find-buffer-visiting (nth 1 val))
121           (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
122           (error "Register access aborted"))
123       (find-file (nth 1 val))
124       (goto-char (nth 2 val)))
125      (t
126       (error "Register doesn't contain a buffer position or configuration")))))
127
128 ;; Turn markers into file-query references when a buffer is killed.
129 (defun register-swap-out ()
130   (and buffer-file-name
131        (let ((tail register-alist))
132          (while tail
133            (and (markerp (cdr (car tail)))
134                 (eq (marker-buffer (cdr (car tail))) (current-buffer))
135                 (setcdr (car tail)
136                         (list 'file-query
137                               buffer-file-name
138                               (marker-position (cdr (car tail))))))
139            (setq tail (cdr tail))))))
140
141 (add-hook 'kill-buffer-hook 'register-swap-out)
142
143 (defun number-to-register (number register)
144   "Store a number in a register.
145 Two args, NUMBER and REGISTER (a character, naming the register).
146 If NUMBER is nil, a decimal number is read from the buffer starting
147 at point, and point moves to the end of that number.
148 Interactively, NUMBER is the prefix arg (none means nil)."
149   (interactive "P\ncNumber to register: ")
150   (set-register register 
151                 (if number
152                     (prefix-numeric-value number)
153                   (if (looking-at "\\s-*-?[0-9]+")
154                       (progn
155                         (goto-char (match-end 0))
156                         (string-to-int (match-string 0)))
157                     0))))
158
159 (defun increment-register (number register)
160   "Add NUMBER to the contents of register REGISTER.
161 Interactively, NUMBER is the prefix arg."
162   (interactive "p\ncIncrement register: ")
163   (or (numberp (get-register register))
164       (error "Register does not contain a number"))
165   (set-register register (+ number (get-register register))))
166
167 (defun view-register (register)
168   "Display what is contained in register named REGISTER.
169 The Lisp value REGISTER is a character."
170   (interactive "cView register: ")
171   (let ((val (get-register register)))
172     (if (null val)
173         (message "Register %s is empty" (single-key-description register))
174       (with-output-to-temp-buffer "*Output*"
175         (princ "Register ")
176         (princ (single-key-description register))
177         (princ " contains ")
178         (cond
179          ((numberp val)
180           (princ val))
181
182          ((markerp val)
183           (let ((buf (marker-buffer val)))
184             (if (null buf)
185                 (princ "a marker in no buffer")
186               (princ "a buffer position:\nbuffer ")
187               (princ (buffer-name buf))
188               (princ ", position ")
189               (princ (marker-position val)))))
190
191          ((and (consp val) (window-configuration-p (car val)))
192           (princ "a window configuration."))
193
194          ((and (consp val) (frame-configuration-p (car val)))
195           (princ "a frame configuration."))
196
197          ((and (consp val) (eq (car val) 'file))
198           (princ "the file ")
199           (prin1 (cdr val))
200           (princ "."))
201
202          ((and (consp val) (eq (car val) 'file-query))
203           (princ "a file-query reference:\nfile ")
204           (prin1 (car (cdr val)))
205           (princ ",\nposition ")
206           (princ (car (cdr (cdr val))))
207           (princ "."))
208
209          ((consp val)
210           (princ "the rectangle:\n")
211           (while val
212             (princ (car val))
213             (terpri)
214             (setq val (cdr val))))
215
216          ((stringp val)
217           (princ "the text:\n")
218           (princ val))
219
220          (t
221           (princ "Garbage:\n")
222           (prin1 val)))))))
223
224 (defun insert-register (register &optional arg)
225   "Insert contents of register REGISTER.  (REGISTER is a character.)
226 Normally puts point before and mark after the inserted text.
227 If optional second arg is non-nil, puts mark before and point after.
228 Interactively, second arg is non-nil if prefix arg is supplied."
229   (interactive "*cInsert register: \nP")
230   (push-mark)
231   (let ((val (get-register register)))
232     (cond
233      ((consp val)
234       (insert-rectangle val))
235      ((stringp val)
236       (insert val))
237      ((numberp val)
238       (princ val (current-buffer)))
239      ((and (markerp val) (marker-position val))
240       (princ (marker-position val) (current-buffer)))
241      (t
242       (error "Register does not contain text"))))
243   ;; XEmacs: don't activate the region.  It's annoying.
244   (if (not arg) (exchange-point-and-mark t)))
245
246 (defun copy-to-register (register start end &optional delete-flag)
247   "Copy region into register REGISTER.  With prefix arg, delete as well.
248 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
249 START and END are buffer positions indicating what to copy."
250   (interactive "cCopy to register: \nr\nP")
251   (set-register register (buffer-substring start end))
252   (if delete-flag (delete-region start end)))
253
254 (defun append-to-register (register start end &optional delete-flag)
255   "Append region to text in register REGISTER.
256 With prefix arg, delete as well.
257 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
258 START and END are buffer positions indicating what to append."
259   (interactive "cAppend to register: \nr\nP")
260   (or (stringp (get-register register))
261       (error "Register does not contain text"))
262   (set-register register (concat (get-register register)
263                             (buffer-substring start end)))
264   (if delete-flag (delete-region start end)))
265
266 (defun prepend-to-register (register start end &optional delete-flag)
267   "Prepend region to text in register REGISTER.
268 With prefix arg, delete as well.
269 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
270 START and END are buffer positions indicating what to prepend."
271   (interactive "cPrepend to register: \nr\nP")
272   (or (stringp (get-register register))
273       (error "Register does not contain text"))
274   (set-register register (concat (buffer-substring start end)
275                             (get-register register)))
276   (if delete-flag (delete-region start end)))
277
278 (defun copy-rectangle-to-register (register start end &optional delete-flag)
279   "Copy rectangular region into register REGISTER.
280 With prefix arg, delete as well.
281 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
282 START and END are buffer positions giving two corners of rectangle."
283   (interactive "cCopy rectangle to register: \nr\nP")
284   (set-register register
285                 (if delete-flag
286                     (delete-extract-rectangle start end)
287                   (extract-rectangle start end))))
288
289 ;;; register.el ends here