(browse-url): Re-fix case of
[emacs.git] / lisp / subr.el
blobee6eadaa59fc0fe216033649b2d862306cd3d98e
1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
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.
17 ;; You should have received a copy of the GNU General Public License
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.
22 ;;; Code:
23 (defvar custom-declare-variable-list nil
24 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
25 Each element of this list holds the arguments to one call to `defcustom'.")
27 ;; Use this, rather than defcustom, in subr.el and other files loaded
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)))
33 ;;;; Lisp language features.
35 (defmacro lambda (&rest cdr)
36 "Return a lambda expression.
37 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
38 self-quoting; the result of evaluating the lambda expression is the
39 expression itself. The lambda expression may then be treated as a
40 function, i.e., stored as the function value of a symbol, passed to
41 funcall or mapcar, etc.
43 ARGS should take the same form as an argument list for a `defun'.
44 DOCSTRING 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.
47 INTERACTIVE should be a call to the function `interactive', which see.
48 It may also be omitted.
49 BODY 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)))
54 (defmacro push (newelt listname)
55 "Add NEWELT to the list stored in the symbol LISTNAME.
56 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
57 LISTNAME must be a symbol."
58 (list 'setq listname
59 (list 'cons newelt listname)))
61 (defmacro pop (listname)
62 "Return the first element of LISTNAME's value, and remove it from the list.
63 LISTNAME must be a symbol whose value is a list.
64 If the value is nil, `pop' returns nil but does not actually
65 change the list."
66 (list 'prog1 (list 'car listname)
67 (list 'setq listname (list 'cdr listname))))
69 (defmacro when (cond &rest body)
70 "If COND yields non-nil, do BODY, else return nil."
71 (list 'if cond (cons 'progn body)))
73 (defmacro unless (cond &rest body)
74 "If COND yields nil, do BODY, else return nil."
75 (cons 'if (cons cond (cons nil body))))
77 (defmacro dolist (spec &rest body)
78 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
79 Evaluate BODY with VAR bound to each car from LIST, in turn.
80 Then evaluate RESULT to get return value, default nil."
81 (let ((temp (make-symbol "--dolist-temp--")))
82 (list 'let (list (list temp (nth 1 spec)) (car spec))
83 (list 'while temp
84 (list 'setq (car spec) (list 'car temp))
85 (cons 'progn
86 (append body
87 (list (list 'setq temp (list 'cdr temp))))))
88 (if (cdr (cdr spec))
89 (cons 'progn
90 (cons (list 'setq (car spec) nil) (cdr (cdr spec))))))))
92 (defmacro dotimes (spec &rest body)
93 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
94 Evaluate BODY with VAR bound to successive integers running from 0,
95 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
96 the return value (nil if RESULT is omitted)."
97 (let ((temp (make-symbol "--dotimes-temp--")))
98 (list 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
99 (list 'while (list '< (car spec) temp)
100 (cons 'progn
101 (append body (list (list 'setq (car spec)
102 (list '1+ (car spec)))))))
103 (if (cdr (cdr spec))
104 (car (cdr (cdr spec)))
105 nil))))
107 (defsubst caar (x)
108 "Return the car of the car of X."
109 (car (car x)))
111 (defsubst cadr (x)
112 "Return the car of the cdr of X."
113 (car (cdr x)))
115 (defsubst cdar (x)
116 "Return the cdr of the car of X."
117 (cdr (car x)))
119 (defsubst cddr (x)
120 "Return the cdr of the cdr of X."
121 (cdr (cdr x)))
123 (defun last (x &optional n)
124 "Return the last link of the list X. Its car is the last element.
125 If X is nil, return nil.
126 If N is non-nil, return the Nth-to-last link of X.
127 If N is bigger than the length of X, return X."
128 (if n
129 (let ((m 0) (p x))
130 (while (consp p)
131 (setq m (1+ m) p (cdr p)))
132 (if (<= n 0) p
133 (if (< n m) (nthcdr (- m n) x) x)))
134 (while (cdr x)
135 (setq x (cdr x)))
138 (defun assoc-default (key alist &optional test default)
139 "Find object KEY in a pseudo-alist ALIST.
140 ALIST is a list of conses or objects. Each element (or the element's car,
141 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
142 If that is non-nil, the element matches;
143 then `assoc-default' returns the element's cdr, if it is a cons,
144 or DEFAULT if the element is not a cons.
146 If no element matches, the value is nil.
147 If TEST is omitted or nil, `equal' is used."
148 (let (found (tail alist) value)
149 (while (and tail (not found))
150 (let ((elt (car tail)))
151 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
152 (setq found t value (if (consp elt) (cdr elt) default))))
153 (setq tail (cdr tail)))
154 value))
156 (defun assoc-ignore-case (key alist)
157 "Like `assoc', but ignores differences in case and text representation.
158 KEY must be a string. Upper-case and lower-case letters are treated as equal.
159 Unibyte strings are converted to multibyte for comparison."
160 (let (element)
161 (while (and alist (not element))
162 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
163 (setq element (car alist)))
164 (setq alist (cdr alist)))
165 element))
167 (defun assoc-ignore-representation (key alist)
168 "Like `assoc', but ignores differences in text representation.
169 KEY must be a string.
170 Unibyte strings are converted to multibyte for comparison."
171 (let (element)
172 (while (and alist (not element))
173 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
174 (setq element (car alist)))
175 (setq alist (cdr alist)))
176 element))
178 ;;;; Keymap support.
180 (defun undefined ()
181 (interactive)
182 (ding))
184 ;Prevent the \{...} documentation construct
185 ;from mentioning keys that run this command.
186 (put 'undefined 'suppress-keymap t)
188 (defun suppress-keymap (map &optional nodigits)
189 "Make MAP override all normally self-inserting keys to be undefined.
190 Normally, as an exception, digits and minus-sign are set to make prefix args,
191 but optional second arg NODIGITS non-nil treats them like other chars."
192 (substitute-key-definition 'self-insert-command 'undefined map global-map)
193 (or nodigits
194 (let (loop)
195 (define-key map "-" 'negative-argument)
196 ;; Make plain numbers do numeric args.
197 (setq loop ?0)
198 (while (<= loop ?9)
199 (define-key map (char-to-string loop) 'digit-argument)
200 (setq loop (1+ loop))))))
202 ;Moved to keymap.c
203 ;(defun copy-keymap (keymap)
204 ; "Return a copy of KEYMAP"
205 ; (while (not (keymapp keymap))
206 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
207 ; (if (vectorp keymap)
208 ; (copy-sequence keymap)
209 ; (copy-alist keymap)))
211 (defvar key-substitution-in-progress nil
212 "Used internally by substitute-key-definition.")
214 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
215 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
216 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
217 If optional fourth argument OLDMAP is specified, we redefine
218 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
219 (or prefix (setq prefix ""))
220 (let* ((scan (or oldmap keymap))
221 (vec1 (vector nil))
222 (prefix1 (vconcat prefix vec1))
223 (key-substitution-in-progress
224 (cons scan key-substitution-in-progress)))
225 ;; Scan OLDMAP, finding each char or event-symbol that
226 ;; has any definition, and act on it with hack-key.
227 (while (consp scan)
228 (if (consp (car scan))
229 (let ((char (car (car scan)))
230 (defn (cdr (car scan))))
231 ;; The inside of this let duplicates exactly
232 ;; the inside of the following let that handles array elements.
233 (aset vec1 0 char)
234 (aset prefix1 (length prefix) char)
235 (let (inner-def skipped)
236 ;; Skip past menu-prompt.
237 (while (stringp (car-safe defn))
238 (setq skipped (cons (car defn) skipped))
239 (setq defn (cdr defn)))
240 ;; Skip past cached key-equivalence data for menu items.
241 (and (consp defn) (consp (car defn))
242 (setq defn (cdr defn)))
243 (setq inner-def defn)
244 ;; Look past a symbol that names a keymap.
245 (while (and (symbolp inner-def)
246 (fboundp inner-def))
247 (setq inner-def (symbol-function inner-def)))
248 (if (or (eq defn olddef)
249 ;; Compare with equal if definition is a key sequence.
250 ;; That is useful for operating on function-key-map.
251 (and (or (stringp defn) (vectorp defn))
252 (equal defn olddef)))
253 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
254 (if (and (keymapp defn)
255 ;; Avoid recursively scanning
256 ;; where KEYMAP does not have a submap.
257 (let ((elt (lookup-key keymap prefix1)))
258 (or (null elt)
259 (keymapp elt)))
260 ;; Avoid recursively rescanning keymap being scanned.
261 (not (memq inner-def
262 key-substitution-in-progress)))
263 ;; If this one isn't being scanned already,
264 ;; scan it now.
265 (substitute-key-definition olddef newdef keymap
266 inner-def
267 prefix1)))))
268 (if (vectorp (car scan))
269 (let* ((array (car scan))
270 (len (length array))
271 (i 0))
272 (while (< i len)
273 (let ((char i) (defn (aref array i)))
274 ;; The inside of this let duplicates exactly
275 ;; the inside of the previous let.
276 (aset vec1 0 char)
277 (aset prefix1 (length prefix) char)
278 (let (inner-def skipped)
279 ;; Skip past menu-prompt.
280 (while (stringp (car-safe defn))
281 (setq skipped (cons (car defn) skipped))
282 (setq defn (cdr defn)))
283 (and (consp defn) (consp (car defn))
284 (setq defn (cdr defn)))
285 (setq inner-def defn)
286 (while (and (symbolp inner-def)
287 (fboundp inner-def))
288 (setq inner-def (symbol-function inner-def)))
289 (if (or (eq defn olddef)
290 (and (or (stringp defn) (vectorp defn))
291 (equal defn olddef)))
292 (define-key keymap prefix1
293 (nconc (nreverse skipped) newdef))
294 (if (and (keymapp defn)
295 (let ((elt (lookup-key keymap prefix1)))
296 (or (null elt)
297 (keymapp elt)))
298 (not (memq inner-def
299 key-substitution-in-progress)))
300 (substitute-key-definition olddef newdef keymap
301 inner-def
302 prefix1)))))
303 (setq i (1+ i))))
304 (if (char-table-p (car scan))
305 (map-char-table
306 (function (lambda (char defn)
307 (let ()
308 ;; The inside of this let duplicates exactly
309 ;; the inside of the previous let,
310 ;; except that it uses set-char-table-range
311 ;; instead of define-key.
312 (aset vec1 0 char)
313 (aset prefix1 (length prefix) char)
314 (let (inner-def skipped)
315 ;; Skip past menu-prompt.
316 (while (stringp (car-safe defn))
317 (setq skipped (cons (car defn) skipped))
318 (setq defn (cdr defn)))
319 (and (consp defn) (consp (car defn))
320 (setq defn (cdr defn)))
321 (setq inner-def defn)
322 (while (and (symbolp inner-def)
323 (fboundp inner-def))
324 (setq inner-def (symbol-function inner-def)))
325 (if (or (eq defn olddef)
326 (and (or (stringp defn) (vectorp defn))
327 (equal defn olddef)))
328 (define-key keymap prefix1
329 (nconc (nreverse skipped) newdef))
330 (if (and (keymapp defn)
331 (let ((elt (lookup-key keymap prefix1)))
332 (or (null elt)
333 (keymapp elt)))
334 (not (memq inner-def
335 key-substitution-in-progress)))
336 (substitute-key-definition olddef newdef keymap
337 inner-def
338 prefix1)))))))
339 (car scan)))))
340 (setq scan (cdr scan)))))
342 (defun define-key-after (keymap key definition &optional after)
343 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
344 This is like `define-key' except that the binding for KEY is placed
345 just after the binding for the event AFTER, instead of at the beginning
346 of the map. Note that AFTER must be an event type (like KEY), NOT a command
347 \(like DEFINITION).
349 If AFTER is t or omitted, the new binding goes at the end of the keymap.
351 KEY must contain just one event type--that is to say, it must be a
352 string or vector of length 1, but AFTER should be a single event
353 type--a symbol or a character, not a sequence.
355 Bindings are always added before any inherited map.
357 The order of bindings in a keymap matters when it is used as a menu."
358 (unless after (setq after t))
359 (or (keymapp keymap)
360 (signal 'wrong-type-argument (list 'keymapp keymap)))
361 (if (> (length key) 1)
362 (error "multi-event key specified in `define-key-after'"))
363 (let ((tail keymap) done inserted
364 (first (aref key 0)))
365 (while (and (not done) tail)
366 ;; Delete any earlier bindings for the same key.
367 (if (eq (car-safe (car (cdr tail))) first)
368 (setcdr tail (cdr (cdr tail))))
369 ;; When we reach AFTER's binding, insert the new binding after.
370 ;; If we reach an inherited keymap, insert just before that.
371 ;; If we reach the end of this keymap, insert at the end.
372 (if (or (and (eq (car-safe (car tail)) after)
373 (not (eq after t)))
374 (eq (car (cdr tail)) 'keymap)
375 (null (cdr tail)))
376 (progn
377 ;; Stop the scan only if we find a parent keymap.
378 ;; Keep going past the inserted element
379 ;; so we can delete any duplications that come later.
380 (if (eq (car (cdr tail)) 'keymap)
381 (setq done t))
382 ;; Don't insert more than once.
383 (or inserted
384 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
385 (setq inserted t)))
386 (setq tail (cdr tail)))))
388 (defmacro kbd (keys)
389 "Convert KEYS to the internal Emacs key representation.
390 KEYS should be a string constant in the format used for
391 saving keyboard macros (see `insert-kbd-macro')."
392 (read-kbd-macro keys))
394 (put 'keyboard-translate-table 'char-table-extra-slots 0)
396 (defun keyboard-translate (from to)
397 "Translate character FROM to TO at a low level.
398 This function creates a `keyboard-translate-table' if necessary
399 and then modifies one entry in it."
400 (or (char-table-p keyboard-translate-table)
401 (setq keyboard-translate-table
402 (make-char-table 'keyboard-translate-table nil)))
403 (aset keyboard-translate-table from to))
406 ;;;; The global keymap tree.
408 ;;; global-map, esc-map, and ctl-x-map have their values set up in
409 ;;; keymap.c; we just give them docstrings here.
411 (defvar global-map nil
412 "Default global keymap mapping Emacs keyboard input into commands.
413 The value is a keymap which is usually (but not necessarily) Emacs's
414 global map.")
416 (defvar esc-map nil
417 "Default keymap for ESC (meta) commands.
418 The normal global definition of the character ESC indirects to this keymap.")
420 (defvar ctl-x-map nil
421 "Default keymap for C-x commands.
422 The normal global definition of the character C-x indirects to this keymap.")
424 (defvar ctl-x-4-map (make-sparse-keymap)
425 "Keymap for subcommands of C-x 4")
426 (defalias 'ctl-x-4-prefix ctl-x-4-map)
427 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
429 (defvar ctl-x-5-map (make-sparse-keymap)
430 "Keymap for frame commands.")
431 (defalias 'ctl-x-5-prefix ctl-x-5-map)
432 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
435 ;;;; Event manipulation functions.
437 ;; The call to `read' is to ensure that the value is computed at load time
438 ;; and not compiled into the .elc file. The value is negative on most
439 ;; machines, but not on all!
440 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
442 (defun listify-key-sequence (key)
443 "Convert a key sequence to a list of events."
444 (if (vectorp key)
445 (append key nil)
446 (mapcar (function (lambda (c)
447 (if (> c 127)
448 (logxor c listify-key-sequence-1)
449 c)))
450 (append key nil))))
452 (defsubst eventp (obj)
453 "True if the argument is an event object."
454 (or (integerp obj)
455 (and (symbolp obj)
456 (get obj 'event-symbol-elements))
457 (and (consp obj)
458 (symbolp (car obj))
459 (get (car obj) 'event-symbol-elements))))
461 (defun event-modifiers (event)
462 "Returns a list of symbols representing the modifier keys in event EVENT.
463 The elements of the list may include `meta', `control',
464 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
465 and `down'."
466 (let ((type event))
467 (if (listp type)
468 (setq type (car type)))
469 (if (symbolp type)
470 (cdr (get type 'event-symbol-elements))
471 (let ((list nil))
472 (or (zerop (logand type ?\M-\^@))
473 (setq list (cons 'meta list)))
474 (or (and (zerop (logand type ?\C-\^@))
475 (>= (logand type 127) 32))
476 (setq list (cons 'control list)))
477 (or (and (zerop (logand type ?\S-\^@))
478 (= (logand type 255) (downcase (logand type 255))))
479 (setq list (cons 'shift list)))
480 (or (zerop (logand type ?\H-\^@))
481 (setq list (cons 'hyper list)))
482 (or (zerop (logand type ?\s-\^@))
483 (setq list (cons 'super list)))
484 (or (zerop (logand type ?\A-\^@))
485 (setq list (cons 'alt list)))
486 list))))
488 (defun event-basic-type (event)
489 "Returns the basic type of the given event (all modifiers removed).
490 The value is an ASCII printing character (not upper case) or a symbol."
491 (if (consp event)
492 (setq event (car event)))
493 (if (symbolp event)
494 (car (get event 'event-symbol-elements))
495 (let ((base (logand event (1- (lsh 1 18)))))
496 (downcase (if (< base 32) (logior base 64) base)))))
498 (defsubst mouse-movement-p (object)
499 "Return non-nil if OBJECT is a mouse movement event."
500 (and (consp object)
501 (eq (car object) 'mouse-movement)))
503 (defsubst event-start (event)
504 "Return the starting position of EVENT.
505 If EVENT is a mouse press or a mouse click, this returns the location
506 of the event.
507 If EVENT is a drag, this returns the drag's starting position.
508 The return value is of the form
509 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
510 The `posn-' functions access elements of such lists."
511 (nth 1 event))
513 (defsubst event-end (event)
514 "Return the ending location of EVENT. EVENT should be a click or drag event.
515 If EVENT is a click event, this function is the same as `event-start'.
516 The return value is of the form
517 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
518 The `posn-' functions access elements of such lists."
519 (nth (if (consp (nth 2 event)) 2 1) event))
521 (defsubst event-click-count (event)
522 "Return the multi-click count of EVENT, a click or drag event.
523 The return value is a positive integer."
524 (if (integerp (nth 2 event)) (nth 2 event) 1))
526 (defsubst posn-window (position)
527 "Return the window in POSITION.
528 POSITION should be a list of the form
529 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
530 as returned by the `event-start' and `event-end' functions."
531 (nth 0 position))
533 (defsubst posn-point (position)
534 "Return the buffer location in POSITION.
535 POSITION should be a list of the form
536 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
537 as returned by the `event-start' and `event-end' functions."
538 (if (consp (nth 1 position))
539 (car (nth 1 position))
540 (nth 1 position)))
542 (defsubst posn-x-y (position)
543 "Return the x and y coordinates in POSITION.
544 POSITION should be a list of the form
545 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
546 as returned by the `event-start' and `event-end' functions."
547 (nth 2 position))
549 (defun posn-col-row (position)
550 "Return the column and row in POSITION, measured in characters.
551 POSITION should be a list of the form
552 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
553 as returned by the `event-start' and `event-end' functions.
554 For a scroll-bar event, the result column is 0, and the row
555 corresponds to the vertical position of the click in the scroll bar."
556 (let ((pair (nth 2 position))
557 (window (posn-window position)))
558 (if (eq (if (consp (nth 1 position))
559 (car (nth 1 position))
560 (nth 1 position))
561 'vertical-scroll-bar)
562 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
563 (if (eq (if (consp (nth 1 position))
564 (car (nth 1 position))
565 (nth 1 position))
566 'horizontal-scroll-bar)
567 (cons (scroll-bar-scale pair (window-width window)) 0)
568 (let* ((frame (if (framep window) window (window-frame window)))
569 (x (/ (car pair) (frame-char-width frame)))
570 (y (/ (cdr pair) (frame-char-height frame))))
571 (cons x y))))))
573 (defsubst posn-timestamp (position)
574 "Return the timestamp of POSITION.
575 POSITION should be a list of the form
576 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
577 as returned by the `event-start' and `event-end' functions."
578 (nth 3 position))
581 ;;;; Obsolescent names for functions.
583 (defalias 'dot 'point)
584 (defalias 'dot-marker 'point-marker)
585 (defalias 'dot-min 'point-min)
586 (defalias 'dot-max 'point-max)
587 (defalias 'window-dot 'window-point)
588 (defalias 'set-window-dot 'set-window-point)
589 (defalias 'read-input 'read-string)
590 (defalias 'send-string 'process-send-string)
591 (defalias 'send-region 'process-send-region)
592 (defalias 'show-buffer 'set-window-buffer)
593 (defalias 'buffer-flush-undo 'buffer-disable-undo)
594 (defalias 'eval-current-buffer 'eval-buffer)
595 (defalias 'compiled-function-p 'byte-code-function-p)
596 (defalias 'define-function 'defalias)
598 (defalias 'sref 'aref)
599 (make-obsolete 'sref 'aref)
600 (make-obsolete 'char-bytes "Now this function always returns 1")
602 ;; Some programs still use this as a function.
603 (defun baud-rate ()
604 "Obsolete function returning the value of the `baud-rate' variable.
605 Please convert your programs to use the variable `baud-rate' directly."
606 baud-rate)
608 (defalias 'focus-frame 'ignore)
609 (defalias 'unfocus-frame 'ignore)
611 ;;;; Alternate names for functions - these are not being phased out.
613 (defalias 'string= 'string-equal)
614 (defalias 'string< 'string-lessp)
615 (defalias 'move-marker 'set-marker)
616 (defalias 'not 'null)
617 (defalias 'rplaca 'setcar)
618 (defalias 'rplacd 'setcdr)
619 (defalias 'beep 'ding) ;preserve lingual purity
620 (defalias 'indent-to-column 'indent-to)
621 (defalias 'backward-delete-char 'delete-backward-char)
622 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
623 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
624 (defalias 'int-to-string 'number-to-string)
625 (defalias 'store-match-data 'set-match-data)
626 (defalias 'point-at-eol 'line-end-position)
627 (defalias 'point-at-bol 'line-beginning-position)
629 ;;; Should this be an obsolete name? If you decide it should, you get
630 ;;; to go through all the sources and change them.
631 (defalias 'string-to-int 'string-to-number)
633 ;;;; Hook manipulation functions.
635 (defun make-local-hook (hook)
636 "Make the hook HOOK local to the current buffer.
637 The return value is HOOK.
639 When a hook is local, its local and global values
640 work in concert: running the hook actually runs all the hook
641 functions listed in *either* the local value *or* the global value
642 of the hook variable.
644 This function works by making `t' a member of the buffer-local value,
645 which acts as a flag to run the hook functions in the default value as
646 well. This works for all normal hooks, but does not work for most
647 non-normal hooks yet. We will be changing the callers of non-normal
648 hooks so that they can handle localness; this has to be done one by
649 one.
651 This function does nothing if HOOK is already local in the current
652 buffer.
654 Do not use `make-local-variable' to make a hook variable buffer-local."
655 (if (local-variable-p hook)
657 (or (boundp hook) (set hook nil))
658 (make-local-variable hook)
659 (set hook (list t)))
660 hook)
662 (defun add-hook (hook function &optional append local)
663 "Add to the value of HOOK the function FUNCTION.
664 FUNCTION is not added if already present.
665 FUNCTION is added (if necessary) at the beginning of the hook list
666 unless the optional argument APPEND is non-nil, in which case
667 FUNCTION is added at the end.
669 The optional fourth argument, LOCAL, if non-nil, says to modify
670 the hook's buffer-local value rather than its default value.
671 This makes no difference if the hook is not buffer-local.
672 To make a hook variable buffer-local, always use
673 `make-local-hook', not `make-local-variable'.
675 HOOK should be a symbol, and FUNCTION may be any valid function. If
676 HOOK is void, it is first set to nil. If HOOK's value is a single
677 function, it is changed to a list of functions."
678 (or (boundp hook) (set hook nil))
679 (or (default-boundp hook) (set-default hook nil))
680 ;; If the hook value is a single function, turn it into a list.
681 (let ((old (symbol-value hook)))
682 (if (or (not (listp old)) (eq (car old) 'lambda))
683 (set hook (list old))))
684 (if (or local
685 ;; Detect the case where make-local-variable was used on a hook
686 ;; and do what we used to do.
687 (and (local-variable-if-set-p hook)
688 (not (memq t (symbol-value hook)))))
689 ;; Alter the local value only.
690 (or (if (or (consp function) (byte-code-function-p function))
691 (member function (symbol-value hook))
692 (memq function (symbol-value hook)))
693 (set hook
694 (if append
695 (append (symbol-value hook) (list function))
696 (cons function (symbol-value hook)))))
697 ;; Alter the global value (which is also the only value,
698 ;; if the hook doesn't have a local value).
699 (or (if (or (consp function) (byte-code-function-p function))
700 (member function (default-value hook))
701 (memq function (default-value hook)))
702 (set-default hook
703 (if append
704 (append (default-value hook) (list function))
705 (cons function (default-value hook)))))))
707 (defun remove-hook (hook function &optional local)
708 "Remove from the value of HOOK the function FUNCTION.
709 HOOK should be a symbol, and FUNCTION may be any valid function. If
710 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
711 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
713 The optional third argument, LOCAL, if non-nil, says to modify
714 the hook's buffer-local value rather than its default value.
715 This makes no difference if the hook is not buffer-local.
716 To make a hook variable buffer-local, always use
717 `make-local-hook', not `make-local-variable'."
718 (if (or (not (boundp hook)) ;unbound symbol, or
719 (not (default-boundp hook))
720 (null (symbol-value hook)) ;value is nil, or
721 (null function)) ;function is nil, then
722 nil ;Do nothing.
723 (if (or local
724 ;; Detect the case where make-local-variable was used on a hook
725 ;; and do what we used to do.
726 (and (local-variable-p hook)
727 (consp (symbol-value hook))
728 (not (memq t (symbol-value hook)))))
729 (let ((hook-value (symbol-value hook)))
730 (if (consp hook-value)
731 (if (member function hook-value)
732 (setq hook-value (delete function (copy-sequence hook-value))))
733 (if (equal hook-value function)
734 (setq hook-value nil)))
735 (set hook hook-value))
736 (let ((hook-value (default-value hook)))
737 (if (and (consp hook-value) (not (functionp hook-value)))
738 (if (member function hook-value)
739 (setq hook-value (delete function (copy-sequence hook-value))))
740 (if (equal hook-value function)
741 (setq hook-value nil)))
742 (set-default hook hook-value)))))
744 (defun add-to-list (list-var element)
745 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
746 The test for presence of ELEMENT is done with `equal'.
747 If ELEMENT is added, it is added at the beginning of the list.
749 If you want to use `add-to-list' on a variable that is not defined
750 until a certain package is loaded, you should put the call to `add-to-list'
751 into a hook function that will be run only after loading the package.
752 `eval-after-load' provides one way to do this. In some cases
753 other hooks, such as major mode hooks, can do the job."
754 (if (member element (symbol-value list-var))
755 (symbol-value list-var)
756 (set list-var (cons element (symbol-value list-var)))))
758 ;;;; Specifying things to do after certain files are loaded.
760 (defun eval-after-load (file form)
761 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
762 This makes or adds to an entry on `after-load-alist'.
763 If FILE is already loaded, evaluate FORM right now.
764 It does nothing if FORM is already on the list for FILE.
765 FILE should be the name of a library, with no directory name."
766 ;; Make sure there is an element for FILE.
767 (or (assoc file after-load-alist)
768 (setq after-load-alist (cons (list file) after-load-alist)))
769 ;; Add FORM to the element if it isn't there.
770 (let ((elt (assoc file after-load-alist)))
771 (or (member form (cdr elt))
772 (progn
773 (nconc elt (list form))
774 ;; If the file has been loaded already, run FORM right away.
775 (and (assoc file load-history)
776 (eval form)))))
777 form)
779 (defun eval-next-after-load (file)
780 "Read the following input sexp, and run it whenever FILE is loaded.
781 This makes or adds to an entry on `after-load-alist'.
782 FILE should be the name of a library, with no directory name."
783 (eval-after-load file (read)))
786 ;;;; Input and display facilities.
788 (defvar read-quoted-char-radix 8
789 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
790 Legitimate radix values are 8, 10 and 16.")
792 (custom-declare-variable-early
793 'read-quoted-char-radix 8
794 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
795 Legitimate radix values are 8, 10 and 16."
796 :type '(choice (const 8) (const 10) (const 16))
797 :group 'editing-basics)
799 (defun read-quoted-char (&optional prompt)
800 "Like `read-char', but do not allow quitting.
801 Also, if the first character read is an octal digit,
802 we read any number of octal digits and return the
803 specified character code. Any nondigit terminates the sequence.
804 If the terminator is RET, it is discarded;
805 any other terminator is used itself as input.
807 The optional argument PROMPT specifies a string to use to prompt the user.
808 The variable `read-quoted-char-radix' controls which radix to use
809 for numeric input."
810 (let ((message-log-max nil) done (first t) (code 0) char)
811 (while (not done)
812 (let ((inhibit-quit first)
813 ;; Don't let C-h get the help message--only help function keys.
814 (help-char nil)
815 (help-form
816 "Type the special character you want to use,
817 or the octal character code.
818 RET terminates the character code and is discarded;
819 any other non-digit terminates the character code and is then used as input."))
820 (setq char (read-event (and prompt (format "%s-" prompt)) t))
821 (if inhibit-quit (setq quit-flag nil)))
822 ;; Translate TAB key into control-I ASCII character, and so on.
823 (and char
824 (let ((translated (lookup-key function-key-map (vector char))))
825 (if (arrayp translated)
826 (setq char (aref translated 0)))))
827 (cond ((null char))
828 ((not (integerp char))
829 (setq unread-command-events (list char)
830 done t))
831 ((/= (logand char ?\M-\^@) 0)
832 ;; Turn a meta-character into a character with the 0200 bit set.
833 (setq code (logior (logand char (lognot ?\M-\^@)) 128)
834 done t))
835 ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix))))
836 (setq code (+ (* code read-quoted-char-radix) (- char ?0)))
837 (and prompt (setq prompt (message "%s %c" prompt char))))
838 ((and (<= ?a (downcase char))
839 (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix))))
840 (setq code (+ (* code read-quoted-char-radix)
841 (+ 10 (- (downcase char) ?a))))
842 (and prompt (setq prompt (message "%s %c" prompt char))))
843 ((and (not first) (eq char ?\C-m))
844 (setq done t))
845 ((not first)
846 (setq unread-command-events (list char)
847 done t))
848 (t (setq code char
849 done t)))
850 (setq first nil))
851 code))
853 (defun read-passwd (prompt &optional confirm default)
854 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
855 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
856 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
857 Optional DEFAULT is a default password to use instead of empty input."
858 (if confirm
859 (let (success)
860 (while (not success)
861 (let ((first (read-passwd prompt nil default))
862 (second (read-passwd "Confirm password: " nil default)))
863 (if (equal first second)
864 (setq success first)
865 (message "Password not repeated accurately; please start over")
866 (sit-for 1))))
867 success)
868 (let ((pass nil)
869 (c 0)
870 (echo-keystrokes 0)
871 (cursor-in-echo-area t))
872 (while (progn (message "%s%s"
873 prompt
874 (make-string (length pass) ?.))
875 (setq c (read-char nil t))
876 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
877 (if (= c ?\C-u)
878 (setq pass "")
879 (if (and (/= c ?\b) (/= c ?\177))
880 (setq pass (concat pass (char-to-string c)))
881 (if (> (length pass) 0)
882 (setq pass (substring pass 0 -1))))))
883 (clear-this-command-keys)
884 (message nil)
885 (or pass default ""))))
887 (defun force-mode-line-update (&optional all)
888 "Force the mode-line of the current buffer to be redisplayed.
889 With optional non-nil ALL, force redisplay of all mode-lines."
890 (if all (save-excursion (set-buffer (other-buffer))))
891 (set-buffer-modified-p (buffer-modified-p)))
893 (defun momentary-string-display (string pos &optional exit-char message)
894 "Momentarily display STRING in the buffer at POS.
895 Display remains until next character is typed.
896 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
897 otherwise it is then available as input (as a command if nothing else).
898 Display MESSAGE (optional fourth arg) in the echo area.
899 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
900 (or exit-char (setq exit-char ?\ ))
901 (let ((inhibit-read-only t)
902 ;; Don't modify the undo list at all.
903 (buffer-undo-list t)
904 (modified (buffer-modified-p))
905 (name buffer-file-name)
906 insert-end)
907 (unwind-protect
908 (progn
909 (save-excursion
910 (goto-char pos)
911 ;; defeat file locking... don't try this at home, kids!
912 (setq buffer-file-name nil)
913 (insert-before-markers string)
914 (setq insert-end (point))
915 ;; If the message end is off screen, recenter now.
916 (if (< (window-end nil t) insert-end)
917 (recenter (/ (window-height) 2)))
918 ;; If that pushed message start off the screen,
919 ;; scroll to start it at the top of the screen.
920 (move-to-window-line 0)
921 (if (> (point) pos)
922 (progn
923 (goto-char pos)
924 (recenter 0))))
925 (message (or message "Type %s to continue editing.")
926 (single-key-description exit-char))
927 (let ((char (read-event)))
928 (or (eq char exit-char)
929 (setq unread-command-events (list char)))))
930 (if insert-end
931 (save-excursion
932 (delete-region pos insert-end)))
933 (setq buffer-file-name name)
934 (set-buffer-modified-p modified))))
937 ;;;; Miscellanea.
939 ;; A number of major modes set this locally.
940 ;; Give it a global value to avoid compiler warnings.
941 (defvar font-lock-defaults nil)
943 (defvar suspend-hook nil
944 "Normal hook run by `suspend-emacs', before suspending.")
946 (defvar suspend-resume-hook nil
947 "Normal hook run by `suspend-emacs', after Emacs is continued.")
949 ;; Avoid compiler warnings about this variable,
950 ;; which has a special meaning on certain system types.
951 (defvar buffer-file-type nil
952 "Non-nil if the visited file is a binary file.
953 This variable is meaningful on MS-DOG and Windows NT.
954 On those systems, it is automatically local in every buffer.
955 On other systems, this variable is normally always nil.")
957 ;; This should probably be written in C (i.e., without using `walk-windows').
958 (defun get-buffer-window-list (buffer &optional minibuf frame)
959 "Return windows currently displaying BUFFER, or nil if none.
960 See `walk-windows' for the meaning of MINIBUF and FRAME."
961 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
962 (walk-windows (function (lambda (window)
963 (if (eq (window-buffer window) buffer)
964 (setq windows (cons window windows)))))
965 minibuf frame)
966 windows))
968 (defun ignore (&rest ignore)
969 "Do nothing and return nil.
970 This function accepts any number of arguments, but ignores them."
971 (interactive)
972 nil)
974 (defun error (&rest args)
975 "Signal an error, making error message by passing all args to `format'.
976 In Emacs, the convention is that error messages start with a capital
977 letter but *do not* end with a period. Please follow this convention
978 for the sake of consistency."
979 (while t
980 (signal 'error (list (apply 'format args)))))
982 (defalias 'user-original-login-name 'user-login-name)
984 (defun start-process-shell-command (name buffer &rest args)
985 "Start a program in a subprocess. Return the process object for it.
986 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
987 NAME is name for process. It is modified if necessary to make it unique.
988 BUFFER is the buffer or (buffer-name) to associate with the process.
989 Process output goes at end of that buffer, unless you specify
990 an output stream or filter function to handle the output.
991 BUFFER may be also nil, meaning that this process is not associated
992 with any buffer
993 Third arg is command name, the name of a shell command.
994 Remaining arguments are the arguments for the command.
995 Wildcards and redirection are handled as usual in the shell."
996 (cond
997 ((eq system-type 'vax-vms)
998 (apply 'start-process name buffer args))
999 ;; We used to use `exec' to replace the shell with the command,
1000 ;; but that failed to handle (...) and semicolon, etc.
1002 (start-process name buffer shell-file-name shell-command-switch
1003 (mapconcat 'identity args " ")))))
1005 (defmacro with-current-buffer (buffer &rest body)
1006 "Execute the forms in BODY with BUFFER as the current buffer.
1007 The value returned is the value of the last form in BODY.
1008 See also `with-temp-buffer'."
1009 (cons 'save-current-buffer
1010 (cons (list 'set-buffer buffer)
1011 body)))
1013 (defmacro with-temp-file (file &rest body)
1014 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1015 The value returned is the value of the last form in BODY.
1016 See also `with-temp-buffer'."
1017 (let ((temp-file (make-symbol "temp-file"))
1018 (temp-buffer (make-symbol "temp-buffer")))
1019 `(let ((,temp-file ,file)
1020 (,temp-buffer
1021 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1022 (unwind-protect
1023 (prog1
1024 (with-current-buffer ,temp-buffer
1025 ,@body)
1026 (with-current-buffer ,temp-buffer
1027 (widen)
1028 (write-region (point-min) (point-max) ,temp-file nil 0)))
1029 (and (buffer-name ,temp-buffer)
1030 (kill-buffer ,temp-buffer))))))
1032 (defmacro with-temp-message (message &rest body)
1033 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1034 The original message is restored to the echo area after BODY has finished.
1035 The value returned is the value of the last form in BODY.
1036 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1037 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1038 Use a MESSAGE of \"\" to temporarily clear the echo area."
1039 (let ((current-message (make-symbol "current-message"))
1040 (temp-message (make-symbol "with-temp-message")))
1041 `(let ((,temp-message ,message)
1042 (,current-message))
1043 (unwind-protect
1044 (progn
1045 (when ,temp-message
1046 (setq ,current-message (current-message))
1047 (message "%s" ,temp-message))
1048 ,@body)
1049 (and ,temp-message ,current-message
1050 (message "%s" ,current-message))))))
1052 (defmacro with-temp-buffer (&rest body)
1053 "Create a temporary buffer, and evaluate BODY there like `progn'.
1054 See also `with-temp-file' and `with-output-to-string'."
1055 (let ((temp-buffer (make-symbol "temp-buffer")))
1056 `(let ((,temp-buffer
1057 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1058 (unwind-protect
1059 (with-current-buffer ,temp-buffer
1060 ,@body)
1061 (and (buffer-name ,temp-buffer)
1062 (kill-buffer ,temp-buffer))))))
1064 (defmacro with-output-to-string (&rest body)
1065 "Execute BODY, return the text it sent to `standard-output', as a string."
1066 `(let ((standard-output
1067 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1068 (let ((standard-output standard-output))
1069 ,@body)
1070 (with-current-buffer standard-output
1071 (prog1
1072 (buffer-string)
1073 (kill-buffer nil)))))
1075 (defmacro combine-after-change-calls (&rest body)
1076 "Execute BODY, but don't call the after-change functions till the end.
1077 If BODY makes changes in the buffer, they are recorded
1078 and the functions on `after-change-functions' are called several times
1079 when BODY is finished.
1080 The return value is the value of the last form in BODY.
1082 If `before-change-functions' is non-nil, then calls to the after-change
1083 functions can't be deferred, so in that case this macro has no effect.
1085 Do not alter `after-change-functions' or `before-change-functions'
1086 in BODY."
1087 `(unwind-protect
1088 (let ((combine-after-change-calls t))
1089 . ,body)
1090 (combine-after-change-execute)))
1093 (defvar combine-run-hooks t
1094 "List of hooks delayed. Or t if we're not delaying hooks.")
1096 (defmacro combine-run-hooks (&rest body)
1097 "Execute BODY, but delay any `run-hooks' until the end."
1098 (let ((saved-combine-run-hooks (make-symbol "saved-combine-run-hooks"))
1099 (saved-run-hooks (make-symbol "saved-run-hooks")))
1100 `(let ((,saved-combine-run-hooks combine-run-hooks)
1101 (,saved-run-hooks (symbol-function 'run-hooks)))
1102 (unwind-protect
1103 (progn
1104 ;; If we're not delaying hooks yet, setup the delaying mode
1105 (unless (listp combine-run-hooks)
1106 (setq combine-run-hooks nil)
1107 (fset 'run-hooks
1108 ,(lambda (&rest hooks)
1109 (setq combine-run-hooks
1110 (append combine-run-hooks hooks)))))
1111 ,@body)
1112 ;; If we were not already delaying, then it's now time to set things
1113 ;; back to normal and to execute the delayed hooks.
1114 (unless (listp ,saved-combine-run-hooks)
1115 (setq ,saved-combine-run-hooks combine-run-hooks)
1116 (fset 'run-hooks ,saved-run-hooks)
1117 (setq combine-run-hooks t)
1118 (apply 'run-hooks ,saved-combine-run-hooks))))))
1121 (defmacro with-syntax-table (table &rest body)
1122 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1123 The syntax table of the current buffer is saved, BODY is evaluated, and the
1124 saved table is restored, even in case of an abnormal exit.
1125 Value is what BODY returns."
1126 (let ((old-table (make-symbol "table"))
1127 (old-buffer (make-symbol "buffer")))
1128 `(let ((,old-table (syntax-table))
1129 (,old-buffer (current-buffer)))
1130 (unwind-protect
1131 (progn
1132 (set-syntax-table (copy-syntax-table ,table))
1133 ,@body)
1134 (save-current-buffer
1135 (set-buffer ,old-buffer)
1136 (set-syntax-table ,old-table))))))
1138 (defvar save-match-data-internal)
1140 ;; We use save-match-data-internal as the local variable because
1141 ;; that works ok in practice (people should not use that variable elsewhere).
1142 ;; We used to use an uninterned symbol; the compiler handles that properly
1143 ;; now, but it generates slower code.
1144 (defmacro save-match-data (&rest body)
1145 "Execute the BODY forms, restoring the global value of the match data."
1146 ;; It is better not to use backquote here,
1147 ;; because that makes a bootstrapping problem
1148 ;; if you need to recompile all the Lisp files using interpreted code.
1149 (list 'let
1150 '((save-match-data-internal (match-data)))
1151 (list 'unwind-protect
1152 (cons 'progn body)
1153 '(set-match-data save-match-data-internal))))
1155 (defun match-string (num &optional string)
1156 "Return string of text matched by last search.
1157 NUM specifies which parenthesized expression in the last regexp.
1158 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1159 Zero means the entire text matched by the whole regexp or whole string.
1160 STRING should be given if the last search was by `string-match' on STRING."
1161 (if (match-beginning num)
1162 (if string
1163 (substring string (match-beginning num) (match-end num))
1164 (buffer-substring (match-beginning num) (match-end num)))))
1166 (defun match-string-no-properties (num &optional string)
1167 "Return string of text matched by last search, without text properties.
1168 NUM specifies which parenthesized expression in the last regexp.
1169 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1170 Zero means the entire text matched by the whole regexp or whole string.
1171 STRING should be given if the last search was by `string-match' on STRING."
1172 (if (match-beginning num)
1173 (if string
1174 (let ((result
1175 (substring string (match-beginning num) (match-end num))))
1176 (set-text-properties 0 (length result) nil result)
1177 result)
1178 (buffer-substring-no-properties (match-beginning num)
1179 (match-end num)))))
1181 (defun split-string (string &optional separators)
1182 "Splits STRING into substrings where there are matches for SEPARATORS.
1183 Each match for SEPARATORS is a splitting point.
1184 The substrings between the splitting points are made into a list
1185 which is returned.
1186 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1188 If there is match for SEPARATORS at the beginning of STRING, we do not
1189 include a null substring for that. Likewise, if there is a match
1190 at the end of STRING, we don't include a null substring for that.
1192 Modifies the match data; use `save-match-data' if necessary."
1193 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1194 (start 0)
1195 notfirst
1196 (list nil))
1197 (while (and (string-match rexp string
1198 (if (and notfirst
1199 (= start (match-beginning 0))
1200 (< start (length string)))
1201 (1+ start) start))
1202 (< (match-beginning 0) (length string)))
1203 (setq notfirst t)
1204 (or (eq (match-beginning 0) 0)
1205 (and (eq (match-beginning 0) (match-end 0))
1206 (eq (match-beginning 0) start))
1207 (setq list
1208 (cons (substring string start (match-beginning 0))
1209 list)))
1210 (setq start (match-end 0)))
1211 (or (eq start (length string))
1212 (setq list
1213 (cons (substring string start)
1214 list)))
1215 (nreverse list)))
1217 (defun subst-char-in-string (fromchar tochar string &optional inplace)
1218 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1219 Unless optional argument INPLACE is non-nil, return a new string."
1220 (let ((i (length string))
1221 (newstr (if inplace string (copy-sequence string))))
1222 (while (> i 0)
1223 (setq i (1- i))
1224 (if (eq (aref newstr i) fromchar)
1225 (aset newstr i tochar)))
1226 newstr))
1228 (defun replace-regexp-in-string (regexp rep string &optional
1229 fixedcase literal subexp start)
1230 "Replace all matches for REGEXP with REP in STRING.
1232 Return a new string containing the replacements.
1234 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1235 arguments with the same names of function `replace-match'. If START
1236 is non-nil, start replacements at that index in STRING.
1238 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1239 function. If it is a function it is applied to each match to generate
1240 the replacement passed to `replace-match'; the match-data at this
1241 point are such that match 0 is the function's argument.
1243 To replace only the first match (if any), make REGEXP match up to \\'
1244 and replace a sub-expression, e.g.
1245 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
1246 => \" bar foo\"
1249 ;; To avoid excessive consing from multiple matches in long strings,
1250 ;; don't just call `replace-match' continually. Walk down the
1251 ;; string looking for matches of REGEXP and building up a (reversed)
1252 ;; list MATCHES. This comprises segments of STRING which weren't
1253 ;; matched interspersed with replacements for segments that were.
1254 ;; [For a `large' number of replacments it's more efficient to
1255 ;; operate in a temporary buffer; we can't tell from the function's
1256 ;; args whether to choose the buffer-based implementation, though it
1257 ;; might be reasonable to do so for long enough STRING.]
1258 (let ((l (length string))
1259 (start (or start 0))
1260 matches str mb me)
1261 (save-match-data
1262 (while (and (< start l) (string-match regexp string start))
1263 (setq mb (match-beginning 0)
1264 me (match-end 0))
1265 ;; If we matched the empty string, make sure we advance by one char
1266 (when (= me mb) (setq me (min l (1+ mb))))
1267 ;; Generate a replacement for the matched substring.
1268 ;; Operate only on the substring to minimize string consing.
1269 ;; Set up match data for the substring for replacement;
1270 ;; presumably this is likely to be faster than munging the
1271 ;; match data directly in Lisp.
1272 (string-match regexp (setq str (substring string mb me)))
1273 (setq matches
1274 (cons (replace-match (if (stringp rep)
1276 (funcall rep (match-string 0 str)))
1277 fixedcase literal str subexp)
1278 (cons (substring string start mb) ; unmatched prefix
1279 matches)))
1280 (setq start me))
1281 ;; Reconstruct a string from the pieces.
1282 (setq matches (cons (substring string start l) matches)) ; leftover
1283 (apply #'concat (nreverse matches)))))
1285 (defun shell-quote-argument (argument)
1286 "Quote an argument for passing as argument to an inferior shell."
1287 (if (eq system-type 'ms-dos)
1288 ;; Quote using double quotes, but escape any existing quotes in
1289 ;; the argument with backslashes.
1290 (let ((result "")
1291 (start 0)
1292 end)
1293 (if (or (null (string-match "[^\"]" argument))
1294 (< (match-end 0) (length argument)))
1295 (while (string-match "[\"]" argument start)
1296 (setq end (match-beginning 0)
1297 result (concat result (substring argument start end)
1298 "\\" (substring argument end (1+ end)))
1299 start (1+ end))))
1300 (concat "\"" result (substring argument start) "\""))
1301 (if (eq system-type 'windows-nt)
1302 (concat "\"" argument "\"")
1303 (if (equal argument "")
1304 "''"
1305 ;; Quote everything except POSIX filename characters.
1306 ;; This should be safe enough even for really weird shells.
1307 (let ((result "") (start 0) end)
1308 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1309 (setq end (match-beginning 0)
1310 result (concat result (substring argument start end)
1311 "\\" (substring argument end (1+ end)))
1312 start (1+ end)))
1313 (concat result (substring argument start)))))))
1315 (defun make-syntax-table (&optional oldtable)
1316 "Return a new syntax table.
1317 If OLDTABLE is non-nil, copy OLDTABLE.
1318 Otherwise, create a syntax table which inherits
1319 all letters and control characters from the standard syntax table;
1320 other characters are copied from the standard syntax table."
1321 (if oldtable
1322 (copy-syntax-table oldtable)
1323 (let ((table (copy-syntax-table))
1325 (setq i 0)
1326 (while (<= i 31)
1327 (aset table i nil)
1328 (setq i (1+ i)))
1329 (setq i ?A)
1330 (while (<= i ?Z)
1331 (aset table i nil)
1332 (setq i (1+ i)))
1333 (setq i ?a)
1334 (while (<= i ?z)
1335 (aset table i nil)
1336 (setq i (1+ i)))
1337 (setq i 128)
1338 (while (<= i 255)
1339 (aset table i nil)
1340 (setq i (1+ i)))
1341 table)))
1343 (defun add-to-invisibility-spec (arg)
1344 "Add elements to `buffer-invisibility-spec'.
1345 See documentation for `buffer-invisibility-spec' for the kind of elements
1346 that can be added."
1347 (cond
1348 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
1349 (setq buffer-invisibility-spec (list arg)))
1351 (setq buffer-invisibility-spec
1352 (cons arg buffer-invisibility-spec)))))
1354 (defun remove-from-invisibility-spec (arg)
1355 "Remove elements from `buffer-invisibility-spec'."
1356 (if (consp buffer-invisibility-spec)
1357 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
1359 (defun global-set-key (key command)
1360 "Give KEY a global binding as COMMAND.
1361 COMMAND is the command definition to use; usually it is
1362 a symbol naming an interactively-callable function.
1363 KEY is a key sequence; noninteractively, it is a string or vector
1364 of characters or event types, and non-ASCII characters with codes
1365 above 127 (such as ISO Latin-1) can be included if you use a vector.
1367 Note that if KEY has a local binding in the current buffer,
1368 that local binding will continue to shadow any global binding
1369 that you make with this function."
1370 (interactive "KSet key globally: \nCSet key %s to command: ")
1371 (or (vectorp key) (stringp key)
1372 (signal 'wrong-type-argument (list 'arrayp key)))
1373 (define-key (current-global-map) key command))
1375 (defun local-set-key (key command)
1376 "Give KEY a local binding as COMMAND.
1377 COMMAND is the command definition to use; usually it is
1378 a symbol naming an interactively-callable function.
1379 KEY is a key sequence; noninteractively, it is a string or vector
1380 of characters or event types, and non-ASCII characters with codes
1381 above 127 (such as ISO Latin-1) can be included if you use a vector.
1383 The binding goes in the current buffer's local map,
1384 which in most cases is shared with all other buffers in the same major mode."
1385 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1386 (let ((map (current-local-map)))
1387 (or map
1388 (use-local-map (setq map (make-sparse-keymap))))
1389 (or (vectorp key) (stringp key)
1390 (signal 'wrong-type-argument (list 'arrayp key)))
1391 (define-key map key command)))
1393 (defun global-unset-key (key)
1394 "Remove global binding of KEY.
1395 KEY is a string representing a sequence of keystrokes."
1396 (interactive "kUnset key globally: ")
1397 (global-set-key key nil))
1399 (defun local-unset-key (key)
1400 "Remove local binding of KEY.
1401 KEY is a string representing a sequence of keystrokes."
1402 (interactive "kUnset key locally: ")
1403 (if (current-local-map)
1404 (local-set-key key nil))
1405 nil)
1407 ;; We put this here instead of in frame.el so that it's defined even on
1408 ;; systems where frame.el isn't loaded.
1409 (defun frame-configuration-p (object)
1410 "Return non-nil if OBJECT seems to be a frame configuration.
1411 Any list whose car is `frame-configuration' is assumed to be a frame
1412 configuration."
1413 (and (consp object)
1414 (eq (car object) 'frame-configuration)))
1416 (defun functionp (object)
1417 "Non-nil if OBJECT is a type of object that can be called as a function."
1418 (or (subrp object) (byte-code-function-p object)
1419 (eq (car-safe object) 'lambda)
1420 (and (symbolp object) (fboundp object))))
1422 ;; now in fns.c
1423 ;(defun nth (n list)
1424 ; "Returns the Nth element of LIST.
1425 ;N counts from zero. If LIST is not that long, nil is returned."
1426 ; (car (nthcdr n list)))
1428 ;(defun copy-alist (alist)
1429 ; "Return a copy of ALIST.
1430 ;This is a new alist which represents the same mapping
1431 ;from objects to objects, but does not share the alist structure with ALIST.
1432 ;The objects mapped (cars and cdrs of elements of the alist)
1433 ;are shared, however."
1434 ; (setq alist (copy-sequence alist))
1435 ; (let ((tail alist))
1436 ; (while tail
1437 ; (if (consp (car tail))
1438 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1439 ; (setq tail (cdr tail))))
1440 ; alist)
1442 (defun assq-delete-all (key alist)
1443 "Delete from ALIST all elements whose car is KEY.
1444 Return the modified alist."
1445 (let ((tail alist))
1446 (while tail
1447 (if (eq (car (car tail)) key)
1448 (setq alist (delq (car tail) alist)))
1449 (setq tail (cdr tail)))
1450 alist))
1452 (defun make-temp-file (prefix &optional dir-flag)
1453 "Create a temporary file.
1454 The returned file name (created by appending some random characters at the end
1455 of PREFIX, and expanding against `temporary-file-directory' if necessary,
1456 is guaranteed to point to a newly created empty file.
1457 You can then use `write-region' to write new data into the file.
1459 If DIR-FLAG is non-nil, create a new empty directory instead of a file."
1460 (let (file)
1461 (while (condition-case ()
1462 (progn
1463 (setq file
1464 (make-temp-name
1465 (expand-file-name prefix temporary-file-directory)))
1466 (if dir-flag
1467 (make-directory file)
1468 (write-region "" nil file nil 'silent nil 'excl))
1469 nil)
1470 (file-already-exists t))
1471 ;; the file was somehow created by someone else between
1472 ;; `make-temp-name' and `write-region', let's try again.
1473 nil)
1474 file))
1476 ;;; subr.el ends here