1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993-1994, 2001-2012 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 (eval-when-compile (require 'cl-lib
))
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))
40 (registerv (:constructor nil
)
41 (:constructor registerv--make
(&optional data print-func
42 jump-func insert-func
))
46 (data nil
:read-only t
)
47 (print-func nil
:read-only t
)
48 (jump-func nil
:read-only t
)
49 (insert-func nil
:read-only t
))
51 (cl-defun registerv-make (data &key print-func jump-func insert-func
)
52 "Create a register value object.
54 DATA can be any value.
55 PRINT-FUNC if provided controls how `list-registers' and
56 `view-register' print the register. It should be a function
57 receiving one argument DATA and print text that completes
59 Register X contains [TEXT PRINTED BY PRINT-FUNC]
60 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
61 INSERT-FUNC if provided, controls how `insert-register' insert the register.
62 They both receive DATA as argument."
63 (registerv--make data print-func jump-func insert-func
))
65 (defvar register-alist nil
66 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
67 NAME is a character (a number). CONTENTS is a string, number, marker, list
68 or a struct returned by `registerv-make'.
69 A list of strings represents a rectangle.
70 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
71 A list of the form (file-query FILE-NAME POSITION) represents
72 position POSITION in the file named FILE-NAME, but query before
74 A list of the form (WINDOW-CONFIGURATION POSITION)
75 represents a saved window configuration plus a saved value of point.
76 A list of the form (FRAME-CONFIGURATION POSITION)
77 represents a saved frame configuration plus a saved value of point.")
79 (defgroup register nil
84 (defcustom register-separator nil
85 "Register containing the text to put between collected texts, or nil if none.
87 When collecting text with
88 `append-to-register' (resp. `prepend-to-register') contents of
89 this register is added to the beginning (resp. end) of the marked
92 :type
'(choice (const :tag
"None" nil
)
93 (character :tag
"Use register" :value ?
+)))
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
)))
105 (push (cons register value
) register-alist
))
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))
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
)))
153 (cl-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
)))
164 (or (marker-buffer val
)
165 (error "That register's buffer no longer exists"))
166 (switch-to-buffer (marker-buffer 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
)
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))
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
204 (prefix-numeric-value number
)
205 (if (looking-at "\\s-*-?[0-9]+")
207 (goto-char (match-end 0))
208 (string-to-number (match-string 0)))
211 (defun increment-register (prefix register
)
212 "Augment contents of REGISTER.
213 Interactively, PREFIX is in raw form.
215 If REGISTER contains a number, add `prefix-numeric-value' of
218 If REGISTER is empty or if it contains text, call
219 `append-to-register' with `delete-flag' set to PREFIX."
220 (interactive "P\ncIncrement register: ")
221 (let ((register-val (get-register register
)))
223 ((numberp register-val
)
224 (let ((number (prefix-numeric-value prefix
)))
225 (set-register register
(+ number register-val
))))
226 ((or (not register-val
) (stringp register-val
))
227 (append-to-register register
(region-beginning) (region-end) prefix
))
228 (t (error "Register does not contain a number or text")))))
230 (defun view-register (register)
231 "Display what is contained in register named REGISTER.
232 The Lisp value REGISTER is a character."
233 (interactive "cView register: ")
234 (let ((val (get-register register
)))
236 (message "Register %s is empty" (single-key-description register
))
237 (with-output-to-temp-buffer "*Output*"
238 (describe-register-1 register t
)))))
240 (defun list-registers ()
241 "Display a list of nonempty registers saying briefly what they contain."
243 (let ((list (copy-sequence register-alist
)))
244 (setq list
(sort list
(lambda (a b
) (< (car a
) (car b
)))))
245 (with-output-to-temp-buffer "*Output*"
247 (when (get-register (car elt
))
248 (describe-register-1 (car elt
))
251 (defun describe-register-1 (register &optional verbose
)
253 (princ (single-key-description register
))
255 (let ((val (get-register register
)))
258 (if (registerv-print-func val
)
259 (funcall (registerv-print-func val
) (registerv-data val
))
260 (princ "[UNPRINTABLE CONTENTS].")))
266 (let ((buf (marker-buffer val
)))
268 (princ "a marker in no buffer")
269 (princ "a buffer position:\n buffer ")
270 (princ (buffer-name buf
))
271 (princ ", position ")
272 (princ (marker-position val
)))))
274 ((and (consp val
) (window-configuration-p (car val
)))
275 (princ "a window configuration."))
277 ((and (consp val
) (frame-configuration-p (car val
)))
278 (princ "a frame configuration."))
280 ((and (consp val
) (eq (car val
) 'file
))
285 ((and (consp val
) (eq (car val
) 'file-query
))
286 (princ "a file-query reference:\n file ")
287 (prin1 (car (cdr val
)))
288 (princ ",\n position ")
289 (princ (car (cdr (cdr val
))))
295 (princ "the rectangle:\n")
300 (setq val
(cdr val
))))
301 (princ "a rectangle starting with ")
305 (if (eq yank-excluded-properties t
)
306 (set-text-properties 0 (length val
) nil val
)
307 (remove-list-of-text-properties 0 (length val
)
308 yank-excluded-properties val
))
311 (princ "the text:\n")
314 ;; Extract first N characters starting with first non-whitespace.
315 ((string-match (format "[^ \t\n].\\{,%d\\}"
316 ;; Deduct 6 for the spaces inserted below.
317 (min 20 (max 0 (- (window-width) 6))))
319 (princ "text starting with\n ")
320 (princ (match-string 0 val
)))
321 ((string-match "^[ \t\n]+$" val
)
322 (princ "whitespace"))
324 (princ "the empty string")))))
327 (if verbose
(prin1 val
))))))
329 (defun insert-register (register &optional arg
)
330 "Insert contents of register REGISTER. (REGISTER is a character.)
331 Normally puts point before and mark after the inserted text.
332 If optional second arg is non-nil, puts mark before and point after.
333 Interactively, second arg is non-nil if prefix arg is supplied."
334 (interactive "*cInsert register: \nP")
336 (let ((val (get-register register
)))
339 (cl-assert (registerv-insert-func val
) nil
340 "Don't know how to insert register %s"
341 (single-key-description register
))
342 (funcall (registerv-insert-func val
) (registerv-data val
)))
344 (insert-rectangle val
))
346 (insert-for-yank val
))
348 (princ val
(current-buffer)))
349 ((and (markerp val
) (marker-position val
))
350 (princ (marker-position val
) (current-buffer)))
351 ((and (fboundp 'semantic-foreign-tag-p
)
353 (semantic-foreign-tag-p val
))
354 (semantic-insert-foreign-tag val
))
356 (error "Register does not contain text"))))
357 (if (not arg
) (exchange-point-and-mark)))
359 (defun copy-to-register (register start end
&optional delete-flag
)
360 "Copy region into register REGISTER.
361 With prefix arg, delete as well.
362 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
363 START and END are buffer positions indicating what to copy."
364 (interactive "cCopy to register: \nr\nP")
365 (set-register register
(filter-buffer-substring start end
))
366 (setq deactivate-mark t
)
368 (delete-region start end
))
369 ((called-interactively-p 'interactive
)
370 (indicate-copied-region))))
372 (defun append-to-register (register start end
&optional delete-flag
)
373 "Append region to text in register REGISTER.
374 With prefix arg, delete as well.
375 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
376 START and END are buffer positions indicating what to append."
377 (interactive "cAppend to register: \nr\nP")
378 (let ((reg (get-register register
))
379 (text (filter-buffer-substring start end
))
380 (separator (and register-separator
(get-register register-separator
))))
382 register
(cond ((not reg
) text
)
383 ((stringp reg
) (concat reg separator text
))
384 (t (error "Register does not contain text")))))
385 (setq deactivate-mark t
)
387 (delete-region start end
))
388 ((called-interactively-p 'interactive
)
389 (indicate-copied-region))))
391 (defun prepend-to-register (register start end
&optional delete-flag
)
392 "Prepend region to text in register REGISTER.
393 With prefix arg, delete as well.
394 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
395 START and END are buffer positions indicating what to prepend."
396 (interactive "cPrepend to register: \nr\nP")
397 (let ((reg (get-register register
))
398 (text (filter-buffer-substring start end
))
399 (separator (and register-separator
(get-register register-separator
))))
401 register
(cond ((not reg
) text
)
402 ((stringp reg
) (concat text separator reg
))
403 (t (error "Register does not contain text")))))
404 (setq deactivate-mark t
)
406 (delete-region start end
))
407 ((called-interactively-p 'interactive
)
408 (indicate-copied-region))))
410 (defun copy-rectangle-to-register (register start end
&optional delete-flag
)
411 "Copy rectangular region into register REGISTER.
412 With prefix arg, delete as well.
413 To insert this register in the buffer, use \\[insert-register].
415 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
416 START and END are buffer positions giving two corners of rectangle."
417 (interactive "cCopy rectangle to register: \nr\nP")
418 (let ((rectangle (if delete-flag
419 (delete-extract-rectangle start end
)
420 (extract-rectangle start end
))))
421 (set-register register rectangle
)
422 (when (and (null delete-flag
)
423 (called-interactively-p 'interactive
))
424 (setq deactivate-mark t
)
425 (indicate-copied-region (length (car rectangle
))))))
429 ;;; register.el ends here