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