(gnus-group-enter-directory): Virtual server definition
[emacs.git] / lisp / subr.el
blobb68876b66dac334055613d3feb66afcd98aa50f9
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 the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
22 ;;; Code:
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)))
55 ;; 'args))))
58 ;;;; Keymap support.
60 (defun undefined ()
61 (interactive)
62 (ding))
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)
73 (or nodigits
74 (let (loop)
75 (define-key map "-" 'negative-argument)
76 ;; Make plain numbers do numeric args.
77 (setq loop ?0)
78 (while (<= loop ?9)
79 (define-key map (char-to-string loop) 'digit-argument)
80 (setq loop (1+ loop))))))
82 ;Moved to keymap.c
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))
101 (vec1 (vector nil))
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.
107 (while (consp scan)
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.
113 (aset vec1 0 char)
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)
126 (fboundp inner-def))
127 (setq inner-def (symbol-function inner-def)))
128 (if (eq defn olddef)
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)))
134 (or (null elt)
135 (keymapp elt)))
136 ;; Avoid recursively rescanning keymap being scanned.
137 (not (memq inner-def
138 key-substitution-in-progress)))
139 ;; If this one isn't being scanned already,
140 ;; scan it now.
141 (substitute-key-definition olddef newdef keymap
142 inner-def
143 prefix1)))))
144 (if (arrayp (car scan))
145 (let* ((array (car scan))
146 (len (length array))
147 (i 0))
148 (while (< i len)
149 (let ((char i) (defn (aref array i)))
150 ;; The inside of this let duplicates exactly
151 ;; the inside of the previous let.
152 (aset vec1 0 char)
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)
163 (fboundp inner-def))
164 (setq inner-def (symbol-function inner-def)))
165 (if (eq defn olddef)
166 (define-key keymap prefix1
167 (nconc (nreverse skipped) newdef))
168 (if (and (keymapp defn)
169 (let ((elt (lookup-key keymap prefix1)))
170 (or (null elt)
171 (keymapp elt)))
172 (not (memq inner-def
173 key-substitution-in-progress)))
174 (substitute-key-definition olddef newdef keymap
175 inner-def
176 prefix1)))))
177 (setq i (1+ i))))))
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
184 of the map.
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."
188 (or (keymapp keymap)
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)
203 (null (cdr tail)))
204 (progn
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)
209 (setq done t))
210 ;; Don't insert more than once.
211 (or inserted
212 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
213 (setq inserted t)))
214 (setq tail (cdr tail)))))
216 (put 'keyboard-translate-table 'char-table-extra-slots 0)
218 (defun keyboard-translate (from to)
219 "Translate character FROM to TO at a low level.
220 This function creates a `keyboard-translate-table' if necessary
221 and then modifies one entry in it."
222 (or (char-table-p keyboard-translate-table)
223 (setq keyboard-translate-table
224 (make-char-table 'keyboard-translate-table nil)))
225 (aset keyboard-translate-table from to))
228 ;;;; The global keymap tree.
230 ;;; global-map, esc-map, and ctl-x-map have their values set up in
231 ;;; keymap.c; we just give them docstrings here.
233 (defvar global-map nil
234 "Default global keymap mapping Emacs keyboard input into commands.
235 The value is a keymap which is usually (but not necessarily) Emacs's
236 global map.")
238 (defvar esc-map nil
239 "Default keymap for ESC (meta) commands.
240 The normal global definition of the character ESC indirects to this keymap.")
242 (defvar ctl-x-map nil
243 "Default keymap for C-x commands.
244 The normal global definition of the character C-x indirects to this keymap.")
246 (defvar ctl-x-4-map (make-sparse-keymap)
247 "Keymap for subcommands of C-x 4")
248 (defalias 'ctl-x-4-prefix ctl-x-4-map)
249 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
251 (defvar ctl-x-5-map (make-sparse-keymap)
252 "Keymap for frame commands.")
253 (defalias 'ctl-x-5-prefix ctl-x-5-map)
254 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
257 ;;;; Event manipulation functions.
259 ;; The call to `read' is to ensure that the value is computed at load time
260 ;; and not compiled into the .elc file. The value is negative on most
261 ;; machines, but not on all!
262 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
264 (defun listify-key-sequence (key)
265 "Convert a key sequence to a list of events."
266 (if (vectorp key)
267 (append key nil)
268 (mapcar (function (lambda (c)
269 (if (> c 127)
270 (logxor c listify-key-sequence-1)
271 c)))
272 (append key nil))))
274 (defsubst eventp (obj)
275 "True if the argument is an event object."
276 (or (integerp obj)
277 (and (symbolp obj)
278 (get obj 'event-symbol-elements))
279 (and (consp obj)
280 (symbolp (car obj))
281 (get (car obj) 'event-symbol-elements))))
283 (defun event-modifiers (event)
284 "Returns a list of symbols representing the modifier keys in event EVENT.
285 The elements of the list may include `meta', `control',
286 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
287 and `down'."
288 (let ((type event))
289 (if (listp type)
290 (setq type (car type)))
291 (if (symbolp type)
292 (cdr (get type 'event-symbol-elements))
293 (let ((list nil))
294 (or (zerop (logand type ?\M-\^@))
295 (setq list (cons 'meta list)))
296 (or (and (zerop (logand type ?\C-\^@))
297 (>= (logand type 127) 32))
298 (setq list (cons 'control list)))
299 (or (and (zerop (logand type ?\S-\^@))
300 (= (logand type 255) (downcase (logand type 255))))
301 (setq list (cons 'shift list)))
302 (or (zerop (logand type ?\H-\^@))
303 (setq list (cons 'hyper list)))
304 (or (zerop (logand type ?\s-\^@))
305 (setq list (cons 'super list)))
306 (or (zerop (logand type ?\A-\^@))
307 (setq list (cons 'alt list)))
308 list))))
310 (defun event-basic-type (event)
311 "Returns the basic type of the given event (all modifiers removed).
312 The value is an ASCII printing character (not upper case) or a symbol."
313 (if (consp event)
314 (setq event (car event)))
315 (if (symbolp event)
316 (car (get event 'event-symbol-elements))
317 (let ((base (logand event (1- (lsh 1 18)))))
318 (downcase (if (< base 32) (logior base 64) base)))))
320 (defsubst mouse-movement-p (object)
321 "Return non-nil if OBJECT is a mouse movement event."
322 (and (consp object)
323 (eq (car object) 'mouse-movement)))
325 (defsubst event-start (event)
326 "Return the starting position of EVENT.
327 If EVENT is a mouse press or a mouse click, this returns the location
328 of the event.
329 If EVENT is a drag, this returns the drag's starting position.
330 The return value is of the form
331 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
332 The `posn-' functions access elements of such lists."
333 (nth 1 event))
335 (defsubst event-end (event)
336 "Return the ending location of EVENT. EVENT should be a click or drag event.
337 If EVENT is a click event, this function is the same as `event-start'.
338 The return value is of the form
339 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
340 The `posn-' functions access elements of such lists."
341 (nth (if (consp (nth 2 event)) 2 1) event))
343 (defsubst event-click-count (event)
344 "Return the multi-click count of EVENT, a click or drag event.
345 The return value is a positive integer."
346 (if (integerp (nth 2 event)) (nth 2 event) 1))
348 (defsubst posn-window (position)
349 "Return the window in POSITION.
350 POSITION should be a list of the form
351 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
352 as returned by the `event-start' and `event-end' functions."
353 (nth 0 position))
355 (defsubst posn-point (position)
356 "Return the buffer location 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."
360 (if (consp (nth 1 position))
361 (car (nth 1 position))
362 (nth 1 position)))
364 (defsubst posn-x-y (position)
365 "Return the x and y coordinates in POSITION.
366 POSITION should be a list of the form
367 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
368 as returned by the `event-start' and `event-end' functions."
369 (nth 2 position))
371 (defun posn-col-row (position)
372 "Return the column and row in POSITION, measured in characters.
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.
376 For a scroll-bar event, the result column is 0, and the row
377 corresponds to the vertical position of the click in the scroll bar."
378 (let ((pair (nth 2 position))
379 (window (posn-window position)))
380 (if (eq (if (consp (nth 1 position))
381 (car (nth 1 position))
382 (nth 1 position))
383 'vertical-scroll-bar)
384 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
385 (if (eq (if (consp (nth 1 position))
386 (car (nth 1 position))
387 (nth 1 position))
388 'horizontal-scroll-bar)
389 (cons (scroll-bar-scale pair (window-width window)) 0)
390 (let* ((frame (if (framep window) window (window-frame window)))
391 (x (/ (car pair) (frame-char-width frame)))
392 (y (/ (cdr pair) (frame-char-height frame))))
393 (cons x y))))))
395 (defsubst posn-timestamp (position)
396 "Return the timestamp of POSITION.
397 POSITION should be a list of the form
398 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
399 as returned by the `event-start' and `event-end' functions."
400 (nth 3 position))
403 ;;;; Obsolescent names for functions.
405 (defalias 'dot 'point)
406 (defalias 'dot-marker 'point-marker)
407 (defalias 'dot-min 'point-min)
408 (defalias 'dot-max 'point-max)
409 (defalias 'window-dot 'window-point)
410 (defalias 'set-window-dot 'set-window-point)
411 (defalias 'read-input 'read-string)
412 (defalias 'send-string 'process-send-string)
413 (defalias 'send-region 'process-send-region)
414 (defalias 'show-buffer 'set-window-buffer)
415 (defalias 'buffer-flush-undo 'buffer-disable-undo)
416 (defalias 'eval-current-buffer 'eval-buffer)
417 (defalias 'compiled-function-p 'byte-code-function-p)
419 ;; Some programs still use this as a function.
420 (defun baud-rate ()
421 "Obsolete function returning the value of the `baud-rate' variable.
422 Please convert your programs to use the variable `baud-rate' directly."
423 baud-rate)
425 (defalias 'focus-frame 'ignore)
426 (defalias 'unfocus-frame 'ignore)
428 ;;;; Alternate names for functions - these are not being phased out.
430 (defalias 'string= 'string-equal)
431 (defalias 'string< 'string-lessp)
432 (defalias 'move-marker 'set-marker)
433 (defalias 'eql 'eq)
434 (defalias 'not 'null)
435 (defalias 'rplaca 'setcar)
436 (defalias 'rplacd 'setcdr)
437 (defalias 'beep 'ding) ;preserve lingual purity
438 (defalias 'indent-to-column 'indent-to)
439 (defalias 'backward-delete-char 'delete-backward-char)
440 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
441 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
442 (defalias 'int-to-string 'number-to-string)
443 (defalias 'set-match-data 'store-match-data)
445 ;;; Should this be an obsolete name? If you decide it should, you get
446 ;;; to go through all the sources and change them.
447 (defalias 'string-to-int 'string-to-number)
449 ;;;; Hook manipulation functions.
451 ;; We used to have this variable so that C code knew how to run hooks. That
452 ;; calling convention is made obsolete now the hook running functions are in C.
453 (defconst run-hooks 'run-hooks
454 "Variable by which C primitives find the function `run-hooks'.
455 Don't change it. Don't use it either; use the hook running C primitives.")
457 (defun make-local-hook (hook)
458 "Make the hook HOOK local to the current buffer.
459 When a hook is local, its local and global values
460 work in concert: running the hook actually runs all the hook
461 functions listed in *either* the local value *or* the global value
462 of the hook variable.
464 This function works by making `t' a member of the buffer-local value,
465 which acts as a flag to run the hook functions in the default value as
466 well. This works for all normal hooks, but does not work for most
467 non-normal hooks yet. We will be changing the callers of non-normal
468 hooks so that they can handle localness; this has to be done one by
469 one.
471 This function does nothing if HOOK is already local in the current
472 buffer.
474 Do not use `make-local-variable' to make a hook variable buffer-local."
475 (if (local-variable-p hook)
477 (or (boundp hook) (set hook nil))
478 (make-local-variable hook)
479 (set hook (list t))))
481 (defun add-hook (hook function &optional append local)
482 "Add to the value of HOOK the function FUNCTION.
483 FUNCTION is not added if already present.
484 FUNCTION is added (if necessary) at the beginning of the hook list
485 unless the optional argument APPEND is non-nil, in which case
486 FUNCTION is added at the end.
488 The optional fourth argument, LOCAL, if non-nil, says to modify
489 the hook's buffer-local value rather than its default value.
490 This makes no difference if the hook is not buffer-local.
491 To make a hook variable buffer-local, always use
492 `make-local-hook', not `make-local-variable'.
494 HOOK should be a symbol, and FUNCTION may be any valid function. If
495 HOOK is void, it is first set to nil. If HOOK's value is a single
496 function, it is changed to a list of functions."
497 (or (boundp hook) (set hook nil))
498 (or (default-boundp hook) (set-default hook nil))
499 ;; If the hook value is a single function, turn it into a list.
500 (let ((old (symbol-value hook)))
501 (if (or (not (listp old)) (eq (car old) 'lambda))
502 (set hook (list old))))
503 (if (or local
504 ;; Detect the case where make-local-variable was used on a hook
505 ;; and do what we used to do.
506 (and (local-variable-if-set-p hook)
507 (not (memq t (symbol-value hook)))))
508 ;; Alter the local value only.
509 (or (if (consp function)
510 (member function (symbol-value hook))
511 (memq function (symbol-value hook)))
512 (set hook
513 (if append
514 (append (symbol-value hook) (list function))
515 (cons function (symbol-value hook)))))
516 ;; Alter the global value (which is also the only value,
517 ;; if the hook doesn't have a local value).
518 (or (if (consp function)
519 (member function (default-value hook))
520 (memq function (default-value hook)))
521 (set-default hook
522 (if append
523 (append (default-value hook) (list function))
524 (cons function (default-value hook)))))))
526 (defun remove-hook (hook function &optional local)
527 "Remove from the value of HOOK the function FUNCTION.
528 HOOK should be a symbol, and FUNCTION may be any valid function. If
529 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
530 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
532 The optional third argument, LOCAL, if non-nil, says to modify
533 the hook's buffer-local value rather than its default value.
534 This makes no difference if the hook is not buffer-local.
535 To make a hook variable buffer-local, always use
536 `make-local-hook', not `make-local-variable'."
537 (if (or (not (boundp hook)) ;unbound symbol, or
538 (not (default-boundp 'hook))
539 (null (symbol-value hook)) ;value is nil, or
540 (null function)) ;function is nil, then
541 nil ;Do nothing.
542 (if (or local
543 ;; Detect the case where make-local-variable was used on a hook
544 ;; and do what we used to do.
545 (and (local-variable-p hook)
546 (not (memq t (symbol-value hook)))))
547 (let ((hook-value (symbol-value hook)))
548 (if (consp hook-value)
549 (if (member function hook-value)
550 (setq hook-value (delete function (copy-sequence hook-value))))
551 (if (equal hook-value function)
552 (setq hook-value nil)))
553 (set hook hook-value))
554 (let ((hook-value (default-value hook)))
555 (if (consp hook-value)
556 (if (member function hook-value)
557 (setq hook-value (delete function (copy-sequence hook-value))))
558 (if (equal hook-value function)
559 (setq hook-value nil)))
560 (set-default hook hook-value)))))
562 (defun add-to-list (list-var element)
563 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
564 The test for presence of ELEMENT is done with `equal'.
565 If you want to use `add-to-list' on a variable that is not defined
566 until a certain package is loaded, you should put the call to `add-to-list'
567 into a hook function that will be run only after loading the package.
568 `eval-after-load' provides one way to do this. In some cases
569 other hooks, such as major mode hooks, can do the job."
570 (or (member element (symbol-value list-var))
571 (set list-var (cons element (symbol-value list-var)))))
573 ;;;; Specifying things to do after certain files are loaded.
575 (defun eval-after-load (file form)
576 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
577 This makes or adds to an entry on `after-load-alist'.
578 If FILE is already loaded, evaluate FORM right now.
579 It does nothing if FORM is already on the list for FILE.
580 FILE should be the name of a library, with no directory name."
581 ;; Make sure there is an element for FILE.
582 (or (assoc file after-load-alist)
583 (setq after-load-alist (cons (list file) after-load-alist)))
584 ;; Add FORM to the element if it isn't there.
585 (let ((elt (assoc file after-load-alist)))
586 (or (member form (cdr elt))
587 (progn
588 (nconc elt (list form))
589 ;; If the file has been loaded already, run FORM right away.
590 (and (assoc file load-history)
591 (eval form)))))
592 form)
594 (defun eval-next-after-load (file)
595 "Read the following input sexp, and run it whenever FILE is loaded.
596 This makes or adds to an entry on `after-load-alist'.
597 FILE should be the name of a library, with no directory name."
598 (eval-after-load file (read)))
601 ;;;; Input and display facilities.
603 (defun read-quoted-char (&optional prompt)
604 "Like `read-char', except that if the first character read is an octal
605 digit, we read up to two more octal digits and return the character
606 represented by the octal number consisting of those digits.
607 Optional argument PROMPT specifies a string to use to prompt the user."
608 (let ((message-log-max nil) (count 0) (code 0) char)
609 (while (< count 3)
610 (let ((inhibit-quit (zerop count))
611 ;; Don't let C-h get the help message--only help function keys.
612 (help-char nil)
613 (help-form
614 "Type the special character you want to use,
615 or three octal digits representing its character code."))
616 (and prompt (message "%s-" prompt))
617 (setq char (read-char))
618 (if inhibit-quit (setq quit-flag nil)))
619 (cond ((null char))
620 ((and (<= ?0 char) (<= char ?7))
621 (setq code (+ (* code 8) (- char ?0))
622 count (1+ count))
623 (and prompt (setq prompt (message "%s %c" prompt char))))
624 ((> count 0)
625 (setq unread-command-events (list char) count 259))
626 (t (setq code char count 259))))
627 ;; Turn a meta-character into a character with the 0200 bit set.
628 (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
629 (logand 255 code))))
631 (defun force-mode-line-update (&optional all)
632 "Force the mode-line of the current buffer to be redisplayed.
633 With optional non-nil ALL, force redisplay of all mode-lines."
634 (if all (save-excursion (set-buffer (other-buffer))))
635 (set-buffer-modified-p (buffer-modified-p)))
637 (defun momentary-string-display (string pos &optional exit-char message)
638 "Momentarily display STRING in the buffer at POS.
639 Display remains until next character is typed.
640 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
641 otherwise it is then available as input (as a command if nothing else).
642 Display MESSAGE (optional fourth arg) in the echo area.
643 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
644 (or exit-char (setq exit-char ?\ ))
645 (let ((buffer-read-only nil)
646 ;; Don't modify the undo list at all.
647 (buffer-undo-list t)
648 (modified (buffer-modified-p))
649 (name buffer-file-name)
650 insert-end)
651 (unwind-protect
652 (progn
653 (save-excursion
654 (goto-char pos)
655 ;; defeat file locking... don't try this at home, kids!
656 (setq buffer-file-name nil)
657 (insert-before-markers string)
658 (setq insert-end (point))
659 ;; If the message end is off screen, recenter now.
660 (if (> (window-end) insert-end)
661 (recenter (/ (window-height) 2)))
662 ;; If that pushed message start off the screen,
663 ;; scroll to start it at the top of the screen.
664 (move-to-window-line 0)
665 (if (> (point) pos)
666 (progn
667 (goto-char pos)
668 (recenter 0))))
669 (message (or message "Type %s to continue editing.")
670 (single-key-description exit-char))
671 (let ((char (read-event)))
672 (or (eq char exit-char)
673 (setq unread-command-events (list char)))))
674 (if insert-end
675 (save-excursion
676 (delete-region pos insert-end)))
677 (setq buffer-file-name name)
678 (set-buffer-modified-p modified))))
681 ;;;; Miscellanea.
683 ;; A number of major modes set this locally.
684 ;; Give it a global value to avoid compiler warnings.
685 (defvar font-lock-defaults nil)
687 ;; Avoid compiler warnings about this variable,
688 ;; which has a special meaning on certain system types.
689 (defvar buffer-file-type nil
690 "Non-nil if the visited file is a binary file.
691 This variable is meaningful on MS-DOG and Windows NT.
692 On those systems, it is automatically local in every buffer.
693 On other systems, this variable is normally always nil.")
695 ;; This should probably be written in C (i.e., without using `walk-windows').
696 (defun get-buffer-window-list (buffer &optional minibuf frame)
697 "Return windows currently displaying BUFFER, or nil if none.
698 See `walk-windows' for the meaning of MINIBUF and FRAME."
699 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
700 (walk-windows (function (lambda (window)
701 (if (eq (window-buffer window) buffer)
702 (setq windows (cons window windows)))))
703 minibuf frame)
704 windows))
706 (defun ignore (&rest ignore)
707 "Do nothing and return nil.
708 This function accepts any number of arguments, but ignores them."
709 (interactive)
710 nil)
712 (defun error (&rest args)
713 "Signal an error, making error message by passing all args to `format'.
714 In Emacs, the convention is that error messages start with a capital
715 letter but *do not* end with a period. Please follow this convention
716 for the sake of consistency."
717 (while t
718 (signal 'error (list (apply 'format args)))))
720 (defalias 'user-original-login-name 'user-login-name)
722 (defun start-process-shell-command (name buffer &rest args)
723 "Start a program in a subprocess. Return the process object for it.
724 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
725 NAME is name for process. It is modified if necessary to make it unique.
726 BUFFER is the buffer or (buffer-name) to associate with the process.
727 Process output goes at end of that buffer, unless you specify
728 an output stream or filter function to handle the output.
729 BUFFER may be also nil, meaning that this process is not associated
730 with any buffer
731 Third arg is command name, the name of a shell command.
732 Remaining arguments are the arguments for the command.
733 Wildcards and redirection are handled as usual in the shell."
734 (cond
735 ((eq system-type 'vax-vms)
736 (apply 'start-process name buffer args))
737 ;; We used to use `exec' to replace the shell with the command,
738 ;; but that failed to handle (...) and semicolon, etc.
740 (start-process name buffer shell-file-name shell-command-switch
741 (mapconcat 'identity args " ")))))
743 (defmacro save-match-data (&rest body)
744 "Execute the BODY forms, restoring the global value of the match data."
745 (let ((original (make-symbol "match-data")))
746 (list 'let (list (list original '(match-data)))
747 (list 'unwind-protect
748 (cons 'progn body)
749 (list 'store-match-data original)))))
751 (defun match-string (num &optional string)
752 "Return string of text matched by last search.
753 NUM specifies which parenthesized expression in the last regexp.
754 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
755 Zero means the entire text matched by the whole regexp or whole string.
756 STRING should be given if the last search was by `string-match' on STRING."
757 (if (match-beginning num)
758 (if string
759 (substring string (match-beginning num) (match-end num))
760 (buffer-substring (match-beginning num) (match-end num)))))
762 (defun shell-quote-argument (argument)
763 "Quote an argument for passing as argument to an inferior shell."
764 (if (eq system-type 'ms-dos)
765 ;; MS-DOS shells don't have quoting, so don't do any.
766 argument
767 (if (eq system-type 'windows-nt)
768 (concat "\"" argument "\"")
769 ;; Quote everything except POSIX filename characters.
770 ;; This should be safe enough even for really weird shells.
771 (let ((result "") (start 0) end)
772 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
773 (setq end (match-beginning 0)
774 result (concat result (substring argument start end)
775 "\\" (substring argument end (1+ end)))
776 start (1+ end)))
777 (concat result (substring argument start))))))
779 (defun make-syntax-table (&optional oldtable)
780 "Return a new syntax table.
781 It inherits all letters and control characters from the standard
782 syntax table; other characters are copied from the standard syntax table."
783 (if oldtable
784 (copy-syntax-table oldtable)
785 (let ((table (copy-syntax-table))
787 (setq i 0)
788 (while (<= i 31)
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 ?a)
796 (while (<= i ?z)
797 (aset table i nil)
798 (setq i (1+ i)))
799 (setq i 128)
800 (while (<= i 255)
801 (aset table i nil)
802 (setq i (1+ i)))
803 table)))
805 (defun global-set-key (key command)
806 "Give KEY a global binding as COMMAND.
807 COMMAND is a symbol naming an interactively-callable function.
808 KEY is a key sequence (a string or vector of characters or event types).
809 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
810 can be included if you use a vector.
811 Note that if KEY has a local binding in the current buffer
812 that local binding will continue to shadow any global binding."
813 (interactive "KSet key globally: \nCSet key %s to command: ")
814 (or (vectorp key) (stringp key)
815 (signal 'wrong-type-argument (list 'arrayp key)))
816 (define-key (current-global-map) key command)
817 nil)
819 (defun local-set-key (key command)
820 "Give KEY a local binding as COMMAND.
821 COMMAND is a symbol naming an interactively-callable function.
822 KEY is a key sequence (a string or vector of characters or event types).
823 Non-ASCII characters with codes above 127 (such as ISO Latin-1)
824 can be included if you use a vector.
825 The binding goes in the current buffer's local map,
826 which in most cases is shared with all other buffers in the same major mode."
827 (interactive "KSet key locally: \nCSet key %s locally to command: ")
828 (let ((map (current-local-map)))
829 (or map
830 (use-local-map (setq map (make-sparse-keymap))))
831 (or (vectorp key) (stringp key)
832 (signal 'wrong-type-argument (list 'arrayp key)))
833 (define-key map key command))
834 nil)
836 (defun global-unset-key (key)
837 "Remove global binding of KEY.
838 KEY is a string representing a sequence of keystrokes."
839 (interactive "kUnset key globally: ")
840 (global-set-key key nil))
842 (defun local-unset-key (key)
843 "Remove local binding of KEY.
844 KEY is a string representing a sequence of keystrokes."
845 (interactive "kUnset key locally: ")
846 (if (current-local-map)
847 (local-set-key key nil))
848 nil)
850 ;; We put this here instead of in frame.el so that it's defined even on
851 ;; systems where frame.el isn't loaded.
852 (defun frame-configuration-p (object)
853 "Return non-nil if OBJECT seems to be a frame configuration.
854 Any list whose car is `frame-configuration' is assumed to be a frame
855 configuration."
856 (and (consp object)
857 (eq (car object) 'frame-configuration)))
859 ;; now in fns.c
860 ;(defun nth (n list)
861 ; "Returns the Nth element of LIST.
862 ;N counts from zero. If LIST is not that long, nil is returned."
863 ; (car (nthcdr n list)))
865 ;(defun copy-alist (alist)
866 ; "Return a copy of ALIST.
867 ;This is a new alist which represents the same mapping
868 ;from objects to objects, but does not share the alist structure with ALIST.
869 ;The objects mapped (cars and cdrs of elements of the alist)
870 ;are shared, however."
871 ; (setq alist (copy-sequence alist))
872 ; (let ((tail alist))
873 ; (while tail
874 ; (if (consp (car tail))
875 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
876 ; (setq tail (cdr tail))))
877 ; alist)
879 ;;; subr.el ends here