Add defgroup; use defcustom for user vars.
[emacs.git] / lisp / subr.el
blobedd425d33e6134cbb7e925adb0c42b0ac4d9f4bc
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)
10 ;; any later version.
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.
22 ;;; Code:
25 ;;;; Lisp language features.
27 (defmacro lambda (&rest cdr)
28 "Return a lambda expression.
29 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
30 self-quoting; the result of evaluating the lambda expression is the
31 expression itself. The lambda expression may then be treated as a
32 function, i.e., stored as the function value of a symbol, passed to
33 funcall or mapcar, etc.
35 ARGS should take the same form as an argument list for a `defun'.
36 DOCSTRING is an optional documentation string.
37 If present, it should describe how to call the function.
38 But documentation strings are usually not useful in nameless functions.
39 INTERACTIVE should be a call to the function `interactive', which see.
40 It may also be omitted.
41 BODY should be a list of lisp expressions."
42 ;; Note that this definition should not use backquotes; subr.el should not
43 ;; depend on backquote.el.
44 (list 'function (cons 'lambda cdr)))
46 (defmacro when (cond &rest body)
47 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
48 (list 'if cond (cons 'progn body)))
49 (put 'when 'lisp-indent-function 1)
50 (put 'when 'edebug-form-spec '(&rest form))
52 (defmacro unless (cond &rest body)
53 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
54 (cons 'if (cons cond (cons nil body))))
55 (put 'unless 'lisp-indent-function 1)
56 (put 'unless 'edebug-form-spec '(&rest form))
58 ;;;; Keymap support.
60 (defun undefined ()
61 (interactive)
62 (ding))
64 ;Prevent the \{...} documentation construct
65 ;from mentioning keys that run this command.
66 (put 'undefined 'suppress-keymap t)
68 (defun suppress-keymap (map &optional nodigits)
69 "Make MAP override all normally self-inserting keys to be undefined.
70 Normally, as an exception, digits and minus-sign are set to make prefix args,
71 but optional second arg NODIGITS non-nil treats them like other chars."
72 (substitute-key-definition 'self-insert-command 'undefined map global-map)
73 (or nodigits
74 (let (loop)
75 (define-key map "-" 'negative-argument)
76 ;; Make plain numbers do numeric args.
77 (setq loop ?0)
78 (while (<= loop ?9)
79 (define-key map (char-to-string loop) 'digit-argument)
80 (setq loop (1+ loop))))))
82 ;Moved to keymap.c
83 ;(defun copy-keymap (keymap)
84 ; "Return a copy of KEYMAP"
85 ; (while (not (keymapp keymap))
86 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
87 ; (if (vectorp keymap)
88 ; (copy-sequence keymap)
89 ; (copy-alist keymap)))
91 (defvar key-substitution-in-progress nil
92 "Used internally by substitute-key-definition.")
94 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
95 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
96 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
97 If optional fourth argument OLDMAP is specified, we redefine
98 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
99 (or prefix (setq prefix ""))
100 (let* ((scan (or oldmap keymap))
101 (vec1 (vector nil))
102 (prefix1 (vconcat prefix vec1))
103 (key-substitution-in-progress
104 (cons scan key-substitution-in-progress)))
105 ;; Scan OLDMAP, finding each char or event-symbol that
106 ;; has any definition, and act on it with hack-key.
107 (while (consp scan)
108 (if (consp (car scan))
109 (let ((char (car (car scan)))
110 (defn (cdr (car scan))))
111 ;; The inside of this let duplicates exactly
112 ;; the inside of the following let that handles array elements.
113 (aset vec1 0 char)
114 (aset prefix1 (length prefix) char)
115 (let (inner-def skipped)
116 ;; Skip past menu-prompt.
117 (while (stringp (car-safe defn))
118 (setq skipped (cons (car defn) skipped))
119 (setq defn (cdr defn)))
120 ;; Skip past cached key-equivalence data for menu items.
121 (and (consp defn) (consp (car defn))
122 (setq defn (cdr defn)))
123 (setq inner-def defn)
124 ;; Look past a symbol that names a keymap.
125 (while (and (symbolp inner-def)
126 (fboundp inner-def))
127 (setq inner-def (symbol-function inner-def)))
128 (if (or (eq defn olddef)
129 ;; Compare with equal if definition is a key sequence.
130 ;; That is useful for operating on function-key-map.
131 (and (or (stringp defn) (vectorp defn))
132 (equal defn olddef)))
133 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
134 (if (and (keymapp defn)
135 ;; Avoid recursively scanning
136 ;; where KEYMAP does not have a submap.
137 (let ((elt (lookup-key keymap prefix1)))
138 (or (null elt)
139 (keymapp elt)))
140 ;; Avoid recursively rescanning keymap being scanned.
141 (not (memq inner-def
142 key-substitution-in-progress)))
143 ;; If this one isn't being scanned already,
144 ;; scan it now.
145 (substitute-key-definition olddef newdef keymap
146 inner-def
147 prefix1)))))
148 (if (arrayp (car scan))
149 (let* ((array (car scan))
150 (len (length array))
151 (i 0))
152 (while (< i len)
153 (let ((char i) (defn (aref array i)))
154 ;; The inside of this let duplicates exactly
155 ;; the inside of the previous let.
156 (aset vec1 0 char)
157 (aset prefix1 (length prefix) char)
158 (let (inner-def skipped)
159 ;; Skip past menu-prompt.
160 (while (stringp (car-safe defn))
161 (setq skipped (cons (car defn) skipped))
162 (setq defn (cdr defn)))
163 (and (consp defn) (consp (car defn))
164 (setq defn (cdr defn)))
165 (setq inner-def defn)
166 (while (and (symbolp inner-def)
167 (fboundp inner-def))
168 (setq inner-def (symbol-function inner-def)))
169 (if (or (eq defn olddef)
170 (and (or (stringp defn) (vectorp defn))
171 (equal defn olddef)))
172 (define-key keymap prefix1
173 (nconc (nreverse skipped) newdef))
174 (if (and (keymapp defn)
175 (let ((elt (lookup-key keymap prefix1)))
176 (or (null elt)
177 (keymapp elt)))
178 (not (memq inner-def
179 key-substitution-in-progress)))
180 (substitute-key-definition olddef newdef keymap
181 inner-def
182 prefix1)))))
183 (setq i (1+ i))))))
184 (setq scan (cdr scan)))))
186 (defun define-key-after (keymap key definition after)
187 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
188 This is like `define-key' except that the binding for KEY is placed
189 just after the binding for the event AFTER, instead of at the beginning
190 of the map. Note that AFTER must be an event type (like KEY), NOT a command
191 \(like DEFINITION).
193 If AFTER is t, the new binding goes at the end of the keymap.
195 KEY must contain just one event type--that is to say, it must be
196 a string or vector of length 1.
198 The order of bindings in a keymap matters when it is used as a menu."
200 (or (keymapp keymap)
201 (signal 'wrong-type-argument (list 'keymapp keymap)))
202 (if (> (length key) 1)
203 (error "multi-event key specified in `define-key-after'"))
204 (let ((tail keymap) done inserted
205 (first (aref key 0)))
206 (while (and (not done) tail)
207 ;; Delete any earlier bindings for the same key.
208 (if (eq (car-safe (car (cdr tail))) first)
209 (setcdr tail (cdr (cdr tail))))
210 ;; When we reach AFTER's binding, insert the new binding after.
211 ;; If we reach an inherited keymap, insert just before that.
212 ;; If we reach the end of this keymap, insert at the end.
213 (if (or (and (eq (car-safe (car tail)) after)
214 (not (eq after t)))
215 (eq (car (cdr tail)) 'keymap)
216 (null (cdr tail)))
217 (progn
218 ;; Stop the scan only if we find a parent keymap.
219 ;; Keep going past the inserted element
220 ;; so we can delete any duplications that come later.
221 (if (eq (car (cdr tail)) 'keymap)
222 (setq done t))
223 ;; Don't insert more than once.
224 (or inserted
225 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
226 (setq inserted t)))
227 (setq tail (cdr tail)))))
229 (defmacro kbd (keys)
230 "Convert KEYS to the internal Emacs key representation.
231 KEYS should be a string constant in the format used for
232 saving keyboard macros (see `insert-kbd-macro')."
233 (read-kbd-macro keys))
235 (put 'keyboard-translate-table 'char-table-extra-slots 0)
237 (defun keyboard-translate (from to)
238 "Translate character FROM to TO at a low level.
239 This function creates a `keyboard-translate-table' if necessary
240 and then modifies one entry in it."
241 (or (char-table-p keyboard-translate-table)
242 (setq keyboard-translate-table
243 (make-char-table 'keyboard-translate-table nil)))
244 (aset keyboard-translate-table from to))
247 ;;;; The global keymap tree.
249 ;;; global-map, esc-map, and ctl-x-map have their values set up in
250 ;;; keymap.c; we just give them docstrings here.
252 (defvar global-map nil
253 "Default global keymap mapping Emacs keyboard input into commands.
254 The value is a keymap which is usually (but not necessarily) Emacs's
255 global map.")
257 (defvar esc-map nil
258 "Default keymap for ESC (meta) commands.
259 The normal global definition of the character ESC indirects to this keymap.")
261 (defvar ctl-x-map nil
262 "Default keymap for C-x commands.
263 The normal global definition of the character C-x indirects to this keymap.")
265 (defvar ctl-x-4-map (make-sparse-keymap)
266 "Keymap for subcommands of C-x 4")
267 (defalias 'ctl-x-4-prefix ctl-x-4-map)
268 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
270 (defvar ctl-x-5-map (make-sparse-keymap)
271 "Keymap for frame commands.")
272 (defalias 'ctl-x-5-prefix ctl-x-5-map)
273 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
276 ;;;; Event manipulation functions.
278 ;; The call to `read' is to ensure that the value is computed at load time
279 ;; and not compiled into the .elc file. The value is negative on most
280 ;; machines, but not on all!
281 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
283 (defun listify-key-sequence (key)
284 "Convert a key sequence to a list of events."
285 (if (vectorp key)
286 (append key nil)
287 (mapcar (function (lambda (c)
288 (if (> c 127)
289 (logxor c listify-key-sequence-1)
290 c)))
291 (append key nil))))
293 (defsubst eventp (obj)
294 "True if the argument is an event object."
295 (or (integerp obj)
296 (and (symbolp obj)
297 (get obj 'event-symbol-elements))
298 (and (consp obj)
299 (symbolp (car obj))
300 (get (car obj) 'event-symbol-elements))))
302 (defun event-modifiers (event)
303 "Returns a list of symbols representing the modifier keys in event EVENT.
304 The elements of the list may include `meta', `control',
305 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
306 and `down'."
307 (let ((type event))
308 (if (listp type)
309 (setq type (car type)))
310 (if (symbolp type)
311 (cdr (get type 'event-symbol-elements))
312 (let ((list nil))
313 (or (zerop (logand type ?\M-\^@))
314 (setq list (cons 'meta list)))
315 (or (and (zerop (logand type ?\C-\^@))
316 (>= (logand type 127) 32))
317 (setq list (cons 'control list)))
318 (or (and (zerop (logand type ?\S-\^@))
319 (= (logand type 255) (downcase (logand type 255))))
320 (setq list (cons 'shift list)))
321 (or (zerop (logand type ?\H-\^@))
322 (setq list (cons 'hyper list)))
323 (or (zerop (logand type ?\s-\^@))
324 (setq list (cons 'super list)))
325 (or (zerop (logand type ?\A-\^@))
326 (setq list (cons 'alt list)))
327 list))))
329 (defun event-basic-type (event)
330 "Returns the basic type of the given event (all modifiers removed).
331 The value is an ASCII printing character (not upper case) or a symbol."
332 (if (consp event)
333 (setq event (car event)))
334 (if (symbolp event)
335 (car (get event 'event-symbol-elements))
336 (let ((base (logand event (1- (lsh 1 18)))))
337 (downcase (if (< base 32) (logior base 64) base)))))
339 (defsubst mouse-movement-p (object)
340 "Return non-nil if OBJECT is a mouse movement event."
341 (and (consp object)
342 (eq (car object) 'mouse-movement)))
344 (defsubst event-start (event)
345 "Return the starting position of EVENT.
346 If EVENT is a mouse press or a mouse click, this returns the location
347 of the event.
348 If EVENT is a drag, this returns the drag's starting position.
349 The return value is of the form
350 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
351 The `posn-' functions access elements of such lists."
352 (nth 1 event))
354 (defsubst event-end (event)
355 "Return the ending location of EVENT. EVENT should be a click or drag event.
356 If EVENT is a click event, this function is the same as `event-start'.
357 The return value is of the form
358 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
359 The `posn-' functions access elements of such lists."
360 (nth (if (consp (nth 2 event)) 2 1) event))
362 (defsubst event-click-count (event)
363 "Return the multi-click count of EVENT, a click or drag event.
364 The return value is a positive integer."
365 (if (integerp (nth 2 event)) (nth 2 event) 1))
367 (defsubst posn-window (position)
368 "Return the window in POSITION.
369 POSITION should be a list of the form
370 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
371 as returned by the `event-start' and `event-end' functions."
372 (nth 0 position))
374 (defsubst posn-point (position)
375 "Return the buffer location in POSITION.
376 POSITION should be a list of the form
377 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
378 as returned by the `event-start' and `event-end' functions."
379 (if (consp (nth 1 position))
380 (car (nth 1 position))
381 (nth 1 position)))
383 (defsubst posn-x-y (position)
384 "Return the x and y coordinates in POSITION.
385 POSITION should be a list of the form
386 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
387 as returned by the `event-start' and `event-end' functions."
388 (nth 2 position))
390 (defun posn-col-row (position)
391 "Return the column and row in POSITION, measured in characters.
392 POSITION should be a list of the form
393 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
394 as returned by the `event-start' and `event-end' functions.
395 For a scroll-bar event, the result column is 0, and the row
396 corresponds to the vertical position of the click in the scroll bar."
397 (let ((pair (nth 2 position))
398 (window (posn-window position)))
399 (if (eq (if (consp (nth 1 position))
400 (car (nth 1 position))
401 (nth 1 position))
402 'vertical-scroll-bar)
403 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
404 (if (eq (if (consp (nth 1 position))
405 (car (nth 1 position))
406 (nth 1 position))
407 'horizontal-scroll-bar)
408 (cons (scroll-bar-scale pair (window-width window)) 0)
409 (let* ((frame (if (framep window) window (window-frame window)))
410 (x (/ (car pair) (frame-char-width frame)))
411 (y (/ (cdr pair) (frame-char-height frame))))
412 (cons x y))))))
414 (defsubst posn-timestamp (position)
415 "Return the timestamp of POSITION.
416 POSITION should be a list of the form
417 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
418 as returned by the `event-start' and `event-end' functions."
419 (nth 3 position))
422 ;;;; Obsolescent names for functions.
424 (defalias 'dot 'point)
425 (defalias 'dot-marker 'point-marker)
426 (defalias 'dot-min 'point-min)
427 (defalias 'dot-max 'point-max)
428 (defalias 'window-dot 'window-point)
429 (defalias 'set-window-dot 'set-window-point)
430 (defalias 'read-input 'read-string)
431 (defalias 'send-string 'process-send-string)
432 (defalias 'send-region 'process-send-region)
433 (defalias 'show-buffer 'set-window-buffer)
434 (defalias 'buffer-flush-undo 'buffer-disable-undo)
435 (defalias 'eval-current-buffer 'eval-buffer)
436 (defalias 'compiled-function-p 'byte-code-function-p)
437 (defalias 'define-function 'defalias)
439 ;; Some programs still use this as a function.
440 (defun baud-rate ()
441 "Obsolete function returning the value of the `baud-rate' variable.
442 Please convert your programs to use the variable `baud-rate' directly."
443 baud-rate)
445 (defalias 'focus-frame 'ignore)
446 (defalias 'unfocus-frame 'ignore)
448 ;;;; Alternate names for functions - these are not being phased out.
450 (defalias 'string= 'string-equal)
451 (defalias 'string< 'string-lessp)
452 (defalias 'move-marker 'set-marker)
453 (defalias 'not 'null)
454 (defalias 'rplaca 'setcar)
455 (defalias 'rplacd 'setcdr)
456 (defalias 'beep 'ding) ;preserve lingual purity
457 (defalias 'indent-to-column 'indent-to)
458 (defalias 'backward-delete-char 'delete-backward-char)
459 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
460 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
461 (defalias 'int-to-string 'number-to-string)
462 (defalias 'set-match-data 'store-match-data)
464 ;;; Should this be an obsolete name? If you decide it should, you get
465 ;;; to go through all the sources and change them.
466 (defalias 'string-to-int 'string-to-number)
468 ;;;; Hook manipulation functions.
470 (defun make-local-hook (hook)
471 "Make the hook HOOK local to the current buffer.
472 When a hook is local, its local and global values
473 work in concert: running the hook actually runs all the hook
474 functions listed in *either* the local value *or* the global value
475 of the hook variable.
477 This function works by making `t' a member of the buffer-local value,
478 which acts as a flag to run the hook functions in the default value as
479 well. This works for all normal hooks, but does not work for most
480 non-normal hooks yet. We will be changing the callers of non-normal
481 hooks so that they can handle localness; this has to be done one by
482 one.
484 This function does nothing if HOOK is already local in the current
485 buffer.
487 Do not use `make-local-variable' to make a hook variable buffer-local."
488 (if (local-variable-p hook)
490 (or (boundp hook) (set hook nil))
491 (make-local-variable hook)
492 (set hook (list t))))
494 (defun add-hook (hook function &optional append local)
495 "Add to the value of HOOK the function FUNCTION.
496 FUNCTION is not added if already present.
497 FUNCTION is added (if necessary) at the beginning of the hook list
498 unless the optional argument APPEND is non-nil, in which case
499 FUNCTION is added at the end.
501 The optional fourth argument, LOCAL, if non-nil, says to modify
502 the hook's buffer-local value rather than its default value.
503 This makes no difference if the hook is not buffer-local.
504 To make a hook variable buffer-local, always use
505 `make-local-hook', not `make-local-variable'.
507 HOOK should be a symbol, and FUNCTION may be any valid function. If
508 HOOK is void, it is first set to nil. If HOOK's value is a single
509 function, it is changed to a list of functions."
510 (or (boundp hook) (set hook nil))
511 (or (default-boundp hook) (set-default hook nil))
512 ;; If the hook value is a single function, turn it into a list.
513 (let ((old (symbol-value hook)))
514 (if (or (not (listp old)) (eq (car old) 'lambda))
515 (set hook (list old))))
516 (if (or local
517 ;; Detect the case where make-local-variable was used on a hook
518 ;; and do what we used to do.
519 (and (local-variable-if-set-p hook)
520 (not (memq t (symbol-value hook)))))
521 ;; Alter the local value only.
522 (or (if (consp function)
523 (member function (symbol-value hook))
524 (memq function (symbol-value hook)))
525 (set hook
526 (if append
527 (append (symbol-value hook) (list function))
528 (cons function (symbol-value hook)))))
529 ;; Alter the global value (which is also the only value,
530 ;; if the hook doesn't have a local value).
531 (or (if (consp function)
532 (member function (default-value hook))
533 (memq function (default-value hook)))
534 (set-default hook
535 (if append
536 (append (default-value hook) (list function))
537 (cons function (default-value hook)))))))
539 (defun remove-hook (hook function &optional local)
540 "Remove from the value of HOOK the function FUNCTION.
541 HOOK should be a symbol, and FUNCTION may be any valid function. If
542 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
543 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
545 The optional third argument, LOCAL, if non-nil, says to modify
546 the hook's buffer-local value rather than its default value.
547 This makes no difference if the hook is not buffer-local.
548 To make a hook variable buffer-local, always use
549 `make-local-hook', not `make-local-variable'."
550 (if (or (not (boundp hook)) ;unbound symbol, or
551 (not (default-boundp 'hook))
552 (null (symbol-value hook)) ;value is nil, or
553 (null function)) ;function is nil, then
554 nil ;Do nothing.
555 (if (or local
556 ;; Detect the case where make-local-variable was used on a hook
557 ;; and do what we used to do.
558 (and (local-variable-p hook)
559 (not (memq t (symbol-value hook)))))
560 (let ((hook-value (symbol-value hook)))
561 (if (consp hook-value)
562 (if (member function hook-value)
563 (setq hook-value (delete function (copy-sequence hook-value))))
564 (if (equal hook-value function)
565 (setq hook-value nil)))
566 (set hook hook-value))
567 (let ((hook-value (default-value hook)))
568 (if (consp hook-value)
569 (if (member function hook-value)
570 (setq hook-value (delete function (copy-sequence hook-value))))
571 (if (equal hook-value function)
572 (setq hook-value nil)))
573 (set-default hook hook-value)))))
575 (defun add-to-list (list-var element)
576 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
577 The test for presence of ELEMENT is done with `equal'.
578 If you want to use `add-to-list' on a variable that is not defined
579 until a certain package is loaded, you should put the call to `add-to-list'
580 into a hook function that will be run only after loading the package.
581 `eval-after-load' provides one way to do this. In some cases
582 other hooks, such as major mode hooks, can do the job."
583 (or (member element (symbol-value list-var))
584 (set list-var (cons element (symbol-value list-var)))))
586 ;;;; Specifying things to do after certain files are loaded.
588 (defun eval-after-load (file form)
589 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
590 This makes or adds to an entry on `after-load-alist'.
591 If FILE is already loaded, evaluate FORM right now.
592 It does nothing if FORM is already on the list for FILE.
593 FILE should be the name of a library, with no directory name."
594 ;; Make sure there is an element for FILE.
595 (or (assoc file after-load-alist)
596 (setq after-load-alist (cons (list file) after-load-alist)))
597 ;; Add FORM to the element if it isn't there.
598 (let ((elt (assoc file after-load-alist)))
599 (or (member form (cdr elt))
600 (progn
601 (nconc elt (list form))
602 ;; If the file has been loaded already, run FORM right away.
603 (and (assoc file load-history)
604 (eval form)))))
605 form)
607 (defun eval-next-after-load (file)
608 "Read the following input sexp, and run it whenever FILE is loaded.
609 This makes or adds to an entry on `after-load-alist'.
610 FILE should be the name of a library, with no directory name."
611 (eval-after-load file (read)))
614 ;;;; Input and display facilities.
616 (defun read-quoted-char (&optional prompt)
617 "Like `read-char', except that if the first character read is an octal
618 digit, we read up to two more octal digits and return the character
619 represented by the octal number consisting of those digits.
620 Optional argument PROMPT specifies a string to use to prompt the user."
621 (let ((message-log-max nil) (count 0) (code 0) char)
622 (while (< count 3)
623 (let ((inhibit-quit (zerop count))
624 ;; Don't let C-h get the help message--only help function keys.
625 (help-char nil)
626 (help-form
627 "Type the special character you want to use,
628 or three octal digits representing its character code."))
629 (and prompt (message "%s-" prompt))
630 (setq char (read-char))
631 (if inhibit-quit (setq quit-flag nil)))
632 (cond ((null char))
633 ((and (<= ?0 char) (<= char ?7))
634 (setq code (+ (* code 8) (- char ?0))
635 count (1+ count))
636 (and prompt (setq prompt (message "%s %c" prompt char))))
637 ((> count 0)
638 (setq unread-command-events (list char) count 259))
639 (t (setq code char count 259))))
640 ;; Turn a meta-character into a character with the 0200 bit set.
641 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
642 (logand 255 code))))
644 (defun force-mode-line-update (&optional all)
645 "Force the mode-line of the current buffer to be redisplayed.
646 With optional non-nil ALL, force redisplay of all mode-lines."
647 (if all (save-excursion (set-buffer (other-buffer))))
648 (set-buffer-modified-p (buffer-modified-p)))
650 (defun momentary-string-display (string pos &optional exit-char message)
651 "Momentarily display STRING in the buffer at POS.
652 Display remains until next character is typed.
653 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
654 otherwise it is then available as input (as a command if nothing else).
655 Display MESSAGE (optional fourth arg) in the echo area.
656 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
657 (or exit-char (setq exit-char ?\ ))
658 (let ((buffer-read-only nil)
659 ;; Don't modify the undo list at all.
660 (buffer-undo-list t)
661 (modified (buffer-modified-p))
662 (name buffer-file-name)
663 insert-end)
664 (unwind-protect
665 (progn
666 (save-excursion
667 (goto-char pos)
668 ;; defeat file locking... don't try this at home, kids!
669 (setq buffer-file-name nil)
670 (insert-before-markers string)
671 (setq insert-end (point))
672 ;; If the message end is off screen, recenter now.
673 (if (> (window-end) insert-end)
674 (recenter (/ (window-height) 2)))
675 ;; If that pushed message start off the screen,
676 ;; scroll to start it at the top of the screen.
677 (move-to-window-line 0)
678 (if (> (point) pos)
679 (progn
680 (goto-char pos)
681 (recenter 0))))
682 (message (or message "Type %s to continue editing.")
683 (single-key-description exit-char))
684 (let ((char (read-event)))
685 (or (eq char exit-char)
686 (setq unread-command-events (list char)))))
687 (if insert-end
688 (save-excursion
689 (delete-region pos insert-end)))
690 (setq buffer-file-name name)
691 (set-buffer-modified-p modified))))
694 ;;;; Miscellanea.
696 ;; A number of major modes set this locally.
697 ;; Give it a global value to avoid compiler warnings.
698 (defvar font-lock-defaults nil)
700 ;; Avoid compiler warnings about this variable,
701 ;; which has a special meaning on certain system types.
702 (defvar buffer-file-type nil
703 "Non-nil if the visited file is a binary file.
704 This variable is meaningful on MS-DOG and Windows NT.
705 On those systems, it is automatically local in every buffer.
706 On other systems, this variable is normally always nil.")
708 ;; This should probably be written in C (i.e., without using `walk-windows').
709 (defun get-buffer-window-list (buffer &optional minibuf frame)
710 "Return windows currently displaying BUFFER, or nil if none.
711 See `walk-windows' for the meaning of MINIBUF and FRAME."
712 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
713 (walk-windows (function (lambda (window)
714 (if (eq (window-buffer window) buffer)
715 (setq windows (cons window windows)))))
716 minibuf frame)
717 windows))
719 (defun ignore (&rest ignore)
720 "Do nothing and return nil.
721 This function accepts any number of arguments, but ignores them."
722 (interactive)
723 nil)
725 (defun error (&rest args)
726 "Signal an error, making error message by passing all args to `format'.
727 In Emacs, the convention is that error messages start with a capital
728 letter but *do not* end with a period. Please follow this convention
729 for the sake of consistency."
730 (while t
731 (signal 'error (list (apply 'format args)))))
733 (defalias 'user-original-login-name 'user-login-name)
735 (defun start-process-shell-command (name buffer &rest args)
736 "Start a program in a subprocess. Return the process object for it.
737 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
738 NAME is name for process. It is modified if necessary to make it unique.
739 BUFFER is the buffer or (buffer-name) to associate with the process.
740 Process output goes at end of that buffer, unless you specify
741 an output stream or filter function to handle the output.
742 BUFFER may be also nil, meaning that this process is not associated
743 with any buffer
744 Third arg is command name, the name of a shell command.
745 Remaining arguments are the arguments for the command.
746 Wildcards and redirection are handled as usual in the shell."
747 (cond
748 ((eq system-type 'vax-vms)
749 (apply 'start-process name buffer args))
750 ;; We used to use `exec' to replace the shell with the command,
751 ;; but that failed to handle (...) and semicolon, etc.
753 (start-process name buffer shell-file-name shell-command-switch
754 (mapconcat 'identity args " ")))))
756 (defmacro with-current-buffer (buffer &rest body)
757 "Execute the forms in BODY with BUFFER as the current buffer.
758 The value returned is the value of the last form in BODY.
759 See also `with-temp-buffer'."
760 `(save-current-buffer
761 (set-buffer ,buffer)
762 ,@body))
764 (defmacro with-temp-file (file &rest forms)
765 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
766 The value of the last form in FORMS is returned, like `progn'.
767 See also `with-temp-buffer'."
768 (let ((temp-file (make-symbol "temp-file"))
769 (temp-buffer (make-symbol "temp-buffer")))
770 `(let ((,temp-file ,file)
771 (,temp-buffer
772 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
773 (unwind-protect
774 (prog1
775 (with-current-buffer ,temp-buffer
776 ,@forms)
777 (with-current-buffer ,temp-buffer
778 (widen)
779 (write-region (point-min) (point-max) ,temp-file nil 0)))
780 (and (buffer-name ,temp-buffer)
781 (kill-buffer ,temp-buffer))))))
783 (defmacro with-temp-buffer (&rest forms)
784 "Create a temporary buffer, and evaluate FORMS there like `progn'.
785 See also `with-temp-file' and `with-output-to-string'."
786 (let ((temp-buffer (make-symbol "temp-buffer")))
787 `(let ((,temp-buffer
788 (get-buffer-create (generate-new-buffer-name " *temp*"))))
789 (unwind-protect
790 (with-current-buffer ,temp-buffer
791 ,@forms)
792 (and (buffer-name ,temp-buffer)
793 (kill-buffer ,temp-buffer))))))
795 (defmacro with-output-to-string (&rest body)
796 "Execute BODY, return the text it sent to `standard-output', as a string."
797 `(let ((standard-output
798 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
799 (let ((standard-output standard-output))
800 ,@body)
801 (with-current-buffer standard-output
802 (prog1
803 (buffer-string)
804 (kill-buffer nil)))))
806 (defmacro combine-after-change-calls (&rest body)
807 "Execute BODY, but don't call the after-change functions till the end.
808 If BODY makes changes in the buffer, they are recorded
809 and the functions on `after-change-functions' are called several times
810 when BODY is finished.
811 The return value is the value of the last form in BODY.
813 If `before-change-functions' is non-nil, then calls to the after-change
814 functions can't be deferred, so in that case this macro has no effect.
816 Do not alter `after-change-functions' or `before-change-functions'
817 in BODY."
818 `(unwind-protect
819 (let ((combine-after-change-calls t))
820 . ,body)
821 (combine-after-change-execute)))
824 (defvar save-match-data-internal)
826 ;; We use save-match-data-internal as the local variable because
827 ;; that works ok in practice (people should not use that variable elsewhere).
828 ;; We used to use an uninterned symbol; the compiler handles that properly
829 ;; now, but it generates slower code.
830 (defmacro save-match-data (&rest body)
831 "Execute the BODY forms, restoring the global value of the match data."
832 `(let ((save-match-data-internal (match-data)))
833 (unwind-protect
834 (progn ,@body)
835 (store-match-data save-match-data-internal))))
837 (defun match-string (num &optional string)
838 "Return string of text matched by last search.
839 NUM specifies which parenthesized expression in the last regexp.
840 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
841 Zero means the entire text matched by the whole regexp or whole string.
842 STRING should be given if the last search was by `string-match' on STRING."
843 (if (match-beginning num)
844 (if string
845 (substring string (match-beginning num) (match-end num))
846 (buffer-substring (match-beginning num) (match-end num)))))
848 (defun split-string (string &optional separators)
849 "Splits STRING into substrings where there are matches for SEPARATORS.
850 Each match for SEPARATORS is a splitting point.
851 The substrings between the splitting points are made into a list
852 which is returned.
853 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
854 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
855 (start 0)
856 (list nil))
857 (while (string-match rexp string start)
858 (or (eq (match-beginning 0) 0)
859 (setq list
860 (cons (substring string start (match-beginning 0))
861 list)))
862 (setq start (match-end 0)))
863 (or (eq start (length string))
864 (setq list
865 (cons (substring string start)
866 list)))
867 (nreverse list)))
869 (defun shell-quote-argument (argument)
870 "Quote an argument for passing as argument to an inferior shell."
871 (if (eq system-type 'ms-dos)
872 ;; MS-DOS shells don't have quoting, so don't do any.
873 argument
874 (if (eq system-type 'windows-nt)
875 (concat "\"" argument "\"")
876 ;; Quote everything except POSIX filename characters.
877 ;; This should be safe enough even for really weird shells.
878 (let ((result "") (start 0) end)
879 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
880 (setq end (match-beginning 0)
881 result (concat result (substring argument start end)
882 "\\" (substring argument end (1+ end)))
883 start (1+ end)))
884 (concat result (substring argument start))))))
886 (defun make-syntax-table (&optional oldtable)
887 "Return a new syntax table.
888 It inherits all letters and control characters from the standard
889 syntax table; other characters are copied from the standard syntax table."
890 (if oldtable
891 (copy-syntax-table oldtable)
892 (let ((table (copy-syntax-table))
894 (setq i 0)
895 (while (<= i 31)
896 (aset table i nil)
897 (setq i (1+ i)))
898 (setq i ?A)
899 (while (<= i ?Z)
900 (aset table i nil)
901 (setq i (1+ i)))
902 (setq i ?a)
903 (while (<= i ?z)
904 (aset table i nil)
905 (setq i (1+ i)))
906 (setq i 128)
907 (while (<= i 255)
908 (aset table i nil)
909 (setq i (1+ i)))
910 table)))
912 (defun add-to-invisibility-spec (arg)
913 "Add elements to `buffer-invisibility-spec'.
914 See documentation for `buffer-invisibility-spec' for the kind of elements
915 that can be added."
916 (cond
917 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
918 (setq buffer-invisibility-spec (list arg)))
920 (setq buffer-invisibility-spec
921 (cons arg buffer-invisibility-spec)))))
923 (defun remove-from-invisibility-spec (arg)
924 "Remove elements from `buffer-invisibility-spec'."
925 (if buffer-invisibility-spec
926 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
928 (defun global-set-key (key command)
929 "Give KEY a global binding as COMMAND.
930 COMMAND is a symbol naming an interactively-callable function.
931 KEY is a key sequence (a string or vector of characters or event types).
932 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
933 can be included if you use a vector.
934 Note that if KEY has a local binding in the current buffer
935 that local binding will continue to shadow any global binding."
936 (interactive "KSet key globally: \nCSet key %s to command: ")
937 (or (vectorp key) (stringp key)
938 (signal 'wrong-type-argument (list 'arrayp key)))
939 (define-key (current-global-map) key command)
940 nil)
942 (defun local-set-key (key command)
943 "Give KEY a local binding as COMMAND.
944 COMMAND is a symbol naming an interactively-callable function.
945 KEY is a key sequence (a string or vector of characters or event types).
946 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
947 can be included if you use a vector.
948 The binding goes in the current buffer's local map,
949 which in most cases is shared with all other buffers in the same major mode."
950 (interactive "KSet key locally: \nCSet key %s locally to command: ")
951 (let ((map (current-local-map)))
952 (or map
953 (use-local-map (setq map (make-sparse-keymap))))
954 (or (vectorp key) (stringp key)
955 (signal 'wrong-type-argument (list 'arrayp key)))
956 (define-key map key command))
957 nil)
959 (defun global-unset-key (key)
960 "Remove global binding of KEY.
961 KEY is a string representing a sequence of keystrokes."
962 (interactive "kUnset key globally: ")
963 (global-set-key key nil))
965 (defun local-unset-key (key)
966 "Remove local binding of KEY.
967 KEY is a string representing a sequence of keystrokes."
968 (interactive "kUnset key locally: ")
969 (if (current-local-map)
970 (local-set-key key nil))
971 nil)
973 ;; We put this here instead of in frame.el so that it's defined even on
974 ;; systems where frame.el isn't loaded.
975 (defun frame-configuration-p (object)
976 "Return non-nil if OBJECT seems to be a frame configuration.
977 Any list whose car is `frame-configuration' is assumed to be a frame
978 configuration."
979 (and (consp object)
980 (eq (car object) 'frame-configuration)))
982 (defun functionp (object)
983 "Non-nil of OBJECT is a type of object that can be called as a function."
984 (or (subrp object) (compiled-function-p object)
985 (eq (car-safe object) 'lambda)
986 (and (symbolp object) (fboundp object))))
988 ;; now in fns.c
989 ;(defun nth (n list)
990 ; "Returns the Nth element of LIST.
991 ;N counts from zero. If LIST is not that long, nil is returned."
992 ; (car (nthcdr n list)))
994 ;(defun copy-alist (alist)
995 ; "Return a copy of ALIST.
996 ;This is a new alist which represents the same mapping
997 ;from objects to objects, but does not share the alist structure with ALIST.
998 ;The objects mapped (cars and cdrs of elements of the alist)
999 ;are shared, however."
1000 ; (setq alist (copy-sequence alist))
1001 ; (let ((tail alist))
1002 ; (while tail
1003 ; (if (consp (car tail))
1004 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1005 ; (setq tail (cdr tail))))
1006 ; alist)
1008 ;;; subr.el ends here