(C_SWITCH_SYSTEM): Increase maxmem when using IBM's compiler.
[bpt/emacs.git] / lisp / subr.el
CommitLineData
c88ab9ce 1;;; subr.el --- basic lisp subroutines for Emacs
630cc463 2
492878e4 3;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
be9b65ac
DL
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
492878e4 9;; the Free Software Foundation; either version 2, or (at your option)
be9b65ac
DL
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
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
630cc463 21;;; Code:
be9b65ac 22
9a5336ae
JB
23\f
24;;;; Lisp language features.
25
26(defmacro lambda (&rest cdr)
27 "Return a lambda expression.
28A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
29self-quoting; the result of evaluating the lambda expression is the
30expression itself. The lambda expression may then be treated as a
31function, i. e. stored as the function value of a symbol, passed to
32funcall or mapcar, etcetera.
33ARGS should take the same form as an argument list for a `defun'.
34DOCSTRING should be a string, as described for `defun'. It may be omitted.
35INTERACTIVE should be a call to the function `interactive', which see.
36It may also be omitted.
37BODY should be a list of lisp expressions."
38 ;; Note that this definition should not use backquotes; subr.el should not
39 ;; depend on backquote.el.
40 (list 'function (cons 'lambda cdr)))
41
42;;(defmacro defun-inline (name args &rest body)
43;; "Create an \"inline defun\" (actually a macro).
44;;Use just like `defun'."
45;; (nconc (list 'defmacro name '(&rest args))
46;; (if (stringp (car body))
47;; (prog1 (list (car body))
48;; (setq body (or (cdr body) body))))
49;; (list (list 'cons (list 'quote
50;; (cons 'lambda (cons args body)))
51;; 'args))))
52
53\f
54;;;; Window tree functions.
55
492878e4 56(defun one-window-p (&optional nomini)
be9b65ac
DL
57 "Returns non-nil if there is only one window.
58Optional arg NOMINI non-nil means don't count the minibuffer
59even if it is active."
492878e4
JB
60 (let ((base-window (selected-window)))
61 (if (and nomini (eq base-window (minibuffer-window)))
62 (setq base-window (next-window base-window)))
63 (eq base-window
64 (next-window base-window (if nomini 'arg)))))
be9b65ac 65
0cc89026 66(defun walk-windows (proc &optional minibuf all-frames)
be9b65ac
DL
67 "Cycle through all visible windows, calling PROC for each one.
68PROC is called with a window as argument.
69Optional second arg MINIBUF t means count the minibuffer window
70even if not active. If MINIBUF is neither t nor nil it means
71not to count the minibuffer even if it is active.
f1f2d09a
RS
72
73Optional third arg ALL-FRAMES, if t, means include all frames.
74ALL-FRAMES nil or omitted means cycle within the selected frame,
75but include the minibuffer window (if MINIBUF says so) that that
76frame uses, even if it is on another frame.
77If ALL-FRAMES is neither nil nor t, stick strictly to the selected frame."
7a18f5c2
RS
78 ;; If we start from the minibuffer window, don't fail to come back to it.
79 (if (window-minibuffer-p (selected-window))
80 (setq minibuf t))
be9b65ac
DL
81 (let* ((walk-windows-start (selected-window))
82 (walk-windows-current walk-windows-start))
83 (while (progn
84 (setq walk-windows-current
0cc89026 85 (next-window walk-windows-current minibuf all-frames))
be9b65ac
DL
86 (funcall proc walk-windows-current)
87 (not (eq walk-windows-current walk-windows-start))))))
88
79e0df73
RS
89(defun minibuffer-window-active-p (window)
90 "Return t if WINDOW (a minibuffer window) is now active."
91 ;; nil nil means include WINDOW's frame
92 ;; and other frames using WINDOW as minibuffer,
93 ;; and include minibuffer if active.
94 (let ((prev (previous-window window nil nil)))
95 ;; If PREV equals WINDOW, WINDOW must be on a minibuffer-only frame
96 ;; and it's not currently being used. So return nil.
97 (and (not (eq window prev))
98 (let ((should-be-same (next-window prev nil nil)))
99 ;; If next-window doesn't reverse previous-window,
100 ;; WINDOW must be outside the cycle specified by nil nil.
101 (eq should-be-same window)))))
9a5336ae
JB
102\f
103;;;; Keymap support.
be9b65ac
DL
104
105(defun undefined ()
106 (interactive)
107 (ding))
108
109;Prevent the \{...} documentation construct
110;from mentioning keys that run this command.
111(put 'undefined 'suppress-keymap t)
112
113(defun suppress-keymap (map &optional nodigits)
114 "Make MAP override all normally self-inserting keys to be undefined.
115Normally, as an exception, digits and minus-sign are set to make prefix args,
116but optional second arg NODIGITS non-nil treats them like other chars."
80e7b471 117 (substitute-key-definition 'self-insert-command 'undefined map global-map)
be9b65ac
DL
118 (or nodigits
119 (let (loop)
120 (define-key map "-" 'negative-argument)
121 ;; Make plain numbers do numeric args.
122 (setq loop ?0)
123 (while (<= loop ?9)
124 (define-key map (char-to-string loop) 'digit-argument)
125 (setq loop (1+ loop))))))
126
be9b65ac
DL
127;Moved to keymap.c
128;(defun copy-keymap (keymap)
129; "Return a copy of KEYMAP"
130; (while (not (keymapp keymap))
131; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
132; (if (vectorp keymap)
133; (copy-sequence keymap)
134; (copy-alist keymap)))
135
7f2c2edd 136(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
137 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
138In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
139If optional fourth argument OLDMAP is specified, we redefine
140in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
141 (or prefix (setq prefix ""))
142 (let* ((scan (or oldmap keymap))
143 (vec1 (vector nil))
144 (prefix1 (vconcat prefix vec1)))
145 ;; Scan OLDMAP, finding each char or event-symbol that
146 ;; has any definition, and act on it with hack-key.
147 (while (consp scan)
148 (if (consp (car scan))
149 (let ((char (car (car scan)))
150 (defn (cdr (car scan))))
151 ;; The inside of this let duplicates exactly
152 ;; the inside of the following let that handles array elements.
153 (aset vec1 0 char)
154 (aset prefix1 (length prefix) char)
44d798af 155 (let (inner-def skipped)
7f2c2edd
RS
156 ;; Skip past menu-prompt.
157 (while (stringp (car-safe defn))
44d798af 158 (setq skipped (cons (car defn) skipped))
7f2c2edd
RS
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)
44d798af 165 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
7f2c2edd
RS
166 (if (keymapp defn)
167 (substitute-key-definition olddef newdef keymap
168 inner-def
169 prefix1)))))
170 (if (arrayp (car scan))
171 (let* ((array (car scan))
172 (len (length array))
173 (i 0))
174 (while (< i len)
175 (let ((char i) (defn (aref array i)))
176 ;; The inside of this let duplicates exactly
177 ;; the inside of the previous let.
178 (aset vec1 0 char)
179 (aset prefix1 (length prefix) char)
44d798af 180 (let (inner-def skipped)
7f2c2edd
RS
181 ;; Skip past menu-prompt.
182 (while (stringp (car-safe defn))
44d798af 183 (setq skipped (cons (car defn) skipped))
7f2c2edd
RS
184 (setq defn (cdr defn)))
185 (setq inner-def defn)
186 (while (and (symbolp inner-def)
187 (fboundp inner-def))
188 (setq inner-def (symbol-function inner-def)))
189 (if (eq defn olddef)
44d798af
RS
190 (define-key keymap prefix1
191 (nconc (nreverse skipped) newdef))
7f2c2edd
RS
192 (if (keymapp defn)
193 (substitute-key-definition olddef newdef keymap
194 inner-def
195 prefix1)))))
196 (setq i (1+ i))))))
197 (setq scan (cdr scan)))))
9a5336ae 198
06ae9cf2 199(defun define-key-after (keymap key definition after)
4434d61b
RS
200 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
201This is like `define-key' except that the binding for KEY is placed
202just after the binding for the event AFTER, instead of at the beginning
203of the map.
626f67f3
RS
204The order matters when the keymap is used as a menu.
205KEY must contain just one event type--it must be a string or vector
206of length 1."
4434d61b
RS
207 (or (keymapp keymap)
208 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 209 (if (> (length key) 1)
626f67f3 210 (error "multi-event key specified in `define-key-after'"))
113d28a8 211 (let ((tail keymap) done inserted
4434d61b
RS
212 (first (aref key 0)))
213 (while (and (not done) tail)
214 ;; Delete any earlier bindings for the same key.
215 (if (eq (car-safe (car (cdr tail))) first)
216 (setcdr tail (cdr (cdr tail))))
217 ;; When we reach AFTER's binding, insert the new binding after.
218 ;; If we reach an inherited keymap, insert just before that.
113d28a8 219 ;; If we reach the end of this keymap, insert at the end.
4434d61b 220 (if (or (eq (car-safe (car tail)) after)
113d28a8
RS
221 (eq (car (cdr tail)) 'keymap)
222 (null (cdr tail)))
4434d61b 223 (progn
113d28a8
RS
224 ;; Stop the scan only if we find a parent keymap.
225 ;; Keep going past the inserted element
226 ;; so we can delete any duplications that come later.
227 (if (eq (car (cdr tail)) 'keymap)
228 (setq done t))
229 ;; Don't insert more than once.
230 (or inserted
231 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
232 (setq inserted t)))
4434d61b
RS
233 (setq tail (cdr tail)))))
234
9a5336ae
JB
235(defun keyboard-translate (from to)
236 "Translate character FROM to TO at a low level.
237This function creates a `keyboard-translate-table' if necessary
238and then modifies one entry in it."
239 (or (arrayp keyboard-translate-table)
240 (setq keyboard-translate-table ""))
241 (if (or (> from (length keyboard-translate-table))
242 (> to (length keyboard-translate-table)))
243 (progn
244 (let* ((i (length keyboard-translate-table))
f4ebbdbe
RS
245 (table (concat keyboard-translate-table
246 (make-string (- 256 i) 0))))
9a5336ae
JB
247 (while (< i 256)
248 (aset table i i)
249 (setq i (1+ i)))
250 (setq keyboard-translate-table table))))
251 (aset keyboard-translate-table from to))
252
253\f
254;;;; The global keymap tree.
255
256;;; global-map, esc-map, and ctl-x-map have their values set up in
257;;; keymap.c; we just give them docstrings here.
258
259(defvar global-map nil
260 "Default global keymap mapping Emacs keyboard input into commands.
261The value is a keymap which is usually (but not necessarily) Emacs's
262global map.")
263
264(defvar esc-map nil
265 "Default keymap for ESC (meta) commands.
266The normal global definition of the character ESC indirects to this keymap.")
267
268(defvar ctl-x-map nil
269 "Default keymap for C-x commands.
270The normal global definition of the character C-x indirects to this keymap.")
271
272(defvar ctl-x-4-map (make-sparse-keymap)
273 "Keymap for subcommands of C-x 4")
059184dd 274(defalias 'ctl-x-4-prefix ctl-x-4-map)
9a5336ae
JB
275(define-key ctl-x-map "4" 'ctl-x-4-prefix)
276
277(defvar ctl-x-5-map (make-sparse-keymap)
278 "Keymap for frame commands.")
059184dd 279(defalias 'ctl-x-5-prefix ctl-x-5-map)
9a5336ae
JB
280(define-key ctl-x-map "5" 'ctl-x-5-prefix)
281
0f03054a 282\f
9a5336ae
JB
283;;;; Event manipulation functions.
284
114137b8
RS
285;; This code exists specifically to make sure that the
286;; resulting number does not appear in the .elc file.
287;; The number is negative on most machines, but not on all!
288(defconst listify-key-sequence-1
289 (lsh 1 7))
290(setq listify-key-sequence-1 (logior (lsh 1 23) listify-key-sequence-1))
291
cde6d7e3
RS
292(defun listify-key-sequence (key)
293 "Convert a key sequence to a list of events."
294 (if (vectorp key)
295 (append key nil)
296 (mapcar (function (lambda (c)
297 (if (> c 127)
114137b8 298 (logxor c listify-key-sequence-1)
cde6d7e3
RS
299 c)))
300 (append key nil))))
301
53e5a4e8
RS
302(defsubst eventp (obj)
303 "True if the argument is an event object."
304 (or (integerp obj)
305 (and (symbolp obj)
306 (get obj 'event-symbol-elements))
307 (and (consp obj)
308 (symbolp (car obj))
309 (get (car obj) 'event-symbol-elements))))
310
311(defun event-modifiers (event)
312 "Returns a list of symbols representing the modifier keys in event EVENT.
313The elements of the list may include `meta', `control',
32295976
RS
314`shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
315and `down'."
53e5a4e8
RS
316 (let ((type event))
317 (if (listp type)
318 (setq type (car type)))
319 (if (symbolp type)
320 (cdr (get type 'event-symbol-elements))
321 (let ((list nil))
322 (or (zerop (logand type (lsh 1 23)))
323 (setq list (cons 'meta list)))
324 (or (and (zerop (logand type (lsh 1 22)))
325 (>= (logand type 127) 32))
326 (setq list (cons 'control list)))
327 (or (and (zerop (logand type (lsh 1 21)))
328 (= (logand type 255) (downcase (logand type 255))))
329 (setq list (cons 'shift list)))
330 (or (zerop (logand type (lsh 1 20)))
331 (setq list (cons 'hyper list)))
332 (or (zerop (logand type (lsh 1 19)))
333 (setq list (cons 'super list)))
334 (or (zerop (logand type (lsh 1 18)))
335 (setq list (cons 'alt list)))
336 list))))
337
d63de416
RS
338(defun event-basic-type (event)
339 "Returns the basic type of the given event (all modifiers removed).
340The value is an ASCII printing character (not upper case) or a symbol."
2b0f4ba5
JB
341 (if (consp event)
342 (setq event (car event)))
d63de416
RS
343 (if (symbolp event)
344 (car (get event 'event-symbol-elements))
345 (let ((base (logand event (1- (lsh 1 18)))))
346 (downcase (if (< base 32) (logior base 64) base)))))
347
0f03054a
RS
348(defsubst mouse-movement-p (object)
349 "Return non-nil if OBJECT is a mouse movement event."
350 (and (consp object)
351 (eq (car object) 'mouse-movement)))
352
353(defsubst event-start (event)
354 "Return the starting position of EVENT.
355If EVENT is a mouse press or a mouse click, this returns the location
356of the event.
357If EVENT is a drag, this returns the drag's starting position.
358The return value is of the form
e55c21be 359 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
360The `posn-' functions access elements of such lists."
361 (nth 1 event))
362
363(defsubst event-end (event)
364 "Return the ending location of EVENT. EVENT should be a click or drag event.
365If EVENT is a click event, this function is the same as `event-start'.
366The return value is of the form
e55c21be 367 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 368The `posn-' functions access elements of such lists."
69b95560 369 (nth (if (consp (nth 2 event)) 2 1) event))
0f03054a 370
32295976
RS
371(defsubst event-click-count (event)
372 "Return the multi-click count of EVENT, a click or drag event.
373The return value is a positive integer."
374 (if (integerp (nth 2 event)) (nth 2 event) 1))
375
0f03054a
RS
376(defsubst posn-window (position)
377 "Return the window in POSITION.
378POSITION should be a list of the form
e55c21be 379 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
380as returned by the `event-start' and `event-end' functions."
381 (nth 0 position))
382
383(defsubst posn-point (position)
384 "Return the buffer location in POSITION.
385POSITION should be a list of the form
e55c21be 386 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 387as returned by the `event-start' and `event-end' functions."
15db4e0e
JB
388 (if (consp (nth 1 position))
389 (car (nth 1 position))
390 (nth 1 position)))
0f03054a 391
e55c21be
RS
392(defsubst posn-x-y (position)
393 "Return the x and y coordinates in POSITION.
0f03054a 394POSITION should be a list of the form
e55c21be 395 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
396as returned by the `event-start' and `event-end' functions."
397 (nth 2 position))
398
e55c21be
RS
399(defsubst posn-col-row (position)
400 "Return the row and column in POSITION, measured in characters.
401POSITION should be a list of the form
402 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
403as returned by the `event-start' and `event-end' functions."
404 (let* ((pair (nth 2 position))
405 (window (posn-window position))
406 (frame (if (framep window) window (window-frame window)))
407 (x (/ (car pair) (frame-char-width frame)))
408 (y (/ (cdr pair) (frame-char-height frame))))
409 (cons x y)))
410
0f03054a
RS
411(defsubst posn-timestamp (position)
412 "Return the timestamp of POSITION.
413POSITION should be a list of the form
e55c21be 414 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
f415c00c 415as returned by the `event-start' and `event-end' functions."
0f03054a 416 (nth 3 position))
9a5336ae 417
0f03054a 418\f
9a5336ae
JB
419;;;; Obsolescent names for functions.
420
059184dd
ER
421(defalias 'dot 'point)
422(defalias 'dot-marker 'point-marker)
423(defalias 'dot-min 'point-min)
424(defalias 'dot-max 'point-max)
425(defalias 'window-dot 'window-point)
426(defalias 'set-window-dot 'set-window-point)
427(defalias 'read-input 'read-string)
428(defalias 'send-string 'process-send-string)
429(defalias 'send-region 'process-send-region)
430(defalias 'show-buffer 'set-window-buffer)
431(defalias 'buffer-flush-undo 'buffer-disable-undo)
432(defalias 'eval-current-buffer 'eval-buffer)
433(defalias 'compiled-function-p 'byte-code-function-p)
be9b65ac 434
9a5336ae
JB
435;; Some programs still use this as a function.
436(defun baud-rate ()
bcacc42c
RS
437 "Obsolete function returning the value of the `baud-rate' variable.
438Please convert your programs to use the variable `baud-rate' directly."
9a5336ae
JB
439 baud-rate)
440
441\f
442;;;; Alternate names for functions - these are not being phased out.
443
059184dd
ER
444(defalias 'string= 'string-equal)
445(defalias 'string< 'string-lessp)
446(defalias 'move-marker 'set-marker)
447(defalias 'eql 'eq)
448(defalias 'not 'null)
449(defalias 'rplaca 'setcar)
450(defalias 'rplacd 'setcdr)
eb8c3be9 451(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
452(defalias 'indent-to-column 'indent-to)
453(defalias 'backward-delete-char 'delete-backward-char)
454(defalias 'search-forward-regexp (symbol-function 're-search-forward))
455(defalias 'search-backward-regexp (symbol-function 're-search-backward))
456(defalias 'int-to-string 'number-to-string)
37f6661a
JB
457
458;;; Should this be an obsolete name? If you decide it should, you get
459;;; to go through all the sources and change them.
059184dd 460(defalias 'string-to-int 'string-to-number)
be9b65ac 461\f
9a5336ae 462;;;; Hook manipulation functions.
be9b65ac 463
be9b65ac
DL
464(defun run-hooks (&rest hooklist)
465 "Takes hook names and runs each one in turn. Major mode functions use this.
466Each argument should be a symbol, a hook variable.
467These symbols are processed in the order specified.
468If a hook symbol has a non-nil value, that value may be a function
469or a list of functions to be called to run the hook.
470If the value is a function, it is called with no arguments.
471If it is a list, the elements are called, in order, with no arguments."
472 (while hooklist
473 (let ((sym (car hooklist)))
474 (and (boundp sym)
475 (symbol-value sym)
476 (let ((value (symbol-value sym)))
477 (if (and (listp value) (not (eq (car value) 'lambda)))
478 (mapcar 'funcall value)
479 (funcall value)))))
480 (setq hooklist (cdr hooklist))))
481
482;; Tell C code how to call this function.
483(defconst run-hooks 'run-hooks
484 "Variable by which C primitives find the function `run-hooks'.
485Don't change it.")
486
08159178 487(defun add-hook (hook function &optional append)
32295976
RS
488 "Add to the value of HOOK the function FUNCTION.
489FUNCTION is not added if already present.
490FUNCTION is added (if necessary) at the beginning of the hook list
491unless the optional argument APPEND is non-nil, in which case
492FUNCTION is added at the end.
493
494HOOK should be a symbol, and FUNCTION may be any valid function. If
495HOOK is void, it is first set to nil. If HOOK's value is a single
496function, it is changed to a list of functions."
be9b65ac 497 (or (boundp hook) (set hook nil))
32295976
RS
498 ;; If the hook value is a single function, turn it into a list.
499 (let ((old (symbol-value hook)))
500 (if (or (not (listp old)) (eq (car old) 'lambda))
501 (set hook (list old))))
be9b65ac 502 (or (if (consp function)
f48c1d8d 503 (member function (symbol-value hook))
be9b65ac 504 (memq function (symbol-value hook)))
08159178
ER
505 (set hook
506 (if append
507 (nconc (symbol-value hook) (list function))
508 (cons function (symbol-value hook))))))
9a5336ae 509
24980d16
RS
510(defun remove-hook (hook function)
511 "Remove from the value of HOOK the function FUNCTION.
512HOOK should be a symbol, and FUNCTION may be any valid function. If
513FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
827534ea 514list of hooks to run in HOOK, then nothing is done. See `add-hook'."
24980d16
RS
515 (if (or (not (boundp hook)) ;unbound symbol, or
516 (null (symbol-value hook)) ;value is nil, or
517 (null function)) ;function is nil, then
518 nil ;Do nothing.
519 (let ((hook-value (symbol-value hook)))
520 (if (consp hook-value)
521 (setq hook-value (delete function hook-value))
f48c1d8d 522 (if (equal hook-value function)
24980d16
RS
523 (setq hook-value nil)))
524 (set hook hook-value))))
be9b65ac 525\f
9a5336ae
JB
526;;;; Specifying things to do after certain files are loaded.
527
528(defun eval-after-load (file form)
529 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
530This makes or adds to an entry on `after-load-alist'.
12c7071c 531It does nothing if FORM is already on the list for FILE.
9a5336ae
JB
532FILE should be the name of a library, with no directory name."
533 (or (assoc file after-load-alist)
534 (setq after-load-alist (cons (list file) after-load-alist)))
12c7071c
RS
535 (let ((elt (assoc file after-load-alist)))
536 (or (member form (cdr elt))
537 (nconc elt (list form))))
9a5336ae
JB
538 form)
539
540(defun eval-next-after-load (file)
541 "Read the following input sexp, and run it whenever FILE is loaded.
542This makes or adds to an entry on `after-load-alist'.
543FILE should be the name of a library, with no directory name."
544 (eval-after-load file (read)))
545
546\f
547;;;; Input and display facilities.
548
549(defun read-quoted-char (&optional prompt)
550 "Like `read-char', except that if the first character read is an octal
551digit, we read up to two more octal digits and return the character
552represented by the octal number consisting of those digits.
553Optional argument PROMPT specifies a string to use to prompt the user."
554 (let ((count 0) (code 0) char)
555 (while (< count 3)
556 (let ((inhibit-quit (zerop count))
557 (help-form nil))
558 (and prompt (message "%s-" prompt))
559 (setq char (read-char))
560 (if inhibit-quit (setq quit-flag nil)))
561 (cond ((null char))
562 ((and (<= ?0 char) (<= char ?7))
563 (setq code (+ (* code 8) (- char ?0))
564 count (1+ count))
565 (and prompt (message (setq prompt
566 (format "%s %c" prompt char)))))
567 ((> count 0)
568 (setq unread-command-events (list char) count 259))
569 (t (setq code char count 259))))
570 (logand 255 code)))
571
572(defun force-mode-line-update (&optional all)
573 "Force the mode-line of the current buffer to be redisplayed.
574With optional non-nil ALL then force then force redisplay of all mode-lines."
575 (if all (save-excursion (set-buffer (other-buffer))))
576 (set-buffer-modified-p (buffer-modified-p)))
577
be9b65ac
DL
578(defun momentary-string-display (string pos &optional exit-char message)
579 "Momentarily display STRING in the buffer at POS.
580Display remains until next character is typed.
581If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
582otherwise it is then available as input (as a command if nothing else).
583Display MESSAGE (optional fourth arg) in the echo area.
584If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
585 (or exit-char (setq exit-char ?\ ))
586 (let ((buffer-read-only nil)
587 (modified (buffer-modified-p))
588 (name buffer-file-name)
589 insert-end)
590 (unwind-protect
591 (progn
592 (save-excursion
593 (goto-char pos)
594 ;; defeat file locking... don't try this at home, kids!
595 (setq buffer-file-name nil)
596 (insert-before-markers string)
3eec84bf
RS
597 (setq insert-end (point))
598 ;; If the message end is off screen, recenter now.
599 (if (> (window-end) insert-end)
600 (recenter (/ (window-height) 2)))
601 ;; If that pushed message start off the screen,
602 ;; scroll to start it at the top of the screen.
603 (move-to-window-line 0)
604 (if (> (point) pos)
605 (progn
606 (goto-char pos)
607 (recenter 0))))
be9b65ac
DL
608 (message (or message "Type %s to continue editing.")
609 (single-key-description exit-char))
3547c855 610 (let ((char (read-event)))
be9b65ac 611 (or (eq char exit-char)
dbc4e1c1 612 (setq unread-command-events (list char)))))
be9b65ac
DL
613 (if insert-end
614 (save-excursion
615 (delete-region pos insert-end)))
616 (setq buffer-file-name name)
617 (set-buffer-modified-p modified))))
618
9a5336ae
JB
619\f
620;;;; Miscellanea.
621
622(defun ignore (&rest ignore)
623 "Do nothing.
624Accept any number of arguments, but ignore them."
625 nil)
626
627(defun error (&rest args)
628 "Signal an error, making error message by passing all args to `format'."
629 (while t
630 (signal 'error (list (apply 'format args)))))
631
cef7ae6e 632(defalias 'user-original-login-name 'user-login-name)
9a5336ae 633
be9b65ac
DL
634(defun start-process-shell-command (name buffer &rest args)
635 "Start a program in a subprocess. Return the process object for it.
636Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
637NAME is name for process. It is modified if necessary to make it unique.
638BUFFER is the buffer or (buffer-name) to associate with the process.
639 Process output goes at end of that buffer, unless you specify
640 an output stream or filter function to handle the output.
641 BUFFER may be also nil, meaning that this process is not associated
642 with any buffer
643Third arg is command name, the name of a shell command.
644Remaining arguments are the arguments for the command.
4f1d6310 645Wildcards and redirection are handled as usual in the shell."
be9b65ac
DL
646 (if (eq system-type 'vax-vms)
647 (apply 'start-process name buffer args)
648 (start-process name buffer shell-file-name "-c"
649 (concat "exec " (mapconcat 'identity args " ")))))
be9b65ac 650
9a5336ae
JB
651(defmacro save-match-data (&rest body)
652 "Execute the BODY forms, restoring the global value of the match data."
653 (let ((original (make-symbol "match-data")))
654 (list
655 'let (list (list original '(match-data)))
656 (list 'unwind-protect
657 (cons 'progn body)
658 (list 'store-match-data original)))))
ffd56f97 659
8af7df60
RS
660(defun shell-quote-argument (argument)
661 "Quote an argument for passing as argument to an inferior shell."
662 ;; Quote everything except POSIX filename characters.
663 ;; This should be safe enough even for really weird shells.
664 (let ((result "") (start 0) end)
665 (while (string-match "[^---0-9a-zA-Z_./]" argument start)
666 (setq end (match-beginning 0)
667 result (concat result (substring argument start end)
668 "\\" (substring argument end (1+ end)))
669 start (1+ end)))
670 (concat result (substring argument start))))
671
297d863b 672(defun make-syntax-table (&optional oldtable)
984f718a
RS
673 "Return a new syntax table.
674It inherits all letters and control characters from the standard
675syntax table; other characters are copied from the standard syntax table."
297d863b
KH
676 (if oldtable
677 (copy-syntax-table oldtable)
678 (let ((table (copy-syntax-table))
679 i)
680 (setq i 0)
681 (while (<= i 31)
682 (aset table i 13)
683 (setq i (1+ i)))
684 (setq i ?A)
685 (while (<= i ?Z)
686 (aset table i 13)
687 (setq i (1+ i)))
688 (setq i ?a)
689 (while (<= i ?z)
690 (aset table i 13)
691 (setq i (1+ i)))
692 (setq i 128)
693 (while (<= i 255)
694 (aset table i 13)
695 (setq i (1+ i)))
696 table)))
984f718a 697
9a5336ae
JB
698;; now in fns.c
699;(defun nth (n list)
700; "Returns the Nth element of LIST.
701;N counts from zero. If LIST is not that long, nil is returned."
702; (car (nthcdr n list)))
703;
704;(defun copy-alist (alist)
705; "Return a copy of ALIST.
706;This is a new alist which represents the same mapping
707;from objects to objects, but does not share the alist structure with ALIST.
708;The objects mapped (cars and cdrs of elements of the alist)
709;are shared, however."
710; (setq alist (copy-sequence alist))
711; (let ((tail alist))
712; (while tail
713; (if (consp (car tail))
714; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
715; (setq tail (cdr tail))))
716; alist)
630cc463
ER
717
718;;; subr.el ends here
9a5336ae 719