(Buffer-menu-save): Fix the test for header line.
[emacs.git] / lisp / subr.el
blob07bbdb45b5e78a7c2711814c9994070fb23a1be8
1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;;; Copyright (C) 1985, 1986, 1992, 1994 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
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;;; Code:
24 ;;;; Lisp language features.
26 (defmacro lambda (&rest cdr)
27 "Return a lambda expression.
28 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
29 self-quoting; the result of evaluating the lambda expression is the
30 expression itself. The lambda expression may then be treated as a
31 function, i.e., stored as the function value of a symbol, passed to
32 funcall or mapcar, etc.
34 ARGS should take the same form as an argument list for a `defun'.
35 DOCSTRING should be a string, as described for `defun'. It may be omitted.
36 INTERACTIVE should be a call to the function `interactive', which see.
37 It may also be omitted.
38 BODY should be a list of lisp expressions."
39 ;; Note that this definition should not use backquotes; subr.el should not
40 ;; depend on backquote.el.
41 (list 'function (cons 'lambda cdr)))
43 ;;(defmacro defun-inline (name args &rest body)
44 ;; "Create an \"inline defun\" (actually a macro).
45 ;;Use just like `defun'."
46 ;; (nconc (list 'defmacro name '(&rest args))
47 ;; (if (stringp (car body))
48 ;; (prog1 (list (car body))
49 ;; (setq body (or (cdr body) body))))
50 ;; (list (list 'cons (list 'quote
51 ;; (cons 'lambda (cons args body)))
52 ;; 'args))))
55 ;;;; Window tree functions.
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.
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))))
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.
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))))))
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)))))
110 ;;;; Keymap support.
112 (defun undefined ()
113 (interactive)
114 (ding))
116 ;Prevent the \{...} documentation construct
117 ;from mentioning keys that run this command.
118 (put 'undefined 'suppress-keymap t)
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))))))
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)))
143 (defvar key-substitution-in-progress nil
144 "Used internally by substitute-key-definition.")
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)))))
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)))))
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))
283 ;;;; The global keymap tree.
285 ;;; global-map, esc-map, and ctl-x-map have their values set up in
286 ;;; keymap.c; we just give them docstrings here.
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.")
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.")
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.")
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)
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)
312 ;;;; Event manipulation functions.
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-\\^@")))
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))))
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))))
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))))
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)))))
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)))
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))
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))
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))
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))
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)))
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))
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))))))
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))
458 ;;;; Obsolescent names for functions.
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)
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)
481 ;;;; Alternate names for functions - these are not being phased out.
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)
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)
502 ;;;; Hook manipulation functions.
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.
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))))
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.
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)))))
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.
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)))
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.
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)))
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.")
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.
628 This function does nothing if HOOK is already local in the current buffer.
630 Do not use `make-local-variable' to make a hook variable buffer-local."
631 (if (local-variable-p hook)
633 (or (boundp hook) (set hook nil))
634 (make-local-variable hook)
635 (set hook (list t))))
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.
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'.
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)))))))
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'.
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)))))
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)))))
728 ;;;; Specifying things to do after certain files are loaded.
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 It does nothing if FORM is already on the list for FILE.
734 FILE should be the name of a library, with no directory name."
735 (or (assoc file after-load-alist)
736 (setq after-load-alist (cons (list file) after-load-alist)))
737 (let ((elt (assoc file after-load-alist)))
738 (or (member form (cdr elt))
739 (nconc elt (list form))))
740 form)
742 (defun eval-next-after-load (file)
743 "Read the following input sexp, and run it whenever FILE is loaded.
744 This makes or adds to an entry on `after-load-alist'.
745 FILE should be the name of a library, with no directory name."
746 (eval-after-load file (read)))
749 ;;;; Input and display facilities.
751 (defun read-quoted-char (&optional prompt)
752 "Like `read-char', except that if the first character read is an octal
753 digit, we read up to two more octal digits and return the character
754 represented by the octal number consisting of those digits.
755 Optional argument PROMPT specifies a string to use to prompt the user."
756 (let ((message-log-max nil) (count 0) (code 0) char)
757 (while (< count 3)
758 (let ((inhibit-quit (zerop count))
759 (help-form nil))
760 (and prompt (message "%s-" prompt))
761 (setq char (read-char))
762 (if inhibit-quit (setq quit-flag nil)))
763 (cond ((null char))
764 ((and (<= ?0 char) (<= char ?7))
765 (setq code (+ (* code 8) (- char ?0))
766 count (1+ count))
767 (and prompt (message (setq prompt
768 (format "%s %c" prompt char)))))
769 ((> count 0)
770 (setq unread-command-events (list char) count 259))
771 (t (setq code char count 259))))
772 ;; Turn a meta-character into a character with the 0200 bit set.
773 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
774 (logand 255 code))))
776 (defun force-mode-line-update (&optional all)
777 "Force the mode-line of the current buffer to be redisplayed.
778 With optional non-nil ALL, force redisplay of all mode-lines."
779 (if all (save-excursion (set-buffer (other-buffer))))
780 (set-buffer-modified-p (buffer-modified-p)))
782 (defun momentary-string-display (string pos &optional exit-char message)
783 "Momentarily display STRING in the buffer at POS.
784 Display remains until next character is typed.
785 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
786 otherwise it is then available as input (as a command if nothing else).
787 Display MESSAGE (optional fourth arg) in the echo area.
788 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
789 (or exit-char (setq exit-char ?\ ))
790 (let ((buffer-read-only nil)
791 ;; Don't modify the undo list at all.
792 (buffer-undo-list t)
793 (modified (buffer-modified-p))
794 (name buffer-file-name)
795 insert-end)
796 (unwind-protect
797 (progn
798 (save-excursion
799 (goto-char pos)
800 ;; defeat file locking... don't try this at home, kids!
801 (setq buffer-file-name nil)
802 (insert-before-markers string)
803 (setq insert-end (point))
804 ;; If the message end is off screen, recenter now.
805 (if (> (window-end) insert-end)
806 (recenter (/ (window-height) 2)))
807 ;; If that pushed message start off the screen,
808 ;; scroll to start it at the top of the screen.
809 (move-to-window-line 0)
810 (if (> (point) pos)
811 (progn
812 (goto-char pos)
813 (recenter 0))))
814 (message (or message "Type %s to continue editing.")
815 (single-key-description exit-char))
816 (let ((char (read-event)))
817 (or (eq char exit-char)
818 (setq unread-command-events (list char)))))
819 (if insert-end
820 (save-excursion
821 (delete-region pos insert-end)))
822 (setq buffer-file-name name)
823 (set-buffer-modified-p modified))))
826 ;;;; Miscellanea.
828 ;; A number of major modes set this locally.
829 ;; Give it a global value to avoid compiler warnings.
830 (defvar font-lock-defaults nil)
832 ;; Avoid compiler warnings about this variable,
833 ;; which has a special meaning on certain system types.
834 (defvar buffer-file-type nil
835 "Non-nil if the visited file is a binary file.
836 This variable is meaningful on MS-DOG and Windows NT.
837 On those systems, it is automatically local in every buffer.
838 On other systems, this variable is normally always nil.")
840 (defun ignore (&rest ignore)
841 "Do nothing and return nil.
842 This function accepts any number of arguments, but ignores them."
843 (interactive)
844 nil)
846 (defun error (&rest args)
847 "Signal an error, making error message by passing all args to `format'."
848 (while t
849 (signal 'error (list (apply 'format args)))))
851 (defalias 'user-original-login-name 'user-login-name)
853 (defun start-process-shell-command (name buffer &rest args)
854 "Start a program in a subprocess. Return the process object for it.
855 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
856 NAME is name for process. It is modified if necessary to make it unique.
857 BUFFER is the buffer or (buffer-name) to associate with the process.
858 Process output goes at end of that buffer, unless you specify
859 an output stream or filter function to handle the output.
860 BUFFER may be also nil, meaning that this process is not associated
861 with any buffer
862 Third arg is command name, the name of a shell command.
863 Remaining arguments are the arguments for the command.
864 Wildcards and redirection are handled as usual in the shell."
865 (cond
866 ((eq system-type 'vax-vms)
867 (apply 'start-process name buffer args))
868 ;; We used to use `exec' to replace the shell with the command,
869 ;; but that failed to handle (...) and semicolon, etc.
871 (start-process name buffer shell-file-name shell-command-switch
872 (mapconcat 'identity args " ")))))
874 (defmacro save-match-data (&rest body)
875 "Execute the BODY forms, restoring the global value of the match data."
876 (let ((original (make-symbol "match-data")))
877 (list
878 'let (list (list original '(match-data)))
879 (list 'unwind-protect
880 (cons 'progn body)
881 (list 'store-match-data original)))))
883 (defun match-string (n &optional string)
884 "Return the Nth subexpression matched by the last regexp search or match.
885 If the last search or match was done against a string,
886 specify that string as the second argument STRING."
887 (if string
888 (substring string (match-beginning n) (match-end n))
889 (buffer-substring (match-beginning n) (match-end n))))
891 (defun shell-quote-argument (argument)
892 "Quote an argument for passing as argument to an inferior shell."
893 ;; Quote everything except POSIX filename characters.
894 ;; This should be safe enough even for really weird shells.
895 (let ((result "") (start 0) end)
896 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
897 (setq end (match-beginning 0)
898 result (concat result (substring argument start end)
899 "\\" (substring argument end (1+ end)))
900 start (1+ end)))
901 (concat result (substring argument start))))
903 (defun make-syntax-table (&optional oldtable)
904 "Return a new syntax table.
905 It inherits all letters and control characters from the standard
906 syntax table; other characters are copied from the standard syntax table."
907 (if oldtable
908 (copy-syntax-table oldtable)
909 (let ((table (copy-syntax-table))
911 (setq i 0)
912 (while (<= i 31)
913 (aset table i 13)
914 (setq i (1+ i)))
915 (setq i ?A)
916 (while (<= i ?Z)
917 (aset table i 13)
918 (setq i (1+ i)))
919 (setq i ?a)
920 (while (<= i ?z)
921 (aset table i 13)
922 (setq i (1+ i)))
923 (setq i 128)
924 (while (<= i 255)
925 (aset table i 13)
926 (setq i (1+ i)))
927 table)))
929 ;; now in fns.c
930 ;(defun nth (n list)
931 ; "Returns the Nth element of LIST.
932 ;N counts from zero. If LIST is not that long, nil is returned."
933 ; (car (nthcdr n list)))
935 ;(defun copy-alist (alist)
936 ; "Return a copy of ALIST.
937 ;This is a new alist which represents the same mapping
938 ;from objects to objects, but does not share the alist structure with ALIST.
939 ;The objects mapped (cars and cdrs of elements of the alist)
940 ;are shared, however."
941 ; (setq alist (copy-sequence alist))
942 ; (let ((tail alist))
943 ; (while tail
944 ; (if (consp (car tail))
945 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
946 ; (setq tail (cdr tail))))
947 ; alist)
949 ;;; subr.el ends here