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