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