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