1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
23 (defvar custom-declare-variable-list nil
24 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
25 Each element of this list holds the arguments to one call to `defcustom'.")
27 ;; Use this, rather than defcustom, in subr.el and other files loaded
29 (defun custom-declare-variable-early (&rest arguments)
30 (setq custom-declare-variable-list
31 (cons arguments custom-declare-variable-list)))
33 ;;;; Lisp language features.
35 (defmacro lambda (&rest cdr)
36 "Return a lambda expression.
37 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
38 self-quoting; the result of evaluating the lambda expression is the
39 expression itself. The lambda expression may then be treated as a
40 function, i.e., stored as the function value of a symbol, passed to
41 funcall or mapcar, etc.
43 ARGS should take the same form as an argument list for a `defun'.
44 DOCSTRING is an optional documentation string.
45 If present, it should describe how to call the function.
46 But documentation strings are usually not useful in nameless functions.
47 INTERACTIVE should be a call to the function `interactive', which see.
48 It may also be omitted.
49 BODY should be a list of lisp expressions."
50 ;; Note that this definition should not use backquotes; subr.el should not
51 ;; depend on backquote.el.
52 (list 'function (cons 'lambda cdr)))
54 (defmacro when (cond &rest body)
55 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
56 (list 'if cond (cons 'progn body)))
57 (put 'when 'lisp-indent-function 1)
58 (put 'when 'edebug-form-spec '(&rest form))
60 (defmacro unless (cond &rest body)
61 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
62 (cons 'if (cons cond (cons nil body))))
63 (put 'unless 'lisp-indent-function 1)
64 (put 'unless 'edebug-form-spec '(&rest form))
67 "Return the car of the car of X."
71 "Return the car of the cdr of X."
75 "Return the cdr of the car of X."
79 "Return the cdr of the cdr of X."
82 (defun last (x &optional n)
83 "Return the last link of the list X. Its car is the last element.
84 If X is nil, return nil.
85 If N is non-nil, return the Nth-to-last link of X.
86 If N is bigger than the length of X, return X."
90 (setq m (1+ m) p (cdr p)))
92 (if (< n m) (nthcdr (- m n) x) x)))
103 ;Prevent the \{...} documentation construct
104 ;from mentioning keys that run this command.
105 (put 'undefined 'suppress-keymap t)
107 (defun suppress-keymap (map &optional nodigits)
108 "Make MAP override all normally self-inserting keys to be undefined.
109 Normally, as an exception, digits and minus-sign are set to make prefix args,
110 but optional second arg NODIGITS non-nil treats them like other chars."
111 (substitute-key-definition 'self-insert-command 'undefined map global-map)
114 (define-key map "-" 'negative-argument)
115 ;; Make plain numbers do numeric args.
118 (define-key map (char-to-string loop) 'digit-argument)
119 (setq loop (1+ loop))))))
122 ;(defun copy-keymap (keymap)
123 ; "Return a copy of KEYMAP"
124 ; (while (not (keymapp keymap))
125 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
126 ; (if (vectorp keymap)
127 ; (copy-sequence keymap)
128 ; (copy-alist keymap)))
130 (defvar key-substitution-in-progress nil
131 "Used internally by substitute-key-definition.")
133 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
134 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
135 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
136 If optional fourth argument OLDMAP is specified, we redefine
137 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
138 (or prefix (setq prefix ""))
139 (let* ((scan (or oldmap keymap))
141 (prefix1 (vconcat prefix vec1))
142 (key-substitution-in-progress
143 (cons scan key-substitution-in-progress)))
144 ;; Scan OLDMAP, finding each char or event-symbol that
145 ;; has any definition, and act on it with hack-key.
147 (if (consp (car scan))
148 (let ((char (car (car scan)))
149 (defn (cdr (car scan))))
150 ;; The inside of this let duplicates exactly
151 ;; the inside of the following let that handles array elements.
153 (aset prefix1 (length prefix) char)
154 (let (inner-def skipped)
155 ;; Skip past menu-prompt.
156 (while (stringp (car-safe defn))
157 (setq skipped (cons (car defn) skipped))
158 (setq defn (cdr defn)))
159 ;; Skip past cached key-equivalence data for menu items.
160 (and (consp defn) (consp (car defn))
161 (setq defn (cdr defn)))
162 (setq inner-def defn)
163 ;; Look past a symbol that names a keymap.
164 (while (and (symbolp inner-def)
166 (setq inner-def (symbol-function inner-def)))
167 (if (or (eq defn olddef)
168 ;; Compare with equal if definition is a key sequence.
169 ;; That is useful for operating on function-key-map.
170 (and (or (stringp defn) (vectorp defn))
171 (equal defn olddef)))
172 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
173 (if (and (keymapp defn)
174 ;; Avoid recursively scanning
175 ;; where KEYMAP does not have a submap.
176 (let ((elt (lookup-key keymap prefix1)))
179 ;; Avoid recursively rescanning keymap being scanned.
181 key-substitution-in-progress)))
182 ;; If this one isn't being scanned already,
184 (substitute-key-definition olddef newdef keymap
187 (if (vectorp (car scan))
188 (let* ((array (car scan))
192 (let ((char i) (defn (aref array i)))
193 ;; The inside of this let duplicates exactly
194 ;; the inside of the previous let.
196 (aset prefix1 (length prefix) char)
197 (let (inner-def skipped)
198 ;; Skip past menu-prompt.
199 (while (stringp (car-safe defn))
200 (setq skipped (cons (car defn) skipped))
201 (setq defn (cdr defn)))
202 (and (consp defn) (consp (car defn))
203 (setq defn (cdr defn)))
204 (setq inner-def defn)
205 (while (and (symbolp inner-def)
207 (setq inner-def (symbol-function inner-def)))
208 (if (or (eq defn olddef)
209 (and (or (stringp defn) (vectorp defn))
210 (equal defn olddef)))
211 (define-key keymap prefix1
212 (nconc (nreverse skipped) newdef))
213 (if (and (keymapp defn)
214 (let ((elt (lookup-key keymap prefix1)))
218 key-substitution-in-progress)))
219 (substitute-key-definition olddef newdef keymap
223 (if (char-table-p (car scan))
225 (function (lambda (char defn)
227 ;; The inside of this let duplicates exactly
228 ;; the inside of the previous let,
229 ;; except that it uses set-char-table-range
230 ;; instead of define-key.
232 (aset prefix1 (length prefix) char)
233 (let (inner-def skipped)
234 ;; Skip past menu-prompt.
235 (while (stringp (car-safe defn))
236 (setq skipped (cons (car defn) skipped))
237 (setq defn (cdr defn)))
238 (and (consp defn) (consp (car defn))
239 (setq defn (cdr defn)))
240 (setq inner-def defn)
241 (while (and (symbolp inner-def)
243 (setq inner-def (symbol-function inner-def)))
244 (if (or (eq defn olddef)
245 (and (or (stringp defn) (vectorp defn))
246 (equal defn olddef)))
247 (define-key keymap prefix1
248 (nconc (nreverse skipped) newdef))
249 (if (and (keymapp defn)
250 (let ((elt (lookup-key keymap prefix1)))
254 key-substitution-in-progress)))
255 (substitute-key-definition olddef newdef keymap
259 (setq scan (cdr scan)))))
261 (defun define-key-after (keymap key definition after)
262 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
263 This is like `define-key' except that the binding for KEY is placed
264 just after the binding for the event AFTER, instead of at the beginning
265 of the map. Note that AFTER must be an event type (like KEY), NOT a command
268 If AFTER is t, the new binding goes at the end of the keymap.
270 KEY must contain just one event type--that is to say, it must be
271 a string or vector of length 1.
273 The order of bindings in a keymap matters when it is used as a menu."
276 (signal 'wrong-type-argument (list 'keymapp keymap)))
277 (if (> (length key) 1)
278 (error "multi-event key specified in `define-key-after'"))
279 (let ((tail keymap) done inserted
280 (first (aref key 0)))
281 (while (and (not done) tail)
282 ;; Delete any earlier bindings for the same key.
283 (if (eq (car-safe (car (cdr tail))) first)
284 (setcdr tail (cdr (cdr tail))))
285 ;; When we reach AFTER's binding, insert the new binding after.
286 ;; If we reach an inherited keymap, insert just before that.
287 ;; If we reach the end of this keymap, insert at the end.
288 (if (or (and (eq (car-safe (car tail)) after)
290 (eq (car (cdr tail)) 'keymap)
293 ;; Stop the scan only if we find a parent keymap.
294 ;; Keep going past the inserted element
295 ;; so we can delete any duplications that come later.
296 (if (eq (car (cdr tail)) 'keymap)
298 ;; Don't insert more than once.
300 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
302 (setq tail (cdr tail)))))
305 "Convert KEYS to the internal Emacs key representation.
306 KEYS should be a string constant in the format used for
307 saving keyboard macros (see `insert-kbd-macro')."
308 (read-kbd-macro keys))
310 (put 'keyboard-translate-table 'char-table-extra-slots 0)
312 (defun keyboard-translate (from to)
313 "Translate character FROM to TO at a low level.
314 This function creates a `keyboard-translate-table' if necessary
315 and then modifies one entry in it."
316 (or (char-table-p keyboard-translate-table)
317 (setq keyboard-translate-table
318 (make-char-table 'keyboard-translate-table nil)))
319 (aset keyboard-translate-table from to))
322 ;;;; The global keymap tree.
324 ;;; global-map, esc-map, and ctl-x-map have their values set up in
325 ;;; keymap.c; we just give them docstrings here.
327 (defvar global-map nil
328 "Default global keymap mapping Emacs keyboard input into commands.
329 The value is a keymap which is usually (but not necessarily) Emacs's
333 "Default keymap for ESC (meta) commands.
334 The normal global definition of the character ESC indirects to this keymap.")
336 (defvar ctl-x-map nil
337 "Default keymap for C-x commands.
338 The normal global definition of the character C-x indirects to this keymap.")
340 (defvar ctl-x-4-map (make-sparse-keymap)
341 "Keymap for subcommands of C-x 4")
342 (defalias 'ctl-x-4-prefix ctl-x-4-map)
343 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
345 (defvar ctl-x-5-map (make-sparse-keymap)
346 "Keymap for frame commands.")
347 (defalias 'ctl-x-5-prefix ctl-x-5-map)
348 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
351 ;;;; Event manipulation functions.
353 ;; The call to `read' is to ensure that the value is computed at load time
354 ;; and not compiled into the .elc file. The value is negative on most
355 ;; machines, but not on all!
356 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
358 (defun listify-key-sequence (key)
359 "Convert a key sequence to a list of events."
362 (mapcar (function (lambda (c)
364 (logxor c listify-key-sequence-1)
368 (defsubst eventp (obj)
369 "True if the argument is an event object."
372 (get obj 'event-symbol-elements))
375 (get (car obj) 'event-symbol-elements))))
377 (defun event-modifiers (event)
378 "Returns a list of symbols representing the modifier keys in event EVENT.
379 The elements of the list may include `meta', `control',
380 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
384 (setq type (car type)))
386 (cdr (get type 'event-symbol-elements))
388 (or (zerop (logand type ?\M-\^@))
389 (setq list (cons 'meta list)))
390 (or (and (zerop (logand type ?\C-\^@))
391 (>= (logand type 127) 32))
392 (setq list (cons 'control list)))
393 (or (and (zerop (logand type ?\S-\^@))
394 (= (logand type 255) (downcase (logand type 255))))
395 (setq list (cons 'shift list)))
396 (or (zerop (logand type ?\H-\^@))
397 (setq list (cons 'hyper list)))
398 (or (zerop (logand type ?\s-\^@))
399 (setq list (cons 'super list)))
400 (or (zerop (logand type ?\A-\^@))
401 (setq list (cons 'alt list)))
404 (defun event-basic-type (event)
405 "Returns the basic type of the given event (all modifiers removed).
406 The value is an ASCII printing character (not upper case) or a symbol."
408 (setq event (car event)))
410 (car (get event 'event-symbol-elements))
411 (let ((base (logand event (1- (lsh 1 18)))))
412 (downcase (if (< base 32) (logior base 64) base)))))
414 (defsubst mouse-movement-p (object)
415 "Return non-nil if OBJECT is a mouse movement event."
417 (eq (car object) 'mouse-movement)))
419 (defsubst event-start (event)
420 "Return the starting position of EVENT.
421 If EVENT is a mouse press or a mouse click, this returns the location
423 If EVENT is a drag, this returns the drag's starting position.
424 The return value is of the form
425 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
426 The `posn-' functions access elements of such lists."
429 (defsubst event-end (event)
430 "Return the ending location of EVENT. EVENT should be a click or drag event.
431 If EVENT is a click event, this function is the same as `event-start'.
432 The return value is of the form
433 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
434 The `posn-' functions access elements of such lists."
435 (nth (if (consp (nth 2 event)) 2 1) event))
437 (defsubst event-click-count (event)
438 "Return the multi-click count of EVENT, a click or drag event.
439 The return value is a positive integer."
440 (if (integerp (nth 2 event)) (nth 2 event) 1))
442 (defsubst posn-window (position)
443 "Return the window in POSITION.
444 POSITION should be a list of the form
445 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
446 as returned by the `event-start' and `event-end' functions."
449 (defsubst posn-point (position)
450 "Return the buffer location in POSITION.
451 POSITION should be a list of the form
452 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
453 as returned by the `event-start' and `event-end' functions."
454 (if (consp (nth 1 position))
455 (car (nth 1 position))
458 (defsubst posn-x-y (position)
459 "Return the x and y coordinates in POSITION.
460 POSITION should be a list of the form
461 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
462 as returned by the `event-start' and `event-end' functions."
465 (defun posn-col-row (position)
466 "Return the column and row in POSITION, measured in characters.
467 POSITION should be a list of the form
468 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
469 as returned by the `event-start' and `event-end' functions.
470 For a scroll-bar event, the result column is 0, and the row
471 corresponds to the vertical position of the click in the scroll bar."
472 (let ((pair (nth 2 position))
473 (window (posn-window position)))
474 (if (eq (if (consp (nth 1 position))
475 (car (nth 1 position))
477 'vertical-scroll-bar)
478 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
479 (if (eq (if (consp (nth 1 position))
480 (car (nth 1 position))
482 'horizontal-scroll-bar)
483 (cons (scroll-bar-scale pair (window-width window)) 0)
484 (let* ((frame (if (framep window) window (window-frame window)))
485 (x (/ (car pair) (frame-char-width frame)))
486 (y (/ (cdr pair) (frame-char-height frame))))
489 (defsubst posn-timestamp (position)
490 "Return the timestamp of POSITION.
491 POSITION should be a list of the form
492 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
493 as returned by the `event-start' and `event-end' functions."
497 ;;;; Obsolescent names for functions.
499 (defalias 'dot 'point)
500 (defalias 'dot-marker 'point-marker)
501 (defalias 'dot-min 'point-min)
502 (defalias 'dot-max 'point-max)
503 (defalias 'window-dot 'window-point)
504 (defalias 'set-window-dot 'set-window-point)
505 (defalias 'read-input 'read-string)
506 (defalias 'send-string 'process-send-string)
507 (defalias 'send-region 'process-send-region)
508 (defalias 'show-buffer 'set-window-buffer)
509 (defalias 'buffer-flush-undo 'buffer-disable-undo)
510 (defalias 'eval-current-buffer 'eval-buffer)
511 (defalias 'compiled-function-p 'byte-code-function-p)
512 (defalias 'define-function 'defalias)
514 (defun sref (string byte-index)
515 "Obsolete function returning a character in STRING at BYTE-INDEX.
516 Please convert your programs to use `aref' with character-base index."
517 (let ((byte 0) (char 0))
518 (while (< byte byte-index)
519 (setq byte (+ byte (char-bytes (aref string char)))
523 ;; Some programs still use this as a function.
525 "Obsolete function returning the value of the `baud-rate' variable.
526 Please convert your programs to use the variable `baud-rate' directly."
529 (defalias 'focus-frame 'ignore)
530 (defalias 'unfocus-frame 'ignore)
532 ;;;; Alternate names for functions - these are not being phased out.
534 (defalias 'string= 'string-equal)
535 (defalias 'string< 'string-lessp)
536 (defalias 'move-marker 'set-marker)
537 (defalias 'not 'null)
538 (defalias 'rplaca 'setcar)
539 (defalias 'rplacd 'setcdr)
540 (defalias 'beep 'ding) ;preserve lingual purity
541 (defalias 'indent-to-column 'indent-to)
542 (defalias 'backward-delete-char 'delete-backward-char)
543 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
544 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
545 (defalias 'int-to-string 'number-to-string)
546 (defalias 'store-match-data 'set-match-data)
548 ;;; Should this be an obsolete name? If you decide it should, you get
549 ;;; to go through all the sources and change them.
550 (defalias 'string-to-int 'string-to-number)
552 ;;;; Hook manipulation functions.
554 (defun make-local-hook (hook)
555 "Make the hook HOOK local to the current buffer.
556 When a hook is local, its local and global values
557 work in concert: running the hook actually runs all the hook
558 functions listed in *either* the local value *or* the global value
559 of the hook variable.
561 This function works by making `t' a member of the buffer-local value,
562 which acts as a flag to run the hook functions in the default value as
563 well. This works for all normal hooks, but does not work for most
564 non-normal hooks yet. We will be changing the callers of non-normal
565 hooks so that they can handle localness; this has to be done one by
568 This function does nothing if HOOK is already local in the current
571 Do not use `make-local-variable' to make a hook variable buffer-local."
572 (if (local-variable-p hook)
574 (or (boundp hook) (set hook nil))
575 (make-local-variable hook)
576 (set hook (list t))))
578 (defun add-hook (hook function &optional append local)
579 "Add to the value of HOOK the function FUNCTION.
580 FUNCTION is not added if already present.
581 FUNCTION is added (if necessary) at the beginning of the hook list
582 unless the optional argument APPEND is non-nil, in which case
583 FUNCTION is added at the end.
585 The optional fourth argument, LOCAL, if non-nil, says to modify
586 the hook's buffer-local value rather than its default value.
587 This makes no difference if the hook is not buffer-local.
588 To make a hook variable buffer-local, always use
589 `make-local-hook', not `make-local-variable'.
591 HOOK should be a symbol, and FUNCTION may be any valid function. If
592 HOOK is void, it is first set to nil. If HOOK's value is a single
593 function, it is changed to a list of functions."
594 (or (boundp hook) (set hook nil))
595 (or (default-boundp hook) (set-default hook nil))
596 ;; If the hook value is a single function, turn it into a list.
597 (let ((old (symbol-value hook)))
598 (if (or (not (listp old)) (eq (car old) 'lambda))
599 (set hook (list old))))
601 ;; Detect the case where make-local-variable was used on a hook
602 ;; and do what we used to do.
603 (and (local-variable-if-set-p hook)
604 (not (memq t (symbol-value hook)))))
605 ;; Alter the local value only.
606 (or (if (consp function)
607 (member function (symbol-value hook))
608 (memq function (symbol-value hook)))
611 (append (symbol-value hook) (list function))
612 (cons function (symbol-value hook)))))
613 ;; Alter the global value (which is also the only value,
614 ;; if the hook doesn't have a local value).
615 (or (if (consp function)
616 (member function (default-value hook))
617 (memq function (default-value hook)))
620 (append (default-value hook) (list function))
621 (cons function (default-value hook)))))))
623 (defun remove-hook (hook function &optional local)
624 "Remove from the value of HOOK the function FUNCTION.
625 HOOK should be a symbol, and FUNCTION may be any valid function. If
626 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
627 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
629 The optional third argument, LOCAL, if non-nil, says to modify
630 the hook's buffer-local value rather than its default value.
631 This makes no difference if the hook is not buffer-local.
632 To make a hook variable buffer-local, always use
633 `make-local-hook', not `make-local-variable'."
634 (if (or (not (boundp hook)) ;unbound symbol, or
635 (not (default-boundp hook))
636 (null (symbol-value hook)) ;value is nil, or
637 (null function)) ;function is nil, then
640 ;; Detect the case where make-local-variable was used on a hook
641 ;; and do what we used to do.
642 (and (local-variable-p hook)
643 (not (memq t (symbol-value hook)))))
644 (let ((hook-value (symbol-value hook)))
645 (if (consp hook-value)
646 (if (member function hook-value)
647 (setq hook-value (delete function (copy-sequence hook-value))))
648 (if (equal hook-value function)
649 (setq hook-value nil)))
650 (set hook hook-value))
651 (let ((hook-value (default-value hook)))
652 (if (consp hook-value)
653 (if (member function hook-value)
654 (setq hook-value (delete function (copy-sequence hook-value))))
655 (if (equal hook-value function)
656 (setq hook-value nil)))
657 (set-default hook hook-value)))))
659 (defun add-to-list (list-var element)
660 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
661 The test for presence of ELEMENT is done with `equal'.
662 If you want to use `add-to-list' on a variable that is not defined
663 until a certain package is loaded, you should put the call to `add-to-list'
664 into a hook function that will be run only after loading the package.
665 `eval-after-load' provides one way to do this. In some cases
666 other hooks, such as major mode hooks, can do the job."
667 (if (member element (symbol-value list-var))
668 (symbol-value list-var)
669 (set list-var (cons element (symbol-value list-var)))))
671 ;;;; Specifying things to do after certain files are loaded.
673 (defun eval-after-load (file form)
674 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
675 This makes or adds to an entry on `after-load-alist'.
676 If FILE is already loaded, evaluate FORM right now.
677 It does nothing if FORM is already on the list for FILE.
678 FILE should be the name of a library, with no directory name."
679 ;; Make sure there is an element for FILE.
680 (or (assoc file after-load-alist)
681 (setq after-load-alist (cons (list file) after-load-alist)))
682 ;; Add FORM to the element if it isn't there.
683 (let ((elt (assoc file after-load-alist)))
684 (or (member form (cdr elt))
686 (nconc elt (list form))
687 ;; If the file has been loaded already, run FORM right away.
688 (and (assoc file load-history)
692 (defun eval-next-after-load (file)
693 "Read the following input sexp, and run it whenever FILE is loaded.
694 This makes or adds to an entry on `after-load-alist'.
695 FILE should be the name of a library, with no directory name."
696 (eval-after-load file (read)))
699 ;;;; Input and display facilities.
701 (defvar read-quoted-char-radix 8
702 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
703 Legitimate radix values are 8, 10 and 16.")
705 (custom-declare-variable-early
706 'read-quoted-char-radix 8
707 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
708 Legitimate radix values are 8, 10 and 16."
709 :type '(choice (const 8) (const 10) (const 16))
710 :group 'editing-basics)
712 (defun read-quoted-char (&optional prompt)
713 "Like `read-char', but do not allow quitting.
714 Also, if the first character read is an octal digit,
715 we read any number of octal digits and return the
716 specified character code. Any nondigit terminates the sequence.
717 If the terminator is RET, it is discarded;
718 any other terminator is used itself as input.
720 The optional argument PROMPT specifies a string to use to prompt the user.
721 The variable `read-quoted-char-radix' controls which radix to use
723 (let ((message-log-max nil) done (first t) (code 0) char)
725 (let ((inhibit-quit first)
726 ;; Don't let C-h get the help message--only help function keys.
729 "Type the special character you want to use,
730 or the octal character code.
731 RET terminates the character code and is discarded;
732 any other non-digit terminates the character code and is then used as input."))
733 (and prompt (message "%s-" prompt))
734 (setq char (read-event))
735 (if inhibit-quit (setq quit-flag nil)))
736 ;; Translate TAB key into control-I ASCII character, and so on.
738 (let ((translated (lookup-key function-key-map (vector char))))
739 (if (arrayp translated)
740 (setq char (aref translated 0)))))
742 ((not (integerp char))
743 (setq unread-command-events (list char)
745 ((/= (logand char ?\M-\^@) 0)
746 ;; Turn a meta-character into a character with the 0200 bit set.
747 (setq code (logior (logand char (lognot ?\M-\^@)) 128)
749 ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix))))
750 (setq code (+ (* code read-quoted-char-radix) (- char ?0)))
751 (and prompt (setq prompt (message "%s %c" prompt char))))
752 ((and (<= ?a (downcase char))
753 (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix))))
754 (setq code (+ (* code read-quoted-char-radix)
755 (+ 10 (- (downcase char) ?a))))
756 (and prompt (setq prompt (message "%s %c" prompt char))))
757 ((and (not first) (eq char ?\C-m))
760 (setq unread-command-events (list char)
767 (defun read-passwd (prompt &optional confirm default)
768 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
769 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
770 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
771 Optional DEFAULT is a default password to use instead of empty input."
775 (let ((first (read-passwd prompt nil default))
776 (second (read-passwd "Confirm password: " nil default)))
777 (if (equal first second)
779 (message "Password not repeated accurately; please start over")
785 (cursor-in-echo-area t))
786 (while (progn (message "%s%s"
788 (make-string (length pass) ?.))
790 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
793 (if (and (/= c ?\b) (/= c ?\177))
794 (setq pass (concat pass (char-to-string c)))
795 (if (> (length pass) 0)
796 (setq pass (substring pass 0 -1))))))
798 (or pass default ""))))
800 (defun force-mode-line-update (&optional all)
801 "Force the mode-line of the current buffer to be redisplayed.
802 With optional non-nil ALL, force redisplay of all mode-lines."
803 (if all (save-excursion (set-buffer (other-buffer))))
804 (set-buffer-modified-p (buffer-modified-p)))
806 (defun momentary-string-display (string pos &optional exit-char message)
807 "Momentarily display STRING in the buffer at POS.
808 Display remains until next character is typed.
809 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
810 otherwise it is then available as input (as a command if nothing else).
811 Display MESSAGE (optional fourth arg) in the echo area.
812 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
813 (or exit-char (setq exit-char ?\ ))
814 (let ((buffer-read-only nil)
815 ;; Don't modify the undo list at all.
817 (modified (buffer-modified-p))
818 (name buffer-file-name)
824 ;; defeat file locking... don't try this at home, kids!
825 (setq buffer-file-name nil)
826 (insert-before-markers string)
827 (setq insert-end (point))
828 ;; If the message end is off screen, recenter now.
829 (if (< (window-end nil t) insert-end)
830 (recenter (/ (window-height) 2)))
831 ;; If that pushed message start off the screen,
832 ;; scroll to start it at the top of the screen.
833 (move-to-window-line 0)
838 (message (or message "Type %s to continue editing.")
839 (single-key-description exit-char))
840 (let ((char (read-event)))
841 (or (eq char exit-char)
842 (setq unread-command-events (list char)))))
845 (delete-region pos insert-end)))
846 (setq buffer-file-name name)
847 (set-buffer-modified-p modified))))
852 ;; A number of major modes set this locally.
853 ;; Give it a global value to avoid compiler warnings.
854 (defvar font-lock-defaults nil)
856 (defvar suspend-hook nil
857 "Normal hook run by `suspend-emacs', before suspending.")
859 (defvar suspend-resume-hook nil
860 "Normal hook run by `suspend-emacs', after Emacs is continued.")
862 ;; Avoid compiler warnings about this variable,
863 ;; which has a special meaning on certain system types.
864 (defvar buffer-file-type nil
865 "Non-nil if the visited file is a binary file.
866 This variable is meaningful on MS-DOG and Windows NT.
867 On those systems, it is automatically local in every buffer.
868 On other systems, this variable is normally always nil.")
870 ;; This should probably be written in C (i.e., without using `walk-windows').
871 (defun get-buffer-window-list (buffer &optional minibuf frame)
872 "Return windows currently displaying BUFFER, or nil if none.
873 See `walk-windows' for the meaning of MINIBUF and FRAME."
874 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
875 (walk-windows (function (lambda (window)
876 (if (eq (window-buffer window) buffer)
877 (setq windows (cons window windows)))))
881 (defun ignore (&rest ignore)
882 "Do nothing and return nil.
883 This function accepts any number of arguments, but ignores them."
887 (defun error (&rest args)
888 "Signal an error, making error message by passing all args to `format'.
889 In Emacs, the convention is that error messages start with a capital
890 letter but *do not* end with a period. Please follow this convention
891 for the sake of consistency."
893 (signal 'error (list (apply 'format args)))))
895 (defalias 'user-original-login-name 'user-login-name)
897 (defun start-process-shell-command (name buffer &rest args)
898 "Start a program in a subprocess. Return the process object for it.
899 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
900 NAME is name for process. It is modified if necessary to make it unique.
901 BUFFER is the buffer or (buffer-name) to associate with the process.
902 Process output goes at end of that buffer, unless you specify
903 an output stream or filter function to handle the output.
904 BUFFER may be also nil, meaning that this process is not associated
906 Third arg is command name, the name of a shell command.
907 Remaining arguments are the arguments for the command.
908 Wildcards and redirection are handled as usual in the shell."
910 ((eq system-type 'vax-vms)
911 (apply 'start-process name buffer args))
912 ;; We used to use `exec' to replace the shell with the command,
913 ;; but that failed to handle (...) and semicolon, etc.
915 (start-process name buffer shell-file-name shell-command-switch
916 (mapconcat 'identity args " ")))))
918 (defmacro with-current-buffer (buffer &rest body)
919 "Execute the forms in BODY with BUFFER as the current buffer.
920 The value returned is the value of the last form in BODY.
921 See also `with-temp-buffer'."
922 `(save-current-buffer
926 (defmacro with-temp-file (file &rest forms)
927 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
928 The value of the last form in FORMS is returned, like `progn'.
929 See also `with-temp-buffer'."
930 (let ((temp-file (make-symbol "temp-file"))
931 (temp-buffer (make-symbol "temp-buffer")))
932 `(let ((,temp-file ,file)
934 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
937 (with-current-buffer ,temp-buffer
939 (with-current-buffer ,temp-buffer
941 (write-region (point-min) (point-max) ,temp-file nil 0)))
942 (and (buffer-name ,temp-buffer)
943 (kill-buffer ,temp-buffer))))))
945 (defmacro with-temp-buffer (&rest forms)
946 "Create a temporary buffer, and evaluate FORMS there like `progn'.
947 See also `with-temp-file' and `with-output-to-string'."
948 (let ((temp-buffer (make-symbol "temp-buffer")))
950 (get-buffer-create (generate-new-buffer-name " *temp*"))))
952 (with-current-buffer ,temp-buffer
954 (and (buffer-name ,temp-buffer)
955 (kill-buffer ,temp-buffer))))))
957 (defmacro with-output-to-string (&rest body)
958 "Execute BODY, return the text it sent to `standard-output', as a string."
959 `(let ((standard-output
960 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
961 (let ((standard-output standard-output))
963 (with-current-buffer standard-output
966 (kill-buffer nil)))))
968 (defmacro combine-after-change-calls (&rest body)
969 "Execute BODY, but don't call the after-change functions till the end.
970 If BODY makes changes in the buffer, they are recorded
971 and the functions on `after-change-functions' are called several times
972 when BODY is finished.
973 The return value is the value of the last form in BODY.
975 If `before-change-functions' is non-nil, then calls to the after-change
976 functions can't be deferred, so in that case this macro has no effect.
978 Do not alter `after-change-functions' or `before-change-functions'
981 (let ((combine-after-change-calls t))
983 (combine-after-change-execute)))
986 (defvar save-match-data-internal)
988 ;; We use save-match-data-internal as the local variable because
989 ;; that works ok in practice (people should not use that variable elsewhere).
990 ;; We used to use an uninterned symbol; the compiler handles that properly
991 ;; now, but it generates slower code.
992 (defmacro save-match-data (&rest body)
993 "Execute the BODY forms, restoring the global value of the match data."
994 `(let ((save-match-data-internal (match-data)))
997 (set-match-data save-match-data-internal))))
999 (defun match-string (num &optional string)
1000 "Return string of text matched by last search.
1001 NUM specifies which parenthesized expression in the last regexp.
1002 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1003 Zero means the entire text matched by the whole regexp or whole string.
1004 STRING should be given if the last search was by `string-match' on STRING."
1005 (if (match-beginning num)
1007 (substring string (match-beginning num) (match-end num))
1008 (buffer-substring (match-beginning num) (match-end num)))))
1010 (defun match-string-no-properties (num &optional string)
1011 "Return string of text matched by last search, without text properties.
1012 NUM specifies which parenthesized expression in the last regexp.
1013 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1014 Zero means the entire text matched by the whole regexp or whole string.
1015 STRING should be given if the last search was by `string-match' on STRING."
1016 (if (match-beginning num)
1019 (substring string (match-beginning num) (match-end num))))
1020 (set-text-properties 0 (length result) nil result)
1022 (buffer-substring-no-properties (match-beginning num)
1025 (defun split-string (string &optional separators)
1026 "Splits STRING into substrings where there are matches for SEPARATORS.
1027 Each match for SEPARATORS is a splitting point.
1028 The substrings between the splitting points are made into a list
1030 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1032 If there is match for SEPARATORS at the beginning of STRING, we do not
1033 include a null substring for that. Likewise, if there is a match
1034 at the end of STRING, we don't include a null substring for that."
1035 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1039 (while (and (string-match rexp string
1041 (= start (match-beginning 0))
1042 (< start (length string)))
1044 (< (match-beginning 0) (length string)))
1046 (or (eq (match-beginning 0) 0)
1047 (and (eq (match-beginning 0) (match-end 0))
1048 (eq (match-beginning 0) start))
1050 (cons (substring string start (match-beginning 0))
1052 (setq start (match-end 0)))
1053 (or (eq start (length string))
1055 (cons (substring string start)
1059 (defun shell-quote-argument (argument)
1060 "Quote an argument for passing as argument to an inferior shell."
1061 (if (eq system-type 'ms-dos)
1062 ;; MS-DOS shells don't have quoting, so don't do any.
1064 (if (eq system-type 'windows-nt)
1065 (concat "\"" argument "\"")
1066 (if (equal argument "")
1068 ;; Quote everything except POSIX filename characters.
1069 ;; This should be safe enough even for really weird shells.
1070 (let ((result "") (start 0) end)
1071 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1072 (setq end (match-beginning 0)
1073 result (concat result (substring argument start end)
1074 "\\" (substring argument end (1+ end)))
1076 (concat result (substring argument start)))))))
1078 (defun make-syntax-table (&optional oldtable)
1079 "Return a new syntax table.
1080 If OLDTABLE is non-nil, copy OLDTABLE.
1081 Otherwise, create a syntax table which inherits
1082 all letters and control characters from the standard syntax table;
1083 other characters are copied from the standard syntax table."
1085 (copy-syntax-table oldtable)
1086 (let ((table (copy-syntax-table))
1106 (defun add-to-invisibility-spec (arg)
1107 "Add elements to `buffer-invisibility-spec'.
1108 See documentation for `buffer-invisibility-spec' for the kind of elements
1111 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
1112 (setq buffer-invisibility-spec (list arg)))
1114 (setq buffer-invisibility-spec
1115 (cons arg buffer-invisibility-spec)))))
1117 (defun remove-from-invisibility-spec (arg)
1118 "Remove elements from `buffer-invisibility-spec'."
1119 (if buffer-invisibility-spec
1120 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
1122 (defun global-set-key (key command)
1123 "Give KEY a global binding as COMMAND.
1124 COMMAND is the command definition to use; usually it is
1125 a symbol naming an interactively-callable function.
1126 KEY is a key sequence; noninteractively, it is a string or vector
1127 of characters or event types, and non-ASCII characters with codes
1128 above 127 (such as ISO Latin-1) can be included if you use a vector.
1130 Note that if KEY has a local binding in the current buffer,
1131 that local binding will continue to shadow any global binding
1132 that you make with this function."
1133 (interactive "KSet key globally: \nCSet key %s to command: ")
1134 (or (vectorp key) (stringp key)
1135 (signal 'wrong-type-argument (list 'arrayp key)))
1136 (define-key (current-global-map) key command))
1138 (defun local-set-key (key command)
1139 "Give KEY a local binding as COMMAND.
1140 COMMAND is the command definition to use; usually it is
1141 a symbol naming an interactively-callable function.
1142 KEY is a key sequence; noninteractively, it is a string or vector
1143 of characters or event types, and non-ASCII characters with codes
1144 above 127 (such as ISO Latin-1) can be included if you use a vector.
1146 The binding goes in the current buffer's local map,
1147 which in most cases is shared with all other buffers in the same major mode."
1148 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1149 (let ((map (current-local-map)))
1151 (use-local-map (setq map (make-sparse-keymap))))
1152 (or (vectorp key) (stringp key)
1153 (signal 'wrong-type-argument (list 'arrayp key)))
1154 (define-key map key command)))
1156 (defun global-unset-key (key)
1157 "Remove global binding of KEY.
1158 KEY is a string representing a sequence of keystrokes."
1159 (interactive "kUnset key globally: ")
1160 (global-set-key key nil))
1162 (defun local-unset-key (key)
1163 "Remove local binding of KEY.
1164 KEY is a string representing a sequence of keystrokes."
1165 (interactive "kUnset key locally: ")
1166 (if (current-local-map)
1167 (local-set-key key nil))
1170 ;; We put this here instead of in frame.el so that it's defined even on
1171 ;; systems where frame.el isn't loaded.
1172 (defun frame-configuration-p (object)
1173 "Return non-nil if OBJECT seems to be a frame configuration.
1174 Any list whose car is `frame-configuration' is assumed to be a frame
1177 (eq (car object) 'frame-configuration)))
1179 (defun functionp (object)
1180 "Non-nil if OBJECT is a type of object that can be called as a function."
1181 (or (subrp object) (byte-code-function-p object)
1182 (eq (car-safe object) 'lambda)
1183 (and (symbolp object) (fboundp object))))
1186 ;(defun nth (n list)
1187 ; "Returns the Nth element of LIST.
1188 ;N counts from zero. If LIST is not that long, nil is returned."
1189 ; (car (nthcdr n list)))
1191 ;(defun copy-alist (alist)
1192 ; "Return a copy of ALIST.
1193 ;This is a new alist which represents the same mapping
1194 ;from objects to objects, but does not share the alist structure with ALIST.
1195 ;The objects mapped (cars and cdrs of elements of the alist)
1196 ;are shared, however."
1197 ; (setq alist (copy-sequence alist))
1198 ; (let ((tail alist))
1200 ; (if (consp (car tail))
1201 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1202 ; (setq tail (cdr tail))))
1205 ;;; subr.el ends here