]> code.delx.au - gnu-emacs/blob - lisp/register.el
Initial revision
[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.
33 Returns VALUE."
34 (let ((aelt (assq char register-alist)))
35 (if aelt
36 (setcdr aelt value)
37 (setq aelt (cons char value))
38 (setq register-alist (cons aelt register-alist)))
39 value))
40
41 (defun point-to-register (char arg)
42 "Store current location of point in register REGISTER.
43 With prefix argument, store current screen configuration.
44 Use \\[jump-to-register] to go to that location or restore that configuration.
45 Argument is a character, naming the register."
46 (interactive "cPoint to register: \nP")
47 (set-register char (if arg (current-screen-configuration) (point-marker))))
48
49 (fset 'register-to-point 'jump-to-register)
50 (defun jump-to-register (char)
51 "Move point to location stored in a register.
52 Argument is a character, naming the register."
53 (interactive "cJump to register: ")
54 (let ((val (get-register char)))
55 (condition-case ()
56 (set-screen-configuration val)
57 (error
58 (if (markerp val)
59 (progn
60 (switch-to-buffer (marker-buffer val))
61 (goto-char val))
62 (error "Register doesn't contain a buffer position or screen configuration"))))))
63
64 ;(defun number-to-register (arg char)
65 ; "Store a number in a register.
66 ;Two args, NUMBER and REGISTER (a character, naming the register).
67 ;If NUMBER is nil, digits in the buffer following point are read
68 ;to get the number to store.
69 ;Interactively, NUMBER is the prefix arg (none means nil)."
70 ; (interactive "P\ncNumber to register: ")
71 ; (set-register char
72 ; (if arg
73 ; (prefix-numeric-value arg)
74 ; (if (looking-at "[0-9][0-9]*")
75 ; (save-excursion
76 ; (save-restriction
77 ; (narrow-to-region (point)
78 ; (progn (skip-chars-forward "0-9")
79 ; (point)))
80 ; (goto-char (point-min))
81 ; (read (current-buffer))))
82 ; 0))))
83
84 ;(defun increment-register (arg char)
85 ; "Add NUMBER to the contents of register REGISTER.
86 ;Interactively, NUMBER is the prefix arg (none means nil)."
87 ; (interactive "p\ncNumber to register: ")
88 ; (or (integerp (get-register char))
89 ; (error "Register does not contain a number"))
90 ; (set-register char (+ arg (get-register char))))
91
92 (defun view-register (char)
93 "Display what is contained in register named REGISTER.
94 REGISTER is a character."
95 (interactive "cView register: ")
96 (let ((val (get-register char)))
97 (if (null val)
98 (message "Register %s is empty" (single-key-description char))
99 (with-output-to-temp-buffer "*Output*"
100 (princ "Register ")
101 (princ (single-key-description char))
102 (princ " contains ")
103 (if (integerp val)
104 (princ val)
105 (if (markerp val)
106 (progn
107 (princ "a buffer position:\nbuffer ")
108 (princ (buffer-name (marker-buffer val)))
109 (princ ", position ")
110 (princ (+ 0 val)))
111 (if (consp val)
112 (progn
113 (princ "the rectangle:\n")
114 (while val
115 (princ (car val))
116 (terpri)
117 (setq val (cdr val))))
118 (princ "the string:\n")
119 (princ val))))))))
120
121 (defun insert-register (char &optional arg)
122 "Insert contents of register REG. REG is a character.
123 Normally puts point before and mark after the inserted text.
124 If optional second arg is non-nil, puts mark before and point after.
125 Interactively, second arg is non-nil if prefix arg is supplied."
126 (interactive "cInsert register: \nP")
127 (push-mark)
128 (let ((val (get-register char)))
129 (if (consp val)
130 (insert-rectangle val)
131 (if (stringp val)
132 (insert val)
133 (if (or (integerp val) (markerp val))
134 (princ (+ 0 val) (current-buffer))
135 (error "Register does not contain text")))))
136 (if (not arg) (exchange-point-and-mark)))
137
138 (defun copy-to-register (char start end &optional delete-flag)
139 "Copy region into register REG.
140 With prefix arg, delete as well.
141 Called from program, takes four args:
142 REG, START, END and DELETE-FLAG.
143 START and END are buffer positions indicating what to copy."
144 (interactive "cCopy to register: \nr\nP")
145 (set-register char (buffer-substring start end))
146 (if delete-flag (delete-region start end)))
147
148 (defun append-to-register (char start end &optional delete-flag)
149 "Append region to text in register REG.
150 With prefix arg, delete as well.
151 Called from program, takes four args:
152 REG, START, END and DELETE-FLAG.
153 START and END are buffer positions indicating what to append."
154 (interactive "cAppend to register: \nr\nP")
155 (or (stringp (get-register char))
156 (error "Register does not contain text"))
157 (set-register char (concat (get-register char)
158 (buffer-substring start end)))
159 (if delete-flag (delete-region start end)))
160
161 (defun prepend-to-register (char start end &optional delete-flag)
162 "Prepend region to text in register REG.
163 With prefix arg, delete as well.
164 Called from program, takes four args:
165 REG, START, END and DELETE-FLAG.
166 START and END are buffer positions indicating what to prepend."
167 (interactive "cPrepend to register: \nr\nP")
168 (or (stringp (get-register char))
169 (error "Register does not contain text"))
170 (set-register char (concat (buffer-substring start end)
171 (get-register char)))
172 (if delete-flag (delete-region start end)))
173
174 (defun copy-rectangle-to-register (char start end &optional delete-flag)
175 "Copy rectangular region into register REG.
176 With prefix arg, delete as well.
177 Called from program, takes four args:
178 REG, START, END and DELETE-FLAG.
179 START and END are buffer positions giving two corners of rectangle."
180 (interactive "cCopy rectangle to register: \nr\nP")
181 (set-register char
182 (if delete-flag
183 (delete-extract-rectangle start end)
184 (extract-rectangle start end))))