* lisp/simple.el: Use lexical-binding.
[emacs.git] / lisp / register.el
blobae2f7cf3e2ade415a49955dbd0669c20ab21302e
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 (declare-function semantic-insert-foreign-tag "semantic/tag" (foreign-tag))
35 (declare-function semantic-tag-buffer "semantic/tag" (tag))
36 (declare-function semantic-tag-start "semantic/tag" (tag))
38 ;;; Code:
40 (cl-defstruct
41 (registerv (:constructor nil)
42 (:constructor registerv--make (&optional data print-func
43 jump-func insert-func))
44 (:copier nil)
45 (:type vector)
46 :named)
47 (data nil :read-only t)
48 (print-func nil :read-only t)
49 (jump-func nil :read-only t)
50 (insert-func nil :read-only t))
52 (cl-defun registerv-make (data &key print-func jump-func insert-func)
53 "Create a register value object.
55 DATA can be any value.
56 PRINT-FUNC if provided controls how `list-registers' and
57 `view-register' print the register. It should be a function
58 receiving one argument DATA and print text that completes
59 this sentence:
60 Register X contains [TEXT PRINTED BY PRINT-FUNC]
61 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
62 INSERT-FUNC if provided, controls how `insert-register' insert the register.
63 They both receive DATA as argument."
64 (registerv--make data print-func jump-func insert-func))
66 (defvar register-alist nil
67 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
68 NAME is a character (a number). CONTENTS is a string, number, marker, list
69 or a struct returned by `registerv-make'.
70 A list of strings represents a rectangle.
71 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
72 A list of the form (file-query FILE-NAME POSITION) represents
73 position POSITION in the file named FILE-NAME, but query before
74 visiting it.
75 A list of the form (WINDOW-CONFIGURATION POSITION)
76 represents a saved window configuration plus a saved value of point.
77 A list of the form (FRAME-CONFIGURATION POSITION)
78 represents a saved frame configuration plus a saved value of point.")
80 (defgroup register nil
81 "Register commands."
82 :group 'convenience
83 :version "24.3")
85 (defcustom register-separator nil
86 "Register containing the text to put between collected texts, or nil if none.
88 When collecting text with
89 `append-to-register' (resp. `prepend-to-register') contents of
90 this register is added to the beginning (resp. end) of the marked
91 text."
92 :group 'register
93 :type '(choice (const :tag "None" nil)
94 (character :tag "Use register" :value ?+)))
96 (defun get-register (register)
97 "Return contents of Emacs register named REGISTER, or nil if none."
98 (cdr (assq register register-alist)))
100 (defun set-register (register value)
101 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
102 See the documentation of the variable `register-alist' for possible VALUEs."
103 (let ((aelt (assq register register-alist)))
104 (if aelt
105 (setcdr aelt value)
106 (push (cons register value) register-alist))
107 value))
109 (defun point-to-register (register &optional arg)
110 "Store current location of point in register REGISTER.
111 With prefix argument, store current frame configuration.
112 Use \\[jump-to-register] to go to that location or restore that configuration.
113 Argument is a character, naming the register."
114 (interactive "cPoint to register: \nP")
115 ;; Turn the marker into a file-ref if the buffer is killed.
116 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
117 (set-register register
118 (if arg (list (current-frame-configuration) (point-marker))
119 (point-marker))))
121 (defun window-configuration-to-register (register &optional _arg)
122 "Store the window configuration of the selected frame in register REGISTER.
123 Use \\[jump-to-register] to restore the configuration.
124 Argument is a character, naming the register."
125 (interactive "cWindow configuration to register: \nP")
126 ;; current-window-configuration does not include the value
127 ;; of point in the current buffer, so record that separately.
128 (set-register register (list (current-window-configuration) (point-marker))))
130 (defun frame-configuration-to-register (register &optional _arg)
131 "Store the window configuration of all frames in register REGISTER.
132 Use \\[jump-to-register] to restore the configuration.
133 Argument is a character, naming the register."
134 (interactive "cFrame configuration to register: \nP")
135 ;; current-frame-configuration does not include the value
136 ;; of point in the current buffer, so record that separately.
137 (set-register register (list (current-frame-configuration) (point-marker))))
139 (defalias 'register-to-point 'jump-to-register)
140 (defun jump-to-register (register &optional delete)
141 "Move point to location stored in a register.
142 If the register contains a file name, find that file.
143 \(To put a file name in a register, you must use `set-register'.)
144 If the register contains a window configuration (one frame) or a frame
145 configuration (all frames), restore that frame or all frames accordingly.
146 First argument is a character, naming the register.
147 Optional second arg non-nil (interactively, prefix argument) says to
148 delete any existing frames that the frame configuration doesn't mention.
149 \(Otherwise, these frames are iconified.)"
150 (interactive "cJump to register: \nP")
151 (let ((val (get-register register)))
152 (cond
153 ((registerv-p val)
154 (cl-assert (registerv-jump-func val) nil
155 "Don't know how to jump to register %s"
156 (single-key-description register))
157 (funcall (registerv-jump-func val) (registerv-data val)))
158 ((and (consp val) (frame-configuration-p (car val)))
159 (set-frame-configuration (car val) (not delete))
160 (goto-char (cadr val)))
161 ((and (consp val) (window-configuration-p (car val)))
162 (set-window-configuration (car val))
163 (goto-char (cadr val)))
164 ((markerp val)
165 (or (marker-buffer val)
166 (error "That register's buffer no longer exists"))
167 (switch-to-buffer (marker-buffer val))
168 (goto-char val))
169 ((and (consp val) (eq (car val) 'file))
170 (find-file (cdr val)))
171 ((and (consp val) (eq (car val) 'file-query))
172 (or (find-buffer-visiting (nth 1 val))
173 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
174 (error "Register access aborted"))
175 (find-file (nth 1 val))
176 (goto-char (nth 2 val)))
177 ((and (fboundp 'semantic-foreign-tag-p)
178 semantic-mode
179 (semantic-foreign-tag-p val))
180 (switch-to-buffer (semantic-tag-buffer val))
181 (goto-char (semantic-tag-start val)))
183 (error "Register doesn't contain a buffer position or configuration")))))
185 (defun register-swap-out ()
186 "Turn markers into file-query references when a buffer is killed."
187 (and buffer-file-name
188 (dolist (elem register-alist)
189 (and (markerp (cdr elem))
190 (eq (marker-buffer (cdr elem)) (current-buffer))
191 (setcdr elem
192 (list 'file-query
193 buffer-file-name
194 (marker-position (cdr elem))))))))
196 (defun number-to-register (number register)
197 "Store a number in a register.
198 Two args, NUMBER and REGISTER (a character, naming the register).
199 If NUMBER is nil, a decimal number is read from the buffer starting
200 at point, and point moves to the end of that number.
201 Interactively, NUMBER is the prefix arg (none means nil)."
202 (interactive "P\ncNumber to register: ")
203 (set-register register
204 (if number
205 (prefix-numeric-value number)
206 (if (looking-at "\\s-*-?[0-9]+")
207 (progn
208 (goto-char (match-end 0))
209 (string-to-number (match-string 0)))
210 0))))
212 (defun increment-register (prefix register)
213 "Augment contents of REGISTER.
214 Interactively, PREFIX is in raw form.
216 If REGISTER contains a number, add `prefix-numeric-value' of
217 PREFIX to it.
219 If REGISTER is empty or if it contains text, call
220 `append-to-register' with `delete-flag' set to PREFIX."
221 (interactive "P\ncIncrement register: ")
222 (let ((register-val (get-register register)))
223 (cond
224 ((numberp register-val)
225 (let ((number (prefix-numeric-value prefix)))
226 (set-register register (+ number register-val))))
227 ((or (not register-val) (stringp register-val))
228 (append-to-register register (region-beginning) (region-end) prefix))
229 (t (error "Register does not contain a number or text")))))
231 (defun view-register (register)
232 "Display what is contained in register named REGISTER.
233 The Lisp value REGISTER is a character."
234 (interactive "cView register: ")
235 (let ((val (get-register register)))
236 (if (null val)
237 (message "Register %s is empty" (single-key-description register))
238 (with-output-to-temp-buffer "*Output*"
239 (describe-register-1 register t)))))
241 (defun list-registers ()
242 "Display a list of nonempty registers saying briefly what they contain."
243 (interactive)
244 (let ((list (copy-sequence register-alist)))
245 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
246 (with-output-to-temp-buffer "*Output*"
247 (dolist (elt list)
248 (when (get-register (car elt))
249 (describe-register-1 (car elt))
250 (terpri))))))
252 (defun describe-register-1 (register &optional verbose)
253 (princ "Register ")
254 (princ (single-key-description register))
255 (princ " contains ")
256 (let ((val (get-register register)))
257 (cond
258 ((registerv-p val)
259 (if (registerv-print-func val)
260 (funcall (registerv-print-func val) (registerv-data val))
261 (princ "[UNPRINTABLE CONTENTS].")))
263 ((numberp val)
264 (princ val))
266 ((markerp val)
267 (let ((buf (marker-buffer val)))
268 (if (null buf)
269 (princ "a marker in no buffer")
270 (princ "a buffer position:\n buffer ")
271 (princ (buffer-name buf))
272 (princ ", position ")
273 (princ (marker-position val)))))
275 ((and (consp val) (window-configuration-p (car val)))
276 (princ "a window configuration."))
278 ((and (consp val) (frame-configuration-p (car val)))
279 (princ "a frame configuration."))
281 ((and (consp val) (eq (car val) 'file))
282 (princ "the file ")
283 (prin1 (cdr val))
284 (princ "."))
286 ((and (consp val) (eq (car val) 'file-query))
287 (princ "a file-query reference:\n file ")
288 (prin1 (car (cdr val)))
289 (princ ",\n position ")
290 (princ (car (cdr (cdr val))))
291 (princ "."))
293 ((consp val)
294 (if verbose
295 (progn
296 (princ "the rectangle:\n")
297 (while val
298 (princ " ")
299 (princ (car val))
300 (terpri)
301 (setq val (cdr val))))
302 (princ "a rectangle starting with ")
303 (princ (car val))))
305 ((stringp val)
306 (if (eq yank-excluded-properties t)
307 (set-text-properties 0 (length val) nil val)
308 (remove-list-of-text-properties 0 (length val)
309 yank-excluded-properties val))
310 (if verbose
311 (progn
312 (princ "the text:\n")
313 (princ val))
314 (cond
315 ;; Extract first N characters starting with first non-whitespace.
316 ((string-match (format "[^ \t\n].\\{,%d\\}"
317 ;; Deduct 6 for the spaces inserted below.
318 (min 20 (max 0 (- (window-width) 6))))
319 val)
320 (princ "text starting with\n ")
321 (princ (match-string 0 val)))
322 ((string-match "^[ \t\n]+$" val)
323 (princ "whitespace"))
325 (princ "the empty string")))))
327 (princ "Garbage:\n")
328 (if verbose (prin1 val))))))
330 (defun insert-register (register &optional arg)
331 "Insert contents of register REGISTER. (REGISTER is a character.)
332 Normally puts point before and mark after the inserted text.
333 If optional second arg is non-nil, puts mark before and point after.
334 Interactively, second arg is non-nil if prefix arg is supplied."
335 (interactive "*cInsert register: \nP")
336 (push-mark)
337 (let ((val (get-register register)))
338 (cond
339 ((registerv-p val)
340 (cl-assert (registerv-insert-func val) nil
341 "Don't know how to insert register %s"
342 (single-key-description register))
343 (funcall (registerv-insert-func val) (registerv-data val)))
344 ((consp val)
345 (insert-rectangle val))
346 ((stringp val)
347 (insert-for-yank val))
348 ((numberp val)
349 (princ val (current-buffer)))
350 ((and (markerp val) (marker-position val))
351 (princ (marker-position val) (current-buffer)))
352 ((and (fboundp 'semantic-foreign-tag-p)
353 semantic-mode
354 (semantic-foreign-tag-p val))
355 (semantic-insert-foreign-tag val))
357 (error "Register does not contain text"))))
358 (if (not arg) (exchange-point-and-mark)))
360 (defun copy-to-register (register start end &optional delete-flag)
361 "Copy region into 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 copy."
365 (interactive "cCopy to register: \nr\nP")
366 (set-register register (filter-buffer-substring start end))
367 (setq deactivate-mark t)
368 (cond (delete-flag
369 (delete-region start end))
370 ((called-interactively-p 'interactive)
371 (indicate-copied-region))))
373 (defun append-to-register (register start end &optional delete-flag)
374 "Append region to text in register REGISTER.
375 With prefix arg, delete as well.
376 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
377 START and END are buffer positions indicating what to append."
378 (interactive "cAppend to register: \nr\nP")
379 (let ((reg (get-register register))
380 (text (filter-buffer-substring start end))
381 (separator (and register-separator (get-register register-separator))))
382 (set-register
383 register (cond ((not reg) text)
384 ((stringp reg) (concat reg separator text))
385 (t (error "Register does not contain text")))))
386 (setq deactivate-mark t)
387 (cond (delete-flag
388 (delete-region start end))
389 ((called-interactively-p 'interactive)
390 (indicate-copied-region))))
392 (defun prepend-to-register (register start end &optional delete-flag)
393 "Prepend region to text in register REGISTER.
394 With prefix arg, delete as well.
395 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
396 START and END are buffer positions indicating what to prepend."
397 (interactive "cPrepend to register: \nr\nP")
398 (let ((reg (get-register register))
399 (text (filter-buffer-substring start end))
400 (separator (and register-separator (get-register register-separator))))
401 (set-register
402 register (cond ((not reg) text)
403 ((stringp reg) (concat text separator reg))
404 (t (error "Register does not contain text")))))
405 (setq deactivate-mark t)
406 (cond (delete-flag
407 (delete-region start end))
408 ((called-interactively-p 'interactive)
409 (indicate-copied-region))))
411 (defun copy-rectangle-to-register (register start end &optional delete-flag)
412 "Copy rectangular region into register REGISTER.
413 With prefix arg, delete as well.
414 To insert this register in the buffer, use \\[insert-register].
416 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
417 START and END are buffer positions giving two corners of rectangle."
418 (interactive "cCopy rectangle to register: \nr\nP")
419 (let ((rectangle (if delete-flag
420 (delete-extract-rectangle start end)
421 (extract-rectangle start end))))
422 (set-register register rectangle)
423 (when (and (null delete-flag)
424 (called-interactively-p 'interactive))
425 (setq deactivate-mark t)
426 (indicate-copied-region (length (car rectangle))))))
429 (provide 'register)
430 ;;; register.el ends here