Inline functions to examine and change buffer intervals.
[emacs.git] / lisp / register.el
blob2816c9831deea32256eec14f1d8a99924a430f6b
1 ;;; register.el --- register commands for Emacs
3 ;; Copyright (C) 1985, 1993-1994, 2001-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 (eval-when-compile (require 'cl-lib))
33 (declare-function semantic-insert-foreign-tag "semantic/tag" (foreign-tag))
34 (declare-function semantic-tag-buffer "semantic/tag" (tag))
35 (declare-function semantic-tag-start "semantic/tag" (tag))
37 ;;; Code:
39 (cl-defstruct
40 (registerv (:constructor nil)
41 (:constructor registerv--make (&optional data print-func
42 jump-func insert-func))
43 (:copier nil)
44 (:type vector)
45 :named)
46 (data nil :read-only t)
47 (print-func nil :read-only t)
48 (jump-func nil :read-only t)
49 (insert-func nil :read-only t))
51 (cl-defun registerv-make (data &key print-func jump-func insert-func)
52 "Create a register value object.
54 DATA can be any value.
55 PRINT-FUNC if provided controls how `list-registers' and
56 `view-register' print the register. It should be a function
57 receiving one argument DATA and print text that completes
58 this sentence:
59 Register X contains [TEXT PRINTED BY PRINT-FUNC]
60 JUMP-FUNC if provided, controls how `jump-to-register' jumps to the register.
61 INSERT-FUNC if provided, controls how `insert-register' insert the register.
62 They both receive DATA as argument."
63 (registerv--make data print-func jump-func insert-func))
65 (defvar register-alist nil
66 "Alist of elements (NAME . CONTENTS), one for each Emacs register.
67 NAME is a character (a number). CONTENTS is a string, number, marker, list
68 or a struct returned by `registerv-make'.
69 A list of strings represents a rectangle.
70 A list of the form (file . FILE-NAME) represents the file named FILE-NAME.
71 A list of the form (file-query FILE-NAME POSITION) represents
72 position POSITION in the file named FILE-NAME, but query before
73 visiting it.
74 A list of the form (WINDOW-CONFIGURATION POSITION)
75 represents a saved window configuration plus a saved value of point.
76 A list of the form (FRAME-CONFIGURATION POSITION)
77 represents a saved frame configuration plus a saved value of point.")
79 (defun get-register (register)
80 "Return contents of Emacs register named REGISTER, or nil if none."
81 (cdr (assq register register-alist)))
83 (defun set-register (register value)
84 "Set contents of Emacs register named REGISTER to VALUE. Returns VALUE.
85 See the documentation of the variable `register-alist' for possible VALUEs."
86 (let ((aelt (assq register register-alist)))
87 (if aelt
88 (setcdr aelt value)
89 (push (cons register value) register-alist))
90 value))
92 (defun point-to-register (register &optional arg)
93 "Store current location of point in register REGISTER.
94 With prefix argument, store current frame configuration.
95 Use \\[jump-to-register] to go to that location or restore that configuration.
96 Argument is a character, naming the register."
97 (interactive "cPoint to register: \nP")
98 ;; Turn the marker into a file-ref if the buffer is killed.
99 (add-hook 'kill-buffer-hook 'register-swap-out nil t)
100 (set-register register
101 (if arg (list (current-frame-configuration) (point-marker))
102 (point-marker))))
104 (defun window-configuration-to-register (register &optional _arg)
105 "Store the window configuration of the selected frame in register REGISTER.
106 Use \\[jump-to-register] to restore the configuration.
107 Argument is a character, naming the register."
108 (interactive "cWindow configuration to register: \nP")
109 ;; current-window-configuration does not include the value
110 ;; of point in the current buffer, so record that separately.
111 (set-register register (list (current-window-configuration) (point-marker))))
113 (defun frame-configuration-to-register (register &optional _arg)
114 "Store the window configuration of all frames in register REGISTER.
115 Use \\[jump-to-register] to restore the configuration.
116 Argument is a character, naming the register."
117 (interactive "cFrame configuration to register: \nP")
118 ;; current-frame-configuration does not include the value
119 ;; of point in the current buffer, so record that separately.
120 (set-register register (list (current-frame-configuration) (point-marker))))
122 (defalias 'register-to-point 'jump-to-register)
123 (defun jump-to-register (register &optional delete)
124 "Move point to location stored in a register.
125 If the register contains a file name, find that file.
126 \(To put a file name in a register, you must use `set-register'.)
127 If the register contains a window configuration (one frame) or a frame
128 configuration (all frames), restore that frame or all frames accordingly.
129 First argument is a character, naming the register.
130 Optional second arg non-nil (interactively, prefix argument) says to
131 delete any existing frames that the frame configuration doesn't mention.
132 \(Otherwise, these frames are iconified.)"
133 (interactive "cJump to register: \nP")
134 (let ((val (get-register register)))
135 (cond
136 ((registerv-p val)
137 (cl-assert (registerv-jump-func val) nil
138 "Don't know how to jump to register %s"
139 (single-key-description register))
140 (funcall (registerv-jump-func val) (registerv-data val)))
141 ((and (consp val) (frame-configuration-p (car val)))
142 (set-frame-configuration (car val) (not delete))
143 (goto-char (cadr val)))
144 ((and (consp val) (window-configuration-p (car val)))
145 (set-window-configuration (car val))
146 (goto-char (cadr val)))
147 ((markerp val)
148 (or (marker-buffer val)
149 (error "That register's buffer no longer exists"))
150 (switch-to-buffer (marker-buffer val))
151 (goto-char val))
152 ((and (consp val) (eq (car val) 'file))
153 (find-file (cdr val)))
154 ((and (consp val) (eq (car val) 'file-query))
155 (or (find-buffer-visiting (nth 1 val))
156 (y-or-n-p (format "Visit file %s again? " (nth 1 val)))
157 (error "Register access aborted"))
158 (find-file (nth 1 val))
159 (goto-char (nth 2 val)))
160 ((and (fboundp 'semantic-foreign-tag-p)
161 semantic-mode
162 (semantic-foreign-tag-p val))
163 (switch-to-buffer (semantic-tag-buffer val))
164 (goto-char (semantic-tag-start val)))
166 (error "Register doesn't contain a buffer position or configuration")))))
168 (defun register-swap-out ()
169 "Turn markers into file-query references when a buffer is killed."
170 (and buffer-file-name
171 (dolist (elem register-alist)
172 (and (markerp (cdr elem))
173 (eq (marker-buffer (cdr elem)) (current-buffer))
174 (setcdr elem
175 (list 'file-query
176 buffer-file-name
177 (marker-position (cdr elem))))))))
179 (defun number-to-register (number register)
180 "Store a number in a register.
181 Two args, NUMBER and REGISTER (a character, naming the register).
182 If NUMBER is nil, a decimal number is read from the buffer starting
183 at point, and point moves to the end of that number.
184 Interactively, NUMBER is the prefix arg (none means nil)."
185 (interactive "P\ncNumber to register: ")
186 (set-register register
187 (if number
188 (prefix-numeric-value number)
189 (if (looking-at "\\s-*-?[0-9]+")
190 (progn
191 (goto-char (match-end 0))
192 (string-to-number (match-string 0)))
193 0))))
195 (defun increment-register (number register)
196 "Add NUMBER to the contents of register REGISTER.
197 Interactively, NUMBER is the prefix arg."
198 (interactive "p\ncIncrement register: ")
199 (or (numberp (get-register register))
200 (error "Register does not contain a number"))
201 (set-register register (+ number (get-register register))))
203 (defun view-register (register)
204 "Display what is contained in register named REGISTER.
205 The Lisp value REGISTER is a character."
206 (interactive "cView register: ")
207 (let ((val (get-register register)))
208 (if (null val)
209 (message "Register %s is empty" (single-key-description register))
210 (with-output-to-temp-buffer "*Output*"
211 (describe-register-1 register t)))))
213 (defun list-registers ()
214 "Display a list of nonempty registers saying briefly what they contain."
215 (interactive)
216 (let ((list (copy-sequence register-alist)))
217 (setq list (sort list (lambda (a b) (< (car a) (car b)))))
218 (with-output-to-temp-buffer "*Output*"
219 (dolist (elt list)
220 (when (get-register (car elt))
221 (describe-register-1 (car elt))
222 (terpri))))))
224 (defun describe-register-1 (register &optional verbose)
225 (princ "Register ")
226 (princ (single-key-description register))
227 (princ " contains ")
228 (let ((val (get-register register)))
229 (cond
230 ((registerv-p val)
231 (if (registerv-print-func val)
232 (funcall (registerv-print-func val) (registerv-data val))
233 (princ "[UNPRINTABLE CONTENTS].")))
235 ((numberp val)
236 (princ val))
238 ((markerp val)
239 (let ((buf (marker-buffer val)))
240 (if (null buf)
241 (princ "a marker in no buffer")
242 (princ "a buffer position:\n buffer ")
243 (princ (buffer-name buf))
244 (princ ", position ")
245 (princ (marker-position val)))))
247 ((and (consp val) (window-configuration-p (car val)))
248 (princ "a window configuration."))
250 ((and (consp val) (frame-configuration-p (car val)))
251 (princ "a frame configuration."))
253 ((and (consp val) (eq (car val) 'file))
254 (princ "the file ")
255 (prin1 (cdr val))
256 (princ "."))
258 ((and (consp val) (eq (car val) 'file-query))
259 (princ "a file-query reference:\n file ")
260 (prin1 (car (cdr val)))
261 (princ ",\n position ")
262 (princ (car (cdr (cdr val))))
263 (princ "."))
265 ((consp val)
266 (if verbose
267 (progn
268 (princ "the rectangle:\n")
269 (while val
270 (princ " ")
271 (princ (car val))
272 (terpri)
273 (setq val (cdr val))))
274 (princ "a rectangle starting with ")
275 (princ (car val))))
277 ((stringp val)
278 (if (eq yank-excluded-properties t)
279 (set-text-properties 0 (length val) nil val)
280 (remove-list-of-text-properties 0 (length val)
281 yank-excluded-properties val))
282 (if verbose
283 (progn
284 (princ "the text:\n")
285 (princ val))
286 (cond
287 ;; Extract first N characters starting with first non-whitespace.
288 ((string-match (format "[^ \t\n].\\{,%d\\}"
289 ;; Deduct 6 for the spaces inserted below.
290 (min 20 (max 0 (- (window-width) 6))))
291 val)
292 (princ "text starting with\n ")
293 (princ (match-string 0 val)))
294 ((string-match "^[ \t\n]+$" val)
295 (princ "whitespace"))
297 (princ "the empty string")))))
299 (princ "Garbage:\n")
300 (if verbose (prin1 val))))))
302 (defun insert-register (register &optional arg)
303 "Insert contents of register REGISTER. (REGISTER is a character.)
304 Normally puts point before and mark after the inserted text.
305 If optional second arg is non-nil, puts mark before and point after.
306 Interactively, second arg is non-nil if prefix arg is supplied."
307 (interactive "*cInsert register: \nP")
308 (push-mark)
309 (let ((val (get-register register)))
310 (cond
311 ((registerv-p val)
312 (cl-assert (registerv-insert-func val) nil
313 "Don't know how to insert register %s"
314 (single-key-description register))
315 (funcall (registerv-insert-func val) (registerv-data val)))
316 ((consp val)
317 (insert-rectangle val))
318 ((stringp val)
319 (insert-for-yank val))
320 ((numberp val)
321 (princ val (current-buffer)))
322 ((and (markerp val) (marker-position val))
323 (princ (marker-position val) (current-buffer)))
324 ((and (fboundp 'semantic-foreign-tag-p)
325 semantic-mode
326 (semantic-foreign-tag-p val))
327 (semantic-insert-foreign-tag val))
329 (error "Register does not contain text"))))
330 (if (not arg) (exchange-point-and-mark)))
332 (defun copy-to-register (register start end &optional delete-flag)
333 "Copy region into register REGISTER.
334 With prefix arg, delete as well.
335 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
336 START and END are buffer positions indicating what to copy."
337 (interactive "cCopy to register: \nr\nP")
338 (set-register register (filter-buffer-substring start end))
339 (setq deactivate-mark t)
340 (cond (delete-flag
341 (delete-region start end))
342 ((called-interactively-p 'interactive)
343 (indicate-copied-region))))
345 (defun append-to-register (register start end &optional delete-flag)
346 "Append region to text in register REGISTER.
347 With prefix arg, delete as well.
348 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
349 START and END are buffer positions indicating what to append."
350 (interactive "cAppend to register: \nr\nP")
351 (let ((reg (get-register register))
352 (text (filter-buffer-substring start end)))
353 (set-register
354 register (cond ((not reg) text)
355 ((stringp reg) (concat reg text))
356 (t (error "Register does not contain text")))))
357 (cond (delete-flag
358 (delete-region start end))
359 ((called-interactively-p 'interactive)
360 (indicate-copied-region))))
362 (defun prepend-to-register (register start end &optional delete-flag)
363 "Prepend region to text in register REGISTER.
364 With prefix arg, delete as well.
365 Called from program, takes four args: REGISTER, START, END and DELETE-FLAG.
366 START and END are buffer positions indicating what to prepend."
367 (interactive "cPrepend to register: \nr\nP")
368 (let ((reg (get-register register))
369 (text (filter-buffer-substring start end)))
370 (set-register
371 register (cond ((not reg) text)
372 ((stringp reg) (concat text reg))
373 (t (error "Register does not contain text")))))
374 (cond (delete-flag
375 (delete-region start end))
376 ((called-interactively-p 'interactive)
377 (indicate-copied-region))))
379 (defun copy-rectangle-to-register (register start end &optional delete-flag)
380 "Copy rectangular region into register REGISTER.
381 With prefix arg, delete as well.
382 To insert this register in the buffer, use \\[insert-register].
384 Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
385 START and END are buffer positions giving two corners of rectangle."
386 (interactive "cCopy rectangle to register: \nr\nP")
387 (let ((rectangle (if delete-flag
388 (delete-extract-rectangle start end)
389 (extract-rectangle start end))))
390 (set-register register rectangle)
391 (when (and (null delete-flag)
392 (called-interactively-p 'interactive))
393 (setq deactivate-mark t)
394 (indicate-copied-region (length (car rectangle))))))
397 (provide 'register)
398 ;;; register.el ends here