(x_real_positions, x_set_mouse_color, Fx_list_fonts):
[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
debff3c3
RS
46(defmacro when (cond &rest body)
47 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
48 (list 'if cond (cons 'progn body)))
11d431ad
KH
49(put 'when 'lisp-indent-function 1)
50(put 'when 'edebug-form-spec '(&rest form))
9a5336ae 51
debff3c3
RS
52(defmacro unless (cond &rest body)
53 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
54 (cons 'if (cons cond (cons nil body))))
11d431ad
KH
55(put 'unless 'lisp-indent-function 1)
56(put 'unless 'edebug-form-spec '(&rest form))
9a5336ae 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)))
328a37ec
RS
128 (if (or (eq defn olddef)
129 ;; Compare with equal if definition is a key sequence.
130 ;; That is useful for operating on function-key-map.
131 (and (or (stringp defn) (vectorp defn))
132 (equal defn olddef)))
44d798af 133 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
f14dbba7 134 (if (and (keymapp defn)
350b7567
RS
135 ;; Avoid recursively scanning
136 ;; where KEYMAP does not have a submap.
afd9831b
RS
137 (let ((elt (lookup-key keymap prefix1)))
138 (or (null elt)
139 (keymapp elt)))
350b7567 140 ;; Avoid recursively rescanning keymap being scanned.
f14dbba7
KH
141 (not (memq inner-def
142 key-substitution-in-progress)))
e025dddf
RS
143 ;; If this one isn't being scanned already,
144 ;; scan it now.
7f2c2edd
RS
145 (substitute-key-definition olddef newdef keymap
146 inner-def
147 prefix1)))))
148 (if (arrayp (car scan))
149 (let* ((array (car scan))
150 (len (length array))
151 (i 0))
152 (while (< i len)
153 (let ((char i) (defn (aref array i)))
154 ;; The inside of this let duplicates exactly
155 ;; the inside of the previous let.
156 (aset vec1 0 char)
157 (aset prefix1 (length prefix) char)
44d798af 158 (let (inner-def skipped)
7f2c2edd
RS
159 ;; Skip past menu-prompt.
160 (while (stringp (car-safe defn))
44d798af 161 (setq skipped (cons (car defn) skipped))
7f2c2edd 162 (setq defn (cdr defn)))
e025dddf
RS
163 (and (consp defn) (consp (car defn))
164 (setq defn (cdr defn)))
7f2c2edd
RS
165 (setq inner-def defn)
166 (while (and (symbolp inner-def)
167 (fboundp inner-def))
168 (setq inner-def (symbol-function inner-def)))
328a37ec
RS
169 (if (or (eq defn olddef)
170 (and (or (stringp defn) (vectorp defn))
171 (equal defn olddef)))
44d798af
RS
172 (define-key keymap prefix1
173 (nconc (nreverse skipped) newdef))
f14dbba7 174 (if (and (keymapp defn)
afd9831b
RS
175 (let ((elt (lookup-key keymap prefix1)))
176 (or (null elt)
177 (keymapp elt)))
f14dbba7
KH
178 (not (memq inner-def
179 key-substitution-in-progress)))
7f2c2edd
RS
180 (substitute-key-definition olddef newdef keymap
181 inner-def
182 prefix1)))))
183 (setq i (1+ i))))))
184 (setq scan (cdr scan)))))
9a5336ae 185
06ae9cf2 186(defun define-key-after (keymap key definition after)
4434d61b
RS
187 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
188This is like `define-key' except that the binding for KEY is placed
189just after the binding for the event AFTER, instead of at the beginning
c34a9d34
RS
190of the map. Note that AFTER must be an event type (like KEY), NOT a command
191\(like DEFINITION).
192
193If AFTER is t, the new binding goes at the end of the keymap.
194
9ed287b0 195KEY must contain just one event type--that is to say, it must be
c34a9d34
RS
196a string or vector of length 1.
197
198The order of bindings in a keymap matters when it is used as a menu."
199
4434d61b
RS
200 (or (keymapp keymap)
201 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 202 (if (> (length key) 1)
626f67f3 203 (error "multi-event key specified in `define-key-after'"))
113d28a8 204 (let ((tail keymap) done inserted
4434d61b
RS
205 (first (aref key 0)))
206 (while (and (not done) tail)
207 ;; Delete any earlier bindings for the same key.
208 (if (eq (car-safe (car (cdr tail))) first)
209 (setcdr tail (cdr (cdr tail))))
210 ;; When we reach AFTER's binding, insert the new binding after.
211 ;; If we reach an inherited keymap, insert just before that.
113d28a8 212 ;; If we reach the end of this keymap, insert at the end.
c34a9d34
RS
213 (if (or (and (eq (car-safe (car tail)) after)
214 (not (eq after t)))
113d28a8
RS
215 (eq (car (cdr tail)) 'keymap)
216 (null (cdr tail)))
4434d61b 217 (progn
113d28a8
RS
218 ;; Stop the scan only if we find a parent keymap.
219 ;; Keep going past the inserted element
220 ;; so we can delete any duplications that come later.
221 (if (eq (car (cdr tail)) 'keymap)
222 (setq done t))
223 ;; Don't insert more than once.
224 (or inserted
225 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
226 (setq inserted t)))
4434d61b
RS
227 (setq tail (cdr tail)))))
228
d128fe85
RS
229(defmacro kbd (keys)
230 "Convert KEYS to the internal Emacs key representation.
231KEYS should be a string constant in the format used for
232saving keyboard macros (see `insert-kbd-macro')."
233 (read-kbd-macro keys))
234
8bed5e3d
RS
235(put 'keyboard-translate-table 'char-table-extra-slots 0)
236
9a5336ae
JB
237(defun keyboard-translate (from to)
238 "Translate character FROM to TO at a low level.
239This function creates a `keyboard-translate-table' if necessary
240and then modifies one entry in it."
8bed5e3d
RS
241 (or (char-table-p keyboard-translate-table)
242 (setq keyboard-translate-table
243 (make-char-table 'keyboard-translate-table nil)))
9a5336ae
JB
244 (aset keyboard-translate-table from to))
245
246\f
247;;;; The global keymap tree.
248
249;;; global-map, esc-map, and ctl-x-map have their values set up in
250;;; keymap.c; we just give them docstrings here.
251
252(defvar global-map nil
253 "Default global keymap mapping Emacs keyboard input into commands.
254The value is a keymap which is usually (but not necessarily) Emacs's
255global map.")
256
257(defvar esc-map nil
258 "Default keymap for ESC (meta) commands.
259The normal global definition of the character ESC indirects to this keymap.")
260
261(defvar ctl-x-map nil
262 "Default keymap for C-x commands.
263The normal global definition of the character C-x indirects to this keymap.")
264
265(defvar ctl-x-4-map (make-sparse-keymap)
266 "Keymap for subcommands of C-x 4")
059184dd 267(defalias 'ctl-x-4-prefix ctl-x-4-map)
9a5336ae
JB
268(define-key ctl-x-map "4" 'ctl-x-4-prefix)
269
270(defvar ctl-x-5-map (make-sparse-keymap)
271 "Keymap for frame commands.")
059184dd 272(defalias 'ctl-x-5-prefix ctl-x-5-map)
9a5336ae
JB
273(define-key ctl-x-map "5" 'ctl-x-5-prefix)
274
0f03054a 275\f
9a5336ae
JB
276;;;; Event manipulation functions.
277
da16e648
KH
278;; The call to `read' is to ensure that the value is computed at load time
279;; and not compiled into the .elc file. The value is negative on most
280;; machines, but not on all!
281(defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
114137b8 282
cde6d7e3
RS
283(defun listify-key-sequence (key)
284 "Convert a key sequence to a list of events."
285 (if (vectorp key)
286 (append key nil)
287 (mapcar (function (lambda (c)
288 (if (> c 127)
114137b8 289 (logxor c listify-key-sequence-1)
cde6d7e3
RS
290 c)))
291 (append key nil))))
292
53e5a4e8
RS
293(defsubst eventp (obj)
294 "True if the argument is an event object."
295 (or (integerp obj)
296 (and (symbolp obj)
297 (get obj 'event-symbol-elements))
298 (and (consp obj)
299 (symbolp (car obj))
300 (get (car obj) 'event-symbol-elements))))
301
302(defun event-modifiers (event)
303 "Returns a list of symbols representing the modifier keys in event EVENT.
304The elements of the list may include `meta', `control',
32295976
RS
305`shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
306and `down'."
53e5a4e8
RS
307 (let ((type event))
308 (if (listp type)
309 (setq type (car type)))
310 (if (symbolp type)
311 (cdr (get type 'event-symbol-elements))
312 (let ((list nil))
da16e648 313 (or (zerop (logand type ?\M-\^@))
53e5a4e8 314 (setq list (cons 'meta list)))
da16e648 315 (or (and (zerop (logand type ?\C-\^@))
53e5a4e8
RS
316 (>= (logand type 127) 32))
317 (setq list (cons 'control list)))
da16e648 318 (or (and (zerop (logand type ?\S-\^@))
53e5a4e8
RS
319 (= (logand type 255) (downcase (logand type 255))))
320 (setq list (cons 'shift list)))
da16e648 321 (or (zerop (logand type ?\H-\^@))
53e5a4e8 322 (setq list (cons 'hyper list)))
da16e648 323 (or (zerop (logand type ?\s-\^@))
53e5a4e8 324 (setq list (cons 'super list)))
da16e648 325 (or (zerop (logand type ?\A-\^@))
53e5a4e8
RS
326 (setq list (cons 'alt list)))
327 list))))
328
d63de416
RS
329(defun event-basic-type (event)
330 "Returns the basic type of the given event (all modifiers removed).
331The value is an ASCII printing character (not upper case) or a symbol."
2b0f4ba5
JB
332 (if (consp event)
333 (setq event (car event)))
d63de416
RS
334 (if (symbolp event)
335 (car (get event 'event-symbol-elements))
336 (let ((base (logand event (1- (lsh 1 18)))))
337 (downcase (if (< base 32) (logior base 64) base)))))
338
0f03054a
RS
339(defsubst mouse-movement-p (object)
340 "Return non-nil if OBJECT is a mouse movement event."
341 (and (consp object)
342 (eq (car object) 'mouse-movement)))
343
344(defsubst event-start (event)
345 "Return the starting position of EVENT.
346If EVENT is a mouse press or a mouse click, this returns the location
347of the event.
348If EVENT is a drag, this returns the drag's starting position.
349The return value is of the form
e55c21be 350 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
351The `posn-' functions access elements of such lists."
352 (nth 1 event))
353
354(defsubst event-end (event)
355 "Return the ending location of EVENT. EVENT should be a click or drag event.
356If EVENT is a click event, this function is the same as `event-start'.
357The return value is of the form
e55c21be 358 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 359The `posn-' functions access elements of such lists."
69b95560 360 (nth (if (consp (nth 2 event)) 2 1) event))
0f03054a 361
32295976
RS
362(defsubst event-click-count (event)
363 "Return the multi-click count of EVENT, a click or drag event.
364The return value is a positive integer."
365 (if (integerp (nth 2 event)) (nth 2 event) 1))
366
0f03054a
RS
367(defsubst posn-window (position)
368 "Return the window in POSITION.
369POSITION should be a list of the form
e55c21be 370 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
371as returned by the `event-start' and `event-end' functions."
372 (nth 0 position))
373
374(defsubst posn-point (position)
375 "Return the buffer location in POSITION.
376POSITION should be a list of the form
e55c21be 377 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 378as returned by the `event-start' and `event-end' functions."
15db4e0e
JB
379 (if (consp (nth 1 position))
380 (car (nth 1 position))
381 (nth 1 position)))
0f03054a 382
e55c21be
RS
383(defsubst posn-x-y (position)
384 "Return the x and y coordinates in POSITION.
0f03054a 385POSITION should be a list of the form
e55c21be 386 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
387as returned by the `event-start' and `event-end' functions."
388 (nth 2 position))
389
ed627e08 390(defun posn-col-row (position)
dbbcac56 391 "Return the column and row in POSITION, measured in characters.
e55c21be
RS
392POSITION should be a list of the form
393 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
ed627e08
RS
394as returned by the `event-start' and `event-end' functions.
395For a scroll-bar event, the result column is 0, and the row
396corresponds to the vertical position of the click in the scroll bar."
397 (let ((pair (nth 2 position))
398 (window (posn-window position)))
dbbcac56
KH
399 (if (eq (if (consp (nth 1 position))
400 (car (nth 1 position))
401 (nth 1 position))
ed627e08
RS
402 'vertical-scroll-bar)
403 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
dbbcac56
KH
404 (if (eq (if (consp (nth 1 position))
405 (car (nth 1 position))
406 (nth 1 position))
ed627e08
RS
407 'horizontal-scroll-bar)
408 (cons (scroll-bar-scale pair (window-width window)) 0)
9ba60df9
RS
409 (let* ((frame (if (framep window) window (window-frame window)))
410 (x (/ (car pair) (frame-char-width frame)))
411 (y (/ (cdr pair) (frame-char-height frame))))
ed627e08 412 (cons x y))))))
e55c21be 413
0f03054a
RS
414(defsubst posn-timestamp (position)
415 "Return the timestamp of POSITION.
416POSITION should be a list of the form
e55c21be 417 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
f415c00c 418as returned by the `event-start' and `event-end' functions."
0f03054a 419 (nth 3 position))
9a5336ae 420
0f03054a 421\f
9a5336ae
JB
422;;;; Obsolescent names for functions.
423
059184dd
ER
424(defalias 'dot 'point)
425(defalias 'dot-marker 'point-marker)
426(defalias 'dot-min 'point-min)
427(defalias 'dot-max 'point-max)
428(defalias 'window-dot 'window-point)
429(defalias 'set-window-dot 'set-window-point)
430(defalias 'read-input 'read-string)
431(defalias 'send-string 'process-send-string)
432(defalias 'send-region 'process-send-region)
433(defalias 'show-buffer 'set-window-buffer)
434(defalias 'buffer-flush-undo 'buffer-disable-undo)
435(defalias 'eval-current-buffer 'eval-buffer)
436(defalias 'compiled-function-p 'byte-code-function-p)
ae1cc031 437(defalias 'define-function 'defalias)
be9b65ac 438
9a5336ae
JB
439;; Some programs still use this as a function.
440(defun baud-rate ()
bcacc42c
RS
441 "Obsolete function returning the value of the `baud-rate' variable.
442Please convert your programs to use the variable `baud-rate' directly."
9a5336ae
JB
443 baud-rate)
444
0a5c0893
MB
445(defalias 'focus-frame 'ignore)
446(defalias 'unfocus-frame 'ignore)
9a5336ae
JB
447\f
448;;;; Alternate names for functions - these are not being phased out.
449
059184dd
ER
450(defalias 'string= 'string-equal)
451(defalias 'string< 'string-lessp)
452(defalias 'move-marker 'set-marker)
059184dd
ER
453(defalias 'not 'null)
454(defalias 'rplaca 'setcar)
455(defalias 'rplacd 'setcdr)
eb8c3be9 456(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
457(defalias 'indent-to-column 'indent-to)
458(defalias 'backward-delete-char 'delete-backward-char)
459(defalias 'search-forward-regexp (symbol-function 're-search-forward))
460(defalias 'search-backward-regexp (symbol-function 're-search-backward))
461(defalias 'int-to-string 'number-to-string)
1e0a78a1 462(defalias 'set-match-data 'store-match-data)
37f6661a
JB
463
464;;; Should this be an obsolete name? If you decide it should, you get
465;;; to go through all the sources and change them.
059184dd 466(defalias 'string-to-int 'string-to-number)
be9b65ac 467\f
9a5336ae 468;;;; Hook manipulation functions.
be9b65ac 469
0e4d378b
RS
470(defun make-local-hook (hook)
471 "Make the hook HOOK local to the current buffer.
472When a hook is local, its local and global values
473work in concert: running the hook actually runs all the hook
474functions listed in *either* the local value *or* the global value
475of the hook variable.
476
7dd1926e
RS
477This function works by making `t' a member of the buffer-local value,
478which acts as a flag to run the hook functions in the default value as
479well. This works for all normal hooks, but does not work for most
480non-normal hooks yet. We will be changing the callers of non-normal
481hooks so that they can handle localness; this has to be done one by
482one.
483
484This function does nothing if HOOK is already local in the current
485buffer.
0e4d378b
RS
486
487Do not use `make-local-variable' to make a hook variable buffer-local."
488 (if (local-variable-p hook)
489 nil
490 (or (boundp hook) (set hook nil))
491 (make-local-variable hook)
492 (set hook (list t))))
493
494(defun add-hook (hook function &optional append local)
32295976
RS
495 "Add to the value of HOOK the function FUNCTION.
496FUNCTION is not added if already present.
497FUNCTION is added (if necessary) at the beginning of the hook list
498unless the optional argument APPEND is non-nil, in which case
499FUNCTION is added at the end.
500
0e4d378b
RS
501The optional fourth argument, LOCAL, if non-nil, says to modify
502the hook's buffer-local value rather than its default value.
503This makes no difference if the hook is not buffer-local.
504To make a hook variable buffer-local, always use
505`make-local-hook', not `make-local-variable'.
506
32295976
RS
507HOOK should be a symbol, and FUNCTION may be any valid function. If
508HOOK is void, it is first set to nil. If HOOK's value is a single
aa09b5ca 509function, it is changed to a list of functions."
be9b65ac 510 (or (boundp hook) (set hook nil))
0e4d378b 511 (or (default-boundp hook) (set-default hook nil))
32295976
RS
512 ;; If the hook value is a single function, turn it into a list.
513 (let ((old (symbol-value hook)))
514 (if (or (not (listp old)) (eq (car old) 'lambda))
515 (set hook (list old))))
f4e5bca5
RS
516 (if (or local
517 ;; Detect the case where make-local-variable was used on a hook
518 ;; and do what we used to do.
cd2db344 519 (and (local-variable-if-set-p hook)
f4e5bca5 520 (not (memq t (symbol-value hook)))))
0e4d378b
RS
521 ;; Alter the local value only.
522 (or (if (consp function)
523 (member function (symbol-value hook))
524 (memq function (symbol-value hook)))
525 (set hook
526 (if append
527 (append (symbol-value hook) (list function))
528 (cons function (symbol-value hook)))))
529 ;; Alter the global value (which is also the only value,
530 ;; if the hook doesn't have a local value).
531 (or (if (consp function)
532 (member function (default-value hook))
533 (memq function (default-value hook)))
534 (set-default hook
535 (if append
536 (append (default-value hook) (list function))
537 (cons function (default-value hook)))))))
538
539(defun remove-hook (hook function &optional local)
24980d16
RS
540 "Remove from the value of HOOK the function FUNCTION.
541HOOK should be a symbol, and FUNCTION may be any valid function. If
542FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
0e4d378b
RS
543list of hooks to run in HOOK, then nothing is done. See `add-hook'.
544
545The optional third argument, LOCAL, if non-nil, says to modify
546the hook's buffer-local value rather than its default value.
547This makes no difference if the hook is not buffer-local.
548To make a hook variable buffer-local, always use
549`make-local-hook', not `make-local-variable'."
24980d16 550 (if (or (not (boundp hook)) ;unbound symbol, or
0e4d378b 551 (not (default-boundp 'hook))
24980d16
RS
552 (null (symbol-value hook)) ;value is nil, or
553 (null function)) ;function is nil, then
554 nil ;Do nothing.
f4e5bca5
RS
555 (if (or local
556 ;; Detect the case where make-local-variable was used on a hook
557 ;; and do what we used to do.
558 (and (local-variable-p hook)
559 (not (memq t (symbol-value hook)))))
0e4d378b
RS
560 (let ((hook-value (symbol-value hook)))
561 (if (consp hook-value)
562 (if (member function hook-value)
563 (setq hook-value (delete function (copy-sequence hook-value))))
564 (if (equal hook-value function)
565 (setq hook-value nil)))
566 (set hook hook-value))
567 (let ((hook-value (default-value hook)))
568 (if (consp hook-value)
569 (if (member function hook-value)
570 (setq hook-value (delete function (copy-sequence hook-value))))
571 (if (equal hook-value function)
572 (setq hook-value nil)))
573 (set-default hook hook-value)))))
6e3af630
RS
574
575(defun add-to-list (list-var element)
8851c1f0 576 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
9f0b1f09 577The test for presence of ELEMENT is done with `equal'.
8851c1f0
RS
578If you want to use `add-to-list' on a variable that is not defined
579until a certain package is loaded, you should put the call to `add-to-list'
580into a hook function that will be run only after loading the package.
581`eval-after-load' provides one way to do this. In some cases
582other hooks, such as major mode hooks, can do the job."
6e3af630
RS
583 (or (member element (symbol-value list-var))
584 (set list-var (cons element (symbol-value list-var)))))
be9b65ac 585\f
9a5336ae
JB
586;;;; Specifying things to do after certain files are loaded.
587
588(defun eval-after-load (file form)
589 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
590This makes or adds to an entry on `after-load-alist'.
90914938 591If FILE is already loaded, evaluate FORM right now.
12c7071c 592It does nothing if FORM is already on the list for FILE.
9a5336ae 593FILE should be the name of a library, with no directory name."
90914938 594 ;; Make sure there is an element for FILE.
9a5336ae
JB
595 (or (assoc file after-load-alist)
596 (setq after-load-alist (cons (list file) after-load-alist)))
90914938 597 ;; Add FORM to the element if it isn't there.
12c7071c
RS
598 (let ((elt (assoc file after-load-alist)))
599 (or (member form (cdr elt))
90914938
RS
600 (progn
601 (nconc elt (list form))
602 ;; If the file has been loaded already, run FORM right away.
603 (and (assoc file load-history)
604 (eval form)))))
9a5336ae
JB
605 form)
606
607(defun eval-next-after-load (file)
608 "Read the following input sexp, and run it whenever FILE is loaded.
609This makes or adds to an entry on `after-load-alist'.
610FILE should be the name of a library, with no directory name."
611 (eval-after-load file (read)))
612
613\f
614;;;; Input and display facilities.
615
616(defun read-quoted-char (&optional prompt)
617 "Like `read-char', except that if the first character read is an octal
618digit, we read up to two more octal digits and return the character
619represented by the octal number consisting of those digits.
620Optional argument PROMPT specifies a string to use to prompt the user."
1219a2a4 621 (let ((message-log-max nil) (count 0) (code 0) char)
9a5336ae
JB
622 (while (< count 3)
623 (let ((inhibit-quit (zerop count))
42e636f0
KH
624 ;; Don't let C-h get the help message--only help function keys.
625 (help-char nil)
626 (help-form
627 "Type the special character you want to use,
628or three octal digits representing its character code."))
9a5336ae
JB
629 (and prompt (message "%s-" prompt))
630 (setq char (read-char))
631 (if inhibit-quit (setq quit-flag nil)))
632 (cond ((null char))
633 ((and (<= ?0 char) (<= char ?7))
634 (setq code (+ (* code 8) (- char ?0))
635 count (1+ count))
91a6acc3 636 (and prompt (setq prompt (message "%s %c" prompt char))))
9a5336ae
JB
637 ((> count 0)
638 (setq unread-command-events (list char) count 259))
639 (t (setq code char count 259))))
0342b545 640 ;; Turn a meta-character into a character with the 0200 bit set.
1219a2a4 641 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
0342b545 642 (logand 255 code))))
9a5336ae
JB
643
644(defun force-mode-line-update (&optional all)
645 "Force the mode-line of the current buffer to be redisplayed.
7ec2a18c 646With optional non-nil ALL, force redisplay of all mode-lines."
9a5336ae
JB
647 (if all (save-excursion (set-buffer (other-buffer))))
648 (set-buffer-modified-p (buffer-modified-p)))
649
be9b65ac
DL
650(defun momentary-string-display (string pos &optional exit-char message)
651 "Momentarily display STRING in the buffer at POS.
652Display remains until next character is typed.
653If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
654otherwise it is then available as input (as a command if nothing else).
655Display MESSAGE (optional fourth arg) in the echo area.
656If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
657 (or exit-char (setq exit-char ?\ ))
658 (let ((buffer-read-only nil)
ca2ec1c5
RS
659 ;; Don't modify the undo list at all.
660 (buffer-undo-list t)
be9b65ac
DL
661 (modified (buffer-modified-p))
662 (name buffer-file-name)
663 insert-end)
664 (unwind-protect
665 (progn
666 (save-excursion
667 (goto-char pos)
668 ;; defeat file locking... don't try this at home, kids!
669 (setq buffer-file-name nil)
670 (insert-before-markers string)
3eec84bf
RS
671 (setq insert-end (point))
672 ;; If the message end is off screen, recenter now.
673 (if (> (window-end) insert-end)
674 (recenter (/ (window-height) 2)))
675 ;; If that pushed message start off the screen,
676 ;; scroll to start it at the top of the screen.
677 (move-to-window-line 0)
678 (if (> (point) pos)
679 (progn
680 (goto-char pos)
681 (recenter 0))))
be9b65ac
DL
682 (message (or message "Type %s to continue editing.")
683 (single-key-description exit-char))
3547c855 684 (let ((char (read-event)))
be9b65ac 685 (or (eq char exit-char)
dbc4e1c1 686 (setq unread-command-events (list char)))))
be9b65ac
DL
687 (if insert-end
688 (save-excursion
689 (delete-region pos insert-end)))
690 (setq buffer-file-name name)
691 (set-buffer-modified-p modified))))
692
9a5336ae
JB
693\f
694;;;; Miscellanea.
695
448b61c9
RS
696;; A number of major modes set this locally.
697;; Give it a global value to avoid compiler warnings.
698(defvar font-lock-defaults nil)
699
700;; Avoid compiler warnings about this variable,
701;; which has a special meaning on certain system types.
702(defvar buffer-file-type nil
703 "Non-nil if the visited file is a binary file.
704This variable is meaningful on MS-DOG and Windows NT.
705On those systems, it is automatically local in every buffer.
706On other systems, this variable is normally always nil.")
707
a860d25f 708;; This should probably be written in C (i.e., without using `walk-windows').
63503b24 709(defun get-buffer-window-list (buffer &optional minibuf frame)
a860d25f 710 "Return windows currently displaying BUFFER, or nil if none.
63503b24 711See `walk-windows' for the meaning of MINIBUF and FRAME."
43c5ac8c 712 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
a860d25f
SM
713 (walk-windows (function (lambda (window)
714 (if (eq (window-buffer window) buffer)
715 (setq windows (cons window windows)))))
63503b24 716 minibuf frame)
a860d25f
SM
717 windows))
718
f9269e19
RS
719(defun ignore (&rest ignore)
720 "Do nothing and return nil.
721This function accepts any number of arguments, but ignores them."
c0f1a4f6 722 (interactive)
9a5336ae
JB
723 nil)
724
725(defun error (&rest args)
aa308ce2
RS
726 "Signal an error, making error message by passing all args to `format'.
727In Emacs, the convention is that error messages start with a capital
728letter but *do not* end with a period. Please follow this convention
729for the sake of consistency."
9a5336ae
JB
730 (while t
731 (signal 'error (list (apply 'format args)))))
732
cef7ae6e 733(defalias 'user-original-login-name 'user-login-name)
9a5336ae 734
be9b65ac
DL
735(defun start-process-shell-command (name buffer &rest args)
736 "Start a program in a subprocess. Return the process object for it.
737Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
738NAME is name for process. It is modified if necessary to make it unique.
739BUFFER is the buffer or (buffer-name) to associate with the process.
740 Process output goes at end of that buffer, unless you specify
741 an output stream or filter function to handle the output.
742 BUFFER may be also nil, meaning that this process is not associated
743 with any buffer
744Third arg is command name, the name of a shell command.
745Remaining arguments are the arguments for the command.
4f1d6310 746Wildcards and redirection are handled as usual in the shell."
a247bf21
KH
747 (cond
748 ((eq system-type 'vax-vms)
749 (apply 'start-process name buffer args))
b59f6d7a
RS
750 ;; We used to use `exec' to replace the shell with the command,
751 ;; but that failed to handle (...) and semicolon, etc.
a247bf21
KH
752 (t
753 (start-process name buffer shell-file-name shell-command-switch
b59f6d7a 754 (mapconcat 'identity args " ")))))
a7ed4c2a 755\f
a7f284ec
RS
756(defmacro with-current-buffer (buffer &rest body)
757 "Execute the forms in BODY with BUFFER as the current buffer.
a2fdb55c
EN
758The value returned is the value of the last form in BODY.
759See also `with-temp-buffer'."
a7f284ec
RS
760 `(save-current-buffer
761 (set-buffer ,buffer)
a2fdb55c 762 ,@body))
a7f284ec 763
a7ed4c2a
RS
764(defmacro with-temp-file (file &rest forms)
765 "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
a2fdb55c
EN
766The value of the last form in FORMS is returned, like `progn'.
767See also `with-temp-buffer'."
a7ed4c2a 768 (let ((temp-file (make-symbol "temp-file"))
a2fdb55c
EN
769 (temp-buffer (make-symbol "temp-buffer")))
770 `(let ((,temp-file ,file)
771 (,temp-buffer
772 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
773 (unwind-protect
774 (prog1
775 (with-current-buffer ,temp-buffer
776 ,@forms)
777 (with-current-buffer ,temp-buffer
778 (widen)
779 (write-region (point-min) (point-max) ,temp-file nil 0)))
780 (and (buffer-name ,temp-buffer)
781 (kill-buffer ,temp-buffer))))))
782
783(defmacro with-temp-buffer (&rest forms)
784 "Create a temporary buffer, and evaluate FORMS there like `progn'.
785See also `with-temp-file' and `with-output-to-string'."
786 (let ((temp-buffer (make-symbol "temp-buffer")))
787 `(let ((,temp-buffer
788 (get-buffer-create (generate-new-buffer-name " *temp*"))))
789 (unwind-protect
790 (with-current-buffer ,temp-buffer
791 ,@forms)
792 (and (buffer-name ,temp-buffer)
793 (kill-buffer ,temp-buffer))))))
794
5db7925d
RS
795(defmacro with-output-to-string (&rest body)
796 "Execute BODY, return the text it sent to `standard-output', as a string."
a2fdb55c
EN
797 `(let ((standard-output
798 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
5db7925d
RS
799 (let ((standard-output standard-output))
800 ,@body)
a2fdb55c
EN
801 (with-current-buffer standard-output
802 (prog1
803 (buffer-string)
804 (kill-buffer nil)))))
2ec9c94e
RS
805
806(defmacro combine-after-change-calls (&rest body)
807 "Execute BODY, but don't call the after-change functions till the end.
808If BODY makes changes in the buffer, they are recorded
809and the functions on `after-change-functions' are called several times
810when BODY is finished.
31aa282e 811The return value is the value of the last form in BODY.
2ec9c94e
RS
812
813If `before-change-functions' is non-nil, then calls to the after-change
814functions can't be deferred, so in that case this macro has no effect.
815
816Do not alter `after-change-functions' or `before-change-functions'
817in BODY."
818 `(unwind-protect
819 (let ((combine-after-change-calls t))
820 . ,body)
821 (combine-after-change-execute)))
822
a2fdb55c 823\f
c7ca41e6
RS
824(defvar save-match-data-internal)
825
826;; We use save-match-data-internal as the local variable because
827;; that works ok in practice (people should not use that variable elsewhere).
828;; We used to use an uninterned symbol; the compiler handles that properly
829;; now, but it generates slower code.
9a5336ae
JB
830(defmacro save-match-data (&rest body)
831 "Execute the BODY forms, restoring the global value of the match data."
9fc0eb95 832 `(let ((save-match-data-internal (match-data)))
c7ca41e6
RS
833 (unwind-protect
834 (progn ,@body)
ecc06779 835 (store-match-data save-match-data-internal))))
993713ce 836
cd323f89 837(defun match-string (num &optional string)
993713ce
SM
838 "Return string of text matched by last search.
839NUM specifies which parenthesized expression in the last regexp.
840 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
841Zero means the entire text matched by the whole regexp or whole string.
842STRING should be given if the last search was by `string-match' on STRING."
cd323f89
SM
843 (if (match-beginning num)
844 (if string
845 (substring string (match-beginning num) (match-end num))
846 (buffer-substring (match-beginning num) (match-end num)))))
58f950b4 847
edce3654
RS
848(defun split-string (string &optional separators)
849 "Splits STRING into substrings where there are matches for SEPARATORS.
850Each match for SEPARATORS is a splitting point.
851The substrings between the splitting points are made into a list
852which is returned.
853If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
854 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
855 (start 0)
856 (list nil))
857 (while (string-match rexp string start)
7eb47123 858 (or (eq (match-beginning 0) 0)
edce3654
RS
859 (setq list
860 (cons (substring string start (match-beginning 0))
861 list)))
862 (setq start (match-end 0)))
863 (or (eq start (length string))
864 (setq list
865 (cons (substring string start)
866 list)))
867 (nreverse list)))
a7ed4c2a 868\f
8af7df60
RS
869(defun shell-quote-argument (argument)
870 "Quote an argument for passing as argument to an inferior shell."
c1c74b43
RS
871 (if (eq system-type 'ms-dos)
872 ;; MS-DOS shells don't have quoting, so don't do any.
873 argument
874 (if (eq system-type 'windows-nt)
875 (concat "\"" argument "\"")
876 ;; Quote everything except POSIX filename characters.
877 ;; This should be safe enough even for really weird shells.
878 (let ((result "") (start 0) end)
879 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
880 (setq end (match-beginning 0)
881 result (concat result (substring argument start end)
882 "\\" (substring argument end (1+ end)))
883 start (1+ end)))
884 (concat result (substring argument start))))))
8af7df60 885
297d863b 886(defun make-syntax-table (&optional oldtable)
984f718a
RS
887 "Return a new syntax table.
888It inherits all letters and control characters from the standard
889syntax table; other characters are copied from the standard syntax table."
297d863b
KH
890 (if oldtable
891 (copy-syntax-table oldtable)
892 (let ((table (copy-syntax-table))
893 i)
894 (setq i 0)
895 (while (<= i 31)
a6889c57 896 (aset table i nil)
297d863b
KH
897 (setq i (1+ i)))
898 (setq i ?A)
899 (while (<= i ?Z)
a6889c57 900 (aset table i nil)
297d863b
KH
901 (setq i (1+ i)))
902 (setq i ?a)
903 (while (<= i ?z)
a6889c57 904 (aset table i nil)
297d863b
KH
905 (setq i (1+ i)))
906 (setq i 128)
907 (while (<= i 255)
a6889c57 908 (aset table i nil)
297d863b
KH
909 (setq i (1+ i)))
910 table)))
31aa282e
KH
911
912(defun add-to-invisibility-spec (arg)
913 "Add elements to `buffer-invisibility-spec'.
914See documentation for `buffer-invisibility-spec' for the kind of elements
915that can be added."
916 (cond
917 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
918 (setq buffer-invisibility-spec (list arg)))
919 (t
11d431ad
KH
920 (setq buffer-invisibility-spec
921 (cons arg buffer-invisibility-spec)))))
31aa282e
KH
922
923(defun remove-from-invisibility-spec (arg)
924 "Remove elements from `buffer-invisibility-spec'."
925 (if buffer-invisibility-spec
071a2a71 926 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
baed0109
RS
927\f
928(defun global-set-key (key command)
929 "Give KEY a global binding as COMMAND.
930COMMAND is a symbol naming an interactively-callable function.
931KEY is a key sequence (a string or vector of characters or event types).
932Non-ASCII characters with codes above 127 (such as ISO Latin-1)
933can be included if you use a vector.
934Note that if KEY has a local binding in the current buffer
935that local binding will continue to shadow any global binding."
936 (interactive "KSet key globally: \nCSet key %s to command: ")
937 (or (vectorp key) (stringp key)
938 (signal 'wrong-type-argument (list 'arrayp key)))
939 (define-key (current-global-map) key command)
940 nil)
941
942(defun local-set-key (key command)
943 "Give KEY a local binding as COMMAND.
944COMMAND is a symbol naming an interactively-callable function.
945KEY is a key sequence (a string or vector of characters or event types).
946Non-ASCII characters with codes above 127 (such as ISO Latin-1)
947can be included if you use a vector.
948The binding goes in the current buffer's local map,
949which in most cases is shared with all other buffers in the same major mode."
950 (interactive "KSet key locally: \nCSet key %s locally to command: ")
951 (let ((map (current-local-map)))
952 (or map
953 (use-local-map (setq map (make-sparse-keymap))))
954 (or (vectorp key) (stringp key)
955 (signal 'wrong-type-argument (list 'arrayp key)))
956 (define-key map key command))
957 nil)
984f718a 958
baed0109
RS
959(defun global-unset-key (key)
960 "Remove global binding of KEY.
961KEY is a string representing a sequence of keystrokes."
962 (interactive "kUnset key globally: ")
963 (global-set-key key nil))
964
db2474b8 965(defun local-unset-key (key)
baed0109
RS
966 "Remove local binding of KEY.
967KEY is a string representing a sequence of keystrokes."
968 (interactive "kUnset key locally: ")
969 (if (current-local-map)
db2474b8 970 (local-set-key key nil))
baed0109
RS
971 nil)
972\f
4809d0dd
KH
973;; We put this here instead of in frame.el so that it's defined even on
974;; systems where frame.el isn't loaded.
975(defun frame-configuration-p (object)
976 "Return non-nil if OBJECT seems to be a frame configuration.
977Any list whose car is `frame-configuration' is assumed to be a frame
978configuration."
979 (and (consp object)
980 (eq (car object) 'frame-configuration)))
981
a9a44ed1
RS
982(defun functionp (object)
983 "Non-nil of OBJECT is a type of object that can be called as a function."
984 (or (subrp object) (compiled-function-p object)
985 (eq (car-safe object) 'lambda)
986 (and (symbolp object) (fboundp object))))
987
9a5336ae
JB
988;; now in fns.c
989;(defun nth (n list)
990; "Returns the Nth element of LIST.
991;N counts from zero. If LIST is not that long, nil is returned."
992; (car (nthcdr n list)))
993;
994;(defun copy-alist (alist)
995; "Return a copy of ALIST.
996;This is a new alist which represents the same mapping
997;from objects to objects, but does not share the alist structure with ALIST.
998;The objects mapped (cars and cdrs of elements of the alist)
999;are shared, however."
1000; (setq alist (copy-sequence alist))
1001; (let ((tail alist))
1002; (while tail
1003; (if (consp (car tail))
1004; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1005; (setq tail (cdr tail))))
1006; alist)
630cc463
ER
1007
1008;;; subr.el ends here