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