Commit | Line | Data |
---|---|---|
85698d63 | 1 | ;;; register.el --- register commands for Emacs -*- lexical-binding: t; -*- |
c88ab9ce | 2 | |
ba318903 PE |
3 | ;; Copyright (C) 1985, 1993-1994, 2001-2014 Free Software Foundation, |
4 | ;; Inc. | |
9750e079 | 5 | |
34dc21db | 6 | ;; Maintainer: emacs-devel@gnu.org |
d7b4d18f | 7 | ;; Keywords: internal |
bd78fa1d | 8 | ;; Package: emacs |
4821e2af | 9 | |
efeae993 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
efeae993 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
efeae993 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
efeae993 | 24 | |
d9ecc911 ER |
25 | ;;; Commentary: |
26 | ||
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. | |
31 | ||
f58e0fd5 | 32 | (eval-when-compile (require 'cl-lib)) |
6302e0d3 | 33 | |
4821e2af | 34 | ;;; Code: |
efeae993 | 35 | |
f58e0fd5 | 36 | (cl-defstruct |
6302e0d3 LL |
37 | (registerv (:constructor nil) |
38 | (:constructor registerv--make (&optional data print-func | |
39 | jump-func insert-func)) | |
40 | (:copier nil) | |
41 | (:type vector) | |
42 | :named) | |
43 | (data nil :read-only t) | |
44 | (print-func nil :read-only t) | |
45 | (jump-func nil :read-only t) | |
46 | (insert-func nil :read-only t)) | |
47 | ||
f58e0fd5 | 48 | (cl-defun registerv-make (data &key print-func jump-func insert-func) |
6302e0d3 LL |
49 | "Create a register value object. |
50 | ||
51 | DATA can be any value. | |
52 | PRINT-FUNC if provided controls how `list-registers' and | |
53 | `view-register' print the register. It should be a function | |
9173deec | 54 | receiving one argument DATA and print text that completes |
6302e0d3 LL |
55 | this sentence: |
56 | Register X contains [TEXT PRINTED BY PRINT-FUNC] | |
57 | JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register. | |
58 | INSERT-FUNC if provided, controls how `insert-register' insert the register. | |
59 | They both receive DATA as argument." | |
60 | (registerv--make data print-func jump-func insert-func)) | |
61 | ||
efeae993 RS |
62 | (defvar register-alist nil |
63 | "Alist of elements (NAME . CONTENTS), one for each Emacs register. | |
6302e0d3 LL |
64 | NAME is a character (a number). CONTENTS is a string, number, marker, list |
65 | or a struct returned by `registerv-make'. | |
22073dda | 66 | A list of strings represents a rectangle. |
5858bcc4 CY |
67 | A list of the form (file . FILE-NAME) represents the file named FILE-NAME. |
68 | A list of the form (file-query FILE-NAME POSITION) represents | |
69 | position POSITION in the file named FILE-NAME, but query before | |
70 | visiting it. | |
070c2506 KH |
71 | A list of the form (WINDOW-CONFIGURATION POSITION) |
72 | represents a saved window configuration plus a saved value of point. | |
73 | A list of the form (FRAME-CONFIGURATION POSITION) | |
77187e6f | 74 | represents a saved frame configuration plus a saved value of point.") |
efeae993 | 75 | |
0979429b J |
76 | (defgroup register nil |
77 | "Register commands." | |
78 | :group 'convenience | |
bfabf70a | 79 | :version "24.3") |
0979429b | 80 | |
bfabf70a AS |
81 | (defcustom register-separator nil |
82 | "Register containing the text to put between collected texts, or nil if none. | |
0979429b | 83 | |
a5765a3d JB |
84 | When collecting text with \\[append-to-register] (or \\[prepend-to-register]), |
85 | contents of this register is added to the beginning (or end, respectively) | |
86 | of the marked text." | |
0979429b J |
87 | :group 'register |
88 | :type '(choice (const :tag "None" nil) | |
89 | (character :tag "Use register" :value ?+))) | |
90 | ||
85698d63 | 91 | (defcustom register-preview-delay 1 |
da942af1 GM |
92 | "If non-nil, time to wait in seconds before popping up a preview window. |
93 | If nil, do not show register previews, unless `help-char' (or a member of | |
94 | `help-event-list') is pressed." | |
bb098075 | 95 | :version "24.4" |
da942af1 | 96 | :type '(choice number (const :tag "No preview unless requested" nil)) |
85698d63 LL |
97 | :group 'register) |
98 | ||
1a86cc81 JB |
99 | (defun get-register (register) |
100 | "Return contents of Emacs register named REGISTER, or nil if none." | |
101 | (cdr (assq register register-alist))) | |
efeae993 | 102 | |
1b8cac5d RS |
103 | (defun set-register (register value) |
104 | "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE. | |
1a86cc81 | 105 | See the documentation of the variable `register-alist' for possible VALUEs." |
1b8cac5d | 106 | (let ((aelt (assq register register-alist))) |
efeae993 RS |
107 | (if aelt |
108 | (setcdr aelt value) | |
ddbb3cc7 | 109 | (push (cons register value) register-alist)) |
efeae993 RS |
110 | value)) |
111 | ||
85698d63 LL |
112 | (defun register-describe-oneline (c) |
113 | "One-line description of register C." | |
114 | (let ((d (replace-regexp-in-string | |
115 | "\n[ \t]*" " " | |
116 | (with-output-to-string (describe-register-1 c))))) | |
117 | (if (string-match "Register.+? contains \\(?:an? \\|the \\)?" d) | |
118 | (substring d (match-end 0)) | |
119 | d))) | |
120 | ||
7c324762 GM |
121 | (defun register-preview-default (r) |
122 | "Default function for the variable `register-preview-function'." | |
a5765a3d JB |
123 | (format "%s: %s\n" |
124 | (single-key-description (car r)) | |
7c324762 GM |
125 | (register-describe-oneline (car r)))) |
126 | ||
127 | (defvar register-preview-function #'register-preview-default | |
128 | "Function to format a register for previewing. | |
129 | Takes one argument, a cons (NAME . CONTENTS) as found in `register-alist'. | |
130 | Returns a string.") | |
85698d63 LL |
131 | |
132 | (defun register-preview (buffer &optional show-empty) | |
133 | "Pop up a window to show register preview in BUFFER. | |
7c324762 GM |
134 | If SHOW-EMPTY is non-nil show the window even if no registers. |
135 | Format of each entry is controlled by the variable `register-preview-function'." | |
85698d63 | 136 | (when (or show-empty (consp register-alist)) |
b92631bf | 137 | (with-current-buffer-window |
cf2b7efc MR |
138 | buffer |
139 | (cons 'display-buffer-below-selected | |
140 | '((window-height . fit-window-to-buffer))) | |
141 | nil | |
142 | (with-current-buffer standard-output | |
143 | (setq cursor-in-non-selected-windows nil) | |
7c324762 | 144 | (insert (mapconcat register-preview-function register-alist "")))))) |
85698d63 LL |
145 | |
146 | (defun register-read-with-preview (prompt) | |
4472a196 GM |
147 | "Read and return a register name, possibly showing existing registers. |
148 | Prompt with the string PROMPT. If `register-alist' and | |
149 | `register-preview-delay' are both non-nil, display a window | |
150 | listing existing registers after `register-preview-delay' seconds. | |
151 | If `help-char' (or a member of `help-event-list') is pressed, | |
152 | display such a window regardless." | |
85698d63 LL |
153 | (let* ((buffer "*Register Preview*") |
154 | (timer (when (numberp register-preview-delay) | |
155 | (run-with-timer register-preview-delay nil | |
156 | (lambda () | |
157 | (unless (get-buffer-window buffer) | |
158 | (register-preview buffer)))))) | |
159 | (help-chars (cl-loop for c in (cons help-char help-event-list) | |
160 | when (not (get-register c)) | |
161 | collect c))) | |
162 | (unwind-protect | |
163 | (progn | |
164 | (while (memq (read-event (propertize prompt 'face 'minibuffer-prompt)) | |
165 | help-chars) | |
166 | (unless (get-buffer-window buffer) | |
167 | (register-preview buffer 'show-empty))) | |
7c324762 GM |
168 | (if (characterp last-input-event) last-input-event |
169 | (error "Non-character input-event"))) | |
85698d63 LL |
170 | (and (timerp timer) (cancel-timer timer)) |
171 | (let ((w (get-buffer-window buffer))) | |
172 | (and (window-live-p w) (delete-window w))) | |
173 | (and (get-buffer buffer) (kill-buffer buffer))))) | |
174 | ||
1b8cac5d | 175 | (defun point-to-register (register &optional arg) |
b42e6156 | 176 | "Store current location of point in register REGISTER. |
0cc89026 | 177 | With prefix argument, store current frame configuration. |
b42e6156 | 178 | Use \\[jump-to-register] to go to that location or restore that configuration. |
4472a196 GM |
179 | Argument is a character, naming the register. |
180 | ||
181 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
182 | (interactive (list (register-read-with-preview "Point to register: ") |
183 | current-prefix-arg)) | |
ddbb3cc7 SM |
184 | ;; Turn the marker into a file-ref if the buffer is killed. |
185 | (add-hook 'kill-buffer-hook 'register-swap-out nil t) | |
1b8cac5d | 186 | (set-register register |
070c2506 KH |
187 | (if arg (list (current-frame-configuration) (point-marker)) |
188 | (point-marker)))) | |
efeae993 | 189 | |
06b60517 | 190 | (defun window-configuration-to-register (register &optional _arg) |
83b5d757 RS |
191 | "Store the window configuration of the selected frame in register REGISTER. |
192 | Use \\[jump-to-register] to restore the configuration. | |
4472a196 GM |
193 | Argument is a character, naming the register. |
194 | ||
195 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
196 | (interactive (list (register-read-with-preview |
197 | "Window configuration to register: ") | |
198 | current-prefix-arg)) | |
b4a91f43 KH |
199 | ;; current-window-configuration does not include the value |
200 | ;; of point in the current buffer, so record that separately. | |
070c2506 | 201 | (set-register register (list (current-window-configuration) (point-marker)))) |
83b5d757 | 202 | |
7bcbca40 GM |
203 | ;; It has had the optional arg for ages, but never used it. |
204 | (set-advertised-calling-convention 'window-configuration-to-register | |
205 | '(register) "24.4") | |
206 | ||
06b60517 | 207 | (defun frame-configuration-to-register (register &optional _arg) |
83b5d757 RS |
208 | "Store the window configuration of all frames in register REGISTER. |
209 | Use \\[jump-to-register] to restore the configuration. | |
4472a196 GM |
210 | Argument is a character, naming the register. |
211 | ||
212 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
213 | (interactive (list (register-read-with-preview |
214 | "Frame configuration to register: ") | |
215 | current-prefix-arg)) | |
b4a91f43 KH |
216 | ;; current-frame-configuration does not include the value |
217 | ;; of point in the current buffer, so record that separately. | |
070c2506 | 218 | (set-register register (list (current-frame-configuration) (point-marker)))) |
83b5d757 | 219 | |
7bcbca40 GM |
220 | ;; It has had the optional arg for ages, but never used it. |
221 | (set-advertised-calling-convention 'frame-configuration-to-register | |
222 | '(register) "24.4") | |
223 | ||
123c84f9 GM |
224 | (make-obsolete 'frame-configuration-to-register 'frameset-to-register' "24.4") |
225 | ||
31e1d920 | 226 | (defalias 'register-to-point 'jump-to-register) |
1b8cac5d | 227 | (defun jump-to-register (register &optional delete) |
efeae993 | 228 | "Move point to location stored in a register. |
22073dda | 229 | If the register contains a file name, find that file. |
1a86cc81 | 230 | \(To put a file name in a register, you must use `set-register'.) |
2805a651 JB |
231 | If the register contains a window configuration (one frame) or a frameset |
232 | \(all frames), restore that frame or all frames accordingly. | |
e7683fff | 233 | First argument is a character, naming the register. |
1542ad37 | 234 | Optional second arg non-nil (interactively, prefix argument) says to |
2805a651 | 235 | delete any existing frames that the frameset doesn't mention. |
4472a196 GM |
236 | \(Otherwise, these frames are iconified.) |
237 | ||
238 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
239 | (interactive (list (register-read-with-preview "Jump to register: ") |
240 | current-prefix-arg)) | |
1b8cac5d | 241 | (let ((val (get-register register))) |
376a7584 | 242 | (cond |
6302e0d3 | 243 | ((registerv-p val) |
f58e0fd5 | 244 | (cl-assert (registerv-jump-func val) nil |
6302e0d3 LL |
245 | "Don't know how to jump to register %s" |
246 | (single-key-description register)) | |
247 | (funcall (registerv-jump-func val) (registerv-data val))) | |
b4a91f43 KH |
248 | ((and (consp val) (frame-configuration-p (car val))) |
249 | (set-frame-configuration (car val) (not delete)) | |
250 | (goto-char (cadr val))) | |
251 | ((and (consp val) (window-configuration-p (car val))) | |
252 | (set-window-configuration (car val)) | |
253 | (goto-char (cadr val))) | |
376a7584 | 254 | ((markerp val) |
8c4ca60c KH |
255 | (or (marker-buffer val) |
256 | (error "That register's buffer no longer exists")) | |
376a7584 JB |
257 | (switch-to-buffer (marker-buffer val)) |
258 | (goto-char val)) | |
22073dda RS |
259 | ((and (consp val) (eq (car val) 'file)) |
260 | (find-file (cdr val))) | |
28cbd14d RS |
261 | ((and (consp val) (eq (car val) 'file-query)) |
262 | (or (find-buffer-visiting (nth 1 val)) | |
263 | (y-or-n-p (format "Visit file %s again? " (nth 1 val))) | |
264 | (error "Register access aborted")) | |
265 | (find-file (nth 1 val)) | |
266 | (goto-char (nth 2 val))) | |
376a7584 JB |
267 | (t |
268 | (error "Register doesn't contain a buffer position or configuration"))))) | |
efeae993 | 269 | |
28cbd14d | 270 | (defun register-swap-out () |
ddbb3cc7 | 271 | "Turn markers into file-query references when a buffer is killed." |
28cbd14d | 272 | (and buffer-file-name |
ddbb3cc7 SM |
273 | (dolist (elem register-alist) |
274 | (and (markerp (cdr elem)) | |
275 | (eq (marker-buffer (cdr elem)) (current-buffer)) | |
276 | (setcdr elem | |
277 | (list 'file-query | |
278 | buffer-file-name | |
279 | (marker-position (cdr elem)))))))) | |
28cbd14d | 280 | |
0e07a458 | 281 | (defun number-to-register (number register) |
070c2506 KH |
282 | "Store a number in a register. |
283 | Two args, NUMBER and REGISTER (a character, naming the register). | |
4d2caa07 KH |
284 | If NUMBER is nil, a decimal number is read from the buffer starting |
285 | at point, and point moves to the end of that number. | |
4472a196 GM |
286 | Interactively, NUMBER is the prefix arg (none means nil). |
287 | ||
288 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
289 | (interactive (list current-prefix-arg |
290 | (register-read-with-preview "Number to register: "))) | |
4f23d31c | 291 | (set-register register |
0e07a458 KH |
292 | (if number |
293 | (prefix-numeric-value number) | |
4d2caa07 KH |
294 | (if (looking-at "\\s-*-?[0-9]+") |
295 | (progn | |
296 | (goto-char (match-end 0)) | |
fe22eed0 | 297 | (string-to-number (match-string 0))) |
070c2506 KH |
298 | 0)))) |
299 | ||
0979429b J |
300 | (defun increment-register (prefix register) |
301 | "Augment contents of REGISTER. | |
302 | Interactively, PREFIX is in raw form. | |
303 | ||
304 | If REGISTER contains a number, add `prefix-numeric-value' of | |
305 | PREFIX to it. | |
306 | ||
307 | If REGISTER is empty or if it contains text, call | |
6a6b8e40 GM |
308 | `append-to-register' with `delete-flag' set to PREFIX. |
309 | ||
310 | Interactively, reads the register using `register-read-with-preview'." | |
311 | (interactive (list current-prefix-arg | |
312 | (register-read-with-preview "Increment register: "))) | |
0979429b J |
313 | (let ((register-val (get-register register))) |
314 | (cond | |
315 | ((numberp register-val) | |
316 | (let ((number (prefix-numeric-value prefix))) | |
317 | (set-register register (+ number register-val)))) | |
318 | ((or (not register-val) (stringp register-val)) | |
319 | (append-to-register register (region-beginning) (region-end) prefix)) | |
320 | (t (error "Register does not contain a number or text"))))) | |
efeae993 | 321 | |
1b8cac5d | 322 | (defun view-register (register) |
efeae993 | 323 | "Display what is contained in register named REGISTER. |
4472a196 GM |
324 | The Lisp value REGISTER is a character. |
325 | ||
326 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 | 327 | (interactive (list (register-read-with-preview "View register: "))) |
1b8cac5d | 328 | (let ((val (get-register register))) |
efeae993 | 329 | (if (null val) |
1b8cac5d | 330 | (message "Register %s is empty" (single-key-description register)) |
efeae993 | 331 | (with-output-to-temp-buffer "*Output*" |
92840a31 RS |
332 | (describe-register-1 register t))))) |
333 | ||
334 | (defun list-registers () | |
335 | "Display a list of nonempty registers saying briefly what they contain." | |
336 | (interactive) | |
337 | (let ((list (copy-sequence register-alist))) | |
338 | (setq list (sort list (lambda (a b) (< (car a) (car b))))) | |
339 | (with-output-to-temp-buffer "*Output*" | |
340 | (dolist (elt list) | |
341 | (when (get-register (car elt)) | |
342 | (describe-register-1 (car elt)) | |
343 | (terpri)))))) | |
344 | ||
345 | (defun describe-register-1 (register &optional verbose) | |
346 | (princ "Register ") | |
347 | (princ (single-key-description register)) | |
348 | (princ " contains ") | |
6ed21409 RS |
349 | (let ((val (get-register register))) |
350 | (cond | |
6302e0d3 LL |
351 | ((registerv-p val) |
352 | (if (registerv-print-func val) | |
353 | (funcall (registerv-print-func val) (registerv-data val)) | |
354 | (princ "[UNPRINTABLE CONTENTS]."))) | |
355 | ||
6ed21409 RS |
356 | ((numberp val) |
357 | (princ val)) | |
358 | ||
359 | ((markerp val) | |
360 | (let ((buf (marker-buffer val))) | |
361 | (if (null buf) | |
362 | (princ "a marker in no buffer") | |
363 | (princ "a buffer position:\n buffer ") | |
364 | (princ (buffer-name buf)) | |
365 | (princ ", position ") | |
366 | (princ (marker-position val))))) | |
367 | ||
368 | ((and (consp val) (window-configuration-p (car val))) | |
369 | (princ "a window configuration.")) | |
370 | ||
371 | ((and (consp val) (frame-configuration-p (car val))) | |
372 | (princ "a frame configuration.")) | |
373 | ||
374 | ((and (consp val) (eq (car val) 'file)) | |
375 | (princ "the file ") | |
376 | (prin1 (cdr val)) | |
377 | (princ ".")) | |
378 | ||
379 | ((and (consp val) (eq (car val) 'file-query)) | |
380 | (princ "a file-query reference:\n file ") | |
381 | (prin1 (car (cdr val))) | |
382 | (princ ",\n position ") | |
383 | (princ (car (cdr (cdr val)))) | |
384 | (princ ".")) | |
385 | ||
386 | ((consp val) | |
387 | (if verbose | |
388 | (progn | |
389 | (princ "the rectangle:\n") | |
390 | (while val | |
391 | (princ " ") | |
392 | (princ (car val)) | |
393 | (terpri) | |
394 | (setq val (cdr val)))) | |
395 | (princ "a rectangle starting with ") | |
396 | (princ (car val)))) | |
397 | ||
398 | ((stringp val) | |
00a2b823 | 399 | (setq val (copy-sequence val)) |
9c7cc04b RS |
400 | (if (eq yank-excluded-properties t) |
401 | (set-text-properties 0 (length val) nil val) | |
402 | (remove-list-of-text-properties 0 (length val) | |
403 | yank-excluded-properties val)) | |
6ed21409 RS |
404 | (if verbose |
405 | (progn | |
406 | (princ "the text:\n") | |
407 | (princ val)) | |
f1180544 | 408 | (cond |
13d6f302 RS |
409 | ;; Extract first N characters starting with first non-whitespace. |
410 | ((string-match (format "[^ \t\n].\\{,%d\\}" | |
411 | ;; Deduct 6 for the spaces inserted below. | |
412 | (min 20 (max 0 (- (window-width) 6)))) | |
413 | val) | |
414 | (princ "text starting with\n ") | |
415 | (princ (match-string 0 val))) | |
416 | ((string-match "^[ \t\n]+$" val) | |
417 | (princ "whitespace")) | |
418 | (t | |
419 | (princ "the empty string"))))) | |
6ed21409 RS |
420 | (t |
421 | (princ "Garbage:\n") | |
422 | (if verbose (prin1 val)))))) | |
efeae993 | 423 | |
1b8cac5d RS |
424 | (defun insert-register (register &optional arg) |
425 | "Insert contents of register REGISTER. (REGISTER is a character.) | |
efeae993 RS |
426 | Normally puts point before and mark after the inserted text. |
427 | If optional second arg is non-nil, puts mark before and point after. | |
4472a196 GM |
428 | Interactively, second arg is non-nil if prefix arg is supplied. |
429 | ||
430 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
431 | (interactive (progn |
432 | (barf-if-buffer-read-only) | |
5ea75d23 BG |
433 | (list (register-read-with-preview "Insert register: ") |
434 | current-prefix-arg))) | |
efeae993 | 435 | (push-mark) |
1b8cac5d | 436 | (let ((val (get-register register))) |
cbd4993c | 437 | (cond |
6302e0d3 | 438 | ((registerv-p val) |
f58e0fd5 | 439 | (cl-assert (registerv-insert-func val) nil |
6302e0d3 LL |
440 | "Don't know how to insert register %s" |
441 | (single-key-description register)) | |
442 | (funcall (registerv-insert-func val) (registerv-data val))) | |
2d43b8c9 LL |
443 | ((consp val) |
444 | (insert-rectangle val)) | |
cbd4993c | 445 | ((stringp val) |
e7c765c3 | 446 | (insert-for-yank val)) |
070c2506 | 447 | ((numberp val) |
cbd4993c KH |
448 | (princ val (current-buffer))) |
449 | ((and (markerp val) (marker-position val)) | |
450 | (princ (marker-position val) (current-buffer))) | |
451 | (t | |
452 | (error "Register does not contain text")))) | |
efeae993 RS |
453 | (if (not arg) (exchange-point-and-mark))) |
454 | ||
00a2b823 | 455 | (defun copy-to-register (register start end &optional delete-flag region) |
1a86cc81 JB |
456 | "Copy region into register REGISTER. |
457 | With prefix arg, delete as well. | |
a5765a3d JB |
458 | Called from program, takes five args: REGISTER, START, END, DELETE-FLAG, |
459 | and REGION. START and END are buffer positions indicating what to copy. | |
460 | The optional argument REGION if non-nil, indicates that we're not just | |
461 | copying some text between START and END, but we're copying the region. | |
4472a196 GM |
462 | |
463 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
464 | (interactive (list (register-read-with-preview "Copy to register: ") |
465 | (region-beginning) | |
466 | (region-end) | |
00a2b823 SM |
467 | current-prefix-arg |
468 | t)) | |
469 | (set-register register (if region | |
470 | (funcall region-extract-function delete-flag) | |
471 | (prog1 (filter-buffer-substring start end) | |
472 | (if delete-flag (delete-region start end))))) | |
2549c068 | 473 | (setq deactivate-mark t) |
00a2b823 | 474 | (cond (delete-flag) |
2549c068 CY |
475 | ((called-interactively-p 'interactive) |
476 | (indicate-copied-region)))) | |
efeae993 | 477 | |
1b8cac5d RS |
478 | (defun append-to-register (register start end &optional delete-flag) |
479 | "Append region to text in register REGISTER. | |
480 | With prefix arg, delete as well. | |
481 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
4472a196 GM |
482 | START and END are buffer positions indicating what to append. |
483 | ||
484 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
485 | (interactive (list (register-read-with-preview "Append to register: ") |
486 | (region-beginning) | |
487 | (region-end) | |
488 | current-prefix-arg)) | |
c81f72ce | 489 | (let ((reg (get-register register)) |
0979429b | 490 | (text (filter-buffer-substring start end)) |
bfabf70a | 491 | (separator (and register-separator (get-register register-separator)))) |
c81f72ce TTN |
492 | (set-register |
493 | register (cond ((not reg) text) | |
0979429b | 494 | ((stringp reg) (concat reg separator text)) |
c81f72ce | 495 | (t (error "Register does not contain text"))))) |
5694896d | 496 | (setq deactivate-mark t) |
2549c068 CY |
497 | (cond (delete-flag |
498 | (delete-region start end)) | |
499 | ((called-interactively-p 'interactive) | |
500 | (indicate-copied-region)))) | |
efeae993 | 501 | |
1b8cac5d RS |
502 | (defun prepend-to-register (register start end &optional delete-flag) |
503 | "Prepend region to text in register REGISTER. | |
504 | With prefix arg, delete as well. | |
505 | Called from program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
4472a196 GM |
506 | START and END are buffer positions indicating what to prepend. |
507 | ||
508 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
509 | (interactive (list (register-read-with-preview "Prepend to register: ") |
510 | (region-beginning) | |
511 | (region-end) | |
512 | current-prefix-arg)) | |
c81f72ce | 513 | (let ((reg (get-register register)) |
0979429b | 514 | (text (filter-buffer-substring start end)) |
bfabf70a | 515 | (separator (and register-separator (get-register register-separator)))) |
c81f72ce TTN |
516 | (set-register |
517 | register (cond ((not reg) text) | |
0979429b | 518 | ((stringp reg) (concat text separator reg)) |
c81f72ce | 519 | (t (error "Register does not contain text"))))) |
5694896d | 520 | (setq deactivate-mark t) |
2549c068 CY |
521 | (cond (delete-flag |
522 | (delete-region start end)) | |
523 | ((called-interactively-p 'interactive) | |
524 | (indicate-copied-region)))) | |
efeae993 | 525 | |
1b8cac5d RS |
526 | (defun copy-rectangle-to-register (register start end &optional delete-flag) |
527 | "Copy rectangular region into register REGISTER. | |
1a86cc81 JB |
528 | With prefix arg, delete as well. |
529 | To insert this register in the buffer, use \\[insert-register]. | |
5ef5d6ce RS |
530 | |
531 | Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG. | |
4472a196 GM |
532 | START and END are buffer positions giving two corners of rectangle. |
533 | ||
534 | Interactively, reads the register using `register-read-with-preview'." | |
85698d63 LL |
535 | (interactive (list (register-read-with-preview |
536 | "Copy rectangle to register: ") | |
537 | (region-beginning) | |
538 | (region-end) | |
539 | current-prefix-arg)) | |
2549c068 CY |
540 | (let ((rectangle (if delete-flag |
541 | (delete-extract-rectangle start end) | |
542 | (extract-rectangle start end)))) | |
543 | (set-register register rectangle) | |
544 | (when (and (null delete-flag) | |
545 | (called-interactively-p 'interactive)) | |
546 | (setq deactivate-mark t) | |
547 | (indicate-copied-region (length (car rectangle)))))) | |
548 | ||
0f214cdf | 549 | (provide 'register) |
c88ab9ce | 550 | ;;; register.el ends here |