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