428
|
1 ;;; subr.el --- basic lisp subroutines for XEmacs
|
|
2
|
2525
|
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 2003
|
|
4 ;; Free Software Foundation, Inc.
|
428
|
5 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
6 ;; Copyright (C) 1995 Sun Microsystems.
|
1333
|
7 ;; Copyright (C) 2000, 2001, 2002, 2003 Ben Wing.
|
428
|
8
|
|
9 ;; Maintainer: XEmacs Development Team
|
2525
|
10 ;; Keywords: extensions, dumped, internal
|
428
|
11
|
|
12 ;; This file is part of XEmacs.
|
|
13
|
|
14 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
15 ;; under the terms of the GNU General Public License as published by
|
|
16 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
17 ;; any later version.
|
|
18
|
|
19 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
22 ;; General Public License for more details.
|
|
23
|
|
24 ;; You should have received a copy of the GNU General Public License
|
|
25 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
3000
|
26 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
27 ;; Boston, MA 02110-1301, USA.
|
428
|
28
|
1333
|
29 ;;; Synched up with: FSF 19.34. Some things synched up with later versions.
|
428
|
30
|
|
31 ;;; Commentary:
|
|
32
|
|
33 ;; This file is dumped with XEmacs.
|
|
34
|
|
35 ;; There's not a whole lot in common now with the FSF version,
|
|
36 ;; be wary when applying differences. I've left in a number of lines
|
|
37 ;; of commentary just to give diff(1) something to synch itself with to
|
|
38 ;; provide useful context diffs. -sb
|
|
39
|
1333
|
40 ;; BEGIN SYNCHED WITH FSF 21.2
|
|
41
|
428
|
42 ;;; Code:
|
1333
|
43 (defvar custom-declare-variable-list nil
|
|
44 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
|
|
45 Each element of this list holds the arguments to one call to `defcustom'.")
|
428
|
46
|
1333
|
47 ;; Use this, rather than defcustom, in subr.el and other files loaded
|
1336
|
48 ;; before custom.el. See dumped-lisp.el.
|
1333
|
49 (defun custom-declare-variable-early (&rest arguments)
|
|
50 (setq custom-declare-variable-list
|
|
51 (cons arguments custom-declare-variable-list)))
|
2525
|
52
|
|
53
|
|
54 (defun macro-declaration-function (macro decl)
|
|
55 "Process a declaration found in a macro definition.
|
|
56 This is set as the value of the variable `macro-declaration-function'.
|
|
57 MACRO is the name of the macro being defined.
|
|
58 DECL is a list `(declare ...)' containing the declarations.
|
|
59 The return value of this function is not used."
|
|
60 (dolist (d (cdr decl))
|
|
61 (cond ((and (consp d) (eq (car d) 'indent))
|
|
62 (put macro 'lisp-indent-function (cadr d)))
|
|
63 ((and (consp d) (eq (car d) 'debug))
|
|
64 (put macro 'edebug-form-spec (cadr d)))
|
|
65 (t
|
|
66 (message "Unknown declaration %s" d)))))
|
|
67
|
|
68 (setq macro-declaration-function 'macro-declaration-function)
|
|
69
|
428
|
70
|
|
71 ;;;; Lisp language features.
|
|
72
|
|
73 (defmacro lambda (&rest cdr)
|
|
74 "Return a lambda expression.
|
|
75 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
|
|
76 self-quoting; the result of evaluating the lambda expression is the
|
|
77 expression itself. The lambda expression may then be treated as a
|
|
78 function, i.e., stored as the function value of a symbol, passed to
|
|
79 funcall or mapcar, etc.
|
|
80
|
|
81 ARGS should take the same form as an argument list for a `defun'.
|
3842
|
82 Optional DOCSTRING is a documentation string.
|
|
83 If present, it should describe how to call the function. Docstrings are
|
|
84 rarely useful unless the lambda will be named, eg, using `fset'.
|
|
85 Optional INTERACTIVE should be a call to the function `interactive'.
|
|
86 BODY should be a list of lisp expressions.
|
|
87
|
|
88 The byte-compiler treats lambda expressions specially. If the lambda
|
|
89 expression is syntactically a function to be called, it will be compiled
|
|
90 unless protected by `quote'. Conversely, quoting a lambda expression with
|
|
91 `function' hints to the byte-compiler that it should compile the expression.
|
|
92 \(The byte-compiler may or may not actually compile it; for example it will
|
|
93 never compile lambdas nested in a data structure: `'(#'(lambda (x) x))').
|
|
94
|
|
95 The byte-compiler will warn about common problems such as the form
|
|
96 `(fset 'f '(lambda (x) x))' (the lambda cannot be byte-compiled; probably
|
|
97 the programmer intended `#'', although leaving the lambda unquoted will
|
|
98 normally suffice), but in general is it the programmer's responsibility to
|
|
99 quote lambda expressions appropriately."
|
428
|
100 `(function (lambda ,@cdr)))
|
|
101
|
1333
|
102 ;; FSF 21.2 has various basic macros here. We don't because they're either
|
|
103 ;; in cl*.el (which we dump and hence is always available) or built-in.
|
|
104
|
|
105 ;; More powerful versions in cl.el.
|
|
106 ;(defmacro push (newelt listname)
|
|
107 ;(defmacro pop (listname)
|
|
108
|
|
109 ;; Built-in.
|
|
110 ;(defmacro when (cond &rest body)
|
|
111 ;(defmacro unless (cond &rest body)
|
|
112
|
|
113 ;; More powerful versions in cl-macs.el.
|
|
114 ;(defmacro dolist (spec &rest body)
|
|
115 ;(defmacro dotimes (spec &rest body)
|
|
116
|
|
117 ;; In cl.el. Ours are defun, but cl arranges for them to be inlined anyway.
|
|
118 ;(defsubst caar (x)
|
|
119 ;(defsubst cadr (x)
|
|
120 ;(defsubst cdar (x)
|
|
121 ;(defsubst cddr (x)
|
|
122
|
|
123 ;; Built-in. Our `last' is more powerful in that it handles circularity.
|
|
124 ;(defun last (x &optional n)
|
|
125 ;(defun butlast (x &optional n)
|
|
126 ;(defun nbutlast (x &optional n)
|
|
127
|
|
128 ;; In cl-seq.el.
|
|
129 ;(defun remove (elt seq)
|
|
130 ;(defun remq (elt list)
|
|
131
|
428
|
132 (defmacro defun-when-void (&rest args)
|
|
133 "Define a function, just like `defun', unless it's already defined.
|
|
134 Used for compatibility among different emacs variants."
|
|
135 `(if (fboundp ',(car args))
|
|
136 nil
|
|
137 (defun ,@args)))
|
|
138
|
|
139 (defmacro define-function-when-void (&rest args)
|
|
140 "Define a function, just like `define-function', unless it's already defined.
|
|
141 Used for compatibility among different emacs variants."
|
|
142 `(if (fboundp ,(car args))
|
|
143 nil
|
|
144 (define-function ,@args)))
|
|
145
|
|
146
|
1333
|
147 (defun assoc-default (key alist &optional test default)
|
|
148 "Find object KEY in a pseudo-alist ALIST.
|
|
149 ALIST is a list of conses or objects. Each element (or the element's car,
|
|
150 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
|
|
151 If that is non-nil, the element matches;
|
|
152 then `assoc-default' returns the element's cdr, if it is a cons,
|
|
153 or DEFAULT if the element is not a cons.
|
|
154
|
|
155 If no element matches, the value is nil.
|
|
156 If TEST is omitted or nil, `equal' is used."
|
|
157 (let (found (tail alist) value)
|
|
158 (while (and tail (not found))
|
|
159 (let ((elt (car tail)))
|
|
160 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
|
|
161 (setq found t value (if (consp elt) (cdr elt) default))))
|
|
162 (setq tail (cdr tail)))
|
|
163 value))
|
|
164
|
|
165 (defun assoc-ignore-case (key alist)
|
|
166 "Like `assoc', but ignores differences in case and text representation.
|
|
167 KEY must be a string. Upper-case and lower-case letters are treated as equal."
|
|
168 (let (element)
|
|
169 (while (and alist (not element))
|
|
170 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
|
|
171 (setq element (car alist)))
|
|
172 (setq alist (cdr alist)))
|
|
173 element))
|
|
174
|
|
175 (defun assoc-ignore-representation (key alist)
|
|
176 "Like `assoc', but ignores differences in text representation.
|
|
177 KEY must be a string."
|
|
178 (let (element)
|
|
179 (while (and alist (not element))
|
|
180 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
|
|
181 (setq element (car alist)))
|
|
182 (setq alist (cdr alist)))
|
|
183 element))
|
|
184
|
|
185 (defun member-ignore-case (elt list)
|
|
186 "Like `member', but ignores differences in case and text representation.
|
|
187 ELT must be a string. Upper-case and lower-case letters are treated as equal."
|
|
188 (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t))))
|
|
189 (setq list (cdr list)))
|
|
190 list)
|
|
191
|
|
192
|
428
|
193 ;;;; Keymap support.
|
|
194 ;; XEmacs: removed to keymap.el
|
|
195
|
|
196 ;;;; The global keymap tree.
|
|
197
|
|
198 ;;; global-map, esc-map, and ctl-x-map have their values set up in
|
|
199 ;;; keymap.c; we just give them docstrings here.
|
|
200
|
|
201 ;;;; Event manipulation functions.
|
|
202
|
|
203 ;; XEmacs: This stuff is done in C Code.
|
|
204
|
1333
|
205 ;;;; Obsolescent names for functions generally appear elsewhere, in
|
|
206 ;;;; obsolete.el or in the files they are related do. Many very old
|
|
207 ;;;; obsolete stuff has been removed entirely (e.g. anything with `dot' in
|
|
208 ;;;; place of `point').
|
|
209
|
|
210 ; alternate names (not obsolete)
|
|
211 (if (not (fboundp 'mod)) (define-function 'mod '%))
|
|
212 (define-function 'move-marker 'set-marker)
|
|
213 (define-function 'beep 'ding) ; preserve lingual purity
|
|
214 (define-function 'indent-to-column 'indent-to)
|
|
215 (define-function 'backward-delete-char 'delete-backward-char)
|
|
216 (define-function 'search-forward-regexp (symbol-function 're-search-forward))
|
|
217 (define-function 'search-backward-regexp (symbol-function 're-search-backward))
|
|
218 (define-function 'remove-directory 'delete-directory)
|
|
219 (define-function 'set-match-data 'store-match-data)
|
|
220 (define-function 'send-string-to-terminal 'external-debugging-output)
|
428
|
221
|
|
222 ;; XEmacs:
|
|
223 (defun local-variable-if-set-p (sym buffer)
|
|
224 "Return t if SYM would be local to BUFFER after it is set.
|
|
225 A nil value for BUFFER is *not* the same as (current-buffer), but
|
|
226 can be used to determine whether `make-variable-buffer-local' has been
|
|
227 called on SYM."
|
|
228 (local-variable-p sym buffer t))
|
|
229
|
|
230
|
|
231 ;;;; Hook manipulation functions.
|
|
232
|
|
233 ;; (defconst run-hooks 'run-hooks ...)
|
|
234
|
|
235 (defun make-local-hook (hook)
|
|
236 "Make the hook HOOK local to the current buffer.
|
1333
|
237 The return value is HOOK.
|
|
238
|
|
239 You never need to call this function now that `add-hook' does it for you
|
|
240 if its LOCAL argument is non-nil.
|
|
241
|
428
|
242 When a hook is local, its local and global values
|
|
243 work in concert: running the hook actually runs all the hook
|
|
244 functions listed in *either* the local value *or* the global value
|
|
245 of the hook variable.
|
|
246
|
|
247 This function works by making `t' a member of the buffer-local value,
|
|
248 which acts as a flag to run the hook functions in the default value as
|
|
249 well. This works for all normal hooks, but does not work for most
|
|
250 non-normal hooks yet. We will be changing the callers of non-normal
|
|
251 hooks so that they can handle localness; this has to be done one by
|
|
252 one.
|
|
253
|
|
254 This function does nothing if HOOK is already local in the current
|
|
255 buffer.
|
|
256
|
1333
|
257 Do not use `make-local-variable' to make a hook variable buffer-local."
|
428
|
258 (if (local-variable-p hook (current-buffer)) ; XEmacs
|
|
259 nil
|
|
260 (or (boundp hook) (set hook nil))
|
|
261 (make-local-variable hook)
|
1333
|
262 (set hook (list t)))
|
|
263 hook)
|
428
|
264
|
|
265 (defun add-hook (hook function &optional append local)
|
|
266 "Add to the value of HOOK the function FUNCTION.
|
|
267 FUNCTION is not added if already present.
|
|
268 FUNCTION is added (if necessary) at the beginning of the hook list
|
|
269 unless the optional argument APPEND is non-nil, in which case
|
|
270 FUNCTION is added at the end.
|
|
271
|
|
272 The optional fourth argument, LOCAL, if non-nil, says to modify
|
|
273 the hook's buffer-local value rather than its default value.
|
1333
|
274 This makes the hook buffer-local if needed.
|
428
|
275 To make a hook variable buffer-local, always use
|
|
276 `make-local-hook', not `make-local-variable'.
|
|
277
|
|
278 HOOK should be a symbol, and FUNCTION may be any valid function. If
|
|
279 HOOK is void, it is first set to nil. If HOOK's value is a single
|
442
|
280 function, it is changed to a list of functions.
|
|
281
|
|
282 You can remove this hook yourself using `remove-hook'.
|
|
283
|
1333
|
284 See also `add-one-shot-hook'."
|
428
|
285 (or (boundp hook) (set hook nil))
|
|
286 (or (default-boundp hook) (set-default hook nil))
|
1333
|
287 (if local (unless (local-variable-if-set-p hook (current-buffer)) ; XEmacs
|
|
288 (make-local-hook hook))
|
|
289 ;; Detect the case where make-local-variable was used on a hook
|
|
290 ;; and do what we used to do.
|
|
291 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
|
|
292 (setq local t)))
|
|
293 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
|
|
294 ;; If the hook value is a single function, turn it into a list.
|
|
295 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
|
|
296 (setq hook-value (list hook-value)))
|
|
297 ;; Do the actual addition if necessary
|
|
298 (unless (member function hook-value)
|
|
299 (setq hook-value
|
|
300 (if append
|
|
301 (append hook-value (list function))
|
|
302 (cons function hook-value))))
|
|
303 ;; Set the actual variable
|
|
304 (if local (set hook hook-value) (set-default hook hook-value))))
|
428
|
305
|
|
306 (defun remove-hook (hook function &optional local)
|
|
307 "Remove from the value of HOOK the function FUNCTION.
|
|
308 HOOK should be a symbol, and FUNCTION may be any valid function. If
|
|
309 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
|
|
310 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
|
|
311
|
|
312 The optional third argument, LOCAL, if non-nil, says to modify
|
|
313 the hook's buffer-local value rather than its default value.
|
1333
|
314 This makes the hook buffer-local if needed.
|
428
|
315 To make a hook variable buffer-local, always use
|
|
316 `make-local-hook', not `make-local-variable'."
|
1333
|
317 (or (boundp hook) (set hook nil))
|
|
318 (or (default-boundp hook) (set-default hook nil))
|
|
319 (if local (unless (local-variable-if-set-p hook (current-buffer)) ; XEmacs
|
|
320 (make-local-hook hook))
|
|
321 ;; Detect the case where make-local-variable was used on a hook
|
|
322 ;; and do what we used to do.
|
|
323 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
|
|
324 (setq local t)))
|
|
325 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
|
|
326 ;; Remove the function, for both the list and the non-list cases.
|
|
327 ;; XEmacs: add hook-test, for handling one-shot hooks.
|
|
328 (flet ((hook-test
|
|
329 (fn hel)
|
|
330 (or (equal fn hel)
|
|
331 (and (symbolp hel)
|
|
332 (equal fn
|
|
333 (get hel 'one-shot-hook-fun))))))
|
|
334 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
|
|
335 (if (equal hook-value function) (setq hook-value nil))
|
|
336 (setq hook-value (delete* function (copy-sequence hook-value)
|
|
337 :test 'hook-test)))
|
|
338 ;; If the function is on the global hook, we need to shadow it locally
|
|
339 ;;(when (and local (member* function (default-value hook)
|
|
340 ;; :test 'hook-test)
|
|
341 ;; (not (member* (cons 'not function) hook-value
|
|
342 ;; :test 'hook-test)))
|
|
343 ;; (push (cons 'not function) hook-value))
|
|
344 ;; Set the actual variable
|
|
345 (if local (set hook hook-value) (set-default hook hook-value)))))
|
442
|
346
|
|
347 ;; XEmacs addition
|
|
348 ;; #### we need a coherent scheme for indicating compatibility info,
|
|
349 ;; so that it can be programmatically retrieved.
|
|
350 (defun add-local-hook (hook function &optional append)
|
|
351 "Add to the local value of HOOK the function FUNCTION.
|
1333
|
352 You don't need this any more. It's equivalent to specifying the LOCAL
|
|
353 argument to `add-hook'."
|
442
|
354 (add-hook hook function append t))
|
|
355
|
|
356 ;; XEmacs addition
|
|
357 (defun remove-local-hook (hook function)
|
|
358 "Remove from the local value of HOOK the function FUNCTION.
|
1333
|
359 You don't need this any more. It's equivalent to specifying the LOCAL
|
|
360 argument to `remove-hook'."
|
|
361 (remove-hook hook function t))
|
442
|
362
|
|
363 (defun add-one-shot-hook (hook function &optional append local)
|
|
364 "Add to the value of HOOK the one-shot function FUNCTION.
|
|
365 FUNCTION will automatically be removed from the hook the first time
|
|
366 after it runs (whether to completion or to an error).
|
|
367 FUNCTION is not added if already present.
|
|
368 FUNCTION is added (if necessary) at the beginning of the hook list
|
|
369 unless the optional argument APPEND is non-nil, in which case
|
|
370 FUNCTION is added at the end.
|
|
371
|
|
372 HOOK should be a symbol, and FUNCTION may be any valid function. If
|
|
373 HOOK is void, it is first set to nil. If HOOK's value is a single
|
|
374 function, it is changed to a list of functions.
|
|
375
|
|
376 You can remove this hook yourself using `remove-hook'.
|
|
377
|
1333
|
378 See also `add-hook'."
|
442
|
379 (let ((sym (gensym)))
|
|
380 (fset sym `(lambda (&rest args)
|
|
381 (unwind-protect
|
|
382 (apply ',function args)
|
|
383 (remove-hook ',hook ',sym ',local))))
|
|
384 (put sym 'one-shot-hook-fun function)
|
|
385 (add-hook hook sym append local)))
|
|
386
|
|
387 (defun add-local-one-shot-hook (hook function &optional append)
|
|
388 "Add to the local value of HOOK the one-shot function FUNCTION.
|
1333
|
389 You don't need this any more. It's equivalent to specifying the LOCAL
|
|
390 argument to `add-one-shot-hook'."
|
442
|
391 (add-one-shot-hook hook function append t))
|
428
|
392
|
878
|
393 (defun add-to-list (list-var element &optional append)
|
428
|
394 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
|
|
395 The test for presence of ELEMENT is done with `equal'.
|
878
|
396 If ELEMENT is added, it is added at the beginning of the list,
|
|
397 unless the optional argument APPEND is non-nil, in which case
|
|
398 ELEMENT is added at the end.
|
|
399
|
428
|
400 If you want to use `add-to-list' on a variable that is not defined
|
|
401 until a certain package is loaded, you should put the call to `add-to-list'
|
|
402 into a hook function that will be run only after loading the package.
|
|
403 `eval-after-load' provides one way to do this. In some cases
|
|
404 other hooks, such as major mode hooks, can do the job."
|
878
|
405 (if (member element (symbol-value list-var))
|
|
406 (symbol-value list-var)
|
|
407 (set list-var
|
|
408 (if append
|
|
409 (append (symbol-value list-var) (list element))
|
|
410 (cons element (symbol-value list-var))))))
|
428
|
411
|
1333
|
412 ;; END SYNCHED WITH FSF 21.2
|
|
413
|
428
|
414 ;; XEmacs additions
|
|
415 ;; called by Fkill_buffer()
|
|
416 (defvar kill-buffer-hook nil
|
|
417 "Function or functions to be called when a buffer is killed.
|
|
418 The value of this variable may be buffer-local.
|
|
419 The buffer about to be killed is current when this hook is run.")
|
|
420
|
|
421 ;; in C in FSFmacs
|
|
422 (defvar kill-emacs-hook nil
|
|
423 "Function or functions to be called when `kill-emacs' is called,
|
|
424 just before emacs is actually killed.")
|
|
425
|
|
426 ;; not obsolete.
|
|
427 ;; #### These are a bad idea, because the CL RPLACA and RPLACD
|
|
428 ;; return the cons cell, not the new CAR/CDR. -hniksic
|
|
429 ;; The proper definition would be:
|
|
430 ;; (defun rplaca (conscell newcar)
|
|
431 ;; (setcar conscell newcar)
|
|
432 ;; conscell)
|
|
433 ;; ...and analogously for RPLACD.
|
|
434 (define-function 'rplaca 'setcar)
|
|
435 (define-function 'rplacd 'setcdr)
|
|
436
|
|
437 (defun copy-symbol (symbol &optional copy-properties)
|
|
438 "Return a new uninterned symbol with the same name as SYMBOL.
|
|
439 If COPY-PROPERTIES is non-nil, the new symbol will have a copy of
|
|
440 SYMBOL's value, function, and property lists."
|
|
441 (let ((new (make-symbol (symbol-name symbol))))
|
|
442 (when copy-properties
|
|
443 ;; This will not copy SYMBOL's chain of forwarding objects, but
|
|
444 ;; I think that's OK. Callers should not expect such magic to
|
|
445 ;; keep working in the copy in the first place.
|
|
446 (and (boundp symbol)
|
|
447 (set new (symbol-value symbol)))
|
|
448 (and (fboundp symbol)
|
|
449 (fset new (symbol-function symbol)))
|
|
450 (setplist new (copy-list (symbol-plist symbol))))
|
|
451 new))
|
|
452
|
442
|
453 (defun set-symbol-value-in-buffer (sym val buffer)
|
|
454 "Set the value of SYM to VAL in BUFFER. Useful with buffer-local variables.
|
|
455 If SYM has a buffer-local value in BUFFER, or will have one if set, this
|
|
456 function allows you to set the local value.
|
|
457
|
|
458 NOTE: At some point, this will be moved into C and will be very fast."
|
|
459 (with-current-buffer buffer
|
|
460 (set sym val)))
|
444
|
461
|
1333
|
462
|
|
463 ;; BEGIN SYNCHED WITH FSF 21.2
|
|
464
|
|
465 ;; #### #### #### AAaargh! Must be in C, because it is used insanely
|
|
466 ;; early in the bootstrap process.
|
|
467 ;(defun split-path (path)
|
|
468 ; "Explode a search path into a list of strings.
|
|
469 ;The path components are separated with the characters specified
|
|
470 ;with `path-separator'."
|
|
471 ; (while (or (not stringp path-separator)
|
|
472 ; (/= (length path-separator) 1))
|
|
473 ; (setq path-separator (signal 'error (list "\
|
|
474 ;`path-separator' should be set to a single-character string"
|
|
475 ; path-separator))))
|
|
476 ; (split-string-by-char path (aref separator 0)))
|
|
477
|
|
478 (defmacro with-current-buffer (buffer &rest body)
|
|
479 "Temporarily make BUFFER the current buffer and execute the forms in BODY.
|
|
480 The value returned is the value of the last form in BODY.
|
|
481 See also `with-temp-buffer'."
|
|
482 `(save-current-buffer
|
|
483 (set-buffer ,buffer)
|
|
484 ,@body))
|
|
485
|
|
486 (defmacro with-temp-file (filename &rest forms)
|
|
487 "Create a new buffer, evaluate FORMS there, and write the buffer to FILENAME.
|
|
488 The value of the last form in FORMS is returned, like `progn'.
|
|
489 See also `with-temp-buffer'."
|
|
490 (let ((temp-file (make-symbol "temp-file"))
|
|
491 (temp-buffer (make-symbol "temp-buffer")))
|
|
492 `(let ((,temp-file ,filename)
|
|
493 (,temp-buffer
|
|
494 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
|
|
495 (unwind-protect
|
|
496 (prog1
|
|
497 (with-current-buffer ,temp-buffer
|
|
498 ,@forms)
|
|
499 (with-current-buffer ,temp-buffer
|
|
500 (widen)
|
|
501 (write-region (point-min) (point-max) ,temp-file nil 0)))
|
|
502 (and (buffer-name ,temp-buffer)
|
|
503 (kill-buffer ,temp-buffer))))))
|
|
504
|
|
505 ;; FSF compatibility
|
|
506 (defmacro with-temp-message (message &rest body)
|
|
507 "Display MESSAGE temporarily while BODY is evaluated.
|
|
508 The original message is restored to the echo area after BODY has finished.
|
|
509 The value returned is the value of the last form in BODY.
|
|
510 If MESSAGE is nil, the echo area and message log buffer are unchanged.
|
|
511 Use a MESSAGE of \"\" to temporarily clear the echo area.
|
428
|
512
|
1333
|
513 Note that this function exists for FSF compatibility purposes. A better way
|
|
514 under XEmacs is to give the message a particular label (see `display-message');
|
|
515 then, the old message is automatically restored when you clear your message
|
|
516 with `clear-message'."
|
|
517 ;; FSF additional doc string from 21.2:
|
|
518 ;; MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
|
|
519 (let ((current-message (make-symbol "current-message"))
|
|
520 (temp-message (make-symbol "with-temp-message")))
|
|
521 `(let ((,temp-message ,message)
|
|
522 (,current-message))
|
|
523 (unwind-protect
|
|
524 (progn
|
|
525 (when ,temp-message
|
|
526 (setq ,current-message (current-message))
|
|
527 (message "%s" ,temp-message))
|
|
528 ,@body)
|
|
529 (and ,temp-message ,current-message
|
|
530 (message "%s" ,current-message))))))
|
|
531
|
|
532 (defmacro with-temp-buffer (&rest forms)
|
|
533 "Create a temporary buffer, and evaluate FORMS there like `progn'.
|
|
534 See also `with-temp-file' and `with-output-to-string'."
|
|
535 (let ((temp-buffer (make-symbol "temp-buffer")))
|
|
536 `(let ((,temp-buffer
|
|
537 (get-buffer-create (generate-new-buffer-name " *temp*"))))
|
|
538 (unwind-protect
|
|
539 (with-current-buffer ,temp-buffer
|
|
540 ,@forms)
|
|
541 (and (buffer-name ,temp-buffer)
|
|
542 (kill-buffer ,temp-buffer))))))
|
|
543
|
|
544 (defmacro with-output-to-string (&rest body)
|
|
545 "Execute BODY, return the text it sent to `standard-output', as a string."
|
|
546 `(let ((standard-output
|
|
547 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
|
|
548 (let ((standard-output standard-output))
|
|
549 ,@body)
|
|
550 (with-current-buffer standard-output
|
|
551 (prog1
|
|
552 (buffer-string)
|
|
553 (kill-buffer nil)))))
|
|
554
|
2135
|
555 (defmacro with-local-quit (&rest body)
|
|
556 "Execute BODY with `inhibit-quit' temporarily bound to nil."
|
|
557 `(condition-case nil
|
|
558 (let ((inhibit-quit nil))
|
|
559 ,@body)
|
|
560 (quit (setq quit-flag t))))
|
|
561
|
|
562 ;; FSF 21.3.
|
1333
|
563
|
|
564 ; (defmacro combine-after-change-calls (&rest body)
|
|
565 ; "Execute BODY, but don't call the after-change functions till the end.
|
|
566 ; If BODY makes changes in the buffer, they are recorded
|
|
567 ; and the functions on `after-change-functions' are called several times
|
|
568 ; when BODY is finished.
|
|
569 ; The return value is the value of the last form in BODY.
|
|
570
|
|
571 ; If `before-change-functions' is non-nil, then calls to the after-change
|
|
572 ; functions can't be deferred, so in that case this macro has no effect.
|
|
573
|
|
574 ; Do not alter `after-change-functions' or `before-change-functions'
|
|
575 ; in BODY."
|
2135
|
576 ; (declare (indent 0) (debug t))
|
1333
|
577 ; `(unwind-protect
|
|
578 ; (let ((combine-after-change-calls t))
|
|
579 ; . ,body)
|
|
580 ; (combine-after-change-execute)))
|
801
|
581
|
2135
|
582
|
|
583 (defvar delay-mode-hooks nil
|
|
584 "If non-nil, `run-mode-hooks' should delay running the hooks.")
|
|
585 (defvar delayed-mode-hooks nil
|
|
586 "List of delayed mode hooks waiting to be run.")
|
|
587 (make-variable-buffer-local 'delayed-mode-hooks)
|
|
588 (put 'delay-mode-hooks 'permanent-local t)
|
|
589
|
|
590 (defun run-mode-hooks (&rest hooks)
|
|
591 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
|
|
592 Execution is delayed if `delay-mode-hooks' is non-nil.
|
|
593 Major mode functions should use this."
|
|
594 (if delay-mode-hooks
|
|
595 ;; Delaying case.
|
|
596 (dolist (hook hooks)
|
|
597 (push hook delayed-mode-hooks))
|
|
598 ;; Normal case, just run the hook as before plus any delayed hooks.
|
|
599 (setq hooks (nconc (nreverse delayed-mode-hooks) hooks))
|
|
600 (setq delayed-mode-hooks nil)
|
|
601 (apply 'run-hooks hooks)))
|
|
602
|
|
603 (defmacro delay-mode-hooks (&rest body)
|
|
604 "Execute BODY, but delay any `run-mode-hooks'.
|
|
605 Only affects hooks run in the current buffer."
|
|
606 `(progn
|
|
607 (make-local-variable 'delay-mode-hooks)
|
|
608 (let ((delay-mode-hooks t))
|
|
609 ,@body)))
|
|
610
|
1333
|
611 (defmacro with-syntax-table (table &rest body)
|
|
612 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
|
|
613 The syntax table of the current buffer is saved, BODY is evaluated, and the
|
|
614 saved table is restored, even in case of an abnormal exit.
|
|
615 Value is what BODY returns."
|
|
616 (let ((old-table (make-symbol "table"))
|
|
617 (old-buffer (make-symbol "buffer")))
|
|
618 `(let ((,old-table (syntax-table))
|
|
619 (,old-buffer (current-buffer)))
|
|
620 (unwind-protect
|
|
621 (progn
|
|
622 (set-syntax-table (copy-syntax-table ,table))
|
|
623 ,@body)
|
|
624 (save-current-buffer
|
|
625 (set-buffer ,old-buffer)
|
|
626 (set-syntax-table ,old-table))))))
|
|
627
|
|
628 (put 'with-syntax-table 'lisp-indent-function 1)
|
|
629 (put 'with-syntax-table 'edebug-form-spec '(form body))
|
|
630
|
|
631
|
|
632 ;; Moved from mule-coding.el.
|
|
633 (defmacro with-string-as-buffer-contents (str &rest body)
|
|
634 "With the contents of the current buffer being STR, run BODY.
|
|
635 Returns the new contents of the buffer, as modified by BODY.
|
|
636 The original current buffer is restored afterwards."
|
|
637 `(with-temp-buffer
|
|
638 (insert ,str)
|
|
639 ,@body
|
|
640 (buffer-string)))
|
|
641
|
|
642
|
|
643 (defmacro save-match-data (&rest body)
|
|
644 "Execute BODY forms, restoring the global value of the match data."
|
|
645 (let ((original (make-symbol "match-data")))
|
|
646 (list 'let (list (list original '(match-data)))
|
|
647 (list 'unwind-protect
|
|
648 (cons 'progn body)
|
|
649 (list 'store-match-data original)))))
|
|
650
|
|
651
|
|
652 (defun match-string (num &optional string)
|
|
653 "Return string of text matched by last search.
|
|
654 NUM specifies which parenthesized expression in the last regexp.
|
|
655 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
|
|
656 Zero means the entire text matched by the whole regexp or whole string.
|
|
657 STRING should be given if the last search was by `string-match' on STRING."
|
|
658 (if (match-beginning num)
|
|
659 (if string
|
|
660 (substring string (match-beginning num) (match-end num))
|
|
661 (buffer-substring (match-beginning num) (match-end num)))))
|
801
|
662
|
1333
|
663 (defun match-string-no-properties (num &optional string)
|
|
664 "Return string of text matched by last search, without text properties.
|
|
665 NUM specifies which parenthesized expression in the last regexp.
|
|
666 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
|
|
667 Zero means the entire text matched by the whole regexp or whole string.
|
|
668 STRING should be given if the last search was by `string-match' on STRING."
|
|
669 (if (match-beginning num)
|
|
670 (if string
|
|
671 (let ((result
|
|
672 (substring string (match-beginning num) (match-end num))))
|
|
673 (set-text-properties 0 (length result) nil result)
|
|
674 result)
|
|
675 (buffer-substring-no-properties (match-beginning num)
|
|
676 (match-end num)))))
|
|
677
|
1425
|
678 (defconst split-string-default-separators "[ \f\t\n\r\v]+"
|
|
679 "The default value of separators for `split-string'.
|
|
680
|
|
681 A regexp matching strings of whitespace. May be locale-dependent
|
|
682 \(as yet unimplemented). Should not match non-breaking spaces.
|
|
683
|
|
684 Warning: binding this to a different value and using it as default is
|
|
685 likely to have undesired semantics.")
|
|
686
|
|
687 ;; specification for `split-string' agreed with rms 2003-04-23
|
|
688 ;; xemacs design <87vfx5vor0.fsf@tleepslib.sk.tsukuba.ac.jp>
|
|
689
|
1495
|
690 ;; The specification says that if both SEPARATORS and OMIT-NULLS are
|
|
691 ;; defaulted, OMIT-NULLS should be treated as t. Simplifying the logical
|
|
692 ;; expression leads to the equivalent implementation that if SEPARATORS
|
|
693 ;; is defaulted, OMIT-NULLS is treated as t.
|
|
694
|
1425
|
695 (defun split-string (string &optional separators omit-nulls)
|
|
696 "Splits STRING into substrings bounded by matches for SEPARATORS.
|
|
697
|
|
698 The beginning and end of STRING, and each match for SEPARATORS, are
|
|
699 splitting points. The substrings matching SEPARATORS are removed, and
|
|
700 the substrings between the splitting points are collected as a list,
|
1333
|
701 which is returned.
|
1425
|
702
|
2138
|
703 If SEPARATORS is non-`nil', it should be a regular expression matching text
|
|
704 which separates, but is not part of, the substrings. If `nil' it defaults to
|
1495
|
705 `split-string-default-separators', normally \"[ \\f\\t\\n\\r\\v]+\", and
|
2138
|
706 OMIT-NULLS is forced to `t'.
|
1333
|
707
|
2138
|
708 If OMIT-NULLS is `t', zero-length substrings are omitted from the list \(so
|
1425
|
709 that for the default value of SEPARATORS leading and trailing whitespace
|
2138
|
710 are effectively trimmed). If `nil', all zero-length substrings are retained,
|
1425
|
711 which correctly parses CSV format, for example.
|
|
712
|
1495
|
713 Note that the effect of `(split-string STRING)' is the same as
|
|
714 `(split-string STRING split-string-default-separators t)'). In the rare
|
|
715 case that you wish to retain zero-length substrings when splitting on
|
|
716 whitespace, use `(split-string STRING split-string-default-separators nil)'.
|
1333
|
717
|
2138
|
718 Modifies the match data when successful; use `save-match-data' if necessary."
|
1425
|
719
|
1495
|
720 (let ((keep-nulls (not (if separators omit-nulls t)))
|
1425
|
721 (rexp (or separators split-string-default-separators))
|
1333
|
722 (start 0)
|
|
723 notfirst
|
|
724 (list nil))
|
|
725 (while (and (string-match rexp string
|
|
726 (if (and notfirst
|
|
727 (= start (match-beginning 0))
|
|
728 (< start (length string)))
|
|
729 (1+ start) start))
|
1425
|
730 (< start (length string)))
|
1333
|
731 (setq notfirst t)
|
1425
|
732 (if (or keep-nulls (< start (match-beginning 0)))
|
1333
|
733 (setq list
|
|
734 (cons (substring string start (match-beginning 0))
|
|
735 list)))
|
|
736 (setq start (match-end 0)))
|
1425
|
737 (if (or keep-nulls (< start (length string)))
|
1333
|
738 (setq list
|
|
739 (cons (substring string start)
|
|
740 list)))
|
|
741 (nreverse list)))
|
|
742
|
|
743 (defun subst-char-in-string (fromchar tochar string &optional inplace)
|
|
744 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
|
|
745 Unless optional argument INPLACE is non-nil, return a new string."
|
|
746 (let ((i (length string))
|
|
747 (newstr (if inplace string (copy-sequence string))))
|
|
748 (while (> i 0)
|
|
749 (setq i (1- i))
|
|
750 (if (eq (aref newstr i) fromchar)
|
|
751 (aset newstr i tochar)))
|
|
752 newstr))
|
|
753
|
|
754
|
|
755 ;; XEmacs addition:
|
428
|
756 (defun replace-in-string (str regexp newtext &optional literal)
|
|
757 "Replace all matches in STR for REGEXP with NEWTEXT string,
|
|
758 and returns the new string.
|
|
759 Optional LITERAL non-nil means do a literal replacement.
|
442
|
760 Otherwise treat `\\' in NEWTEXT as special:
|
|
761 `\\&' in NEWTEXT means substitute original matched text.
|
|
762 `\\N' means substitute what matched the Nth `\\(...\\)'.
|
|
763 If Nth parens didn't match, substitute nothing.
|
|
764 `\\\\' means insert one `\\'.
|
|
765 `\\u' means upcase the next character.
|
|
766 `\\l' means downcase the next character.
|
|
767 `\\U' means begin upcasing all following characters.
|
|
768 `\\L' means begin downcasing all following characters.
|
|
769 `\\E' means terminate the effect of any `\\U' or `\\L'."
|
428
|
770 (check-argument-type 'stringp str)
|
|
771 (check-argument-type 'stringp newtext)
|
442
|
772 (if (> (length str) 50)
|
924
|
773 (let ((cfs case-fold-search))
|
|
774 (with-temp-buffer
|
|
775 (setq case-fold-search cfs)
|
|
776 (insert str)
|
|
777 (goto-char 1)
|
442
|
778 (while (re-search-forward regexp nil t)
|
|
779 (replace-match newtext t literal))
|
924
|
780 (buffer-string)))
|
|
781 (let ((start 0) newstr)
|
|
782 (while (string-match regexp str start)
|
|
783 (setq newstr (replace-match newtext t literal str)
|
|
784 start (+ (match-end 0) (- (length newstr) (length str)))
|
|
785 str newstr))
|
|
786 str)))
|
428
|
787
|
1333
|
788 (defun replace-regexp-in-string (regexp rep string &optional
|
|
789 fixedcase literal subexp start)
|
|
790 "Replace all matches for REGEXP with REP in STRING.
|
|
791
|
|
792 Return a new string containing the replacements.
|
|
793
|
|
794 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
|
|
795 arguments with the same names of function `replace-match'. If START
|
|
796 is non-nil, start replacements at that index in STRING.
|
428
|
797
|
1333
|
798 REP is either a string used as the NEWTEXT arg of `replace-match' or a
|
|
799 function. If it is a function it is applied to each match to generate
|
|
800 the replacement passed to `replace-match'; the match-data at this
|
|
801 point are such that match 0 is the function's argument.
|
428
|
802
|
1333
|
803 To replace only the first match (if any), make REGEXP match up to \\'
|
|
804 and replace a sub-expression, e.g.
|
|
805 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
|
|
806 => \" bar foo\"
|
|
807 "
|
428
|
808
|
1333
|
809 ;; To avoid excessive consing from multiple matches in long strings,
|
|
810 ;; don't just call `replace-match' continually. Walk down the
|
|
811 ;; string looking for matches of REGEXP and building up a (reversed)
|
|
812 ;; list MATCHES. This comprises segments of STRING which weren't
|
|
813 ;; matched interspersed with replacements for segments that were.
|
|
814 ;; [For a `large' number of replacments it's more efficient to
|
|
815 ;; operate in a temporary buffer; we can't tell from the function's
|
|
816 ;; args whether to choose the buffer-based implementation, though it
|
|
817 ;; might be reasonable to do so for long enough STRING.]
|
|
818 (let ((l (length string))
|
|
819 (start (or start 0))
|
|
820 matches str mb me)
|
|
821 (save-match-data
|
|
822 (while (and (< start l) (string-match regexp string start))
|
|
823 (setq mb (match-beginning 0)
|
|
824 me (match-end 0))
|
|
825 ;; If we matched the empty string, make sure we advance by one char
|
|
826 (when (= me mb) (setq me (min l (1+ mb))))
|
|
827 ;; Generate a replacement for the matched substring.
|
|
828 ;; Operate only on the substring to minimize string consing.
|
|
829 ;; Set up match data for the substring for replacement;
|
|
830 ;; presumably this is likely to be faster than munging the
|
|
831 ;; match data directly in Lisp.
|
|
832 (string-match regexp (setq str (substring string mb me)))
|
|
833 (setq matches
|
|
834 (cons (replace-match (if (stringp rep)
|
|
835 rep
|
|
836 (funcall rep (match-string 0 str)))
|
|
837 fixedcase literal str subexp)
|
|
838 (cons (substring string start mb) ; unmatched prefix
|
|
839 matches)))
|
|
840 (setq start me))
|
|
841 ;; Reconstruct a string from the pieces.
|
|
842 (setq matches (cons (substring string start l) matches)) ; leftover
|
|
843 (apply #'concat (nreverse matches)))))
|
428
|
844
|
1333
|
845 ;; END SYNCHED WITH FSF 21.2
|
|
846
|
|
847
|
1899
|
848 ;; BEGIN SYNCHED WITH FSF 21.3
|
|
849
|
|
850 (defun add-to-invisibility-spec (arg)
|
|
851 "Add elements to `buffer-invisibility-spec'.
|
|
852 See documentation for `buffer-invisibility-spec' for the kind of elements
|
|
853 that can be added."
|
|
854 (if (eq buffer-invisibility-spec t)
|
|
855 (setq buffer-invisibility-spec (list t)))
|
|
856 (setq buffer-invisibility-spec
|
|
857 (cons arg buffer-invisibility-spec)))
|
|
858
|
|
859 (defun remove-from-invisibility-spec (arg)
|
|
860 "Remove elements from `buffer-invisibility-spec'."
|
|
861 (if (consp buffer-invisibility-spec)
|
|
862 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
|
|
863
|
|
864 ;; END SYNCHED WITH FSF 21.3
|
|
865
|
|
866
|
1333
|
867 ;;; Basic string functions
|
883
|
868
|
1333
|
869 ;; XEmacs
|
|
870 (defun string-equal-ignore-case (str1 str2)
|
|
871 "Return t if two strings have identical contents, ignoring case differences.
|
|
872 Case is not significant. Text properties and extents are ignored.
|
|
873 Symbols are also allowed; their print names are used instead.
|
428
|
874
|
1333
|
875 See also `equalp'."
|
|
876 (if (symbolp str1)
|
|
877 (setq str1 (symbol-name str1)))
|
|
878 (if (symbolp str2)
|
|
879 (setq str2 (symbol-name str2)))
|
|
880 (eq t (compare-strings str1 nil nil str2 nil nil t)))
|
428
|
881
|
|
882 (defun insert-face (string face)
|
|
883 "Insert STRING and highlight with FACE. Return the extent created."
|
|
884 (let ((p (point)) ext)
|
|
885 (insert string)
|
|
886 (setq ext (make-extent p (point)))
|
|
887 (set-extent-face ext face)
|
|
888 ext))
|
|
889
|
|
890 ;; not obsolete.
|
|
891 (define-function 'string= 'string-equal)
|
|
892 (define-function 'string< 'string-lessp)
|
|
893 (define-function 'int-to-string 'number-to-string)
|
|
894 (define-function 'string-to-int 'string-to-number)
|
|
895
|
|
896 ;; These two names are a bit awkward, as they conflict with the normal
|
|
897 ;; foo-to-bar naming scheme, but CLtL2 has them, so they stay.
|
|
898 (define-function 'char-int 'char-to-int)
|
|
899 (define-function 'int-char 'int-to-char)
|
|
900
|
771
|
901 (defun string-width (string)
|
|
902 "Return number of columns STRING occupies when displayed.
|
|
903 With international (Mule) support, uses the charset-columns attribute of
|
|
904 the characters in STRING, which may not accurately represent the actual
|
|
905 display width when using a window system. With no international support,
|
|
906 simply returns the length of the string."
|
|
907 (if (featurep 'mule)
|
|
908 (let ((col 0)
|
|
909 (len (length string))
|
|
910 (i 0))
|
772
|
911 (with-fboundp '(charset-width char-charset)
|
|
912 (while (< i len)
|
|
913 (setq col (+ col (charset-width (char-charset (aref string i)))))
|
|
914 (setq i (1+ i))))
|
771
|
915 col)
|
|
916 (length string)))
|
|
917
|
777
|
918 (defun char-width (character)
|
|
919 "Return number of columns a CHARACTER occupies when displayed."
|
|
920 (if (featurep 'mule)
|
|
921 (with-fboundp '(charset-width char-charset)
|
|
922 (charset-width (char-charset character)))
|
|
923 1))
|
|
924
|
|
925 ;; The following several functions are useful in GNU Emacs 20 because
|
|
926 ;; of the multibyte "characters" the internal representation of which
|
|
927 ;; leaks into Lisp. In XEmacs/Mule they are trivial and unnecessary.
|
|
928 ;; We provide them for compatibility reasons solely.
|
|
929
|
|
930 (defun string-to-sequence (string type)
|
|
931 "Convert STRING to a sequence of TYPE which contains characters in STRING.
|
|
932 TYPE should be `list' or `vector'."
|
|
933 (ecase type
|
|
934 (list
|
|
935 (mapcar #'identity string))
|
|
936 (vector
|
|
937 (mapvector #'identity string))))
|
|
938
|
|
939 (defun string-to-list (string)
|
|
940 "Return a list of characters in STRING."
|
|
941 (mapcar #'identity string))
|
|
942
|
|
943 (defun string-to-vector (string)
|
|
944 "Return a vector of characters in STRING."
|
|
945 (mapvector #'identity string))
|
|
946
|
|
947 (defun store-substring (string idx obj)
|
|
948 "Embed OBJ (string or character) at index IDX of STRING."
|
|
949 (let* ((str (cond ((stringp obj) obj)
|
|
950 ((characterp obj) (char-to-string obj))
|
|
951 (t (error
|
|
952 "Invalid argument (should be string or character): %s"
|
|
953 obj))))
|
|
954 (string-len (length string))
|
|
955 (len (length str))
|
|
956 (i 0))
|
|
957 (while (and (< i len) (< idx string-len))
|
|
958 (aset string idx (aref str i))
|
|
959 (setq idx (1+ idx) i (1+ i)))
|
|
960 string))
|
|
961
|
851
|
962 ;; From FSF 21.1; ELLIPSES is XEmacs addition.
|
|
963
|
|
964 (defun truncate-string-to-width (str end-column &optional start-column padding
|
1333
|
965 ellipses)
|
777
|
966 "Truncate string STR to end at column END-COLUMN.
|
814
|
967 The optional 3rd arg START-COLUMN, if non-nil, specifies
|
777
|
968 the starting column; that means to return the characters occupying
|
|
969 columns START-COLUMN ... END-COLUMN of STR.
|
|
970
|
814
|
971 The optional 4th arg PADDING, if non-nil, specifies a padding character
|
777
|
972 to add at the end of the result if STR doesn't reach column END-COLUMN,
|
|
973 or if END-COLUMN comes in the middle of a character in STR.
|
|
974 PADDING is also added at the beginning of the result
|
|
975 if column START-COLUMN appears in the middle of a character in STR.
|
|
976
|
|
977 If PADDING is nil, no padding is added in these cases, so
|
851
|
978 the resulting string may be narrower than END-COLUMN.
|
|
979
|
|
980 BUG: Currently assumes that the padding character is of width one. You
|
|
981 will get weird results if not.
|
|
982
|
|
983 If ELLIPSES is non-nil, add ellipses (specified by ELLIPSES if a string,
|
|
984 else `...') if STR extends past END-COLUMN. The ellipses will be added in
|
|
985 such a way that the total string occupies no more than END-COLUMN columns
|
|
986 -- i.e. if the string goes past END-COLUMN, it will be truncated somewhere
|
|
987 short of END-COLUMN so that, with the ellipses added (and padding, if the
|
|
988 proper place to truncate the string would be in the middle of a character),
|
|
989 the string occupies exactly END-COLUMN columns."
|
777
|
990 (or start-column
|
|
991 (setq start-column 0))
|
814
|
992 (let ((len (length str))
|
|
993 (idx 0)
|
|
994 (column 0)
|
|
995 (head-padding "") (tail-padding "")
|
|
996 ch last-column last-idx from-idx)
|
851
|
997
|
|
998 ;; find the index of START-COLUMN; bail out if end of string reached.
|
814
|
999 (condition-case nil
|
|
1000 (while (< column start-column)
|
|
1001 (setq ch (aref str idx)
|
|
1002 column (+ column (char-width ch))
|
|
1003 idx (1+ idx)))
|
|
1004 (args-out-of-range (setq idx len)))
|
|
1005 (if (< column start-column)
|
851
|
1006 ;; if string ends before START-COLUMN, return either a blank string
|
|
1007 ;; or a string entirely padded.
|
|
1008 (if padding (make-string (- end-column start-column) padding) "")
|
814
|
1009 (if (and padding (> column start-column))
|
|
1010 (setq head-padding (make-string (- column start-column) padding)))
|
|
1011 (setq from-idx idx)
|
851
|
1012 ;; If END-COLUMN is before START-COLUMN, then bail out.
|
814
|
1013 (if (< end-column column)
|
851
|
1014 (setq idx from-idx ellipses "")
|
|
1015
|
|
1016 ;; handle ELLIPSES
|
|
1017 (cond ((null ellipses) (setq ellipses ""))
|
|
1018 ((if (<= (string-width str) end-column)
|
|
1019 ;; string fits, no ellipses
|
|
1020 (setq ellipses "")))
|
|
1021 (t
|
|
1022 ;; else, insert default value and ...
|
|
1023 (or (stringp ellipses) (setq ellipses "..."))
|
|
1024 ;; ... take away the width of the ellipses from the
|
|
1025 ;; destination. do all computations with new, shorter
|
|
1026 ;; width. the padding computed will get us exactly up to
|
|
1027 ;; the shorted width, which is right -- it just gets added
|
|
1028 ;; to the right of the ellipses.
|
924
|
1029 (setq end-column (- end-column (string-width ellipses)))))
|
851
|
1030
|
|
1031 ;; find the index of END-COLUMN; bail out if end of string reached.
|
814
|
1032 (condition-case nil
|
|
1033 (while (< column end-column)
|
|
1034 (setq last-column column
|
|
1035 last-idx idx
|
|
1036 ch (aref str idx)
|
|
1037 column (+ column (char-width ch))
|
|
1038 idx (1+ idx)))
|
|
1039 (args-out-of-range (setq idx len)))
|
851
|
1040 ;; if we went too far (stopped in middle of character), back up.
|
814
|
1041 (if (> column end-column)
|
|
1042 (setq column last-column idx last-idx))
|
851
|
1043 ;; compute remaining padding
|
814
|
1044 (if (and padding (< column end-column))
|
|
1045 (setq tail-padding (make-string (- end-column column) padding))))
|
851
|
1046 ;; get substring ...
|
814
|
1047 (setq str (substring str from-idx idx))
|
851
|
1048 ;; and construct result
|
814
|
1049 (if padding
|
851
|
1050 (concat head-padding str tail-padding ellipses)
|
|
1051 (concat str ellipses)))))
|
801
|
1052
|
428
|
1053
|
|
1054 ;; alist/plist functions
|
|
1055 (defun plist-to-alist (plist)
|
|
1056 "Convert property list PLIST into the equivalent association-list form.
|
|
1057 The alist is returned. This converts from
|
|
1058
|
|
1059 \(a 1 b 2 c 3)
|
|
1060
|
|
1061 into
|
|
1062
|
|
1063 \((a . 1) (b . 2) (c . 3))
|
|
1064
|
|
1065 The original plist is not modified. See also `destructive-plist-to-alist'."
|
|
1066 (let (alist)
|
|
1067 (while plist
|
|
1068 (setq alist (cons (cons (car plist) (cadr plist)) alist))
|
|
1069 (setq plist (cddr plist)))
|
|
1070 (nreverse alist)))
|
|
1071
|
783
|
1072 (defun map-plist (_mp_fun _mp_plist)
|
|
1073 "Map _MP_FUN (a function of two args) over each key/value pair in _MP_PLIST.
|
|
1074 Return a list of the results."
|
|
1075 (let (_mp_result)
|
|
1076 (while _mp_plist
|
|
1077 (push (funcall _mp_fun (car _mp_plist) (cadr _mp_plist)) _mp_result)
|
|
1078 (setq _mp_plist (cddr _mp_plist)))
|
|
1079 (nreverse _mp_result)))
|
|
1080
|
428
|
1081 (defun destructive-plist-to-alist (plist)
|
|
1082 "Convert property list PLIST into the equivalent association-list form.
|
|
1083 The alist is returned. This converts from
|
|
1084
|
|
1085 \(a 1 b 2 c 3)
|
|
1086
|
|
1087 into
|
|
1088
|
|
1089 \((a . 1) (b . 2) (c . 3))
|
|
1090
|
|
1091 The original plist is destroyed in the process of constructing the alist.
|
|
1092 See also `plist-to-alist'."
|
|
1093 (let ((head plist)
|
|
1094 next)
|
|
1095 (while plist
|
|
1096 ;; remember the next plist pair.
|
|
1097 (setq next (cddr plist))
|
|
1098 ;; make the cons holding the property value into the alist element.
|
|
1099 (setcdr (cdr plist) (cadr plist))
|
|
1100 (setcar (cdr plist) (car plist))
|
|
1101 ;; reattach into alist form.
|
|
1102 (setcar plist (cdr plist))
|
|
1103 (setcdr plist next)
|
|
1104 (setq plist next))
|
|
1105 head))
|
|
1106
|
|
1107 (defun alist-to-plist (alist)
|
|
1108 "Convert association list ALIST into the equivalent property-list form.
|
|
1109 The plist is returned. This converts from
|
|
1110
|
|
1111 \((a . 1) (b . 2) (c . 3))
|
|
1112
|
|
1113 into
|
|
1114
|
|
1115 \(a 1 b 2 c 3)
|
|
1116
|
|
1117 The original alist is not modified. See also `destructive-alist-to-plist'."
|
|
1118 (let (plist)
|
|
1119 (while alist
|
|
1120 (let ((el (car alist)))
|
|
1121 (setq plist (cons (cdr el) (cons (car el) plist))))
|
|
1122 (setq alist (cdr alist)))
|
|
1123 (nreverse plist)))
|
|
1124
|
|
1125 ;; getf, remf in cl*.el.
|
|
1126
|
444
|
1127 (defmacro putf (plist property value)
|
|
1128 "Add property PROPERTY to plist PLIST with value VALUE.
|
|
1129 Analogous to (setq PLIST (plist-put PLIST PROPERTY VALUE))."
|
|
1130 `(setq ,plist (plist-put ,plist ,property ,value)))
|
428
|
1131
|
444
|
1132 (defmacro laxputf (lax-plist property value)
|
|
1133 "Add property PROPERTY to lax plist LAX-PLIST with value VALUE.
|
|
1134 Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROPERTY VALUE))."
|
|
1135 `(setq ,lax-plist (lax-plist-put ,lax-plist ,property ,value)))
|
428
|
1136
|
444
|
1137 (defmacro laxremf (lax-plist property)
|
|
1138 "Remove property PROPERTY from lax plist LAX-PLIST.
|
|
1139 Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROPERTY))."
|
|
1140 `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,property)))
|
428
|
1141
|
|
1142 ;;; Error functions
|
|
1143
|
442
|
1144 (defun error (datum &rest args)
|
|
1145 "Signal a non-continuable error.
|
|
1146 DATUM should normally be an error symbol, i.e. a symbol defined using
|
|
1147 `define-error'. ARGS will be made into a list, and DATUM and ARGS passed
|
|
1148 as the two arguments to `signal', the most basic error handling function.
|
|
1149
|
428
|
1150 This error is not continuable: you cannot continue execution after the
|
442
|
1151 error using the debugger `r' command. See also `cerror'.
|
|
1152
|
|
1153 The correct semantics of ARGS varies from error to error, but for most
|
|
1154 errors that need to be generated in Lisp code, the first argument
|
|
1155 should be a string describing the *context* of the error (i.e. the
|
|
1156 exact operation being performed and what went wrong), and the remaining
|
|
1157 arguments or \"frobs\" (most often, there is one) specify the
|
|
1158 offending object(s) and/or provide additional details such as the exact
|
|
1159 error when a file error occurred, e.g.:
|
|
1160
|
|
1161 -- the buffer in which an editing error occurred.
|
|
1162 -- an invalid value that was encountered. (In such cases, the string
|
|
1163 should describe the purpose or \"semantics\" of the value [e.g. if the
|
|
1164 value is an argument to a function, the name of the argument; if the value
|
|
1165 is the value corresponding to a keyword, the name of the keyword; if the
|
|
1166 value is supposed to be a list length, say this and say what the purpose
|
|
1167 of the list is; etc.] as well as specifying why the value is invalid, if
|
|
1168 that's not self-evident.)
|
|
1169 -- the file in which an error occurred. (In such cases, there should be a
|
|
1170 second frob, probably a string, specifying the exact error that occurred.
|
|
1171 This does not occur in the string that precedes the first frob, because
|
|
1172 that frob describes the exact operation that was happening.
|
|
1173
|
|
1174 For historical compatibility, DATUM can also be a string. In this case,
|
|
1175 DATUM and ARGS are passed together as the arguments to `format', and then
|
|
1176 an error is signalled using the error symbol `error' and formatted string.
|
|
1177 Although this usage of `error' is very common, it is deprecated because it
|
|
1178 totally defeats the purpose of having structured errors. There is now
|
|
1179 a rich set of defined errors you can use:
|
|
1180
|
563
|
1181 quit
|
|
1182
|
442
|
1183 error
|
|
1184 invalid-argument
|
563
|
1185 syntax-error
|
|
1186 invalid-read-syntax
|
|
1187 invalid-regexp
|
|
1188 structure-formation-error
|
|
1189 list-formation-error
|
|
1190 malformed-list
|
|
1191 malformed-property-list
|
|
1192 circular-list
|
|
1193 circular-property-list
|
|
1194 invalid-function
|
|
1195 no-catch
|
|
1196 undefined-keystroke-sequence
|
|
1197 invalid-constant
|
442
|
1198 wrong-type-argument
|
|
1199 args-out-of-range
|
|
1200 wrong-number-of-arguments
|
428
|
1201
|
442
|
1202 invalid-state
|
|
1203 void-function
|
|
1204 cyclic-function-indirection
|
|
1205 void-variable
|
|
1206 cyclic-variable-indirection
|
509
|
1207 invalid-byte-code
|
563
|
1208 stack-overflow
|
|
1209 out-of-memory
|
|
1210 invalid-key-binding
|
|
1211 internal-error
|
442
|
1212
|
|
1213 invalid-operation
|
|
1214 invalid-change
|
|
1215 setting-constant
|
563
|
1216 protected-field
|
442
|
1217 editing-error
|
|
1218 beginning-of-buffer
|
|
1219 end-of-buffer
|
|
1220 buffer-read-only
|
|
1221 io-error
|
509
|
1222 file-error
|
|
1223 file-already-exists
|
|
1224 file-locked
|
|
1225 file-supersession
|
563
|
1226 end-of-file
|
|
1227 process-error
|
|
1228 network-error
|
509
|
1229 tooltalk-error
|
563
|
1230 gui-error
|
|
1231 dialog-box-error
|
|
1232 sound-error
|
|
1233 conversion-error
|
|
1234 text-conversion-error
|
|
1235 image-conversion-error
|
|
1236 base64-conversion-error
|
|
1237 selection-conversion-error
|
442
|
1238 arith-error
|
|
1239 range-error
|
|
1240 domain-error
|
|
1241 singularity-error
|
|
1242 overflow-error
|
|
1243 underflow-error
|
509
|
1244 search-failed
|
563
|
1245 printing-unreadable-object
|
|
1246 unimplemented
|
509
|
1247
|
563
|
1248 Note the semantic differences between some of the more common errors:
|
442
|
1249
|
563
|
1250 -- `invalid-argument' is for all cases where a bad value is encountered.
|
|
1251 -- `invalid-constant' is for arguments where only a specific set of values
|
|
1252 is allowed.
|
|
1253 -- `syntax-error' is when complex structures (parsed strings, lists,
|
|
1254 and the like) are badly formed. If the problem is just a single bad
|
|
1255 value inside the structure, you should probably be using something else,
|
|
1256 e.g. `invalid-constant', `wrong-type-argument', or `invalid-argument'.
|
442
|
1257 -- `invalid-state' means that some settings have been changed in such a way
|
|
1258 that their current state is unallowable. More and more, code is being
|
|
1259 written more carefully, and catches the error when the settings are being
|
|
1260 changed, rather than afterwards. This leads us to the next error:
|
|
1261 -- `invalid-change' means that an attempt is being made to change some settings
|
|
1262 into an invalid state. `invalid-change' is a type of `invalid-operation'.
|
|
1263 -- `invalid-operation' refers to all cases where code is trying to do something
|
563
|
1264 that's disallowed, or when an error occurred during an operation. (These
|
|
1265 two concepts are merged because there's no clear distinction between them.)
|
|
1266 -- `io-error' refers to errors involving interaction with any external
|
|
1267 components (files, other programs, the operating system, etc).
|
442
|
1268
|
|
1269 See also `cerror', `signal', and `signal-error'."
|
|
1270 (while t (apply
|
|
1271 'cerror datum args)))
|
|
1272
|
|
1273 (defun cerror (datum &rest args)
|
428
|
1274 "Like `error' but signals a continuable error."
|
442
|
1275 (cond ((stringp datum)
|
|
1276 (signal 'error (list (apply 'format datum args))))
|
|
1277 ((defined-error-p datum)
|
|
1278 (signal datum args))
|
|
1279 (t
|
|
1280 (error 'invalid-argument "datum not string or error symbol" datum))))
|
428
|
1281
|
|
1282 (defmacro check-argument-type (predicate argument)
|
|
1283 "Check that ARGUMENT satisfies PREDICATE.
|
442
|
1284 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue,
|
|
1285 this function signals a continuable `wrong-type-argument' error until the
|
|
1286 returned value satisfies PREDICATE, and assigns the returned value
|
|
1287 to ARGUMENT. Otherwise, this function signals a non-continuable
|
|
1288 `wrong-type-argument' error if the returned value does not satisfy PREDICATE."
|
|
1289 (if (symbolp argument)
|
|
1290 `(if (not (,(eval predicate) ,argument))
|
|
1291 (setq ,argument
|
|
1292 (wrong-type-argument ,predicate ,argument)))
|
|
1293 `(if (not (,(eval predicate) ,argument))
|
|
1294 (signal-error 'wrong-type-argument (list ,predicate ,argument)))))
|
428
|
1295
|
872
|
1296 (defun args-out-of-range (value min max)
|
|
1297 "Signal an error until the correct in-range value is given by the user.
|
|
1298 This function loops, signalling a continuable `args-out-of-range' error
|
|
1299 with VALUE, MIN and MAX as the data associated with the error and then
|
|
1300 checking the returned value to make sure it's not outside the given
|
|
1301 boundaries \(nil for either means no boundary on that side). At that
|
|
1302 point, the gotten value is returned."
|
|
1303 (loop
|
|
1304 for newval = (signal 'args-out-of-range (list value min max))
|
|
1305 do (setq value newval)
|
|
1306 finally return value
|
|
1307 while (not (argument-in-range-p value min max))))
|
|
1308
|
|
1309 (defun argument-in-range-p (argument min max)
|
|
1310 "Return true if ARGUMENT is within the range of [MIN, MAX].
|
|
1311 This includes boundaries. nil for either value means no limit on that side."
|
|
1312 (and (or (not min) (<= min argument))
|
|
1313 (or (not max) (<= argument max))))
|
|
1314
|
|
1315 (defmacro check-argument-range (argument min max)
|
|
1316 "Check that ARGUMENT is within the range [MIN, MAX].
|
|
1317 This is a macro, and ARGUMENT is not evaluated. If ARGUMENT is an lvalue,
|
|
1318 this function signals a continuable `args-out-of-range' error until the
|
|
1319 returned value is within range, and assigns the returned value
|
|
1320 to ARGUMENT. Otherwise, this function signals a non-continuable
|
|
1321 `args-out-of-range' error if the returned value is out of range."
|
|
1322 (if (symbolp argument)
|
|
1323 `(if (not (argument-in-range-p ,argument ,min ,max))
|
924
|
1324 (setq ,argument
|
|
1325 (args-out-of-range ,argument ,min ,max)))
|
872
|
1326 (let ((newsym (gensym)))
|
|
1327 `(let ((,newsym ,argument))
|
924
|
1328 (if (not (argument-in-range-p ,newsym ,min ,max))
|
|
1329 (signal-error 'args-out-of-range ,newsym ,min ,max))))))
|
872
|
1330
|
428
|
1331 (defun signal-error (error-symbol data)
|
|
1332 "Signal a non-continuable error. Args are ERROR-SYMBOL, and associated DATA.
|
|
1333 An error symbol is a symbol defined using `define-error'.
|
|
1334 DATA should be a list. Its elements are printed as part of the error message.
|
|
1335 If the signal is handled, DATA is made available to the handler.
|
|
1336 See also `signal', and the functions to handle errors: `condition-case'
|
|
1337 and `call-with-condition-handler'."
|
|
1338 (while t
|
|
1339 (signal error-symbol data)))
|
|
1340
|
|
1341 (defun define-error (error-sym doc-string &optional inherits-from)
|
|
1342 "Define a new error, denoted by ERROR-SYM.
|
|
1343 DOC-STRING is an informative message explaining the error, and will be
|
|
1344 printed out when an unhandled error occurs.
|
|
1345 ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error').
|
|
1346
|
|
1347 \[`define-error' internally works by putting on ERROR-SYM an `error-message'
|
|
1348 property whose value is DOC-STRING, and an `error-conditions' property
|
|
1349 that is a list of ERROR-SYM followed by each of its super-errors, up
|
|
1350 to and including `error'. You will sometimes see code that sets this up
|
|
1351 directly rather than calling `define-error', but you should *not* do this
|
|
1352 yourself.]"
|
|
1353 (check-argument-type 'symbolp error-sym)
|
|
1354 (check-argument-type 'stringp doc-string)
|
|
1355 (put error-sym 'error-message doc-string)
|
|
1356 (or inherits-from (setq inherits-from 'error))
|
|
1357 (let ((conds (get inherits-from 'error-conditions)))
|
|
1358 (or conds (signal-error 'error (list "Not an error symbol" error-sym)))
|
|
1359 (put error-sym 'error-conditions (cons error-sym conds))))
|
|
1360
|
442
|
1361 (defun defined-error-p (sym)
|
|
1362 "Returns non-nil if SYM names a currently-defined error."
|
|
1363 (and (symbolp sym) (not (null (get sym 'error-conditions)))))
|
|
1364
|
793
|
1365 (defun backtrace-in-condition-handler-eliminating-handler (handler-arg-name)
|
|
1366 "Return a backtrace inside of a condition handler, eliminating the handler.
|
|
1367 This is for use in the condition handler inside of call-with-condition-handler,
|
|
1368 when written like this:
|
|
1369
|
|
1370 \(call-with-condition-handler
|
|
1371 #'(lambda (__some_weird_arg__)
|
|
1372 do the handling ...)
|
|
1373 #'(lambda ()
|
|
1374 do the stuff that might cause an error))
|
|
1375
|
|
1376 Pass in the name (a symbol) of the argument used in the lambda function
|
|
1377 that specifies the handler, and make sure the argument name is unique, and
|
|
1378 this function generates a backtrace and strips off the part above where the
|
|
1379 error occurred (i.e. the handler itself)."
|
|
1380 (let* ((bt (with-output-to-string (backtrace nil t)))
|
|
1381 (bt (save-match-data
|
|
1382 ;; Try to eliminate the part of the backtrace
|
|
1383 ;; above where the error occurred.
|
|
1384 (if (string-match
|
|
1385 (concat "bind (\\(?:.* \\)?" (symbol-name handler-arg-name)
|
|
1386 "\\(?:.* \\)?)[ \t\n]*\\(?:(lambda \\|#<compiled-function \\)("
|
|
1387 (symbol-name handler-arg-name)
|
|
1388 ").*\n\\(\\(?:.\\|\n\\)*\\)$")
|
|
1389 bt) (match-string 1 bt) bt))))
|
|
1390 bt))
|
|
1391
|
|
1392 (put 'with-trapping-errors 'lisp-indent-function 0)
|
|
1393 (defmacro with-trapping-errors (&rest keys-body)
|
|
1394 "Trap errors in BODY, outputting a warning and a backtrace.
|
|
1395 Usage looks like
|
|
1396
|
|
1397 \(with-trapping-errors
|
|
1398 [:operation OPERATION]
|
|
1399 [:error-form ERROR-FORM]
|
|
1400 [:no-backtrace NO-BACKTRACE]
|
|
1401 [:class CLASS]
|
|
1402 [:level LEVEL]
|
|
1403 [:resignal RESIGNAL]
|
|
1404 BODY)
|
|
1405
|
|
1406 Return value without error is whatever BODY returns. With error, return
|
|
1407 result of ERROR-FORM (which will be evaluated only when the error actually
|
|
1408 occurs), which defaults to nil. OPERATION is given in the warning message.
|
|
1409 CLASS and LEVEL are the warning class and level (default to class
|
|
1410 `general', level `warning'). If NO-BACKTRACE is given, no backtrace is
|
|
1411 displayed. If RESIGNAL is given, the error is resignaled after the warning
|
|
1412 is displayed and the ERROR-FORM is executed."
|
|
1413 (let ((operation "unknown")
|
|
1414 (error-form nil)
|
|
1415 (no-backtrace nil)
|
|
1416 (class ''general)
|
|
1417 (level ''warning)
|
|
1418 (resignal nil))
|
|
1419 (let* ((keys '(operation error-form no-backtrace class level resignal))
|
|
1420 (keys-with-colon
|
|
1421 (mapcar #'(lambda (sym)
|
|
1422 (intern (concat ":" (symbol-name sym)))) keys)))
|
|
1423 (while (memq (car keys-body) keys-with-colon)
|
|
1424 (let* ((key-with-colon (pop keys-body))
|
|
1425 (key (intern (substring (symbol-name key-with-colon) 1))))
|
|
1426 (set key (pop keys-body)))))
|
|
1427 `(condition-case ,(if resignal '__cte_cc_var__ nil)
|
|
1428 (call-with-condition-handler
|
|
1429 #'(lambda (__call_trapping_errors_arg__)
|
|
1430 (let ((errstr (error-message-string
|
|
1431 __call_trapping_errors_arg__)))
|
|
1432 ,(if no-backtrace
|
|
1433 `(lwarn ,class ,level
|
|
1434 (if (warning-level-<
|
|
1435 ,level
|
|
1436 display-warning-minimum-level)
|
|
1437 "Error in %s: %s"
|
|
1438 "Error in %s:\n%s\n")
|
|
1439 ,operation errstr)
|
|
1440 `(lwarn ,class ,level
|
|
1441 "Error in %s: %s\n\nBacktrace follows:\n\n%s"
|
|
1442 ,operation errstr
|
|
1443 (backtrace-in-condition-handler-eliminating-handler
|
|
1444 '__call_trapping_errors_arg__)))))
|
|
1445 #'(lambda ()
|
|
1446 (progn ,@keys-body)))
|
|
1447 (error
|
|
1448 ,error-form
|
|
1449 ,@(if resignal '((signal (car __cte_cc_var__) (cdr __cte_cc_var__)))))
|
|
1450 )))
|
|
1451
|
428
|
1452 ;;;; Miscellanea.
|
|
1453
|
|
1454 ;; This is now in C.
|
444
|
1455 ;(defun buffer-substring-no-properties (start end)
|
|
1456 ; "Return the text from START to END, without text properties, as a string."
|
|
1457 ; (let ((string (buffer-substring start end)))
|
428
|
1458 ; (set-text-properties 0 (length string) nil string)
|
|
1459 ; string))
|
|
1460
|
|
1461 (defun get-buffer-window-list (&optional buffer minibuf frame)
|
|
1462 "Return windows currently displaying BUFFER, or nil if none.
|
|
1463 BUFFER defaults to the current buffer.
|
|
1464 See `walk-windows' for the meaning of MINIBUF and FRAME."
|
|
1465 (cond ((null buffer)
|
|
1466 (setq buffer (current-buffer)))
|
|
1467 ((not (bufferp buffer))
|
|
1468 (setq buffer (get-buffer buffer))))
|
|
1469 (let (windows)
|
|
1470 (walk-windows (lambda (window)
|
|
1471 (if (eq (window-buffer window) buffer)
|
|
1472 (push window windows)))
|
|
1473 minibuf frame)
|
|
1474 windows))
|
|
1475
|
|
1476 (defun ignore (&rest ignore)
|
|
1477 "Do nothing and return nil.
|
|
1478 This function accepts any number of arguments, but ignores them."
|
|
1479 (interactive)
|
|
1480 nil)
|
|
1481
|
883
|
1482 ;; defined in lisp/bindings.el in GNU Emacs.
|
|
1483 (defmacro bound-and-true-p (var)
|
|
1484 "Return the value of symbol VAR if it is bound, else nil."
|
|
1485 `(and (boundp (quote ,var)) ,var))
|
|
1486
|
|
1487 ;; `propertize' is a builtin in GNU Emacs 21.
|
|
1488 (defun propertize (string &rest properties)
|
|
1489 "Return a copy of STRING with text properties added.
|
|
1490 First argument is the string to copy.
|
|
1491 Remaining arguments form a sequence of PROPERTY VALUE pairs for text
|
|
1492 properties to add to the result."
|
|
1493 (let ((str (copy-sequence string)))
|
|
1494 (add-text-properties 0 (length str)
|
|
1495 properties
|
|
1496 str)
|
|
1497 str))
|
|
1498
|
|
1499 ;; `delete-and-extract-region' is a builtin in GNU Emacs 21.
|
|
1500 (defun delete-and-extract-region (start end)
|
|
1501 "Delete the text between START and END and return it."
|
|
1502 (let ((region (buffer-substring start end)))
|
|
1503 (delete-region start end)
|
|
1504 region))
|
|
1505
|
428
|
1506 (define-function 'eval-in-buffer 'with-current-buffer)
|
|
1507 (make-obsolete 'eval-in-buffer 'with-current-buffer)
|
|
1508
|
|
1509 ;;; The real defn is in abbrev.el but some early callers
|
|
1510 ;;; (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded...
|
|
1511
|
|
1512 (if (not (fboundp 'define-abbrev-table))
|
|
1513 (progn
|
|
1514 (setq abbrev-table-name-list '())
|
924
|
1515 (fset 'define-abbrev-table
|
|
1516 (function (lambda (name defs)
|
|
1517 ;; These are fixed-up when abbrev.el loads.
|
|
1518 (setq abbrev-table-name-list
|
|
1519 (cons (cons name defs)
|
|
1520 abbrev-table-name-list)))))))
|
428
|
1521
|
|
1522 ;;; `functionp' has been moved into C.
|
|
1523
|
|
1524 ;;(defun functionp (object)
|
|
1525 ;; "Non-nil if OBJECT can be called as a function."
|
|
1526 ;; (or (and (symbolp object) (fboundp object))
|
|
1527 ;; (subrp object)
|
|
1528 ;; (compiled-function-p object)
|
|
1529 ;; (eq (car-safe object) 'lambda)))
|
|
1530
|
|
1531 (defun function-interactive (function)
|
|
1532 "Return the interactive specification of FUNCTION.
|
|
1533 FUNCTION can be any funcallable object.
|
|
1534 The specification will be returned as the list of the symbol `interactive'
|
|
1535 and the specs.
|
|
1536 If FUNCTION is not interactive, nil will be returned."
|
|
1537 (setq function (indirect-function function))
|
|
1538 (cond ((compiled-function-p function)
|
|
1539 (compiled-function-interactive function))
|
|
1540 ((subrp function)
|
|
1541 (subr-interactive function))
|
|
1542 ((eq (car-safe function) 'lambda)
|
|
1543 (let ((spec (if (stringp (nth 2 function))
|
|
1544 (nth 3 function)
|
|
1545 (nth 2 function))))
|
|
1546 (and (eq (car-safe spec) 'interactive)
|
|
1547 spec)))
|
|
1548 (t
|
|
1549 (error "Non-funcallable object: %s" function))))
|
|
1550
|
442
|
1551 (defun function-allows-args (function n)
|
|
1552 "Return whether FUNCTION can be called with N arguments."
|
|
1553 (and (<= (function-min-args function) n)
|
|
1554 (or (null (function-max-args function))
|
|
1555 (<= n (function-max-args function)))))
|
|
1556
|
428
|
1557 ;; This function used to be an alias to `buffer-substring', except
|
|
1558 ;; that FSF Emacs 20.4 added a BUFFER argument in an incompatible way.
|
|
1559 ;; The new FSF's semantics makes more sense, but we try to support
|
|
1560 ;; both for backward compatibility.
|
|
1561 (defun buffer-string (&optional buffer old-end old-buffer)
|
|
1562 "Return the contents of the current buffer as a string.
|
|
1563 If narrowing is in effect, this function returns only the visible part
|
|
1564 of the buffer.
|
|
1565
|
|
1566 If BUFFER is specified, the contents of that buffer are returned.
|
|
1567
|
|
1568 The arguments OLD-END and OLD-BUFFER are supported for backward
|
|
1569 compatibility with pre-21.2 XEmacsen times when arguments to this
|
|
1570 function were (buffer-string &optional START END BUFFER)."
|
|
1571 (cond
|
|
1572 ((or (stringp buffer) (bufferp buffer))
|
|
1573 ;; Most definitely the new way.
|
|
1574 (buffer-substring nil nil buffer))
|
|
1575 ((or (stringp old-buffer) (bufferp old-buffer)
|
|
1576 (natnump buffer) (natnump old-end))
|
|
1577 ;; Definitely the old way.
|
|
1578 (buffer-substring buffer old-end old-buffer))
|
|
1579 (t
|
|
1580 ;; Probably the old way.
|
|
1581 (buffer-substring buffer old-end old-buffer))))
|
|
1582
|
1333
|
1583 ;; BEGIN SYNC WITH FSF 21.2
|
|
1584
|
428
|
1585 ;; This was not present before. I think Jamie had some objections
|
|
1586 ;; to this, so I'm leaving this undefined for now. --ben
|
|
1587
|
|
1588 ;;; The objection is this: there is more than one way to load the same file.
|
|
1589 ;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different
|
|
1590 ;;; ways to load the exact same code. `eval-after-load' is too stupid to
|
|
1591 ;;; deal with this sort of thing. If this sort of feature is desired, then
|
|
1592 ;;; it should work off of a hook on `provide'. Features are unique and
|
|
1593 ;;; the arguments to (load) are not. --Stig
|
|
1594
|
|
1595 ;; We provide this for FSFmacs compatibility, at least until we devise
|
|
1596 ;; something better.
|
|
1597
|
|
1598 ;;;; Specifying things to do after certain files are loaded.
|
|
1599
|
|
1600 (defun eval-after-load (file form)
|
|
1601 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
|
|
1602 This makes or adds to an entry on `after-load-alist'.
|
|
1603 If FILE is already loaded, evaluate FORM right now.
|
|
1604 It does nothing if FORM is already on the list for FILE.
|
1333
|
1605 FILE must match exactly. Normally FILE is the name of a library,
|
|
1606 with no directory or extension specified, since that is how `load'
|
|
1607 is normally called."
|
|
1608 ;; Make sure `load-history' contains the files dumped with Emacs
|
|
1609 ;; for the case that FILE is one of the files dumped with Emacs.
|
|
1610 (if-fboundp 'load-symbol-file-load-history
|
|
1611 (load-symbol-file-load-history))
|
428
|
1612 ;; Make sure there is an element for FILE.
|
|
1613 (or (assoc file after-load-alist)
|
|
1614 (setq after-load-alist (cons (list file) after-load-alist)))
|
|
1615 ;; Add FORM to the element if it isn't there.
|
|
1616 (let ((elt (assoc file after-load-alist)))
|
|
1617 (or (member form (cdr elt))
|
|
1618 (progn
|
|
1619 (nconc elt (list form))
|
|
1620 ;; If the file has been loaded already, run FORM right away.
|
|
1621 (and (assoc file load-history)
|
|
1622 (eval form)))))
|
|
1623 form)
|
|
1624 (make-compatible 'eval-after-load "")
|
|
1625
|
|
1626 (defun eval-next-after-load (file)
|
|
1627 "Read the following input sexp, and run it whenever FILE is loaded.
|
|
1628 This makes or adds to an entry on `after-load-alist'.
|
|
1629 FILE should be the name of a library, with no directory name."
|
|
1630 (eval-after-load file (read)))
|
|
1631 (make-compatible 'eval-next-after-load "")
|
|
1632
|
1333
|
1633 ;; END SYNC WITH FSF 21.2
|
428
|
1634
|
3000
|
1635 ;; BEGIN SYNC WITH FSF 22.0.50.1 (CVS)
|
|
1636 (defun delete-dups (list)
|
|
1637 "Destructively remove `equal' duplicates from LIST.
|
|
1638 Store the result in LIST and return it. LIST must be a proper list.
|
|
1639 Of several `equal' occurrences of an element in LIST, the first
|
|
1640 one is kept."
|
|
1641 (let ((tail list))
|
|
1642 (while tail
|
|
1643 (setcdr tail (delete (car tail) (cdr tail)))
|
|
1644 (setq tail (cdr tail))))
|
|
1645 list)
|
|
1646
|
|
1647 ;; END SYNC WITH FSF 22.0.50.1 (CVS)
|
|
1648
|
2525
|
1649 ;; (defun shell-quote-argument (argument) in process.el.
|
|
1650
|
|
1651 ;; (defun make-syntax-table (&optional oldtable) in syntax.el.
|
|
1652
|
|
1653 ;; (defun syntax-after (pos) #### doesn't exist.
|
|
1654
|
|
1655 ;; global-set-key, local-set-key, global-unset-key, local-unset-key in
|
|
1656 ;; keymap.el.
|
|
1657
|
|
1658 ;; frame-configuration-p is in frame.el.
|
|
1659
|
|
1660 ;; functionp is built-in.
|
|
1661
|
|
1662 ;; interactive-form in obsolete.el.
|
|
1663
|
|
1664 ;; assq-del-all in obsolete.el.
|
|
1665
|
|
1666 ;; (defun make-temp-file (prefix &optional dir-flag suffix) #### doesn't exist.
|
|
1667
|
|
1668 ;; add-minor-mode in modeline.el.
|
|
1669
|
|
1670 ;; text-clone stuff #### doesn't exist; should go in text-props.el and
|
|
1671 ;; requires changes to extents.c (modification hooks).
|
|
1672
|
|
1673 ;; play-sound is built-in.
|
|
1674
|
|
1675 ;; define-mail-user-agent is in simple.el.
|
|
1676
|
428
|
1677 ;;; subr.el ends here
|