*** empty log message ***
[emacs.git] / lisp / subr.el
blobaee62d11f94c6a17a6555f52dc1ef273a5c140cc
1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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.
21 ;;; Code:
23 (defun one-window-p (&optional nomini)
24 "Returns non-nil if there is only one window.
25 Optional arg NOMINI non-nil means don't count the minibuffer
26 even if it is active."
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)))))
33 (defun walk-windows (proc &optional minibuf all-frames)
34 "Cycle through all visible windows, calling PROC for each one.
35 PROC is called with a window as argument.
36 Optional second arg MINIBUF t means count the minibuffer window
37 even if not active. If MINIBUF is neither t nor nil it means
38 not to count the minibuffer even if it is active.
39 Optional third arg ALL-FRAMES t means include all windows in all frames;
40 otherwise cycle within the selected frame."
41 (let* ((walk-windows-start (selected-window))
42 (walk-windows-current walk-windows-start))
43 (while (progn
44 (setq walk-windows-current
45 (next-window walk-windows-current minibuf all-frames))
46 (funcall proc walk-windows-current)
47 (not (eq walk-windows-current walk-windows-start))))))
49 (defun read-quoted-char (&optional prompt)
50 "Like `read-char', except that if the first character read is an octal
51 digit, we read up to two more octal digits and return the character
52 represented by the octal number consisting of those digits.
53 Optional 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)))
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)))))
77 (defun undefined ()
78 (interactive)
79 (ding))
81 ;Prevent the \{...} documentation construct
82 ;from mentioning keys that run this command.
83 (put 'undefined 'suppress-keymap t)
85 (defun suppress-keymap (map &optional nodigits)
86 "Make MAP override all normally self-inserting keys to be undefined.
87 Normally, as an exception, digits and minus-sign are set to make prefix args,
88 but 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))))))
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)))
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)
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)))
132 (defun substitute-key-definition (olddef newdef keymap)
133 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
134 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
135 Prefix keymaps reached from KEYMAP are not checked recursively;
136 perhaps 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)))))
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.
153 (fset 'ignore '(lambda (&rest ignore)
154 "Do nothing.
155 Accept any number of arguments, but ignore them."
156 nil))
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)
172 (fset 'eval-current-buffer 'eval-buffer)
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)
186 (fset 'search-forward-regexp (symbol-function 're-search-forward))
187 (fset 'search-backward-regexp (symbol-function 're-search-backward))
189 ;;; global-map, esc-map, and ctl-x-map have their values set up
190 ;;; in keymap.c.
191 (defvar global-map nil
192 "Default global keymap mapping Emacs keyboard input into commands.
193 The value is a keymap which is usually (but not necessarily) Emacs's
194 global map.")
196 (defvar esc-map nil
197 "Default keymap for ESC (meta) commands.
198 The normal global definition of the character ESC indirects to this keymap.")
200 (defvar ctl-x-map nil
201 "Default keymap for C-x commands.
202 The normal global definition of the character C-x indirects to this keymap.")
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)
209 (defvar ctl-x-5-map (make-sparse-keymap)
210 "Keymap for frame commands.")
211 (fset 'ctl-x-5-prefix ctl-x-5-map)
212 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
215 (defun run-hooks (&rest hooklist)
216 "Takes hook names and runs each one in turn. Major mode functions use this.
217 Each argument should be a symbol, a hook variable.
218 These symbols are processed in the order specified.
219 If a hook symbol has a non-nil value, that value may be a function
220 or a list of functions to be called to run the hook.
221 If the value is a function, it is called with no arguments.
222 If 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))))
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'.
236 Don't change it.")
238 (defun add-hook (hook function)
239 "Add to the value of HOOK the function FUNCTION unless already present.
240 HOOK should be a symbol, and FUNCTION may be any valid function.
241 HOOK's value should be a list of functions, not a single function.
242 If 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)))
250 (set hook (cons function (symbol-value hook)))))
252 (defun momentary-string-display (string pos &optional exit-char message)
253 "Momentarily display STRING in the buffer at POS.
254 Display remains until next character is typed.
255 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
256 otherwise it is then available as input (as a command if nothing else).
257 Display MESSAGE (optional fourth arg) in the echo area.
258 If 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))))
283 (defun start-process-shell-command (name buffer &rest args)
284 "Start a program in a subprocess. Return the process object for it.
285 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
286 NAME is name for process. It is modified if necessary to make it unique.
287 BUFFER 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
292 Third arg is command name, the name of a shell command.
293 Remaining arguments are the arguments for the command.
294 Wildcards 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 " ")))))
300 (defun eval-after-load (file form)
301 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
302 This makes or adds to an entry on `after-load-alist'.
303 FILE 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)
309 (defun eval-next-after-load (file)
310 "Read the following input sexp, and run it whenever FILE is loaded.
311 This makes or adds to an entry on `after-load-alist'.
312 FILE should be the name of a library, with no directory name."
313 (eval-after-load file (read)))
315 (defmacro defun-inline (name args &rest body)
316 "Create an \"inline defun\" (actually a macro).
317 Use 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))))
326 (defun user-original-login-name ()
327 "Return user's login name from original login.
328 This tries to remain unaffected by `su', by looking in environment variables."
329 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
331 (defun force-mode-line-update (&optional all)
332 "Force the mode-line of the current buffer to be redisplayed.
333 With 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)))
337 (defun keyboard-translate (from to)
338 "Translate character FROM to TO at a low level.
339 This function creates a `keyboard-translate-table' if necessary
340 and 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))
351 (defmacro lambda (&rest cdr)
352 "Macro which allows one to write (lambda ...) for anonymous functions
353 instead of having to write (function (lambda ...)) or '(lambda ...), the
354 latter of which won't get byte-compiled."
355 (` (function (lambda (,@ cdr)))))
357 ;;; subr.el ends here