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