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