]> code.delx.au - gnu-emacs/blob - lisp/subr.el
(substitute-key-definition): Don't discard menu strings.
[gnu-emacs] / 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 ;; 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))
81 (let* ((walk-windows-start (selected-window))
82 (walk-windows-current walk-windows-start))
83 (while (progn
84 (setq walk-windows-current
85 (next-window walk-windows-current minibuf all-frames))
86 (funcall proc walk-windows-current)
87 (not (eq walk-windows-current walk-windows-start))))))
88
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)))))
102 \f
103 ;;;; Keymap support.
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.
115 Normally, as an exception, digits and minus-sign are set to make prefix args,
116 but optional second arg NODIGITS non-nil treats them like other chars."
117 (substitute-key-definition 'self-insert-command 'undefined map global-map)
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
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
136 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
137 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
138 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
139 If optional fourth argument OLDMAP is specified, we redefine
140 in 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)
155 (let (inner-def skipped)
156 ;; Skip past menu-prompt.
157 (while (stringp (car-safe defn))
158 (setq skipped (cons (car defn) skipped))
159 (setq defn (cdr defn)))
160 (setq inner-def defn)
161 (while (and (symbolp inner-def)
162 (fboundp inner-def))
163 (setq inner-def (symbol-function inner-def)))
164 (if (eq defn olddef)
165 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
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)
180 (let (inner-def skipped)
181 ;; Skip past menu-prompt.
182 (while (stringp (car-safe defn))
183 (setq skipped (cons (car defn) skipped))
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)
190 (define-key keymap prefix1
191 (nconc (nreverse skipped) newdef))
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)))))
198
199 (defun define-key-after (keymap key definition after)
200 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
201 This is like `define-key' except that the binding for KEY is placed
202 just after the binding for the event AFTER, instead of at the beginning
203 of the map.
204 The order matters when the keymap is used as a menu.
205 KEY must contain just one event type--it must be a string or vector
206 of length 1."
207 (or (keymapp keymap)
208 (signal 'wrong-type-argument (list 'keymapp keymap)))
209 (if (> (length key) 1)
210 (error "multi-event key specified in `define-key-after'"))
211 (let ((tail keymap) done inserted
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.
219 ;; If we reach the end of this keymap, insert at the end.
220 (if (or (eq (car-safe (car tail)) after)
221 (eq (car (cdr tail)) 'keymap)
222 (null (cdr tail)))
223 (progn
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)))
233 (setq tail (cdr tail)))))
234
235 (defun keyboard-translate (from to)
236 "Translate character FROM to TO at a low level.
237 This function creates a `keyboard-translate-table' if necessary
238 and 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))
245 (table (concat keyboard-translate-table
246 (make-string (- 256 i) 0))))
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.
261 The value is a keymap which is usually (but not necessarily) Emacs's
262 global map.")
263
264 (defvar esc-map nil
265 "Default keymap for ESC (meta) commands.
266 The 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.
270 The 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")
274 (defalias 'ctl-x-4-prefix ctl-x-4-map)
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.")
279 (defalias 'ctl-x-5-prefix ctl-x-5-map)
280 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
281
282 \f
283 ;;;; Event manipulation functions.
284
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
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)
298 (logxor c listify-key-sequence-1)
299 c)))
300 (append key nil))))
301
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.
313 The elements of the list may include `meta', `control',
314 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
315 and `down'."
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
338 (defun event-basic-type (event)
339 "Returns the basic type of the given event (all modifiers removed).
340 The value is an ASCII printing character (not upper case) or a symbol."
341 (if (consp event)
342 (setq event (car event)))
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
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.
355 If EVENT is a mouse press or a mouse click, this returns the location
356 of the event.
357 If EVENT is a drag, this returns the drag's starting position.
358 The return value is of the form
359 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
360 The `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.
365 If EVENT is a click event, this function is the same as `event-start'.
366 The return value is of the form
367 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
368 The `posn-' functions access elements of such lists."
369 (nth (if (consp (nth 2 event)) 2 1) event))
370
371 (defsubst event-click-count (event)
372 "Return the multi-click count of EVENT, a click or drag event.
373 The return value is a positive integer."
374 (if (integerp (nth 2 event)) (nth 2 event) 1))
375
376 (defsubst posn-window (position)
377 "Return the window in POSITION.
378 POSITION should be a list of the form
379 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
380 as 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.
385 POSITION should be a list of the form
386 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
387 as returned by the `event-start' and `event-end' functions."
388 (if (consp (nth 1 position))
389 (car (nth 1 position))
390 (nth 1 position)))
391
392 (defsubst posn-col-row (position)
393 "Return the row and column in POSITION.
394 POSITION should be a list of the form
395 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
396 as returned by the `event-start' and `event-end' functions."
397 (nth 2 position))
398
399 (defsubst posn-timestamp (position)
400 "Return the timestamp of POSITION.
401 POSITION should be a list of the form
402 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
403 as returned by the `event-start' and `event-end' functions."
404 (nth 3 position))
405
406 \f
407 ;;;; Obsolescent names for functions.
408
409 (defalias 'dot 'point)
410 (defalias 'dot-marker 'point-marker)
411 (defalias 'dot-min 'point-min)
412 (defalias 'dot-max 'point-max)
413 (defalias 'window-dot 'window-point)
414 (defalias 'set-window-dot 'set-window-point)
415 (defalias 'read-input 'read-string)
416 (defalias 'send-string 'process-send-string)
417 (defalias 'send-region 'process-send-region)
418 (defalias 'show-buffer 'set-window-buffer)
419 (defalias 'buffer-flush-undo 'buffer-disable-undo)
420 (defalias 'eval-current-buffer 'eval-buffer)
421 (defalias 'compiled-function-p 'byte-code-function-p)
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 \f
430 ;;;; Alternate names for functions - these are not being phased out.
431
432 (defalias 'string= 'string-equal)
433 (defalias 'string< 'string-lessp)
434 (defalias 'move-marker 'set-marker)
435 (defalias 'eql 'eq)
436 (defalias 'not 'null)
437 (defalias 'rplaca 'setcar)
438 (defalias 'rplacd 'setcdr)
439 (defalias 'beep 'ding) ;preserve lingual purity
440 (defalias 'indent-to-column 'indent-to)
441 (defalias 'backward-delete-char 'delete-backward-char)
442 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
443 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
444 (defalias 'int-to-string 'number-to-string)
445
446 ;;; Should this be an obsolete name? If you decide it should, you get
447 ;;; to go through all the sources and change them.
448 (defalias 'string-to-int 'string-to-number)
449 \f
450 ;;;; Hook manipulation functions.
451
452 (defun run-hooks (&rest hooklist)
453 "Takes hook names and runs each one in turn. Major mode functions use this.
454 Each argument should be a symbol, a hook variable.
455 These symbols are processed in the order specified.
456 If a hook symbol has a non-nil value, that value may be a function
457 or a list of functions to be called to run the hook.
458 If the value is a function, it is called with no arguments.
459 If it is a list, the elements are called, in order, with no arguments."
460 (while hooklist
461 (let ((sym (car hooklist)))
462 (and (boundp sym)
463 (symbol-value sym)
464 (let ((value (symbol-value sym)))
465 (if (and (listp value) (not (eq (car value) 'lambda)))
466 (mapcar 'funcall value)
467 (funcall value)))))
468 (setq hooklist (cdr hooklist))))
469
470 ;; Tell C code how to call this function.
471 (defconst run-hooks 'run-hooks
472 "Variable by which C primitives find the function `run-hooks'.
473 Don't change it.")
474
475 (defun add-hook (hook function &optional append)
476 "Add to the value of HOOK the function FUNCTION.
477 FUNCTION is not added if already present.
478 FUNCTION is added (if necessary) at the beginning of the hook list
479 unless the optional argument APPEND is non-nil, in which case
480 FUNCTION is added at the end.
481
482 HOOK should be a symbol, and FUNCTION may be any valid function. If
483 HOOK is void, it is first set to nil. If HOOK's value is a single
484 function, it is changed to a list of functions."
485 (or (boundp hook) (set hook nil))
486 ;; If the hook value is a single function, turn it into a list.
487 (let ((old (symbol-value hook)))
488 (if (or (not (listp old)) (eq (car old) 'lambda))
489 (set hook (list old))))
490 (or (if (consp function)
491 (member function (symbol-value hook))
492 (memq function (symbol-value hook)))
493 (set hook
494 (if append
495 (nconc (symbol-value hook) (list function))
496 (cons function (symbol-value hook))))))
497
498 (defun remove-hook (hook function)
499 "Remove from the value of HOOK the function FUNCTION.
500 HOOK should be a symbol, and FUNCTION may be any valid function. If
501 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
502 list of hooks to run in HOOK, then nothing is done. See `add-hook'."
503 (if (or (not (boundp hook)) ;unbound symbol, or
504 (null (symbol-value hook)) ;value is nil, or
505 (null function)) ;function is nil, then
506 nil ;Do nothing.
507 (let ((hook-value (symbol-value hook)))
508 (if (consp hook-value)
509 (setq hook-value (delete function hook-value))
510 (if (equal hook-value function)
511 (setq hook-value nil)))
512 (set hook hook-value))))
513 \f
514 ;;;; Specifying things to do after certain files are loaded.
515
516 (defun eval-after-load (file form)
517 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
518 This makes or adds to an entry on `after-load-alist'.
519 It does nothing if FORM is already on the list for FILE.
520 FILE should be the name of a library, with no directory name."
521 (or (assoc file after-load-alist)
522 (setq after-load-alist (cons (list file) after-load-alist)))
523 (let ((elt (assoc file after-load-alist)))
524 (or (member form (cdr elt))
525 (nconc elt (list form))))
526 form)
527
528 (defun eval-next-after-load (file)
529 "Read the following input sexp, and run it whenever FILE is loaded.
530 This makes or adds to an entry on `after-load-alist'.
531 FILE should be the name of a library, with no directory name."
532 (eval-after-load file (read)))
533
534 \f
535 ;;;; Input and display facilities.
536
537 (defun read-quoted-char (&optional prompt)
538 "Like `read-char', except that if the first character read is an octal
539 digit, we read up to two more octal digits and return the character
540 represented by the octal number consisting of those digits.
541 Optional argument PROMPT specifies a string to use to prompt the user."
542 (let ((count 0) (code 0) char)
543 (while (< count 3)
544 (let ((inhibit-quit (zerop count))
545 (help-form nil))
546 (and prompt (message "%s-" prompt))
547 (setq char (read-char))
548 (if inhibit-quit (setq quit-flag nil)))
549 (cond ((null char))
550 ((and (<= ?0 char) (<= char ?7))
551 (setq code (+ (* code 8) (- char ?0))
552 count (1+ count))
553 (and prompt (message (setq prompt
554 (format "%s %c" prompt char)))))
555 ((> count 0)
556 (setq unread-command-events (list char) count 259))
557 (t (setq code char count 259))))
558 (logand 255 code)))
559
560 (defun force-mode-line-update (&optional all)
561 "Force the mode-line of the current buffer to be redisplayed.
562 With optional non-nil ALL then force then force redisplay of all mode-lines."
563 (if all (save-excursion (set-buffer (other-buffer))))
564 (set-buffer-modified-p (buffer-modified-p)))
565
566 (defun momentary-string-display (string pos &optional exit-char message)
567 "Momentarily display STRING in the buffer at POS.
568 Display remains until next character is typed.
569 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
570 otherwise it is then available as input (as a command if nothing else).
571 Display MESSAGE (optional fourth arg) in the echo area.
572 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
573 (or exit-char (setq exit-char ?\ ))
574 (let ((buffer-read-only nil)
575 (modified (buffer-modified-p))
576 (name buffer-file-name)
577 insert-end)
578 (unwind-protect
579 (progn
580 (save-excursion
581 (goto-char pos)
582 ;; defeat file locking... don't try this at home, kids!
583 (setq buffer-file-name nil)
584 (insert-before-markers string)
585 (setq insert-end (point))
586 ;; If the message end is off screen, recenter now.
587 (if (> (window-end) insert-end)
588 (recenter (/ (window-height) 2)))
589 ;; If that pushed message start off the screen,
590 ;; scroll to start it at the top of the screen.
591 (move-to-window-line 0)
592 (if (> (point) pos)
593 (progn
594 (goto-char pos)
595 (recenter 0))))
596 (message (or message "Type %s to continue editing.")
597 (single-key-description exit-char))
598 (let ((char (read-event)))
599 (or (eq char exit-char)
600 (setq unread-command-events (list char)))))
601 (if insert-end
602 (save-excursion
603 (delete-region pos insert-end)))
604 (setq buffer-file-name name)
605 (set-buffer-modified-p modified))))
606
607 \f
608 ;;;; Miscellanea.
609
610 (defun ignore (&rest ignore)
611 "Do nothing.
612 Accept any number of arguments, but ignore them."
613 nil)
614
615 (defun error (&rest args)
616 "Signal an error, making error message by passing all args to `format'."
617 (while t
618 (signal 'error (list (apply 'format args)))))
619
620 (defalias 'user-original-login-name 'user-login-name)
621
622 (defun start-process-shell-command (name buffer &rest args)
623 "Start a program in a subprocess. Return the process object for it.
624 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
625 NAME is name for process. It is modified if necessary to make it unique.
626 BUFFER is the buffer or (buffer-name) to associate with the process.
627 Process output goes at end of that buffer, unless you specify
628 an output stream or filter function to handle the output.
629 BUFFER may be also nil, meaning that this process is not associated
630 with any buffer
631 Third arg is command name, the name of a shell command.
632 Remaining arguments are the arguments for the command.
633 Wildcards and redirection are handled as usual in the shell."
634 (if (eq system-type 'vax-vms)
635 (apply 'start-process name buffer args)
636 (start-process name buffer shell-file-name "-c"
637 (concat "exec " (mapconcat 'identity args " ")))))
638
639 (defmacro save-match-data (&rest body)
640 "Execute the BODY forms, restoring the global value of the match data."
641 (let ((original (make-symbol "match-data")))
642 (list
643 'let (list (list original '(match-data)))
644 (list 'unwind-protect
645 (cons 'progn body)
646 (list 'store-match-data original)))))
647
648 (defun shell-quote-argument (argument)
649 "Quote an argument for passing as argument to an inferior shell."
650 ;; Quote everything except POSIX filename characters.
651 ;; This should be safe enough even for really weird shells.
652 (let ((result "") (start 0) end)
653 (while (string-match "[^---0-9a-zA-Z_./]" argument start)
654 (setq end (match-beginning 0)
655 result (concat result (substring argument start end)
656 "\\" (substring argument end (1+ end)))
657 start (1+ end)))
658 (concat result (substring argument start))))
659
660 (defun make-syntax-table (&optional oldtable)
661 "Return a new syntax table.
662 It inherits all letters and control characters from the standard
663 syntax table; other characters are copied from the standard syntax table."
664 (if oldtable
665 (copy-syntax-table oldtable)
666 (let ((table (copy-syntax-table))
667 i)
668 (setq i 0)
669 (while (<= i 31)
670 (aset table i 13)
671 (setq i (1+ i)))
672 (setq i ?A)
673 (while (<= i ?Z)
674 (aset table i 13)
675 (setq i (1+ i)))
676 (setq i ?a)
677 (while (<= i ?z)
678 (aset table i 13)
679 (setq i (1+ i)))
680 (setq i 128)
681 (while (<= i 255)
682 (aset table i 13)
683 (setq i (1+ i)))
684 table)))
685
686 ;; now in fns.c
687 ;(defun nth (n list)
688 ; "Returns the Nth element of LIST.
689 ;N counts from zero. If LIST is not that long, nil is returned."
690 ; (car (nthcdr n list)))
691 ;
692 ;(defun copy-alist (alist)
693 ; "Return a copy of ALIST.
694 ;This is a new alist which represents the same mapping
695 ;from objects to objects, but does not share the alist structure with ALIST.
696 ;The objects mapped (cars and cdrs of elements of the alist)
697 ;are shared, however."
698 ; (setq alist (copy-sequence alist))
699 ; (let ((tail alist))
700 ; (while tail
701 ; (if (consp (car tail))
702 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
703 ; (setq tail (cdr tail))))
704 ; alist)
705
706 ;;; subr.el ends here
707