entered into RCS
[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.
0cc89026
JB
39Optional third arg ALL-FRAMES t means include all windows in all frames;
40otherwise cycle within the selected frame."
be9b65ac
DL
41 (let* ((walk-windows-start (selected-window))
42 (walk-windows-current walk-windows-start))
43 (while (progn
44 (setq walk-windows-current
0cc89026 45 (next-window walk-windows-current minibuf all-frames))
be9b65ac
DL
46 (funcall proc walk-windows-current)
47 (not (eq walk-windows-current walk-windows-start))))))
48
49(defun read-quoted-char (&optional prompt)
50 "Like `read-char', except that if the first character read is an octal
51digit, we read up to two more octal digits and return the character
52represented by the octal number consisting of those digits.
53Optional argument PROMPT specifies a string to use to prompt the user."
54 (let ((count 0) (code 0) char)
55 (while (< count 3)
56 (let ((inhibit-quit (zerop count))
57 (help-form nil))
58 (and prompt (message "%s-" prompt))
59 (setq char (read-char))
60 (if inhibit-quit (setq quit-flag nil)))
61 (cond ((null char))
62 ((and (<= ?0 char) (<= char ?7))
63 (setq code (+ (* code 8) (- char ?0))
64 count (1+ count))
65 (and prompt (message (setq prompt
66 (format "%s %c" prompt char)))))
67 ((> count 0)
68 (setq unread-command-char char count 259))
69 (t (setq code char count 259))))
70 (logand 255 code)))
71
72(defun error (&rest args)
73 "Signal an error, making error message by passing all args to `format'."
74 (while t
75 (signal 'error (list (apply 'format args)))))
76
77(defun undefined ()
78 (interactive)
79 (ding))
80
81;Prevent the \{...} documentation construct
82;from mentioning keys that run this command.
83(put 'undefined 'suppress-keymap t)
84
85(defun suppress-keymap (map &optional nodigits)
86 "Make MAP override all normally self-inserting keys to be undefined.
87Normally, as an exception, digits and minus-sign are set to make prefix args,
88but optional second arg NODIGITS non-nil treats them like other chars."
89 (let ((i 0))
90 (while (<= i 127)
91 (if (eql (lookup-key global-map (char-to-string i)) 'self-insert-command)
92 (define-key map (char-to-string i) 'undefined))
93 (setq i (1+ i))))
94 (or nodigits
95 (let (loop)
96 (define-key map "-" 'negative-argument)
97 ;; Make plain numbers do numeric args.
98 (setq loop ?0)
99 (while (<= loop ?9)
100 (define-key map (char-to-string loop) 'digit-argument)
101 (setq loop (1+ loop))))))
102
103;; now in fns.c
104;(defun nth (n list)
105; "Returns the Nth element of LIST.
106;N counts from zero. If LIST is not that long, nil is returned."
107; (car (nthcdr n list)))
108;
109;(defun copy-alist (alist)
110; "Return a copy of ALIST.
111;This is a new alist which represents the same mapping
112;from objects to objects, but does not share the alist structure with ALIST.
113;The objects mapped (cars and cdrs of elements of the alist)
114;are shared, however."
115; (setq alist (copy-sequence alist))
116; (let ((tail alist))
117; (while tail
118; (if (consp (car tail))
119; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
120; (setq tail (cdr tail))))
121; alist)
122
123;Moved to keymap.c
124;(defun copy-keymap (keymap)
125; "Return a copy of KEYMAP"
126; (while (not (keymapp keymap))
127; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
128; (if (vectorp keymap)
129; (copy-sequence keymap)
130; (copy-alist keymap)))
131
132(defun substitute-key-definition (olddef newdef keymap)
133 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
134In other words, OLDDEF is replaced with NEWDEF where ever it appears.
135Prefix keymaps reached from KEYMAP are not checked recursively;
136perhaps they ought to be."
137 (if (arrayp keymap)
138 (let ((len (length keymap))
139 (i 0))
140 (while (< i len)
141 (if (eq (aref keymap i) olddef)
142 (aset keymap i newdef))
143 (setq i (1+ i))))
144 (while keymap
145 (if (eq (cdr-safe (car-safe keymap)) olddef)
146 (setcdr (car keymap) newdef))
147 (setq keymap (cdr keymap)))))
148
149;; Avoids useless byte-compilation.
150;; In the future, would be better to fix byte compiler
151;; not to really compile in cases like this,
152;; and use defun here.
dbc3787c
JB
153(fset 'ignore '(lambda (&rest ignore)
154 "Do nothing.
155Accept any number of arguments, but ignore them."
156 nil))
be9b65ac
DL
157
158\f
159; old names
160(fset 'make-syntax-table 'copy-syntax-table)
161(fset 'dot 'point)
162(fset 'dot-marker 'point-marker)
163(fset 'dot-min 'point-min)
164(fset 'dot-max 'point-max)
165(fset 'window-dot 'window-point)
166(fset 'set-window-dot 'set-window-point)
167(fset 'read-input 'read-string)
168(fset 'send-string 'process-send-string)
169(fset 'send-region 'process-send-region)
170(fset 'show-buffer 'set-window-buffer)
171(fset 'buffer-flush-undo 'buffer-disable-undo)
94b304d7 172(fset 'eval-current-buffer 'eval-buffer)
be9b65ac
DL
173
174; alternate names
175(fset 'string= 'string-equal)
176(fset 'string< 'string-lessp)
177(fset 'move-marker 'set-marker)
178(fset 'eql 'eq)
179(fset 'not 'null)
180(fset 'numberp 'integerp)
181(fset 'rplaca 'setcar)
182(fset 'rplacd 'setcdr)
183(fset 'beep 'ding) ;preserve lingual purtity
184(fset 'indent-to-column 'indent-to)
185(fset 'backward-delete-char 'delete-backward-char)
6f236da8
JB
186(fset 'search-forward-regexp (symbol-function 're-search-forward))
187(fset 'search-backward-regexp (symbol-function 're-search-backward))
be9b65ac 188\f
d95b111b
JB
189;;; global-map, esc-map, and ctl-x-map have their values set up
190;;; in keymap.c.
be9b65ac
DL
191(defvar global-map nil
192 "Default global keymap mapping Emacs keyboard input into commands.
193The value is a keymap which is usually (but not necessarily) Emacs's
194global map.")
195
d95b111b
JB
196(defvar esc-map nil
197 "Default keymap for ESC (meta) commands.
198The normal global definition of the character ESC indirects to this keymap.")
199
be9b65ac
DL
200(defvar ctl-x-map nil
201 "Default keymap for C-x commands.
202The normal global definition of the character C-x indirects to this keymap.")
203
d95b111b
JB
204(defvar ctl-x-4-map (make-sparse-keymap)
205 "Keymap for subcommands of C-x 4")
206(fset 'ctl-x-4-prefix ctl-x-4-map)
207(define-key ctl-x-map "4" 'ctl-x-4-prefix)
208
492878e4 209(defvar ctl-x-5-map (make-sparse-keymap)
0cc89026 210 "Keymap for frame commands.")
492878e4
JB
211(fset 'ctl-x-5-prefix ctl-x-5-map)
212(define-key ctl-x-map "5" 'ctl-x-5-prefix)
be9b65ac 213
be9b65ac
DL
214\f
215(defun run-hooks (&rest hooklist)
216 "Takes hook names and runs each one in turn. Major mode functions use this.
217Each argument should be a symbol, a hook variable.
218These symbols are processed in the order specified.
219If a hook symbol has a non-nil value, that value may be a function
220or a list of functions to be called to run the hook.
221If the value is a function, it is called with no arguments.
222If it is a list, the elements are called, in order, with no arguments."
223 (while hooklist
224 (let ((sym (car hooklist)))
225 (and (boundp sym)
226 (symbol-value sym)
227 (let ((value (symbol-value sym)))
228 (if (and (listp value) (not (eq (car value) 'lambda)))
229 (mapcar 'funcall value)
230 (funcall value)))))
231 (setq hooklist (cdr hooklist))))
232
233;; Tell C code how to call this function.
234(defconst run-hooks 'run-hooks
235 "Variable by which C primitives find the function `run-hooks'.
236Don't change it.")
237
238(defun add-hook (hook function)
239 "Add to the value of HOOK the function FUNCTION unless already present.
240HOOK should be a symbol, and FUNCTION may be any valid function.
241HOOK's value should be a list of functions, not a single function.
242If HOOK is void, it is first set to nil."
243 (or (boundp hook) (set hook nil))
244 (or (if (consp function)
245 ;; Clever way to tell whether a given lambda-expression
246 ;; is equal to anything in the hook.
247 (let ((tail (assoc (cdr function) (symbol-value hook))))
248 (equal function tail))
249 (memq function (symbol-value hook)))
f35fe3c6 250 (set hook (cons function (symbol-value hook)))))
be9b65ac
DL
251\f
252(defun momentary-string-display (string pos &optional exit-char message)
253 "Momentarily display STRING in the buffer at POS.
254Display remains until next character is typed.
255If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
256otherwise it is then available as input (as a command if nothing else).
257Display MESSAGE (optional fourth arg) in the echo area.
258If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
259 (or exit-char (setq exit-char ?\ ))
260 (let ((buffer-read-only nil)
261 (modified (buffer-modified-p))
262 (name buffer-file-name)
263 insert-end)
264 (unwind-protect
265 (progn
266 (save-excursion
267 (goto-char pos)
268 ;; defeat file locking... don't try this at home, kids!
269 (setq buffer-file-name nil)
270 (insert-before-markers string)
271 (setq insert-end (point)))
272 (message (or message "Type %s to continue editing.")
273 (single-key-description exit-char))
274 (let ((char (read-char)))
275 (or (eq char exit-char)
276 (setq unread-command-char char))))
277 (if insert-end
278 (save-excursion
279 (delete-region pos insert-end)))
280 (setq buffer-file-name name)
281 (set-buffer-modified-p modified))))
282
283(defun start-process-shell-command (name buffer &rest args)
284 "Start a program in a subprocess. Return the process object for it.
285Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
286NAME is name for process. It is modified if necessary to make it unique.
287BUFFER is the buffer or (buffer-name) to associate with the process.
288 Process output goes at end of that buffer, unless you specify
289 an output stream or filter function to handle the output.
290 BUFFER may be also nil, meaning that this process is not associated
291 with any buffer
292Third arg is command name, the name of a shell command.
293Remaining arguments are the arguments for the command.
294Wildcards and redirection are handle as usual in the shell."
295 (if (eq system-type 'vax-vms)
296 (apply 'start-process name buffer args)
297 (start-process name buffer shell-file-name "-c"
298 (concat "exec " (mapconcat 'identity args " ")))))
299\f
300(defun eval-after-load (file form)
301 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
302This makes or adds to an entry on `after-load-alist'.
303FILE should be the name of a library, with no directory name."
304 (or (assoc file after-load-alist)
305 (setq after-load-alist (cons (list file) after-load-alist)))
306 (nconc (assoc file after-load-alist) (list form))
307 form)
308
309(defun eval-next-after-load (file)
310 "Read the following input sexp, and run it whenever FILE is loaded.
311This makes or adds to an entry on `after-load-alist'.
312FILE should be the name of a library, with no directory name."
313 (eval-after-load file (read)))
408a4c8f
RS
314
315(defmacro defun-inline (name args &rest body)
316 "Create an \"inline defun\" (actually a macro).
317Use just like `defun'."
318 (nconc (list 'defmacro name '(&rest args))
319 (if (stringp (car body))
320 (prog1 (list (car body))
321 (setq body (or (cdr body) body))))
322 (list (list 'cons (list 'quote
323 (cons 'lambda (cons args body)))
324 'args))))
be9b65ac
DL
325\f
326(defun user-original-login-name ()
327 "Return user's login name from original login.
328This tries to remain unaffected by `su', by looking in environment variables."
329 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
330\f
331(defun force-mode-line-update (&optional all)
332 "Force the mode-line of the current buffer to be redisplayed.
333With optional non-nil ALL then force then force redisplay of all mode-lines."
334 (if all (save-excursion (set-buffer (other-buffer))))
335 (set-buffer-modified-p (buffer-modified-p)))
336
337(defun keyboard-translate (from to)
338 "Translate character FROM to TO at a low level.
339This function creates a `keyboard-translate-table' if necessary
340and then modifies one entry in it."
341 (or (boundp 'keyboard-translate-table)
342 (let ((table (make-string 256))
343 (i 0))
344 (while (< i 256)
345 (aset table i i)
346 (setq i (1+ i)))
347 (setq keyboard-translate-table table)))
348 (aset keyboard-translate-table from to))
ffd56f97
JB
349
350\f
351(defmacro lambda (&rest cdr)
75d0ce1b
NF
352 "Macro which allows one to write (lambda ...) for anonymous functions
353instead of having to write (function (lambda ...)) or '(lambda ...), the
354latter of which won't get byte-compiled."
ffd56f97 355 (` (function (lambda (,@ cdr)))))
630cc463
ER
356
357;;; subr.el ends here