(LIBES): Add LOADLIBES and LDLIBS.
[bpt/emacs.git] / lisp / subr.el
CommitLineData
c88ab9ce 1;;; subr.el --- basic lisp subroutines for Emacs
630cc463 2
b578f267 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
b578f267
EN
18;; along with GNU Emacs; see the file COPYING. If not, write to the
19;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20;; Boston, MA 02111-1307, USA.
be9b65ac 21
630cc463 22;;; Code:
be9b65ac 23
9a5336ae
JB
24\f
25;;;; Lisp language features.
26
27(defmacro lambda (&rest cdr)
28 "Return a lambda expression.
29A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
30self-quoting; the result of evaluating the lambda expression is the
31expression itself. The lambda expression may then be treated as a
bec0d7f9
RS
32function, i.e., stored as the function value of a symbol, passed to
33funcall or mapcar, etc.
34
9a5336ae 35ARGS should take the same form as an argument list for a `defun'.
8fd68088
RS
36DOCSTRING is an optional documentation string.
37 If present, it should describe how to call the function.
38 But documentation strings are usually not useful in nameless functions.
9a5336ae
JB
39INTERACTIVE should be a call to the function `interactive', which see.
40It may also be omitted.
41BODY should be a list of lisp expressions."
42 ;; Note that this definition should not use backquotes; subr.el should not
43 ;; depend on backquote.el.
44 (list 'function (cons 'lambda cdr)))
45
46;;(defmacro defun-inline (name args &rest body)
47;; "Create an \"inline defun\" (actually a macro).
48;;Use just like `defun'."
49;; (nconc (list 'defmacro name '(&rest args))
50;; (if (stringp (car body))
51;; (prog1 (list (car body))
52;; (setq body (or (cdr body) body))))
53;; (list (list 'cons (list 'quote
54;; (cons 'lambda (cons args body)))
55;; 'args))))
56
57\f
9a5336ae 58;;;; Keymap support.
be9b65ac
DL
59
60(defun undefined ()
61 (interactive)
62 (ding))
63
64;Prevent the \{...} documentation construct
65;from mentioning keys that run this command.
66(put 'undefined 'suppress-keymap t)
67
68(defun suppress-keymap (map &optional nodigits)
69 "Make MAP override all normally self-inserting keys to be undefined.
70Normally, as an exception, digits and minus-sign are set to make prefix args,
71but optional second arg NODIGITS non-nil treats them like other chars."
80e7b471 72 (substitute-key-definition 'self-insert-command 'undefined map global-map)
be9b65ac
DL
73 (or nodigits
74 (let (loop)
75 (define-key map "-" 'negative-argument)
76 ;; Make plain numbers do numeric args.
77 (setq loop ?0)
78 (while (<= loop ?9)
79 (define-key map (char-to-string loop) 'digit-argument)
80 (setq loop (1+ loop))))))
81
be9b65ac
DL
82;Moved to keymap.c
83;(defun copy-keymap (keymap)
84; "Return a copy of KEYMAP"
85; (while (not (keymapp keymap))
86; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
87; (if (vectorp keymap)
88; (copy-sequence keymap)
89; (copy-alist keymap)))
90
f14dbba7
KH
91(defvar key-substitution-in-progress nil
92 "Used internally by substitute-key-definition.")
93
7f2c2edd 94(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
95 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
96In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
97If optional fourth argument OLDMAP is specified, we redefine
98in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
99 (or prefix (setq prefix ""))
100 (let* ((scan (or oldmap keymap))
101 (vec1 (vector nil))
f14dbba7
KH
102 (prefix1 (vconcat prefix vec1))
103 (key-substitution-in-progress
104 (cons scan key-substitution-in-progress)))
7f2c2edd
RS
105 ;; Scan OLDMAP, finding each char or event-symbol that
106 ;; has any definition, and act on it with hack-key.
107 (while (consp scan)
108 (if (consp (car scan))
109 (let ((char (car (car scan)))
110 (defn (cdr (car scan))))
111 ;; The inside of this let duplicates exactly
112 ;; the inside of the following let that handles array elements.
113 (aset vec1 0 char)
114 (aset prefix1 (length prefix) char)
44d798af 115 (let (inner-def skipped)
7f2c2edd
RS
116 ;; Skip past menu-prompt.
117 (while (stringp (car-safe defn))
44d798af 118 (setq skipped (cons (car defn) skipped))
7f2c2edd 119 (setq defn (cdr defn)))
e025dddf
RS
120 ;; Skip past cached key-equivalence data for menu items.
121 (and (consp defn) (consp (car defn))
122 (setq defn (cdr defn)))
7f2c2edd 123 (setq inner-def defn)
e025dddf 124 ;; Look past a symbol that names a keymap.
7f2c2edd
RS
125 (while (and (symbolp inner-def)
126 (fboundp inner-def))
127 (setq inner-def (symbol-function inner-def)))
128 (if (eq defn olddef)
44d798af 129 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
f14dbba7 130 (if (and (keymapp defn)
350b7567
RS
131 ;; Avoid recursively scanning
132 ;; where KEYMAP does not have a submap.
afd9831b
RS
133 (let ((elt (lookup-key keymap prefix1)))
134 (or (null elt)
135 (keymapp elt)))
350b7567 136 ;; Avoid recursively rescanning keymap being scanned.
f14dbba7
KH
137 (not (memq inner-def
138 key-substitution-in-progress)))
e025dddf
RS
139 ;; If this one isn't being scanned already,
140 ;; scan it now.
7f2c2edd
RS
141 (substitute-key-definition olddef newdef keymap
142 inner-def
143 prefix1)))))
144 (if (arrayp (car scan))
145 (let* ((array (car scan))
146 (len (length array))
147 (i 0))
148 (while (< i len)
149 (let ((char i) (defn (aref array i)))
150 ;; The inside of this let duplicates exactly
151 ;; the inside of the previous let.
152 (aset vec1 0 char)
153 (aset prefix1 (length prefix) char)
44d798af 154 (let (inner-def skipped)
7f2c2edd
RS
155 ;; Skip past menu-prompt.
156 (while (stringp (car-safe defn))
44d798af 157 (setq skipped (cons (car defn) skipped))
7f2c2edd 158 (setq defn (cdr defn)))
e025dddf
RS
159 (and (consp defn) (consp (car defn))
160 (setq defn (cdr defn)))
7f2c2edd
RS
161 (setq inner-def defn)
162 (while (and (symbolp inner-def)
163 (fboundp inner-def))
164 (setq inner-def (symbol-function inner-def)))
165 (if (eq defn olddef)
44d798af
RS
166 (define-key keymap prefix1
167 (nconc (nreverse skipped) newdef))
f14dbba7 168 (if (and (keymapp defn)
afd9831b
RS
169 (let ((elt (lookup-key keymap prefix1)))
170 (or (null elt)
171 (keymapp elt)))
f14dbba7
KH
172 (not (memq inner-def
173 key-substitution-in-progress)))
7f2c2edd
RS
174 (substitute-key-definition olddef newdef keymap
175 inner-def
176 prefix1)))))
177 (setq i (1+ i))))))
178 (setq scan (cdr scan)))))
9a5336ae 179
06ae9cf2 180(defun define-key-after (keymap key definition after)
4434d61b
RS
181 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
182This is like `define-key' except that the binding for KEY is placed
183just after the binding for the event AFTER, instead of at the beginning
184of the map.
626f67f3 185The order matters when the keymap is used as a menu.
9ed287b0
RS
186KEY must contain just one event type--that is to say, it must be
187a string or vector of length 1."
4434d61b
RS
188 (or (keymapp keymap)
189 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 190 (if (> (length key) 1)
626f67f3 191 (error "multi-event key specified in `define-key-after'"))
113d28a8 192 (let ((tail keymap) done inserted
4434d61b
RS
193 (first (aref key 0)))
194 (while (and (not done) tail)
195 ;; Delete any earlier bindings for the same key.
196 (if (eq (car-safe (car (cdr tail))) first)
197 (setcdr tail (cdr (cdr tail))))
198 ;; When we reach AFTER's binding, insert the new binding after.
199 ;; If we reach an inherited keymap, insert just before that.
113d28a8 200 ;; If we reach the end of this keymap, insert at the end.
4434d61b 201 (if (or (eq (car-safe (car tail)) after)
113d28a8
RS
202 (eq (car (cdr tail)) 'keymap)
203 (null (cdr tail)))
4434d61b 204 (progn
113d28a8
RS
205 ;; Stop the scan only if we find a parent keymap.
206 ;; Keep going past the inserted element
207 ;; so we can delete any duplications that come later.
208 (if (eq (car (cdr tail)) 'keymap)
209 (setq done t))
210 ;; Don't insert more than once.
211 (or inserted
212 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
213 (setq inserted t)))
4434d61b
RS
214 (setq tail (cdr tail)))))
215
8bed5e3d
RS
216(put 'keyboard-translate-table 'char-table-extra-slots 0)
217
9a5336ae
JB
218(defun keyboard-translate (from to)
219 "Translate character FROM to TO at a low level.
220This function creates a `keyboard-translate-table' if necessary
221and then modifies one entry in it."
8bed5e3d
RS
222 (or (char-table-p keyboard-translate-table)
223 (setq keyboard-translate-table
224 (make-char-table 'keyboard-translate-table nil)))
9a5336ae
JB
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
0a5c0893
MB
425(defalias 'focus-frame 'ignore)
426(defalias 'unfocus-frame 'ignore)
9a5336ae
JB
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)
059184dd
ER
433(defalias 'not 'null)
434(defalias 'rplaca 'setcar)
435(defalias 'rplacd 'setcdr)
eb8c3be9 436(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
437(defalias 'indent-to-column 'indent-to)
438(defalias 'backward-delete-char 'delete-backward-char)
439(defalias 'search-forward-regexp (symbol-function 're-search-forward))
440(defalias 'search-backward-regexp (symbol-function 're-search-backward))
441(defalias 'int-to-string 'number-to-string)
1e0a78a1 442(defalias 'set-match-data 'store-match-data)
37f6661a
JB
443
444;;; Should this be an obsolete name? If you decide it should, you get
445;;; to go through all the sources and change them.
059184dd 446(defalias 'string-to-int 'string-to-number)
be9b65ac 447\f
9a5336ae 448;;;; Hook manipulation functions.
be9b65ac 449
0e4d378b
RS
450(defun make-local-hook (hook)
451 "Make the hook HOOK local to the current buffer.
452When a hook is local, its local and global values
453work in concert: running the hook actually runs all the hook
454functions listed in *either* the local value *or* the global value
455of the hook variable.
456
7dd1926e
RS
457This function works by making `t' a member of the buffer-local value,
458which acts as a flag to run the hook functions in the default value as
459well. This works for all normal hooks, but does not work for most
460non-normal hooks yet. We will be changing the callers of non-normal
461hooks so that they can handle localness; this has to be done one by
462one.
463
464This function does nothing if HOOK is already local in the current
465buffer.
0e4d378b
RS
466
467Do not use `make-local-variable' to make a hook variable buffer-local."
468 (if (local-variable-p hook)
469 nil
470 (or (boundp hook) (set hook nil))
471 (make-local-variable hook)
472 (set hook (list t))))
473
474(defun add-hook (hook function &optional append local)
32295976
RS
475 "Add to the value of HOOK the function FUNCTION.
476FUNCTION is not added if already present.
477FUNCTION is added (if necessary) at the beginning of the hook list
478unless the optional argument APPEND is non-nil, in which case
479FUNCTION is added at the end.
480
0e4d378b
RS
481The optional fourth argument, LOCAL, if non-nil, says to modify
482the hook's buffer-local value rather than its default value.
483This makes no difference if the hook is not buffer-local.
484To make a hook variable buffer-local, always use
485`make-local-hook', not `make-local-variable'.
486
32295976
RS
487HOOK should be a symbol, and FUNCTION may be any valid function. If
488HOOK is void, it is first set to nil. If HOOK's value is a single
aa09b5ca 489function, it is changed to a list of functions."
be9b65ac 490 (or (boundp hook) (set hook nil))
0e4d378b 491 (or (default-boundp hook) (set-default hook nil))
32295976
RS
492 ;; If the hook value is a single function, turn it into a list.
493 (let ((old (symbol-value hook)))
494 (if (or (not (listp old)) (eq (car old) 'lambda))
495 (set hook (list old))))
f4e5bca5
RS
496 (if (or local
497 ;; Detect the case where make-local-variable was used on a hook
498 ;; and do what we used to do.
cd2db344 499 (and (local-variable-if-set-p hook)
f4e5bca5 500 (not (memq t (symbol-value hook)))))
0e4d378b
RS
501 ;; Alter the local value only.
502 (or (if (consp function)
503 (member function (symbol-value hook))
504 (memq function (symbol-value hook)))
505 (set hook
506 (if append
507 (append (symbol-value hook) (list function))
508 (cons function (symbol-value hook)))))
509 ;; Alter the global value (which is also the only value,
510 ;; if the hook doesn't have a local value).
511 (or (if (consp function)
512 (member function (default-value hook))
513 (memq function (default-value hook)))
514 (set-default hook
515 (if append
516 (append (default-value hook) (list function))
517 (cons function (default-value hook)))))))
518
519(defun remove-hook (hook function &optional local)
24980d16
RS
520 "Remove from the value of HOOK the function FUNCTION.
521HOOK should be a symbol, and FUNCTION may be any valid function. If
522FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
0e4d378b
RS
523list of hooks to run in HOOK, then nothing is done. See `add-hook'.
524
525The optional third argument, LOCAL, if non-nil, says to modify
526the hook's buffer-local value rather than its default value.
527This makes no difference if the hook is not buffer-local.
528To make a hook variable buffer-local, always use
529`make-local-hook', not `make-local-variable'."
24980d16 530 (if (or (not (boundp hook)) ;unbound symbol, or
0e4d378b 531 (not (default-boundp 'hook))
24980d16
RS
532 (null (symbol-value hook)) ;value is nil, or
533 (null function)) ;function is nil, then
534 nil ;Do nothing.
f4e5bca5
RS
535 (if (or local
536 ;; Detect the case where make-local-variable was used on a hook
537 ;; and do what we used to do.
538 (and (local-variable-p hook)
539 (not (memq t (symbol-value hook)))))
0e4d378b
RS
540 (let ((hook-value (symbol-value hook)))
541 (if (consp hook-value)
542 (if (member function hook-value)
543 (setq hook-value (delete function (copy-sequence hook-value))))
544 (if (equal hook-value function)
545 (setq hook-value nil)))
546 (set hook hook-value))
547 (let ((hook-value (default-value hook)))
548 (if (consp hook-value)
549 (if (member function hook-value)
550 (setq hook-value (delete function (copy-sequence hook-value))))
551 (if (equal hook-value function)
552 (setq hook-value nil)))
553 (set-default hook hook-value)))))
6e3af630
RS
554
555(defun add-to-list (list-var element)
8851c1f0 556 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
9f0b1f09 557The test for presence of ELEMENT is done with `equal'.
8851c1f0
RS
558If you want to use `add-to-list' on a variable that is not defined
559until a certain package is loaded, you should put the call to `add-to-list'
560into a hook function that will be run only after loading the package.
561`eval-after-load' provides one way to do this. In some cases
562other hooks, such as major mode hooks, can do the job."
6e3af630
RS
563 (or (member element (symbol-value list-var))
564 (set list-var (cons element (symbol-value list-var)))))
be9b65ac 565\f
9a5336ae
JB
566;;;; Specifying things to do after certain files are loaded.
567
568(defun eval-after-load (file form)
569 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
570This makes or adds to an entry on `after-load-alist'.
90914938 571If FILE is already loaded, evaluate FORM right now.
12c7071c 572It does nothing if FORM is already on the list for FILE.
9a5336ae 573FILE should be the name of a library, with no directory name."
90914938 574 ;; Make sure there is an element for FILE.
9a5336ae
JB
575 (or (assoc file after-load-alist)
576 (setq after-load-alist (cons (list file) after-load-alist)))
90914938 577 ;; Add FORM to the element if it isn't there.
12c7071c
RS
578 (let ((elt (assoc file after-load-alist)))
579 (or (member form (cdr elt))
90914938
RS
580 (progn
581 (nconc elt (list form))
582 ;; If the file has been loaded already, run FORM right away.
583 (and (assoc file load-history)
584 (eval form)))))
9a5336ae
JB
585 form)
586
587(defun eval-next-after-load (file)
588 "Read the following input sexp, and run it whenever FILE is loaded.
589This makes or adds to an entry on `after-load-alist'.
590FILE should be the name of a library, with no directory name."
591 (eval-after-load file (read)))
592
593\f
594;;;; Input and display facilities.
595
596(defun read-quoted-char (&optional prompt)
597 "Like `read-char', except that if the first character read is an octal
598digit, we read up to two more octal digits and return the character
599represented by the octal number consisting of those digits.
600Optional argument PROMPT specifies a string to use to prompt the user."
1219a2a4 601 (let ((message-log-max nil) (count 0) (code 0) char)
9a5336ae
JB
602 (while (< count 3)
603 (let ((inhibit-quit (zerop count))
42e636f0
KH
604 ;; Don't let C-h get the help message--only help function keys.
605 (help-char nil)
606 (help-form
607 "Type the special character you want to use,
608or three octal digits representing its character code."))
9a5336ae
JB
609 (and prompt (message "%s-" prompt))
610 (setq char (read-char))
611 (if inhibit-quit (setq quit-flag nil)))
612 (cond ((null char))
613 ((and (<= ?0 char) (<= char ?7))
614 (setq code (+ (* code 8) (- char ?0))
615 count (1+ count))
91a6acc3 616 (and prompt (setq prompt (message "%s %c" prompt char))))
9a5336ae
JB
617 ((> count 0)
618 (setq unread-command-events (list char) count 259))
619 (t (setq code char count 259))))
0342b545 620 ;; Turn a meta-character into a character with the 0200 bit set.
1219a2a4 621 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
0342b545 622 (logand 255 code))))
9a5336ae
JB
623
624(defun force-mode-line-update (&optional all)
625 "Force the mode-line of the current buffer to be redisplayed.
7ec2a18c 626With optional non-nil ALL, force redisplay of all mode-lines."
9a5336ae
JB
627 (if all (save-excursion (set-buffer (other-buffer))))
628 (set-buffer-modified-p (buffer-modified-p)))
629
be9b65ac
DL
630(defun momentary-string-display (string pos &optional exit-char message)
631 "Momentarily display STRING in the buffer at POS.
632Display remains until next character is typed.
633If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
634otherwise it is then available as input (as a command if nothing else).
635Display MESSAGE (optional fourth arg) in the echo area.
636If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
637 (or exit-char (setq exit-char ?\ ))
638 (let ((buffer-read-only nil)
ca2ec1c5
RS
639 ;; Don't modify the undo list at all.
640 (buffer-undo-list t)
be9b65ac
DL
641 (modified (buffer-modified-p))
642 (name buffer-file-name)
643 insert-end)
644 (unwind-protect
645 (progn
646 (save-excursion
647 (goto-char pos)
648 ;; defeat file locking... don't try this at home, kids!
649 (setq buffer-file-name nil)
650 (insert-before-markers string)
3eec84bf
RS
651 (setq insert-end (point))
652 ;; If the message end is off screen, recenter now.
653 (if (> (window-end) insert-end)
654 (recenter (/ (window-height) 2)))
655 ;; If that pushed message start off the screen,
656 ;; scroll to start it at the top of the screen.
657 (move-to-window-line 0)
658 (if (> (point) pos)
659 (progn
660 (goto-char pos)
661 (recenter 0))))
be9b65ac
DL
662 (message (or message "Type %s to continue editing.")
663 (single-key-description exit-char))
3547c855 664 (let ((char (read-event)))
be9b65ac 665 (or (eq char exit-char)
dbc4e1c1 666 (setq unread-command-events (list char)))))
be9b65ac
DL
667 (if insert-end
668 (save-excursion
669 (delete-region pos insert-end)))
670 (setq buffer-file-name name)
671 (set-buffer-modified-p modified))))
672
9a5336ae
JB
673\f
674;;;; Miscellanea.
675
448b61c9
RS
676;; A number of major modes set this locally.
677;; Give it a global value to avoid compiler warnings.
678(defvar font-lock-defaults nil)
679
680;; Avoid compiler warnings about this variable,
681;; which has a special meaning on certain system types.
682(defvar buffer-file-type nil
683 "Non-nil if the visited file is a binary file.
684This variable is meaningful on MS-DOG and Windows NT.
685On those systems, it is automatically local in every buffer.
686On other systems, this variable is normally always nil.")
687
a860d25f 688;; This should probably be written in C (i.e., without using `walk-windows').
63503b24 689(defun get-buffer-window-list (buffer &optional minibuf frame)
a860d25f 690 "Return windows currently displaying BUFFER, or nil if none.
63503b24 691See `walk-windows' for the meaning of MINIBUF and FRAME."
43c5ac8c 692 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
a860d25f
SM
693 (walk-windows (function (lambda (window)
694 (if (eq (window-buffer window) buffer)
695 (setq windows (cons window windows)))))
63503b24 696 minibuf frame)
a860d25f
SM
697 windows))
698
f9269e19
RS
699(defun ignore (&rest ignore)
700 "Do nothing and return nil.
701This function accepts any number of arguments, but ignores them."
c0f1a4f6 702 (interactive)
9a5336ae
JB
703 nil)
704
705(defun error (&rest args)
aa308ce2
RS
706 "Signal an error, making error message by passing all args to `format'.
707In Emacs, the convention is that error messages start with a capital
708letter but *do not* end with a period. Please follow this convention
709for the sake of consistency."
9a5336ae
JB
710 (while t
711 (signal 'error (list (apply 'format args)))))
712
cef7ae6e 713(defalias 'user-original-login-name 'user-login-name)
9a5336ae 714
be9b65ac
DL
715(defun start-process-shell-command (name buffer &rest args)
716 "Start a program in a subprocess. Return the process object for it.
717Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
718NAME is name for process. It is modified if necessary to make it unique.
719BUFFER is the buffer or (buffer-name) to associate with the process.
720 Process output goes at end of that buffer, unless you specify
721 an output stream or filter function to handle the output.
722 BUFFER may be also nil, meaning that this process is not associated
723 with any buffer
724Third arg is command name, the name of a shell command.
725Remaining arguments are the arguments for the command.
4f1d6310 726Wildcards and redirection are handled as usual in the shell."
a247bf21
KH
727 (cond
728 ((eq system-type 'vax-vms)
729 (apply 'start-process name buffer args))
b59f6d7a
RS
730 ;; We used to use `exec' to replace the shell with the command,
731 ;; but that failed to handle (...) and semicolon, etc.
a247bf21
KH
732 (t
733 (start-process name buffer shell-file-name shell-command-switch
b59f6d7a 734 (mapconcat 'identity args " ")))))
be9b65ac 735
a7f284ec
RS
736(defmacro with-current-buffer (buffer &rest body)
737 "Execute the forms in BODY with BUFFER as the current buffer.
738The value returned is the value of the last form in BODY."
739 `(save-current-buffer
740 (set-buffer ,buffer)
741 . ,body))
742
5db7925d
RS
743(defmacro with-output-to-string (&rest body)
744 "Execute BODY, return the text it sent to `standard-output', as a string."
745 `(let ((standard-output (get-buffer-create " *string-output*")))
746 (save-excursion
747 (set-buffer standard-output)
748 (buffer-disable-undo (current-buffer))
749 (let ((inhibit-read-only t))
750 (erase-buffer))
751 (setq buffer-read-only nil))
752 (let ((standard-output standard-output))
753 ,@body)
754 (save-excursion
755 (set-buffer standard-output)
756 (buffer-string))))
757
c7ca41e6
RS
758(defvar save-match-data-internal)
759
760;; We use save-match-data-internal as the local variable because
761;; that works ok in practice (people should not use that variable elsewhere).
762;; We used to use an uninterned symbol; the compiler handles that properly
763;; now, but it generates slower code.
9a5336ae
JB
764(defmacro save-match-data (&rest body)
765 "Execute the BODY forms, restoring the global value of the match data."
9fc0eb95 766 `(let ((save-match-data-internal (match-data)))
c7ca41e6
RS
767 (unwind-protect
768 (progn ,@body)
ecc06779 769 (store-match-data save-match-data-internal))))
993713ce 770
cd323f89 771(defun match-string (num &optional string)
993713ce
SM
772 "Return string of text matched by last search.
773NUM specifies which parenthesized expression in the last regexp.
774 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
775Zero means the entire text matched by the whole regexp or whole string.
776STRING should be given if the last search was by `string-match' on STRING."
cd323f89
SM
777 (if (match-beginning num)
778 (if string
779 (substring string (match-beginning num) (match-end num))
780 (buffer-substring (match-beginning num) (match-end num)))))
58f950b4 781
8af7df60
RS
782(defun shell-quote-argument (argument)
783 "Quote an argument for passing as argument to an inferior shell."
c1c74b43
RS
784 (if (eq system-type 'ms-dos)
785 ;; MS-DOS shells don't have quoting, so don't do any.
786 argument
787 (if (eq system-type 'windows-nt)
788 (concat "\"" argument "\"")
789 ;; Quote everything except POSIX filename characters.
790 ;; This should be safe enough even for really weird shells.
791 (let ((result "") (start 0) end)
792 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
793 (setq end (match-beginning 0)
794 result (concat result (substring argument start end)
795 "\\" (substring argument end (1+ end)))
796 start (1+ end)))
797 (concat result (substring argument start))))))
8af7df60 798
297d863b 799(defun make-syntax-table (&optional oldtable)
984f718a
RS
800 "Return a new syntax table.
801It inherits all letters and control characters from the standard
802syntax table; other characters are copied from the standard syntax table."
297d863b
KH
803 (if oldtable
804 (copy-syntax-table oldtable)
805 (let ((table (copy-syntax-table))
806 i)
807 (setq i 0)
808 (while (<= i 31)
a6889c57 809 (aset table i nil)
297d863b
KH
810 (setq i (1+ i)))
811 (setq i ?A)
812 (while (<= i ?Z)
a6889c57 813 (aset table i nil)
297d863b
KH
814 (setq i (1+ i)))
815 (setq i ?a)
816 (while (<= i ?z)
a6889c57 817 (aset table i nil)
297d863b
KH
818 (setq i (1+ i)))
819 (setq i 128)
820 (while (<= i 255)
a6889c57 821 (aset table i nil)
297d863b
KH
822 (setq i (1+ i)))
823 table)))
baed0109
RS
824\f
825(defun global-set-key (key command)
826 "Give KEY a global binding as COMMAND.
827COMMAND is a symbol naming an interactively-callable function.
828KEY is a key sequence (a string or vector of characters or event types).
829Non-ASCII characters with codes above 127 (such as ISO Latin-1)
830can be included if you use a vector.
831Note that if KEY has a local binding in the current buffer
832that local binding will continue to shadow any global binding."
833 (interactive "KSet key globally: \nCSet key %s to command: ")
834 (or (vectorp key) (stringp key)
835 (signal 'wrong-type-argument (list 'arrayp key)))
836 (define-key (current-global-map) key command)
837 nil)
838
839(defun local-set-key (key command)
840 "Give KEY a local binding as COMMAND.
841COMMAND is a symbol naming an interactively-callable function.
842KEY is a key sequence (a string or vector of characters or event types).
843Non-ASCII characters with codes above 127 (such as ISO Latin-1)
844can be included if you use a vector.
845The binding goes in the current buffer's local map,
846which in most cases is shared with all other buffers in the same major mode."
847 (interactive "KSet key locally: \nCSet key %s locally to command: ")
848 (let ((map (current-local-map)))
849 (or map
850 (use-local-map (setq map (make-sparse-keymap))))
851 (or (vectorp key) (stringp key)
852 (signal 'wrong-type-argument (list 'arrayp key)))
853 (define-key map key command))
854 nil)
984f718a 855
baed0109
RS
856(defun global-unset-key (key)
857 "Remove global binding of KEY.
858KEY is a string representing a sequence of keystrokes."
859 (interactive "kUnset key globally: ")
860 (global-set-key key nil))
861
db2474b8 862(defun local-unset-key (key)
baed0109
RS
863 "Remove local binding of KEY.
864KEY is a string representing a sequence of keystrokes."
865 (interactive "kUnset key locally: ")
866 (if (current-local-map)
db2474b8 867 (local-set-key key nil))
baed0109
RS
868 nil)
869\f
4809d0dd
KH
870;; We put this here instead of in frame.el so that it's defined even on
871;; systems where frame.el isn't loaded.
872(defun frame-configuration-p (object)
873 "Return non-nil if OBJECT seems to be a frame configuration.
874Any list whose car is `frame-configuration' is assumed to be a frame
875configuration."
876 (and (consp object)
877 (eq (car object) 'frame-configuration)))
878
9a5336ae
JB
879;; now in fns.c
880;(defun nth (n list)
881; "Returns the Nth element of LIST.
882;N counts from zero. If LIST is not that long, nil is returned."
883; (car (nthcdr n list)))
884;
885;(defun copy-alist (alist)
886; "Return a copy of ALIST.
887;This is a new alist which represents the same mapping
888;from objects to objects, but does not share the alist structure with ALIST.
889;The objects mapped (cars and cdrs of elements of the alist)
890;are shared, however."
891; (setq alist (copy-sequence alist))
892; (let ((tail alist))
893; (while tail
894; (if (consp (car tail))
895; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
896; (setq tail (cdr tail))))
897; alist)
630cc463
ER
898
899;;; subr.el ends here
9a5336ae 900