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