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