(listify-key-sequence-1): Update for change in CHAR_META.
[bpt/emacs.git] / 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 ;; This code exists specifically to make sure that the
315 ;; resulting number does not appear in the .elc file.
316 ;; The number is negative on most machines, but not on all!
317 (defconst listify-key-sequence-1
318 (lsh 1 7))
319 (setq listify-key-sequence-1 (logior (lsh 1 27) listify-key-sequence-1))
320
321 (defun listify-key-sequence (key)
322 "Convert a key sequence to a list of events."
323 (if (vectorp key)
324 (append key nil)
325 (mapcar (function (lambda (c)
326 (if (> c 127)
327 (logxor c listify-key-sequence-1)
328 c)))
329 (append key nil))))
330
331 (defsubst eventp (obj)
332 "True if the argument is an event object."
333 (or (integerp obj)
334 (and (symbolp obj)
335 (get obj 'event-symbol-elements))
336 (and (consp obj)
337 (symbolp (car obj))
338 (get (car obj) 'event-symbol-elements))))
339
340 (defun event-modifiers (event)
341 "Returns a list of symbols representing the modifier keys in event EVENT.
342 The elements of the list may include `meta', `control',
343 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
344 and `down'."
345 (let ((type event))
346 (if (listp type)
347 (setq type (car type)))
348 (if (symbolp type)
349 (cdr (get type 'event-symbol-elements))
350 (let ((list nil))
351 (or (zerop (logand type (lsh 1 23)))
352 (setq list (cons 'meta list)))
353 (or (and (zerop (logand type (lsh 1 22)))
354 (>= (logand type 127) 32))
355 (setq list (cons 'control list)))
356 (or (and (zerop (logand type (lsh 1 21)))
357 (= (logand type 255) (downcase (logand type 255))))
358 (setq list (cons 'shift list)))
359 (or (zerop (logand type (lsh 1 20)))
360 (setq list (cons 'hyper list)))
361 (or (zerop (logand type (lsh 1 19)))
362 (setq list (cons 'super list)))
363 (or (zerop (logand type (lsh 1 18)))
364 (setq list (cons 'alt list)))
365 list))))
366
367 (defun event-basic-type (event)
368 "Returns the basic type of the given event (all modifiers removed).
369 The value is an ASCII printing character (not upper case) or a symbol."
370 (if (consp event)
371 (setq event (car event)))
372 (if (symbolp event)
373 (car (get event 'event-symbol-elements))
374 (let ((base (logand event (1- (lsh 1 18)))))
375 (downcase (if (< base 32) (logior base 64) base)))))
376
377 (defsubst mouse-movement-p (object)
378 "Return non-nil if OBJECT is a mouse movement event."
379 (and (consp object)
380 (eq (car object) 'mouse-movement)))
381
382 (defsubst event-start (event)
383 "Return the starting position of EVENT.
384 If EVENT is a mouse press or a mouse click, this returns the location
385 of the event.
386 If EVENT is a drag, this returns the drag's starting position.
387 The return value is of the form
388 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
389 The `posn-' functions access elements of such lists."
390 (nth 1 event))
391
392 (defsubst event-end (event)
393 "Return the ending location of EVENT. EVENT should be a click or drag event.
394 If EVENT is a click event, this function is the same as `event-start'.
395 The return value is of the form
396 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
397 The `posn-' functions access elements of such lists."
398 (nth (if (consp (nth 2 event)) 2 1) event))
399
400 (defsubst event-click-count (event)
401 "Return the multi-click count of EVENT, a click or drag event.
402 The return value is a positive integer."
403 (if (integerp (nth 2 event)) (nth 2 event) 1))
404
405 (defsubst posn-window (position)
406 "Return the window in POSITION.
407 POSITION should be a list of the form
408 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
409 as returned by the `event-start' and `event-end' functions."
410 (nth 0 position))
411
412 (defsubst posn-point (position)
413 "Return the buffer location in POSITION.
414 POSITION should be a list of the form
415 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
416 as returned by the `event-start' and `event-end' functions."
417 (if (consp (nth 1 position))
418 (car (nth 1 position))
419 (nth 1 position)))
420
421 (defsubst posn-x-y (position)
422 "Return the x and y coordinates in POSITION.
423 POSITION should be a list of the form
424 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
425 as returned by the `event-start' and `event-end' functions."
426 (nth 2 position))
427
428 (defun posn-col-row (position)
429 "Return the column and row in POSITION, measured in characters.
430 POSITION should be a list of the form
431 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
432 as returned by the `event-start' and `event-end' functions.
433 For a scroll-bar event, the result column is 0, and the row
434 corresponds to the vertical position of the click in the scroll bar."
435 (let ((pair (nth 2 position))
436 (window (posn-window position)))
437 (if (eq (if (consp (nth 1 position))
438 (car (nth 1 position))
439 (nth 1 position))
440 'vertical-scroll-bar)
441 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
442 (if (eq (if (consp (nth 1 position))
443 (car (nth 1 position))
444 (nth 1 position))
445 'horizontal-scroll-bar)
446 (cons (scroll-bar-scale pair (window-width window)) 0)
447 (let* ((frame (if (framep window) window (window-frame window)))
448 (x (/ (car pair) (frame-char-width frame)))
449 (y (/ (cdr pair) (frame-char-height frame))))
450 (cons x y))))))
451
452 (defsubst posn-timestamp (position)
453 "Return the timestamp of POSITION.
454 POSITION should be a list of the form
455 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
456 as returned by the `event-start' and `event-end' functions."
457 (nth 3 position))
458
459 \f
460 ;;;; Obsolescent names for functions.
461
462 (defalias 'dot 'point)
463 (defalias 'dot-marker 'point-marker)
464 (defalias 'dot-min 'point-min)
465 (defalias 'dot-max 'point-max)
466 (defalias 'window-dot 'window-point)
467 (defalias 'set-window-dot 'set-window-point)
468 (defalias 'read-input 'read-string)
469 (defalias 'send-string 'process-send-string)
470 (defalias 'send-region 'process-send-region)
471 (defalias 'show-buffer 'set-window-buffer)
472 (defalias 'buffer-flush-undo 'buffer-disable-undo)
473 (defalias 'eval-current-buffer 'eval-buffer)
474 (defalias 'compiled-function-p 'byte-code-function-p)
475
476 ;; Some programs still use this as a function.
477 (defun baud-rate ()
478 "Obsolete function returning the value of the `baud-rate' variable.
479 Please convert your programs to use the variable `baud-rate' directly."
480 baud-rate)
481
482 \f
483 ;;;; Alternate names for functions - these are not being phased out.
484
485 (defalias 'string= 'string-equal)
486 (defalias 'string< 'string-lessp)
487 (defalias 'move-marker 'set-marker)
488 (defalias 'eql 'eq)
489 (defalias 'not 'null)
490 (defalias 'rplaca 'setcar)
491 (defalias 'rplacd 'setcdr)
492 (defalias 'beep 'ding) ;preserve lingual purity
493 (defalias 'indent-to-column 'indent-to)
494 (defalias 'backward-delete-char 'delete-backward-char)
495 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
496 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
497 (defalias 'int-to-string 'number-to-string)
498 (defalias 'set-match-data 'store-match-data)
499
500 ;;; Should this be an obsolete name? If you decide it should, you get
501 ;;; to go through all the sources and change them.
502 (defalias 'string-to-int 'string-to-number)
503 \f
504 ;;;; Hook manipulation functions.
505
506 (defun run-hooks (&rest hooklist)
507 "Takes hook names and runs each one in turn. Major mode functions use this.
508 Each argument should be a symbol, a hook variable.
509 These symbols are processed in the order specified.
510 If a hook symbol has a non-nil value, that value may be a function
511 or a list of functions to be called to run the hook.
512 If the value is a function, it is called with no arguments.
513 If it is a list, the elements are called, in order, with no arguments.
514
515 To make a hook variable buffer-local, use `make-local-hook', not
516 `make-local-variable'."
517 (while hooklist
518 (let ((sym (car hooklist)))
519 (and (boundp sym)
520 (symbol-value sym)
521 (let ((value (symbol-value sym)))
522 (if (and (listp value) (not (eq (car value) 'lambda)))
523 (while value
524 (if (eq (car value) t)
525 ;; t indicates this hook has a local binding;
526 ;; it means to run the global binding too.
527 (let ((functions (default-value sym)))
528 (while functions
529 (funcall (car functions))
530 (setq functions (cdr functions))))
531 (funcall (car value)))
532 (setq value (cdr value)))
533 (funcall value)))))
534 (setq hooklist (cdr hooklist))))
535
536 (defun run-hook-with-args (hook &rest args)
537 "Run HOOK with the specified arguments ARGS.
538 HOOK should be a symbol, a hook variable. If HOOK has a non-nil
539 value, that value may be a function or a list of functions to be
540 called to run the hook. If the value is a function, it is called with
541 the given arguments and its return value is returned. If it is a list
542 of functions, those functions are called, in order,
543 with the given arguments ARGS.
544 It is best not to depend on the value return by `run-hook-with-args',
545 as that may change.
546
547 To make a hook variable buffer-local, use `make-local-hook', not
548 `make-local-variable'."
549 (and (boundp hook)
550 (symbol-value hook)
551 (let ((value (symbol-value hook)))
552 (if (and (listp value) (not (eq (car value) 'lambda)))
553 (while value
554 (if (eq (car value) t)
555 ;; t indicates this hook has a local binding;
556 ;; it means to run the global binding too.
557 (let ((functions (default-value hook)))
558 (while functions
559 (apply (car functions) args)
560 (setq functions (cdr functions))))
561 (apply (car value) args))
562 (setq value (cdr value)))
563 (apply value args)))))
564
565 (defun run-hook-with-args-until-success (hook &rest args)
566 "Run HOOK with the specified arguments ARGS.
567 HOOK should be a symbol, a hook variable. Its value should
568 be a list of functions. We call those functions, one by one,
569 passing arguments ARGS to each of them, until one of them
570 returns a non-nil value. Then we return that value.
571 If all the functions return nil, we return nil.
572
573 To make a hook variable buffer-local, use `make-local-hook', not
574 `make-local-variable'."
575 (and (boundp hook)
576 (symbol-value hook)
577 (let ((value (symbol-value hook))
578 success)
579 (while (and value (not success))
580 (if (eq (car value) t)
581 ;; t indicates this hook has a local binding;
582 ;; it means to run the global binding too.
583 (let ((functions (default-value hook)))
584 (while (and functions (not success))
585 (setq success (apply (car functions) args))
586 (setq functions (cdr functions))))
587 (setq success (apply (car value) args)))
588 (setq value (cdr value)))
589 success)))
590
591 (defun run-hook-with-args-until-failure (hook &rest args)
592 "Run HOOK with the specified arguments ARGS.
593 HOOK should be a symbol, a hook variable. Its value should
594 be a list of functions. We call those functions, one by one,
595 passing arguments ARGS to each of them, until one of them
596 returns nil. Then we return nil.
597 If all the functions return non-nil, we return non-nil.
598
599 To make a hook variable buffer-local, use `make-local-hook', not
600 `make-local-variable'."
601 ;; We must return non-nil if there are no hook functions!
602 (or (not (boundp hook))
603 (not (symbol-value hook))
604 (let ((value (symbol-value hook))
605 (success t))
606 (while (and value success)
607 (if (eq (car value) t)
608 ;; t indicates this hook has a local binding;
609 ;; it means to run the global binding too.
610 (let ((functions (default-value hook)))
611 (while (and functions success)
612 (setq success (apply (car functions) args))
613 (setq functions (cdr functions))))
614 (setq success (apply (car value) args)))
615 (setq value (cdr value)))
616 success)))
617
618 ;; Tell C code how to call this function.
619 (defconst run-hooks 'run-hooks
620 "Variable by which C primitives find the function `run-hooks'.
621 Don't change it.")
622
623 (defun make-local-hook (hook)
624 "Make the hook HOOK local to the current buffer.
625 When a hook is local, its local and global values
626 work in concert: running the hook actually runs all the hook
627 functions listed in *either* the local value *or* the global value
628 of the hook variable.
629
630 This function does nothing if HOOK is already local in the current buffer.
631
632 Do not use `make-local-variable' to make a hook variable buffer-local."
633 (if (local-variable-p hook)
634 nil
635 (or (boundp hook) (set hook nil))
636 (make-local-variable hook)
637 (set hook (list t))))
638
639 (defun add-hook (hook function &optional append local)
640 "Add to the value of HOOK the function FUNCTION.
641 FUNCTION is not added if already present.
642 FUNCTION is added (if necessary) at the beginning of the hook list
643 unless the optional argument APPEND is non-nil, in which case
644 FUNCTION is added at the end.
645
646 The optional fourth argument, LOCAL, if non-nil, says to modify
647 the hook's buffer-local value rather than its default value.
648 This makes no difference if the hook is not buffer-local.
649 To make a hook variable buffer-local, always use
650 `make-local-hook', not `make-local-variable'.
651
652 HOOK should be a symbol, and FUNCTION may be any valid function. If
653 HOOK is void, it is first set to nil. If HOOK's value is a single
654 function, it is changed to a list of functions."
655 (or (boundp hook) (set hook nil))
656 (or (default-boundp hook) (set-default hook nil))
657 ;; If the hook value is a single function, turn it into a list.
658 (let ((old (symbol-value hook)))
659 (if (or (not (listp old)) (eq (car old) 'lambda))
660 (set hook (list old))))
661 (if (or local
662 ;; Detect the case where make-local-variable was used on a hook
663 ;; and do what we used to do.
664 (and (local-variable-p hook)
665 (not (memq t (symbol-value hook)))))
666 ;; Alter the local value only.
667 (or (if (consp function)
668 (member function (symbol-value hook))
669 (memq function (symbol-value hook)))
670 (set hook
671 (if append
672 (append (symbol-value hook) (list function))
673 (cons function (symbol-value hook)))))
674 ;; Alter the global value (which is also the only value,
675 ;; if the hook doesn't have a local value).
676 (or (if (consp function)
677 (member function (default-value hook))
678 (memq function (default-value hook)))
679 (set-default hook
680 (if append
681 (append (default-value hook) (list function))
682 (cons function (default-value hook)))))))
683
684 (defun remove-hook (hook function &optional local)
685 "Remove from the value of HOOK the function FUNCTION.
686 HOOK should be a symbol, and FUNCTION may be any valid function. If
687 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
688 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
689
690 The optional third argument, LOCAL, if non-nil, says to modify
691 the hook's buffer-local value rather than its default value.
692 This makes no difference if the hook is not buffer-local.
693 To make a hook variable buffer-local, always use
694 `make-local-hook', not `make-local-variable'."
695 (if (or (not (boundp hook)) ;unbound symbol, or
696 (not (default-boundp 'hook))
697 (null (symbol-value hook)) ;value is nil, or
698 (null function)) ;function is nil, then
699 nil ;Do nothing.
700 (if (or local
701 ;; Detect the case where make-local-variable was used on a hook
702 ;; and do what we used to do.
703 (and (local-variable-p hook)
704 (not (memq t (symbol-value hook)))))
705 (let ((hook-value (symbol-value hook)))
706 (if (consp hook-value)
707 (if (member function hook-value)
708 (setq hook-value (delete function (copy-sequence hook-value))))
709 (if (equal hook-value function)
710 (setq hook-value nil)))
711 (set hook hook-value))
712 (let ((hook-value (default-value hook)))
713 (if (consp hook-value)
714 (if (member function hook-value)
715 (setq hook-value (delete function (copy-sequence hook-value))))
716 (if (equal hook-value function)
717 (setq hook-value nil)))
718 (set-default hook hook-value)))))
719
720 (defun add-to-list (list-var element)
721 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
722 If you want to use `add-to-list' on a variable that is not defined
723 until a certain package is loaded, you should put the call to `add-to-list'
724 into a hook function that will be run only after loading the package.
725 `eval-after-load' provides one way to do this. In some cases
726 other hooks, such as major mode hooks, can do the job."
727 (or (member element (symbol-value list-var))
728 (set list-var (cons element (symbol-value list-var)))))
729 \f
730 ;;;; Specifying things to do after certain files are loaded.
731
732 (defun eval-after-load (file form)
733 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
734 This makes or adds to an entry on `after-load-alist'.
735 It does nothing if FORM is already on the list for FILE.
736 FILE should be the name of a library, with no directory name."
737 (or (assoc file after-load-alist)
738 (setq after-load-alist (cons (list file) after-load-alist)))
739 (let ((elt (assoc file after-load-alist)))
740 (or (member form (cdr elt))
741 (nconc elt (list form))))
742 form)
743
744 (defun eval-next-after-load (file)
745 "Read the following input sexp, and run it whenever FILE is loaded.
746 This makes or adds to an entry on `after-load-alist'.
747 FILE should be the name of a library, with no directory name."
748 (eval-after-load file (read)))
749
750 \f
751 ;;;; Input and display facilities.
752
753 (defun read-quoted-char (&optional prompt)
754 "Like `read-char', except that if the first character read is an octal
755 digit, we read up to two more octal digits and return the character
756 represented by the octal number consisting of those digits.
757 Optional argument PROMPT specifies a string to use to prompt the user."
758 (let ((count 0) (code 0) char)
759 (while (< count 3)
760 (let ((inhibit-quit (zerop count))
761 (help-form nil))
762 (and prompt (message "%s-" prompt))
763 (setq char (read-char))
764 (if inhibit-quit (setq quit-flag nil)))
765 (cond ((null char))
766 ((and (<= ?0 char) (<= char ?7))
767 (setq code (+ (* code 8) (- char ?0))
768 count (1+ count))
769 (and prompt (message (setq prompt
770 (format "%s %c" prompt char)))))
771 ((> count 0)
772 (setq unread-command-events (list char) count 259))
773 (t (setq code char count 259))))
774 ;; Turn a meta-character into a character with the 0200 bit set.
775 (logior (if (/= (logand code (lsh 1 23)) 0) 128 0)
776 (logand 255 code))))
777
778 (defun force-mode-line-update (&optional all)
779 "Force the mode-line of the current buffer to be redisplayed.
780 With optional non-nil ALL, force redisplay of all mode-lines."
781 (if all (save-excursion (set-buffer (other-buffer))))
782 (set-buffer-modified-p (buffer-modified-p)))
783
784 (defun momentary-string-display (string pos &optional exit-char message)
785 "Momentarily display STRING in the buffer at POS.
786 Display remains until next character is typed.
787 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
788 otherwise it is then available as input (as a command if nothing else).
789 Display MESSAGE (optional fourth arg) in the echo area.
790 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
791 (or exit-char (setq exit-char ?\ ))
792 (let ((buffer-read-only nil)
793 ;; Don't modify the undo list at all.
794 (buffer-undo-list t)
795 (modified (buffer-modified-p))
796 (name buffer-file-name)
797 insert-end)
798 (unwind-protect
799 (progn
800 (save-excursion
801 (goto-char pos)
802 ;; defeat file locking... don't try this at home, kids!
803 (setq buffer-file-name nil)
804 (insert-before-markers string)
805 (setq insert-end (point))
806 ;; If the message end is off screen, recenter now.
807 (if (> (window-end) insert-end)
808 (recenter (/ (window-height) 2)))
809 ;; If that pushed message start off the screen,
810 ;; scroll to start it at the top of the screen.
811 (move-to-window-line 0)
812 (if (> (point) pos)
813 (progn
814 (goto-char pos)
815 (recenter 0))))
816 (message (or message "Type %s to continue editing.")
817 (single-key-description exit-char))
818 (let ((char (read-event)))
819 (or (eq char exit-char)
820 (setq unread-command-events (list char)))))
821 (if insert-end
822 (save-excursion
823 (delete-region pos insert-end)))
824 (setq buffer-file-name name)
825 (set-buffer-modified-p modified))))
826
827 \f
828 ;;;; Miscellanea.
829
830 ;; A number of major modes set this locally.
831 ;; Give it a global value to avoid compiler warnings.
832 (defvar font-lock-defaults nil)
833
834 ;; Avoid compiler warnings about this variable,
835 ;; which has a special meaning on certain system types.
836 (defvar buffer-file-type nil
837 "Non-nil if the visited file is a binary file.
838 This variable is meaningful on MS-DOG and Windows NT.
839 On those systems, it is automatically local in every buffer.
840 On other systems, this variable is normally always nil.")
841
842 (defun ignore (&rest ignore)
843 "Do nothing and return nil.
844 This function accepts any number of arguments, but ignores them."
845 (interactive)
846 nil)
847
848 (defun error (&rest args)
849 "Signal an error, making error message by passing all args to `format'."
850 (while t
851 (signal 'error (list (apply 'format args)))))
852
853 (defalias 'user-original-login-name 'user-login-name)
854
855 (defun start-process-shell-command (name buffer &rest args)
856 "Start a program in a subprocess. Return the process object for it.
857 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
858 NAME is name for process. It is modified if necessary to make it unique.
859 BUFFER is the buffer or (buffer-name) to associate with the process.
860 Process output goes at end of that buffer, unless you specify
861 an output stream or filter function to handle the output.
862 BUFFER may be also nil, meaning that this process is not associated
863 with any buffer
864 Third arg is command name, the name of a shell command.
865 Remaining arguments are the arguments for the command.
866 Wildcards and redirection are handled as usual in the shell."
867 (cond
868 ((eq system-type 'vax-vms)
869 (apply 'start-process name buffer args))
870 ;; We used to use `exec' to replace the shell with the command,
871 ;; but that failed to handle (...) and semicolon, etc.
872 (t
873 (start-process name buffer shell-file-name shell-command-switch
874 (mapconcat 'identity args " ")))))
875
876 (defmacro save-match-data (&rest body)
877 "Execute the BODY forms, restoring the global value of the match data."
878 (let ((original (make-symbol "match-data")))
879 (list
880 'let (list (list original '(match-data)))
881 (list 'unwind-protect
882 (cons 'progn body)
883 (list 'store-match-data original)))))
884
885 (defun shell-quote-argument (argument)
886 "Quote an argument for passing as argument to an inferior shell."
887 ;; Quote everything except POSIX filename characters.
888 ;; This should be safe enough even for really weird shells.
889 (let ((result "") (start 0) end)
890 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
891 (setq end (match-beginning 0)
892 result (concat result (substring argument start end)
893 "\\" (substring argument end (1+ end)))
894 start (1+ end)))
895 (concat result (substring argument start))))
896
897 (defun make-syntax-table (&optional oldtable)
898 "Return a new syntax table.
899 It inherits all letters and control characters from the standard
900 syntax table; other characters are copied from the standard syntax table."
901 (if oldtable
902 (copy-syntax-table oldtable)
903 (let ((table (copy-syntax-table))
904 i)
905 (setq i 0)
906 (while (<= i 31)
907 (aset table i 13)
908 (setq i (1+ i)))
909 (setq i ?A)
910 (while (<= i ?Z)
911 (aset table i 13)
912 (setq i (1+ i)))
913 (setq i ?a)
914 (while (<= i ?z)
915 (aset table i 13)
916 (setq i (1+ i)))
917 (setq i 128)
918 (while (<= i 255)
919 (aset table i 13)
920 (setq i (1+ i)))
921 table)))
922
923 ;; now in fns.c
924 ;(defun nth (n list)
925 ; "Returns the Nth element of LIST.
926 ;N counts from zero. If LIST is not that long, nil is returned."
927 ; (car (nthcdr n list)))
928 ;
929 ;(defun copy-alist (alist)
930 ; "Return a copy of ALIST.
931 ;This is a new alist which represents the same mapping
932 ;from objects to objects, but does not share the alist structure with ALIST.
933 ;The objects mapped (cars and cdrs of elements of the alist)
934 ;are shared, however."
935 ; (setq alist (copy-sequence alist))
936 ; (let ((tail alist))
937 ; (while tail
938 ; (if (consp (car tail))
939 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
940 ; (setq tail (cdr tail))))
941 ; alist)
942
943 ;;; subr.el ends here
944