XEmacs 21.2.33 "Melpomene".
[chise/xemacs-chise.git.1] / lisp / subr.el
1 ;;; subr.el --- basic lisp subroutines for XEmacs
2
3 ;; Copyright (C) 1985, 1986, 1992, 1994-5, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 1995 Sun Microsystems.
6 ;; Copyright (C) 2000 Ben Wing.
7
8 ;; Maintainer: XEmacs Development Team
9 ;; Keywords: extensions, dumped
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;; 02111-1307, USA.
27
28 ;;; Synched up with: FSF 19.34.
29
30 ;;; Commentary:
31
32 ;; This file is dumped with XEmacs.
33
34 ;; There's not a whole lot in common now with the FSF version,
35 ;; be wary when applying differences.  I've left in a number of lines
36 ;; of commentary just to give diff(1) something to synch itself with to
37 ;; provide useful context diffs. -sb
38
39 ;;; Code:
40
41 \f
42 ;;;; Lisp language features.
43
44 (defmacro lambda (&rest cdr)
45   "Return a lambda expression.
46 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
47 self-quoting; the result of evaluating the lambda expression is the
48 expression itself.  The lambda expression may then be treated as a
49 function, i.e., stored as the function value of a symbol, passed to
50 funcall or mapcar, etc.
51
52 ARGS should take the same form as an argument list for a `defun'.
53 DOCSTRING is an optional documentation string.
54  If present, it should describe how to call the function.
55  But documentation strings are usually not useful in nameless functions.
56 INTERACTIVE should be a call to the function `interactive', which see.
57 It may also be omitted.
58 BODY should be a list of lisp expressions."
59   `(function (lambda ,@cdr)))
60
61 (defmacro defun-when-void (&rest args)
62   "Define a function, just like `defun', unless it's already defined.
63 Used for compatibility among different emacs variants."
64   `(if (fboundp ',(car args))
65        nil
66      (defun ,@args)))
67
68 (defmacro define-function-when-void (&rest args)
69   "Define a function, just like `define-function', unless it's already defined.
70 Used for compatibility among different emacs variants."
71   `(if (fboundp ,(car args))
72        nil
73      (define-function ,@args)))
74
75 \f
76 ;;;; Keymap support.
77 ;; XEmacs: removed to keymap.el
78
79 ;;;; The global keymap tree.
80
81 ;;; global-map, esc-map, and ctl-x-map have their values set up in
82 ;;; keymap.c; we just give them docstrings here.
83
84 ;;;; Event manipulation functions.
85
86 ;; XEmacs: This stuff is done in C Code.
87
88 ;;;; Obsolescent names for functions.
89 ;; XEmacs: not used.
90
91 ;; XEmacs:
92 (defun local-variable-if-set-p (sym buffer)
93   "Return t if SYM would be local to BUFFER after it is set.
94 A nil value for BUFFER is *not* the same as (current-buffer), but
95 can be used to determine whether `make-variable-buffer-local' has been
96 called on SYM."
97   (local-variable-p sym buffer t))
98
99 \f
100 ;;;; Hook manipulation functions.
101
102 ;; (defconst run-hooks 'run-hooks ...)
103
104 (defun make-local-hook (hook)
105   "Make the hook HOOK local to the current buffer.
106 When a hook is local, its local and global values
107 work in concert: running the hook actually runs all the hook
108 functions listed in *either* the local value *or* the global value
109 of the hook variable.
110
111 This function works by making `t' a member of the buffer-local value,
112 which acts as a flag to run the hook functions in the default value as
113 well.  This works for all normal hooks, but does not work for most
114 non-normal hooks yet.  We will be changing the callers of non-normal
115 hooks so that they can handle localness; this has to be done one by
116 one.
117
118 This function does nothing if HOOK is already local in the current
119 buffer.
120
121 Do not use `make-local-variable' to make a hook variable buffer-local.
122
123 See also `add-local-hook' and `remove-local-hook'."
124   (if (local-variable-p hook (current-buffer)) ; XEmacs
125       nil
126     (or (boundp hook) (set hook nil))
127     (make-local-variable hook)
128     (set hook (list t))))
129
130 (defun add-hook (hook function &optional append local)
131   "Add to the value of HOOK the function FUNCTION.
132 FUNCTION is not added if already present.
133 FUNCTION is added (if necessary) at the beginning of the hook list
134 unless the optional argument APPEND is non-nil, in which case
135 FUNCTION is added at the end.
136
137 The optional fourth argument, LOCAL, if non-nil, says to modify
138 the hook's buffer-local value rather than its default value.
139 This makes no difference if the hook is not buffer-local.
140 To make a hook variable buffer-local, always use
141 `make-local-hook', not `make-local-variable'.
142
143 HOOK should be a symbol, and FUNCTION may be any valid function.  If
144 HOOK is void, it is first set to nil.  If HOOK's value is a single
145 function, it is changed to a list of functions.
146
147 You can remove this hook yourself using `remove-hook'.
148
149 See also `add-local-hook' and `add-one-shot-hook'."
150   (or (boundp hook) (set hook nil))
151   (or (default-boundp hook) (set-default hook nil))
152   ;; If the hook value is a single function, turn it into a list.
153   (let ((old (symbol-value hook)))
154     (if (or (not (listp old)) (eq (car old) 'lambda))
155         (set hook (list old))))
156   (if (or local
157           ;; Detect the case where make-local-variable was used on a hook
158           ;; and do what we used to do.
159           (and (local-variable-if-set-p hook (current-buffer)) ; XEmacs
160                (not (memq t (symbol-value hook)))))
161       ;; Alter the local value only.
162       (or (if (consp function)
163               (member function (symbol-value hook))
164             (memq function (symbol-value hook)))
165           (set hook
166                (if append
167                    (append (symbol-value hook) (list function))
168                  (cons function (symbol-value hook)))))
169     ;; Alter the global value (which is also the only value,
170     ;; if the hook doesn't have a local value).
171     (or (if (consp function)
172             (member function (default-value hook))
173           (memq function (default-value hook)))
174         (set-default hook
175                      (if append
176                          (append (default-value hook) (list function))
177                        (cons function (default-value hook)))))))
178
179 (defun remove-hook (hook function &optional local)
180   "Remove from the value of HOOK the function FUNCTION.
181 HOOK should be a symbol, and FUNCTION may be any valid function.  If
182 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
183 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
184
185 The optional third argument, LOCAL, if non-nil, says to modify
186 the hook's buffer-local value rather than its default value.
187 This makes no difference if the hook is not buffer-local.
188 To make a hook variable buffer-local, always use
189 `make-local-hook', not `make-local-variable'."
190   (if (or (not (boundp hook))           ;unbound symbol, or
191           (not (default-boundp 'hook))
192           (null (symbol-value hook))    ;value is nil, or
193           (null function))              ;function is nil, then
194       nil                               ;Do nothing.
195     (flet ((hook-remove
196             (function hook-value)
197             (flet ((hook-test
198                     (fn hel)
199                     (or (equal fn hel)
200                         (and (symbolp hel)
201                              (equal fn
202                                     (get hel 'one-shot-hook-fun))))))
203               (if (and (consp hook-value)
204                        (not (functionp hook-value)))
205                   (if (member* function hook-value :test 'hook-test)
206                       (setq hook-value
207                             (delete* function (copy-sequence hook-value)
208                                      :test 'hook-test)))
209                 (if (equal hook-value function)
210                     (setq hook-value nil)))
211               hook-value)))
212       (if (or local
213               ;; Detect the case where make-local-variable was used on a hook
214               ;; and do what we used to do.
215               (and (local-variable-p hook (current-buffer))
216                    (not (memq t (symbol-value hook)))))
217           (set hook (hook-remove function (symbol-value hook)))
218         (set-default hook (hook-remove function (default-value hook)))))))
219
220 ;; XEmacs addition
221 ;; #### we need a coherent scheme for indicating compatibility info,
222 ;; so that it can be programmatically retrieved.
223 (defun add-local-hook (hook function &optional append)
224   "Add to the local value of HOOK the function FUNCTION.
225 This modifies only the buffer-local value for the hook (which is
226 automatically make buffer-local, if necessary), not its default value.
227 FUNCTION is not added if already present.
228 FUNCTION is added (if necessary) at the beginning of the hook list
229 unless the optional argument APPEND is non-nil, in which case
230 FUNCTION is added at the end.
231
232 HOOK should be a symbol, and FUNCTION may be any valid function.  If
233 HOOK is void, it is first set to nil.  If HOOK's value is a single
234 function, it is changed to a list of functions.
235
236 You can remove this hook yourself using `remove-local-hook'.
237
238 See also `add-hook' and `make-local-hook'."
239   (make-local-hook hook)
240   (add-hook hook function append t))
241
242 ;; XEmacs addition
243 (defun remove-local-hook (hook function)
244   "Remove from the local value of HOOK the function FUNCTION.
245 This modifies only the buffer-local value for the hook, not its default
246 value. (Nothing happens if the hook is not buffer-local.)
247 HOOK should be a symbol, and FUNCTION may be any valid function.  If
248 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
249 list of hooks to run in HOOK, then nothing is done.  See `add-hook'.
250
251 See also `add-local-hook' and `make-local-hook'."
252   (if (local-variable-p hook (current-buffer))
253       (remove-hook hook function t)))
254
255 (defun add-one-shot-hook (hook function &optional append local)
256   "Add to the value of HOOK the one-shot function FUNCTION.
257 FUNCTION will automatically be removed from the hook the first time
258 after it runs (whether to completion or to an error).
259 FUNCTION is not added if already present.
260 FUNCTION is added (if necessary) at the beginning of the hook list
261 unless the optional argument APPEND is non-nil, in which case
262 FUNCTION is added at the end.
263
264 HOOK should be a symbol, and FUNCTION may be any valid function.  If
265 HOOK is void, it is first set to nil.  If HOOK's value is a single
266 function, it is changed to a list of functions.
267
268 You can remove this hook yourself using `remove-hook'.
269
270 See also `add-hook', `add-local-hook', and `add-local-one-shot-hook'."
271   (let ((sym (gensym)))
272     (fset sym `(lambda (&rest args)
273                  (unwind-protect
274                      (apply ',function args)
275                    (remove-hook ',hook ',sym ',local))))
276     (put sym 'one-shot-hook-fun function)
277     (add-hook hook sym append local)))
278
279 (defun add-local-one-shot-hook (hook function &optional append)
280   "Add to the local value of HOOK the one-shot function FUNCTION.
281 FUNCTION will automatically be removed from the hook the first time
282 after it runs (whether to completion or to an error).
283 FUNCTION is not added if already present.
284 FUNCTION is added (if necessary) at the beginning of the hook list
285 unless the optional argument APPEND is non-nil, in which case
286 FUNCTION is added at the end.
287
288 The optional fourth argument, LOCAL, if non-nil, says to modify
289 the hook's buffer-local value rather than its default value.
290 This makes no difference if the hook is not buffer-local.
291 To make a hook variable buffer-local, always use
292 `make-local-hook', not `make-local-variable'.
293
294 HOOK should be a symbol, and FUNCTION may be any valid function.  If
295 HOOK is void, it is first set to nil.  If HOOK's value is a single
296 function, it is changed to a list of functions.
297
298 You can remove this hook yourself using `remove-local-hook'.
299
300 See also `add-hook', `add-local-hook', and `add-local-one-shot-hook'."
301   (make-local-hook hook)
302   (add-one-shot-hook hook function append t))
303
304 (defun add-to-list (list-var element)
305   "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
306 The test for presence of ELEMENT is done with `equal'.
307 If you want to use `add-to-list' on a variable that is not defined
308 until a certain package is loaded, you should put the call to `add-to-list'
309 into a hook function that will be run only after loading the package.
310 `eval-after-load' provides one way to do this.  In some cases
311 other hooks, such as major mode hooks, can do the job."
312   (or (member element (symbol-value list-var))
313       (set list-var (cons element (symbol-value list-var)))))
314
315 ;; XEmacs additions
316 ;; called by Fkill_buffer()
317 (defvar kill-buffer-hook nil
318   "Function or functions to be called when a buffer is killed.
319 The value of this variable may be buffer-local.
320 The buffer about to be killed is current when this hook is run.")
321
322 ;; in C in FSFmacs
323 (defvar kill-emacs-hook nil
324   "Function or functions to be called when `kill-emacs' is called,
325 just before emacs is actually killed.")
326
327 ;; not obsolete.
328 ;; #### These are a bad idea, because the CL RPLACA and RPLACD
329 ;; return the cons cell, not the new CAR/CDR.         -hniksic
330 ;; The proper definition would be:
331 ;; (defun rplaca (conscell newcar)
332 ;;   (setcar conscell newcar)
333 ;;   conscell)
334 ;; ...and analogously for RPLACD.
335 (define-function 'rplaca 'setcar)
336 (define-function 'rplacd 'setcdr)
337
338 (defun copy-symbol (symbol &optional copy-properties)
339   "Return a new uninterned symbol with the same name as SYMBOL.
340 If COPY-PROPERTIES is non-nil, the new symbol will have a copy of
341 SYMBOL's value, function, and property lists."
342   (let ((new (make-symbol (symbol-name symbol))))
343     (when copy-properties
344       ;; This will not copy SYMBOL's chain of forwarding objects, but
345       ;; I think that's OK.  Callers should not expect such magic to
346       ;; keep working in the copy in the first place.
347       (and (boundp symbol)
348            (set new (symbol-value symbol)))
349       (and (fboundp symbol)
350            (fset new (symbol-function symbol)))
351       (setplist new (copy-list (symbol-plist symbol))))
352     new))
353
354 ;;;; String functions.
355
356 ;; XEmacs
357 (defun replace-in-string (str regexp newtext &optional literal)
358   "Replace all matches in STR for REGEXP with NEWTEXT string,
359  and returns the new string.
360 Optional LITERAL non-nil means do a literal replacement.
361 Otherwise treat \\ in NEWTEXT string as special:
362   \\& means substitute original matched text,
363   \\N means substitute match for \(...\) number N,
364   \\\\ means insert one \\."
365   (check-argument-type 'stringp str)
366   (check-argument-type 'stringp newtext)
367   (let ((rtn-str "")
368         (start 0)
369         (special)
370         match prev-start)
371     (while (setq match (string-match regexp str start))
372       (setq prev-start start
373             start (match-end 0)
374             rtn-str
375             (concat
376               rtn-str
377               (substring str prev-start match)
378               (cond (literal newtext)
379                     (t (mapconcat
380                         (lambda (c)
381                           (if special
382                               (progn
383                                 (setq special nil)
384                                 (cond ((eq c ?\\) "\\")
385                                       ((eq c ?&)
386                                        (substring str
387                                                   (match-beginning 0)
388                                                   (match-end 0)))
389                                       ((and (>= c ?0) (<= c ?9))
390                                        (if (> c (+ ?0 (length
391                                                        (match-data))))
392                                            ;; Invalid match num
393                                            (error "Invalid match num: %c" c)
394                                          (setq c (- c ?0))
395                                          (substring str
396                                                     (match-beginning c)
397                                                     (match-end c))))
398                                       (t (char-to-string c))))
399                             (if (eq c ?\\) (progn (setq special t) nil)
400                               (char-to-string c))))
401                          newtext ""))))))
402     (concat rtn-str (substring str start))))
403
404 (defun split-string (string &optional pattern)
405   "Return a list of substrings of STRING which are separated by PATTERN.
406 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
407   (or pattern
408       (setq pattern "[ \f\t\n\r\v]+"))
409   (let (parts (start 0) (len (length string)))
410     (if (string-match pattern string)
411         (setq parts (cons (substring string 0 (match-beginning 0)) parts)
412               start (match-end 0)))
413     (while (and (< start len)
414                 (string-match pattern string (if (> start (match-beginning 0))
415                                                  start
416                                                (1+ start))))
417       (setq parts (cons (substring string start (match-beginning 0)) parts)
418             start (match-end 0)))
419     (nreverse (cons (substring string start) parts))))
420
421 ;; #### #### #### AAaargh!  Must be in C, because it is used insanely
422 ;; early in the bootstrap process.
423 ;(defun split-path (path)
424 ;  "Explode a search path into a list of strings.
425 ;The path components are separated with the characters specified
426 ;with `path-separator'."
427 ;  (while (or (not stringp path-separator)
428 ;            (/= (length path-separator) 1))
429 ;    (setq path-separator (signal 'error (list "\
430 ;`path-separator' should be set to a single-character string"
431 ;                                             path-separator))))
432 ;  (split-string-by-char path (aref separator 0)))
433
434 (defmacro with-output-to-string (&rest forms)
435   "Collect output to `standard-output' while evaluating FORMS and return
436 it as a string."
437   ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu> w/ mods from Stig
438   `(with-current-buffer (get-buffer-create
439                          (generate-new-buffer-name " *string-output*"))
440      (setq buffer-read-only nil)
441      (buffer-disable-undo (current-buffer))
442      (erase-buffer)
443      (let ((standard-output (current-buffer)))
444        ,@forms)
445      (prog1
446          (buffer-string)
447        (erase-buffer))))
448
449 (defmacro with-current-buffer (buffer &rest body)
450   "Temporarily make BUFFER the current buffer and execute the forms in BODY.
451 The value returned is the value of the last form in BODY.
452 See also `with-temp-buffer'."
453   `(save-current-buffer
454     (set-buffer ,buffer)
455     ,@body))
456
457 (defmacro with-temp-file (file &rest forms)
458   "Create a new buffer, evaluate FORMS there, and write the buffer to FILE.
459 The value of the last form in FORMS is returned, like `progn'.
460 See also `with-temp-buffer'."
461   (let ((temp-file (make-symbol "temp-file"))
462         (temp-buffer (make-symbol "temp-buffer")))
463     `(let ((,temp-file ,file)
464            (,temp-buffer
465             (get-buffer-create (generate-new-buffer-name " *temp file*"))))
466        (unwind-protect
467            (prog1
468                (with-current-buffer ,temp-buffer
469                  ,@forms)
470              (with-current-buffer ,temp-buffer
471                (widen)
472                (write-region (point-min) (point-max) ,temp-file nil 0)))
473          (and (buffer-name ,temp-buffer)
474               (kill-buffer ,temp-buffer))))))
475
476 (defmacro with-temp-buffer (&rest forms)
477   "Create a temporary buffer, and evaluate FORMS there like `progn'.
478 See also `with-temp-file' and `with-output-to-string'."
479   (let ((temp-buffer (make-symbol "temp-buffer")))
480     `(let ((,temp-buffer
481             (get-buffer-create (generate-new-buffer-name " *temp*"))))
482        (unwind-protect
483            (with-current-buffer ,temp-buffer
484              ,@forms)
485          (and (buffer-name ,temp-buffer)
486               (kill-buffer ,temp-buffer))))))
487
488 ;; Moved from mule-coding.el.
489 (defmacro with-string-as-buffer-contents (str &rest body)
490   "With the contents of the current buffer being STR, run BODY.
491 Returns the new contents of the buffer, as modified by BODY.
492 The original current buffer is restored afterwards."
493   `(with-temp-buffer
494      (insert ,str)
495      ,@body
496      (buffer-string)))
497
498 (defun insert-face (string face)
499   "Insert STRING and highlight with FACE.  Return the extent created."
500   (let ((p (point)) ext)
501     (insert string)
502     (setq ext (make-extent p (point)))
503     (set-extent-face ext face)
504     ext))
505
506 ;; not obsolete.
507 (define-function 'string= 'string-equal)
508 (define-function 'string< 'string-lessp)
509 (define-function 'int-to-string 'number-to-string)
510 (define-function 'string-to-int 'string-to-number)
511
512 ;; These two names are a bit awkward, as they conflict with the normal
513 ;; foo-to-bar naming scheme, but CLtL2 has them, so they stay.
514 (define-function 'char-int 'char-to-int)
515 (define-function 'int-char 'int-to-char)
516
517 \f
518 ;; alist/plist functions
519 (defun plist-to-alist (plist)
520   "Convert property list PLIST into the equivalent association-list form.
521 The alist is returned.  This converts from
522
523 \(a 1 b 2 c 3)
524
525 into
526
527 \((a . 1) (b . 2) (c . 3))
528
529 The original plist is not modified.  See also `destructive-plist-to-alist'."
530   (let (alist)
531     (while plist
532       (setq alist (cons (cons (car plist) (cadr plist)) alist))
533       (setq plist (cddr plist)))
534     (nreverse alist)))
535
536 (defun destructive-plist-to-alist (plist)
537   "Convert property list PLIST into the equivalent association-list form.
538 The alist is returned.  This converts from
539
540 \(a 1 b 2 c 3)
541
542 into
543
544 \((a . 1) (b . 2) (c . 3))
545
546 The original plist is destroyed in the process of constructing the alist.
547 See also `plist-to-alist'."
548   (let ((head plist)
549         next)
550     (while plist
551       ;; remember the next plist pair.
552       (setq next (cddr plist))
553       ;; make the cons holding the property value into the alist element.
554       (setcdr (cdr plist) (cadr plist))
555       (setcar (cdr plist) (car plist))
556       ;; reattach into alist form.
557       (setcar plist (cdr plist))
558       (setcdr plist next)
559       (setq plist next))
560     head))
561
562 (defun alist-to-plist (alist)
563   "Convert association list ALIST into the equivalent property-list form.
564 The plist is returned.  This converts from
565
566 \((a . 1) (b . 2) (c . 3))
567
568 into
569
570 \(a 1 b 2 c 3)
571
572 The original alist is not modified.  See also `destructive-alist-to-plist'."
573   (let (plist)
574     (while alist
575       (let ((el (car alist)))
576         (setq plist (cons (cdr el) (cons (car el) plist))))
577       (setq alist (cdr alist)))
578     (nreverse plist)))
579
580 ;; getf, remf in cl*.el.
581
582 (defmacro putf (plist prop val)
583   "Add property PROP to plist PLIST with value VAL.
584 Analogous to (setq PLIST (plist-put PLIST PROP VAL))."
585   `(setq ,plist (plist-put ,plist ,prop ,val)))
586
587 (defmacro laxputf (lax-plist prop val)
588   "Add property PROP to lax plist LAX-PLIST with value VAL.
589 Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROP VAL))."
590   `(setq ,lax-plist (lax-plist-put ,lax-plist ,prop ,val)))
591
592 (defmacro laxremf (lax-plist prop)
593   "Remove property PROP from lax plist LAX-PLIST.
594 Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROP))."
595   `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,prop)))
596 \f
597 ;;; Error functions
598
599 (defun error (&rest args)
600   "Signal an error, making error message by passing all args to `format'.
601 This error is not continuable: you cannot continue execution after the
602 error using the debugger `r' command.  See also `cerror'."
603   (while t
604     (apply 'cerror args)))
605
606 (defun cerror (&rest args)
607   "Like `error' but signals a continuable error."
608   (signal 'error (list (apply 'format args))))
609
610 (defmacro check-argument-type (predicate argument)
611   "Check that ARGUMENT satisfies PREDICATE.
612 If not, signal a continuable `wrong-type-argument' error until the
613 returned value satisfies PREDICATE, and assign the returned value
614 to ARGUMENT."
615   `(if (not (,(eval predicate) ,argument))
616        (setq ,argument
617              (wrong-type-argument ,predicate ,argument))))
618
619 (defun signal-error (error-symbol data)
620   "Signal a non-continuable error.  Args are ERROR-SYMBOL, and associated DATA.
621 An error symbol is a symbol defined using `define-error'.
622 DATA should be a list.  Its elements are printed as part of the error message.
623 If the signal is handled, DATA is made available to the handler.
624 See also `signal', and the functions to handle errors: `condition-case'
625 and `call-with-condition-handler'."
626   (while t
627     (signal error-symbol data)))
628
629 (defun define-error (error-sym doc-string &optional inherits-from)
630   "Define a new error, denoted by ERROR-SYM.
631 DOC-STRING is an informative message explaining the error, and will be
632 printed out when an unhandled error occurs.
633 ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error').
634
635 \[`define-error' internally works by putting on ERROR-SYM an `error-message'
636 property whose value is DOC-STRING, and an `error-conditions' property
637 that is a list of ERROR-SYM followed by each of its super-errors, up
638 to and including `error'.  You will sometimes see code that sets this up
639 directly rather than calling `define-error', but you should *not* do this
640 yourself.]"
641   (check-argument-type 'symbolp error-sym)
642   (check-argument-type 'stringp doc-string)
643   (put error-sym 'error-message doc-string)
644   (or inherits-from (setq inherits-from 'error))
645   (let ((conds (get inherits-from 'error-conditions)))
646     (or conds (signal-error 'error (list "Not an error symbol" error-sym)))
647     (put error-sym 'error-conditions (cons error-sym conds))))
648
649 ;;;; Miscellanea.
650
651 ;; This is now in C.
652 ;(defun buffer-substring-no-properties (beg end)
653 ;  "Return the text from BEG to END, without text properties, as a string."
654 ;  (let ((string (buffer-substring beg end)))
655 ;    (set-text-properties 0 (length string) nil string)
656 ;    string))
657
658 (defun get-buffer-window-list (&optional buffer minibuf frame)
659   "Return windows currently displaying BUFFER, or nil if none.
660 BUFFER defaults to the current buffer.
661 See `walk-windows' for the meaning of MINIBUF and FRAME."
662   (cond ((null buffer)
663          (setq buffer (current-buffer)))
664         ((not (bufferp buffer))
665          (setq buffer (get-buffer buffer))))
666   (let (windows)
667     (walk-windows (lambda (window)
668                     (if (eq (window-buffer window) buffer)
669                         (push window windows)))
670                   minibuf frame)
671     windows))
672
673 (defun ignore (&rest ignore)
674   "Do nothing and return nil.
675 This function accepts any number of arguments, but ignores them."
676   (interactive)
677   nil)
678
679 (define-function 'eval-in-buffer 'with-current-buffer)
680 (make-obsolete 'eval-in-buffer 'with-current-buffer)
681
682 ;;; The real defn is in abbrev.el but some early callers
683 ;;;  (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded...
684
685 (if (not (fboundp 'define-abbrev-table))
686     (progn
687       (setq abbrev-table-name-list '())
688       (fset 'define-abbrev-table (function (lambda (name defs)
689                                    ;; These are fixed-up when abbrev.el loads.
690                                    (setq abbrev-table-name-list
691                                          (cons (cons name defs)
692                                                abbrev-table-name-list)))))))
693
694 ;;; `functionp' has been moved into C.
695
696 ;;(defun functionp (object)
697 ;;  "Non-nil if OBJECT can be called as a function."
698 ;;  (or (and (symbolp object) (fboundp object))
699 ;;      (subrp object)
700 ;;      (compiled-function-p object)
701 ;;      (eq (car-safe object) 'lambda)))
702
703
704
705 (defun function-interactive (function)
706   "Return the interactive specification of FUNCTION.
707 FUNCTION can be any funcallable object.
708 The specification will be returned as the list of the symbol `interactive'
709  and the specs.
710 If FUNCTION is not interactive, nil will be returned."
711   (setq function (indirect-function function))
712   (cond ((compiled-function-p function)
713          (compiled-function-interactive function))
714         ((subrp function)
715          (subr-interactive function))
716         ((eq (car-safe function) 'lambda)
717          (let ((spec (if (stringp (nth 2 function))
718                          (nth 3 function)
719                        (nth 2 function))))
720            (and (eq (car-safe spec) 'interactive)
721                 spec)))
722         (t
723          (error "Non-funcallable object: %s" function))))
724
725 (defun function-allows-args (function n)
726   "Return whether FUNCTION can be called with N arguments."
727   (and (<= (function-min-args function) n)
728        (or (null (function-max-args function))
729            (<= n (function-max-args function)))))
730
731 ;; This function used to be an alias to `buffer-substring', except
732 ;; that FSF Emacs 20.4 added a BUFFER argument in an incompatible way.
733 ;; The new FSF's semantics makes more sense, but we try to support
734 ;; both for backward compatibility.
735 (defun buffer-string (&optional buffer old-end old-buffer)
736   "Return the contents of the current buffer as a string.
737 If narrowing is in effect, this function returns only the visible part
738 of the buffer.
739
740 If BUFFER is specified, the contents of that buffer are returned.
741
742 The arguments OLD-END and OLD-BUFFER are supported for backward
743 compatibility with pre-21.2 XEmacsen times when arguments to this
744 function were (buffer-string &optional START END BUFFER)."
745   (cond
746    ((or (stringp buffer) (bufferp buffer))
747     ;; Most definitely the new way.
748     (buffer-substring nil nil buffer))
749    ((or (stringp old-buffer) (bufferp old-buffer)
750         (natnump buffer) (natnump old-end))
751     ;; Definitely the old way.
752     (buffer-substring buffer old-end old-buffer))
753    (t
754     ;; Probably the old way.
755     (buffer-substring buffer old-end old-buffer))))
756
757 ;; This was not present before.  I think Jamie had some objections
758 ;; to this, so I'm leaving this undefined for now. --ben
759
760 ;;; The objection is this: there is more than one way to load the same file.
761 ;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different
762 ;;; ways to load the exact same code.  `eval-after-load' is too stupid to
763 ;;; deal with this sort of thing.  If this sort of feature is desired, then
764 ;;; it should work off of a hook on `provide'.  Features are unique and
765 ;;; the arguments to (load) are not.  --Stig
766
767 ;; We provide this for FSFmacs compatibility, at least until we devise
768 ;; something better.
769
770 ;;;; Specifying things to do after certain files are loaded.
771
772 (defun eval-after-load (file form)
773   "Arrange that, if FILE is ever loaded, FORM will be run at that time.
774 This makes or adds to an entry on `after-load-alist'.
775 If FILE is already loaded, evaluate FORM right now.
776 It does nothing if FORM is already on the list for FILE.
777 FILE should be the name of a library, with no directory name."
778   ;; Make sure there is an element for FILE.
779   (or (assoc file after-load-alist)
780       (setq after-load-alist (cons (list file) after-load-alist)))
781   ;; Add FORM to the element if it isn't there.
782   (let ((elt (assoc file after-load-alist)))
783     (or (member form (cdr elt))
784         (progn
785           (nconc elt (list form))
786           ;; If the file has been loaded already, run FORM right away.
787           (and (assoc file load-history)
788                (eval form)))))
789   form)
790 (make-compatible 'eval-after-load "")
791
792 (defun eval-next-after-load (file)
793   "Read the following input sexp, and run it whenever FILE is loaded.
794 This makes or adds to an entry on `after-load-alist'.
795 FILE should be the name of a library, with no directory name."
796   (eval-after-load file (read)))
797 (make-compatible 'eval-next-after-load "")
798
799 ; alternate names (not obsolete)
800 (if (not (fboundp 'mod)) (define-function 'mod '%))
801 (define-function 'move-marker 'set-marker)
802 (define-function 'beep 'ding)  ; preserve lingual purity
803 (define-function 'indent-to-column 'indent-to)
804 (define-function 'backward-delete-char 'delete-backward-char)
805 (define-function 'search-forward-regexp (symbol-function 're-search-forward))
806 (define-function 'search-backward-regexp (symbol-function 're-search-backward))
807 (define-function 'remove-directory 'delete-directory)
808 (define-function 'set-match-data 'store-match-data)
809 (define-function 'send-string-to-terminal 'external-debugging-output)
810
811 ;;; subr.el ends here