]> code.delx.au - gnu-emacs/blob - lisp/register.el
945fb28bffe4a6ab8f5d37cff0aa74bbebb13132
[gnu-emacs] / lisp / register.el
1 ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 1985, 1993-1994, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
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
31 (eval-when-compile (require 'cl-lib))
32
33 ;;; Code:
34
35 (cl-defstruct
36 (registerv (:constructor nil)
37 (:constructor registerv--make (&optional data print-func
38 jump-func insert-func))
39 (:copier nil)
40 (:type vector)
41 :named)
42 (data nil :read-only t)
43 (print-func nil :read-only t)
44 (jump-func nil :read-only t)
45 (insert-func nil :read-only t))
46
47 (cl-defun registerv-make (data &key print-func jump-func insert-func)
48 "Create a register value object.
49
50 DATA can be any value.
51 PRINT-FUNC if provided controls how `list-registers' and
52 `view-register' print the register. It should be a function
53 receiving one argument DATA and print text that completes
54 this sentence:
55 Register X contains [TEXT PRINTED BY PRINT-FUNC]
56 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
57 INSERT-FUNC if provided, controls how `insert-register' insert the register.
58 They both receive DATA as argument."
59 (registerv--make data print-func jump-func insert-func))
60
61 (defvar register-alist nil
62 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
63 NAME is a character (a number). CONTENTS is a string, number, marker, list
64 or a struct returned by `registerv-make'.
65 A list of strings represents a rectangle.
66 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
67 A list of the form (file-query FILE-NAME POSITION) represents
68 position POSITION in the file named FILE-NAME, but query before
69 visiting it.
70 A list of the form (WINDOW-CONFIGURATION POSITION)
71 represents a saved window configuration plus a saved value of point.
72 A list of the form (FRAME-CONFIGURATION POSITION)
73 represents a saved frame configuration plus a saved value of point.")
74
75 (defgroup register nil
76 "Register commands."
77 :group 'convenience
78 :version "24.3")
79
80 (defcustom register-separator nil
81 "Register containing the text to put between collected texts, or nil if none.
82
83 When collecting text with
84 `append-to-register' (resp. `prepend-to-register') contents of
85 this register is added to the beginning (resp. end) of the marked
86 text."
87 :group 'register
88 :type '(choice (const :tag "None" nil)
89 (character :tag "Use register" :value ?+)))
90
91 (defcustom register-preview-delay 1
92 "If non-nil delay in seconds to pop up the preview window."
93 :version "24.4"
94 :type '(choice number (const :tag "Indefinitely" nil))
95 :group 'register)
96
97 (defun get-register (register)
98 "Return contents of Emacs register named REGISTER, or nil if none."
99 (cdr (assq register register-alist)))
100
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)))
105 (if aelt
106 (setcdr aelt value)
107 (push (cons register value) register-alist))
108 value))
109
110 (defun register-describe-oneline (c)
111 "One-line description of register C."
112 (let ((d (replace-regexp-in-string
113 "\n[ \t]*" " "
114 (with-output-to-string (describe-register-1 c)))))
115 (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d)
116 (substring d (match-end 0))
117 d)))
118
119 (defvar register-preview-functions nil)
120
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 (with-temp-buffer-window
126 buffer
127 (cons 'display-buffer-below-selected
128 '((window-height . fit-window-to-buffer)))
129 nil
130 (with-current-buffer standard-output
131 (setq cursor-in-non-selected-windows nil)
132 (mapc
133 (lambda (r)
134 (insert (or (run-hook-with-args-until-success
135 'register-preview-functions r)
136 (format "%s %s\n"
137 (concat (single-key-description (car r)) ":")
138 (register-describe-oneline (car r))))))
139 register-alist)))))
140
141 (defun register-read-with-preview (prompt)
142 "Read an event with register preview using PROMPT.
143 Pop up a register preview window if the input is a help char but
144 is not a register. Alternatively if `register-preview-delay' is a
145 number the preview window is popped up after some delay."
146 (let* ((buffer "*Register Preview*")
147 (timer (when (numberp register-preview-delay)
148 (run-with-timer register-preview-delay nil
149 (lambda ()
150 (unless (get-buffer-window buffer)
151 (register-preview buffer))))))
152 (help-chars (cl-loop for c in (cons help-char help-event-list)
153 when (not (get-register c))
154 collect c)))
155 (unwind-protect
156 (progn
157 (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt))
158 help-chars)
159 (unless (get-buffer-window buffer)
160 (register-preview buffer 'show-empty)))
161 last-input-event)
162 (and (timerp timer) (cancel-timer timer))
163 (let ((w (get-buffer-window buffer)))
164 (and (window-live-p w) (delete-window w)))
165 (and (get-buffer buffer) (kill-buffer buffer)))))
166
167 (defun point-to-register (register &optional arg)
168 "Store current location of point in register REGISTER.
169 With prefix argument, store current frame configuration.
170 Use \\[jump-to-register] to go to that location or restore that configuration.
171 Argument is a character, naming the register."
172 (interactive (list (register-read-with-preview "Point to register: ")
173 current-prefix-arg))
174 ;; Turn the marker into a file-ref if the buffer is killed.
175 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
176 (set-register register
177 (if arg (list (current-frame-configuration) (point-marker))
178 (point-marker))))
179
180 (defun window-configuration-to-register (register &optional _arg)
181 "Store the window configuration of the selected frame in register REGISTER.
182 Use \\[jump-to-register] to restore the configuration.
183 Argument is a character, naming the register."
184 (interactive (list (register-read-with-preview
185 "Window configuration to register: ")
186 current-prefix-arg))
187 ;; current-window-configuration does not include the value
188 ;; of point in the current buffer, so record that separately.
189 (set-register register (list (current-window-configuration) (point-marker))))
190
191 (defun frame-configuration-to-register (register &optional _arg)
192 "Store the window configuration of all frames in register REGISTER.
193 Use \\[jump-to-register] to restore the configuration.
194 Argument is a character, naming the register."
195 (interactive (list (register-read-with-preview
196 "Frame configuration to register: ")
197 current-prefix-arg))
198 ;; current-frame-configuration does not include the value
199 ;; of point in the current buffer, so record that separately.
200 (set-register register (list (current-frame-configuration) (point-marker))))
201
202 (defalias 'register-to-point 'jump-to-register)
203 (defun jump-to-register (register &optional delete)
204 "Move point to location stored in a register.
205 If the register contains a file name, find that file.
206 \(To put a file name in a register, you must use `set-register'.)
207 If the register contains a window configuration (one frame) or a frameset
208 \(all frames), restore that frame or all frames accordingly.
209 First argument is a character, naming the register.
210 Optional second arg non-nil (interactively, prefix argument) says to
211 delete any existing frames that the frameset doesn't mention.
212 \(Otherwise, these frames are iconified.)"
213 (interactive (list (register-read-with-preview "Jump to register: ")
214 current-prefix-arg))
215 (let ((val (get-register register)))
216 (cond
217 ((registerv-p val)
218 (cl-assert (registerv-jump-func val) nil
219 "Don't know how to jump to register %s"
220 (single-key-description register))
221 (funcall (registerv-jump-func val) (registerv-data val)))
222 ((and (consp val) (frame-configuration-p (car val)))
223 (set-frame-configuration (car val) (not delete))
224 (goto-char (cadr val)))
225 ((and (consp val) (window-configuration-p (car val)))
226 (set-window-configuration (car val))
227 (goto-char (cadr val)))
228 ((markerp val)
229 (or (marker-buffer val)
230 (error "That register's buffer no longer exists"))
231 (switch-to-buffer (marker-buffer val))
232 (goto-char val))
233 ((and (consp val) (eq (car val) 'file))
234 (find-file (cdr val)))
235 ((and (consp val) (eq (car val) 'file-query))
236 (or (find-buffer-visiting (nth 1 val))
237 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
238 (error "Register access aborted"))
239 (find-file (nth 1 val))
240 (goto-char (nth 2 val)))
241 (t
242 (error "Register doesn't contain a buffer position or configuration")))))
243
244 (defun register-swap-out ()
245 "Turn markers into file-query references when a buffer is killed."
246 (and buffer-file-name
247 (dolist (elem register-alist)
248 (and (markerp (cdr elem))
249 (eq (marker-buffer (cdr elem)) (current-buffer))
250 (setcdr elem
251 (list 'file-query
252 buffer-file-name
253 (marker-position (cdr elem))))))))
254
255 (defun number-to-register (number register)
256 "Store a number in a register.
257 Two args, NUMBER and REGISTER (a character, naming the register).
258 If NUMBER is nil, a decimal number is read from the buffer starting
259 at point, and point moves to the end of that number.
260 Interactively, NUMBER is the prefix arg (none means nil)."
261 (interactive (list current-prefix-arg
262 (register-read-with-preview "Number to register: ")))
263 (set-register register
264 (if number
265 (prefix-numeric-value number)
266 (if (looking-at "\\s-*-?[0-9]+")
267 (progn
268 (goto-char (match-end 0))
269 (string-to-number (match-string 0)))
270 0))))
271
272 (defun increment-register (prefix register)
273 "Augment contents of REGISTER.
274 Interactively, PREFIX is in raw form.
275
276 If REGISTER contains a number, add `prefix-numeric-value' of
277 PREFIX to it.
278
279 If REGISTER is empty or if it contains text, call
280 `append-to-register' with `delete-flag' set to PREFIX."
281 (interactive "P\ncIncrement register: ")
282 (let ((register-val (get-register register)))
283 (cond
284 ((numberp register-val)
285 (let ((number (prefix-numeric-value prefix)))
286 (set-register register (+ number register-val))))
287 ((or (not register-val) (stringp register-val))
288 (append-to-register register (region-beginning) (region-end) prefix))
289 (t (error "Register does not contain a number or text")))))
290
291 (defun view-register (register)
292 "Display what is contained in register named REGISTER.
293 The Lisp value REGISTER is a character."
294 (interactive (list (register-read-with-preview "View register: ")))
295 (let ((val (get-register register)))
296 (if (null val)
297 (message "Register %s is empty" (single-key-description register))
298 (with-output-to-temp-buffer "*Output*"
299 (describe-register-1 register t)))))
300
301 (defun list-registers ()
302 "Display a list of nonempty registers saying briefly what they contain."
303 (interactive)
304 (let ((list (copy-sequence register-alist)))
305 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
306 (with-output-to-temp-buffer "*Output*"
307 (dolist (elt list)
308 (when (get-register (car elt))
309 (describe-register-1 (car elt))
310 (terpri))))))
311
312 (defun describe-register-1 (register &optional verbose)
313 (princ "Register ")
314 (princ (single-key-description register))
315 (princ " contains ")
316 (let ((val (get-register register)))
317 (cond
318 ((registerv-p val)
319 (if (registerv-print-func val)
320 (funcall (registerv-print-func val) (registerv-data val))
321 (princ "[UNPRINTABLE CONTENTS].")))
322
323 ((numberp val)
324 (princ val))
325
326 ((markerp val)
327 (let ((buf (marker-buffer val)))
328 (if (null buf)
329 (princ "a marker in no buffer")
330 (princ "a buffer position:\n buffer ")
331 (princ (buffer-name buf))
332 (princ ", position ")
333 (princ (marker-position val)))))
334
335 ((and (consp val) (window-configuration-p (car val)))
336 (princ "a window configuration."))
337
338 ((and (consp val) (frame-configuration-p (car val)))
339 (princ "a frame configuration."))
340
341 ((and (consp val) (eq (car val) 'file))
342 (princ "the file ")
343 (prin1 (cdr val))
344 (princ "."))
345
346 ((and (consp val) (eq (car val) 'file-query))
347 (princ "a file-query reference:\n file ")
348 (prin1 (car (cdr val)))
349 (princ ",\n position ")
350 (princ (car (cdr (cdr val))))
351 (princ "."))
352
353 ((consp val)
354 (if verbose
355 (progn
356 (princ "the rectangle:\n")
357 (while val
358 (princ " ")
359 (princ (car val))
360 (terpri)
361 (setq val (cdr val))))
362 (princ "a rectangle starting with ")
363 (princ (car val))))
364
365 ((stringp val)
366 (setq val (copy-sequence val))
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))
371 (if verbose
372 (progn
373 (princ "the text:\n")
374 (princ val))
375 (cond
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))))
380 val)
381 (princ "text starting with\n ")
382 (princ (match-string 0 val)))
383 ((string-match "^[ \t\n]+$" val)
384 (princ "whitespace"))
385 (t
386 (princ "the empty string")))))
387 (t
388 (princ "Garbage:\n")
389 (if verbose (prin1 val))))))
390
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."
396 (interactive (progn
397 (barf-if-buffer-read-only)
398 (list (register-read-with-preview "Insert register: ")
399 current-prefix-arg)))
400 (push-mark)
401 (let ((val (get-register register)))
402 (cond
403 ((registerv-p val)
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)))
408 ((consp val)
409 (insert-rectangle val))
410 ((stringp val)
411 (insert-for-yank val))
412 ((numberp val)
413 (princ val (current-buffer)))
414 ((and (markerp val) (marker-position val))
415 (princ (marker-position val) (current-buffer)))
416 (t
417 (error "Register does not contain text"))))
418 (if (not arg) (exchange-point-and-mark)))
419
420 (defun copy-to-register (register start end &optional delete-flag region)
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 The optional argument REGION if non-nil, indicates that we're not just copying
426 some text between START and END, but we're copying the region."
427 (interactive (list (register-read-with-preview "Copy to register: ")
428 (region-beginning)
429 (region-end)
430 current-prefix-arg
431 t))
432 (set-register register (if region
433 (funcall region-extract-function delete-flag)
434 (prog1 (filter-buffer-substring start end)
435 (if delete-flag (delete-region start end)))))
436 (setq deactivate-mark t)
437 (cond (delete-flag)
438 ((called-interactively-p 'interactive)
439 (indicate-copied-region))))
440
441 (defun append-to-register (register start end &optional delete-flag)
442 "Append region to text in register REGISTER.
443 With prefix arg, delete as well.
444 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
445 START and END are buffer positions indicating what to append."
446 (interactive (list (register-read-with-preview "Append to register: ")
447 (region-beginning)
448 (region-end)
449 current-prefix-arg))
450 (let ((reg (get-register register))
451 (text (filter-buffer-substring start end))
452 (separator (and register-separator (get-register register-separator))))
453 (set-register
454 register (cond ((not reg) text)
455 ((stringp reg) (concat reg separator text))
456 (t (error "Register does not contain text")))))
457 (setq deactivate-mark t)
458 (cond (delete-flag
459 (delete-region start end))
460 ((called-interactively-p 'interactive)
461 (indicate-copied-region))))
462
463 (defun prepend-to-register (register start end &optional delete-flag)
464 "Prepend region to text in register REGISTER.
465 With prefix arg, delete as well.
466 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
467 START and END are buffer positions indicating what to prepend."
468 (interactive (list (register-read-with-preview "Prepend to register: ")
469 (region-beginning)
470 (region-end)
471 current-prefix-arg))
472 (let ((reg (get-register register))
473 (text (filter-buffer-substring start end))
474 (separator (and register-separator (get-register register-separator))))
475 (set-register
476 register (cond ((not reg) text)
477 ((stringp reg) (concat text separator reg))
478 (t (error "Register does not contain text")))))
479 (setq deactivate-mark t)
480 (cond (delete-flag
481 (delete-region start end))
482 ((called-interactively-p 'interactive)
483 (indicate-copied-region))))
484
485 (defun copy-rectangle-to-register (register start end &optional delete-flag)
486 "Copy rectangular region into register REGISTER.
487 With prefix arg, delete as well.
488 To insert this register in the buffer, use \\[insert-register].
489
490 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
491 START and END are buffer positions giving two corners of rectangle."
492 (interactive (list (register-read-with-preview
493 "Copy rectangle to register: ")
494 (region-beginning)
495 (region-end)
496 current-prefix-arg))
497 (let ((rectangle (if delete-flag
498 (delete-extract-rectangle start end)
499 (extract-rectangle start end))))
500 (set-register register rectangle)
501 (when (and (null delete-flag)
502 (called-interactively-p 'interactive))
503 (setq deactivate-mark t)
504 (indicate-copied-region (length (car rectangle))))))
505
506 (provide 'register)
507 ;;; register.el ends here