(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / register.el
blobeaa53446c56684fb54266805f45a7d04aa11ebfd
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, marker or list.
37 A list of strings represents a rectangle.
38 A list of the form (file . NAME) represents the file named NAME.
39 A list of the form (file-query NAME POSITION) represents position POSITION
40 in the file named NAME, but query before visiting it.
41 A list of the form (WINDOW-CONFIGURATION POSITION)
42 represents a saved window configuration plus a saved value of point.
43 A list of the form (FRAME-CONFIGURATION POSITION)
44 represents a saved frame configuration plus a saved value of point.")
46 (defun get-register (reg)
47 "Return contents of Emacs register named REG, or nil if none."
48 (cdr (assq reg register-alist)))
50 (defun set-register (register value)
51 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
52 See the documentation of the variable `register-alist' for possible VALUE."
53 (let ((aelt (assq register register-alist)))
54 (if aelt
55 (setcdr aelt value)
56 (push (cons register value) register-alist))
57 value))
59 (defun point-to-register (register &optional arg)
60 "Store current location of point in register REGISTER.
61 With prefix argument, store current frame configuration.
62 Use \\[jump-to-register] to go to that location or restore that configuration.
63 Argument is a character, naming the register."
64 (interactive "cPoint to register: \nP")
65 ;; Turn the marker into a file-ref if the buffer is killed.
66 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
67 (set-register register
68 (if arg (list (current-frame-configuration) (point-marker))
69 (point-marker))))
71 (defun window-configuration-to-register (register &optional arg)
72 "Store the window configuration of the selected frame in register REGISTER.
73 Use \\[jump-to-register] to restore the configuration.
74 Argument is a character, naming the register."
75 (interactive "cWindow configuration to register: \nP")
76 ;; current-window-configuration does not include the value
77 ;; of point in the current buffer, so record that separately.
78 (set-register register (list (current-window-configuration) (point-marker))))
80 (defun frame-configuration-to-register (register &optional arg)
81 "Store the window configuration of all frames in register REGISTER.
82 Use \\[jump-to-register] to restore the configuration.
83 Argument is a character, naming the register."
84 (interactive "cFrame configuration to register: \nP")
85 ;; current-frame-configuration does not include the value
86 ;; of point in the current buffer, so record that separately.
87 (set-register register (list (current-frame-configuration) (point-marker))))
89 (defalias 'register-to-point 'jump-to-register)
90 (defun jump-to-register (register &optional delete)
91 "Move point to location stored in a register.
92 If the register contains a file name, find that file.
93 \(To put a file name in a register, you must use `set-register'.)
94 If the register contains a window configuration (one frame) or a frame
95 configuration (all frames), restore that frame or all frames accordingly.
96 First argument is a character, naming the register.
97 Optional second arg non-nil (interactively, prefix argument) says to
98 delete any existing frames that the frame configuration doesn't mention.
99 \(Otherwise, these frames are iconified.)"
100 (interactive "cJump to register: \nP")
101 (let ((val (get-register register)))
102 (cond
103 ((and (consp val) (frame-configuration-p (car val)))
104 (set-frame-configuration (car val) (not delete))
105 (goto-char (cadr val)))
106 ((and (consp val) (window-configuration-p (car val)))
107 (set-window-configuration (car val))
108 (goto-char (cadr val)))
109 ((markerp val)
110 (or (marker-buffer val)
111 (error "That register's buffer no longer exists"))
112 (switch-to-buffer (marker-buffer val))
113 (goto-char val))
114 ((and (consp val) (eq (car val) 'file))
115 (find-file (cdr val)))
116 ((and (consp val) (eq (car val) 'file-query))
117 (or (find-buffer-visiting (nth 1 val))
118 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
119 (error "Register access aborted"))
120 (find-file (nth 1 val))
121 (goto-char (nth 2 val)))
123 (error "Register doesn't contain a buffer position or configuration")))))
125 (defun register-swap-out ()
126 "Turn markers into file-query references when a buffer is killed."
127 (and buffer-file-name
128 (dolist (elem register-alist)
129 (and (markerp (cdr elem))
130 (eq (marker-buffer (cdr elem)) (current-buffer))
131 (setcdr elem
132 (list 'file-query
133 buffer-file-name
134 (marker-position (cdr elem))))))))
136 (defun number-to-register (number register)
137 "Store a number in a register.
138 Two args, NUMBER and REGISTER (a character, naming the register).
139 If NUMBER is nil, a decimal number is read from the buffer starting
140 at point, and point moves to the end of that number.
141 Interactively, NUMBER is the prefix arg (none means nil)."
142 (interactive "P\ncNumber to register: ")
143 (set-register register
144 (if number
145 (prefix-numeric-value number)
146 (if (looking-at "\\s-*-?[0-9]+")
147 (progn
148 (goto-char (match-end 0))
149 (string-to-number (match-string 0)))
150 0))))
152 (defun increment-register (number register)
153 "Add NUMBER to the contents of register REGISTER.
154 Interactively, NUMBER is the prefix arg."
155 (interactive "p\ncIncrement register: ")
156 (or (numberp (get-register register))
157 (error "Register does not contain a number"))
158 (set-register register (+ number (get-register register))))
160 (defun view-register (register)
161 "Display what is contained in register named REGISTER.
162 The Lisp value REGISTER is a character."
163 (interactive "cView register: ")
164 (let ((val (get-register register)))
165 (if (null val)
166 (message "Register %s is empty" (single-key-description register))
167 (with-output-to-temp-buffer "*Output*"
168 (describe-register-1 register t)))))
170 (defun list-registers ()
171 "Display a list of nonempty registers saying briefly what they contain."
172 (interactive)
173 (let ((list (copy-sequence register-alist)))
174 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
175 (with-output-to-temp-buffer "*Output*"
176 (dolist (elt list)
177 (when (get-register (car elt))
178 (describe-register-1 (car elt))
179 (terpri))))))
181 (defun describe-register-1 (register &optional verbose)
182 (princ "Register ")
183 (princ (single-key-description register))
184 (princ " contains ")
185 (let ((val (get-register register)))
186 (cond
187 ((numberp val)
188 (princ val))
190 ((markerp val)
191 (let ((buf (marker-buffer val)))
192 (if (null buf)
193 (princ "a marker in no buffer")
194 (princ "a buffer position:\n buffer ")
195 (princ (buffer-name buf))
196 (princ ", position ")
197 (princ (marker-position val)))))
199 ((and (consp val) (window-configuration-p (car val)))
200 (princ "a window configuration."))
202 ((and (consp val) (frame-configuration-p (car val)))
203 (princ "a frame configuration."))
205 ((and (consp val) (eq (car val) 'file))
206 (princ "the file ")
207 (prin1 (cdr val))
208 (princ "."))
210 ((and (consp val) (eq (car val) 'file-query))
211 (princ "a file-query reference:\n file ")
212 (prin1 (car (cdr val)))
213 (princ ",\n position ")
214 (princ (car (cdr (cdr val))))
215 (princ "."))
217 ((consp val)
218 (if verbose
219 (progn
220 (princ "the rectangle:\n")
221 (while val
222 (princ " ")
223 (princ (car val))
224 (terpri)
225 (setq val (cdr val))))
226 (princ "a rectangle starting with ")
227 (princ (car val))))
229 ((stringp val)
230 (if (eq yank-excluded-properties t)
231 (set-text-properties 0 (length val) nil val)
232 (remove-list-of-text-properties 0 (length val)
233 yank-excluded-properties val))
234 (if verbose
235 (progn
236 (princ "the text:\n")
237 (princ val))
238 (cond
239 ;; Extract first N characters starting with first non-whitespace.
240 ((string-match (format "[^ \t\n].\\{,%d\\}"
241 ;; Deduct 6 for the spaces inserted below.
242 (min 20 (max 0 (- (window-width) 6))))
243 val)
244 (princ "text starting with\n ")
245 (princ (match-string 0 val)))
246 ((string-match "^[ \t\n]+$" val)
247 (princ "whitespace"))
249 (princ "the empty string")))))
251 (princ "Garbage:\n")
252 (if verbose (prin1 val))))))
254 (defun insert-register (register &optional arg)
255 "Insert contents of register REGISTER. (REGISTER is a character.)
256 Normally puts point before and mark after the inserted text.
257 If optional second arg is non-nil, puts mark before and point after.
258 Interactively, second arg is non-nil if prefix arg is supplied."
259 (interactive "*cInsert register: \nP")
260 (push-mark)
261 (let ((val (get-register register)))
262 (cond
263 ((consp val)
264 (insert-rectangle val))
265 ((stringp val)
266 (insert-for-yank val))
267 ((numberp val)
268 (princ val (current-buffer)))
269 ((and (markerp val) (marker-position val))
270 (princ (marker-position val) (current-buffer)))
272 (error "Register does not contain text"))))
273 (if (not arg) (exchange-point-and-mark)))
275 (defun copy-to-register (register start end &optional delete-flag)
276 "Copy region into register REGISTER. With prefix arg, delete as well.
277 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
278 START and END are buffer positions indicating what to copy."
279 (interactive "cCopy to register: \nr\nP")
280 (set-register register (filter-buffer-substring start end))
281 (if delete-flag (delete-region start end)))
283 (defun append-to-register (register start end &optional delete-flag)
284 "Append region to text in register REGISTER.
285 With prefix arg, delete as well.
286 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
287 START and END are buffer positions indicating what to append."
288 (interactive "cAppend to register: \nr\nP")
289 (or (stringp (get-register register))
290 (error "Register does not contain text"))
291 (set-register register (concat (get-register register)
292 (filter-buffer-substring start end)))
293 (if delete-flag (delete-region start end)))
295 (defun prepend-to-register (register start end &optional delete-flag)
296 "Prepend region to text in register REGISTER.
297 With prefix arg, delete as well.
298 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
299 START and END are buffer positions indicating what to prepend."
300 (interactive "cPrepend to register: \nr\nP")
301 (or (stringp (get-register register))
302 (error "Register does not contain text"))
303 (set-register register (concat (filter-buffer-substring start end)
304 (get-register register)))
305 (if delete-flag (delete-region start end)))
307 (defun copy-rectangle-to-register (register start end &optional delete-flag)
308 "Copy rectangular region into register REGISTER.
309 With prefix arg, delete as well. To insert this register
310 in the buffer, use \\[insert-register].
312 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
313 START and END are buffer positions giving two corners of rectangle."
314 (interactive "cCopy rectangle to register: \nr\nP")
315 (set-register register
316 (if delete-flag
317 (delete-extract-rectangle start end)
318 (extract-rectangle start end))))
320 (provide 'register)
321 ;;; arch-tag: ce14dd68-8265-475f-9341-5d4ec5a53035
322 ;;; register.el ends here