Commit | Line | Data |
---|---|---|
c88ab9ce | 1 | ;;; subr.el --- basic lisp subroutines for Emacs |
630cc463 | 2 | |
492878e4 | 3 | ;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc. |
be9b65ac DL |
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 | |
492878e4 | 9 | ;; the Free Software Foundation; either version 2, or (at your option) |
be9b65ac DL |
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 | ||
630cc463 | 21 | ;;; Code: |
be9b65ac | 22 | |
9a5336ae JB |
23 | \f |
24 | ;;;; Lisp language features. | |
25 | ||
26 | (defmacro lambda (&rest cdr) | |
27 | "Return a lambda expression. | |
28 | A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is | |
29 | self-quoting; the result of evaluating the lambda expression is the | |
30 | expression itself. The lambda expression may then be treated as a | |
31 | function, i. e. stored as the function value of a symbol, passed to | |
32 | funcall or mapcar, etcetera. | |
33 | ARGS should take the same form as an argument list for a `defun'. | |
34 | DOCSTRING should be a string, as described for `defun'. It may be omitted. | |
35 | INTERACTIVE should be a call to the function `interactive', which see. | |
36 | It may also be omitted. | |
37 | BODY should be a list of lisp expressions." | |
38 | ;; Note that this definition should not use backquotes; subr.el should not | |
39 | ;; depend on backquote.el. | |
40 | (list 'function (cons 'lambda cdr))) | |
41 | ||
42 | ;;(defmacro defun-inline (name args &rest body) | |
43 | ;; "Create an \"inline defun\" (actually a macro). | |
44 | ;;Use just like `defun'." | |
45 | ;; (nconc (list 'defmacro name '(&rest args)) | |
46 | ;; (if (stringp (car body)) | |
47 | ;; (prog1 (list (car body)) | |
48 | ;; (setq body (or (cdr body) body)))) | |
49 | ;; (list (list 'cons (list 'quote | |
50 | ;; (cons 'lambda (cons args body))) | |
51 | ;; 'args)))) | |
52 | ||
53 | \f | |
54 | ;;;; Window tree functions. | |
55 | ||
492878e4 | 56 | (defun one-window-p (&optional nomini) |
be9b65ac DL |
57 | "Returns non-nil if there is only one window. |
58 | Optional arg NOMINI non-nil means don't count the minibuffer | |
59 | even if it is active." | |
492878e4 JB |
60 | (let ((base-window (selected-window))) |
61 | (if (and nomini (eq base-window (minibuffer-window))) | |
62 | (setq base-window (next-window base-window))) | |
63 | (eq base-window | |
64 | (next-window base-window (if nomini 'arg))))) | |
be9b65ac | 65 | |
0cc89026 | 66 | (defun walk-windows (proc &optional minibuf all-frames) |
be9b65ac DL |
67 | "Cycle through all visible windows, calling PROC for each one. |
68 | PROC is called with a window as argument. | |
69 | Optional second arg MINIBUF t means count the minibuffer window | |
70 | even if not active. If MINIBUF is neither t nor nil it means | |
71 | not to count the minibuffer even if it is active. | |
f1f2d09a RS |
72 | |
73 | Optional third arg ALL-FRAMES, if t, means include all frames. | |
74 | ALL-FRAMES nil or omitted means cycle within the selected frame, | |
75 | but include the minibuffer window (if MINIBUF says so) that that | |
76 | frame uses, even if it is on another frame. | |
77 | If ALL-FRAMES is neither nil nor t, stick strictly to the selected frame." | |
be9b65ac DL |
78 | (let* ((walk-windows-start (selected-window)) |
79 | (walk-windows-current walk-windows-start)) | |
80 | (while (progn | |
81 | (setq walk-windows-current | |
0cc89026 | 82 | (next-window walk-windows-current minibuf all-frames)) |
be9b65ac DL |
83 | (funcall proc walk-windows-current) |
84 | (not (eq walk-windows-current walk-windows-start)))))) | |
85 | ||
9a5336ae JB |
86 | \f |
87 | ;;;; Keymap support. | |
be9b65ac DL |
88 | |
89 | (defun undefined () | |
90 | (interactive) | |
91 | (ding)) | |
92 | ||
93 | ;Prevent the \{...} documentation construct | |
94 | ;from mentioning keys that run this command. | |
95 | (put 'undefined 'suppress-keymap t) | |
96 | ||
97 | (defun suppress-keymap (map &optional nodigits) | |
98 | "Make MAP override all normally self-inserting keys to be undefined. | |
99 | Normally, as an exception, digits and minus-sign are set to make prefix args, | |
100 | but optional second arg NODIGITS non-nil treats them like other chars." | |
101 | (let ((i 0)) | |
102 | (while (<= i 127) | |
103 | (if (eql (lookup-key global-map (char-to-string i)) 'self-insert-command) | |
104 | (define-key map (char-to-string i) 'undefined)) | |
105 | (setq i (1+ i)))) | |
106 | (or nodigits | |
107 | (let (loop) | |
108 | (define-key map "-" 'negative-argument) | |
109 | ;; Make plain numbers do numeric args. | |
110 | (setq loop ?0) | |
111 | (while (<= loop ?9) | |
112 | (define-key map (char-to-string loop) 'digit-argument) | |
113 | (setq loop (1+ loop)))))) | |
114 | ||
be9b65ac DL |
115 | ;Moved to keymap.c |
116 | ;(defun copy-keymap (keymap) | |
117 | ; "Return a copy of KEYMAP" | |
118 | ; (while (not (keymapp keymap)) | |
119 | ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap)))) | |
120 | ; (if (vectorp keymap) | |
121 | ; (copy-sequence keymap) | |
122 | ; (copy-alist keymap))) | |
123 | ||
7f2c2edd | 124 | (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix) |
be9b65ac DL |
125 | "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF. |
126 | In other words, OLDDEF is replaced with NEWDEF where ever it appears. | |
7f2c2edd RS |
127 | If optional fourth argument OLDMAP is specified, we redefine |
128 | in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP." | |
129 | (or prefix (setq prefix "")) | |
130 | (let* ((scan (or oldmap keymap)) | |
131 | (vec1 (vector nil)) | |
132 | (prefix1 (vconcat prefix vec1))) | |
133 | ;; Scan OLDMAP, finding each char or event-symbol that | |
134 | ;; has any definition, and act on it with hack-key. | |
135 | (while (consp scan) | |
136 | (if (consp (car scan)) | |
137 | (let ((char (car (car scan))) | |
138 | (defn (cdr (car scan)))) | |
139 | ;; The inside of this let duplicates exactly | |
140 | ;; the inside of the following let that handles array elements. | |
141 | (aset vec1 0 char) | |
142 | (aset prefix1 (length prefix) char) | |
143 | (let (inner-def) | |
144 | ;; Skip past menu-prompt. | |
145 | (while (stringp (car-safe defn)) | |
146 | (setq defn (cdr defn))) | |
147 | (setq inner-def defn) | |
148 | (while (and (symbolp inner-def) | |
149 | (fboundp inner-def)) | |
150 | (setq inner-def (symbol-function inner-def))) | |
151 | (if (eq defn olddef) | |
152 | (define-key keymap prefix1 newdef) | |
153 | (if (keymapp defn) | |
154 | (substitute-key-definition olddef newdef keymap | |
155 | inner-def | |
156 | prefix1))))) | |
157 | (if (arrayp (car scan)) | |
158 | (let* ((array (car scan)) | |
159 | (len (length array)) | |
160 | (i 0)) | |
161 | (while (< i len) | |
162 | (let ((char i) (defn (aref array i))) | |
163 | ;; The inside of this let duplicates exactly | |
164 | ;; the inside of the previous let. | |
165 | (aset vec1 0 char) | |
166 | (aset prefix1 (length prefix) char) | |
167 | (let (inner-def) | |
168 | ;; Skip past menu-prompt. | |
169 | (while (stringp (car-safe defn)) | |
170 | (setq defn (cdr defn))) | |
171 | (setq inner-def defn) | |
172 | (while (and (symbolp inner-def) | |
173 | (fboundp inner-def)) | |
174 | (setq inner-def (symbol-function inner-def))) | |
175 | (if (eq defn olddef) | |
176 | (define-key keymap prefix1 newdef) | |
177 | (if (keymapp defn) | |
178 | (substitute-key-definition olddef newdef keymap | |
179 | inner-def | |
180 | prefix1))))) | |
181 | (setq i (1+ i)))))) | |
182 | (setq scan (cdr scan))))) | |
9a5336ae | 183 | |
06ae9cf2 | 184 | (defun define-key-after (keymap key definition after) |
4434d61b RS |
185 | "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding. |
186 | This is like `define-key' except that the binding for KEY is placed | |
187 | just after the binding for the event AFTER, instead of at the beginning | |
188 | of the map. | |
626f67f3 RS |
189 | The order matters when the keymap is used as a menu. |
190 | KEY must contain just one event type--it must be a string or vector | |
191 | of length 1." | |
4434d61b RS |
192 | (or (keymapp keymap) |
193 | (signal 'wrong-type-argument (list 'keymapp keymap))) | |
626f67f3 RS |
194 | (if (> (length key) 0) |
195 | (error "multi-event key specified in `define-key-after'")) | |
113d28a8 | 196 | (let ((tail keymap) done inserted |
4434d61b RS |
197 | (first (aref key 0))) |
198 | (while (and (not done) tail) | |
199 | ;; Delete any earlier bindings for the same key. | |
200 | (if (eq (car-safe (car (cdr tail))) first) | |
201 | (setcdr tail (cdr (cdr tail)))) | |
202 | ;; When we reach AFTER's binding, insert the new binding after. | |
203 | ;; If we reach an inherited keymap, insert just before that. | |
113d28a8 | 204 | ;; If we reach the end of this keymap, insert at the end. |
4434d61b | 205 | (if (or (eq (car-safe (car tail)) after) |
113d28a8 RS |
206 | (eq (car (cdr tail)) 'keymap) |
207 | (null (cdr tail))) | |
4434d61b | 208 | (progn |
113d28a8 RS |
209 | ;; Stop the scan only if we find a parent keymap. |
210 | ;; Keep going past the inserted element | |
211 | ;; so we can delete any duplications that come later. | |
212 | (if (eq (car (cdr tail)) 'keymap) | |
213 | (setq done t)) | |
214 | ;; Don't insert more than once. | |
215 | (or inserted | |
216 | (setcdr tail (cons (cons (aref key 0) definition) (cdr tail)))) | |
217 | (setq inserted t))) | |
4434d61b RS |
218 | (setq tail (cdr tail))))) |
219 | ||
9a5336ae JB |
220 | (defun keyboard-translate (from to) |
221 | "Translate character FROM to TO at a low level. | |
222 | This function creates a `keyboard-translate-table' if necessary | |
223 | and then modifies one entry in it." | |
224 | (or (arrayp keyboard-translate-table) | |
225 | (setq keyboard-translate-table "")) | |
226 | (if (or (> from (length keyboard-translate-table)) | |
227 | (> to (length keyboard-translate-table))) | |
228 | (progn | |
229 | (let* ((i (length keyboard-translate-table)) | |
230 | (table (make-string (- 256 i) 0))) | |
231 | (while (< i 256) | |
232 | (aset table i i) | |
233 | (setq i (1+ i))) | |
234 | (setq keyboard-translate-table table)))) | |
235 | (aset keyboard-translate-table from to)) | |
236 | ||
237 | \f | |
238 | ;;;; The global keymap tree. | |
239 | ||
240 | ;;; global-map, esc-map, and ctl-x-map have their values set up in | |
241 | ;;; keymap.c; we just give them docstrings here. | |
242 | ||
243 | (defvar global-map nil | |
244 | "Default global keymap mapping Emacs keyboard input into commands. | |
245 | The value is a keymap which is usually (but not necessarily) Emacs's | |
246 | global map.") | |
247 | ||
248 | (defvar esc-map nil | |
249 | "Default keymap for ESC (meta) commands. | |
250 | The normal global definition of the character ESC indirects to this keymap.") | |
251 | ||
252 | (defvar ctl-x-map nil | |
253 | "Default keymap for C-x commands. | |
254 | The normal global definition of the character C-x indirects to this keymap.") | |
255 | ||
256 | (defvar ctl-x-4-map (make-sparse-keymap) | |
257 | "Keymap for subcommands of C-x 4") | |
059184dd | 258 | (defalias 'ctl-x-4-prefix ctl-x-4-map) |
9a5336ae JB |
259 | (define-key ctl-x-map "4" 'ctl-x-4-prefix) |
260 | ||
261 | (defvar ctl-x-5-map (make-sparse-keymap) | |
262 | "Keymap for frame commands.") | |
059184dd | 263 | (defalias 'ctl-x-5-prefix ctl-x-5-map) |
9a5336ae JB |
264 | (define-key ctl-x-map "5" 'ctl-x-5-prefix) |
265 | ||
0f03054a | 266 | \f |
9a5336ae JB |
267 | ;;;; Event manipulation functions. |
268 | ||
114137b8 RS |
269 | ;; This code exists specifically to make sure that the |
270 | ;; resulting number does not appear in the .elc file. | |
271 | ;; The number is negative on most machines, but not on all! | |
272 | (defconst listify-key-sequence-1 | |
273 | (lsh 1 7)) | |
274 | (setq listify-key-sequence-1 (logior (lsh 1 23) listify-key-sequence-1)) | |
275 | ||
cde6d7e3 RS |
276 | (defun listify-key-sequence (key) |
277 | "Convert a key sequence to a list of events." | |
278 | (if (vectorp key) | |
279 | (append key nil) | |
280 | (mapcar (function (lambda (c) | |
281 | (if (> c 127) | |
114137b8 | 282 | (logxor c listify-key-sequence-1) |
cde6d7e3 RS |
283 | c))) |
284 | (append key nil)))) | |
285 | ||
53e5a4e8 RS |
286 | (defsubst eventp (obj) |
287 | "True if the argument is an event object." | |
288 | (or (integerp obj) | |
289 | (and (symbolp obj) | |
290 | (get obj 'event-symbol-elements)) | |
291 | (and (consp obj) | |
292 | (symbolp (car obj)) | |
293 | (get (car obj) 'event-symbol-elements)))) | |
294 | ||
295 | (defun event-modifiers (event) | |
296 | "Returns a list of symbols representing the modifier keys in event EVENT. | |
297 | The elements of the list may include `meta', `control', | |
1ce4cf38 | 298 | `shift', `hyper', `super', `alt', `click', `drag', and `down'." |
53e5a4e8 RS |
299 | (let ((type event)) |
300 | (if (listp type) | |
301 | (setq type (car type))) | |
302 | (if (symbolp type) | |
303 | (cdr (get type 'event-symbol-elements)) | |
304 | (let ((list nil)) | |
305 | (or (zerop (logand type (lsh 1 23))) | |
306 | (setq list (cons 'meta list))) | |
307 | (or (and (zerop (logand type (lsh 1 22))) | |
308 | (>= (logand type 127) 32)) | |
309 | (setq list (cons 'control list))) | |
310 | (or (and (zerop (logand type (lsh 1 21))) | |
311 | (= (logand type 255) (downcase (logand type 255)))) | |
312 | (setq list (cons 'shift list))) | |
313 | (or (zerop (logand type (lsh 1 20))) | |
314 | (setq list (cons 'hyper list))) | |
315 | (or (zerop (logand type (lsh 1 19))) | |
316 | (setq list (cons 'super list))) | |
317 | (or (zerop (logand type (lsh 1 18))) | |
318 | (setq list (cons 'alt list))) | |
319 | list)))) | |
320 | ||
d63de416 RS |
321 | (defun event-basic-type (event) |
322 | "Returns the basic type of the given event (all modifiers removed). | |
323 | The value is an ASCII printing character (not upper case) or a symbol." | |
2b0f4ba5 JB |
324 | (if (consp event) |
325 | (setq event (car event))) | |
d63de416 RS |
326 | (if (symbolp event) |
327 | (car (get event 'event-symbol-elements)) | |
328 | (let ((base (logand event (1- (lsh 1 18))))) | |
329 | (downcase (if (< base 32) (logior base 64) base))))) | |
330 | ||
0f03054a RS |
331 | (defsubst mouse-movement-p (object) |
332 | "Return non-nil if OBJECT is a mouse movement event." | |
333 | (and (consp object) | |
334 | (eq (car object) 'mouse-movement))) | |
335 | ||
336 | (defsubst event-start (event) | |
337 | "Return the starting position of EVENT. | |
338 | If EVENT is a mouse press or a mouse click, this returns the location | |
339 | of the event. | |
340 | If EVENT is a drag, this returns the drag's starting position. | |
341 | The return value is of the form | |
342 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
343 | The `posn-' functions access elements of such lists." | |
344 | (nth 1 event)) | |
345 | ||
346 | (defsubst event-end (event) | |
347 | "Return the ending location of EVENT. EVENT should be a click or drag event. | |
348 | If EVENT is a click event, this function is the same as `event-start'. | |
349 | The return value is of the form | |
350 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
351 | The `posn-' functions access elements of such lists." | |
69b95560 | 352 | (nth (if (consp (nth 2 event)) 2 1) event)) |
0f03054a RS |
353 | |
354 | (defsubst posn-window (position) | |
355 | "Return the window in POSITION. | |
356 | POSITION should be a list of the form | |
357 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
358 | as returned by the `event-start' and `event-end' functions." | |
359 | (nth 0 position)) | |
360 | ||
361 | (defsubst posn-point (position) | |
362 | "Return the buffer location in POSITION. | |
363 | POSITION should be a list of the form | |
364 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
365 | as returned by the `event-start' and `event-end' functions." | |
15db4e0e JB |
366 | (if (consp (nth 1 position)) |
367 | (car (nth 1 position)) | |
368 | (nth 1 position))) | |
0f03054a RS |
369 | |
370 | (defsubst posn-col-row (position) | |
371 | "Return the row and column in POSITION. | |
372 | POSITION should be a list of the form | |
373 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
374 | as returned by the `event-start' and `event-end' functions." | |
375 | (nth 2 position)) | |
376 | ||
377 | (defsubst posn-timestamp (position) | |
378 | "Return the timestamp of POSITION. | |
379 | POSITION should be a list of the form | |
380 | (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP) | |
f415c00c | 381 | as returned by the `event-start' and `event-end' functions." |
0f03054a | 382 | (nth 3 position)) |
9a5336ae | 383 | |
0f03054a | 384 | \f |
9a5336ae JB |
385 | ;;;; Obsolescent names for functions. |
386 | ||
059184dd ER |
387 | (defalias 'make-syntax-table 'copy-syntax-table) |
388 | (defalias 'dot 'point) | |
389 | (defalias 'dot-marker 'point-marker) | |
390 | (defalias 'dot-min 'point-min) | |
391 | (defalias 'dot-max 'point-max) | |
392 | (defalias 'window-dot 'window-point) | |
393 | (defalias 'set-window-dot 'set-window-point) | |
394 | (defalias 'read-input 'read-string) | |
395 | (defalias 'send-string 'process-send-string) | |
396 | (defalias 'send-region 'process-send-region) | |
397 | (defalias 'show-buffer 'set-window-buffer) | |
398 | (defalias 'buffer-flush-undo 'buffer-disable-undo) | |
399 | (defalias 'eval-current-buffer 'eval-buffer) | |
400 | (defalias 'compiled-function-p 'byte-code-function-p) | |
be9b65ac | 401 | |
d2ec8956 JB |
402 | ;;; This name isn't mentioned in the manual, and we've been hoping to |
403 | ;;; phase it out, but there's still a lot of code out there, even for | |
404 | ;;; Emacs 18.59, which uses mod. I'm going to let the byte compiler's | |
405 | ;;; make-obsolete function to poke people a little more, and leave the | |
406 | ;;; `mod' name around for a while longer. | |
059184dd | 407 | (defalias 'mod '%) |
d2ec8956 | 408 | |
9a5336ae JB |
409 | ;; Some programs still use this as a function. |
410 | (defun baud-rate () | |
bcacc42c RS |
411 | "Obsolete function returning the value of the `baud-rate' variable. |
412 | Please convert your programs to use the variable `baud-rate' directly." | |
9a5336ae JB |
413 | baud-rate) |
414 | ||
415 | \f | |
416 | ;;;; Alternate names for functions - these are not being phased out. | |
417 | ||
059184dd ER |
418 | (defalias 'string= 'string-equal) |
419 | (defalias 'string< 'string-lessp) | |
420 | (defalias 'move-marker 'set-marker) | |
421 | (defalias 'eql 'eq) | |
422 | (defalias 'not 'null) | |
423 | (defalias 'rplaca 'setcar) | |
424 | (defalias 'rplacd 'setcdr) | |
eb8c3be9 | 425 | (defalias 'beep 'ding) ;preserve lingual purity |
059184dd ER |
426 | (defalias 'indent-to-column 'indent-to) |
427 | (defalias 'backward-delete-char 'delete-backward-char) | |
428 | (defalias 'search-forward-regexp (symbol-function 're-search-forward)) | |
429 | (defalias 'search-backward-regexp (symbol-function 're-search-backward)) | |
430 | (defalias 'int-to-string 'number-to-string) | |
37f6661a JB |
431 | |
432 | ;;; Should this be an obsolete name? If you decide it should, you get | |
433 | ;;; to go through all the sources and change them. | |
059184dd | 434 | (defalias 'string-to-int 'string-to-number) |
be9b65ac | 435 | \f |
9a5336ae | 436 | ;;;; Hook manipulation functions. |
be9b65ac | 437 | |
be9b65ac DL |
438 | (defun run-hooks (&rest hooklist) |
439 | "Takes hook names and runs each one in turn. Major mode functions use this. | |
440 | Each argument should be a symbol, a hook variable. | |
441 | These symbols are processed in the order specified. | |
442 | If a hook symbol has a non-nil value, that value may be a function | |
443 | or a list of functions to be called to run the hook. | |
444 | If the value is a function, it is called with no arguments. | |
445 | If it is a list, the elements are called, in order, with no arguments." | |
446 | (while hooklist | |
447 | (let ((sym (car hooklist))) | |
448 | (and (boundp sym) | |
449 | (symbol-value sym) | |
450 | (let ((value (symbol-value sym))) | |
451 | (if (and (listp value) (not (eq (car value) 'lambda))) | |
452 | (mapcar 'funcall value) | |
453 | (funcall value))))) | |
454 | (setq hooklist (cdr hooklist)))) | |
455 | ||
456 | ;; Tell C code how to call this function. | |
457 | (defconst run-hooks 'run-hooks | |
458 | "Variable by which C primitives find the function `run-hooks'. | |
459 | Don't change it.") | |
460 | ||
08159178 ER |
461 | (defun add-hook (hook function &optional append) |
462 | "Add to the value of HOOK the function FUNCTION unless already present (it | |
463 | becomes the first hook on the list unless optional APPEND is non-nil, in | |
464 | which case it becomes the last). HOOK should be a symbol, and FUNCTION may be | |
465 | any valid function. HOOK's value should be a list of functions, not a single | |
466 | function. If HOOK is void, it is first set to nil." | |
be9b65ac DL |
467 | (or (boundp hook) (set hook nil)) |
468 | (or (if (consp function) | |
469 | ;; Clever way to tell whether a given lambda-expression | |
470 | ;; is equal to anything in the hook. | |
471 | (let ((tail (assoc (cdr function) (symbol-value hook)))) | |
472 | (equal function tail)) | |
473 | (memq function (symbol-value hook))) | |
08159178 ER |
474 | (set hook |
475 | (if append | |
476 | (nconc (symbol-value hook) (list function)) | |
477 | (cons function (symbol-value hook)))))) | |
9a5336ae | 478 | |
be9b65ac | 479 | \f |
9a5336ae JB |
480 | ;;;; Specifying things to do after certain files are loaded. |
481 | ||
482 | (defun eval-after-load (file form) | |
483 | "Arrange that, if FILE is ever loaded, FORM will be run at that time. | |
484 | This makes or adds to an entry on `after-load-alist'. | |
485 | FILE should be the name of a library, with no directory name." | |
486 | (or (assoc file after-load-alist) | |
487 | (setq after-load-alist (cons (list file) after-load-alist))) | |
488 | (nconc (assoc file after-load-alist) (list form)) | |
489 | form) | |
490 | ||
491 | (defun eval-next-after-load (file) | |
492 | "Read the following input sexp, and run it whenever FILE is loaded. | |
493 | This makes or adds to an entry on `after-load-alist'. | |
494 | FILE should be the name of a library, with no directory name." | |
495 | (eval-after-load file (read))) | |
496 | ||
497 | \f | |
498 | ;;;; Input and display facilities. | |
499 | ||
500 | (defun read-quoted-char (&optional prompt) | |
501 | "Like `read-char', except that if the first character read is an octal | |
502 | digit, we read up to two more octal digits and return the character | |
503 | represented by the octal number consisting of those digits. | |
504 | Optional argument PROMPT specifies a string to use to prompt the user." | |
505 | (let ((count 0) (code 0) char) | |
506 | (while (< count 3) | |
507 | (let ((inhibit-quit (zerop count)) | |
508 | (help-form nil)) | |
509 | (and prompt (message "%s-" prompt)) | |
510 | (setq char (read-char)) | |
511 | (if inhibit-quit (setq quit-flag nil))) | |
512 | (cond ((null char)) | |
513 | ((and (<= ?0 char) (<= char ?7)) | |
514 | (setq code (+ (* code 8) (- char ?0)) | |
515 | count (1+ count)) | |
516 | (and prompt (message (setq prompt | |
517 | (format "%s %c" prompt char))))) | |
518 | ((> count 0) | |
519 | (setq unread-command-events (list char) count 259)) | |
520 | (t (setq code char count 259)))) | |
521 | (logand 255 code))) | |
522 | ||
523 | (defun force-mode-line-update (&optional all) | |
524 | "Force the mode-line of the current buffer to be redisplayed. | |
525 | With optional non-nil ALL then force then force redisplay of all mode-lines." | |
526 | (if all (save-excursion (set-buffer (other-buffer)))) | |
527 | (set-buffer-modified-p (buffer-modified-p))) | |
528 | ||
be9b65ac DL |
529 | (defun momentary-string-display (string pos &optional exit-char message) |
530 | "Momentarily display STRING in the buffer at POS. | |
531 | Display remains until next character is typed. | |
532 | If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed; | |
533 | otherwise it is then available as input (as a command if nothing else). | |
534 | Display MESSAGE (optional fourth arg) in the echo area. | |
535 | If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there." | |
536 | (or exit-char (setq exit-char ?\ )) | |
537 | (let ((buffer-read-only nil) | |
538 | (modified (buffer-modified-p)) | |
539 | (name buffer-file-name) | |
540 | insert-end) | |
541 | (unwind-protect | |
542 | (progn | |
543 | (save-excursion | |
544 | (goto-char pos) | |
545 | ;; defeat file locking... don't try this at home, kids! | |
546 | (setq buffer-file-name nil) | |
547 | (insert-before-markers string) | |
548 | (setq insert-end (point))) | |
549 | (message (or message "Type %s to continue editing.") | |
550 | (single-key-description exit-char)) | |
3547c855 | 551 | (let ((char (read-event))) |
be9b65ac | 552 | (or (eq char exit-char) |
dbc4e1c1 | 553 | (setq unread-command-events (list char))))) |
be9b65ac DL |
554 | (if insert-end |
555 | (save-excursion | |
556 | (delete-region pos insert-end))) | |
557 | (setq buffer-file-name name) | |
558 | (set-buffer-modified-p modified)))) | |
559 | ||
9a5336ae JB |
560 | \f |
561 | ;;;; Miscellanea. | |
562 | ||
563 | (defun ignore (&rest ignore) | |
564 | "Do nothing. | |
565 | Accept any number of arguments, but ignore them." | |
566 | nil) | |
567 | ||
568 | (defun error (&rest args) | |
569 | "Signal an error, making error message by passing all args to `format'." | |
570 | (while t | |
571 | (signal 'error (list (apply 'format args))))) | |
572 | ||
573 | (defun user-original-login-name () | |
574 | "Return user's login name from original login. | |
575 | This tries to remain unaffected by `su', by looking in environment variables." | |
576 | (or (getenv "LOGNAME") (getenv "USER") (user-login-name))) | |
577 | ||
be9b65ac DL |
578 | (defun start-process-shell-command (name buffer &rest args) |
579 | "Start a program in a subprocess. Return the process object for it. | |
580 | Args are NAME BUFFER COMMAND &rest COMMAND-ARGS. | |
581 | NAME is name for process. It is modified if necessary to make it unique. | |
582 | BUFFER is the buffer or (buffer-name) to associate with the process. | |
583 | Process output goes at end of that buffer, unless you specify | |
584 | an output stream or filter function to handle the output. | |
585 | BUFFER may be also nil, meaning that this process is not associated | |
586 | with any buffer | |
587 | Third arg is command name, the name of a shell command. | |
588 | Remaining arguments are the arguments for the command. | |
589 | Wildcards and redirection are handle as usual in the shell." | |
590 | (if (eq system-type 'vax-vms) | |
591 | (apply 'start-process name buffer args) | |
592 | (start-process name buffer shell-file-name "-c" | |
593 | (concat "exec " (mapconcat 'identity args " "))))) | |
be9b65ac | 594 | |
9a5336ae JB |
595 | (defmacro save-match-data (&rest body) |
596 | "Execute the BODY forms, restoring the global value of the match data." | |
597 | (let ((original (make-symbol "match-data"))) | |
598 | (list | |
599 | 'let (list (list original '(match-data))) | |
600 | (list 'unwind-protect | |
601 | (cons 'progn body) | |
602 | (list 'store-match-data original))))) | |
ffd56f97 | 603 | |
9a5336ae JB |
604 | ;; now in fns.c |
605 | ;(defun nth (n list) | |
606 | ; "Returns the Nth element of LIST. | |
607 | ;N counts from zero. If LIST is not that long, nil is returned." | |
608 | ; (car (nthcdr n list))) | |
609 | ; | |
610 | ;(defun copy-alist (alist) | |
611 | ; "Return a copy of ALIST. | |
612 | ;This is a new alist which represents the same mapping | |
613 | ;from objects to objects, but does not share the alist structure with ALIST. | |
614 | ;The objects mapped (cars and cdrs of elements of the alist) | |
615 | ;are shared, however." | |
616 | ; (setq alist (copy-sequence alist)) | |
617 | ; (let ((tail alist)) | |
618 | ; (while tail | |
619 | ; (if (consp (car tail)) | |
620 | ; (setcar tail (cons (car (car tail)) (cdr (car tail))))) | |
621 | ; (setq tail (cdr tail)))) | |
622 | ; alist) | |
630cc463 ER |
623 | |
624 | ;;; subr.el ends here | |
9a5336ae | 625 |