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