Reduce use of (require 'cl).
[bpt/emacs.git] / lisp / register.el
CommitLineData
55535639 1;;; register.el --- register commands for Emacs
c88ab9ce 2
acaf905b 3;; Copyright (C) 1985, 1993-1994, 2001-2012 Free Software Foundation, Inc.
9750e079 4
4821e2af 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: internal
bd78fa1d 7;; Package: emacs
4821e2af 8
efeae993
RS
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
efeae993 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
efeae993
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
efeae993 23
d9ecc911
ER
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
f58e0fd5 31(eval-when-compile (require 'cl-lib))
6302e0d3 32
4cf1d7e3
CY
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))
36
8daffab7
JL
37;;; Global key bindings
38
09c01323
DN
39(define-key ctl-x-r-map "\C-@" 'point-to-register)
40(define-key ctl-x-r-map [?\C-\ ] 'point-to-register)
41(define-key ctl-x-r-map " " 'point-to-register)
42(define-key ctl-x-r-map "j" 'jump-to-register)
43(define-key ctl-x-r-map "s" 'copy-to-register)
44(define-key ctl-x-r-map "x" 'copy-to-register)
45(define-key ctl-x-r-map "i" 'insert-register)
46(define-key ctl-x-r-map "g" 'insert-register)
47(define-key ctl-x-r-map "r" 'copy-rectangle-to-register)
48(define-key ctl-x-r-map "n" 'number-to-register)
49(define-key ctl-x-r-map "+" 'increment-register)
50(define-key ctl-x-r-map "w" 'window-configuration-to-register)
51(define-key ctl-x-r-map "f" 'frame-configuration-to-register)
8daffab7 52
4821e2af 53;;; Code:
efeae993 54
f58e0fd5 55(cl-defstruct
6302e0d3
LL
56 (registerv (:constructor nil)
57 (:constructor registerv--make (&optional data print-func
58 jump-func insert-func))
59 (:copier nil)
60 (:type vector)
61 :named)
62 (data nil :read-only t)
63 (print-func nil :read-only t)
64 (jump-func nil :read-only t)
65 (insert-func nil :read-only t))
66
f58e0fd5 67(cl-defun registerv-make (data &key print-func jump-func insert-func)
6302e0d3
LL
68 "Create a register value object.
69
70DATA can be any value.
71PRINT-FUNC if provided controls how `list-registers' and
72`view-register' print the register. It should be a function
9173deec 73receiving one argument DATA and print text that completes
6302e0d3
LL
74this sentence:
75 Register X contains [TEXT PRINTED BY PRINT-FUNC]
76JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
77INSERT-FUNC if provided, controls how `insert-register' insert the register.
78They both receive DATA as argument."
79 (registerv--make data print-func jump-func insert-func))
80
efeae993
RS
81(defvar register-alist nil
82 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
6302e0d3
LL
83NAME is a character (a number). CONTENTS is a string, number, marker, list
84or a struct returned by `registerv-make'.
22073dda 85A list of strings represents a rectangle.
5858bcc4
CY
86A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
87A list of the form (file-query FILE-NAME POSITION) represents
88 position POSITION in the file named FILE-NAME, but query before
89 visiting it.
070c2506
KH
90A list of the form (WINDOW-CONFIGURATION POSITION)
91 represents a saved window configuration plus a saved value of point.
92A list of the form (FRAME-CONFIGURATION POSITION)
93 represents a saved frame configuration plus a saved value of point.")
efeae993 94
1a86cc81
JB
95(defun get-register (register)
96 "Return contents of Emacs register named REGISTER, or nil if none."
97 (cdr (assq register register-alist)))
efeae993 98
1b8cac5d
RS
99(defun set-register (register value)
100 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
1a86cc81 101See the documentation of the variable `register-alist' for possible VALUEs."
1b8cac5d 102 (let ((aelt (assq register register-alist)))
efeae993
RS
103 (if aelt
104 (setcdr aelt value)
ddbb3cc7 105 (push (cons register value) register-alist))
efeae993
RS
106 value))
107
1b8cac5d 108(defun point-to-register (register &optional arg)
b42e6156 109 "Store current location of point in register REGISTER.
0cc89026 110With prefix argument, store current frame configuration.
b42e6156 111Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 112Argument is a character, naming the register."
b42e6156 113 (interactive "cPoint to register: \nP")
ddbb3cc7
SM
114 ;; Turn the marker into a file-ref if the buffer is killed.
115 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 116 (set-register register
070c2506
KH
117 (if arg (list (current-frame-configuration) (point-marker))
118 (point-marker))))
efeae993 119
06b60517 120(defun window-configuration-to-register (register &optional _arg)
83b5d757
RS
121 "Store the window configuration of the selected frame in register REGISTER.
122Use \\[jump-to-register] to restore the configuration.
123Argument is a character, naming the register."
5f517806 124 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
125 ;; current-window-configuration does not include the value
126 ;; of point in the current buffer, so record that separately.
070c2506 127 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 128
06b60517 129(defun frame-configuration-to-register (register &optional _arg)
83b5d757
RS
130 "Store the window configuration of all frames in register REGISTER.
131Use \\[jump-to-register] to restore the configuration.
132Argument is a character, naming the register."
5f517806 133 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
134 ;; current-frame-configuration does not include the value
135 ;; of point in the current buffer, so record that separately.
070c2506 136 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 137
31e1d920 138(defalias 'register-to-point 'jump-to-register)
1b8cac5d 139(defun jump-to-register (register &optional delete)
efeae993 140 "Move point to location stored in a register.
22073dda 141If the register contains a file name, find that file.
1a86cc81 142\(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
143If the register contains a window configuration (one frame) or a frame
144configuration (all frames), restore that frame or all frames accordingly.
e7683fff 145First argument is a character, naming the register.
1542ad37
RS
146Optional second arg non-nil (interactively, prefix argument) says to
147delete any existing frames that the frame configuration doesn't mention.
a21d94f9 148\(Otherwise, these frames are iconified.)"
e7683fff 149 (interactive "cJump to register: \nP")
1b8cac5d 150 (let ((val (get-register register)))
376a7584 151 (cond
6302e0d3 152 ((registerv-p val)
f58e0fd5 153 (cl-assert (registerv-jump-func val) nil
6302e0d3
LL
154 "Don't know how to jump to register %s"
155 (single-key-description register))
156 (funcall (registerv-jump-func val) (registerv-data val)))
b4a91f43
KH
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)))
376a7584 163 ((markerp val)
8c4ca60c
KH
164 (or (marker-buffer val)
165 (error "That register's buffer no longer exists"))
376a7584
JB
166 (switch-to-buffer (marker-buffer val))
167 (goto-char val))
22073dda
RS
168 ((and (consp val) (eq (car val) 'file))
169 (find-file (cdr val)))
28cbd14d
RS
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)))
4cf1d7e3
CY
176 ((and (fboundp 'semantic-foreign-tag-p)
177 semantic-mode
178 (semantic-foreign-tag-p val))
179 (switch-to-buffer (semantic-tag-buffer val))
180 (goto-char (semantic-tag-start val)))
376a7584
JB
181 (t
182 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 183
28cbd14d 184(defun register-swap-out ()
ddbb3cc7 185 "Turn markers into file-query references when a buffer is killed."
28cbd14d 186 (and buffer-file-name
ddbb3cc7
SM
187 (dolist (elem register-alist)
188 (and (markerp (cdr elem))
189 (eq (marker-buffer (cdr elem)) (current-buffer))
190 (setcdr elem
191 (list 'file-query
192 buffer-file-name
193 (marker-position (cdr elem))))))))
28cbd14d 194
0e07a458 195(defun number-to-register (number register)
070c2506
KH
196 "Store a number in a register.
197Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
198If NUMBER is nil, a decimal number is read from the buffer starting
199at point, and point moves to the end of that number.
070c2506
KH
200Interactively, NUMBER is the prefix arg (none means nil)."
201 (interactive "P\ncNumber to register: ")
4f23d31c 202 (set-register register
0e07a458
KH
203 (if number
204 (prefix-numeric-value number)
4d2caa07
KH
205 (if (looking-at "\\s-*-?[0-9]+")
206 (progn
207 (goto-char (match-end 0))
fe22eed0 208 (string-to-number (match-string 0)))
070c2506
KH
209 0))))
210
0e07a458 211(defun increment-register (number register)
070c2506 212 "Add NUMBER to the contents of register REGISTER.
0e07a458 213Interactively, NUMBER is the prefix arg."
070c2506 214 (interactive "p\ncIncrement register: ")
0e07a458 215 (or (numberp (get-register register))
070c2506 216 (error "Register does not contain a number"))
0e07a458 217 (set-register register (+ number (get-register register))))
efeae993 218
1b8cac5d 219(defun view-register (register)
efeae993 220 "Display what is contained in register named REGISTER.
1b8cac5d 221The Lisp value REGISTER is a character."
efeae993 222 (interactive "cView register: ")
1b8cac5d 223 (let ((val (get-register register)))
efeae993 224 (if (null val)
1b8cac5d 225 (message "Register %s is empty" (single-key-description register))
efeae993 226 (with-output-to-temp-buffer "*Output*"
92840a31
RS
227 (describe-register-1 register t)))))
228
229(defun list-registers ()
230 "Display a list of nonempty registers saying briefly what they contain."
231 (interactive)
232 (let ((list (copy-sequence register-alist)))
233 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
234 (with-output-to-temp-buffer "*Output*"
235 (dolist (elt list)
236 (when (get-register (car elt))
237 (describe-register-1 (car elt))
238 (terpri))))))
239
240(defun describe-register-1 (register &optional verbose)
241 (princ "Register ")
242 (princ (single-key-description register))
243 (princ " contains ")
6ed21409
RS
244 (let ((val (get-register register)))
245 (cond
6302e0d3
LL
246 ((registerv-p val)
247 (if (registerv-print-func val)
248 (funcall (registerv-print-func val) (registerv-data val))
249 (princ "[UNPRINTABLE CONTENTS].")))
250
6ed21409
RS
251 ((numberp val)
252 (princ val))
253
254 ((markerp val)
255 (let ((buf (marker-buffer val)))
256 (if (null buf)
257 (princ "a marker in no buffer")
258 (princ "a buffer position:\n buffer ")
259 (princ (buffer-name buf))
260 (princ ", position ")
261 (princ (marker-position val)))))
262
263 ((and (consp val) (window-configuration-p (car val)))
264 (princ "a window configuration."))
265
266 ((and (consp val) (frame-configuration-p (car val)))
267 (princ "a frame configuration."))
268
269 ((and (consp val) (eq (car val) 'file))
270 (princ "the file ")
271 (prin1 (cdr val))
272 (princ "."))
273
274 ((and (consp val) (eq (car val) 'file-query))
275 (princ "a file-query reference:\n file ")
276 (prin1 (car (cdr val)))
277 (princ ",\n position ")
278 (princ (car (cdr (cdr val))))
279 (princ "."))
280
281 ((consp val)
282 (if verbose
283 (progn
284 (princ "the rectangle:\n")
285 (while val
286 (princ " ")
287 (princ (car val))
288 (terpri)
289 (setq val (cdr val))))
290 (princ "a rectangle starting with ")
291 (princ (car val))))
292
293 ((stringp val)
9c7cc04b
RS
294 (if (eq yank-excluded-properties t)
295 (set-text-properties 0 (length val) nil val)
296 (remove-list-of-text-properties 0 (length val)
297 yank-excluded-properties val))
6ed21409
RS
298 (if verbose
299 (progn
300 (princ "the text:\n")
301 (princ val))
f1180544 302 (cond
13d6f302
RS
303 ;; Extract first N characters starting with first non-whitespace.
304 ((string-match (format "[^ \t\n].\\{,%d\\}"
305 ;; Deduct 6 for the spaces inserted below.
306 (min 20 (max 0 (- (window-width) 6))))
307 val)
308 (princ "text starting with\n ")
309 (princ (match-string 0 val)))
310 ((string-match "^[ \t\n]+$" val)
311 (princ "whitespace"))
312 (t
313 (princ "the empty string")))))
6ed21409
RS
314 (t
315 (princ "Garbage:\n")
316 (if verbose (prin1 val))))))
efeae993 317
1b8cac5d
RS
318(defun insert-register (register &optional arg)
319 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
320Normally puts point before and mark after the inserted text.
321If optional second arg is non-nil, puts mark before and point after.
322Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 323 (interactive "*cInsert register: \nP")
efeae993 324 (push-mark)
1b8cac5d 325 (let ((val (get-register register)))
cbd4993c 326 (cond
6302e0d3 327 ((registerv-p val)
f58e0fd5 328 (cl-assert (registerv-insert-func val) nil
6302e0d3
LL
329 "Don't know how to insert register %s"
330 (single-key-description register))
331 (funcall (registerv-insert-func val) (registerv-data val)))
2d43b8c9
LL
332 ((consp val)
333 (insert-rectangle val))
cbd4993c 334 ((stringp val)
e7c765c3 335 (insert-for-yank val))
070c2506 336 ((numberp val)
cbd4993c
KH
337 (princ val (current-buffer)))
338 ((and (markerp val) (marker-position val))
339 (princ (marker-position val) (current-buffer)))
4cf1d7e3
CY
340 ((and (fboundp 'semantic-foreign-tag-p)
341 semantic-mode
342 (semantic-foreign-tag-p val))
343 (semantic-insert-foreign-tag val))
cbd4993c
KH
344 (t
345 (error "Register does not contain text"))))
efeae993
RS
346 (if (not arg) (exchange-point-and-mark)))
347
1b8cac5d 348(defun copy-to-register (register start end &optional delete-flag)
1a86cc81
JB
349 "Copy region into register REGISTER.
350With prefix arg, delete as well.
1b8cac5d 351Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
352START and END are buffer positions indicating what to copy."
353 (interactive "cCopy to register: \nr\nP")
13191e32 354 (set-register register (filter-buffer-substring start end))
efeae993
RS
355 (if delete-flag (delete-region start end)))
356
1b8cac5d
RS
357(defun append-to-register (register start end &optional delete-flag)
358 "Append region to text in register REGISTER.
359With prefix arg, delete as well.
360Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
361START and END are buffer positions indicating what to append."
362 (interactive "cAppend to register: \nr\nP")
c81f72ce
TTN
363 (let ((reg (get-register register))
364 (text (filter-buffer-substring start end)))
365 (set-register
366 register (cond ((not reg) text)
367 ((stringp reg) (concat reg text))
368 (t (error "Register does not contain text")))))
efeae993
RS
369 (if delete-flag (delete-region start end)))
370
1b8cac5d
RS
371(defun prepend-to-register (register start end &optional delete-flag)
372 "Prepend region to text in register REGISTER.
373With prefix arg, delete as well.
374Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
375START and END are buffer positions indicating what to prepend."
376 (interactive "cPrepend to register: \nr\nP")
c81f72ce
TTN
377 (let ((reg (get-register register))
378 (text (filter-buffer-substring start end)))
379 (set-register
380 register (cond ((not reg) text)
381 ((stringp reg) (concat text reg))
382 (t (error "Register does not contain text")))))
efeae993
RS
383 (if delete-flag (delete-region start end)))
384
1b8cac5d
RS
385(defun copy-rectangle-to-register (register start end &optional delete-flag)
386 "Copy rectangular region into register REGISTER.
1a86cc81
JB
387With prefix arg, delete as well.
388To insert this register in the buffer, use \\[insert-register].
5ef5d6ce
RS
389
390Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
391START and END are buffer positions giving two corners of rectangle."
392 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 393 (set-register register
efeae993
RS
394 (if delete-flag
395 (delete-extract-rectangle start end)
396 (extract-rectangle start end))))
c88ab9ce 397
0f214cdf 398(provide 'register)
c88ab9ce 399;;; register.el ends here