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
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;;; Lisp language features.
26 (defmacro lambda (&rest cdr)
27 "Return a lambda expression.
28 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
29 self-quoting; the result of evaluating the lambda expression is the
30 expression itself. The lambda expression may then be treated as a
31 function, i.e., stored as the function value of a symbol, passed to
32 funcall or mapcar, etc.
34 ARGS should take the same form as an argument list for a `defun'.
35 DOCSTRING should be a string, as described for `defun'. It may be omitted.
36 INTERACTIVE should be a call to the function `interactive', which see.
37 It may also be omitted.
38 BODY should be a list of lisp expressions."
39 ;; Note that this definition should not use backquotes; subr.el should not
40 ;; depend on backquote.el.
41 (list 'function (cons 'lambda cdr)))
43 ;;(defmacro defun-inline (name args &rest body)
44 ;; "Create an \"inline defun\" (actually a macro).
45 ;;Use just like `defun'."
46 ;; (nconc (list 'defmacro name '(&rest args))
47 ;; (if (stringp (car body))
48 ;; (prog1 (list (car body))
49 ;; (setq body (or (cdr body) body))))
50 ;; (list (list 'cons (list 'quote
51 ;; (cons 'lambda (cons args body)))
61 ;Prevent the \{...} documentation construct
62 ;from mentioning keys that run this command.
63 (put 'undefined 'suppress-keymap t)
65 (defun suppress-keymap (map &optional nodigits)
66 "Make MAP override all normally self-inserting keys to be undefined.
67 Normally, as an exception, digits and minus-sign are set to make prefix args,
68 but optional second arg NODIGITS non-nil treats them like other chars."
69 (substitute-key-definition 'self-insert-command 'undefined map global-map)
72 (define-key map "-" 'negative-argument)
73 ;; Make plain numbers do numeric args.
76 (define-key map (char-to-string loop) 'digit-argument)
77 (setq loop (1+ loop))))))
80 ;(defun copy-keymap (keymap)
81 ; "Return a copy of KEYMAP"
82 ; (while (not (keymapp keymap))
83 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
84 ; (if (vectorp keymap)
85 ; (copy-sequence keymap)
86 ; (copy-alist keymap)))
88 (defvar key-substitution-in-progress nil
89 "Used internally by substitute-key-definition.")
91 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
92 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
93 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
94 If optional fourth argument OLDMAP is specified, we redefine
95 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
96 (or prefix (setq prefix ""))
97 (let* ((scan (or oldmap keymap))
99 (prefix1 (vconcat prefix vec1))
100 (key-substitution-in-progress
101 (cons scan key-substitution-in-progress)))
102 ;; Scan OLDMAP, finding each char or event-symbol that
103 ;; has any definition, and act on it with hack-key.
105 (if (consp (car scan))
106 (let ((char (car (car scan)))
107 (defn (cdr (car scan))))
108 ;; The inside of this let duplicates exactly
109 ;; the inside of the following let that handles array elements.
111 (aset prefix1 (length prefix) char)
112 (let (inner-def skipped)
113 ;; Skip past menu-prompt.
114 (while (stringp (car-safe defn))
115 (setq skipped (cons (car defn) skipped))
116 (setq defn (cdr defn)))
117 ;; Skip past cached key-equivalence data for menu items.
118 (and (consp defn) (consp (car defn))
119 (setq defn (cdr defn)))
120 (setq inner-def defn)
121 ;; Look past a symbol that names a keymap.
122 (while (and (symbolp inner-def)
124 (setq inner-def (symbol-function inner-def)))
126 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
127 (if (and (keymapp defn)
128 ;; Avoid recursively scanning
129 ;; where KEYMAP does not have a submap.
130 (keymapp (lookup-key keymap prefix1))
131 ;; Avoid recursively rescanning keymap being scanned.
133 key-substitution-in-progress)))
134 ;; If this one isn't being scanned already,
136 (substitute-key-definition olddef newdef keymap
139 (if (arrayp (car scan))
140 (let* ((array (car scan))
144 (let ((char i) (defn (aref array i)))
145 ;; The inside of this let duplicates exactly
146 ;; the inside of the previous let.
148 (aset prefix1 (length prefix) char)
149 (let (inner-def skipped)
150 ;; Skip past menu-prompt.
151 (while (stringp (car-safe defn))
152 (setq skipped (cons (car defn) skipped))
153 (setq defn (cdr defn)))
154 (and (consp defn) (consp (car defn))
155 (setq defn (cdr defn)))
156 (setq inner-def defn)
157 (while (and (symbolp inner-def)
159 (setq inner-def (symbol-function inner-def)))
161 (define-key keymap prefix1
162 (nconc (nreverse skipped) newdef))
163 (if (and (keymapp defn)
164 (keymapp (lookup-key keymap prefix1))
166 key-substitution-in-progress)))
167 (substitute-key-definition olddef newdef keymap
171 (setq scan (cdr scan)))))
173 (defun define-key-after (keymap key definition after)
174 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
175 This is like `define-key' except that the binding for KEY is placed
176 just after the binding for the event AFTER, instead of at the beginning
178 The order matters when the keymap is used as a menu.
179 KEY must contain just one event type--that is to say, it must be
180 a string or vector of length 1."
182 (signal 'wrong-type-argument (list 'keymapp keymap)))
183 (if (> (length key) 1)
184 (error "multi-event key specified in `define-key-after'"))
185 (let ((tail keymap) done inserted
186 (first (aref key 0)))
187 (while (and (not done) tail)
188 ;; Delete any earlier bindings for the same key.
189 (if (eq (car-safe (car (cdr tail))) first)
190 (setcdr tail (cdr (cdr tail))))
191 ;; When we reach AFTER's binding, insert the new binding after.
192 ;; If we reach an inherited keymap, insert just before that.
193 ;; If we reach the end of this keymap, insert at the end.
194 (if (or (eq (car-safe (car tail)) after)
195 (eq (car (cdr tail)) 'keymap)
198 ;; Stop the scan only if we find a parent keymap.
199 ;; Keep going past the inserted element
200 ;; so we can delete any duplications that come later.
201 (if (eq (car (cdr tail)) 'keymap)
203 ;; Don't insert more than once.
205 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
207 (setq tail (cdr tail)))))
209 (defun keyboard-translate (from to)
210 "Translate character FROM to TO at a low level.
211 This function creates a `keyboard-translate-table' if necessary
212 and then modifies one entry in it."
213 (or (arrayp keyboard-translate-table)
214 (setq keyboard-translate-table ""))
215 (if (or (> from (length keyboard-translate-table))
216 (> to (length keyboard-translate-table)))
218 (let* ((i (length keyboard-translate-table))
219 (table (concat keyboard-translate-table
220 (make-string (- 256 i) 0))))
224 (setq keyboard-translate-table table))))
225 (aset keyboard-translate-table from to))
228 ;;;; The global keymap tree.
230 ;;; global-map, esc-map, and ctl-x-map have their values set up in
231 ;;; keymap.c; we just give them docstrings here.
233 (defvar global-map nil
234 "Default global keymap mapping Emacs keyboard input into commands.
235 The value is a keymap which is usually (but not necessarily) Emacs's
239 "Default keymap for ESC (meta) commands.
240 The normal global definition of the character ESC indirects to this keymap.")
242 (defvar ctl-x-map nil
243 "Default keymap for C-x commands.
244 The normal global definition of the character C-x indirects to this keymap.")
246 (defvar ctl-x-4-map (make-sparse-keymap)
247 "Keymap for subcommands of C-x 4")
248 (defalias 'ctl-x-4-prefix ctl-x-4-map)
249 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
251 (defvar ctl-x-5-map (make-sparse-keymap)
252 "Keymap for frame commands.")
253 (defalias 'ctl-x-5-prefix ctl-x-5-map)
254 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
257 ;;;; Event manipulation functions.
259 ;; The call to `read' is to ensure that the value is computed at load time
260 ;; and not compiled into the .elc file. The value is negative on most
261 ;; machines, but not on all!
262 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
264 (defun listify-key-sequence (key)
265 "Convert a key sequence to a list of events."
268 (mapcar (function (lambda (c)
270 (logxor c listify-key-sequence-1)
274 (defsubst eventp (obj)
275 "True if the argument is an event object."
278 (get obj 'event-symbol-elements))
281 (get (car obj) 'event-symbol-elements))))
283 (defun event-modifiers (event)
284 "Returns a list of symbols representing the modifier keys in event EVENT.
285 The elements of the list may include `meta', `control',
286 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
290 (setq type (car type)))
292 (cdr (get type 'event-symbol-elements))
294 (or (zerop (logand type ?\M-\^@))
295 (setq list (cons 'meta list)))
296 (or (and (zerop (logand type ?\C-\^@))
297 (>= (logand type 127) 32))
298 (setq list (cons 'control list)))
299 (or (and (zerop (logand type ?\S-\^@))
300 (= (logand type 255) (downcase (logand type 255))))
301 (setq list (cons 'shift list)))
302 (or (zerop (logand type ?\H-\^@))
303 (setq list (cons 'hyper list)))
304 (or (zerop (logand type ?\s-\^@))
305 (setq list (cons 'super list)))
306 (or (zerop (logand type ?\A-\^@))
307 (setq list (cons 'alt list)))
310 (defun event-basic-type (event)
311 "Returns the basic type of the given event (all modifiers removed).
312 The value is an ASCII printing character (not upper case) or a symbol."
314 (setq event (car event)))
316 (car (get event 'event-symbol-elements))
317 (let ((base (logand event (1- (lsh 1 18)))))
318 (downcase (if (< base 32) (logior base 64) base)))))
320 (defsubst mouse-movement-p (object)
321 "Return non-nil if OBJECT is a mouse movement event."
323 (eq (car object) 'mouse-movement)))
325 (defsubst event-start (event)
326 "Return the starting position of EVENT.
327 If EVENT is a mouse press or a mouse click, this returns the location
329 If EVENT is a drag, this returns the drag's starting position.
330 The return value is of the form
331 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
332 The `posn-' functions access elements of such lists."
335 (defsubst event-end (event)
336 "Return the ending location of EVENT. EVENT should be a click or drag event.
337 If EVENT is a click event, this function is the same as `event-start'.
338 The return value is of the form
339 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
340 The `posn-' functions access elements of such lists."
341 (nth (if (consp (nth 2 event)) 2 1) event))
343 (defsubst event-click-count (event)
344 "Return the multi-click count of EVENT, a click or drag event.
345 The return value is a positive integer."
346 (if (integerp (nth 2 event)) (nth 2 event) 1))
348 (defsubst posn-window (position)
349 "Return the window in POSITION.
350 POSITION should be a list of the form
351 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
352 as returned by the `event-start' and `event-end' functions."
355 (defsubst posn-point (position)
356 "Return the buffer location in POSITION.
357 POSITION should be a list of the form
358 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
359 as returned by the `event-start' and `event-end' functions."
360 (if (consp (nth 1 position))
361 (car (nth 1 position))
364 (defsubst posn-x-y (position)
365 "Return the x and y coordinates in POSITION.
366 POSITION should be a list of the form
367 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
368 as returned by the `event-start' and `event-end' functions."
371 (defun posn-col-row (position)
372 "Return the column and row in POSITION, measured in characters.
373 POSITION should be a list of the form
374 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
375 as returned by the `event-start' and `event-end' functions.
376 For a scroll-bar event, the result column is 0, and the row
377 corresponds to the vertical position of the click in the scroll bar."
378 (let ((pair (nth 2 position))
379 (window (posn-window position)))
380 (if (eq (if (consp (nth 1 position))
381 (car (nth 1 position))
383 'vertical-scroll-bar)
384 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
385 (if (eq (if (consp (nth 1 position))
386 (car (nth 1 position))
388 'horizontal-scroll-bar)
389 (cons (scroll-bar-scale pair (window-width window)) 0)
390 (let* ((frame (if (framep window) window (window-frame window)))
391 (x (/ (car pair) (frame-char-width frame)))
392 (y (/ (cdr pair) (frame-char-height frame))))
395 (defsubst posn-timestamp (position)
396 "Return the timestamp of POSITION.
397 POSITION should be a list of the form
398 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
399 as returned by the `event-start' and `event-end' functions."
403 ;;;; Obsolescent names for functions.
405 (defalias 'dot 'point)
406 (defalias 'dot-marker 'point-marker)
407 (defalias 'dot-min 'point-min)
408 (defalias 'dot-max 'point-max)
409 (defalias 'window-dot 'window-point)
410 (defalias 'set-window-dot 'set-window-point)
411 (defalias 'read-input 'read-string)
412 (defalias 'send-string 'process-send-string)
413 (defalias 'send-region 'process-send-region)
414 (defalias 'show-buffer 'set-window-buffer)
415 (defalias 'buffer-flush-undo 'buffer-disable-undo)
416 (defalias 'eval-current-buffer 'eval-buffer)
417 (defalias 'compiled-function-p 'byte-code-function-p)
419 ;; Some programs still use this as a function.
421 "Obsolete function returning the value of the `baud-rate' variable.
422 Please convert your programs to use the variable `baud-rate' directly."
426 ;;;; Alternate names for functions - these are not being phased out.
428 (defalias 'string= 'string-equal)
429 (defalias 'string< 'string-lessp)
430 (defalias 'move-marker 'set-marker)
432 (defalias 'not 'null)
433 (defalias 'rplaca 'setcar)
434 (defalias 'rplacd 'setcdr)
435 (defalias 'beep 'ding) ;preserve lingual purity
436 (defalias 'indent-to-column 'indent-to)
437 (defalias 'backward-delete-char 'delete-backward-char)
438 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
439 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
440 (defalias 'int-to-string 'number-to-string)
441 (defalias 'set-match-data 'store-match-data)
443 ;;; Should this be an obsolete name? If you decide it should, you get
444 ;;; to go through all the sources and change them.
445 (defalias 'string-to-int 'string-to-number)
447 ;;;; Hook manipulation functions.
449 (defun run-hooks (&rest hooklist)
450 "Takes hook names and runs each one in turn. Major mode functions use this.
451 Each argument should be a symbol, a hook variable.
452 These symbols are processed in the order specified.
453 If a hook symbol has a non-nil value, that value may be a function
454 or a list of functions to be called to run the hook.
455 If the value is a function, it is called with no arguments.
456 If it is a list, the elements are called, in order, with no arguments.
458 To make a hook variable buffer-local, use `make-local-hook', not
459 `make-local-variable'."
461 (let ((sym (car hooklist)))
464 (let ((value (symbol-value sym)))
465 (if (and (listp value) (not (eq (car value) 'lambda)))
467 (if (eq (car value) t)
468 ;; t indicates this hook has a local binding;
469 ;; it means to run the global binding too.
470 (let ((functions (default-value sym)))
472 (funcall (car functions))
473 (setq functions (cdr functions))))
474 (funcall (car value)))
475 (setq value (cdr value)))
477 (setq hooklist (cdr hooklist))))
479 (defun run-hook-with-args (hook &rest args)
480 "Run HOOK with the specified arguments ARGS.
481 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
482 value, that value may be a function or a list of functions to be
483 called to run the hook. If the value is a function, it is called with
484 the given arguments and its return value is returned. If it is a list
485 of functions, those functions are called, in order,
486 with the given arguments ARGS.
487 It is best not to depend on the value return by `run-hook-with-args',
490 To make a hook variable buffer-local, use `make-local-hook', not
491 `make-local-variable'."
494 (let ((value (symbol-value hook)))
495 (if (and (listp value) (not (eq (car value) 'lambda)))
497 (if (eq (car value) t)
498 ;; t indicates this hook has a local binding;
499 ;; it means to run the global binding too.
500 (let ((functions (default-value hook)))
502 (apply (car functions) args)
503 (setq functions (cdr functions))))
504 (apply (car value) args))
505 (setq value (cdr value)))
506 (apply value args)))))
508 (defun run-hook-with-args-until-success (hook &rest args)
509 "Run HOOK with the specified arguments ARGS.
510 HOOK should be a symbol, a hook variable. Its value should
511 be a list of functions. We call those functions, one by one,
512 passing arguments ARGS to each of them, until one of them
513 returns a non-nil value. Then we return that value.
514 If all the functions return nil, we return nil.
516 To make a hook variable buffer-local, use `make-local-hook', not
517 `make-local-variable'."
520 (let ((value (symbol-value hook))
522 (while (and value (not success))
523 (if (eq (car value) t)
524 ;; t indicates this hook has a local binding;
525 ;; it means to run the global binding too.
526 (let ((functions (default-value hook)))
527 (while (and functions (not success))
528 (setq success (apply (car functions) args))
529 (setq functions (cdr functions))))
530 (setq success (apply (car value) args)))
531 (setq value (cdr value)))
534 (defun run-hook-with-args-until-failure (hook &rest args)
535 "Run HOOK with the specified arguments ARGS.
536 HOOK should be a symbol, a hook variable. Its value should
537 be a list of functions. We call those functions, one by one,
538 passing arguments ARGS to each of them, until one of them
539 returns nil. Then we return nil.
540 If all the functions return non-nil, we return non-nil.
542 To make a hook variable buffer-local, use `make-local-hook', not
543 `make-local-variable'."
544 ;; We must return non-nil if there are no hook functions!
545 (or (not (boundp hook))
546 (not (symbol-value hook))
547 (let ((value (symbol-value hook))
549 (while (and value success)
550 (if (eq (car value) t)
551 ;; t indicates this hook has a local binding;
552 ;; it means to run the global binding too.
553 (let ((functions (default-value hook)))
554 (while (and functions success)
555 (setq success (apply (car functions) args))
556 (setq functions (cdr functions))))
557 (setq success (apply (car value) args)))
558 (setq value (cdr value)))
561 ;; Tell C code how to call this function.
562 (defconst run-hooks 'run-hooks
563 "Variable by which C primitives find the function `run-hooks'.
566 (defun make-local-hook (hook)
567 "Make the hook HOOK local to the current buffer.
568 When a hook is local, its local and global values
569 work in concert: running the hook actually runs all the hook
570 functions listed in *either* the local value *or* the global value
571 of the hook variable.
573 This function works by making `t' a member of the buffer-local value,
574 which acts as a flag to run the hook functions in the default value as
575 well. This works for all normal hooks, but does not work for most
576 non-normal hooks yet. We will be changing the callers of non-normal
577 hooks so that they can handle localness; this has to be done one by
580 This function does nothing if HOOK is already local in the current
583 Do not use `make-local-variable' to make a hook variable buffer-local."
584 (if (local-variable-p hook)
586 (or (boundp hook) (set hook nil))
587 (make-local-variable hook)
588 (set hook (list t))))
590 (defun add-hook (hook function &optional append local)
591 "Add to the value of HOOK the function FUNCTION.
592 FUNCTION is not added if already present.
593 FUNCTION is added (if necessary) at the beginning of the hook list
594 unless the optional argument APPEND is non-nil, in which case
595 FUNCTION is added at the end.
597 The optional fourth argument, LOCAL, if non-nil, says to modify
598 the hook's buffer-local value rather than its default value.
599 This makes no difference if the hook is not buffer-local.
600 To make a hook variable buffer-local, always use
601 `make-local-hook', not `make-local-variable'.
603 HOOK should be a symbol, and FUNCTION may be any valid function. If
604 HOOK is void, it is first set to nil. If HOOK's value is a single
605 function, it is changed to a list of functions."
606 (or (boundp hook) (set hook nil))
607 (or (default-boundp hook) (set-default hook nil))
608 ;; If the hook value is a single function, turn it into a list.
609 (let ((old (symbol-value hook)))
610 (if (or (not (listp old)) (eq (car old) 'lambda))
611 (set hook (list old))))
613 ;; Detect the case where make-local-variable was used on a hook
614 ;; and do what we used to do.
615 (and (local-variable-if-set-p hook)
616 (not (memq t (symbol-value hook)))))
617 ;; Alter the local value only.
618 (or (if (consp function)
619 (member function (symbol-value hook))
620 (memq function (symbol-value hook)))
623 (append (symbol-value hook) (list function))
624 (cons function (symbol-value hook)))))
625 ;; Alter the global value (which is also the only value,
626 ;; if the hook doesn't have a local value).
627 (or (if (consp function)
628 (member function (default-value hook))
629 (memq function (default-value hook)))
632 (append (default-value hook) (list function))
633 (cons function (default-value hook)))))))
635 (defun remove-hook (hook function &optional local)
636 "Remove from the value of HOOK the function FUNCTION.
637 HOOK should be a symbol, and FUNCTION may be any valid function. If
638 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
639 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
641 The optional third argument, LOCAL, if non-nil, says to modify
642 the hook's buffer-local value rather than its default value.
643 This makes no difference if the hook is not buffer-local.
644 To make a hook variable buffer-local, always use
645 `make-local-hook', not `make-local-variable'."
646 (if (or (not (boundp hook)) ;unbound symbol, or
647 (not (default-boundp 'hook))
648 (null (symbol-value hook)) ;value is nil, or
649 (null function)) ;function is nil, then
652 ;; Detect the case where make-local-variable was used on a hook
653 ;; and do what we used to do.
654 (and (local-variable-p hook)
655 (not (memq t (symbol-value hook)))))
656 (let ((hook-value (symbol-value hook)))
657 (if (consp hook-value)
658 (if (member function hook-value)
659 (setq hook-value (delete function (copy-sequence hook-value))))
660 (if (equal hook-value function)
661 (setq hook-value nil)))
662 (set hook hook-value))
663 (let ((hook-value (default-value hook)))
664 (if (consp hook-value)
665 (if (member function hook-value)
666 (setq hook-value (delete function (copy-sequence hook-value))))
667 (if (equal hook-value function)
668 (setq hook-value nil)))
669 (set-default hook hook-value)))))
671 (defun add-to-list (list-var element)
672 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
673 If you want to use `add-to-list' on a variable that is not defined
674 until a certain package is loaded, you should put the call to `add-to-list'
675 into a hook function that will be run only after loading the package.
676 `eval-after-load' provides one way to do this. In some cases
677 other hooks, such as major mode hooks, can do the job."
678 (or (member element (symbol-value list-var))
679 (set list-var (cons element (symbol-value list-var)))))
681 ;;;; Specifying things to do after certain files are loaded.
683 (defun eval-after-load (file form)
684 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
685 This makes or adds to an entry on `after-load-alist'.
686 If FILE is already loaded, evaluate FORM right now.
687 It does nothing if FORM is already on the list for FILE.
688 FILE should be the name of a library, with no directory name."
689 ;; Make sure there is an element for FILE.
690 (or (assoc file after-load-alist)
691 (setq after-load-alist (cons (list file) after-load-alist)))
692 ;; Add FORM to the element if it isn't there.
693 (let ((elt (assoc file after-load-alist)))
694 (or (member form (cdr elt))
696 (nconc elt (list form))
697 ;; If the file has been loaded already, run FORM right away.
698 (and (assoc file load-history)
702 (defun eval-next-after-load (file)
703 "Read the following input sexp, and run it whenever FILE is loaded.
704 This makes or adds to an entry on `after-load-alist'.
705 FILE should be the name of a library, with no directory name."
706 (eval-after-load file (read)))
709 ;;;; Input and display facilities.
711 (defun read-quoted-char (&optional prompt)
712 "Like `read-char', except that if the first character read is an octal
713 digit, we read up to two more octal digits and return the character
714 represented by the octal number consisting of those digits.
715 Optional argument PROMPT specifies a string to use to prompt the user."
716 (let ((message-log-max nil) (count 0) (code 0) char)
718 (let ((inhibit-quit (zerop count))
719 ;; Don't let C-h get the help message--only help function keys.
722 "Type the special character you want to use,
723 or three octal digits representing its character code."))
724 (and prompt (message "%s-" prompt))
725 (setq char (read-char))
726 (if inhibit-quit (setq quit-flag nil)))
728 ((and (<= ?0 char) (<= char ?7))
729 (setq code (+ (* code 8) (- char ?0))
731 (and prompt (message (setq prompt
732 (format "%s %c" prompt char)))))
734 (setq unread-command-events (list char) count 259))
735 (t (setq code char count 259))))
736 ;; Turn a meta-character into a character with the 0200 bit set.
737 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
740 (defun force-mode-line-update (&optional all)
741 "Force the mode-line of the current buffer to be redisplayed.
742 With optional non-nil ALL, force redisplay of all mode-lines."
743 (if all (save-excursion (set-buffer (other-buffer))))
744 (set-buffer-modified-p (buffer-modified-p)))
746 (defun momentary-string-display (string pos &optional exit-char message)
747 "Momentarily display STRING in the buffer at POS.
748 Display remains until next character is typed.
749 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
750 otherwise it is then available as input (as a command if nothing else).
751 Display MESSAGE (optional fourth arg) in the echo area.
752 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
753 (or exit-char (setq exit-char ?\ ))
754 (let ((buffer-read-only nil)
755 ;; Don't modify the undo list at all.
757 (modified (buffer-modified-p))
758 (name buffer-file-name)
764 ;; defeat file locking... don't try this at home, kids!
765 (setq buffer-file-name nil)
766 (insert-before-markers string)
767 (setq insert-end (point))
768 ;; If the message end is off screen, recenter now.
769 (if (> (window-end) insert-end)
770 (recenter (/ (window-height) 2)))
771 ;; If that pushed message start off the screen,
772 ;; scroll to start it at the top of the screen.
773 (move-to-window-line 0)
778 (message (or message "Type %s to continue editing.")
779 (single-key-description exit-char))
780 (let ((char (read-event)))
781 (or (eq char exit-char)
782 (setq unread-command-events (list char)))))
785 (delete-region pos insert-end)))
786 (setq buffer-file-name name)
787 (set-buffer-modified-p modified))))
792 ;; A number of major modes set this locally.
793 ;; Give it a global value to avoid compiler warnings.
794 (defvar font-lock-defaults nil)
796 ;; Avoid compiler warnings about this variable,
797 ;; which has a special meaning on certain system types.
798 (defvar buffer-file-type nil
799 "Non-nil if the visited file is a binary file.
800 This variable is meaningful on MS-DOG and Windows NT.
801 On those systems, it is automatically local in every buffer.
802 On other systems, this variable is normally always nil.")
804 (defun ignore (&rest ignore)
805 "Do nothing and return nil.
806 This function accepts any number of arguments, but ignores them."
810 (defun error (&rest args)
811 "Signal an error, making error message by passing all args to `format'."
813 (signal 'error (list (apply 'format args)))))
815 (defalias 'user-original-login-name 'user-login-name)
817 (defun start-process-shell-command (name buffer &rest args)
818 "Start a program in a subprocess. Return the process object for it.
819 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
820 NAME is name for process. It is modified if necessary to make it unique.
821 BUFFER is the buffer or (buffer-name) to associate with the process.
822 Process output goes at end of that buffer, unless you specify
823 an output stream or filter function to handle the output.
824 BUFFER may be also nil, meaning that this process is not associated
826 Third arg is command name, the name of a shell command.
827 Remaining arguments are the arguments for the command.
828 Wildcards and redirection are handled as usual in the shell."
830 ((eq system-type 'vax-vms)
831 (apply 'start-process name buffer args))
832 ;; We used to use `exec' to replace the shell with the command,
833 ;; but that failed to handle (...) and semicolon, etc.
835 (start-process name buffer shell-file-name shell-command-switch
836 (mapconcat 'identity args " ")))))
838 (defmacro save-match-data (&rest body)
839 "Execute the BODY forms, restoring the global value of the match data."
840 (let ((original (make-symbol "match-data")))
841 (list 'let (list (list original '(match-data)))
842 (list 'unwind-protect
844 (list 'store-match-data original)))))
846 (defun match-string (num &optional string)
847 "Return string of text matched by last search.
848 NUM specifies which parenthesized expression in the last regexp.
849 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
850 Zero means the entire text matched by the whole regexp or whole string.
851 STRING should be given if the last search was by `string-match' on STRING."
852 (if (match-beginning num)
854 (substring string (match-beginning num) (match-end num))
855 (buffer-substring (match-beginning num) (match-end num)))))
857 (defun buffer-substring-no-properties (beg end)
858 "Return the text from BEG to END, without text properties, as a string."
859 (let ((string (buffer-substring beg end)))
860 (set-text-properties 0 (length string) nil string)
863 (defun shell-quote-argument (argument)
864 "Quote an argument for passing as argument to an inferior shell."
865 ;; Quote everything except POSIX filename characters.
866 ;; This should be safe enough even for really weird shells.
867 (if (eq system-type 'windows-nt)
868 (concat "\"" argument "\"")
869 (let ((result "") (start 0) end)
870 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
871 (setq end (match-beginning 0)
872 result (concat result (substring argument start end)
873 "\\" (substring argument end (1+ end)))
875 (concat result (substring argument start)))))
877 (defun make-syntax-table (&optional oldtable)
878 "Return a new syntax table.
879 It inherits all letters and control characters from the standard
880 syntax table; other characters are copied from the standard syntax table."
882 (copy-syntax-table oldtable)
883 (let ((table (copy-syntax-table))
903 (defun global-set-key (key command)
904 "Give KEY a global binding as COMMAND.
905 COMMAND is a symbol naming an interactively-callable function.
906 KEY is a key sequence (a string or vector of characters or event types).
907 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
908 can be included if you use a vector.
909 Note that if KEY has a local binding in the current buffer
910 that local binding will continue to shadow any global binding."
911 (interactive "KSet key globally: \nCSet key %s to command: ")
912 (or (vectorp key) (stringp key)
913 (signal 'wrong-type-argument (list 'arrayp key)))
914 (define-key (current-global-map) key command)
917 (defun local-set-key (key command)
918 "Give KEY a local binding as COMMAND.
919 COMMAND is a symbol naming an interactively-callable function.
920 KEY is a key sequence (a string or vector of characters or event types).
921 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
922 can be included if you use a vector.
923 The binding goes in the current buffer's local map,
924 which in most cases is shared with all other buffers in the same major mode."
925 (interactive "KSet key locally: \nCSet key %s locally to command: ")
926 (let ((map (current-local-map)))
928 (use-local-map (setq map (make-sparse-keymap))))
929 (or (vectorp key) (stringp key)
930 (signal 'wrong-type-argument (list 'arrayp key)))
931 (define-key map key command))
934 (defun global-unset-key (key)
935 "Remove global binding of KEY.
936 KEY is a string representing a sequence of keystrokes."
937 (interactive "kUnset key globally: ")
938 (global-set-key key nil))
940 (defun local-unset-key (key)
941 "Remove local binding of KEY.
942 KEY is a string representing a sequence of keystrokes."
943 (interactive "kUnset key locally: ")
944 (if (current-local-map)
945 (local-set-key key nil))
948 ;; We put this here instead of in frame.el so that it's defined even on
949 ;; systems where frame.el isn't loaded.
950 (defun frame-configuration-p (object)
951 "Return non-nil if OBJECT seems to be a frame configuration.
952 Any list whose car is `frame-configuration' is assumed to be a frame
955 (eq (car object) 'frame-configuration)))
959 ; "Returns the Nth element of LIST.
960 ;N counts from zero. If LIST is not that long, nil is returned."
961 ; (car (nthcdr n list)))
963 ;(defun copy-alist (alist)
964 ; "Return a copy of ALIST.
965 ;This is a new alist which represents the same mapping
966 ;from objects to objects, but does not share the alist structure with ALIST.
967 ;The objects mapped (cars and cdrs of elements of the alist)
968 ;are shared, however."
969 ; (setq alist (copy-sequence alist))
970 ; (let ((tail alist))
972 ; (if (consp (car tail))
973 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
974 ; (setq tail (cdr tail))))
977 ;;; subr.el ends here