]> code.delx.au - gnu-emacs/blob - lisp/register.el
Comment changes.
[gnu-emacs] / lisp / register.el
1 ;;; register.el --- register commands for Emacs.
2
3 ;; Copyright (C) 1985, 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; This package of functions emulates and somewhat extends the venerable
27 ;; TECO's `register' feature, which permits you to save various useful
28 ;; pieces of buffer state to named variables. The entry points are
29 ;; documented in the Emacs user's manual.
30
31 ;;; Code:
32
33 (defvar register-alist nil
34 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
35 NAME is a character (a number). CONTENTS is a string, number,
36 frame configuration, mark or list.
37 A list of strings represents a rectangle.
38 A list of the form (file . NAME) represents the file named NAME.")
39
40 (defun get-register (char)
41 "Return contents of Emacs register named CHAR, or nil if none."
42 (cdr (assq char register-alist)))
43
44 (defun set-register (char value)
45 "Set contents of Emacs register named CHAR to VALUE. Returns VALUE.
46 See the documentation of the variable `register-alist' for possible VALUE."
47 (let ((aelt (assq char register-alist)))
48 (if aelt
49 (setcdr aelt value)
50 (setq aelt (cons char value))
51 (setq register-alist (cons aelt register-alist)))
52 value))
53
54 (defun point-to-register (char &optional arg)
55 "Store current location of point in register REGISTER.
56 With prefix argument, store current frame configuration.
57 Use \\[jump-to-register] to go to that location or restore that configuration.
58 Argument is a character, naming the register."
59 (interactive "cPoint to register: \nP")
60 (set-register char (if arg (current-frame-configuration) (point-marker))))
61
62 (defun window-configuration-to-register (char &optional arg)
63 "Store the window configuration of the selected frame in register REGISTER.
64 Use \\[jump-to-register] to restore the configuration.
65 Argument is a character, naming the register."
66 (interactive "cWindow configuration to register: \nP")
67 (set-register char (current-window-configuration)))
68
69 (defun frame-configuration-to-register (char &optional arg)
70 "Store the window configuration of all frames in register REGISTER.
71 Use \\[jump-to-register] to restore the configuration.
72 Argument is a character, naming the register."
73 (interactive "cFrame configuration to register: \nP")
74 (set-register char (current-frame-configuration)))
75
76 (defalias 'register-to-point 'jump-to-register)
77 (defun jump-to-register (char &optional delete)
78 "Move point to location stored in a register.
79 If the register contains a file name, find that file.
80 \(To put a file name in a register, you must use `set-register'.)
81 If the register contains a window configuration (one frame) or a frame
82 configuration (all frames), restore that frame or all frames accordingly.
83 First argument is a character, naming the register.
84 Optional second arg non-nil (interactively, prefix argument) says to
85 delete any existing frames that the frame configuration doesn't mention.
86 \(Otherwise, these frames are iconified.)"
87 (interactive "cJump to register: \nP")
88 (let ((val (get-register char)))
89 (cond
90 ((and (fboundp 'frame-configuration-p)
91 (frame-configuration-p val))
92 (set-frame-configuration val (not delete)))
93 ((window-configuration-p val)
94 (set-window-configuration val))
95 ((markerp val)
96 (switch-to-buffer (marker-buffer val))
97 (goto-char val))
98 ((and (consp val) (eq (car val) 'file))
99 (find-file (cdr val)))
100 (t
101 (error "Register doesn't contain a buffer position or configuration")))))
102
103 ;(defun number-to-register (arg char)
104 ; "Store a number in a register.
105 ;Two args, NUMBER and REGISTER (a character, naming the register).
106 ;If NUMBER is nil, digits in the buffer following point are read
107 ;to get the number to store.
108 ;Interactively, NUMBER is the prefix arg (none means nil)."
109 ; (interactive "P\ncNumber to register: ")
110 ; (set-register char
111 ; (if arg
112 ; (prefix-numeric-value arg)
113 ; (if (looking-at "[0-9][0-9]*")
114 ; (save-excursion
115 ; (save-restriction
116 ; (narrow-to-region (point)
117 ; (progn (skip-chars-forward "0-9")
118 ; (point)))
119 ; (goto-char (point-min))
120 ; (read (current-buffer))))
121 ; 0))))
122
123 ;(defun increment-register (arg char)
124 ; "Add NUMBER to the contents of register REGISTER.
125 ;Interactively, NUMBER is the prefix arg (none means nil)."
126 ; (interactive "p\ncNumber to register: ")
127 ; (or (integerp (get-register char))
128 ; (error "Register does not contain a number"))
129 ; (set-register char (+ arg (get-register char))))
130
131 (defun view-register (char)
132 "Display what is contained in register named REGISTER.
133 REGISTER is a character."
134 (interactive "cView register: ")
135 (let ((val (get-register char)))
136 (if (null val)
137 (message "Register %s is empty" (single-key-description char))
138 (with-output-to-temp-buffer "*Output*"
139 (princ "Register ")
140 (princ (single-key-description char))
141 (princ " contains ")
142 (cond
143 ((integerp val)
144 (princ val))
145
146 ((markerp val)
147 (princ "a buffer position:\nbuffer ")
148 (princ (buffer-name (marker-buffer val)))
149 (princ ", position ")
150 (princ (+ 0 val)))
151
152 ((window-configuration-p val)
153 (princ "a window configuration."))
154
155 ((frame-configuration-p val)
156 (princ "a frame configuration."))
157
158 ((and (consp val) (eq (car val) 'file))
159 (princ "the file ")
160 (prin1 (cdr val))
161 (princ "."))
162
163 ((consp val)
164 (princ "the rectangle:\n")
165 (while val
166 (princ (car val))
167 (terpri)
168 (setq val (cdr val))))
169
170 ((stringp val)
171 (princ "the text:\n")
172 (princ val))
173
174 (t
175 (princ "Garbage:\n")
176 (prin1 val)))))))
177
178 (defun insert-register (char &optional arg)
179 "Insert contents of register REG. REG is a character.
180 Normally puts point before and mark after the inserted text.
181 If optional second arg is non-nil, puts mark before and point after.
182 Interactively, second arg is non-nil if prefix arg is supplied."
183 (interactive "cInsert register: \nP")
184 (push-mark)
185 (let ((val (get-register char)))
186 (if (consp val)
187 (insert-rectangle val)
188 (if (stringp val)
189 (insert val)
190 (if (or (integerp val) (markerp val))
191 (princ (+ 0 val) (current-buffer))
192 (error "Register does not contain text")))))
193 (if (not arg) (exchange-point-and-mark)))
194
195 (defun copy-to-register (char start end &optional delete-flag)
196 "Copy region into register REG. With prefix arg, delete as well.
197 Called from program, takes four args: REG, START, END and DELETE-FLAG.
198 START and END are buffer positions indicating what to copy."
199 (interactive "cCopy to register: \nr\nP")
200 (set-register char (buffer-substring start end))
201 (if delete-flag (delete-region start end)))
202
203 (defun append-to-register (char start end &optional delete-flag)
204 "Append region to text in register REG. With prefix arg, delete as well.
205 Called from program, takes four args: REG, START, END and DELETE-FLAG.
206 START and END are buffer positions indicating what to append."
207 (interactive "cAppend to register: \nr\nP")
208 (or (stringp (get-register char))
209 (error "Register does not contain text"))
210 (set-register char (concat (get-register char)
211 (buffer-substring start end)))
212 (if delete-flag (delete-region start end)))
213
214 (defun prepend-to-register (char start end &optional delete-flag)
215 "Prepend region to text in register REG. With prefix arg, delete as well.
216 Called from program, takes four args: REG, START, END and DELETE-FLAG.
217 START and END are buffer positions indicating what to prepend."
218 (interactive "cPrepend to register: \nr\nP")
219 (or (stringp (get-register char))
220 (error "Register does not contain text"))
221 (set-register char (concat (buffer-substring start end)
222 (get-register char)))
223 (if delete-flag (delete-region start end)))
224
225 (defun copy-rectangle-to-register (char start end &optional delete-flag)
226 "Copy rectangular region into register REG. With prefix arg, delete as well.
227 Called from program, takes four args: REG, START, END and DELETE-FLAG.
228 START and END are buffer positions giving two corners of rectangle."
229 (interactive "cCopy rectangle to register: \nr\nP")
230 (set-register char
231 (if delete-flag
232 (delete-extract-rectangle start end)
233 (extract-rectangle start end))))
234
235 ;;; register.el ends here