1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
23 (defvar custom-declare-variable-list nil
24 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
25 Each element of this list holds the arguments to one call to `defcustom'.")
27 ;; Use this, rather than defcustom, in subr.el and other files loaded
29 (defun custom-declare-variable-early (&rest arguments
)
30 (setq custom-declare-variable-list
31 (cons arguments custom-declare-variable-list
)))
33 ;;;; Lisp language features.
35 (defmacro lambda
(&rest cdr
)
36 "Return a lambda expression.
37 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
38 self-quoting; the result of evaluating the lambda expression is the
39 expression itself. The lambda expression may then be treated as a
40 function, i.e., stored as the function value of a symbol, passed to
41 funcall or mapcar, etc.
43 ARGS should take the same form as an argument list for a `defun'.
44 DOCSTRING is an optional documentation string.
45 If present, it should describe how to call the function.
46 But documentation strings are usually not useful in nameless functions.
47 INTERACTIVE should be a call to the function `interactive', which see.
48 It may also be omitted.
49 BODY should be a list of lisp expressions."
50 ;; Note that this definition should not use backquotes; subr.el should not
51 ;; depend on backquote.el.
52 (list 'function
(cons 'lambda cdr
)))
54 (defmacro push
(newelt listname
)
55 "Add NEWELT to the list stored in the symbol LISTNAME.
56 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
57 LISTNAME must be a symbol."
59 (list 'cons newelt listname
)))
61 (defmacro pop
(listname)
62 "Return the first element of LISTNAME's value, and remove it from the list.
63 LISTNAME must be a symbol whose value is a list.
64 If the value is nil, `pop' returns nil but does not actually
66 (list 'prog1
(list 'car listname
)
67 (list 'setq listname
(list 'cdr listname
))))
69 (defmacro when
(cond &rest body
)
70 "If COND yields non-nil, do BODY, else return nil."
71 (list 'if cond
(cons 'progn body
)))
73 (defmacro unless
(cond &rest body
)
74 "If COND yields nil, do BODY, else return nil."
75 (cons 'if
(cons cond
(cons nil body
))))
77 (defmacro dolist
(spec &rest body
)
78 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
79 Evaluate BODY with VAR bound to each car from LIST, in turn.
80 Then evaluate RESULT to get return value, default nil."
81 (let ((temp (make-symbol "--dolist-temp--")))
82 (list 'let
(list (list temp
(nth 1 spec
)) (car spec
))
84 (list 'setq
(car spec
) (list 'car temp
))
87 (list (list 'setq temp
(list 'cdr temp
))))))
90 (cons (list 'setq
(car spec
) nil
) (cdr (cdr spec
))))))))
92 (defmacro dotimes
(spec &rest body
)
93 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
94 Evaluate BODY with VAR bound to successive integers running from 0,
95 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
96 the return value (nil if RESULT is omitted)."
97 (let ((temp (make-symbol "--dotimes-temp--")))
98 (list 'let
(list (list temp
(nth 1 spec
)) (list (car spec
) 0))
99 (list 'while
(list '< (car spec
) temp
)
101 (append body
(list (list 'setq
(car spec
)
102 (list '1+ (car spec
)))))))
104 (car (cdr (cdr spec
)))
108 "Return the car of the car of X."
112 "Return the car of the cdr of X."
116 "Return the cdr of the car of X."
120 "Return the cdr of the cdr of X."
123 (defun last (x &optional n
)
124 "Return the last link of the list X. Its car is the last element.
125 If X is nil, return nil.
126 If N is non-nil, return the Nth-to-last link of X.
127 If N is bigger than the length of X, return X."
131 (setq m
(1+ m
) p
(cdr p
)))
133 (if (< n m
) (nthcdr (- m n
) x
) x
)))
138 (defun assoc-default (key alist
&optional test default
)
139 "Find object KEY in a pseudo-alist ALIST.
140 ALIST is a list of conses or objects. Each element (or the element's car,
141 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
142 If that is non-nil, the element matches;
143 then `assoc-default' returns the element's cdr, if it is a cons,
144 or DEFAULT if the element is not a cons.
146 If no element matches, the value is nil.
147 If TEST is omitted or nil, `equal' is used."
148 (let (found (tail alist
) value
)
149 (while (and tail
(not found
))
150 (let ((elt (car tail
)))
151 (when (funcall (or test
'equal
) (if (consp elt
) (car elt
) elt
) key
)
152 (setq found t value
(if (consp elt
) (cdr elt
) default
))))
153 (setq tail
(cdr tail
)))
156 (defun assoc-ignore-case (key alist
)
157 "Like `assoc', but ignores differences in case and text representation.
158 KEY must be a string. Upper-case and lower-case letters are treated as equal.
159 Unibyte strings are converted to multibyte for comparison."
161 (while (and alist
(not element
))
162 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil t
))
163 (setq element
(car alist
)))
164 (setq alist
(cdr alist
)))
167 (defun assoc-ignore-representation (key alist
)
168 "Like `assoc', but ignores differences in text representation.
169 KEY must be a string.
170 Unibyte strings are converted to multibyte for comparison."
172 (while (and alist
(not element
))
173 (if (eq t
(compare-strings key
0 nil
(car (car alist
)) 0 nil
))
174 (setq element
(car alist
)))
175 (setq alist
(cdr alist
)))
178 (defun member-ignore-case (elt list
)
179 "Like `member', but ignores differences in case and text representation.
180 ELT must be a string. Upper-case and lower-case letters are treated as equal.
181 Unibyte strings are converted to multibyte for comparison."
183 (while (and list
(not element
))
184 (if (eq t
(compare-strings elt
0 nil
(car list
) 0 nil t
))
185 (setq element
(car list
)))
186 (setq list
(cdr list
)))
196 ;Prevent the \{...} documentation construct
197 ;from mentioning keys that run this command.
198 (put 'undefined
'suppress-keymap t
)
200 (defun suppress-keymap (map &optional nodigits
)
201 "Make MAP override all normally self-inserting keys to be undefined.
202 Normally, as an exception, digits and minus-sign are set to make prefix args,
203 but optional second arg NODIGITS non-nil treats them like other chars."
204 (substitute-key-definition 'self-insert-command
'undefined map global-map
)
207 (define-key map
"-" 'negative-argument
)
208 ;; Make plain numbers do numeric args.
211 (define-key map
(char-to-string loop
) 'digit-argument
)
212 (setq loop
(1+ loop
))))))
215 ;(defun copy-keymap (keymap)
216 ; "Return a copy of KEYMAP"
217 ; (while (not (keymapp keymap))
218 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
219 ; (if (vectorp keymap)
220 ; (copy-sequence keymap)
221 ; (copy-alist keymap)))
223 (defvar key-substitution-in-progress nil
224 "Used internally by substitute-key-definition.")
226 (defun substitute-key-definition (olddef newdef keymap
&optional oldmap prefix
)
227 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
228 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
229 Alternatively, if optional fourth argument OLDMAP is specified, we redefine
230 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
231 (or prefix
(setq prefix
""))
232 (let* ((scan (or oldmap keymap
))
234 (prefix1 (vconcat prefix vec1
))
235 (key-substitution-in-progress
236 (cons scan key-substitution-in-progress
)))
237 ;; Scan OLDMAP, finding each char or event-symbol that
238 ;; has any definition, and act on it with hack-key.
240 (if (consp (car scan
))
241 (let ((char (car (car scan
)))
242 (defn (cdr (car scan
))))
243 ;; The inside of this let duplicates exactly
244 ;; the inside of the following let that handles array elements.
246 (aset prefix1
(length prefix
) char
)
247 (let (inner-def skipped
)
248 ;; Skip past menu-prompt.
249 (while (stringp (car-safe defn
))
250 (setq skipped
(cons (car defn
) skipped
))
251 (setq defn
(cdr defn
)))
252 ;; Skip past cached key-equivalence data for menu items.
253 (and (consp defn
) (consp (car defn
))
254 (setq defn
(cdr defn
)))
255 (setq inner-def defn
)
256 ;; Look past a symbol that names a keymap.
257 (while (and (symbolp inner-def
)
259 (setq inner-def
(symbol-function inner-def
)))
260 (if (or (eq defn olddef
)
261 ;; Compare with equal if definition is a key sequence.
262 ;; That is useful for operating on function-key-map.
263 (and (or (stringp defn
) (vectorp defn
))
264 (equal defn olddef
)))
265 (define-key keymap prefix1
(nconc (nreverse skipped
) newdef
))
266 (if (and (keymapp defn
)
267 ;; Avoid recursively scanning
268 ;; where KEYMAP does not have a submap.
269 (let ((elt (lookup-key keymap prefix1
)))
272 ;; Avoid recursively rescanning keymap being scanned.
274 key-substitution-in-progress
)))
275 ;; If this one isn't being scanned already,
277 (substitute-key-definition olddef newdef keymap
280 (if (vectorp (car scan
))
281 (let* ((array (car scan
))
285 (let ((char i
) (defn (aref array i
)))
286 ;; The inside of this let duplicates exactly
287 ;; the inside of the previous let.
289 (aset prefix1
(length prefix
) char
)
290 (let (inner-def skipped
)
291 ;; Skip past menu-prompt.
292 (while (stringp (car-safe defn
))
293 (setq skipped
(cons (car defn
) skipped
))
294 (setq defn
(cdr defn
)))
295 (and (consp defn
) (consp (car defn
))
296 (setq defn
(cdr defn
)))
297 (setq inner-def defn
)
298 (while (and (symbolp inner-def
)
300 (setq inner-def
(symbol-function inner-def
)))
301 (if (or (eq defn olddef
)
302 (and (or (stringp defn
) (vectorp defn
))
303 (equal defn olddef
)))
304 (define-key keymap prefix1
305 (nconc (nreverse skipped
) newdef
))
306 (if (and (keymapp defn
)
307 (let ((elt (lookup-key keymap prefix1
)))
311 key-substitution-in-progress
)))
312 (substitute-key-definition olddef newdef keymap
316 (if (char-table-p (car scan
))
318 (function (lambda (char defn
)
320 ;; The inside of this let duplicates exactly
321 ;; the inside of the previous let,
322 ;; except that it uses set-char-table-range
323 ;; instead of define-key.
325 (aset prefix1
(length prefix
) char
)
326 (let (inner-def skipped
)
327 ;; Skip past menu-prompt.
328 (while (stringp (car-safe defn
))
329 (setq skipped
(cons (car defn
) skipped
))
330 (setq defn
(cdr defn
)))
331 (and (consp defn
) (consp (car defn
))
332 (setq defn
(cdr defn
)))
333 (setq inner-def defn
)
334 (while (and (symbolp inner-def
)
336 (setq inner-def
(symbol-function inner-def
)))
337 (if (or (eq defn olddef
)
338 (and (or (stringp defn
) (vectorp defn
))
339 (equal defn olddef
)))
340 (define-key keymap prefix1
341 (nconc (nreverse skipped
) newdef
))
342 (if (and (keymapp defn
)
343 (let ((elt (lookup-key keymap prefix1
)))
347 key-substitution-in-progress
)))
348 (substitute-key-definition olddef newdef keymap
352 (setq scan
(cdr scan
)))))
354 (defun define-key-after (keymap key definition
&optional after
)
355 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
356 This is like `define-key' except that the binding for KEY is placed
357 just after the binding for the event AFTER, instead of at the beginning
358 of the map. Note that AFTER must be an event type (like KEY), NOT a command
361 If AFTER is t or omitted, the new binding goes at the end of the keymap.
363 KEY must contain just one event type--that is to say, it must be a
364 string or vector of length 1, but AFTER should be a single event
365 type--a symbol or a character, not a sequence.
367 Bindings are always added before any inherited map.
369 The order of bindings in a keymap matters when it is used as a menu."
370 (unless after
(setq after t
))
372 (signal 'wrong-type-argument
(list 'keymapp keymap
)))
373 (if (> (length key
) 1)
374 (error "multi-event key specified in `define-key-after'"))
375 (let ((tail keymap
) done inserted
376 (first (aref key
0)))
377 (while (and (not done
) tail
)
378 ;; Delete any earlier bindings for the same key.
379 (if (eq (car-safe (car (cdr tail
))) first
)
380 (setcdr tail
(cdr (cdr tail
))))
381 ;; When we reach AFTER's binding, insert the new binding after.
382 ;; If we reach an inherited keymap, insert just before that.
383 ;; If we reach the end of this keymap, insert at the end.
384 (if (or (and (eq (car-safe (car tail
)) after
)
386 (eq (car (cdr tail
)) 'keymap
)
389 ;; Stop the scan only if we find a parent keymap.
390 ;; Keep going past the inserted element
391 ;; so we can delete any duplications that come later.
392 (if (eq (car (cdr tail
)) 'keymap
)
394 ;; Don't insert more than once.
396 (setcdr tail
(cons (cons (aref key
0) definition
) (cdr tail
))))
398 (setq tail
(cdr tail
)))))
401 "Convert KEYS to the internal Emacs key representation.
402 KEYS should be a string constant in the format used for
403 saving keyboard macros (see `insert-kbd-macro')."
404 (read-kbd-macro keys
))
406 (put 'keyboard-translate-table
'char-table-extra-slots
0)
408 (defun keyboard-translate (from to
)
409 "Translate character FROM to TO at a low level.
410 This function creates a `keyboard-translate-table' if necessary
411 and then modifies one entry in it."
412 (or (char-table-p keyboard-translate-table
)
413 (setq keyboard-translate-table
414 (make-char-table 'keyboard-translate-table nil
)))
415 (aset keyboard-translate-table from to
))
418 ;;;; The global keymap tree.
420 ;;; global-map, esc-map, and ctl-x-map have their values set up in
421 ;;; keymap.c; we just give them docstrings here.
423 (defvar global-map nil
424 "Default global keymap mapping Emacs keyboard input into commands.
425 The value is a keymap which is usually (but not necessarily) Emacs's
429 "Default keymap for ESC (meta) commands.
430 The normal global definition of the character ESC indirects to this keymap.")
432 (defvar ctl-x-map nil
433 "Default keymap for C-x commands.
434 The normal global definition of the character C-x indirects to this keymap.")
436 (defvar ctl-x-4-map
(make-sparse-keymap)
437 "Keymap for subcommands of C-x 4")
438 (defalias 'ctl-x-4-prefix ctl-x-4-map
)
439 (define-key ctl-x-map
"4" 'ctl-x-4-prefix
)
441 (defvar ctl-x-5-map
(make-sparse-keymap)
442 "Keymap for frame commands.")
443 (defalias 'ctl-x-5-prefix ctl-x-5-map
)
444 (define-key ctl-x-map
"5" 'ctl-x-5-prefix
)
447 ;;;; Event manipulation functions.
449 ;; The call to `read' is to ensure that the value is computed at load time
450 ;; and not compiled into the .elc file. The value is negative on most
451 ;; machines, but not on all!
452 (defconst listify-key-sequence-1
(logior 128 (read "?\\M-\\^@")))
454 (defun listify-key-sequence (key)
455 "Convert a key sequence to a list of events."
458 (mapcar (function (lambda (c)
460 (logxor c listify-key-sequence-1
)
464 (defsubst eventp
(obj)
465 "True if the argument is an event object."
468 (get obj
'event-symbol-elements
))
471 (get (car obj
) 'event-symbol-elements
))))
473 (defun event-modifiers (event)
474 "Returns a list of symbols representing the modifier keys in event EVENT.
475 The elements of the list may include `meta', `control',
476 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
480 (setq type
(car type
)))
482 (cdr (get type
'event-symbol-elements
))
484 (or (zerop (logand type ?\M-\^
@))
485 (setq list
(cons 'meta list
)))
486 (or (and (zerop (logand type ?\C-\^
@))
487 (>= (logand type
127) 32))
488 (setq list
(cons 'control list
)))
489 (or (and (zerop (logand type ?\S-\^
@))
490 (= (logand type
255) (downcase (logand type
255))))
491 (setq list
(cons 'shift list
)))
492 (or (zerop (logand type ?\H-\^
@))
493 (setq list
(cons 'hyper list
)))
494 (or (zerop (logand type ?\s-\^
@))
495 (setq list
(cons 'super list
)))
496 (or (zerop (logand type ?\A-\^
@))
497 (setq list
(cons 'alt list
)))
500 (defun event-basic-type (event)
501 "Returns the basic type of the given event (all modifiers removed).
502 The value is an ASCII printing character (not upper case) or a symbol."
504 (setq event
(car event
)))
506 (car (get event
'event-symbol-elements
))
507 (let ((base (logand event
(1- (lsh 1 18)))))
508 (downcase (if (< base
32) (logior base
64) base
)))))
510 (defsubst mouse-movement-p
(object)
511 "Return non-nil if OBJECT is a mouse movement event."
513 (eq (car object
) 'mouse-movement
)))
515 (defsubst event-start
(event)
516 "Return the starting position of EVENT.
517 If EVENT is a mouse press or a mouse click, this returns the location
519 If EVENT is a drag, this returns the drag's starting position.
520 The return value is of the form
521 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
522 The `posn-' functions access elements of such lists."
525 (defsubst event-end
(event)
526 "Return the ending location of EVENT. EVENT should be a click or drag event.
527 If EVENT is a click event, this function is the same as `event-start'.
528 The return value is of the form
529 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
530 The `posn-' functions access elements of such lists."
531 (nth (if (consp (nth 2 event
)) 2 1) event
))
533 (defsubst event-click-count
(event)
534 "Return the multi-click count of EVENT, a click or drag event.
535 The return value is a positive integer."
536 (if (integerp (nth 2 event
)) (nth 2 event
) 1))
538 (defsubst posn-window
(position)
539 "Return the window in POSITION.
540 POSITION should be a list of the form
541 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
542 as returned by the `event-start' and `event-end' functions."
545 (defsubst posn-point
(position)
546 "Return the buffer location in POSITION.
547 POSITION should be a list of the form
548 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
549 as returned by the `event-start' and `event-end' functions."
550 (if (consp (nth 1 position
))
551 (car (nth 1 position
))
554 (defsubst posn-x-y
(position)
555 "Return the x and y coordinates in POSITION.
556 POSITION should be a list of the form
557 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
558 as returned by the `event-start' and `event-end' functions."
561 (defun posn-col-row (position)
562 "Return the column and row in POSITION, measured in characters.
563 POSITION should be a list of the form
564 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
565 as returned by the `event-start' and `event-end' functions.
566 For a scroll-bar event, the result column is 0, and the row
567 corresponds to the vertical position of the click in the scroll bar."
568 (let ((pair (nth 2 position
))
569 (window (posn-window position
)))
570 (if (eq (if (consp (nth 1 position
))
571 (car (nth 1 position
))
573 'vertical-scroll-bar
)
574 (cons 0 (scroll-bar-scale pair
(1- (window-height window
))))
575 (if (eq (if (consp (nth 1 position
))
576 (car (nth 1 position
))
578 'horizontal-scroll-bar
)
579 (cons (scroll-bar-scale pair
(window-width window
)) 0)
580 (let* ((frame (if (framep window
) window
(window-frame window
)))
581 (x (/ (car pair
) (frame-char-width frame
)))
582 (y (/ (cdr pair
) (frame-char-height frame
))))
585 (defsubst posn-timestamp
(position)
586 "Return the timestamp of POSITION.
587 POSITION should be a list of the form
588 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
589 as returned by the `event-start' and `event-end' functions."
593 ;;;; Obsolescent names for functions.
595 (defalias 'dot
'point
)
596 (defalias 'dot-marker
'point-marker
)
597 (defalias 'dot-min
'point-min
)
598 (defalias 'dot-max
'point-max
)
599 (defalias 'window-dot
'window-point
)
600 (defalias 'set-window-dot
'set-window-point
)
601 (defalias 'read-input
'read-string
)
602 (defalias 'send-string
'process-send-string
)
603 (defalias 'send-region
'process-send-region
)
604 (defalias 'show-buffer
'set-window-buffer
)
605 (defalias 'buffer-flush-undo
'buffer-disable-undo
)
606 (defalias 'eval-current-buffer
'eval-buffer
)
607 (defalias 'compiled-function-p
'byte-code-function-p
)
608 (defalias 'define-function
'defalias
)
610 (defalias 'sref
'aref
)
611 (make-obsolete 'sref
'aref
)
612 (make-obsolete 'char-bytes
"Now this function always returns 1")
614 ;; Some programs still use this as a function.
616 "Obsolete function returning the value of the `baud-rate' variable.
617 Please convert your programs to use the variable `baud-rate' directly."
620 (defalias 'focus-frame
'ignore
)
621 (defalias 'unfocus-frame
'ignore
)
623 ;;;; Alternate names for functions - these are not being phased out.
625 (defalias 'string
= 'string-equal
)
626 (defalias 'string
< 'string-lessp
)
627 (defalias 'move-marker
'set-marker
)
628 (defalias 'not
'null
)
629 (defalias 'rplaca
'setcar
)
630 (defalias 'rplacd
'setcdr
)
631 (defalias 'beep
'ding
) ;preserve lingual purity
632 (defalias 'indent-to-column
'indent-to
)
633 (defalias 'backward-delete-char
'delete-backward-char
)
634 (defalias 'search-forward-regexp
(symbol-function 're-search-forward
))
635 (defalias 'search-backward-regexp
(symbol-function 're-search-backward
))
636 (defalias 'int-to-string
'number-to-string
)
637 (defalias 'store-match-data
'set-match-data
)
638 (defalias 'point-at-eol
'line-end-position
)
639 (defalias 'point-at-bol
'line-beginning-position
)
641 ;;; Should this be an obsolete name? If you decide it should, you get
642 ;;; to go through all the sources and change them.
643 (defalias 'string-to-int
'string-to-number
)
645 ;;;; Hook manipulation functions.
647 (defun make-local-hook (hook)
648 "Make the hook HOOK local to the current buffer.
649 The return value is HOOK.
651 When a hook is local, its local and global values
652 work in concert: running the hook actually runs all the hook
653 functions listed in *either* the local value *or* the global value
654 of the hook variable.
656 This function works by making `t' a member of the buffer-local value,
657 which acts as a flag to run the hook functions in the default value as
658 well. This works for all normal hooks, but does not work for most
659 non-normal hooks yet. We will be changing the callers of non-normal
660 hooks so that they can handle localness; this has to be done one by
663 This function does nothing if HOOK is already local in the current
666 Do not use `make-local-variable' to make a hook variable buffer-local."
667 (if (local-variable-p hook
)
669 (or (boundp hook
) (set hook nil
))
670 (make-local-variable hook
)
674 (defun add-hook (hook function
&optional append local
)
675 "Add to the value of HOOK the function FUNCTION.
676 FUNCTION is not added if already present.
677 FUNCTION is added (if necessary) at the beginning of the hook list
678 unless the optional argument APPEND is non-nil, in which case
679 FUNCTION is added at the end.
681 The optional fourth argument, LOCAL, if non-nil, says to modify
682 the hook's buffer-local value rather than its default value.
683 This makes the hook buffer-local if needed.
684 To make a hook variable buffer-local, always use
685 `make-local-hook', not `make-local-variable'.
687 HOOK should be a symbol, and FUNCTION may be any valid function. If
688 HOOK is void, it is first set to nil. If HOOK's value is a single
689 function, it is changed to a list of functions."
690 (or (boundp hook
) (set hook nil
))
691 (or (default-boundp hook
) (set-default hook nil
))
692 (if local
(make-local-hook hook
)
693 ;; Detect the case where make-local-variable was used on a hook
694 ;; and do what we used to do.
695 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
697 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
698 ;; If the hook value is a single function, turn it into a list.
699 (when (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
700 (set hook-value
(list hook-value
)))
701 ;; Do the actual addition if necessary
702 (unless (member function hook-value
)
705 (append hook-value
(list function
))
706 (cons function hook-value
))))
707 ;; Set the actual variable
708 (if local
(set hook hook-value
) (set-default hook hook-value
))))
710 (defun remove-hook (hook function
&optional local
)
711 "Remove from the value of HOOK the function FUNCTION.
712 HOOK should be a symbol, and FUNCTION may be any valid function. If
713 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
714 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
716 The optional third argument, LOCAL, if non-nil, says to modify
717 the hook's buffer-local value rather than its default value.
718 This makes the hook buffer-local if needed.
719 To make a hook variable buffer-local, always use
720 `make-local-hook', not `make-local-variable'."
721 (or (boundp hook
) (set hook nil
))
722 (or (default-boundp hook
) (set-default hook nil
))
723 (if local
(make-local-hook hook
)
724 ;; Detect the case where make-local-variable was used on a hook
725 ;; and do what we used to do.
726 (unless (and (consp (symbol-value hook
)) (memq t
(symbol-value hook
)))
728 (let ((hook-value (if local
(symbol-value hook
) (default-value hook
))))
729 ;; If the hook value is a single function, turn it into a list.
730 (when (or (not (listp hook-value
)) (eq (car hook-value
) 'lambda
))
731 (set hook-value
(list hook-value
)))
732 ;; Do the actual removal if necessary
733 (setq hook-value
(delete function
(copy-sequence hook-value
)))
734 ;; If the function is on the global hook, we need to shadow it locally
735 ;;(when (and local (member function (default-value hook))
736 ;; (not (member (cons 'not function) hook-value)))
737 ;; (push (cons 'not function) hook-value))
738 ;; Set the actual variable
739 (if local
(set hook hook-value
) (set-default hook hook-value
))))
741 (defun add-to-list (list-var element
)
742 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
743 The test for presence of ELEMENT is done with `equal'.
744 If ELEMENT is added, it is added at the beginning of the list.
746 If you want to use `add-to-list' on a variable that is not defined
747 until a certain package is loaded, you should put the call to `add-to-list'
748 into a hook function that will be run only after loading the package.
749 `eval-after-load' provides one way to do this. In some cases
750 other hooks, such as major mode hooks, can do the job."
751 (if (member element
(symbol-value list-var
))
752 (symbol-value list-var
)
753 (set list-var
(cons element
(symbol-value list-var
)))))
755 ;;;; Specifying things to do after certain files are loaded.
757 (defun eval-after-load (file form
)
758 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
759 This makes or adds to an entry on `after-load-alist'.
760 If FILE is already loaded, evaluate FORM right now.
761 It does nothing if FORM is already on the list for FILE.
762 FILE should be the name of a library, with no directory name."
763 ;; Make sure there is an element for FILE.
764 (or (assoc file after-load-alist
)
765 (setq after-load-alist
(cons (list file
) after-load-alist
)))
766 ;; Add FORM to the element if it isn't there.
767 (let ((elt (assoc file after-load-alist
)))
768 (or (member form
(cdr elt
))
770 (nconc elt
(list form
))
771 ;; If the file has been loaded already, run FORM right away.
772 (and (assoc file load-history
)
776 (defun eval-next-after-load (file)
777 "Read the following input sexp, and run it whenever FILE is loaded.
778 This makes or adds to an entry on `after-load-alist'.
779 FILE should be the name of a library, with no directory name."
780 (eval-after-load file
(read)))
783 ;;;; Input and display facilities.
785 (defvar read-quoted-char-radix
8
786 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
787 Legitimate radix values are 8, 10 and 16.")
789 (custom-declare-variable-early
790 'read-quoted-char-radix
8
791 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
792 Legitimate radix values are 8, 10 and 16."
793 :type
'(choice (const 8) (const 10) (const 16))
794 :group
'editing-basics
)
796 (defun read-quoted-char (&optional prompt
)
797 "Like `read-char', but do not allow quitting.
798 Also, if the first character read is an octal digit,
799 we read any number of octal digits and return the
800 specified character code. Any nondigit terminates the sequence.
801 If the terminator is RET, it is discarded;
802 any other terminator is used itself as input.
804 The optional argument PROMPT specifies a string to use to prompt the user.
805 The variable `read-quoted-char-radix' controls which radix to use
807 (let ((message-log-max nil
) done
(first t
) (code 0) char
)
809 (let ((inhibit-quit first
)
810 ;; Don't let C-h get the help message--only help function keys.
813 "Type the special character you want to use,
814 or the octal character code.
815 RET terminates the character code and is discarded;
816 any other non-digit terminates the character code and is then used as input."))
817 (setq char
(read-event (and prompt
(format "%s-" prompt
)) t
))
818 (if inhibit-quit
(setq quit-flag nil
)))
819 ;; Translate TAB key into control-I ASCII character, and so on.
821 (let ((translated (lookup-key function-key-map
(vector char
))))
822 (if (arrayp translated
)
823 (setq char
(aref translated
0)))))
825 ((not (integerp char
))
826 (setq unread-command-events
(list char
)
828 ((/= (logand char ?\M-\^
@) 0)
829 ;; Turn a meta-character into a character with the 0200 bit set.
830 (setq code
(logior (logand char
(lognot ?\M-\^
@)) 128)
832 ((and (<= ?
0 char
) (< char
(+ ?
0 (min 10 read-quoted-char-radix
))))
833 (setq code
(+ (* code read-quoted-char-radix
) (- char ?
0)))
834 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
835 ((and (<= ?a
(downcase char
))
836 (< (downcase char
) (+ ?a -
10 (min 26 read-quoted-char-radix
))))
837 (setq code
(+ (* code read-quoted-char-radix
)
838 (+ 10 (- (downcase char
) ?a
))))
839 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
840 ((and (not first
) (eq char ?\C-m
))
843 (setq unread-command-events
(list char
)
850 (defun read-passwd (prompt &optional confirm default
)
851 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
852 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
853 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
854 Optional DEFAULT is a default password to use instead of empty input."
858 (let ((first (read-passwd prompt nil default
))
859 (second (read-passwd "Confirm password: " nil default
)))
860 (if (equal first second
)
862 (message "Password not repeated accurately; please start over")
868 (cursor-in-echo-area t
))
869 (while (progn (message "%s%s"
871 (make-string (length pass
) ?.
))
872 (setq c
(read-char-exclusive nil t
))
873 (and (/= c ?
\r) (/= c ?
\n) (/= c ?\e
)))
876 (if (and (/= c ?
\b) (/= c ?
\177))
877 (setq pass
(concat pass
(char-to-string c
)))
878 (if (> (length pass
) 0)
879 (setq pass
(substring pass
0 -
1))))))
880 (clear-this-command-keys)
882 (or pass default
""))))
884 (defun force-mode-line-update (&optional all
)
885 "Force the mode-line of the current buffer to be redisplayed.
886 With optional non-nil ALL, force redisplay of all mode-lines."
887 (if all
(save-excursion (set-buffer (other-buffer))))
888 (set-buffer-modified-p (buffer-modified-p)))
890 (defun momentary-string-display (string pos
&optional exit-char message
)
891 "Momentarily display STRING in the buffer at POS.
892 Display remains until next character is typed.
893 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
894 otherwise it is then available as input (as a command if nothing else).
895 Display MESSAGE (optional fourth arg) in the echo area.
896 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
897 (or exit-char
(setq exit-char ?\
))
898 (let ((inhibit-read-only t
)
899 ;; Don't modify the undo list at all.
901 (modified (buffer-modified-p))
902 (name buffer-file-name
)
908 ;; defeat file locking... don't try this at home, kids!
909 (setq buffer-file-name nil
)
910 (insert-before-markers string
)
911 (setq insert-end
(point))
912 ;; If the message end is off screen, recenter now.
913 (if (< (window-end nil t
) insert-end
)
914 (recenter (/ (window-height) 2)))
915 ;; If that pushed message start off the screen,
916 ;; scroll to start it at the top of the screen.
917 (move-to-window-line 0)
922 (message (or message
"Type %s to continue editing.")
923 (single-key-description exit-char
))
924 (let ((char (read-event)))
925 (or (eq char exit-char
)
926 (setq unread-command-events
(list char
)))))
929 (delete-region pos insert-end
)))
930 (setq buffer-file-name name
)
931 (set-buffer-modified-p modified
))))
936 ;; A number of major modes set this locally.
937 ;; Give it a global value to avoid compiler warnings.
938 (defvar font-lock-defaults nil
)
940 (defvar suspend-hook nil
941 "Normal hook run by `suspend-emacs', before suspending.")
943 (defvar suspend-resume-hook nil
944 "Normal hook run by `suspend-emacs', after Emacs is continued.")
946 ;; Avoid compiler warnings about this variable,
947 ;; which has a special meaning on certain system types.
948 (defvar buffer-file-type nil
949 "Non-nil if the visited file is a binary file.
950 This variable is meaningful on MS-DOG and Windows NT.
951 On those systems, it is automatically local in every buffer.
952 On other systems, this variable is normally always nil.")
954 ;; This should probably be written in C (i.e., without using `walk-windows').
955 (defun get-buffer-window-list (buffer &optional minibuf frame
)
956 "Return windows currently displaying BUFFER, or nil if none.
957 See `walk-windows' for the meaning of MINIBUF and FRAME."
958 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
959 (walk-windows (function (lambda (window)
960 (if (eq (window-buffer window
) buffer
)
961 (setq windows
(cons window windows
)))))
965 (defun ignore (&rest ignore
)
966 "Do nothing and return nil.
967 This function accepts any number of arguments, but ignores them."
971 (defun error (&rest args
)
972 "Signal an error, making error message by passing all args to `format'.
973 In Emacs, the convention is that error messages start with a capital
974 letter but *do not* end with a period. Please follow this convention
975 for the sake of consistency."
977 (signal 'error
(list (apply 'format args
)))))
979 (defalias 'user-original-login-name
'user-login-name
)
981 (defun start-process-shell-command (name buffer
&rest args
)
982 "Start a program in a subprocess. Return the process object for it.
983 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
984 NAME is name for process. It is modified if necessary to make it unique.
985 BUFFER is the buffer or (buffer-name) to associate with the process.
986 Process output goes at end of that buffer, unless you specify
987 an output stream or filter function to handle the output.
988 BUFFER may be also nil, meaning that this process is not associated
990 Third arg is command name, the name of a shell command.
991 Remaining arguments are the arguments for the command.
992 Wildcards and redirection are handled as usual in the shell."
994 ((eq system-type
'vax-vms
)
995 (apply 'start-process name buffer args
))
996 ;; We used to use `exec' to replace the shell with the command,
997 ;; but that failed to handle (...) and semicolon, etc.
999 (start-process name buffer shell-file-name shell-command-switch
1000 (mapconcat 'identity args
" ")))))
1002 (defmacro with-current-buffer
(buffer &rest body
)
1003 "Execute the forms in BODY with BUFFER as the current buffer.
1004 The value returned is the value of the last form in BODY.
1005 See also `with-temp-buffer'."
1006 (cons 'save-current-buffer
1007 (cons (list 'set-buffer buffer
)
1010 (defmacro with-temp-file
(file &rest body
)
1011 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1012 The value returned is the value of the last form in BODY.
1013 See also `with-temp-buffer'."
1014 (let ((temp-file (make-symbol "temp-file"))
1015 (temp-buffer (make-symbol "temp-buffer")))
1016 `(let ((,temp-file
,file
)
1018 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1021 (with-current-buffer ,temp-buffer
1023 (with-current-buffer ,temp-buffer
1025 (write-region (point-min) (point-max) ,temp-file nil
0)))
1026 (and (buffer-name ,temp-buffer
)
1027 (kill-buffer ,temp-buffer
))))))
1029 (defmacro with-temp-message
(message &rest body
)
1030 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1031 The original message is restored to the echo area after BODY has finished.
1032 The value returned is the value of the last form in BODY.
1033 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1034 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1035 Use a MESSAGE of \"\" to temporarily clear the echo area."
1036 (let ((current-message (make-symbol "current-message"))
1037 (temp-message (make-symbol "with-temp-message")))
1038 `(let ((,temp-message
,message
)
1043 (setq ,current-message
(current-message))
1044 (message "%s" ,temp-message
))
1046 (and ,temp-message
,current-message
1047 (message "%s" ,current-message
))))))
1049 (defmacro with-temp-buffer
(&rest body
)
1050 "Create a temporary buffer, and evaluate BODY there like `progn'.
1051 See also `with-temp-file' and `with-output-to-string'."
1052 (let ((temp-buffer (make-symbol "temp-buffer")))
1053 `(let ((,temp-buffer
1054 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1056 (with-current-buffer ,temp-buffer
1058 (and (buffer-name ,temp-buffer
)
1059 (kill-buffer ,temp-buffer
))))))
1061 (defmacro with-output-to-string
(&rest body
)
1062 "Execute BODY, return the text it sent to `standard-output', as a string."
1063 `(let ((standard-output
1064 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1065 (let ((standard-output standard-output
))
1067 (with-current-buffer standard-output
1070 (kill-buffer nil
)))))
1072 (defmacro combine-after-change-calls
(&rest body
)
1073 "Execute BODY, but don't call the after-change functions till the end.
1074 If BODY makes changes in the buffer, they are recorded
1075 and the functions on `after-change-functions' are called several times
1076 when BODY is finished.
1077 The return value is the value of the last form in BODY.
1079 If `before-change-functions' is non-nil, then calls to the after-change
1080 functions can't be deferred, so in that case this macro has no effect.
1082 Do not alter `after-change-functions' or `before-change-functions'
1085 (let ((combine-after-change-calls t
))
1087 (combine-after-change-execute)))
1090 (defvar combine-run-hooks t
1091 "List of hooks delayed. Or t if we're not delaying hooks.")
1093 (defmacro combine-run-hooks
(&rest body
)
1094 "Execute BODY, but delay any `run-hooks' until the end."
1095 (let ((saved-combine-run-hooks (make-symbol "saved-combine-run-hooks"))
1096 (saved-run-hooks (make-symbol "saved-run-hooks")))
1097 `(let ((,saved-combine-run-hooks combine-run-hooks
)
1098 (,saved-run-hooks
(symbol-function 'run-hooks
)))
1101 ;; If we're not delaying hooks yet, setup the delaying mode
1102 (unless (listp combine-run-hooks
)
1103 (setq combine-run-hooks nil
)
1105 ,(lambda (&rest hooks
)
1106 (setq combine-run-hooks
1107 (append combine-run-hooks hooks
)))))
1109 ;; If we were not already delaying, then it's now time to set things
1110 ;; back to normal and to execute the delayed hooks.
1111 (unless (listp ,saved-combine-run-hooks
)
1112 (setq ,saved-combine-run-hooks combine-run-hooks
)
1113 (fset 'run-hooks
,saved-run-hooks
)
1114 (setq combine-run-hooks t
)
1115 (apply 'run-hooks
,saved-combine-run-hooks
))))))
1118 (defmacro with-syntax-table
(table &rest body
)
1119 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1120 The syntax table of the current buffer is saved, BODY is evaluated, and the
1121 saved table is restored, even in case of an abnormal exit.
1122 Value is what BODY returns."
1123 (let ((old-table (make-symbol "table"))
1124 (old-buffer (make-symbol "buffer")))
1125 `(let ((,old-table
(syntax-table))
1126 (,old-buffer
(current-buffer)))
1129 (set-syntax-table (copy-syntax-table ,table
))
1131 (save-current-buffer
1132 (set-buffer ,old-buffer
)
1133 (set-syntax-table ,old-table
))))))
1135 (defvar save-match-data-internal
)
1137 ;; We use save-match-data-internal as the local variable because
1138 ;; that works ok in practice (people should not use that variable elsewhere).
1139 ;; We used to use an uninterned symbol; the compiler handles that properly
1140 ;; now, but it generates slower code.
1141 (defmacro save-match-data
(&rest body
)
1142 "Execute the BODY forms, restoring the global value of the match data."
1143 ;; It is better not to use backquote here,
1144 ;; because that makes a bootstrapping problem
1145 ;; if you need to recompile all the Lisp files using interpreted code.
1147 '((save-match-data-internal (match-data)))
1148 (list 'unwind-protect
1150 '(set-match-data save-match-data-internal
))))
1152 (defun match-string (num &optional string
)
1153 "Return string of text matched by last search.
1154 NUM specifies which parenthesized expression in the last regexp.
1155 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1156 Zero means the entire text matched by the whole regexp or whole string.
1157 STRING should be given if the last search was by `string-match' on STRING."
1158 (if (match-beginning num
)
1160 (substring string
(match-beginning num
) (match-end num
))
1161 (buffer-substring (match-beginning num
) (match-end num
)))))
1163 (defun match-string-no-properties (num &optional string
)
1164 "Return string of text matched by last search, without text properties.
1165 NUM specifies which parenthesized expression in the last regexp.
1166 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1167 Zero means the entire text matched by the whole regexp or whole string.
1168 STRING should be given if the last search was by `string-match' on STRING."
1169 (if (match-beginning num
)
1172 (substring string
(match-beginning num
) (match-end num
))))
1173 (set-text-properties 0 (length result
) nil result
)
1175 (buffer-substring-no-properties (match-beginning num
)
1178 (defun split-string (string &optional separators
)
1179 "Splits STRING into substrings where there are matches for SEPARATORS.
1180 Each match for SEPARATORS is a splitting point.
1181 The substrings between the splitting points are made into a list
1183 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1185 If there is match for SEPARATORS at the beginning of STRING, we do not
1186 include a null substring for that. Likewise, if there is a match
1187 at the end of STRING, we don't include a null substring for that.
1189 Modifies the match data; use `save-match-data' if necessary."
1190 (let ((rexp (or separators
"[ \f\t\n\r\v]+"))
1194 (while (and (string-match rexp string
1196 (= start
(match-beginning 0))
1197 (< start
(length string
)))
1199 (< (match-beginning 0) (length string
)))
1201 (or (eq (match-beginning 0) 0)
1202 (and (eq (match-beginning 0) (match-end 0))
1203 (eq (match-beginning 0) start
))
1205 (cons (substring string start
(match-beginning 0))
1207 (setq start
(match-end 0)))
1208 (or (eq start
(length string
))
1210 (cons (substring string start
)
1214 (defun subst-char-in-string (fromchar tochar string
&optional inplace
)
1215 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1216 Unless optional argument INPLACE is non-nil, return a new string."
1217 (let ((i (length string
))
1218 (newstr (if inplace string
(copy-sequence string
))))
1221 (if (eq (aref newstr i
) fromchar
)
1222 (aset newstr i tochar
)))
1225 (defun replace-regexp-in-string (regexp rep string
&optional
1226 fixedcase literal subexp start
)
1227 "Replace all matches for REGEXP with REP in STRING.
1229 Return a new string containing the replacements.
1231 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1232 arguments with the same names of function `replace-match'. If START
1233 is non-nil, start replacements at that index in STRING.
1235 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1236 function. If it is a function it is applied to each match to generate
1237 the replacement passed to `replace-match'; the match-data at this
1238 point are such that match 0 is the function's argument.
1240 To replace only the first match (if any), make REGEXP match up to \\'
1241 and replace a sub-expression, e.g.
1242 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
1246 ;; To avoid excessive consing from multiple matches in long strings,
1247 ;; don't just call `replace-match' continually. Walk down the
1248 ;; string looking for matches of REGEXP and building up a (reversed)
1249 ;; list MATCHES. This comprises segments of STRING which weren't
1250 ;; matched interspersed with replacements for segments that were.
1251 ;; [For a `large' number of replacments it's more efficient to
1252 ;; operate in a temporary buffer; we can't tell from the function's
1253 ;; args whether to choose the buffer-based implementation, though it
1254 ;; might be reasonable to do so for long enough STRING.]
1255 (let ((l (length string
))
1256 (start (or start
0))
1259 (while (and (< start l
) (string-match regexp string start
))
1260 (setq mb
(match-beginning 0)
1262 ;; If we matched the empty string, make sure we advance by one char
1263 (when (= me mb
) (setq me
(min l
(1+ mb
))))
1264 ;; Generate a replacement for the matched substring.
1265 ;; Operate only on the substring to minimize string consing.
1266 ;; Set up match data for the substring for replacement;
1267 ;; presumably this is likely to be faster than munging the
1268 ;; match data directly in Lisp.
1269 (string-match regexp
(setq str
(substring string mb me
)))
1271 (cons (replace-match (if (stringp rep
)
1273 (funcall rep
(match-string 0 str
)))
1274 fixedcase literal str subexp
)
1275 (cons (substring string start mb
) ; unmatched prefix
1278 ;; Reconstruct a string from the pieces.
1279 (setq matches
(cons (substring string start l
) matches
)) ; leftover
1280 (apply #'concat
(nreverse matches
)))))
1282 (defun shell-quote-argument (argument)
1283 "Quote an argument for passing as argument to an inferior shell."
1284 (if (eq system-type
'ms-dos
)
1285 ;; Quote using double quotes, but escape any existing quotes in
1286 ;; the argument with backslashes.
1290 (if (or (null (string-match "[^\"]" argument
))
1291 (< (match-end 0) (length argument
)))
1292 (while (string-match "[\"]" argument start
)
1293 (setq end
(match-beginning 0)
1294 result
(concat result
(substring argument start end
)
1295 "\\" (substring argument end
(1+ end
)))
1297 (concat "\"" result
(substring argument start
) "\""))
1298 (if (eq system-type
'windows-nt
)
1299 (concat "\"" argument
"\"")
1300 (if (equal argument
"")
1302 ;; Quote everything except POSIX filename characters.
1303 ;; This should be safe enough even for really weird shells.
1304 (let ((result "") (start 0) end
)
1305 (while (string-match "[^-0-9a-zA-Z_./]" argument start
)
1306 (setq end
(match-beginning 0)
1307 result
(concat result
(substring argument start end
)
1308 "\\" (substring argument end
(1+ end
)))
1310 (concat result
(substring argument start
)))))))
1312 (defun make-syntax-table (&optional oldtable
)
1313 "Return a new syntax table.
1314 If OLDTABLE is non-nil, copy OLDTABLE.
1315 Otherwise, create a syntax table which inherits
1316 all letters and control characters from the standard syntax table;
1317 other characters are copied from the standard syntax table."
1319 (copy-syntax-table oldtable
)
1320 (let ((table (copy-syntax-table))
1340 (defun add-to-invisibility-spec (arg)
1341 "Add elements to `buffer-invisibility-spec'.
1342 See documentation for `buffer-invisibility-spec' for the kind of elements
1345 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
1346 (setq buffer-invisibility-spec
(list arg
)))
1348 (setq buffer-invisibility-spec
1349 (cons arg buffer-invisibility-spec
)))))
1351 (defun remove-from-invisibility-spec (arg)
1352 "Remove elements from `buffer-invisibility-spec'."
1353 (if (consp buffer-invisibility-spec
)
1354 (setq buffer-invisibility-spec
(delete arg buffer-invisibility-spec
))))
1356 (defun global-set-key (key command
)
1357 "Give KEY a global binding as COMMAND.
1358 COMMAND is the command definition to use; usually it is
1359 a symbol naming an interactively-callable function.
1360 KEY is a key sequence; noninteractively, it is a string or vector
1361 of characters or event types, and non-ASCII characters with codes
1362 above 127 (such as ISO Latin-1) can be included if you use a vector.
1364 Note that if KEY has a local binding in the current buffer,
1365 that local binding will continue to shadow any global binding
1366 that you make with this function."
1367 (interactive "KSet key globally: \nCSet key %s to command: ")
1368 (or (vectorp key
) (stringp key
)
1369 (signal 'wrong-type-argument
(list 'arrayp key
)))
1370 (define-key (current-global-map) key command
))
1372 (defun local-set-key (key command
)
1373 "Give KEY a local binding as COMMAND.
1374 COMMAND is the command definition to use; usually it is
1375 a symbol naming an interactively-callable function.
1376 KEY is a key sequence; noninteractively, it is a string or vector
1377 of characters or event types, and non-ASCII characters with codes
1378 above 127 (such as ISO Latin-1) can be included if you use a vector.
1380 The binding goes in the current buffer's local map,
1381 which in most cases is shared with all other buffers in the same major mode."
1382 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1383 (let ((map (current-local-map)))
1385 (use-local-map (setq map
(make-sparse-keymap))))
1386 (or (vectorp key
) (stringp key
)
1387 (signal 'wrong-type-argument
(list 'arrayp key
)))
1388 (define-key map key command
)))
1390 (defun global-unset-key (key)
1391 "Remove global binding of KEY.
1392 KEY is a string representing a sequence of keystrokes."
1393 (interactive "kUnset key globally: ")
1394 (global-set-key key nil
))
1396 (defun local-unset-key (key)
1397 "Remove local binding of KEY.
1398 KEY is a string representing a sequence of keystrokes."
1399 (interactive "kUnset key locally: ")
1400 (if (current-local-map)
1401 (local-set-key key nil
))
1404 ;; We put this here instead of in frame.el so that it's defined even on
1405 ;; systems where frame.el isn't loaded.
1406 (defun frame-configuration-p (object)
1407 "Return non-nil if OBJECT seems to be a frame configuration.
1408 Any list whose car is `frame-configuration' is assumed to be a frame
1411 (eq (car object
) 'frame-configuration
)))
1413 (defun functionp (object)
1414 "Non-nil if OBJECT is a type of object that can be called as a function."
1415 (or (subrp object
) (byte-code-function-p object
)
1416 (eq (car-safe object
) 'lambda
)
1417 (and (symbolp object
) (fboundp object
))))
1420 ;(defun nth (n list)
1421 ; "Returns the Nth element of LIST.
1422 ;N counts from zero. If LIST is not that long, nil is returned."
1423 ; (car (nthcdr n list)))
1425 ;(defun copy-alist (alist)
1426 ; "Return a copy of ALIST.
1427 ;This is a new alist which represents the same mapping
1428 ;from objects to objects, but does not share the alist structure with ALIST.
1429 ;The objects mapped (cars and cdrs of elements of the alist)
1430 ;are shared, however."
1431 ; (setq alist (copy-sequence alist))
1432 ; (let ((tail alist))
1434 ; (if (consp (car tail))
1435 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1436 ; (setq tail (cdr tail))))
1439 (defun assq-delete-all (key alist
)
1440 "Delete from ALIST all elements whose car is KEY.
1441 Return the modified alist."
1444 (if (eq (car (car tail
)) key
)
1445 (setq alist
(delq (car tail
) alist
)))
1446 (setq tail
(cdr tail
)))
1449 (defun make-temp-file (prefix &optional dir-flag
)
1450 "Create a temporary file.
1451 The returned file name (created by appending some random characters at the end
1452 of PREFIX, and expanding against `temporary-file-directory' if necessary,
1453 is guaranteed to point to a newly created empty file.
1454 You can then use `write-region' to write new data into the file.
1456 If DIR-FLAG is non-nil, create a new empty directory instead of a file."
1458 (while (condition-case ()
1462 (expand-file-name prefix temporary-file-directory
)))
1464 (make-directory file
)
1465 (write-region "" nil file nil
'silent nil
'excl
))
1467 (file-already-exists t
))
1468 ;; the file was somehow created by someone else between
1469 ;; `make-temp-name' and `write-region', let's try again.
1474 (defun add-minor-mode (toggle name
&optional keymap after toggle-fun
)
1475 "Register a new minor mode.
1477 TOGGLE is a symbol which is the name of a buffer-local variable that
1478 is toggled on or off to say whether the minor mode is active or not.
1480 NAME specifies what will appear in the mode line when the minor mode
1481 is active. NAME should be either a string starting with a space, or a
1482 symbol whose value is such a string.
1484 Optional KEYMAP is the keymap for the minor mode that will be added
1485 to `minor-mode-map-alist'.
1487 Optional AFTER specifies that TOGGLE should be added after AFTER
1488 in `minor-mode-alist'.
1490 Optional TOGGLE-FUN is there for compatiblity with other Emacsen.
1491 It is currently not used.
1493 In most cases, `define-minor-mode' should be used instead."
1495 (let ((existing (assq toggle minor-mode-alist
))
1496 (name (if (symbolp name
) (symbol-value name
) name
)))
1497 (cond ((null existing
)
1498 (let ((tail minor-mode-alist
) found
)
1499 (while (and tail
(not found
))
1500 (if (eq after
(caar tail
))
1502 (setq tail
(cdr tail
))))
1504 (let ((rest (cdr found
)))
1506 (nconc found
(list (list toggle name
)) rest
))
1507 (setq minor-mode-alist
(cons (list toggle name
)
1508 minor-mode-alist
)))))
1510 (setcdr existing
(list name
))))))
1513 (let ((existing (assq toggle minor-mode-map-alist
)))
1514 (cond ((null existing
)
1515 (let ((tail minor-mode-map-alist
) found
)
1516 (while (and tail
(not found
))
1517 (if (eq after
(caar tail
))
1519 (setq tail
(cdr tail
))))
1521 (let ((rest (cdr found
)))
1523 (nconc found
(list (cons toggle keymap
)) rest
))
1524 (setq minor-mode-map-alist
(cons (cons toggle keymap
)
1525 minor-mode-map-alist
)))))
1527 (setcdr existing keymap
))))))
1530 ;;; subr.el ends here