]> code.delx.au - gnu-emacs/blob - lisp/subr.el
(save-abbrevs): Default value is t.
[gnu-emacs] / lisp / subr.el
1 ;;; subr.el --- basic lisp subroutines for Emacs
2
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26 (defvar custom-declare-variable-list nil
27 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
28 Each element of this list holds the arguments to one call to `defcustom'.")
29
30 ;; Use this, rather than defcustom, in subr.el and other files loaded
31 ;; before custom.el.
32 (defun custom-declare-variable-early (&rest arguments)
33 (setq custom-declare-variable-list
34 (cons arguments custom-declare-variable-list)))
35 \f
36 ;;;; Lisp language features.
37
38 (defalias 'not 'null)
39
40 (defmacro lambda (&rest cdr)
41 "Return a lambda expression.
42 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
43 self-quoting; the result of evaluating the lambda expression is the
44 expression itself. The lambda expression may then be treated as a
45 function, i.e., stored as the function value of a symbol, passed to
46 funcall or mapcar, etc.
47
48 ARGS should take the same form as an argument list for a `defun'.
49 DOCSTRING is an optional documentation string.
50 If present, it should describe how to call the function.
51 But documentation strings are usually not useful in nameless functions.
52 INTERACTIVE should be a call to the function `interactive', which see.
53 It may also be omitted.
54 BODY should be a list of lisp expressions."
55 ;; Note that this definition should not use backquotes; subr.el should not
56 ;; depend on backquote.el.
57 (list 'function (cons 'lambda cdr)))
58
59 (defmacro push (newelt listname)
60 "Add NEWELT to the list stored in the symbol LISTNAME.
61 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
62 LISTNAME must be a symbol."
63 (list 'setq listname
64 (list 'cons newelt listname)))
65
66 (defmacro pop (listname)
67 "Return the first element of LISTNAME's value, and remove it from the list.
68 LISTNAME must be a symbol whose value is a list.
69 If the value is nil, `pop' returns nil but does not actually
70 change the list."
71 (list 'prog1 (list 'car listname)
72 (list 'setq listname (list 'cdr listname))))
73
74 (defmacro when (cond &rest body)
75 "If COND yields non-nil, do BODY, else return nil."
76 (list 'if cond (cons 'progn body)))
77
78 (defmacro unless (cond &rest body)
79 "If COND yields nil, do BODY, else return nil."
80 (cons 'if (cons cond (cons nil body))))
81
82 (defmacro dolist (spec &rest body)
83 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
84 Evaluate BODY with VAR bound to each car from LIST, in turn.
85 Then evaluate RESULT to get return value, default nil."
86 (let ((temp (make-symbol "--dolist-temp--")))
87 (list 'let (list (list temp (nth 1 spec)) (car spec))
88 (list 'while temp
89 (list 'setq (car spec) (list 'car temp))
90 (cons 'progn
91 (append body
92 (list (list 'setq temp (list 'cdr temp))))))
93 (if (cdr (cdr spec))
94 (cons 'progn
95 (cons (list 'setq (car spec) nil) (cdr (cdr spec))))))))
96
97 (defmacro dotimes (spec &rest body)
98 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
99 Evaluate BODY with VAR bound to successive integers running from 0,
100 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
101 the return value (nil if RESULT is omitted)."
102 (let ((temp (make-symbol "--dotimes-temp--")))
103 (list 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
104 (list 'while (list '< (car spec) temp)
105 (cons 'progn
106 (append body (list (list 'setq (car spec)
107 (list '1+ (car spec)))))))
108 (if (cdr (cdr spec))
109 (car (cdr (cdr spec)))
110 nil))))
111
112 (defsubst caar (x)
113 "Return the car of the car of X."
114 (car (car x)))
115
116 (defsubst cadr (x)
117 "Return the car of the cdr of X."
118 (car (cdr x)))
119
120 (defsubst cdar (x)
121 "Return the cdr of the car of X."
122 (cdr (car x)))
123
124 (defsubst cddr (x)
125 "Return the cdr of the cdr of X."
126 (cdr (cdr x)))
127
128 (defun last (x &optional n)
129 "Return the last link of the list X. Its car is the last element.
130 If X is nil, return nil.
131 If N is non-nil, return the Nth-to-last link of X.
132 If N is bigger than the length of X, return X."
133 (if n
134 (let ((m 0) (p x))
135 (while (consp p)
136 (setq m (1+ m) p (cdr p)))
137 (if (<= n 0) p
138 (if (< n m) (nthcdr (- m n) x) x)))
139 (while (consp (cdr x))
140 (setq x (cdr x)))
141 x))
142
143 (defun butlast (x &optional n)
144 "Returns a copy of LIST with the last N elements removed."
145 (if (and n (<= n 0)) x
146 (nbutlast (copy-sequence x) n)))
147
148 (defun nbutlast (x &optional n)
149 "Modifies LIST to remove the last N elements."
150 (let ((m (length x)))
151 (or n (setq n 1))
152 (and (< n m)
153 (progn
154 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
155 x))))
156
157 (defun remove (elt seq)
158 "Return a copy of SEQ with all occurences of ELT removed.
159 SEQ must be a list, vector, or string. The comparison is done with `equal'."
160 (if (nlistp seq)
161 ;; If SEQ isn't a list, there's no need to copy SEQ because
162 ;; `delete' will return a new object.
163 (delete elt seq)
164 (delete elt (copy-sequence seq))))
165
166 (defun remq (elt list)
167 "Return a copy of LIST with all occurences of ELT removed.
168 The comparison is done with `eq'."
169 (if (memq elt list)
170 (delq elt (copy-sequence list))
171 list))
172
173 (defun assoc-default (key alist &optional test default)
174 "Find object KEY in a pseudo-alist ALIST.
175 ALIST is a list of conses or objects. Each element (or the element's car,
176 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
177 If that is non-nil, the element matches;
178 then `assoc-default' returns the element's cdr, if it is a cons,
179 or DEFAULT if the element is not a cons.
180
181 If no element matches, the value is nil.
182 If TEST is omitted or nil, `equal' is used."
183 (let (found (tail alist) value)
184 (while (and tail (not found))
185 (let ((elt (car tail)))
186 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
187 (setq found t value (if (consp elt) (cdr elt) default))))
188 (setq tail (cdr tail)))
189 value))
190
191 (defun assoc-ignore-case (key alist)
192 "Like `assoc', but ignores differences in case and text representation.
193 KEY must be a string. Upper-case and lower-case letters are treated as equal.
194 Unibyte strings are converted to multibyte for comparison."
195 (let (element)
196 (while (and alist (not element))
197 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
198 (setq element (car alist)))
199 (setq alist (cdr alist)))
200 element))
201
202 (defun assoc-ignore-representation (key alist)
203 "Like `assoc', but ignores differences in text representation.
204 KEY must be a string.
205 Unibyte strings are converted to multibyte for comparison."
206 (let (element)
207 (while (and alist (not element))
208 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
209 (setq element (car alist)))
210 (setq alist (cdr alist)))
211 element))
212
213 (defun member-ignore-case (elt list)
214 "Like `member', but ignores differences in case and text representation.
215 ELT must be a string. Upper-case and lower-case letters are treated as equal.
216 Unibyte strings are converted to multibyte for comparison."
217 (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t))))
218 (setq list (cdr list)))
219 list)
220
221 \f
222 ;;;; Keymap support.
223
224 (defun undefined ()
225 (interactive)
226 (ding))
227
228 ;Prevent the \{...} documentation construct
229 ;from mentioning keys that run this command.
230 (put 'undefined 'suppress-keymap t)
231
232 (defun suppress-keymap (map &optional nodigits)
233 "Make MAP override all normally self-inserting keys to be undefined.
234 Normally, as an exception, digits and minus-sign are set to make prefix args,
235 but optional second arg NODIGITS non-nil treats them like other chars."
236 (substitute-key-definition 'self-insert-command 'undefined map global-map)
237 (or nodigits
238 (let (loop)
239 (define-key map "-" 'negative-argument)
240 ;; Make plain numbers do numeric args.
241 (setq loop ?0)
242 (while (<= loop ?9)
243 (define-key map (char-to-string loop) 'digit-argument)
244 (setq loop (1+ loop))))))
245
246 ;Moved to keymap.c
247 ;(defun copy-keymap (keymap)
248 ; "Return a copy of KEYMAP"
249 ; (while (not (keymapp keymap))
250 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
251 ; (if (vectorp keymap)
252 ; (copy-sequence keymap)
253 ; (copy-alist keymap)))
254
255 (defvar key-substitution-in-progress nil
256 "Used internally by substitute-key-definition.")
257
258 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
259 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
260 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
261 Alternatively, if optional fourth argument OLDMAP is specified, we redefine
262 in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP."
263 ;; Don't document PREFIX in the doc string because we don't want to
264 ;; advertise it. It's meant for recursive calls only. Here's its
265 ;; meaning
266
267 ;; If optional argument PREFIX is specified, it should be a key
268 ;; prefix, a string. Redefined bindings will then be bound to the
269 ;; original key, with PREFIX added at the front.
270 (or prefix (setq prefix ""))
271 (let* ((scan (or oldmap keymap))
272 (vec1 (vector nil))
273 (prefix1 (vconcat prefix vec1))
274 (key-substitution-in-progress
275 (cons scan key-substitution-in-progress)))
276 ;; Scan OLDMAP, finding each char or event-symbol that
277 ;; has any definition, and act on it with hack-key.
278 (while (consp scan)
279 (if (consp (car scan))
280 (let ((char (car (car scan)))
281 (defn (cdr (car scan))))
282 ;; The inside of this let duplicates exactly
283 ;; the inside of the following let that handles array elements.
284 (aset vec1 0 char)
285 (aset prefix1 (length prefix) char)
286 (let (inner-def skipped)
287 ;; Skip past menu-prompt.
288 (while (stringp (car-safe defn))
289 (setq skipped (cons (car defn) skipped))
290 (setq defn (cdr defn)))
291 ;; Skip past cached key-equivalence data for menu items.
292 (and (consp defn) (consp (car defn))
293 (setq defn (cdr defn)))
294 (setq inner-def defn)
295 ;; Look past a symbol that names a keymap.
296 (while (and (symbolp inner-def)
297 (fboundp inner-def))
298 (setq inner-def (symbol-function inner-def)))
299 (if (or (eq defn olddef)
300 ;; Compare with equal if definition is a key sequence.
301 ;; That is useful for operating on function-key-map.
302 (and (or (stringp defn) (vectorp defn))
303 (equal defn olddef)))
304 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
305 (if (and (keymapp defn)
306 ;; Avoid recursively scanning
307 ;; where KEYMAP does not have a submap.
308 (let ((elt (lookup-key keymap prefix1)))
309 (or (null elt)
310 (keymapp elt)))
311 ;; Avoid recursively rescanning keymap being scanned.
312 (not (memq inner-def
313 key-substitution-in-progress)))
314 ;; If this one isn't being scanned already,
315 ;; scan it now.
316 (substitute-key-definition olddef newdef keymap
317 inner-def
318 prefix1)))))
319 (if (vectorp (car scan))
320 (let* ((array (car scan))
321 (len (length array))
322 (i 0))
323 (while (< i len)
324 (let ((char i) (defn (aref array i)))
325 ;; The inside of this let duplicates exactly
326 ;; the inside of the previous let.
327 (aset vec1 0 char)
328 (aset prefix1 (length prefix) char)
329 (let (inner-def skipped)
330 ;; Skip past menu-prompt.
331 (while (stringp (car-safe defn))
332 (setq skipped (cons (car defn) skipped))
333 (setq defn (cdr defn)))
334 (and (consp defn) (consp (car defn))
335 (setq defn (cdr defn)))
336 (setq inner-def defn)
337 (while (and (symbolp inner-def)
338 (fboundp inner-def))
339 (setq inner-def (symbol-function inner-def)))
340 (if (or (eq defn olddef)
341 (and (or (stringp defn) (vectorp defn))
342 (equal defn olddef)))
343 (define-key keymap prefix1
344 (nconc (nreverse skipped) newdef))
345 (if (and (keymapp defn)
346 (let ((elt (lookup-key keymap prefix1)))
347 (or (null elt)
348 (keymapp elt)))
349 (not (memq inner-def
350 key-substitution-in-progress)))
351 (substitute-key-definition olddef newdef keymap
352 inner-def
353 prefix1)))))
354 (setq i (1+ i))))
355 (if (char-table-p (car scan))
356 (map-char-table
357 (function (lambda (char defn)
358 (let ()
359 ;; The inside of this let duplicates exactly
360 ;; the inside of the previous let,
361 ;; except that it uses set-char-table-range
362 ;; instead of define-key.
363 (aset vec1 0 char)
364 (aset prefix1 (length prefix) char)
365 (let (inner-def skipped)
366 ;; Skip past menu-prompt.
367 (while (stringp (car-safe defn))
368 (setq skipped (cons (car defn) skipped))
369 (setq defn (cdr defn)))
370 (and (consp defn) (consp (car defn))
371 (setq defn (cdr defn)))
372 (setq inner-def defn)
373 (while (and (symbolp inner-def)
374 (fboundp inner-def))
375 (setq inner-def (symbol-function inner-def)))
376 (if (or (eq defn olddef)
377 (and (or (stringp defn) (vectorp defn))
378 (equal defn olddef)))
379 (define-key keymap prefix1
380 (nconc (nreverse skipped) newdef))
381 (if (and (keymapp defn)
382 (let ((elt (lookup-key keymap prefix1)))
383 (or (null elt)
384 (keymapp elt)))
385 (not (memq inner-def
386 key-substitution-in-progress)))
387 (substitute-key-definition olddef newdef keymap
388 inner-def
389 prefix1)))))))
390 (car scan)))))
391 (setq scan (cdr scan)))))
392
393 (defun define-key-after (keymap key definition &optional after)
394 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
395 This is like `define-key' except that the binding for KEY is placed
396 just after the binding for the event AFTER, instead of at the beginning
397 of the map. Note that AFTER must be an event type (like KEY), NOT a command
398 \(like DEFINITION).
399
400 If AFTER is t or omitted, the new binding goes at the end of the keymap.
401 AFTER should be a single event type--a symbol or a character, not a sequence.
402
403 Bindings are always added before any inherited map.
404
405 The order of bindings in a keymap matters when it is used as a menu."
406 (unless after (setq after t))
407 (or (keymapp keymap)
408 (signal 'wrong-type-argument (list 'keymapp keymap)))
409 (setq key
410 (if (<= (length key) 1) (aref key 0)
411 (setq keymap (lookup-key keymap
412 (apply 'vector
413 (butlast (mapcar 'identity key)))))
414 (aref key (1- (length key)))))
415 (let ((tail keymap) done inserted)
416 (while (and (not done) tail)
417 ;; Delete any earlier bindings for the same key.
418 (if (eq (car-safe (car (cdr tail))) key)
419 (setcdr tail (cdr (cdr tail))))
420 ;; If we hit an included map, go down that one.
421 (if (keymapp (car tail)) (setq tail (car tail)))
422 ;; When we reach AFTER's binding, insert the new binding after.
423 ;; If we reach an inherited keymap, insert just before that.
424 ;; If we reach the end of this keymap, insert at the end.
425 (if (or (and (eq (car-safe (car tail)) after)
426 (not (eq after t)))
427 (eq (car (cdr tail)) 'keymap)
428 (null (cdr tail)))
429 (progn
430 ;; Stop the scan only if we find a parent keymap.
431 ;; Keep going past the inserted element
432 ;; so we can delete any duplications that come later.
433 (if (eq (car (cdr tail)) 'keymap)
434 (setq done t))
435 ;; Don't insert more than once.
436 (or inserted
437 (setcdr tail (cons (cons key definition) (cdr tail))))
438 (setq inserted t)))
439 (setq tail (cdr tail)))))
440
441 (defmacro kbd (keys)
442 "Convert KEYS to the internal Emacs key representation.
443 KEYS should be a string constant in the format used for
444 saving keyboard macros (see `insert-kbd-macro')."
445 (read-kbd-macro keys))
446
447 (put 'keyboard-translate-table 'char-table-extra-slots 0)
448
449 (defun keyboard-translate (from to)
450 "Translate character FROM to TO at a low level.
451 This function creates a `keyboard-translate-table' if necessary
452 and then modifies one entry in it."
453 (or (char-table-p keyboard-translate-table)
454 (setq keyboard-translate-table
455 (make-char-table 'keyboard-translate-table nil)))
456 (aset keyboard-translate-table from to))
457
458 \f
459 ;;;; The global keymap tree.
460
461 ;;; global-map, esc-map, and ctl-x-map have their values set up in
462 ;;; keymap.c; we just give them docstrings here.
463
464 (defvar global-map nil
465 "Default global keymap mapping Emacs keyboard input into commands.
466 The value is a keymap which is usually (but not necessarily) Emacs's
467 global map.")
468
469 (defvar esc-map nil
470 "Default keymap for ESC (meta) commands.
471 The normal global definition of the character ESC indirects to this keymap.")
472
473 (defvar ctl-x-map nil
474 "Default keymap for C-x commands.
475 The normal global definition of the character C-x indirects to this keymap.")
476
477 (defvar ctl-x-4-map (make-sparse-keymap)
478 "Keymap for subcommands of C-x 4")
479 (defalias 'ctl-x-4-prefix ctl-x-4-map)
480 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
481
482 (defvar ctl-x-5-map (make-sparse-keymap)
483 "Keymap for frame commands.")
484 (defalias 'ctl-x-5-prefix ctl-x-5-map)
485 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
486
487 \f
488 ;;;; Event manipulation functions.
489
490 ;; The call to `read' is to ensure that the value is computed at load time
491 ;; and not compiled into the .elc file. The value is negative on most
492 ;; machines, but not on all!
493 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
494
495 (defun listify-key-sequence (key)
496 "Convert a key sequence to a list of events."
497 (if (vectorp key)
498 (append key nil)
499 (mapcar (function (lambda (c)
500 (if (> c 127)
501 (logxor c listify-key-sequence-1)
502 c)))
503 (append key nil))))
504
505 (defsubst eventp (obj)
506 "True if the argument is an event object."
507 (or (integerp obj)
508 (and (symbolp obj)
509 (get obj 'event-symbol-elements))
510 (and (consp obj)
511 (symbolp (car obj))
512 (get (car obj) 'event-symbol-elements))))
513
514 (defun event-modifiers (event)
515 "Returns a list of symbols representing the modifier keys in event EVENT.
516 The elements of the list may include `meta', `control',
517 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
518 and `down'."
519 (let ((type event))
520 (if (listp type)
521 (setq type (car type)))
522 (if (symbolp type)
523 (cdr (get type 'event-symbol-elements))
524 (let ((list nil))
525 (or (zerop (logand type ?\M-\^@))
526 (setq list (cons 'meta list)))
527 (or (and (zerop (logand type ?\C-\^@))
528 (>= (logand type 127) 32))
529 (setq list (cons 'control list)))
530 (or (and (zerop (logand type ?\S-\^@))
531 (= (logand type 255) (downcase (logand type 255))))
532 (setq list (cons 'shift list)))
533 (or (zerop (logand type ?\H-\^@))
534 (setq list (cons 'hyper list)))
535 (or (zerop (logand type ?\s-\^@))
536 (setq list (cons 'super list)))
537 (or (zerop (logand type ?\A-\^@))
538 (setq list (cons 'alt list)))
539 list))))
540
541 (defun event-basic-type (event)
542 "Returns the basic type of the given event (all modifiers removed).
543 The value is a printing character (not upper case) or a symbol."
544 (if (consp event)
545 (setq event (car event)))
546 (if (symbolp event)
547 (car (get event 'event-symbol-elements))
548 (let ((base (logand event (1- (lsh 1 18)))))
549 (downcase (if (< base 32) (logior base 64) base)))))
550
551 (defsubst mouse-movement-p (object)
552 "Return non-nil if OBJECT is a mouse movement event."
553 (and (consp object)
554 (eq (car object) 'mouse-movement)))
555
556 (defsubst event-start (event)
557 "Return the starting position of EVENT.
558 If EVENT is a mouse press or a mouse click, this returns the location
559 of the event.
560 If EVENT is a drag, this returns the drag's starting position.
561 The return value is of the form
562 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
563 The `posn-' functions access elements of such lists."
564 (nth 1 event))
565
566 (defsubst event-end (event)
567 "Return the ending location of EVENT. EVENT should be a click or drag event.
568 If EVENT is a click event, this function is the same as `event-start'.
569 The return value is of the form
570 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
571 The `posn-' functions access elements of such lists."
572 (nth (if (consp (nth 2 event)) 2 1) event))
573
574 (defsubst event-click-count (event)
575 "Return the multi-click count of EVENT, a click or drag event.
576 The return value is a positive integer."
577 (if (integerp (nth 2 event)) (nth 2 event) 1))
578
579 (defsubst posn-window (position)
580 "Return the window in POSITION.
581 POSITION should be a list of the form
582 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
583 as returned by the `event-start' and `event-end' functions."
584 (nth 0 position))
585
586 (defsubst posn-point (position)
587 "Return the buffer location in POSITION.
588 POSITION should be a list of the form
589 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
590 as returned by the `event-start' and `event-end' functions."
591 (if (consp (nth 1 position))
592 (car (nth 1 position))
593 (nth 1 position)))
594
595 (defsubst posn-x-y (position)
596 "Return the x and y coordinates in POSITION.
597 POSITION should be a list of the form
598 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
599 as returned by the `event-start' and `event-end' functions."
600 (nth 2 position))
601
602 (defun posn-col-row (position)
603 "Return the column and row in POSITION, measured in characters.
604 POSITION should be a list of the form
605 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
606 as returned by the `event-start' and `event-end' functions.
607 For a scroll-bar event, the result column is 0, and the row
608 corresponds to the vertical position of the click in the scroll bar."
609 (let ((pair (nth 2 position))
610 (window (posn-window position)))
611 (if (eq (if (consp (nth 1 position))
612 (car (nth 1 position))
613 (nth 1 position))
614 'vertical-scroll-bar)
615 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
616 (if (eq (if (consp (nth 1 position))
617 (car (nth 1 position))
618 (nth 1 position))
619 'horizontal-scroll-bar)
620 (cons (scroll-bar-scale pair (window-width window)) 0)
621 (let* ((frame (if (framep window) window (window-frame window)))
622 (x (/ (car pair) (frame-char-width frame)))
623 (y (/ (cdr pair) (frame-char-height frame))))
624 (cons x y))))))
625
626 (defsubst posn-timestamp (position)
627 "Return the timestamp of POSITION.
628 POSITION should be a list of the form
629 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
630 as returned by the `event-start' and `event-end' functions."
631 (nth 3 position))
632
633 \f
634 ;;;; Obsolescent names for functions.
635
636 (defalias 'dot 'point)
637 (defalias 'dot-marker 'point-marker)
638 (defalias 'dot-min 'point-min)
639 (defalias 'dot-max 'point-max)
640 (defalias 'window-dot 'window-point)
641 (defalias 'set-window-dot 'set-window-point)
642 (defalias 'read-input 'read-string)
643 (defalias 'send-string 'process-send-string)
644 (defalias 'send-region 'process-send-region)
645 (defalias 'show-buffer 'set-window-buffer)
646 (defalias 'buffer-flush-undo 'buffer-disable-undo)
647 (defalias 'eval-current-buffer 'eval-buffer)
648 (defalias 'compiled-function-p 'byte-code-function-p)
649 (defalias 'define-function 'defalias)
650
651 (defalias 'sref 'aref)
652 (make-obsolete 'sref 'aref "20.4")
653 (make-obsolete 'char-bytes "Now this function always returns 1" "20.4")
654
655 ;; Some programs still use this as a function.
656 (defun baud-rate ()
657 "Obsolete function returning the value of the `baud-rate' variable.
658 Please convert your programs to use the variable `baud-rate' directly."
659 baud-rate)
660
661 (defalias 'focus-frame 'ignore)
662 (defalias 'unfocus-frame 'ignore)
663 \f
664 ;;;; Alternate names for functions - these are not being phased out.
665
666 (defalias 'string= 'string-equal)
667 (defalias 'string< 'string-lessp)
668 (defalias 'move-marker 'set-marker)
669 (defalias 'rplaca 'setcar)
670 (defalias 'rplacd 'setcdr)
671 (defalias 'beep 'ding) ;preserve lingual purity
672 (defalias 'indent-to-column 'indent-to)
673 (defalias 'backward-delete-char 'delete-backward-char)
674 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
675 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
676 (defalias 'int-to-string 'number-to-string)
677 (defalias 'store-match-data 'set-match-data)
678 ;; These are the XEmacs names:
679 (defalias 'point-at-eol 'line-end-position)
680 (defalias 'point-at-bol 'line-beginning-position)
681
682 ;;; Should this be an obsolete name? If you decide it should, you get
683 ;;; to go through all the sources and change them.
684 (defalias 'string-to-int 'string-to-number)
685 \f
686 ;;;; Hook manipulation functions.
687
688 (defun make-local-hook (hook)
689 "Make the hook HOOK local to the current buffer.
690 The return value is HOOK.
691
692 You never need to call this function now that `add-hook' does it for you
693 if its LOCAL argument is non-nil.
694
695 When a hook is local, its local and global values
696 work in concert: running the hook actually runs all the hook
697 functions listed in *either* the local value *or* the global value
698 of the hook variable.
699
700 This function works by making t a member of the buffer-local value,
701 which acts as a flag to run the hook functions in the default value as
702 well. This works for all normal hooks, but does not work for most
703 non-normal hooks yet. We will be changing the callers of non-normal
704 hooks so that they can handle localness; this has to be done one by
705 one.
706
707 This function does nothing if HOOK is already local in the current
708 buffer.
709
710 Do not use `make-local-variable' to make a hook variable buffer-local."
711 (if (local-variable-p hook)
712 nil
713 (or (boundp hook) (set hook nil))
714 (make-local-variable hook)
715 (set hook (list t)))
716 hook)
717 (make-obsolete 'make-local-hook "Not necessary any more." "21.1")
718
719 (defun add-hook (hook function &optional append local)
720 "Add to the value of HOOK the function FUNCTION.
721 FUNCTION is not added if already present.
722 FUNCTION is added (if necessary) at the beginning of the hook list
723 unless the optional argument APPEND is non-nil, in which case
724 FUNCTION is added at the end.
725
726 The optional fourth argument, LOCAL, if non-nil, says to modify
727 the hook's buffer-local value rather than its default value.
728 This makes the hook buffer-local if needed.
729
730 HOOK should be a symbol, and FUNCTION may be any valid function. If
731 HOOK is void, it is first set to nil. If HOOK's value is a single
732 function, it is changed to a list of functions."
733 (or (boundp hook) (set hook nil))
734 (or (default-boundp hook) (set-default hook nil))
735 (if local (unless (local-variable-if-set-p hook)
736 (set (make-local-variable hook) (list t)))
737 ;; Detect the case where make-local-variable was used on a hook
738 ;; and do what we used to do.
739 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
740 (setq local t)))
741 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
742 ;; If the hook value is a single function, turn it into a list.
743 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
744 (setq hook-value (list hook-value)))
745 ;; Do the actual addition if necessary
746 (unless (member function hook-value)
747 (setq hook-value
748 (if append
749 (append hook-value (list function))
750 (cons function hook-value))))
751 ;; Set the actual variable
752 (if local (set hook hook-value) (set-default hook hook-value))))
753
754 (defun remove-hook (hook function &optional local)
755 "Remove from the value of HOOK the function FUNCTION.
756 HOOK should be a symbol, and FUNCTION may be any valid function. If
757 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
758 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
759
760 The optional third argument, LOCAL, if non-nil, says to modify
761 the hook's buffer-local value rather than its default value.
762 This makes the hook buffer-local if needed."
763 (or (boundp hook) (set hook nil))
764 (or (default-boundp hook) (set-default hook nil))
765 (if local (unless (local-variable-if-set-p hook)
766 (set (make-local-variable hook) (list t)))
767 ;; Detect the case where make-local-variable was used on a hook
768 ;; and do what we used to do.
769 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
770 (setq local t)))
771 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
772 ;; Remove the function, for both the list and the non-list cases.
773 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
774 (if (equal hook-value function) (setq hook-value nil))
775 (setq hook-value (delete function (copy-sequence hook-value))))
776 ;; If the function is on the global hook, we need to shadow it locally
777 ;;(when (and local (member function (default-value hook))
778 ;; (not (member (cons 'not function) hook-value)))
779 ;; (push (cons 'not function) hook-value))
780 ;; Set the actual variable
781 (if local (set hook hook-value) (set-default hook hook-value))))
782
783 (defun add-to-list (list-var element &optional append)
784 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
785 The test for presence of ELEMENT is done with `equal'.
786 If ELEMENT is added, it is added at the beginning of the list,
787 unless the optional argument APPEND is non-nil, in which case
788 ELEMENT is added at the end.
789
790 If you want to use `add-to-list' on a variable that is not defined
791 until a certain package is loaded, you should put the call to `add-to-list'
792 into a hook function that will be run only after loading the package.
793 `eval-after-load' provides one way to do this. In some cases
794 other hooks, such as major mode hooks, can do the job."
795 (if (member element (symbol-value list-var))
796 (symbol-value list-var)
797 (set list-var
798 (if append
799 (append (symbol-value list-var) (list element))
800 (cons element (symbol-value list-var))))))
801
802 \f
803 ;;; Load history
804
805 (defvar symbol-file-load-history-loaded nil
806 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
807 That file records the part of `load-history' for preloaded files,
808 which is cleared out before dumping to make Emacs smaller.")
809
810 (defun load-symbol-file-load-history ()
811 "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
812 That file records the part of `load-history' for preloaded files,
813 which is cleared out before dumping to make Emacs smaller."
814 (unless symbol-file-load-history-loaded
815 (load (expand-file-name
816 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
817 (if (eq system-type 'ms-dos)
818 "fns.el"
819 (format "fns-%s.el" emacs-version))
820 exec-directory)
821 ;; The file name fns-%s.el already has a .el extension.
822 nil nil t)
823 (setq symbol-file-load-history-loaded t)))
824
825 (defun symbol-file (function)
826 "Return the input source from which FUNCTION was loaded.
827 The value is normally a string that was passed to `load':
828 either an absolute file name, or a library name
829 \(with no directory name and no `.el' or `.elc' at the end).
830 It can also be nil, if the definition is not associated with any file."
831 (load-symbol-file-load-history)
832 (let ((files load-history)
833 file functions)
834 (while files
835 (if (memq function (cdr (car files)))
836 (setq file (car (car files)) files nil))
837 (setq files (cdr files)))
838 file))
839
840 \f
841 ;;;; Specifying things to do after certain files are loaded.
842
843 (defun eval-after-load (file form)
844 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
845 This makes or adds to an entry on `after-load-alist'.
846 If FILE is already loaded, evaluate FORM right now.
847 It does nothing if FORM is already on the list for FILE.
848 FILE must match exactly. Normally FILE is the name of a library,
849 with no directory or extension specified, since that is how `load'
850 is normally called."
851 ;; Make sure `load-history' contains the files dumped with Emacs
852 ;; for the case that FILE is one of the files dumped with Emacs.
853 (load-symbol-file-load-history)
854 ;; Make sure there is an element for FILE.
855 (or (assoc file after-load-alist)
856 (setq after-load-alist (cons (list file) after-load-alist)))
857 ;; Add FORM to the element if it isn't there.
858 (let ((elt (assoc file after-load-alist)))
859 (or (member form (cdr elt))
860 (progn
861 (nconc elt (list form))
862 ;; If the file has been loaded already, run FORM right away.
863 (and (assoc file load-history)
864 (eval form)))))
865 form)
866
867 (defun eval-next-after-load (file)
868 "Read the following input sexp, and run it whenever FILE is loaded.
869 This makes or adds to an entry on `after-load-alist'.
870 FILE should be the name of a library, with no directory name."
871 (eval-after-load file (read)))
872
873 \f
874 ;;;; Input and display facilities.
875
876 (defvar read-quoted-char-radix 8
877 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
878 Legitimate radix values are 8, 10 and 16.")
879
880 (custom-declare-variable-early
881 'read-quoted-char-radix 8
882 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
883 Legitimate radix values are 8, 10 and 16."
884 :type '(choice (const 8) (const 10) (const 16))
885 :group 'editing-basics)
886
887 (defun read-quoted-char (&optional prompt)
888 "Like `read-char', but do not allow quitting.
889 Also, if the first character read is an octal digit,
890 we read any number of octal digits and return the
891 specified character code. Any nondigit terminates the sequence.
892 If the terminator is RET, it is discarded;
893 any other terminator is used itself as input.
894
895 The optional argument PROMPT specifies a string to use to prompt the user.
896 The variable `read-quoted-char-radix' controls which radix to use
897 for numeric input."
898 (let ((message-log-max nil) done (first t) (code 0) char)
899 (while (not done)
900 (let ((inhibit-quit first)
901 ;; Don't let C-h get the help message--only help function keys.
902 (help-char nil)
903 (help-form
904 "Type the special character you want to use,
905 or the octal character code.
906 RET terminates the character code and is discarded;
907 any other non-digit terminates the character code and is then used as input."))
908 (setq char (read-event (and prompt (format "%s-" prompt)) t))
909 (if inhibit-quit (setq quit-flag nil)))
910 ;; Translate TAB key into control-I ASCII character, and so on.
911 (and char
912 (let ((translated (lookup-key function-key-map (vector char))))
913 (if (arrayp translated)
914 (setq char (aref translated 0)))))
915 (cond ((null char))
916 ((not (integerp char))
917 (setq unread-command-events (list char)
918 done t))
919 ((/= (logand char ?\M-\^@) 0)
920 ;; Turn a meta-character into a character with the 0200 bit set.
921 (setq code (logior (logand char (lognot ?\M-\^@)) 128)
922 done t))
923 ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix))))
924 (setq code (+ (* code read-quoted-char-radix) (- char ?0)))
925 (and prompt (setq prompt (message "%s %c" prompt char))))
926 ((and (<= ?a (downcase char))
927 (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix))))
928 (setq code (+ (* code read-quoted-char-radix)
929 (+ 10 (- (downcase char) ?a))))
930 (and prompt (setq prompt (message "%s %c" prompt char))))
931 ((and (not first) (eq char ?\C-m))
932 (setq done t))
933 ((not first)
934 (setq unread-command-events (list char)
935 done t))
936 (t (setq code char
937 done t)))
938 (setq first nil))
939 code))
940
941 (defun read-passwd (prompt &optional confirm default)
942 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
943 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
944 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
945 Optional DEFAULT is a default password to use instead of empty input."
946 (if confirm
947 (let (success)
948 (while (not success)
949 (let ((first (read-passwd prompt nil default))
950 (second (read-passwd "Confirm password: " nil default)))
951 (if (equal first second)
952 (progn
953 (and (arrayp second) (fillarray second ?\0))
954 (setq success first))
955 (and (arrayp first) (fillarray first ?\0))
956 (and (arrayp second) (fillarray second ?\0))
957 (message "Password not repeated accurately; please start over")
958 (sit-for 1))))
959 success)
960 (let ((pass nil)
961 (c 0)
962 (echo-keystrokes 0)
963 (cursor-in-echo-area t))
964 (while (progn (message "%s%s"
965 prompt
966 (make-string (length pass) ?.))
967 (setq c (read-char-exclusive nil t))
968 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
969 (clear-this-command-keys)
970 (if (= c ?\C-u)
971 (progn
972 (and (arrayp pass) (fillarray pass ?\0))
973 (setq pass ""))
974 (if (and (/= c ?\b) (/= c ?\177))
975 (let* ((new-char (char-to-string c))
976 (new-pass (concat pass new-char)))
977 (and (arrayp pass) (fillarray pass ?\0))
978 (fillarray new-char ?\0)
979 (setq c ?\0)
980 (setq pass new-pass))
981 (if (> (length pass) 0)
982 (let ((new-pass (substring pass 0 -1)))
983 (and (arrayp pass) (fillarray pass ?\0))
984 (setq pass new-pass))))))
985 (message nil)
986 (or pass default ""))))
987 \f
988 (defun force-mode-line-update (&optional all)
989 "Force the mode-line of the current buffer to be redisplayed.
990 With optional non-nil ALL, force redisplay of all mode-lines."
991 (if all (save-excursion (set-buffer (other-buffer))))
992 (set-buffer-modified-p (buffer-modified-p)))
993
994 (defun momentary-string-display (string pos &optional exit-char message)
995 "Momentarily display STRING in the buffer at POS.
996 Display remains until next character is typed.
997 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
998 otherwise it is then available as input (as a command if nothing else).
999 Display MESSAGE (optional fourth arg) in the echo area.
1000 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
1001 (or exit-char (setq exit-char ?\ ))
1002 (let ((inhibit-read-only t)
1003 ;; Don't modify the undo list at all.
1004 (buffer-undo-list t)
1005 (modified (buffer-modified-p))
1006 (name buffer-file-name)
1007 insert-end)
1008 (unwind-protect
1009 (progn
1010 (save-excursion
1011 (goto-char pos)
1012 ;; defeat file locking... don't try this at home, kids!
1013 (setq buffer-file-name nil)
1014 (insert-before-markers string)
1015 (setq insert-end (point))
1016 ;; If the message end is off screen, recenter now.
1017 (if (< (window-end nil t) insert-end)
1018 (recenter (/ (window-height) 2)))
1019 ;; If that pushed message start off the screen,
1020 ;; scroll to start it at the top of the screen.
1021 (move-to-window-line 0)
1022 (if (> (point) pos)
1023 (progn
1024 (goto-char pos)
1025 (recenter 0))))
1026 (message (or message "Type %s to continue editing.")
1027 (single-key-description exit-char))
1028 (let ((char (read-event)))
1029 (or (eq char exit-char)
1030 (setq unread-command-events (list char)))))
1031 (if insert-end
1032 (save-excursion
1033 (delete-region pos insert-end)))
1034 (setq buffer-file-name name)
1035 (set-buffer-modified-p modified))))
1036
1037 \f
1038 ;;;; Miscellanea.
1039
1040 ;; A number of major modes set this locally.
1041 ;; Give it a global value to avoid compiler warnings.
1042 (defvar font-lock-defaults nil)
1043
1044 (defvar suspend-hook nil
1045 "Normal hook run by `suspend-emacs', before suspending.")
1046
1047 (defvar suspend-resume-hook nil
1048 "Normal hook run by `suspend-emacs', after Emacs is continued.")
1049
1050 ;; Avoid compiler warnings about this variable,
1051 ;; which has a special meaning on certain system types.
1052 (defvar buffer-file-type nil
1053 "Non-nil if the visited file is a binary file.
1054 This variable is meaningful on MS-DOG and Windows NT.
1055 On those systems, it is automatically local in every buffer.
1056 On other systems, this variable is normally always nil.")
1057
1058 ;; This should probably be written in C (i.e., without using `walk-windows').
1059 (defun get-buffer-window-list (buffer &optional minibuf frame)
1060 "Return windows currently displaying BUFFER, or nil if none.
1061 See `walk-windows' for the meaning of MINIBUF and FRAME."
1062 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
1063 (walk-windows (function (lambda (window)
1064 (if (eq (window-buffer window) buffer)
1065 (setq windows (cons window windows)))))
1066 minibuf frame)
1067 windows))
1068
1069 (defun ignore (&rest ignore)
1070 "Do nothing and return nil.
1071 This function accepts any number of arguments, but ignores them."
1072 (interactive)
1073 nil)
1074
1075 (defun error (&rest args)
1076 "Signal an error, making error message by passing all args to `format'.
1077 In Emacs, the convention is that error messages start with a capital
1078 letter but *do not* end with a period. Please follow this convention
1079 for the sake of consistency."
1080 (while t
1081 (signal 'error (list (apply 'format args)))))
1082
1083 (defalias 'user-original-login-name 'user-login-name)
1084
1085 (defun start-process-shell-command (name buffer &rest args)
1086 "Start a program in a subprocess. Return the process object for it.
1087 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1088 NAME is name for process. It is modified if necessary to make it unique.
1089 BUFFER is the buffer or (buffer-name) to associate with the process.
1090 Process output goes at end of that buffer, unless you specify
1091 an output stream or filter function to handle the output.
1092 BUFFER may be also nil, meaning that this process is not associated
1093 with any buffer
1094 Third arg is command name, the name of a shell command.
1095 Remaining arguments are the arguments for the command.
1096 Wildcards and redirection are handled as usual in the shell."
1097 (cond
1098 ((eq system-type 'vax-vms)
1099 (apply 'start-process name buffer args))
1100 ;; We used to use `exec' to replace the shell with the command,
1101 ;; but that failed to handle (...) and semicolon, etc.
1102 (t
1103 (start-process name buffer shell-file-name shell-command-switch
1104 (mapconcat 'identity args " ")))))
1105
1106 (defun call-process-shell-command (command &optional infile buffer display
1107 &rest args)
1108 "Execute the shell command COMMAND synchronously in separate process.
1109 The remaining arguments are optional.
1110 The program's input comes from file INFILE (nil means `/dev/null').
1111 Insert output in BUFFER before point; t means current buffer;
1112 nil for BUFFER means discard it; 0 means discard and don't wait.
1113 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1114 REAL-BUFFER says what to do with standard output, as above,
1115 while STDERR-FILE says what to do with standard error in the child.
1116 STDERR-FILE may be nil (discard standard error output),
1117 t (mix it with ordinary output), or a file name string.
1118
1119 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1120 Remaining arguments are strings passed as additional arguments for COMMAND.
1121 Wildcards and redirection are handled as usual in the shell.
1122
1123 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
1124 Otherwise it waits for COMMAND to terminate and returns a numeric exit
1125 status or a signal description string.
1126 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
1127 (cond
1128 ((eq system-type 'vax-vms)
1129 (apply 'call-process command infile buffer display args))
1130 ;; We used to use `exec' to replace the shell with the command,
1131 ;; but that failed to handle (...) and semicolon, etc.
1132 (t
1133 (call-process shell-file-name
1134 infile buffer display
1135 shell-command-switch
1136 (mapconcat 'identity (cons command args) " ")))))
1137 \f
1138 (defmacro with-current-buffer (buffer &rest body)
1139 "Execute the forms in BODY with BUFFER as the current buffer.
1140 The value returned is the value of the last form in BODY.
1141 See also `with-temp-buffer'."
1142 (cons 'save-current-buffer
1143 (cons (list 'set-buffer buffer)
1144 body)))
1145
1146 (defmacro with-temp-file (file &rest body)
1147 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1148 The value returned is the value of the last form in BODY.
1149 See also `with-temp-buffer'."
1150 (let ((temp-file (make-symbol "temp-file"))
1151 (temp-buffer (make-symbol "temp-buffer")))
1152 `(let ((,temp-file ,file)
1153 (,temp-buffer
1154 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1155 (unwind-protect
1156 (prog1
1157 (with-current-buffer ,temp-buffer
1158 ,@body)
1159 (with-current-buffer ,temp-buffer
1160 (widen)
1161 (write-region (point-min) (point-max) ,temp-file nil 0)))
1162 (and (buffer-name ,temp-buffer)
1163 (kill-buffer ,temp-buffer))))))
1164
1165 (defmacro with-temp-message (message &rest body)
1166 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1167 The original message is restored to the echo area after BODY has finished.
1168 The value returned is the value of the last form in BODY.
1169 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1170 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1171 Use a MESSAGE of \"\" to temporarily clear the echo area."
1172 (let ((current-message (make-symbol "current-message"))
1173 (temp-message (make-symbol "with-temp-message")))
1174 `(let ((,temp-message ,message)
1175 (,current-message))
1176 (unwind-protect
1177 (progn
1178 (when ,temp-message
1179 (setq ,current-message (current-message))
1180 (message "%s" ,temp-message))
1181 ,@body)
1182 (and ,temp-message ,current-message
1183 (message "%s" ,current-message))))))
1184
1185 (defmacro with-temp-buffer (&rest body)
1186 "Create a temporary buffer, and evaluate BODY there like `progn'.
1187 See also `with-temp-file' and `with-output-to-string'."
1188 (let ((temp-buffer (make-symbol "temp-buffer")))
1189 `(let ((,temp-buffer
1190 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1191 (unwind-protect
1192 (with-current-buffer ,temp-buffer
1193 ,@body)
1194 (and (buffer-name ,temp-buffer)
1195 (kill-buffer ,temp-buffer))))))
1196
1197 (defmacro with-output-to-string (&rest body)
1198 "Execute BODY, return the text it sent to `standard-output', as a string."
1199 `(let ((standard-output
1200 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1201 (let ((standard-output standard-output))
1202 ,@body)
1203 (with-current-buffer standard-output
1204 (prog1
1205 (buffer-string)
1206 (kill-buffer nil)))))
1207
1208 (defmacro with-local-quit (&rest body)
1209 "Execute BODY with `inhibit-quit' temporarily bound to nil."
1210 `(condition-case nil
1211 (let ((inhibit-quit nil))
1212 ,@body)
1213 (quit (setq quit-flag t))))
1214
1215 (defmacro combine-after-change-calls (&rest body)
1216 "Execute BODY, but don't call the after-change functions till the end.
1217 If BODY makes changes in the buffer, they are recorded
1218 and the functions on `after-change-functions' are called several times
1219 when BODY is finished.
1220 The return value is the value of the last form in BODY.
1221
1222 If `before-change-functions' is non-nil, then calls to the after-change
1223 functions can't be deferred, so in that case this macro has no effect.
1224
1225 Do not alter `after-change-functions' or `before-change-functions'
1226 in BODY."
1227 `(unwind-protect
1228 (let ((combine-after-change-calls t))
1229 . ,body)
1230 (combine-after-change-execute)))
1231
1232
1233 (defvar delay-mode-hooks nil
1234 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1235 (defvar delayed-mode-hooks nil
1236 "List of delayed mode hooks waiting to be run.")
1237 (make-variable-buffer-local 'delayed-mode-hooks)
1238
1239 (defun run-mode-hooks (&rest hooks)
1240 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1241 Execution is delayed if `delay-mode-hooks' is non-nil.
1242 Major mode functions should use this."
1243 (if delay-mode-hooks
1244 ;; Delaying case.
1245 (dolist (hook hooks)
1246 (push hook delayed-mode-hooks))
1247 ;; Normal case, just run the hook as before plus any delayed hooks.
1248 (setq hooks (nconc (nreverse delayed-mode-hooks) hooks))
1249 (setq delayed-mode-hooks nil)
1250 (apply 'run-hooks hooks)))
1251
1252 (defmacro delay-mode-hooks (&rest body)
1253 "Execute BODY, but delay any `run-mode-hooks'.
1254 Only affects hooks run in the current buffer."
1255 `(progn
1256 (make-local-variable 'delay-mode-hooks)
1257 (let ((delay-mode-hooks t))
1258 ,@body)))
1259
1260 (defmacro with-syntax-table (table &rest body)
1261 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1262 The syntax table of the current buffer is saved, BODY is evaluated, and the
1263 saved table is restored, even in case of an abnormal exit.
1264 Value is what BODY returns."
1265 (let ((old-table (make-symbol "table"))
1266 (old-buffer (make-symbol "buffer")))
1267 `(let ((,old-table (syntax-table))
1268 (,old-buffer (current-buffer)))
1269 (unwind-protect
1270 (progn
1271 (set-syntax-table (copy-syntax-table ,table))
1272 ,@body)
1273 (save-current-buffer
1274 (set-buffer ,old-buffer)
1275 (set-syntax-table ,old-table))))))
1276 \f
1277 (defvar save-match-data-internal)
1278
1279 ;; We use save-match-data-internal as the local variable because
1280 ;; that works ok in practice (people should not use that variable elsewhere).
1281 ;; We used to use an uninterned symbol; the compiler handles that properly
1282 ;; now, but it generates slower code.
1283 (defmacro save-match-data (&rest body)
1284 "Execute the BODY forms, restoring the global value of the match data."
1285 ;; It is better not to use backquote here,
1286 ;; because that makes a bootstrapping problem
1287 ;; if you need to recompile all the Lisp files using interpreted code.
1288 (list 'let
1289 '((save-match-data-internal (match-data)))
1290 (list 'unwind-protect
1291 (cons 'progn body)
1292 '(set-match-data save-match-data-internal))))
1293
1294 (defun match-string (num &optional string)
1295 "Return string of text matched by last search.
1296 NUM specifies which parenthesized expression in the last regexp.
1297 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1298 Zero means the entire text matched by the whole regexp or whole string.
1299 STRING should be given if the last search was by `string-match' on STRING."
1300 (if (match-beginning num)
1301 (if string
1302 (substring string (match-beginning num) (match-end num))
1303 (buffer-substring (match-beginning num) (match-end num)))))
1304
1305 (defun match-string-no-properties (num &optional string)
1306 "Return string of text matched by last search, without text properties.
1307 NUM specifies which parenthesized expression in the last regexp.
1308 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1309 Zero means the entire text matched by the whole regexp or whole string.
1310 STRING should be given if the last search was by `string-match' on STRING."
1311 (if (match-beginning num)
1312 (if string
1313 (let ((result
1314 (substring string (match-beginning num) (match-end num))))
1315 (set-text-properties 0 (length result) nil result)
1316 result)
1317 (buffer-substring-no-properties (match-beginning num)
1318 (match-end num)))))
1319
1320 (defun split-string (string &optional separators)
1321 "Splits STRING into substrings where there are matches for SEPARATORS.
1322 Each match for SEPARATORS is a splitting point.
1323 The substrings between the splitting points are made into a list
1324 which is returned.
1325 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1326
1327 If there is match for SEPARATORS at the beginning of STRING, we do not
1328 include a null substring for that. Likewise, if there is a match
1329 at the end of STRING, we don't include a null substring for that.
1330
1331 Modifies the match data; use `save-match-data' if necessary."
1332 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1333 (start 0)
1334 notfirst
1335 (list nil))
1336 (while (and (string-match rexp string
1337 (if (and notfirst
1338 (= start (match-beginning 0))
1339 (< start (length string)))
1340 (1+ start) start))
1341 (< (match-beginning 0) (length string)))
1342 (setq notfirst t)
1343 (or (eq (match-beginning 0) 0)
1344 (and (eq (match-beginning 0) (match-end 0))
1345 (eq (match-beginning 0) start))
1346 (setq list
1347 (cons (substring string start (match-beginning 0))
1348 list)))
1349 (setq start (match-end 0)))
1350 (or (eq start (length string))
1351 (setq list
1352 (cons (substring string start)
1353 list)))
1354 (nreverse list)))
1355
1356 (defun subst-char-in-string (fromchar tochar string &optional inplace)
1357 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1358 Unless optional argument INPLACE is non-nil, return a new string."
1359 (let ((i (length string))
1360 (newstr (if inplace string (copy-sequence string))))
1361 (while (> i 0)
1362 (setq i (1- i))
1363 (if (eq (aref newstr i) fromchar)
1364 (aset newstr i tochar)))
1365 newstr))
1366
1367 (defun replace-regexp-in-string (regexp rep string &optional
1368 fixedcase literal subexp start)
1369 "Replace all matches for REGEXP with REP in STRING.
1370
1371 Return a new string containing the replacements.
1372
1373 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1374 arguments with the same names of function `replace-match'. If START
1375 is non-nil, start replacements at that index in STRING.
1376
1377 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1378 function. If it is a function it is applied to each match to generate
1379 the replacement passed to `replace-match'; the match-data at this
1380 point are such that match 0 is the function's argument.
1381
1382 To replace only the first match (if any), make REGEXP match up to \\'
1383 and replace a sub-expression, e.g.
1384 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
1385 => \" bar foo\"
1386 "
1387
1388 ;; To avoid excessive consing from multiple matches in long strings,
1389 ;; don't just call `replace-match' continually. Walk down the
1390 ;; string looking for matches of REGEXP and building up a (reversed)
1391 ;; list MATCHES. This comprises segments of STRING which weren't
1392 ;; matched interspersed with replacements for segments that were.
1393 ;; [For a `large' number of replacements it's more efficient to
1394 ;; operate in a temporary buffer; we can't tell from the function's
1395 ;; args whether to choose the buffer-based implementation, though it
1396 ;; might be reasonable to do so for long enough STRING.]
1397 (let ((l (length string))
1398 (start (or start 0))
1399 matches str mb me)
1400 (save-match-data
1401 (while (and (< start l) (string-match regexp string start))
1402 (setq mb (match-beginning 0)
1403 me (match-end 0))
1404 ;; If we matched the empty string, make sure we advance by one char
1405 (when (= me mb) (setq me (min l (1+ mb))))
1406 ;; Generate a replacement for the matched substring.
1407 ;; Operate only on the substring to minimize string consing.
1408 ;; Set up match data for the substring for replacement;
1409 ;; presumably this is likely to be faster than munging the
1410 ;; match data directly in Lisp.
1411 (string-match regexp (setq str (substring string mb me)))
1412 (setq matches
1413 (cons (replace-match (if (stringp rep)
1414 rep
1415 (funcall rep (match-string 0 str)))
1416 fixedcase literal str subexp)
1417 (cons (substring string start mb) ; unmatched prefix
1418 matches)))
1419 (setq start me))
1420 ;; Reconstruct a string from the pieces.
1421 (setq matches (cons (substring string start l) matches)) ; leftover
1422 (apply #'concat (nreverse matches)))))
1423 \f
1424 (defun shell-quote-argument (argument)
1425 "Quote an argument for passing as argument to an inferior shell."
1426 (if (eq system-type 'ms-dos)
1427 ;; Quote using double quotes, but escape any existing quotes in
1428 ;; the argument with backslashes.
1429 (let ((result "")
1430 (start 0)
1431 end)
1432 (if (or (null (string-match "[^\"]" argument))
1433 (< (match-end 0) (length argument)))
1434 (while (string-match "[\"]" argument start)
1435 (setq end (match-beginning 0)
1436 result (concat result (substring argument start end)
1437 "\\" (substring argument end (1+ end)))
1438 start (1+ end))))
1439 (concat "\"" result (substring argument start) "\""))
1440 (if (eq system-type 'windows-nt)
1441 (concat "\"" argument "\"")
1442 (if (equal argument "")
1443 "''"
1444 ;; Quote everything except POSIX filename characters.
1445 ;; This should be safe enough even for really weird shells.
1446 (let ((result "") (start 0) end)
1447 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1448 (setq end (match-beginning 0)
1449 result (concat result (substring argument start end)
1450 "\\" (substring argument end (1+ end)))
1451 start (1+ end)))
1452 (concat result (substring argument start)))))))
1453
1454 (defun make-syntax-table (&optional oldtable)
1455 "Return a new syntax table.
1456 Create a syntax table which inherits from OLDTABLE (if non-nil) or
1457 from `standard-syntax-table' otherwise."
1458 (let ((table (make-char-table 'syntax-table nil)))
1459 (set-char-table-parent table (or oldtable (standard-syntax-table)))
1460 table))
1461
1462 (defun add-to-invisibility-spec (arg)
1463 "Add elements to `buffer-invisibility-spec'.
1464 See documentation for `buffer-invisibility-spec' for the kind of elements
1465 that can be added."
1466 (cond
1467 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
1468 (setq buffer-invisibility-spec (list arg)))
1469 (t
1470 (setq buffer-invisibility-spec
1471 (cons arg buffer-invisibility-spec)))))
1472
1473 (defun remove-from-invisibility-spec (arg)
1474 "Remove elements from `buffer-invisibility-spec'."
1475 (if (consp buffer-invisibility-spec)
1476 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
1477 \f
1478 (defun global-set-key (key command)
1479 "Give KEY a global binding as COMMAND.
1480 COMMAND is the command definition to use; usually it is
1481 a symbol naming an interactively-callable function.
1482 KEY is a key sequence; noninteractively, it is a string or vector
1483 of characters or event types, and non-ASCII characters with codes
1484 above 127 (such as ISO Latin-1) can be included if you use a vector.
1485
1486 Note that if KEY has a local binding in the current buffer,
1487 that local binding will continue to shadow any global binding
1488 that you make with this function."
1489 (interactive "KSet key globally: \nCSet key %s to command: ")
1490 (or (vectorp key) (stringp key)
1491 (signal 'wrong-type-argument (list 'arrayp key)))
1492 (define-key (current-global-map) key command))
1493
1494 (defun local-set-key (key command)
1495 "Give KEY a local binding as COMMAND.
1496 COMMAND is the command definition to use; usually it is
1497 a symbol naming an interactively-callable function.
1498 KEY is a key sequence; noninteractively, it is a string or vector
1499 of characters or event types, and non-ASCII characters with codes
1500 above 127 (such as ISO Latin-1) can be included if you use a vector.
1501
1502 The binding goes in the current buffer's local map,
1503 which in most cases is shared with all other buffers in the same major mode."
1504 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1505 (let ((map (current-local-map)))
1506 (or map
1507 (use-local-map (setq map (make-sparse-keymap))))
1508 (or (vectorp key) (stringp key)
1509 (signal 'wrong-type-argument (list 'arrayp key)))
1510 (define-key map key command)))
1511
1512 (defun global-unset-key (key)
1513 "Remove global binding of KEY.
1514 KEY is a string representing a sequence of keystrokes."
1515 (interactive "kUnset key globally: ")
1516 (global-set-key key nil))
1517
1518 (defun local-unset-key (key)
1519 "Remove local binding of KEY.
1520 KEY is a string representing a sequence of keystrokes."
1521 (interactive "kUnset key locally: ")
1522 (if (current-local-map)
1523 (local-set-key key nil))
1524 nil)
1525 \f
1526 ;; We put this here instead of in frame.el so that it's defined even on
1527 ;; systems where frame.el isn't loaded.
1528 (defun frame-configuration-p (object)
1529 "Return non-nil if OBJECT seems to be a frame configuration.
1530 Any list whose car is `frame-configuration' is assumed to be a frame
1531 configuration."
1532 (and (consp object)
1533 (eq (car object) 'frame-configuration)))
1534
1535 (defun functionp (object)
1536 "Non-nil iff OBJECT is a type of object that can be called as a function."
1537 (or (and (symbolp object)
1538 (fboundp object)
1539 (setq object (indirect-function object))
1540 (eq (car-safe object) 'autoload)
1541 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object)))))))
1542 (subrp object) (byte-code-function-p object)
1543 (eq (car-safe object) 'lambda)))
1544
1545 (defun interactive-form (function)
1546 "Return the interactive form of FUNCTION.
1547 If function is a command (see `commandp'), value is a list of the form
1548 \(interactive SPEC). If function is not a command, return nil."
1549 (setq function (indirect-function function))
1550 (when (commandp function)
1551 (cond ((byte-code-function-p function)
1552 (when (> (length function) 5)
1553 (let ((spec (aref function 5)))
1554 (if spec
1555 (list 'interactive spec)
1556 (list 'interactive)))))
1557 ((subrp function)
1558 (subr-interactive-form function))
1559 ((eq (car-safe function) 'lambda)
1560 (setq function (cddr function))
1561 (when (stringp (car function))
1562 (setq function (cdr function)))
1563 (let ((form (car function)))
1564 (when (eq (car-safe form) 'interactive)
1565 (copy-sequence form)))))))
1566
1567 (defun assq-delete-all (key alist)
1568 "Delete from ALIST all elements whose car is KEY.
1569 Return the modified alist."
1570 (let ((tail alist))
1571 (while tail
1572 (if (eq (car (car tail)) key)
1573 (setq alist (delq (car tail) alist)))
1574 (setq tail (cdr tail)))
1575 alist))
1576
1577 (defun make-temp-file (prefix &optional dir-flag)
1578 "Create a temporary file.
1579 The returned file name (created by appending some random characters at the end
1580 of PREFIX, and expanding against `temporary-file-directory' if necessary,
1581 is guaranteed to point to a newly created empty file.
1582 You can then use `write-region' to write new data into the file.
1583
1584 If DIR-FLAG is non-nil, create a new empty directory instead of a file."
1585 (let (file)
1586 (while (condition-case ()
1587 (progn
1588 (setq file
1589 (make-temp-name
1590 (expand-file-name prefix temporary-file-directory)))
1591 (if dir-flag
1592 (make-directory file)
1593 (write-region "" nil file nil 'silent nil 'excl))
1594 nil)
1595 (file-already-exists t))
1596 ;; the file was somehow created by someone else between
1597 ;; `make-temp-name' and `write-region', let's try again.
1598 nil)
1599 file))
1600
1601 \f
1602 (defun add-minor-mode (toggle name &optional keymap after toggle-fun)
1603 "Register a new minor mode.
1604
1605 This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
1606
1607 TOGGLE is a symbol which is the name of a buffer-local variable that
1608 is toggled on or off to say whether the minor mode is active or not.
1609
1610 NAME specifies what will appear in the mode line when the minor mode
1611 is active. NAME should be either a string starting with a space, or a
1612 symbol whose value is such a string.
1613
1614 Optional KEYMAP is the keymap for the minor mode that will be added
1615 to `minor-mode-map-alist'.
1616
1617 Optional AFTER specifies that TOGGLE should be added after AFTER
1618 in `minor-mode-alist'.
1619
1620 Optional TOGGLE-FUN is an interactive function to toggle the mode.
1621 It defaults to (and should by convention be) TOGGLE.
1622
1623 If TOGGLE has a non-nil `:included' property, an entry for the mode is
1624 included in the mode-line minor mode menu.
1625 If TOGGLE has a `:menu-tag', that is used for the menu item's label."
1626 (unless toggle-fun (setq toggle-fun toggle))
1627 ;; Add the toggle to the minor-modes menu if requested.
1628 (when (get toggle :included)
1629 (define-key mode-line-mode-menu
1630 (vector toggle)
1631 (list 'menu-item
1632 (or (get toggle :menu-tag)
1633 (if (stringp name) name (symbol-name toggle)))
1634 toggle-fun
1635 :button (cons :toggle toggle))))
1636 ;; Add the name to the minor-mode-alist.
1637 (when name
1638 (let ((existing (assq toggle minor-mode-alist)))
1639 (when (and (stringp name) (not (get-text-property 0 'local-map name)))
1640 (setq name
1641 (propertize name
1642 'local-map mode-line-minor-mode-keymap
1643 'help-echo "mouse-3: minor mode menu")))
1644 (if existing
1645 (setcdr existing (list name))
1646 (let ((tail minor-mode-alist) found)
1647 (while (and tail (not found))
1648 (if (eq after (caar tail))
1649 (setq found tail)
1650 (setq tail (cdr tail))))
1651 (if found
1652 (let ((rest (cdr found)))
1653 (setcdr found nil)
1654 (nconc found (list (list toggle name)) rest))
1655 (setq minor-mode-alist (cons (list toggle name)
1656 minor-mode-alist)))))))
1657 ;; Add the map to the minor-mode-map-alist.
1658 (when keymap
1659 (let ((existing (assq toggle minor-mode-map-alist)))
1660 (if existing
1661 (setcdr existing keymap)
1662 (let ((tail minor-mode-map-alist) found)
1663 (while (and tail (not found))
1664 (if (eq after (caar tail))
1665 (setq found tail)
1666 (setq tail (cdr tail))))
1667 (if found
1668 (let ((rest (cdr found)))
1669 (setcdr found nil)
1670 (nconc found (list (cons toggle keymap)) rest))
1671 (setq minor-mode-map-alist (cons (cons toggle keymap)
1672 minor-mode-map-alist))))))))
1673
1674 ;; XEmacs compatibility/convenience.
1675 (if (fboundp 'play-sound)
1676 (defun play-sound-file (file &optional volume device)
1677 "Play sound stored in FILE.
1678 VOLUME and DEVICE correspond to the keywords of the sound
1679 specification for `play-sound'."
1680 (interactive "fPlay sound file: ")
1681 (let ((sound (list :file file)))
1682 (if volume
1683 (plist-put sound :volume volume))
1684 (if device
1685 (plist-put sound :device device))
1686 (push 'sound sound)
1687 (play-sound sound))))
1688
1689 ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1690
1691 (defun text-clone-maintain (ol1 after beg end &optional len)
1692 "Propagate the changes made under the overlay OL1 to the other clones.
1693 This is used on the `modification-hooks' property of text clones."
1694 (when (and after (not undo-in-progress) (overlay-start ol1))
1695 (let ((margin (if (overlay-get ol1 'text-clone-spreadp) 1 0)))
1696 (setq beg (max beg (+ (overlay-start ol1) margin)))
1697 (setq end (min end (- (overlay-end ol1) margin)))
1698 (when (<= beg end)
1699 (save-excursion
1700 (when (overlay-get ol1 'text-clone-syntax)
1701 ;; Check content of the clone's text.
1702 (let ((cbeg (+ (overlay-start ol1) margin))
1703 (cend (- (overlay-end ol1) margin)))
1704 (goto-char cbeg)
1705 (save-match-data
1706 (if (not (re-search-forward
1707 (overlay-get ol1 'text-clone-syntax) cend t))
1708 ;; Mark the overlay for deletion.
1709 (overlay-put ol1 'text-clones nil)
1710 (when (< (match-end 0) cend)
1711 ;; Shrink the clone at its end.
1712 (setq end (min end (match-end 0)))
1713 (move-overlay ol1 (overlay-start ol1)
1714 (+ (match-end 0) margin)))
1715 (when (> (match-beginning 0) cbeg)
1716 ;; Shrink the clone at its beginning.
1717 (setq beg (max (match-beginning 0) beg))
1718 (move-overlay ol1 (- (match-beginning 0) margin)
1719 (overlay-end ol1)))))))
1720 ;; Now go ahead and update the clones.
1721 (let ((head (- beg (overlay-start ol1)))
1722 (tail (- (overlay-end ol1) end))
1723 (str (buffer-substring beg end))
1724 (nothing-left t)
1725 (inhibit-modification-hooks t))
1726 (dolist (ol2 (overlay-get ol1 'text-clones))
1727 (let ((oe (overlay-end ol2)))
1728 (unless (or (eq ol1 ol2) (null oe))
1729 (setq nothing-left nil)
1730 (let ((mod-beg (+ (overlay-start ol2) head)))
1731 ;;(overlay-put ol2 'modification-hooks nil)
1732 (goto-char (- (overlay-end ol2) tail))
1733 (unless (> mod-beg (point))
1734 (save-excursion (insert str))
1735 (delete-region mod-beg (point)))
1736 ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain))
1737 ))))
1738 (if nothing-left (delete-overlay ol1))))))))
1739
1740 (defun text-clone-create (start end &optional spreadp syntax)
1741 "Create a text clone of START...END at point.
1742 Text clones are chunks of text that are automatically kept identical:
1743 changes done to one of the clones will be immediately propagated to the other.
1744
1745 The buffer's content at point is assumed to be already identical to
1746 the one between START and END.
1747 If SYNTAX is provided it's a regexp that describes the possible text of
1748 the clones; the clone will be shrunk or killed if necessary to ensure that
1749 its text matches the regexp.
1750 If SPREADP is non-nil it indicates that text inserted before/after the
1751 clone should be incorporated in the clone."
1752 ;; To deal with SPREADP we can either use an overlay with `nil t' along
1753 ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay
1754 ;; (with a one-char margin at each end) with `t nil'.
1755 ;; We opted for a larger overlay because it behaves better in the case
1756 ;; where the clone is reduced to the empty string (we want the overlay to
1757 ;; stay when the clone's content is the empty string and we want to use
1758 ;; `evaporate' to make sure those overlays get deleted when needed).
1759 ;;
1760 (let* ((pt-end (+ (point) (- end start)))
1761 (start-margin (if (or (not spreadp) (bobp) (<= start (point-min)))
1762 0 1))
1763 (end-margin (if (or (not spreadp)
1764 (>= pt-end (point-max))
1765 (>= start (point-max)))
1766 0 1))
1767 (ol1 (make-overlay (- start start-margin) (+ end end-margin) nil t))
1768 (ol2 (make-overlay (- (point) start-margin) (+ pt-end end-margin) nil t))
1769 (dups (list ol1 ol2)))
1770 (overlay-put ol1 'modification-hooks '(text-clone-maintain))
1771 (when spreadp (overlay-put ol1 'text-clone-spreadp t))
1772 (when syntax (overlay-put ol1 'text-clone-syntax syntax))
1773 ;;(overlay-put ol1 'face 'underline)
1774 (overlay-put ol1 'evaporate t)
1775 (overlay-put ol1 'text-clones dups)
1776 ;;
1777 (overlay-put ol2 'modification-hooks '(text-clone-maintain))
1778 (when spreadp (overlay-put ol2 'text-clone-spreadp t))
1779 (when syntax (overlay-put ol2 'text-clone-syntax syntax))
1780 ;;(overlay-put ol2 'face 'underline)
1781 (overlay-put ol2 'evaporate t)
1782 (overlay-put ol2 'text-clones dups)))
1783
1784 ;;; subr.el ends here