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