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