Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; register.el --- register commands for Emacs. |
2 | ||
9750e079 ER |
3 | ;; Copyright (C) 1985 Free Software Foundation, Inc. |
4 | ||
4821e2af | 5 | ;; Maintainer: FSF |
d7b4d18f | 6 | ;; Keywords: internal |
4821e2af | 7 | |
efeae993 RS |
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 | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
efeae993 RS |
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 | ||
d9ecc911 ER |
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 | ||
4821e2af | 31 | ;;; Code: |
efeae993 RS |
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, | |
0cc89026 | 36 | frame configuration, mark or list. |
b42e6156 | 37 | A list represents a rectangle; its elements are strings.") |
efeae993 RS |
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) | |
14c5b721 | 44 | "Set contents of Emacs register named CHAR to VALUE. Returns VALUE." |
efeae993 RS |
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 | ||
a8a6776b | 52 | (defun point-to-register (char &optional arg) |
b42e6156 | 53 | "Store current location of point in register REGISTER. |
0cc89026 | 54 | With prefix argument, store current frame configuration. |
b42e6156 | 55 | Use \\[jump-to-register] to go to that location or restore that configuration. |
efeae993 | 56 | Argument is a character, naming the register." |
b42e6156 | 57 | (interactive "cPoint to register: \nP") |
0cc89026 | 58 | (set-register char (if arg (current-frame-configuration) (point-marker)))) |
efeae993 | 59 | |
a8a6776b | 60 | (defun window-configuration-to-register (char &optional arg) |
83b5d757 RS |
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 | ||
a8a6776b | 67 | (defun frame-configuration-to-register (char &optional arg) |
83b5d757 RS |
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 | ||
31e1d920 | 74 | (defalias 'register-to-point 'jump-to-register) |
efeae993 RS |
75 | (defun jump-to-register (char) |
76 | "Move point to location stored in a register. | |
83b5d757 RS |
77 | If the register contains a window configuration (one frame) or a frame |
78 | configuration (all frames), restore that frame or all frames accordingly. | |
efeae993 RS |
79 | Argument is a character, naming the register." |
80 | (interactive "cJump to register: ") | |
81 | (let ((val (get-register char))) | |
376a7584 JB |
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"))))) | |
efeae993 RS |
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 ") | |
ed83b192 JB |
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))))))) | |
efeae993 RS |
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) | |
14c5b721 JB |
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. | |
efeae993 RS |
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) | |
14c5b721 JB |
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. | |
efeae993 RS |
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) | |
14c5b721 JB |
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. | |
efeae993 RS |
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) | |
14c5b721 JB |
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. | |
efeae993 RS |
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)))) | |
c88ab9ce ER |
219 | |
220 | ;;; register.el ends here |