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