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