Commit | Line | Data |
---|---|---|
c88ab9ce | 1 | ;;; subr.el --- basic lisp subroutines for Emacs |
630cc463 | 2 | |
b578f267 | 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 | |
b578f267 EN |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
be9b65ac | 21 | |
630cc463 | 22 | ;;; Code: |
77a5664f RS |
23 | (defvar custom-declare-variable-list nil |
24 | "Record `defcustom' calls made before `custom.el' is loaded to handle them. | |
25 | Each element of this list holds the arguments to one call to `defcustom'.") | |
26 | ||
68e3e5f5 | 27 | ;; Use this, rather than defcustom, in subr.el and other files loaded |
77a5664f RS |
28 | ;; before custom.el. |
29 | (defun custom-declare-variable-early (&rest arguments) | |
30 | (setq custom-declare-variable-list | |
31 | (cons arguments custom-declare-variable-list))) | |
9a5336ae JB |
32 | \f |
33 | ;;;; Lisp language features. | |
34 | ||
35 | (defmacro lambda (&rest cdr) | |
36 | "Return a lambda expression. | |
37 | A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is | |
38 | self-quoting; the result of evaluating the lambda expression is the | |
39 | expression itself. The lambda expression may then be treated as a | |
bec0d7f9 RS |
40 | function, i.e., stored as the function value of a symbol, passed to |
41 | funcall or mapcar, etc. | |
42 | ||
9a5336ae | 43 | ARGS should take the same form as an argument list for a `defun'. |
8fd68088 RS |
44 | DOCSTRING is an optional documentation string. |
45 | If present, it should describe how to call the function. | |
46 | But documentation strings are usually not useful in nameless functions. | |
9a5336ae JB |
47 | INTERACTIVE should be a call to the function `interactive', which see. |
48 | It may also be omitted. | |
49 | BODY should be a list of lisp expressions." | |
50 | ;; Note that this definition should not use backquotes; subr.el should not | |
51 | ;; depend on backquote.el. | |
52 | (list 'function (cons 'lambda cdr))) | |
53 | ||
1be152fc | 54 | (defmacro push (newelt listname) |
fa65505b | 55 | "Add NEWELT to the list stored in the symbol LISTNAME. |
1be152fc | 56 | This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)). |
d270117a | 57 | LISTNAME must be a symbol." |
22d85d00 DL |
58 | (list 'setq listname |
59 | (list 'cons newelt listname))) | |
d270117a RS |
60 | |
61 | (defmacro pop (listname) | |
62 | "Return the first element of LISTNAME's value, and remove it from the list. | |
63 | LISTNAME must be a symbol whose value is a list. | |
64 | If the value is nil, `pop' returns nil but does not actually | |
65 | change the list." | |
66 | (list 'prog1 (list 'car listname) | |
67 | (list 'setq listname (list 'cdr listname)))) | |
68 | ||
debff3c3 RS |
69 | (defmacro when (cond &rest body) |
70 | "(when COND BODY...): if COND yields non-nil, do BODY, else return nil." | |
71 | (list 'if cond (cons 'progn body))) | |
11d431ad KH |
72 | (put 'when 'lisp-indent-function 1) |
73 | (put 'when 'edebug-form-spec '(&rest form)) | |
9a5336ae | 74 | |
debff3c3 RS |
75 | (defmacro unless (cond &rest body) |
76 | "(unless COND BODY...): if COND yields nil, do BODY, else return nil." | |
77 | (cons 'if (cons cond (cons nil body)))) | |
11d431ad KH |
78 | (put 'unless 'lisp-indent-function 1) |
79 | (put 'unless 'edebug-form-spec '(&rest form)) | |
d370591d RS |
80 | |
81 | (defsubst caar (x) | |
82 | "Return the car of the car of X." | |
83 | (car (car x))) | |
84 | ||
85 | (defsubst cadr (x) | |
86 | "Return the car of the cdr of X." | |
87 | (car (cdr x))) | |
88 | ||
89 | (defsubst cdar (x) | |
90 | "Return the cdr of the car of X." | |
91 | (cdr (car x))) | |
92 | ||
93 | (defsubst cddr (x) | |
94 | "Return the cdr of the cdr of X." | |
95 | (cdr (cdr x))) | |
e8c32c99 | 96 | |
369fba5f RS |
97 | (defun last (x &optional n) |
98 | "Return the last link of the list X. Its car is the last element. | |
99 | If X is nil, return nil. | |
100 | If N is non-nil, return the Nth-to-last link of X. | |
101 | If N is bigger than the length of X, return X." | |
102 | (if n | |
103 | (let ((m 0) (p x)) | |
104 | (while (consp p) | |
105 | (setq m (1+ m) p (cdr p))) | |
106 | (if (<= n 0) p | |
107 | (if (< n m) (nthcdr (- m n) x) x))) | |
108 | (while (cdr x) | |
109 | (setq x (cdr x))) | |
110 | x)) | |
526d204e | 111 | |
8a288450 RS |
112 | (defun assoc-default (key alist &optional test default) |
113 | "Find object KEY in a pseudo-alist ALIST. | |
114 | ALIST is a list of conses or objects. Each element (or the element's car, | |
115 | if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY). | |
116 | If that is non-nil, the element matches; | |
117 | then `assoc-default' returns the element's cdr, if it is a cons, | |
526d204e | 118 | or DEFAULT if the element is not a cons. |
8a288450 RS |
119 | |
120 | If no element matches, the value is nil. | |
121 | If TEST is omitted or nil, `equal' is used." | |
122 | (let (found (tail alist) value) | |
123 | (while (and tail (not found)) | |
124 | (let ((elt (car tail))) | |
125 | (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key) | |
126 | (setq found t value (if (consp elt) (cdr elt) default)))) | |
127 | (setq tail (cdr tail))) | |
128 | value)) | |
98aae5f6 KH |
129 | |
130 | (defun assoc-ignore-case (key alist) | |
131 | "Like `assoc', but ignores differences in case and text representation. | |
132 | KEY must be a string. Upper-case and lower-case letters are treated as equal. | |
133 | Unibyte strings are converted to multibyte for comparison." | |
134 | (let (element) | |
135 | (while (and alist (not element)) | |
136 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t)) | |
137 | (setq element (car alist))) | |
138 | (setq alist (cdr alist))) | |
139 | element)) | |
140 | ||
141 | (defun assoc-ignore-representation (key alist) | |
142 | "Like `assoc', but ignores differences in text representation. | |
143 | KEY must be a string. | |
144 | Unibyte strings are converted to multibyte for comparison." | |
145 | (let (element) | |
146 | (while (and alist (not element)) | |
147 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil)) | |
148 | (setq element (car alist))) | |
149 | (setq alist (cdr alist))) | |
150 | element)) | |
9a5336ae | 151 | \f |
9a5336ae | 152 | ;;;; Keymap support. |
be9b65ac DL |
153 | |
154 | (defun undefined () | |
155 | (interactive) | |
156 | (ding)) | |
157 | ||
158 | ;Prevent the \{...} documentation construct | |
159 | ;from mentioning keys that run this command. | |
160 | (put 'undefined 'suppress-keymap t) | |
161 | ||
162 | (defun suppress-keymap (map &optional nodigits) | |
163 | "Make MAP override all normally self-inserting keys to be undefined. | |
164 | Normally, as an exception, digits and minus-sign are set to make prefix args, | |
165 | but optional second arg NODIGITS non-nil treats them like other chars." | |
80e7b471 | 166 | (substitute-key-definition 'self-insert-command 'undefined map global-map) |
be9b65ac DL |
167 | (or nodigits |
168 | (let (loop) | |
169 | (define-key map "-" 'negative-argument) | |
170 | ;; Make plain numbers do numeric args. | |
171 | (setq loop ?0) | |
172 | (while (<= loop ?9) | |
173 | (define-key map (char-to-string loop) 'digit-argument) | |
174 | (setq loop (1+ loop)))))) | |
175 | ||
be9b65ac DL |
176 | ;Moved to keymap.c |
177 | ;(defun copy-keymap (keymap) | |
178 | ; "Return a copy of KEYMAP" | |
179 | ; (while (not (keymapp keymap)) | |
180 | ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap)))) | |
181 | ; (if (vectorp keymap) | |
182 | ; (copy-sequence keymap) | |
183 | ; (copy-alist keymap))) | |
184 | ||
f14dbba7 KH |
185 | (defvar key-substitution-in-progress nil |
186 | "Used internally by substitute-key-definition.") | |
187 | ||
7f2c2edd | 188 | (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix) |
be9b65ac DL |
189 | "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF. |
190 | In other words, OLDDEF is replaced with NEWDEF where ever it appears. | |
7f2c2edd RS |
191 | If optional fourth argument OLDMAP is specified, we redefine |
192 | in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP." | |
193 | (or prefix (setq prefix "")) | |
194 | (let* ((scan (or oldmap keymap)) | |
195 | (vec1 (vector nil)) | |
f14dbba7 KH |
196 | (prefix1 (vconcat prefix vec1)) |
197 | (key-substitution-in-progress | |
198 | (cons scan key-substitution-in-progress))) | |
7f2c2edd RS |
199 | ;; Scan OLDMAP, finding each char or event-symbol that |
200 | ;; has any definition, and act on it with hack-key. | |
201 | (while (consp scan) | |
202 | (if (consp (car scan)) | |
203 | (let ((char (car (car scan))) | |
204 | (defn (cdr (car scan)))) | |
205 | ;; The inside of this let duplicates exactly | |
206 | ;; the inside of the following let that handles array elements. | |
207 | (aset vec1 0 char) | |
208 | (aset prefix1 (length prefix) char) | |
44d798af | 209 | (let (inner-def skipped) |
7f2c2edd RS |
210 | ;; Skip past menu-prompt. |
211 | (while (stringp (car-safe defn)) | |
44d798af | 212 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 213 | (setq defn (cdr defn))) |
e025dddf RS |
214 | ;; Skip past cached key-equivalence data for menu items. |
215 | (and (consp defn) (consp (car defn)) | |
216 | (setq defn (cdr defn))) | |
7f2c2edd | 217 | (setq inner-def defn) |
e025dddf | 218 | ;; Look past a symbol that names a keymap. |
7f2c2edd RS |
219 | (while (and (symbolp inner-def) |
220 | (fboundp inner-def)) | |
221 | (setq inner-def (symbol-function inner-def))) | |
328a37ec RS |
222 | (if (or (eq defn olddef) |
223 | ;; Compare with equal if definition is a key sequence. | |
224 | ;; That is useful for operating on function-key-map. | |
225 | (and (or (stringp defn) (vectorp defn)) | |
226 | (equal defn olddef))) | |
44d798af | 227 | (define-key keymap prefix1 (nconc (nreverse skipped) newdef)) |
f14dbba7 | 228 | (if (and (keymapp defn) |
350b7567 RS |
229 | ;; Avoid recursively scanning |
230 | ;; where KEYMAP does not have a submap. | |
afd9831b RS |
231 | (let ((elt (lookup-key keymap prefix1))) |
232 | (or (null elt) | |
233 | (keymapp elt))) | |
350b7567 | 234 | ;; Avoid recursively rescanning keymap being scanned. |
f14dbba7 KH |
235 | (not (memq inner-def |
236 | key-substitution-in-progress))) | |
e025dddf RS |
237 | ;; If this one isn't being scanned already, |
238 | ;; scan it now. | |
7f2c2edd RS |
239 | (substitute-key-definition olddef newdef keymap |
240 | inner-def | |
241 | prefix1))))) | |
916cc49f | 242 | (if (vectorp (car scan)) |
7f2c2edd RS |
243 | (let* ((array (car scan)) |
244 | (len (length array)) | |
245 | (i 0)) | |
246 | (while (< i len) | |
247 | (let ((char i) (defn (aref array i))) | |
248 | ;; The inside of this let duplicates exactly | |
249 | ;; the inside of the previous let. | |
250 | (aset vec1 0 char) | |
251 | (aset prefix1 (length prefix) char) | |
44d798af | 252 | (let (inner-def skipped) |
7f2c2edd RS |
253 | ;; Skip past menu-prompt. |
254 | (while (stringp (car-safe defn)) | |
44d798af | 255 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 256 | (setq defn (cdr defn))) |
e025dddf RS |
257 | (and (consp defn) (consp (car defn)) |
258 | (setq defn (cdr defn))) | |
7f2c2edd RS |
259 | (setq inner-def defn) |
260 | (while (and (symbolp inner-def) | |
261 | (fboundp inner-def)) | |
262 | (setq inner-def (symbol-function inner-def))) | |
328a37ec RS |
263 | (if (or (eq defn olddef) |
264 | (and (or (stringp defn) (vectorp defn)) | |
265 | (equal defn olddef))) | |
44d798af RS |
266 | (define-key keymap prefix1 |
267 | (nconc (nreverse skipped) newdef)) | |
f14dbba7 | 268 | (if (and (keymapp defn) |
afd9831b RS |
269 | (let ((elt (lookup-key keymap prefix1))) |
270 | (or (null elt) | |
271 | (keymapp elt))) | |
f14dbba7 KH |
272 | (not (memq inner-def |
273 | key-substitution-in-progress))) | |
7f2c2edd RS |
274 | (substitute-key-definition olddef newdef keymap |
275 | inner-def | |
276 | prefix1))))) | |
97fd9abf RS |
277 | (setq i (1+ i)))) |
278 | (if (char-table-p (car scan)) | |
279 | (map-char-table | |
280 | (function (lambda (char defn) | |
281 | (let () | |
282 | ;; The inside of this let duplicates exactly | |
283 | ;; the inside of the previous let, | |
284 | ;; except that it uses set-char-table-range | |
285 | ;; instead of define-key. | |
286 | (aset vec1 0 char) | |
287 | (aset prefix1 (length prefix) char) | |
288 | (let (inner-def skipped) | |
289 | ;; Skip past menu-prompt. | |
290 | (while (stringp (car-safe defn)) | |
291 | (setq skipped (cons (car defn) skipped)) | |
292 | (setq defn (cdr defn))) | |
293 | (and (consp defn) (consp (car defn)) | |
294 | (setq defn (cdr defn))) | |
295 | (setq inner-def defn) | |
296 | (while (and (symbolp inner-def) | |
297 | (fboundp inner-def)) | |
298 | (setq inner-def (symbol-function inner-def))) | |
299 | (if (or (eq defn olddef) | |
300 | (and (or (stringp defn) (vectorp defn)) | |
301 | (equal defn olddef))) | |
9a5114ac RS |
302 | (define-key keymap prefix1 |
303 | (nconc (nreverse skipped) newdef)) | |
97fd9abf RS |
304 | (if (and (keymapp defn) |
305 | (let ((elt (lookup-key keymap prefix1))) | |
306 | (or (null elt) | |
307 | (keymapp elt))) | |
308 | (not (memq inner-def | |
309 | key-substitution-in-progress))) | |
310 | (substitute-key-definition olddef newdef keymap | |
311 | inner-def | |
312 | prefix1))))))) | |
313 | (car scan))))) | |
7f2c2edd | 314 | (setq scan (cdr scan))))) |
9a5336ae | 315 | |
06ae9cf2 | 316 | (defun define-key-after (keymap key definition after) |
4434d61b RS |
317 | "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding. |
318 | This is like `define-key' except that the binding for KEY is placed | |
319 | just after the binding for the event AFTER, instead of at the beginning | |
c34a9d34 RS |
320 | of the map. Note that AFTER must be an event type (like KEY), NOT a command |
321 | \(like DEFINITION). | |
322 | ||
323 | If AFTER is t, the new binding goes at the end of the keymap. | |
324 | ||
9ed287b0 | 325 | KEY must contain just one event type--that is to say, it must be |
c34a9d34 RS |
326 | a string or vector of length 1. |
327 | ||
328 | The order of bindings in a keymap matters when it is used as a menu." | |
329 | ||
4434d61b RS |
330 | (or (keymapp keymap) |
331 | (signal 'wrong-type-argument (list 'keymapp keymap))) | |
ab375e6c | 332 | (if (> (length key) 1) |
626f67f3 | 333 | (error "multi-event key specified in `define-key-after'")) |
113d28a8 | 334 | (let ((tail keymap) done inserted |
4434d61b RS |
335 | (first (aref key 0))) |
336 | (while (and (not done) tail) | |
337 | ;; Delete any earlier bindings for the same key. | |
338 | (if (eq (car-safe (car (cdr tail))) first) | |
339 | (setcdr tail (cdr (cdr tail)))) | |
340 | ;; When we reach AFTER's binding, insert the new binding after. | |
341 | ;; If we reach an inherited keymap, insert just before that. | |
113d28a8 | 342 | ;; If we reach the end of this keymap, insert at the end. |
c34a9d34 RS |
343 | (if (or (and (eq (car-safe (car tail)) after) |
344 | (not (eq after t))) | |
113d28a8 RS |
345 | (eq (car (cdr tail)) 'keymap) |
346 | (null (cdr tail))) | |
4434d61b | 347 | (progn |
113d28a8 RS |
348 | ;; Stop the scan only if we find a parent keymap. |
349 | ;; Keep going past the inserted element | |
350 | ;; so we can delete any duplications that come later. | |
351 | (if (eq (car (cdr tail)) 'keymap) | |
352 | (setq done t)) | |
353 | ;; Don't insert more than once. | |
354 | (or inserted | |
355 | (setcdr tail (cons (cons (aref key 0) definition) (cdr tail)))) | |
356 | (setq inserted t))) | |
4434d61b RS |
357 | (setq tail (cdr tail))))) |
358 | ||
d128fe85 RS |
359 | (defmacro kbd (keys) |
360 | "Convert KEYS to the internal Emacs key representation. | |
361 | KEYS should be a string constant in the format used for | |
362 | saving keyboard macros (see `insert-kbd-macro')." | |
363 | (read-kbd-macro keys)) | |
364 | ||
8bed5e3d RS |
365 | (put 'keyboard-translate-table 'char-table-extra-slots 0) |
366 | ||
9a5336ae JB |
367 | (defun keyboard-translate (from to) |
368 | "Translate character FROM to TO at a low level. | |
369 | This function creates a `keyboard-translate-table' if necessary | |
370 | and then modifies one entry in it." | |
8bed5e3d RS |
371 | (or (char-table-p keyboard-translate-table) |
372 | (setq keyboard-translate-table | |
373 | (make-char-table 'keyboard-translate-table nil))) | |
9a5336ae JB |
374 | (aset keyboard-translate-table from to)) |
375 | ||
376 | \f | |
377 | ;;;; The global keymap tree. | |
378 | ||
379 | ;;; global-map, esc-map, and ctl-x-map have their values set up in | |
380 | ;;; keymap.c; we just give them docstrings here. | |
381 | ||
382 | (defvar global-map nil | |
383 | "Default global keymap mapping Emacs keyboard input into commands. | |
384 | The value is a keymap which is usually (but not necessarily) Emacs's | |
385 | global map.") | |
386 | ||
387 | (defvar esc-map nil | |
388 | "Default keymap for ESC (meta) commands. | |
389 | The normal global definition of the character ESC indirects to this keymap.") | |
390 | ||
391 | (defvar ctl-x-map nil | |
392 | "Default keymap for C-x commands. | |
393 | The normal global definition of the character C-x indirects to this keymap.") | |
394 | ||
395 | (defvar ctl-x-4-map (make-sparse-keymap) | |
396 | "Keymap for subcommands of C-x 4") | |
059184dd | 397 | (defalias 'ctl-x-4-prefix ctl-x-4-map) |
9a5336ae JB |
398 | (define-key ctl-x-map "4" 'ctl-x-4-prefix) |
399 | ||
400 | (defvar ctl-x-5-map (make-sparse-keymap) | |
401 | "Keymap for frame commands.") | |
059184dd | 402 | (defalias 'ctl-x-5-prefix ctl-x-5-map) |
9a5336ae JB |
403 | (define-key ctl-x-map "5" 'ctl-x-5-prefix) |
404 | ||
0f03054a | 405 | \f |
9a5336ae JB |
406 | ;;;; Event manipulation functions. |
407 | ||
da16e648 KH |
408 | ;; The call to `read' is to ensure that the value is computed at load time |
409 | ;; and not compiled into the .elc file. The value is negative on most | |
410 | ;; machines, but not on all! | |
411 | (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@"))) | |
114137b8 | 412 | |
cde6d7e3 RS |
413 | (defun listify-key-sequence (key) |
414 | "Convert a key sequence to a list of events." | |
415 | (if (vectorp key) | |
416 | (append key nil) | |
417 | (mapcar (function (lambda (c) | |
418 | (if (> c 127) | |
114137b8 | 419 | (logxor c listify-key-sequence-1) |
cde6d7e3 RS |
420 | c))) |
421 | (append key nil)))) | |
422 | ||
53e5a4e8 RS |
423 | (defsubst eventp (obj) |
424 | "True if the argument is an event object." | |
425 | (or (integerp obj) | |
426 | (and (symbolp obj) | |
427 | (get obj 'event-symbol-elements)) | |
428 | (and (consp obj) | |
429 | (symbolp (car obj)) | |
430 | (get (car obj) 'event-symbol-elements)))) | |
431 | ||
432 | (defun event-modifiers (event) | |
433 | "Returns a list of symbols representing the modifier keys in event EVENT. | |
434 | The elements of the list may include `meta', `control', | |
32295976 RS |
435 | `shift', `hyper', `super', `alt', `click', `double', `triple', `drag', |
436 | and `down'." | |
53e5a4e8 RS |
437 | (let ((type event)) |
438 | (if (listp type) | |
439 | (setq type (car type))) | |
440 | (if (symbolp type) | |
441 | (cdr (get type 'event-symbol-elements)) | |
442 | (let ((list nil)) | |
da16e648 | 443 | (or (zerop (logand type ?\M-\^@)) |
53e5a4e8 | 444 | (setq list (cons 'meta list))) |
da16e648 | 445 | (or (and (zerop (logand type ?\C-\^@)) |
53e5a4e8 RS |
446 | (>= (logand type 127) 32)) |
447 | (setq list (cons 'control list))) | |
da16e648 | 448 | (or (and (zerop (logand type ?\S-\^@)) |
53e5a4e8 RS |
449 | (= (logand type 255) (downcase (logand type 255)))) |
450 | (setq list (cons 'shift list))) | |
da16e648 | 451 | (or (zerop (logand type ?\H-\^@)) |
53e5a4e8 | 452 | (setq list (cons 'hyper list))) |
da16e648 | 453 | (or (zerop (logand type ?\s-\^@)) |
53e5a4e8 | 454 | (setq list (cons 'super list))) |
da16e648 | 455 | (or (zerop (logand type ?\A-\^@)) |
53e5a4e8 RS |
456 | (setq list (cons 'alt list))) |
457 | list)))) | |
458 | ||
d63de416 RS |
459 | (defun event-basic-type (event) |
460 | "Returns the basic type of the given event (all modifiers removed). | |
461 | The value is an ASCII printing character (not upper case) or a symbol." | |
2b0f4ba5 JB |
462 | (if (consp event) |
463 | (setq event (car event))) | |
d63de416 RS |
464 | (if (symbolp event) |
465 | (car (get event 'event-symbol-elements)) | |
466 | (let ((base (logand event (1- (lsh 1 18))))) | |
467 | (downcase (if (< base 32) (logior base 64) base))))) | |
468 | ||
0f03054a RS |
469 | (defsubst mouse-movement-p (object) |
470 | "Return non-nil if OBJECT is a mouse movement event." | |
471 | (and (consp object) | |
472 | (eq (car object) 'mouse-movement))) | |
473 | ||
474 | (defsubst event-start (event) | |
475 | "Return the starting position of EVENT. | |
476 | If EVENT is a mouse press or a mouse click, this returns the location | |
477 | of the event. | |
478 | If EVENT is a drag, this returns the drag's starting position. | |
479 | The return value is of the form | |
e55c21be | 480 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
481 | The `posn-' functions access elements of such lists." |
482 | (nth 1 event)) | |
483 | ||
484 | (defsubst event-end (event) | |
485 | "Return the ending location of EVENT. EVENT should be a click or drag event. | |
486 | If EVENT is a click event, this function is the same as `event-start'. | |
487 | The return value is of the form | |
e55c21be | 488 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 489 | The `posn-' functions access elements of such lists." |
69b95560 | 490 | (nth (if (consp (nth 2 event)) 2 1) event)) |
0f03054a | 491 | |
32295976 RS |
492 | (defsubst event-click-count (event) |
493 | "Return the multi-click count of EVENT, a click or drag event. | |
494 | The return value is a positive integer." | |
495 | (if (integerp (nth 2 event)) (nth 2 event) 1)) | |
496 | ||
0f03054a RS |
497 | (defsubst posn-window (position) |
498 | "Return the window in POSITION. | |
499 | POSITION should be a list of the form | |
e55c21be | 500 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
501 | as returned by the `event-start' and `event-end' functions." |
502 | (nth 0 position)) | |
503 | ||
504 | (defsubst posn-point (position) | |
505 | "Return the buffer location in POSITION. | |
506 | POSITION should be a list of the form | |
e55c21be | 507 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 508 | as returned by the `event-start' and `event-end' functions." |
15db4e0e JB |
509 | (if (consp (nth 1 position)) |
510 | (car (nth 1 position)) | |
511 | (nth 1 position))) | |
0f03054a | 512 | |
e55c21be RS |
513 | (defsubst posn-x-y (position) |
514 | "Return the x and y coordinates in POSITION. | |
0f03054a | 515 | POSITION should be a list of the form |
e55c21be | 516 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
517 | as returned by the `event-start' and `event-end' functions." |
518 | (nth 2 position)) | |
519 | ||
ed627e08 | 520 | (defun posn-col-row (position) |
dbbcac56 | 521 | "Return the column and row in POSITION, measured in characters. |
e55c21be RS |
522 | POSITION should be a list of the form |
523 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) | |
ed627e08 RS |
524 | as returned by the `event-start' and `event-end' functions. |
525 | For a scroll-bar event, the result column is 0, and the row | |
526 | corresponds to the vertical position of the click in the scroll bar." | |
527 | (let ((pair (nth 2 position)) | |
528 | (window (posn-window position))) | |
dbbcac56 KH |
529 | (if (eq (if (consp (nth 1 position)) |
530 | (car (nth 1 position)) | |
531 | (nth 1 position)) | |
ed627e08 RS |
532 | 'vertical-scroll-bar) |
533 | (cons 0 (scroll-bar-scale pair (1- (window-height window)))) | |
dbbcac56 KH |
534 | (if (eq (if (consp (nth 1 position)) |
535 | (car (nth 1 position)) | |
536 | (nth 1 position)) | |
ed627e08 RS |
537 | 'horizontal-scroll-bar) |
538 | (cons (scroll-bar-scale pair (window-width window)) 0) | |
9ba60df9 RS |
539 | (let* ((frame (if (framep window) window (window-frame window))) |
540 | (x (/ (car pair) (frame-char-width frame))) | |
541 | (y (/ (cdr pair) (frame-char-height frame)))) | |
ed627e08 | 542 | (cons x y)))))) |
e55c21be | 543 | |
0f03054a RS |
544 | (defsubst posn-timestamp (position) |
545 | "Return the timestamp of POSITION. | |
546 | POSITION should be a list of the form | |
e55c21be | 547 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
f415c00c | 548 | as returned by the `event-start' and `event-end' functions." |
0f03054a | 549 | (nth 3 position)) |
9a5336ae | 550 | |
0f03054a | 551 | \f |
9a5336ae JB |
552 | ;;;; Obsolescent names for functions. |
553 | ||
059184dd ER |
554 | (defalias 'dot 'point) |
555 | (defalias 'dot-marker 'point-marker) | |
556 | (defalias 'dot-min 'point-min) | |
557 | (defalias 'dot-max 'point-max) | |
558 | (defalias 'window-dot 'window-point) | |
559 | (defalias 'set-window-dot 'set-window-point) | |
560 | (defalias 'read-input 'read-string) | |
561 | (defalias 'send-string 'process-send-string) | |
562 | (defalias 'send-region 'process-send-region) | |
563 | (defalias 'show-buffer 'set-window-buffer) | |
564 | (defalias 'buffer-flush-undo 'buffer-disable-undo) | |
565 | (defalias 'eval-current-buffer 'eval-buffer) | |
566 | (defalias 'compiled-function-p 'byte-code-function-p) | |
ae1cc031 | 567 | (defalias 'define-function 'defalias) |
be9b65ac | 568 | |
0cba3a0f KH |
569 | (defalias 'sref 'aref) |
570 | (make-obsolete 'sref 'aref) | |
571 | (make-obsolete 'char-bytes "Now this function always returns 1") | |
6bb762b3 | 572 | |
9a5336ae JB |
573 | ;; Some programs still use this as a function. |
574 | (defun baud-rate () | |
bcacc42c RS |
575 | "Obsolete function returning the value of the `baud-rate' variable. |
576 | Please convert your programs to use the variable `baud-rate' directly." | |
9a5336ae JB |
577 | baud-rate) |
578 | ||
0a5c0893 MB |
579 | (defalias 'focus-frame 'ignore) |
580 | (defalias 'unfocus-frame 'ignore) | |
9a5336ae JB |
581 | \f |
582 | ;;;; Alternate names for functions - these are not being phased out. | |
583 | ||
059184dd ER |
584 | (defalias 'string= 'string-equal) |
585 | (defalias 'string< 'string-lessp) | |
586 | (defalias 'move-marker 'set-marker) | |
059184dd ER |
587 | (defalias 'not 'null) |
588 | (defalias 'rplaca 'setcar) | |
589 | (defalias 'rplacd 'setcdr) | |
eb8c3be9 | 590 | (defalias 'beep 'ding) ;preserve lingual purity |
059184dd ER |
591 | (defalias 'indent-to-column 'indent-to) |
592 | (defalias 'backward-delete-char 'delete-backward-char) | |
593 | (defalias 'search-forward-regexp (symbol-function 're-search-forward)) | |
594 | (defalias 'search-backward-regexp (symbol-function 're-search-backward)) | |
595 | (defalias 'int-to-string 'number-to-string) | |
024ae2c6 | 596 | (defalias 'store-match-data 'set-match-data) |
475fb2fb KH |
597 | (defalias 'point-at-eol 'line-end-position) |
598 | (defalias 'point-at-bol 'line-beginning-position) | |
37f6661a JB |
599 | |
600 | ;;; Should this be an obsolete name? If you decide it should, you get | |
601 | ;;; to go through all the sources and change them. | |
059184dd | 602 | (defalias 'string-to-int 'string-to-number) |
be9b65ac | 603 | \f |
9a5336ae | 604 | ;;;; Hook manipulation functions. |
be9b65ac | 605 | |
0e4d378b RS |
606 | (defun make-local-hook (hook) |
607 | "Make the hook HOOK local to the current buffer. | |
71c78f01 RS |
608 | The return value is HOOK. |
609 | ||
0e4d378b RS |
610 | When a hook is local, its local and global values |
611 | work in concert: running the hook actually runs all the hook | |
612 | functions listed in *either* the local value *or* the global value | |
613 | of the hook variable. | |
614 | ||
7dd1926e RS |
615 | This function works by making `t' a member of the buffer-local value, |
616 | which acts as a flag to run the hook functions in the default value as | |
617 | well. This works for all normal hooks, but does not work for most | |
618 | non-normal hooks yet. We will be changing the callers of non-normal | |
619 | hooks so that they can handle localness; this has to be done one by | |
620 | one. | |
621 | ||
622 | This function does nothing if HOOK is already local in the current | |
623 | buffer. | |
0e4d378b RS |
624 | |
625 | Do not use `make-local-variable' to make a hook variable buffer-local." | |
626 | (if (local-variable-p hook) | |
627 | nil | |
628 | (or (boundp hook) (set hook nil)) | |
629 | (make-local-variable hook) | |
71c78f01 RS |
630 | (set hook (list t))) |
631 | hook) | |
0e4d378b RS |
632 | |
633 | (defun add-hook (hook function &optional append local) | |
32295976 RS |
634 | "Add to the value of HOOK the function FUNCTION. |
635 | FUNCTION is not added if already present. | |
636 | FUNCTION is added (if necessary) at the beginning of the hook list | |
637 | unless the optional argument APPEND is non-nil, in which case | |
638 | FUNCTION is added at the end. | |
639 | ||
0e4d378b RS |
640 | The optional fourth argument, LOCAL, if non-nil, says to modify |
641 | the hook's buffer-local value rather than its default value. | |
642 | This makes no difference if the hook is not buffer-local. | |
643 | To make a hook variable buffer-local, always use | |
644 | `make-local-hook', not `make-local-variable'. | |
645 | ||
32295976 RS |
646 | HOOK should be a symbol, and FUNCTION may be any valid function. If |
647 | HOOK is void, it is first set to nil. If HOOK's value is a single | |
aa09b5ca | 648 | function, it is changed to a list of functions." |
be9b65ac | 649 | (or (boundp hook) (set hook nil)) |
0e4d378b | 650 | (or (default-boundp hook) (set-default hook nil)) |
32295976 RS |
651 | ;; If the hook value is a single function, turn it into a list. |
652 | (let ((old (symbol-value hook))) | |
653 | (if (or (not (listp old)) (eq (car old) 'lambda)) | |
654 | (set hook (list old)))) | |
f4e5bca5 RS |
655 | (if (or local |
656 | ;; Detect the case where make-local-variable was used on a hook | |
657 | ;; and do what we used to do. | |
cd2db344 | 658 | (and (local-variable-if-set-p hook) |
f4e5bca5 | 659 | (not (memq t (symbol-value hook))))) |
0e4d378b | 660 | ;; Alter the local value only. |
1fa0de2c | 661 | (or (if (or (consp function) (byte-code-function-p function)) |
0e4d378b RS |
662 | (member function (symbol-value hook)) |
663 | (memq function (symbol-value hook))) | |
664 | (set hook | |
665 | (if append | |
666 | (append (symbol-value hook) (list function)) | |
667 | (cons function (symbol-value hook))))) | |
668 | ;; Alter the global value (which is also the only value, | |
669 | ;; if the hook doesn't have a local value). | |
1fa0de2c | 670 | (or (if (or (consp function) (byte-code-function-p function)) |
0e4d378b RS |
671 | (member function (default-value hook)) |
672 | (memq function (default-value hook))) | |
673 | (set-default hook | |
674 | (if append | |
675 | (append (default-value hook) (list function)) | |
676 | (cons function (default-value hook))))))) | |
677 | ||
678 | (defun remove-hook (hook function &optional local) | |
24980d16 RS |
679 | "Remove from the value of HOOK the function FUNCTION. |
680 | HOOK should be a symbol, and FUNCTION may be any valid function. If | |
681 | FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the | |
0e4d378b RS |
682 | list of hooks to run in HOOK, then nothing is done. See `add-hook'. |
683 | ||
684 | The optional third argument, LOCAL, if non-nil, says to modify | |
685 | the hook's buffer-local value rather than its default value. | |
686 | This makes no difference if the hook is not buffer-local. | |
687 | To make a hook variable buffer-local, always use | |
688 | `make-local-hook', not `make-local-variable'." | |
24980d16 | 689 | (if (or (not (boundp hook)) ;unbound symbol, or |
d46490e3 | 690 | (not (default-boundp hook)) |
24980d16 RS |
691 | (null (symbol-value hook)) ;value is nil, or |
692 | (null function)) ;function is nil, then | |
693 | nil ;Do nothing. | |
f4e5bca5 RS |
694 | (if (or local |
695 | ;; Detect the case where make-local-variable was used on a hook | |
696 | ;; and do what we used to do. | |
697 | (and (local-variable-p hook) | |
cf4a60a3 DL |
698 | (consp (symbol-value hook)) |
699 | (not (memq t (symbol-value hook))))) | |
0e4d378b RS |
700 | (let ((hook-value (symbol-value hook))) |
701 | (if (consp hook-value) | |
702 | (if (member function hook-value) | |
703 | (setq hook-value (delete function (copy-sequence hook-value)))) | |
704 | (if (equal hook-value function) | |
705 | (setq hook-value nil))) | |
706 | (set hook hook-value)) | |
707 | (let ((hook-value (default-value hook))) | |
cf4a60a3 | 708 | (if (and (consp hook-value) (not (functionp hook-value))) |
0e4d378b RS |
709 | (if (member function hook-value) |
710 | (setq hook-value (delete function (copy-sequence hook-value)))) | |
711 | (if (equal hook-value function) | |
712 | (setq hook-value nil))) | |
713 | (set-default hook hook-value))))) | |
6e3af630 RS |
714 | |
715 | (defun add-to-list (list-var element) | |
8851c1f0 | 716 | "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
9f0b1f09 | 717 | The test for presence of ELEMENT is done with `equal'. |
508bcbca RS |
718 | If ELEMENT is added, it is added at the beginning of the list. |
719 | ||
8851c1f0 RS |
720 | If you want to use `add-to-list' on a variable that is not defined |
721 | until a certain package is loaded, you should put the call to `add-to-list' | |
722 | into a hook function that will be run only after loading the package. | |
723 | `eval-after-load' provides one way to do this. In some cases | |
724 | other hooks, such as major mode hooks, can do the job." | |
15171a06 KH |
725 | (if (member element (symbol-value list-var)) |
726 | (symbol-value list-var) | |
727 | (set list-var (cons element (symbol-value list-var))))) | |
be9b65ac | 728 | \f |
9a5336ae JB |
729 | ;;;; Specifying things to do after certain files are loaded. |
730 | ||
731 | (defun eval-after-load (file form) | |
732 | "Arrange that, if FILE is ever loaded, FORM will be run at that time. | |
733 | This makes or adds to an entry on `after-load-alist'. | |
90914938 | 734 | If FILE is already loaded, evaluate FORM right now. |
12c7071c | 735 | It does nothing if FORM is already on the list for FILE. |
9a5336ae | 736 | FILE should be the name of a library, with no directory name." |
90914938 | 737 | ;; Make sure there is an element for FILE. |
9a5336ae JB |
738 | (or (assoc file after-load-alist) |
739 | (setq after-load-alist (cons (list file) after-load-alist))) | |
90914938 | 740 | ;; Add FORM to the element if it isn't there. |
12c7071c RS |
741 | (let ((elt (assoc file after-load-alist))) |
742 | (or (member form (cdr elt)) | |
90914938 RS |
743 | (progn |
744 | (nconc elt (list form)) | |
745 | ;; If the file has been loaded already, run FORM right away. | |
746 | (and (assoc file load-history) | |
747 | (eval form))))) | |
9a5336ae JB |
748 | form) |
749 | ||
750 | (defun eval-next-after-load (file) | |
751 | "Read the following input sexp, and run it whenever FILE is loaded. | |
752 | This makes or adds to an entry on `after-load-alist'. | |
753 | FILE should be the name of a library, with no directory name." | |
754 | (eval-after-load file (read))) | |
755 | ||
756 | \f | |
757 | ;;;; Input and display facilities. | |
758 | ||
77a5664f | 759 | (defvar read-quoted-char-radix 8 |
1ba764de | 760 | "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'. |
77a5664f RS |
761 | Legitimate radix values are 8, 10 and 16.") |
762 | ||
763 | (custom-declare-variable-early | |
764 | 'read-quoted-char-radix 8 | |
765 | "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'. | |
1ba764de RS |
766 | Legitimate radix values are 8, 10 and 16." |
767 | :type '(choice (const 8) (const 10) (const 16)) | |
768 | :group 'editing-basics) | |
769 | ||
9a5336ae | 770 | (defun read-quoted-char (&optional prompt) |
2444730b RS |
771 | "Like `read-char', but do not allow quitting. |
772 | Also, if the first character read is an octal digit, | |
773 | we read any number of octal digits and return the | |
569b03f2 | 774 | specified character code. Any nondigit terminates the sequence. |
1ba764de | 775 | If the terminator is RET, it is discarded; |
2444730b RS |
776 | any other terminator is used itself as input. |
777 | ||
569b03f2 RS |
778 | The optional argument PROMPT specifies a string to use to prompt the user. |
779 | The variable `read-quoted-char-radix' controls which radix to use | |
780 | for numeric input." | |
2444730b RS |
781 | (let ((message-log-max nil) done (first t) (code 0) char) |
782 | (while (not done) | |
783 | (let ((inhibit-quit first) | |
42e636f0 KH |
784 | ;; Don't let C-h get the help message--only help function keys. |
785 | (help-char nil) | |
786 | (help-form | |
787 | "Type the special character you want to use, | |
2444730b | 788 | or the octal character code. |
1ba764de | 789 | RET terminates the character code and is discarded; |
2444730b | 790 | any other non-digit terminates the character code and is then used as input.")) |
b7de4d62 | 791 | (setq char (read-event (and prompt (format "%s-" prompt)) t)) |
9a5336ae | 792 | (if inhibit-quit (setq quit-flag nil))) |
4867f7b2 RS |
793 | ;; Translate TAB key into control-I ASCII character, and so on. |
794 | (and char | |
795 | (let ((translated (lookup-key function-key-map (vector char)))) | |
bf896a1b | 796 | (if (arrayp translated) |
4867f7b2 | 797 | (setq char (aref translated 0))))) |
9a5336ae | 798 | (cond ((null char)) |
1ba764de RS |
799 | ((not (integerp char)) |
800 | (setq unread-command-events (list char) | |
801 | done t)) | |
bf896a1b RS |
802 | ((/= (logand char ?\M-\^@) 0) |
803 | ;; Turn a meta-character into a character with the 0200 bit set. | |
804 | (setq code (logior (logand char (lognot ?\M-\^@)) 128) | |
805 | done t)) | |
1ba764de RS |
806 | ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix)))) |
807 | (setq code (+ (* code read-quoted-char-radix) (- char ?0))) | |
808 | (and prompt (setq prompt (message "%s %c" prompt char)))) | |
809 | ((and (<= ?a (downcase char)) | |
810 | (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix)))) | |
92304bc8 RS |
811 | (setq code (+ (* code read-quoted-char-radix) |
812 | (+ 10 (- (downcase char) ?a)))) | |
91a6acc3 | 813 | (and prompt (setq prompt (message "%s %c" prompt char)))) |
1ba764de | 814 | ((and (not first) (eq char ?\C-m)) |
2444730b RS |
815 | (setq done t)) |
816 | ((not first) | |
817 | (setq unread-command-events (list char) | |
818 | done t)) | |
819 | (t (setq code char | |
820 | done t))) | |
821 | (setq first nil)) | |
bf896a1b | 822 | code)) |
9a5336ae | 823 | |
44071d6b RS |
824 | (defun read-passwd (prompt &optional confirm default) |
825 | "Read a password, prompting with PROMPT. Echo `.' for each character typed. | |
e0e4cb7a | 826 | End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. |
44071d6b RS |
827 | Optional argument CONFIRM, if non-nil, then read it twice to make sure. |
828 | Optional DEFAULT is a default password to use instead of empty input." | |
829 | (if confirm | |
830 | (let (success) | |
831 | (while (not success) | |
832 | (let ((first (read-passwd prompt nil default)) | |
833 | (second (read-passwd "Confirm password: " nil default))) | |
834 | (if (equal first second) | |
835 | (setq success first) | |
836 | (message "Password not repeated accurately; please start over") | |
837 | (sit-for 1)))) | |
838 | success) | |
8723b7f3 | 839 | (clear-this-command-keys) |
44071d6b RS |
840 | (let ((pass nil) |
841 | (c 0) | |
842 | (echo-keystrokes 0) | |
843 | (cursor-in-echo-area t)) | |
844 | (while (progn (message "%s%s" | |
845 | prompt | |
846 | (make-string (length pass) ?.)) | |
acc81368 | 847 | (setq c (read-char nil t)) |
44071d6b RS |
848 | (and (/= c ?\r) (/= c ?\n) (/= c ?\e))) |
849 | (if (= c ?\C-u) | |
850 | (setq pass "") | |
851 | (if (and (/= c ?\b) (/= c ?\177)) | |
852 | (setq pass (concat pass (char-to-string c))) | |
853 | (if (> (length pass) 0) | |
854 | (setq pass (substring pass 0 -1)))))) | |
855 | (message nil) | |
856 | (or pass default "")))) | |
e0e4cb7a | 857 | \f |
9a5336ae JB |
858 | (defun force-mode-line-update (&optional all) |
859 | "Force the mode-line of the current buffer to be redisplayed. | |
7ec2a18c | 860 | With optional non-nil ALL, force redisplay of all mode-lines." |
9a5336ae JB |
861 | (if all (save-excursion (set-buffer (other-buffer)))) |
862 | (set-buffer-modified-p (buffer-modified-p))) | |
863 | ||
be9b65ac DL |
864 | (defun momentary-string-display (string pos &optional exit-char message) |
865 | "Momentarily display STRING in the buffer at POS. | |
866 | Display remains until next character is typed. | |
867 | If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed; | |
868 | otherwise it is then available as input (as a command if nothing else). | |
869 | Display MESSAGE (optional fourth arg) in the echo area. | |
870 | If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there." | |
871 | (or exit-char (setq exit-char ?\ )) | |
c306e0e0 | 872 | (let ((inhibit-read-only t) |
ca2ec1c5 RS |
873 | ;; Don't modify the undo list at all. |
874 | (buffer-undo-list t) | |
be9b65ac DL |
875 | (modified (buffer-modified-p)) |
876 | (name buffer-file-name) | |
877 | insert-end) | |
878 | (unwind-protect | |
879 | (progn | |
880 | (save-excursion | |
881 | (goto-char pos) | |
882 | ;; defeat file locking... don't try this at home, kids! | |
883 | (setq buffer-file-name nil) | |
884 | (insert-before-markers string) | |
3eec84bf RS |
885 | (setq insert-end (point)) |
886 | ;; If the message end is off screen, recenter now. | |
024ae2c6 | 887 | (if (< (window-end nil t) insert-end) |
3eec84bf RS |
888 | (recenter (/ (window-height) 2))) |
889 | ;; If that pushed message start off the screen, | |
890 | ;; scroll to start it at the top of the screen. | |
891 | (move-to-window-line 0) | |
892 | (if (> (point) pos) | |
893 | (progn | |
894 | (goto-char pos) | |
895 | (recenter 0)))) | |
be9b65ac DL |
896 | (message (or message "Type %s to continue editing.") |
897 | (single-key-description exit-char)) | |
3547c855 | 898 | (let ((char (read-event))) |
be9b65ac | 899 | (or (eq char exit-char) |
dbc4e1c1 | 900 | (setq unread-command-events (list char))))) |
be9b65ac DL |
901 | (if insert-end |
902 | (save-excursion | |
903 | (delete-region pos insert-end))) | |
904 | (setq buffer-file-name name) | |
905 | (set-buffer-modified-p modified)))) | |
906 | ||
9a5336ae JB |
907 | \f |
908 | ;;;; Miscellanea. | |
909 | ||
448b61c9 RS |
910 | ;; A number of major modes set this locally. |
911 | ;; Give it a global value to avoid compiler warnings. | |
912 | (defvar font-lock-defaults nil) | |
913 | ||
4fb17037 RS |
914 | (defvar suspend-hook nil |
915 | "Normal hook run by `suspend-emacs', before suspending.") | |
916 | ||
917 | (defvar suspend-resume-hook nil | |
918 | "Normal hook run by `suspend-emacs', after Emacs is continued.") | |
919 | ||
448b61c9 RS |
920 | ;; Avoid compiler warnings about this variable, |
921 | ;; which has a special meaning on certain system types. | |
922 | (defvar buffer-file-type nil | |
923 | "Non-nil if the visited file is a binary file. | |
924 | This variable is meaningful on MS-DOG and Windows NT. | |
925 | On those systems, it is automatically local in every buffer. | |
926 | On other systems, this variable is normally always nil.") | |
927 | ||
a860d25f | 928 | ;; This should probably be written in C (i.e., without using `walk-windows'). |
63503b24 | 929 | (defun get-buffer-window-list (buffer &optional minibuf frame) |
a860d25f | 930 | "Return windows currently displaying BUFFER, or nil if none. |
63503b24 | 931 | See `walk-windows' for the meaning of MINIBUF and FRAME." |
43c5ac8c | 932 | (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows) |
a860d25f SM |
933 | (walk-windows (function (lambda (window) |
934 | (if (eq (window-buffer window) buffer) | |
935 | (setq windows (cons window windows))))) | |
63503b24 | 936 | minibuf frame) |
a860d25f SM |
937 | windows)) |
938 | ||
f9269e19 RS |
939 | (defun ignore (&rest ignore) |
940 | "Do nothing and return nil. | |
941 | This function accepts any number of arguments, but ignores them." | |
c0f1a4f6 | 942 | (interactive) |
9a5336ae JB |
943 | nil) |
944 | ||
945 | (defun error (&rest args) | |
aa308ce2 RS |
946 | "Signal an error, making error message by passing all args to `format'. |
947 | In Emacs, the convention is that error messages start with a capital | |
948 | letter but *do not* end with a period. Please follow this convention | |
949 | for the sake of consistency." | |
9a5336ae JB |
950 | (while t |
951 | (signal 'error (list (apply 'format args))))) | |
952 | ||
cef7ae6e | 953 | (defalias 'user-original-login-name 'user-login-name) |
9a5336ae | 954 | |
be9b65ac DL |
955 | (defun start-process-shell-command (name buffer &rest args) |
956 | "Start a program in a subprocess. Return the process object for it. | |
957 | Args are NAME BUFFER COMMAND &rest COMMAND-ARGS. | |
958 | NAME is name for process. It is modified if necessary to make it unique. | |
959 | BUFFER is the buffer or (buffer-name) to associate with the process. | |
960 | Process output goes at end of that buffer, unless you specify | |
961 | an output stream or filter function to handle the output. | |
962 | BUFFER may be also nil, meaning that this process is not associated | |
963 | with any buffer | |
964 | Third arg is command name, the name of a shell command. | |
965 | Remaining arguments are the arguments for the command. | |
4f1d6310 | 966 | Wildcards and redirection are handled as usual in the shell." |
a247bf21 KH |
967 | (cond |
968 | ((eq system-type 'vax-vms) | |
969 | (apply 'start-process name buffer args)) | |
b59f6d7a RS |
970 | ;; We used to use `exec' to replace the shell with the command, |
971 | ;; but that failed to handle (...) and semicolon, etc. | |
a247bf21 KH |
972 | (t |
973 | (start-process name buffer shell-file-name shell-command-switch | |
b59f6d7a | 974 | (mapconcat 'identity args " "))))) |
a7ed4c2a | 975 | \f |
a7f284ec RS |
976 | (defmacro with-current-buffer (buffer &rest body) |
977 | "Execute the forms in BODY with BUFFER as the current buffer. | |
a2fdb55c EN |
978 | The value returned is the value of the last form in BODY. |
979 | See also `with-temp-buffer'." | |
a7f284ec RS |
980 | `(save-current-buffer |
981 | (set-buffer ,buffer) | |
a2fdb55c | 982 | ,@body)) |
a7f284ec | 983 | |
e5bb8a8c SM |
984 | (defmacro with-temp-file (file &rest body) |
985 | "Create a new buffer, evaluate BODY there, and write the buffer to FILE. | |
986 | The value returned is the value of the last form in BODY. | |
a2fdb55c | 987 | See also `with-temp-buffer'." |
a7ed4c2a | 988 | (let ((temp-file (make-symbol "temp-file")) |
a2fdb55c EN |
989 | (temp-buffer (make-symbol "temp-buffer"))) |
990 | `(let ((,temp-file ,file) | |
991 | (,temp-buffer | |
992 | (get-buffer-create (generate-new-buffer-name " *temp file*")))) | |
993 | (unwind-protect | |
994 | (prog1 | |
995 | (with-current-buffer ,temp-buffer | |
e5bb8a8c | 996 | ,@body) |
a2fdb55c EN |
997 | (with-current-buffer ,temp-buffer |
998 | (widen) | |
999 | (write-region (point-min) (point-max) ,temp-file nil 0))) | |
1000 | (and (buffer-name ,temp-buffer) | |
1001 | (kill-buffer ,temp-buffer)))))) | |
1002 | ||
e5bb8a8c | 1003 | (defmacro with-temp-message (message &rest body) |
a600effe | 1004 | "Display MESSAGE temporarily if non-nil while BODY is evaluated. |
e5bb8a8c SM |
1005 | The original message is restored to the echo area after BODY has finished. |
1006 | The value returned is the value of the last form in BODY. | |
a600effe SM |
1007 | MESSAGE is written to the message log buffer if `message-log-max' is non-nil. |
1008 | If MESSAGE is nil, the echo area and message log buffer are unchanged. | |
1009 | Use a MESSAGE of \"\" to temporarily clear the echo area." | |
110201c8 SM |
1010 | (let ((current-message (make-symbol "current-message")) |
1011 | (temp-message (make-symbol "with-temp-message"))) | |
1012 | `(let ((,temp-message ,message) | |
1013 | (,current-message)) | |
e5bb8a8c SM |
1014 | (unwind-protect |
1015 | (progn | |
110201c8 SM |
1016 | (when ,temp-message |
1017 | (setq ,current-message (current-message)) | |
aadf7ff3 | 1018 | (message "%s" ,temp-message)) |
e5bb8a8c | 1019 | ,@body) |
75545902 KH |
1020 | (and ,temp-message ,current-message |
1021 | (message "%s" ,current-message)))))) | |
e5bb8a8c SM |
1022 | |
1023 | (defmacro with-temp-buffer (&rest body) | |
1024 | "Create a temporary buffer, and evaluate BODY there like `progn'. | |
a2fdb55c EN |
1025 | See also `with-temp-file' and `with-output-to-string'." |
1026 | (let ((temp-buffer (make-symbol "temp-buffer"))) | |
1027 | `(let ((,temp-buffer | |
1028 | (get-buffer-create (generate-new-buffer-name " *temp*")))) | |
1029 | (unwind-protect | |
1030 | (with-current-buffer ,temp-buffer | |
e5bb8a8c | 1031 | ,@body) |
a2fdb55c EN |
1032 | (and (buffer-name ,temp-buffer) |
1033 | (kill-buffer ,temp-buffer)))))) | |
1034 | ||
5db7925d RS |
1035 | (defmacro with-output-to-string (&rest body) |
1036 | "Execute BODY, return the text it sent to `standard-output', as a string." | |
a2fdb55c EN |
1037 | `(let ((standard-output |
1038 | (get-buffer-create (generate-new-buffer-name " *string-output*")))) | |
5db7925d RS |
1039 | (let ((standard-output standard-output)) |
1040 | ,@body) | |
a2fdb55c EN |
1041 | (with-current-buffer standard-output |
1042 | (prog1 | |
1043 | (buffer-string) | |
1044 | (kill-buffer nil))))) | |
2ec9c94e RS |
1045 | |
1046 | (defmacro combine-after-change-calls (&rest body) | |
1047 | "Execute BODY, but don't call the after-change functions till the end. | |
1048 | If BODY makes changes in the buffer, they are recorded | |
1049 | and the functions on `after-change-functions' are called several times | |
1050 | when BODY is finished. | |
31aa282e | 1051 | The return value is the value of the last form in BODY. |
2ec9c94e RS |
1052 | |
1053 | If `before-change-functions' is non-nil, then calls to the after-change | |
1054 | functions can't be deferred, so in that case this macro has no effect. | |
1055 | ||
1056 | Do not alter `after-change-functions' or `before-change-functions' | |
1057 | in BODY." | |
1058 | `(unwind-protect | |
1059 | (let ((combine-after-change-calls t)) | |
1060 | . ,body) | |
1061 | (combine-after-change-execute))) | |
1062 | ||
a2fdb55c | 1063 | \f |
c7ca41e6 RS |
1064 | (defvar save-match-data-internal) |
1065 | ||
1066 | ;; We use save-match-data-internal as the local variable because | |
1067 | ;; that works ok in practice (people should not use that variable elsewhere). | |
1068 | ;; We used to use an uninterned symbol; the compiler handles that properly | |
1069 | ;; now, but it generates slower code. | |
9a5336ae JB |
1070 | (defmacro save-match-data (&rest body) |
1071 | "Execute the BODY forms, restoring the global value of the match data." | |
9fc0eb95 | 1072 | `(let ((save-match-data-internal (match-data))) |
c7ca41e6 RS |
1073 | (unwind-protect |
1074 | (progn ,@body) | |
024ae2c6 | 1075 | (set-match-data save-match-data-internal)))) |
993713ce | 1076 | |
cd323f89 | 1077 | (defun match-string (num &optional string) |
993713ce SM |
1078 | "Return string of text matched by last search. |
1079 | NUM specifies which parenthesized expression in the last regexp. | |
1080 | Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
1081 | Zero means the entire text matched by the whole regexp or whole string. | |
1082 | STRING should be given if the last search was by `string-match' on STRING." | |
cd323f89 SM |
1083 | (if (match-beginning num) |
1084 | (if string | |
1085 | (substring string (match-beginning num) (match-end num)) | |
1086 | (buffer-substring (match-beginning num) (match-end num))))) | |
58f950b4 | 1087 | |
bb760c71 RS |
1088 | (defun match-string-no-properties (num &optional string) |
1089 | "Return string of text matched by last search, without text properties. | |
1090 | NUM specifies which parenthesized expression in the last regexp. | |
1091 | Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
1092 | Zero means the entire text matched by the whole regexp or whole string. | |
1093 | STRING should be given if the last search was by `string-match' on STRING." | |
1094 | (if (match-beginning num) | |
1095 | (if string | |
1096 | (let ((result | |
1097 | (substring string (match-beginning num) (match-end num)))) | |
1098 | (set-text-properties 0 (length result) nil result) | |
1099 | result) | |
1100 | (buffer-substring-no-properties (match-beginning num) | |
1101 | (match-end num))))) | |
1102 | ||
edce3654 RS |
1103 | (defun split-string (string &optional separators) |
1104 | "Splits STRING into substrings where there are matches for SEPARATORS. | |
1105 | Each match for SEPARATORS is a splitting point. | |
1106 | The substrings between the splitting points are made into a list | |
1107 | which is returned. | |
b222b786 RS |
1108 | If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\". |
1109 | ||
1110 | If there is match for SEPARATORS at the beginning of STRING, we do not | |
1111 | include a null substring for that. Likewise, if there is a match | |
1112 | at the end of STRING, we don't include a null substring for that." | |
edce3654 RS |
1113 | (let ((rexp (or separators "[ \f\t\n\r\v]+")) |
1114 | (start 0) | |
b222b786 | 1115 | notfirst |
edce3654 | 1116 | (list nil)) |
b222b786 RS |
1117 | (while (and (string-match rexp string |
1118 | (if (and notfirst | |
1119 | (= start (match-beginning 0)) | |
1120 | (< start (length string))) | |
1121 | (1+ start) start)) | |
1122 | (< (match-beginning 0) (length string))) | |
1123 | (setq notfirst t) | |
7eb47123 | 1124 | (or (eq (match-beginning 0) 0) |
b222b786 RS |
1125 | (and (eq (match-beginning 0) (match-end 0)) |
1126 | (eq (match-beginning 0) start)) | |
edce3654 RS |
1127 | (setq list |
1128 | (cons (substring string start (match-beginning 0)) | |
1129 | list))) | |
1130 | (setq start (match-end 0))) | |
1131 | (or (eq start (length string)) | |
1132 | (setq list | |
1133 | (cons (substring string start) | |
1134 | list))) | |
1135 | (nreverse list))) | |
1ccaea52 AI |
1136 | |
1137 | (defun subst-char-in-string (fromchar tochar string &optional inplace) | |
1138 | "Replace FROMCHAR with TOCHAR in STRING each time it occurs. | |
1139 | Unless optional argument INPLACE is non-nil, return a new string." | |
1140 | (let ((i (length string)) | |
1141 | (newstr (if inplace string (copy-sequence string)))) | |
1142 | (while (> i 0) | |
1143 | (setq i (1- i)) | |
1144 | (if (eq (aref newstr i) fromchar) | |
1145 | (aset newstr i tochar))) | |
1146 | newstr)) | |
a7ed4c2a | 1147 | \f |
8af7df60 RS |
1148 | (defun shell-quote-argument (argument) |
1149 | "Quote an argument for passing as argument to an inferior shell." | |
c1c74b43 RS |
1150 | (if (eq system-type 'ms-dos) |
1151 | ;; MS-DOS shells don't have quoting, so don't do any. | |
1152 | argument | |
1153 | (if (eq system-type 'windows-nt) | |
1154 | (concat "\"" argument "\"") | |
e1b65a6b RS |
1155 | (if (equal argument "") |
1156 | "''" | |
1157 | ;; Quote everything except POSIX filename characters. | |
1158 | ;; This should be safe enough even for really weird shells. | |
1159 | (let ((result "") (start 0) end) | |
1160 | (while (string-match "[^-0-9a-zA-Z_./]" argument start) | |
1161 | (setq end (match-beginning 0) | |
1162 | result (concat result (substring argument start end) | |
1163 | "\\" (substring argument end (1+ end))) | |
1164 | start (1+ end))) | |
1165 | (concat result (substring argument start))))))) | |
8af7df60 | 1166 | |
297d863b | 1167 | (defun make-syntax-table (&optional oldtable) |
984f718a | 1168 | "Return a new syntax table. |
888eb98e RS |
1169 | If OLDTABLE is non-nil, copy OLDTABLE. |
1170 | Otherwise, create a syntax table which inherits | |
1171 | all letters and control characters from the standard syntax table; | |
1172 | other characters are copied from the standard syntax table." | |
297d863b KH |
1173 | (if oldtable |
1174 | (copy-syntax-table oldtable) | |
1175 | (let ((table (copy-syntax-table)) | |
1176 | i) | |
1177 | (setq i 0) | |
1178 | (while (<= i 31) | |
a6889c57 | 1179 | (aset table i nil) |
297d863b KH |
1180 | (setq i (1+ i))) |
1181 | (setq i ?A) | |
1182 | (while (<= i ?Z) | |
a6889c57 | 1183 | (aset table i nil) |
297d863b KH |
1184 | (setq i (1+ i))) |
1185 | (setq i ?a) | |
1186 | (while (<= i ?z) | |
a6889c57 | 1187 | (aset table i nil) |
297d863b KH |
1188 | (setq i (1+ i))) |
1189 | (setq i 128) | |
1190 | (while (<= i 255) | |
a6889c57 | 1191 | (aset table i nil) |
297d863b KH |
1192 | (setq i (1+ i))) |
1193 | table))) | |
31aa282e KH |
1194 | |
1195 | (defun add-to-invisibility-spec (arg) | |
1196 | "Add elements to `buffer-invisibility-spec'. | |
1197 | See documentation for `buffer-invisibility-spec' for the kind of elements | |
1198 | that can be added." | |
1199 | (cond | |
1200 | ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t)) | |
1201 | (setq buffer-invisibility-spec (list arg))) | |
1202 | (t | |
11d431ad KH |
1203 | (setq buffer-invisibility-spec |
1204 | (cons arg buffer-invisibility-spec))))) | |
31aa282e KH |
1205 | |
1206 | (defun remove-from-invisibility-spec (arg) | |
1207 | "Remove elements from `buffer-invisibility-spec'." | |
e93b8cbb | 1208 | (if (consp buffer-invisibility-spec) |
071a2a71 | 1209 | (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec)))) |
baed0109 RS |
1210 | \f |
1211 | (defun global-set-key (key command) | |
1212 | "Give KEY a global binding as COMMAND. | |
7bba1895 KH |
1213 | COMMAND is the command definition to use; usually it is |
1214 | a symbol naming an interactively-callable function. | |
1215 | KEY is a key sequence; noninteractively, it is a string or vector | |
1216 | of characters or event types, and non-ASCII characters with codes | |
1217 | above 127 (such as ISO Latin-1) can be included if you use a vector. | |
1218 | ||
1219 | Note that if KEY has a local binding in the current buffer, | |
1220 | that local binding will continue to shadow any global binding | |
1221 | that you make with this function." | |
baed0109 RS |
1222 | (interactive "KSet key globally: \nCSet key %s to command: ") |
1223 | (or (vectorp key) (stringp key) | |
1224 | (signal 'wrong-type-argument (list 'arrayp key))) | |
ff663bbe | 1225 | (define-key (current-global-map) key command)) |
baed0109 RS |
1226 | |
1227 | (defun local-set-key (key command) | |
1228 | "Give KEY a local binding as COMMAND. | |
7bba1895 KH |
1229 | COMMAND is the command definition to use; usually it is |
1230 | a symbol naming an interactively-callable function. | |
1231 | KEY is a key sequence; noninteractively, it is a string or vector | |
1232 | of characters or event types, and non-ASCII characters with codes | |
1233 | above 127 (such as ISO Latin-1) can be included if you use a vector. | |
1234 | ||
baed0109 RS |
1235 | The binding goes in the current buffer's local map, |
1236 | which in most cases is shared with all other buffers in the same major mode." | |
1237 | (interactive "KSet key locally: \nCSet key %s locally to command: ") | |
1238 | (let ((map (current-local-map))) | |
1239 | (or map | |
1240 | (use-local-map (setq map (make-sparse-keymap)))) | |
1241 | (or (vectorp key) (stringp key) | |
1242 | (signal 'wrong-type-argument (list 'arrayp key))) | |
ff663bbe | 1243 | (define-key map key command))) |
984f718a | 1244 | |
baed0109 RS |
1245 | (defun global-unset-key (key) |
1246 | "Remove global binding of KEY. | |
1247 | KEY is a string representing a sequence of keystrokes." | |
1248 | (interactive "kUnset key globally: ") | |
1249 | (global-set-key key nil)) | |
1250 | ||
db2474b8 | 1251 | (defun local-unset-key (key) |
baed0109 RS |
1252 | "Remove local binding of KEY. |
1253 | KEY is a string representing a sequence of keystrokes." | |
1254 | (interactive "kUnset key locally: ") | |
1255 | (if (current-local-map) | |
db2474b8 | 1256 | (local-set-key key nil)) |
baed0109 RS |
1257 | nil) |
1258 | \f | |
4809d0dd KH |
1259 | ;; We put this here instead of in frame.el so that it's defined even on |
1260 | ;; systems where frame.el isn't loaded. | |
1261 | (defun frame-configuration-p (object) | |
1262 | "Return non-nil if OBJECT seems to be a frame configuration. | |
1263 | Any list whose car is `frame-configuration' is assumed to be a frame | |
1264 | configuration." | |
1265 | (and (consp object) | |
1266 | (eq (car object) 'frame-configuration))) | |
1267 | ||
a9a44ed1 | 1268 | (defun functionp (object) |
77a5664f | 1269 | "Non-nil if OBJECT is a type of object that can be called as a function." |
c029995c | 1270 | (or (subrp object) (byte-code-function-p object) |
a9a44ed1 RS |
1271 | (eq (car-safe object) 'lambda) |
1272 | (and (symbolp object) (fboundp object)))) | |
1273 | ||
9a5336ae JB |
1274 | ;; now in fns.c |
1275 | ;(defun nth (n list) | |
1276 | ; "Returns the Nth element of LIST. | |
1277 | ;N counts from zero. If LIST is not that long, nil is returned." | |
1278 | ; (car (nthcdr n list))) | |
1279 | ; | |
1280 | ;(defun copy-alist (alist) | |
1281 | ; "Return a copy of ALIST. | |
1282 | ;This is a new alist which represents the same mapping | |
1283 | ;from objects to objects, but does not share the alist structure with ALIST. | |
1284 | ;The objects mapped (cars and cdrs of elements of the alist) | |
1285 | ;are shared, however." | |
1286 | ; (setq alist (copy-sequence alist)) | |
1287 | ; (let ((tail alist)) | |
1288 | ; (while tail | |
1289 | ; (if (consp (car tail)) | |
1290 | ; (setcar tail (cons (car (car tail)) (cdr (car tail))))) | |
1291 | ; (setq tail (cdr tail)))) | |
1292 | ; alist) | |
630cc463 | 1293 | |
a62d6695 DL |
1294 | (defun assoc-delete-all (key alist) |
1295 | "Delete from ALIST all elements whose car is KEY. | |
1296 | Return the modified alist." | |
1297 | (setq alist (copy-sequence alist)) | |
1298 | (let ((tail alist)) | |
1299 | (while tail | |
1300 | (if (eq (car (car tail)) key) | |
1301 | (setq alist (delq (car tail) alist))) | |
1302 | (setq tail (cdr tail))) | |
1303 | alist)) | |
1304 | ||
630cc463 | 1305 | ;;; subr.el ends here |