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