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
94 (list 'setq listname
(list 'cdr listname
)))))
96 (defmacro when
(cond &rest body
)
97 "If COND yields non-nil, do BODY, else return nil."
98 (list 'if cond
(cons 'progn body
)))
100 (defmacro unless
(cond &rest body
)
101 "If COND yields nil, do BODY, else return nil."
102 (cons 'if
(cons cond
(cons nil body
))))
104 (defmacro dolist
(spec &rest body
)
105 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
106 Evaluate BODY with VAR bound to each car from LIST, in turn.
107 Then evaluate RESULT to get return value, default nil."
108 (let ((temp (make-symbol "--dolist-temp--")))
109 (list 'let
(list (list temp
(nth 1 spec
)) (car spec
))
111 (list 'setq
(car spec
) (list 'car temp
))
114 (list (list 'setq temp
(list 'cdr temp
))))))
117 (cons (list 'setq
(car spec
) nil
) (cdr (cdr spec
))))))))
119 (defmacro dotimes
(spec &rest body
)
120 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
121 Evaluate BODY with VAR bound to successive integers running from 0,
122 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
123 the return value (nil if RESULT is omitted)."
124 (let ((temp (make-symbol "--dotimes-temp--")))
125 (list 'let
(list (list temp
(nth 1 spec
)) (list (car spec
) 0))
126 (list 'while
(list '< (car spec
) temp
)
128 (append body
(list (list 'setq
(car spec
)
129 (list '1+ (car spec
)))))))
131 (car (cdr (cdr spec
)))
135 "Return the car of the car of X."
139 "Return the car of the cdr of X."
143 "Return the cdr of the car of X."
147 "Return the cdr of the cdr of X."
150 (defun last (x &optional n
)
151 "Return the last link of the list X. Its car is the last element.
152 If X is nil, return nil.
153 If N is non-nil, return the Nth-to-last link of X.
154 If N is bigger than the length of X, return X."
158 (setq m
(1+ m
) p
(cdr p
)))
160 (if (< n m
) (nthcdr (- m n
) x
) x
)))
161 (while (consp (cdr x
))
165 (defun butlast (x &optional n
)
166 "Returns a copy of LIST with the last N elements removed."
167 (if (and n
(<= n
0)) x
168 (nbutlast (copy-sequence x
) n
)))
170 (defun nbutlast (x &optional n
)
171 "Modifies LIST to remove the last N elements."
172 (let ((m (length x
)))
176 (if (> n
0) (setcdr (nthcdr (- (1- m
) n
) x
) nil
))
179 (defun remove (elt seq
)
180 "Return a copy of SEQ with all occurrences of ELT removed.
181 SEQ must be a list, vector, or string. The comparison is done with `equal'."
183 ;; If SEQ isn't a list, there's no need to copy SEQ because
184 ;; `delete' will return a new object.
186 (delete elt
(copy-sequence seq
))))
188 (defun remq (elt list
)
189 "Return a copy of LIST with all occurrences of ELT removed.
190 The comparison is done with `eq'."
192 (delq elt
(copy-sequence list
))
195 (defun copy-tree (tree &optional vecp
)
196 "Make a copy of TREE.
197 If TREE is a cons cell, this recursively copies both its car and its cdr.
198 Contrast to `copy-sequence', which copies only along the cdrs. With second
199 argument VECP, this copies vectors as well as conses."
203 (let ((newcar (car tree
)))
204 (if (or (consp (car tree
)) (and vecp
(vectorp (car tree
))))
205 (setq newcar
(copy-tree (car tree
) vecp
)))
206 (push newcar result
))
207 (setq tree
(cdr tree
)))
208 (nconc (nreverse result
) tree
))
209 (if (and vecp
(vectorp tree
))
210 (let ((i (length (setq tree
(copy-sequence tree
)))))
211 (while (>= (setq i
(1- i
)) 0)
212 (aset tree i
(copy-tree (aref tree i
) vecp
)))
216 (defun assoc-default (key alist
&optional test default
)
217 "Find object KEY in a pseudo-alist ALIST.
218 ALIST is a list of conses or objects. Each element (or the element's car,
219 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
220 If that is non-nil, the element matches;
221 then `assoc-default' returns the element's cdr, if it is a cons,
222 or DEFAULT if the element is not a cons.
224 If no element matches, the value is nil.
225 If TEST is omitted or nil, `equal' is used."
226 (let (found (tail alist
) value
)
227 (while (and tail
(not found
))
228 (let ((elt (car tail
)))
229 (when (funcall (or test
'equal
) (if (consp elt
) (car elt
) elt
) key
)
230 (setq found t value
(if (consp elt
) (cdr elt
) default
))))
231 (setq tail
(cdr tail
)))
234 (defun assoc-ignore-case (key alist
)
235 "Like `assoc', but ignores differences in case and text representation.
236 KEY must be a string. Upper-case and lower-case letters are treated as equal.
237 Unibyte strings are converted to multibyte for comparison."
239 (while (and alist
(not element
))
240 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil t
))
241 (setq element
(car alist
)))
242 (setq alist
(cdr alist
)))
245 (defun assoc-ignore-representation (key alist
)
246 "Like `assoc', but ignores differences in text representation.
247 KEY must be a string.
248 Unibyte strings are converted to multibyte for comparison."
250 (while (and alist
(not element
))
251 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil
))
252 (setq element
(car alist
)))
253 (setq alist
(cdr alist
)))
256 (defun member-ignore-case (elt list
)
257 "Like `member', but ignores differences in case and text representation.
258 ELT must be a string. Upper-case and lower-case letters are treated as equal.
259 Unibyte strings are converted to multibyte for comparison.
260 Non-strings in LIST are ignored."
262 (not (and (stringp (car list
))
263 (eq t
(compare-strings elt
0 nil
(car list
) 0 nil t
)))))
264 (setq list
(cdr list
)))
274 ;Prevent the \{...} documentation construct
275 ;from mentioning keys that run this command.
276 (put 'undefined
'suppress-keymap t
)
278 (defun suppress-keymap (map &optional nodigits
)
279 "Make MAP override all normally self-inserting keys to be undefined.
280 Normally, as an exception, digits and minus-sign are set to make prefix args,
281 but optional second arg NODIGITS non-nil treats them like other chars."
282 (define-key map
[remap self-insert-command
] 'undefined
)
285 (define-key map
"-" 'negative-argument
)
286 ;; Make plain numbers do numeric args.
289 (define-key map
(char-to-string loop
) 'digit-argument
)
290 (setq loop
(1+ loop
))))))
293 ;(defun copy-keymap (keymap)
294 ; "Return a copy of KEYMAP"
295 ; (while (not (keymapp keymap))
296 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
297 ; (if (vectorp keymap)
298 ; (copy-sequence keymap)
299 ; (copy-alist keymap)))
301 (defvar key-substitution-in-progress nil
302 "Used internally by substitute-key-definition.")
304 (defun substitute-key-definition (olddef newdef keymap
&optional oldmap prefix
)
305 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
306 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
307 Alternatively, if optional fourth argument OLDMAP is specified, we redefine
308 in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP."
309 ;; Don't document PREFIX in the doc string because we don't want to
310 ;; advertise it. It's meant for recursive calls only. Here's its
313 ;; If optional argument PREFIX is specified, it should be a key
314 ;; prefix, a string. Redefined bindings will then be bound to the
315 ;; original key, with PREFIX added at the front.
316 (or prefix
(setq prefix
""))
317 (let* ((scan (or oldmap keymap
))
319 (prefix1 (vconcat prefix vec1
))
320 (key-substitution-in-progress
321 (cons scan key-substitution-in-progress
)))
322 ;; Scan OLDMAP, finding each char or event-symbol that
323 ;; has any definition, and act on it with hack-key.
325 (if (consp (car scan
))
326 (let ((char (car (car scan
)))
327 (defn (cdr (car scan
))))
328 ;; The inside of this let duplicates exactly
329 ;; the inside of the following let that handles array elements.
331 (aset prefix1
(length prefix
) char
)
332 (let (inner-def skipped
)
333 ;; Skip past menu-prompt.
334 (while (stringp (car-safe defn
))
335 (setq skipped
(cons (car defn
) skipped
))
336 (setq defn
(cdr defn
)))
337 ;; Skip past cached key-equivalence data for menu items.
338 (and (consp defn
) (consp (car defn
))
339 (setq defn
(cdr defn
)))
340 (setq inner-def defn
)
341 ;; Look past a symbol that names a keymap.
342 (while (and (symbolp inner-def
)
344 (setq inner-def
(symbol-function inner-def
)))
345 (if (or (eq defn olddef
)
346 ;; Compare with equal if definition is a key sequence.
347 ;; That is useful for operating on function-key-map.
348 (and (or (stringp defn
) (vectorp defn
))
349 (equal defn olddef
)))
350 (define-key keymap prefix1
(nconc (nreverse skipped
) newdef
))
351 (if (and (keymapp defn
)
352 ;; Avoid recursively scanning
353 ;; where KEYMAP does not have a submap.
354 (let ((elt (lookup-key keymap prefix1
)))
357 ;; Avoid recursively rescanning keymap being scanned.
359 key-substitution-in-progress
)))
360 ;; If this one isn't being scanned already,
362 (substitute-key-definition olddef newdef keymap
365 (if (vectorp (car scan
))
366 (let* ((array (car scan
))
370 (let ((char i
) (defn (aref array i
)))
371 ;; The inside of this let duplicates exactly
372 ;; the inside of the previous let.
374 (aset prefix1
(length prefix
) char
)
375 (let (inner-def skipped
)
376 ;; Skip past menu-prompt.
377 (while (stringp (car-safe defn
))
378 (setq skipped
(cons (car defn
) skipped
))
379 (setq defn
(cdr defn
)))
380 (and (consp defn
) (consp (car defn
))
381 (setq defn
(cdr defn
)))
382 (setq inner-def defn
)
383 (while (and (symbolp inner-def
)
385 (setq inner-def
(symbol-function inner-def
)))
386 (if (or (eq defn olddef
)
387 (and (or (stringp defn
) (vectorp defn
))
388 (equal defn olddef
)))
389 (define-key keymap prefix1
390 (nconc (nreverse skipped
) newdef
))
391 (if (and (keymapp defn
)
392 (let ((elt (lookup-key keymap prefix1
)))
396 key-substitution-in-progress
)))
397 (substitute-key-definition olddef newdef keymap
401 (if (char-table-p (car scan
))
403 (function (lambda (char defn
)
405 ;; The inside of this let duplicates exactly
406 ;; the inside of the previous let,
407 ;; except that it uses set-char-table-range
408 ;; instead of define-key.
410 (aset prefix1
(length prefix
) char
)
411 (let (inner-def skipped
)
412 ;; Skip past menu-prompt.
413 (while (stringp (car-safe defn
))
414 (setq skipped
(cons (car defn
) skipped
))
415 (setq defn
(cdr defn
)))
416 (and (consp defn
) (consp (car defn
))
417 (setq defn
(cdr defn
)))
418 (setq inner-def defn
)
419 (while (and (symbolp inner-def
)
421 (setq inner-def
(symbol-function inner-def
)))
422 (if (or (eq defn olddef
)
423 (and (or (stringp defn
) (vectorp defn
))
424 (equal defn olddef
)))
425 (define-key keymap prefix1
426 (nconc (nreverse skipped
) newdef
))
427 (if (and (keymapp defn
)
428 (let ((elt (lookup-key keymap prefix1
)))
432 key-substitution-in-progress
)))
433 (substitute-key-definition olddef newdef keymap
437 (setq scan
(cdr scan
)))))
439 (defun define-key-after (keymap key definition
&optional after
)
440 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
441 This is like `define-key' except that the binding for KEY is placed
442 just after the binding for the event AFTER, instead of at the beginning
443 of the map. Note that AFTER must be an event type (like KEY), NOT a command
446 If AFTER is t or omitted, the new binding goes at the end of the keymap.
447 AFTER should be a single event type--a symbol or a character, not a sequence.
449 Bindings are always added before any inherited map.
451 The order of bindings in a keymap matters when it is used as a menu."
452 (unless after
(setq after t
))
454 (signal 'wrong-type-argument
(list 'keymapp keymap
)))
456 (if (<= (length key
) 1) (aref key
0)
457 (setq keymap
(lookup-key keymap
459 (butlast (mapcar 'identity key
)))))
460 (aref key
(1- (length key
)))))
461 (let ((tail keymap
) done inserted
)
462 (while (and (not done
) tail
)
463 ;; Delete any earlier bindings for the same key.
464 (if (eq (car-safe (car (cdr tail
))) key
)
465 (setcdr tail
(cdr (cdr tail
))))
466 ;; If we hit an included map, go down that one.
467 (if (keymapp (car tail
)) (setq tail
(car tail
)))
468 ;; When we reach AFTER's binding, insert the new binding after.
469 ;; If we reach an inherited keymap, insert just before that.
470 ;; If we reach the end of this keymap, insert at the end.
471 (if (or (and (eq (car-safe (car tail
)) after
)
473 (eq (car (cdr tail
)) 'keymap
)
476 ;; Stop the scan only if we find a parent keymap.
477 ;; Keep going past the inserted element
478 ;; so we can delete any duplications that come later.
479 (if (eq (car (cdr tail
)) 'keymap
)
481 ;; Don't insert more than once.
483 (setcdr tail
(cons (cons key definition
) (cdr tail
))))
485 (setq tail
(cdr tail
)))))
489 "Convert KEYS to the internal Emacs key representation.
490 KEYS should be a string constant in the format used for
491 saving keyboard macros (see `insert-kbd-macro')."
492 (read-kbd-macro keys
))
494 (put 'keyboard-translate-table
'char-table-extra-slots
0)
496 (defun keyboard-translate (from to
)
497 "Translate character FROM to TO at a low level.
498 This function creates a `keyboard-translate-table' if necessary
499 and then modifies one entry in it."
500 (or (char-table-p keyboard-translate-table
)
501 (setq keyboard-translate-table
502 (make-char-table 'keyboard-translate-table nil
)))
503 (aset keyboard-translate-table from to
))
506 ;;;; The global keymap tree.
508 ;;; global-map, esc-map, and ctl-x-map have their values set up in
509 ;;; keymap.c; we just give them docstrings here.
511 (defvar global-map nil
512 "Default global keymap mapping Emacs keyboard input into commands.
513 The value is a keymap which is usually (but not necessarily) Emacs's
517 "Default keymap for ESC (meta) commands.
518 The normal global definition of the character ESC indirects to this keymap.")
520 (defvar ctl-x-map nil
521 "Default keymap for C-x commands.
522 The normal global definition of the character C-x indirects to this keymap.")
524 (defvar ctl-x-4-map
(make-sparse-keymap)
525 "Keymap for subcommands of C-x 4.")
526 (defalias 'ctl-x-4-prefix ctl-x-4-map
)
527 (define-key ctl-x-map
"4" 'ctl-x-4-prefix
)
529 (defvar ctl-x-5-map
(make-sparse-keymap)
530 "Keymap for frame commands.")
531 (defalias 'ctl-x-5-prefix ctl-x-5-map
)
532 (define-key ctl-x-map
"5" 'ctl-x-5-prefix
)
535 ;;;; Event manipulation functions.
537 ;; The call to `read' is to ensure that the value is computed at load time
538 ;; and not compiled into the .elc file. The value is negative on most
539 ;; machines, but not on all!
540 (defconst listify-key-sequence-1
(logior 128 (read "?\\M-\\^@")))
542 (defun listify-key-sequence (key)
543 "Convert a key sequence to a list of events."
546 (mapcar (function (lambda (c)
548 (logxor c listify-key-sequence-1
)
552 (defsubst eventp
(obj)
553 "True if the argument is an event object."
556 (get obj
'event-symbol-elements
))
559 (get (car obj
) 'event-symbol-elements
))))
561 (defun event-modifiers (event)
562 "Returns a list of symbols representing the modifier keys in event EVENT.
563 The elements of the list may include `meta', `control',
564 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
568 (setq type
(car type
)))
570 (cdr (get type
'event-symbol-elements
))
572 (or (zerop (logand type ?\M-\^
@))
573 (setq list
(cons 'meta list
)))
574 (or (and (zerop (logand type ?\C-\^
@))
575 (>= (logand type
127) 32))
576 (setq list
(cons 'control list
)))
577 (or (and (zerop (logand type ?\S-\^
@))
578 (= (logand type
255) (downcase (logand type
255))))
579 (setq list
(cons 'shift list
)))
580 (or (zerop (logand type ?\H-\^
@))
581 (setq list
(cons 'hyper list
)))
582 (or (zerop (logand type ?\s-\^
@))
583 (setq list
(cons 'super list
)))
584 (or (zerop (logand type ?\A-\^
@))
585 (setq list
(cons 'alt list
)))
588 (defun event-basic-type (event)
589 "Returns the basic type of the given event (all modifiers removed).
590 The value is a printing character (not upper case) or a symbol."
592 (setq event
(car event
)))
594 (car (get event
'event-symbol-elements
))
595 (let ((base (logand event
(1- (lsh 1 18)))))
596 (downcase (if (< base
32) (logior base
64) base
)))))
598 (defsubst mouse-movement-p
(object)
599 "Return non-nil if OBJECT is a mouse movement event."
601 (eq (car object
) 'mouse-movement
)))
603 (defsubst event-start
(event)
604 "Return the starting position of EVENT.
605 If EVENT is a mouse press or a mouse click, this returns the location
607 If EVENT is a drag, this returns the drag's starting position.
608 The return value is of the form
609 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
610 The `posn-' functions access elements of such lists."
611 (if (consp event
) (nth 1 event
)
612 (list (selected-window) (point) '(0 .
0) 0)))
614 (defsubst event-end
(event)
615 "Return the ending location of EVENT. EVENT should be a click or drag event.
616 If EVENT is a click event, this function is the same as `event-start'.
617 The return value is of the form
618 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
619 The `posn-' functions access elements of such lists."
620 (if (consp event
) (nth (if (consp (nth 2 event
)) 2 1) event
)
621 (list (selected-window) (point) '(0 .
0) 0)))
623 (defsubst event-click-count
(event)
624 "Return the multi-click count of EVENT, a click or drag event.
625 The return value is a positive integer."
626 (if (and (consp event
) (integerp (nth 2 event
))) (nth 2 event
) 1))
628 (defsubst posn-window
(position)
629 "Return the window in POSITION.
630 POSITION should be a list of the form
631 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
632 as returned by the `event-start' and `event-end' functions."
635 (defsubst posn-point
(position)
636 "Return the buffer location in POSITION.
637 POSITION should be a list of the form
638 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
639 as returned by the `event-start' and `event-end' functions."
640 (if (consp (nth 1 position
))
641 (car (nth 1 position
))
644 (defsubst posn-x-y
(position)
645 "Return the x and y coordinates in POSITION.
646 POSITION should be a list of the form
647 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
648 as returned by the `event-start' and `event-end' functions."
651 (defun posn-col-row (position)
652 "Return the column and row in POSITION, measured in characters.
653 POSITION should be a list of the form
654 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
655 as returned by the `event-start' and `event-end' functions.
656 For a scroll-bar event, the result column is 0, and the row
657 corresponds to the vertical position of the click in the scroll bar."
658 (let ((pair (nth 2 position
))
659 (window (posn-window position
)))
660 (if (eq (if (consp (nth 1 position
))
661 (car (nth 1 position
))
663 'vertical-scroll-bar
)
664 (cons 0 (scroll-bar-scale pair
(1- (window-height window
))))
665 (if (eq (if (consp (nth 1 position
))
666 (car (nth 1 position
))
668 'horizontal-scroll-bar
)
669 (cons (scroll-bar-scale pair
(window-width window
)) 0)
670 (let* ((frame (if (framep window
) window
(window-frame window
)))
671 (x (/ (car pair
) (frame-char-width frame
)))
672 (y (/ (cdr pair
) (frame-char-height frame
))))
675 (defsubst posn-timestamp
(position)
676 "Return the timestamp of POSITION.
677 POSITION should be a list of the form
678 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
679 as returned by the `event-start' and `event-end' functions."
683 ;;;; Obsolescent names for functions.
685 (defalias 'dot
'point
)
686 (defalias 'dot-marker
'point-marker
)
687 (defalias 'dot-min
'point-min
)
688 (defalias 'dot-max
'point-max
)
689 (defalias 'window-dot
'window-point
)
690 (defalias 'set-window-dot
'set-window-point
)
691 (defalias 'read-input
'read-string
)
692 (defalias 'send-string
'process-send-string
)
693 (defalias 'send-region
'process-send-region
)
694 (defalias 'show-buffer
'set-window-buffer
)
695 (defalias 'buffer-flush-undo
'buffer-disable-undo
)
696 (defalias 'eval-current-buffer
'eval-buffer
)
697 (defalias 'compiled-function-p
'byte-code-function-p
)
698 (defalias 'define-function
'defalias
)
700 (defalias 'sref
'aref
)
701 (make-obsolete 'sref
'aref
"20.4")
702 (make-obsolete 'char-bytes
"now always returns 1." "20.4")
703 (make-obsolete 'chars-in-region
"use (abs (- BEG END))." "20.3")
704 (make-obsolete 'dot
'point
"before 19.15")
705 (make-obsolete 'dot-max
'point-max
"before 19.15")
706 (make-obsolete 'dot-min
'point-min
"before 19.15")
707 (make-obsolete 'dot-marker
'point-marker
"before 19.15")
708 (make-obsolete 'buffer-flush-undo
'buffer-disable-undo
"before 19.15")
709 (make-obsolete 'baud-rate
"use the baud-rate variable instead." "before 19.15")
710 (make-obsolete 'compiled-function-p
'byte-code-function-p
"before 19.15")
711 (make-obsolete 'define-function
'defalias
"20.1")
713 (defun insert-string (&rest args
)
714 "Mocklisp-compatibility insert function.
715 Like the function `insert' except that any argument that is a number
716 is converted into a string by expressing it in decimal."
718 (insert (if (integerp el
) (number-to-string el
) el
))))
719 (make-obsolete 'insert-string
'insert
"21.4")
720 (defun makehash (&optional test
) (make-hash-table :test
(or test
'eql
)))
721 (make-obsolete 'makehash
'make-hash-table
"21.4")
723 ;; Some programs still use this as a function.
725 "Return the value of the `baud-rate' variable."
728 (defalias 'focus-frame
'ignore
)
729 (defalias 'unfocus-frame
'ignore
)
732 ;;;; Obsolescence declarations for variables.
734 (make-obsolete-variable 'directory-sep-char
"do not use it." "21.1")
735 (make-obsolete-variable 'mode-line-inverse-video
"use the appropriate faces instead." "21.1")
736 (make-obsolete-variable 'unread-command-char
737 "use `unread-command-events' instead. That variable is a list of events to reread, so it now uses nil to mean `no event', instead of -1."
739 (make-obsolete-variable 'executing-macro
'executing-kbd-macro
"before 19.34")
740 (make-obsolete-variable 'post-command-idle-hook
741 "use timers instead, with `run-with-idle-timer'." "before 19.34")
742 (make-obsolete-variable 'post-command-idle-delay
743 "use timers instead, with `run-with-idle-timer'." "before 19.34")
746 ;;;; Alternate names for functions - these are not being phased out.
748 (defalias 'string
= 'string-equal
)
749 (defalias 'string
< 'string-lessp
)
750 (defalias 'move-marker
'set-marker
)
751 (defalias 'rplaca
'setcar
)
752 (defalias 'rplacd
'setcdr
)
753 (defalias 'beep
'ding
) ;preserve lingual purity
754 (defalias 'indent-to-column
'indent-to
)
755 (defalias 'backward-delete-char
'delete-backward-char
)
756 (defalias 'search-forward-regexp
(symbol-function 're-search-forward
))
757 (defalias 'search-backward-regexp
(symbol-function 're-search-backward
))
758 (defalias 'int-to-string
'number-to-string
)
759 (defalias 'store-match-data
'set-match-data
)
760 (defalias 'make-variable-frame-localizable
'make-variable-frame-local
)
761 ;; These are the XEmacs names:
762 (defalias 'point-at-eol
'line-end-position
)
763 (defalias 'point-at-bol
'line-beginning-position
)
765 ;;; Should this be an obsolete name? If you decide it should, you get
766 ;;; to go through all the sources and change them.
767 (defalias 'string-to-int
'string-to-number
)
769 ;;;; Hook manipulation functions.
771 (defun make-local-hook (hook)
772 "Make the hook HOOK local to the current buffer.
773 The return value is HOOK.
775 You never need to call this function now that `add-hook' does it for you
776 if its LOCAL argument is non-nil.
778 When a hook is local, its local and global values
779 work in concert: running the hook actually runs all the hook
780 functions listed in *either* the local value *or* the global value
781 of the hook variable.
783 This function works by making t a member of the buffer-local value,
784 which acts as a flag to run the hook functions in the default value as
785 well. This works for all normal hooks, but does not work for most
786 non-normal hooks yet. We will be changing the callers of non-normal
787 hooks so that they can handle localness; this has to be done one by
790 This function does nothing if HOOK is already local in the current
793 Do not use `make-local-variable' to make a hook variable buffer-local."
794 (if (local-variable-p hook
)
796 (or (boundp hook
) (set hook nil
))
797 (make-local-variable hook
)
800 (make-obsolete 'make-local-hook
"not necessary any more." "21.1")
802 (defun add-hook (hook function
&optional append local
)
803 "Add to the value of HOOK the function FUNCTION.
804 FUNCTION is not added if already present.
805 FUNCTION is added (if necessary) at the beginning of the hook list
806 unless the optional argument APPEND is non-nil, in which case
807 FUNCTION is added at the end.
809 The optional fourth argument, LOCAL, if non-nil, says to modify
810 the hook's buffer-local value rather than its default value.
811 This makes the hook buffer-local if needed, and it makes t a member
812 of the buffer-local value. That acts as a flag to run the hook
813 functions in the default value as well as in the local value.
815 HOOK should be a symbol, and FUNCTION may be any valid function. If
816 HOOK is void, it is first set to nil. If HOOK's value is a single
817 function, it is changed to a list of functions."
818 (or (boundp hook
) (set hook nil
))
819 (or (default-boundp hook
) (set-default hook nil
))
820 (if local
(unless (local-variable-if-set-p hook
)
821 (set (make-local-variable hook
) (list t
)))
822 ;; Detect the case where make-local-variable was used on a hook
823 ;; and do what we used to do.
824 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
826 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
827 ;; If the hook value is a single function, turn it into a list.
828 (when (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
829 (setq hook-value
(list hook-value
)))
830 ;; Do the actual addition if necessary
831 (unless (member function hook-value
)
834 (append hook-value
(list function
))
835 (cons function hook-value
))))
836 ;; Set the actual variable
837 (if local
(set hook hook-value
) (set-default hook hook-value
))))
839 (defun remove-hook (hook function
&optional local
)
840 "Remove from the value of HOOK the function FUNCTION.
841 HOOK should be a symbol, and FUNCTION may be any valid function. If
842 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
843 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
845 The optional third argument, LOCAL, if non-nil, says to modify
846 the hook's buffer-local value rather than its default value.
847 This makes the hook buffer-local if needed."
848 (or (boundp hook
) (set hook nil
))
849 (or (default-boundp hook
) (set-default hook nil
))
850 (if local
(unless (local-variable-if-set-p hook
)
851 (set (make-local-variable hook
) (list t
)))
852 ;; Detect the case where make-local-variable was used on a hook
853 ;; and do what we used to do.
854 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
856 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
857 ;; Remove the function, for both the list and the non-list cases.
858 (if (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
859 (if (equal hook-value function
) (setq hook-value nil
))
860 (setq hook-value
(delete function
(copy-sequence hook-value
))))
861 ;; If the function is on the global hook, we need to shadow it locally
862 ;;(when (and local (member function (default-value hook))
863 ;; (not (member (cons 'not function) hook-value)))
864 ;; (push (cons 'not function) hook-value))
865 ;; Set the actual variable
867 (set-default hook hook-value
)
868 (if (equal hook-value
'(t))
869 (kill-local-variable hook
)
870 (set hook hook-value
)))))
872 (defun add-to-list (list-var element
&optional append
)
873 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
874 The test for presence of ELEMENT is done with `equal'.
875 If ELEMENT is added, it is added at the beginning of the list,
876 unless the optional argument APPEND is non-nil, in which case
877 ELEMENT is added at the end.
879 The return value is the new value of LIST-VAR.
881 If you want to use `add-to-list' on a variable that is not defined
882 until a certain package is loaded, you should put the call to `add-to-list'
883 into a hook function that will be run only after loading the package.
884 `eval-after-load' provides one way to do this. In some cases
885 other hooks, such as major mode hooks, can do the job."
886 (if (member element
(symbol-value list-var
))
887 (symbol-value list-var
)
890 (append (symbol-value list-var
) (list element
))
891 (cons element
(symbol-value list-var
))))))
896 ;;; (defvar symbol-file-load-history-loaded nil
897 ;;; "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
898 ;;; That file records the part of `load-history' for preloaded files,
899 ;;; which is cleared out before dumping to make Emacs smaller.")
901 ;;; (defun load-symbol-file-load-history ()
902 ;;; "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
903 ;;; That file records the part of `load-history' for preloaded files,
904 ;;; which is cleared out before dumping to make Emacs smaller."
905 ;;; (unless symbol-file-load-history-loaded
906 ;;; (load (expand-file-name
907 ;;; ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
908 ;;; (if (eq system-type 'ms-dos)
910 ;;; (format "fns-%s.el" emacs-version))
912 ;;; ;; The file name fns-%s.el already has a .el extension.
914 ;;; (setq symbol-file-load-history-loaded t)))
916 (defun symbol-file (function)
917 "Return the input source from which FUNCTION was loaded.
918 The value is normally a string that was passed to `load':
919 either an absolute file name, or a library name
920 \(with no directory name and no `.el' or `.elc' at the end).
921 It can also be nil, if the definition is not associated with any file."
922 (if (and (symbolp function
) (fboundp function
)
923 (eq 'autoload
(car-safe (symbol-function function
))))
924 (nth 1 (symbol-function function
))
925 (let ((files load-history
)
928 (if (member function
(cdr (car files
)))
929 (setq file
(car (car files
)) files nil
))
930 (setq files
(cdr files
)))
934 ;;;; Specifying things to do after certain files are loaded.
936 (defun eval-after-load (file form
)
937 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
938 This makes or adds to an entry on `after-load-alist'.
939 If FILE is already loaded, evaluate FORM right now.
940 It does nothing if FORM is already on the list for FILE.
941 FILE must match exactly. Normally FILE is the name of a library,
942 with no directory or extension specified, since that is how `load'
944 FILE can also be a feature (i.e. a symbol), in which case FORM is
945 evaluated whenever that feature is `provide'd."
946 (let ((elt (assoc file after-load-alist
)))
947 ;; Make sure there is an element for FILE.
948 (unless elt
(setq elt
(list file
)) (push elt after-load-alist
))
949 ;; Add FORM to the element if it isn't there.
950 (unless (member form
(cdr elt
))
951 (nconc elt
(list form
))
952 ;; If the file has been loaded already, run FORM right away.
953 (if (if (symbolp file
)
955 ;; Make sure `load-history' contains the files dumped with
956 ;; Emacs for the case that FILE is one of them.
957 ;; (load-symbol-file-load-history)
958 (assoc file load-history
))
962 (defun eval-next-after-load (file)
963 "Read the following input sexp, and run it whenever FILE is loaded.
964 This makes or adds to an entry on `after-load-alist'.
965 FILE should be the name of a library, with no directory name."
966 (eval-after-load file
(read)))
968 ;;; make-network-process wrappers
970 (if (featurep 'make-network-process
)
973 (defun open-network-stream (name buffer host service
)
974 "Open a TCP connection for a service to a host.
975 Returns a subprocess-object to represent the connection.
976 Input and output work as for subprocesses; `delete-process' closes it.
977 Args are NAME BUFFER HOST SERVICE.
978 NAME is name for process. It is modified if necessary to make it unique.
979 BUFFER is the buffer (or buffer-name) to associate with the process.
980 Process output goes at end of that buffer, unless you specify
981 an output stream or filter function to handle the output.
982 BUFFER may be also nil, meaning that this process is not associated
984 Third arg is name of the host to connect to, or its IP address.
985 Fourth arg SERVICE is name of the service desired, or an integer
986 specifying a port number to connect to."
987 (make-network-process :name name
:buffer buffer
988 :host host
:service service
))
990 (defun open-network-stream-nowait (name buffer host service
&optional sentinel filter
)
991 "Initiate connection to a TCP connection for a service to a host.
992 It returns nil if non-blocking connects are not supported; otherwise,
993 it returns a subprocess-object to represent the connection.
995 This function is similar to `open-network-stream', except that this
996 function returns before the connection is established. When the
997 connection is completed, the sentinel function will be called with
998 second arg matching `open' (if successful) or `failed' (on error).
1000 Args are NAME BUFFER HOST SERVICE SENTINEL FILTER.
1001 NAME, BUFFER, HOST, and SERVICE are as for `open-network-stream'.
1002 Optional args, SENTINEL and FILTER specifies the sentinel and filter
1003 functions to be used for this network stream."
1004 (if (featurep 'make-network-process
'(:nowait t
))
1005 (make-network-process :name name
:buffer buffer
:nowait t
1006 :host host
:service service
1007 :filter filter
:sentinel sentinel
)))
1009 (defun open-network-stream-server (name buffer service
&optional sentinel filter
)
1010 "Create a network server process for a TCP service.
1011 It returns nil if server processes are not supported; otherwise,
1012 it returns a subprocess-object to represent the server.
1014 When a client connects to the specified service, a new subprocess
1015 is created to handle the new connection, and the sentinel function
1016 is called for the new process.
1018 Args are NAME BUFFER SERVICE SENTINEL FILTER.
1019 NAME is name for the server process. Client processes are named by
1020 appending the ip-address and port number of the client to NAME.
1021 BUFFER is the buffer (or buffer-name) to associate with the server
1022 process. Client processes will not get a buffer if a process filter
1023 is specified or BUFFER is nil; otherwise, a new buffer is created for
1024 the client process. The name is similar to the process name.
1025 Third arg SERVICE is name of the service desired, or an integer
1026 specifying a port number to connect to. It may also be t to selected
1027 an unused port number for the server.
1028 Optional args, SENTINEL and FILTER specifies the sentinel and filter
1029 functions to be used for the client processes; the server process
1030 does not use these function."
1031 (if (featurep 'make-network-process
'(:server t
))
1032 (make-network-process :name name
:buffer buffer
1033 :service service
:server t
:noquery t
1034 :sentinel sentinel
:filter filter
)))
1036 )) ;; (featurep 'make-network-process)
1041 (defun process-kill-without-query (process &optional flag
)
1042 "Say no query needed if PROCESS is running when Emacs is exited.
1043 Optional second argument if non-nil says to require a query.
1044 Value is t if a query was formerly required.
1045 New code should not use this function; use `process-query-on-exit-flag'
1046 or `set-process-query-on-exit-flag' instead."
1047 (let ((old (process-query-on-exit-flag process
)))
1048 (set-process-query-on-exit-flag process nil
)
1051 ;; process plist management
1053 (defun process-get (process propname
)
1054 "Return the value of PROCESS' PROPNAME property.
1055 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
1056 (plist-get (process-plist process
) propname
))
1058 (defun process-put (process propname value
)
1059 "Change PROCESS' PROPNAME property to VALUE.
1060 It can be retrieved with `(process-get PROCESS PROPNAME)'."
1061 (set-process-plist process
1062 (plist-put (process-plist process
) propname value
)))
1065 ;;;; Input and display facilities.
1067 (defvar read-quoted-char-radix
8
1068 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1069 Legitimate radix values are 8, 10 and 16.")
1071 (custom-declare-variable-early
1072 'read-quoted-char-radix
8
1073 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1074 Legitimate radix values are 8, 10 and 16."
1075 :type
'(choice (const 8) (const 10) (const 16))
1076 :group
'editing-basics
)
1078 (defun read-quoted-char (&optional prompt
)
1079 "Like `read-char', but do not allow quitting.
1080 Also, if the first character read is an octal digit,
1081 we read any number of octal digits and return the
1082 specified character code. Any nondigit terminates the sequence.
1083 If the terminator is RET, it is discarded;
1084 any other terminator is used itself as input.
1086 The optional argument PROMPT specifies a string to use to prompt the user.
1087 The variable `read-quoted-char-radix' controls which radix to use
1089 (let ((message-log-max nil
) done
(first t
) (code 0) char
)
1091 (let ((inhibit-quit first
)
1092 ;; Don't let C-h get the help message--only help function keys.
1095 "Type the special character you want to use,
1096 or the octal character code.
1097 RET terminates the character code and is discarded;
1098 any other non-digit terminates the character code and is then used as input."))
1099 (setq char
(read-event (and prompt
(format "%s-" prompt
)) t
))
1100 (if inhibit-quit
(setq quit-flag nil
)))
1101 ;; Translate TAB key into control-I ASCII character, and so on.
1102 ;; Note: `read-char' does it using the `ascii-character' property.
1103 ;; We could try and use read-key-sequence instead, but then C-q ESC
1104 ;; or C-q C-x might not return immediately since ESC or C-x might be
1105 ;; bound to some prefix in function-key-map or key-translation-map.
1107 (let ((translated (lookup-key function-key-map
(vector char
))))
1108 (if (arrayp translated
)
1109 (setq char
(aref translated
0)))))
1111 ((not (integerp char
))
1112 (setq unread-command-events
(listify-key-sequence (this-single-command-raw-keys))
1114 ((/= (logand char ?\M-\^
@) 0)
1115 ;; Turn a meta-character into a character with the 0200 bit set.
1116 (setq code
(logior (logand char
(lognot ?\M-\^
@)) 128)
1118 ((and (<= ?
0 char
) (< char
(+ ?
0 (min 10 read-quoted-char-radix
))))
1119 (setq code
(+ (* code read-quoted-char-radix
) (- char ?
0)))
1120 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
1121 ((and (<= ?a
(downcase char
))
1122 (< (downcase char
) (+ ?a -
10 (min 26 read-quoted-char-radix
))))
1123 (setq code
(+ (* code read-quoted-char-radix
)
1124 (+ 10 (- (downcase char
) ?a
))))
1125 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
1126 ((and (not first
) (eq char ?\C-m
))
1129 (setq unread-command-events
(listify-key-sequence (this-single-command-raw-keys))
1136 (defun read-passwd (prompt &optional confirm default
)
1137 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
1138 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1139 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
1140 Optional DEFAULT is a default password to use instead of empty input."
1143 (while (not success
)
1144 (let ((first (read-passwd prompt nil default
))
1145 (second (read-passwd "Confirm password: " nil default
)))
1146 (if (equal first second
)
1148 (and (arrayp second
) (fillarray second ?\
0))
1149 (setq success first
))
1150 (and (arrayp first
) (fillarray first ?\
0))
1151 (and (arrayp second
) (fillarray second ?\
0))
1152 (message "Password not repeated accurately; please start over")
1158 (cursor-in-echo-area t
))
1159 (while (progn (message "%s%s"
1161 (make-string (length pass
) ?.
))
1162 (setq c
(read-char-exclusive nil t
))
1163 (and (/= c ?
\r) (/= c ?
\n) (/= c ?\e
)))
1164 (clear-this-command-keys)
1167 (and (arrayp pass
) (fillarray pass ?\
0))
1169 (if (and (/= c ?
\b) (/= c ?
\177))
1170 (let* ((new-char (char-to-string c
))
1171 (new-pass (concat pass new-char
)))
1172 (and (arrayp pass
) (fillarray pass ?\
0))
1173 (fillarray new-char ?\
0)
1175 (setq pass new-pass
))
1176 (if (> (length pass
) 0)
1177 (let ((new-pass (substring pass
0 -
1)))
1178 (and (arrayp pass
) (fillarray pass ?\
0))
1179 (setq pass new-pass
))))))
1181 (or pass default
""))))
1183 ;;; Atomic change groups.
1185 (defmacro atomic-change-group
(&rest body
)
1186 "Perform BODY as an atomic change group.
1187 This means that if BODY exits abnormally,
1188 all of its changes to the current buffer are undone.
1189 This works regardless of whether undo is enabled in the buffer.
1191 This mechanism is transparent to ordinary use of undo;
1192 if undo is enabled in the buffer and BODY succeeds, the
1193 user can undo the change normally."
1194 (let ((handle (make-symbol "--change-group-handle--"))
1195 (success (make-symbol "--change-group-success--")))
1196 `(let ((,handle
(prepare-change-group))
1200 ;; This is inside the unwind-protect because
1201 ;; it enables undo if that was disabled; we need
1202 ;; to make sure that it gets disabled again.
1203 (activate-change-group ,handle
)
1206 ;; Either of these functions will disable undo
1207 ;; if it was disabled before.
1209 (accept-change-group ,handle
)
1210 (cancel-change-group ,handle
))))))
1212 (defun prepare-change-group (&optional buffer
)
1213 "Return a handle for the current buffer's state, for a change group.
1214 If you specify BUFFER, make a handle for BUFFER's state instead.
1216 Pass the handle to `activate-change-group' afterward to initiate
1217 the actual changes of the change group.
1219 To finish the change group, call either `accept-change-group' or
1220 `cancel-change-group' passing the same handle as argument. Call
1221 `accept-change-group' to accept the changes in the group as final;
1222 call `cancel-change-group' to undo them all. You should use
1223 `unwind-protect' to make sure the group is always finished. The call
1224 to `activate-change-group' should be inside the `unwind-protect'.
1225 Once you finish the group, don't use the handle again--don't try to
1226 finish the same group twice. For a simple example of correct use, see
1227 the source code of `atomic-change-group'.
1229 The handle records only the specified buffer. To make a multibuffer
1230 change group, call this function once for each buffer you want to
1231 cover, then use `nconc' to combine the returned values, like this:
1233 (nconc (prepare-change-group buffer-1)
1234 (prepare-change-group buffer-2))
1236 You can then activate that multibuffer change group with a single
1237 call to `activate-change-group' and finish it with a single call
1238 to `accept-change-group' or `cancel-change-group'."
1240 (list (cons (current-buffer) buffer-undo-list
)))
1242 (defun activate-change-group (handle)
1243 "Activate a change group made with `prepare-change-group' (which see)."
1244 (dolist (elt handle
)
1245 (with-current-buffer (car elt
)
1246 (if (eq buffer-undo-list t
)
1247 (setq buffer-undo-list nil
)))))
1249 (defun accept-change-group (handle)
1250 "Finish a change group made with `prepare-change-group' (which see).
1251 This finishes the change group by accepting its changes as final."
1252 (dolist (elt handle
)
1253 (with-current-buffer (car elt
)
1255 (setq buffer-undo-list t
)))))
1257 (defun cancel-change-group (handle)
1258 "Finish a change group made with `prepare-change-group' (which see).
1259 This finishes the change group by reverting all of its changes."
1260 (dolist (elt handle
)
1261 (with-current-buffer (car elt
)
1262 (setq elt
(cdr elt
))
1264 (if (consp elt
) (car elt
)))
1266 (if (consp elt
) (cdr elt
))))
1267 ;; Temporarily truncate the undo log at ELT.
1269 (setcar elt nil
) (setcdr elt nil
))
1270 (unless (eq last-command
'undo
) (undo-start))
1271 ;; Make sure there's no confusion.
1272 (when (and (consp elt
) (not (eq elt
(last pending-undo-list
))))
1273 (error "Undoing to some unrelated state"))
1275 (while pending-undo-list
(undo-more 1))
1276 ;; Reset the modified cons cell ELT to its original content.
1278 (setcar elt old-car
)
1279 (setcdr elt old-cdr
))
1280 ;; Revert the undo info to what it was when we grabbed the state.
1281 (setq buffer-undo-list elt
)))))
1283 ;; For compatibility.
1284 (defalias 'redraw-modeline
'force-mode-line-update
)
1286 (defun force-mode-line-update (&optional all
)
1287 "Force the mode line of the current buffer to be redisplayed.
1288 With optional non-nil ALL, force redisplay of all mode lines."
1289 (if all
(save-excursion (set-buffer (other-buffer))))
1290 (set-buffer-modified-p (buffer-modified-p)))
1292 (defun momentary-string-display (string pos
&optional exit-char message
)
1293 "Momentarily display STRING in the buffer at POS.
1294 Display remains until next character is typed.
1295 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
1296 otherwise it is then available as input (as a command if nothing else).
1297 Display MESSAGE (optional fourth arg) in the echo area.
1298 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
1299 (or exit-char
(setq exit-char ?\
))
1300 (let ((inhibit-read-only t
)
1301 ;; Don't modify the undo list at all.
1302 (buffer-undo-list t
)
1303 (modified (buffer-modified-p))
1304 (name buffer-file-name
)
1310 ;; defeat file locking... don't try this at home, kids!
1311 (setq buffer-file-name nil
)
1312 (insert-before-markers string
)
1313 (setq insert-end
(point))
1314 ;; If the message end is off screen, recenter now.
1315 (if (< (window-end nil t
) insert-end
)
1316 (recenter (/ (window-height) 2)))
1317 ;; If that pushed message start off the screen,
1318 ;; scroll to start it at the top of the screen.
1319 (move-to-window-line 0)
1324 (message (or message
"Type %s to continue editing.")
1325 (single-key-description exit-char
))
1326 (let ((char (read-event)))
1327 (or (eq char exit-char
)
1328 (setq unread-command-events
(list char
)))))
1331 (delete-region pos insert-end
)))
1332 (setq buffer-file-name name
)
1333 (set-buffer-modified-p modified
))))
1336 ;;;; Overlay operations
1338 (defun copy-overlay (o)
1339 "Return a copy of overlay O."
1340 (let ((o1 (make-overlay (overlay-start o
) (overlay-end o
)
1341 ;; FIXME: there's no easy way to find the
1342 ;; insertion-type of the two markers.
1343 (overlay-buffer o
)))
1344 (props (overlay-properties o
)))
1346 (overlay-put o1
(pop props
) (pop props
)))
1349 (defun remove-overlays (beg end name val
)
1350 "Clear BEG and END of overlays whose property NAME has value VAL.
1351 Overlays might be moved and or split."
1353 (setq beg
(prog1 end
(setq end beg
))))
1355 (dolist (o (overlays-in beg end
))
1356 (when (eq (overlay-get o name
) val
)
1357 ;; Either push this overlay outside beg...end
1358 ;; or split it to exclude beg...end
1359 ;; or delete it entirely (if it is contained in beg...end).
1360 (if (< (overlay-start o
) beg
)
1361 (if (> (overlay-end o
) end
)
1363 (move-overlay (copy-overlay o
)
1364 (overlay-start o
) beg
)
1365 (move-overlay o end
(overlay-end o
)))
1366 (move-overlay o
(overlay-start o
) beg
))
1367 (if (> (overlay-end o
) end
)
1368 (move-overlay o end
(overlay-end o
))
1369 (delete-overlay o
)))))))
1373 ;; A number of major modes set this locally.
1374 ;; Give it a global value to avoid compiler warnings.
1375 (defvar font-lock-defaults nil
)
1377 (defvar suspend-hook nil
1378 "Normal hook run by `suspend-emacs', before suspending.")
1380 (defvar suspend-resume-hook nil
1381 "Normal hook run by `suspend-emacs', after Emacs is continued.")
1383 (defvar temp-buffer-show-hook nil
1384 "Normal hook run by `with-output-to-temp-buffer' after displaying the buffer.
1385 When the hook runs, the temporary buffer is current, and the window it
1386 was displayed in is selected. This hook is normally set up with a
1387 function to make the buffer read only, and find function names and
1388 variable names in it, provided the major mode is still Help mode.")
1390 (defvar temp-buffer-setup-hook nil
1391 "Normal hook run by `with-output-to-temp-buffer' at the start.
1392 When the hook runs, the temporary buffer is current.
1393 This hook is normally set up with a function to put the buffer in Help
1396 ;; Avoid compiler warnings about this variable,
1397 ;; which has a special meaning on certain system types.
1398 (defvar buffer-file-type nil
1399 "Non-nil if the visited file is a binary file.
1400 This variable is meaningful on MS-DOG and Windows NT.
1401 On those systems, it is automatically local in every buffer.
1402 On other systems, this variable is normally always nil.")
1404 ;; This should probably be written in C (i.e., without using `walk-windows').
1405 (defun get-buffer-window-list (buffer &optional minibuf frame
)
1406 "Return windows currently displaying BUFFER, or nil if none.
1407 See `walk-windows' for the meaning of MINIBUF and FRAME."
1408 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
1409 (walk-windows (function (lambda (window)
1410 (if (eq (window-buffer window
) buffer
)
1411 (setq windows
(cons window windows
)))))
1415 (defun ignore (&rest ignore
)
1416 "Do nothing and return nil.
1417 This function accepts any number of arguments, but ignores them."
1421 (defun error (&rest args
)
1422 "Signal an error, making error message by passing all args to `format'.
1423 In Emacs, the convention is that error messages start with a capital
1424 letter but *do not* end with a period. Please follow this convention
1425 for the sake of consistency."
1427 (signal 'error
(list (apply 'format args
)))))
1429 (defalias 'user-original-login-name
'user-login-name
)
1431 (defvar yank-excluded-properties
)
1433 (defun remove-yank-excluded-properties (start end
)
1434 "Remove `yank-excluded-properties' between START and END positions.
1435 Replaces `category' properties with their defined properties."
1436 (let ((inhibit-read-only t
))
1437 ;; Replace any `category' property with the properties it stands for.
1438 (unless (memq yank-excluded-properties
'(t nil
))
1441 (while (< (point) end
)
1442 (let ((cat (get-text-property (point) 'category
))
1445 (next-single-property-change (point) 'category nil end
))
1447 (let (run-end2 original
)
1448 (remove-list-of-text-properties (point) run-end
'(category))
1449 (while (< (point) run-end
)
1450 (setq run-end2
(next-property-change (point) nil run-end
))
1451 (setq original
(text-properties-at (point)))
1452 (set-text-properties (point) run-end2
(symbol-plist cat
))
1453 (add-text-properties (point) run-end2 original
)
1454 (goto-char run-end2
))))
1455 (goto-char run-end
)))))
1456 (if (eq yank-excluded-properties t
)
1457 (set-text-properties start end nil
)
1458 (remove-list-of-text-properties start end yank-excluded-properties
))))
1460 (defvar yank-undo-function
)
1462 (defun insert-for-yank (string)
1463 "Insert STRING at point, stripping some text properties.
1464 Strip text properties from the inserted text according to
1465 `yank-excluded-properties'. Otherwise just like (insert STRING).
1467 If STRING has a non-nil yank-handler property on the first character,
1468 the normal insert behaviour is modified in various ways. The value of
1469 the yank-handler property must be a list with one to five elements
1470 with the following format: (FUNCTION PARAM NOEXCLUDE UNDO COMMAND).
1471 When FUNCTION is present and non-nil, it is called instead of `insert'
1472 to insert the string. FUNCTION takes one argument--the object to insert.
1473 If PARAM is present and non-nil, it replaces STRING as the object
1474 passed to FUNCTION (or `insert'); for example, if FUNCTION is
1475 `yank-rectangle', PARAM may be a list of strings to insert as a
1477 If NOEXCLUDE is present and non-nil, the normal removal of the
1478 yank-excluded-properties is not performed; instead FUNCTION is
1479 responsible for removing those properties. This may be necessary
1480 if FUNCTION adjusts point before or after inserting the object.
1481 If UNDO is present and non-nil, it is a function that will be called
1482 by `yank-pop' to undo the insertion of the current object. It is
1483 called with two arguments
1484 FUNCTION may set `yank-undo-function' to override this.
1485 If COMMAND is present and non-nil, `this-command' is set to COMMAND
1486 after calling FUNCTION (or insert). Note that setting `this-command'
1487 to a value different from `yank' will prevent `yank-pop' from undoing
1489 (let* ((method (get-text-property 0 'yank-handler string
))
1490 (param (or (nth 1 method
) string
))
1492 (setq yank-undo-function
(nth 3 method
)) ;; UNDO
1493 (if (nth 0 method
) ;; FUNCTION
1494 (funcall (car method
) param
)
1495 (setq opoint
(point))
1497 (unless (nth 2 method
) ;; NOEXCLUDE
1498 (remove-yank-excluded-properties opoint
(point)))
1499 (if (nth 4 method
) ;; COMMAND
1500 (setq this-command
(nth 4 method
)))))
1502 (defun insert-buffer-substring-no-properties (buf &optional start end
)
1503 "Insert before point a substring of buffer BUFFER, without text properties.
1504 BUFFER may be a buffer or a buffer name.
1505 Arguments START and END are character numbers specifying the substring.
1506 They default to the beginning and the end of BUFFER."
1507 (let ((opoint (point)))
1508 (insert-buffer-substring buf start end
)
1509 (let ((inhibit-read-only t
))
1510 (set-text-properties opoint
(point) nil
))))
1512 (defun insert-buffer-substring-as-yank (buf &optional start end
)
1513 "Insert before point a part of buffer BUFFER, stripping some text properties.
1514 BUFFER may be a buffer or a buffer name. Arguments START and END are
1515 character numbers specifying the substring. They default to the
1516 beginning and the end of BUFFER. Strip text properties from the
1517 inserted text according to `yank-excluded-properties'."
1518 (let ((opoint (point)))
1519 (insert-buffer-substring buf start end
)
1520 (remove-yank-excluded-properties opoint
(point))))
1523 ;; Synchronous shell commands.
1525 (defun start-process-shell-command (name buffer
&rest args
)
1526 "Start a program in a subprocess. Return the process object for it.
1527 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1528 NAME is name for process. It is modified if necessary to make it unique.
1529 BUFFER is the buffer or (buffer-name) to associate with the process.
1530 Process output goes at end of that buffer, unless you specify
1531 an output stream or filter function to handle the output.
1532 BUFFER may be also nil, meaning that this process is not associated
1534 Third arg is command name, the name of a shell command.
1535 Remaining arguments are the arguments for the command.
1536 Wildcards and redirection are handled as usual in the shell."
1538 ((eq system-type
'vax-vms
)
1539 (apply 'start-process name buffer args
))
1540 ;; We used to use `exec' to replace the shell with the command,
1541 ;; but that failed to handle (...) and semicolon, etc.
1543 (start-process name buffer shell-file-name shell-command-switch
1544 (mapconcat 'identity args
" ")))))
1546 (defun call-process-shell-command (command &optional infile buffer display
1548 "Execute the shell command COMMAND synchronously in separate process.
1549 The remaining arguments are optional.
1550 The program's input comes from file INFILE (nil means `/dev/null').
1551 Insert output in BUFFER before point; t means current buffer;
1552 nil for BUFFER means discard it; 0 means discard and don't wait.
1553 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1554 REAL-BUFFER says what to do with standard output, as above,
1555 while STDERR-FILE says what to do with standard error in the child.
1556 STDERR-FILE may be nil (discard standard error output),
1557 t (mix it with ordinary output), or a file name string.
1559 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1560 Remaining arguments are strings passed as additional arguments for COMMAND.
1561 Wildcards and redirection are handled as usual in the shell.
1563 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
1564 Otherwise it waits for COMMAND to terminate and returns a numeric exit
1565 status or a signal description string.
1566 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
1568 ((eq system-type
'vax-vms
)
1569 (apply 'call-process command infile buffer display args
))
1570 ;; We used to use `exec' to replace the shell with the command,
1571 ;; but that failed to handle (...) and semicolon, etc.
1573 (call-process shell-file-name
1574 infile buffer display
1575 shell-command-switch
1576 (mapconcat 'identity
(cons command args
) " ")))))
1578 (defmacro with-current-buffer
(buffer &rest body
)
1579 "Execute the forms in BODY with BUFFER as the current buffer.
1580 The value returned is the value of the last form in BODY.
1581 See also `with-temp-buffer'."
1582 (cons 'save-current-buffer
1583 (cons (list 'set-buffer buffer
)
1586 (defmacro with-temp-file
(file &rest body
)
1587 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1588 The value returned is the value of the last form in BODY.
1589 See also `with-temp-buffer'."
1590 (let ((temp-file (make-symbol "temp-file"))
1591 (temp-buffer (make-symbol "temp-buffer")))
1592 `(let ((,temp-file
,file
)
1594 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1597 (with-current-buffer ,temp-buffer
1599 (with-current-buffer ,temp-buffer
1601 (write-region (point-min) (point-max) ,temp-file nil
0)))
1602 (and (buffer-name ,temp-buffer
)
1603 (kill-buffer ,temp-buffer
))))))
1605 (defmacro with-temp-message
(message &rest body
)
1606 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1607 The original message is restored to the echo area after BODY has finished.
1608 The value returned is the value of the last form in BODY.
1609 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1610 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1611 Use a MESSAGE of \"\" to temporarily clear the echo area."
1612 (let ((current-message (make-symbol "current-message"))
1613 (temp-message (make-symbol "with-temp-message")))
1614 `(let ((,temp-message
,message
)
1619 (setq ,current-message
(current-message))
1620 (message "%s" ,temp-message
))
1623 (if ,current-message
1624 (message "%s" ,current-message
)
1627 (defmacro with-temp-buffer
(&rest body
)
1628 "Create a temporary buffer, and evaluate BODY there like `progn'.
1629 See also `with-temp-file' and `with-output-to-string'."
1630 (let ((temp-buffer (make-symbol "temp-buffer")))
1631 `(let ((,temp-buffer
1632 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1634 (with-current-buffer ,temp-buffer
1636 (and (buffer-name ,temp-buffer
)
1637 (kill-buffer ,temp-buffer
))))))
1639 (defmacro with-output-to-string
(&rest body
)
1640 "Execute BODY, return the text it sent to `standard-output', as a string."
1641 `(let ((standard-output
1642 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1643 (let ((standard-output standard-output
))
1645 (with-current-buffer standard-output
1648 (kill-buffer nil
)))))
1650 (defmacro with-local-quit
(&rest body
)
1651 "Execute BODY with `inhibit-quit' temporarily bound to nil."
1652 (declare (debug t
) (indent 0))
1653 `(condition-case nil
1654 (let ((inhibit-quit nil
))
1656 (quit (setq quit-flag t
))))
1658 (defmacro combine-after-change-calls
(&rest body
)
1659 "Execute BODY, but don't call the after-change functions till the end.
1660 If BODY makes changes in the buffer, they are recorded
1661 and the functions on `after-change-functions' are called several times
1662 when BODY is finished.
1663 The return value is the value of the last form in BODY.
1665 If `before-change-functions' is non-nil, then calls to the after-change
1666 functions can't be deferred, so in that case this macro has no effect.
1668 Do not alter `after-change-functions' or `before-change-functions'
1671 (let ((combine-after-change-calls t
))
1673 (combine-after-change-execute)))
1676 (defvar delay-mode-hooks nil
1677 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1678 (defvar delayed-mode-hooks nil
1679 "List of delayed mode hooks waiting to be run.")
1680 (make-variable-buffer-local 'delayed-mode-hooks
)
1682 (defun run-mode-hooks (&rest hooks
)
1683 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1684 Execution is delayed if `delay-mode-hooks' is non-nil.
1685 Major mode functions should use this."
1686 (if delay-mode-hooks
1688 (dolist (hook hooks
)
1689 (push hook delayed-mode-hooks
))
1690 ;; Normal case, just run the hook as before plus any delayed hooks.
1691 (setq hooks
(nconc (nreverse delayed-mode-hooks
) hooks
))
1692 (setq delayed-mode-hooks nil
)
1693 (apply 'run-hooks hooks
)))
1695 (defmacro delay-mode-hooks
(&rest body
)
1696 "Execute BODY, but delay any `run-mode-hooks'.
1697 Only affects hooks run in the current buffer."
1699 (make-local-variable 'delay-mode-hooks
)
1700 (let ((delay-mode-hooks t
))
1703 ;; PUBLIC: find if the current mode derives from another.
1705 (defun derived-mode-p (&rest modes
)
1706 "Non-nil if the current major mode is derived from one of MODES.
1707 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1708 (let ((parent major-mode
))
1709 (while (and (not (memq parent modes
))
1710 (setq parent
(get parent
'derived-mode-parent
))))
1713 (defmacro with-syntax-table
(table &rest body
)
1714 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1715 The syntax table of the current buffer is saved, BODY is evaluated, and the
1716 saved table is restored, even in case of an abnormal exit.
1717 Value is what BODY returns."
1718 (let ((old-table (make-symbol "table"))
1719 (old-buffer (make-symbol "buffer")))
1720 `(let ((,old-table
(syntax-table))
1721 (,old-buffer
(current-buffer)))
1724 (set-syntax-table (copy-syntax-table ,table
))
1726 (save-current-buffer
1727 (set-buffer ,old-buffer
)
1728 (set-syntax-table ,old-table
))))))
1730 ;;; Matching and substitution
1732 (defvar save-match-data-internal
)
1734 ;; We use save-match-data-internal as the local variable because
1735 ;; that works ok in practice (people should not use that variable elsewhere).
1736 ;; We used to use an uninterned symbol; the compiler handles that properly
1737 ;; now, but it generates slower code.
1738 (defmacro save-match-data
(&rest body
)
1739 "Execute the BODY forms, restoring the global value of the match data.
1740 The value returned is the value of the last form in BODY."
1741 ;; It is better not to use backquote here,
1742 ;; because that makes a bootstrapping problem
1743 ;; if you need to recompile all the Lisp files using interpreted code.
1745 '((save-match-data-internal (match-data)))
1746 (list 'unwind-protect
1748 '(set-match-data save-match-data-internal
))))
1750 (defun match-string (num &optional string
)
1751 "Return string of text matched by last search.
1752 NUM specifies which parenthesized expression in the last regexp.
1753 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1754 Zero means the entire text matched by the whole regexp or whole string.
1755 STRING should be given if the last search was by `string-match' on STRING."
1756 (if (match-beginning num
)
1758 (substring string
(match-beginning num
) (match-end num
))
1759 (buffer-substring (match-beginning num
) (match-end num
)))))
1761 (defun match-string-no-properties (num &optional string
)
1762 "Return string of text matched by last search, without text properties.
1763 NUM specifies which parenthesized expression in the last regexp.
1764 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1765 Zero means the entire text matched by the whole regexp or whole string.
1766 STRING should be given if the last search was by `string-match' on STRING."
1767 (if (match-beginning num
)
1770 (substring string
(match-beginning num
) (match-end num
))))
1771 (set-text-properties 0 (length result
) nil result
)
1773 (buffer-substring-no-properties (match-beginning num
)
1776 (defun split-string (string &optional separators
)
1777 "Splits STRING into substrings where there are matches for SEPARATORS.
1778 Each match for SEPARATORS is a splitting point.
1779 The substrings between the splitting points are made into a list
1781 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1783 If there is match for SEPARATORS at the beginning of STRING, we do not
1784 include a null substring for that. Likewise, if there is a match
1785 at the end of STRING, we don't include a null substring for that.
1787 Modifies the match data; use `save-match-data' if necessary."
1788 (let ((rexp (or separators
"[ \f\t\n\r\v]+"))
1792 (while (and (string-match rexp string
1794 (= start
(match-beginning 0))
1795 (< start
(length string
)))
1797 (< (match-beginning 0) (length string
)))
1799 (or (eq (match-beginning 0) 0)
1800 (and (eq (match-beginning 0) (match-end 0))
1801 (eq (match-beginning 0) start
))
1803 (cons (substring string start
(match-beginning 0))
1805 (setq start
(match-end 0)))
1806 (or (eq start
(length string
))
1808 (cons (substring string start
)
1812 (defun subst-char-in-string (fromchar tochar string
&optional inplace
)
1813 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1814 Unless optional argument INPLACE is non-nil, return a new string."
1815 (let ((i (length string
))
1816 (newstr (if inplace string
(copy-sequence string
))))
1819 (if (eq (aref newstr i
) fromchar
)
1820 (aset newstr i tochar
)))
1823 (defun replace-regexp-in-string (regexp rep string
&optional
1824 fixedcase literal subexp start
)
1825 "Replace all matches for REGEXP with REP in STRING.
1827 Return a new string containing the replacements.
1829 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1830 arguments with the same names of function `replace-match'. If START
1831 is non-nil, start replacements at that index in STRING.
1833 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1834 function. If it is a function it is applied to each match to generate
1835 the replacement passed to `replace-match'; the match-data at this
1836 point are such that match 0 is the function's argument.
1838 To replace only the first match (if any), make REGEXP match up to \\'
1839 and replace a sub-expression, e.g.
1840 (replace-regexp-in-string \"\\\\(foo\\\\).*\\\\'\" \"bar\" \" foo foo\" nil nil 1)
1844 ;; To avoid excessive consing from multiple matches in long strings,
1845 ;; don't just call `replace-match' continually. Walk down the
1846 ;; string looking for matches of REGEXP and building up a (reversed)
1847 ;; list MATCHES. This comprises segments of STRING which weren't
1848 ;; matched interspersed with replacements for segments that were.
1849 ;; [For a `large' number of replacements it's more efficient to
1850 ;; operate in a temporary buffer; we can't tell from the function's
1851 ;; args whether to choose the buffer-based implementation, though it
1852 ;; might be reasonable to do so for long enough STRING.]
1853 (let ((l (length string
))
1854 (start (or start
0))
1857 (while (and (< start l
) (string-match regexp string start
))
1858 (setq mb
(match-beginning 0)
1860 ;; If we matched the empty string, make sure we advance by one char
1861 (when (= me mb
) (setq me
(min l
(1+ mb
))))
1862 ;; Generate a replacement for the matched substring.
1863 ;; Operate only on the substring to minimize string consing.
1864 ;; Set up match data for the substring for replacement;
1865 ;; presumably this is likely to be faster than munging the
1866 ;; match data directly in Lisp.
1867 (string-match regexp
(setq str
(substring string mb me
)))
1869 (cons (replace-match (if (stringp rep
)
1871 (funcall rep
(match-string 0 str
)))
1872 fixedcase literal str subexp
)
1873 (cons (substring string start mb
) ; unmatched prefix
1876 ;; Reconstruct a string from the pieces.
1877 (setq matches
(cons (substring string start l
) matches
)) ; leftover
1878 (apply #'concat
(nreverse matches
)))))
1880 (defun shell-quote-argument (argument)
1881 "Quote an argument for passing as argument to an inferior shell."
1882 (if (eq system-type
'ms-dos
)
1883 ;; Quote using double quotes, but escape any existing quotes in
1884 ;; the argument with backslashes.
1888 (if (or (null (string-match "[^\"]" argument
))
1889 (< (match-end 0) (length argument
)))
1890 (while (string-match "[\"]" argument start
)
1891 (setq end
(match-beginning 0)
1892 result
(concat result
(substring argument start end
)
1893 "\\" (substring argument end
(1+ end
)))
1895 (concat "\"" result
(substring argument start
) "\""))
1896 (if (eq system-type
'windows-nt
)
1897 (concat "\"" argument
"\"")
1898 (if (equal argument
"")
1900 ;; Quote everything except POSIX filename characters.
1901 ;; This should be safe enough even for really weird shells.
1902 (let ((result "") (start 0) end
)
1903 (while (string-match "[^-0-9a-zA-Z_./]" argument start
)
1904 (setq end
(match-beginning 0)
1905 result
(concat result
(substring argument start end
)
1906 "\\" (substring argument end
(1+ end
)))
1908 (concat result
(substring argument start
)))))))
1910 (defun make-syntax-table (&optional oldtable
)
1911 "Return a new syntax table.
1912 Create a syntax table which inherits from OLDTABLE (if non-nil) or
1913 from `standard-syntax-table' otherwise."
1914 (let ((table (make-char-table 'syntax-table nil
)))
1915 (set-char-table-parent table
(or oldtable
(standard-syntax-table)))
1918 (defun syntax-after (pos)
1919 "Return the syntax of the char after POS."
1920 (unless (or (< pos
(point-min)) (>= pos
(point-max)))
1921 (let ((st (if parse-sexp-lookup-properties
1922 (get-char-property pos
'syntax-table
))))
1924 (aref (or st
(syntax-table)) (char-after pos
))))))
1926 (defun add-to-invisibility-spec (arg)
1927 "Add elements to `buffer-invisibility-spec'.
1928 See documentation for `buffer-invisibility-spec' for the kind of elements
1930 (if (eq buffer-invisibility-spec t
)
1931 (setq buffer-invisibility-spec
(list t
)))
1932 (setq buffer-invisibility-spec
1933 (cons arg buffer-invisibility-spec
)))
1935 (defun remove-from-invisibility-spec (arg)
1936 "Remove elements from `buffer-invisibility-spec'."
1937 (if (consp buffer-invisibility-spec
)
1938 (setq buffer-invisibility-spec
(delete arg buffer-invisibility-spec
))))
1940 (defun global-set-key (key command
)
1941 "Give KEY a global binding as COMMAND.
1942 COMMAND is the command definition to use; usually it is
1943 a symbol naming an interactively-callable function.
1944 KEY is a key sequence; noninteractively, it is a string or vector
1945 of characters or event types, and non-ASCII characters with codes
1946 above 127 (such as ISO Latin-1) can be included if you use a vector.
1948 Note that if KEY has a local binding in the current buffer,
1949 that local binding will continue to shadow any global binding
1950 that you make with this function."
1951 (interactive "KSet key globally: \nCSet key %s to command: ")
1952 (or (vectorp key
) (stringp key
)
1953 (signal 'wrong-type-argument
(list 'arrayp key
)))
1954 (define-key (current-global-map) key command
))
1956 (defun local-set-key (key command
)
1957 "Give KEY a local binding as COMMAND.
1958 COMMAND is the command definition to use; usually it is
1959 a symbol naming an interactively-callable function.
1960 KEY is a key sequence; noninteractively, it is a string or vector
1961 of characters or event types, and non-ASCII characters with codes
1962 above 127 (such as ISO Latin-1) can be included if you use a vector.
1964 The binding goes in the current buffer's local map,
1965 which in most cases is shared with all other buffers in the same major mode."
1966 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1967 (let ((map (current-local-map)))
1969 (use-local-map (setq map
(make-sparse-keymap))))
1970 (or (vectorp key
) (stringp key
)
1971 (signal 'wrong-type-argument
(list 'arrayp key
)))
1972 (define-key map key command
)))
1974 (defun global-unset-key (key)
1975 "Remove global binding of KEY.
1976 KEY is a string representing a sequence of keystrokes."
1977 (interactive "kUnset key globally: ")
1978 (global-set-key key nil
))
1980 (defun local-unset-key (key)
1981 "Remove local binding of KEY.
1982 KEY is a string representing a sequence of keystrokes."
1983 (interactive "kUnset key locally: ")
1984 (if (current-local-map)
1985 (local-set-key key nil
))
1988 ;; We put this here instead of in frame.el so that it's defined even on
1989 ;; systems where frame.el isn't loaded.
1990 (defun frame-configuration-p (object)
1991 "Return non-nil if OBJECT seems to be a frame configuration.
1992 Any list whose car is `frame-configuration' is assumed to be a frame
1995 (eq (car object
) 'frame-configuration
)))
1997 (defun functionp (object)
1998 "Non-nil iff OBJECT is a type of object that can be called as a function."
1999 (or (and (symbolp object
) (fboundp object
)
2001 (setq object
(indirect-function object
))
2003 (eq (car-safe object
) 'autoload
)
2004 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object
)))))))
2005 (subrp object
) (byte-code-function-p object
)
2006 (eq (car-safe object
) 'lambda
)))
2008 (defun interactive-form (function)
2009 "Return the interactive form of FUNCTION.
2010 If function is a command (see `commandp'), value is a list of the form
2011 \(interactive SPEC). If function is not a command, return nil."
2012 (setq function
(indirect-function function
))
2013 (when (commandp function
)
2014 (cond ((byte-code-function-p function
)
2015 (when (> (length function
) 5)
2016 (let ((spec (aref function
5)))
2018 (list 'interactive spec
)
2019 (list 'interactive
)))))
2021 (subr-interactive-form function
))
2022 ((eq (car-safe function
) 'lambda
)
2023 (setq function
(cddr function
))
2024 (when (stringp (car function
))
2025 (setq function
(cdr function
)))
2026 (let ((form (car function
)))
2027 (when (eq (car-safe form
) 'interactive
)
2028 (copy-sequence form
)))))))
2030 (defun assq-delete-all (key alist
)
2031 "Delete from ALIST all elements whose car is KEY.
2032 Return the modified alist."
2035 (if (eq (car (car tail
)) key
)
2036 (setq alist
(delq (car tail
) alist
)))
2037 (setq tail
(cdr tail
)))
2040 (defun make-temp-file (prefix &optional dir-flag suffix
)
2041 "Create a temporary file.
2042 The returned file name (created by appending some random characters at the end
2043 of PREFIX, and expanding against `temporary-file-directory' if necessary),
2044 is guaranteed to point to a newly created empty file.
2045 You can then use `write-region' to write new data into the file.
2047 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
2049 If SUFFIX is non-nil, add that at the end of the file name."
2050 (let ((umask (default-file-modes))
2054 ;; Create temp files with strict access rights. It's easy to
2055 ;; loosen them later, whereas it's impossible to close the
2056 ;; time-window of loose permissions otherwise.
2057 (set-default-file-modes ?
\700)
2058 (while (condition-case ()
2062 (expand-file-name prefix temporary-file-directory
)))
2064 (setq file
(concat file suffix
)))
2066 (make-directory file
)
2067 (write-region "" nil file nil
'silent nil
'excl
))
2069 (file-already-exists t
))
2070 ;; the file was somehow created by someone else between
2071 ;; `make-temp-name' and `write-region', let's try again.
2075 (set-default-file-modes umask
))))
2078 (defun add-minor-mode (toggle name
&optional keymap after toggle-fun
)
2079 "Register a new minor mode.
2081 This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
2083 TOGGLE is a symbol which is the name of a buffer-local variable that
2084 is toggled on or off to say whether the minor mode is active or not.
2086 NAME specifies what will appear in the mode line when the minor mode
2087 is active. NAME should be either a string starting with a space, or a
2088 symbol whose value is such a string.
2090 Optional KEYMAP is the keymap for the minor mode that will be added
2091 to `minor-mode-map-alist'.
2093 Optional AFTER specifies that TOGGLE should be added after AFTER
2094 in `minor-mode-alist'.
2096 Optional TOGGLE-FUN is an interactive function to toggle the mode.
2097 It defaults to (and should by convention be) TOGGLE.
2099 If TOGGLE has a non-nil `:included' property, an entry for the mode is
2100 included in the mode-line minor mode menu.
2101 If TOGGLE has a `:menu-tag', that is used for the menu item's label."
2102 (unless toggle-fun
(setq toggle-fun toggle
))
2103 ;; Add the name to the minor-mode-alist.
2105 (let ((existing (assq toggle minor-mode-alist
)))
2107 (setcdr existing
(list name
))
2108 (let ((tail minor-mode-alist
) found
)
2109 (while (and tail
(not found
))
2110 (if (eq after
(caar tail
))
2112 (setq tail
(cdr tail
))))
2114 (let ((rest (cdr found
)))
2116 (nconc found
(list (list toggle name
)) rest
))
2117 (setq minor-mode-alist
(cons (list toggle name
)
2118 minor-mode-alist
)))))))
2119 ;; Add the toggle to the minor-modes menu if requested.
2120 (when (get toggle
:included
)
2121 (define-key mode-line-mode-menu
2125 (or (get toggle
:menu-tag
)
2126 (if (stringp name
) name
(symbol-name toggle
)))
2127 (let ((mode-name (if (symbolp name
) (symbol-value name
))))
2128 (if (and (stringp mode-name
) (string-match "[^ ]+" mode-name
))
2129 (concat " (" (match-string 0 mode-name
) ")"))))
2131 :button
(cons :toggle toggle
))))
2133 ;; Add the map to the minor-mode-map-alist.
2135 (let ((existing (assq toggle minor-mode-map-alist
)))
2137 (setcdr existing keymap
)
2138 (let ((tail minor-mode-map-alist
) found
)
2139 (while (and tail
(not found
))
2140 (if (eq after
(caar tail
))
2142 (setq tail
(cdr tail
))))
2144 (let ((rest (cdr found
)))
2146 (nconc found
(list (cons toggle keymap
)) rest
))
2147 (setq minor-mode-map-alist
(cons (cons toggle keymap
)
2148 minor-mode-map-alist
))))))))
2150 ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2152 (defun text-clone-maintain (ol1 after beg end
&optional len
)
2153 "Propagate the changes made under the overlay OL1 to the other clones.
2154 This is used on the `modification-hooks' property of text clones."
2155 (when (and after
(not undo-in-progress
) (overlay-start ol1
))
2156 (let ((margin (if (overlay-get ol1
'text-clone-spreadp
) 1 0)))
2157 (setq beg
(max beg
(+ (overlay-start ol1
) margin
)))
2158 (setq end
(min end
(- (overlay-end ol1
) margin
)))
2161 (when (overlay-get ol1
'text-clone-syntax
)
2162 ;; Check content of the clone's text.
2163 (let ((cbeg (+ (overlay-start ol1
) margin
))
2164 (cend (- (overlay-end ol1
) margin
)))
2167 (if (not (re-search-forward
2168 (overlay-get ol1
'text-clone-syntax
) cend t
))
2169 ;; Mark the overlay for deletion.
2170 (overlay-put ol1
'text-clones nil
)
2171 (when (< (match-end 0) cend
)
2172 ;; Shrink the clone at its end.
2173 (setq end
(min end
(match-end 0)))
2174 (move-overlay ol1
(overlay-start ol1
)
2175 (+ (match-end 0) margin
)))
2176 (when (> (match-beginning 0) cbeg
)
2177 ;; Shrink the clone at its beginning.
2178 (setq beg
(max (match-beginning 0) beg
))
2179 (move-overlay ol1
(- (match-beginning 0) margin
)
2180 (overlay-end ol1
)))))))
2181 ;; Now go ahead and update the clones.
2182 (let ((head (- beg
(overlay-start ol1
)))
2183 (tail (- (overlay-end ol1
) end
))
2184 (str (buffer-substring beg end
))
2186 (inhibit-modification-hooks t
))
2187 (dolist (ol2 (overlay-get ol1
'text-clones
))
2188 (let ((oe (overlay-end ol2
)))
2189 (unless (or (eq ol1 ol2
) (null oe
))
2190 (setq nothing-left nil
)
2191 (let ((mod-beg (+ (overlay-start ol2
) head
)))
2192 ;;(overlay-put ol2 'modification-hooks nil)
2193 (goto-char (- (overlay-end ol2
) tail
))
2194 (unless (> mod-beg
(point))
2195 (save-excursion (insert str
))
2196 (delete-region mod-beg
(point)))
2197 ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain))
2199 (if nothing-left
(delete-overlay ol1
))))))))
2201 (defun text-clone-create (start end
&optional spreadp syntax
)
2202 "Create a text clone of START...END at point.
2203 Text clones are chunks of text that are automatically kept identical:
2204 changes done to one of the clones will be immediately propagated to the other.
2206 The buffer's content at point is assumed to be already identical to
2207 the one between START and END.
2208 If SYNTAX is provided it's a regexp that describes the possible text of
2209 the clones; the clone will be shrunk or killed if necessary to ensure that
2210 its text matches the regexp.
2211 If SPREADP is non-nil it indicates that text inserted before/after the
2212 clone should be incorporated in the clone."
2213 ;; To deal with SPREADP we can either use an overlay with `nil t' along
2214 ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay
2215 ;; (with a one-char margin at each end) with `t nil'.
2216 ;; We opted for a larger overlay because it behaves better in the case
2217 ;; where the clone is reduced to the empty string (we want the overlay to
2218 ;; stay when the clone's content is the empty string and we want to use
2219 ;; `evaporate' to make sure those overlays get deleted when needed).
2221 (let* ((pt-end (+ (point) (- end start
)))
2222 (start-margin (if (or (not spreadp
) (bobp) (<= start
(point-min)))
2224 (end-margin (if (or (not spreadp
)
2225 (>= pt-end
(point-max))
2226 (>= start
(point-max)))
2228 (ol1 (make-overlay (- start start-margin
) (+ end end-margin
) nil t
))
2229 (ol2 (make-overlay (- (point) start-margin
) (+ pt-end end-margin
) nil t
))
2230 (dups (list ol1 ol2
)))
2231 (overlay-put ol1
'modification-hooks
'(text-clone-maintain))
2232 (when spreadp
(overlay-put ol1
'text-clone-spreadp t
))
2233 (when syntax
(overlay-put ol1
'text-clone-syntax syntax
))
2234 ;;(overlay-put ol1 'face 'underline)
2235 (overlay-put ol1
'evaporate t
)
2236 (overlay-put ol1
'text-clones dups
)
2238 (overlay-put ol2
'modification-hooks
'(text-clone-maintain))
2239 (when spreadp
(overlay-put ol2
'text-clone-spreadp t
))
2240 (when syntax
(overlay-put ol2
'text-clone-syntax syntax
))
2241 ;;(overlay-put ol2 'face 'underline)
2242 (overlay-put ol2
'evaporate t
)
2243 (overlay-put ol2
'text-clones dups
)))
2245 (defun play-sound (sound)
2246 "SOUND is a list of the form `(sound KEYWORD VALUE...)'.
2247 The following keywords are recognized:
2249 :file FILE - read sound data from FILE. If FILE isn't an
2250 absolute file name, it is searched in `data-directory'.
2252 :data DATA - read sound data from string DATA.
2254 Exactly one of :file or :data must be present.
2256 :volume VOL - set volume to VOL. VOL must an integer in the
2257 range 0..100 or a float in the range 0..1.0. If not specified,
2258 don't change the volume setting of the sound device.
2260 :device DEVICE - play sound on DEVICE. If not specified,
2261 a system-dependent default device name is used."
2262 (unless (fboundp 'play-sound-internal
)
2263 (error "This Emacs binary lacks sound support"))
2264 (play-sound-internal sound
))
2266 (defun define-mail-user-agent (symbol composefunc sendfunc
2267 &optional abortfunc hookvar
)
2268 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
2270 SYMBOL can be any Lisp symbol. Its function definition and/or
2271 value as a variable do not matter for this usage; we use only certain
2272 properties on its property list, to encode the rest of the arguments.
2274 COMPOSEFUNC is program callable function that composes an outgoing
2275 mail message buffer. This function should set up the basics of the
2276 buffer without requiring user interaction. It should populate the
2277 standard mail headers, leaving the `to:' and `subject:' headers blank
2280 COMPOSEFUNC should accept several optional arguments--the same
2281 arguments that `compose-mail' takes. See that function's documentation.
2283 SENDFUNC is the command a user would run to send the message.
2285 Optional ABORTFUNC is the command a user would run to abort the
2286 message. For mail packages that don't have a separate abort function,
2287 this can be `kill-buffer' (the equivalent of omitting this argument).
2289 Optional HOOKVAR is a hook variable that gets run before the message
2290 is actually sent. Callers that use the `mail-user-agent' may
2291 install a hook function temporarily on this hook variable.
2292 If HOOKVAR is nil, `mail-send-hook' is used.
2294 The properties used on SYMBOL are `composefunc', `sendfunc',
2295 `abortfunc', and `hookvar'."
2296 (put symbol
'composefunc composefunc
)
2297 (put symbol
'sendfunc sendfunc
)
2298 (put symbol
'abortfunc
(or abortfunc
'kill-buffer
))
2299 (put symbol
'hookvar
(or hookvar
'mail-send-hook
)))
2301 ;;; subr.el ends here