1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
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))
35 ;;; Global key bindings
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
)
53 (defvar register-alist nil
54 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
55 NAME is a character (a number). CONTENTS is a string, number, marker or list.
56 A list of strings represents a rectangle.
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
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.")
66 (defun get-register (register)
67 "Return contents of Emacs register named REGISTER, or nil if none."
68 (cdr (assq register register-alist
)))
70 (defun set-register (register value
)
71 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
72 See the documentation of the variable `register-alist' for possible VALUEs."
73 (let ((aelt (assq register register-alist
)))
76 (push (cons register value
) register-alist
))
79 (defun point-to-register (register &optional arg
)
80 "Store current location of point in register REGISTER.
81 With prefix argument, store current frame configuration.
82 Use \\[jump-to-register] to go to that location or restore that configuration.
83 Argument is a character, naming the register."
84 (interactive "cPoint to register: \nP")
85 ;; Turn the marker into a file-ref if the buffer is killed.
86 (add-hook 'kill-buffer-hook
'register-swap-out nil t
)
87 (set-register register
88 (if arg
(list (current-frame-configuration) (point-marker))
91 (defun window-configuration-to-register (register &optional arg
)
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."
95 (interactive "cWindow configuration to register: \nP")
96 ;; current-window-configuration does not include the value
97 ;; of point in the current buffer, so record that separately.
98 (set-register register
(list (current-window-configuration) (point-marker))))
100 (defun frame-configuration-to-register (register &optional arg
)
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."
104 (interactive "cFrame configuration to register: \nP")
105 ;; current-frame-configuration does not include the value
106 ;; of point in the current buffer, so record that separately.
107 (set-register register
(list (current-frame-configuration) (point-marker))))
109 (defalias 'register-to-point
'jump-to-register
)
110 (defun jump-to-register (register &optional delete
)
111 "Move point to location stored in a register.
112 If the register contains a file name, find that file.
113 \(To put a file name in a register, you must use `set-register'.)
114 If the register contains a window configuration (one frame) or a frame
115 configuration (all frames), restore that frame or all frames accordingly.
116 First argument is a character, naming the register.
117 Optional second arg non-nil (interactively, prefix argument) says to
118 delete any existing frames that the frame configuration doesn't mention.
119 \(Otherwise, these frames are iconified.)"
120 (interactive "cJump to register: \nP")
121 (let ((val (get-register register
)))
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
)))
130 (or (marker-buffer val
)
131 (error "That register's buffer no longer exists"))
132 (switch-to-buffer (marker-buffer val
))
134 ((and (consp val
) (eq (car val
) 'file
))
135 (find-file (cdr val
)))
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
)))
142 ((and (fboundp 'semantic-foreign-tag-p
)
144 (semantic-foreign-tag-p val
))
145 (switch-to-buffer (semantic-tag-buffer val
))
146 (goto-char (semantic-tag-start val
)))
148 (error "Register doesn't contain a buffer position or configuration")))))
150 (defun register-swap-out ()
151 "Turn markers into file-query references when a buffer is killed."
152 (and buffer-file-name
153 (dolist (elem register-alist
)
154 (and (markerp (cdr elem
))
155 (eq (marker-buffer (cdr elem
)) (current-buffer))
159 (marker-position (cdr elem
))))))))
161 (defun number-to-register (number register
)
162 "Store a number in a register.
163 Two args, NUMBER and REGISTER (a character, naming the register).
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.
166 Interactively, NUMBER is the prefix arg (none means nil)."
167 (interactive "P\ncNumber to register: ")
168 (set-register register
170 (prefix-numeric-value number
)
171 (if (looking-at "\\s-*-?[0-9]+")
173 (goto-char (match-end 0))
174 (string-to-number (match-string 0)))
177 (defun increment-register (number register
)
178 "Add NUMBER to the contents of register REGISTER.
179 Interactively, NUMBER is the prefix arg."
180 (interactive "p\ncIncrement register: ")
181 (or (numberp (get-register register
))
182 (error "Register does not contain a number"))
183 (set-register register
(+ number
(get-register register
))))
185 (defun view-register (register)
186 "Display what is contained in register named REGISTER.
187 The Lisp value REGISTER is a character."
188 (interactive "cView register: ")
189 (let ((val (get-register register
)))
191 (message "Register %s is empty" (single-key-description register
))
192 (with-output-to-temp-buffer "*Output*"
193 (describe-register-1 register t
)))))
195 (defun list-registers ()
196 "Display a list of nonempty registers saying briefly what they contain."
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*"
202 (when (get-register (car elt
))
203 (describe-register-1 (car elt
))
206 (defun describe-register-1 (register &optional verbose
)
208 (princ (single-key-description register
))
210 (let ((val (get-register register
)))
216 (let ((buf (marker-buffer val
)))
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
)))))
224 ((and (consp val
) (window-configuration-p (car val
)))
225 (princ "a window configuration."))
227 ((and (consp val
) (frame-configuration-p (car val
)))
228 (princ "a frame configuration."))
230 ((and (consp val
) (eq (car val
) 'file
))
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
))))
245 (princ "the rectangle:\n")
250 (setq val
(cdr val
))))
251 (princ "a rectangle starting with ")
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
))
261 (princ "the text:\n")
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))))
269 (princ "text starting with\n ")
270 (princ (match-string 0 val
)))
271 ((string-match "^[ \t\n]+$" val
)
272 (princ "whitespace"))
274 (princ "the empty string")))))
277 (if verbose
(prin1 val
))))))
279 (defun insert-register (register &optional arg
)
280 "Insert contents of register REGISTER. (REGISTER is a character.)
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."
284 (interactive "*cInsert register: \nP")
286 (let ((val (get-register register
)))
289 (insert-rectangle val
))
291 (insert-for-yank val
))
293 (princ val
(current-buffer)))
294 ((and (markerp val
) (marker-position val
))
295 (princ (marker-position val
) (current-buffer)))
296 ((and (fboundp 'semantic-foreign-tag-p
)
298 (semantic-foreign-tag-p val
))
299 (semantic-insert-foreign-tag val
))
301 (error "Register does not contain text"))))
302 (if (not arg
) (exchange-point-and-mark)))
304 (defun copy-to-register (register start end
&optional delete-flag
)
305 "Copy region into register REGISTER.
306 With prefix arg, delete as well.
307 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
308 START and END are buffer positions indicating what to copy."
309 (interactive "cCopy to register: \nr\nP")
310 (set-register register
(filter-buffer-substring start end
))
311 (if delete-flag
(delete-region start end
)))
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.
317 START and END are buffer positions indicating what to append."
318 (interactive "cAppend to register: \nr\nP")
319 (let ((reg (get-register register
))
320 (text (filter-buffer-substring start end
)))
322 register
(cond ((not reg
) text
)
323 ((stringp reg
) (concat reg text
))
324 (t (error "Register does not contain text")))))
325 (if delete-flag
(delete-region start end
)))
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.
331 START and END are buffer positions indicating what to prepend."
332 (interactive "cPrepend to register: \nr\nP")
333 (let ((reg (get-register register
))
334 (text (filter-buffer-substring start end
)))
336 register
(cond ((not reg
) text
)
337 ((stringp reg
) (concat text reg
))
338 (t (error "Register does not contain text")))))
339 (if delete-flag
(delete-region start end
)))
341 (defun copy-rectangle-to-register (register start end
&optional delete-flag
)
342 "Copy rectangular region into register REGISTER.
343 With prefix arg, delete as well.
344 To insert this register in the buffer, use \\[insert-register].
346 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
347 START and END are buffer positions giving two corners of rectangle."
348 (interactive "cCopy rectangle to register: \nr\nP")
349 (set-register register
351 (delete-extract-rectangle start end
)
352 (extract-rectangle start end
))))
355 ;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
356 ;;; register.el ends here