* lisp/emacs-lisp/package.el: Update package-alist after install.
[emacs.git] / lisp / register.el
blob4876c614642c80548f61c8f43717783391bf46f4
1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993-1994, 2001-2013 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
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))
34 ;;; Code:
36 (cl-defstruct
37 (registerv (:constructor nil)
38 (:constructor registerv--make (&optional data print-func
39 jump-func insert-func))
40 (:copier nil)
41 (:type vector)
42 :named)
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
55 this sentence:
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
70 visiting it.
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
77 "Register commands."
78 :group 'convenience
79 :version "24.3")
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
87 text."
88 :group 'register
89 :type '(choice (const :tag "None" nil)
90 (character :tag "Use register" :value ?+)))
92 (defun get-register (register)
93 "Return contents of Emacs register named REGISTER, or nil if none."
94 (cdr (assq register register-alist)))
96 (defun set-register (register value)
97 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
98 See the documentation of the variable `register-alist' for possible VALUEs."
99 (let ((aelt (assq register register-alist)))
100 (if aelt
101 (setcdr aelt value)
102 (push (cons register value) register-alist))
103 value))
105 (defun point-to-register (register &optional arg)
106 "Store current location of point in register REGISTER.
107 With prefix argument, store current frame configuration.
108 Use \\[jump-to-register] to go to that location or restore that configuration.
109 Argument is a character, naming the register."
110 (interactive "cPoint to register: \nP")
111 ;; Turn the marker into a file-ref if the buffer is killed.
112 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
113 (set-register register
114 (if arg (list (current-frame-configuration) (point-marker))
115 (point-marker))))
117 (defun window-configuration-to-register (register &optional _arg)
118 "Store the window configuration of the selected frame in register REGISTER.
119 Use \\[jump-to-register] to restore the configuration.
120 Argument is a character, naming the register."
121 (interactive "cWindow configuration to register: \nP")
122 ;; current-window-configuration does not include the value
123 ;; of point in the current buffer, so record that separately.
124 (set-register register (list (current-window-configuration) (point-marker))))
126 (defun frame-configuration-to-register (register &optional _arg)
127 "Store the window configuration of all frames in register REGISTER.
128 Use \\[jump-to-register] to restore the configuration.
129 Argument is a character, naming the register."
130 (interactive "cFrame configuration to register: \nP")
131 ;; current-frame-configuration does not include the value
132 ;; of point in the current buffer, so record that separately.
133 (set-register register (list (current-frame-configuration) (point-marker))))
135 (defalias 'register-to-point 'jump-to-register)
136 (defun jump-to-register (register &optional delete)
137 "Move point to location stored in a register.
138 If the register contains a file name, find that file.
139 \(To put a file name in a register, you must use `set-register'.)
140 If the register contains a window configuration (one frame) or a frame
141 configuration (all frames), restore that frame or all frames accordingly.
142 First argument is a character, naming the register.
143 Optional second arg non-nil (interactively, prefix argument) says to
144 delete any existing frames that the frame configuration doesn't mention.
145 \(Otherwise, these frames are iconified.)"
146 (interactive "cJump to register: \nP")
147 (let ((val (get-register register)))
148 (cond
149 ((registerv-p val)
150 (cl-assert (registerv-jump-func val) nil
151 "Don't know how to jump to register %s"
152 (single-key-description register))
153 (funcall (registerv-jump-func val) (registerv-data val)))
154 ((and (consp val) (frame-configuration-p (car val)))
155 (set-frame-configuration (car val) (not delete))
156 (goto-char (cadr val)))
157 ((and (consp val) (window-configuration-p (car val)))
158 (set-window-configuration (car val))
159 (goto-char (cadr val)))
160 ((markerp val)
161 (or (marker-buffer val)
162 (error "That register's buffer no longer exists"))
163 (switch-to-buffer (marker-buffer val))
164 (goto-char val))
165 ((and (consp val) (eq (car val) 'file))
166 (find-file (cdr val)))
167 ((and (consp val) (eq (car val) 'file-query))
168 (or (find-buffer-visiting (nth 1 val))
169 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
170 (error "Register access aborted"))
171 (find-file (nth 1 val))
172 (goto-char (nth 2 val)))
174 (error "Register doesn't contain a buffer position or configuration")))))
176 (defun register-swap-out ()
177 "Turn markers into file-query references when a buffer is killed."
178 (and buffer-file-name
179 (dolist (elem register-alist)
180 (and (markerp (cdr elem))
181 (eq (marker-buffer (cdr elem)) (current-buffer))
182 (setcdr elem
183 (list 'file-query
184 buffer-file-name
185 (marker-position (cdr elem))))))))
187 (defun number-to-register (number register)
188 "Store a number in a register.
189 Two args, NUMBER and REGISTER (a character, naming the register).
190 If NUMBER is nil, a decimal number is read from the buffer starting
191 at point, and point moves to the end of that number.
192 Interactively, NUMBER is the prefix arg (none means nil)."
193 (interactive "P\ncNumber to register: ")
194 (set-register register
195 (if number
196 (prefix-numeric-value number)
197 (if (looking-at "\\s-*-?[0-9]+")
198 (progn
199 (goto-char (match-end 0))
200 (string-to-number (match-string 0)))
201 0))))
203 (defun increment-register (prefix register)
204 "Augment contents of REGISTER.
205 Interactively, PREFIX is in raw form.
207 If REGISTER contains a number, add `prefix-numeric-value' of
208 PREFIX to it.
210 If REGISTER is empty or if it contains text, call
211 `append-to-register' with `delete-flag' set to PREFIX."
212 (interactive "P\ncIncrement register: ")
213 (let ((register-val (get-register register)))
214 (cond
215 ((numberp register-val)
216 (let ((number (prefix-numeric-value prefix)))
217 (set-register register (+ number register-val))))
218 ((or (not register-val) (stringp register-val))
219 (append-to-register register (region-beginning) (region-end) prefix))
220 (t (error "Register does not contain a number or text")))))
222 (defun view-register (register)
223 "Display what is contained in register named REGISTER.
224 The Lisp value REGISTER is a character."
225 (interactive "cView register: ")
226 (let ((val (get-register register)))
227 (if (null val)
228 (message "Register %s is empty" (single-key-description register))
229 (with-output-to-temp-buffer "*Output*"
230 (describe-register-1 register t)))))
232 (defun list-registers ()
233 "Display a list of nonempty registers saying briefly what they contain."
234 (interactive)
235 (let ((list (copy-sequence register-alist)))
236 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
237 (with-output-to-temp-buffer "*Output*"
238 (dolist (elt list)
239 (when (get-register (car elt))
240 (describe-register-1 (car elt))
241 (terpri))))))
243 (defun describe-register-1 (register &optional verbose)
244 (princ "Register ")
245 (princ (single-key-description register))
246 (princ " contains ")
247 (let ((val (get-register register)))
248 (cond
249 ((registerv-p val)
250 (if (registerv-print-func val)
251 (funcall (registerv-print-func val) (registerv-data val))
252 (princ "[UNPRINTABLE CONTENTS].")))
254 ((numberp val)
255 (princ val))
257 ((markerp val)
258 (let ((buf (marker-buffer val)))
259 (if (null buf)
260 (princ "a marker in no buffer")
261 (princ "a buffer position:\n buffer ")
262 (princ (buffer-name buf))
263 (princ ", position ")
264 (princ (marker-position val)))))
266 ((and (consp val) (window-configuration-p (car val)))
267 (princ "a window configuration."))
269 ((and (consp val) (frame-configuration-p (car val)))
270 (princ "a frame configuration."))
272 ((and (consp val) (eq (car val) 'file))
273 (princ "the file ")
274 (prin1 (cdr val))
275 (princ "."))
277 ((and (consp val) (eq (car val) 'file-query))
278 (princ "a file-query reference:\n file ")
279 (prin1 (car (cdr val)))
280 (princ ",\n position ")
281 (princ (car (cdr (cdr val))))
282 (princ "."))
284 ((consp val)
285 (if verbose
286 (progn
287 (princ "the rectangle:\n")
288 (while val
289 (princ " ")
290 (princ (car val))
291 (terpri)
292 (setq val (cdr val))))
293 (princ "a rectangle starting with ")
294 (princ (car val))))
296 ((stringp val)
297 (if (eq yank-excluded-properties t)
298 (set-text-properties 0 (length val) nil val)
299 (remove-list-of-text-properties 0 (length val)
300 yank-excluded-properties val))
301 (if verbose
302 (progn
303 (princ "the text:\n")
304 (princ val))
305 (cond
306 ;; Extract first N characters starting with first non-whitespace.
307 ((string-match (format "[^ \t\n].\\{,%d\\}"
308 ;; Deduct 6 for the spaces inserted below.
309 (min 20 (max 0 (- (window-width) 6))))
310 val)
311 (princ "text starting with\n ")
312 (princ (match-string 0 val)))
313 ((string-match "^[ \t\n]+$" val)
314 (princ "whitespace"))
316 (princ "the empty string")))))
318 (princ "Garbage:\n")
319 (if verbose (prin1 val))))))
321 (defun insert-register (register &optional arg)
322 "Insert contents of register REGISTER. (REGISTER is a character.)
323 Normally puts point before and mark after the inserted text.
324 If optional second arg is non-nil, puts mark before and point after.
325 Interactively, second arg is non-nil if prefix arg is supplied."
326 (interactive "*cInsert register: \nP")
327 (push-mark)
328 (let ((val (get-register register)))
329 (cond
330 ((registerv-p val)
331 (cl-assert (registerv-insert-func val) nil
332 "Don't know how to insert register %s"
333 (single-key-description register))
334 (funcall (registerv-insert-func val) (registerv-data val)))
335 ((consp val)
336 (insert-rectangle val))
337 ((stringp val)
338 (insert-for-yank val))
339 ((numberp val)
340 (princ val (current-buffer)))
341 ((and (markerp val) (marker-position val))
342 (princ (marker-position val) (current-buffer)))
344 (error "Register does not contain text"))))
345 (if (not arg) (exchange-point-and-mark)))
347 (defun copy-to-register (register start end &optional delete-flag)
348 "Copy region into register REGISTER.
349 With prefix arg, delete as well.
350 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
351 START and END are buffer positions indicating what to copy."
352 (interactive "cCopy to register: \nr\nP")
353 (set-register register (filter-buffer-substring start end))
354 (setq deactivate-mark t)
355 (cond (delete-flag
356 (delete-region start end))
357 ((called-interactively-p 'interactive)
358 (indicate-copied-region))))
360 (defun append-to-register (register start end &optional delete-flag)
361 "Append region to text in register REGISTER.
362 With prefix arg, delete as well.
363 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
364 START and END are buffer positions indicating what to append."
365 (interactive "cAppend to register: \nr\nP")
366 (let ((reg (get-register register))
367 (text (filter-buffer-substring start end))
368 (separator (and register-separator (get-register register-separator))))
369 (set-register
370 register (cond ((not reg) text)
371 ((stringp reg) (concat reg separator text))
372 (t (error "Register does not contain text")))))
373 (setq deactivate-mark t)
374 (cond (delete-flag
375 (delete-region start end))
376 ((called-interactively-p 'interactive)
377 (indicate-copied-region))))
379 (defun prepend-to-register (register start end &optional delete-flag)
380 "Prepend region to text in register REGISTER.
381 With prefix arg, delete as well.
382 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
383 START and END are buffer positions indicating what to prepend."
384 (interactive "cPrepend to register: \nr\nP")
385 (let ((reg (get-register register))
386 (text (filter-buffer-substring start end))
387 (separator (and register-separator (get-register register-separator))))
388 (set-register
389 register (cond ((not reg) text)
390 ((stringp reg) (concat text separator reg))
391 (t (error "Register does not contain text")))))
392 (setq deactivate-mark t)
393 (cond (delete-flag
394 (delete-region start end))
395 ((called-interactively-p 'interactive)
396 (indicate-copied-region))))
398 (defun copy-rectangle-to-register (register start end &optional delete-flag)
399 "Copy rectangular region into register REGISTER.
400 With prefix arg, delete as well.
401 To insert this register in the buffer, use \\[insert-register].
403 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
404 START and END are buffer positions giving two corners of rectangle."
405 (interactive "cCopy rectangle to register: \nr\nP")
406 (let ((rectangle (if delete-flag
407 (delete-extract-rectangle start end)
408 (extract-rectangle start end))))
409 (set-register register rectangle)
410 (when (and (null delete-flag)
411 (called-interactively-p 'interactive))
412 (setq deactivate-mark t)
413 (indicate-copied-region (length (car rectangle))))))
416 (provide 'register)
417 ;;; register.el ends here