Improve the explanation of version control to include concepts
[emacs.git] / lisp / register.el
blob8f8b786cb9e6ba192f35d20de83db6cecd7fe732
1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
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.
33 ;;; Code:
35 (defvar register-alist nil
36 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
37 NAME is a character (a number). CONTENTS is a string, number, marker or list.
38 A list of strings represents a rectangle.
39 A list of the form (file . NAME) represents the file named NAME.
40 A list of the form (file-query NAME POSITION) represents position POSITION
41 in the file named NAME, but query before visiting it.
42 A list of the form (WINDOW-CONFIGURATION POSITION)
43 represents a saved window configuration plus a saved value of point.
44 A list of the form (FRAME-CONFIGURATION POSITION)
45 represents a saved frame configuration plus a saved value of point.")
47 (defun get-register (reg)
48 "Return contents of Emacs register named REG, or nil if none."
49 (cdr (assq reg register-alist)))
51 (defun set-register (register value)
52 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
53 See the documentation of the variable `register-alist' for possible VALUE."
54 (let ((aelt (assq register register-alist)))
55 (if aelt
56 (setcdr aelt value)
57 (push (cons register value) register-alist))
58 value))
60 (defun point-to-register (register &optional arg)
61 "Store current location of point in register REGISTER.
62 With prefix argument, store current frame configuration.
63 Use \\[jump-to-register] to go to that location or restore that configuration.
64 Argument is a character, naming the register."
65 (interactive "cPoint to register: \nP")
66 ;; Turn the marker into a file-ref if the buffer is killed.
67 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
68 (set-register register
69 (if arg (list (current-frame-configuration) (point-marker))
70 (point-marker))))
72 (defun window-configuration-to-register (register &optional arg)
73 "Store the window configuration of the selected frame in register REGISTER.
74 Use \\[jump-to-register] to restore the configuration.
75 Argument is a character, naming the register."
76 (interactive "cWindow configuration to register: \nP")
77 ;; current-window-configuration does not include the value
78 ;; of point in the current buffer, so record that separately.
79 (set-register register (list (current-window-configuration) (point-marker))))
81 (defun frame-configuration-to-register (register &optional arg)
82 "Store the window configuration of all frames in register REGISTER.
83 Use \\[jump-to-register] to restore the configuration.
84 Argument is a character, naming the register."
85 (interactive "cFrame configuration to register: \nP")
86 ;; current-frame-configuration does not include the value
87 ;; of point in the current buffer, so record that separately.
88 (set-register register (list (current-frame-configuration) (point-marker))))
90 (defalias 'register-to-point 'jump-to-register)
91 (defun jump-to-register (register &optional delete)
92 "Move point to location stored in a register.
93 If the register contains a file name, find that file.
94 \(To put a file name in a register, you must use `set-register'.)
95 If the register contains a window configuration (one frame) or a frame
96 configuration (all frames), restore that frame or all frames accordingly.
97 First argument is a character, naming the register.
98 Optional second arg non-nil (interactively, prefix argument) says to
99 delete any existing frames that the frame configuration doesn't mention.
100 \(Otherwise, these frames are iconified.)"
101 (interactive "cJump to register: \nP")
102 (let ((val (get-register register)))
103 (cond
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)))
110 ((markerp val)
111 (or (marker-buffer val)
112 (error "That register's buffer no longer exists"))
113 (switch-to-buffer (marker-buffer val))
114 (goto-char val))
115 ((and (consp val) (eq (car val) 'file))
116 (find-file (cdr val)))
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)))
124 (error "Register doesn't contain a buffer position or configuration")))))
126 (defun register-swap-out ()
127 "Turn markers into file-query references when a buffer is killed."
128 (and buffer-file-name
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))))))))
137 (defun number-to-register (number register)
138 "Store a number in a register.
139 Two args, NUMBER and REGISTER (a character, naming the register).
140 If NUMBER is nil, a decimal number is read from the buffer starting
141 at point, and point moves to the end of that number.
142 Interactively, NUMBER is the prefix arg (none means nil)."
143 (interactive "P\ncNumber to register: ")
144 (set-register register
145 (if number
146 (prefix-numeric-value number)
147 (if (looking-at "\\s-*-?[0-9]+")
148 (progn
149 (goto-char (match-end 0))
150 (string-to-number (match-string 0)))
151 0))))
153 (defun increment-register (number register)
154 "Add NUMBER to the contents of register REGISTER.
155 Interactively, NUMBER is the prefix arg."
156 (interactive "p\ncIncrement register: ")
157 (or (numberp (get-register register))
158 (error "Register does not contain a number"))
159 (set-register register (+ number (get-register register))))
161 (defun view-register (register)
162 "Display what is contained in register named REGISTER.
163 The Lisp value REGISTER is a character."
164 (interactive "cView register: ")
165 (let ((val (get-register register)))
166 (if (null val)
167 (message "Register %s is empty" (single-key-description register))
168 (with-output-to-temp-buffer "*Output*"
169 (describe-register-1 register t)))))
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))))))
182 (defun describe-register-1 (register &optional verbose)
183 (princ "Register ")
184 (princ (single-key-description register))
185 (princ " contains ")
186 (let ((val (get-register register)))
187 (cond
188 ((numberp val)
189 (princ val))
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)))))
200 ((and (consp val) (window-configuration-p (car val)))
201 (princ "a window configuration."))
203 ((and (consp val) (frame-configuration-p (car val)))
204 (princ "a frame configuration."))
206 ((and (consp val) (eq (car val) 'file))
207 (princ "the file ")
208 (prin1 (cdr val))
209 (princ "."))
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 "."))
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))))
230 ((stringp val)
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))
235 (if verbose
236 (progn
237 (princ "the text:\n")
238 (princ val))
239 (cond
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"))
250 (princ "the empty string")))))
252 (princ "Garbage:\n")
253 (if verbose (prin1 val))))))
255 (defun insert-register (register &optional arg)
256 "Insert contents of register REGISTER. (REGISTER is a character.)
257 Normally puts point before and mark after the inserted text.
258 If optional second arg is non-nil, puts mark before and point after.
259 Interactively, second arg is non-nil if prefix arg is supplied."
260 (interactive "*cInsert register: \nP")
261 (push-mark)
262 (let ((val (get-register register)))
263 (cond
264 ((consp val)
265 (insert-rectangle val))
266 ((stringp val)
267 (insert-for-yank val))
268 ((numberp val)
269 (princ val (current-buffer)))
270 ((and (markerp val) (marker-position val))
271 (princ (marker-position val) (current-buffer)))
273 (error "Register does not contain text"))))
274 (if (not arg) (exchange-point-and-mark)))
276 (defun copy-to-register (register start end &optional delete-flag)
277 "Copy region into register REGISTER. With prefix arg, delete as well.
278 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
279 START and END are buffer positions indicating what to copy."
280 (interactive "cCopy to register: \nr\nP")
281 (set-register register (filter-buffer-substring start end))
282 (if delete-flag (delete-region start end)))
284 (defun append-to-register (register start end &optional delete-flag)
285 "Append region to text in register REGISTER.
286 With prefix arg, delete as well.
287 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
288 START and END are buffer positions indicating what to append."
289 (interactive "cAppend to register: \nr\nP")
290 (or (stringp (get-register register))
291 (error "Register does not contain text"))
292 (set-register register (concat (get-register register)
293 (filter-buffer-substring start end)))
294 (if delete-flag (delete-region start end)))
296 (defun prepend-to-register (register start end &optional delete-flag)
297 "Prepend region to text in register REGISTER.
298 With prefix arg, delete as well.
299 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
300 START and END are buffer positions indicating what to prepend."
301 (interactive "cPrepend to register: \nr\nP")
302 (or (stringp (get-register register))
303 (error "Register does not contain text"))
304 (set-register register (concat (filter-buffer-substring start end)
305 (get-register register)))
306 (if delete-flag (delete-region start end)))
308 (defun copy-rectangle-to-register (register start end &optional delete-flag)
309 "Copy rectangular region into register REGISTER.
310 With prefix arg, delete as well. To insert this register
311 in the buffer, use \\[insert-register].
313 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
314 START and END are buffer positions giving two corners of rectangle."
315 (interactive "cCopy rectangle to register: \nr\nP")
316 (set-register register
317 (if delete-flag
318 (delete-extract-rectangle start end)
319 (extract-rectangle start end))))
321 (provide 'register)
322 ;;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
323 ;;; register.el ends here