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") |
1b8cac5d | 71 | (set-register register (current-window-configuration))) |
83b5d757 | 72 | |
1b8cac5d | 73 | (defun frame-configuration-to-register (register &optional arg) |
83b5d757 RS |
74 | "Store the window configuration of all frames in register REGISTER. |
75 | Use \\[jump-to-register] to restore the configuration. | |
76 | Argument is a character, naming the register." | |
5f517806 | 77 | (interactive "cFrame configuration to register: \nP") |
1b8cac5d | 78 | (set-register register (current-frame-configuration))) |
83b5d757 | 79 | |
31e1d920 | 80 | (defalias 'register-to-point 'jump-to-register) |
1b8cac5d | 81 | (defun jump-to-register (register &optional delete) |
efeae993 | 82 | "Move point to location stored in a register. |
22073dda RS |
83 | If the register contains a file name, find that file. |
84 | \(To put a file name in a register, you must use `set-register'.) | |
83b5d757 RS |
85 | If the register contains a window configuration (one frame) or a frame |
86 | configuration (all frames), restore that frame or all frames accordingly. | |
e7683fff | 87 | First argument is a character, naming the register. |
1542ad37 RS |
88 | Optional second arg non-nil (interactively, prefix argument) says to |
89 | delete any existing frames that the frame configuration doesn't mention. | |
a21d94f9 | 90 | \(Otherwise, these frames are iconified.)" |
e7683fff | 91 | (interactive "cJump to register: \nP") |
1b8cac5d | 92 | (let ((val (get-register register))) |
376a7584 | 93 | (cond |
bf77ce53 RS |
94 | ((and (fboundp 'frame-configuration-p) |
95 | (frame-configuration-p val)) | |
1542ad37 | 96 | (set-frame-configuration val (not delete))) |
376a7584 JB |
97 | ((window-configuration-p val) |
98 | (set-window-configuration val)) | |
99 | ((markerp val) | |
8c4ca60c KH |
100 | (or (marker-buffer val) |
101 | (error "That register's buffer no longer exists")) | |
376a7584 JB |
102 | (switch-to-buffer (marker-buffer val)) |
103 | (goto-char val)) | |
22073dda RS |
104 | ((and (consp val) (eq (car val) 'file)) |
105 | (find-file (cdr val))) | |
28cbd14d RS |
106 | ((and (consp val) (eq (car val) 'file-query)) |
107 | (or (find-buffer-visiting (nth 1 val)) | |
108 | (y-or-n-p (format "Visit file %s again? " (nth 1 val))) | |
109 | (error "Register access aborted")) | |
110 | (find-file (nth 1 val)) | |
111 | (goto-char (nth 2 val))) | |
376a7584 JB |
112 | (t |
113 | (error "Register doesn't contain a buffer position or configuration"))))) | |
efeae993 | 114 | |
28cbd14d RS |
115 | ;; Turn markers into file-query references when a buffer is killed. |
116 | (defun register-swap-out () | |
117 | (and buffer-file-name | |
118 | (let ((tail register-alist)) | |
119 | (while tail | |
120 | (and (markerp (cdr (car tail))) | |
121 | (eq (marker-buffer (cdr (car tail))) (current-buffer)) | |
122 | (setcdr (car tail) | |
123 | (list 'file-query | |
124 | buffer-file-name | |
125 | (marker-position (cdr (car tail)))))) | |
126 | (setq tail (cdr tail)))))) | |
127 | ||
128 | (add-hook 'kill-buffer-hook 'register-swap-out) | |
129 | ||
efeae993 RS |
130 | ;(defun number-to-register (arg char) |
131 | ; "Store a number in a register. | |
132 | ;Two args, NUMBER and REGISTER (a character, naming the register). | |
133 | ;If NUMBER is nil, digits in the buffer following point are read | |
134 | ;to get the number to store. | |
135 | ;Interactively, NUMBER is the prefix arg (none means nil)." | |
136 | ; (interactive "P\ncNumber to register: ") | |
137 | ; (set-register char | |
138 | ; (if arg | |
139 | ; (prefix-numeric-value arg) | |
140 | ; (if (looking-at "[0-9][0-9]*") | |
141 | ; (save-excursion | |
142 | ; (save-restriction | |
143 | ; (narrow-to-region (point) | |
144 | ; (progn (skip-chars-forward "0-9") | |
145 | ; (point))) | |
146 | ; (goto-char (point-min)) | |
147 | ; (read (current-buffer)))) | |
148 | ; 0)))) | |
149 | ||
150 | ;(defun increment-register (arg char) | |
151 | ; "Add NUMBER to the contents of register REGISTER. | |
152 | ;Interactively, NUMBER is the prefix arg (none means nil)." | |
153 | ; (interactive "p\ncNumber to register: ") | |
154 | ; (or (integerp (get-register char)) | |
155 | ; (error "Register does not contain a number")) | |
156 | ; (set-register char (+ arg (get-register char)))) | |
157 | ||
1b8cac5d | 158 | (defun view-register (register) |
efeae993 | 159 | "Display what is contained in register named REGISTER. |
1b8cac5d | 160 | The Lisp value REGISTER is a character." |
efeae993 | 161 | (interactive "cView register: ") |
1b8cac5d | 162 | (let ((val (get-register register))) |
efeae993 | 163 | (if (null val) |
1b8cac5d | 164 | (message "Register %s is empty" (single-key-description register)) |
efeae993 RS |
165 | (with-output-to-temp-buffer "*Output*" |
166 | (princ "Register ") | |
1b8cac5d | 167 | (princ (single-key-description register)) |
efeae993 | 168 | (princ " contains ") |
ed83b192 JB |
169 | (cond |
170 | ((integerp val) | |
171 | (princ val)) | |
172 | ||
173 | ((markerp val) | |
cbd4993c KH |
174 | (let ((buf (marker-buffer val))) |
175 | (if (null buf) | |
176 | (princ "a marker in no buffer") | |
177 | (princ "a buffer position:\nbuffer ") | |
178 | (princ (buffer-name buf)) | |
179 | (princ ", position ") | |
180 | (princ (marker-position val))))) | |
ed83b192 JB |
181 | |
182 | ((window-configuration-p val) | |
183 | (princ "a window configuration.")) | |
184 | ||
185 | ((frame-configuration-p val) | |
186 | (princ "a frame configuration.")) | |
187 | ||
6bfb7922 RS |
188 | ((and (consp val) (eq (car val) 'file)) |
189 | (princ "the file ") | |
190 | (prin1 (cdr val)) | |
191 | (princ ".")) | |
192 | ||
ed83b192 JB |
193 | ((consp val) |
194 | (princ "the rectangle:\n") | |
195 | (while val | |
196 | (princ (car val)) | |
197 | (terpri) | |
198 | (setq val (cdr val)))) | |
199 | ||
200 | ((stringp val) | |
201 | (princ "the text:\n") | |
202 | (princ val)) | |
203 | ||
204 | (t | |
205 | (princ "Garbage:\n") | |
206 | (prin1 val))))))) | |
efeae993 | 207 | |
1b8cac5d RS |
208 | (defun insert-register (register &optional arg) |
209 | "Insert contents of register REGISTER. (REGISTER is a character.) | |
efeae993 RS |
210 | Normally puts point before and mark after the inserted text. |
211 | If optional second arg is non-nil, puts mark before and point after. | |
212 | Interactively, second arg is non-nil if prefix arg is supplied." | |
ecfc7eb3 | 213 | (interactive "*cInsert register: \nP") |
efeae993 | 214 | (push-mark) |
1b8cac5d | 215 | (let ((val (get-register register))) |
cbd4993c KH |
216 | (cond |
217 | ((consp val) | |
218 | (insert-rectangle val)) | |
219 | ((stringp val) | |
220 | (insert val)) | |
221 | ((integerp val) | |
222 | (princ val (current-buffer))) | |
223 | ((and (markerp val) (marker-position val)) | |
224 | (princ (marker-position val) (current-buffer))) | |
225 | (t | |
226 | (error "Register does not contain text")))) | |
efeae993 RS |
227 | (if (not arg) (exchange-point-and-mark))) |
228 | ||
1b8cac5d RS |
229 | (defun copy-to-register (register start end &optional delete-flag) |
230 | "Copy region into register REGISTER. With prefix arg, delete as well. | |
231 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
232 | START and END are buffer positions indicating what to copy." |
233 | (interactive "cCopy to register: \nr\nP") | |
1b8cac5d | 234 | (set-register register (buffer-substring start end)) |
efeae993 RS |
235 | (if delete-flag (delete-region start end))) |
236 | ||
1b8cac5d RS |
237 | (defun append-to-register (register start end &optional delete-flag) |
238 | "Append region to text in register REGISTER. | |
239 | With prefix arg, delete as well. | |
240 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
241 | START and END are buffer positions indicating what to append." |
242 | (interactive "cAppend to register: \nr\nP") | |
1b8cac5d | 243 | (or (stringp (get-register register)) |
efeae993 | 244 | (error "Register does not contain text")) |
1b8cac5d RS |
245 | (set-register register (concat (get-register register) |
246 | (buffer-substring start end))) | |
efeae993 RS |
247 | (if delete-flag (delete-region start end))) |
248 | ||
1b8cac5d RS |
249 | (defun prepend-to-register (register start end &optional delete-flag) |
250 | "Prepend region to text in register REGISTER. | |
251 | With prefix arg, delete as well. | |
252 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
253 | START and END are buffer positions indicating what to prepend." |
254 | (interactive "cPrepend to register: \nr\nP") | |
1b8cac5d | 255 | (or (stringp (get-register register)) |
efeae993 | 256 | (error "Register does not contain text")) |
1b8cac5d RS |
257 | (set-register register (concat (buffer-substring start end) |
258 | (get-register register))) | |
efeae993 RS |
259 | (if delete-flag (delete-region start end))) |
260 | ||
1b8cac5d RS |
261 | (defun copy-rectangle-to-register (register start end &optional delete-flag) |
262 | "Copy rectangular region into register REGISTER. | |
263 | With prefix arg, delete as well. | |
264 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
265 | START and END are buffer positions giving two corners of rectangle." |
266 | (interactive "cCopy rectangle to register: \nr\nP") | |
1b8cac5d | 267 | (set-register register |
efeae993 RS |
268 | (if delete-flag |
269 | (delete-extract-rectangle start end) | |
270 | (extract-rectangle start end)))) | |
c88ab9ce ER |
271 | |
272 | ;;; register.el ends here |