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