Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
[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
492878e4 23(defun one-window-p (&optional nomini)
be9b65ac
DL
24 "Returns non-nil if there is only one window.
25Optional arg NOMINI non-nil means don't count the minibuffer
26even if it is active."
492878e4
JB
27 (let ((base-window (selected-window)))
28 (if (and nomini (eq base-window (minibuffer-window)))
29 (setq base-window (next-window base-window)))
30 (eq base-window
31 (next-window base-window (if nomini 'arg)))))
be9b65ac 32
0cc89026 33(defun walk-windows (proc &optional minibuf all-frames)
be9b65ac
DL
34 "Cycle through all visible windows, calling PROC for each one.
35PROC is called with a window as argument.
36Optional second arg MINIBUF t means count the minibuffer window
37even if not active. If MINIBUF is neither t nor nil it means
38not to count the minibuffer even if it is active.
f1f2d09a
RS
39
40Optional third arg ALL-FRAMES, if t, means include all frames.
41ALL-FRAMES nil or omitted means cycle within the selected frame,
42but include the minibuffer window (if MINIBUF says so) that that
43frame uses, even if it is on another frame.
44If ALL-FRAMES is neither nil nor t, stick strictly to the selected frame."
be9b65ac
DL
45 (let* ((walk-windows-start (selected-window))
46 (walk-windows-current walk-windows-start))
47 (while (progn
48 (setq walk-windows-current
0cc89026 49 (next-window walk-windows-current minibuf all-frames))
be9b65ac
DL
50 (funcall proc walk-windows-current)
51 (not (eq walk-windows-current walk-windows-start))))))
52
53(defun read-quoted-char (&optional prompt)
54 "Like `read-char', except that if the first character read is an octal
55digit, we read up to two more octal digits and return the character
56represented by the octal number consisting of those digits.
57Optional argument PROMPT specifies a string to use to prompt the user."
58 (let ((count 0) (code 0) char)
59 (while (< count 3)
60 (let ((inhibit-quit (zerop count))
61 (help-form nil))
62 (and prompt (message "%s-" prompt))
63 (setq char (read-char))
64 (if inhibit-quit (setq quit-flag nil)))
65 (cond ((null char))
66 ((and (<= ?0 char) (<= char ?7))
67 (setq code (+ (* code 8) (- char ?0))
68 count (1+ count))
69 (and prompt (message (setq prompt
70 (format "%s %c" prompt char)))))
71 ((> count 0)
dbc4e1c1 72 (setq unread-command-events (list char) count 259))
be9b65ac
DL
73 (t (setq code char count 259))))
74 (logand 255 code)))
75
76(defun error (&rest args)
77 "Signal an error, making error message by passing all args to `format'."
78 (while t
79 (signal 'error (list (apply 'format args)))))
80
81(defun undefined ()
82 (interactive)
83 (ding))
84
8b824916
RS
85;; Some programs still use this as a function.
86(defun baud-rate ()
87 "Obsolete function returning the value of the `baud-rate' variable."
88 baud-rate)
89
be9b65ac
DL
90;Prevent the \{...} documentation construct
91;from mentioning keys that run this command.
92(put 'undefined 'suppress-keymap t)
93
94(defun suppress-keymap (map &optional nodigits)
95 "Make MAP override all normally self-inserting keys to be undefined.
96Normally, as an exception, digits and minus-sign are set to make prefix args,
97but optional second arg NODIGITS non-nil treats them like other chars."
98 (let ((i 0))
99 (while (<= i 127)
100 (if (eql (lookup-key global-map (char-to-string i)) 'self-insert-command)
101 (define-key map (char-to-string i) 'undefined))
102 (setq i (1+ i))))
103 (or nodigits
104 (let (loop)
105 (define-key map "-" 'negative-argument)
106 ;; Make plain numbers do numeric args.
107 (setq loop ?0)
108 (while (<= loop ?9)
109 (define-key map (char-to-string loop) 'digit-argument)
110 (setq loop (1+ loop))))))
111
112;; now in fns.c
113;(defun nth (n list)
114; "Returns the Nth element of LIST.
115;N counts from zero. If LIST is not that long, nil is returned."
116; (car (nthcdr n list)))
117;
118;(defun copy-alist (alist)
119; "Return a copy of ALIST.
120;This is a new alist which represents the same mapping
121;from objects to objects, but does not share the alist structure with ALIST.
122;The objects mapped (cars and cdrs of elements of the alist)
123;are shared, however."
124; (setq alist (copy-sequence alist))
125; (let ((tail alist))
126; (while tail
127; (if (consp (car tail))
128; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
129; (setq tail (cdr tail))))
130; alist)
131
132;Moved to keymap.c
133;(defun copy-keymap (keymap)
134; "Return a copy of KEYMAP"
135; (while (not (keymapp keymap))
136; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
137; (if (vectorp keymap)
138; (copy-sequence keymap)
139; (copy-alist keymap)))
140
7f2c2edd 141(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
142 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
143In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
144If optional fourth argument OLDMAP is specified, we redefine
145in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
146 (or prefix (setq prefix ""))
147 (let* ((scan (or oldmap keymap))
148 (vec1 (vector nil))
149 (prefix1 (vconcat prefix vec1)))
150 ;; Scan OLDMAP, finding each char or event-symbol that
151 ;; has any definition, and act on it with hack-key.
152 (while (consp scan)
153 (if (consp (car scan))
154 (let ((char (car (car scan)))
155 (defn (cdr (car scan))))
156 ;; The inside of this let duplicates exactly
157 ;; the inside of the following let that handles array elements.
158 (aset vec1 0 char)
159 (aset prefix1 (length prefix) char)
160 (let (inner-def)
161 ;; Skip past menu-prompt.
162 (while (stringp (car-safe defn))
163 (setq defn (cdr defn)))
164 (setq inner-def defn)
165 (while (and (symbolp inner-def)
166 (fboundp inner-def))
167 (setq inner-def (symbol-function inner-def)))
168 (if (eq defn olddef)
169 (define-key keymap prefix1 newdef)
170 (if (keymapp defn)
171 (substitute-key-definition olddef newdef keymap
172 inner-def
173 prefix1)))))
174 (if (arrayp (car scan))
175 (let* ((array (car scan))
176 (len (length array))
177 (i 0))
178 (while (< i len)
179 (let ((char i) (defn (aref array i)))
180 ;; The inside of this let duplicates exactly
181 ;; the inside of the previous let.
182 (aset vec1 0 char)
183 (aset prefix1 (length prefix) char)
184 (let (inner-def)
185 ;; Skip past menu-prompt.
186 (while (stringp (car-safe defn))
187 (setq defn (cdr defn)))
188 (setq inner-def defn)
189 (while (and (symbolp inner-def)
190 (fboundp inner-def))
191 (setq inner-def (symbol-function inner-def)))
192 (if (eq defn olddef)
193 (define-key keymap prefix1 newdef)
194 (if (keymapp defn)
195 (substitute-key-definition olddef newdef keymap
196 inner-def
197 prefix1)))))
198 (setq i (1+ i))))))
199 (setq scan (cdr scan)))))
0f03054a 200\f
cde6d7e3
RS
201(defun listify-key-sequence (key)
202 "Convert a key sequence to a list of events."
203 (if (vectorp key)
204 (append key nil)
205 (mapcar (function (lambda (c)
206 (if (> c 127)
207 (logxor c 8388736)
208 c)))
209 (append key nil))))
210
53e5a4e8
RS
211(defsubst eventp (obj)
212 "True if the argument is an event object."
213 (or (integerp obj)
214 (and (symbolp obj)
215 (get obj 'event-symbol-elements))
216 (and (consp obj)
217 (symbolp (car obj))
218 (get (car obj) 'event-symbol-elements))))
219
220(defun event-modifiers (event)
221 "Returns a list of symbols representing the modifier keys in event EVENT.
222The elements of the list may include `meta', `control',
223`shift', `hyper', `super', `alt'.
224See also the function `event-modifier-bits'."
225 (let ((type event))
226 (if (listp type)
227 (setq type (car type)))
228 (if (symbolp type)
229 (cdr (get type 'event-symbol-elements))
230 (let ((list nil))
231 (or (zerop (logand type (lsh 1 23)))
232 (setq list (cons 'meta list)))
233 (or (and (zerop (logand type (lsh 1 22)))
234 (>= (logand type 127) 32))
235 (setq list (cons 'control list)))
236 (or (and (zerop (logand type (lsh 1 21)))
237 (= (logand type 255) (downcase (logand type 255))))
238 (setq list (cons 'shift list)))
239 (or (zerop (logand type (lsh 1 20)))
240 (setq list (cons 'hyper list)))
241 (or (zerop (logand type (lsh 1 19)))
242 (setq list (cons 'super list)))
243 (or (zerop (logand type (lsh 1 18)))
244 (setq list (cons 'alt list)))
245 list))))
246
d63de416
RS
247(defun event-basic-type (event)
248 "Returns the basic type of the given event (all modifiers removed).
249The value is an ASCII printing character (not upper case) or a symbol."
250 (if (symbolp event)
251 (car (get event 'event-symbol-elements))
252 (let ((base (logand event (1- (lsh 1 18)))))
253 (downcase (if (< base 32) (logior base 64) base)))))
254
0f03054a
RS
255(defsubst mouse-movement-p (object)
256 "Return non-nil if OBJECT is a mouse movement event."
257 (and (consp object)
258 (eq (car object) 'mouse-movement)))
259
260(defsubst event-start (event)
261 "Return the starting position of EVENT.
262If EVENT is a mouse press or a mouse click, this returns the location
263of the event.
264If EVENT is a drag, this returns the drag's starting position.
265The return value is of the form
266 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
267The `posn-' functions access elements of such lists."
268 (nth 1 event))
269
270(defsubst event-end (event)
271 "Return the ending location of EVENT. EVENT should be a click or drag event.
272If EVENT is a click event, this function is the same as `event-start'.
273The return value is of the form
274 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
275The `posn-' functions access elements of such lists."
276 (nth (1- (length event)) event))
277
278(defsubst posn-window (position)
279 "Return the window in POSITION.
280POSITION should be a list of the form
281 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
282as returned by the `event-start' and `event-end' functions."
283 (nth 0 position))
284
285(defsubst posn-point (position)
286 "Return the buffer location in POSITION.
287POSITION should be a list of the form
288 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
289as returned by the `event-start' and `event-end' functions."
290 (nth 1 position))
291
292(defsubst posn-col-row (position)
293 "Return the row and column in POSITION.
294POSITION should be a list of the form
295 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
296as returned by the `event-start' and `event-end' functions."
297 (nth 2 position))
298
299(defsubst posn-timestamp (position)
300 "Return the timestamp of POSITION.
301POSITION should be a list of the form
302 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
303nas returned by the `event-start' and `event-end' functions."
304 (nth 3 position))
305\f
715984d3
RS
306(defmacro save-match-data (&rest body)
307 "Execute the BODY forms, restoring the global value of the match data."
308 (let ((original (make-symbol "match-data")))
309 (list
310 'let (list (list original '(match-data)))
311 (list 'unwind-protect
312 (cons 'progn body)
313 (list 'store-match-data original)))))
314
a42a4305
RM
315(defun ignore (&rest ignore)
316 "Do nothing.
dbc3787c 317Accept any number of arguments, but ignore them."
a42a4305 318 nil)
be9b65ac
DL
319\f
320; old names
321(fset 'make-syntax-table 'copy-syntax-table)
322(fset 'dot 'point)
323(fset 'dot-marker 'point-marker)
324(fset 'dot-min 'point-min)
325(fset 'dot-max 'point-max)
326(fset 'window-dot 'window-point)
327(fset 'set-window-dot 'set-window-point)
328(fset 'read-input 'read-string)
329(fset 'send-string 'process-send-string)
330(fset 'send-region 'process-send-region)
331(fset 'show-buffer 'set-window-buffer)
332(fset 'buffer-flush-undo 'buffer-disable-undo)
94b304d7 333(fset 'eval-current-buffer 'eval-buffer)
dbc4e1c1 334(fset 'compiled-function-p 'byte-code-function-p)
be9b65ac 335
d2ec8956
JB
336;;; This name isn't mentioned in the manual, and we've been hoping to
337;;; phase it out, but there's still a lot of code out there, even for
338;;; Emacs 18.59, which uses mod. I'm going to let the byte compiler's
339;;; make-obsolete function to poke people a little more, and leave the
340;;; `mod' name around for a while longer.
341(fset 'mod '%)
342
be9b65ac
DL
343; alternate names
344(fset 'string= 'string-equal)
345(fset 'string< 'string-lessp)
346(fset 'move-marker 'set-marker)
347(fset 'eql 'eq)
348(fset 'not 'null)
be9b65ac
DL
349(fset 'rplaca 'setcar)
350(fset 'rplacd 'setcdr)
351(fset 'beep 'ding) ;preserve lingual purtity
352(fset 'indent-to-column 'indent-to)
353(fset 'backward-delete-char 'delete-backward-char)
6f236da8
JB
354(fset 'search-forward-regexp (symbol-function 're-search-forward))
355(fset 'search-backward-regexp (symbol-function 're-search-backward))
555b2421 356(fset 'int-to-string 'number-to-string)
37f6661a
JB
357
358;;; Should this be an obsolete name? If you decide it should, you get
359;;; to go through all the sources and change them.
360(fset 'string-to-int 'string-to-number)
be9b65ac 361\f
d95b111b
JB
362;;; global-map, esc-map, and ctl-x-map have their values set up
363;;; in keymap.c.
be9b65ac
DL
364(defvar global-map nil
365 "Default global keymap mapping Emacs keyboard input into commands.
366The value is a keymap which is usually (but not necessarily) Emacs's
367global map.")
368
d95b111b
JB
369(defvar esc-map nil
370 "Default keymap for ESC (meta) commands.
371The normal global definition of the character ESC indirects to this keymap.")
372
be9b65ac
DL
373(defvar ctl-x-map nil
374 "Default keymap for C-x commands.
375The normal global definition of the character C-x indirects to this keymap.")
376
d95b111b
JB
377(defvar ctl-x-4-map (make-sparse-keymap)
378 "Keymap for subcommands of C-x 4")
379(fset 'ctl-x-4-prefix ctl-x-4-map)
380(define-key ctl-x-map "4" 'ctl-x-4-prefix)
381
492878e4 382(defvar ctl-x-5-map (make-sparse-keymap)
0cc89026 383 "Keymap for frame commands.")
492878e4
JB
384(fset 'ctl-x-5-prefix ctl-x-5-map)
385(define-key ctl-x-map "5" 'ctl-x-5-prefix)
be9b65ac 386
be9b65ac
DL
387\f
388(defun run-hooks (&rest hooklist)
389 "Takes hook names and runs each one in turn. Major mode functions use this.
390Each argument should be a symbol, a hook variable.
391These symbols are processed in the order specified.
392If a hook symbol has a non-nil value, that value may be a function
393or a list of functions to be called to run the hook.
394If the value is a function, it is called with no arguments.
395If it is a list, the elements are called, in order, with no arguments."
396 (while hooklist
397 (let ((sym (car hooklist)))
398 (and (boundp sym)
399 (symbol-value sym)
400 (let ((value (symbol-value sym)))
401 (if (and (listp value) (not (eq (car value) 'lambda)))
402 (mapcar 'funcall value)
403 (funcall value)))))
404 (setq hooklist (cdr hooklist))))
405
406;; Tell C code how to call this function.
407(defconst run-hooks 'run-hooks
408 "Variable by which C primitives find the function `run-hooks'.
409Don't change it.")
410
08159178
ER
411(defun add-hook (hook function &optional append)
412 "Add to the value of HOOK the function FUNCTION unless already present (it
413becomes the first hook on the list unless optional APPEND is non-nil, in
414which case it becomes the last). HOOK should be a symbol, and FUNCTION may be
415any valid function. HOOK's value should be a list of functions, not a single
416function. If HOOK is void, it is first set to nil."
be9b65ac
DL
417 (or (boundp hook) (set hook nil))
418 (or (if (consp function)
419 ;; Clever way to tell whether a given lambda-expression
420 ;; is equal to anything in the hook.
421 (let ((tail (assoc (cdr function) (symbol-value hook))))
422 (equal function tail))
423 (memq function (symbol-value hook)))
08159178
ER
424 (set hook
425 (if append
426 (nconc (symbol-value hook) (list function))
427 (cons function (symbol-value hook))))))
be9b65ac
DL
428\f
429(defun momentary-string-display (string pos &optional exit-char message)
430 "Momentarily display STRING in the buffer at POS.
431Display remains until next character is typed.
432If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
433otherwise it is then available as input (as a command if nothing else).
434Display MESSAGE (optional fourth arg) in the echo area.
435If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
436 (or exit-char (setq exit-char ?\ ))
437 (let ((buffer-read-only nil)
438 (modified (buffer-modified-p))
439 (name buffer-file-name)
440 insert-end)
441 (unwind-protect
442 (progn
443 (save-excursion
444 (goto-char pos)
445 ;; defeat file locking... don't try this at home, kids!
446 (setq buffer-file-name nil)
447 (insert-before-markers string)
448 (setq insert-end (point)))
449 (message (or message "Type %s to continue editing.")
450 (single-key-description exit-char))
3547c855 451 (let ((char (read-event)))
be9b65ac 452 (or (eq char exit-char)
dbc4e1c1 453 (setq unread-command-events (list char)))))
be9b65ac
DL
454 (if insert-end
455 (save-excursion
456 (delete-region pos insert-end)))
457 (setq buffer-file-name name)
458 (set-buffer-modified-p modified))))
459
460(defun start-process-shell-command (name buffer &rest args)
461 "Start a program in a subprocess. Return the process object for it.
462Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
463NAME is name for process. It is modified if necessary to make it unique.
464BUFFER is the buffer or (buffer-name) to associate with the process.
465 Process output goes at end of that buffer, unless you specify
466 an output stream or filter function to handle the output.
467 BUFFER may be also nil, meaning that this process is not associated
468 with any buffer
469Third arg is command name, the name of a shell command.
470Remaining arguments are the arguments for the command.
471Wildcards and redirection are handle as usual in the shell."
472 (if (eq system-type 'vax-vms)
473 (apply 'start-process name buffer args)
474 (start-process name buffer shell-file-name "-c"
475 (concat "exec " (mapconcat 'identity args " ")))))
476\f
477(defun eval-after-load (file form)
478 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
479This makes or adds to an entry on `after-load-alist'.
480FILE should be the name of a library, with no directory name."
481 (or (assoc file after-load-alist)
482 (setq after-load-alist (cons (list file) after-load-alist)))
483 (nconc (assoc file after-load-alist) (list form))
484 form)
485
486(defun eval-next-after-load (file)
487 "Read the following input sexp, and run it whenever FILE is loaded.
488This makes or adds to an entry on `after-load-alist'.
489FILE should be the name of a library, with no directory name."
490 (eval-after-load file (read)))
408a4c8f 491
e6dfdce5
RS
492;;(defmacro defun-inline (name args &rest body)
493;; "Create an \"inline defun\" (actually a macro).
494;;Use just like `defun'."
495;; (nconc (list 'defmacro name '(&rest args))
496;; (if (stringp (car body))
497;; (prog1 (list (car body))
498;; (setq body (or (cdr body) body))))
499;; (list (list 'cons (list 'quote
500;; (cons 'lambda (cons args body)))
501;; 'args))))
be9b65ac
DL
502\f
503(defun user-original-login-name ()
504 "Return user's login name from original login.
505This tries to remain unaffected by `su', by looking in environment variables."
506 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
507\f
508(defun force-mode-line-update (&optional all)
509 "Force the mode-line of the current buffer to be redisplayed.
510With optional non-nil ALL then force then force redisplay of all mode-lines."
511 (if all (save-excursion (set-buffer (other-buffer))))
512 (set-buffer-modified-p (buffer-modified-p)))
513
514(defun keyboard-translate (from to)
515 "Translate character FROM to TO at a low level.
516This function creates a `keyboard-translate-table' if necessary
517and then modifies one entry in it."
b7cceaf1
JB
518 (or (arrayp keyboard-translate-table)
519 (setq keyboard-translate-table ""))
520 (if (or (> from (length keyboard-translate-table))
521 (> to (length keyboard-translate-table)))
522 (progn
523 (let* ((i (length keyboard-translate-table))
524 (table (make-string (- 256 i) 0)))
525 (while (< i 256)
526 (aset table i i)
527 (setq i (1+ i)))
528 (setq keyboard-translate-table table))))
be9b65ac 529 (aset keyboard-translate-table from to))
ffd56f97
JB
530
531\f
532(defmacro lambda (&rest cdr)
397587ee
JB
533 "Return a lambda expression.
534A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
535self-quoting; the result of evaluating the lambda expression is the
536expression itself. The lambda expression may then be treated as a
537function, i. e. stored as the function value of a symbol, passed to
538funcall or mapcar, etcetera.
539ARGS should take the same form as an argument list for a `defun'.
540DOCSTRING should be a string, as described for `defun'. It may be omitted.
541INTERACTIVE should be a call to the function `interactive', which see.
542It may also be omitted.
543BODY should be a list of lisp expressions."
d77c36e8
JB
544 ;; Note that this definition should not use backquotes; subr.el should not
545 ;; depend on backquote.el.
546 (list 'function (cons 'lambda cdr)))
630cc463
ER
547
548;;; subr.el ends here