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)
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.
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)))
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
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
)))
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))
88 (let* ((walk-windows-start (selected-window))
89 (walk-windows-current walk-windows-start
))
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
)))))
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
)
127 (define-key map
"-" 'negative-argument
)
128 ;; Make plain numbers do numeric args.
131 (define-key map
(char-to-string loop
) 'digit-argument
)
132 (setq loop
(1+ loop
))))))
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
))
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.
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.
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
)
179 (setq inner-def
(symbol-function inner-def
)))
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.
188 key-substitution-in-progress
)))
189 ;; If this one isn't being scanned already,
191 (substitute-key-definition olddef newdef keymap
194 (if (arrayp (car scan
))
195 (let* ((array (car scan
))
199 (let ((char i
) (defn (aref array i
)))
200 ;; The inside of this let duplicates exactly
201 ;; the inside of the previous let.
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
)
214 (setq inner-def
(symbol-function inner-def
)))
216 (define-key keymap prefix1
217 (nconc (nreverse skipped
) newdef
))
218 (if (and (keymapp defn
)
219 (keymapp (lookup-key keymap prefix1
))
221 key-substitution-in-progress
)))
222 (substitute-key-definition olddef newdef keymap
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
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."
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
)
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
)
258 ;; Don't insert more than once.
260 (setcdr tail
(cons (cons (aref key
0) definition
) (cdr tail
))))
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
)))
273 (let* ((i (length keyboard-translate-table
))
274 (table (concat keyboard-translate-table
275 (make-string (- 256 i
) 0))))
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
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 ;; 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
319 (setq listify-key-sequence-1
(logior (lsh 1 27) listify-key-sequence-1
))
321 (defun listify-key-sequence (key)
322 "Convert a key sequence to a list of events."
325 (mapcar (function (lambda (c)
327 (logxor c listify-key-sequence-1
)
331 (defsubst eventp
(obj)
332 "True if the argument is an event object."
335 (get obj
'event-symbol-elements
))
338 (get (car obj
) 'event-symbol-elements
))))
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',
347 (setq type
(car type
)))
349 (cdr (get type
'event-symbol-elements
))
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
)))
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."
371 (setq event
(car 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
)))))
377 (defsubst mouse-movement-p
(object)
378 "Return non-nil if OBJECT is a mouse movement event."
380 (eq (car object
) 'mouse-movement
)))
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
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."
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
))
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))
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."
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
))
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."
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
))
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
))
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
))))
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."
460 ;;;; Obsolescent names for functions.
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
)
476 ;; Some programs still use this as a function.
478 "Obsolete function returning the value of the `baud-rate' variable.
479 Please convert your programs to use the variable `baud-rate' directly."
483 ;;;; Alternate names for functions - these are not being phased out.
485 (defalias 'string
= 'string-equal
)
486 (defalias 'string
< 'string-lessp
)
487 (defalias 'move-marker
'set-marker
)
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
)
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
)
504 ;;;; Hook manipulation functions.
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.
515 To make a hook variable buffer-local, use `make-local-hook', not
516 `make-local-variable'."
518 (let ((sym (car hooklist
)))
521 (let ((value (symbol-value sym
)))
522 (if (and (listp value
) (not (eq (car value
) 'lambda
)))
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
)))
529 (funcall (car functions
))
530 (setq functions
(cdr functions
))))
531 (funcall (car value
)))
532 (setq value
(cdr value
)))
534 (setq hooklist
(cdr hooklist
))))
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',
547 To make a hook variable buffer-local, use `make-local-hook', not
548 `make-local-variable'."
551 (let ((value (symbol-value hook
)))
552 (if (and (listp value
) (not (eq (car value
) 'lambda
)))
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
)))
559 (apply (car functions
) args
)
560 (setq functions
(cdr functions
))))
561 (apply (car value
) args
))
562 (setq value
(cdr value
)))
563 (apply value args
)))))
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.
573 To make a hook variable buffer-local, use `make-local-hook', not
574 `make-local-variable'."
577 (let ((value (symbol-value hook
))
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
)))
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.
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
))
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
)))
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'.
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.
630 This function does nothing if HOOK is already local in the current buffer.
632 Do not use `make-local-variable' to make a hook variable buffer-local."
633 (if (local-variable-p hook
)
635 (or (boundp hook
) (set hook nil
))
636 (make-local-variable hook
)
637 (set hook
(list t
))))
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.
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'.
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
))))
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
)))
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
)))
681 (append (default-value hook
) (list function
))
682 (cons function
(default-value hook
)))))))
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'.
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
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
)))))
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
)))))
730 ;;;; Specifying things to do after certain files are loaded.
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
))))
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)))
751 ;;;; Input and display facilities.
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
)
760 (let ((inhibit-quit (zerop count
))
762 (and prompt
(message "%s-" prompt
))
763 (setq char
(read-char))
764 (if inhibit-quit
(setq quit-flag nil
)))
766 ((and (<= ?
0 char
) (<= char ?
7))
767 (setq code
(+ (* code
8) (- char ?
0))
769 (and prompt
(message (setq prompt
770 (format "%s %c" prompt char
)))))
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)
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)))
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.
795 (modified (buffer-modified-p))
796 (name buffer-file-name
)
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)
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
)))))
823 (delete-region pos insert-end
)))
824 (setq buffer-file-name name
)
825 (set-buffer-modified-p modified
))))
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
)
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.")
842 (defun ignore (&rest ignore
)
843 "Do nothing and return nil.
844 This function accepts any number of arguments, but ignores them."
848 (defun error (&rest args
)
849 "Signal an error, making error message by passing all args to `format'."
851 (signal 'error
(list (apply 'format args
)))))
853 (defalias 'user-original-login-name
'user-login-name
)
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
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."
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.
873 (start-process name buffer shell-file-name shell-command-switch
874 (mapconcat 'identity args
" ")))))
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")))
880 'let
(list (list original
'(match-data)))
881 (list 'unwind-protect
883 (list 'store-match-data original
)))))
885 (defun match-string (n &optional string
)
886 "Return the Nth subexpression matched by the last regexp search or match.
887 If the last search or match was done against a string,
888 specify that string as the second argument STRING."
890 (substring string
(match-beginning n
) (match-end n
))
891 (buffer-substring (match-beginning n
) (match-end n
))))
893 (defun shell-quote-argument (argument)
894 "Quote an argument for passing as argument to an inferior shell."
895 ;; Quote everything except POSIX filename characters.
896 ;; This should be safe enough even for really weird shells.
897 (let ((result "") (start 0) end
)
898 (while (string-match "[^-0-9a-zA-Z_./]" argument start
)
899 (setq end
(match-beginning 0)
900 result
(concat result
(substring argument start end
)
901 "\\" (substring argument end
(1+ end
)))
903 (concat result
(substring argument start
))))
905 (defun make-syntax-table (&optional oldtable
)
906 "Return a new syntax table.
907 It inherits all letters and control characters from the standard
908 syntax table; other characters are copied from the standard syntax table."
910 (copy-syntax-table oldtable
)
911 (let ((table (copy-syntax-table))
933 ; "Returns the Nth element of LIST.
934 ;N counts from zero. If LIST is not that long, nil is returned."
935 ; (car (nthcdr n list)))
937 ;(defun copy-alist (alist)
938 ; "Return a copy of ALIST.
939 ;This is a new alist which represents the same mapping
940 ;from objects to objects, but does not share the alist structure with ALIST.
941 ;The objects mapped (cars and cdrs of elements of the alist)
942 ;are shared, however."
943 ; (setq alist (copy-sequence alist))
944 ; (let ((tail alist))
946 ; (if (consp (car tail))
947 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
948 ; (setq tail (cdr tail))))
951 ;;; subr.el ends here