Commit | Line | Data |
---|---|---|
c88ab9ce | 1 | ;;; subr.el --- basic lisp subroutines for Emacs |
630cc463 | 2 | |
cd323f89 | 3 | ;;; Copyright (C) 1985, 1986, 1992, 1994, 1995 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 | |
bec0d7f9 RS |
31 | function, i.e., stored as the function value of a symbol, passed to |
32 | funcall or mapcar, etc. | |
33 | ||
9a5336ae | 34 | ARGS should take the same form as an argument list for a `defun'. |
8fd68088 RS |
35 | DOCSTRING is an optional documentation string. |
36 | If present, it should describe how to call the function. | |
37 | But documentation strings are usually not useful in nameless functions. | |
9a5336ae JB |
38 | INTERACTIVE should be a call to the function `interactive', which see. |
39 | It may also be omitted. | |
40 | BODY should be a list of lisp expressions." | |
41 | ;; Note that this definition should not use backquotes; subr.el should not | |
42 | ;; depend on backquote.el. | |
43 | (list 'function (cons 'lambda cdr))) | |
44 | ||
45 | ;;(defmacro defun-inline (name args &rest body) | |
46 | ;; "Create an \"inline defun\" (actually a macro). | |
47 | ;;Use just like `defun'." | |
48 | ;; (nconc (list 'defmacro name '(&rest args)) | |
49 | ;; (if (stringp (car body)) | |
50 | ;; (prog1 (list (car body)) | |
51 | ;; (setq body (or (cdr body) body)))) | |
52 | ;; (list (list 'cons (list 'quote | |
53 | ;; (cons 'lambda (cons args body))) | |
54 | ;; 'args)))) | |
55 | ||
56 | \f | |
9a5336ae | 57 | ;;;; Keymap support. |
be9b65ac DL |
58 | |
59 | (defun undefined () | |
60 | (interactive) | |
61 | (ding)) | |
62 | ||
63 | ;Prevent the \{...} documentation construct | |
64 | ;from mentioning keys that run this command. | |
65 | (put 'undefined 'suppress-keymap t) | |
66 | ||
67 | (defun suppress-keymap (map &optional nodigits) | |
68 | "Make MAP override all normally self-inserting keys to be undefined. | |
69 | Normally, as an exception, digits and minus-sign are set to make prefix args, | |
70 | but optional second arg NODIGITS non-nil treats them like other chars." | |
80e7b471 | 71 | (substitute-key-definition 'self-insert-command 'undefined map global-map) |
be9b65ac DL |
72 | (or nodigits |
73 | (let (loop) | |
74 | (define-key map "-" 'negative-argument) | |
75 | ;; Make plain numbers do numeric args. | |
76 | (setq loop ?0) | |
77 | (while (<= loop ?9) | |
78 | (define-key map (char-to-string loop) 'digit-argument) | |
79 | (setq loop (1+ loop)))))) | |
80 | ||
be9b65ac DL |
81 | ;Moved to keymap.c |
82 | ;(defun copy-keymap (keymap) | |
83 | ; "Return a copy of KEYMAP" | |
84 | ; (while (not (keymapp keymap)) | |
85 | ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap)))) | |
86 | ; (if (vectorp keymap) | |
87 | ; (copy-sequence keymap) | |
88 | ; (copy-alist keymap))) | |
89 | ||
f14dbba7 KH |
90 | (defvar key-substitution-in-progress nil |
91 | "Used internally by substitute-key-definition.") | |
92 | ||
7f2c2edd | 93 | (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix) |
be9b65ac DL |
94 | "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF. |
95 | In other words, OLDDEF is replaced with NEWDEF where ever it appears. | |
7f2c2edd RS |
96 | If optional fourth argument OLDMAP is specified, we redefine |
97 | in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP." | |
98 | (or prefix (setq prefix "")) | |
99 | (let* ((scan (or oldmap keymap)) | |
100 | (vec1 (vector nil)) | |
f14dbba7 KH |
101 | (prefix1 (vconcat prefix vec1)) |
102 | (key-substitution-in-progress | |
103 | (cons scan key-substitution-in-progress))) | |
7f2c2edd RS |
104 | ;; Scan OLDMAP, finding each char or event-symbol that |
105 | ;; has any definition, and act on it with hack-key. | |
106 | (while (consp scan) | |
107 | (if (consp (car scan)) | |
108 | (let ((char (car (car scan))) | |
109 | (defn (cdr (car scan)))) | |
110 | ;; The inside of this let duplicates exactly | |
111 | ;; the inside of the following let that handles array elements. | |
112 | (aset vec1 0 char) | |
113 | (aset prefix1 (length prefix) char) | |
44d798af | 114 | (let (inner-def skipped) |
7f2c2edd RS |
115 | ;; Skip past menu-prompt. |
116 | (while (stringp (car-safe defn)) | |
44d798af | 117 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 118 | (setq defn (cdr defn))) |
e025dddf RS |
119 | ;; Skip past cached key-equivalence data for menu items. |
120 | (and (consp defn) (consp (car defn)) | |
121 | (setq defn (cdr defn))) | |
7f2c2edd | 122 | (setq inner-def defn) |
e025dddf | 123 | ;; Look past a symbol that names a keymap. |
7f2c2edd RS |
124 | (while (and (symbolp inner-def) |
125 | (fboundp inner-def)) | |
126 | (setq inner-def (symbol-function inner-def))) | |
127 | (if (eq defn olddef) | |
44d798af | 128 | (define-key keymap prefix1 (nconc (nreverse skipped) newdef)) |
f14dbba7 | 129 | (if (and (keymapp defn) |
350b7567 RS |
130 | ;; Avoid recursively scanning |
131 | ;; where KEYMAP does not have a submap. | |
afd9831b RS |
132 | (let ((elt (lookup-key keymap prefix1))) |
133 | (or (null elt) | |
134 | (keymapp elt))) | |
350b7567 | 135 | ;; Avoid recursively rescanning keymap being scanned. |
f14dbba7 KH |
136 | (not (memq inner-def |
137 | key-substitution-in-progress))) | |
e025dddf RS |
138 | ;; If this one isn't being scanned already, |
139 | ;; scan it now. | |
7f2c2edd RS |
140 | (substitute-key-definition olddef newdef keymap |
141 | inner-def | |
142 | prefix1))))) | |
143 | (if (arrayp (car scan)) | |
144 | (let* ((array (car scan)) | |
145 | (len (length array)) | |
146 | (i 0)) | |
147 | (while (< i len) | |
148 | (let ((char i) (defn (aref array i))) | |
149 | ;; The inside of this let duplicates exactly | |
150 | ;; the inside of the previous let. | |
151 | (aset vec1 0 char) | |
152 | (aset prefix1 (length prefix) char) | |
44d798af | 153 | (let (inner-def skipped) |
7f2c2edd RS |
154 | ;; Skip past menu-prompt. |
155 | (while (stringp (car-safe defn)) | |
44d798af | 156 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 157 | (setq defn (cdr defn))) |
e025dddf RS |
158 | (and (consp defn) (consp (car defn)) |
159 | (setq defn (cdr defn))) | |
7f2c2edd RS |
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) | |
44d798af RS |
165 | (define-key keymap prefix1 |
166 | (nconc (nreverse skipped) newdef)) | |
f14dbba7 | 167 | (if (and (keymapp defn) |
afd9831b RS |
168 | (let ((elt (lookup-key keymap prefix1))) |
169 | (or (null elt) | |
170 | (keymapp elt))) | |
f14dbba7 KH |
171 | (not (memq inner-def |
172 | key-substitution-in-progress))) | |
7f2c2edd RS |
173 | (substitute-key-definition olddef newdef keymap |
174 | inner-def | |
175 | prefix1))))) | |
176 | (setq i (1+ i)))))) | |
177 | (setq scan (cdr scan))))) | |
9a5336ae | 178 | |
06ae9cf2 | 179 | (defun define-key-after (keymap key definition after) |
4434d61b RS |
180 | "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding. |
181 | This is like `define-key' except that the binding for KEY is placed | |
182 | just after the binding for the event AFTER, instead of at the beginning | |
183 | of the map. | |
626f67f3 | 184 | The order matters when the keymap is used as a menu. |
9ed287b0 RS |
185 | KEY must contain just one event type--that is to say, it must be |
186 | a string or vector of length 1." | |
4434d61b RS |
187 | (or (keymapp keymap) |
188 | (signal 'wrong-type-argument (list 'keymapp keymap))) | |
ab375e6c | 189 | (if (> (length key) 1) |
626f67f3 | 190 | (error "multi-event key specified in `define-key-after'")) |
113d28a8 | 191 | (let ((tail keymap) done inserted |
4434d61b RS |
192 | (first (aref key 0))) |
193 | (while (and (not done) tail) | |
194 | ;; Delete any earlier bindings for the same key. | |
195 | (if (eq (car-safe (car (cdr tail))) first) | |
196 | (setcdr tail (cdr (cdr tail)))) | |
197 | ;; When we reach AFTER's binding, insert the new binding after. | |
198 | ;; If we reach an inherited keymap, insert just before that. | |
113d28a8 | 199 | ;; If we reach the end of this keymap, insert at the end. |
4434d61b | 200 | (if (or (eq (car-safe (car tail)) after) |
113d28a8 RS |
201 | (eq (car (cdr tail)) 'keymap) |
202 | (null (cdr tail))) | |
4434d61b | 203 | (progn |
113d28a8 RS |
204 | ;; Stop the scan only if we find a parent keymap. |
205 | ;; Keep going past the inserted element | |
206 | ;; so we can delete any duplications that come later. | |
207 | (if (eq (car (cdr tail)) 'keymap) | |
208 | (setq done t)) | |
209 | ;; Don't insert more than once. | |
210 | (or inserted | |
211 | (setcdr tail (cons (cons (aref key 0) definition) (cdr tail)))) | |
212 | (setq inserted t))) | |
4434d61b RS |
213 | (setq tail (cdr tail))))) |
214 | ||
9a5336ae JB |
215 | (defun keyboard-translate (from to) |
216 | "Translate character FROM to TO at a low level. | |
217 | This function creates a `keyboard-translate-table' if necessary | |
218 | and then modifies one entry in it." | |
219 | (or (arrayp keyboard-translate-table) | |
220 | (setq keyboard-translate-table "")) | |
221 | (if (or (> from (length keyboard-translate-table)) | |
222 | (> to (length keyboard-translate-table))) | |
223 | (progn | |
224 | (let* ((i (length keyboard-translate-table)) | |
f4ebbdbe RS |
225 | (table (concat keyboard-translate-table |
226 | (make-string (- 256 i) 0)))) | |
9a5336ae JB |
227 | (while (< i 256) |
228 | (aset table i i) | |
229 | (setq i (1+ i))) | |
230 | (setq keyboard-translate-table table)))) | |
231 | (aset keyboard-translate-table from to)) | |
232 | ||
233 | \f | |
234 | ;;;; The global keymap tree. | |
235 | ||
236 | ;;; global-map, esc-map, and ctl-x-map have their values set up in | |
237 | ;;; keymap.c; we just give them docstrings here. | |
238 | ||
239 | (defvar global-map nil | |
240 | "Default global keymap mapping Emacs keyboard input into commands. | |
241 | The value is a keymap which is usually (but not necessarily) Emacs's | |
242 | global map.") | |
243 | ||
244 | (defvar esc-map nil | |
245 | "Default keymap for ESC (meta) commands. | |
246 | The normal global definition of the character ESC indirects to this keymap.") | |
247 | ||
248 | (defvar ctl-x-map nil | |
249 | "Default keymap for C-x commands. | |
250 | The normal global definition of the character C-x indirects to this keymap.") | |
251 | ||
252 | (defvar ctl-x-4-map (make-sparse-keymap) | |
253 | "Keymap for subcommands of C-x 4") | |
059184dd | 254 | (defalias 'ctl-x-4-prefix ctl-x-4-map) |
9a5336ae JB |
255 | (define-key ctl-x-map "4" 'ctl-x-4-prefix) |
256 | ||
257 | (defvar ctl-x-5-map (make-sparse-keymap) | |
258 | "Keymap for frame commands.") | |
059184dd | 259 | (defalias 'ctl-x-5-prefix ctl-x-5-map) |
9a5336ae JB |
260 | (define-key ctl-x-map "5" 'ctl-x-5-prefix) |
261 | ||
0f03054a | 262 | \f |
9a5336ae JB |
263 | ;;;; Event manipulation functions. |
264 | ||
da16e648 KH |
265 | ;; The call to `read' is to ensure that the value is computed at load time |
266 | ;; and not compiled into the .elc file. The value is negative on most | |
267 | ;; machines, but not on all! | |
268 | (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@"))) | |
114137b8 | 269 | |
cde6d7e3 RS |
270 | (defun listify-key-sequence (key) |
271 | "Convert a key sequence to a list of events." | |
272 | (if (vectorp key) | |
273 | (append key nil) | |
274 | (mapcar (function (lambda (c) | |
275 | (if (> c 127) | |
114137b8 | 276 | (logxor c listify-key-sequence-1) |
cde6d7e3 RS |
277 | c))) |
278 | (append key nil)))) | |
279 | ||
53e5a4e8 RS |
280 | (defsubst eventp (obj) |
281 | "True if the argument is an event object." | |
282 | (or (integerp obj) | |
283 | (and (symbolp obj) | |
284 | (get obj 'event-symbol-elements)) | |
285 | (and (consp obj) | |
286 | (symbolp (car obj)) | |
287 | (get (car obj) 'event-symbol-elements)))) | |
288 | ||
289 | (defun event-modifiers (event) | |
290 | "Returns a list of symbols representing the modifier keys in event EVENT. | |
291 | The elements of the list may include `meta', `control', | |
32295976 RS |
292 | `shift', `hyper', `super', `alt', `click', `double', `triple', `drag', |
293 | and `down'." | |
53e5a4e8 RS |
294 | (let ((type event)) |
295 | (if (listp type) | |
296 | (setq type (car type))) | |
297 | (if (symbolp type) | |
298 | (cdr (get type 'event-symbol-elements)) | |
299 | (let ((list nil)) | |
da16e648 | 300 | (or (zerop (logand type ?\M-\^@)) |
53e5a4e8 | 301 | (setq list (cons 'meta list))) |
da16e648 | 302 | (or (and (zerop (logand type ?\C-\^@)) |
53e5a4e8 RS |
303 | (>= (logand type 127) 32)) |
304 | (setq list (cons 'control list))) | |
da16e648 | 305 | (or (and (zerop (logand type ?\S-\^@)) |
53e5a4e8 RS |
306 | (= (logand type 255) (downcase (logand type 255)))) |
307 | (setq list (cons 'shift list))) | |
da16e648 | 308 | (or (zerop (logand type ?\H-\^@)) |
53e5a4e8 | 309 | (setq list (cons 'hyper list))) |
da16e648 | 310 | (or (zerop (logand type ?\s-\^@)) |
53e5a4e8 | 311 | (setq list (cons 'super list))) |
da16e648 | 312 | (or (zerop (logand type ?\A-\^@)) |
53e5a4e8 RS |
313 | (setq list (cons 'alt list))) |
314 | list)))) | |
315 | ||
d63de416 RS |
316 | (defun event-basic-type (event) |
317 | "Returns the basic type of the given event (all modifiers removed). | |
318 | The value is an ASCII printing character (not upper case) or a symbol." | |
2b0f4ba5 JB |
319 | (if (consp event) |
320 | (setq event (car event))) | |
d63de416 RS |
321 | (if (symbolp event) |
322 | (car (get event 'event-symbol-elements)) | |
323 | (let ((base (logand event (1- (lsh 1 18))))) | |
324 | (downcase (if (< base 32) (logior base 64) base))))) | |
325 | ||
0f03054a RS |
326 | (defsubst mouse-movement-p (object) |
327 | "Return non-nil if OBJECT is a mouse movement event." | |
328 | (and (consp object) | |
329 | (eq (car object) 'mouse-movement))) | |
330 | ||
331 | (defsubst event-start (event) | |
332 | "Return the starting position of EVENT. | |
333 | If EVENT is a mouse press or a mouse click, this returns the location | |
334 | of the event. | |
335 | If EVENT is a drag, this returns the drag's starting position. | |
336 | The return value is of the form | |
e55c21be | 337 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
338 | The `posn-' functions access elements of such lists." |
339 | (nth 1 event)) | |
340 | ||
341 | (defsubst event-end (event) | |
342 | "Return the ending location of EVENT. EVENT should be a click or drag event. | |
343 | If EVENT is a click event, this function is the same as `event-start'. | |
344 | The return value is of the form | |
e55c21be | 345 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 346 | The `posn-' functions access elements of such lists." |
69b95560 | 347 | (nth (if (consp (nth 2 event)) 2 1) event)) |
0f03054a | 348 | |
32295976 RS |
349 | (defsubst event-click-count (event) |
350 | "Return the multi-click count of EVENT, a click or drag event. | |
351 | The return value is a positive integer." | |
352 | (if (integerp (nth 2 event)) (nth 2 event) 1)) | |
353 | ||
0f03054a RS |
354 | (defsubst posn-window (position) |
355 | "Return the window in POSITION. | |
356 | POSITION should be a list of the form | |
e55c21be | 357 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
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 | |
e55c21be | 364 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 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 | 369 | |
e55c21be RS |
370 | (defsubst posn-x-y (position) |
371 | "Return the x and y coordinates in POSITION. | |
0f03054a | 372 | POSITION should be a list of the form |
e55c21be | 373 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
374 | as returned by the `event-start' and `event-end' functions." |
375 | (nth 2 position)) | |
376 | ||
ed627e08 | 377 | (defun posn-col-row (position) |
dbbcac56 | 378 | "Return the column and row in POSITION, measured in characters. |
e55c21be RS |
379 | POSITION should be a list of the form |
380 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) | |
ed627e08 RS |
381 | as returned by the `event-start' and `event-end' functions. |
382 | For a scroll-bar event, the result column is 0, and the row | |
383 | corresponds to the vertical position of the click in the scroll bar." | |
384 | (let ((pair (nth 2 position)) | |
385 | (window (posn-window position))) | |
dbbcac56 KH |
386 | (if (eq (if (consp (nth 1 position)) |
387 | (car (nth 1 position)) | |
388 | (nth 1 position)) | |
ed627e08 RS |
389 | 'vertical-scroll-bar) |
390 | (cons 0 (scroll-bar-scale pair (1- (window-height window)))) | |
dbbcac56 KH |
391 | (if (eq (if (consp (nth 1 position)) |
392 | (car (nth 1 position)) | |
393 | (nth 1 position)) | |
ed627e08 RS |
394 | 'horizontal-scroll-bar) |
395 | (cons (scroll-bar-scale pair (window-width window)) 0) | |
9ba60df9 RS |
396 | (let* ((frame (if (framep window) window (window-frame window))) |
397 | (x (/ (car pair) (frame-char-width frame))) | |
398 | (y (/ (cdr pair) (frame-char-height frame)))) | |
ed627e08 | 399 | (cons x y)))))) |
e55c21be | 400 | |
0f03054a RS |
401 | (defsubst posn-timestamp (position) |
402 | "Return the timestamp of POSITION. | |
403 | POSITION should be a list of the form | |
e55c21be | 404 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
f415c00c | 405 | as returned by the `event-start' and `event-end' functions." |
0f03054a | 406 | (nth 3 position)) |
9a5336ae | 407 | |
0f03054a | 408 | \f |
9a5336ae JB |
409 | ;;;; Obsolescent names for functions. |
410 | ||
059184dd ER |
411 | (defalias 'dot 'point) |
412 | (defalias 'dot-marker 'point-marker) | |
413 | (defalias 'dot-min 'point-min) | |
414 | (defalias 'dot-max 'point-max) | |
415 | (defalias 'window-dot 'window-point) | |
416 | (defalias 'set-window-dot 'set-window-point) | |
417 | (defalias 'read-input 'read-string) | |
418 | (defalias 'send-string 'process-send-string) | |
419 | (defalias 'send-region 'process-send-region) | |
420 | (defalias 'show-buffer 'set-window-buffer) | |
421 | (defalias 'buffer-flush-undo 'buffer-disable-undo) | |
422 | (defalias 'eval-current-buffer 'eval-buffer) | |
423 | (defalias 'compiled-function-p 'byte-code-function-p) | |
be9b65ac | 424 | |
9a5336ae JB |
425 | ;; Some programs still use this as a function. |
426 | (defun baud-rate () | |
bcacc42c RS |
427 | "Obsolete function returning the value of the `baud-rate' variable. |
428 | Please convert your programs to use the variable `baud-rate' directly." | |
9a5336ae JB |
429 | baud-rate) |
430 | ||
431 | \f | |
432 | ;;;; Alternate names for functions - these are not being phased out. | |
433 | ||
059184dd ER |
434 | (defalias 'string= 'string-equal) |
435 | (defalias 'string< 'string-lessp) | |
436 | (defalias 'move-marker 'set-marker) | |
437 | (defalias 'eql 'eq) | |
438 | (defalias 'not 'null) | |
439 | (defalias 'rplaca 'setcar) | |
440 | (defalias 'rplacd 'setcdr) | |
eb8c3be9 | 441 | (defalias 'beep 'ding) ;preserve lingual purity |
059184dd ER |
442 | (defalias 'indent-to-column 'indent-to) |
443 | (defalias 'backward-delete-char 'delete-backward-char) | |
444 | (defalias 'search-forward-regexp (symbol-function 're-search-forward)) | |
445 | (defalias 'search-backward-regexp (symbol-function 're-search-backward)) | |
446 | (defalias 'int-to-string 'number-to-string) | |
1e0a78a1 | 447 | (defalias 'set-match-data 'store-match-data) |
37f6661a JB |
448 | |
449 | ;;; Should this be an obsolete name? If you decide it should, you get | |
450 | ;;; to go through all the sources and change them. | |
059184dd | 451 | (defalias 'string-to-int 'string-to-number) |
be9b65ac | 452 | \f |
9a5336ae | 453 | ;;;; Hook manipulation functions. |
be9b65ac | 454 | |
ba1cc309 SM |
455 | ;; We used to have this variable so that C code knew how to run hooks. That |
456 | ;; calling convention is made obsolete now the hook running functions are in C. | |
be9b65ac DL |
457 | (defconst run-hooks 'run-hooks |
458 | "Variable by which C primitives find the function `run-hooks'. | |
ba1cc309 | 459 | Don't change it. Don't use it either; use the hook running C primitives.") |
be9b65ac | 460 | |
0e4d378b RS |
461 | (defun make-local-hook (hook) |
462 | "Make the hook HOOK local to the current buffer. | |
463 | When a hook is local, its local and global values | |
464 | work in concert: running the hook actually runs all the hook | |
465 | functions listed in *either* the local value *or* the global value | |
466 | of the hook variable. | |
467 | ||
7dd1926e RS |
468 | This function works by making `t' a member of the buffer-local value, |
469 | which acts as a flag to run the hook functions in the default value as | |
470 | well. This works for all normal hooks, but does not work for most | |
471 | non-normal hooks yet. We will be changing the callers of non-normal | |
472 | hooks so that they can handle localness; this has to be done one by | |
473 | one. | |
474 | ||
475 | This function does nothing if HOOK is already local in the current | |
476 | buffer. | |
0e4d378b RS |
477 | |
478 | Do not use `make-local-variable' to make a hook variable buffer-local." | |
479 | (if (local-variable-p hook) | |
480 | nil | |
481 | (or (boundp hook) (set hook nil)) | |
482 | (make-local-variable hook) | |
483 | (set hook (list t)))) | |
484 | ||
485 | (defun add-hook (hook function &optional append local) | |
32295976 RS |
486 | "Add to the value of HOOK the function FUNCTION. |
487 | FUNCTION is not added if already present. | |
488 | FUNCTION is added (if necessary) at the beginning of the hook list | |
489 | unless the optional argument APPEND is non-nil, in which case | |
490 | FUNCTION is added at the end. | |
491 | ||
0e4d378b RS |
492 | The optional fourth argument, LOCAL, if non-nil, says to modify |
493 | the hook's buffer-local value rather than its default value. | |
494 | This makes no difference if the hook is not buffer-local. | |
495 | To make a hook variable buffer-local, always use | |
496 | `make-local-hook', not `make-local-variable'. | |
497 | ||
32295976 RS |
498 | HOOK should be a symbol, and FUNCTION may be any valid function. If |
499 | HOOK is void, it is first set to nil. If HOOK's value is a single | |
aa09b5ca | 500 | function, it is changed to a list of functions." |
be9b65ac | 501 | (or (boundp hook) (set hook nil)) |
0e4d378b | 502 | (or (default-boundp hook) (set-default hook nil)) |
32295976 RS |
503 | ;; If the hook value is a single function, turn it into a list. |
504 | (let ((old (symbol-value hook))) | |
505 | (if (or (not (listp old)) (eq (car old) 'lambda)) | |
506 | (set hook (list old)))) | |
f4e5bca5 RS |
507 | (if (or local |
508 | ;; Detect the case where make-local-variable was used on a hook | |
509 | ;; and do what we used to do. | |
cd2db344 | 510 | (and (local-variable-if-set-p hook) |
f4e5bca5 | 511 | (not (memq t (symbol-value hook))))) |
0e4d378b RS |
512 | ;; Alter the local value only. |
513 | (or (if (consp function) | |
514 | (member function (symbol-value hook)) | |
515 | (memq function (symbol-value hook))) | |
516 | (set hook | |
517 | (if append | |
518 | (append (symbol-value hook) (list function)) | |
519 | (cons function (symbol-value hook))))) | |
520 | ;; Alter the global value (which is also the only value, | |
521 | ;; if the hook doesn't have a local value). | |
522 | (or (if (consp function) | |
523 | (member function (default-value hook)) | |
524 | (memq function (default-value hook))) | |
525 | (set-default hook | |
526 | (if append | |
527 | (append (default-value hook) (list function)) | |
528 | (cons function (default-value hook))))))) | |
529 | ||
530 | (defun remove-hook (hook function &optional local) | |
24980d16 RS |
531 | "Remove from the value of HOOK the function FUNCTION. |
532 | HOOK should be a symbol, and FUNCTION may be any valid function. If | |
533 | FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the | |
0e4d378b RS |
534 | list of hooks to run in HOOK, then nothing is done. See `add-hook'. |
535 | ||
536 | The optional third argument, LOCAL, if non-nil, says to modify | |
537 | the hook's buffer-local value rather than its default value. | |
538 | This makes no difference if the hook is not buffer-local. | |
539 | To make a hook variable buffer-local, always use | |
540 | `make-local-hook', not `make-local-variable'." | |
24980d16 | 541 | (if (or (not (boundp hook)) ;unbound symbol, or |
0e4d378b | 542 | (not (default-boundp 'hook)) |
24980d16 RS |
543 | (null (symbol-value hook)) ;value is nil, or |
544 | (null function)) ;function is nil, then | |
545 | nil ;Do nothing. | |
f4e5bca5 RS |
546 | (if (or local |
547 | ;; Detect the case where make-local-variable was used on a hook | |
548 | ;; and do what we used to do. | |
549 | (and (local-variable-p hook) | |
550 | (not (memq t (symbol-value hook))))) | |
0e4d378b RS |
551 | (let ((hook-value (symbol-value hook))) |
552 | (if (consp hook-value) | |
553 | (if (member function hook-value) | |
554 | (setq hook-value (delete function (copy-sequence hook-value)))) | |
555 | (if (equal hook-value function) | |
556 | (setq hook-value nil))) | |
557 | (set hook hook-value)) | |
558 | (let ((hook-value (default-value hook))) | |
559 | (if (consp hook-value) | |
560 | (if (member function hook-value) | |
561 | (setq hook-value (delete function (copy-sequence hook-value)))) | |
562 | (if (equal hook-value function) | |
563 | (setq hook-value nil))) | |
564 | (set-default hook hook-value))))) | |
6e3af630 RS |
565 | |
566 | (defun add-to-list (list-var element) | |
8851c1f0 | 567 | "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
9f0b1f09 | 568 | The test for presence of ELEMENT is done with `equal'. |
8851c1f0 RS |
569 | If you want to use `add-to-list' on a variable that is not defined |
570 | until a certain package is loaded, you should put the call to `add-to-list' | |
571 | into a hook function that will be run only after loading the package. | |
572 | `eval-after-load' provides one way to do this. In some cases | |
573 | other hooks, such as major mode hooks, can do the job." | |
6e3af630 RS |
574 | (or (member element (symbol-value list-var)) |
575 | (set list-var (cons element (symbol-value list-var))))) | |
be9b65ac | 576 | \f |
9a5336ae JB |
577 | ;;;; Specifying things to do after certain files are loaded. |
578 | ||
579 | (defun eval-after-load (file form) | |
580 | "Arrange that, if FILE is ever loaded, FORM will be run at that time. | |
581 | This makes or adds to an entry on `after-load-alist'. | |
90914938 | 582 | If FILE is already loaded, evaluate FORM right now. |
12c7071c | 583 | It does nothing if FORM is already on the list for FILE. |
9a5336ae | 584 | FILE should be the name of a library, with no directory name." |
90914938 | 585 | ;; Make sure there is an element for FILE. |
9a5336ae JB |
586 | (or (assoc file after-load-alist) |
587 | (setq after-load-alist (cons (list file) after-load-alist))) | |
90914938 | 588 | ;; Add FORM to the element if it isn't there. |
12c7071c RS |
589 | (let ((elt (assoc file after-load-alist))) |
590 | (or (member form (cdr elt)) | |
90914938 RS |
591 | (progn |
592 | (nconc elt (list form)) | |
593 | ;; If the file has been loaded already, run FORM right away. | |
594 | (and (assoc file load-history) | |
595 | (eval form))))) | |
9a5336ae JB |
596 | form) |
597 | ||
598 | (defun eval-next-after-load (file) | |
599 | "Read the following input sexp, and run it whenever FILE is loaded. | |
600 | This makes or adds to an entry on `after-load-alist'. | |
601 | FILE should be the name of a library, with no directory name." | |
602 | (eval-after-load file (read))) | |
603 | ||
604 | \f | |
605 | ;;;; Input and display facilities. | |
606 | ||
607 | (defun read-quoted-char (&optional prompt) | |
608 | "Like `read-char', except that if the first character read is an octal | |
609 | digit, we read up to two more octal digits and return the character | |
610 | represented by the octal number consisting of those digits. | |
611 | Optional argument PROMPT specifies a string to use to prompt the user." | |
1219a2a4 | 612 | (let ((message-log-max nil) (count 0) (code 0) char) |
9a5336ae JB |
613 | (while (< count 3) |
614 | (let ((inhibit-quit (zerop count)) | |
42e636f0 KH |
615 | ;; Don't let C-h get the help message--only help function keys. |
616 | (help-char nil) | |
617 | (help-form | |
618 | "Type the special character you want to use, | |
619 | or three octal digits representing its character code.")) | |
9a5336ae JB |
620 | (and prompt (message "%s-" prompt)) |
621 | (setq char (read-char)) | |
622 | (if inhibit-quit (setq quit-flag nil))) | |
623 | (cond ((null char)) | |
624 | ((and (<= ?0 char) (<= char ?7)) | |
625 | (setq code (+ (* code 8) (- char ?0)) | |
626 | count (1+ count)) | |
627 | (and prompt (message (setq prompt | |
628 | (format "%s %c" prompt char))))) | |
629 | ((> count 0) | |
630 | (setq unread-command-events (list char) count 259)) | |
631 | (t (setq code char count 259)))) | |
0342b545 | 632 | ;; Turn a meta-character into a character with the 0200 bit set. |
1219a2a4 | 633 | (logior (if (/= (logand code ?\M-\^@) 0) 128 0) |
0342b545 | 634 | (logand 255 code)))) |
9a5336ae JB |
635 | |
636 | (defun force-mode-line-update (&optional all) | |
637 | "Force the mode-line of the current buffer to be redisplayed. | |
7ec2a18c | 638 | With optional non-nil ALL, force redisplay of all mode-lines." |
9a5336ae JB |
639 | (if all (save-excursion (set-buffer (other-buffer)))) |
640 | (set-buffer-modified-p (buffer-modified-p))) | |
641 | ||
be9b65ac DL |
642 | (defun momentary-string-display (string pos &optional exit-char message) |
643 | "Momentarily display STRING in the buffer at POS. | |
644 | Display remains until next character is typed. | |
645 | If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed; | |
646 | otherwise it is then available as input (as a command if nothing else). | |
647 | Display MESSAGE (optional fourth arg) in the echo area. | |
648 | If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there." | |
649 | (or exit-char (setq exit-char ?\ )) | |
650 | (let ((buffer-read-only nil) | |
ca2ec1c5 RS |
651 | ;; Don't modify the undo list at all. |
652 | (buffer-undo-list t) | |
be9b65ac DL |
653 | (modified (buffer-modified-p)) |
654 | (name buffer-file-name) | |
655 | insert-end) | |
656 | (unwind-protect | |
657 | (progn | |
658 | (save-excursion | |
659 | (goto-char pos) | |
660 | ;; defeat file locking... don't try this at home, kids! | |
661 | (setq buffer-file-name nil) | |
662 | (insert-before-markers string) | |
3eec84bf RS |
663 | (setq insert-end (point)) |
664 | ;; If the message end is off screen, recenter now. | |
665 | (if (> (window-end) insert-end) | |
666 | (recenter (/ (window-height) 2))) | |
667 | ;; If that pushed message start off the screen, | |
668 | ;; scroll to start it at the top of the screen. | |
669 | (move-to-window-line 0) | |
670 | (if (> (point) pos) | |
671 | (progn | |
672 | (goto-char pos) | |
673 | (recenter 0)))) | |
be9b65ac DL |
674 | (message (or message "Type %s to continue editing.") |
675 | (single-key-description exit-char)) | |
3547c855 | 676 | (let ((char (read-event))) |
be9b65ac | 677 | (or (eq char exit-char) |
dbc4e1c1 | 678 | (setq unread-command-events (list char))))) |
be9b65ac DL |
679 | (if insert-end |
680 | (save-excursion | |
681 | (delete-region pos insert-end))) | |
682 | (setq buffer-file-name name) | |
683 | (set-buffer-modified-p modified)))) | |
684 | ||
9a5336ae JB |
685 | \f |
686 | ;;;; Miscellanea. | |
687 | ||
448b61c9 RS |
688 | ;; A number of major modes set this locally. |
689 | ;; Give it a global value to avoid compiler warnings. | |
690 | (defvar font-lock-defaults nil) | |
691 | ||
692 | ;; Avoid compiler warnings about this variable, | |
693 | ;; which has a special meaning on certain system types. | |
694 | (defvar buffer-file-type nil | |
695 | "Non-nil if the visited file is a binary file. | |
696 | This variable is meaningful on MS-DOG and Windows NT. | |
697 | On those systems, it is automatically local in every buffer. | |
698 | On other systems, this variable is normally always nil.") | |
699 | ||
f9269e19 RS |
700 | (defun ignore (&rest ignore) |
701 | "Do nothing and return nil. | |
702 | This function accepts any number of arguments, but ignores them." | |
c0f1a4f6 | 703 | (interactive) |
9a5336ae JB |
704 | nil) |
705 | ||
706 | (defun error (&rest args) | |
aa308ce2 RS |
707 | "Signal an error, making error message by passing all args to `format'. |
708 | In Emacs, the convention is that error messages start with a capital | |
709 | letter but *do not* end with a period. Please follow this convention | |
710 | for the sake of consistency." | |
9a5336ae JB |
711 | (while t |
712 | (signal 'error (list (apply 'format args))))) | |
713 | ||
cef7ae6e | 714 | (defalias 'user-original-login-name 'user-login-name) |
9a5336ae | 715 | |
be9b65ac DL |
716 | (defun start-process-shell-command (name buffer &rest args) |
717 | "Start a program in a subprocess. Return the process object for it. | |
718 | Args are NAME BUFFER COMMAND &rest COMMAND-ARGS. | |
719 | NAME is name for process. It is modified if necessary to make it unique. | |
720 | BUFFER is the buffer or (buffer-name) to associate with the process. | |
721 | Process output goes at end of that buffer, unless you specify | |
722 | an output stream or filter function to handle the output. | |
723 | BUFFER may be also nil, meaning that this process is not associated | |
724 | with any buffer | |
725 | Third arg is command name, the name of a shell command. | |
726 | Remaining arguments are the arguments for the command. | |
4f1d6310 | 727 | Wildcards and redirection are handled as usual in the shell." |
a247bf21 KH |
728 | (cond |
729 | ((eq system-type 'vax-vms) | |
730 | (apply 'start-process name buffer args)) | |
b59f6d7a RS |
731 | ;; We used to use `exec' to replace the shell with the command, |
732 | ;; but that failed to handle (...) and semicolon, etc. | |
a247bf21 KH |
733 | (t |
734 | (start-process name buffer shell-file-name shell-command-switch | |
b59f6d7a | 735 | (mapconcat 'identity args " "))))) |
be9b65ac | 736 | |
9a5336ae JB |
737 | (defmacro save-match-data (&rest body) |
738 | "Execute the BODY forms, restoring the global value of the match data." | |
739 | (let ((original (make-symbol "match-data"))) | |
993713ce SM |
740 | (list 'let (list (list original '(match-data))) |
741 | (list 'unwind-protect | |
742 | (cons 'progn body) | |
743 | (list 'store-match-data original))))) | |
744 | ||
cd323f89 | 745 | (defun match-string (num &optional string) |
993713ce SM |
746 | "Return string of text matched by last search. |
747 | NUM specifies which parenthesized expression in the last regexp. | |
748 | Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
749 | Zero means the entire text matched by the whole regexp or whole string. | |
750 | STRING should be given if the last search was by `string-match' on STRING." | |
cd323f89 SM |
751 | (if (match-beginning num) |
752 | (if string | |
753 | (substring string (match-beginning num) (match-end num)) | |
754 | (buffer-substring (match-beginning num) (match-end num))))) | |
58f950b4 | 755 | |
8af7df60 RS |
756 | (defun shell-quote-argument (argument) |
757 | "Quote an argument for passing as argument to an inferior shell." | |
c1c74b43 RS |
758 | (if (eq system-type 'ms-dos) |
759 | ;; MS-DOS shells don't have quoting, so don't do any. | |
760 | argument | |
761 | (if (eq system-type 'windows-nt) | |
762 | (concat "\"" argument "\"") | |
763 | ;; Quote everything except POSIX filename characters. | |
764 | ;; This should be safe enough even for really weird shells. | |
765 | (let ((result "") (start 0) end) | |
766 | (while (string-match "[^-0-9a-zA-Z_./]" argument start) | |
767 | (setq end (match-beginning 0) | |
768 | result (concat result (substring argument start end) | |
769 | "\\" (substring argument end (1+ end))) | |
770 | start (1+ end))) | |
771 | (concat result (substring argument start)))))) | |
8af7df60 | 772 | |
297d863b | 773 | (defun make-syntax-table (&optional oldtable) |
984f718a RS |
774 | "Return a new syntax table. |
775 | It inherits all letters and control characters from the standard | |
776 | syntax table; other characters are copied from the standard syntax table." | |
297d863b KH |
777 | (if oldtable |
778 | (copy-syntax-table oldtable) | |
779 | (let ((table (copy-syntax-table)) | |
780 | i) | |
781 | (setq i 0) | |
782 | (while (<= i 31) | |
a6889c57 | 783 | (aset table i nil) |
297d863b KH |
784 | (setq i (1+ i))) |
785 | (setq i ?A) | |
786 | (while (<= i ?Z) | |
a6889c57 | 787 | (aset table i nil) |
297d863b KH |
788 | (setq i (1+ i))) |
789 | (setq i ?a) | |
790 | (while (<= i ?z) | |
a6889c57 | 791 | (aset table i nil) |
297d863b KH |
792 | (setq i (1+ i))) |
793 | (setq i 128) | |
794 | (while (<= i 255) | |
a6889c57 | 795 | (aset table i nil) |
297d863b KH |
796 | (setq i (1+ i))) |
797 | table))) | |
baed0109 RS |
798 | \f |
799 | (defun global-set-key (key command) | |
800 | "Give KEY a global binding as COMMAND. | |
801 | COMMAND is a symbol naming an interactively-callable function. | |
802 | KEY is a key sequence (a string or vector of characters or event types). | |
803 | Non-ASCII characters with codes above 127 (such as ISO Latin-1) | |
804 | can be included if you use a vector. | |
805 | Note that if KEY has a local binding in the current buffer | |
806 | that local binding will continue to shadow any global binding." | |
807 | (interactive "KSet key globally: \nCSet key %s to command: ") | |
808 | (or (vectorp key) (stringp key) | |
809 | (signal 'wrong-type-argument (list 'arrayp key))) | |
810 | (define-key (current-global-map) key command) | |
811 | nil) | |
812 | ||
813 | (defun local-set-key (key command) | |
814 | "Give KEY a local binding as COMMAND. | |
815 | COMMAND is a symbol naming an interactively-callable function. | |
816 | KEY is a key sequence (a string or vector of characters or event types). | |
817 | Non-ASCII characters with codes above 127 (such as ISO Latin-1) | |
818 | can be included if you use a vector. | |
819 | The binding goes in the current buffer's local map, | |
820 | which in most cases is shared with all other buffers in the same major mode." | |
821 | (interactive "KSet key locally: \nCSet key %s locally to command: ") | |
822 | (let ((map (current-local-map))) | |
823 | (or map | |
824 | (use-local-map (setq map (make-sparse-keymap)))) | |
825 | (or (vectorp key) (stringp key) | |
826 | (signal 'wrong-type-argument (list 'arrayp key))) | |
827 | (define-key map key command)) | |
828 | nil) | |
984f718a | 829 | |
baed0109 RS |
830 | (defun global-unset-key (key) |
831 | "Remove global binding of KEY. | |
832 | KEY is a string representing a sequence of keystrokes." | |
833 | (interactive "kUnset key globally: ") | |
834 | (global-set-key key nil)) | |
835 | ||
db2474b8 | 836 | (defun local-unset-key (key) |
baed0109 RS |
837 | "Remove local binding of KEY. |
838 | KEY is a string representing a sequence of keystrokes." | |
839 | (interactive "kUnset key locally: ") | |
840 | (if (current-local-map) | |
db2474b8 | 841 | (local-set-key key nil)) |
baed0109 RS |
842 | nil) |
843 | \f | |
4809d0dd KH |
844 | ;; We put this here instead of in frame.el so that it's defined even on |
845 | ;; systems where frame.el isn't loaded. | |
846 | (defun frame-configuration-p (object) | |
847 | "Return non-nil if OBJECT seems to be a frame configuration. | |
848 | Any list whose car is `frame-configuration' is assumed to be a frame | |
849 | configuration." | |
850 | (and (consp object) | |
851 | (eq (car object) 'frame-configuration))) | |
852 | ||
9a5336ae JB |
853 | ;; now in fns.c |
854 | ;(defun nth (n list) | |
855 | ; "Returns the Nth element of LIST. | |
856 | ;N counts from zero. If LIST is not that long, nil is returned." | |
857 | ; (car (nthcdr n list))) | |
858 | ; | |
859 | ;(defun copy-alist (alist) | |
860 | ; "Return a copy of ALIST. | |
861 | ;This is a new alist which represents the same mapping | |
862 | ;from objects to objects, but does not share the alist structure with ALIST. | |
863 | ;The objects mapped (cars and cdrs of elements of the alist) | |
864 | ;are shared, however." | |
865 | ; (setq alist (copy-sequence alist)) | |
866 | ; (let ((tail alist)) | |
867 | ; (while tail | |
868 | ; (if (consp (car tail)) | |
869 | ; (setcar tail (cons (car (car tail)) (cdr (car tail))))) | |
870 | ; (setq tail (cdr tail)))) | |
871 | ; alist) | |
630cc463 ER |
872 | |
873 | ;;; subr.el ends here | |
9a5336ae | 874 |