Require cl only when compiling.
[emacs.git] / lisp / register.el
blob54851f0bbe16a593f6926cdfe5ac4e3e991e01fb
1 ;;; register.el --- register commands for Emacs.
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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 ;;; Code:
34 (defvar register-alist nil
35 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
36 NAME is a character (a number). CONTENTS is a string, number,
37 frame configuration, mark or list.
38 A list of strings represents a rectangle.
39 A list of the form (file . NAME) represents the file named NAME.")
41 (defun get-register (reg)
42 "Return contents of Emacs register named REG, or nil if none."
43 (cdr (assq reg register-alist)))
45 (defun set-register (register value)
46 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
47 See the documentation of the variable `register-alist' for possible VALUE."
48 (let ((aelt (assq register register-alist)))
49 (if aelt
50 (setcdr aelt value)
51 (setq aelt (cons register value))
52 (setq register-alist (cons aelt register-alist)))
53 value))
55 (defun point-to-register (register &optional arg)
56 "Store current location of point in register REGISTER.
57 With prefix argument, store current frame configuration.
58 Use \\[jump-to-register] to go to that location or restore that configuration.
59 Argument is a character, naming the register."
60 (interactive "cPoint to register: \nP")
61 (set-register register
62 (if arg (current-frame-configuration) (point-marker))))
64 (defun window-configuration-to-register (register &optional arg)
65 "Store the window configuration of the selected frame in register REGISTER.
66 Use \\[jump-to-register] to restore the configuration.
67 Argument is a character, naming the register."
68 (interactive "cWindow configuration to register: \nP")
69 (set-register register (current-window-configuration)))
71 (defun frame-configuration-to-register (register &optional arg)
72 "Store the window configuration of all frames in register REGISTER.
73 Use \\[jump-to-register] to restore the configuration.
74 Argument is a character, naming the register."
75 (interactive "cFrame configuration to register: \nP")
76 (set-register register (current-frame-configuration)))
78 (defalias 'register-to-point 'jump-to-register)
79 (defun jump-to-register (register &optional delete)
80 "Move point to location stored in a register.
81 If the register contains a file name, find that file.
82 \(To put a file name in a register, you must use `set-register'.)
83 If the register contains a window configuration (one frame) or a frame
84 configuration (all frames), restore that frame or all frames accordingly.
85 First argument is a character, naming the register.
86 Optional second arg non-nil (interactively, prefix argument) says to
87 delete any existing frames that the frame configuration doesn't mention.
88 \(Otherwise, these frames are iconified.)"
89 (interactive "cJump to register: \nP")
90 (let ((val (get-register register)))
91 (cond
92 ((and (fboundp 'frame-configuration-p)
93 (frame-configuration-p val))
94 (set-frame-configuration val (not delete)))
95 ((window-configuration-p val)
96 (set-window-configuration val))
97 ((markerp val)
98 (or (marker-buffer val)
99 (error "That register's buffer no longer exists"))
100 (switch-to-buffer (marker-buffer val))
101 (goto-char val))
102 ((and (consp val) (eq (car val) 'file))
103 (find-file (cdr val)))
105 (error "Register doesn't contain a buffer position or configuration")))))
107 ;(defun number-to-register (arg char)
108 ; "Store a number in a register.
109 ;Two args, NUMBER and REGISTER (a character, naming the register).
110 ;If NUMBER is nil, digits in the buffer following point are read
111 ;to get the number to store.
112 ;Interactively, NUMBER is the prefix arg (none means nil)."
113 ; (interactive "P\ncNumber to register: ")
114 ; (set-register char
115 ; (if arg
116 ; (prefix-numeric-value arg)
117 ; (if (looking-at "[0-9][0-9]*")
118 ; (save-excursion
119 ; (save-restriction
120 ; (narrow-to-region (point)
121 ; (progn (skip-chars-forward "0-9")
122 ; (point)))
123 ; (goto-char (point-min))
124 ; (read (current-buffer))))
125 ; 0))))
127 ;(defun increment-register (arg char)
128 ; "Add NUMBER to the contents of register REGISTER.
129 ;Interactively, NUMBER is the prefix arg (none means nil)."
130 ; (interactive "p\ncNumber to register: ")
131 ; (or (integerp (get-register char))
132 ; (error "Register does not contain a number"))
133 ; (set-register char (+ arg (get-register char))))
135 (defun view-register (register)
136 "Display what is contained in register named REGISTER.
137 The Lisp value REGISTER is a character."
138 (interactive "cView register: ")
139 (let ((val (get-register register)))
140 (if (null val)
141 (message "Register %s is empty" (single-key-description register))
142 (with-output-to-temp-buffer "*Output*"
143 (princ "Register ")
144 (princ (single-key-description register))
145 (princ " contains ")
146 (cond
147 ((integerp val)
148 (princ val))
150 ((markerp val)
151 (let ((buf (marker-buffer val)))
152 (if (null buf)
153 (princ "a marker in no buffer")
154 (princ "a buffer position:\nbuffer ")
155 (princ (buffer-name buf))
156 (princ ", position ")
157 (princ (marker-position val)))))
159 ((window-configuration-p val)
160 (princ "a window configuration."))
162 ((frame-configuration-p val)
163 (princ "a frame configuration."))
165 ((and (consp val) (eq (car val) 'file))
166 (princ "the file ")
167 (prin1 (cdr val))
168 (princ "."))
170 ((consp val)
171 (princ "the rectangle:\n")
172 (while val
173 (princ (car val))
174 (terpri)
175 (setq val (cdr val))))
177 ((stringp val)
178 (princ "the text:\n")
179 (princ val))
182 (princ "Garbage:\n")
183 (prin1 val)))))))
185 (defun insert-register (register &optional arg)
186 "Insert contents of register REGISTER. (REGISTER is a character.)
187 Normally puts point before and mark after the inserted text.
188 If optional second arg is non-nil, puts mark before and point after.
189 Interactively, second arg is non-nil if prefix arg is supplied."
190 (interactive "*cInsert register: \nP")
191 (push-mark)
192 (let ((val (get-register register)))
193 (cond
194 ((consp val)
195 (insert-rectangle val))
196 ((stringp val)
197 (insert val))
198 ((integerp val)
199 (princ val (current-buffer)))
200 ((and (markerp val) (marker-position val))
201 (princ (marker-position val) (current-buffer)))
203 (error "Register does not contain text"))))
204 (if (not arg) (exchange-point-and-mark)))
206 (defun copy-to-register (register start end &optional delete-flag)
207 "Copy region into register REGISTER. With prefix arg, delete as well.
208 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
209 START and END are buffer positions indicating what to copy."
210 (interactive "cCopy to register: \nr\nP")
211 (set-register register (buffer-substring start end))
212 (if delete-flag (delete-region start end)))
214 (defun append-to-register (register start end &optional delete-flag)
215 "Append region to text in register REGISTER.
216 With prefix arg, delete as well.
217 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
218 START and END are buffer positions indicating what to append."
219 (interactive "cAppend to register: \nr\nP")
220 (or (stringp (get-register register))
221 (error "Register does not contain text"))
222 (set-register register (concat (get-register register)
223 (buffer-substring start end)))
224 (if delete-flag (delete-region start end)))
226 (defun prepend-to-register (register start end &optional delete-flag)
227 "Prepend region to text in register REGISTER.
228 With prefix arg, delete as well.
229 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
230 START and END are buffer positions indicating what to prepend."
231 (interactive "cPrepend to register: \nr\nP")
232 (or (stringp (get-register register))
233 (error "Register does not contain text"))
234 (set-register register (concat (buffer-substring start end)
235 (get-register register)))
236 (if delete-flag (delete-region start end)))
238 (defun copy-rectangle-to-register (register start end &optional delete-flag)
239 "Copy rectangular region into register REGISTER.
240 With prefix arg, delete as well.
241 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
242 START and END are buffer positions giving two corners of rectangle."
243 (interactive "cCopy rectangle to register: \nr\nP")
244 (set-register register
245 (if delete-flag
246 (delete-extract-rectangle start end)
247 (extract-rectangle start end))))
249 ;;; register.el ends here