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