(Fyes_or_no_p): Use Qyes_or_no_p_history.
[emacs.git] / lisp / register.el
blob91ef6c404236157d8ca266a4e05b34f933a3e71a
1 ;;; register.el --- register commands for Emacs.
3 ;; Copyright (C) 1985, 1993 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 (char)
41 "Return contents of Emacs register named CHAR, or nil if none."
42 (cdr (assq char register-alist)))
44 (defun set-register (char value)
45 "Set contents of Emacs register named CHAR to VALUE. Returns VALUE.
46 See the documentation of the variable `register-alist' for possible VALUE."
47 (let ((aelt (assq char register-alist)))
48 (if aelt
49 (setcdr aelt value)
50 (setq aelt (cons char value))
51 (setq register-alist (cons aelt register-alist)))
52 value))
54 (defun point-to-register (char &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 char (if arg (current-frame-configuration) (point-marker))))
62 (defun window-configuration-to-register (char &optional arg)
63 "Store the window configuration of the selected frame in register REGISTER.
64 Use \\[jump-to-register] to restore the configuration.
65 Argument is a character, naming the register."
66 (interactive "cPoint to register: \nP")
67 (set-register char (current-window-configuration)))
69 (defun frame-configuration-to-register (char &optional arg)
70 "Store the window configuration of all frames in register REGISTER.
71 Use \\[jump-to-register] to restore the configuration.
72 Argument is a character, naming the register."
73 (interactive "cPoint to register: \nP")
74 (set-register char (current-frame-configuration)))
76 (defalias 'register-to-point 'jump-to-register)
77 (defun jump-to-register (char)
78 "Move point to location stored in a register.
79 If the register contains a file name, find that file.
80 \(To put a file name in a register, you must use `set-register'.)
81 If the register contains a window configuration (one frame) or a frame
82 configuration (all frames), restore that frame or all frames accordingly.
83 Argument is a character, naming the register."
84 (interactive "cJump to register: ")
85 (let ((val (get-register char)))
86 (cond
87 ((and (fboundp 'frame-configuration-p)
88 (frame-configuration-p val))
89 (set-frame-configuration val))
90 ((window-configuration-p val)
91 (set-window-configuration val))
92 ((markerp val)
93 (switch-to-buffer (marker-buffer val))
94 (goto-char val))
95 ((and (consp val) (eq (car val) 'file))
96 (find-file (cdr val)))
98 (error "Register doesn't contain a buffer position or configuration")))))
100 ;(defun number-to-register (arg char)
101 ; "Store a number in a register.
102 ;Two args, NUMBER and REGISTER (a character, naming the register).
103 ;If NUMBER is nil, digits in the buffer following point are read
104 ;to get the number to store.
105 ;Interactively, NUMBER is the prefix arg (none means nil)."
106 ; (interactive "P\ncNumber to register: ")
107 ; (set-register char
108 ; (if arg
109 ; (prefix-numeric-value arg)
110 ; (if (looking-at "[0-9][0-9]*")
111 ; (save-excursion
112 ; (save-restriction
113 ; (narrow-to-region (point)
114 ; (progn (skip-chars-forward "0-9")
115 ; (point)))
116 ; (goto-char (point-min))
117 ; (read (current-buffer))))
118 ; 0))))
120 ;(defun increment-register (arg char)
121 ; "Add NUMBER to the contents of register REGISTER.
122 ;Interactively, NUMBER is the prefix arg (none means nil)."
123 ; (interactive "p\ncNumber to register: ")
124 ; (or (integerp (get-register char))
125 ; (error "Register does not contain a number"))
126 ; (set-register char (+ arg (get-register char))))
128 (defun view-register (char)
129 "Display what is contained in register named REGISTER.
130 REGISTER is a character."
131 (interactive "cView register: ")
132 (let ((val (get-register char)))
133 (if (null val)
134 (message "Register %s is empty" (single-key-description char))
135 (with-output-to-temp-buffer "*Output*"
136 (princ "Register ")
137 (princ (single-key-description char))
138 (princ " contains ")
139 (cond
140 ((integerp val)
141 (princ val))
143 ((markerp val)
144 (princ "a buffer position:\nbuffer ")
145 (princ (buffer-name (marker-buffer val)))
146 (princ ", position ")
147 (princ (+ 0 val)))
149 ((window-configuration-p val)
150 (princ "a window configuration."))
152 ((frame-configuration-p val)
153 (princ "a frame configuration."))
155 ((and (consp val) (eq (car val) 'file))
156 (princ "the file ")
157 (prin1 (cdr val))
158 (princ "."))
160 ((consp val)
161 (princ "the rectangle:\n")
162 (while val
163 (princ (car val))
164 (terpri)
165 (setq val (cdr val))))
167 ((stringp val)
168 (princ "the text:\n")
169 (princ val))
172 (princ "Garbage:\n")
173 (prin1 val)))))))
175 (defun insert-register (char &optional arg)
176 "Insert contents of register REG. REG is a character.
177 Normally puts point before and mark after the inserted text.
178 If optional second arg is non-nil, puts mark before and point after.
179 Interactively, second arg is non-nil if prefix arg is supplied."
180 (interactive "cInsert register: \nP")
181 (push-mark)
182 (let ((val (get-register char)))
183 (if (consp val)
184 (insert-rectangle val)
185 (if (stringp val)
186 (insert val)
187 (if (or (integerp val) (markerp val))
188 (princ (+ 0 val) (current-buffer))
189 (error "Register does not contain text")))))
190 (if (not arg) (exchange-point-and-mark)))
192 (defun copy-to-register (char start end &optional delete-flag)
193 "Copy region into register REG. With prefix arg, delete as well.
194 Called from program, takes four args: REG, START, END and DELETE-FLAG.
195 START and END are buffer positions indicating what to copy."
196 (interactive "cCopy to register: \nr\nP")
197 (set-register char (buffer-substring start end))
198 (if delete-flag (delete-region start end)))
200 (defun append-to-register (char start end &optional delete-flag)
201 "Append region to text in register REG. With prefix arg, delete as well.
202 Called from program, takes four args: REG, START, END and DELETE-FLAG.
203 START and END are buffer positions indicating what to append."
204 (interactive "cAppend to register: \nr\nP")
205 (or (stringp (get-register char))
206 (error "Register does not contain text"))
207 (set-register char (concat (get-register char)
208 (buffer-substring start end)))
209 (if delete-flag (delete-region start end)))
211 (defun prepend-to-register (char start end &optional delete-flag)
212 "Prepend region to text in register REG. With prefix arg, delete as well.
213 Called from program, takes four args: REG, START, END and DELETE-FLAG.
214 START and END are buffer positions indicating what to prepend."
215 (interactive "cPrepend to register: \nr\nP")
216 (or (stringp (get-register char))
217 (error "Register does not contain text"))
218 (set-register char (concat (buffer-substring start end)
219 (get-register char)))
220 (if delete-flag (delete-region start end)))
222 (defun copy-rectangle-to-register (char start end &optional delete-flag)
223 "Copy rectangular region into register REG. With prefix arg, delete as well.
224 Called from program, takes four args: REG, START, END and DELETE-FLAG.
225 START and END are buffer positions giving two corners of rectangle."
226 (interactive "cCopy rectangle to register: \nr\nP")
227 (set-register char
228 (if delete-flag
229 (delete-extract-rectangle start end)
230 (extract-rectangle start end))))
232 ;;; register.el ends here