428
|
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.
|
772
|
6 ;; Copyright (C) 2000, 2001, 2002 Ben Wing.
|
428
|
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
|
|
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
|
|
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
|
|
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
|
442
|
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'."
|
428
|
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
|
442
|
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'."
|
428
|
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.
|
442
|
195 (flet ((hook-remove
|
924
|
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)))
|
442
|
212 (if (or local
|
924
|
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)))))
|
442
|
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))
|
428
|
303
|
878
|
304 (defun add-to-list (list-var element &optional append)
|
428
|
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'.
|
878
|
307 If ELEMENT is added, it is added at the beginning of the list,
|
|
308 unless the optional argument APPEND is non-nil, in which case
|
|
309 ELEMENT is added at the end.
|
|
310
|
428
|
311 If you want to use `add-to-list' on a variable that is not defined
|
|
312 until a certain package is loaded, you should put the call to `add-to-list'
|
|
313 into a hook function that will be run only after loading the package.
|
|
314 `eval-after-load' provides one way to do this. In some cases
|
|
315 other hooks, such as major mode hooks, can do the job."
|
878
|
316 (if (member element (symbol-value list-var))
|
|
317 (symbol-value list-var)
|
|
318 (set list-var
|
|
319 (if append
|
|
320 (append (symbol-value list-var) (list element))
|
|
321 (cons element (symbol-value list-var))))))
|
428
|
322
|
|
323 ;; XEmacs additions
|
|
324 ;; called by Fkill_buffer()
|
|
325 (defvar kill-buffer-hook nil
|
|
326 "Function or functions to be called when a buffer is killed.
|
|
327 The value of this variable may be buffer-local.
|
|
328 The buffer about to be killed is current when this hook is run.")
|
|
329
|
|
330 ;; in C in FSFmacs
|
|
331 (defvar kill-emacs-hook nil
|
|
332 "Function or functions to be called when `kill-emacs' is called,
|
|
333 just before emacs is actually killed.")
|
|
334
|
|
335 ;; not obsolete.
|
|
336 ;; #### These are a bad idea, because the CL RPLACA and RPLACD
|
|
337 ;; return the cons cell, not the new CAR/CDR. -hniksic
|
|
338 ;; The proper definition would be:
|
|
339 ;; (defun rplaca (conscell newcar)
|
|
340 ;; (setcar conscell newcar)
|
|
341 ;; conscell)
|
|
342 ;; ...and analogously for RPLACD.
|
|
343 (define-function 'rplaca 'setcar)
|
|
344 (define-function 'rplacd 'setcdr)
|
|
345
|
|
346 (defun copy-symbol (symbol &optional copy-properties)
|
|
347 "Return a new uninterned symbol with the same name as SYMBOL.
|
|
348 If COPY-PROPERTIES is non-nil, the new symbol will have a copy of
|
|
349 SYMBOL's value, function, and property lists."
|
|
350 (let ((new (make-symbol (symbol-name symbol))))
|
|
351 (when copy-properties
|
|
352 ;; This will not copy SYMBOL's chain of forwarding objects, but
|
|
353 ;; I think that's OK. Callers should not expect such magic to
|
|
354 ;; keep working in the copy in the first place.
|
|
355 (and (boundp symbol)
|
|
356 (set new (symbol-value symbol)))
|
|
357 (and (fboundp symbol)
|
|
358 (fset new (symbol-function symbol)))
|
|
359 (setplist new (copy-list (symbol-plist symbol))))
|
|
360 new))
|
|
361
|
442
|
362 (defun set-symbol-value-in-buffer (sym val buffer)
|
|
363 "Set the value of SYM to VAL in BUFFER. Useful with buffer-local variables.
|
|
364 If SYM has a buffer-local value in BUFFER, or will have one if set, this
|
|
365 function allows you to set the local value.
|
|
366
|
|
367 NOTE: At some point, this will be moved into C and will be very fast."
|
|
368 (with-current-buffer buffer
|
|
369 (set sym val)))
|
444
|
370
|
428
|
371 ;;;; String functions.
|
|
372
|
|
373 ;; XEmacs
|
801
|
374 (defun string-equal-ignore-case (str1 str2)
|
|
375 "Return t if two strings have identical contents, ignoring case differences.
|
|
376 Case is not significant. Text properties and extents are ignored.
|
|
377 Symbols are also allowed; their print names are used instead.
|
|
378
|
|
379 See also `equalp'."
|
|
380 (if (symbolp str1)
|
|
381 (setq str1 (symbol-name str1)))
|
|
382 (if (symbolp str2)
|
|
383 (setq str2 (symbol-name str2)))
|
|
384 (eq t (compare-strings str1 nil nil str2 nil nil t)))
|
|
385
|
|
386 ;; XEmacs
|
428
|
387 (defun replace-in-string (str regexp newtext &optional literal)
|
|
388 "Replace all matches in STR for REGEXP with NEWTEXT string,
|
|
389 and returns the new string.
|
|
390 Optional LITERAL non-nil means do a literal replacement.
|
442
|
391 Otherwise treat `\\' in NEWTEXT as special:
|
|
392 `\\&' in NEWTEXT means substitute original matched text.
|
|
393 `\\N' means substitute what matched the Nth `\\(...\\)'.
|
|
394 If Nth parens didn't match, substitute nothing.
|
|
395 `\\\\' means insert one `\\'.
|
|
396 `\\u' means upcase the next character.
|
|
397 `\\l' means downcase the next character.
|
|
398 `\\U' means begin upcasing all following characters.
|
|
399 `\\L' means begin downcasing all following characters.
|
|
400 `\\E' means terminate the effect of any `\\U' or `\\L'."
|
428
|
401 (check-argument-type 'stringp str)
|
|
402 (check-argument-type 'stringp newtext)
|
442
|
403 (if (> (length str) 50)
|
924
|
404 (let ((cfs case-fold-search))
|
|
405 (with-temp-buffer
|
|
406 (setq case-fold-search cfs)
|
|
407 (insert str)
|
|
408 (goto-char 1)
|
442
|
409 (while (re-search-forward regexp nil t)
|
|
410 (replace-match newtext t literal))
|
924
|
411 (buffer-string)))
|
|
412 (let ((start 0) newstr)
|
|
413 (while (string-match regexp str start)
|
|
414 (setq newstr (replace-match newtext t literal str)
|
|
415 start (+ (match-end 0) (- (length newstr) (length str)))
|
|
416 str newstr))
|
|
417 str)))
|
428
|
418
|
|
419 (defun split-string (string &optional pattern)
|
|
420 "Return a list of substrings of STRING which are separated by PATTERN.
|
|
421 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
|
|
422 (or pattern
|
|
423 (setq pattern "[ \f\t\n\r\v]+"))
|
|
424 (let (parts (start 0) (len (length string)))
|
|
425 (if (string-match pattern string)
|
|
426 (setq parts (cons (substring string 0 (match-beginning 0)) parts)
|
|
427 start (match-end 0)))
|
|
428 (while (and (< start len)
|
|
429 (string-match pattern string (if (> start (match-beginning 0))
|
|
430 start
|
|
431 (1+ start))))
|
|
432 (setq parts (cons (substring string start (match-beginning 0)) parts)
|
|
433 start (match-end 0)))
|
|
434 (nreverse (cons (substring string start) parts))))
|
|
435
|
|
436 ;; #### #### #### AAaargh! Must be in C, because it is used insanely
|
|
437 ;; early in the bootstrap process.
|
|
438 ;(defun split-path (path)
|
|
439 ; "Explode a search path into a list of strings.
|
|
440 ;The path components are separated with the characters specified
|
|
441 ;with `path-separator'."
|
|
442 ; (while (or (not stringp path-separator)
|
|
443 ; (/= (length path-separator) 1))
|
|
444 ; (setq path-separator (signal 'error (list "\
|
|
445 ;`path-separator' should be set to a single-character string"
|
|
446 ; path-separator))))
|
|
447 ; (split-string-by-char path (aref separator 0)))
|
|
448
|
1037
|
449 (defmacro with-output-to-string (&rest body)
|
|
450 "Execute BODY, return the text it sent to `standard-output', as a string."
|
|
451 `(let ((standard-output
|
|
452 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
|
|
453 (let ((standard-output standard-output))
|
|
454 ,@body)
|
|
455 (with-current-buffer standard-output
|
|
456 (prog1
|
|
457 (buffer-string)
|
|
458 (kill-buffer nil)))))
|
428
|
459
|
|
460 (defmacro with-current-buffer (buffer &rest body)
|
|
461 "Temporarily make BUFFER the current buffer and execute the forms in BODY.
|
|
462 The value returned is the value of the last form in BODY.
|
|
463 See also `with-temp-buffer'."
|
|
464 `(save-current-buffer
|
924
|
465 (set-buffer ,buffer)
|
|
466 ,@body))
|
428
|
467
|
444
|
468 (defmacro with-temp-file (filename &rest forms)
|
|
469 "Create a new buffer, evaluate FORMS there, and write the buffer to FILENAME.
|
428
|
470 The value of the last form in FORMS is returned, like `progn'.
|
|
471 See also `with-temp-buffer'."
|
|
472 (let ((temp-file (make-symbol "temp-file"))
|
|
473 (temp-buffer (make-symbol "temp-buffer")))
|
444
|
474 `(let ((,temp-file ,filename)
|
428
|
475 (,temp-buffer
|
|
476 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
|
|
477 (unwind-protect
|
|
478 (prog1
|
|
479 (with-current-buffer ,temp-buffer
|
|
480 ,@forms)
|
|
481 (with-current-buffer ,temp-buffer
|
|
482 (widen)
|
|
483 (write-region (point-min) (point-max) ,temp-file nil 0)))
|
|
484 (and (buffer-name ,temp-buffer)
|
|
485 (kill-buffer ,temp-buffer))))))
|
|
486
|
883
|
487 (defmacro with-temp-message (message &rest body)
|
|
488 "Display MESSAGE temporarily while BODY is evaluated.
|
|
489 The original message is restored to the echo area after BODY has finished.
|
|
490 The value returned is the value of the last form in BODY."
|
|
491 (let ((current-message (make-symbol "current-message"))
|
|
492 (temp-message (make-symbol "with-temp-message")))
|
|
493 `(let ((,temp-message ,message)
|
|
494 (,current-message))
|
|
495 (unwind-protect
|
|
496 (progn
|
|
497 (when ,temp-message
|
|
498 (setq ,current-message (current-message))
|
|
499 (message "%s" ,temp-message))
|
|
500 ,@body)
|
|
501 (and ,temp-message ,current-message
|
|
502 (message "%s" ,current-message))))))
|
|
503
|
428
|
504 (defmacro with-temp-buffer (&rest forms)
|
|
505 "Create a temporary buffer, and evaluate FORMS there like `progn'.
|
|
506 See also `with-temp-file' and `with-output-to-string'."
|
|
507 (let ((temp-buffer (make-symbol "temp-buffer")))
|
|
508 `(let ((,temp-buffer
|
|
509 (get-buffer-create (generate-new-buffer-name " *temp*"))))
|
|
510 (unwind-protect
|
|
511 (with-current-buffer ,temp-buffer
|
|
512 ,@forms)
|
|
513 (and (buffer-name ,temp-buffer)
|
|
514 (kill-buffer ,temp-buffer))))))
|
|
515
|
|
516 ;; Moved from mule-coding.el.
|
|
517 (defmacro with-string-as-buffer-contents (str &rest body)
|
|
518 "With the contents of the current buffer being STR, run BODY.
|
|
519 Returns the new contents of the buffer, as modified by BODY.
|
|
520 The original current buffer is restored afterwards."
|
442
|
521 `(with-temp-buffer
|
|
522 (insert ,str)
|
|
523 ,@body
|
|
524 (buffer-string)))
|
428
|
525
|
|
526 (defun insert-face (string face)
|
|
527 "Insert STRING and highlight with FACE. Return the extent created."
|
|
528 (let ((p (point)) ext)
|
|
529 (insert string)
|
|
530 (setq ext (make-extent p (point)))
|
|
531 (set-extent-face ext face)
|
|
532 ext))
|
|
533
|
|
534 ;; not obsolete.
|
|
535 (define-function 'string= 'string-equal)
|
|
536 (define-function 'string< 'string-lessp)
|
|
537 (define-function 'int-to-string 'number-to-string)
|
|
538 (define-function 'string-to-int 'string-to-number)
|
|
539
|
|
540 ;; These two names are a bit awkward, as they conflict with the normal
|
|
541 ;; foo-to-bar naming scheme, but CLtL2 has them, so they stay.
|
|
542 (define-function 'char-int 'char-to-int)
|
|
543 (define-function 'int-char 'int-to-char)
|
|
544
|
771
|
545 (defun string-width (string)
|
|
546 "Return number of columns STRING occupies when displayed.
|
|
547 With international (Mule) support, uses the charset-columns attribute of
|
|
548 the characters in STRING, which may not accurately represent the actual
|
|
549 display width when using a window system. With no international support,
|
|
550 simply returns the length of the string."
|
|
551 (if (featurep 'mule)
|
|
552 (let ((col 0)
|
|
553 (len (length string))
|
|
554 (i 0))
|
772
|
555 (with-fboundp '(charset-width char-charset)
|
|
556 (while (< i len)
|
|
557 (setq col (+ col (charset-width (char-charset (aref string i)))))
|
|
558 (setq i (1+ i))))
|
771
|
559 col)
|
|
560 (length string)))
|
|
561
|
777
|
562 (defun char-width (character)
|
|
563 "Return number of columns a CHARACTER occupies when displayed."
|
|
564 (if (featurep 'mule)
|
|
565 (with-fboundp '(charset-width char-charset)
|
|
566 (charset-width (char-charset character)))
|
|
567 1))
|
|
568
|
|
569 ;; The following several functions are useful in GNU Emacs 20 because
|
|
570 ;; of the multibyte "characters" the internal representation of which
|
|
571 ;; leaks into Lisp. In XEmacs/Mule they are trivial and unnecessary.
|
|
572 ;; We provide them for compatibility reasons solely.
|
|
573
|
|
574 (defun string-to-sequence (string type)
|
|
575 "Convert STRING to a sequence of TYPE which contains characters in STRING.
|
|
576 TYPE should be `list' or `vector'."
|
|
577 (ecase type
|
|
578 (list
|
|
579 (mapcar #'identity string))
|
|
580 (vector
|
|
581 (mapvector #'identity string))))
|
|
582
|
|
583 (defun string-to-list (string)
|
|
584 "Return a list of characters in STRING."
|
|
585 (mapcar #'identity string))
|
|
586
|
|
587 (defun string-to-vector (string)
|
|
588 "Return a vector of characters in STRING."
|
|
589 (mapvector #'identity string))
|
|
590
|
|
591 (defun store-substring (string idx obj)
|
|
592 "Embed OBJ (string or character) at index IDX of STRING."
|
|
593 (let* ((str (cond ((stringp obj) obj)
|
|
594 ((characterp obj) (char-to-string obj))
|
|
595 (t (error
|
|
596 "Invalid argument (should be string or character): %s"
|
|
597 obj))))
|
|
598 (string-len (length string))
|
|
599 (len (length str))
|
|
600 (i 0))
|
|
601 (while (and (< i len) (< idx string-len))
|
|
602 (aset string idx (aref str i))
|
|
603 (setq idx (1+ idx) i (1+ i)))
|
|
604 string))
|
|
605
|
851
|
606 ;; From FSF 21.1; ELLIPSES is XEmacs addition.
|
|
607
|
|
608 (defun truncate-string-to-width (str end-column &optional start-column padding
|
|
609 ellipses)
|
777
|
610 "Truncate string STR to end at column END-COLUMN.
|
814
|
611 The optional 3rd arg START-COLUMN, if non-nil, specifies
|
777
|
612 the starting column; that means to return the characters occupying
|
|
613 columns START-COLUMN ... END-COLUMN of STR.
|
|
614
|
814
|
615 The optional 4th arg PADDING, if non-nil, specifies a padding character
|
777
|
616 to add at the end of the result if STR doesn't reach column END-COLUMN,
|
|
617 or if END-COLUMN comes in the middle of a character in STR.
|
|
618 PADDING is also added at the beginning of the result
|
|
619 if column START-COLUMN appears in the middle of a character in STR.
|
|
620
|
|
621 If PADDING is nil, no padding is added in these cases, so
|
851
|
622 the resulting string may be narrower than END-COLUMN.
|
|
623
|
|
624 BUG: Currently assumes that the padding character is of width one. You
|
|
625 will get weird results if not.
|
|
626
|
|
627 If ELLIPSES is non-nil, add ellipses (specified by ELLIPSES if a string,
|
|
628 else `...') if STR extends past END-COLUMN. The ellipses will be added in
|
|
629 such a way that the total string occupies no more than END-COLUMN columns
|
|
630 -- i.e. if the string goes past END-COLUMN, it will be truncated somewhere
|
|
631 short of END-COLUMN so that, with the ellipses added (and padding, if the
|
|
632 proper place to truncate the string would be in the middle of a character),
|
|
633 the string occupies exactly END-COLUMN columns."
|
777
|
634 (or start-column
|
|
635 (setq start-column 0))
|
814
|
636 (let ((len (length str))
|
|
637 (idx 0)
|
|
638 (column 0)
|
|
639 (head-padding "") (tail-padding "")
|
|
640 ch last-column last-idx from-idx)
|
851
|
641
|
|
642 ;; find the index of START-COLUMN; bail out if end of string reached.
|
814
|
643 (condition-case nil
|
|
644 (while (< column start-column)
|
|
645 (setq ch (aref str idx)
|
|
646 column (+ column (char-width ch))
|
|
647 idx (1+ idx)))
|
|
648 (args-out-of-range (setq idx len)))
|
|
649 (if (< column start-column)
|
851
|
650 ;; if string ends before START-COLUMN, return either a blank string
|
|
651 ;; or a string entirely padded.
|
|
652 (if padding (make-string (- end-column start-column) padding) "")
|
814
|
653 (if (and padding (> column start-column))
|
|
654 (setq head-padding (make-string (- column start-column) padding)))
|
|
655 (setq from-idx idx)
|
851
|
656 ;; If END-COLUMN is before START-COLUMN, then bail out.
|
814
|
657 (if (< end-column column)
|
851
|
658 (setq idx from-idx ellipses "")
|
|
659
|
|
660 ;; handle ELLIPSES
|
|
661 (cond ((null ellipses) (setq ellipses ""))
|
|
662 ((if (<= (string-width str) end-column)
|
|
663 ;; string fits, no ellipses
|
|
664 (setq ellipses "")))
|
|
665 (t
|
|
666 ;; else, insert default value and ...
|
|
667 (or (stringp ellipses) (setq ellipses "..."))
|
|
668 ;; ... take away the width of the ellipses from the
|
|
669 ;; destination. do all computations with new, shorter
|
|
670 ;; width. the padding computed will get us exactly up to
|
|
671 ;; the shorted width, which is right -- it just gets added
|
|
672 ;; to the right of the ellipses.
|
924
|
673 (setq end-column (- end-column (string-width ellipses)))))
|
851
|
674
|
|
675 ;; find the index of END-COLUMN; bail out if end of string reached.
|
814
|
676 (condition-case nil
|
|
677 (while (< column end-column)
|
|
678 (setq last-column column
|
|
679 last-idx idx
|
|
680 ch (aref str idx)
|
|
681 column (+ column (char-width ch))
|
|
682 idx (1+ idx)))
|
|
683 (args-out-of-range (setq idx len)))
|
851
|
684 ;; if we went too far (stopped in middle of character), back up.
|
814
|
685 (if (> column end-column)
|
|
686 (setq column last-column idx last-idx))
|
851
|
687 ;; compute remaining padding
|
814
|
688 (if (and padding (< column end-column))
|
|
689 (setq tail-padding (make-string (- end-column column) padding))))
|
851
|
690 ;; get substring ...
|
814
|
691 (setq str (substring str from-idx idx))
|
851
|
692 ;; and construct result
|
814
|
693 (if padding
|
851
|
694 (concat head-padding str tail-padding ellipses)
|
|
695 (concat str ellipses)))))
|
801
|
696
|
428
|
697
|
|
698 ;; alist/plist functions
|
|
699 (defun plist-to-alist (plist)
|
|
700 "Convert property list PLIST into the equivalent association-list form.
|
|
701 The alist is returned. This converts from
|
|
702
|
|
703 \(a 1 b 2 c 3)
|
|
704
|
|
705 into
|
|
706
|
|
707 \((a . 1) (b . 2) (c . 3))
|
|
708
|
|
709 The original plist is not modified. See also `destructive-plist-to-alist'."
|
|
710 (let (alist)
|
|
711 (while plist
|
|
712 (setq alist (cons (cons (car plist) (cadr plist)) alist))
|
|
713 (setq plist (cddr plist)))
|
|
714 (nreverse alist)))
|
|
715
|
783
|
716 (defun map-plist (_mp_fun _mp_plist)
|
|
717 "Map _MP_FUN (a function of two args) over each key/value pair in _MP_PLIST.
|
|
718 Return a list of the results."
|
|
719 (let (_mp_result)
|
|
720 (while _mp_plist
|
|
721 (push (funcall _mp_fun (car _mp_plist) (cadr _mp_plist)) _mp_result)
|
|
722 (setq _mp_plist (cddr _mp_plist)))
|
|
723 (nreverse _mp_result)))
|
|
724
|
428
|
725 (defun destructive-plist-to-alist (plist)
|
|
726 "Convert property list PLIST into the equivalent association-list form.
|
|
727 The alist is returned. This converts from
|
|
728
|
|
729 \(a 1 b 2 c 3)
|
|
730
|
|
731 into
|
|
732
|
|
733 \((a . 1) (b . 2) (c . 3))
|
|
734
|
|
735 The original plist is destroyed in the process of constructing the alist.
|
|
736 See also `plist-to-alist'."
|
|
737 (let ((head plist)
|
|
738 next)
|
|
739 (while plist
|
|
740 ;; remember the next plist pair.
|
|
741 (setq next (cddr plist))
|
|
742 ;; make the cons holding the property value into the alist element.
|
|
743 (setcdr (cdr plist) (cadr plist))
|
|
744 (setcar (cdr plist) (car plist))
|
|
745 ;; reattach into alist form.
|
|
746 (setcar plist (cdr plist))
|
|
747 (setcdr plist next)
|
|
748 (setq plist next))
|
|
749 head))
|
|
750
|
|
751 (defun alist-to-plist (alist)
|
|
752 "Convert association list ALIST into the equivalent property-list form.
|
|
753 The plist is returned. This converts from
|
|
754
|
|
755 \((a . 1) (b . 2) (c . 3))
|
|
756
|
|
757 into
|
|
758
|
|
759 \(a 1 b 2 c 3)
|
|
760
|
|
761 The original alist is not modified. See also `destructive-alist-to-plist'."
|
|
762 (let (plist)
|
|
763 (while alist
|
|
764 (let ((el (car alist)))
|
|
765 (setq plist (cons (cdr el) (cons (car el) plist))))
|
|
766 (setq alist (cdr alist)))
|
|
767 (nreverse plist)))
|
|
768
|
|
769 ;; getf, remf in cl*.el.
|
|
770
|
444
|
771 (defmacro putf (plist property value)
|
|
772 "Add property PROPERTY to plist PLIST with value VALUE.
|
|
773 Analogous to (setq PLIST (plist-put PLIST PROPERTY VALUE))."
|
|
774 `(setq ,plist (plist-put ,plist ,property ,value)))
|
428
|
775
|
444
|
776 (defmacro laxputf (lax-plist property value)
|
|
777 "Add property PROPERTY to lax plist LAX-PLIST with value VALUE.
|
|
778 Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROPERTY VALUE))."
|
|
779 `(setq ,lax-plist (lax-plist-put ,lax-plist ,property ,value)))
|
428
|
780
|
444
|
781 (defmacro laxremf (lax-plist property)
|
|
782 "Remove property PROPERTY from lax plist LAX-PLIST.
|
|
783 Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROPERTY))."
|
|
784 `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,property)))
|
428
|
785
|
|
786 ;;; Error functions
|
|
787
|
442
|
788 (defun error (datum &rest args)
|
|
789 "Signal a non-continuable error.
|
|
790 DATUM should normally be an error symbol, i.e. a symbol defined using
|
|
791 `define-error'. ARGS will be made into a list, and DATUM and ARGS passed
|
|
792 as the two arguments to `signal', the most basic error handling function.
|
|
793
|
428
|
794 This error is not continuable: you cannot continue execution after the
|
442
|
795 error using the debugger `r' command. See also `cerror'.
|
|
796
|
|
797 The correct semantics of ARGS varies from error to error, but for most
|
|
798 errors that need to be generated in Lisp code, the first argument
|
|
799 should be a string describing the *context* of the error (i.e. the
|
|
800 exact operation being performed and what went wrong), and the remaining
|
|
801 arguments or \"frobs\" (most often, there is one) specify the
|
|
802 offending object(s) and/or provide additional details such as the exact
|
|
803 error when a file error occurred, e.g.:
|
|
804
|
|
805 -- the buffer in which an editing error occurred.
|
|
806 -- an invalid value that was encountered. (In such cases, the string
|
|
807 should describe the purpose or \"semantics\" of the value [e.g. if the
|
|
808 value is an argument to a function, the name of the argument; if the value
|
|
809 is the value corresponding to a keyword, the name of the keyword; if the
|
|
810 value is supposed to be a list length, say this and say what the purpose
|
|
811 of the list is; etc.] as well as specifying why the value is invalid, if
|
|
812 that's not self-evident.)
|
|
813 -- the file in which an error occurred. (In such cases, there should be a
|
|
814 second frob, probably a string, specifying the exact error that occurred.
|
|
815 This does not occur in the string that precedes the first frob, because
|
|
816 that frob describes the exact operation that was happening.
|
|
817
|
|
818 For historical compatibility, DATUM can also be a string. In this case,
|
|
819 DATUM and ARGS are passed together as the arguments to `format', and then
|
|
820 an error is signalled using the error symbol `error' and formatted string.
|
|
821 Although this usage of `error' is very common, it is deprecated because it
|
|
822 totally defeats the purpose of having structured errors. There is now
|
|
823 a rich set of defined errors you can use:
|
|
824
|
563
|
825 quit
|
|
826
|
442
|
827 error
|
|
828 invalid-argument
|
563
|
829 syntax-error
|
|
830 invalid-read-syntax
|
|
831 invalid-regexp
|
|
832 structure-formation-error
|
|
833 list-formation-error
|
|
834 malformed-list
|
|
835 malformed-property-list
|
|
836 circular-list
|
|
837 circular-property-list
|
|
838 invalid-function
|
|
839 no-catch
|
|
840 undefined-keystroke-sequence
|
|
841 invalid-constant
|
442
|
842 wrong-type-argument
|
|
843 args-out-of-range
|
|
844 wrong-number-of-arguments
|
428
|
845
|
442
|
846 invalid-state
|
|
847 void-function
|
|
848 cyclic-function-indirection
|
|
849 void-variable
|
|
850 cyclic-variable-indirection
|
509
|
851 invalid-byte-code
|
563
|
852 stack-overflow
|
|
853 out-of-memory
|
|
854 invalid-key-binding
|
|
855 internal-error
|
442
|
856
|
|
857 invalid-operation
|
|
858 invalid-change
|
|
859 setting-constant
|
563
|
860 protected-field
|
442
|
861 editing-error
|
|
862 beginning-of-buffer
|
|
863 end-of-buffer
|
|
864 buffer-read-only
|
|
865 io-error
|
509
|
866 file-error
|
|
867 file-already-exists
|
|
868 file-locked
|
|
869 file-supersession
|
563
|
870 end-of-file
|
|
871 process-error
|
|
872 network-error
|
509
|
873 tooltalk-error
|
563
|
874 gui-error
|
|
875 dialog-box-error
|
|
876 sound-error
|
|
877 conversion-error
|
|
878 text-conversion-error
|
|
879 image-conversion-error
|
|
880 base64-conversion-error
|
|
881 selection-conversion-error
|
442
|
882 arith-error
|
|
883 range-error
|
|
884 domain-error
|
|
885 singularity-error
|
|
886 overflow-error
|
|
887 underflow-error
|
509
|
888 search-failed
|
563
|
889 printing-unreadable-object
|
|
890 unimplemented
|
509
|
891
|
563
|
892 Note the semantic differences between some of the more common errors:
|
442
|
893
|
563
|
894 -- `invalid-argument' is for all cases where a bad value is encountered.
|
|
895 -- `invalid-constant' is for arguments where only a specific set of values
|
|
896 is allowed.
|
|
897 -- `syntax-error' is when complex structures (parsed strings, lists,
|
|
898 and the like) are badly formed. If the problem is just a single bad
|
|
899 value inside the structure, you should probably be using something else,
|
|
900 e.g. `invalid-constant', `wrong-type-argument', or `invalid-argument'.
|
442
|
901 -- `invalid-state' means that some settings have been changed in such a way
|
|
902 that their current state is unallowable. More and more, code is being
|
|
903 written more carefully, and catches the error when the settings are being
|
|
904 changed, rather than afterwards. This leads us to the next error:
|
|
905 -- `invalid-change' means that an attempt is being made to change some settings
|
|
906 into an invalid state. `invalid-change' is a type of `invalid-operation'.
|
|
907 -- `invalid-operation' refers to all cases where code is trying to do something
|
563
|
908 that's disallowed, or when an error occurred during an operation. (These
|
|
909 two concepts are merged because there's no clear distinction between them.)
|
|
910 -- `io-error' refers to errors involving interaction with any external
|
|
911 components (files, other programs, the operating system, etc).
|
442
|
912
|
|
913 See also `cerror', `signal', and `signal-error'."
|
|
914 (while t (apply
|
|
915 'cerror datum args)))
|
|
916
|
|
917 (defun cerror (datum &rest args)
|
428
|
918 "Like `error' but signals a continuable error."
|
442
|
919 (cond ((stringp datum)
|
|
920 (signal 'error (list (apply 'format datum args))))
|
|
921 ((defined-error-p datum)
|
|
922 (signal datum args))
|
|
923 (t
|
|
924 (error 'invalid-argument "datum not string or error symbol" datum))))
|
428
|
925
|
|
926 (defmacro check-argument-type (predicate argument)
|
|
927 "Check that ARGUMENT satisfies PREDICATE.
|
442
|
928 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue,
|
|
929 this function signals a continuable `wrong-type-argument' error until the
|
|
930 returned value satisfies PREDICATE, and assigns the returned value
|
|
931 to ARGUMENT. Otherwise, this function signals a non-continuable
|
|
932 `wrong-type-argument' error if the returned value does not satisfy PREDICATE."
|
|
933 (if (symbolp argument)
|
|
934 `(if (not (,(eval predicate) ,argument))
|
|
935 (setq ,argument
|
|
936 (wrong-type-argument ,predicate ,argument)))
|
|
937 `(if (not (,(eval predicate) ,argument))
|
|
938 (signal-error 'wrong-type-argument (list ,predicate ,argument)))))
|
428
|
939
|
872
|
940 (defun args-out-of-range (value min max)
|
|
941 "Signal an error until the correct in-range value is given by the user.
|
|
942 This function loops, signalling a continuable `args-out-of-range' error
|
|
943 with VALUE, MIN and MAX as the data associated with the error and then
|
|
944 checking the returned value to make sure it's not outside the given
|
|
945 boundaries \(nil for either means no boundary on that side). At that
|
|
946 point, the gotten value is returned."
|
|
947 (loop
|
|
948 for newval = (signal 'args-out-of-range (list value min max))
|
|
949 do (setq value newval)
|
|
950 finally return value
|
|
951 while (not (argument-in-range-p value min max))))
|
|
952
|
|
953 (defun argument-in-range-p (argument min max)
|
|
954 "Return true if ARGUMENT is within the range of [MIN, MAX].
|
|
955 This includes boundaries. nil for either value means no limit on that side."
|
|
956 (and (or (not min) (<= min argument))
|
|
957 (or (not max) (<= argument max))))
|
|
958
|
|
959 (defmacro check-argument-range (argument min max)
|
|
960 "Check that ARGUMENT is within the range [MIN, MAX].
|
|
961 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue,
|
|
962 this function signals a continuable `args-out-of-range' error until the
|
|
963 returned value is within range, and assigns the returned value
|
|
964 to ARGUMENT. Otherwise, this function signals a non-continuable
|
|
965 `args-out-of-range' error if the returned value is out of range."
|
|
966 (if (symbolp argument)
|
|
967 `(if (not (argument-in-range-p ,argument ,min ,max))
|
924
|
968 (setq ,argument
|
|
969 (args-out-of-range ,argument ,min ,max)))
|
872
|
970 (let ((newsym (gensym)))
|
|
971 `(let ((,newsym ,argument))
|
924
|
972 (if (not (argument-in-range-p ,newsym ,min ,max))
|
|
973 (signal-error 'args-out-of-range ,newsym ,min ,max))))))
|
872
|
974
|
428
|
975 (defun signal-error (error-symbol data)
|
|
976 "Signal a non-continuable error. Args are ERROR-SYMBOL, and associated DATA.
|
|
977 An error symbol is a symbol defined using `define-error'.
|
|
978 DATA should be a list. Its elements are printed as part of the error message.
|
|
979 If the signal is handled, DATA is made available to the handler.
|
|
980 See also `signal', and the functions to handle errors: `condition-case'
|
|
981 and `call-with-condition-handler'."
|
|
982 (while t
|
|
983 (signal error-symbol data)))
|
|
984
|
|
985 (defun define-error (error-sym doc-string &optional inherits-from)
|
|
986 "Define a new error, denoted by ERROR-SYM.
|
|
987 DOC-STRING is an informative message explaining the error, and will be
|
|
988 printed out when an unhandled error occurs.
|
|
989 ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error').
|
|
990
|
|
991 \[`define-error' internally works by putting on ERROR-SYM an `error-message'
|
|
992 property whose value is DOC-STRING, and an `error-conditions' property
|
|
993 that is a list of ERROR-SYM followed by each of its super-errors, up
|
|
994 to and including `error'. You will sometimes see code that sets this up
|
|
995 directly rather than calling `define-error', but you should *not* do this
|
|
996 yourself.]"
|
|
997 (check-argument-type 'symbolp error-sym)
|
|
998 (check-argument-type 'stringp doc-string)
|
|
999 (put error-sym 'error-message doc-string)
|
|
1000 (or inherits-from (setq inherits-from 'error))
|
|
1001 (let ((conds (get inherits-from 'error-conditions)))
|
|
1002 (or conds (signal-error 'error (list "Not an error symbol" error-sym)))
|
|
1003 (put error-sym 'error-conditions (cons error-sym conds))))
|
|
1004
|
442
|
1005 (defun defined-error-p (sym)
|
|
1006 "Returns non-nil if SYM names a currently-defined error."
|
|
1007 (and (symbolp sym) (not (null (get sym 'error-conditions)))))
|
|
1008
|
793
|
1009 (defun backtrace-in-condition-handler-eliminating-handler (handler-arg-name)
|
|
1010 "Return a backtrace inside of a condition handler, eliminating the handler.
|
|
1011 This is for use in the condition handler inside of call-with-condition-handler,
|
|
1012 when written like this:
|
|
1013
|
|
1014 \(call-with-condition-handler
|
|
1015 #'(lambda (__some_weird_arg__)
|
|
1016 do the handling ...)
|
|
1017 #'(lambda ()
|
|
1018 do the stuff that might cause an error))
|
|
1019
|
|
1020 Pass in the name (a symbol) of the argument used in the lambda function
|
|
1021 that specifies the handler, and make sure the argument name is unique, and
|
|
1022 this function generates a backtrace and strips off the part above where the
|
|
1023 error occurred (i.e. the handler itself)."
|
|
1024 (let* ((bt (with-output-to-string (backtrace nil t)))
|
|
1025 (bt (save-match-data
|
|
1026 ;; Try to eliminate the part of the backtrace
|
|
1027 ;; above where the error occurred.
|
|
1028 (if (string-match
|
|
1029 (concat "bind (\\(?:.* \\)?" (symbol-name handler-arg-name)
|
|
1030 "\\(?:.* \\)?)[ \t\n]*\\(?:(lambda \\|#<compiled-function \\)("
|
|
1031 (symbol-name handler-arg-name)
|
|
1032 ").*\n\\(\\(?:.\\|\n\\)*\\)$")
|
|
1033 bt) (match-string 1 bt) bt))))
|
|
1034 bt))
|
|
1035
|
|
1036 (put 'with-trapping-errors 'lisp-indent-function 0)
|
|
1037 (defmacro with-trapping-errors (&rest keys-body)
|
|
1038 "Trap errors in BODY, outputting a warning and a backtrace.
|
|
1039 Usage looks like
|
|
1040
|
|
1041 \(with-trapping-errors
|
|
1042 [:operation OPERATION]
|
|
1043 [:error-form ERROR-FORM]
|
|
1044 [:no-backtrace NO-BACKTRACE]
|
|
1045 [:class CLASS]
|
|
1046 [:level LEVEL]
|
|
1047 [:resignal RESIGNAL]
|
|
1048 BODY)
|
|
1049
|
|
1050 Return value without error is whatever BODY returns. With error, return
|
|
1051 result of ERROR-FORM (which will be evaluated only when the error actually
|
|
1052 occurs), which defaults to nil. OPERATION is given in the warning message.
|
|
1053 CLASS and LEVEL are the warning class and level (default to class
|
|
1054 `general', level `warning'). If NO-BACKTRACE is given, no backtrace is
|
|
1055 displayed. If RESIGNAL is given, the error is resignaled after the warning
|
|
1056 is displayed and the ERROR-FORM is executed."
|
|
1057 (let ((operation "unknown")
|
|
1058 (error-form nil)
|
|
1059 (no-backtrace nil)
|
|
1060 (class ''general)
|
|
1061 (level ''warning)
|
|
1062 (resignal nil))
|
|
1063 (let* ((keys '(operation error-form no-backtrace class level resignal))
|
|
1064 (keys-with-colon
|
|
1065 (mapcar #'(lambda (sym)
|
|
1066 (intern (concat ":" (symbol-name sym)))) keys)))
|
|
1067 (while (memq (car keys-body) keys-with-colon)
|
|
1068 (let* ((key-with-colon (pop keys-body))
|
|
1069 (key (intern (substring (symbol-name key-with-colon) 1))))
|
|
1070 (set key (pop keys-body)))))
|
|
1071 `(condition-case ,(if resignal '__cte_cc_var__ nil)
|
|
1072 (call-with-condition-handler
|
|
1073 #'(lambda (__call_trapping_errors_arg__)
|
|
1074 (let ((errstr (error-message-string
|
|
1075 __call_trapping_errors_arg__)))
|
|
1076 ,(if no-backtrace
|
|
1077 `(lwarn ,class ,level
|
|
1078 (if (warning-level-<
|
|
1079 ,level
|
|
1080 display-warning-minimum-level)
|
|
1081 "Error in %s: %s"
|
|
1082 "Error in %s:\n%s\n")
|
|
1083 ,operation errstr)
|
|
1084 `(lwarn ,class ,level
|
|
1085 "Error in %s: %s\n\nBacktrace follows:\n\n%s"
|
|
1086 ,operation errstr
|
|
1087 (backtrace-in-condition-handler-eliminating-handler
|
|
1088 '__call_trapping_errors_arg__)))))
|
|
1089 #'(lambda ()
|
|
1090 (progn ,@keys-body)))
|
|
1091 (error
|
|
1092 ,error-form
|
|
1093 ,@(if resignal '((signal (car __cte_cc_var__) (cdr __cte_cc_var__)))))
|
|
1094 )))
|
|
1095
|
428
|
1096 ;;;; Miscellanea.
|
|
1097
|
|
1098 ;; This is now in C.
|
444
|
1099 ;(defun buffer-substring-no-properties (start end)
|
|
1100 ; "Return the text from START to END, without text properties, as a string."
|
|
1101 ; (let ((string (buffer-substring start end)))
|
428
|
1102 ; (set-text-properties 0 (length string) nil string)
|
|
1103 ; string))
|
|
1104
|
|
1105 (defun get-buffer-window-list (&optional buffer minibuf frame)
|
|
1106 "Return windows currently displaying BUFFER, or nil if none.
|
|
1107 BUFFER defaults to the current buffer.
|
|
1108 See `walk-windows' for the meaning of MINIBUF and FRAME."
|
|
1109 (cond ((null buffer)
|
|
1110 (setq buffer (current-buffer)))
|
|
1111 ((not (bufferp buffer))
|
|
1112 (setq buffer (get-buffer buffer))))
|
|
1113 (let (windows)
|
|
1114 (walk-windows (lambda (window)
|
|
1115 (if (eq (window-buffer window) buffer)
|
|
1116 (push window windows)))
|
|
1117 minibuf frame)
|
|
1118 windows))
|
|
1119
|
|
1120 (defun ignore (&rest ignore)
|
|
1121 "Do nothing and return nil.
|
|
1122 This function accepts any number of arguments, but ignores them."
|
|
1123 (interactive)
|
|
1124 nil)
|
|
1125
|
883
|
1126 ;; defined in lisp/bindings.el in GNU Emacs.
|
|
1127 (defmacro bound-and-true-p (var)
|
|
1128 "Return the value of symbol VAR if it is bound, else nil."
|
|
1129 `(and (boundp (quote ,var)) ,var))
|
|
1130
|
|
1131 ;; `propertize' is a builtin in GNU Emacs 21.
|
|
1132 (defun propertize (string &rest properties)
|
|
1133 "Return a copy of STRING with text properties added.
|
|
1134 First argument is the string to copy.
|
|
1135 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
|
|
1136 properties to add to the result."
|
|
1137 (let ((str (copy-sequence string)))
|
|
1138 (add-text-properties 0 (length str)
|
|
1139 properties
|
|
1140 str)
|
|
1141 str))
|
|
1142
|
|
1143 ;; `delete-and-extract-region' is a builtin in GNU Emacs 21.
|
|
1144 (defun delete-and-extract-region (start end)
|
|
1145 "Delete the text between START and END and return it."
|
|
1146 (let ((region (buffer-substring start end)))
|
|
1147 (delete-region start end)
|
|
1148 region))
|
|
1149
|
428
|
1150 (define-function 'eval-in-buffer 'with-current-buffer)
|
|
1151 (make-obsolete 'eval-in-buffer 'with-current-buffer)
|
|
1152
|
|
1153 ;;; The real defn is in abbrev.el but some early callers
|
|
1154 ;;; (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded...
|
|
1155
|
|
1156 (if (not (fboundp 'define-abbrev-table))
|
|
1157 (progn
|
|
1158 (setq abbrev-table-name-list '())
|
924
|
1159 (fset 'define-abbrev-table
|
|
1160 (function (lambda (name defs)
|
|
1161 ;; These are fixed-up when abbrev.el loads.
|
|
1162 (setq abbrev-table-name-list
|
|
1163 (cons (cons name defs)
|
|
1164 abbrev-table-name-list)))))))
|
428
|
1165
|
|
1166 ;;; `functionp' has been moved into C.
|
|
1167
|
|
1168 ;;(defun functionp (object)
|
|
1169 ;; "Non-nil if OBJECT can be called as a function."
|
|
1170 ;; (or (and (symbolp object) (fboundp object))
|
|
1171 ;; (subrp object)
|
|
1172 ;; (compiled-function-p object)
|
|
1173 ;; (eq (car-safe object) 'lambda)))
|
|
1174
|
|
1175 (defun function-interactive (function)
|
|
1176 "Return the interactive specification of FUNCTION.
|
|
1177 FUNCTION can be any funcallable object.
|
|
1178 The specification will be returned as the list of the symbol `interactive'
|
|
1179 and the specs.
|
|
1180 If FUNCTION is not interactive, nil will be returned."
|
|
1181 (setq function (indirect-function function))
|
|
1182 (cond ((compiled-function-p function)
|
|
1183 (compiled-function-interactive function))
|
|
1184 ((subrp function)
|
|
1185 (subr-interactive function))
|
|
1186 ((eq (car-safe function) 'lambda)
|
|
1187 (let ((spec (if (stringp (nth 2 function))
|
|
1188 (nth 3 function)
|
|
1189 (nth 2 function))))
|
|
1190 (and (eq (car-safe spec) 'interactive)
|
|
1191 spec)))
|
|
1192 (t
|
|
1193 (error "Non-funcallable object: %s" function))))
|
|
1194
|
442
|
1195 (defun function-allows-args (function n)
|
|
1196 "Return whether FUNCTION can be called with N arguments."
|
|
1197 (and (<= (function-min-args function) n)
|
|
1198 (or (null (function-max-args function))
|
|
1199 (<= n (function-max-args function)))))
|
|
1200
|
428
|
1201 ;; This function used to be an alias to `buffer-substring', except
|
|
1202 ;; that FSF Emacs 20.4 added a BUFFER argument in an incompatible way.
|
|
1203 ;; The new FSF's semantics makes more sense, but we try to support
|
|
1204 ;; both for backward compatibility.
|
|
1205 (defun buffer-string (&optional buffer old-end old-buffer)
|
|
1206 "Return the contents of the current buffer as a string.
|
|
1207 If narrowing is in effect, this function returns only the visible part
|
|
1208 of the buffer.
|
|
1209
|
|
1210 If BUFFER is specified, the contents of that buffer are returned.
|
|
1211
|
|
1212 The arguments OLD-END and OLD-BUFFER are supported for backward
|
|
1213 compatibility with pre-21.2 XEmacsen times when arguments to this
|
|
1214 function were (buffer-string &optional START END BUFFER)."
|
|
1215 (cond
|
|
1216 ((or (stringp buffer) (bufferp buffer))
|
|
1217 ;; Most definitely the new way.
|
|
1218 (buffer-substring nil nil buffer))
|
|
1219 ((or (stringp old-buffer) (bufferp old-buffer)
|
|
1220 (natnump buffer) (natnump old-end))
|
|
1221 ;; Definitely the old way.
|
|
1222 (buffer-substring buffer old-end old-buffer))
|
|
1223 (t
|
|
1224 ;; Probably the old way.
|
|
1225 (buffer-substring buffer old-end old-buffer))))
|
|
1226
|
|
1227 ;; This was not present before. I think Jamie had some objections
|
|
1228 ;; to this, so I'm leaving this undefined for now. --ben
|
|
1229
|
|
1230 ;;; The objection is this: there is more than one way to load the same file.
|
|
1231 ;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different
|
|
1232 ;;; ways to load the exact same code. `eval-after-load' is too stupid to
|
|
1233 ;;; deal with this sort of thing. If this sort of feature is desired, then
|
|
1234 ;;; it should work off of a hook on `provide'. Features are unique and
|
|
1235 ;;; the arguments to (load) are not. --Stig
|
|
1236
|
|
1237 ;; We provide this for FSFmacs compatibility, at least until we devise
|
|
1238 ;; something better.
|
|
1239
|
|
1240 ;;;; Specifying things to do after certain files are loaded.
|
|
1241
|
|
1242 (defun eval-after-load (file form)
|
|
1243 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
|
|
1244 This makes or adds to an entry on `after-load-alist'.
|
|
1245 If FILE is already loaded, evaluate FORM right now.
|
|
1246 It does nothing if FORM is already on the list for FILE.
|
|
1247 FILE should be the name of a library, with no directory name."
|
|
1248 ;; Make sure there is an element for FILE.
|
|
1249 (or (assoc file after-load-alist)
|
|
1250 (setq after-load-alist (cons (list file) after-load-alist)))
|
|
1251 ;; Add FORM to the element if it isn't there.
|
|
1252 (let ((elt (assoc file after-load-alist)))
|
|
1253 (or (member form (cdr elt))
|
|
1254 (progn
|
|
1255 (nconc elt (list form))
|
|
1256 ;; If the file has been loaded already, run FORM right away.
|
|
1257 (and (assoc file load-history)
|
|
1258 (eval form)))))
|
|
1259 form)
|
|
1260 (make-compatible 'eval-after-load "")
|
|
1261
|
|
1262 (defun eval-next-after-load (file)
|
|
1263 "Read the following input sexp, and run it whenever FILE is loaded.
|
|
1264 This makes or adds to an entry on `after-load-alist'.
|
|
1265 FILE should be the name of a library, with no directory name."
|
|
1266 (eval-after-load file (read)))
|
|
1267 (make-compatible 'eval-next-after-load "")
|
|
1268
|
|
1269 ; alternate names (not obsolete)
|
|
1270 (if (not (fboundp 'mod)) (define-function 'mod '%))
|
|
1271 (define-function 'move-marker 'set-marker)
|
924
|
1272 (define-function 'beep 'ding) ; preserve lingual purity
|
428
|
1273 (define-function 'indent-to-column 'indent-to)
|
|
1274 (define-function 'backward-delete-char 'delete-backward-char)
|
|
1275 (define-function 'search-forward-regexp (symbol-function 're-search-forward))
|
|
1276 (define-function 'search-backward-regexp (symbol-function 're-search-backward))
|
|
1277 (define-function 'remove-directory 'delete-directory)
|
|
1278 (define-function 'set-match-data 'store-match-data)
|
|
1279 (define-function 'send-string-to-terminal 'external-debugging-output)
|
|
1280
|
|
1281 ;;; subr.el ends here
|