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