Fix commit 2011-07-01T01:34:38Z!ueno@unixuser.org.
[emacs.git] / lisp / register.el
blob82a0cf33c3e0be7099c567ad9b81a79f1ca665c7
1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993-1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
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 (eval-when-compile (require 'cl))
33 (declare-function semantic-insert-foreign-tag "semantic/tag" (foreign-tag))
34 (declare-function semantic-tag-buffer "semantic/tag" (tag))
35 (declare-function semantic-tag-start "semantic/tag" (tag))
37 ;;; Global key bindings
39 (define-key ctl-x-r-map "\C-@" 'point-to-register)
40 (define-key ctl-x-r-map [?\C-\ ] 'point-to-register)
41 (define-key ctl-x-r-map " " 'point-to-register)
42 (define-key ctl-x-r-map "j" 'jump-to-register)
43 (define-key ctl-x-r-map "s" 'copy-to-register)
44 (define-key ctl-x-r-map "x" 'copy-to-register)
45 (define-key ctl-x-r-map "i" 'insert-register)
46 (define-key ctl-x-r-map "g" 'insert-register)
47 (define-key ctl-x-r-map "r" 'copy-rectangle-to-register)
48 (define-key ctl-x-r-map "n" 'number-to-register)
49 (define-key ctl-x-r-map "+" 'increment-register)
50 (define-key ctl-x-r-map "w" 'window-configuration-to-register)
51 (define-key ctl-x-r-map "f" 'frame-configuration-to-register)
53 ;;; Code:
55 (defstruct
56 (registerv (:constructor nil)
57 (:constructor registerv--make (&optional data print-func
58 jump-func insert-func))
59 (:copier nil)
60 (:type vector)
61 :named)
62 (data nil :read-only t)
63 (print-func nil :read-only t)
64 (jump-func nil :read-only t)
65 (insert-func nil :read-only t))
67 (defun* registerv-make (data &key print-func jump-func insert-func)
68 "Create a register value object.
70 DATA can be any value.
71 PRINT-FUNC if provided controls how `list-registers' and
72 `view-register' print the register. It should be a function
73 recieving one argument DATA and print text that completes
74 this sentence:
75 Register X contains [TEXT PRINTED BY PRINT-FUNC]
76 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
77 INSERT-FUNC if provided, controls how `insert-register' insert the register.
78 They both receive DATA as argument."
79 (registerv--make data print-func jump-func insert-func))
81 (defvar register-alist nil
82 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
83 NAME is a character (a number). CONTENTS is a string, number, marker, list
84 or a struct returned by `registerv-make'.
85 A list of strings represents a rectangle.
86 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
87 A list of the form (file-query FILE-NAME POSITION) represents
88 position POSITION in the file named FILE-NAME, but query before
89 visiting it.
90 A list of the form (WINDOW-CONFIGURATION POSITION)
91 represents a saved window configuration plus a saved value of point.
92 A list of the form (FRAME-CONFIGURATION POSITION)
93 represents a saved frame configuration plus a saved value of point.")
95 (defun get-register (register)
96 "Return contents of Emacs register named REGISTER, or nil if none."
97 (cdr (assq register register-alist)))
99 (defun set-register (register value)
100 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
101 See the documentation of the variable `register-alist' for possible VALUEs."
102 (let ((aelt (assq register register-alist)))
103 (if aelt
104 (setcdr aelt value)
105 (push (cons register value) register-alist))
106 value))
108 (defun point-to-register (register &optional arg)
109 "Store current location of point in register REGISTER.
110 With prefix argument, store current frame configuration.
111 Use \\[jump-to-register] to go to that location or restore that configuration.
112 Argument is a character, naming the register."
113 (interactive "cPoint to register: \nP")
114 ;; Turn the marker into a file-ref if the buffer is killed.
115 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
116 (set-register register
117 (if arg (list (current-frame-configuration) (point-marker))
118 (point-marker))))
120 (defun window-configuration-to-register (register &optional _arg)
121 "Store the window configuration of the selected frame in register REGISTER.
122 Use \\[jump-to-register] to restore the configuration.
123 Argument is a character, naming the register."
124 (interactive "cWindow configuration to register: \nP")
125 ;; current-window-configuration does not include the value
126 ;; of point in the current buffer, so record that separately.
127 (set-register register (list (current-window-configuration) (point-marker))))
129 (defun frame-configuration-to-register (register &optional _arg)
130 "Store the window configuration of all frames in register REGISTER.
131 Use \\[jump-to-register] to restore the configuration.
132 Argument is a character, naming the register."
133 (interactive "cFrame configuration to register: \nP")
134 ;; current-frame-configuration does not include the value
135 ;; of point in the current buffer, so record that separately.
136 (set-register register (list (current-frame-configuration) (point-marker))))
138 (defalias 'register-to-point 'jump-to-register)
139 (defun jump-to-register (register &optional delete)
140 "Move point to location stored in a register.
141 If the register contains a file name, find that file.
142 \(To put a file name in a register, you must use `set-register'.)
143 If the register contains a window configuration (one frame) or a frame
144 configuration (all frames), restore that frame or all frames accordingly.
145 First argument is a character, naming the register.
146 Optional second arg non-nil (interactively, prefix argument) says to
147 delete any existing frames that the frame configuration doesn't mention.
148 \(Otherwise, these frames are iconified.)"
149 (interactive "cJump to register: \nP")
150 (let ((val (get-register register)))
151 (cond
152 ((registerv-p val)
153 (assert (registerv-jump-func val) nil
154 "Don't know how to jump to register %s"
155 (single-key-description register))
156 (funcall (registerv-jump-func val) (registerv-data val)))
157 ((and (consp val) (frame-configuration-p (car val)))
158 (set-frame-configuration (car val) (not delete))
159 (goto-char (cadr val)))
160 ((and (consp val) (window-configuration-p (car val)))
161 (set-window-configuration (car val))
162 (goto-char (cadr val)))
163 ((markerp val)
164 (or (marker-buffer val)
165 (error "That register's buffer no longer exists"))
166 (switch-to-buffer (marker-buffer val))
167 (goto-char val))
168 ((and (consp val) (eq (car val) 'file))
169 (find-file (cdr val)))
170 ((and (consp val) (eq (car val) 'file-query))
171 (or (find-buffer-visiting (nth 1 val))
172 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
173 (error "Register access aborted"))
174 (find-file (nth 1 val))
175 (goto-char (nth 2 val)))
176 ((and (fboundp 'semantic-foreign-tag-p)
177 semantic-mode
178 (semantic-foreign-tag-p val))
179 (switch-to-buffer (semantic-tag-buffer val))
180 (goto-char (semantic-tag-start val)))
182 (error "Register doesn't contain a buffer position or configuration")))))
184 (defun register-swap-out ()
185 "Turn markers into file-query references when a buffer is killed."
186 (and buffer-file-name
187 (dolist (elem register-alist)
188 (and (markerp (cdr elem))
189 (eq (marker-buffer (cdr elem)) (current-buffer))
190 (setcdr elem
191 (list 'file-query
192 buffer-file-name
193 (marker-position (cdr elem))))))))
195 (defun number-to-register (number register)
196 "Store a number in a register.
197 Two args, NUMBER and REGISTER (a character, naming the register).
198 If NUMBER is nil, a decimal number is read from the buffer starting
199 at point, and point moves to the end of that number.
200 Interactively, NUMBER is the prefix arg (none means nil)."
201 (interactive "P\ncNumber to register: ")
202 (set-register register
203 (if number
204 (prefix-numeric-value number)
205 (if (looking-at "\\s-*-?[0-9]+")
206 (progn
207 (goto-char (match-end 0))
208 (string-to-number (match-string 0)))
209 0))))
211 (defun increment-register (number register)
212 "Add NUMBER to the contents of register REGISTER.
213 Interactively, NUMBER is the prefix arg."
214 (interactive "p\ncIncrement register: ")
215 (or (numberp (get-register register))
216 (error "Register does not contain a number"))
217 (set-register register (+ number (get-register register))))
219 (defun view-register (register)
220 "Display what is contained in register named REGISTER.
221 The Lisp value REGISTER is a character."
222 (interactive "cView register: ")
223 (let ((val (get-register register)))
224 (if (null val)
225 (message "Register %s is empty" (single-key-description register))
226 (with-output-to-temp-buffer "*Output*"
227 (describe-register-1 register t)))))
229 (defun list-registers ()
230 "Display a list of nonempty registers saying briefly what they contain."
231 (interactive)
232 (let ((list (copy-sequence register-alist)))
233 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
234 (with-output-to-temp-buffer "*Output*"
235 (dolist (elt list)
236 (when (get-register (car elt))
237 (describe-register-1 (car elt))
238 (terpri))))))
240 (defun describe-register-1 (register &optional verbose)
241 (princ "Register ")
242 (princ (single-key-description register))
243 (princ " contains ")
244 (let ((val (get-register register)))
245 (cond
246 ((registerv-p val)
247 (if (registerv-print-func val)
248 (funcall (registerv-print-func val) (registerv-data val))
249 (princ "[UNPRINTABLE CONTENTS].")))
251 ((numberp val)
252 (princ val))
254 ((markerp val)
255 (let ((buf (marker-buffer val)))
256 (if (null buf)
257 (princ "a marker in no buffer")
258 (princ "a buffer position:\n buffer ")
259 (princ (buffer-name buf))
260 (princ ", position ")
261 (princ (marker-position val)))))
263 ((and (consp val) (window-configuration-p (car val)))
264 (princ "a window configuration."))
266 ((and (consp val) (frame-configuration-p (car val)))
267 (princ "a frame configuration."))
269 ((and (consp val) (eq (car val) 'file))
270 (princ "the file ")
271 (prin1 (cdr val))
272 (princ "."))
274 ((and (consp val) (eq (car val) 'file-query))
275 (princ "a file-query reference:\n file ")
276 (prin1 (car (cdr val)))
277 (princ ",\n position ")
278 (princ (car (cdr (cdr val))))
279 (princ "."))
281 ((consp val)
282 (if verbose
283 (progn
284 (princ "the rectangle:\n")
285 (while val
286 (princ " ")
287 (princ (car val))
288 (terpri)
289 (setq val (cdr val))))
290 (princ "a rectangle starting with ")
291 (princ (car val))))
293 ((stringp val)
294 (if (eq yank-excluded-properties t)
295 (set-text-properties 0 (length val) nil val)
296 (remove-list-of-text-properties 0 (length val)
297 yank-excluded-properties val))
298 (if verbose
299 (progn
300 (princ "the text:\n")
301 (princ val))
302 (cond
303 ;; Extract first N characters starting with first non-whitespace.
304 ((string-match (format "[^ \t\n].\\{,%d\\}"
305 ;; Deduct 6 for the spaces inserted below.
306 (min 20 (max 0 (- (window-width) 6))))
307 val)
308 (princ "text starting with\n ")
309 (princ (match-string 0 val)))
310 ((string-match "^[ \t\n]+$" val)
311 (princ "whitespace"))
313 (princ "the empty string")))))
315 (princ "Garbage:\n")
316 (if verbose (prin1 val))))))
318 (defun insert-register (register &optional arg)
319 "Insert contents of register REGISTER. (REGISTER is a character.)
320 Normally puts point before and mark after the inserted text.
321 If optional second arg is non-nil, puts mark before and point after.
322 Interactively, second arg is non-nil if prefix arg is supplied."
323 (interactive "*cInsert register: \nP")
324 (push-mark)
325 (let ((val (get-register register)))
326 (cond
327 ((registerv-p val)
328 (assert (registerv-insert-func val) nil
329 "Don't know how to insert register %s"
330 (single-key-description register))
331 (funcall (registerv-insert-func val) (registerv-data val)))
332 ((stringp val)
333 (insert-for-yank val))
334 ((numberp val)
335 (princ val (current-buffer)))
336 ((and (markerp val) (marker-position val))
337 (princ (marker-position val) (current-buffer)))
338 ((and (fboundp 'semantic-foreign-tag-p)
339 semantic-mode
340 (semantic-foreign-tag-p val))
341 (semantic-insert-foreign-tag val))
343 (error "Register does not contain text"))))
344 (if (not arg) (exchange-point-and-mark)))
346 (defun copy-to-register (register start end &optional delete-flag)
347 "Copy region into register REGISTER.
348 With prefix arg, delete as well.
349 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
350 START and END are buffer positions indicating what to copy."
351 (interactive "cCopy to register: \nr\nP")
352 (set-register register (filter-buffer-substring start end))
353 (if delete-flag (delete-region start end)))
355 (defun append-to-register (register start end &optional delete-flag)
356 "Append region to text in register REGISTER.
357 With prefix arg, delete as well.
358 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
359 START and END are buffer positions indicating what to append."
360 (interactive "cAppend to register: \nr\nP")
361 (let ((reg (get-register register))
362 (text (filter-buffer-substring start end)))
363 (set-register
364 register (cond ((not reg) text)
365 ((stringp reg) (concat reg text))
366 (t (error "Register does not contain text")))))
367 (if delete-flag (delete-region start end)))
369 (defun prepend-to-register (register start end &optional delete-flag)
370 "Prepend region to text in register REGISTER.
371 With prefix arg, delete as well.
372 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
373 START and END are buffer positions indicating what to prepend."
374 (interactive "cPrepend to register: \nr\nP")
375 (let ((reg (get-register register))
376 (text (filter-buffer-substring start end)))
377 (set-register
378 register (cond ((not reg) text)
379 ((stringp reg) (concat text reg))
380 (t (error "Register does not contain text")))))
381 (if delete-flag (delete-region start end)))
383 (defun copy-rectangle-to-register (register start end &optional delete-flag)
384 "Copy rectangular region into register REGISTER.
385 With prefix arg, delete as well.
386 To insert this register in the buffer, use \\[insert-register].
388 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
389 START and END are buffer positions giving two corners of rectangle."
390 (interactive "cCopy rectangle to register: \nr\nP")
391 (set-register register
392 (if delete-flag
393 (delete-extract-rectangle start end)
394 (extract-rectangle start end))))
396 (provide 'register)
397 ;;; register.el ends here