]> code.delx.au - gnu-emacs/blob - lisp/register.el
All fsets changed to defaliases.
[gnu-emacs] / lisp / register.el
1 ;;; register.el --- register commands for Emacs.
2
3 ;; Copyright (C) 1985 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 represents a rectangle; its elements are strings.")
38
39 (defun get-register (char)
40 "Return contents of Emacs register named CHAR, or nil if none."
41 (cdr (assq char register-alist)))
42
43 (defun set-register (char value)
44 "Set contents of Emacs register named CHAR to VALUE. Returns VALUE."
45 (let ((aelt (assq char register-alist)))
46 (if aelt
47 (setcdr aelt value)
48 (setq aelt (cons char value))
49 (setq register-alist (cons aelt register-alist)))
50 value))
51
52 (defun point-to-register (char &optional arg)
53 "Store current location of point in register REGISTER.
54 With prefix argument, store current frame configuration.
55 Use \\[jump-to-register] to go to that location or restore that configuration.
56 Argument is a character, naming the register."
57 (interactive "cPoint to register: \nP")
58 (set-register char (if arg (current-frame-configuration) (point-marker))))
59
60 (defun window-configuration-to-register (char &optional arg)
61 "Store the window configuration of the selected frame in register REGISTER.
62 Use \\[jump-to-register] to restore the configuration.
63 Argument is a character, naming the register."
64 (interactive "cPoint to register: \nP")
65 (set-register char (current-window-configuration)))
66
67 (defun frame-configuration-to-register (char &optional arg)
68 "Store the window configuration of all frames in register REGISTER.
69 Use \\[jump-to-register] to restore the configuration.
70 Argument is a character, naming the register."
71 (interactive "cPoint to register: \nP")
72 (set-register char (current-frame-configuration)))
73
74 (defalias 'register-to-point 'jump-to-register)
75 (defun jump-to-register (char)
76 "Move point to location stored in a register.
77 If the register contains a window configuration (one frame) or a frame
78 configuration (all frames), restore that frame or all frames accordingly.
79 Argument is a character, naming the register."
80 (interactive "cJump to register: ")
81 (let ((val (get-register char)))
82 (cond
83 ((frame-configuration-p val)
84 (set-frame-configuration val))
85 ((window-configuration-p val)
86 (set-window-configuration val))
87 ((markerp val)
88 (switch-to-buffer (marker-buffer val))
89 (goto-char val))
90 (t
91 (error "Register doesn't contain a buffer position or configuration")))))
92
93 ;(defun number-to-register (arg char)
94 ; "Store a number in a register.
95 ;Two args, NUMBER and REGISTER (a character, naming the register).
96 ;If NUMBER is nil, digits in the buffer following point are read
97 ;to get the number to store.
98 ;Interactively, NUMBER is the prefix arg (none means nil)."
99 ; (interactive "P\ncNumber to register: ")
100 ; (set-register char
101 ; (if arg
102 ; (prefix-numeric-value arg)
103 ; (if (looking-at "[0-9][0-9]*")
104 ; (save-excursion
105 ; (save-restriction
106 ; (narrow-to-region (point)
107 ; (progn (skip-chars-forward "0-9")
108 ; (point)))
109 ; (goto-char (point-min))
110 ; (read (current-buffer))))
111 ; 0))))
112
113 ;(defun increment-register (arg char)
114 ; "Add NUMBER to the contents of register REGISTER.
115 ;Interactively, NUMBER is the prefix arg (none means nil)."
116 ; (interactive "p\ncNumber to register: ")
117 ; (or (integerp (get-register char))
118 ; (error "Register does not contain a number"))
119 ; (set-register char (+ arg (get-register char))))
120
121 (defun view-register (char)
122 "Display what is contained in register named REGISTER.
123 REGISTER is a character."
124 (interactive "cView register: ")
125 (let ((val (get-register char)))
126 (if (null val)
127 (message "Register %s is empty" (single-key-description char))
128 (with-output-to-temp-buffer "*Output*"
129 (princ "Register ")
130 (princ (single-key-description char))
131 (princ " contains ")
132 (cond
133 ((integerp val)
134 (princ val))
135
136 ((markerp val)
137 (princ "a buffer position:\nbuffer ")
138 (princ (buffer-name (marker-buffer val)))
139 (princ ", position ")
140 (princ (+ 0 val)))
141
142 ((window-configuration-p val)
143 (princ "a window configuration."))
144
145 ((frame-configuration-p val)
146 (princ "a frame configuration."))
147
148 ((consp val)
149 (princ "the rectangle:\n")
150 (while val
151 (princ (car val))
152 (terpri)
153 (setq val (cdr val))))
154
155 ((stringp val)
156 (princ "the text:\n")
157 (princ val))
158
159 (t
160 (princ "Garbage:\n")
161 (prin1 val)))))))
162
163 (defun insert-register (char &optional arg)
164 "Insert contents of register REG. REG is a character.
165 Normally puts point before and mark after the inserted text.
166 If optional second arg is non-nil, puts mark before and point after.
167 Interactively, second arg is non-nil if prefix arg is supplied."
168 (interactive "cInsert register: \nP")
169 (push-mark)
170 (let ((val (get-register char)))
171 (if (consp val)
172 (insert-rectangle val)
173 (if (stringp val)
174 (insert val)
175 (if (or (integerp val) (markerp val))
176 (princ (+ 0 val) (current-buffer))
177 (error "Register does not contain text")))))
178 (if (not arg) (exchange-point-and-mark)))
179
180 (defun copy-to-register (char start end &optional delete-flag)
181 "Copy region into register REG. With prefix arg, delete as well.
182 Called from program, takes four args: REG, START, END and DELETE-FLAG.
183 START and END are buffer positions indicating what to copy."
184 (interactive "cCopy to register: \nr\nP")
185 (set-register char (buffer-substring start end))
186 (if delete-flag (delete-region start end)))
187
188 (defun append-to-register (char start end &optional delete-flag)
189 "Append region to text in register REG. With prefix arg, delete as well.
190 Called from program, takes four args: REG, START, END and DELETE-FLAG.
191 START and END are buffer positions indicating what to append."
192 (interactive "cAppend to register: \nr\nP")
193 (or (stringp (get-register char))
194 (error "Register does not contain text"))
195 (set-register char (concat (get-register char)
196 (buffer-substring start end)))
197 (if delete-flag (delete-region start end)))
198
199 (defun prepend-to-register (char start end &optional delete-flag)
200 "Prepend region to text in register REG. With prefix arg, delete as well.
201 Called from program, takes four args: REG, START, END and DELETE-FLAG.
202 START and END are buffer positions indicating what to prepend."
203 (interactive "cPrepend to register: \nr\nP")
204 (or (stringp (get-register char))
205 (error "Register does not contain text"))
206 (set-register char (concat (buffer-substring start end)
207 (get-register char)))
208 (if delete-flag (delete-region start end)))
209
210 (defun copy-rectangle-to-register (char start end &optional delete-flag)
211 "Copy rectangular region into register REG. With prefix arg, delete as well.
212 Called from program, takes four args: REG, START, END and DELETE-FLAG.
213 START and END are buffer positions giving two corners of rectangle."
214 (interactive "cCopy rectangle to register: \nr\nP")
215 (set-register char
216 (if delete-flag
217 (delete-extract-rectangle start end)
218 (extract-rectangle start end))))
219
220 ;;; register.el ends here