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