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