]> code.delx.au - gnu-emacs/blob - lisp/register.el
*** empty log message ***
[gnu-emacs] / lisp / register.el
1 ;; Register commands for Emacs.
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21 (defvar register-alist nil
22 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
23 NAME is a character (a number). CONTENTS is a string, number,
24 screen configuration, mark or list.
25 A list represents a rectangle; its elements are strings.")
26
27 (defun get-register (char)
28 "Return contents of Emacs register named CHAR, or nil if none."
29 (cdr (assq char register-alist)))
30
31 (defun set-register (char value)
32 "Set contents of Emacs register named CHAR to VALUE. Returns VALUE."
33 (let ((aelt (assq char register-alist)))
34 (if aelt
35 (setcdr aelt value)
36 (setq aelt (cons char value))
37 (setq register-alist (cons aelt register-alist)))
38 value))
39
40 (defun point-to-register (char arg)
41 "Store current location of point in register REGISTER.
42 With prefix argument, store current screen configuration.
43 Use \\[jump-to-register] to go to that location or restore that configuration.
44 Argument is a character, naming the register."
45 (interactive "cPoint to register: \nP")
46 (set-register char (if arg (current-screen-configuration) (point-marker))))
47
48 (fset 'register-to-point 'jump-to-register)
49 (defun jump-to-register (char)
50 "Move point to location stored in a register.
51 Argument is a character, naming the register."
52 (interactive "cJump to register: ")
53 (let ((val (get-register char)))
54 (condition-case ()
55 (set-screen-configuration val)
56 (error
57 (if (markerp val)
58 (progn
59 (switch-to-buffer (marker-buffer val))
60 (goto-char val))
61 (error "Register doesn't contain a buffer position or screen configuration"))))))
62
63 ;(defun number-to-register (arg char)
64 ; "Store a number in a register.
65 ;Two args, NUMBER and REGISTER (a character, naming the register).
66 ;If NUMBER is nil, digits in the buffer following point are read
67 ;to get the number to store.
68 ;Interactively, NUMBER is the prefix arg (none means nil)."
69 ; (interactive "P\ncNumber to register: ")
70 ; (set-register char
71 ; (if arg
72 ; (prefix-numeric-value arg)
73 ; (if (looking-at "[0-9][0-9]*")
74 ; (save-excursion
75 ; (save-restriction
76 ; (narrow-to-region (point)
77 ; (progn (skip-chars-forward "0-9")
78 ; (point)))
79 ; (goto-char (point-min))
80 ; (read (current-buffer))))
81 ; 0))))
82
83 ;(defun increment-register (arg char)
84 ; "Add NUMBER to the contents of register REGISTER.
85 ;Interactively, NUMBER is the prefix arg (none means nil)."
86 ; (interactive "p\ncNumber to register: ")
87 ; (or (integerp (get-register char))
88 ; (error "Register does not contain a number"))
89 ; (set-register char (+ arg (get-register char))))
90
91 (defun view-register (char)
92 "Display what is contained in register named REGISTER.
93 REGISTER is a character."
94 (interactive "cView register: ")
95 (let ((val (get-register char)))
96 (if (null val)
97 (message "Register %s is empty" (single-key-description char))
98 (with-output-to-temp-buffer "*Output*"
99 (princ "Register ")
100 (princ (single-key-description char))
101 (princ " contains ")
102 (if (integerp val)
103 (princ val)
104 (if (markerp val)
105 (progn
106 (princ "a buffer position:\nbuffer ")
107 (princ (buffer-name (marker-buffer val)))
108 (princ ", position ")
109 (princ (+ 0 val)))
110 (if (consp val)
111 (progn
112 (princ "the rectangle:\n")
113 (while val
114 (princ (car val))
115 (terpri)
116 (setq val (cdr val))))
117 (princ "the string:\n")
118 (princ val))))))))
119
120 (defun insert-register (char &optional arg)
121 "Insert contents of register REG. REG is a character.
122 Normally puts point before and mark after the inserted text.
123 If optional second arg is non-nil, puts mark before and point after.
124 Interactively, second arg is non-nil if prefix arg is supplied."
125 (interactive "cInsert register: \nP")
126 (push-mark)
127 (let ((val (get-register char)))
128 (if (consp val)
129 (insert-rectangle val)
130 (if (stringp val)
131 (insert val)
132 (if (or (integerp val) (markerp val))
133 (princ (+ 0 val) (current-buffer))
134 (error "Register does not contain text")))))
135 (if (not arg) (exchange-point-and-mark)))
136
137 (defun copy-to-register (char start end &optional delete-flag)
138 "Copy region into register REG. With prefix arg, delete as well.
139 Called from program, takes four args: REG, START, END and DELETE-FLAG.
140 START and END are buffer positions indicating what to copy."
141 (interactive "cCopy to register: \nr\nP")
142 (set-register char (buffer-substring start end))
143 (if delete-flag (delete-region start end)))
144
145 (defun append-to-register (char start end &optional delete-flag)
146 "Append region to text in register REG. With prefix arg, delete as well.
147 Called from program, takes four args: REG, START, END and DELETE-FLAG.
148 START and END are buffer positions indicating what to append."
149 (interactive "cAppend to register: \nr\nP")
150 (or (stringp (get-register char))
151 (error "Register does not contain text"))
152 (set-register char (concat (get-register char)
153 (buffer-substring start end)))
154 (if delete-flag (delete-region start end)))
155
156 (defun prepend-to-register (char start end &optional delete-flag)
157 "Prepend region to text in register REG. With prefix arg, delete as well.
158 Called from program, takes four args: REG, START, END and DELETE-FLAG.
159 START and END are buffer positions indicating what to prepend."
160 (interactive "cPrepend to register: \nr\nP")
161 (or (stringp (get-register char))
162 (error "Register does not contain text"))
163 (set-register char (concat (buffer-substring start end)
164 (get-register char)))
165 (if delete-flag (delete-region start end)))
166
167 (defun copy-rectangle-to-register (char start end &optional delete-flag)
168 "Copy rectangular region into register REG. With prefix arg, delete as well.
169 Called from program, takes four args: REG, START, END and DELETE-FLAG.
170 START and END are buffer positions giving two corners of rectangle."
171 (interactive "cCopy rectangle to register: \nr\nP")
172 (set-register char
173 (if delete-flag
174 (delete-extract-rectangle start end)
175 (extract-rectangle start end))))