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