1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 1985, 1993-1994, 2001-2013 Free Software Foundation,
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package of functions emulates and somewhat extends the venerable
28 ;; TECO's `register' feature, which permits you to save various useful
29 ;; pieces of buffer state to named variables. The entry points are
30 ;; documented in the Emacs user's manual.
32 (eval-when-compile (require 'cl-lib
))
37 (registerv (:constructor nil
)
38 (:constructor registerv--make
(&optional data print-func
39 jump-func insert-func
))
43 (data nil
:read-only t
)
44 (print-func nil
:read-only t
)
45 (jump-func nil
:read-only t
)
46 (insert-func nil
:read-only t
))
48 (cl-defun registerv-make (data &key print-func jump-func insert-func
)
49 "Create a register value object.
51 DATA can be any value.
52 PRINT-FUNC if provided controls how `list-registers' and
53 `view-register' print the register. It should be a function
54 receiving one argument DATA and print text that completes
56 Register X contains [TEXT PRINTED BY PRINT-FUNC]
57 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
58 INSERT-FUNC if provided, controls how `insert-register' insert the register.
59 They both receive DATA as argument."
60 (registerv--make data print-func jump-func insert-func
))
62 (defvar register-alist nil
63 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
64 NAME is a character (a number). CONTENTS is a string, number, marker, list
65 or a struct returned by `registerv-make'.
66 A list of strings represents a rectangle.
67 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
68 A list of the form (file-query FILE-NAME POSITION) represents
69 position POSITION in the file named FILE-NAME, but query before
71 A list of the form (WINDOW-CONFIGURATION POSITION)
72 represents a saved window configuration plus a saved value of point.
73 A list of the form (FRAME-CONFIGURATION POSITION)
74 represents a saved frame configuration plus a saved value of point.")
76 (defgroup register nil
81 (defcustom register-separator nil
82 "Register containing the text to put between collected texts, or nil if none.
84 When collecting text with
85 `append-to-register' (resp. `prepend-to-register') contents of
86 this register is added to the beginning (resp. end) of the marked
89 :type
'(choice (const :tag
"None" nil
)
90 (character :tag
"Use register" :value ?
+)))
92 (defcustom register-preview-delay
1
93 "If non-nil delay in seconds to pop up the preview window."
94 :type
'(choice number
(const :tag
"Indefinitely" nil
))
97 (defun get-register (register)
98 "Return contents of Emacs register named REGISTER, or nil if none."
99 (cdr (assq register register-alist
)))
101 (defun set-register (register value
)
102 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
103 See the documentation of the variable `register-alist' for possible VALUEs."
104 (let ((aelt (assq register register-alist
)))
107 (push (cons register value
) register-alist
))
110 (defun register-describe-oneline (c)
111 "One-line description of register C."
112 (let ((d (replace-regexp-in-string
114 (with-output-to-string (describe-register-1 c
)))))
115 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d
)
116 (substring d
(match-end 0))
119 (defvar register-preview-functions nil
)
121 (defun register-preview (buffer &optional show-empty
)
122 "Pop up a window to show register preview in BUFFER.
123 If SHOW-EMPTY is non-nil show the window even if no registers."
124 (when (or show-empty
(consp register-alist
))
125 (let ((split-height-threshold 0))
126 (with-temp-buffer-window
128 (cons 'display-buffer-below-selected
129 '((window-height . fit-window-to-buffer
)))
131 (with-current-buffer standard-output
132 (setq cursor-in-non-selected-windows nil
)
135 (insert (or (run-hook-with-args-until-success
136 'register-preview-functions r
)
138 (concat (single-key-description (car r
)) ":")
139 (register-describe-oneline (car r
))))))
142 (defun register-read-with-preview (prompt)
143 "Read an event with register preview using PROMPT.
144 Pop up a register preview window if the input is a help char but
145 is not a register. Alternatively if `register-preview-delay' is a
146 number the preview window is popped up after some delay."
147 (let* ((buffer "*Register Preview*")
148 (timer (when (numberp register-preview-delay
)
149 (run-with-timer register-preview-delay nil
151 (unless (get-buffer-window buffer
)
152 (register-preview buffer
))))))
153 (help-chars (cl-loop for c in
(cons help-char help-event-list
)
154 when
(not (get-register c
))
158 (while (memq (read-event (propertize prompt
'face
'minibuffer-prompt
))
160 (unless (get-buffer-window buffer
)
161 (register-preview buffer
'show-empty
)))
163 (and (timerp timer
) (cancel-timer timer
))
164 (let ((w (get-buffer-window buffer
)))
165 (and (window-live-p w
) (delete-window w
)))
166 (and (get-buffer buffer
) (kill-buffer buffer
)))))
168 (defun point-to-register (register &optional arg
)
169 "Store current location of point in register REGISTER.
170 With prefix argument, store current frame configuration.
171 Use \\[jump-to-register] to go to that location or restore that configuration.
172 Argument is a character, naming the register."
173 (interactive (list (register-read-with-preview "Point to register: ")
175 ;; Turn the marker into a file-ref if the buffer is killed.
176 (add-hook 'kill-buffer-hook
'register-swap-out nil t
)
177 (set-register register
178 (if arg
(list (current-frame-configuration) (point-marker))
181 (defun window-configuration-to-register (register &optional _arg
)
182 "Store the window configuration of the selected frame in register REGISTER.
183 Use \\[jump-to-register] to restore the configuration.
184 Argument is a character, naming the register."
185 (interactive (list (register-read-with-preview
186 "Window configuration to register: ")
188 ;; current-window-configuration does not include the value
189 ;; of point in the current buffer, so record that separately.
190 (set-register register
(list (current-window-configuration) (point-marker))))
192 (defun frame-configuration-to-register (register &optional _arg
)
193 "Store the window configuration of all frames in register REGISTER.
194 Use \\[jump-to-register] to restore the configuration.
195 Argument is a character, naming the register."
196 (interactive (list (register-read-with-preview
197 "Frame configuration to register: ")
199 ;; current-frame-configuration does not include the value
200 ;; of point in the current buffer, so record that separately.
201 (set-register register
(list (current-frame-configuration) (point-marker))))
203 (defalias 'register-to-point
'jump-to-register
)
204 (defun jump-to-register (register &optional delete
)
205 "Move point to location stored in a register.
206 If the register contains a file name, find that file.
207 \(To put a file name in a register, you must use `set-register'.)
208 If the register contains a window configuration (one frame) or a frameset
209 \(all frames), restore that frame or all frames accordingly.
210 First argument is a character, naming the register.
211 Optional second arg non-nil (interactively, prefix argument) says to
212 delete any existing frames that the frameset doesn't mention.
213 \(Otherwise, these frames are iconified.)"
214 (interactive (list (register-read-with-preview "Jump to register: ")
216 (let ((val (get-register register
)))
219 (cl-assert (registerv-jump-func val
) nil
220 "Don't know how to jump to register %s"
221 (single-key-description register
))
222 (funcall (registerv-jump-func val
) (registerv-data val
)))
223 ((and (consp val
) (frame-configuration-p (car val
)))
224 (set-frame-configuration (car val
) (not delete
))
225 (goto-char (cadr val
)))
226 ((and (consp val
) (window-configuration-p (car val
)))
227 (set-window-configuration (car val
))
228 (goto-char (cadr val
)))
230 (or (marker-buffer val
)
231 (error "That register's buffer no longer exists"))
232 (switch-to-buffer (marker-buffer val
))
234 ((and (consp val
) (eq (car val
) 'file
))
235 (find-file (cdr val
)))
236 ((and (consp val
) (eq (car val
) 'file-query
))
237 (or (find-buffer-visiting (nth 1 val
))
238 (y-or-n-p (format "Visit file %s again? " (nth 1 val
)))
239 (error "Register access aborted"))
240 (find-file (nth 1 val
))
241 (goto-char (nth 2 val
)))
243 (error "Register doesn't contain a buffer position or configuration")))))
245 (defun register-swap-out ()
246 "Turn markers into file-query references when a buffer is killed."
247 (and buffer-file-name
248 (dolist (elem register-alist
)
249 (and (markerp (cdr elem
))
250 (eq (marker-buffer (cdr elem
)) (current-buffer))
254 (marker-position (cdr elem
))))))))
256 (defun number-to-register (number register
)
257 "Store a number in a register.
258 Two args, NUMBER and REGISTER (a character, naming the register).
259 If NUMBER is nil, a decimal number is read from the buffer starting
260 at point, and point moves to the end of that number.
261 Interactively, NUMBER is the prefix arg (none means nil)."
262 (interactive (list current-prefix-arg
263 (register-read-with-preview "Number to register: ")))
264 (set-register register
266 (prefix-numeric-value number
)
267 (if (looking-at "\\s-*-?[0-9]+")
269 (goto-char (match-end 0))
270 (string-to-number (match-string 0)))
273 (defun increment-register (prefix register
)
274 "Augment contents of REGISTER.
275 Interactively, PREFIX is in raw form.
277 If REGISTER contains a number, add `prefix-numeric-value' of
280 If REGISTER is empty or if it contains text, call
281 `append-to-register' with `delete-flag' set to PREFIX."
282 (interactive "P\ncIncrement register: ")
283 (let ((register-val (get-register register
)))
285 ((numberp register-val
)
286 (let ((number (prefix-numeric-value prefix
)))
287 (set-register register
(+ number register-val
))))
288 ((or (not register-val
) (stringp register-val
))
289 (append-to-register register
(region-beginning) (region-end) prefix
))
290 (t (error "Register does not contain a number or text")))))
292 (defun view-register (register)
293 "Display what is contained in register named REGISTER.
294 The Lisp value REGISTER is a character."
295 (interactive (list (register-read-with-preview "View register: ")))
296 (let ((val (get-register register
)))
298 (message "Register %s is empty" (single-key-description register
))
299 (with-output-to-temp-buffer "*Output*"
300 (describe-register-1 register t
)))))
302 (defun list-registers ()
303 "Display a list of nonempty registers saying briefly what they contain."
305 (let ((list (copy-sequence register-alist
)))
306 (setq list
(sort list
(lambda (a b
) (< (car a
) (car b
)))))
307 (with-output-to-temp-buffer "*Output*"
309 (when (get-register (car elt
))
310 (describe-register-1 (car elt
))
313 (defun describe-register-1 (register &optional verbose
)
315 (princ (single-key-description register
))
317 (let ((val (get-register register
)))
320 (if (registerv-print-func val
)
321 (funcall (registerv-print-func val
) (registerv-data val
))
322 (princ "[UNPRINTABLE CONTENTS].")))
328 (let ((buf (marker-buffer val
)))
330 (princ "a marker in no buffer")
331 (princ "a buffer position:\n buffer ")
332 (princ (buffer-name buf
))
333 (princ ", position ")
334 (princ (marker-position val
)))))
336 ((and (consp val
) (window-configuration-p (car val
)))
337 (princ "a window configuration."))
339 ((and (consp val
) (frame-configuration-p (car val
)))
340 (princ "a frame configuration."))
342 ((and (consp val
) (eq (car val
) 'file
))
347 ((and (consp val
) (eq (car val
) 'file-query
))
348 (princ "a file-query reference:\n file ")
349 (prin1 (car (cdr val
)))
350 (princ ",\n position ")
351 (princ (car (cdr (cdr val
))))
357 (princ "the rectangle:\n")
362 (setq val
(cdr val
))))
363 (princ "a rectangle starting with ")
367 (if (eq yank-excluded-properties t
)
368 (set-text-properties 0 (length val
) nil val
)
369 (remove-list-of-text-properties 0 (length val
)
370 yank-excluded-properties val
))
373 (princ "the text:\n")
376 ;; Extract first N characters starting with first non-whitespace.
377 ((string-match (format "[^ \t\n].\\{,%d\\}"
378 ;; Deduct 6 for the spaces inserted below.
379 (min 20 (max 0 (- (window-width) 6))))
381 (princ "text starting with\n ")
382 (princ (match-string 0 val
)))
383 ((string-match "^[ \t\n]+$" val
)
384 (princ "whitespace"))
386 (princ "the empty string")))))
389 (if verbose
(prin1 val
))))))
391 (defun insert-register (register &optional arg
)
392 "Insert contents of register REGISTER. (REGISTER is a character.)
393 Normally puts point before and mark after the inserted text.
394 If optional second arg is non-nil, puts mark before and point after.
395 Interactively, second arg is non-nil if prefix arg is supplied."
397 (barf-if-buffer-read-only)
398 (list (register-read-with-preview "Insert register: ")
399 current-prefix-arg
)))
401 (let ((val (get-register register
)))
404 (cl-assert (registerv-insert-func val
) nil
405 "Don't know how to insert register %s"
406 (single-key-description register
))
407 (funcall (registerv-insert-func val
) (registerv-data val
)))
409 (insert-rectangle val
))
411 (insert-for-yank val
))
413 (princ val
(current-buffer)))
414 ((and (markerp val
) (marker-position val
))
415 (princ (marker-position val
) (current-buffer)))
417 (error "Register does not contain text"))))
418 (if (not arg
) (exchange-point-and-mark)))
420 (defun copy-to-register (register start end
&optional delete-flag
)
421 "Copy region into register REGISTER.
422 With prefix arg, delete as well.
423 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
424 START and END are buffer positions indicating what to copy."
425 (interactive (list (register-read-with-preview "Copy to register: ")
429 (set-register register
(filter-buffer-substring start end
))
430 (setq deactivate-mark t
)
432 (delete-region start end
))
433 ((called-interactively-p 'interactive
)
434 (indicate-copied-region))))
436 (defun append-to-register (register start end
&optional delete-flag
)
437 "Append region to text in register REGISTER.
438 With prefix arg, delete as well.
439 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
440 START and END are buffer positions indicating what to append."
441 (interactive (list (register-read-with-preview "Append to register: ")
445 (let ((reg (get-register register
))
446 (text (filter-buffer-substring start end
))
447 (separator (and register-separator
(get-register register-separator
))))
449 register
(cond ((not reg
) text
)
450 ((stringp reg
) (concat reg separator text
))
451 (t (error "Register does not contain text")))))
452 (setq deactivate-mark t
)
454 (delete-region start end
))
455 ((called-interactively-p 'interactive
)
456 (indicate-copied-region))))
458 (defun prepend-to-register (register start end
&optional delete-flag
)
459 "Prepend region to text in register REGISTER.
460 With prefix arg, delete as well.
461 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
462 START and END are buffer positions indicating what to prepend."
463 (interactive (list (register-read-with-preview "Prepend to register: ")
467 (let ((reg (get-register register
))
468 (text (filter-buffer-substring start end
))
469 (separator (and register-separator
(get-register register-separator
))))
471 register
(cond ((not reg
) text
)
472 ((stringp reg
) (concat text separator reg
))
473 (t (error "Register does not contain text")))))
474 (setq deactivate-mark t
)
476 (delete-region start end
))
477 ((called-interactively-p 'interactive
)
478 (indicate-copied-region))))
480 (defun copy-rectangle-to-register (register start end
&optional delete-flag
)
481 "Copy rectangular region into register REGISTER.
482 With prefix arg, delete as well.
483 To insert this register in the buffer, use \\[insert-register].
485 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
486 START and END are buffer positions giving two corners of rectangle."
487 (interactive (list (register-read-with-preview
488 "Copy rectangle to register: ")
492 (let ((rectangle (if delete-flag
493 (delete-extract-rectangle start end
)
494 (extract-rectangle start end
))))
495 (set-register register rectangle
)
496 (when (and (null delete-flag
)
497 (called-interactively-p 'interactive
))
498 (setq deactivate-mark t
)
499 (indicate-copied-region (length (car rectangle
))))))
502 ;;; register.el ends here