1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995 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.
25 ;;;; Lisp language features.
27 (defmacro lambda
(&rest cdr
)
28 "Return a lambda expression.
29 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
30 self-quoting; the result of evaluating the lambda expression is the
31 expression itself. The lambda expression may then be treated as a
32 function, i.e., stored as the function value of a symbol, passed to
33 funcall or mapcar, etc.
35 ARGS should take the same form as an argument list for a `defun'.
36 DOCSTRING is an optional documentation string.
37 If present, it should describe how to call the function.
38 But documentation strings are usually not useful in nameless functions.
39 INTERACTIVE should be a call to the function `interactive', which see.
40 It may also be omitted.
41 BODY should be a list of lisp expressions."
42 ;; Note that this definition should not use backquotes; subr.el should not
43 ;; depend on backquote.el.
44 (list 'function
(cons 'lambda cdr
)))
46 ;;(defmacro defun-inline (name args &rest body)
47 ;; "Create an \"inline defun\" (actually a macro).
48 ;;Use just like `defun'."
49 ;; (nconc (list 'defmacro name '(&rest args))
50 ;; (if (stringp (car body))
51 ;; (prog1 (list (car body))
52 ;; (setq body (or (cdr body) body))))
53 ;; (list (list 'cons (list 'quote
54 ;; (cons 'lambda (cons args body)))
64 ;Prevent the \{...} documentation construct
65 ;from mentioning keys that run this command.
66 (put 'undefined
'suppress-keymap t
)
68 (defun suppress-keymap (map &optional nodigits
)
69 "Make MAP override all normally self-inserting keys to be undefined.
70 Normally, as an exception, digits and minus-sign are set to make prefix args,
71 but optional second arg NODIGITS non-nil treats them like other chars."
72 (substitute-key-definition 'self-insert-command
'undefined map global-map
)
75 (define-key map
"-" 'negative-argument
)
76 ;; Make plain numbers do numeric args.
79 (define-key map
(char-to-string loop
) 'digit-argument
)
80 (setq loop
(1+ loop
))))))
83 ;(defun copy-keymap (keymap)
84 ; "Return a copy of KEYMAP"
85 ; (while (not (keymapp keymap))
86 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
87 ; (if (vectorp keymap)
88 ; (copy-sequence keymap)
89 ; (copy-alist keymap)))
91 (defvar key-substitution-in-progress nil
92 "Used internally by substitute-key-definition.")
94 (defun substitute-key-definition (olddef newdef keymap
&optional oldmap prefix
)
95 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
96 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
97 If optional fourth argument OLDMAP is specified, we redefine
98 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
99 (or prefix
(setq prefix
""))
100 (let* ((scan (or oldmap keymap
))
102 (prefix1 (vconcat prefix vec1
))
103 (key-substitution-in-progress
104 (cons scan key-substitution-in-progress
)))
105 ;; Scan OLDMAP, finding each char or event-symbol that
106 ;; has any definition, and act on it with hack-key.
108 (if (consp (car scan
))
109 (let ((char (car (car scan
)))
110 (defn (cdr (car scan
))))
111 ;; The inside of this let duplicates exactly
112 ;; the inside of the following let that handles array elements.
114 (aset prefix1
(length prefix
) char
)
115 (let (inner-def skipped
)
116 ;; Skip past menu-prompt.
117 (while (stringp (car-safe defn
))
118 (setq skipped
(cons (car defn
) skipped
))
119 (setq defn
(cdr defn
)))
120 ;; Skip past cached key-equivalence data for menu items.
121 (and (consp defn
) (consp (car defn
))
122 (setq defn
(cdr defn
)))
123 (setq inner-def defn
)
124 ;; Look past a symbol that names a keymap.
125 (while (and (symbolp inner-def
)
127 (setq inner-def
(symbol-function inner-def
)))
129 (define-key keymap prefix1
(nconc (nreverse skipped
) newdef
))
130 (if (and (keymapp defn
)
131 ;; Avoid recursively scanning
132 ;; where KEYMAP does not have a submap.
133 (let ((elt (lookup-key keymap prefix1
)))
136 ;; Avoid recursively rescanning keymap being scanned.
138 key-substitution-in-progress
)))
139 ;; If this one isn't being scanned already,
141 (substitute-key-definition olddef newdef keymap
144 (if (arrayp (car scan
))
145 (let* ((array (car scan
))
149 (let ((char i
) (defn (aref array i
)))
150 ;; The inside of this let duplicates exactly
151 ;; the inside of the previous let.
153 (aset prefix1
(length prefix
) char
)
154 (let (inner-def skipped
)
155 ;; Skip past menu-prompt.
156 (while (stringp (car-safe defn
))
157 (setq skipped
(cons (car defn
) skipped
))
158 (setq defn
(cdr defn
)))
159 (and (consp defn
) (consp (car defn
))
160 (setq defn
(cdr defn
)))
161 (setq inner-def defn
)
162 (while (and (symbolp inner-def
)
164 (setq inner-def
(symbol-function inner-def
)))
166 (define-key keymap prefix1
167 (nconc (nreverse skipped
) newdef
))
168 (if (and (keymapp defn
)
169 (let ((elt (lookup-key keymap prefix1
)))
173 key-substitution-in-progress
)))
174 (substitute-key-definition olddef newdef keymap
178 (setq scan
(cdr scan
)))))
180 (defun define-key-after (keymap key definition after
)
181 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
182 This is like `define-key' except that the binding for KEY is placed
183 just after the binding for the event AFTER, instead of at the beginning
185 The order matters when the keymap is used as a menu.
186 KEY must contain just one event type--that is to say, it must be
187 a string or vector of length 1."
189 (signal 'wrong-type-argument
(list 'keymapp keymap
)))
190 (if (> (length key
) 1)
191 (error "multi-event key specified in `define-key-after'"))
192 (let ((tail keymap
) done inserted
193 (first (aref key
0)))
194 (while (and (not done
) tail
)
195 ;; Delete any earlier bindings for the same key.
196 (if (eq (car-safe (car (cdr tail
))) first
)
197 (setcdr tail
(cdr (cdr tail
))))
198 ;; When we reach AFTER's binding, insert the new binding after.
199 ;; If we reach an inherited keymap, insert just before that.
200 ;; If we reach the end of this keymap, insert at the end.
201 (if (or (eq (car-safe (car tail
)) after
)
202 (eq (car (cdr tail
)) 'keymap
)
205 ;; Stop the scan only if we find a parent keymap.
206 ;; Keep going past the inserted element
207 ;; so we can delete any duplications that come later.
208 (if (eq (car (cdr tail
)) 'keymap
)
210 ;; Don't insert more than once.
212 (setcdr tail
(cons (cons (aref key
0) definition
) (cdr tail
))))
214 (setq tail
(cdr tail
)))))
216 (defun keyboard-translate (from to
)
217 "Translate character FROM to TO at a low level.
218 This function creates a `keyboard-translate-table' if necessary
219 and then modifies one entry in it."
220 (or (arrayp keyboard-translate-table
)
221 (setq keyboard-translate-table
""))
222 (if (or (> from
(length keyboard-translate-table
))
223 (> to
(length keyboard-translate-table
)))
225 (let* ((i (length keyboard-translate-table
))
226 (table (concat keyboard-translate-table
227 (make-string (- 256 i
) 0))))
231 (setq keyboard-translate-table table
))))
232 (aset keyboard-translate-table from to
))
235 ;;;; The global keymap tree.
237 ;;; global-map, esc-map, and ctl-x-map have their values set up in
238 ;;; keymap.c; we just give them docstrings here.
240 (defvar global-map nil
241 "Default global keymap mapping Emacs keyboard input into commands.
242 The value is a keymap which is usually (but not necessarily) Emacs's
246 "Default keymap for ESC (meta) commands.
247 The normal global definition of the character ESC indirects to this keymap.")
249 (defvar ctl-x-map nil
250 "Default keymap for C-x commands.
251 The normal global definition of the character C-x indirects to this keymap.")
253 (defvar ctl-x-4-map
(make-sparse-keymap)
254 "Keymap for subcommands of C-x 4")
255 (defalias 'ctl-x-4-prefix ctl-x-4-map
)
256 (define-key ctl-x-map
"4" 'ctl-x-4-prefix
)
258 (defvar ctl-x-5-map
(make-sparse-keymap)
259 "Keymap for frame commands.")
260 (defalias 'ctl-x-5-prefix ctl-x-5-map
)
261 (define-key ctl-x-map
"5" 'ctl-x-5-prefix
)
264 ;;;; Event manipulation functions.
266 ;; The call to `read' is to ensure that the value is computed at load time
267 ;; and not compiled into the .elc file. The value is negative on most
268 ;; machines, but not on all!
269 (defconst listify-key-sequence-1
(logior 128 (read "?\\M-\\^@")))
271 (defun listify-key-sequence (key)
272 "Convert a key sequence to a list of events."
275 (mapcar (function (lambda (c)
277 (logxor c listify-key-sequence-1
)
281 (defsubst eventp
(obj)
282 "True if the argument is an event object."
285 (get obj
'event-symbol-elements
))
288 (get (car obj
) 'event-symbol-elements
))))
290 (defun event-modifiers (event)
291 "Returns a list of symbols representing the modifier keys in event EVENT.
292 The elements of the list may include `meta', `control',
293 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
297 (setq type
(car type
)))
299 (cdr (get type
'event-symbol-elements
))
301 (or (zerop (logand type ?\M-\^
@))
302 (setq list
(cons 'meta list
)))
303 (or (and (zerop (logand type ?\C-\^
@))
304 (>= (logand type
127) 32))
305 (setq list
(cons 'control list
)))
306 (or (and (zerop (logand type ?\S-\^
@))
307 (= (logand type
255) (downcase (logand type
255))))
308 (setq list
(cons 'shift list
)))
309 (or (zerop (logand type ?\H-\^
@))
310 (setq list
(cons 'hyper list
)))
311 (or (zerop (logand type ?\s-\^
@))
312 (setq list
(cons 'super list
)))
313 (or (zerop (logand type ?\A-\^
@))
314 (setq list
(cons 'alt list
)))
317 (defun event-basic-type (event)
318 "Returns the basic type of the given event (all modifiers removed).
319 The value is an ASCII printing character (not upper case) or a symbol."
321 (setq event
(car event
)))
323 (car (get event
'event-symbol-elements
))
324 (let ((base (logand event
(1- (lsh 1 18)))))
325 (downcase (if (< base
32) (logior base
64) base
)))))
327 (defsubst mouse-movement-p
(object)
328 "Return non-nil if OBJECT is a mouse movement event."
330 (eq (car object
) 'mouse-movement
)))
332 (defsubst event-start
(event)
333 "Return the starting position of EVENT.
334 If EVENT is a mouse press or a mouse click, this returns the location
336 If EVENT is a drag, this returns the drag's starting position.
337 The return value is of the form
338 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
339 The `posn-' functions access elements of such lists."
342 (defsubst event-end
(event)
343 "Return the ending location of EVENT. EVENT should be a click or drag event.
344 If EVENT is a click event, this function is the same as `event-start'.
345 The return value is of the form
346 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
347 The `posn-' functions access elements of such lists."
348 (nth (if (consp (nth 2 event
)) 2 1) event
))
350 (defsubst event-click-count
(event)
351 "Return the multi-click count of EVENT, a click or drag event.
352 The return value is a positive integer."
353 (if (integerp (nth 2 event
)) (nth 2 event
) 1))
355 (defsubst posn-window
(position)
356 "Return the window in POSITION.
357 POSITION should be a list of the form
358 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
359 as returned by the `event-start' and `event-end' functions."
362 (defsubst posn-point
(position)
363 "Return the buffer location in POSITION.
364 POSITION should be a list of the form
365 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
366 as returned by the `event-start' and `event-end' functions."
367 (if (consp (nth 1 position
))
368 (car (nth 1 position
))
371 (defsubst posn-x-y
(position)
372 "Return the x and y coordinates in POSITION.
373 POSITION should be a list of the form
374 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
375 as returned by the `event-start' and `event-end' functions."
378 (defun posn-col-row (position)
379 "Return the column and row in POSITION, measured in characters.
380 POSITION should be a list of the form
381 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
382 as returned by the `event-start' and `event-end' functions.
383 For a scroll-bar event, the result column is 0, and the row
384 corresponds to the vertical position of the click in the scroll bar."
385 (let ((pair (nth 2 position
))
386 (window (posn-window position
)))
387 (if (eq (if (consp (nth 1 position
))
388 (car (nth 1 position
))
390 'vertical-scroll-bar
)
391 (cons 0 (scroll-bar-scale pair
(1- (window-height window
))))
392 (if (eq (if (consp (nth 1 position
))
393 (car (nth 1 position
))
395 'horizontal-scroll-bar
)
396 (cons (scroll-bar-scale pair
(window-width window
)) 0)
397 (let* ((frame (if (framep window
) window
(window-frame window
)))
398 (x (/ (car pair
) (frame-char-width frame
)))
399 (y (/ (cdr pair
) (frame-char-height frame
))))
402 (defsubst posn-timestamp
(position)
403 "Return the timestamp of POSITION.
404 POSITION should be a list of the form
405 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
406 as returned by the `event-start' and `event-end' functions."
410 ;;;; Obsolescent names for functions.
412 (defalias 'dot
'point
)
413 (defalias 'dot-marker
'point-marker
)
414 (defalias 'dot-min
'point-min
)
415 (defalias 'dot-max
'point-max
)
416 (defalias 'window-dot
'window-point
)
417 (defalias 'set-window-dot
'set-window-point
)
418 (defalias 'read-input
'read-string
)
419 (defalias 'send-string
'process-send-string
)
420 (defalias 'send-region
'process-send-region
)
421 (defalias 'show-buffer
'set-window-buffer
)
422 (defalias 'buffer-flush-undo
'buffer-disable-undo
)
423 (defalias 'eval-current-buffer
'eval-buffer
)
424 (defalias 'compiled-function-p
'byte-code-function-p
)
426 ;; Some programs still use this as a function.
428 "Obsolete function returning the value of the `baud-rate' variable.
429 Please convert your programs to use the variable `baud-rate' directly."
433 ;;;; Alternate names for functions - these are not being phased out.
435 (defalias 'string
= 'string-equal
)
436 (defalias 'string
< 'string-lessp
)
437 (defalias 'move-marker
'set-marker
)
439 (defalias 'not
'null
)
440 (defalias 'rplaca
'setcar
)
441 (defalias 'rplacd
'setcdr
)
442 (defalias 'beep
'ding
) ;preserve lingual purity
443 (defalias 'indent-to-column
'indent-to
)
444 (defalias 'backward-delete-char
'delete-backward-char
)
445 (defalias 'search-forward-regexp
(symbol-function 're-search-forward
))
446 (defalias 'search-backward-regexp
(symbol-function 're-search-backward
))
447 (defalias 'int-to-string
'number-to-string
)
448 (defalias 'set-match-data
'store-match-data
)
450 ;;; Should this be an obsolete name? If you decide it should, you get
451 ;;; to go through all the sources and change them.
452 (defalias 'string-to-int
'string-to-number
)
454 ;;;; Hook manipulation functions.
456 ;; We used to have this variable so that C code knew how to run hooks. That
457 ;; calling convention is made obsolete now the hook running functions are in C.
458 (defconst run-hooks
'run-hooks
459 "Variable by which C primitives find the function `run-hooks'.
460 Don't change it. Don't use it either; use the hook running C primitives.")
462 (defun make-local-hook (hook)
463 "Make the hook HOOK local to the current buffer.
464 When a hook is local, its local and global values
465 work in concert: running the hook actually runs all the hook
466 functions listed in *either* the local value *or* the global value
467 of the hook variable.
469 This function works by making `t' a member of the buffer-local value,
470 which acts as a flag to run the hook functions in the default value as
471 well. This works for all normal hooks, but does not work for most
472 non-normal hooks yet. We will be changing the callers of non-normal
473 hooks so that they can handle localness; this has to be done one by
476 This function does nothing if HOOK is already local in the current
479 Do not use `make-local-variable' to make a hook variable buffer-local."
480 (if (local-variable-p hook
)
482 (or (boundp hook
) (set hook nil
))
483 (make-local-variable hook
)
484 (set hook
(list t
))))
486 (defun add-hook (hook function
&optional append local
)
487 "Add to the value of HOOK the function FUNCTION.
488 FUNCTION is not added if already present.
489 FUNCTION is added (if necessary) at the beginning of the hook list
490 unless the optional argument APPEND is non-nil, in which case
491 FUNCTION is added at the end.
493 The optional fourth argument, LOCAL, if non-nil, says to modify
494 the hook's buffer-local value rather than its default value.
495 This makes no difference if the hook is not buffer-local.
496 To make a hook variable buffer-local, always use
497 `make-local-hook', not `make-local-variable'.
499 HOOK should be a symbol, and FUNCTION may be any valid function. If
500 HOOK is void, it is first set to nil. If HOOK's value is a single
501 function, it is changed to a list of functions."
502 (or (boundp hook
) (set hook nil
))
503 (or (default-boundp hook
) (set-default hook nil
))
504 ;; If the hook value is a single function, turn it into a list.
505 (let ((old (symbol-value hook
)))
506 (if (or (not (listp old
)) (eq (car old
) 'lambda
))
507 (set hook
(list old
))))
509 ;; Detect the case where make-local-variable was used on a hook
510 ;; and do what we used to do.
511 (and (local-variable-if-set-p hook
)
512 (not (memq t
(symbol-value hook
)))))
513 ;; Alter the local value only.
514 (or (if (consp function
)
515 (member function
(symbol-value hook
))
516 (memq function
(symbol-value hook
)))
519 (append (symbol-value hook
) (list function
))
520 (cons function
(symbol-value hook
)))))
521 ;; Alter the global value (which is also the only value,
522 ;; if the hook doesn't have a local value).
523 (or (if (consp function
)
524 (member function
(default-value hook
))
525 (memq function
(default-value hook
)))
528 (append (default-value hook
) (list function
))
529 (cons function
(default-value hook
)))))))
531 (defun remove-hook (hook function
&optional local
)
532 "Remove from the value of HOOK the function FUNCTION.
533 HOOK should be a symbol, and FUNCTION may be any valid function. If
534 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
535 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
537 The optional third argument, LOCAL, if non-nil, says to modify
538 the hook's buffer-local value rather than its default value.
539 This makes no difference if the hook is not buffer-local.
540 To make a hook variable buffer-local, always use
541 `make-local-hook', not `make-local-variable'."
542 (if (or (not (boundp hook
)) ;unbound symbol, or
543 (not (default-boundp 'hook
))
544 (null (symbol-value hook
)) ;value is nil, or
545 (null function
)) ;function is nil, then
548 ;; Detect the case where make-local-variable was used on a hook
549 ;; and do what we used to do.
550 (and (local-variable-p hook
)
551 (not (memq t
(symbol-value hook
)))))
552 (let ((hook-value (symbol-value hook
)))
553 (if (consp hook-value
)
554 (if (member function hook-value
)
555 (setq hook-value
(delete function
(copy-sequence hook-value
))))
556 (if (equal hook-value function
)
557 (setq hook-value nil
)))
558 (set hook hook-value
))
559 (let ((hook-value (default-value hook
)))
560 (if (consp hook-value
)
561 (if (member function hook-value
)
562 (setq hook-value
(delete function
(copy-sequence hook-value
))))
563 (if (equal hook-value function
)
564 (setq hook-value nil
)))
565 (set-default hook hook-value
)))))
567 (defun add-to-list (list-var element
)
568 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
569 The test for presence of ELEMENT is done with `equal'.
570 If you want to use `add-to-list' on a variable that is not defined
571 until a certain package is loaded, you should put the call to `add-to-list'
572 into a hook function that will be run only after loading the package.
573 `eval-after-load' provides one way to do this. In some cases
574 other hooks, such as major mode hooks, can do the job."
575 (or (member element
(symbol-value list-var
))
576 (set list-var
(cons element
(symbol-value list-var
)))))
578 ;;;; Specifying things to do after certain files are loaded.
580 (defun eval-after-load (file form
)
581 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
582 This makes or adds to an entry on `after-load-alist'.
583 If FILE is already loaded, evaluate FORM right now.
584 It does nothing if FORM is already on the list for FILE.
585 FILE should be the name of a library, with no directory name."
586 ;; Make sure there is an element for FILE.
587 (or (assoc file after-load-alist
)
588 (setq after-load-alist
(cons (list file
) after-load-alist
)))
589 ;; Add FORM to the element if it isn't there.
590 (let ((elt (assoc file after-load-alist
)))
591 (or (member form
(cdr elt
))
593 (nconc elt
(list form
))
594 ;; If the file has been loaded already, run FORM right away.
595 (and (assoc file load-history
)
599 (defun eval-next-after-load (file)
600 "Read the following input sexp, and run it whenever FILE is loaded.
601 This makes or adds to an entry on `after-load-alist'.
602 FILE should be the name of a library, with no directory name."
603 (eval-after-load file
(read)))
606 ;;;; Input and display facilities.
608 (defun read-quoted-char (&optional prompt
)
609 "Like `read-char', except that if the first character read is an octal
610 digit, we read up to two more octal digits and return the character
611 represented by the octal number consisting of those digits.
612 Optional argument PROMPT specifies a string to use to prompt the user."
613 (let ((message-log-max nil
) (count 0) (code 0) char
)
615 (let ((inhibit-quit (zerop count
))
616 ;; Don't let C-h get the help message--only help function keys.
619 "Type the special character you want to use,
620 or three octal digits representing its character code."))
621 (and prompt
(message "%s-" prompt
))
622 (setq char
(read-char))
623 (if inhibit-quit
(setq quit-flag nil
)))
625 ((and (<= ?
0 char
) (<= char ?
7))
626 (setq code
(+ (* code
8) (- char ?
0))
628 (and prompt
(setq prompt
(message "%s %c" prompt char
))))
630 (setq unread-command-events
(list char
) count
259))
631 (t (setq code char count
259))))
632 ;; Turn a meta-character into a character with the 0200 bit set.
633 (logior (if (/= (logand code ?\M-\^
@) 0) 128 0)
636 (defun force-mode-line-update (&optional all
)
637 "Force the mode-line of the current buffer to be redisplayed.
638 With optional non-nil ALL, force redisplay of all mode-lines."
639 (if all
(save-excursion (set-buffer (other-buffer))))
640 (set-buffer-modified-p (buffer-modified-p)))
642 (defun momentary-string-display (string pos
&optional exit-char message
)
643 "Momentarily display STRING in the buffer at POS.
644 Display remains until next character is typed.
645 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
646 otherwise it is then available as input (as a command if nothing else).
647 Display MESSAGE (optional fourth arg) in the echo area.
648 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
649 (or exit-char
(setq exit-char ?\
))
650 (let ((buffer-read-only nil
)
651 ;; Don't modify the undo list at all.
653 (modified (buffer-modified-p))
654 (name buffer-file-name
)
660 ;; defeat file locking... don't try this at home, kids!
661 (setq buffer-file-name nil
)
662 (insert-before-markers string
)
663 (setq insert-end
(point))
664 ;; If the message end is off screen, recenter now.
665 (if (> (window-end) insert-end
)
666 (recenter (/ (window-height) 2)))
667 ;; If that pushed message start off the screen,
668 ;; scroll to start it at the top of the screen.
669 (move-to-window-line 0)
674 (message (or message
"Type %s to continue editing.")
675 (single-key-description exit-char
))
676 (let ((char (read-event)))
677 (or (eq char exit-char
)
678 (setq unread-command-events
(list char
)))))
681 (delete-region pos insert-end
)))
682 (setq buffer-file-name name
)
683 (set-buffer-modified-p modified
))))
688 ;; A number of major modes set this locally.
689 ;; Give it a global value to avoid compiler warnings.
690 (defvar font-lock-defaults nil
)
692 ;; Avoid compiler warnings about this variable,
693 ;; which has a special meaning on certain system types.
694 (defvar buffer-file-type nil
695 "Non-nil if the visited file is a binary file.
696 This variable is meaningful on MS-DOG and Windows NT.
697 On those systems, it is automatically local in every buffer.
698 On other systems, this variable is normally always nil.")
700 ;; This should probably be written in C (i.e., without using `walk-windows').
701 (defun get-buffer-window-list (buffer &optional frame
)
702 "Return windows currently displaying BUFFER, or nil if none.
703 See `get-buffer-window' for the meaning of FRAME."
704 (let ((buffer (if (bufferp buffer
) buffer
(get-buffer buffer
))) windows
)
705 (walk-windows (function (lambda (window)
706 (if (eq (window-buffer window
) buffer
)
707 (setq windows
(cons window windows
)))))
711 (defun ignore (&rest ignore
)
712 "Do nothing and return nil.
713 This function accepts any number of arguments, but ignores them."
717 (defun error (&rest args
)
718 "Signal an error, making error message by passing all args to `format'.
719 In Emacs, the convention is that error messages start with a capital
720 letter but *do not* end with a period. Please follow this convention
721 for the sake of consistency."
723 (signal 'error
(list (apply 'format args
)))))
725 (defalias 'user-original-login-name
'user-login-name
)
727 (defun start-process-shell-command (name buffer
&rest args
)
728 "Start a program in a subprocess. Return the process object for it.
729 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
730 NAME is name for process. It is modified if necessary to make it unique.
731 BUFFER is the buffer or (buffer-name) to associate with the process.
732 Process output goes at end of that buffer, unless you specify
733 an output stream or filter function to handle the output.
734 BUFFER may be also nil, meaning that this process is not associated
736 Third arg is command name, the name of a shell command.
737 Remaining arguments are the arguments for the command.
738 Wildcards and redirection are handled as usual in the shell."
740 ((eq system-type
'vax-vms
)
741 (apply 'start-process name buffer args
))
742 ;; We used to use `exec' to replace the shell with the command,
743 ;; but that failed to handle (...) and semicolon, etc.
745 (start-process name buffer shell-file-name shell-command-switch
746 (mapconcat 'identity args
" ")))))
748 (defmacro save-match-data
(&rest body
)
749 "Execute the BODY forms, restoring the global value of the match data."
750 (let ((original (make-symbol "match-data")))
751 (list 'let
(list (list original
'(match-data)))
752 (list 'unwind-protect
754 (list 'store-match-data original
)))))
756 (defun match-string (num &optional string
)
757 "Return string of text matched by last search.
758 NUM specifies which parenthesized expression in the last regexp.
759 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
760 Zero means the entire text matched by the whole regexp or whole string.
761 STRING should be given if the last search was by `string-match' on STRING."
762 (if (match-beginning num
)
764 (substring string
(match-beginning num
) (match-end num
))
765 (buffer-substring (match-beginning num
) (match-end num
)))))
767 (defun shell-quote-argument (argument)
768 "Quote an argument for passing as argument to an inferior shell."
769 (if (eq system-type
'ms-dos
)
770 ;; MS-DOS shells don't have quoting, so don't do any.
772 (if (eq system-type
'windows-nt
)
773 (concat "\"" argument
"\"")
774 ;; Quote everything except POSIX filename characters.
775 ;; This should be safe enough even for really weird shells.
776 (let ((result "") (start 0) end
)
777 (while (string-match "[^-0-9a-zA-Z_./]" argument start
)
778 (setq end
(match-beginning 0)
779 result
(concat result
(substring argument start end
)
780 "\\" (substring argument end
(1+ end
)))
782 (concat result
(substring argument start
))))))
784 (defun make-syntax-table (&optional oldtable
)
785 "Return a new syntax table.
786 It inherits all letters and control characters from the standard
787 syntax table; other characters are copied from the standard syntax table."
789 (copy-syntax-table oldtable
)
790 (let ((table (copy-syntax-table))
810 (defun global-set-key (key command
)
811 "Give KEY a global binding as COMMAND.
812 COMMAND is a symbol naming an interactively-callable function.
813 KEY is a key sequence (a string or vector of characters or event types).
814 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
815 can be included if you use a vector.
816 Note that if KEY has a local binding in the current buffer
817 that local binding will continue to shadow any global binding."
818 (interactive "KSet key globally: \nCSet key %s to command: ")
819 (or (vectorp key
) (stringp key
)
820 (signal 'wrong-type-argument
(list 'arrayp key
)))
821 (define-key (current-global-map) key command
)
824 (defun local-set-key (key command
)
825 "Give KEY a local binding as COMMAND.
826 COMMAND is a symbol naming an interactively-callable function.
827 KEY is a key sequence (a string or vector of characters or event types).
828 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
829 can be included if you use a vector.
830 The binding goes in the current buffer's local map,
831 which in most cases is shared with all other buffers in the same major mode."
832 (interactive "KSet key locally: \nCSet key %s locally to command: ")
833 (let ((map (current-local-map)))
835 (use-local-map (setq map
(make-sparse-keymap))))
836 (or (vectorp key
) (stringp key
)
837 (signal 'wrong-type-argument
(list 'arrayp key
)))
838 (define-key map key command
))
841 (defun global-unset-key (key)
842 "Remove global binding of KEY.
843 KEY is a string representing a sequence of keystrokes."
844 (interactive "kUnset key globally: ")
845 (global-set-key key nil
))
847 (defun local-unset-key (key)
848 "Remove local binding of KEY.
849 KEY is a string representing a sequence of keystrokes."
850 (interactive "kUnset key locally: ")
851 (if (current-local-map)
852 (local-set-key key nil
))
855 ;; We put this here instead of in frame.el so that it's defined even on
856 ;; systems where frame.el isn't loaded.
857 (defun frame-configuration-p (object)
858 "Return non-nil if OBJECT seems to be a frame configuration.
859 Any list whose car is `frame-configuration' is assumed to be a frame
862 (eq (car object
) 'frame-configuration
)))
866 ; "Returns the Nth element of LIST.
867 ;N counts from zero. If LIST is not that long, nil is returned."
868 ; (car (nthcdr n list)))
870 ;(defun copy-alist (alist)
871 ; "Return a copy of ALIST.
872 ;This is a new alist which represents the same mapping
873 ;from objects to objects, but does not share the alist structure with ALIST.
874 ;The objects mapped (cars and cdrs of elements of the alist)
875 ;are shared, however."
876 ; (setq alist (copy-sequence alist))
877 ; (let ((tail alist))
879 ; (if (consp (car tail))
880 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
881 ; (setq tail (cdr tail))))
884 ;;; subr.el ends here