]> code.delx.au - gnu-emacs/blob - lisp/subr.el
*** empty log message ***
[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 (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)))))
32
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))))))
48
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)))
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.
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))))))
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.
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)))))
148
149 (defmacro save-match-data (&rest body)
150 "Execute the BODY forms, restoring the global value of the match data."
151 (let ((original (make-symbol "match-data")))
152 (list
153 'let (list (list original '(match-data)))
154 (list 'unwind-protect
155 (cons 'progn body)
156 (list 'store-match-data original)))))
157
158 ;; Avoids useless byte-compilation.
159 ;; In the future, would be better to fix byte compiler
160 ;; not to really compile in cases like this,
161 ;; and use defun here.
162 (fset 'ignore '(lambda (&rest ignore)
163 "Do nothing.
164 Accept any number of arguments, but ignore them."
165 nil))
166
167 \f
168 ; old names
169 (fset 'make-syntax-table 'copy-syntax-table)
170 (fset 'dot 'point)
171 (fset 'dot-marker 'point-marker)
172 (fset 'dot-min 'point-min)
173 (fset 'dot-max 'point-max)
174 (fset 'window-dot 'window-point)
175 (fset 'set-window-dot 'set-window-point)
176 (fset 'read-input 'read-string)
177 (fset 'send-string 'process-send-string)
178 (fset 'send-region 'process-send-region)
179 (fset 'show-buffer 'set-window-buffer)
180 (fset 'buffer-flush-undo 'buffer-disable-undo)
181 (fset 'eval-current-buffer 'eval-buffer)
182
183 ; alternate names
184 (fset 'string= 'string-equal)
185 (fset 'string< 'string-lessp)
186 (fset 'move-marker 'set-marker)
187 (fset 'eql 'eq)
188 (fset 'not 'null)
189 (fset 'numberp 'integerp)
190 (fset 'rplaca 'setcar)
191 (fset 'rplacd 'setcdr)
192 (fset 'beep 'ding) ;preserve lingual purtity
193 (fset 'indent-to-column 'indent-to)
194 (fset 'backward-delete-char 'delete-backward-char)
195 (fset 'search-forward-regexp (symbol-function 're-search-forward))
196 (fset 'search-backward-regexp (symbol-function 're-search-backward))
197 \f
198 ;;; global-map, esc-map, and ctl-x-map have their values set up
199 ;;; in keymap.c.
200 (defvar global-map nil
201 "Default global keymap mapping Emacs keyboard input into commands.
202 The value is a keymap which is usually (but not necessarily) Emacs's
203 global map.")
204
205 (defvar esc-map nil
206 "Default keymap for ESC (meta) commands.
207 The normal global definition of the character ESC indirects to this keymap.")
208
209 (defvar ctl-x-map nil
210 "Default keymap for C-x commands.
211 The normal global definition of the character C-x indirects to this keymap.")
212
213 (defvar ctl-x-4-map (make-sparse-keymap)
214 "Keymap for subcommands of C-x 4")
215 (fset 'ctl-x-4-prefix ctl-x-4-map)
216 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
217
218 (defvar ctl-x-5-map (make-sparse-keymap)
219 "Keymap for frame commands.")
220 (fset 'ctl-x-5-prefix ctl-x-5-map)
221 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
222
223 \f
224 (defun run-hooks (&rest hooklist)
225 "Takes hook names and runs each one in turn. Major mode functions use this.
226 Each argument should be a symbol, a hook variable.
227 These symbols are processed in the order specified.
228 If a hook symbol has a non-nil value, that value may be a function
229 or a list of functions to be called to run the hook.
230 If the value is a function, it is called with no arguments.
231 If it is a list, the elements are called, in order, with no arguments."
232 (while hooklist
233 (let ((sym (car hooklist)))
234 (and (boundp sym)
235 (symbol-value sym)
236 (let ((value (symbol-value sym)))
237 (if (and (listp value) (not (eq (car value) 'lambda)))
238 (mapcar 'funcall value)
239 (funcall value)))))
240 (setq hooklist (cdr hooklist))))
241
242 ;; Tell C code how to call this function.
243 (defconst run-hooks 'run-hooks
244 "Variable by which C primitives find the function `run-hooks'.
245 Don't change it.")
246
247 (defun add-hook (hook function)
248 "Add to the value of HOOK the function FUNCTION unless already present.
249 HOOK should be a symbol, and FUNCTION may be any valid function.
250 HOOK's value should be a list of functions, not a single function.
251 If HOOK is void, it is first set to nil."
252 (or (boundp hook) (set hook nil))
253 (or (if (consp function)
254 ;; Clever way to tell whether a given lambda-expression
255 ;; is equal to anything in the hook.
256 (let ((tail (assoc (cdr function) (symbol-value hook))))
257 (equal function tail))
258 (memq function (symbol-value hook)))
259 (set hook (cons function (symbol-value hook)))))
260 \f
261 (defun momentary-string-display (string pos &optional exit-char message)
262 "Momentarily display STRING in the buffer at POS.
263 Display remains until next character is typed.
264 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
265 otherwise it is then available as input (as a command if nothing else).
266 Display MESSAGE (optional fourth arg) in the echo area.
267 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
268 (or exit-char (setq exit-char ?\ ))
269 (let ((buffer-read-only nil)
270 (modified (buffer-modified-p))
271 (name buffer-file-name)
272 insert-end)
273 (unwind-protect
274 (progn
275 (save-excursion
276 (goto-char pos)
277 ;; defeat file locking... don't try this at home, kids!
278 (setq buffer-file-name nil)
279 (insert-before-markers string)
280 (setq insert-end (point)))
281 (message (or message "Type %s to continue editing.")
282 (single-key-description exit-char))
283 (let ((char (read-char)))
284 (or (eq char exit-char)
285 (setq unread-command-char char))))
286 (if insert-end
287 (save-excursion
288 (delete-region pos insert-end)))
289 (setq buffer-file-name name)
290 (set-buffer-modified-p modified))))
291
292 (defun start-process-shell-command (name buffer &rest args)
293 "Start a program in a subprocess. Return the process object for it.
294 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
295 NAME is name for process. It is modified if necessary to make it unique.
296 BUFFER is the buffer or (buffer-name) to associate with the process.
297 Process output goes at end of that buffer, unless you specify
298 an output stream or filter function to handle the output.
299 BUFFER may be also nil, meaning that this process is not associated
300 with any buffer
301 Third arg is command name, the name of a shell command.
302 Remaining arguments are the arguments for the command.
303 Wildcards and redirection are handle as usual in the shell."
304 (if (eq system-type 'vax-vms)
305 (apply 'start-process name buffer args)
306 (start-process name buffer shell-file-name "-c"
307 (concat "exec " (mapconcat 'identity args " ")))))
308 \f
309 (defun eval-after-load (file form)
310 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
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 (or (assoc file after-load-alist)
314 (setq after-load-alist (cons (list file) after-load-alist)))
315 (nconc (assoc file after-load-alist) (list form))
316 form)
317
318 (defun eval-next-after-load (file)
319 "Read the following input sexp, and run it whenever FILE is loaded.
320 This makes or adds to an entry on `after-load-alist'.
321 FILE should be the name of a library, with no directory name."
322 (eval-after-load file (read)))
323
324 ;;(defmacro defun-inline (name args &rest body)
325 ;; "Create an \"inline defun\" (actually a macro).
326 ;;Use just like `defun'."
327 ;; (nconc (list 'defmacro name '(&rest args))
328 ;; (if (stringp (car body))
329 ;; (prog1 (list (car body))
330 ;; (setq body (or (cdr body) body))))
331 ;; (list (list 'cons (list 'quote
332 ;; (cons 'lambda (cons args body)))
333 ;; 'args))))
334 \f
335 (defun user-original-login-name ()
336 "Return user's login name from original login.
337 This tries to remain unaffected by `su', by looking in environment variables."
338 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
339 \f
340 (defun force-mode-line-update (&optional all)
341 "Force the mode-line of the current buffer to be redisplayed.
342 With optional non-nil ALL then force then force redisplay of all mode-lines."
343 (if all (save-excursion (set-buffer (other-buffer))))
344 (set-buffer-modified-p (buffer-modified-p)))
345
346 (defun keyboard-translate (from to)
347 "Translate character FROM to TO at a low level.
348 This function creates a `keyboard-translate-table' if necessary
349 and then modifies one entry in it."
350 (or (arrayp keyboard-translate-table)
351 (setq keyboard-translate-table ""))
352 (if (or (> from (length keyboard-translate-table))
353 (> to (length keyboard-translate-table)))
354 (progn
355 (let* ((i (length keyboard-translate-table))
356 (table (make-string (- 256 i) 0)))
357 (while (< i 256)
358 (aset table i i)
359 (setq i (1+ i)))
360 (setq keyboard-translate-table table))))
361 (aset keyboard-translate-table from to))
362
363 \f
364 (defmacro lambda (&rest cdr)
365 "Macro which allows one to write (lambda ...) for anonymous functions
366 instead of having to write (function (lambda ...)) or '(lambda ...), the
367 latter of which won't get byte-compiled."
368 (` (function (lambda (,@ cdr)))))
369
370 ;;; subr.el ends here