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