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