Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; register.el --- register commands for Emacs |
c88ab9ce | 2 | |
c90f2757 | 3 | ;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
9750e079 | 5 | |
4821e2af | 6 | ;; Maintainer: FSF |
d7b4d18f | 7 | ;; Keywords: internal |
4821e2af | 8 | |
efeae993 RS |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
efeae993 RS |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
efeae993 | 25 | |
d9ecc911 ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; This package of functions emulates and somewhat extends the venerable | |
29 | ;; TECO's `register' feature, which permits you to save various useful | |
30 | ;; pieces of buffer state to named variables. The entry points are | |
31 | ;; documented in the Emacs user's manual. | |
32 | ||
4821e2af | 33 | ;;; Code: |
efeae993 RS |
34 | |
35 | (defvar register-alist nil | |
36 | "Alist of elements (NAME . CONTENTS), one for each Emacs register. | |
070c2506 | 37 | NAME is a character (a number). CONTENTS is a string, number, marker 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 | |
070c2506 KH |
41 | in the file named NAME, but query before visiting it. |
42 | A list of the form (WINDOW-CONFIGURATION POSITION) | |
43 | represents a saved window configuration plus a saved value of point. | |
44 | A list of the form (FRAME-CONFIGURATION POSITION) | |
45 | represents a saved frame configuration plus a saved value of point.") | |
efeae993 | 46 | |
1b8cac5d RS |
47 | (defun get-register (reg) |
48 | "Return contents of Emacs register named REG, or nil if none." | |
49 | (cdr (assq reg register-alist))) | |
efeae993 | 50 | |
1b8cac5d RS |
51 | (defun set-register (register value) |
52 | "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE. | |
22073dda | 53 | See the documentation of the variable `register-alist' for possible VALUE." |
1b8cac5d | 54 | (let ((aelt (assq register register-alist))) |
efeae993 RS |
55 | (if aelt |
56 | (setcdr aelt value) | |
ddbb3cc7 | 57 | (push (cons register value) register-alist)) |
efeae993 RS |
58 | value)) |
59 | ||
1b8cac5d | 60 | (defun point-to-register (register &optional arg) |
b42e6156 | 61 | "Store current location of point in register REGISTER. |
0cc89026 | 62 | With prefix argument, store current frame configuration. |
b42e6156 | 63 | Use \\[jump-to-register] to go to that location or restore that configuration. |
efeae993 | 64 | Argument is a character, naming the register." |
b42e6156 | 65 | (interactive "cPoint to register: \nP") |
ddbb3cc7 SM |
66 | ;; Turn the marker into a file-ref if the buffer is killed. |
67 | (add-hook 'kill-buffer-hook 'register-swap-out nil t) | |
1b8cac5d | 68 | (set-register register |
070c2506 KH |
69 | (if arg (list (current-frame-configuration) (point-marker)) |
70 | (point-marker)))) | |
efeae993 | 71 | |
1b8cac5d | 72 | (defun window-configuration-to-register (register &optional arg) |
83b5d757 RS |
73 | "Store the window configuration of the selected frame in register REGISTER. |
74 | Use \\[jump-to-register] to restore the configuration. | |
75 | Argument is a character, naming the register." | |
5f517806 | 76 | (interactive "cWindow configuration to register: \nP") |
b4a91f43 KH |
77 | ;; current-window-configuration does not include the value |
78 | ;; of point in the current buffer, so record that separately. | |
070c2506 | 79 | (set-register register (list (current-window-configuration) (point-marker)))) |
83b5d757 | 80 | |
1b8cac5d | 81 | (defun frame-configuration-to-register (register &optional arg) |
83b5d757 RS |
82 | "Store the window configuration of all frames in register REGISTER. |
83 | Use \\[jump-to-register] to restore the configuration. | |
84 | Argument is a character, naming the register." | |
5f517806 | 85 | (interactive "cFrame configuration to register: \nP") |
b4a91f43 KH |
86 | ;; current-frame-configuration does not include the value |
87 | ;; of point in the current buffer, so record that separately. | |
070c2506 | 88 | (set-register register (list (current-frame-configuration) (point-marker)))) |
83b5d757 | 89 | |
31e1d920 | 90 | (defalias 'register-to-point 'jump-to-register) |
1b8cac5d | 91 | (defun jump-to-register (register &optional delete) |
efeae993 | 92 | "Move point to location stored in a register. |
22073dda RS |
93 | If the register contains a file name, find that file. |
94 | \(To put a file name in a register, you must use `set-register'.) | |
83b5d757 RS |
95 | If the register contains a window configuration (one frame) or a frame |
96 | configuration (all frames), restore that frame or all frames accordingly. | |
e7683fff | 97 | First argument is a character, naming the register. |
1542ad37 RS |
98 | Optional second arg non-nil (interactively, prefix argument) says to |
99 | delete any existing frames that the frame configuration doesn't mention. | |
a21d94f9 | 100 | \(Otherwise, these frames are iconified.)" |
e7683fff | 101 | (interactive "cJump to register: \nP") |
1b8cac5d | 102 | (let ((val (get-register register))) |
376a7584 | 103 | (cond |
b4a91f43 KH |
104 | ((and (consp val) (frame-configuration-p (car val))) |
105 | (set-frame-configuration (car val) (not delete)) | |
106 | (goto-char (cadr val))) | |
107 | ((and (consp val) (window-configuration-p (car val))) | |
108 | (set-window-configuration (car val)) | |
109 | (goto-char (cadr val))) | |
376a7584 | 110 | ((markerp val) |
8c4ca60c KH |
111 | (or (marker-buffer val) |
112 | (error "That register's buffer no longer exists")) | |
376a7584 JB |
113 | (switch-to-buffer (marker-buffer val)) |
114 | (goto-char val)) | |
22073dda RS |
115 | ((and (consp val) (eq (car val) 'file)) |
116 | (find-file (cdr val))) | |
28cbd14d RS |
117 | ((and (consp val) (eq (car val) 'file-query)) |
118 | (or (find-buffer-visiting (nth 1 val)) | |
119 | (y-or-n-p (format "Visit file %s again? " (nth 1 val))) | |
120 | (error "Register access aborted")) | |
121 | (find-file (nth 1 val)) | |
122 | (goto-char (nth 2 val))) | |
376a7584 JB |
123 | (t |
124 | (error "Register doesn't contain a buffer position or configuration"))))) | |
efeae993 | 125 | |
28cbd14d | 126 | (defun register-swap-out () |
ddbb3cc7 | 127 | "Turn markers into file-query references when a buffer is killed." |
28cbd14d | 128 | (and buffer-file-name |
ddbb3cc7 SM |
129 | (dolist (elem register-alist) |
130 | (and (markerp (cdr elem)) | |
131 | (eq (marker-buffer (cdr elem)) (current-buffer)) | |
132 | (setcdr elem | |
133 | (list 'file-query | |
134 | buffer-file-name | |
135 | (marker-position (cdr elem)))))))) | |
28cbd14d | 136 | |
0e07a458 | 137 | (defun number-to-register (number register) |
070c2506 KH |
138 | "Store a number in a register. |
139 | Two args, NUMBER and REGISTER (a character, naming the register). | |
4d2caa07 KH |
140 | If NUMBER is nil, a decimal number is read from the buffer starting |
141 | at point, and point moves to the end of that number. | |
070c2506 KH |
142 | Interactively, NUMBER is the prefix arg (none means nil)." |
143 | (interactive "P\ncNumber to register: ") | |
4f23d31c | 144 | (set-register register |
0e07a458 KH |
145 | (if number |
146 | (prefix-numeric-value number) | |
4d2caa07 KH |
147 | (if (looking-at "\\s-*-?[0-9]+") |
148 | (progn | |
149 | (goto-char (match-end 0)) | |
fe22eed0 | 150 | (string-to-number (match-string 0))) |
070c2506 KH |
151 | 0)))) |
152 | ||
0e07a458 | 153 | (defun increment-register (number register) |
070c2506 | 154 | "Add NUMBER to the contents of register REGISTER. |
0e07a458 | 155 | Interactively, NUMBER is the prefix arg." |
070c2506 | 156 | (interactive "p\ncIncrement register: ") |
0e07a458 | 157 | (or (numberp (get-register register)) |
070c2506 | 158 | (error "Register does not contain a number")) |
0e07a458 | 159 | (set-register register (+ number (get-register register)))) |
efeae993 | 160 | |
1b8cac5d | 161 | (defun view-register (register) |
efeae993 | 162 | "Display what is contained in register named REGISTER. |
1b8cac5d | 163 | The Lisp value REGISTER is a character." |
efeae993 | 164 | (interactive "cView register: ") |
1b8cac5d | 165 | (let ((val (get-register register))) |
efeae993 | 166 | (if (null val) |
1b8cac5d | 167 | (message "Register %s is empty" (single-key-description register)) |
efeae993 | 168 | (with-output-to-temp-buffer "*Output*" |
92840a31 RS |
169 | (describe-register-1 register t))))) |
170 | ||
171 | (defun list-registers () | |
172 | "Display a list of nonempty registers saying briefly what they contain." | |
173 | (interactive) | |
174 | (let ((list (copy-sequence register-alist))) | |
175 | (setq list (sort list (lambda (a b) (< (car a) (car b))))) | |
176 | (with-output-to-temp-buffer "*Output*" | |
177 | (dolist (elt list) | |
178 | (when (get-register (car elt)) | |
179 | (describe-register-1 (car elt)) | |
180 | (terpri)))))) | |
181 | ||
182 | (defun describe-register-1 (register &optional verbose) | |
183 | (princ "Register ") | |
184 | (princ (single-key-description register)) | |
185 | (princ " contains ") | |
6ed21409 RS |
186 | (let ((val (get-register register))) |
187 | (cond | |
188 | ((numberp val) | |
189 | (princ val)) | |
190 | ||
191 | ((markerp val) | |
192 | (let ((buf (marker-buffer val))) | |
193 | (if (null buf) | |
194 | (princ "a marker in no buffer") | |
195 | (princ "a buffer position:\n buffer ") | |
196 | (princ (buffer-name buf)) | |
197 | (princ ", position ") | |
198 | (princ (marker-position val))))) | |
199 | ||
200 | ((and (consp val) (window-configuration-p (car val))) | |
201 | (princ "a window configuration.")) | |
202 | ||
203 | ((and (consp val) (frame-configuration-p (car val))) | |
204 | (princ "a frame configuration.")) | |
205 | ||
206 | ((and (consp val) (eq (car val) 'file)) | |
207 | (princ "the file ") | |
208 | (prin1 (cdr val)) | |
209 | (princ ".")) | |
210 | ||
211 | ((and (consp val) (eq (car val) 'file-query)) | |
212 | (princ "a file-query reference:\n file ") | |
213 | (prin1 (car (cdr val))) | |
214 | (princ ",\n position ") | |
215 | (princ (car (cdr (cdr val)))) | |
216 | (princ ".")) | |
217 | ||
218 | ((consp val) | |
219 | (if verbose | |
220 | (progn | |
221 | (princ "the rectangle:\n") | |
222 | (while val | |
223 | (princ " ") | |
224 | (princ (car val)) | |
225 | (terpri) | |
226 | (setq val (cdr val)))) | |
227 | (princ "a rectangle starting with ") | |
228 | (princ (car val)))) | |
229 | ||
230 | ((stringp val) | |
9c7cc04b RS |
231 | (if (eq yank-excluded-properties t) |
232 | (set-text-properties 0 (length val) nil val) | |
233 | (remove-list-of-text-properties 0 (length val) | |
234 | yank-excluded-properties val)) | |
6ed21409 RS |
235 | (if verbose |
236 | (progn | |
237 | (princ "the text:\n") | |
238 | (princ val)) | |
f1180544 | 239 | (cond |
13d6f302 RS |
240 | ;; Extract first N characters starting with first non-whitespace. |
241 | ((string-match (format "[^ \t\n].\\{,%d\\}" | |
242 | ;; Deduct 6 for the spaces inserted below. | |
243 | (min 20 (max 0 (- (window-width) 6)))) | |
244 | val) | |
245 | (princ "text starting with\n ") | |
246 | (princ (match-string 0 val))) | |
247 | ((string-match "^[ \t\n]+$" val) | |
248 | (princ "whitespace")) | |
249 | (t | |
250 | (princ "the empty string"))))) | |
6ed21409 RS |
251 | (t |
252 | (princ "Garbage:\n") | |
253 | (if verbose (prin1 val)))))) | |
efeae993 | 254 | |
1b8cac5d RS |
255 | (defun insert-register (register &optional arg) |
256 | "Insert contents of register REGISTER. (REGISTER is a character.) | |
efeae993 RS |
257 | Normally puts point before and mark after the inserted text. |
258 | If optional second arg is non-nil, puts mark before and point after. | |
259 | Interactively, second arg is non-nil if prefix arg is supplied." | |
ecfc7eb3 | 260 | (interactive "*cInsert register: \nP") |
efeae993 | 261 | (push-mark) |
1b8cac5d | 262 | (let ((val (get-register register))) |
cbd4993c KH |
263 | (cond |
264 | ((consp val) | |
265 | (insert-rectangle val)) | |
266 | ((stringp val) | |
e7c765c3 | 267 | (insert-for-yank val)) |
070c2506 | 268 | ((numberp val) |
cbd4993c KH |
269 | (princ val (current-buffer))) |
270 | ((and (markerp val) (marker-position val)) | |
271 | (princ (marker-position val) (current-buffer))) | |
272 | (t | |
273 | (error "Register does not contain text")))) | |
efeae993 RS |
274 | (if (not arg) (exchange-point-and-mark))) |
275 | ||
1b8cac5d RS |
276 | (defun copy-to-register (register start end &optional delete-flag) |
277 | "Copy region into register REGISTER. With prefix arg, delete as well. | |
278 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
279 | START and END are buffer positions indicating what to copy." |
280 | (interactive "cCopy to register: \nr\nP") | |
13191e32 | 281 | (set-register register (filter-buffer-substring start end)) |
efeae993 RS |
282 | (if delete-flag (delete-region start end))) |
283 | ||
1b8cac5d RS |
284 | (defun append-to-register (register start end &optional delete-flag) |
285 | "Append region to text in register REGISTER. | |
286 | With prefix arg, delete as well. | |
287 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
288 | START and END are buffer positions indicating what to append." |
289 | (interactive "cAppend to register: \nr\nP") | |
1b8cac5d | 290 | (or (stringp (get-register register)) |
efeae993 | 291 | (error "Register does not contain text")) |
1b8cac5d | 292 | (set-register register (concat (get-register register) |
13191e32 | 293 | (filter-buffer-substring start end))) |
efeae993 RS |
294 | (if delete-flag (delete-region start end))) |
295 | ||
1b8cac5d RS |
296 | (defun prepend-to-register (register start end &optional delete-flag) |
297 | "Prepend region to text in register REGISTER. | |
298 | With prefix arg, delete as well. | |
299 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
300 | START and END are buffer positions indicating what to prepend." |
301 | (interactive "cPrepend to register: \nr\nP") | |
1b8cac5d | 302 | (or (stringp (get-register register)) |
efeae993 | 303 | (error "Register does not contain text")) |
13191e32 | 304 | (set-register register (concat (filter-buffer-substring start end) |
1b8cac5d | 305 | (get-register register))) |
efeae993 RS |
306 | (if delete-flag (delete-region start end))) |
307 | ||
1b8cac5d RS |
308 | (defun copy-rectangle-to-register (register start end &optional delete-flag) |
309 | "Copy rectangular region into register REGISTER. | |
5ef5d6ce RS |
310 | With prefix arg, delete as well. To insert this register |
311 | in the buffer, use \\[insert-register]. | |
312 | ||
313 | Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
efeae993 RS |
314 | START and END are buffer positions giving two corners of rectangle." |
315 | (interactive "cCopy rectangle to register: \nr\nP") | |
1b8cac5d | 316 | (set-register register |
efeae993 RS |
317 | (if delete-flag |
318 | (delete-extract-rectangle start end) | |
319 | (extract-rectangle start end)))) | |
c88ab9ce | 320 | |
0f214cdf | 321 | (provide 'register) |
ab5796a9 | 322 | ;;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035 |
c88ab9ce | 323 | ;;; register.el ends here |