(custom-save-delete): Don't delete whitespace
[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:
77a5664f
RS
23(defvar custom-declare-variable-list nil
24 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
25Each element of this list holds the arguments to one call to `defcustom'.")
26
68e3e5f5 27;; Use this, rather than defcustom, in subr.el and other files loaded
77a5664f
RS
28;; before custom.el.
29(defun custom-declare-variable-early (&rest arguments)
30 (setq custom-declare-variable-list
31 (cons arguments custom-declare-variable-list)))
9a5336ae
JB
32\f
33;;;; Lisp language features.
34
35(defmacro lambda (&rest cdr)
36 "Return a lambda expression.
37A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
38self-quoting; the result of evaluating the lambda expression is the
39expression itself. The lambda expression may then be treated as a
bec0d7f9
RS
40function, i.e., stored as the function value of a symbol, passed to
41funcall or mapcar, etc.
42
9a5336ae 43ARGS should take the same form as an argument list for a `defun'.
8fd68088
RS
44DOCSTRING is an optional documentation string.
45 If present, it should describe how to call the function.
46 But documentation strings are usually not useful in nameless functions.
9a5336ae
JB
47INTERACTIVE should be a call to the function `interactive', which see.
48It may also be omitted.
49BODY should be a list of lisp expressions."
50 ;; Note that this definition should not use backquotes; subr.el should not
51 ;; depend on backquote.el.
52 (list 'function (cons 'lambda cdr)))
53
debff3c3
RS
54(defmacro when (cond &rest body)
55 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
56 (list 'if cond (cons 'progn body)))
11d431ad
KH
57(put 'when 'lisp-indent-function 1)
58(put 'when 'edebug-form-spec '(&rest form))
9a5336ae 59
debff3c3
RS
60(defmacro unless (cond &rest body)
61 "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
62 (cons 'if (cons cond (cons nil body))))
11d431ad
KH
63(put 'unless 'lisp-indent-function 1)
64(put 'unless 'edebug-form-spec '(&rest form))
d370591d
RS
65
66(defsubst caar (x)
67 "Return the car of the car of X."
68 (car (car x)))
69
70(defsubst cadr (x)
71 "Return the car of the cdr of X."
72 (car (cdr x)))
73
74(defsubst cdar (x)
75 "Return the cdr of the car of X."
76 (cdr (car x)))
77
78(defsubst cddr (x)
79 "Return the cdr of the cdr of X."
80 (cdr (cdr x)))
e8c32c99 81
369fba5f
RS
82(defun last (x &optional n)
83 "Return the last link of the list X. Its car is the last element.
84If X is nil, return nil.
85If N is non-nil, return the Nth-to-last link of X.
86If N is bigger than the length of X, return X."
87 (if n
88 (let ((m 0) (p x))
89 (while (consp p)
90 (setq m (1+ m) p (cdr p)))
91 (if (<= n 0) p
92 (if (< n m) (nthcdr (- m n) x) x)))
93 (while (cdr x)
94 (setq x (cdr x)))
95 x))
526d204e 96
8a288450
RS
97(defun assoc-default (key alist &optional test default)
98 "Find object KEY in a pseudo-alist ALIST.
99ALIST is a list of conses or objects. Each element (or the element's car,
100if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
101If that is non-nil, the element matches;
102then `assoc-default' returns the element's cdr, if it is a cons,
526d204e 103or DEFAULT if the element is not a cons.
8a288450
RS
104
105If no element matches, the value is nil.
106If TEST is omitted or nil, `equal' is used."
107 (let (found (tail alist) value)
108 (while (and tail (not found))
109 (let ((elt (car tail)))
110 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
111 (setq found t value (if (consp elt) (cdr elt) default))))
112 (setq tail (cdr tail)))
113 value))
9a5336ae 114\f
9a5336ae 115;;;; Keymap support.
be9b65ac
DL
116
117(defun undefined ()
118 (interactive)
119 (ding))
120
121;Prevent the \{...} documentation construct
122;from mentioning keys that run this command.
123(put 'undefined 'suppress-keymap t)
124
125(defun suppress-keymap (map &optional nodigits)
126 "Make MAP override all normally self-inserting keys to be undefined.
127Normally, as an exception, digits and minus-sign are set to make prefix args,
128but optional second arg NODIGITS non-nil treats them like other chars."
80e7b471 129 (substitute-key-definition 'self-insert-command 'undefined map global-map)
be9b65ac
DL
130 (or nodigits
131 (let (loop)
132 (define-key map "-" 'negative-argument)
133 ;; Make plain numbers do numeric args.
134 (setq loop ?0)
135 (while (<= loop ?9)
136 (define-key map (char-to-string loop) 'digit-argument)
137 (setq loop (1+ loop))))))
138
be9b65ac
DL
139;Moved to keymap.c
140;(defun copy-keymap (keymap)
141; "Return a copy of KEYMAP"
142; (while (not (keymapp keymap))
143; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
144; (if (vectorp keymap)
145; (copy-sequence keymap)
146; (copy-alist keymap)))
147
f14dbba7
KH
148(defvar key-substitution-in-progress nil
149 "Used internally by substitute-key-definition.")
150
7f2c2edd 151(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
152 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
153In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
154If optional fourth argument OLDMAP is specified, we redefine
155in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
156 (or prefix (setq prefix ""))
157 (let* ((scan (or oldmap keymap))
158 (vec1 (vector nil))
f14dbba7
KH
159 (prefix1 (vconcat prefix vec1))
160 (key-substitution-in-progress
161 (cons scan key-substitution-in-progress)))
7f2c2edd
RS
162 ;; Scan OLDMAP, finding each char or event-symbol that
163 ;; has any definition, and act on it with hack-key.
164 (while (consp scan)
165 (if (consp (car scan))
166 (let ((char (car (car scan)))
167 (defn (cdr (car scan))))
168 ;; The inside of this let duplicates exactly
169 ;; the inside of the following let that handles array elements.
170 (aset vec1 0 char)
171 (aset prefix1 (length prefix) char)
44d798af 172 (let (inner-def skipped)
7f2c2edd
RS
173 ;; Skip past menu-prompt.
174 (while (stringp (car-safe defn))
44d798af 175 (setq skipped (cons (car defn) skipped))
7f2c2edd 176 (setq defn (cdr defn)))
e025dddf
RS
177 ;; Skip past cached key-equivalence data for menu items.
178 (and (consp defn) (consp (car defn))
179 (setq defn (cdr defn)))
7f2c2edd 180 (setq inner-def defn)
e025dddf 181 ;; Look past a symbol that names a keymap.
7f2c2edd
RS
182 (while (and (symbolp inner-def)
183 (fboundp inner-def))
184 (setq inner-def (symbol-function inner-def)))
328a37ec
RS
185 (if (or (eq defn olddef)
186 ;; Compare with equal if definition is a key sequence.
187 ;; That is useful for operating on function-key-map.
188 (and (or (stringp defn) (vectorp defn))
189 (equal defn olddef)))
44d798af 190 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
f14dbba7 191 (if (and (keymapp defn)
350b7567
RS
192 ;; Avoid recursively scanning
193 ;; where KEYMAP does not have a submap.
afd9831b
RS
194 (let ((elt (lookup-key keymap prefix1)))
195 (or (null elt)
196 (keymapp elt)))
350b7567 197 ;; Avoid recursively rescanning keymap being scanned.
f14dbba7
KH
198 (not (memq inner-def
199 key-substitution-in-progress)))
e025dddf
RS
200 ;; If this one isn't being scanned already,
201 ;; scan it now.
7f2c2edd
RS
202 (substitute-key-definition olddef newdef keymap
203 inner-def
204 prefix1)))))
916cc49f 205 (if (vectorp (car scan))
7f2c2edd
RS
206 (let* ((array (car scan))
207 (len (length array))
208 (i 0))
209 (while (< i len)
210 (let ((char i) (defn (aref array i)))
211 ;; The inside of this let duplicates exactly
212 ;; the inside of the previous let.
213 (aset vec1 0 char)
214 (aset prefix1 (length prefix) char)
44d798af 215 (let (inner-def skipped)
7f2c2edd
RS
216 ;; Skip past menu-prompt.
217 (while (stringp (car-safe defn))
44d798af 218 (setq skipped (cons (car defn) skipped))
7f2c2edd 219 (setq defn (cdr defn)))
e025dddf
RS
220 (and (consp defn) (consp (car defn))
221 (setq defn (cdr defn)))
7f2c2edd
RS
222 (setq inner-def defn)
223 (while (and (symbolp inner-def)
224 (fboundp inner-def))
225 (setq inner-def (symbol-function inner-def)))
328a37ec
RS
226 (if (or (eq defn olddef)
227 (and (or (stringp defn) (vectorp defn))
228 (equal defn olddef)))
44d798af
RS
229 (define-key keymap prefix1
230 (nconc (nreverse skipped) newdef))
f14dbba7 231 (if (and (keymapp defn)
afd9831b
RS
232 (let ((elt (lookup-key keymap prefix1)))
233 (or (null elt)
234 (keymapp elt)))
f14dbba7
KH
235 (not (memq inner-def
236 key-substitution-in-progress)))
7f2c2edd
RS
237 (substitute-key-definition olddef newdef keymap
238 inner-def
239 prefix1)))))
97fd9abf
RS
240 (setq i (1+ i))))
241 (if (char-table-p (car scan))
242 (map-char-table
243 (function (lambda (char defn)
244 (let ()
245 ;; The inside of this let duplicates exactly
246 ;; the inside of the previous let,
247 ;; except that it uses set-char-table-range
248 ;; instead of define-key.
249 (aset vec1 0 char)
250 (aset prefix1 (length prefix) char)
251 (let (inner-def skipped)
252 ;; Skip past menu-prompt.
253 (while (stringp (car-safe defn))
254 (setq skipped (cons (car defn) skipped))
255 (setq defn (cdr defn)))
256 (and (consp defn) (consp (car defn))
257 (setq defn (cdr defn)))
258 (setq inner-def defn)
259 (while (and (symbolp inner-def)
260 (fboundp inner-def))
261 (setq inner-def (symbol-function inner-def)))
262 (if (or (eq defn olddef)
263 (and (or (stringp defn) (vectorp defn))
264 (equal defn olddef)))
9a5114ac
RS
265 (define-key keymap prefix1
266 (nconc (nreverse skipped) newdef))
97fd9abf
RS
267 (if (and (keymapp defn)
268 (let ((elt (lookup-key keymap prefix1)))
269 (or (null elt)
270 (keymapp elt)))
271 (not (memq inner-def
272 key-substitution-in-progress)))
273 (substitute-key-definition olddef newdef keymap
274 inner-def
275 prefix1)))))))
276 (car scan)))))
7f2c2edd 277 (setq scan (cdr scan)))))
9a5336ae 278
06ae9cf2 279(defun define-key-after (keymap key definition after)
4434d61b
RS
280 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
281This is like `define-key' except that the binding for KEY is placed
282just after the binding for the event AFTER, instead of at the beginning
c34a9d34
RS
283of the map. Note that AFTER must be an event type (like KEY), NOT a command
284\(like DEFINITION).
285
286If AFTER is t, the new binding goes at the end of the keymap.
287
9ed287b0 288KEY must contain just one event type--that is to say, it must be
c34a9d34
RS
289a string or vector of length 1.
290
291The order of bindings in a keymap matters when it is used as a menu."
292
4434d61b
RS
293 (or (keymapp keymap)
294 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 295 (if (> (length key) 1)
626f67f3 296 (error "multi-event key specified in `define-key-after'"))
113d28a8 297 (let ((tail keymap) done inserted
4434d61b
RS
298 (first (aref key 0)))
299 (while (and (not done) tail)
300 ;; Delete any earlier bindings for the same key.
301 (if (eq (car-safe (car (cdr tail))) first)
302 (setcdr tail (cdr (cdr tail))))
303 ;; When we reach AFTER's binding, insert the new binding after.
304 ;; If we reach an inherited keymap, insert just before that.
113d28a8 305 ;; If we reach the end of this keymap, insert at the end.
c34a9d34
RS
306 (if (or (and (eq (car-safe (car tail)) after)
307 (not (eq after t)))
113d28a8
RS
308 (eq (car (cdr tail)) 'keymap)
309 (null (cdr tail)))
4434d61b 310 (progn
113d28a8
RS
311 ;; Stop the scan only if we find a parent keymap.
312 ;; Keep going past the inserted element
313 ;; so we can delete any duplications that come later.
314 (if (eq (car (cdr tail)) 'keymap)
315 (setq done t))
316 ;; Don't insert more than once.
317 (or inserted
318 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
319 (setq inserted t)))
4434d61b
RS
320 (setq tail (cdr tail)))))
321
d128fe85
RS
322(defmacro kbd (keys)
323 "Convert KEYS to the internal Emacs key representation.
324KEYS should be a string constant in the format used for
325saving keyboard macros (see `insert-kbd-macro')."
326 (read-kbd-macro keys))
327
8bed5e3d
RS
328(put 'keyboard-translate-table 'char-table-extra-slots 0)
329
9a5336ae
JB
330(defun keyboard-translate (from to)
331 "Translate character FROM to TO at a low level.
332This function creates a `keyboard-translate-table' if necessary
333and then modifies one entry in it."
8bed5e3d
RS
334 (or (char-table-p keyboard-translate-table)
335 (setq keyboard-translate-table
336 (make-char-table 'keyboard-translate-table nil)))
9a5336ae
JB
337 (aset keyboard-translate-table from to))
338
339\f
340;;;; The global keymap tree.
341
342;;; global-map, esc-map, and ctl-x-map have their values set up in
343;;; keymap.c; we just give them docstrings here.
344
345(defvar global-map nil
346 "Default global keymap mapping Emacs keyboard input into commands.
347The value is a keymap which is usually (but not necessarily) Emacs's
348global map.")
349
350(defvar esc-map nil
351 "Default keymap for ESC (meta) commands.
352The normal global definition of the character ESC indirects to this keymap.")
353
354(defvar ctl-x-map nil
355 "Default keymap for C-x commands.
356The normal global definition of the character C-x indirects to this keymap.")
357
358(defvar ctl-x-4-map (make-sparse-keymap)
359 "Keymap for subcommands of C-x 4")
059184dd 360(defalias 'ctl-x-4-prefix ctl-x-4-map)
9a5336ae
JB
361(define-key ctl-x-map "4" 'ctl-x-4-prefix)
362
363(defvar ctl-x-5-map (make-sparse-keymap)
364 "Keymap for frame commands.")
059184dd 365(defalias 'ctl-x-5-prefix ctl-x-5-map)
9a5336ae
JB
366(define-key ctl-x-map "5" 'ctl-x-5-prefix)
367
0f03054a 368\f
9a5336ae
JB
369;;;; Event manipulation functions.
370
da16e648
KH
371;; The call to `read' is to ensure that the value is computed at load time
372;; and not compiled into the .elc file. The value is negative on most
373;; machines, but not on all!
374(defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
114137b8 375
cde6d7e3
RS
376(defun listify-key-sequence (key)
377 "Convert a key sequence to a list of events."
378 (if (vectorp key)
379 (append key nil)
380 (mapcar (function (lambda (c)
381 (if (> c 127)
114137b8 382 (logxor c listify-key-sequence-1)
cde6d7e3
RS
383 c)))
384 (append key nil))))
385
53e5a4e8
RS
386(defsubst eventp (obj)
387 "True if the argument is an event object."
388 (or (integerp obj)
389 (and (symbolp obj)
390 (get obj 'event-symbol-elements))
391 (and (consp obj)
392 (symbolp (car obj))
393 (get (car obj) 'event-symbol-elements))))
394
395(defun event-modifiers (event)
396 "Returns a list of symbols representing the modifier keys in event EVENT.
397The elements of the list may include `meta', `control',
32295976
RS
398`shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
399and `down'."
53e5a4e8
RS
400 (let ((type event))
401 (if (listp type)
402 (setq type (car type)))
403 (if (symbolp type)
404 (cdr (get type 'event-symbol-elements))
405 (let ((list nil))
da16e648 406 (or (zerop (logand type ?\M-\^@))
53e5a4e8 407 (setq list (cons 'meta list)))
da16e648 408 (or (and (zerop (logand type ?\C-\^@))
53e5a4e8
RS
409 (>= (logand type 127) 32))
410 (setq list (cons 'control list)))
da16e648 411 (or (and (zerop (logand type ?\S-\^@))
53e5a4e8
RS
412 (= (logand type 255) (downcase (logand type 255))))
413 (setq list (cons 'shift list)))
da16e648 414 (or (zerop (logand type ?\H-\^@))
53e5a4e8 415 (setq list (cons 'hyper list)))
da16e648 416 (or (zerop (logand type ?\s-\^@))
53e5a4e8 417 (setq list (cons 'super list)))
da16e648 418 (or (zerop (logand type ?\A-\^@))
53e5a4e8
RS
419 (setq list (cons 'alt list)))
420 list))))
421
d63de416
RS
422(defun event-basic-type (event)
423 "Returns the basic type of the given event (all modifiers removed).
424The value is an ASCII printing character (not upper case) or a symbol."
2b0f4ba5
JB
425 (if (consp event)
426 (setq event (car event)))
d63de416
RS
427 (if (symbolp event)
428 (car (get event 'event-symbol-elements))
429 (let ((base (logand event (1- (lsh 1 18)))))
430 (downcase (if (< base 32) (logior base 64) base)))))
431
0f03054a
RS
432(defsubst mouse-movement-p (object)
433 "Return non-nil if OBJECT is a mouse movement event."
434 (and (consp object)
435 (eq (car object) 'mouse-movement)))
436
437(defsubst event-start (event)
438 "Return the starting position of EVENT.
439If EVENT is a mouse press or a mouse click, this returns the location
440of the event.
441If EVENT is a drag, this returns the drag's starting position.
442The return value is of the form
e55c21be 443 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
444The `posn-' functions access elements of such lists."
445 (nth 1 event))
446
447(defsubst event-end (event)
448 "Return the ending location of EVENT. EVENT should be a click or drag event.
449If EVENT is a click event, this function is the same as `event-start'.
450The return value is of the form
e55c21be 451 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 452The `posn-' functions access elements of such lists."
69b95560 453 (nth (if (consp (nth 2 event)) 2 1) event))
0f03054a 454
32295976
RS
455(defsubst event-click-count (event)
456 "Return the multi-click count of EVENT, a click or drag event.
457The return value is a positive integer."
458 (if (integerp (nth 2 event)) (nth 2 event) 1))
459
0f03054a
RS
460(defsubst posn-window (position)
461 "Return the window in POSITION.
462POSITION should be a list of the form
e55c21be 463 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
464as returned by the `event-start' and `event-end' functions."
465 (nth 0 position))
466
467(defsubst posn-point (position)
468 "Return the buffer location in POSITION.
469POSITION should be a list of the form
e55c21be 470 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 471as returned by the `event-start' and `event-end' functions."
15db4e0e
JB
472 (if (consp (nth 1 position))
473 (car (nth 1 position))
474 (nth 1 position)))
0f03054a 475
e55c21be
RS
476(defsubst posn-x-y (position)
477 "Return the x and y coordinates in POSITION.
0f03054a 478POSITION should be a list of the form
e55c21be 479 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
480as returned by the `event-start' and `event-end' functions."
481 (nth 2 position))
482
ed627e08 483(defun posn-col-row (position)
dbbcac56 484 "Return the column and row in POSITION, measured in characters.
e55c21be
RS
485POSITION should be a list of the form
486 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
ed627e08
RS
487as returned by the `event-start' and `event-end' functions.
488For a scroll-bar event, the result column is 0, and the row
489corresponds to the vertical position of the click in the scroll bar."
490 (let ((pair (nth 2 position))
491 (window (posn-window position)))
dbbcac56
KH
492 (if (eq (if (consp (nth 1 position))
493 (car (nth 1 position))
494 (nth 1 position))
ed627e08
RS
495 'vertical-scroll-bar)
496 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
dbbcac56
KH
497 (if (eq (if (consp (nth 1 position))
498 (car (nth 1 position))
499 (nth 1 position))
ed627e08
RS
500 'horizontal-scroll-bar)
501 (cons (scroll-bar-scale pair (window-width window)) 0)
9ba60df9
RS
502 (let* ((frame (if (framep window) window (window-frame window)))
503 (x (/ (car pair) (frame-char-width frame)))
504 (y (/ (cdr pair) (frame-char-height frame))))
ed627e08 505 (cons x y))))))
e55c21be 506
0f03054a
RS
507(defsubst posn-timestamp (position)
508 "Return the timestamp of POSITION.
509POSITION should be a list of the form
e55c21be 510 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
f415c00c 511as returned by the `event-start' and `event-end' functions."
0f03054a 512 (nth 3 position))
9a5336ae 513
0f03054a 514\f
9a5336ae
JB
515;;;; Obsolescent names for functions.
516
059184dd
ER
517(defalias 'dot 'point)
518(defalias 'dot-marker 'point-marker)
519(defalias 'dot-min 'point-min)
520(defalias 'dot-max 'point-max)
521(defalias 'window-dot 'window-point)
522(defalias 'set-window-dot 'set-window-point)
523(defalias 'read-input 'read-string)
524(defalias 'send-string 'process-send-string)
525(defalias 'send-region 'process-send-region)
526(defalias 'show-buffer 'set-window-buffer)
527(defalias 'buffer-flush-undo 'buffer-disable-undo)
528(defalias 'eval-current-buffer 'eval-buffer)
529(defalias 'compiled-function-p 'byte-code-function-p)
ae1cc031 530(defalias 'define-function 'defalias)
be9b65ac 531
0cba3a0f
KH
532(defalias 'sref 'aref)
533(make-obsolete 'sref 'aref)
534(make-obsolete 'char-bytes "Now this function always returns 1")
6bb762b3 535
9a5336ae
JB
536;; Some programs still use this as a function.
537(defun baud-rate ()
bcacc42c
RS
538 "Obsolete function returning the value of the `baud-rate' variable.
539Please convert your programs to use the variable `baud-rate' directly."
9a5336ae
JB
540 baud-rate)
541
0a5c0893
MB
542(defalias 'focus-frame 'ignore)
543(defalias 'unfocus-frame 'ignore)
9a5336ae
JB
544\f
545;;;; Alternate names for functions - these are not being phased out.
546
059184dd
ER
547(defalias 'string= 'string-equal)
548(defalias 'string< 'string-lessp)
549(defalias 'move-marker 'set-marker)
059184dd
ER
550(defalias 'not 'null)
551(defalias 'rplaca 'setcar)
552(defalias 'rplacd 'setcdr)
eb8c3be9 553(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
554(defalias 'indent-to-column 'indent-to)
555(defalias 'backward-delete-char 'delete-backward-char)
556(defalias 'search-forward-regexp (symbol-function 're-search-forward))
557(defalias 'search-backward-regexp (symbol-function 're-search-backward))
558(defalias 'int-to-string 'number-to-string)
024ae2c6 559(defalias 'store-match-data 'set-match-data)
37f6661a
JB
560
561;;; Should this be an obsolete name? If you decide it should, you get
562;;; to go through all the sources and change them.
059184dd 563(defalias 'string-to-int 'string-to-number)
be9b65ac 564\f
9a5336ae 565;;;; Hook manipulation functions.
be9b65ac 566
0e4d378b
RS
567(defun make-local-hook (hook)
568 "Make the hook HOOK local to the current buffer.
71c78f01
RS
569The return value is HOOK.
570
0e4d378b
RS
571When a hook is local, its local and global values
572work in concert: running the hook actually runs all the hook
573functions listed in *either* the local value *or* the global value
574of the hook variable.
575
7dd1926e
RS
576This function works by making `t' a member of the buffer-local value,
577which acts as a flag to run the hook functions in the default value as
578well. This works for all normal hooks, but does not work for most
579non-normal hooks yet. We will be changing the callers of non-normal
580hooks so that they can handle localness; this has to be done one by
581one.
582
583This function does nothing if HOOK is already local in the current
584buffer.
0e4d378b
RS
585
586Do not use `make-local-variable' to make a hook variable buffer-local."
587 (if (local-variable-p hook)
588 nil
589 (or (boundp hook) (set hook nil))
590 (make-local-variable hook)
71c78f01
RS
591 (set hook (list t)))
592 hook)
0e4d378b
RS
593
594(defun add-hook (hook function &optional append local)
32295976
RS
595 "Add to the value of HOOK the function FUNCTION.
596FUNCTION is not added if already present.
597FUNCTION is added (if necessary) at the beginning of the hook list
598unless the optional argument APPEND is non-nil, in which case
599FUNCTION is added at the end.
600
0e4d378b
RS
601The optional fourth argument, LOCAL, if non-nil, says to modify
602the hook's buffer-local value rather than its default value.
603This makes no difference if the hook is not buffer-local.
604To make a hook variable buffer-local, always use
605`make-local-hook', not `make-local-variable'.
606
32295976
RS
607HOOK should be a symbol, and FUNCTION may be any valid function. If
608HOOK is void, it is first set to nil. If HOOK's value is a single
aa09b5ca 609function, it is changed to a list of functions."
be9b65ac 610 (or (boundp hook) (set hook nil))
0e4d378b 611 (or (default-boundp hook) (set-default hook nil))
32295976
RS
612 ;; If the hook value is a single function, turn it into a list.
613 (let ((old (symbol-value hook)))
614 (if (or (not (listp old)) (eq (car old) 'lambda))
615 (set hook (list old))))
f4e5bca5
RS
616 (if (or local
617 ;; Detect the case where make-local-variable was used on a hook
618 ;; and do what we used to do.
cd2db344 619 (and (local-variable-if-set-p hook)
f4e5bca5 620 (not (memq t (symbol-value hook)))))
0e4d378b 621 ;; Alter the local value only.
1fa0de2c 622 (or (if (or (consp function) (byte-code-function-p function))
0e4d378b
RS
623 (member function (symbol-value hook))
624 (memq function (symbol-value hook)))
625 (set hook
626 (if append
627 (append (symbol-value hook) (list function))
628 (cons function (symbol-value hook)))))
629 ;; Alter the global value (which is also the only value,
630 ;; if the hook doesn't have a local value).
1fa0de2c 631 (or (if (or (consp function) (byte-code-function-p function))
0e4d378b
RS
632 (member function (default-value hook))
633 (memq function (default-value hook)))
634 (set-default hook
635 (if append
636 (append (default-value hook) (list function))
637 (cons function (default-value hook)))))))
638
639(defun remove-hook (hook function &optional local)
24980d16
RS
640 "Remove from the value of HOOK the function FUNCTION.
641HOOK should be a symbol, and FUNCTION may be any valid function. If
642FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
0e4d378b
RS
643list of hooks to run in HOOK, then nothing is done. See `add-hook'.
644
645The optional third argument, LOCAL, if non-nil, says to modify
646the hook's buffer-local value rather than its default value.
647This makes no difference if the hook is not buffer-local.
648To make a hook variable buffer-local, always use
649`make-local-hook', not `make-local-variable'."
24980d16 650 (if (or (not (boundp hook)) ;unbound symbol, or
d46490e3 651 (not (default-boundp hook))
24980d16
RS
652 (null (symbol-value hook)) ;value is nil, or
653 (null function)) ;function is nil, then
654 nil ;Do nothing.
f4e5bca5
RS
655 (if (or local
656 ;; Detect the case where make-local-variable was used on a hook
657 ;; and do what we used to do.
658 (and (local-variable-p hook)
cf4a60a3
DL
659 (consp (symbol-value hook))
660 (not (memq t (symbol-value hook)))))
0e4d378b
RS
661 (let ((hook-value (symbol-value hook)))
662 (if (consp hook-value)
663 (if (member function hook-value)
664 (setq hook-value (delete function (copy-sequence hook-value))))
665 (if (equal hook-value function)
666 (setq hook-value nil)))
667 (set hook hook-value))
668 (let ((hook-value (default-value hook)))
cf4a60a3 669 (if (and (consp hook-value) (not (functionp hook-value)))
0e4d378b
RS
670 (if (member function hook-value)
671 (setq hook-value (delete function (copy-sequence hook-value))))
672 (if (equal hook-value function)
673 (setq hook-value nil)))
674 (set-default hook hook-value)))))
6e3af630
RS
675
676(defun add-to-list (list-var element)
8851c1f0 677 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
9f0b1f09 678The test for presence of ELEMENT is done with `equal'.
8851c1f0
RS
679If you want to use `add-to-list' on a variable that is not defined
680until a certain package is loaded, you should put the call to `add-to-list'
681into a hook function that will be run only after loading the package.
682`eval-after-load' provides one way to do this. In some cases
683other hooks, such as major mode hooks, can do the job."
15171a06
KH
684 (if (member element (symbol-value list-var))
685 (symbol-value list-var)
686 (set list-var (cons element (symbol-value list-var)))))
be9b65ac 687\f
9a5336ae
JB
688;;;; Specifying things to do after certain files are loaded.
689
690(defun eval-after-load (file form)
691 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
692This makes or adds to an entry on `after-load-alist'.
90914938 693If FILE is already loaded, evaluate FORM right now.
12c7071c 694It does nothing if FORM is already on the list for FILE.
9a5336ae 695FILE should be the name of a library, with no directory name."
90914938 696 ;; Make sure there is an element for FILE.
9a5336ae
JB
697 (or (assoc file after-load-alist)
698 (setq after-load-alist (cons (list file) after-load-alist)))
90914938 699 ;; Add FORM to the element if it isn't there.
12c7071c
RS
700 (let ((elt (assoc file after-load-alist)))
701 (or (member form (cdr elt))
90914938
RS
702 (progn
703 (nconc elt (list form))
704 ;; If the file has been loaded already, run FORM right away.
705 (and (assoc file load-history)
706 (eval form)))))
9a5336ae
JB
707 form)
708
709(defun eval-next-after-load (file)
710 "Read the following input sexp, and run it whenever FILE is loaded.
711This makes or adds to an entry on `after-load-alist'.
712FILE should be the name of a library, with no directory name."
713 (eval-after-load file (read)))
714
715\f
716;;;; Input and display facilities.
717
77a5664f 718(defvar read-quoted-char-radix 8
1ba764de 719 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
77a5664f
RS
720Legitimate radix values are 8, 10 and 16.")
721
722(custom-declare-variable-early
723 'read-quoted-char-radix 8
724 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1ba764de
RS
725Legitimate radix values are 8, 10 and 16."
726 :type '(choice (const 8) (const 10) (const 16))
727 :group 'editing-basics)
728
9a5336ae 729(defun read-quoted-char (&optional prompt)
2444730b
RS
730 "Like `read-char', but do not allow quitting.
731Also, if the first character read is an octal digit,
732we read any number of octal digits and return the
569b03f2 733specified character code. Any nondigit terminates the sequence.
1ba764de 734If the terminator is RET, it is discarded;
2444730b
RS
735any other terminator is used itself as input.
736
569b03f2
RS
737The optional argument PROMPT specifies a string to use to prompt the user.
738The variable `read-quoted-char-radix' controls which radix to use
739for numeric input."
2444730b
RS
740 (let ((message-log-max nil) done (first t) (code 0) char)
741 (while (not done)
742 (let ((inhibit-quit first)
42e636f0
KH
743 ;; Don't let C-h get the help message--only help function keys.
744 (help-char nil)
745 (help-form
746 "Type the special character you want to use,
2444730b 747or the octal character code.
1ba764de 748RET terminates the character code and is discarded;
2444730b 749any other non-digit terminates the character code and is then used as input."))
b7de4d62 750 (setq char (read-event (and prompt (format "%s-" prompt)) t))
9a5336ae 751 (if inhibit-quit (setq quit-flag nil)))
4867f7b2
RS
752 ;; Translate TAB key into control-I ASCII character, and so on.
753 (and char
754 (let ((translated (lookup-key function-key-map (vector char))))
bf896a1b 755 (if (arrayp translated)
4867f7b2 756 (setq char (aref translated 0)))))
9a5336ae 757 (cond ((null char))
1ba764de
RS
758 ((not (integerp char))
759 (setq unread-command-events (list char)
760 done t))
bf896a1b
RS
761 ((/= (logand char ?\M-\^@) 0)
762 ;; Turn a meta-character into a character with the 0200 bit set.
763 (setq code (logior (logand char (lognot ?\M-\^@)) 128)
764 done t))
1ba764de
RS
765 ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix))))
766 (setq code (+ (* code read-quoted-char-radix) (- char ?0)))
767 (and prompt (setq prompt (message "%s %c" prompt char))))
768 ((and (<= ?a (downcase char))
769 (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix))))
92304bc8
RS
770 (setq code (+ (* code read-quoted-char-radix)
771 (+ 10 (- (downcase char) ?a))))
91a6acc3 772 (and prompt (setq prompt (message "%s %c" prompt char))))
1ba764de 773 ((and (not first) (eq char ?\C-m))
2444730b
RS
774 (setq done t))
775 ((not first)
776 (setq unread-command-events (list char)
777 done t))
778 (t (setq code char
779 done t)))
780 (setq first nil))
bf896a1b 781 code))
9a5336ae 782
44071d6b
RS
783(defun read-passwd (prompt &optional confirm default)
784 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
e0e4cb7a 785End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
44071d6b
RS
786Optional argument CONFIRM, if non-nil, then read it twice to make sure.
787Optional DEFAULT is a default password to use instead of empty input."
788 (if confirm
789 (let (success)
790 (while (not success)
791 (let ((first (read-passwd prompt nil default))
792 (second (read-passwd "Confirm password: " nil default)))
793 (if (equal first second)
794 (setq success first)
795 (message "Password not repeated accurately; please start over")
796 (sit-for 1))))
797 success)
8723b7f3 798 (clear-this-command-keys)
44071d6b
RS
799 (let ((pass nil)
800 (c 0)
801 (echo-keystrokes 0)
802 (cursor-in-echo-area t))
803 (while (progn (message "%s%s"
804 prompt
805 (make-string (length pass) ?.))
acc81368 806 (setq c (read-char nil t))
44071d6b
RS
807 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
808 (if (= c ?\C-u)
809 (setq pass "")
810 (if (and (/= c ?\b) (/= c ?\177))
811 (setq pass (concat pass (char-to-string c)))
812 (if (> (length pass) 0)
813 (setq pass (substring pass 0 -1))))))
814 (message nil)
815 (or pass default ""))))
e0e4cb7a 816\f
9a5336ae
JB
817(defun force-mode-line-update (&optional all)
818 "Force the mode-line of the current buffer to be redisplayed.
7ec2a18c 819With optional non-nil ALL, force redisplay of all mode-lines."
9a5336ae
JB
820 (if all (save-excursion (set-buffer (other-buffer))))
821 (set-buffer-modified-p (buffer-modified-p)))
822
be9b65ac
DL
823(defun momentary-string-display (string pos &optional exit-char message)
824 "Momentarily display STRING in the buffer at POS.
825Display remains until next character is typed.
826If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
827otherwise it is then available as input (as a command if nothing else).
828Display MESSAGE (optional fourth arg) in the echo area.
829If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
830 (or exit-char (setq exit-char ?\ ))
831 (let ((buffer-read-only nil)
ca2ec1c5
RS
832 ;; Don't modify the undo list at all.
833 (buffer-undo-list t)
be9b65ac
DL
834 (modified (buffer-modified-p))
835 (name buffer-file-name)
836 insert-end)
837 (unwind-protect
838 (progn
839 (save-excursion
840 (goto-char pos)
841 ;; defeat file locking... don't try this at home, kids!
842 (setq buffer-file-name nil)
843 (insert-before-markers string)
3eec84bf
RS
844 (setq insert-end (point))
845 ;; If the message end is off screen, recenter now.
024ae2c6 846 (if (< (window-end nil t) insert-end)
3eec84bf
RS
847 (recenter (/ (window-height) 2)))
848 ;; If that pushed message start off the screen,
849 ;; scroll to start it at the top of the screen.
850 (move-to-window-line 0)
851 (if (> (point) pos)
852 (progn
853 (goto-char pos)
854 (recenter 0))))
be9b65ac
DL
855 (message (or message "Type %s to continue editing.")
856 (single-key-description exit-char))
3547c855 857 (let ((char (read-event)))
be9b65ac 858 (or (eq char exit-char)
dbc4e1c1 859 (setq unread-command-events (list char)))))
be9b65ac
DL
860 (if insert-end
861 (save-excursion
862 (delete-region pos insert-end)))
863 (setq buffer-file-name name)
864 (set-buffer-modified-p modified))))
865
9a5336ae
JB
866\f
867;;;; Miscellanea.
868
448b61c9
RS
869;; A number of major modes set this locally.
870;; Give it a global value to avoid compiler warnings.
871(defvar font-lock-defaults nil)
872
4fb17037
RS
873(defvar suspend-hook nil
874 "Normal hook run by `suspend-emacs', before suspending.")
875
876(defvar suspend-resume-hook nil
877 "Normal hook run by `suspend-emacs', after Emacs is continued.")
878
448b61c9
RS
879;; Avoid compiler warnings about this variable,
880;; which has a special meaning on certain system types.
881(defvar buffer-file-type nil
882 "Non-nil if the visited file is a binary file.
883This variable is meaningful on MS-DOG and Windows NT.
884On those systems, it is automatically local in every buffer.
885On other systems, this variable is normally always nil.")
886
a860d25f 887;; This should probably be written in C (i.e., without using `walk-windows').
63503b24 888(defun get-buffer-window-list (buffer &optional minibuf frame)
a860d25f 889 "Return windows currently displaying BUFFER, or nil if none.
63503b24 890See `walk-windows' for the meaning of MINIBUF and FRAME."
43c5ac8c 891 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
a860d25f
SM
892 (walk-windows (function (lambda (window)
893 (if (eq (window-buffer window) buffer)
894 (setq windows (cons window windows)))))
63503b24 895 minibuf frame)
a860d25f
SM
896 windows))
897
f9269e19
RS
898(defun ignore (&rest ignore)
899 "Do nothing and return nil.
900This function accepts any number of arguments, but ignores them."
c0f1a4f6 901 (interactive)
9a5336ae
JB
902 nil)
903
904(defun error (&rest args)
aa308ce2
RS
905 "Signal an error, making error message by passing all args to `format'.
906In Emacs, the convention is that error messages start with a capital
907letter but *do not* end with a period. Please follow this convention
908for the sake of consistency."
9a5336ae
JB
909 (while t
910 (signal 'error (list (apply 'format args)))))
911
cef7ae6e 912(defalias 'user-original-login-name 'user-login-name)
9a5336ae 913
be9b65ac
DL
914(defun start-process-shell-command (name buffer &rest args)
915 "Start a program in a subprocess. Return the process object for it.
916Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
917NAME is name for process. It is modified if necessary to make it unique.
918BUFFER is the buffer or (buffer-name) to associate with the process.
919 Process output goes at end of that buffer, unless you specify
920 an output stream or filter function to handle the output.
921 BUFFER may be also nil, meaning that this process is not associated
922 with any buffer
923Third arg is command name, the name of a shell command.
924Remaining arguments are the arguments for the command.
4f1d6310 925Wildcards and redirection are handled as usual in the shell."
a247bf21
KH
926 (cond
927 ((eq system-type 'vax-vms)
928 (apply 'start-process name buffer args))
b59f6d7a
RS
929 ;; We used to use `exec' to replace the shell with the command,
930 ;; but that failed to handle (...) and semicolon, etc.
a247bf21
KH
931 (t
932 (start-process name buffer shell-file-name shell-command-switch
b59f6d7a 933 (mapconcat 'identity args " ")))))
a7ed4c2a 934\f
a7f284ec
RS
935(defmacro with-current-buffer (buffer &rest body)
936 "Execute the forms in BODY with BUFFER as the current buffer.
a2fdb55c
EN
937The value returned is the value of the last form in BODY.
938See also `with-temp-buffer'."
a7f284ec
RS
939 `(save-current-buffer
940 (set-buffer ,buffer)
a2fdb55c 941 ,@body))
a7f284ec 942
e5bb8a8c
SM
943(defmacro with-temp-file (file &rest body)
944 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
945The value returned is the value of the last form in BODY.
a2fdb55c 946See also `with-temp-buffer'."
a7ed4c2a 947 (let ((temp-file (make-symbol "temp-file"))
a2fdb55c
EN
948 (temp-buffer (make-symbol "temp-buffer")))
949 `(let ((,temp-file ,file)
950 (,temp-buffer
951 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
952 (unwind-protect
953 (prog1
954 (with-current-buffer ,temp-buffer
e5bb8a8c 955 ,@body)
a2fdb55c
EN
956 (with-current-buffer ,temp-buffer
957 (widen)
958 (write-region (point-min) (point-max) ,temp-file nil 0)))
959 (and (buffer-name ,temp-buffer)
960 (kill-buffer ,temp-buffer))))))
961
e5bb8a8c
SM
962(defmacro with-temp-message (message &rest body)
963 "Display MESSAGE temporarily while BODY is evaluated.
964The original message is restored to the echo area after BODY has finished.
965The value returned is the value of the last form in BODY.
966MESSAGE is written to the message log buffer if `message-log-max' is non-nil."
967 (let ((current-message (make-symbol "current-message")))
968 `(let ((,current-message (current-message)))
969 (unwind-protect
970 (progn
971 (message ,message)
972 ,@body)
973 (message ,current-message)))))
974
975(defmacro with-temp-buffer (&rest body)
976 "Create a temporary buffer, and evaluate BODY there like `progn'.
a2fdb55c
EN
977See also `with-temp-file' and `with-output-to-string'."
978 (let ((temp-buffer (make-symbol "temp-buffer")))
979 `(let ((,temp-buffer
980 (get-buffer-create (generate-new-buffer-name " *temp*"))))
981 (unwind-protect
982 (with-current-buffer ,temp-buffer
e5bb8a8c 983 ,@body)
a2fdb55c
EN
984 (and (buffer-name ,temp-buffer)
985 (kill-buffer ,temp-buffer))))))
986
5db7925d
RS
987(defmacro with-output-to-string (&rest body)
988 "Execute BODY, return the text it sent to `standard-output', as a string."
a2fdb55c
EN
989 `(let ((standard-output
990 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
5db7925d
RS
991 (let ((standard-output standard-output))
992 ,@body)
a2fdb55c
EN
993 (with-current-buffer standard-output
994 (prog1
995 (buffer-string)
996 (kill-buffer nil)))))
2ec9c94e
RS
997
998(defmacro combine-after-change-calls (&rest body)
999 "Execute BODY, but don't call the after-change functions till the end.
1000If BODY makes changes in the buffer, they are recorded
1001and the functions on `after-change-functions' are called several times
1002when BODY is finished.
31aa282e 1003The return value is the value of the last form in BODY.
2ec9c94e
RS
1004
1005If `before-change-functions' is non-nil, then calls to the after-change
1006functions can't be deferred, so in that case this macro has no effect.
1007
1008Do not alter `after-change-functions' or `before-change-functions'
1009in BODY."
1010 `(unwind-protect
1011 (let ((combine-after-change-calls t))
1012 . ,body)
1013 (combine-after-change-execute)))
1014
a2fdb55c 1015\f
c7ca41e6
RS
1016(defvar save-match-data-internal)
1017
1018;; We use save-match-data-internal as the local variable because
1019;; that works ok in practice (people should not use that variable elsewhere).
1020;; We used to use an uninterned symbol; the compiler handles that properly
1021;; now, but it generates slower code.
9a5336ae
JB
1022(defmacro save-match-data (&rest body)
1023 "Execute the BODY forms, restoring the global value of the match data."
9fc0eb95 1024 `(let ((save-match-data-internal (match-data)))
c7ca41e6
RS
1025 (unwind-protect
1026 (progn ,@body)
024ae2c6 1027 (set-match-data save-match-data-internal))))
993713ce 1028
cd323f89 1029(defun match-string (num &optional string)
993713ce
SM
1030 "Return string of text matched by last search.
1031NUM specifies which parenthesized expression in the last regexp.
1032 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1033Zero means the entire text matched by the whole regexp or whole string.
1034STRING should be given if the last search was by `string-match' on STRING."
cd323f89
SM
1035 (if (match-beginning num)
1036 (if string
1037 (substring string (match-beginning num) (match-end num))
1038 (buffer-substring (match-beginning num) (match-end num)))))
58f950b4 1039
bb760c71
RS
1040(defun match-string-no-properties (num &optional string)
1041 "Return string of text matched by last search, without text properties.
1042NUM specifies which parenthesized expression in the last regexp.
1043 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1044Zero means the entire text matched by the whole regexp or whole string.
1045STRING should be given if the last search was by `string-match' on STRING."
1046 (if (match-beginning num)
1047 (if string
1048 (let ((result
1049 (substring string (match-beginning num) (match-end num))))
1050 (set-text-properties 0 (length result) nil result)
1051 result)
1052 (buffer-substring-no-properties (match-beginning num)
1053 (match-end num)))))
1054
edce3654
RS
1055(defun split-string (string &optional separators)
1056 "Splits STRING into substrings where there are matches for SEPARATORS.
1057Each match for SEPARATORS is a splitting point.
1058The substrings between the splitting points are made into a list
1059which is returned.
b222b786
RS
1060If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1061
1062If there is match for SEPARATORS at the beginning of STRING, we do not
1063include a null substring for that. Likewise, if there is a match
1064at the end of STRING, we don't include a null substring for that."
edce3654
RS
1065 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1066 (start 0)
b222b786 1067 notfirst
edce3654 1068 (list nil))
b222b786
RS
1069 (while (and (string-match rexp string
1070 (if (and notfirst
1071 (= start (match-beginning 0))
1072 (< start (length string)))
1073 (1+ start) start))
1074 (< (match-beginning 0) (length string)))
1075 (setq notfirst t)
7eb47123 1076 (or (eq (match-beginning 0) 0)
b222b786
RS
1077 (and (eq (match-beginning 0) (match-end 0))
1078 (eq (match-beginning 0) start))
edce3654
RS
1079 (setq list
1080 (cons (substring string start (match-beginning 0))
1081 list)))
1082 (setq start (match-end 0)))
1083 (or (eq start (length string))
1084 (setq list
1085 (cons (substring string start)
1086 list)))
1087 (nreverse list)))
a7ed4c2a 1088\f
8af7df60
RS
1089(defun shell-quote-argument (argument)
1090 "Quote an argument for passing as argument to an inferior shell."
c1c74b43
RS
1091 (if (eq system-type 'ms-dos)
1092 ;; MS-DOS shells don't have quoting, so don't do any.
1093 argument
1094 (if (eq system-type 'windows-nt)
1095 (concat "\"" argument "\"")
e1b65a6b
RS
1096 (if (equal argument "")
1097 "''"
1098 ;; Quote everything except POSIX filename characters.
1099 ;; This should be safe enough even for really weird shells.
1100 (let ((result "") (start 0) end)
1101 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1102 (setq end (match-beginning 0)
1103 result (concat result (substring argument start end)
1104 "\\" (substring argument end (1+ end)))
1105 start (1+ end)))
1106 (concat result (substring argument start)))))))
8af7df60 1107
297d863b 1108(defun make-syntax-table (&optional oldtable)
984f718a 1109 "Return a new syntax table.
888eb98e
RS
1110If OLDTABLE is non-nil, copy OLDTABLE.
1111Otherwise, create a syntax table which inherits
1112all letters and control characters from the standard syntax table;
1113other characters are copied from the standard syntax table."
297d863b
KH
1114 (if oldtable
1115 (copy-syntax-table oldtable)
1116 (let ((table (copy-syntax-table))
1117 i)
1118 (setq i 0)
1119 (while (<= i 31)
a6889c57 1120 (aset table i nil)
297d863b
KH
1121 (setq i (1+ i)))
1122 (setq i ?A)
1123 (while (<= i ?Z)
a6889c57 1124 (aset table i nil)
297d863b
KH
1125 (setq i (1+ i)))
1126 (setq i ?a)
1127 (while (<= i ?z)
a6889c57 1128 (aset table i nil)
297d863b
KH
1129 (setq i (1+ i)))
1130 (setq i 128)
1131 (while (<= i 255)
a6889c57 1132 (aset table i nil)
297d863b
KH
1133 (setq i (1+ i)))
1134 table)))
31aa282e
KH
1135
1136(defun add-to-invisibility-spec (arg)
1137 "Add elements to `buffer-invisibility-spec'.
1138See documentation for `buffer-invisibility-spec' for the kind of elements
1139that can be added."
1140 (cond
1141 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
1142 (setq buffer-invisibility-spec (list arg)))
1143 (t
11d431ad
KH
1144 (setq buffer-invisibility-spec
1145 (cons arg buffer-invisibility-spec)))))
31aa282e
KH
1146
1147(defun remove-from-invisibility-spec (arg)
1148 "Remove elements from `buffer-invisibility-spec'."
1149 (if buffer-invisibility-spec
071a2a71 1150 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
baed0109
RS
1151\f
1152(defun global-set-key (key command)
1153 "Give KEY a global binding as COMMAND.
7bba1895
KH
1154COMMAND is the command definition to use; usually it is
1155a symbol naming an interactively-callable function.
1156KEY is a key sequence; noninteractively, it is a string or vector
1157of characters or event types, and non-ASCII characters with codes
1158above 127 (such as ISO Latin-1) can be included if you use a vector.
1159
1160Note that if KEY has a local binding in the current buffer,
1161that local binding will continue to shadow any global binding
1162that you make with this function."
baed0109
RS
1163 (interactive "KSet key globally: \nCSet key %s to command: ")
1164 (or (vectorp key) (stringp key)
1165 (signal 'wrong-type-argument (list 'arrayp key)))
ff663bbe 1166 (define-key (current-global-map) key command))
baed0109
RS
1167
1168(defun local-set-key (key command)
1169 "Give KEY a local binding as COMMAND.
7bba1895
KH
1170COMMAND is the command definition to use; usually it is
1171a symbol naming an interactively-callable function.
1172KEY is a key sequence; noninteractively, it is a string or vector
1173of characters or event types, and non-ASCII characters with codes
1174above 127 (such as ISO Latin-1) can be included if you use a vector.
1175
baed0109
RS
1176The binding goes in the current buffer's local map,
1177which in most cases is shared with all other buffers in the same major mode."
1178 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1179 (let ((map (current-local-map)))
1180 (or map
1181 (use-local-map (setq map (make-sparse-keymap))))
1182 (or (vectorp key) (stringp key)
1183 (signal 'wrong-type-argument (list 'arrayp key)))
ff663bbe 1184 (define-key map key command)))
984f718a 1185
baed0109
RS
1186(defun global-unset-key (key)
1187 "Remove global binding of KEY.
1188KEY is a string representing a sequence of keystrokes."
1189 (interactive "kUnset key globally: ")
1190 (global-set-key key nil))
1191
db2474b8 1192(defun local-unset-key (key)
baed0109
RS
1193 "Remove local binding of KEY.
1194KEY is a string representing a sequence of keystrokes."
1195 (interactive "kUnset key locally: ")
1196 (if (current-local-map)
db2474b8 1197 (local-set-key key nil))
baed0109
RS
1198 nil)
1199\f
4809d0dd
KH
1200;; We put this here instead of in frame.el so that it's defined even on
1201;; systems where frame.el isn't loaded.
1202(defun frame-configuration-p (object)
1203 "Return non-nil if OBJECT seems to be a frame configuration.
1204Any list whose car is `frame-configuration' is assumed to be a frame
1205configuration."
1206 (and (consp object)
1207 (eq (car object) 'frame-configuration)))
1208
a9a44ed1 1209(defun functionp (object)
77a5664f 1210 "Non-nil if OBJECT is a type of object that can be called as a function."
c029995c 1211 (or (subrp object) (byte-code-function-p object)
a9a44ed1
RS
1212 (eq (car-safe object) 'lambda)
1213 (and (symbolp object) (fboundp object))))
1214
9a5336ae
JB
1215;; now in fns.c
1216;(defun nth (n list)
1217; "Returns the Nth element of LIST.
1218;N counts from zero. If LIST is not that long, nil is returned."
1219; (car (nthcdr n list)))
1220;
1221;(defun copy-alist (alist)
1222; "Return a copy of ALIST.
1223;This is a new alist which represents the same mapping
1224;from objects to objects, but does not share the alist structure with ALIST.
1225;The objects mapped (cars and cdrs of elements of the alist)
1226;are shared, however."
1227; (setq alist (copy-sequence alist))
1228; (let ((tail alist))
1229; (while tail
1230; (if (consp (car tail))
1231; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1232; (setq tail (cdr tail))))
1233; alist)
630cc463
ER
1234
1235;;; subr.el ends here