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