Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / register.el
CommitLineData
55535639 1;;; register.el --- register commands for Emacs
c88ab9ce 2
c90f2757 3;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
9750e079 5
4821e2af 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: internal
4821e2af 8
efeae993
RS
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
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
efeae993
RS
14;; 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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
efeae993 25
d9ecc911
ER
26;;; Commentary:
27
28;; This package of functions emulates and somewhat extends the venerable
29;; TECO's `register' feature, which permits you to save various useful
30;; pieces of buffer state to named variables. The entry points are
31;; documented in the Emacs user's manual.
32
4821e2af 33;;; Code:
efeae993
RS
34
35(defvar register-alist nil
36 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
070c2506 37NAME is a character (a number). CONTENTS is a string, number, marker or list.
22073dda 38A list of strings represents a rectangle.
28cbd14d
RS
39A list of the form (file . NAME) represents the file named NAME.
40A list of the form (file-query NAME POSITION) represents position POSITION
070c2506
KH
41 in the file named NAME, but query before visiting it.
42A list of the form (WINDOW-CONFIGURATION POSITION)
43 represents a saved window configuration plus a saved value of point.
44A list of the form (FRAME-CONFIGURATION POSITION)
45 represents a saved frame configuration plus a saved value of point.")
efeae993 46
1b8cac5d
RS
47(defun get-register (reg)
48 "Return contents of Emacs register named REG, or nil if none."
49 (cdr (assq reg register-alist)))
efeae993 50
1b8cac5d
RS
51(defun set-register (register value)
52 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
22073dda 53See the documentation of the variable `register-alist' for possible VALUE."
1b8cac5d 54 (let ((aelt (assq register register-alist)))
efeae993
RS
55 (if aelt
56 (setcdr aelt value)
ddbb3cc7 57 (push (cons register value) register-alist))
efeae993
RS
58 value))
59
1b8cac5d 60(defun point-to-register (register &optional arg)
b42e6156 61 "Store current location of point in register REGISTER.
0cc89026 62With prefix argument, store current frame configuration.
b42e6156 63Use \\[jump-to-register] to go to that location or restore that configuration.
efeae993 64Argument is a character, naming the register."
b42e6156 65 (interactive "cPoint to register: \nP")
ddbb3cc7
SM
66 ;; Turn the marker into a file-ref if the buffer is killed.
67 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
1b8cac5d 68 (set-register register
070c2506
KH
69 (if arg (list (current-frame-configuration) (point-marker))
70 (point-marker))))
efeae993 71
1b8cac5d 72(defun window-configuration-to-register (register &optional arg)
83b5d757
RS
73 "Store the window configuration of the selected frame in register REGISTER.
74Use \\[jump-to-register] to restore the configuration.
75Argument is a character, naming the register."
5f517806 76 (interactive "cWindow configuration to register: \nP")
b4a91f43
KH
77 ;; current-window-configuration does not include the value
78 ;; of point in the current buffer, so record that separately.
070c2506 79 (set-register register (list (current-window-configuration) (point-marker))))
83b5d757 80
1b8cac5d 81(defun frame-configuration-to-register (register &optional arg)
83b5d757
RS
82 "Store the window configuration of all frames in register REGISTER.
83Use \\[jump-to-register] to restore the configuration.
84Argument is a character, naming the register."
5f517806 85 (interactive "cFrame configuration to register: \nP")
b4a91f43
KH
86 ;; current-frame-configuration does not include the value
87 ;; of point in the current buffer, so record that separately.
070c2506 88 (set-register register (list (current-frame-configuration) (point-marker))))
83b5d757 89
31e1d920 90(defalias 'register-to-point 'jump-to-register)
1b8cac5d 91(defun jump-to-register (register &optional delete)
efeae993 92 "Move point to location stored in a register.
22073dda
RS
93If the register contains a file name, find that file.
94 \(To put a file name in a register, you must use `set-register'.)
83b5d757
RS
95If the register contains a window configuration (one frame) or a frame
96configuration (all frames), restore that frame or all frames accordingly.
e7683fff 97First argument is a character, naming the register.
1542ad37
RS
98Optional second arg non-nil (interactively, prefix argument) says to
99delete any existing frames that the frame configuration doesn't mention.
a21d94f9 100\(Otherwise, these frames are iconified.)"
e7683fff 101 (interactive "cJump to register: \nP")
1b8cac5d 102 (let ((val (get-register register)))
376a7584 103 (cond
b4a91f43
KH
104 ((and (consp val) (frame-configuration-p (car val)))
105 (set-frame-configuration (car val) (not delete))
106 (goto-char (cadr val)))
107 ((and (consp val) (window-configuration-p (car val)))
108 (set-window-configuration (car val))
109 (goto-char (cadr val)))
376a7584 110 ((markerp val)
8c4ca60c
KH
111 (or (marker-buffer val)
112 (error "That register's buffer no longer exists"))
376a7584
JB
113 (switch-to-buffer (marker-buffer val))
114 (goto-char val))
22073dda
RS
115 ((and (consp val) (eq (car val) 'file))
116 (find-file (cdr val)))
28cbd14d
RS
117 ((and (consp val) (eq (car val) 'file-query))
118 (or (find-buffer-visiting (nth 1 val))
119 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
120 (error "Register access aborted"))
121 (find-file (nth 1 val))
122 (goto-char (nth 2 val)))
376a7584
JB
123 (t
124 (error "Register doesn't contain a buffer position or configuration")))))
efeae993 125
28cbd14d 126(defun register-swap-out ()
ddbb3cc7 127 "Turn markers into file-query references when a buffer is killed."
28cbd14d 128 (and buffer-file-name
ddbb3cc7
SM
129 (dolist (elem register-alist)
130 (and (markerp (cdr elem))
131 (eq (marker-buffer (cdr elem)) (current-buffer))
132 (setcdr elem
133 (list 'file-query
134 buffer-file-name
135 (marker-position (cdr elem))))))))
28cbd14d 136
0e07a458 137(defun number-to-register (number register)
070c2506
KH
138 "Store a number in a register.
139Two args, NUMBER and REGISTER (a character, naming the register).
4d2caa07
KH
140If NUMBER is nil, a decimal number is read from the buffer starting
141at point, and point moves to the end of that number.
070c2506
KH
142Interactively, NUMBER is the prefix arg (none means nil)."
143 (interactive "P\ncNumber to register: ")
4f23d31c 144 (set-register register
0e07a458
KH
145 (if number
146 (prefix-numeric-value number)
4d2caa07
KH
147 (if (looking-at "\\s-*-?[0-9]+")
148 (progn
149 (goto-char (match-end 0))
fe22eed0 150 (string-to-number (match-string 0)))
070c2506
KH
151 0))))
152
0e07a458 153(defun increment-register (number register)
070c2506 154 "Add NUMBER to the contents of register REGISTER.
0e07a458 155Interactively, NUMBER is the prefix arg."
070c2506 156 (interactive "p\ncIncrement register: ")
0e07a458 157 (or (numberp (get-register register))
070c2506 158 (error "Register does not contain a number"))
0e07a458 159 (set-register register (+ number (get-register register))))
efeae993 160
1b8cac5d 161(defun view-register (register)
efeae993 162 "Display what is contained in register named REGISTER.
1b8cac5d 163The Lisp value REGISTER is a character."
efeae993 164 (interactive "cView register: ")
1b8cac5d 165 (let ((val (get-register register)))
efeae993 166 (if (null val)
1b8cac5d 167 (message "Register %s is empty" (single-key-description register))
efeae993 168 (with-output-to-temp-buffer "*Output*"
92840a31
RS
169 (describe-register-1 register t)))))
170
171(defun list-registers ()
172 "Display a list of nonempty registers saying briefly what they contain."
173 (interactive)
174 (let ((list (copy-sequence register-alist)))
175 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
176 (with-output-to-temp-buffer "*Output*"
177 (dolist (elt list)
178 (when (get-register (car elt))
179 (describe-register-1 (car elt))
180 (terpri))))))
181
182(defun describe-register-1 (register &optional verbose)
183 (princ "Register ")
184 (princ (single-key-description register))
185 (princ " contains ")
6ed21409
RS
186 (let ((val (get-register register)))
187 (cond
188 ((numberp val)
189 (princ val))
190
191 ((markerp val)
192 (let ((buf (marker-buffer val)))
193 (if (null buf)
194 (princ "a marker in no buffer")
195 (princ "a buffer position:\n buffer ")
196 (princ (buffer-name buf))
197 (princ ", position ")
198 (princ (marker-position val)))))
199
200 ((and (consp val) (window-configuration-p (car val)))
201 (princ "a window configuration."))
202
203 ((and (consp val) (frame-configuration-p (car val)))
204 (princ "a frame configuration."))
205
206 ((and (consp val) (eq (car val) 'file))
207 (princ "the file ")
208 (prin1 (cdr val))
209 (princ "."))
210
211 ((and (consp val) (eq (car val) 'file-query))
212 (princ "a file-query reference:\n file ")
213 (prin1 (car (cdr val)))
214 (princ ",\n position ")
215 (princ (car (cdr (cdr val))))
216 (princ "."))
217
218 ((consp val)
219 (if verbose
220 (progn
221 (princ "the rectangle:\n")
222 (while val
223 (princ " ")
224 (princ (car val))
225 (terpri)
226 (setq val (cdr val))))
227 (princ "a rectangle starting with ")
228 (princ (car val))))
229
230 ((stringp val)
9c7cc04b
RS
231 (if (eq yank-excluded-properties t)
232 (set-text-properties 0 (length val) nil val)
233 (remove-list-of-text-properties 0 (length val)
234 yank-excluded-properties val))
6ed21409
RS
235 (if verbose
236 (progn
237 (princ "the text:\n")
238 (princ val))
f1180544 239 (cond
13d6f302
RS
240 ;; Extract first N characters starting with first non-whitespace.
241 ((string-match (format "[^ \t\n].\\{,%d\\}"
242 ;; Deduct 6 for the spaces inserted below.
243 (min 20 (max 0 (- (window-width) 6))))
244 val)
245 (princ "text starting with\n ")
246 (princ (match-string 0 val)))
247 ((string-match "^[ \t\n]+$" val)
248 (princ "whitespace"))
249 (t
250 (princ "the empty string")))))
6ed21409
RS
251 (t
252 (princ "Garbage:\n")
253 (if verbose (prin1 val))))))
efeae993 254
1b8cac5d
RS
255(defun insert-register (register &optional arg)
256 "Insert contents of register REGISTER. (REGISTER is a character.)
efeae993
RS
257Normally puts point before and mark after the inserted text.
258If optional second arg is non-nil, puts mark before and point after.
259Interactively, second arg is non-nil if prefix arg is supplied."
ecfc7eb3 260 (interactive "*cInsert register: \nP")
efeae993 261 (push-mark)
1b8cac5d 262 (let ((val (get-register register)))
cbd4993c
KH
263 (cond
264 ((consp val)
265 (insert-rectangle val))
266 ((stringp val)
e7c765c3 267 (insert-for-yank val))
070c2506 268 ((numberp val)
cbd4993c
KH
269 (princ val (current-buffer)))
270 ((and (markerp val) (marker-position val))
271 (princ (marker-position val) (current-buffer)))
272 (t
273 (error "Register does not contain text"))))
efeae993
RS
274 (if (not arg) (exchange-point-and-mark)))
275
1b8cac5d
RS
276(defun copy-to-register (register start end &optional delete-flag)
277 "Copy region into register REGISTER. With prefix arg, delete as well.
278Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
279START and END are buffer positions indicating what to copy."
280 (interactive "cCopy to register: \nr\nP")
13191e32 281 (set-register register (filter-buffer-substring start end))
efeae993
RS
282 (if delete-flag (delete-region start end)))
283
1b8cac5d
RS
284(defun append-to-register (register start end &optional delete-flag)
285 "Append region to text in register REGISTER.
286With prefix arg, delete as well.
287Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
288START and END are buffer positions indicating what to append."
289 (interactive "cAppend to register: \nr\nP")
c81f72ce
TTN
290 (let ((reg (get-register register))
291 (text (filter-buffer-substring start end)))
292 (set-register
293 register (cond ((not reg) text)
294 ((stringp reg) (concat reg text))
295 (t (error "Register does not contain text")))))
efeae993
RS
296 (if delete-flag (delete-region start end)))
297
1b8cac5d
RS
298(defun prepend-to-register (register start end &optional delete-flag)
299 "Prepend region to text in register REGISTER.
300With prefix arg, delete as well.
301Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
302START and END are buffer positions indicating what to prepend."
303 (interactive "cPrepend to register: \nr\nP")
c81f72ce
TTN
304 (let ((reg (get-register register))
305 (text (filter-buffer-substring start end)))
306 (set-register
307 register (cond ((not reg) text)
308 ((stringp reg) (concat text reg))
309 (t (error "Register does not contain text")))))
efeae993
RS
310 (if delete-flag (delete-region start end)))
311
1b8cac5d
RS
312(defun copy-rectangle-to-register (register start end &optional delete-flag)
313 "Copy rectangular region into register REGISTER.
5ef5d6ce
RS
314With prefix arg, delete as well. To insert this register
315in the buffer, use \\[insert-register].
316
317Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
efeae993
RS
318START and END are buffer positions giving two corners of rectangle."
319 (interactive "cCopy rectangle to register: \nr\nP")
1b8cac5d 320 (set-register register
efeae993
RS
321 (if delete-flag
322 (delete-extract-rectangle start end)
323 (extract-rectangle start end))))
c88ab9ce 324
0f214cdf 325(provide 'register)
cbee283d 326;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
c88ab9ce 327;;; register.el ends here