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