1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
29 (defvar custom-declare-variable-list nil
30 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
31 Each element of this list holds the arguments to one call to `defcustom'.")
33 ;; Use this, rather than defcustom, in subr.el and other files loaded
35 (defun custom-declare-variable-early (&rest arguments
)
36 (setq custom-declare-variable-list
37 (cons arguments custom-declare-variable-list
)))
40 (defun macro-declaration-function (macro decl
)
41 "Process a declaration found in a macro definition.
42 This is set as the value of the variable `macro-declaration-function'.
43 MACRO is the name of the macro being defined.
44 DECL is a list `(declare ...)' containing the declarations.
45 The return value of this function is not used."
46 (dolist (d (cdr decl
))
47 (cond ((and (consp d
) (eq (car d
) 'indent
))
48 (put macro
'lisp-indent-function
(cadr d
)))
49 ((and (consp d
) (eq (car d
) 'debug
))
50 (put macro
'edebug-form-spec
(cadr d
)))
52 (message "Unknown declaration %s" d
)))))
54 (setq macro-declaration-function
'macro-declaration-function
)
57 ;;;; Lisp language features.
61 (defmacro lambda
(&rest cdr
)
62 "Return a lambda expression.
63 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
64 self-quoting; the result of evaluating the lambda expression is the
65 expression itself. The lambda expression may then be treated as a
66 function, i.e., stored as the function value of a symbol, passed to
67 funcall or mapcar, etc.
69 ARGS should take the same form as an argument list for a `defun'.
70 DOCSTRING is an optional documentation string.
71 If present, it should describe how to call the function.
72 But documentation strings are usually not useful in nameless functions.
73 INTERACTIVE should be a call to the function `interactive', which see.
74 It may also be omitted.
75 BODY should be a list of lisp expressions."
76 ;; Note that this definition should not use backquotes; subr.el should not
77 ;; depend on backquote.el.
78 (list 'function
(cons 'lambda cdr
)))
80 (defmacro push
(newelt listname
)
81 "Add NEWELT to the list stored in the symbol LISTNAME.
82 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
83 LISTNAME must be a symbol."
85 (list 'cons newelt listname
)))
87 (defmacro pop
(listname)
88 "Return the first element of LISTNAME's value, and remove it from the list.
89 LISTNAME must be a symbol whose value is a list.
90 If the value is nil, `pop' returns nil but does not actually
92 (list 'prog1
(list 'car listname
)
93 (list 'setq listname
(list 'cdr listname
))))
95 (defmacro when
(cond &rest body
)
96 "If COND yields non-nil, do BODY, else return nil."
97 (list 'if cond
(cons 'progn body
)))
99 (defmacro unless
(cond &rest body
)
100 "If COND yields nil, do BODY, else return nil."
101 (cons 'if
(cons cond
(cons nil body
))))
103 (defmacro dolist
(spec &rest body
)
104 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
105 Evaluate BODY with VAR bound to each car from LIST, in turn.
106 Then evaluate RESULT to get return value, default nil."
107 (let ((temp (make-symbol "--dolist-temp--")))
108 (list 'let
(list (list temp
(nth 1 spec
)) (car spec
))
110 (list 'setq
(car spec
) (list 'car temp
))
113 (list (list 'setq temp
(list 'cdr temp
))))))
116 (cons (list 'setq
(car spec
) nil
) (cdr (cdr spec
))))))))
118 (defmacro dotimes
(spec &rest body
)
119 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
120 Evaluate BODY with VAR bound to successive integers running from 0,
121 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
122 the return value (nil if RESULT is omitted)."
123 (let ((temp (make-symbol "--dotimes-temp--")))
124 (list 'let
(list (list temp
(nth 1 spec
)) (list (car spec
) 0))
125 (list 'while
(list '< (car spec
) temp
)
127 (append body
(list (list 'setq
(car spec
)
128 (list '1+ (car spec
)))))))
130 (car (cdr (cdr spec
)))
134 "Return the car of the car of X."
138 "Return the car of the cdr of X."
142 "Return the cdr of the car of X."
146 "Return the cdr of the cdr of X."
149 (defun last (x &optional n
)
150 "Return the last link of the list X. Its car is the last element.
151 If X is nil, return nil.
152 If N is non-nil, return the Nth-to-last link of X.
153 If N is bigger than the length of X, return X."
157 (setq m
(1+ m
) p
(cdr p
)))
159 (if (< n m
) (nthcdr (- m n
) x
) x
)))
160 (while (consp (cdr x
))
164 (defun butlast (x &optional n
)
165 "Returns a copy of LIST with the last N elements removed."
166 (if (and n
(<= n
0)) x
167 (nbutlast (copy-sequence x
) n
)))
169 (defun nbutlast (x &optional n
)
170 "Modifies LIST to remove the last N elements."
171 (let ((m (length x
)))
175 (if (> n
0) (setcdr (nthcdr (- (1- m
) n
) x
) nil
))
178 (defun remove (elt seq
)
179 "Return a copy of SEQ with all occurrences of ELT removed.
180 SEQ must be a list, vector, or string. The comparison is done with `equal'."
182 ;; If SEQ isn't a list, there's no need to copy SEQ because
183 ;; `delete' will return a new object.
185 (delete elt
(copy-sequence seq
))))
187 (defun remq (elt list
)
188 "Return a copy of LIST with all occurences of ELT removed.
189 The comparison is done with `eq'."
191 (delq elt
(copy-sequence list
))
194 (defun copy-list (list)
195 "Return a copy of a list, which may be a dotted list.
196 The elements of the list are not copied, just the list structure itself."
199 (while (consp list
) (push (pop list
) res
))
200 (prog1 (nreverse res
) (setcdr res list
)))
203 (defun copy-tree (tree &optional vecp
)
204 "Make a copy of TREE.
205 If TREE is a cons cell, this recursively copies both its car and its cdr.
206 Contrast to copy-sequence, which copies only along the cdrs. With second
207 argument VECP, this copies vectors as well as conses."
209 (let ((p (setq tree
(copy-list tree
))))
211 (if (or (consp (car p
)) (and vecp
(vectorp (car p
))))
212 (setcar p
(copy-tree (car p
) vecp
)))
213 (or (listp (cdr p
)) (setcdr p
(copy-tree (cdr p
) vecp
)))
215 (if (and vecp
(vectorp tree
))
216 (let ((i (length (setq tree
(copy-sequence tree
)))))
217 (while (>= (setq i
(1- i
)) 0)
218 (aset tree i
(copy-tree (aref tree i
) vecp
))))))
221 (defun assoc-default (key alist
&optional test default
)
222 "Find object KEY in a pseudo-alist ALIST.
223 ALIST is a list of conses or objects. Each element (or the element's car,
224 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
225 If that is non-nil, the element matches;
226 then `assoc-default' returns the element's cdr, if it is a cons,
227 or DEFAULT if the element is not a cons.
229 If no element matches, the value is nil.
230 If TEST is omitted or nil, `equal' is used."
231 (let (found (tail alist
) value
)
232 (while (and tail
(not found
))
233 (let ((elt (car tail
)))
234 (when (funcall (or test
'equal
) (if (consp elt
) (car elt
) elt
) key
)
235 (setq found t value
(if (consp elt
) (cdr elt
) default
))))
236 (setq tail
(cdr tail
)))
239 (defun assoc-ignore-case (key alist
)
240 "Like `assoc', but ignores differences in case and text representation.
241 KEY must be a string. Upper-case and lower-case letters are treated as equal.
242 Unibyte strings are converted to multibyte for comparison."
244 (while (and alist
(not element
))
245 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil t
))
246 (setq element
(car alist
)))
247 (setq alist
(cdr alist
)))
250 (defun assoc-ignore-representation (key alist
)
251 "Like `assoc', but ignores differences in text representation.
252 KEY must be a string.
253 Unibyte strings are converted to multibyte for comparison."
255 (while (and alist
(not element
))
256 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil
))
257 (setq element
(car alist
)))
258 (setq alist
(cdr alist
)))
261 (defun member-ignore-case (elt list
)
262 "Like `member', but ignores differences in case and text representation.
263 ELT must be a string. Upper-case and lower-case letters are treated as equal.
264 Unibyte strings are converted to multibyte for comparison.
265 Non-strings in LIST are ignored."
267 (not (and (stringp (car list
))
268 (eq t
(compare-strings elt
0 nil
(car list
) 0 nil t
)))))
269 (setq list
(cdr list
)))
279 ;Prevent the \{...} documentation construct
280 ;from mentioning keys that run this command.
281 (put 'undefined
'suppress-keymap t
)
283 (defun suppress-keymap (map &optional nodigits
)
284 "Make MAP override all normally self-inserting keys to be undefined.
285 Normally, as an exception, digits and minus-sign are set to make prefix args,
286 but optional second arg NODIGITS non-nil treats them like other chars."
287 (substitute-key-definition 'self-insert-command
'undefined map global-map
)
290 (define-key map
"-" 'negative-argument
)
291 ;; Make plain numbers do numeric args.
294 (define-key map
(char-to-string loop
) 'digit-argument
)
295 (setq loop
(1+ loop
))))))
298 ;(defun copy-keymap (keymap)
299 ; "Return a copy of KEYMAP"
300 ; (while (not (keymapp keymap))
301 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
302 ; (if (vectorp keymap)
303 ; (copy-sequence keymap)
304 ; (copy-alist keymap)))
306 (defvar key-substitution-in-progress nil
307 "Used internally by substitute-key-definition.")
309 (defun substitute-key-definition (olddef newdef keymap
&optional oldmap prefix
)
310 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
311 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
312 Alternatively, if optional fourth argument OLDMAP is specified, we redefine
313 in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP."
314 ;; Don't document PREFIX in the doc string because we don't want to
315 ;; advertise it. It's meant for recursive calls only. Here's its
318 ;; If optional argument PREFIX is specified, it should be a key
319 ;; prefix, a string. Redefined bindings will then be bound to the
320 ;; original key, with PREFIX added at the front.
321 (or prefix
(setq prefix
""))
322 (let* ((scan (or oldmap keymap
))
324 (prefix1 (vconcat prefix vec1
))
325 (key-substitution-in-progress
326 (cons scan key-substitution-in-progress
)))
327 ;; Scan OLDMAP, finding each char or event-symbol that
328 ;; has any definition, and act on it with hack-key.
330 (if (consp (car scan
))
331 (let ((char (car (car scan
)))
332 (defn (cdr (car scan
))))
333 ;; The inside of this let duplicates exactly
334 ;; the inside of the following let that handles array elements.
336 (aset prefix1
(length prefix
) char
)
337 (let (inner-def skipped
)
338 ;; Skip past menu-prompt.
339 (while (stringp (car-safe defn
))
340 (setq skipped
(cons (car defn
) skipped
))
341 (setq defn
(cdr defn
)))
342 ;; Skip past cached key-equivalence data for menu items.
343 (and (consp defn
) (consp (car defn
))
344 (setq defn
(cdr defn
)))
345 (setq inner-def defn
)
346 ;; Look past a symbol that names a keymap.
347 (while (and (symbolp inner-def
)
349 (setq inner-def
(symbol-function inner-def
)))
350 (if (or (eq defn olddef
)
351 ;; Compare with equal if definition is a key sequence.
352 ;; That is useful for operating on function-key-map.
353 (and (or (stringp defn
) (vectorp defn
))
354 (equal defn olddef
)))
355 (define-key keymap prefix1
(nconc (nreverse skipped
) newdef
))
356 (if (and (keymapp defn
)
357 ;; Avoid recursively scanning
358 ;; where KEYMAP does not have a submap.
359 (let ((elt (lookup-key keymap prefix1
)))
362 ;; Avoid recursively rescanning keymap being scanned.
364 key-substitution-in-progress
)))
365 ;; If this one isn't being scanned already,
367 (substitute-key-definition olddef newdef keymap
370 (if (vectorp (car scan
))
371 (let* ((array (car scan
))
375 (let ((char i
) (defn (aref array i
)))
376 ;; The inside of this let duplicates exactly
377 ;; the inside of the previous let.
379 (aset prefix1
(length prefix
) char
)
380 (let (inner-def skipped
)
381 ;; Skip past menu-prompt.
382 (while (stringp (car-safe defn
))
383 (setq skipped
(cons (car defn
) skipped
))
384 (setq defn
(cdr defn
)))
385 (and (consp defn
) (consp (car defn
))
386 (setq defn
(cdr defn
)))
387 (setq inner-def defn
)
388 (while (and (symbolp inner-def
)
390 (setq inner-def
(symbol-function inner-def
)))
391 (if (or (eq defn olddef
)
392 (and (or (stringp defn
) (vectorp defn
))
393 (equal defn olddef
)))
394 (define-key keymap prefix1
395 (nconc (nreverse skipped
) newdef
))
396 (if (and (keymapp defn
)
397 (let ((elt (lookup-key keymap prefix1
)))
401 key-substitution-in-progress
)))
402 (substitute-key-definition olddef newdef keymap
406 (if (char-table-p (car scan
))
408 (function (lambda (char defn
)
410 ;; The inside of this let duplicates exactly
411 ;; the inside of the previous let,
412 ;; except that it uses set-char-table-range
413 ;; instead of define-key.
415 (aset prefix1
(length prefix
) char
)
416 (let (inner-def skipped
)
417 ;; Skip past menu-prompt.
418 (while (stringp (car-safe defn
))
419 (setq skipped
(cons (car defn
) skipped
))
420 (setq defn
(cdr defn
)))
421 (and (consp defn
) (consp (car defn
))
422 (setq defn
(cdr defn
)))
423 (setq inner-def defn
)
424 (while (and (symbolp inner-def
)
426 (setq inner-def
(symbol-function inner-def
)))
427 (if (or (eq defn olddef
)
428 (and (or (stringp defn
) (vectorp defn
))
429 (equal defn olddef
)))
430 (define-key keymap prefix1
431 (nconc (nreverse skipped
) newdef
))
432 (if (and (keymapp defn
)
433 (let ((elt (lookup-key keymap prefix1
)))
437 key-substitution-in-progress
)))
438 (substitute-key-definition olddef newdef keymap
442 (setq scan
(cdr scan
)))))
444 (defun define-key-after (keymap key definition
&optional after
)
445 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
446 This is like `define-key' except that the binding for KEY is placed
447 just after the binding for the event AFTER, instead of at the beginning
448 of the map. Note that AFTER must be an event type (like KEY), NOT a command
451 If AFTER is t or omitted, the new binding goes at the end of the keymap.
452 AFTER should be a single event type--a symbol or a character, not a sequence.
454 Bindings are always added before any inherited map.
456 The order of bindings in a keymap matters when it is used as a menu."
457 (unless after
(setq after t
))
459 (signal 'wrong-type-argument
(list 'keymapp keymap
)))
461 (if (<= (length key
) 1) (aref key
0)
462 (setq keymap
(lookup-key keymap
464 (butlast (mapcar 'identity key
)))))
465 (aref key
(1- (length key
)))))
466 (let ((tail keymap
) done inserted
)
467 (while (and (not done
) tail
)
468 ;; Delete any earlier bindings for the same key.
469 (if (eq (car-safe (car (cdr tail
))) key
)
470 (setcdr tail
(cdr (cdr tail
))))
471 ;; If we hit an included map, go down that one.
472 (if (keymapp (car tail
)) (setq tail
(car tail
)))
473 ;; When we reach AFTER's binding, insert the new binding after.
474 ;; If we reach an inherited keymap, insert just before that.
475 ;; If we reach the end of this keymap, insert at the end.
476 (if (or (and (eq (car-safe (car tail
)) after
)
478 (eq (car (cdr tail
)) 'keymap
)
481 ;; Stop the scan only if we find a parent keymap.
482 ;; Keep going past the inserted element
483 ;; so we can delete any duplications that come later.
484 (if (eq (car (cdr tail
)) 'keymap
)
486 ;; Don't insert more than once.
488 (setcdr tail
(cons (cons key definition
) (cdr tail
))))
490 (setq tail
(cdr tail
)))))
494 "Convert KEYS to the internal Emacs key representation.
495 KEYS should be a string constant in the format used for
496 saving keyboard macros (see `insert-kbd-macro')."
497 (read-kbd-macro keys
))
499 (put 'keyboard-translate-table
'char-table-extra-slots
0)
501 (defun keyboard-translate (from to
)
502 "Translate character FROM to TO at a low level.
503 This function creates a `keyboard-translate-table' if necessary
504 and then modifies one entry in it."
505 (or (char-table-p keyboard-translate-table
)
506 (setq keyboard-translate-table
507 (make-char-table 'keyboard-translate-table nil
)))
508 (aset keyboard-translate-table from to
))
511 ;;;; The global keymap tree.
513 ;;; global-map, esc-map, and ctl-x-map have their values set up in
514 ;;; keymap.c; we just give them docstrings here.
516 (defvar global-map nil
517 "Default global keymap mapping Emacs keyboard input into commands.
518 The value is a keymap which is usually (but not necessarily) Emacs's
522 "Default keymap for ESC (meta) commands.
523 The normal global definition of the character ESC indirects to this keymap.")
525 (defvar ctl-x-map nil
526 "Default keymap for C-x commands.
527 The normal global definition of the character C-x indirects to this keymap.")
529 (defvar ctl-x-4-map
(make-sparse-keymap)
530 "Keymap for subcommands of C-x 4.")
531 (defalias 'ctl-x-4-prefix ctl-x-4-map
)
532 (define-key ctl-x-map
"4" 'ctl-x-4-prefix
)
534 (defvar ctl-x-5-map
(make-sparse-keymap)
535 "Keymap for frame commands.")
536 (defalias 'ctl-x-5-prefix ctl-x-5-map
)
537 (define-key ctl-x-map
"5" 'ctl-x-5-prefix
)
540 ;;;; Event manipulation functions.
542 ;; The call to `read' is to ensure that the value is computed at load time
543 ;; and not compiled into the .elc file. The value is negative on most
544 ;; machines, but not on all!
545 (defconst listify-key-sequence-1
(logior 128 (read "?\\M-\\^@")))
547 (defun listify-key-sequence (key)
548 "Convert a key sequence to a list of events."
551 (mapcar (function (lambda (c)
553 (logxor c listify-key-sequence-1
)
557 (defsubst eventp
(obj)
558 "True if the argument is an event object."
561 (get obj
'event-symbol-elements
))
564 (get (car obj
) 'event-symbol-elements
))))
566 (defun event-modifiers (event)
567 "Returns a list of symbols representing the modifier keys in event EVENT.
568 The elements of the list may include `meta', `control',
569 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
573 (setq type
(car type
)))
575 (cdr (get type
'event-symbol-elements
))
577 (or (zerop (logand type ?\M-\^
@))
578 (setq list
(cons 'meta list
)))
579 (or (and (zerop (logand type ?\C-\^
@))
580 (>= (logand type
127) 32))
581 (setq list
(cons 'control list
)))
582 (or (and (zerop (logand type ?\S-\^
@))
583 (= (logand type
255) (downcase (logand type
255))))
584 (setq list
(cons 'shift list
)))
585 (or (zerop (logand type ?\H-\^
@))
586 (setq list
(cons 'hyper list
)))
587 (or (zerop (logand type ?\s-\^
@))
588 (setq list
(cons 'super list
)))
589 (or (zerop (logand type ?\A-\^
@))
590 (setq list
(cons 'alt list
)))
593 (defun event-basic-type (event)
594 "Returns the basic type of the given event (all modifiers removed).
595 The value is a printing character (not upper case) or a symbol."
597 (setq event
(car event
)))
599 (car (get event
'event-symbol-elements
))
600 (let ((base (logand event
(1- (lsh 1 18)))))
601 (downcase (if (< base
32) (logior base
64) base
)))))
603 (defsubst mouse-movement-p
(object)
604 "Return non-nil if OBJECT is a mouse movement event."
606 (eq (car object
) 'mouse-movement
)))
608 (defsubst event-start
(event)
609 "Return the starting position of EVENT.
610 If EVENT is a mouse press or a mouse click, this returns the location
612 If EVENT is a drag, this returns the drag's starting position.
613 The return value is of the form
614 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
615 The `posn-' functions access elements of such lists."
618 (defsubst event-end
(event)
619 "Return the ending location of EVENT. EVENT should be a click or drag event.
620 If EVENT is a click event, this function is the same as `event-start'.
621 The return value is of the form
622 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
623 The `posn-' functions access elements of such lists."
624 (nth (if (consp (nth 2 event
)) 2 1) event
))
626 (defsubst event-click-count
(event)
627 "Return the multi-click count of EVENT, a click or drag event.
628 The return value is a positive integer."
629 (if (integerp (nth 2 event
)) (nth 2 event
) 1))
631 (defsubst posn-window
(position)
632 "Return the window in POSITION.
633 POSITION should be a list of the form
634 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
635 as returned by the `event-start' and `event-end' functions."
638 (defsubst posn-point
(position)
639 "Return the buffer location in POSITION.
640 POSITION should be a list of the form
641 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
642 as returned by the `event-start' and `event-end' functions."
643 (if (consp (nth 1 position
))
644 (car (nth 1 position
))
647 (defsubst posn-x-y
(position)
648 "Return the x and y coordinates in POSITION.
649 POSITION should be a list of the form
650 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
651 as returned by the `event-start' and `event-end' functions."
654 (defun posn-col-row (position)
655 "Return the column and row in POSITION, measured in characters.
656 POSITION should be a list of the form
657 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
658 as returned by the `event-start' and `event-end' functions.
659 For a scroll-bar event, the result column is 0, and the row
660 corresponds to the vertical position of the click in the scroll bar."
661 (let ((pair (nth 2 position
))
662 (window (posn-window position
)))
663 (if (eq (if (consp (nth 1 position
))
664 (car (nth 1 position
))
666 'vertical-scroll-bar
)
667 (cons 0 (scroll-bar-scale pair
(1- (window-height window
))))
668 (if (eq (if (consp (nth 1 position
))
669 (car (nth 1 position
))
671 'horizontal-scroll-bar
)
672 (cons (scroll-bar-scale pair
(window-width window
)) 0)
673 (let* ((frame (if (framep window
) window
(window-frame window
)))
674 (x (/ (car pair
) (frame-char-width frame
)))
675 (y (/ (cdr pair
) (frame-char-height frame
))))
678 (defsubst posn-timestamp
(position)
679 "Return the timestamp of POSITION.
680 POSITION should be a list of the form
681 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
682 as returned by the `event-start' and `event-end' functions."
686 ;;;; Obsolescent names for functions.
688 (defalias 'dot
'point
)
689 (defalias 'dot-marker
'point-marker
)
690 (defalias 'dot-min
'point-min
)
691 (defalias 'dot-max
'point-max
)
692 (defalias 'window-dot
'window-point
)
693 (defalias 'set-window-dot
'set-window-point
)
694 (defalias 'read-input
'read-string
)
695 (defalias 'send-string
'process-send-string
)
696 (defalias 'send-region
'process-send-region
)
697 (defalias 'show-buffer
'set-window-buffer
)
698 (defalias 'buffer-flush-undo
'buffer-disable-undo
)
699 (defalias 'eval-current-buffer
'eval-buffer
)
700 (defalias 'compiled-function-p
'byte-code-function-p
)
701 (defalias 'define-function
'defalias
)
703 (defalias 'sref
'aref
)
704 (make-obsolete 'sref
'aref
"20.4")
705 (make-obsolete 'char-bytes
"Now this function always returns 1" "20.4")
707 (defun insert-string (&rest args
)
708 "Mocklisp-compatibility insert function.
709 Like the function `insert' except that any argument that is a number
710 is converted into a string by expressing it in decimal."
712 (insert (if (integerp el
) (number-to-string el
) el
))))
714 (make-obsolete 'insert-string
'insert
"21.3")
716 ;; Some programs still use this as a function.
718 "Obsolete function returning the value of the `baud-rate' variable.
719 Please convert your programs to use the variable `baud-rate' directly."
722 (defalias 'focus-frame
'ignore
)
723 (defalias 'unfocus-frame
'ignore
)
725 ;;;; Alternate names for functions - these are not being phased out.
727 (defalias 'string
= 'string-equal
)
728 (defalias 'string
< 'string-lessp
)
729 (defalias 'move-marker
'set-marker
)
730 (defalias 'rplaca
'setcar
)
731 (defalias 'rplacd
'setcdr
)
732 (defalias 'beep
'ding
) ;preserve lingual purity
733 (defalias 'indent-to-column
'indent-to
)
734 (defalias 'backward-delete-char
'delete-backward-char
)
735 (defalias 'search-forward-regexp
(symbol-function 're-search-forward
))
736 (defalias 'search-backward-regexp
(symbol-function 're-search-backward
))
737 (defalias 'int-to-string
'number-to-string
)
738 (defalias 'store-match-data
'set-match-data
)
739 ;; These are the XEmacs names:
740 (defalias 'point-at-eol
'line-end-position
)
741 (defalias 'point-at-bol
'line-beginning-position
)
743 ;;; Should this be an obsolete name? If you decide it should, you get
744 ;;; to go through all the sources and change them.
745 (defalias 'string-to-int
'string-to-number
)
747 ;;;; Hook manipulation functions.
749 (defun make-local-hook (hook)
750 "Make the hook HOOK local to the current buffer.
751 The return value is HOOK.
753 You never need to call this function now that `add-hook' does it for you
754 if its LOCAL argument is non-nil.
756 When a hook is local, its local and global values
757 work in concert: running the hook actually runs all the hook
758 functions listed in *either* the local value *or* the global value
759 of the hook variable.
761 This function works by making t a member of the buffer-local value,
762 which acts as a flag to run the hook functions in the default value as
763 well. This works for all normal hooks, but does not work for most
764 non-normal hooks yet. We will be changing the callers of non-normal
765 hooks so that they can handle localness; this has to be done one by
768 This function does nothing if HOOK is already local in the current
771 Do not use `make-local-variable' to make a hook variable buffer-local."
772 (if (local-variable-p hook
)
774 (or (boundp hook
) (set hook nil
))
775 (make-local-variable hook
)
778 (make-obsolete 'make-local-hook
"Not necessary any more." "21.1")
780 (defun add-hook (hook function
&optional append local
)
781 "Add to the value of HOOK the function FUNCTION.
782 FUNCTION is not added if already present.
783 FUNCTION is added (if necessary) at the beginning of the hook list
784 unless the optional argument APPEND is non-nil, in which case
785 FUNCTION is added at the end.
787 The optional fourth argument, LOCAL, if non-nil, says to modify
788 the hook's buffer-local value rather than its default value.
789 This makes the hook buffer-local if needed, and it makes t a member
790 of the buffer-local value. That acts as a flag to run the hook
791 functions in the default value as well as in the local value.
793 HOOK should be a symbol, and FUNCTION may be any valid function. If
794 HOOK is void, it is first set to nil. If HOOK's value is a single
795 function, it is changed to a list of functions."
796 (or (boundp hook
) (set hook nil
))
797 (or (default-boundp hook
) (set-default hook nil
))
798 (if local
(unless (local-variable-if-set-p hook
)
799 (set (make-local-variable hook
) (list t
)))
800 ;; Detect the case where make-local-variable was used on a hook
801 ;; and do what we used to do.
802 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
804 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
805 ;; If the hook value is a single function, turn it into a list.
806 (when (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
807 (setq hook-value
(list hook-value
)))
808 ;; Do the actual addition if necessary
809 (unless (member function hook-value
)
812 (append hook-value
(list function
))
813 (cons function hook-value
))))
814 ;; Set the actual variable
815 (if local
(set hook hook-value
) (set-default hook hook-value
))))
817 (defun remove-hook (hook function
&optional local
)
818 "Remove from the value of HOOK the function FUNCTION.
819 HOOK should be a symbol, and FUNCTION may be any valid function. If
820 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
821 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
823 The optional third argument, LOCAL, if non-nil, says to modify
824 the hook's buffer-local value rather than its default value.
825 This makes the hook buffer-local if needed."
826 (or (boundp hook
) (set hook nil
))
827 (or (default-boundp hook
) (set-default hook nil
))
828 (if local
(unless (local-variable-if-set-p hook
)
829 (set (make-local-variable hook
) (list t
)))
830 ;; Detect the case where make-local-variable was used on a hook
831 ;; and do what we used to do.
832 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
834 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
835 ;; Remove the function, for both the list and the non-list cases.
836 (if (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
837 (if (equal hook-value function
) (setq hook-value nil
))
838 (setq hook-value
(delete function
(copy-sequence hook-value
))))
839 ;; If the function is on the global hook, we need to shadow it locally
840 ;;(when (and local (member function (default-value hook))
841 ;; (not (member (cons 'not function) hook-value)))
842 ;; (push (cons 'not function) hook-value))
843 ;; Set the actual variable
845 (set-default hook hook-value
)
846 (if (equal hook-value
'(t))
847 (kill-local-variable hook
)
848 (set hook hook-value
)))))
850 (defun add-to-list (list-var element
&optional append
)
851 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
852 The test for presence of ELEMENT is done with `equal'.
853 If ELEMENT is added, it is added at the beginning of the list,
854 unless the optional argument APPEND is non-nil, in which case
855 ELEMENT is added at the end.
857 The return value is the new value of LIST-VAR.
859 If you want to use `add-to-list' on a variable that is not defined
860 until a certain package is loaded, you should put the call to `add-to-list'
861 into a hook function that will be run only after loading the package.
862 `eval-after-load' provides one way to do this. In some cases
863 other hooks, such as major mode hooks, can do the job."
864 (if (member element
(symbol-value list-var
))
865 (symbol-value list-var
)
868 (append (symbol-value list-var
) (list element
))
869 (cons element
(symbol-value list-var
))))))
874 (defvar symbol-file-load-history-loaded nil
875 "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
876 That file records the part of `load-history' for preloaded files,
877 which is cleared out before dumping to make Emacs smaller.")
879 (defun load-symbol-file-load-history ()
880 "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
881 That file records the part of `load-history' for preloaded files,
882 which is cleared out before dumping to make Emacs smaller."
883 (unless symbol-file-load-history-loaded
884 (load (expand-file-name
885 ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
886 (if (eq system-type
'ms-dos
)
888 (format "fns-%s.el" emacs-version
))
890 ;; The file name fns-%s.el already has a .el extension.
892 (setq symbol-file-load-history-loaded t
)))
894 (defun symbol-file (function)
895 "Return the input source from which FUNCTION was loaded.
896 The value is normally a string that was passed to `load':
897 either an absolute file name, or a library name
898 \(with no directory name and no `.el' or `.elc' at the end).
899 It can also be nil, if the definition is not associated with any file."
900 (load-symbol-file-load-history)
901 (let ((files load-history
)
904 (if (memq function
(cdr (car files
)))
905 (setq file
(car (car files
)) files nil
))
906 (setq files
(cdr files
)))
910 ;;;; Specifying things to do after certain files are loaded.
912 (defun eval-after-load (file form
)
913 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
914 This makes or adds to an entry on `after-load-alist'.
915 If FILE is already loaded, evaluate FORM right now.
916 It does nothing if FORM is already on the list for FILE.
917 FILE must match exactly. Normally FILE is the name of a library,
918 with no directory or extension specified, since that is how `load'
920 FILE can also be a feature (i.e. a symbol), in which case FORM is
921 evaluated whenever that feature is `provide'd."
922 (let ((elt (assoc file after-load-alist
)))
923 ;; Make sure there is an element for FILE.
924 (unless elt
(setq elt
(list file
)) (push elt after-load-alist
))
925 ;; Add FORM to the element if it isn't there.
926 (unless (member form
(cdr elt
))
927 (nconc elt
(list form
))
928 ;; If the file has been loaded already, run FORM right away.
929 (if (if (symbolp file
)
931 ;; Make sure `load-history' contains the files dumped with
932 ;; Emacs for the case that FILE is one of them.
933 (load-symbol-file-load-history)
934 (assoc file load-history
))
938 (defun eval-next-after-load (file)
939 "Read the following input sexp, and run it whenever FILE is loaded.
940 This makes or adds to an entry on `after-load-alist'.
941 FILE should be the name of a library, with no directory name."
942 (eval-after-load file
(read)))
944 ;;; make-network-process wrappers
946 (if (featurep 'make-network-process
)
949 (defun open-network-stream (name buffer host service
)
950 "Open a TCP connection for a service to a host.
951 Returns a subprocess-object to represent the connection.
952 Input and output work as for subprocesses; `delete-process' closes it.
953 Args are NAME BUFFER HOST SERVICE.
954 NAME is name for process. It is modified if necessary to make it unique.
955 BUFFER is the buffer (or buffer-name) to associate with the process.
956 Process output goes at end of that buffer, unless you specify
957 an output stream or filter function to handle the output.
958 BUFFER may be also nil, meaning that this process is not associated
960 Third arg is name of the host to connect to, or its IP address.
961 Fourth arg SERVICE is name of the service desired, or an integer
962 specifying a port number to connect to."
963 (make-network-process :name name
:buffer buffer
964 :host host
:service service
))
966 (defun open-network-stream-nowait (name buffer host service
&optional sentinel filter
)
967 "Initiate connection to a TCP connection for a service to a host.
968 It returns nil if non-blocking connects are not supported; otherwise,
969 it returns a subprocess-object to represent the connection.
971 This function is similar to `open-network-stream', except that this
972 function returns before the connection is established. When the
973 connection is completed, the sentinel function will be called with
974 second arg matching `open' (if successful) or `failed' (on error).
976 Args are NAME BUFFER HOST SERVICE SENTINEL FILTER.
977 NAME, BUFFER, HOST, and SERVICE are as for `open-network-stream'.
978 Optional args, SENTINEL and FILTER specifies the sentinel and filter
979 functions to be used for this network stream."
980 (if (featurep 'make-network-process
'(:nowait t
))
981 (make-network-process :name name
:buffer buffer
:nowait t
982 :host host
:service service
983 :filter filter
:sentinel sentinel
)))
985 (defun open-network-stream-server (name buffer service
&optional sentinel filter
)
986 "Create a network server process for a TCP service.
987 It returns nil if server processes are not supported; otherwise,
988 it returns a subprocess-object to represent the server.
990 When a client connects to the specified service, a new subprocess
991 is created to handle the new connection, and the sentinel function
992 is called for the new process.
994 Args are NAME BUFFER SERVICE SENTINEL FILTER.
995 NAME is name for the server process. Client processes are named by
996 appending the ip-address and port number of the client to NAME.
997 BUFFER is the buffer (or buffer-name) to associate with the server
998 process. Client processes will not get a buffer if a process filter
999 is specified or BUFFER is nil; otherwise, a new buffer is created for
1000 the client process. The name is similar to the process name.
1001 Third arg SERVICE is name of the service desired, or an integer
1002 specifying a port number to connect to. It may also be t to selected
1003 an unused port number for the server.
1004 Optional args, SENTINEL and FILTER specifies the sentinel and filter
1005 functions to be used for the client processes; the server process
1006 does not use these function."
1007 (if (featurep 'make-network-process
'(:server t
))
1008 (make-network-process :name name
:buffer buffer
1009 :service service
:server t
:noquery t
1010 :sentinel sentinel
:filter filter
)))
1012 )) ;; (featurep 'make-network-process)
1017 (defun process-kill-without-query (process &optional flag
)
1018 "Say no query needed if PROCESS is running when Emacs is exited.
1019 Optional second argument if non-nil says to require a query.
1020 Value is t if a query was formerly required.
1021 New code should not use this function; use `process-query-on-exit-flag'
1022 or `set-process-query-on-exit-flag' instead."
1023 (let ((old (process-query-on-exit-flag process
)))
1024 (set-process-query-on-exit-flag process nil
)
1028 ;;;; Input and display facilities.
1030 (defvar read-quoted-char-radix
8
1031 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1032 Legitimate radix values are 8, 10 and 16.")
1034 (custom-declare-variable-early
1035 'read-quoted-char-radix
8
1036 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1037 Legitimate radix values are 8, 10 and 16."
1038 :type
'(choice (const 8) (const 10) (const 16))
1039 :group
'editing-basics
)
1041 (defun read-quoted-char (&optional prompt
)
1042 "Like `read-char', but do not allow quitting.
1043 Also, if the first character read is an octal digit,
1044 we read any number of octal digits and return the
1045 specified character code. Any nondigit terminates the sequence.
1046 If the terminator is RET, it is discarded;
1047 any other terminator is used itself as input.
1049 The optional argument PROMPT specifies a string to use to prompt the user.
1050 The variable `read-quoted-char-radix' controls which radix to use
1052 (let ((message-log-max nil
) done
(first t
) (code 0) char
)
1054 (let ((inhibit-quit first
)
1055 ;; Don't let C-h get the help message--only help function keys.
1058 "Type the special character you want to use,
1059 or the octal character code.
1060 RET terminates the character code and is discarded;
1061 any other non-digit terminates the character code and is then used as input."))
1062 (setq char
(read-event (and prompt
(format "%s-" prompt
)) t
))
1063 (if inhibit-quit
(setq quit-flag nil
)))
1064 ;; Translate TAB key into control-I ASCII character, and so on.
1066 (let ((translated (lookup-key function-key-map
(vector char
))))
1067 (if (arrayp translated
)
1068 (setq char
(aref translated
0)))))
1070 ((not (integerp char
))
1071 (setq unread-command-events
(list char
)
1073 ((/= (logand char ?\M-\^
@) 0)
1074 ;; Turn a meta-character into a character with the 0200 bit set.
1075 (setq code
(logior (logand char
(lognot ?\M-\^
@)) 128)
1077 ((and (<= ?
0 char
) (< char
(+ ?
0 (min 10 read-quoted-char-radix
))))
1078 (setq code
(+ (* code read-quoted-char-radix
) (- char ?
0)))
1079 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
1080 ((and (<= ?a
(downcase char
))
1081 (< (downcase char
) (+ ?a -
10 (min 26 read-quoted-char-radix
))))
1082 (setq code
(+ (* code read-quoted-char-radix
)
1083 (+ 10 (- (downcase char
) ?a
))))
1084 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
1085 ((and (not first
) (eq char ?\C-m
))
1088 (setq unread-command-events
(list char
)
1095 (defun read-passwd (prompt &optional confirm default
)
1096 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
1097 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1098 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
1099 Optional DEFAULT is a default password to use instead of empty input."
1102 (while (not success
)
1103 (let ((first (read-passwd prompt nil default
))
1104 (second (read-passwd "Confirm password: " nil default
)))
1105 (if (equal first second
)
1107 (and (arrayp second
) (fillarray second ?\
0))
1108 (setq success first
))
1109 (and (arrayp first
) (fillarray first ?\
0))
1110 (and (arrayp second
) (fillarray second ?\
0))
1111 (message "Password not repeated accurately; please start over")
1117 (cursor-in-echo-area t
))
1118 (while (progn (message "%s%s"
1120 (make-string (length pass
) ?.
))
1121 (setq c
(read-char-exclusive nil t
))
1122 (and (/= c ?
\r) (/= c ?
\n) (/= c ?\e
)))
1123 (clear-this-command-keys)
1126 (and (arrayp pass
) (fillarray pass ?\
0))
1128 (if (and (/= c ?
\b) (/= c ?
\177))
1129 (let* ((new-char (char-to-string c
))
1130 (new-pass (concat pass new-char
)))
1131 (and (arrayp pass
) (fillarray pass ?\
0))
1132 (fillarray new-char ?\
0)
1134 (setq pass new-pass
))
1135 (if (> (length pass
) 0)
1136 (let ((new-pass (substring pass
0 -
1)))
1137 (and (arrayp pass
) (fillarray pass ?\
0))
1138 (setq pass new-pass
))))))
1140 (or pass default
""))))
1142 ;;; Atomic change groups.
1144 (defmacro atomic-change-group
(&rest body
)
1145 "Perform BODY as an atomic change group.
1146 This means that if BODY exits abnormally,
1147 all of its changes to the current buffer are undone.
1148 This works regadless of whether undo is enabled in the buffer.
1150 This mechanism is transparent to ordinary use of undo;
1151 if undo is enabled in the buffer and BODY succeeds, the
1152 user can undo the change normally."
1153 (let ((handle (make-symbol "--change-group-handle--"))
1154 (success (make-symbol "--change-group-success--")))
1155 `(let ((,handle
(prepare-change-group))
1159 ;; This is inside the unwind-protect because
1160 ;; it enables undo if that was disabled; we need
1161 ;; to make sure that it gets disabled again.
1162 (activate-change-group ,handle
)
1165 ;; Either of these functions will disable undo
1166 ;; if it was disabled before.
1168 (accept-change-group ,handle
)
1169 (cancel-change-group ,handle
))))))
1171 (defun prepare-change-group (&optional buffer
)
1172 "Return a handle for the current buffer's state, for a change group.
1173 If you specify BUFFER, make a handle for BUFFER's state instead.
1175 Pass the handle to `activate-change-group' afterward to initiate
1176 the actual changes of the change group.
1178 To finish the change group, call either `accept-change-group' or
1179 `cancel-change-group' passing the same handle as argument. Call
1180 `accept-change-group' to accept the changes in the group as final;
1181 call `cancel-change-group' to undo them all. You should use
1182 `unwind-protect' to make sure the group is always finished. The call
1183 to `activate-change-group' should be inside the `unwind-protect'.
1184 Once you finish the group, don't use the handle again--don't try to
1185 finish the same group twice. For a simple example of correct use, see
1186 the source code of `atomic-change-group'.
1188 The handle records only the specified buffer. To make a multibuffer
1189 change group, call this function once for each buffer you want to
1190 cover, then use `nconc' to combine the returned values, like this:
1192 (nconc (prepare-change-group buffer-1)
1193 (prepare-change-group buffer-2))
1195 You can then activate that multibuffer change group with a single
1196 call to `activate-change-group' and finish it with a single call
1197 to `accept-change-group' or `cancel-change-group'."
1199 (list (cons (current-buffer) buffer-undo-list
)))
1201 (defun activate-change-group (handle)
1202 "Activate a change group made with `prepare-change-group' (which see)."
1203 (dolist (elt handle
)
1204 (with-current-buffer (car elt
)
1205 (if (eq buffer-undo-list t
)
1206 (setq buffer-undo-list nil
)))))
1208 (defun accept-change-group (handle)
1209 "Finish a change group made with `prepare-change-group' (which see).
1210 This finishes the change group by accepting its changes as final."
1211 (dolist (elt handle
)
1212 (with-current-buffer (car elt
)
1214 (setq buffer-undo-list t
)))))
1216 (defun cancel-change-group (handle)
1217 "Finish a change group made with `prepare-change-group' (which see).
1218 This finishes the change group by reverting all of its changes."
1219 (dolist (elt handle
)
1220 (with-current-buffer (car elt
)
1221 (setq elt
(cdr elt
))
1223 (if (consp elt
) (car elt
)))
1225 (if (consp elt
) (cdr elt
))))
1226 ;; Temporarily truncate the undo log at ELT.
1228 (setcar elt nil
) (setcdr elt nil
))
1229 (unless (eq last-command
'undo
) (undo-start))
1230 ;; Make sure there's no confusion.
1231 (when (and (consp elt
) (not (eq elt
(last pending-undo-list
))))
1232 (error "Undoing to some unrelated state"))
1234 (while pending-undo-list
(undo-more 1))
1235 ;; Reset the modified cons cell ELT to its original content.
1237 (setcar elt old-car
)
1238 (setcdr elt old-cdr
))
1239 ;; Revert the undo info to what it was when we grabbed the state.
1240 (setq buffer-undo-list elt
)))))
1242 ;; For compatibility.
1243 (defalias 'redraw-modeline
'force-mode-line-update
)
1245 (defun force-mode-line-update (&optional all
)
1246 "Force the mode line of the current buffer to be redisplayed.
1247 With optional non-nil ALL, force redisplay of all mode lines."
1248 (if all
(save-excursion (set-buffer (other-buffer))))
1249 (set-buffer-modified-p (buffer-modified-p)))
1251 (defun momentary-string-display (string pos
&optional exit-char message
)
1252 "Momentarily display STRING in the buffer at POS.
1253 Display remains until next character is typed.
1254 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
1255 otherwise it is then available as input (as a command if nothing else).
1256 Display MESSAGE (optional fourth arg) in the echo area.
1257 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
1258 (or exit-char
(setq exit-char ?\
))
1259 (let ((inhibit-read-only t
)
1260 ;; Don't modify the undo list at all.
1261 (buffer-undo-list t
)
1262 (modified (buffer-modified-p))
1263 (name buffer-file-name
)
1269 ;; defeat file locking... don't try this at home, kids!
1270 (setq buffer-file-name nil
)
1271 (insert-before-markers string
)
1272 (setq insert-end
(point))
1273 ;; If the message end is off screen, recenter now.
1274 (if (< (window-end nil t
) insert-end
)
1275 (recenter (/ (window-height) 2)))
1276 ;; If that pushed message start off the screen,
1277 ;; scroll to start it at the top of the screen.
1278 (move-to-window-line 0)
1283 (message (or message
"Type %s to continue editing.")
1284 (single-key-description exit-char
))
1285 (let ((char (read-event)))
1286 (or (eq char exit-char
)
1287 (setq unread-command-events
(list char
)))))
1290 (delete-region pos insert-end
)))
1291 (setq buffer-file-name name
)
1292 (set-buffer-modified-p modified
))))
1295 ;;;; Overlay operations
1297 (defun copy-overlay (o)
1298 "Return a copy of overlay O."
1299 (let ((o1 (make-overlay (overlay-start o
) (overlay-end o
)
1300 ;; FIXME: there's no easy way to find the
1301 ;; insertion-type of the two markers.
1302 (overlay-buffer o
)))
1303 (props (overlay-properties o
)))
1305 (overlay-put o1
(pop props
) (pop props
)))
1308 (defun remove-overlays (beg end name val
)
1309 "Clear BEG and END of overlays whose property NAME has value VAL.
1310 Overlays might be moved and or split."
1312 (setq beg
(prog1 end
(setq end beg
))))
1314 (dolist (o (overlays-in beg end
))
1315 (when (eq (overlay-get o name
) val
)
1316 ;; Either push this overlay outside beg...end
1317 ;; or split it to exclude beg...end
1318 ;; or delete it entirely (if it is contained in beg...end).
1319 (if (< (overlay-start o
) beg
)
1320 (if (> (overlay-end o
) end
)
1322 (move-overlay (copy-overlay o
)
1323 (overlay-start o
) beg
)
1324 (move-overlay o end
(overlay-end o
)))
1325 (move-overlay o
(overlay-start o
) beg
))
1326 (if (> (overlay-end o
) end
)
1327 (move-overlay o end
(overlay-end o
))
1328 (delete-overlay o
)))))))
1332 ;; A number of major modes set this locally.
1333 ;; Give it a global value to avoid compiler warnings.
1334 (defvar font-lock-defaults nil
)
1336 (defvar suspend-hook nil
1337 "Normal hook run by `suspend-emacs', before suspending.")
1339 (defvar suspend-resume-hook nil
1340 "Normal hook run by `suspend-emacs', after Emacs is continued.")
1342 (defvar temp-buffer-show-hook nil
1343 "Normal hook run by `with-output-to-temp-buffer' after displaying the buffer.
1344 When the hook runs, the temporary buffer is current, and the window it
1345 was displayed in is selected. This hook is normally set up with a
1346 function to make the buffer read only, and find function names and
1347 variable names in it, provided the major mode is still Help mode.")
1349 (defvar temp-buffer-setup-hook nil
1350 "Normal hook run by `with-output-to-temp-buffer' at the start.
1351 When the hook runs, the temporary buffer is current.
1352 This hook is normally set up with a function to put the buffer in Help
1355 ;; Avoid compiler warnings about this variable,
1356 ;; which has a special meaning on certain system types.
1357 (defvar buffer-file-type nil
1358 "Non-nil if the visited file is a binary file.
1359 This variable is meaningful on MS-DOG and Windows NT.
1360 On those systems, it is automatically local in every buffer.
1361 On other systems, this variable is normally always nil.")
1363 ;; This should probably be written in C (i.e., without using `walk-windows').
1364 (defun get-buffer-window-list (buffer &optional minibuf frame
)
1365 "Return windows currently displaying BUFFER, or nil if none.
1366 See `walk-windows' for the meaning of MINIBUF and FRAME."
1367 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
1368 (walk-windows (function (lambda (window)
1369 (if (eq (window-buffer window
) buffer
)
1370 (setq windows
(cons window windows
)))))
1374 (defun ignore (&rest ignore
)
1375 "Do nothing and return nil.
1376 This function accepts any number of arguments, but ignores them."
1380 (defun error (&rest args
)
1381 "Signal an error, making error message by passing all args to `format'.
1382 In Emacs, the convention is that error messages start with a capital
1383 letter but *do not* end with a period. Please follow this convention
1384 for the sake of consistency."
1386 (signal 'error
(list (apply 'format args
)))))
1388 (defalias 'user-original-login-name
'user-login-name
)
1390 (defvar yank-excluded-properties
)
1392 (defun remove-yank-excluded-properties (start end
)
1393 "Remove `yank-excluded-properties' between START and END positions.
1394 Replaces `category' properties with their defined properties."
1395 (let ((inhibit-read-only t
))
1396 ;; Replace any `category' property with the properties it stands for.
1397 (unless (memq yank-excluded-properties
'(t nil
))
1400 (while (< (point) end
)
1401 (let ((cat (get-text-property (point) 'category
))
1405 (next-single-property-change (point) 'category nil end
))
1406 (remove-list-of-text-properties (point) run-end
'(category))
1407 (add-text-properties (point) run-end
(symbol-plist cat
))
1408 (goto-char (or run-end end
)))
1410 (next-single-property-change (point) 'category nil end
))
1411 (goto-char (or run-end end
))))))
1412 (if (eq yank-excluded-properties t
)
1413 (set-text-properties start end nil
)
1414 (remove-list-of-text-properties start end
1415 yank-excluded-properties
))))
1417 (defun insert-for-yank (&rest strings
)
1418 "Insert STRINGS at point, stripping some text properties.
1419 Strip text properties from the inserted text
1420 according to `yank-excluded-properties'.
1421 Otherwise just like (insert STRINGS...)."
1422 (let ((opoint (point)))
1423 (apply 'insert strings
)
1424 (remove-yank-excluded-properties opoint
(point))))
1426 (defun insert-buffer-substring-no-properties (buf &optional start end
)
1427 "Insert before point a substring of buffer BUFFER, without text properties.
1428 BUFFER may be a buffer or a buffer name.
1429 Arguments START and END are character numbers specifying the substring.
1430 They default to the beginning and the end of BUFFER."
1431 (let ((opoint (point)))
1432 (insert-buffer-substring buf start end
)
1433 (let ((inhibit-read-only t
))
1434 (set-text-properties opoint
(point) nil
))))
1436 (defun insert-buffer-substring-as-yank (buf &optional start end
)
1437 "Insert before point a part of buffer BUFFER, stripping some text properties.
1438 BUFFER may be a buffer or a buffer name. Arguments START and END are
1439 character numbers specifying the substring. They default to the
1440 beginning and the end of BUFFER. Strip text properties from the
1441 inserted text according to `yank-excluded-properties'."
1442 (let ((opoint (point)))
1443 (insert-buffer-substring buf start end
)
1444 (remove-yank-excluded-properties opoint
(point))))
1447 ;; Synchronous shell commands.
1449 (defun start-process-shell-command (name buffer
&rest args
)
1450 "Start a program in a subprocess. Return the process object for it.
1451 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1452 NAME is name for process. It is modified if necessary to make it unique.
1453 BUFFER is the buffer or (buffer-name) to associate with the process.
1454 Process output goes at end of that buffer, unless you specify
1455 an output stream or filter function to handle the output.
1456 BUFFER may be also nil, meaning that this process is not associated
1458 Third arg is command name, the name of a shell command.
1459 Remaining arguments are the arguments for the command.
1460 Wildcards and redirection are handled as usual in the shell."
1462 ((eq system-type
'vax-vms
)
1463 (apply 'start-process name buffer args
))
1464 ;; We used to use `exec' to replace the shell with the command,
1465 ;; but that failed to handle (...) and semicolon, etc.
1467 (start-process name buffer shell-file-name shell-command-switch
1468 (mapconcat 'identity args
" ")))))
1470 (defun call-process-shell-command (command &optional infile buffer display
1472 "Execute the shell command COMMAND synchronously in separate process.
1473 The remaining arguments are optional.
1474 The program's input comes from file INFILE (nil means `/dev/null').
1475 Insert output in BUFFER before point; t means current buffer;
1476 nil for BUFFER means discard it; 0 means discard and don't wait.
1477 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1478 REAL-BUFFER says what to do with standard output, as above,
1479 while STDERR-FILE says what to do with standard error in the child.
1480 STDERR-FILE may be nil (discard standard error output),
1481 t (mix it with ordinary output), or a file name string.
1483 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1484 Remaining arguments are strings passed as additional arguments for COMMAND.
1485 Wildcards and redirection are handled as usual in the shell.
1487 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
1488 Otherwise it waits for COMMAND to terminate and returns a numeric exit
1489 status or a signal description string.
1490 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
1492 ((eq system-type
'vax-vms
)
1493 (apply 'call-process command infile buffer display args
))
1494 ;; We used to use `exec' to replace the shell with the command,
1495 ;; but that failed to handle (...) and semicolon, etc.
1497 (call-process shell-file-name
1498 infile buffer display
1499 shell-command-switch
1500 (mapconcat 'identity
(cons command args
) " ")))))
1502 (defmacro with-current-buffer
(buffer &rest body
)
1503 "Execute the forms in BODY with BUFFER as the current buffer.
1504 The value returned is the value of the last form in BODY.
1505 See also `with-temp-buffer'."
1506 (cons 'save-current-buffer
1507 (cons (list 'set-buffer buffer
)
1510 (defmacro with-temp-file
(file &rest body
)
1511 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1512 The value returned is the value of the last form in BODY.
1513 See also `with-temp-buffer'."
1514 (let ((temp-file (make-symbol "temp-file"))
1515 (temp-buffer (make-symbol "temp-buffer")))
1516 `(let ((,temp-file
,file
)
1518 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1521 (with-current-buffer ,temp-buffer
1523 (with-current-buffer ,temp-buffer
1525 (write-region (point-min) (point-max) ,temp-file nil
0)))
1526 (and (buffer-name ,temp-buffer
)
1527 (kill-buffer ,temp-buffer
))))))
1529 (defmacro with-temp-message
(message &rest body
)
1530 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1531 The original message is restored to the echo area after BODY has finished.
1532 The value returned is the value of the last form in BODY.
1533 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1534 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1535 Use a MESSAGE of \"\" to temporarily clear the echo area."
1536 (let ((current-message (make-symbol "current-message"))
1537 (temp-message (make-symbol "with-temp-message")))
1538 `(let ((,temp-message
,message
)
1543 (setq ,current-message
(current-message))
1544 (message "%s" ,temp-message
))
1547 (if ,current-message
1548 (message "%s" ,current-message
)
1551 (defmacro with-temp-buffer
(&rest body
)
1552 "Create a temporary buffer, and evaluate BODY there like `progn'.
1553 See also `with-temp-file' and `with-output-to-string'."
1554 (let ((temp-buffer (make-symbol "temp-buffer")))
1555 `(let ((,temp-buffer
1556 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1558 (with-current-buffer ,temp-buffer
1560 (and (buffer-name ,temp-buffer
)
1561 (kill-buffer ,temp-buffer
))))))
1563 (defmacro with-output-to-string
(&rest body
)
1564 "Execute BODY, return the text it sent to `standard-output', as a string."
1565 `(let ((standard-output
1566 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1567 (let ((standard-output standard-output
))
1569 (with-current-buffer standard-output
1572 (kill-buffer nil
)))))
1574 (defmacro with-local-quit
(&rest body
)
1575 "Execute BODY with `inhibit-quit' temporarily bound to nil."
1576 `(condition-case nil
1577 (let ((inhibit-quit nil
))
1579 (quit (setq quit-flag t
))))
1581 (defmacro combine-after-change-calls
(&rest body
)
1582 "Execute BODY, but don't call the after-change functions till the end.
1583 If BODY makes changes in the buffer, they are recorded
1584 and the functions on `after-change-functions' are called several times
1585 when BODY is finished.
1586 The return value is the value of the last form in BODY.
1588 If `before-change-functions' is non-nil, then calls to the after-change
1589 functions can't be deferred, so in that case this macro has no effect.
1591 Do not alter `after-change-functions' or `before-change-functions'
1594 (let ((combine-after-change-calls t
))
1596 (combine-after-change-execute)))
1599 (defvar delay-mode-hooks nil
1600 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1601 (defvar delayed-mode-hooks nil
1602 "List of delayed mode hooks waiting to be run.")
1603 (make-variable-buffer-local 'delayed-mode-hooks
)
1605 (defun run-mode-hooks (&rest hooks
)
1606 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1607 Execution is delayed if `delay-mode-hooks' is non-nil.
1608 Major mode functions should use this."
1609 (if delay-mode-hooks
1611 (dolist (hook hooks
)
1612 (push hook delayed-mode-hooks
))
1613 ;; Normal case, just run the hook as before plus any delayed hooks.
1614 (setq hooks
(nconc (nreverse delayed-mode-hooks
) hooks
))
1615 (setq delayed-mode-hooks nil
)
1616 (apply 'run-hooks hooks
)))
1618 (defmacro delay-mode-hooks
(&rest body
)
1619 "Execute BODY, but delay any `run-mode-hooks'.
1620 Only affects hooks run in the current buffer."
1622 (make-local-variable 'delay-mode-hooks
)
1623 (let ((delay-mode-hooks t
))
1626 ;; PUBLIC: find if the current mode derives from another.
1628 (defun derived-mode-p (&rest modes
)
1629 "Non-nil if the current major mode is derived from one of MODES.
1630 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1631 (let ((parent major-mode
))
1632 (while (and (not (memq parent modes
))
1633 (setq parent
(get parent
'derived-mode-parent
))))
1636 (defmacro with-syntax-table
(table &rest body
)
1637 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1638 The syntax table of the current buffer is saved, BODY is evaluated, and the
1639 saved table is restored, even in case of an abnormal exit.
1640 Value is what BODY returns."
1641 (let ((old-table (make-symbol "table"))
1642 (old-buffer (make-symbol "buffer")))
1643 `(let ((,old-table
(syntax-table))
1644 (,old-buffer
(current-buffer)))
1647 (set-syntax-table (copy-syntax-table ,table
))
1649 (save-current-buffer
1650 (set-buffer ,old-buffer
)
1651 (set-syntax-table ,old-table
))))))
1653 ;;; Matching and substitution
1655 (defvar save-match-data-internal
)
1657 ;; We use save-match-data-internal as the local variable because
1658 ;; that works ok in practice (people should not use that variable elsewhere).
1659 ;; We used to use an uninterned symbol; the compiler handles that properly
1660 ;; now, but it generates slower code.
1661 (defmacro save-match-data
(&rest body
)
1662 "Execute the BODY forms, restoring the global value of the match data.
1663 The value returned is the value of the last form in BODY."
1664 ;; It is better not to use backquote here,
1665 ;; because that makes a bootstrapping problem
1666 ;; if you need to recompile all the Lisp files using interpreted code.
1668 '((save-match-data-internal (match-data)))
1669 (list 'unwind-protect
1671 '(set-match-data save-match-data-internal
))))
1673 (defun match-string (num &optional string
)
1674 "Return string of text matched by last search.
1675 NUM specifies which parenthesized expression in the last regexp.
1676 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1677 Zero means the entire text matched by the whole regexp or whole string.
1678 STRING should be given if the last search was by `string-match' on STRING."
1679 (if (match-beginning num
)
1681 (substring string
(match-beginning num
) (match-end num
))
1682 (buffer-substring (match-beginning num
) (match-end num
)))))
1684 (defun match-string-no-properties (num &optional string
)
1685 "Return string of text matched by last search, without text properties.
1686 NUM specifies which parenthesized expression in the last regexp.
1687 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1688 Zero means the entire text matched by the whole regexp or whole string.
1689 STRING should be given if the last search was by `string-match' on STRING."
1690 (if (match-beginning num
)
1693 (substring string
(match-beginning num
) (match-end num
))))
1694 (set-text-properties 0 (length result
) nil result
)
1696 (buffer-substring-no-properties (match-beginning num
)
1699 (defun split-string (string &optional separators
)
1700 "Splits STRING into substrings where there are matches for SEPARATORS.
1701 Each match for SEPARATORS is a splitting point.
1702 The substrings between the splitting points are made into a list
1704 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1706 If there is match for SEPARATORS at the beginning of STRING, we do not
1707 include a null substring for that. Likewise, if there is a match
1708 at the end of STRING, we don't include a null substring for that.
1710 Modifies the match data; use `save-match-data' if necessary."
1711 (let ((rexp (or separators
"[ \f\t\n\r\v]+"))
1715 (while (and (string-match rexp string
1717 (= start
(match-beginning 0))
1718 (< start
(length string
)))
1720 (< (match-beginning 0) (length string
)))
1722 (or (eq (match-beginning 0) 0)
1723 (and (eq (match-beginning 0) (match-end 0))
1724 (eq (match-beginning 0) start
))
1726 (cons (substring string start
(match-beginning 0))
1728 (setq start
(match-end 0)))
1729 (or (eq start
(length string
))
1731 (cons (substring string start
)
1735 (defun subst-char-in-string (fromchar tochar string
&optional inplace
)
1736 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1737 Unless optional argument INPLACE is non-nil, return a new string."
1738 (let ((i (length string
))
1739 (newstr (if inplace string
(copy-sequence string
))))
1742 (if (eq (aref newstr i
) fromchar
)
1743 (aset newstr i tochar
)))
1746 (defun replace-regexp-in-string (regexp rep string
&optional
1747 fixedcase literal subexp start
)
1748 "Replace all matches for REGEXP with REP in STRING.
1750 Return a new string containing the replacements.
1752 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1753 arguments with the same names of function `replace-match'. If START
1754 is non-nil, start replacements at that index in STRING.
1756 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1757 function. If it is a function it is applied to each match to generate
1758 the replacement passed to `replace-match'; the match-data at this
1759 point are such that match 0 is the function's argument.
1761 To replace only the first match (if any), make REGEXP match up to \\'
1762 and replace a sub-expression, e.g.
1763 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
1767 ;; To avoid excessive consing from multiple matches in long strings,
1768 ;; don't just call `replace-match' continually. Walk down the
1769 ;; string looking for matches of REGEXP and building up a (reversed)
1770 ;; list MATCHES. This comprises segments of STRING which weren't
1771 ;; matched interspersed with replacements for segments that were.
1772 ;; [For a `large' number of replacements it's more efficient to
1773 ;; operate in a temporary buffer; we can't tell from the function's
1774 ;; args whether to choose the buffer-based implementation, though it
1775 ;; might be reasonable to do so for long enough STRING.]
1776 (let ((l (length string
))
1777 (start (or start
0))
1780 (while (and (< start l
) (string-match regexp string start
))
1781 (setq mb
(match-beginning 0)
1783 ;; If we matched the empty string, make sure we advance by one char
1784 (when (= me mb
) (setq me
(min l
(1+ mb
))))
1785 ;; Generate a replacement for the matched substring.
1786 ;; Operate only on the substring to minimize string consing.
1787 ;; Set up match data for the substring for replacement;
1788 ;; presumably this is likely to be faster than munging the
1789 ;; match data directly in Lisp.
1790 (string-match regexp
(setq str
(substring string mb me
)))
1792 (cons (replace-match (if (stringp rep
)
1794 (funcall rep
(match-string 0 str
)))
1795 fixedcase literal str subexp
)
1796 (cons (substring string start mb
) ; unmatched prefix
1799 ;; Reconstruct a string from the pieces.
1800 (setq matches
(cons (substring string start l
) matches
)) ; leftover
1801 (apply #'concat
(nreverse matches
)))))
1803 (defun shell-quote-argument (argument)
1804 "Quote an argument for passing as argument to an inferior shell."
1805 (if (eq system-type
'ms-dos
)
1806 ;; Quote using double quotes, but escape any existing quotes in
1807 ;; the argument with backslashes.
1811 (if (or (null (string-match "[^\"]" argument
))
1812 (< (match-end 0) (length argument
)))
1813 (while (string-match "[\"]" argument start
)
1814 (setq end
(match-beginning 0)
1815 result
(concat result
(substring argument start end
)
1816 "\\" (substring argument end
(1+ end
)))
1818 (concat "\"" result
(substring argument start
) "\""))
1819 (if (eq system-type
'windows-nt
)
1820 (concat "\"" argument
"\"")
1821 (if (equal argument
"")
1823 ;; Quote everything except POSIX filename characters.
1824 ;; This should be safe enough even for really weird shells.
1825 (let ((result "") (start 0) end
)
1826 (while (string-match "[^-0-9a-zA-Z_./]" argument start
)
1827 (setq end
(match-beginning 0)
1828 result
(concat result
(substring argument start end
)
1829 "\\" (substring argument end
(1+ end
)))
1831 (concat result
(substring argument start
)))))))
1833 (defun make-syntax-table (&optional oldtable
)
1834 "Return a new syntax table.
1835 Create a syntax table which inherits from OLDTABLE (if non-nil) or
1836 from `standard-syntax-table' otherwise."
1837 (let ((table (make-char-table 'syntax-table nil
)))
1838 (set-char-table-parent table
(or oldtable
(standard-syntax-table)))
1841 (defun add-to-invisibility-spec (arg)
1842 "Add elements to `buffer-invisibility-spec'.
1843 See documentation for `buffer-invisibility-spec' for the kind of elements
1846 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
1847 (setq buffer-invisibility-spec
(list arg
)))
1849 (setq buffer-invisibility-spec
1850 (cons arg buffer-invisibility-spec
)))))
1852 (defun remove-from-invisibility-spec (arg)
1853 "Remove elements from `buffer-invisibility-spec'."
1854 (if (consp buffer-invisibility-spec
)
1855 (setq buffer-invisibility-spec
(delete arg buffer-invisibility-spec
))))
1857 (defun global-set-key (key command
)
1858 "Give KEY a global binding as COMMAND.
1859 COMMAND is the command definition to use; usually it is
1860 a symbol naming an interactively-callable function.
1861 KEY is a key sequence; noninteractively, it is a string or vector
1862 of characters or event types, and non-ASCII characters with codes
1863 above 127 (such as ISO Latin-1) can be included if you use a vector.
1865 Note that if KEY has a local binding in the current buffer,
1866 that local binding will continue to shadow any global binding
1867 that you make with this function."
1868 (interactive "KSet key globally: \nCSet key %s to command: ")
1869 (or (vectorp key
) (stringp key
)
1870 (signal 'wrong-type-argument
(list 'arrayp key
)))
1871 (define-key (current-global-map) key command
))
1873 (defun local-set-key (key command
)
1874 "Give KEY a local binding as COMMAND.
1875 COMMAND is the command definition to use; usually it is
1876 a symbol naming an interactively-callable function.
1877 KEY is a key sequence; noninteractively, it is a string or vector
1878 of characters or event types, and non-ASCII characters with codes
1879 above 127 (such as ISO Latin-1) can be included if you use a vector.
1881 The binding goes in the current buffer's local map,
1882 which in most cases is shared with all other buffers in the same major mode."
1883 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1884 (let ((map (current-local-map)))
1886 (use-local-map (setq map
(make-sparse-keymap))))
1887 (or (vectorp key
) (stringp key
)
1888 (signal 'wrong-type-argument
(list 'arrayp key
)))
1889 (define-key map key command
)))
1891 (defun global-unset-key (key)
1892 "Remove global binding of KEY.
1893 KEY is a string representing a sequence of keystrokes."
1894 (interactive "kUnset key globally: ")
1895 (global-set-key key nil
))
1897 (defun local-unset-key (key)
1898 "Remove local binding of KEY.
1899 KEY is a string representing a sequence of keystrokes."
1900 (interactive "kUnset key locally: ")
1901 (if (current-local-map)
1902 (local-set-key key nil
))
1905 ;; We put this here instead of in frame.el so that it's defined even on
1906 ;; systems where frame.el isn't loaded.
1907 (defun frame-configuration-p (object)
1908 "Return non-nil if OBJECT seems to be a frame configuration.
1909 Any list whose car is `frame-configuration' is assumed to be a frame
1912 (eq (car object
) 'frame-configuration
)))
1914 (defun functionp (object)
1915 "Non-nil iff OBJECT is a type of object that can be called as a function."
1916 (or (and (symbolp object
) (fboundp object
)
1918 (setq object
(indirect-function object
))
1920 (eq (car-safe object
) 'autoload
)
1921 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object
)))))))
1922 (subrp object
) (byte-code-function-p object
)
1923 (eq (car-safe object
) 'lambda
)))
1925 (defun interactive-form (function)
1926 "Return the interactive form of FUNCTION.
1927 If function is a command (see `commandp'), value is a list of the form
1928 \(interactive SPEC). If function is not a command, return nil."
1929 (setq function
(indirect-function function
))
1930 (when (commandp function
)
1931 (cond ((byte-code-function-p function
)
1932 (when (> (length function
) 5)
1933 (let ((spec (aref function
5)))
1935 (list 'interactive spec
)
1936 (list 'interactive
)))))
1938 (subr-interactive-form function
))
1939 ((eq (car-safe function
) 'lambda
)
1940 (setq function
(cddr function
))
1941 (when (stringp (car function
))
1942 (setq function
(cdr function
)))
1943 (let ((form (car function
)))
1944 (when (eq (car-safe form
) 'interactive
)
1945 (copy-sequence form
)))))))
1947 (defun assq-delete-all (key alist
)
1948 "Delete from ALIST all elements whose car is KEY.
1949 Return the modified alist."
1952 (if (eq (car (car tail
)) key
)
1953 (setq alist
(delq (car tail
) alist
)))
1954 (setq tail
(cdr tail
)))
1957 (defun make-temp-file (prefix &optional dir-flag suffix
)
1958 "Create a temporary file.
1959 The returned file name (created by appending some random characters at the end
1960 of PREFIX, and expanding against `temporary-file-directory' if necessary,
1961 is guaranteed to point to a newly created empty file.
1962 You can then use `write-region' to write new data into the file.
1964 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
1966 If SUFFIX is non-nil, add that at the end of the file name."
1968 (while (condition-case ()
1972 (expand-file-name prefix temporary-file-directory
)))
1974 (setq file
(concat file suffix
)))
1976 (make-directory file
)
1977 (write-region "" nil file nil
'silent nil
'excl
))
1979 (file-already-exists t
))
1980 ;; the file was somehow created by someone else between
1981 ;; `make-temp-name' and `write-region', let's try again.
1986 (defun add-minor-mode (toggle name
&optional keymap after toggle-fun
)
1987 "Register a new minor mode.
1989 This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
1991 TOGGLE is a symbol which is the name of a buffer-local variable that
1992 is toggled on or off to say whether the minor mode is active or not.
1994 NAME specifies what will appear in the mode line when the minor mode
1995 is active. NAME should be either a string starting with a space, or a
1996 symbol whose value is such a string.
1998 Optional KEYMAP is the keymap for the minor mode that will be added
1999 to `minor-mode-map-alist'.
2001 Optional AFTER specifies that TOGGLE should be added after AFTER
2002 in `minor-mode-alist'.
2004 Optional TOGGLE-FUN is an interactive function to toggle the mode.
2005 It defaults to (and should by convention be) TOGGLE.
2007 If TOGGLE has a non-nil `:included' property, an entry for the mode is
2008 included in the mode-line minor mode menu.
2009 If TOGGLE has a `:menu-tag', that is used for the menu item's label."
2010 (unless toggle-fun
(setq toggle-fun toggle
))
2011 ;; Add the name to the minor-mode-alist.
2013 (let ((existing (assq toggle minor-mode-alist
)))
2014 (when (and (stringp name
) (not (get-text-property 0 'local-map name
)))
2017 'local-map mode-line-minor-mode-keymap
2018 'help-echo
"mouse-3: minor mode menu")))
2020 (setcdr existing
(list name
))
2021 (let ((tail minor-mode-alist
) found
)
2022 (while (and tail
(not found
))
2023 (if (eq after
(caar tail
))
2025 (setq tail
(cdr tail
))))
2027 (let ((rest (cdr found
)))
2029 (nconc found
(list (list toggle name
)) rest
))
2030 (setq minor-mode-alist
(cons (list toggle name
)
2031 minor-mode-alist
)))))))
2032 ;; Add the toggle to the minor-modes menu if requested.
2033 (when (get toggle
:included
)
2034 (define-key mode-line-mode-menu
2038 (or (get toggle
:menu-tag
)
2039 (if (stringp name
) name
(symbol-name toggle
)))
2040 (let ((mode-name (if (stringp name
) name
2041 (if (symbolp name
) (symbol-value name
)))))
2043 (concat " (" mode-name
")"))))
2045 :button
(cons :toggle toggle
))))
2047 ;; Add the map to the minor-mode-map-alist.
2049 (let ((existing (assq toggle minor-mode-map-alist
)))
2051 (setcdr existing keymap
)
2052 (let ((tail minor-mode-map-alist
) found
)
2053 (while (and tail
(not found
))
2054 (if (eq after
(caar tail
))
2056 (setq tail
(cdr tail
))))
2058 (let ((rest (cdr found
)))
2060 (nconc found
(list (cons toggle keymap
)) rest
))
2061 (setq minor-mode-map-alist
(cons (cons toggle keymap
)
2062 minor-mode-map-alist
))))))))
2064 ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2066 (defun text-clone-maintain (ol1 after beg end
&optional len
)
2067 "Propagate the changes made under the overlay OL1 to the other clones.
2068 This is used on the `modification-hooks' property of text clones."
2069 (when (and after
(not undo-in-progress
) (overlay-start ol1
))
2070 (let ((margin (if (overlay-get ol1
'text-clone-spreadp
) 1 0)))
2071 (setq beg
(max beg
(+ (overlay-start ol1
) margin
)))
2072 (setq end
(min end
(- (overlay-end ol1
) margin
)))
2075 (when (overlay-get ol1
'text-clone-syntax
)
2076 ;; Check content of the clone's text.
2077 (let ((cbeg (+ (overlay-start ol1
) margin
))
2078 (cend (- (overlay-end ol1
) margin
)))
2081 (if (not (re-search-forward
2082 (overlay-get ol1
'text-clone-syntax
) cend t
))
2083 ;; Mark the overlay for deletion.
2084 (overlay-put ol1
'text-clones nil
)
2085 (when (< (match-end 0) cend
)
2086 ;; Shrink the clone at its end.
2087 (setq end
(min end
(match-end 0)))
2088 (move-overlay ol1
(overlay-start ol1
)
2089 (+ (match-end 0) margin
)))
2090 (when (> (match-beginning 0) cbeg
)
2091 ;; Shrink the clone at its beginning.
2092 (setq beg
(max (match-beginning 0) beg
))
2093 (move-overlay ol1
(- (match-beginning 0) margin
)
2094 (overlay-end ol1
)))))))
2095 ;; Now go ahead and update the clones.
2096 (let ((head (- beg
(overlay-start ol1
)))
2097 (tail (- (overlay-end ol1
) end
))
2098 (str (buffer-substring beg end
))
2100 (inhibit-modification-hooks t
))
2101 (dolist (ol2 (overlay-get ol1
'text-clones
))
2102 (let ((oe (overlay-end ol2
)))
2103 (unless (or (eq ol1 ol2
) (null oe
))
2104 (setq nothing-left nil
)
2105 (let ((mod-beg (+ (overlay-start ol2
) head
)))
2106 ;;(overlay-put ol2 'modification-hooks nil)
2107 (goto-char (- (overlay-end ol2
) tail
))
2108 (unless (> mod-beg
(point))
2109 (save-excursion (insert str
))
2110 (delete-region mod-beg
(point)))
2111 ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain))
2113 (if nothing-left
(delete-overlay ol1
))))))))
2115 (defun text-clone-create (start end
&optional spreadp syntax
)
2116 "Create a text clone of START...END at point.
2117 Text clones are chunks of text that are automatically kept identical:
2118 changes done to one of the clones will be immediately propagated to the other.
2120 The buffer's content at point is assumed to be already identical to
2121 the one between START and END.
2122 If SYNTAX is provided it's a regexp that describes the possible text of
2123 the clones; the clone will be shrunk or killed if necessary to ensure that
2124 its text matches the regexp.
2125 If SPREADP is non-nil it indicates that text inserted before/after the
2126 clone should be incorporated in the clone."
2127 ;; To deal with SPREADP we can either use an overlay with `nil t' along
2128 ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay
2129 ;; (with a one-char margin at each end) with `t nil'.
2130 ;; We opted for a larger overlay because it behaves better in the case
2131 ;; where the clone is reduced to the empty string (we want the overlay to
2132 ;; stay when the clone's content is the empty string and we want to use
2133 ;; `evaporate' to make sure those overlays get deleted when needed).
2135 (let* ((pt-end (+ (point) (- end start
)))
2136 (start-margin (if (or (not spreadp
) (bobp) (<= start
(point-min)))
2138 (end-margin (if (or (not spreadp
)
2139 (>= pt-end
(point-max))
2140 (>= start
(point-max)))
2142 (ol1 (make-overlay (- start start-margin
) (+ end end-margin
) nil t
))
2143 (ol2 (make-overlay (- (point) start-margin
) (+ pt-end end-margin
) nil t
))
2144 (dups (list ol1 ol2
)))
2145 (overlay-put ol1
'modification-hooks
'(text-clone-maintain))
2146 (when spreadp
(overlay-put ol1
'text-clone-spreadp t
))
2147 (when syntax
(overlay-put ol1
'text-clone-syntax syntax
))
2148 ;;(overlay-put ol1 'face 'underline)
2149 (overlay-put ol1
'evaporate t
)
2150 (overlay-put ol1
'text-clones dups
)
2152 (overlay-put ol2
'modification-hooks
'(text-clone-maintain))
2153 (when spreadp
(overlay-put ol2
'text-clone-spreadp t
))
2154 (when syntax
(overlay-put ol2
'text-clone-syntax syntax
))
2155 ;;(overlay-put ol2 'face 'underline)
2156 (overlay-put ol2
'evaporate t
)
2157 (overlay-put ol2
'text-clones dups
)))
2159 (defun play-sound (sound)
2160 "SOUND is a list of the form `(sound KEYWORD VALUE...)'.
2161 The following keywords are recognized:
2163 :file FILE - read sound data from FILE. If FILE isn't an
2164 absolute file name, it is searched in `data-directory'.
2166 :data DATA - read sound data from string DATA.
2168 Exactly one of :file or :data must be present.
2170 :volume VOL - set volume to VOL. VOL must an integer in the
2171 range 0..100 or a float in the range 0..1.0. If not specified,
2172 don't change the volume setting of the sound device.
2174 :device DEVICE - play sound on DEVICE. If not specified,
2175 a system-dependent default device name is used."
2176 (unless (fboundp 'play-sound-internal
)
2177 (error "This Emacs binary lacks sound support"))
2178 (play-sound-internal sound
))
2180 ;;; subr.el ends here