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