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