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