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