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