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