(kbd_buffer_get_event, swallow_events): Fix prev change.
[bpt/emacs.git] / lisp / subr.el
CommitLineData
c88ab9ce 1;;; subr.el --- basic lisp subroutines for Emacs
630cc463 2
cd323f89 3;;; Copyright (C) 1985, 1986, 1992, 1994, 1995 Free Software Foundation, Inc.
be9b65ac
DL
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
492878e4 9;; the Free Software Foundation; either version 2, or (at your option)
be9b65ac
DL
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
630cc463 21;;; Code:
be9b65ac 22
9a5336ae
JB
23\f
24;;;; Lisp language features.
25
26(defmacro lambda (&rest cdr)
27 "Return a lambda expression.
28A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
29self-quoting; the result of evaluating the lambda expression is the
30expression itself. The lambda expression may then be treated as a
bec0d7f9
RS
31function, i.e., stored as the function value of a symbol, passed to
32funcall or mapcar, etc.
33
9a5336ae 34ARGS should take the same form as an argument list for a `defun'.
8fd68088
RS
35DOCSTRING 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.
9a5336ae
JB
38INTERACTIVE should be a call to the function `interactive', which see.
39It may also be omitted.
40BODY 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)))
44
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))))
55
56\f
9a5336ae 57;;;; Keymap support.
be9b65ac
DL
58
59(defun undefined ()
60 (interactive)
61 (ding))
62
63;Prevent the \{...} documentation construct
64;from mentioning keys that run this command.
65(put 'undefined 'suppress-keymap t)
66
67(defun suppress-keymap (map &optional nodigits)
68 "Make MAP override all normally self-inserting keys to be undefined.
69Normally, as an exception, digits and minus-sign are set to make prefix args,
70but optional second arg NODIGITS non-nil treats them like other chars."
80e7b471 71 (substitute-key-definition 'self-insert-command 'undefined map global-map)
be9b65ac
DL
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))))))
80
be9b65ac
DL
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)))
89
f14dbba7
KH
90(defvar key-substitution-in-progress nil
91 "Used internally by substitute-key-definition.")
92
7f2c2edd 93(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
94 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
95In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
96If optional fourth argument OLDMAP is specified, we redefine
97in 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))
f14dbba7
KH
101 (prefix1 (vconcat prefix vec1))
102 (key-substitution-in-progress
103 (cons scan key-substitution-in-progress)))
7f2c2edd
RS
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)
44d798af 114 (let (inner-def skipped)
7f2c2edd
RS
115 ;; Skip past menu-prompt.
116 (while (stringp (car-safe defn))
44d798af 117 (setq skipped (cons (car defn) skipped))
7f2c2edd 118 (setq defn (cdr defn)))
e025dddf
RS
119 ;; Skip past cached key-equivalence data for menu items.
120 (and (consp defn) (consp (car defn))
121 (setq defn (cdr defn)))
7f2c2edd 122 (setq inner-def defn)
e025dddf 123 ;; Look past a symbol that names a keymap.
7f2c2edd
RS
124 (while (and (symbolp inner-def)
125 (fboundp inner-def))
126 (setq inner-def (symbol-function inner-def)))
127 (if (eq defn olddef)
44d798af 128 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
f14dbba7 129 (if (and (keymapp defn)
350b7567
RS
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.
f14dbba7
KH
134 (not (memq inner-def
135 key-substitution-in-progress)))
e025dddf
RS
136 ;; If this one isn't being scanned already,
137 ;; scan it now.
7f2c2edd
RS
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)
44d798af 151 (let (inner-def skipped)
7f2c2edd
RS
152 ;; Skip past menu-prompt.
153 (while (stringp (car-safe defn))
44d798af 154 (setq skipped (cons (car defn) skipped))
7f2c2edd 155 (setq defn (cdr defn)))
e025dddf
RS
156 (and (consp defn) (consp (car defn))
157 (setq defn (cdr defn)))
7f2c2edd
RS
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)
44d798af
RS
163 (define-key keymap prefix1
164 (nconc (nreverse skipped) newdef))
f14dbba7 165 (if (and (keymapp defn)
350b7567 166 (keymapp (lookup-key keymap prefix1))
f14dbba7
KH
167 (not (memq inner-def
168 key-substitution-in-progress)))
7f2c2edd
RS
169 (substitute-key-definition olddef newdef keymap
170 inner-def
171 prefix1)))))
172 (setq i (1+ i))))))
173 (setq scan (cdr scan)))))
9a5336ae 174
06ae9cf2 175(defun define-key-after (keymap key definition after)
4434d61b
RS
176 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
177This is like `define-key' except that the binding for KEY is placed
178just after the binding for the event AFTER, instead of at the beginning
179of the map.
626f67f3 180The order matters when the keymap is used as a menu.
9ed287b0
RS
181KEY must contain just one event type--that is to say, it must be
182a string or vector of length 1."
4434d61b
RS
183 (or (keymapp keymap)
184 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 185 (if (> (length key) 1)
626f67f3 186 (error "multi-event key specified in `define-key-after'"))
113d28a8 187 (let ((tail keymap) done inserted
4434d61b
RS
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.
113d28a8 195 ;; If we reach the end of this keymap, insert at the end.
4434d61b 196 (if (or (eq (car-safe (car tail)) after)
113d28a8
RS
197 (eq (car (cdr tail)) 'keymap)
198 (null (cdr tail)))
4434d61b 199 (progn
113d28a8
RS
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)))
4434d61b
RS
209 (setq tail (cdr tail)))))
210
9a5336ae
JB
211(defun keyboard-translate (from to)
212 "Translate character FROM to TO at a low level.
213This function creates a `keyboard-translate-table' if necessary
214and 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))
f4ebbdbe
RS
221 (table (concat keyboard-translate-table
222 (make-string (- 256 i) 0))))
9a5336ae
JB
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))
228
229\f
230;;;; The global keymap tree.
231
232;;; global-map, esc-map, and ctl-x-map have their values set up in
233;;; keymap.c; we just give them docstrings here.
234
235(defvar global-map nil
236 "Default global keymap mapping Emacs keyboard input into commands.
237The value is a keymap which is usually (but not necessarily) Emacs's
238global map.")
239
240(defvar esc-map nil
241 "Default keymap for ESC (meta) commands.
242The normal global definition of the character ESC indirects to this keymap.")
243
244(defvar ctl-x-map nil
245 "Default keymap for C-x commands.
246The normal global definition of the character C-x indirects to this keymap.")
247
248(defvar ctl-x-4-map (make-sparse-keymap)
249 "Keymap for subcommands of C-x 4")
059184dd 250(defalias 'ctl-x-4-prefix ctl-x-4-map)
9a5336ae
JB
251(define-key ctl-x-map "4" 'ctl-x-4-prefix)
252
253(defvar ctl-x-5-map (make-sparse-keymap)
254 "Keymap for frame commands.")
059184dd 255(defalias 'ctl-x-5-prefix ctl-x-5-map)
9a5336ae
JB
256(define-key ctl-x-map "5" 'ctl-x-5-prefix)
257
0f03054a 258\f
9a5336ae
JB
259;;;; Event manipulation functions.
260
da16e648
KH
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-\\^@")))
114137b8 265
cde6d7e3
RS
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)
114137b8 272 (logxor c listify-key-sequence-1)
cde6d7e3
RS
273 c)))
274 (append key nil))))
275
53e5a4e8
RS
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))))
284
285(defun event-modifiers (event)
286 "Returns a list of symbols representing the modifier keys in event EVENT.
287The elements of the list may include `meta', `control',
32295976
RS
288`shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
289and `down'."
53e5a4e8
RS
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))
da16e648 296 (or (zerop (logand type ?\M-\^@))
53e5a4e8 297 (setq list (cons 'meta list)))
da16e648 298 (or (and (zerop (logand type ?\C-\^@))
53e5a4e8
RS
299 (>= (logand type 127) 32))
300 (setq list (cons 'control list)))
da16e648 301 (or (and (zerop (logand type ?\S-\^@))
53e5a4e8
RS
302 (= (logand type 255) (downcase (logand type 255))))
303 (setq list (cons 'shift list)))
da16e648 304 (or (zerop (logand type ?\H-\^@))
53e5a4e8 305 (setq list (cons 'hyper list)))
da16e648 306 (or (zerop (logand type ?\s-\^@))
53e5a4e8 307 (setq list (cons 'super list)))
da16e648 308 (or (zerop (logand type ?\A-\^@))
53e5a4e8
RS
309 (setq list (cons 'alt list)))
310 list))))
311
d63de416
RS
312(defun event-basic-type (event)
313 "Returns the basic type of the given event (all modifiers removed).
314The value is an ASCII printing character (not upper case) or a symbol."
2b0f4ba5
JB
315 (if (consp event)
316 (setq event (car event)))
d63de416
RS
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)))))
321
0f03054a
RS
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)))
326
327(defsubst event-start (event)
328 "Return the starting position of EVENT.
329If EVENT is a mouse press or a mouse click, this returns the location
330of the event.
331If EVENT is a drag, this returns the drag's starting position.
332The return value is of the form
e55c21be 333 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
334The `posn-' functions access elements of such lists."
335 (nth 1 event))
336
337(defsubst event-end (event)
338 "Return the ending location of EVENT. EVENT should be a click or drag event.
339If EVENT is a click event, this function is the same as `event-start'.
340The return value is of the form
e55c21be 341 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 342The `posn-' functions access elements of such lists."
69b95560 343 (nth (if (consp (nth 2 event)) 2 1) event))
0f03054a 344
32295976
RS
345(defsubst event-click-count (event)
346 "Return the multi-click count of EVENT, a click or drag event.
347The return value is a positive integer."
348 (if (integerp (nth 2 event)) (nth 2 event) 1))
349
0f03054a
RS
350(defsubst posn-window (position)
351 "Return the window in POSITION.
352POSITION should be a list of the form
e55c21be 353 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
354as returned by the `event-start' and `event-end' functions."
355 (nth 0 position))
356
357(defsubst posn-point (position)
358 "Return the buffer location in POSITION.
359POSITION should be a list of the form
e55c21be 360 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 361as returned by the `event-start' and `event-end' functions."
15db4e0e
JB
362 (if (consp (nth 1 position))
363 (car (nth 1 position))
364 (nth 1 position)))
0f03054a 365
e55c21be
RS
366(defsubst posn-x-y (position)
367 "Return the x and y coordinates in POSITION.
0f03054a 368POSITION should be a list of the form
e55c21be 369 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
370as returned by the `event-start' and `event-end' functions."
371 (nth 2 position))
372
ed627e08 373(defun posn-col-row (position)
dbbcac56 374 "Return the column and row in POSITION, measured in characters.
e55c21be
RS
375POSITION should be a list of the form
376 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
ed627e08
RS
377as returned by the `event-start' and `event-end' functions.
378For a scroll-bar event, the result column is 0, and the row
379corresponds to the vertical position of the click in the scroll bar."
380 (let ((pair (nth 2 position))
381 (window (posn-window position)))
dbbcac56
KH
382 (if (eq (if (consp (nth 1 position))
383 (car (nth 1 position))
384 (nth 1 position))
ed627e08
RS
385 'vertical-scroll-bar)
386 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
dbbcac56
KH
387 (if (eq (if (consp (nth 1 position))
388 (car (nth 1 position))
389 (nth 1 position))
ed627e08
RS
390 'horizontal-scroll-bar)
391 (cons (scroll-bar-scale pair (window-width window)) 0)
9ba60df9
RS
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))))
ed627e08 395 (cons x y))))))
e55c21be 396
0f03054a
RS
397(defsubst posn-timestamp (position)
398 "Return the timestamp of POSITION.
399POSITION should be a list of the form
e55c21be 400 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
f415c00c 401as returned by the `event-start' and `event-end' functions."
0f03054a 402 (nth 3 position))
9a5336ae 403
0f03054a 404\f
9a5336ae
JB
405;;;; Obsolescent names for functions.
406
059184dd
ER
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)
be9b65ac 420
9a5336ae
JB
421;; Some programs still use this as a function.
422(defun baud-rate ()
bcacc42c
RS
423 "Obsolete function returning the value of the `baud-rate' variable.
424Please convert your programs to use the variable `baud-rate' directly."
9a5336ae
JB
425 baud-rate)
426
427\f
428;;;; Alternate names for functions - these are not being phased out.
429
059184dd
ER
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)
eb8c3be9 437(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
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)
1e0a78a1 443(defalias 'set-match-data 'store-match-data)
37f6661a
JB
444
445;;; Should this be an obsolete name? If you decide it should, you get
446;;; to go through all the sources and change them.
059184dd 447(defalias 'string-to-int 'string-to-number)
be9b65ac 448\f
9a5336ae 449;;;; Hook manipulation functions.
be9b65ac 450
be9b65ac
DL
451(defun run-hooks (&rest hooklist)
452 "Takes hook names and runs each one in turn. Major mode functions use this.
453Each argument should be a symbol, a hook variable.
454These symbols are processed in the order specified.
455If a hook symbol has a non-nil value, that value may be a function
456or a list of functions to be called to run the hook.
457If the value is a function, it is called with no arguments.
0e4d378b
RS
458If it is a list, the elements are called, in order, with no arguments.
459
460To make a hook variable buffer-local, use `make-local-hook', not
461`make-local-variable'."
be9b65ac
DL
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)))
0e4d378b
RS
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)))
be9b65ac
DL
478 (funcall value)))))
479 (setq hooklist (cdr hooklist))))
480
0e4d378b
RS
481(defun run-hook-with-args-until-success (hook &rest args)
482 "Run HOOK with the specified arguments ARGS.
483HOOK should be a symbol, a hook variable. Its value should
484be a list of functions. We call those functions, one by one,
485passing arguments ARGS to each of them, until one of them
486returns a non-nil value. Then we return that value.
487If all the functions return nil, we return nil.
488
489To make a hook variable buffer-local, use `make-local-hook', not
490`make-local-variable'."
491 (and (boundp hook)
492 (symbol-value hook)
493 (let ((value (symbol-value hook))
494 success)
495 (while (and value (not success))
496 (if (eq (car value) t)
497 ;; t indicates this hook has a local binding;
498 ;; it means to run the global binding too.
499 (let ((functions (default-value hook)))
500 (while (and functions (not success))
501 (setq success (apply (car functions) args))
502 (setq functions (cdr functions))))
503 (setq success (apply (car value) args)))
504 (setq value (cdr value)))
505 success)))
506
507(defun run-hook-with-args-until-failure (hook &rest args)
508 "Run HOOK with the specified arguments ARGS.
509HOOK should be a symbol, a hook variable. Its value should
510be a list of functions. We call those functions, one by one,
511passing arguments ARGS to each of them, until one of them
512returns nil. Then we return nil.
513If all the functions return non-nil, we return non-nil.
514
515To make a hook variable buffer-local, use `make-local-hook', not
516`make-local-variable'."
3d1743f7
RS
517 ;; We must return non-nil if there are no hook functions!
518 (or (not (boundp hook))
519 (not (symbol-value hook))
520 (let ((value (symbol-value hook))
521 (success t))
522 (while (and value 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 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)))
0e4d378b 533
be9b65ac
DL
534;; Tell C code how to call this function.
535(defconst run-hooks 'run-hooks
536 "Variable by which C primitives find the function `run-hooks'.
537Don't change it.")
538
0e4d378b
RS
539(defun make-local-hook (hook)
540 "Make the hook HOOK local to the current buffer.
541When a hook is local, its local and global values
542work in concert: running the hook actually runs all the hook
543functions listed in *either* the local value *or* the global value
544of the hook variable.
545
7dd1926e
RS
546This function works by making `t' a member of the buffer-local value,
547which acts as a flag to run the hook functions in the default value as
548well. This works for all normal hooks, but does not work for most
549non-normal hooks yet. We will be changing the callers of non-normal
550hooks so that they can handle localness; this has to be done one by
551one.
552
553This function does nothing if HOOK is already local in the current
554buffer.
0e4d378b
RS
555
556Do not use `make-local-variable' to make a hook variable buffer-local."
557 (if (local-variable-p hook)
558 nil
559 (or (boundp hook) (set hook nil))
560 (make-local-variable hook)
561 (set hook (list t))))
562
563(defun add-hook (hook function &optional append local)
32295976
RS
564 "Add to the value of HOOK the function FUNCTION.
565FUNCTION is not added if already present.
566FUNCTION is added (if necessary) at the beginning of the hook list
567unless the optional argument APPEND is non-nil, in which case
568FUNCTION is added at the end.
569
0e4d378b
RS
570The optional fourth argument, LOCAL, if non-nil, says to modify
571the hook's buffer-local value rather than its default value.
572This makes no difference if the hook is not buffer-local.
573To make a hook variable buffer-local, always use
574`make-local-hook', not `make-local-variable'.
575
32295976
RS
576HOOK should be a symbol, and FUNCTION may be any valid function. If
577HOOK is void, it is first set to nil. If HOOK's value is a single
aa09b5ca 578function, it is changed to a list of functions."
be9b65ac 579 (or (boundp hook) (set hook nil))
0e4d378b 580 (or (default-boundp hook) (set-default hook nil))
32295976
RS
581 ;; If the hook value is a single function, turn it into a list.
582 (let ((old (symbol-value hook)))
583 (if (or (not (listp old)) (eq (car old) 'lambda))
584 (set hook (list old))))
f4e5bca5
RS
585 (if (or local
586 ;; Detect the case where make-local-variable was used on a hook
587 ;; and do what we used to do.
cd2db344 588 (and (local-variable-if-set-p hook)
f4e5bca5 589 (not (memq t (symbol-value hook)))))
0e4d378b
RS
590 ;; Alter the local value only.
591 (or (if (consp function)
592 (member function (symbol-value hook))
593 (memq function (symbol-value hook)))
594 (set hook
595 (if append
596 (append (symbol-value hook) (list function))
597 (cons function (symbol-value hook)))))
598 ;; Alter the global value (which is also the only value,
599 ;; if the hook doesn't have a local value).
600 (or (if (consp function)
601 (member function (default-value hook))
602 (memq function (default-value hook)))
603 (set-default hook
604 (if append
605 (append (default-value hook) (list function))
606 (cons function (default-value hook)))))))
607
608(defun remove-hook (hook function &optional local)
24980d16
RS
609 "Remove from the value of HOOK the function FUNCTION.
610HOOK should be a symbol, and FUNCTION may be any valid function. If
611FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
0e4d378b
RS
612list of hooks to run in HOOK, then nothing is done. See `add-hook'.
613
614The optional third argument, LOCAL, if non-nil, says to modify
615the hook's buffer-local value rather than its default value.
616This makes no difference if the hook is not buffer-local.
617To make a hook variable buffer-local, always use
618`make-local-hook', not `make-local-variable'."
24980d16 619 (if (or (not (boundp hook)) ;unbound symbol, or
0e4d378b 620 (not (default-boundp 'hook))
24980d16
RS
621 (null (symbol-value hook)) ;value is nil, or
622 (null function)) ;function is nil, then
623 nil ;Do nothing.
f4e5bca5
RS
624 (if (or local
625 ;; Detect the case where make-local-variable was used on a hook
626 ;; and do what we used to do.
627 (and (local-variable-p hook)
628 (not (memq t (symbol-value hook)))))
0e4d378b
RS
629 (let ((hook-value (symbol-value hook)))
630 (if (consp hook-value)
631 (if (member function hook-value)
632 (setq hook-value (delete function (copy-sequence hook-value))))
633 (if (equal hook-value function)
634 (setq hook-value nil)))
635 (set hook hook-value))
636 (let ((hook-value (default-value hook)))
637 (if (consp hook-value)
638 (if (member function hook-value)
639 (setq hook-value (delete function (copy-sequence hook-value))))
640 (if (equal hook-value function)
641 (setq hook-value nil)))
642 (set-default hook hook-value)))))
6e3af630
RS
643
644(defun add-to-list (list-var element)
8851c1f0
RS
645 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
646If you want to use `add-to-list' on a variable that is not defined
647until a certain package is loaded, you should put the call to `add-to-list'
648into a hook function that will be run only after loading the package.
649`eval-after-load' provides one way to do this. In some cases
650other hooks, such as major mode hooks, can do the job."
6e3af630
RS
651 (or (member element (symbol-value list-var))
652 (set list-var (cons element (symbol-value list-var)))))
be9b65ac 653\f
9a5336ae
JB
654;;;; Specifying things to do after certain files are loaded.
655
656(defun eval-after-load (file form)
657 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
658This makes or adds to an entry on `after-load-alist'.
90914938 659If FILE is already loaded, evaluate FORM right now.
12c7071c 660It does nothing if FORM is already on the list for FILE.
9a5336ae 661FILE should be the name of a library, with no directory name."
90914938 662 ;; Make sure there is an element for FILE.
9a5336ae
JB
663 (or (assoc file after-load-alist)
664 (setq after-load-alist (cons (list file) after-load-alist)))
90914938 665 ;; Add FORM to the element if it isn't there.
12c7071c
RS
666 (let ((elt (assoc file after-load-alist)))
667 (or (member form (cdr elt))
90914938
RS
668 (progn
669 (nconc elt (list form))
670 ;; If the file has been loaded already, run FORM right away.
671 (and (assoc file load-history)
672 (eval form)))))
9a5336ae
JB
673 form)
674
675(defun eval-next-after-load (file)
676 "Read the following input sexp, and run it whenever FILE is loaded.
677This makes or adds to an entry on `after-load-alist'.
678FILE should be the name of a library, with no directory name."
679 (eval-after-load file (read)))
680
681\f
682;;;; Input and display facilities.
683
684(defun read-quoted-char (&optional prompt)
685 "Like `read-char', except that if the first character read is an octal
686digit, we read up to two more octal digits and return the character
687represented by the octal number consisting of those digits.
688Optional argument PROMPT specifies a string to use to prompt the user."
1219a2a4 689 (let ((message-log-max nil) (count 0) (code 0) char)
9a5336ae
JB
690 (while (< count 3)
691 (let ((inhibit-quit (zerop count))
42e636f0
KH
692 ;; Don't let C-h get the help message--only help function keys.
693 (help-char nil)
694 (help-form
695 "Type the special character you want to use,
696or three octal digits representing its character code."))
9a5336ae
JB
697 (and prompt (message "%s-" prompt))
698 (setq char (read-char))
699 (if inhibit-quit (setq quit-flag nil)))
700 (cond ((null char))
701 ((and (<= ?0 char) (<= char ?7))
702 (setq code (+ (* code 8) (- char ?0))
703 count (1+ count))
704 (and prompt (message (setq prompt
705 (format "%s %c" prompt char)))))
706 ((> count 0)
707 (setq unread-command-events (list char) count 259))
708 (t (setq code char count 259))))
0342b545 709 ;; Turn a meta-character into a character with the 0200 bit set.
1219a2a4 710 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
0342b545 711 (logand 255 code))))
9a5336ae
JB
712
713(defun force-mode-line-update (&optional all)
714 "Force the mode-line of the current buffer to be redisplayed.
7ec2a18c 715With optional non-nil ALL, force redisplay of all mode-lines."
9a5336ae
JB
716 (if all (save-excursion (set-buffer (other-buffer))))
717 (set-buffer-modified-p (buffer-modified-p)))
718
be9b65ac
DL
719(defun momentary-string-display (string pos &optional exit-char message)
720 "Momentarily display STRING in the buffer at POS.
721Display remains until next character is typed.
722If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
723otherwise it is then available as input (as a command if nothing else).
724Display MESSAGE (optional fourth arg) in the echo area.
725If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
726 (or exit-char (setq exit-char ?\ ))
727 (let ((buffer-read-only nil)
ca2ec1c5
RS
728 ;; Don't modify the undo list at all.
729 (buffer-undo-list t)
be9b65ac
DL
730 (modified (buffer-modified-p))
731 (name buffer-file-name)
732 insert-end)
733 (unwind-protect
734 (progn
735 (save-excursion
736 (goto-char pos)
737 ;; defeat file locking... don't try this at home, kids!
738 (setq buffer-file-name nil)
739 (insert-before-markers string)
3eec84bf
RS
740 (setq insert-end (point))
741 ;; If the message end is off screen, recenter now.
742 (if (> (window-end) insert-end)
743 (recenter (/ (window-height) 2)))
744 ;; If that pushed message start off the screen,
745 ;; scroll to start it at the top of the screen.
746 (move-to-window-line 0)
747 (if (> (point) pos)
748 (progn
749 (goto-char pos)
750 (recenter 0))))
be9b65ac
DL
751 (message (or message "Type %s to continue editing.")
752 (single-key-description exit-char))
3547c855 753 (let ((char (read-event)))
be9b65ac 754 (or (eq char exit-char)
dbc4e1c1 755 (setq unread-command-events (list char)))))
be9b65ac
DL
756 (if insert-end
757 (save-excursion
758 (delete-region pos insert-end)))
759 (setq buffer-file-name name)
760 (set-buffer-modified-p modified))))
761
9a5336ae
JB
762\f
763;;;; Miscellanea.
764
448b61c9
RS
765;; A number of major modes set this locally.
766;; Give it a global value to avoid compiler warnings.
767(defvar font-lock-defaults nil)
768
769;; Avoid compiler warnings about this variable,
770;; which has a special meaning on certain system types.
771(defvar buffer-file-type nil
772 "Non-nil if the visited file is a binary file.
773This variable is meaningful on MS-DOG and Windows NT.
774On those systems, it is automatically local in every buffer.
775On other systems, this variable is normally always nil.")
776
f9269e19
RS
777(defun ignore (&rest ignore)
778 "Do nothing and return nil.
779This function accepts any number of arguments, but ignores them."
c0f1a4f6 780 (interactive)
9a5336ae
JB
781 nil)
782
783(defun error (&rest args)
784 "Signal an error, making error message by passing all args to `format'."
785 (while t
786 (signal 'error (list (apply 'format args)))))
787
cef7ae6e 788(defalias 'user-original-login-name 'user-login-name)
9a5336ae 789
be9b65ac
DL
790(defun start-process-shell-command (name buffer &rest args)
791 "Start a program in a subprocess. Return the process object for it.
792Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
793NAME is name for process. It is modified if necessary to make it unique.
794BUFFER is the buffer or (buffer-name) to associate with the process.
795 Process output goes at end of that buffer, unless you specify
796 an output stream or filter function to handle the output.
797 BUFFER may be also nil, meaning that this process is not associated
798 with any buffer
799Third arg is command name, the name of a shell command.
800Remaining arguments are the arguments for the command.
4f1d6310 801Wildcards and redirection are handled as usual in the shell."
a247bf21
KH
802 (cond
803 ((eq system-type 'vax-vms)
804 (apply 'start-process name buffer args))
b59f6d7a
RS
805 ;; We used to use `exec' to replace the shell with the command,
806 ;; but that failed to handle (...) and semicolon, etc.
a247bf21
KH
807 (t
808 (start-process name buffer shell-file-name shell-command-switch
b59f6d7a 809 (mapconcat 'identity args " ")))))
be9b65ac 810
9a5336ae
JB
811(defmacro save-match-data (&rest body)
812 "Execute the BODY forms, restoring the global value of the match data."
813 (let ((original (make-symbol "match-data")))
993713ce
SM
814 (list 'let (list (list original '(match-data)))
815 (list 'unwind-protect
816 (cons 'progn body)
817 (list 'store-match-data original)))))
818
cd323f89 819(defun match-string (num &optional string)
993713ce
SM
820 "Return string of text matched by last search.
821NUM specifies which parenthesized expression in the last regexp.
822 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
823Zero means the entire text matched by the whole regexp or whole string.
824STRING should be given if the last search was by `string-match' on STRING."
cd323f89
SM
825 (if (match-beginning num)
826 (if string
827 (substring string (match-beginning num) (match-end num))
828 (buffer-substring (match-beginning num) (match-end num)))))
58f950b4 829
5f7fc6cf
RS
830(defun buffer-substring-no-properties (beg end)
831 "Return the text from BEG to END, without text properties, as a string."
832 (let ((string (buffer-substring beg end)))
833 (set-text-properties 0 (length string) nil string)
834 string))
835
8af7df60
RS
836(defun shell-quote-argument (argument)
837 "Quote an argument for passing as argument to an inferior shell."
c1c74b43
RS
838 (if (eq system-type 'ms-dos)
839 ;; MS-DOS shells don't have quoting, so don't do any.
840 argument
841 (if (eq system-type 'windows-nt)
842 (concat "\"" argument "\"")
843 ;; Quote everything except POSIX filename characters.
844 ;; This should be safe enough even for really weird shells.
845 (let ((result "") (start 0) end)
846 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
847 (setq end (match-beginning 0)
848 result (concat result (substring argument start end)
849 "\\" (substring argument end (1+ end)))
850 start (1+ end)))
851 (concat result (substring argument start))))))
8af7df60 852
297d863b 853(defun make-syntax-table (&optional oldtable)
984f718a
RS
854 "Return a new syntax table.
855It inherits all letters and control characters from the standard
856syntax table; other characters are copied from the standard syntax table."
297d863b
KH
857 (if oldtable
858 (copy-syntax-table oldtable)
859 (let ((table (copy-syntax-table))
860 i)
861 (setq i 0)
862 (while (<= i 31)
863 (aset table i 13)
864 (setq i (1+ i)))
865 (setq i ?A)
866 (while (<= i ?Z)
867 (aset table i 13)
868 (setq i (1+ i)))
869 (setq i ?a)
870 (while (<= i ?z)
871 (aset table i 13)
872 (setq i (1+ i)))
873 (setq i 128)
874 (while (<= i 255)
875 (aset table i 13)
876 (setq i (1+ i)))
877 table)))
baed0109
RS
878\f
879(defun global-set-key (key command)
880 "Give KEY a global binding as COMMAND.
881COMMAND is a symbol naming an interactively-callable function.
882KEY is a key sequence (a string or vector of characters or event types).
883Non-ASCII characters with codes above 127 (such as ISO Latin-1)
884can be included if you use a vector.
885Note that if KEY has a local binding in the current buffer
886that local binding will continue to shadow any global binding."
887 (interactive "KSet key globally: \nCSet key %s to command: ")
888 (or (vectorp key) (stringp key)
889 (signal 'wrong-type-argument (list 'arrayp key)))
890 (define-key (current-global-map) key command)
891 nil)
892
893(defun local-set-key (key command)
894 "Give KEY a local binding as COMMAND.
895COMMAND is a symbol naming an interactively-callable function.
896KEY is a key sequence (a string or vector of characters or event types).
897Non-ASCII characters with codes above 127 (such as ISO Latin-1)
898can be included if you use a vector.
899The binding goes in the current buffer's local map,
900which in most cases is shared with all other buffers in the same major mode."
901 (interactive "KSet key locally: \nCSet key %s locally to command: ")
902 (let ((map (current-local-map)))
903 (or map
904 (use-local-map (setq map (make-sparse-keymap))))
905 (or (vectorp key) (stringp key)
906 (signal 'wrong-type-argument (list 'arrayp key)))
907 (define-key map key command))
908 nil)
984f718a 909
baed0109
RS
910(defun global-unset-key (key)
911 "Remove global binding of KEY.
912KEY is a string representing a sequence of keystrokes."
913 (interactive "kUnset key globally: ")
914 (global-set-key key nil))
915
db2474b8 916(defun local-unset-key (key)
baed0109
RS
917 "Remove local binding of KEY.
918KEY is a string representing a sequence of keystrokes."
919 (interactive "kUnset key locally: ")
920 (if (current-local-map)
db2474b8 921 (local-set-key key nil))
baed0109
RS
922 nil)
923\f
4809d0dd
KH
924;; We put this here instead of in frame.el so that it's defined even on
925;; systems where frame.el isn't loaded.
926(defun frame-configuration-p (object)
927 "Return non-nil if OBJECT seems to be a frame configuration.
928Any list whose car is `frame-configuration' is assumed to be a frame
929configuration."
930 (and (consp object)
931 (eq (car object) 'frame-configuration)))
932
9a5336ae
JB
933;; now in fns.c
934;(defun nth (n list)
935; "Returns the Nth element of LIST.
936;N counts from zero. If LIST is not that long, nil is returned."
937; (car (nthcdr n list)))
938;
939;(defun copy-alist (alist)
940; "Return a copy of ALIST.
941;This is a new alist which represents the same mapping
942;from objects to objects, but does not share the alist structure with ALIST.
943;The objects mapped (cars and cdrs of elements of the alist)
944;are shared, however."
945; (setq alist (copy-sequence alist))
946; (let ((tail alist))
947; (while tail
948; (if (consp (car tail))
949; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
950; (setq tail (cdr tail))))
951; alist)
630cc463
ER
952
953;;; subr.el ends here
9a5336ae 954