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