(MKTIME_OBJ):
[emacs.git] / lisp / subr.el
blob220560464165c182221dc9a1eb4164df8046a326
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 defun-inline (name args &rest body)
47 ;; "Create an \"inline defun\" (actually a macro).
48 ;;Use just like `defun'."
49 ;; (nconc (list 'defmacro name '(&rest args))
50 ;; (if (stringp (car body))
51 ;; (prog1 (list (car body))
52 ;; (setq body (or (cdr body) body))))
53 ;; (list (list 'cons (list 'quote
54 ;; (cons 'lambda (cons args body)))
55 ;; 'args))))
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 (eq defn olddef)
129 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
130 (if (and (keymapp defn)
131 ;; Avoid recursively scanning
132 ;; where KEYMAP does not have a submap.
133 (let ((elt (lookup-key keymap prefix1)))
134 (or (null elt)
135 (keymapp elt)))
136 ;; Avoid recursively rescanning keymap being scanned.
137 (not (memq inner-def
138 key-substitution-in-progress)))
139 ;; If this one isn't being scanned already,
140 ;; scan it now.
141 (substitute-key-definition olddef newdef keymap
142 inner-def
143 prefix1)))))
144 (if (arrayp (car scan))
145 (let* ((array (car scan))
146 (len (length array))
147 (i 0))
148 (while (< i len)
149 (let ((char i) (defn (aref array i)))
150 ;; The inside of this let duplicates exactly
151 ;; the inside of the previous let.
152 (aset vec1 0 char)
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 (and (consp defn) (consp (car defn))
160 (setq defn (cdr defn)))
161 (setq inner-def defn)
162 (while (and (symbolp inner-def)
163 (fboundp inner-def))
164 (setq inner-def (symbol-function inner-def)))
165 (if (eq defn olddef)
166 (define-key keymap prefix1
167 (nconc (nreverse skipped) newdef))
168 (if (and (keymapp defn)
169 (let ((elt (lookup-key keymap prefix1)))
170 (or (null elt)
171 (keymapp elt)))
172 (not (memq inner-def
173 key-substitution-in-progress)))
174 (substitute-key-definition olddef newdef keymap
175 inner-def
176 prefix1)))))
177 (setq i (1+ i))))))
178 (setq scan (cdr scan)))))
180 (defun define-key-after (keymap key definition after)
181 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
182 This is like `define-key' except that the binding for KEY is placed
183 just after the binding for the event AFTER, instead of at the beginning
184 of the map. Note that AFTER must be an event type (like KEY), NOT a command
185 \(like DEFINITION).
187 If AFTER is t, the new binding goes at the end of the keymap.
189 KEY must contain just one event type--that is to say, it must be
190 a string or vector of length 1.
192 The order of bindings in a keymap matters when it is used as a menu."
194 (or (keymapp keymap)
195 (signal 'wrong-type-argument (list 'keymapp keymap)))
196 (if (> (length key) 1)
197 (error "multi-event key specified in `define-key-after'"))
198 (let ((tail keymap) done inserted
199 (first (aref key 0)))
200 (while (and (not done) tail)
201 ;; Delete any earlier bindings for the same key.
202 (if (eq (car-safe (car (cdr tail))) first)
203 (setcdr tail (cdr (cdr tail))))
204 ;; When we reach AFTER's binding, insert the new binding after.
205 ;; If we reach an inherited keymap, insert just before that.
206 ;; If we reach the end of this keymap, insert at the end.
207 (if (or (and (eq (car-safe (car tail)) after)
208 (not (eq after t)))
209 (eq (car (cdr tail)) 'keymap)
210 (null (cdr tail)))
211 (progn
212 ;; Stop the scan only if we find a parent keymap.
213 ;; Keep going past the inserted element
214 ;; so we can delete any duplications that come later.
215 (if (eq (car (cdr tail)) 'keymap)
216 (setq done t))
217 ;; Don't insert more than once.
218 (or inserted
219 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
220 (setq inserted t)))
221 (setq tail (cdr tail)))))
223 (put 'keyboard-translate-table 'char-table-extra-slots 0)
225 (defun keyboard-translate (from to)
226 "Translate character FROM to TO at a low level.
227 This function creates a `keyboard-translate-table' if necessary
228 and then modifies one entry in it."
229 (or (char-table-p keyboard-translate-table)
230 (setq keyboard-translate-table
231 (make-char-table 'keyboard-translate-table nil)))
232 (aset keyboard-translate-table from to))
235 ;;;; The global keymap tree.
237 ;;; global-map, esc-map, and ctl-x-map have their values set up in
238 ;;; keymap.c; we just give them docstrings here.
240 (defvar global-map nil
241 "Default global keymap mapping Emacs keyboard input into commands.
242 The value is a keymap which is usually (but not necessarily) Emacs's
243 global map.")
245 (defvar esc-map nil
246 "Default keymap for ESC (meta) commands.
247 The normal global definition of the character ESC indirects to this keymap.")
249 (defvar ctl-x-map nil
250 "Default keymap for C-x commands.
251 The normal global definition of the character C-x indirects to this keymap.")
253 (defvar ctl-x-4-map (make-sparse-keymap)
254 "Keymap for subcommands of C-x 4")
255 (defalias 'ctl-x-4-prefix ctl-x-4-map)
256 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
258 (defvar ctl-x-5-map (make-sparse-keymap)
259 "Keymap for frame commands.")
260 (defalias 'ctl-x-5-prefix ctl-x-5-map)
261 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
264 ;;;; Event manipulation functions.
266 ;; The call to `read' is to ensure that the value is computed at load time
267 ;; and not compiled into the .elc file. The value is negative on most
268 ;; machines, but not on all!
269 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
271 (defun listify-key-sequence (key)
272 "Convert a key sequence to a list of events."
273 (if (vectorp key)
274 (append key nil)
275 (mapcar (function (lambda (c)
276 (if (> c 127)
277 (logxor c listify-key-sequence-1)
278 c)))
279 (append key nil))))
281 (defsubst eventp (obj)
282 "True if the argument is an event object."
283 (or (integerp obj)
284 (and (symbolp obj)
285 (get obj 'event-symbol-elements))
286 (and (consp obj)
287 (symbolp (car obj))
288 (get (car obj) 'event-symbol-elements))))
290 (defun event-modifiers (event)
291 "Returns a list of symbols representing the modifier keys in event EVENT.
292 The elements of the list may include `meta', `control',
293 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
294 and `down'."
295 (let ((type event))
296 (if (listp type)
297 (setq type (car type)))
298 (if (symbolp type)
299 (cdr (get type 'event-symbol-elements))
300 (let ((list nil))
301 (or (zerop (logand type ?\M-\^@))
302 (setq list (cons 'meta list)))
303 (or (and (zerop (logand type ?\C-\^@))
304 (>= (logand type 127) 32))
305 (setq list (cons 'control list)))
306 (or (and (zerop (logand type ?\S-\^@))
307 (= (logand type 255) (downcase (logand type 255))))
308 (setq list (cons 'shift list)))
309 (or (zerop (logand type ?\H-\^@))
310 (setq list (cons 'hyper list)))
311 (or (zerop (logand type ?\s-\^@))
312 (setq list (cons 'super list)))
313 (or (zerop (logand type ?\A-\^@))
314 (setq list (cons 'alt list)))
315 list))))
317 (defun event-basic-type (event)
318 "Returns the basic type of the given event (all modifiers removed).
319 The value is an ASCII printing character (not upper case) or a symbol."
320 (if (consp event)
321 (setq event (car event)))
322 (if (symbolp event)
323 (car (get event 'event-symbol-elements))
324 (let ((base (logand event (1- (lsh 1 18)))))
325 (downcase (if (< base 32) (logior base 64) base)))))
327 (defsubst mouse-movement-p (object)
328 "Return non-nil if OBJECT is a mouse movement event."
329 (and (consp object)
330 (eq (car object) 'mouse-movement)))
332 (defsubst event-start (event)
333 "Return the starting position of EVENT.
334 If EVENT is a mouse press or a mouse click, this returns the location
335 of the event.
336 If EVENT is a drag, this returns the drag's starting position.
337 The return value is of the form
338 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
339 The `posn-' functions access elements of such lists."
340 (nth 1 event))
342 (defsubst event-end (event)
343 "Return the ending location of EVENT. EVENT should be a click or drag event.
344 If EVENT is a click event, this function is the same as `event-start'.
345 The return value is of the form
346 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
347 The `posn-' functions access elements of such lists."
348 (nth (if (consp (nth 2 event)) 2 1) event))
350 (defsubst event-click-count (event)
351 "Return the multi-click count of EVENT, a click or drag event.
352 The return value is a positive integer."
353 (if (integerp (nth 2 event)) (nth 2 event) 1))
355 (defsubst posn-window (position)
356 "Return the window 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 (nth 0 position))
362 (defsubst posn-point (position)
363 "Return the buffer location in POSITION.
364 POSITION should be a list of the form
365 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
366 as returned by the `event-start' and `event-end' functions."
367 (if (consp (nth 1 position))
368 (car (nth 1 position))
369 (nth 1 position)))
371 (defsubst posn-x-y (position)
372 "Return the x and y coordinates in POSITION.
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 (nth 2 position))
378 (defun posn-col-row (position)
379 "Return the column and row in POSITION, measured in characters.
380 POSITION should be a list of the form
381 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
382 as returned by the `event-start' and `event-end' functions.
383 For a scroll-bar event, the result column is 0, and the row
384 corresponds to the vertical position of the click in the scroll bar."
385 (let ((pair (nth 2 position))
386 (window (posn-window position)))
387 (if (eq (if (consp (nth 1 position))
388 (car (nth 1 position))
389 (nth 1 position))
390 'vertical-scroll-bar)
391 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
392 (if (eq (if (consp (nth 1 position))
393 (car (nth 1 position))
394 (nth 1 position))
395 'horizontal-scroll-bar)
396 (cons (scroll-bar-scale pair (window-width window)) 0)
397 (let* ((frame (if (framep window) window (window-frame window)))
398 (x (/ (car pair) (frame-char-width frame)))
399 (y (/ (cdr pair) (frame-char-height frame))))
400 (cons x y))))))
402 (defsubst posn-timestamp (position)
403 "Return the timestamp of POSITION.
404 POSITION should be a list of the form
405 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
406 as returned by the `event-start' and `event-end' functions."
407 (nth 3 position))
410 ;;;; Obsolescent names for functions.
412 (defalias 'dot 'point)
413 (defalias 'dot-marker 'point-marker)
414 (defalias 'dot-min 'point-min)
415 (defalias 'dot-max 'point-max)
416 (defalias 'window-dot 'window-point)
417 (defalias 'set-window-dot 'set-window-point)
418 (defalias 'read-input 'read-string)
419 (defalias 'send-string 'process-send-string)
420 (defalias 'send-region 'process-send-region)
421 (defalias 'show-buffer 'set-window-buffer)
422 (defalias 'buffer-flush-undo 'buffer-disable-undo)
423 (defalias 'eval-current-buffer 'eval-buffer)
424 (defalias 'compiled-function-p 'byte-code-function-p)
425 (defalias 'define-function 'defalias)
427 ;; Some programs still use this as a function.
428 (defun baud-rate ()
429 "Obsolete function returning the value of the `baud-rate' variable.
430 Please convert your programs to use the variable `baud-rate' directly."
431 baud-rate)
433 (defalias 'focus-frame 'ignore)
434 (defalias 'unfocus-frame 'ignore)
436 ;;;; Alternate names for functions - these are not being phased out.
438 (defalias 'string= 'string-equal)
439 (defalias 'string< 'string-lessp)
440 (defalias 'move-marker 'set-marker)
441 (defalias 'not 'null)
442 (defalias 'rplaca 'setcar)
443 (defalias 'rplacd 'setcdr)
444 (defalias 'beep 'ding) ;preserve lingual purity
445 (defalias 'indent-to-column 'indent-to)
446 (defalias 'backward-delete-char 'delete-backward-char)
447 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
448 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
449 (defalias 'int-to-string 'number-to-string)
450 (defalias 'set-match-data 'store-match-data)
452 ;;; Should this be an obsolete name? If you decide it should, you get
453 ;;; to go through all the sources and change them.
454 (defalias 'string-to-int 'string-to-number)
456 ;;;; Hook manipulation functions.
458 (defun make-local-hook (hook)
459 "Make the hook HOOK local to the current buffer.
460 When a hook is local, its local and global values
461 work in concert: running the hook actually runs all the hook
462 functions listed in *either* the local value *or* the global value
463 of the hook variable.
465 This function works by making `t' a member of the buffer-local value,
466 which acts as a flag to run the hook functions in the default value as
467 well. This works for all normal hooks, but does not work for most
468 non-normal hooks yet. We will be changing the callers of non-normal
469 hooks so that they can handle localness; this has to be done one by
470 one.
472 This function does nothing if HOOK is already local in the current
473 buffer.
475 Do not use `make-local-variable' to make a hook variable buffer-local."
476 (if (local-variable-p hook)
478 (or (boundp hook) (set hook nil))
479 (make-local-variable hook)
480 (set hook (list t))))
482 (defun add-hook (hook function &optional append local)
483 "Add to the value of HOOK the function FUNCTION.
484 FUNCTION is not added if already present.
485 FUNCTION is added (if necessary) at the beginning of the hook list
486 unless the optional argument APPEND is non-nil, in which case
487 FUNCTION is added at the end.
489 The optional fourth argument, LOCAL, if non-nil, says to modify
490 the hook's buffer-local value rather than its default value.
491 This makes no difference if the hook is not buffer-local.
492 To make a hook variable buffer-local, always use
493 `make-local-hook', not `make-local-variable'.
495 HOOK should be a symbol, and FUNCTION may be any valid function. If
496 HOOK is void, it is first set to nil. If HOOK's value is a single
497 function, it is changed to a list of functions."
498 (or (boundp hook) (set hook nil))
499 (or (default-boundp hook) (set-default hook nil))
500 ;; If the hook value is a single function, turn it into a list.
501 (let ((old (symbol-value hook)))
502 (if (or (not (listp old)) (eq (car old) 'lambda))
503 (set hook (list old))))
504 (if (or local
505 ;; Detect the case where make-local-variable was used on a hook
506 ;; and do what we used to do.
507 (and (local-variable-if-set-p hook)
508 (not (memq t (symbol-value hook)))))
509 ;; Alter the local value only.
510 (or (if (consp function)
511 (member function (symbol-value hook))
512 (memq function (symbol-value hook)))
513 (set hook
514 (if append
515 (append (symbol-value hook) (list function))
516 (cons function (symbol-value hook)))))
517 ;; Alter the global value (which is also the only value,
518 ;; if the hook doesn't have a local value).
519 (or (if (consp function)
520 (member function (default-value hook))
521 (memq function (default-value hook)))
522 (set-default hook
523 (if append
524 (append (default-value hook) (list function))
525 (cons function (default-value hook)))))))
527 (defun remove-hook (hook function &optional local)
528 "Remove from the value of HOOK the function FUNCTION.
529 HOOK should be a symbol, and FUNCTION may be any valid function. If
530 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
531 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
533 The optional third argument, LOCAL, if non-nil, says to modify
534 the hook's buffer-local value rather than its default value.
535 This makes no difference if the hook is not buffer-local.
536 To make a hook variable buffer-local, always use
537 `make-local-hook', not `make-local-variable'."
538 (if (or (not (boundp hook)) ;unbound symbol, or
539 (not (default-boundp 'hook))
540 (null (symbol-value hook)) ;value is nil, or
541 (null function)) ;function is nil, then
542 nil ;Do nothing.
543 (if (or local
544 ;; Detect the case where make-local-variable was used on a hook
545 ;; and do what we used to do.
546 (and (local-variable-p hook)
547 (not (memq t (symbol-value hook)))))
548 (let ((hook-value (symbol-value hook)))
549 (if (consp hook-value)
550 (if (member function hook-value)
551 (setq hook-value (delete function (copy-sequence hook-value))))
552 (if (equal hook-value function)
553 (setq hook-value nil)))
554 (set hook hook-value))
555 (let ((hook-value (default-value hook)))
556 (if (consp hook-value)
557 (if (member function hook-value)
558 (setq hook-value (delete function (copy-sequence hook-value))))
559 (if (equal hook-value function)
560 (setq hook-value nil)))
561 (set-default hook hook-value)))))
563 (defun add-to-list (list-var element)
564 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
565 The test for presence of ELEMENT is done with `equal'.
566 If you want to use `add-to-list' on a variable that is not defined
567 until a certain package is loaded, you should put the call to `add-to-list'
568 into a hook function that will be run only after loading the package.
569 `eval-after-load' provides one way to do this. In some cases
570 other hooks, such as major mode hooks, can do the job."
571 (or (member element (symbol-value list-var))
572 (set list-var (cons element (symbol-value list-var)))))
574 ;;;; Specifying things to do after certain files are loaded.
576 (defun eval-after-load (file form)
577 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
578 This makes or adds to an entry on `after-load-alist'.
579 If FILE is already loaded, evaluate FORM right now.
580 It does nothing if FORM is already on the list for FILE.
581 FILE should be the name of a library, with no directory name."
582 ;; Make sure there is an element for FILE.
583 (or (assoc file after-load-alist)
584 (setq after-load-alist (cons (list file) after-load-alist)))
585 ;; Add FORM to the element if it isn't there.
586 (let ((elt (assoc file after-load-alist)))
587 (or (member form (cdr elt))
588 (progn
589 (nconc elt (list form))
590 ;; If the file has been loaded already, run FORM right away.
591 (and (assoc file load-history)
592 (eval form)))))
593 form)
595 (defun eval-next-after-load (file)
596 "Read the following input sexp, and run it whenever FILE is loaded.
597 This makes or adds to an entry on `after-load-alist'.
598 FILE should be the name of a library, with no directory name."
599 (eval-after-load file (read)))
602 ;;;; Input and display facilities.
604 (defun read-quoted-char (&optional prompt)
605 "Like `read-char', except that if the first character read is an octal
606 digit, we read up to two more octal digits and return the character
607 represented by the octal number consisting of those digits.
608 Optional argument PROMPT specifies a string to use to prompt the user."
609 (let ((message-log-max nil) (count 0) (code 0) char)
610 (while (< count 3)
611 (let ((inhibit-quit (zerop count))
612 ;; Don't let C-h get the help message--only help function keys.
613 (help-char nil)
614 (help-form
615 "Type the special character you want to use,
616 or three octal digits representing its character code."))
617 (and prompt (message "%s-" prompt))
618 (setq char (read-char))
619 (if inhibit-quit (setq quit-flag nil)))
620 (cond ((null char))
621 ((and (<= ?0 char) (<= char ?7))
622 (setq code (+ (* code 8) (- char ?0))
623 count (1+ count))
624 (and prompt (setq prompt (message "%s %c" prompt char))))
625 ((> count 0)
626 (setq unread-command-events (list char) count 259))
627 (t (setq code char count 259))))
628 ;; Turn a meta-character into a character with the 0200 bit set.
629 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
630 (logand 255 code))))
632 (defun force-mode-line-update (&optional all)
633 "Force the mode-line of the current buffer to be redisplayed.
634 With optional non-nil ALL, force redisplay of all mode-lines."
635 (if all (save-excursion (set-buffer (other-buffer))))
636 (set-buffer-modified-p (buffer-modified-p)))
638 (defun momentary-string-display (string pos &optional exit-char message)
639 "Momentarily display STRING in the buffer at POS.
640 Display remains until next character is typed.
641 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
642 otherwise it is then available as input (as a command if nothing else).
643 Display MESSAGE (optional fourth arg) in the echo area.
644 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
645 (or exit-char (setq exit-char ?\ ))
646 (let ((buffer-read-only nil)
647 ;; Don't modify the undo list at all.
648 (buffer-undo-list t)
649 (modified (buffer-modified-p))
650 (name buffer-file-name)
651 insert-end)
652 (unwind-protect
653 (progn
654 (save-excursion
655 (goto-char pos)
656 ;; defeat file locking... don't try this at home, kids!
657 (setq buffer-file-name nil)
658 (insert-before-markers string)
659 (setq insert-end (point))
660 ;; If the message end is off screen, recenter now.
661 (if (> (window-end) insert-end)
662 (recenter (/ (window-height) 2)))
663 ;; If that pushed message start off the screen,
664 ;; scroll to start it at the top of the screen.
665 (move-to-window-line 0)
666 (if (> (point) pos)
667 (progn
668 (goto-char pos)
669 (recenter 0))))
670 (message (or message "Type %s to continue editing.")
671 (single-key-description exit-char))
672 (let ((char (read-event)))
673 (or (eq char exit-char)
674 (setq unread-command-events (list char)))))
675 (if insert-end
676 (save-excursion
677 (delete-region pos insert-end)))
678 (setq buffer-file-name name)
679 (set-buffer-modified-p modified))))
682 ;;;; Miscellanea.
684 ;; A number of major modes set this locally.
685 ;; Give it a global value to avoid compiler warnings.
686 (defvar font-lock-defaults nil)
688 ;; Avoid compiler warnings about this variable,
689 ;; which has a special meaning on certain system types.
690 (defvar buffer-file-type nil
691 "Non-nil if the visited file is a binary file.
692 This variable is meaningful on MS-DOG and Windows NT.
693 On those systems, it is automatically local in every buffer.
694 On other systems, this variable is normally always nil.")
696 ;; This should probably be written in C (i.e., without using `walk-windows').
697 (defun get-buffer-window-list (buffer &optional minibuf frame)
698 "Return windows currently displaying BUFFER, or nil if none.
699 See `walk-windows' for the meaning of MINIBUF and FRAME."
700 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
701 (walk-windows (function (lambda (window)
702 (if (eq (window-buffer window) buffer)
703 (setq windows (cons window windows)))))
704 minibuf frame)
705 windows))
707 (defun ignore (&rest ignore)
708 "Do nothing and return nil.
709 This function accepts any number of arguments, but ignores them."
710 (interactive)
711 nil)
713 (defun error (&rest args)
714 "Signal an error, making error message by passing all args to `format'.
715 In Emacs, the convention is that error messages start with a capital
716 letter but *do not* end with a period. Please follow this convention
717 for the sake of consistency."
718 (while t
719 (signal 'error (list (apply 'format args)))))
721 (defalias 'user-original-login-name 'user-login-name)
723 (defun start-process-shell-command (name buffer &rest args)
724 "Start a program in a subprocess. Return the process object for it.
725 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
726 NAME is name for process. It is modified if necessary to make it unique.
727 BUFFER is the buffer or (buffer-name) to associate with the process.
728 Process output goes at end of that buffer, unless you specify
729 an output stream or filter function to handle the output.
730 BUFFER may be also nil, meaning that this process is not associated
731 with any buffer
732 Third arg is command name, the name of a shell command.
733 Remaining arguments are the arguments for the command.
734 Wildcards and redirection are handled as usual in the shell."
735 (cond
736 ((eq system-type 'vax-vms)
737 (apply 'start-process name buffer args))
738 ;; We used to use `exec' to replace the shell with the command,
739 ;; but that failed to handle (...) and semicolon, etc.
741 (start-process name buffer shell-file-name shell-command-switch
742 (mapconcat 'identity args " ")))))
744 (defmacro with-current-buffer (buffer &rest body)
745 "Execute the forms in BODY with BUFFER as the current buffer.
746 The value returned is the value of the last form in BODY.
747 See also `with-temp-buffer'."
748 `(save-current-buffer
749 (set-buffer ,buffer)
750 ,@body))
752 (defmacro with-temp-file (file &rest forms)
753 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
754 The value of the last form in FORMS is returned, like `progn'.
755 See also `with-temp-buffer'."
756 (let ((temp-file (make-symbol "temp-file"))
757 (temp-buffer (make-symbol "temp-buffer")))
758 `(let ((,temp-file ,file)
759 (,temp-buffer
760 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
761 (unwind-protect
762 (prog1
763 (with-current-buffer ,temp-buffer
764 ,@forms)
765 (with-current-buffer ,temp-buffer
766 (widen)
767 (write-region (point-min) (point-max) ,temp-file nil 0)))
768 (and (buffer-name ,temp-buffer)
769 (kill-buffer ,temp-buffer))))))
771 (defmacro with-temp-buffer (&rest forms)
772 "Create a temporary buffer, and evaluate FORMS there like `progn'.
773 See also `with-temp-file' and `with-output-to-string'."
774 (let ((temp-buffer (make-symbol "temp-buffer")))
775 `(let ((,temp-buffer
776 (get-buffer-create (generate-new-buffer-name " *temp*"))))
777 (unwind-protect
778 (with-current-buffer ,temp-buffer
779 ,@forms)
780 (and (buffer-name ,temp-buffer)
781 (kill-buffer ,temp-buffer))))))
783 (defmacro with-output-to-string (&rest body)
784 "Execute BODY, return the text it sent to `standard-output', as a string."
785 `(let ((standard-output
786 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
787 (let ((standard-output standard-output))
788 ,@body)
789 (with-current-buffer standard-output
790 (prog1
791 (buffer-string)
792 (kill-buffer nil)))))
794 (defmacro combine-after-change-calls (&rest body)
795 "Execute BODY, but don't call the after-change functions till the end.
796 If BODY makes changes in the buffer, they are recorded
797 and the functions on `after-change-functions' are called several times
798 when BODY is finished.
799 The return value is rthe value of the last form in BODY.
801 If `before-change-functions' is non-nil, then calls to the after-change
802 functions can't be deferred, so in that case this macro has no effect.
804 Do not alter `after-change-functions' or `before-change-functions'
805 in BODY."
806 `(unwind-protect
807 (let ((combine-after-change-calls t))
808 . ,body)
809 (combine-after-change-execute)))
812 (defvar save-match-data-internal)
814 ;; We use save-match-data-internal as the local variable because
815 ;; that works ok in practice (people should not use that variable elsewhere).
816 ;; We used to use an uninterned symbol; the compiler handles that properly
817 ;; now, but it generates slower code.
818 (defmacro save-match-data (&rest body)
819 "Execute the BODY forms, restoring the global value of the match data."
820 `(let ((save-match-data-internal (match-data)))
821 (unwind-protect
822 (progn ,@body)
823 (store-match-data save-match-data-internal))))
825 (defun match-string (num &optional string)
826 "Return string of text matched by last search.
827 NUM specifies which parenthesized expression in the last regexp.
828 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
829 Zero means the entire text matched by the whole regexp or whole string.
830 STRING should be given if the last search was by `string-match' on STRING."
831 (if (match-beginning num)
832 (if string
833 (substring string (match-beginning num) (match-end num))
834 (buffer-substring (match-beginning num) (match-end num)))))
836 (defun split-string (string &optional separators)
837 "Splits STRING into substrings where there are matches for SEPARATORS.
838 Each match for SEPARATORS is a splitting point.
839 The substrings between the splitting points are made into a list
840 which is returned.
841 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
842 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
843 (start 0)
844 (list nil))
845 (while (string-match rexp string start)
846 (or (eq (match-beginning 0) 0)
847 (setq list
848 (cons (substring string start (match-beginning 0))
849 list)))
850 (setq start (match-end 0)))
851 (or (eq start (length string))
852 (setq list
853 (cons (substring string start)
854 list)))
855 (nreverse list)))
857 (defun shell-quote-argument (argument)
858 "Quote an argument for passing as argument to an inferior shell."
859 (if (eq system-type 'ms-dos)
860 ;; MS-DOS shells don't have quoting, so don't do any.
861 argument
862 (if (eq system-type 'windows-nt)
863 (concat "\"" argument "\"")
864 ;; Quote everything except POSIX filename characters.
865 ;; This should be safe enough even for really weird shells.
866 (let ((result "") (start 0) end)
867 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
868 (setq end (match-beginning 0)
869 result (concat result (substring argument start end)
870 "\\" (substring argument end (1+ end)))
871 start (1+ end)))
872 (concat result (substring argument start))))))
874 (defun make-syntax-table (&optional oldtable)
875 "Return a new syntax table.
876 It inherits all letters and control characters from the standard
877 syntax table; other characters are copied from the standard syntax table."
878 (if oldtable
879 (copy-syntax-table oldtable)
880 (let ((table (copy-syntax-table))
882 (setq i 0)
883 (while (<= i 31)
884 (aset table i nil)
885 (setq i (1+ i)))
886 (setq i ?A)
887 (while (<= i ?Z)
888 (aset table i nil)
889 (setq i (1+ i)))
890 (setq i ?a)
891 (while (<= i ?z)
892 (aset table i nil)
893 (setq i (1+ i)))
894 (setq i 128)
895 (while (<= i 255)
896 (aset table i nil)
897 (setq i (1+ i)))
898 table)))
900 (defun global-set-key (key command)
901 "Give KEY a global binding as COMMAND.
902 COMMAND is a symbol naming an interactively-callable function.
903 KEY is a key sequence (a string or vector of characters or event types).
904 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
905 can be included if you use a vector.
906 Note that if KEY has a local binding in the current buffer
907 that local binding will continue to shadow any global binding."
908 (interactive "KSet key globally: \nCSet key %s to command: ")
909 (or (vectorp key) (stringp key)
910 (signal 'wrong-type-argument (list 'arrayp key)))
911 (define-key (current-global-map) key command)
912 nil)
914 (defun local-set-key (key command)
915 "Give KEY a local binding as COMMAND.
916 COMMAND is a symbol naming an interactively-callable function.
917 KEY is a key sequence (a string or vector of characters or event types).
918 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
919 can be included if you use a vector.
920 The binding goes in the current buffer's local map,
921 which in most cases is shared with all other buffers in the same major mode."
922 (interactive "KSet key locally: \nCSet key %s locally to command: ")
923 (let ((map (current-local-map)))
924 (or map
925 (use-local-map (setq map (make-sparse-keymap))))
926 (or (vectorp key) (stringp key)
927 (signal 'wrong-type-argument (list 'arrayp key)))
928 (define-key map key command))
929 nil)
931 (defun global-unset-key (key)
932 "Remove global binding of KEY.
933 KEY is a string representing a sequence of keystrokes."
934 (interactive "kUnset key globally: ")
935 (global-set-key key nil))
937 (defun local-unset-key (key)
938 "Remove local binding of KEY.
939 KEY is a string representing a sequence of keystrokes."
940 (interactive "kUnset key locally: ")
941 (if (current-local-map)
942 (local-set-key key nil))
943 nil)
945 ;; We put this here instead of in frame.el so that it's defined even on
946 ;; systems where frame.el isn't loaded.
947 (defun frame-configuration-p (object)
948 "Return non-nil if OBJECT seems to be a frame configuration.
949 Any list whose car is `frame-configuration' is assumed to be a frame
950 configuration."
951 (and (consp object)
952 (eq (car object) 'frame-configuration)))
954 ;; now in fns.c
955 ;(defun nth (n list)
956 ; "Returns the Nth element of LIST.
957 ;N counts from zero. If LIST is not that long, nil is returned."
958 ; (car (nthcdr n list)))
960 ;(defun copy-alist (alist)
961 ; "Return a copy of ALIST.
962 ;This is a new alist which represents the same mapping
963 ;from objects to objects, but does not share the alist structure with ALIST.
964 ;The objects mapped (cars and cdrs of elements of the alist)
965 ;are shared, however."
966 ; (setq alist (copy-sequence alist))
967 ; (let ((tail alist))
968 ; (while tail
969 ; (if (consp (car tail))
970 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
971 ; (setq tail (cdr tail))))
972 ; alist)
974 ;;; subr.el ends here