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