(using-unix-filesystems):
[emacs.git] / lisp / register.el
blob461a517a0f66607a208b52314e472f78552342d2
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
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.
31 ;;; Code:
33 (defvar register-alist nil
34 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
35 NAME is a character (a number). CONTENTS is a string, number,
36 frame configuration, mark or list.
37 A list of strings represents a rectangle.
38 A list of the form (file . NAME) represents the file named NAME.")
40 (defun get-register (reg)
41 "Return contents of Emacs register named REG, or nil if none."
42 (cdr (assq reg register-alist)))
44 (defun set-register (register value)
45 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
46 See the documentation of the variable `register-alist' for possible VALUE."
47 (let ((aelt (assq register register-alist)))
48 (if aelt
49 (setcdr aelt value)
50 (setq aelt (cons register value))
51 (setq register-alist (cons aelt register-alist)))
52 value))
54 (defun point-to-register (register &optional arg)
55 "Store current location of point in register REGISTER.
56 With prefix argument, store current frame configuration.
57 Use \\[jump-to-register] to go to that location or restore that configuration.
58 Argument is a character, naming the register."
59 (interactive "cPoint to register: \nP")
60 (set-register register
61 (if arg (current-frame-configuration) (point-marker))))
63 (defun window-configuration-to-register (register &optional arg)
64 "Store the window configuration of the selected frame in register REGISTER.
65 Use \\[jump-to-register] to restore the configuration.
66 Argument is a character, naming the register."
67 (interactive "cWindow configuration to register: \nP")
68 (set-register register (current-window-configuration)))
70 (defun frame-configuration-to-register (register &optional arg)
71 "Store the window configuration of all frames in register REGISTER.
72 Use \\[jump-to-register] to restore the configuration.
73 Argument is a character, naming the register."
74 (interactive "cFrame configuration to register: \nP")
75 (set-register register (current-frame-configuration)))
77 (defalias 'register-to-point 'jump-to-register)
78 (defun jump-to-register (register &optional delete)
79 "Move point to location stored in a register.
80 If the register contains a file name, find that file.
81 \(To put a file name in a register, you must use `set-register'.)
82 If the register contains a window configuration (one frame) or a frame
83 configuration (all frames), restore that frame or all frames accordingly.
84 First argument is a character, naming the register.
85 Optional second arg non-nil (interactively, prefix argument) says to
86 delete any existing frames that the frame configuration doesn't mention.
87 \(Otherwise, these frames are iconified.)"
88 (interactive "cJump to register: \nP")
89 (let ((val (get-register register)))
90 (cond
91 ((and (fboundp 'frame-configuration-p)
92 (frame-configuration-p val))
93 (set-frame-configuration val (not delete)))
94 ((window-configuration-p val)
95 (set-window-configuration val))
96 ((markerp val)
97 (or (marker-buffer val)
98 (error "That register's buffer no longer exists"))
99 (switch-to-buffer (marker-buffer val))
100 (goto-char val))
101 ((and (consp val) (eq (car val) 'file))
102 (find-file (cdr val)))
104 (error "Register doesn't contain a buffer position or configuration")))))
106 ;(defun number-to-register (arg char)
107 ; "Store a number in a register.
108 ;Two args, NUMBER and REGISTER (a character, naming the register).
109 ;If NUMBER is nil, digits in the buffer following point are read
110 ;to get the number to store.
111 ;Interactively, NUMBER is the prefix arg (none means nil)."
112 ; (interactive "P\ncNumber to register: ")
113 ; (set-register char
114 ; (if arg
115 ; (prefix-numeric-value arg)
116 ; (if (looking-at "[0-9][0-9]*")
117 ; (save-excursion
118 ; (save-restriction
119 ; (narrow-to-region (point)
120 ; (progn (skip-chars-forward "0-9")
121 ; (point)))
122 ; (goto-char (point-min))
123 ; (read (current-buffer))))
124 ; 0))))
126 ;(defun increment-register (arg char)
127 ; "Add NUMBER to the contents of register REGISTER.
128 ;Interactively, NUMBER is the prefix arg (none means nil)."
129 ; (interactive "p\ncNumber to register: ")
130 ; (or (integerp (get-register char))
131 ; (error "Register does not contain a number"))
132 ; (set-register char (+ arg (get-register char))))
134 (defun view-register (register)
135 "Display what is contained in register named REGISTER.
136 The Lisp value REGISTER is a character."
137 (interactive "cView register: ")
138 (let ((val (get-register register)))
139 (if (null val)
140 (message "Register %s is empty" (single-key-description register))
141 (with-output-to-temp-buffer "*Output*"
142 (princ "Register ")
143 (princ (single-key-description register))
144 (princ " contains ")
145 (cond
146 ((integerp val)
147 (princ val))
149 ((markerp val)
150 (let ((buf (marker-buffer val)))
151 (if (null buf)
152 (princ "a marker in no buffer")
153 (princ "a buffer position:\nbuffer ")
154 (princ (buffer-name buf))
155 (princ ", position ")
156 (princ (marker-position val)))))
158 ((window-configuration-p val)
159 (princ "a window configuration."))
161 ((frame-configuration-p val)
162 (princ "a frame configuration."))
164 ((and (consp val) (eq (car val) 'file))
165 (princ "the file ")
166 (prin1 (cdr val))
167 (princ "."))
169 ((consp val)
170 (princ "the rectangle:\n")
171 (while val
172 (princ (car val))
173 (terpri)
174 (setq val (cdr val))))
176 ((stringp val)
177 (princ "the text:\n")
178 (princ val))
181 (princ "Garbage:\n")
182 (prin1 val)))))))
184 (defun insert-register (register &optional arg)
185 "Insert contents of register REGISTER. (REGISTER is a character.)
186 Normally puts point before and mark after the inserted text.
187 If optional second arg is non-nil, puts mark before and point after.
188 Interactively, second arg is non-nil if prefix arg is supplied."
189 (interactive "*cInsert register: \nP")
190 (push-mark)
191 (let ((val (get-register register)))
192 (cond
193 ((consp val)
194 (insert-rectangle val))
195 ((stringp val)
196 (insert val))
197 ((integerp val)
198 (princ val (current-buffer)))
199 ((and (markerp val) (marker-position val))
200 (princ (marker-position val) (current-buffer)))
202 (error "Register does not contain text"))))
203 (if (not arg) (exchange-point-and-mark)))
205 (defun copy-to-register (register start end &optional delete-flag)
206 "Copy region into register REGISTER. With prefix arg, delete as well.
207 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
208 START and END are buffer positions indicating what to copy."
209 (interactive "cCopy to register: \nr\nP")
210 (set-register register (buffer-substring start end))
211 (if delete-flag (delete-region start end)))
213 (defun append-to-register (register start end &optional delete-flag)
214 "Append region to text in register REGISTER.
215 With prefix arg, delete as well.
216 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
217 START and END are buffer positions indicating what to append."
218 (interactive "cAppend to register: \nr\nP")
219 (or (stringp (get-register register))
220 (error "Register does not contain text"))
221 (set-register register (concat (get-register register)
222 (buffer-substring start end)))
223 (if delete-flag (delete-region start end)))
225 (defun prepend-to-register (register start end &optional delete-flag)
226 "Prepend region to text in register REGISTER.
227 With prefix arg, delete as well.
228 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
229 START and END are buffer positions indicating what to prepend."
230 (interactive "cPrepend to register: \nr\nP")
231 (or (stringp (get-register register))
232 (error "Register does not contain text"))
233 (set-register register (concat (buffer-substring start end)
234 (get-register register)))
235 (if delete-flag (delete-region start end)))
237 (defun copy-rectangle-to-register (register start end &optional delete-flag)
238 "Copy rectangular region into register REGISTER.
239 With prefix arg, delete as well.
240 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
241 START and END are buffer positions giving two corners of rectangle."
242 (interactive "cCopy rectangle to register: \nr\nP")
243 (set-register register
244 (if delete-flag
245 (delete-extract-rectangle start end)
246 (extract-rectangle start end))))
248 ;;; register.el ends here