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