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