Commit | Line | Data |
---|---|---|
c88ab9ce | 1 | ;;; subr.el --- basic lisp subroutines for Emacs |
630cc463 | 2 | |
fe10cef0 GM |
3 | ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001 |
4 | ;; Free Software Foundation, Inc. | |
be9b65ac DL |
5 | |
6 | ;; This file is part of GNU Emacs. | |
7 | ||
8 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 | ;; it under the terms of the GNU General Public License as published by | |
492878e4 | 10 | ;; the Free Software Foundation; either version 2, or (at your option) |
be9b65ac DL |
11 | ;; any later version. |
12 | ||
13 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
17 | ||
18 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
19 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
20 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | ;; Boston, MA 02111-1307, USA. | |
be9b65ac | 22 | |
60370d40 PJ |
23 | ;;; Commentary: |
24 | ||
630cc463 | 25 | ;;; Code: |
77a5664f RS |
26 | (defvar custom-declare-variable-list nil |
27 | "Record `defcustom' calls made before `custom.el' is loaded to handle them. | |
28 | Each element of this list holds the arguments to one call to `defcustom'.") | |
29 | ||
68e3e5f5 | 30 | ;; Use this, rather than defcustom, in subr.el and other files loaded |
77a5664f RS |
31 | ;; before custom.el. |
32 | (defun custom-declare-variable-early (&rest arguments) | |
33 | (setq custom-declare-variable-list | |
34 | (cons arguments custom-declare-variable-list))) | |
9a5336ae JB |
35 | \f |
36 | ;;;; Lisp language features. | |
37 | ||
0764e16f SM |
38 | (defalias 'not 'null) |
39 | ||
9a5336ae JB |
40 | (defmacro lambda (&rest cdr) |
41 | "Return a lambda expression. | |
42 | A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is | |
43 | self-quoting; the result of evaluating the lambda expression is the | |
44 | expression itself. The lambda expression may then be treated as a | |
bec0d7f9 RS |
45 | function, i.e., stored as the function value of a symbol, passed to |
46 | funcall or mapcar, etc. | |
47 | ||
9a5336ae | 48 | ARGS should take the same form as an argument list for a `defun'. |
8fd68088 RS |
49 | DOCSTRING is an optional documentation string. |
50 | If present, it should describe how to call the function. | |
51 | But documentation strings are usually not useful in nameless functions. | |
9a5336ae JB |
52 | INTERACTIVE should be a call to the function `interactive', which see. |
53 | It may also be omitted. | |
54 | BODY should be a list of lisp expressions." | |
55 | ;; Note that this definition should not use backquotes; subr.el should not | |
56 | ;; depend on backquote.el. | |
57 | (list 'function (cons 'lambda cdr))) | |
58 | ||
1be152fc | 59 | (defmacro push (newelt listname) |
fa65505b | 60 | "Add NEWELT to the list stored in the symbol LISTNAME. |
1be152fc | 61 | This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)). |
d270117a | 62 | LISTNAME must be a symbol." |
22d85d00 DL |
63 | (list 'setq listname |
64 | (list 'cons newelt listname))) | |
d270117a RS |
65 | |
66 | (defmacro pop (listname) | |
67 | "Return the first element of LISTNAME's value, and remove it from the list. | |
68 | LISTNAME must be a symbol whose value is a list. | |
69 | If the value is nil, `pop' returns nil but does not actually | |
70 | change the list." | |
71 | (list 'prog1 (list 'car listname) | |
72 | (list 'setq listname (list 'cdr listname)))) | |
73 | ||
debff3c3 | 74 | (defmacro when (cond &rest body) |
b021ef18 | 75 | "If COND yields non-nil, do BODY, else return nil." |
debff3c3 | 76 | (list 'if cond (cons 'progn body))) |
9a5336ae | 77 | |
debff3c3 | 78 | (defmacro unless (cond &rest body) |
b021ef18 | 79 | "If COND yields nil, do BODY, else return nil." |
debff3c3 | 80 | (cons 'if (cons cond (cons nil body)))) |
d370591d | 81 | |
a0b0756a RS |
82 | (defmacro dolist (spec &rest body) |
83 | "(dolist (VAR LIST [RESULT]) BODY...): loop over a list. | |
84 | Evaluate BODY with VAR bound to each car from LIST, in turn. | |
85 | Then evaluate RESULT to get return value, default nil." | |
e4295aa1 RS |
86 | (let ((temp (make-symbol "--dolist-temp--"))) |
87 | (list 'let (list (list temp (nth 1 spec)) (car spec)) | |
88 | (list 'while temp | |
89 | (list 'setq (car spec) (list 'car temp)) | |
90 | (cons 'progn | |
91 | (append body | |
92 | (list (list 'setq temp (list 'cdr temp)))))) | |
93 | (if (cdr (cdr spec)) | |
94 | (cons 'progn | |
95 | (cons (list 'setq (car spec) nil) (cdr (cdr spec)))))))) | |
a0b0756a RS |
96 | |
97 | (defmacro dotimes (spec &rest body) | |
98 | "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times. | |
99 | Evaluate BODY with VAR bound to successive integers running from 0, | |
100 | inclusive, to COUNT, exclusive. Then evaluate RESULT to get | |
101 | the return value (nil if RESULT is omitted)." | |
e4295aa1 RS |
102 | (let ((temp (make-symbol "--dotimes-temp--"))) |
103 | (list 'let (list (list temp (nth 1 spec)) (list (car spec) 0)) | |
104 | (list 'while (list '< (car spec) temp) | |
105 | (cons 'progn | |
106 | (append body (list (list 'setq (car spec) | |
107 | (list '1+ (car spec))))))) | |
108 | (if (cdr (cdr spec)) | |
109 | (car (cdr (cdr spec))) | |
110 | nil)))) | |
a0b0756a | 111 | |
d370591d RS |
112 | (defsubst caar (x) |
113 | "Return the car of the car of X." | |
114 | (car (car x))) | |
115 | ||
116 | (defsubst cadr (x) | |
117 | "Return the car of the cdr of X." | |
118 | (car (cdr x))) | |
119 | ||
120 | (defsubst cdar (x) | |
121 | "Return the cdr of the car of X." | |
122 | (cdr (car x))) | |
123 | ||
124 | (defsubst cddr (x) | |
125 | "Return the cdr of the cdr of X." | |
126 | (cdr (cdr x))) | |
e8c32c99 | 127 | |
369fba5f RS |
128 | (defun last (x &optional n) |
129 | "Return the last link of the list X. Its car is the last element. | |
130 | If X is nil, return nil. | |
131 | If N is non-nil, return the Nth-to-last link of X. | |
132 | If N is bigger than the length of X, return X." | |
133 | (if n | |
134 | (let ((m 0) (p x)) | |
135 | (while (consp p) | |
136 | (setq m (1+ m) p (cdr p))) | |
137 | (if (<= n 0) p | |
138 | (if (< n m) (nthcdr (- m n) x) x))) | |
6bfdc2e2 | 139 | (while (consp (cdr x)) |
369fba5f RS |
140 | (setq x (cdr x))) |
141 | x)) | |
526d204e | 142 | |
1c1c65de KH |
143 | (defun butlast (x &optional n) |
144 | "Returns a copy of LIST with the last N elements removed." | |
145 | (if (and n (<= n 0)) x | |
146 | (nbutlast (copy-sequence x) n))) | |
147 | ||
148 | (defun nbutlast (x &optional n) | |
149 | "Modifies LIST to remove the last N elements." | |
150 | (let ((m (length x))) | |
151 | (or n (setq n 1)) | |
152 | (and (< n m) | |
153 | (progn | |
154 | (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil)) | |
155 | x)))) | |
156 | ||
13157efc | 157 | (defun remove (elt seq) |
963f49a2 | 158 | "Return a copy of SEQ with all occurrences of ELT removed. |
13157efc GM |
159 | SEQ must be a list, vector, or string. The comparison is done with `equal'." |
160 | (if (nlistp seq) | |
161 | ;; If SEQ isn't a list, there's no need to copy SEQ because | |
162 | ;; `delete' will return a new object. | |
163 | (delete elt seq) | |
164 | (delete elt (copy-sequence seq)))) | |
165 | ||
166 | (defun remq (elt list) | |
167 | "Return a copy of LIST with all occurences of ELT removed. | |
168 | The comparison is done with `eq'." | |
169 | (if (memq elt list) | |
170 | (delq elt (copy-sequence list)) | |
171 | list)) | |
172 | ||
8a288450 RS |
173 | (defun assoc-default (key alist &optional test default) |
174 | "Find object KEY in a pseudo-alist ALIST. | |
175 | ALIST is a list of conses or objects. Each element (or the element's car, | |
176 | if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY). | |
177 | If that is non-nil, the element matches; | |
178 | then `assoc-default' returns the element's cdr, if it is a cons, | |
526d204e | 179 | or DEFAULT if the element is not a cons. |
8a288450 RS |
180 | |
181 | If no element matches, the value is nil. | |
182 | If TEST is omitted or nil, `equal' is used." | |
183 | (let (found (tail alist) value) | |
184 | (while (and tail (not found)) | |
185 | (let ((elt (car tail))) | |
186 | (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key) | |
187 | (setq found t value (if (consp elt) (cdr elt) default)))) | |
188 | (setq tail (cdr tail))) | |
189 | value)) | |
98aae5f6 KH |
190 | |
191 | (defun assoc-ignore-case (key alist) | |
192 | "Like `assoc', but ignores differences in case and text representation. | |
193 | KEY must be a string. Upper-case and lower-case letters are treated as equal. | |
194 | Unibyte strings are converted to multibyte for comparison." | |
195 | (let (element) | |
196 | (while (and alist (not element)) | |
197 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t)) | |
198 | (setq element (car alist))) | |
199 | (setq alist (cdr alist))) | |
200 | element)) | |
201 | ||
202 | (defun assoc-ignore-representation (key alist) | |
203 | "Like `assoc', but ignores differences in text representation. | |
204 | KEY must be a string. | |
205 | Unibyte strings are converted to multibyte for comparison." | |
206 | (let (element) | |
207 | (while (and alist (not element)) | |
208 | (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil)) | |
209 | (setq element (car alist))) | |
210 | (setq alist (cdr alist))) | |
211 | element)) | |
cbbc3205 GM |
212 | |
213 | (defun member-ignore-case (elt list) | |
214 | "Like `member', but ignores differences in case and text representation. | |
215 | ELT must be a string. Upper-case and lower-case letters are treated as equal. | |
216 | Unibyte strings are converted to multibyte for comparison." | |
242c13e8 MB |
217 | (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t)))) |
218 | (setq list (cdr list))) | |
219 | list) | |
cbbc3205 | 220 | |
9a5336ae | 221 | \f |
9a5336ae | 222 | ;;;; Keymap support. |
be9b65ac DL |
223 | |
224 | (defun undefined () | |
225 | (interactive) | |
226 | (ding)) | |
227 | ||
228 | ;Prevent the \{...} documentation construct | |
229 | ;from mentioning keys that run this command. | |
230 | (put 'undefined 'suppress-keymap t) | |
231 | ||
232 | (defun suppress-keymap (map &optional nodigits) | |
233 | "Make MAP override all normally self-inserting keys to be undefined. | |
234 | Normally, as an exception, digits and minus-sign are set to make prefix args, | |
235 | but optional second arg NODIGITS non-nil treats them like other chars." | |
80e7b471 | 236 | (substitute-key-definition 'self-insert-command 'undefined map global-map) |
be9b65ac DL |
237 | (or nodigits |
238 | (let (loop) | |
239 | (define-key map "-" 'negative-argument) | |
240 | ;; Make plain numbers do numeric args. | |
241 | (setq loop ?0) | |
242 | (while (<= loop ?9) | |
243 | (define-key map (char-to-string loop) 'digit-argument) | |
244 | (setq loop (1+ loop)))))) | |
245 | ||
be9b65ac DL |
246 | ;Moved to keymap.c |
247 | ;(defun copy-keymap (keymap) | |
248 | ; "Return a copy of KEYMAP" | |
249 | ; (while (not (keymapp keymap)) | |
250 | ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap)))) | |
251 | ; (if (vectorp keymap) | |
252 | ; (copy-sequence keymap) | |
253 | ; (copy-alist keymap))) | |
254 | ||
f14dbba7 KH |
255 | (defvar key-substitution-in-progress nil |
256 | "Used internally by substitute-key-definition.") | |
257 | ||
7f2c2edd | 258 | (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix) |
be9b65ac DL |
259 | "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF. |
260 | In other words, OLDDEF is replaced with NEWDEF where ever it appears. | |
4656b314 | 261 | Alternatively, if optional fourth argument OLDMAP is specified, we redefine |
ff77cf40 | 262 | in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP." |
739f2672 GM |
263 | ;; Don't document PREFIX in the doc string because we don't want to |
264 | ;; advertise it. It's meant for recursive calls only. Here's its | |
265 | ;; meaning | |
266 | ||
267 | ;; If optional argument PREFIX is specified, it should be a key | |
268 | ;; prefix, a string. Redefined bindings will then be bound to the | |
269 | ;; original key, with PREFIX added at the front. | |
7f2c2edd RS |
270 | (or prefix (setq prefix "")) |
271 | (let* ((scan (or oldmap keymap)) | |
272 | (vec1 (vector nil)) | |
f14dbba7 KH |
273 | (prefix1 (vconcat prefix vec1)) |
274 | (key-substitution-in-progress | |
275 | (cons scan key-substitution-in-progress))) | |
7f2c2edd RS |
276 | ;; Scan OLDMAP, finding each char or event-symbol that |
277 | ;; has any definition, and act on it with hack-key. | |
278 | (while (consp scan) | |
279 | (if (consp (car scan)) | |
280 | (let ((char (car (car scan))) | |
281 | (defn (cdr (car scan)))) | |
282 | ;; The inside of this let duplicates exactly | |
283 | ;; the inside of the following let that handles array elements. | |
284 | (aset vec1 0 char) | |
285 | (aset prefix1 (length prefix) char) | |
44d798af | 286 | (let (inner-def skipped) |
7f2c2edd RS |
287 | ;; Skip past menu-prompt. |
288 | (while (stringp (car-safe defn)) | |
44d798af | 289 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 290 | (setq defn (cdr defn))) |
e025dddf RS |
291 | ;; Skip past cached key-equivalence data for menu items. |
292 | (and (consp defn) (consp (car defn)) | |
293 | (setq defn (cdr defn))) | |
7f2c2edd | 294 | (setq inner-def defn) |
e025dddf | 295 | ;; Look past a symbol that names a keymap. |
7f2c2edd RS |
296 | (while (and (symbolp inner-def) |
297 | (fboundp inner-def)) | |
298 | (setq inner-def (symbol-function inner-def))) | |
328a37ec RS |
299 | (if (or (eq defn olddef) |
300 | ;; Compare with equal if definition is a key sequence. | |
301 | ;; That is useful for operating on function-key-map. | |
302 | (and (or (stringp defn) (vectorp defn)) | |
303 | (equal defn olddef))) | |
44d798af | 304 | (define-key keymap prefix1 (nconc (nreverse skipped) newdef)) |
f14dbba7 | 305 | (if (and (keymapp defn) |
350b7567 RS |
306 | ;; Avoid recursively scanning |
307 | ;; where KEYMAP does not have a submap. | |
afd9831b RS |
308 | (let ((elt (lookup-key keymap prefix1))) |
309 | (or (null elt) | |
310 | (keymapp elt))) | |
350b7567 | 311 | ;; Avoid recursively rescanning keymap being scanned. |
f14dbba7 KH |
312 | (not (memq inner-def |
313 | key-substitution-in-progress))) | |
e025dddf RS |
314 | ;; If this one isn't being scanned already, |
315 | ;; scan it now. | |
7f2c2edd RS |
316 | (substitute-key-definition olddef newdef keymap |
317 | inner-def | |
318 | prefix1))))) | |
916cc49f | 319 | (if (vectorp (car scan)) |
7f2c2edd RS |
320 | (let* ((array (car scan)) |
321 | (len (length array)) | |
322 | (i 0)) | |
323 | (while (< i len) | |
324 | (let ((char i) (defn (aref array i))) | |
325 | ;; The inside of this let duplicates exactly | |
326 | ;; the inside of the previous let. | |
327 | (aset vec1 0 char) | |
328 | (aset prefix1 (length prefix) char) | |
44d798af | 329 | (let (inner-def skipped) |
7f2c2edd RS |
330 | ;; Skip past menu-prompt. |
331 | (while (stringp (car-safe defn)) | |
44d798af | 332 | (setq skipped (cons (car defn) skipped)) |
7f2c2edd | 333 | (setq defn (cdr defn))) |
e025dddf RS |
334 | (and (consp defn) (consp (car defn)) |
335 | (setq defn (cdr defn))) | |
7f2c2edd RS |
336 | (setq inner-def defn) |
337 | (while (and (symbolp inner-def) | |
338 | (fboundp inner-def)) | |
339 | (setq inner-def (symbol-function inner-def))) | |
328a37ec RS |
340 | (if (or (eq defn olddef) |
341 | (and (or (stringp defn) (vectorp defn)) | |
342 | (equal defn olddef))) | |
44d798af RS |
343 | (define-key keymap prefix1 |
344 | (nconc (nreverse skipped) newdef)) | |
f14dbba7 | 345 | (if (and (keymapp defn) |
afd9831b RS |
346 | (let ((elt (lookup-key keymap prefix1))) |
347 | (or (null elt) | |
348 | (keymapp elt))) | |
f14dbba7 KH |
349 | (not (memq inner-def |
350 | key-substitution-in-progress))) | |
7f2c2edd RS |
351 | (substitute-key-definition olddef newdef keymap |
352 | inner-def | |
353 | prefix1))))) | |
97fd9abf RS |
354 | (setq i (1+ i)))) |
355 | (if (char-table-p (car scan)) | |
356 | (map-char-table | |
357 | (function (lambda (char defn) | |
358 | (let () | |
359 | ;; The inside of this let duplicates exactly | |
360 | ;; the inside of the previous let, | |
361 | ;; except that it uses set-char-table-range | |
362 | ;; instead of define-key. | |
363 | (aset vec1 0 char) | |
364 | (aset prefix1 (length prefix) char) | |
365 | (let (inner-def skipped) | |
366 | ;; Skip past menu-prompt. | |
367 | (while (stringp (car-safe defn)) | |
368 | (setq skipped (cons (car defn) skipped)) | |
369 | (setq defn (cdr defn))) | |
370 | (and (consp defn) (consp (car defn)) | |
371 | (setq defn (cdr defn))) | |
372 | (setq inner-def defn) | |
373 | (while (and (symbolp inner-def) | |
374 | (fboundp inner-def)) | |
375 | (setq inner-def (symbol-function inner-def))) | |
376 | (if (or (eq defn olddef) | |
377 | (and (or (stringp defn) (vectorp defn)) | |
378 | (equal defn olddef))) | |
9a5114ac RS |
379 | (define-key keymap prefix1 |
380 | (nconc (nreverse skipped) newdef)) | |
97fd9abf RS |
381 | (if (and (keymapp defn) |
382 | (let ((elt (lookup-key keymap prefix1))) | |
383 | (or (null elt) | |
384 | (keymapp elt))) | |
385 | (not (memq inner-def | |
386 | key-substitution-in-progress))) | |
387 | (substitute-key-definition olddef newdef keymap | |
388 | inner-def | |
389 | prefix1))))))) | |
390 | (car scan))))) | |
7f2c2edd | 391 | (setq scan (cdr scan))))) |
9a5336ae | 392 | |
4ced66fd | 393 | (defun define-key-after (keymap key definition &optional after) |
4434d61b RS |
394 | "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding. |
395 | This is like `define-key' except that the binding for KEY is placed | |
396 | just after the binding for the event AFTER, instead of at the beginning | |
c34a9d34 RS |
397 | of the map. Note that AFTER must be an event type (like KEY), NOT a command |
398 | \(like DEFINITION). | |
399 | ||
4ced66fd | 400 | If AFTER is t or omitted, the new binding goes at the end of the keymap. |
08b1f8a1 | 401 | AFTER should be a single event type--a symbol or a character, not a sequence. |
c34a9d34 | 402 | |
4ced66fd | 403 | Bindings are always added before any inherited map. |
c34a9d34 | 404 | |
4ced66fd DL |
405 | The order of bindings in a keymap matters when it is used as a menu." |
406 | (unless after (setq after t)) | |
4434d61b RS |
407 | (or (keymapp keymap) |
408 | (signal 'wrong-type-argument (list 'keymapp keymap))) | |
08b1f8a1 GM |
409 | (setq key |
410 | (if (<= (length key) 1) (aref key 0) | |
411 | (setq keymap (lookup-key keymap | |
412 | (apply 'vector | |
413 | (butlast (mapcar 'identity key))))) | |
414 | (aref key (1- (length key))))) | |
415 | (let ((tail keymap) done inserted) | |
4434d61b RS |
416 | (while (and (not done) tail) |
417 | ;; Delete any earlier bindings for the same key. | |
08b1f8a1 | 418 | (if (eq (car-safe (car (cdr tail))) key) |
4434d61b | 419 | (setcdr tail (cdr (cdr tail)))) |
08b1f8a1 GM |
420 | ;; If we hit an included map, go down that one. |
421 | (if (keymapp (car tail)) (setq tail (car tail))) | |
4434d61b RS |
422 | ;; When we reach AFTER's binding, insert the new binding after. |
423 | ;; If we reach an inherited keymap, insert just before that. | |
113d28a8 | 424 | ;; If we reach the end of this keymap, insert at the end. |
c34a9d34 RS |
425 | (if (or (and (eq (car-safe (car tail)) after) |
426 | (not (eq after t))) | |
113d28a8 RS |
427 | (eq (car (cdr tail)) 'keymap) |
428 | (null (cdr tail))) | |
4434d61b | 429 | (progn |
113d28a8 RS |
430 | ;; Stop the scan only if we find a parent keymap. |
431 | ;; Keep going past the inserted element | |
432 | ;; so we can delete any duplications that come later. | |
433 | (if (eq (car (cdr tail)) 'keymap) | |
434 | (setq done t)) | |
435 | ;; Don't insert more than once. | |
436 | (or inserted | |
08b1f8a1 | 437 | (setcdr tail (cons (cons key definition) (cdr tail)))) |
113d28a8 | 438 | (setq inserted t))) |
4434d61b RS |
439 | (setq tail (cdr tail))))) |
440 | ||
d128fe85 RS |
441 | (defmacro kbd (keys) |
442 | "Convert KEYS to the internal Emacs key representation. | |
443 | KEYS should be a string constant in the format used for | |
444 | saving keyboard macros (see `insert-kbd-macro')." | |
445 | (read-kbd-macro keys)) | |
446 | ||
8bed5e3d RS |
447 | (put 'keyboard-translate-table 'char-table-extra-slots 0) |
448 | ||
9a5336ae JB |
449 | (defun keyboard-translate (from to) |
450 | "Translate character FROM to TO at a low level. | |
451 | This function creates a `keyboard-translate-table' if necessary | |
452 | and then modifies one entry in it." | |
8bed5e3d RS |
453 | (or (char-table-p keyboard-translate-table) |
454 | (setq keyboard-translate-table | |
455 | (make-char-table 'keyboard-translate-table nil))) | |
9a5336ae JB |
456 | (aset keyboard-translate-table from to)) |
457 | ||
458 | \f | |
459 | ;;;; The global keymap tree. | |
460 | ||
461 | ;;; global-map, esc-map, and ctl-x-map have their values set up in | |
462 | ;;; keymap.c; we just give them docstrings here. | |
463 | ||
464 | (defvar global-map nil | |
465 | "Default global keymap mapping Emacs keyboard input into commands. | |
466 | The value is a keymap which is usually (but not necessarily) Emacs's | |
467 | global map.") | |
468 | ||
469 | (defvar esc-map nil | |
470 | "Default keymap for ESC (meta) commands. | |
471 | The normal global definition of the character ESC indirects to this keymap.") | |
472 | ||
473 | (defvar ctl-x-map nil | |
474 | "Default keymap for C-x commands. | |
475 | The normal global definition of the character C-x indirects to this keymap.") | |
476 | ||
477 | (defvar ctl-x-4-map (make-sparse-keymap) | |
03eeb110 | 478 | "Keymap for subcommands of C-x 4.") |
059184dd | 479 | (defalias 'ctl-x-4-prefix ctl-x-4-map) |
9a5336ae JB |
480 | (define-key ctl-x-map "4" 'ctl-x-4-prefix) |
481 | ||
482 | (defvar ctl-x-5-map (make-sparse-keymap) | |
483 | "Keymap for frame commands.") | |
059184dd | 484 | (defalias 'ctl-x-5-prefix ctl-x-5-map) |
9a5336ae JB |
485 | (define-key ctl-x-map "5" 'ctl-x-5-prefix) |
486 | ||
0f03054a | 487 | \f |
9a5336ae JB |
488 | ;;;; Event manipulation functions. |
489 | ||
da16e648 KH |
490 | ;; The call to `read' is to ensure that the value is computed at load time |
491 | ;; and not compiled into the .elc file. The value is negative on most | |
492 | ;; machines, but not on all! | |
493 | (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@"))) | |
114137b8 | 494 | |
cde6d7e3 RS |
495 | (defun listify-key-sequence (key) |
496 | "Convert a key sequence to a list of events." | |
497 | (if (vectorp key) | |
498 | (append key nil) | |
499 | (mapcar (function (lambda (c) | |
500 | (if (> c 127) | |
114137b8 | 501 | (logxor c listify-key-sequence-1) |
cde6d7e3 RS |
502 | c))) |
503 | (append key nil)))) | |
504 | ||
53e5a4e8 RS |
505 | (defsubst eventp (obj) |
506 | "True if the argument is an event object." | |
507 | (or (integerp obj) | |
508 | (and (symbolp obj) | |
509 | (get obj 'event-symbol-elements)) | |
510 | (and (consp obj) | |
511 | (symbolp (car obj)) | |
512 | (get (car obj) 'event-symbol-elements)))) | |
513 | ||
514 | (defun event-modifiers (event) | |
515 | "Returns a list of symbols representing the modifier keys in event EVENT. | |
516 | The elements of the list may include `meta', `control', | |
32295976 RS |
517 | `shift', `hyper', `super', `alt', `click', `double', `triple', `drag', |
518 | and `down'." | |
53e5a4e8 RS |
519 | (let ((type event)) |
520 | (if (listp type) | |
521 | (setq type (car type))) | |
522 | (if (symbolp type) | |
523 | (cdr (get type 'event-symbol-elements)) | |
524 | (let ((list nil)) | |
da16e648 | 525 | (or (zerop (logand type ?\M-\^@)) |
53e5a4e8 | 526 | (setq list (cons 'meta list))) |
da16e648 | 527 | (or (and (zerop (logand type ?\C-\^@)) |
53e5a4e8 RS |
528 | (>= (logand type 127) 32)) |
529 | (setq list (cons 'control list))) | |
da16e648 | 530 | (or (and (zerop (logand type ?\S-\^@)) |
53e5a4e8 RS |
531 | (= (logand type 255) (downcase (logand type 255)))) |
532 | (setq list (cons 'shift list))) | |
da16e648 | 533 | (or (zerop (logand type ?\H-\^@)) |
53e5a4e8 | 534 | (setq list (cons 'hyper list))) |
da16e648 | 535 | (or (zerop (logand type ?\s-\^@)) |
53e5a4e8 | 536 | (setq list (cons 'super list))) |
da16e648 | 537 | (or (zerop (logand type ?\A-\^@)) |
53e5a4e8 RS |
538 | (setq list (cons 'alt list))) |
539 | list)))) | |
540 | ||
d63de416 RS |
541 | (defun event-basic-type (event) |
542 | "Returns the basic type of the given event (all modifiers removed). | |
7a0485b2 | 543 | The value is a printing character (not upper case) or a symbol." |
2b0f4ba5 JB |
544 | (if (consp event) |
545 | (setq event (car event))) | |
d63de416 RS |
546 | (if (symbolp event) |
547 | (car (get event 'event-symbol-elements)) | |
548 | (let ((base (logand event (1- (lsh 1 18))))) | |
549 | (downcase (if (< base 32) (logior base 64) base))))) | |
550 | ||
0f03054a RS |
551 | (defsubst mouse-movement-p (object) |
552 | "Return non-nil if OBJECT is a mouse movement event." | |
553 | (and (consp object) | |
554 | (eq (car object) 'mouse-movement))) | |
555 | ||
556 | (defsubst event-start (event) | |
557 | "Return the starting position of EVENT. | |
558 | If EVENT is a mouse press or a mouse click, this returns the location | |
559 | of the event. | |
560 | If EVENT is a drag, this returns the drag's starting position. | |
561 | The return value is of the form | |
e55c21be | 562 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
563 | The `posn-' functions access elements of such lists." |
564 | (nth 1 event)) | |
565 | ||
566 | (defsubst event-end (event) | |
567 | "Return the ending location of EVENT. EVENT should be a click or drag event. | |
568 | If EVENT is a click event, this function is the same as `event-start'. | |
569 | The return value is of the form | |
e55c21be | 570 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 571 | The `posn-' functions access elements of such lists." |
69b95560 | 572 | (nth (if (consp (nth 2 event)) 2 1) event)) |
0f03054a | 573 | |
32295976 RS |
574 | (defsubst event-click-count (event) |
575 | "Return the multi-click count of EVENT, a click or drag event. | |
576 | The return value is a positive integer." | |
577 | (if (integerp (nth 2 event)) (nth 2 event) 1)) | |
578 | ||
0f03054a RS |
579 | (defsubst posn-window (position) |
580 | "Return the window in POSITION. | |
581 | POSITION should be a list of the form | |
e55c21be | 582 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
583 | as returned by the `event-start' and `event-end' functions." |
584 | (nth 0 position)) | |
585 | ||
586 | (defsubst posn-point (position) | |
587 | "Return the buffer location in POSITION. | |
588 | POSITION should be a list of the form | |
e55c21be | 589 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a | 590 | as returned by the `event-start' and `event-end' functions." |
15db4e0e JB |
591 | (if (consp (nth 1 position)) |
592 | (car (nth 1 position)) | |
593 | (nth 1 position))) | |
0f03054a | 594 | |
e55c21be RS |
595 | (defsubst posn-x-y (position) |
596 | "Return the x and y coordinates in POSITION. | |
0f03054a | 597 | POSITION should be a list of the form |
e55c21be | 598 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
0f03054a RS |
599 | as returned by the `event-start' and `event-end' functions." |
600 | (nth 2 position)) | |
601 | ||
ed627e08 | 602 | (defun posn-col-row (position) |
dbbcac56 | 603 | "Return the column and row in POSITION, measured in characters. |
e55c21be RS |
604 | POSITION should be a list of the form |
605 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) | |
ed627e08 RS |
606 | as returned by the `event-start' and `event-end' functions. |
607 | For a scroll-bar event, the result column is 0, and the row | |
608 | corresponds to the vertical position of the click in the scroll bar." | |
609 | (let ((pair (nth 2 position)) | |
610 | (window (posn-window position))) | |
dbbcac56 KH |
611 | (if (eq (if (consp (nth 1 position)) |
612 | (car (nth 1 position)) | |
613 | (nth 1 position)) | |
ed627e08 RS |
614 | 'vertical-scroll-bar) |
615 | (cons 0 (scroll-bar-scale pair (1- (window-height window)))) | |
dbbcac56 KH |
616 | (if (eq (if (consp (nth 1 position)) |
617 | (car (nth 1 position)) | |
618 | (nth 1 position)) | |
ed627e08 RS |
619 | 'horizontal-scroll-bar) |
620 | (cons (scroll-bar-scale pair (window-width window)) 0) | |
9ba60df9 RS |
621 | (let* ((frame (if (framep window) window (window-frame window))) |
622 | (x (/ (car pair) (frame-char-width frame))) | |
623 | (y (/ (cdr pair) (frame-char-height frame)))) | |
ed627e08 | 624 | (cons x y)))))) |
e55c21be | 625 | |
0f03054a RS |
626 | (defsubst posn-timestamp (position) |
627 | "Return the timestamp of POSITION. | |
628 | POSITION should be a list of the form | |
e55c21be | 629 | (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP) |
f415c00c | 630 | as returned by the `event-start' and `event-end' functions." |
0f03054a | 631 | (nth 3 position)) |
9a5336ae | 632 | |
0f03054a | 633 | \f |
9a5336ae JB |
634 | ;;;; Obsolescent names for functions. |
635 | ||
059184dd ER |
636 | (defalias 'dot 'point) |
637 | (defalias 'dot-marker 'point-marker) | |
638 | (defalias 'dot-min 'point-min) | |
639 | (defalias 'dot-max 'point-max) | |
640 | (defalias 'window-dot 'window-point) | |
641 | (defalias 'set-window-dot 'set-window-point) | |
642 | (defalias 'read-input 'read-string) | |
643 | (defalias 'send-string 'process-send-string) | |
644 | (defalias 'send-region 'process-send-region) | |
645 | (defalias 'show-buffer 'set-window-buffer) | |
646 | (defalias 'buffer-flush-undo 'buffer-disable-undo) | |
647 | (defalias 'eval-current-buffer 'eval-buffer) | |
648 | (defalias 'compiled-function-p 'byte-code-function-p) | |
ae1cc031 | 649 | (defalias 'define-function 'defalias) |
be9b65ac | 650 | |
0cba3a0f | 651 | (defalias 'sref 'aref) |
2598a293 SM |
652 | (make-obsolete 'sref 'aref "20.4") |
653 | (make-obsolete 'char-bytes "Now this function always returns 1" "20.4") | |
6bb762b3 | 654 | |
676927b7 PJ |
655 | (defun insert-string (&rest args) |
656 | "Mocklisp-compatibility insert function. | |
657 | Like the function `insert' except that any argument that is a number | |
658 | is converted into a string by expressing it in decimal." | |
659 | (dolist (el args) | |
660 | (insert (if (integerp el) (number-to-string el) el)))) | |
661 | ||
662 | (make-obsolete 'insert-string 'insert "21.3") | |
663 | ||
9a5336ae JB |
664 | ;; Some programs still use this as a function. |
665 | (defun baud-rate () | |
bcacc42c RS |
666 | "Obsolete function returning the value of the `baud-rate' variable. |
667 | Please convert your programs to use the variable `baud-rate' directly." | |
9a5336ae JB |
668 | baud-rate) |
669 | ||
0a5c0893 MB |
670 | (defalias 'focus-frame 'ignore) |
671 | (defalias 'unfocus-frame 'ignore) | |
9a5336ae JB |
672 | \f |
673 | ;;;; Alternate names for functions - these are not being phased out. | |
674 | ||
059184dd ER |
675 | (defalias 'string= 'string-equal) |
676 | (defalias 'string< 'string-lessp) | |
677 | (defalias 'move-marker 'set-marker) | |
059184dd ER |
678 | (defalias 'rplaca 'setcar) |
679 | (defalias 'rplacd 'setcdr) | |
eb8c3be9 | 680 | (defalias 'beep 'ding) ;preserve lingual purity |
059184dd ER |
681 | (defalias 'indent-to-column 'indent-to) |
682 | (defalias 'backward-delete-char 'delete-backward-char) | |
683 | (defalias 'search-forward-regexp (symbol-function 're-search-forward)) | |
684 | (defalias 'search-backward-regexp (symbol-function 're-search-backward)) | |
685 | (defalias 'int-to-string 'number-to-string) | |
024ae2c6 | 686 | (defalias 'store-match-data 'set-match-data) |
d6c22d46 | 687 | ;; These are the XEmacs names: |
475fb2fb KH |
688 | (defalias 'point-at-eol 'line-end-position) |
689 | (defalias 'point-at-bol 'line-beginning-position) | |
37f6661a JB |
690 | |
691 | ;;; Should this be an obsolete name? If you decide it should, you get | |
692 | ;;; to go through all the sources and change them. | |
059184dd | 693 | (defalias 'string-to-int 'string-to-number) |
be9b65ac | 694 | \f |
9a5336ae | 695 | ;;;; Hook manipulation functions. |
be9b65ac | 696 | |
0e4d378b RS |
697 | (defun make-local-hook (hook) |
698 | "Make the hook HOOK local to the current buffer. | |
71c78f01 RS |
699 | The return value is HOOK. |
700 | ||
c344cf32 SM |
701 | You never need to call this function now that `add-hook' does it for you |
702 | if its LOCAL argument is non-nil. | |
703 | ||
0e4d378b RS |
704 | When a hook is local, its local and global values |
705 | work in concert: running the hook actually runs all the hook | |
706 | functions listed in *either* the local value *or* the global value | |
707 | of the hook variable. | |
708 | ||
08b1f8a1 | 709 | This function works by making t a member of the buffer-local value, |
7dd1926e RS |
710 | which acts as a flag to run the hook functions in the default value as |
711 | well. This works for all normal hooks, but does not work for most | |
712 | non-normal hooks yet. We will be changing the callers of non-normal | |
713 | hooks so that they can handle localness; this has to be done one by | |
714 | one. | |
715 | ||
716 | This function does nothing if HOOK is already local in the current | |
717 | buffer. | |
0e4d378b RS |
718 | |
719 | Do not use `make-local-variable' to make a hook variable buffer-local." | |
720 | (if (local-variable-p hook) | |
721 | nil | |
722 | (or (boundp hook) (set hook nil)) | |
723 | (make-local-variable hook) | |
71c78f01 RS |
724 | (set hook (list t))) |
725 | hook) | |
08b1f8a1 | 726 | (make-obsolete 'make-local-hook "Not necessary any more." "21.1") |
0e4d378b RS |
727 | |
728 | (defun add-hook (hook function &optional append local) | |
32295976 RS |
729 | "Add to the value of HOOK the function FUNCTION. |
730 | FUNCTION is not added if already present. | |
731 | FUNCTION is added (if necessary) at the beginning of the hook list | |
732 | unless the optional argument APPEND is non-nil, in which case | |
733 | FUNCTION is added at the end. | |
734 | ||
0e4d378b RS |
735 | The optional fourth argument, LOCAL, if non-nil, says to modify |
736 | the hook's buffer-local value rather than its default value. | |
61a3d8c4 RS |
737 | This makes the hook buffer-local if needed, and it makes t a member |
738 | of the buffer-local value. That acts as a flag to run the hook | |
739 | functions in the default value as well as in the local value. | |
0e4d378b | 740 | |
32295976 RS |
741 | HOOK should be a symbol, and FUNCTION may be any valid function. If |
742 | HOOK is void, it is first set to nil. If HOOK's value is a single | |
aa09b5ca | 743 | function, it is changed to a list of functions." |
be9b65ac | 744 | (or (boundp hook) (set hook nil)) |
0e4d378b | 745 | (or (default-boundp hook) (set-default hook nil)) |
08b1f8a1 GM |
746 | (if local (unless (local-variable-if-set-p hook) |
747 | (set (make-local-variable hook) (list t))) | |
8947a5e2 SM |
748 | ;; Detect the case where make-local-variable was used on a hook |
749 | ;; and do what we used to do. | |
750 | (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) | |
751 | (setq local t))) | |
752 | (let ((hook-value (if local (symbol-value hook) (default-value hook)))) | |
753 | ;; If the hook value is a single function, turn it into a list. | |
754 | (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda)) | |
2248c40d | 755 | (setq hook-value (list hook-value))) |
8947a5e2 SM |
756 | ;; Do the actual addition if necessary |
757 | (unless (member function hook-value) | |
758 | (setq hook-value | |
759 | (if append | |
760 | (append hook-value (list function)) | |
761 | (cons function hook-value)))) | |
762 | ;; Set the actual variable | |
763 | (if local (set hook hook-value) (set-default hook hook-value)))) | |
0e4d378b RS |
764 | |
765 | (defun remove-hook (hook function &optional local) | |
24980d16 RS |
766 | "Remove from the value of HOOK the function FUNCTION. |
767 | HOOK should be a symbol, and FUNCTION may be any valid function. If | |
768 | FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the | |
0e4d378b RS |
769 | list of hooks to run in HOOK, then nothing is done. See `add-hook'. |
770 | ||
771 | The optional third argument, LOCAL, if non-nil, says to modify | |
772 | the hook's buffer-local value rather than its default value. | |
08b1f8a1 | 773 | This makes the hook buffer-local if needed." |
8947a5e2 SM |
774 | (or (boundp hook) (set hook nil)) |
775 | (or (default-boundp hook) (set-default hook nil)) | |
08b1f8a1 GM |
776 | (if local (unless (local-variable-if-set-p hook) |
777 | (set (make-local-variable hook) (list t))) | |
8947a5e2 SM |
778 | ;; Detect the case where make-local-variable was used on a hook |
779 | ;; and do what we used to do. | |
780 | (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) | |
781 | (setq local t))) | |
782 | (let ((hook-value (if local (symbol-value hook) (default-value hook)))) | |
e4da9c1c SM |
783 | ;; Remove the function, for both the list and the non-list cases. |
784 | (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda)) | |
785 | (if (equal hook-value function) (setq hook-value nil)) | |
786 | (setq hook-value (delete function (copy-sequence hook-value)))) | |
8947a5e2 SM |
787 | ;; If the function is on the global hook, we need to shadow it locally |
788 | ;;(when (and local (member function (default-value hook)) | |
789 | ;; (not (member (cons 'not function) hook-value))) | |
790 | ;; (push (cons 'not function) hook-value)) | |
791 | ;; Set the actual variable | |
792 | (if local (set hook hook-value) (set-default hook hook-value)))) | |
6e3af630 | 793 | |
c8bfa689 | 794 | (defun add-to-list (list-var element &optional append) |
8851c1f0 | 795 | "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet. |
9f0b1f09 | 796 | The test for presence of ELEMENT is done with `equal'. |
c8bfa689 MB |
797 | If ELEMENT is added, it is added at the beginning of the list, |
798 | unless the optional argument APPEND is non-nil, in which case | |
799 | ELEMENT is added at the end. | |
508bcbca | 800 | |
daebae3d PJ |
801 | The return value is the new value of LIST-VAR. |
802 | ||
8851c1f0 RS |
803 | If you want to use `add-to-list' on a variable that is not defined |
804 | until a certain package is loaded, you should put the call to `add-to-list' | |
805 | into a hook function that will be run only after loading the package. | |
806 | `eval-after-load' provides one way to do this. In some cases | |
807 | other hooks, such as major mode hooks, can do the job." | |
15171a06 KH |
808 | (if (member element (symbol-value list-var)) |
809 | (symbol-value list-var) | |
c8bfa689 MB |
810 | (set list-var |
811 | (if append | |
812 | (append (symbol-value list-var) (list element)) | |
813 | (cons element (symbol-value list-var)))))) | |
448a0170 MB |
814 | |
815 | \f | |
816 | ;;; Load history | |
817 | ||
818 | (defvar symbol-file-load-history-loaded nil | |
819 | "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'. | |
820 | That file records the part of `load-history' for preloaded files, | |
821 | which is cleared out before dumping to make Emacs smaller.") | |
822 | ||
823 | (defun load-symbol-file-load-history () | |
824 | "Load the file `fns-VERSION.el' in `exec-directory' if not already done. | |
825 | That file records the part of `load-history' for preloaded files, | |
826 | which is cleared out before dumping to make Emacs smaller." | |
827 | (unless symbol-file-load-history-loaded | |
828 | (load (expand-file-name | |
829 | ;; fns-XX.YY.ZZ.el does not work on DOS filesystem. | |
830 | (if (eq system-type 'ms-dos) | |
831 | "fns.el" | |
832 | (format "fns-%s.el" emacs-version)) | |
833 | exec-directory) | |
834 | ;; The file name fns-%s.el already has a .el extension. | |
835 | nil nil t) | |
836 | (setq symbol-file-load-history-loaded t))) | |
837 | ||
838 | (defun symbol-file (function) | |
839 | "Return the input source from which FUNCTION was loaded. | |
840 | The value is normally a string that was passed to `load': | |
841 | either an absolute file name, or a library name | |
842 | \(with no directory name and no `.el' or `.elc' at the end). | |
843 | It can also be nil, if the definition is not associated with any file." | |
844 | (load-symbol-file-load-history) | |
845 | (let ((files load-history) | |
846 | file functions) | |
847 | (while files | |
848 | (if (memq function (cdr (car files))) | |
849 | (setq file (car (car files)) files nil)) | |
850 | (setq files (cdr files))) | |
851 | file)) | |
852 | ||
be9b65ac | 853 | \f |
9a5336ae JB |
854 | ;;;; Specifying things to do after certain files are loaded. |
855 | ||
856 | (defun eval-after-load (file form) | |
857 | "Arrange that, if FILE is ever loaded, FORM will be run at that time. | |
858 | This makes or adds to an entry on `after-load-alist'. | |
90914938 | 859 | If FILE is already loaded, evaluate FORM right now. |
12c7071c | 860 | It does nothing if FORM is already on the list for FILE. |
19594307 DL |
861 | FILE must match exactly. Normally FILE is the name of a library, |
862 | with no directory or extension specified, since that is how `load' | |
a2d7836f SM |
863 | is normally called. |
864 | FILE can also be a feature (i.e. a symbol), in which case FORM is | |
865 | evaluated whenever that feature is `provide'd." | |
12c7071c | 866 | (let ((elt (assoc file after-load-alist))) |
a2d7836f SM |
867 | ;; Make sure there is an element for FILE. |
868 | (unless elt (setq elt (list file)) (push elt after-load-alist)) | |
869 | ;; Add FORM to the element if it isn't there. | |
870 | (unless (member form (cdr elt)) | |
871 | (nconc elt (list form)) | |
872 | ;; If the file has been loaded already, run FORM right away. | |
873 | (if (if (symbolp file) | |
874 | (featurep file) | |
875 | ;; Make sure `load-history' contains the files dumped with | |
876 | ;; Emacs for the case that FILE is one of them. | |
877 | (load-symbol-file-load-history) | |
878 | (assoc file load-history)) | |
879 | (eval form)))) | |
9a5336ae JB |
880 | form) |
881 | ||
882 | (defun eval-next-after-load (file) | |
883 | "Read the following input sexp, and run it whenever FILE is loaded. | |
884 | This makes or adds to an entry on `after-load-alist'. | |
885 | FILE should be the name of a library, with no directory name." | |
886 | (eval-after-load file (read))) | |
887 | ||
888 | \f | |
889 | ;;;; Input and display facilities. | |
890 | ||
77a5664f | 891 | (defvar read-quoted-char-radix 8 |
1ba764de | 892 | "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'. |
77a5664f RS |
893 | Legitimate radix values are 8, 10 and 16.") |
894 | ||
895 | (custom-declare-variable-early | |
896 | 'read-quoted-char-radix 8 | |
897 | "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'. | |
1ba764de RS |
898 | Legitimate radix values are 8, 10 and 16." |
899 | :type '(choice (const 8) (const 10) (const 16)) | |
900 | :group 'editing-basics) | |
901 | ||
9a5336ae | 902 | (defun read-quoted-char (&optional prompt) |
2444730b RS |
903 | "Like `read-char', but do not allow quitting. |
904 | Also, if the first character read is an octal digit, | |
905 | we read any number of octal digits and return the | |
569b03f2 | 906 | specified character code. Any nondigit terminates the sequence. |
1ba764de | 907 | If the terminator is RET, it is discarded; |
2444730b RS |
908 | any other terminator is used itself as input. |
909 | ||
569b03f2 RS |
910 | The optional argument PROMPT specifies a string to use to prompt the user. |
911 | The variable `read-quoted-char-radix' controls which radix to use | |
912 | for numeric input." | |
2444730b RS |
913 | (let ((message-log-max nil) done (first t) (code 0) char) |
914 | (while (not done) | |
915 | (let ((inhibit-quit first) | |
42e636f0 KH |
916 | ;; Don't let C-h get the help message--only help function keys. |
917 | (help-char nil) | |
918 | (help-form | |
919 | "Type the special character you want to use, | |
2444730b | 920 | or the octal character code. |
1ba764de | 921 | RET terminates the character code and is discarded; |
2444730b | 922 | any other non-digit terminates the character code and is then used as input.")) |
b7de4d62 | 923 | (setq char (read-event (and prompt (format "%s-" prompt)) t)) |
9a5336ae | 924 | (if inhibit-quit (setq quit-flag nil))) |
4867f7b2 RS |
925 | ;; Translate TAB key into control-I ASCII character, and so on. |
926 | (and char | |
927 | (let ((translated (lookup-key function-key-map (vector char)))) | |
bf896a1b | 928 | (if (arrayp translated) |
4867f7b2 | 929 | (setq char (aref translated 0))))) |
9a5336ae | 930 | (cond ((null char)) |
1ba764de RS |
931 | ((not (integerp char)) |
932 | (setq unread-command-events (list char) | |
933 | done t)) | |
bf896a1b RS |
934 | ((/= (logand char ?\M-\^@) 0) |
935 | ;; Turn a meta-character into a character with the 0200 bit set. | |
936 | (setq code (logior (logand char (lognot ?\M-\^@)) 128) | |
937 | done t)) | |
1ba764de RS |
938 | ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix)))) |
939 | (setq code (+ (* code read-quoted-char-radix) (- char ?0))) | |
940 | (and prompt (setq prompt (message "%s %c" prompt char)))) | |
941 | ((and (<= ?a (downcase char)) | |
942 | (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix)))) | |
92304bc8 RS |
943 | (setq code (+ (* code read-quoted-char-radix) |
944 | (+ 10 (- (downcase char) ?a)))) | |
91a6acc3 | 945 | (and prompt (setq prompt (message "%s %c" prompt char)))) |
1ba764de | 946 | ((and (not first) (eq char ?\C-m)) |
2444730b RS |
947 | (setq done t)) |
948 | ((not first) | |
949 | (setq unread-command-events (list char) | |
950 | done t)) | |
951 | (t (setq code char | |
952 | done t))) | |
953 | (setq first nil)) | |
bf896a1b | 954 | code)) |
9a5336ae | 955 | |
44071d6b RS |
956 | (defun read-passwd (prompt &optional confirm default) |
957 | "Read a password, prompting with PROMPT. Echo `.' for each character typed. | |
e0e4cb7a | 958 | End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. |
44071d6b RS |
959 | Optional argument CONFIRM, if non-nil, then read it twice to make sure. |
960 | Optional DEFAULT is a default password to use instead of empty input." | |
961 | (if confirm | |
962 | (let (success) | |
963 | (while (not success) | |
964 | (let ((first (read-passwd prompt nil default)) | |
965 | (second (read-passwd "Confirm password: " nil default))) | |
966 | (if (equal first second) | |
fe10cef0 GM |
967 | (progn |
968 | (and (arrayp second) (fillarray second ?\0)) | |
969 | (setq success first)) | |
970 | (and (arrayp first) (fillarray first ?\0)) | |
971 | (and (arrayp second) (fillarray second ?\0)) | |
44071d6b RS |
972 | (message "Password not repeated accurately; please start over") |
973 | (sit-for 1)))) | |
974 | success) | |
975 | (let ((pass nil) | |
976 | (c 0) | |
977 | (echo-keystrokes 0) | |
978 | (cursor-in-echo-area t)) | |
979 | (while (progn (message "%s%s" | |
980 | prompt | |
981 | (make-string (length pass) ?.)) | |
42ccb7c8 | 982 | (setq c (read-char-exclusive nil t)) |
44071d6b | 983 | (and (/= c ?\r) (/= c ?\n) (/= c ?\e))) |
719349f6 | 984 | (clear-this-command-keys) |
44071d6b | 985 | (if (= c ?\C-u) |
fe10cef0 GM |
986 | (progn |
987 | (and (arrayp pass) (fillarray pass ?\0)) | |
988 | (setq pass "")) | |
44071d6b | 989 | (if (and (/= c ?\b) (/= c ?\177)) |
fe10cef0 GM |
990 | (let* ((new-char (char-to-string c)) |
991 | (new-pass (concat pass new-char))) | |
992 | (and (arrayp pass) (fillarray pass ?\0)) | |
993 | (fillarray new-char ?\0) | |
994 | (setq c ?\0) | |
995 | (setq pass new-pass)) | |
44071d6b | 996 | (if (> (length pass) 0) |
fe10cef0 GM |
997 | (let ((new-pass (substring pass 0 -1))) |
998 | (and (arrayp pass) (fillarray pass ?\0)) | |
999 | (setq pass new-pass)))))) | |
44071d6b RS |
1000 | (message nil) |
1001 | (or pass default "")))) | |
e0e4cb7a | 1002 | \f |
69cae2d4 RS |
1003 | (defmacro atomic-change-group (&rest body) |
1004 | "Perform BODY as an atomic change group. | |
1005 | This means that if BODY exits abnormally, | |
1006 | all of its changes to the current buffer are undone. | |
1007 | This works regadless of whether undo is enabled in the buffer. | |
1008 | ||
1009 | This mechanism is transparent to ordinary use of undo; | |
1010 | if undo is enabled in the buffer and BODY succeeds, the | |
1011 | user can undo the change normally." | |
1012 | (let ((handle (make-symbol "--change-group-handle--")) | |
1013 | (success (make-symbol "--change-group-success--"))) | |
1014 | `(let ((,handle (prepare-change-group)) | |
1015 | (,success nil)) | |
1016 | (unwind-protect | |
1017 | (progn | |
1018 | ;; This is inside the unwind-protect because | |
1019 | ;; it enables undo if that was disabled; we need | |
1020 | ;; to make sure that it gets disabled again. | |
1021 | (activate-change-group ,handle) | |
1022 | ,@body | |
1023 | (setq ,success t)) | |
1024 | ;; Either of these functions will disable undo | |
1025 | ;; if it was disabled before. | |
1026 | (if ,success | |
1027 | (accept-change-group ,handle) | |
1028 | (cancel-change-group ,handle)))))) | |
1029 | ||
1030 | (defun prepare-change-group (&optional buffer) | |
1031 | "Return a handle for the current buffer's state, for a change group. | |
1032 | If you specify BUFFER, make a handle for BUFFER's state instead. | |
1033 | ||
1034 | Pass the handle to `activate-change-group' afterward to initiate | |
1035 | the actual changes of the change group. | |
1036 | ||
1037 | To finish the change group, call either `accept-change-group' or | |
1038 | `cancel-change-group' passing the same handle as argument. Call | |
1039 | `accept-change-group' to accept the changes in the group as final; | |
1040 | call `cancel-change-group' to undo them all. You should use | |
1041 | `unwind-protect' to make sure the group is always finished. The call | |
1042 | to `activate-change-group' should be inside the `unwind-protect'. | |
1043 | Once you finish the group, don't use the handle again--don't try to | |
1044 | finish the same group twice. For a simple example of correct use, see | |
1045 | the source code of `atomic-change-group'. | |
1046 | ||
1047 | The handle records only the specified buffer. To make a multibuffer | |
1048 | change group, call this function once for each buffer you want to | |
1049 | cover, then use `nconc' to combine the returned values, like this: | |
1050 | ||
1051 | (nconc (prepare-change-group buffer-1) | |
1052 | (prepare-change-group buffer-2)) | |
1053 | ||
1054 | You can then activate that multibuffer change group with a single | |
1055 | call to `activate-change-group' and finish it with a single call | |
1056 | to `accept-change-group' or `cancel-change-group'." | |
1057 | ||
1058 | (list (cons (current-buffer) buffer-undo-list))) | |
1059 | ||
1060 | (defun activate-change-group (handle) | |
1061 | "Activate a change group made with `prepare-change-group' (which see)." | |
1062 | (dolist (elt handle) | |
1063 | (with-current-buffer (car elt) | |
1064 | (if (eq buffer-undo-list t) | |
1065 | (setq buffer-undo-list nil))))) | |
1066 | ||
1067 | (defun accept-change-group (handle) | |
1068 | "Finish a change group made with `prepare-change-group' (which see). | |
1069 | This finishes the change group by accepting its changes as final." | |
1070 | (dolist (elt handle) | |
1071 | (with-current-buffer (car elt) | |
1072 | (if (eq elt t) | |
1073 | (setq buffer-undo-list t))))) | |
1074 | ||
1075 | (defun cancel-change-group (handle) | |
1076 | "Finish a change group made with `prepare-change-group' (which see). | |
1077 | This finishes the change group by reverting all of its changes." | |
1078 | (dolist (elt handle) | |
1079 | (with-current-buffer (car elt) | |
1080 | (setq elt (cdr elt)) | |
1081 | (let ((old-car | |
1082 | (if (consp elt) (car elt))) | |
1083 | (old-cdr | |
1084 | (if (consp elt) (cdr elt)))) | |
1085 | ;; Temporarily truncate the undo log at ELT. | |
1086 | (when (consp elt) | |
1087 | (setcar elt nil) (setcdr elt nil)) | |
1088 | (unless (eq last-command 'undo) (undo-start)) | |
1089 | ;; Make sure there's no confusion. | |
1090 | (when (and (consp elt) (not (eq elt (last pending-undo-list)))) | |
1091 | (error "Undoing to some unrelated state")) | |
1092 | ;; Undo it all. | |
1093 | (while pending-undo-list (undo-more 1)) | |
1094 | ;; Reset the modified cons cell ELT to its original content. | |
1095 | (when (consp elt) | |
1096 | (setcar elt old-car) | |
1097 | (setcdr elt old-cdr)) | |
1098 | ;; Revert the undo info to what it was when we grabbed the state. | |
1099 | (setq buffer-undo-list elt))))) | |
1100 | \f | |
9a5336ae | 1101 | (defun force-mode-line-update (&optional all) |
dc756612 RS |
1102 | "Force the mode line of the current buffer to be redisplayed. |
1103 | With optional non-nil ALL, force redisplay of all mode lines." | |
9a5336ae JB |
1104 | (if all (save-excursion (set-buffer (other-buffer)))) |
1105 | (set-buffer-modified-p (buffer-modified-p))) | |
1106 | ||
aa3b4ded | 1107 | (defun momentary-string-display (string pos &optional exit-char message) |
be9b65ac DL |
1108 | "Momentarily display STRING in the buffer at POS. |
1109 | Display remains until next character is typed. | |
1110 | If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed; | |
1111 | otherwise it is then available as input (as a command if nothing else). | |
1112 | Display MESSAGE (optional fourth arg) in the echo area. | |
1113 | If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there." | |
1114 | (or exit-char (setq exit-char ?\ )) | |
c306e0e0 | 1115 | (let ((inhibit-read-only t) |
ca2ec1c5 RS |
1116 | ;; Don't modify the undo list at all. |
1117 | (buffer-undo-list t) | |
be9b65ac DL |
1118 | (modified (buffer-modified-p)) |
1119 | (name buffer-file-name) | |
1120 | insert-end) | |
1121 | (unwind-protect | |
1122 | (progn | |
1123 | (save-excursion | |
1124 | (goto-char pos) | |
1125 | ;; defeat file locking... don't try this at home, kids! | |
1126 | (setq buffer-file-name nil) | |
1127 | (insert-before-markers string) | |
3eec84bf RS |
1128 | (setq insert-end (point)) |
1129 | ;; If the message end is off screen, recenter now. | |
024ae2c6 | 1130 | (if (< (window-end nil t) insert-end) |
3eec84bf RS |
1131 | (recenter (/ (window-height) 2))) |
1132 | ;; If that pushed message start off the screen, | |
1133 | ;; scroll to start it at the top of the screen. | |
1134 | (move-to-window-line 0) | |
1135 | (if (> (point) pos) | |
1136 | (progn | |
1137 | (goto-char pos) | |
1138 | (recenter 0)))) | |
be9b65ac DL |
1139 | (message (or message "Type %s to continue editing.") |
1140 | (single-key-description exit-char)) | |
3547c855 | 1141 | (let ((char (read-event))) |
be9b65ac | 1142 | (or (eq char exit-char) |
dbc4e1c1 | 1143 | (setq unread-command-events (list char))))) |
be9b65ac DL |
1144 | (if insert-end |
1145 | (save-excursion | |
1146 | (delete-region pos insert-end))) | |
1147 | (setq buffer-file-name name) | |
1148 | (set-buffer-modified-p modified)))) | |
1149 | ||
9a5336ae | 1150 | \f |
aa3b4ded SM |
1151 | ;;;; Overlay operations |
1152 | ||
1153 | (defun copy-overlay (o) | |
1154 | "Return a copy of overlay O." | |
1155 | (let ((o1 (make-overlay (overlay-start o) (overlay-end o) | |
1156 | ;; FIXME: there's no easy way to find the | |
1157 | ;; insertion-type of the two markers. | |
1158 | (overlay-buffer o))) | |
1159 | (props (overlay-properties o))) | |
1160 | (while props | |
1161 | (overlay-put o1 (pop props) (pop props))) | |
1162 | o1)) | |
1163 | ||
1164 | (defun remove-overlays (beg end name val) | |
1165 | "Clear BEG and END of overlays whose property NAME has value VAL. | |
1166 | Overlays might be moved and or split." | |
1167 | (if (< end beg) | |
1168 | (setq beg (prog1 end (setq end beg)))) | |
1169 | (save-excursion | |
1170 | (dolist (o (overlays-in beg end)) | |
1171 | (when (eq (overlay-get o name) val) | |
1172 | ;; Either push this overlay outside beg...end | |
1173 | ;; or split it to exclude beg...end | |
1174 | ;; or delete it entirely (if it is contained in beg...end). | |
1175 | (if (< (overlay-start o) beg) | |
1176 | (if (> (overlay-end o) end) | |
1177 | (progn | |
1178 | (move-overlay (copy-overlay o) | |
1179 | (overlay-start o) beg) | |
1180 | (move-overlay o end (overlay-end o))) | |
1181 | (move-overlay o (overlay-start o) beg)) | |
1182 | (if (> (overlay-end o) end) | |
1183 | (move-overlay o end (overlay-end o)) | |
1184 | (delete-overlay o))))))) | |
c5802acf | 1185 | \f |
9a5336ae JB |
1186 | ;;;; Miscellanea. |
1187 | ||
448b61c9 RS |
1188 | ;; A number of major modes set this locally. |
1189 | ;; Give it a global value to avoid compiler warnings. | |
1190 | (defvar font-lock-defaults nil) | |
1191 | ||
4fb17037 RS |
1192 | (defvar suspend-hook nil |
1193 | "Normal hook run by `suspend-emacs', before suspending.") | |
1194 | ||
1195 | (defvar suspend-resume-hook nil | |
1196 | "Normal hook run by `suspend-emacs', after Emacs is continued.") | |
1197 | ||
784bc7cd RS |
1198 | (defvar temp-buffer-show-hook nil |
1199 | "Normal hook run by `with-output-to-temp-buffer' after displaying the buffer. | |
1200 | When the hook runs, the temporary buffer is current, and the window it | |
1201 | was displayed in is selected. This hook is normally set up with a | |
1202 | function to make the buffer read only, and find function names and | |
1203 | variable names in it, provided the major mode is still Help mode.") | |
1204 | ||
1205 | (defvar temp-buffer-setup-hook nil | |
1206 | "Normal hook run by `with-output-to-temp-buffer' at the start. | |
1207 | When the hook runs, the temporary buffer is current. | |
1208 | This hook is normally set up with a function to put the buffer in Help | |
1209 | mode.") | |
1210 | ||
448b61c9 RS |
1211 | ;; Avoid compiler warnings about this variable, |
1212 | ;; which has a special meaning on certain system types. | |
1213 | (defvar buffer-file-type nil | |
1214 | "Non-nil if the visited file is a binary file. | |
1215 | This variable is meaningful on MS-DOG and Windows NT. | |
1216 | On those systems, it is automatically local in every buffer. | |
1217 | On other systems, this variable is normally always nil.") | |
1218 | ||
a860d25f | 1219 | ;; This should probably be written in C (i.e., without using `walk-windows'). |
63503b24 | 1220 | (defun get-buffer-window-list (buffer &optional minibuf frame) |
a860d25f | 1221 | "Return windows currently displaying BUFFER, or nil if none. |
63503b24 | 1222 | See `walk-windows' for the meaning of MINIBUF and FRAME." |
43c5ac8c | 1223 | (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows) |
a860d25f SM |
1224 | (walk-windows (function (lambda (window) |
1225 | (if (eq (window-buffer window) buffer) | |
1226 | (setq windows (cons window windows))))) | |
63503b24 | 1227 | minibuf frame) |
a860d25f SM |
1228 | windows)) |
1229 | ||
f9269e19 RS |
1230 | (defun ignore (&rest ignore) |
1231 | "Do nothing and return nil. | |
1232 | This function accepts any number of arguments, but ignores them." | |
c0f1a4f6 | 1233 | (interactive) |
9a5336ae JB |
1234 | nil) |
1235 | ||
1236 | (defun error (&rest args) | |
aa308ce2 RS |
1237 | "Signal an error, making error message by passing all args to `format'. |
1238 | In Emacs, the convention is that error messages start with a capital | |
1239 | letter but *do not* end with a period. Please follow this convention | |
1240 | for the sake of consistency." | |
9a5336ae JB |
1241 | (while t |
1242 | (signal 'error (list (apply 'format args))))) | |
1243 | ||
cef7ae6e | 1244 | (defalias 'user-original-login-name 'user-login-name) |
9a5336ae | 1245 | |
be9b65ac DL |
1246 | (defun start-process-shell-command (name buffer &rest args) |
1247 | "Start a program in a subprocess. Return the process object for it. | |
1248 | Args are NAME BUFFER COMMAND &rest COMMAND-ARGS. | |
1249 | NAME is name for process. It is modified if necessary to make it unique. | |
1250 | BUFFER is the buffer or (buffer-name) to associate with the process. | |
1251 | Process output goes at end of that buffer, unless you specify | |
1252 | an output stream or filter function to handle the output. | |
1253 | BUFFER may be also nil, meaning that this process is not associated | |
1254 | with any buffer | |
1255 | Third arg is command name, the name of a shell command. | |
1256 | Remaining arguments are the arguments for the command. | |
4f1d6310 | 1257 | Wildcards and redirection are handled as usual in the shell." |
a247bf21 KH |
1258 | (cond |
1259 | ((eq system-type 'vax-vms) | |
1260 | (apply 'start-process name buffer args)) | |
b59f6d7a RS |
1261 | ;; We used to use `exec' to replace the shell with the command, |
1262 | ;; but that failed to handle (...) and semicolon, etc. | |
a247bf21 KH |
1263 | (t |
1264 | (start-process name buffer shell-file-name shell-command-switch | |
b59f6d7a | 1265 | (mapconcat 'identity args " "))))) |
93aca633 MB |
1266 | |
1267 | (defun call-process-shell-command (command &optional infile buffer display | |
1268 | &rest args) | |
1269 | "Execute the shell command COMMAND synchronously in separate process. | |
1270 | The remaining arguments are optional. | |
1271 | The program's input comes from file INFILE (nil means `/dev/null'). | |
1272 | Insert output in BUFFER before point; t means current buffer; | |
1273 | nil for BUFFER means discard it; 0 means discard and don't wait. | |
1274 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case, | |
1275 | REAL-BUFFER says what to do with standard output, as above, | |
1276 | while STDERR-FILE says what to do with standard error in the child. | |
1277 | STDERR-FILE may be nil (discard standard error output), | |
1278 | t (mix it with ordinary output), or a file name string. | |
1279 | ||
1280 | Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted. | |
1281 | Remaining arguments are strings passed as additional arguments for COMMAND. | |
1282 | Wildcards and redirection are handled as usual in the shell. | |
1283 | ||
1284 | If BUFFER is 0, `call-process-shell-command' returns immediately with value nil. | |
1285 | Otherwise it waits for COMMAND to terminate and returns a numeric exit | |
1286 | status or a signal description string. | |
1287 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again." | |
1288 | (cond | |
1289 | ((eq system-type 'vax-vms) | |
1290 | (apply 'call-process command infile buffer display args)) | |
1291 | ;; We used to use `exec' to replace the shell with the command, | |
1292 | ;; but that failed to handle (...) and semicolon, etc. | |
1293 | (t | |
1294 | (call-process shell-file-name | |
1295 | infile buffer display | |
1296 | shell-command-switch | |
1297 | (mapconcat 'identity (cons command args) " "))))) | |
a7ed4c2a | 1298 | \f |
a7f284ec RS |
1299 | (defmacro with-current-buffer (buffer &rest body) |
1300 | "Execute the forms in BODY with BUFFER as the current buffer. | |
a2fdb55c EN |
1301 | The value returned is the value of the last form in BODY. |
1302 | See also `with-temp-buffer'." | |
ce87039d SM |
1303 | (cons 'save-current-buffer |
1304 | (cons (list 'set-buffer buffer) | |
1305 | body))) | |
a7f284ec | 1306 | |
e5bb8a8c SM |
1307 | (defmacro with-temp-file (file &rest body) |
1308 | "Create a new buffer, evaluate BODY there, and write the buffer to FILE. | |
1309 | The value returned is the value of the last form in BODY. | |
a2fdb55c | 1310 | See also `with-temp-buffer'." |
a7ed4c2a | 1311 | (let ((temp-file (make-symbol "temp-file")) |
a2fdb55c EN |
1312 | (temp-buffer (make-symbol "temp-buffer"))) |
1313 | `(let ((,temp-file ,file) | |
1314 | (,temp-buffer | |
1315 | (get-buffer-create (generate-new-buffer-name " *temp file*")))) | |
1316 | (unwind-protect | |
1317 | (prog1 | |
1318 | (with-current-buffer ,temp-buffer | |
e5bb8a8c | 1319 | ,@body) |
a2fdb55c EN |
1320 | (with-current-buffer ,temp-buffer |
1321 | (widen) | |
1322 | (write-region (point-min) (point-max) ,temp-file nil 0))) | |
1323 | (and (buffer-name ,temp-buffer) | |
1324 | (kill-buffer ,temp-buffer)))))) | |
1325 | ||
e5bb8a8c | 1326 | (defmacro with-temp-message (message &rest body) |
a600effe | 1327 | "Display MESSAGE temporarily if non-nil while BODY is evaluated. |
e5bb8a8c SM |
1328 | The original message is restored to the echo area after BODY has finished. |
1329 | The value returned is the value of the last form in BODY. | |
a600effe SM |
1330 | MESSAGE is written to the message log buffer if `message-log-max' is non-nil. |
1331 | If MESSAGE is nil, the echo area and message log buffer are unchanged. | |
1332 | Use a MESSAGE of \"\" to temporarily clear the echo area." | |
110201c8 SM |
1333 | (let ((current-message (make-symbol "current-message")) |
1334 | (temp-message (make-symbol "with-temp-message"))) | |
1335 | `(let ((,temp-message ,message) | |
1336 | (,current-message)) | |
e5bb8a8c SM |
1337 | (unwind-protect |
1338 | (progn | |
110201c8 SM |
1339 | (when ,temp-message |
1340 | (setq ,current-message (current-message)) | |
aadf7ff3 | 1341 | (message "%s" ,temp-message)) |
e5bb8a8c | 1342 | ,@body) |
cad84646 RS |
1343 | (and ,temp-message |
1344 | (if ,current-message | |
1345 | (message "%s" ,current-message) | |
1346 | (message nil))))))) | |
e5bb8a8c SM |
1347 | |
1348 | (defmacro with-temp-buffer (&rest body) | |
1349 | "Create a temporary buffer, and evaluate BODY there like `progn'. | |
a2fdb55c EN |
1350 | See also `with-temp-file' and `with-output-to-string'." |
1351 | (let ((temp-buffer (make-symbol "temp-buffer"))) | |
1352 | `(let ((,temp-buffer | |
1353 | (get-buffer-create (generate-new-buffer-name " *temp*")))) | |
1354 | (unwind-protect | |
1355 | (with-current-buffer ,temp-buffer | |
e5bb8a8c | 1356 | ,@body) |
a2fdb55c EN |
1357 | (and (buffer-name ,temp-buffer) |
1358 | (kill-buffer ,temp-buffer)))))) | |
1359 | ||
5db7925d RS |
1360 | (defmacro with-output-to-string (&rest body) |
1361 | "Execute BODY, return the text it sent to `standard-output', as a string." | |
a2fdb55c EN |
1362 | `(let ((standard-output |
1363 | (get-buffer-create (generate-new-buffer-name " *string-output*")))) | |
5db7925d RS |
1364 | (let ((standard-output standard-output)) |
1365 | ,@body) | |
a2fdb55c EN |
1366 | (with-current-buffer standard-output |
1367 | (prog1 | |
1368 | (buffer-string) | |
1369 | (kill-buffer nil))))) | |
2ec9c94e | 1370 | |
0764e16f SM |
1371 | (defmacro with-local-quit (&rest body) |
1372 | "Execute BODY with `inhibit-quit' temporarily bound to nil." | |
1373 | `(condition-case nil | |
1374 | (let ((inhibit-quit nil)) | |
1375 | ,@body) | |
1376 | (quit (setq quit-flag t)))) | |
1377 | ||
2ec9c94e RS |
1378 | (defmacro combine-after-change-calls (&rest body) |
1379 | "Execute BODY, but don't call the after-change functions till the end. | |
1380 | If BODY makes changes in the buffer, they are recorded | |
1381 | and the functions on `after-change-functions' are called several times | |
1382 | when BODY is finished. | |
31aa282e | 1383 | The return value is the value of the last form in BODY. |
2ec9c94e RS |
1384 | |
1385 | If `before-change-functions' is non-nil, then calls to the after-change | |
1386 | functions can't be deferred, so in that case this macro has no effect. | |
1387 | ||
1388 | Do not alter `after-change-functions' or `before-change-functions' | |
1389 | in BODY." | |
1390 | `(unwind-protect | |
1391 | (let ((combine-after-change-calls t)) | |
1392 | . ,body) | |
1393 | (combine-after-change-execute))) | |
1394 | ||
c834b52c | 1395 | |
a13fe4c5 SM |
1396 | (defvar delay-mode-hooks nil |
1397 | "If non-nil, `run-mode-hooks' should delay running the hooks.") | |
1398 | (defvar delayed-mode-hooks nil | |
1399 | "List of delayed mode hooks waiting to be run.") | |
1400 | (make-variable-buffer-local 'delayed-mode-hooks) | |
1401 | ||
1402 | (defun run-mode-hooks (&rest hooks) | |
1403 | "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS. | |
1404 | Execution is delayed if `delay-mode-hooks' is non-nil. | |
1405 | Major mode functions should use this." | |
1406 | (if delay-mode-hooks | |
1407 | ;; Delaying case. | |
1408 | (dolist (hook hooks) | |
1409 | (push hook delayed-mode-hooks)) | |
1410 | ;; Normal case, just run the hook as before plus any delayed hooks. | |
1411 | (setq hooks (nconc (nreverse delayed-mode-hooks) hooks)) | |
1412 | (setq delayed-mode-hooks nil) | |
1413 | (apply 'run-hooks hooks))) | |
1414 | ||
1415 | (defmacro delay-mode-hooks (&rest body) | |
1416 | "Execute BODY, but delay any `run-mode-hooks'. | |
1417 | Only affects hooks run in the current buffer." | |
1418 | `(progn | |
1419 | (make-local-variable 'delay-mode-hooks) | |
1420 | (let ((delay-mode-hooks t)) | |
1421 | ,@body))) | |
1422 | ||
31ca596b RS |
1423 | ;; PUBLIC: find if the current mode derives from another. |
1424 | ||
1425 | (defun derived-mode-p (&rest modes) | |
1426 | "Non-nil if the current major mode is derived from one of MODES. | |
1427 | Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
1428 | (let ((parent major-mode)) | |
1429 | (while (and (not (memq parent modes)) | |
1430 | (setq parent (get parent 'derived-mode-parent)))) | |
1431 | parent)) | |
1432 | ||
7e8539cc RS |
1433 | (defmacro with-syntax-table (table &rest body) |
1434 | "Evaluate BODY with syntax table of current buffer set to a copy of TABLE. | |
1435 | The syntax table of the current buffer is saved, BODY is evaluated, and the | |
1436 | saved table is restored, even in case of an abnormal exit. | |
1437 | Value is what BODY returns." | |
b3f07093 RS |
1438 | (let ((old-table (make-symbol "table")) |
1439 | (old-buffer (make-symbol "buffer"))) | |
7e8539cc RS |
1440 | `(let ((,old-table (syntax-table)) |
1441 | (,old-buffer (current-buffer))) | |
1442 | (unwind-protect | |
1443 | (progn | |
1444 | (set-syntax-table (copy-syntax-table ,table)) | |
1445 | ,@body) | |
1446 | (save-current-buffer | |
1447 | (set-buffer ,old-buffer) | |
1448 | (set-syntax-table ,old-table)))))) | |
a2fdb55c | 1449 | \f |
c7ca41e6 RS |
1450 | (defvar save-match-data-internal) |
1451 | ||
1452 | ;; We use save-match-data-internal as the local variable because | |
1453 | ;; that works ok in practice (people should not use that variable elsewhere). | |
1454 | ;; We used to use an uninterned symbol; the compiler handles that properly | |
1455 | ;; now, but it generates slower code. | |
9a5336ae | 1456 | (defmacro save-match-data (&rest body) |
e4d03691 JB |
1457 | "Execute the BODY forms, restoring the global value of the match data. |
1458 | The value returned is the value of the last form in BODY." | |
64ed733a PE |
1459 | ;; It is better not to use backquote here, |
1460 | ;; because that makes a bootstrapping problem | |
1461 | ;; if you need to recompile all the Lisp files using interpreted code. | |
1462 | (list 'let | |
1463 | '((save-match-data-internal (match-data))) | |
1464 | (list 'unwind-protect | |
1465 | (cons 'progn body) | |
1466 | '(set-match-data save-match-data-internal)))) | |
993713ce | 1467 | |
e1354176 RS |
1468 | (defun substring-no-properties (string &optional from to) |
1469 | "Return a substring of STRING, with no text properties. | |
1470 | The substring starts at index FROM and ends before TO. | |
1471 | If FROM is nil or omitted, it defaults to the beginning of STRING. | |
1472 | If TO is nil or omitted, it defaults to the end of STRING. | |
1473 | If FROM or TO is negative, it counts from the end. | |
1474 | ||
1475 | Simply (substring-no-properties STRING) copies a string without | |
1476 | its properties." | |
1477 | (let ((str (substring string (or from 0) to))) | |
1478 | (set-text-properties 0 (length str) nil str) | |
1479 | str)) | |
c5802acf | 1480 | |
cd323f89 | 1481 | (defun match-string (num &optional string) |
993713ce SM |
1482 | "Return string of text matched by last search. |
1483 | NUM specifies which parenthesized expression in the last regexp. | |
1484 | Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
1485 | Zero means the entire text matched by the whole regexp or whole string. | |
1486 | STRING should be given if the last search was by `string-match' on STRING." | |
cd323f89 SM |
1487 | (if (match-beginning num) |
1488 | (if string | |
1489 | (substring string (match-beginning num) (match-end num)) | |
1490 | (buffer-substring (match-beginning num) (match-end num))))) | |
58f950b4 | 1491 | |
bb760c71 RS |
1492 | (defun match-string-no-properties (num &optional string) |
1493 | "Return string of text matched by last search, without text properties. | |
1494 | NUM specifies which parenthesized expression in the last regexp. | |
1495 | Value is nil if NUMth pair didn't match, or there were less than NUM pairs. | |
1496 | Zero means the entire text matched by the whole regexp or whole string. | |
1497 | STRING should be given if the last search was by `string-match' on STRING." | |
1498 | (if (match-beginning num) | |
1499 | (if string | |
1500 | (let ((result | |
1501 | (substring string (match-beginning num) (match-end num)))) | |
1502 | (set-text-properties 0 (length result) nil result) | |
1503 | result) | |
1504 | (buffer-substring-no-properties (match-beginning num) | |
1505 | (match-end num))))) | |
1506 | ||
edce3654 RS |
1507 | (defun split-string (string &optional separators) |
1508 | "Splits STRING into substrings where there are matches for SEPARATORS. | |
1509 | Each match for SEPARATORS is a splitting point. | |
1510 | The substrings between the splitting points are made into a list | |
1511 | which is returned. | |
b222b786 RS |
1512 | If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\". |
1513 | ||
1514 | If there is match for SEPARATORS at the beginning of STRING, we do not | |
1515 | include a null substring for that. Likewise, if there is a match | |
b021ef18 DL |
1516 | at the end of STRING, we don't include a null substring for that. |
1517 | ||
1518 | Modifies the match data; use `save-match-data' if necessary." | |
edce3654 RS |
1519 | (let ((rexp (or separators "[ \f\t\n\r\v]+")) |
1520 | (start 0) | |
b222b786 | 1521 | notfirst |
edce3654 | 1522 | (list nil)) |
b222b786 RS |
1523 | (while (and (string-match rexp string |
1524 | (if (and notfirst | |
1525 | (= start (match-beginning 0)) | |
1526 | (< start (length string))) | |
1527 | (1+ start) start)) | |
1528 | (< (match-beginning 0) (length string))) | |
1529 | (setq notfirst t) | |
7eb47123 | 1530 | (or (eq (match-beginning 0) 0) |
b222b786 RS |
1531 | (and (eq (match-beginning 0) (match-end 0)) |
1532 | (eq (match-beginning 0) start)) | |
edce3654 RS |
1533 | (setq list |
1534 | (cons (substring string start (match-beginning 0)) | |
1535 | list))) | |
1536 | (setq start (match-end 0))) | |
1537 | (or (eq start (length string)) | |
1538 | (setq list | |
1539 | (cons (substring string start) | |
1540 | list))) | |
1541 | (nreverse list))) | |
1ccaea52 AI |
1542 | |
1543 | (defun subst-char-in-string (fromchar tochar string &optional inplace) | |
1544 | "Replace FROMCHAR with TOCHAR in STRING each time it occurs. | |
1545 | Unless optional argument INPLACE is non-nil, return a new string." | |
e6e71807 SM |
1546 | (let ((i (length string)) |
1547 | (newstr (if inplace string (copy-sequence string)))) | |
1548 | (while (> i 0) | |
1549 | (setq i (1- i)) | |
1550 | (if (eq (aref newstr i) fromchar) | |
1551 | (aset newstr i tochar))) | |
1552 | newstr)) | |
b021ef18 | 1553 | |
1697159c DL |
1554 | (defun replace-regexp-in-string (regexp rep string &optional |
1555 | fixedcase literal subexp start) | |
b021ef18 DL |
1556 | "Replace all matches for REGEXP with REP in STRING. |
1557 | ||
1558 | Return a new string containing the replacements. | |
1559 | ||
1560 | Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the | |
1561 | arguments with the same names of function `replace-match'. If START | |
1562 | is non-nil, start replacements at that index in STRING. | |
1563 | ||
1564 | REP is either a string used as the NEWTEXT arg of `replace-match' or a | |
1565 | function. If it is a function it is applied to each match to generate | |
1566 | the replacement passed to `replace-match'; the match-data at this | |
1567 | point are such that match 0 is the function's argument. | |
1568 | ||
1697159c DL |
1569 | To replace only the first match (if any), make REGEXP match up to \\' |
1570 | and replace a sub-expression, e.g. | |
1571 | (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1) | |
1572 | => \" bar foo\" | |
1573 | " | |
b021ef18 DL |
1574 | |
1575 | ;; To avoid excessive consing from multiple matches in long strings, | |
1576 | ;; don't just call `replace-match' continually. Walk down the | |
1577 | ;; string looking for matches of REGEXP and building up a (reversed) | |
1578 | ;; list MATCHES. This comprises segments of STRING which weren't | |
1579 | ;; matched interspersed with replacements for segments that were. | |
08b1f8a1 | 1580 | ;; [For a `large' number of replacements it's more efficient to |
b021ef18 DL |
1581 | ;; operate in a temporary buffer; we can't tell from the function's |
1582 | ;; args whether to choose the buffer-based implementation, though it | |
1583 | ;; might be reasonable to do so for long enough STRING.] | |
1584 | (let ((l (length string)) | |
1585 | (start (or start 0)) | |
1586 | matches str mb me) | |
1587 | (save-match-data | |
1588 | (while (and (< start l) (string-match regexp string start)) | |
1589 | (setq mb (match-beginning 0) | |
1590 | me (match-end 0)) | |
a9853251 SM |
1591 | ;; If we matched the empty string, make sure we advance by one char |
1592 | (when (= me mb) (setq me (min l (1+ mb)))) | |
1593 | ;; Generate a replacement for the matched substring. | |
1594 | ;; Operate only on the substring to minimize string consing. | |
1595 | ;; Set up match data for the substring for replacement; | |
1596 | ;; presumably this is likely to be faster than munging the | |
1597 | ;; match data directly in Lisp. | |
1598 | (string-match regexp (setq str (substring string mb me))) | |
1599 | (setq matches | |
1600 | (cons (replace-match (if (stringp rep) | |
1601 | rep | |
1602 | (funcall rep (match-string 0 str))) | |
1603 | fixedcase literal str subexp) | |
1604 | (cons (substring string start mb) ; unmatched prefix | |
1605 | matches))) | |
1606 | (setq start me)) | |
b021ef18 DL |
1607 | ;; Reconstruct a string from the pieces. |
1608 | (setq matches (cons (substring string start l) matches)) ; leftover | |
1609 | (apply #'concat (nreverse matches))))) | |
a7ed4c2a | 1610 | \f |
8af7df60 RS |
1611 | (defun shell-quote-argument (argument) |
1612 | "Quote an argument for passing as argument to an inferior shell." | |
c1c74b43 | 1613 | (if (eq system-type 'ms-dos) |
8ee75d03 EZ |
1614 | ;; Quote using double quotes, but escape any existing quotes in |
1615 | ;; the argument with backslashes. | |
1616 | (let ((result "") | |
1617 | (start 0) | |
1618 | end) | |
1619 | (if (or (null (string-match "[^\"]" argument)) | |
1620 | (< (match-end 0) (length argument))) | |
1621 | (while (string-match "[\"]" argument start) | |
1622 | (setq end (match-beginning 0) | |
1623 | result (concat result (substring argument start end) | |
1624 | "\\" (substring argument end (1+ end))) | |
1625 | start (1+ end)))) | |
1626 | (concat "\"" result (substring argument start) "\"")) | |
c1c74b43 RS |
1627 | (if (eq system-type 'windows-nt) |
1628 | (concat "\"" argument "\"") | |
e1b65a6b RS |
1629 | (if (equal argument "") |
1630 | "''" | |
1631 | ;; Quote everything except POSIX filename characters. | |
1632 | ;; This should be safe enough even for really weird shells. | |
1633 | (let ((result "") (start 0) end) | |
1634 | (while (string-match "[^-0-9a-zA-Z_./]" argument start) | |
1635 | (setq end (match-beginning 0) | |
1636 | result (concat result (substring argument start end) | |
1637 | "\\" (substring argument end (1+ end))) | |
1638 | start (1+ end))) | |
1639 | (concat result (substring argument start))))))) | |
8af7df60 | 1640 | |
297d863b | 1641 | (defun make-syntax-table (&optional oldtable) |
984f718a | 1642 | "Return a new syntax table. |
0764e16f SM |
1643 | Create a syntax table which inherits from OLDTABLE (if non-nil) or |
1644 | from `standard-syntax-table' otherwise." | |
1645 | (let ((table (make-char-table 'syntax-table nil))) | |
1646 | (set-char-table-parent table (or oldtable (standard-syntax-table))) | |
1647 | table)) | |
31aa282e KH |
1648 | |
1649 | (defun add-to-invisibility-spec (arg) | |
1650 | "Add elements to `buffer-invisibility-spec'. | |
1651 | See documentation for `buffer-invisibility-spec' for the kind of elements | |
1652 | that can be added." | |
1653 | (cond | |
1654 | ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t)) | |
1655 | (setq buffer-invisibility-spec (list arg))) | |
1656 | (t | |
11d431ad KH |
1657 | (setq buffer-invisibility-spec |
1658 | (cons arg buffer-invisibility-spec))))) | |
31aa282e KH |
1659 | |
1660 | (defun remove-from-invisibility-spec (arg) | |
1661 | "Remove elements from `buffer-invisibility-spec'." | |
e93b8cbb | 1662 | (if (consp buffer-invisibility-spec) |
071a2a71 | 1663 | (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec)))) |
baed0109 RS |
1664 | \f |
1665 | (defun global-set-key (key command) | |
1666 | "Give KEY a global binding as COMMAND. | |
7bba1895 KH |
1667 | COMMAND is the command definition to use; usually it is |
1668 | a symbol naming an interactively-callable function. | |
1669 | KEY is a key sequence; noninteractively, it is a string or vector | |
1670 | of characters or event types, and non-ASCII characters with codes | |
1671 | above 127 (such as ISO Latin-1) can be included if you use a vector. | |
1672 | ||
1673 | Note that if KEY has a local binding in the current buffer, | |
1674 | that local binding will continue to shadow any global binding | |
1675 | that you make with this function." | |
baed0109 | 1676 | (interactive "KSet key globally: \nCSet key %s to command: ") |
a2f9aa84 | 1677 | (or (vectorp key) (stringp key) |
baed0109 | 1678 | (signal 'wrong-type-argument (list 'arrayp key))) |
ff663bbe | 1679 | (define-key (current-global-map) key command)) |
baed0109 RS |
1680 | |
1681 | (defun local-set-key (key command) | |
1682 | "Give KEY a local binding as COMMAND. | |
7bba1895 KH |
1683 | COMMAND is the command definition to use; usually it is |
1684 | a symbol naming an interactively-callable function. | |
1685 | KEY is a key sequence; noninteractively, it is a string or vector | |
1686 | of characters or event types, and non-ASCII characters with codes | |
1687 | above 127 (such as ISO Latin-1) can be included if you use a vector. | |
1688 | ||
baed0109 RS |
1689 | The binding goes in the current buffer's local map, |
1690 | which in most cases is shared with all other buffers in the same major mode." | |
1691 | (interactive "KSet key locally: \nCSet key %s locally to command: ") | |
1692 | (let ((map (current-local-map))) | |
1693 | (or map | |
1694 | (use-local-map (setq map (make-sparse-keymap)))) | |
a2f9aa84 | 1695 | (or (vectorp key) (stringp key) |
baed0109 | 1696 | (signal 'wrong-type-argument (list 'arrayp key))) |
ff663bbe | 1697 | (define-key map key command))) |
984f718a | 1698 | |
baed0109 RS |
1699 | (defun global-unset-key (key) |
1700 | "Remove global binding of KEY. | |
1701 | KEY is a string representing a sequence of keystrokes." | |
1702 | (interactive "kUnset key globally: ") | |
1703 | (global-set-key key nil)) | |
1704 | ||
db2474b8 | 1705 | (defun local-unset-key (key) |
baed0109 RS |
1706 | "Remove local binding of KEY. |
1707 | KEY is a string representing a sequence of keystrokes." | |
1708 | (interactive "kUnset key locally: ") | |
1709 | (if (current-local-map) | |
db2474b8 | 1710 | (local-set-key key nil)) |
baed0109 RS |
1711 | nil) |
1712 | \f | |
4809d0dd KH |
1713 | ;; We put this here instead of in frame.el so that it's defined even on |
1714 | ;; systems where frame.el isn't loaded. | |
1715 | (defun frame-configuration-p (object) | |
1716 | "Return non-nil if OBJECT seems to be a frame configuration. | |
1717 | Any list whose car is `frame-configuration' is assumed to be a frame | |
1718 | configuration." | |
1719 | (and (consp object) | |
1720 | (eq (car object) 'frame-configuration))) | |
1721 | ||
a9a44ed1 | 1722 | (defun functionp (object) |
0764e16f | 1723 | "Non-nil iff OBJECT is a type of object that can be called as a function." |
a2d7836f | 1724 | (or (and (symbolp object) (fboundp object) |
1cf72ff8 | 1725 | (setq object (indirect-function object)) |
0764e16f | 1726 | (eq (car-safe object) 'autoload) |
f1d37f3c | 1727 | (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object))))))) |
0764e16f | 1728 | (subrp object) (byte-code-function-p object) |
60ab6064 | 1729 | (eq (car-safe object) 'lambda))) |
a9a44ed1 | 1730 | |
f65fab59 GM |
1731 | (defun interactive-form (function) |
1732 | "Return the interactive form of FUNCTION. | |
1733 | If function is a command (see `commandp'), value is a list of the form | |
d3200788 | 1734 | \(interactive SPEC). If function is not a command, return nil." |
f65fab59 GM |
1735 | (setq function (indirect-function function)) |
1736 | (when (commandp function) | |
1737 | (cond ((byte-code-function-p function) | |
1738 | (when (> (length function) 5) | |
1739 | (let ((spec (aref function 5))) | |
1740 | (if spec | |
1741 | (list 'interactive spec) | |
1742 | (list 'interactive))))) | |
1743 | ((subrp function) | |
1744 | (subr-interactive-form function)) | |
1745 | ((eq (car-safe function) 'lambda) | |
1746 | (setq function (cddr function)) | |
1747 | (when (stringp (car function)) | |
1748 | (setq function (cdr function))) | |
1749 | (let ((form (car function))) | |
a27b451e | 1750 | (when (eq (car-safe form) 'interactive) |
f65fab59 | 1751 | (copy-sequence form))))))) |
630cc463 | 1752 | |
d3a61a11 | 1753 | (defun assq-delete-all (key alist) |
a62d6695 DL |
1754 | "Delete from ALIST all elements whose car is KEY. |
1755 | Return the modified alist." | |
a62d6695 DL |
1756 | (let ((tail alist)) |
1757 | (while tail | |
1758 | (if (eq (car (car tail)) key) | |
1759 | (setq alist (delq (car tail) alist))) | |
1760 | (setq tail (cdr tail))) | |
1761 | alist)) | |
1762 | ||
cdd9f643 RS |
1763 | (defun make-temp-file (prefix &optional dir-flag) |
1764 | "Create a temporary file. | |
1765 | The returned file name (created by appending some random characters at the end | |
1766 | of PREFIX, and expanding against `temporary-file-directory' if necessary, | |
1767 | is guaranteed to point to a newly created empty file. | |
1768 | You can then use `write-region' to write new data into the file. | |
1769 | ||
1770 | If DIR-FLAG is non-nil, create a new empty directory instead of a file." | |
1771 | (let (file) | |
1772 | (while (condition-case () | |
1773 | (progn | |
1774 | (setq file | |
1775 | (make-temp-name | |
1776 | (expand-file-name prefix temporary-file-directory))) | |
1777 | (if dir-flag | |
1778 | (make-directory file) | |
1779 | (write-region "" nil file nil 'silent nil 'excl)) | |
1780 | nil) | |
08b1f8a1 | 1781 | (file-already-exists t)) |
cdd9f643 RS |
1782 | ;; the file was somehow created by someone else between |
1783 | ;; `make-temp-name' and `write-region', let's try again. | |
1784 | nil) | |
1785 | file)) | |
1786 | ||
d7d47268 | 1787 | \f |
c94f4677 | 1788 | (defun add-minor-mode (toggle name &optional keymap after toggle-fun) |
d7d47268 | 1789 | "Register a new minor mode. |
c94f4677 | 1790 | |
0b2cf11f SM |
1791 | This is an XEmacs-compatibility function. Use `define-minor-mode' instead. |
1792 | ||
c94f4677 GM |
1793 | TOGGLE is a symbol which is the name of a buffer-local variable that |
1794 | is toggled on or off to say whether the minor mode is active or not. | |
1795 | ||
1796 | NAME specifies what will appear in the mode line when the minor mode | |
1797 | is active. NAME should be either a string starting with a space, or a | |
1798 | symbol whose value is such a string. | |
1799 | ||
1800 | Optional KEYMAP is the keymap for the minor mode that will be added | |
1801 | to `minor-mode-map-alist'. | |
1802 | ||
1803 | Optional AFTER specifies that TOGGLE should be added after AFTER | |
1804 | in `minor-mode-alist'. | |
1805 | ||
0b2cf11f SM |
1806 | Optional TOGGLE-FUN is an interactive function to toggle the mode. |
1807 | It defaults to (and should by convention be) TOGGLE. | |
1808 | ||
1809 | If TOGGLE has a non-nil `:included' property, an entry for the mode is | |
1810 | included in the mode-line minor mode menu. | |
1811 | If TOGGLE has a `:menu-tag', that is used for the menu item's label." | |
1812 | (unless toggle-fun (setq toggle-fun toggle)) | |
0b2cf11f | 1813 | ;; Add the name to the minor-mode-alist. |
c94f4677 | 1814 | (when name |
0b2cf11f SM |
1815 | (let ((existing (assq toggle minor-mode-alist))) |
1816 | (when (and (stringp name) (not (get-text-property 0 'local-map name))) | |
d6c22d46 | 1817 | (setq name |
0c107014 GM |
1818 | (propertize name |
1819 | 'local-map mode-line-minor-mode-keymap | |
1820 | 'help-echo "mouse-3: minor mode menu"))) | |
0b2cf11f SM |
1821 | (if existing |
1822 | (setcdr existing (list name)) | |
1823 | (let ((tail minor-mode-alist) found) | |
1824 | (while (and tail (not found)) | |
1825 | (if (eq after (caar tail)) | |
1826 | (setq found tail) | |
1827 | (setq tail (cdr tail)))) | |
1828 | (if found | |
1829 | (let ((rest (cdr found))) | |
1830 | (setcdr found nil) | |
1831 | (nconc found (list (list toggle name)) rest)) | |
1832 | (setq minor-mode-alist (cons (list toggle name) | |
1833 | minor-mode-alist))))))) | |
69cae2d4 RS |
1834 | ;; Add the toggle to the minor-modes menu if requested. |
1835 | (when (get toggle :included) | |
1836 | (define-key mode-line-mode-menu | |
1837 | (vector toggle) | |
1838 | (list 'menu-item | |
1839 | (concat | |
1840 | (or (get toggle :menu-tag) | |
1841 | (if (stringp name) name (symbol-name toggle))) | |
1842 | (let ((mode-name (if (stringp name) name | |
1843 | (if (symbolp name) (symbol-value name))))) | |
1844 | (if mode-name | |
1845 | (concat " (" mode-name ")")))) | |
1846 | toggle-fun | |
1847 | :button (cons :toggle toggle)))) | |
1848 | ||
0b2cf11f | 1849 | ;; Add the map to the minor-mode-map-alist. |
c94f4677 GM |
1850 | (when keymap |
1851 | (let ((existing (assq toggle minor-mode-map-alist))) | |
0b2cf11f SM |
1852 | (if existing |
1853 | (setcdr existing keymap) | |
1854 | (let ((tail minor-mode-map-alist) found) | |
1855 | (while (and tail (not found)) | |
1856 | (if (eq after (caar tail)) | |
1857 | (setq found tail) | |
1858 | (setq tail (cdr tail)))) | |
1859 | (if found | |
1860 | (let ((rest (cdr found))) | |
1861 | (setcdr found nil) | |
1862 | (nconc found (list (cons toggle keymap)) rest)) | |
1863 | (setq minor-mode-map-alist (cons (cons toggle keymap) | |
1864 | minor-mode-map-alist)))))))) | |
d7d47268 | 1865 | |
ff77cf40 DL |
1866 | ;; XEmacs compatibility/convenience. |
1867 | (if (fboundp 'play-sound) | |
1868 | (defun play-sound-file (file &optional volume device) | |
1869 | "Play sound stored in FILE. | |
1870 | VOLUME and DEVICE correspond to the keywords of the sound | |
1871 | specification for `play-sound'." | |
1872 | (interactive "fPlay sound file: ") | |
1873 | (let ((sound (list :file file))) | |
1874 | (if volume | |
1875 | (plist-put sound :volume volume)) | |
1876 | (if device | |
1877 | (plist-put sound :device device)) | |
1878 | (push 'sound sound) | |
1879 | (play-sound sound)))) | |
d7d47268 | 1880 | |
a13fe4c5 SM |
1881 | ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1882 | ||
1883 | (defun text-clone-maintain (ol1 after beg end &optional len) | |
1884 | "Propagate the changes made under the overlay OL1 to the other clones. | |
1885 | This is used on the `modification-hooks' property of text clones." | |
1886 | (when (and after (not undo-in-progress) (overlay-start ol1)) | |
1887 | (let ((margin (if (overlay-get ol1 'text-clone-spreadp) 1 0))) | |
1888 | (setq beg (max beg (+ (overlay-start ol1) margin))) | |
1889 | (setq end (min end (- (overlay-end ol1) margin))) | |
1890 | (when (<= beg end) | |
1891 | (save-excursion | |
1892 | (when (overlay-get ol1 'text-clone-syntax) | |
1893 | ;; Check content of the clone's text. | |
1894 | (let ((cbeg (+ (overlay-start ol1) margin)) | |
1895 | (cend (- (overlay-end ol1) margin))) | |
1896 | (goto-char cbeg) | |
1897 | (save-match-data | |
1898 | (if (not (re-search-forward | |
1899 | (overlay-get ol1 'text-clone-syntax) cend t)) | |
1900 | ;; Mark the overlay for deletion. | |
1901 | (overlay-put ol1 'text-clones nil) | |
1902 | (when (< (match-end 0) cend) | |
1903 | ;; Shrink the clone at its end. | |
1904 | (setq end (min end (match-end 0))) | |
1905 | (move-overlay ol1 (overlay-start ol1) | |
1906 | (+ (match-end 0) margin))) | |
1907 | (when (> (match-beginning 0) cbeg) | |
1908 | ;; Shrink the clone at its beginning. | |
1909 | (setq beg (max (match-beginning 0) beg)) | |
1910 | (move-overlay ol1 (- (match-beginning 0) margin) | |
1911 | (overlay-end ol1))))))) | |
1912 | ;; Now go ahead and update the clones. | |
1913 | (let ((head (- beg (overlay-start ol1))) | |
1914 | (tail (- (overlay-end ol1) end)) | |
1915 | (str (buffer-substring beg end)) | |
1916 | (nothing-left t) | |
1917 | (inhibit-modification-hooks t)) | |
1918 | (dolist (ol2 (overlay-get ol1 'text-clones)) | |
1919 | (let ((oe (overlay-end ol2))) | |
1920 | (unless (or (eq ol1 ol2) (null oe)) | |
1921 | (setq nothing-left nil) | |
1922 | (let ((mod-beg (+ (overlay-start ol2) head))) | |
1923 | ;;(overlay-put ol2 'modification-hooks nil) | |
1924 | (goto-char (- (overlay-end ol2) tail)) | |
1925 | (unless (> mod-beg (point)) | |
1926 | (save-excursion (insert str)) | |
1927 | (delete-region mod-beg (point))) | |
1928 | ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain)) | |
1929 | )))) | |
1930 | (if nothing-left (delete-overlay ol1)))))))) | |
1931 | ||
1932 | (defun text-clone-create (start end &optional spreadp syntax) | |
1933 | "Create a text clone of START...END at point. | |
1934 | Text clones are chunks of text that are automatically kept identical: | |
1935 | changes done to one of the clones will be immediately propagated to the other. | |
1936 | ||
1937 | The buffer's content at point is assumed to be already identical to | |
1938 | the one between START and END. | |
1939 | If SYNTAX is provided it's a regexp that describes the possible text of | |
1940 | the clones; the clone will be shrunk or killed if necessary to ensure that | |
1941 | its text matches the regexp. | |
1942 | If SPREADP is non-nil it indicates that text inserted before/after the | |
1943 | clone should be incorporated in the clone." | |
1944 | ;; To deal with SPREADP we can either use an overlay with `nil t' along | |
1945 | ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay | |
1946 | ;; (with a one-char margin at each end) with `t nil'. | |
1947 | ;; We opted for a larger overlay because it behaves better in the case | |
1948 | ;; where the clone is reduced to the empty string (we want the overlay to | |
1949 | ;; stay when the clone's content is the empty string and we want to use | |
1950 | ;; `evaporate' to make sure those overlays get deleted when needed). | |
1951 | ;; | |
1952 | (let* ((pt-end (+ (point) (- end start))) | |
1953 | (start-margin (if (or (not spreadp) (bobp) (<= start (point-min))) | |
1954 | 0 1)) | |
1955 | (end-margin (if (or (not spreadp) | |
1956 | (>= pt-end (point-max)) | |
1957 | (>= start (point-max))) | |
1958 | 0 1)) | |
1959 | (ol1 (make-overlay (- start start-margin) (+ end end-margin) nil t)) | |
1960 | (ol2 (make-overlay (- (point) start-margin) (+ pt-end end-margin) nil t)) | |
1961 | (dups (list ol1 ol2))) | |
1962 | (overlay-put ol1 'modification-hooks '(text-clone-maintain)) | |
1963 | (when spreadp (overlay-put ol1 'text-clone-spreadp t)) | |
1964 | (when syntax (overlay-put ol1 'text-clone-syntax syntax)) | |
1965 | ;;(overlay-put ol1 'face 'underline) | |
1966 | (overlay-put ol1 'evaporate t) | |
1967 | (overlay-put ol1 'text-clones dups) | |
1968 | ;; | |
1969 | (overlay-put ol2 'modification-hooks '(text-clone-maintain)) | |
1970 | (when spreadp (overlay-put ol2 'text-clone-spreadp t)) | |
1971 | (when syntax (overlay-put ol2 'text-clone-syntax syntax)) | |
1972 | ;;(overlay-put ol2 'face 'underline) | |
1973 | (overlay-put ol2 'evaporate t) | |
1974 | (overlay-put ol2 'text-clones dups))) | |
1975 | ||
630cc463 | 1976 | ;;; subr.el ends here |