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