Mercurial > emacs
annotate src/fns.c @ 464:4ddaee1a9029
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Fri, 20 Dec 1991 07:15:37 +0000 |
parents | 4c9349866dac |
children | 8c615e453683 |
rev | line source |
---|---|
211 | 1 /* Random utility Lisp functions. |
2 Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 1, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
21 #include "config.h" | |
22 | |
23 #ifdef LOAD_AVE_TYPE | |
24 #ifdef BSD | |
25 /* It appears param.h defines BSD and BSD4_3 in 4.3 | |
26 and is not considerate enough to avoid bombing out | |
27 if they are already defined. */ | |
28 #undef BSD | |
29 #ifdef BSD4_3 | |
30 #undef BSD4_3 | |
31 #define XBSD4_3 /* XBSD4_3 says BSD4_3 is supposed to be defined. */ | |
32 #endif | |
33 #include <sys/param.h> | |
34 /* Now if BSD or BSD4_3 was defined and is no longer, | |
35 define it again. */ | |
36 #ifndef BSD | |
37 #define BSD | |
38 #endif | |
39 #ifdef XBSD4_3 | |
40 #ifndef BSD4_3 | |
41 #define BSD4_3 | |
42 #endif | |
43 #endif /* XBSD4_3 */ | |
44 #endif /* BSD */ | |
45 #ifndef VMS | |
46 #ifndef NLIST_STRUCT | |
47 #include <a.out.h> | |
48 #else /* NLIST_STRUCT */ | |
49 #include <nlist.h> | |
50 #endif /* NLIST_STRUCT */ | |
51 #endif /* not VMS */ | |
52 #endif /* LOAD_AVE_TYPE */ | |
53 | |
350 | 54 #ifdef DGUX |
55 #include <sys/dg_sys_info.h> /* for load average info - DJB */ | |
56 #endif | |
57 | |
211 | 58 /* Note on some machines this defines `vector' as a typedef, |
59 so make sure we don't use that name in this file. */ | |
60 #undef vector | |
61 #define vector ***** | |
62 | |
63 #ifdef NULL | |
64 #undef NULL | |
65 #endif | |
66 #include "lisp.h" | |
67 #include "commands.h" | |
68 | |
69 #ifdef MULTI_SCREEN | |
70 #include "screen.h" | |
71 #endif | |
72 | |
73 #include "buffer.h" | |
74 | |
75 Lisp_Object Qstring_lessp; | |
76 | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
77 static Lisp_Object internal_equal (); |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
78 |
211 | 79 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0, |
80 "Return the argument unchanged.") | |
81 (arg) | |
82 Lisp_Object arg; | |
83 { | |
84 return arg; | |
85 } | |
86 | |
87 DEFUN ("random", Frandom, Srandom, 0, 1, 0, | |
88 "Return a pseudo-random number.\n\ | |
89 On most systems all integers representable in Lisp are equally likely.\n\ | |
90 This is 24 bits' worth.\n\ | |
91 With argument N, return random number in interval [0,N).\n\ | |
92 With argument t, set the random number seed from the current time and pid.") | |
93 (arg) | |
94 Lisp_Object arg; | |
95 { | |
96 int val; | |
97 extern long random (); | |
98 extern srandom (); | |
99 extern long time (); | |
100 | |
101 if (EQ (arg, Qt)) | |
102 srandom (getpid () + time (0)); | |
103 val = random (); | |
104 if (XTYPE (arg) == Lisp_Int && XINT (arg) != 0) | |
105 { | |
106 /* Try to take our random number from the higher bits of VAL, | |
107 not the lower, since (says Gentzel) the low bits of `random' | |
108 are less random than the higher ones. */ | |
109 val &= 0xfffffff; /* Ensure positive. */ | |
110 val >>= 5; | |
111 if (XINT (arg) < 10000) | |
112 val >>= 6; | |
113 val %= XINT (arg); | |
114 } | |
115 return make_number (val); | |
116 } | |
117 | |
118 /* Random data-structure functions */ | |
119 | |
120 DEFUN ("length", Flength, Slength, 1, 1, 0, | |
121 "Return the length of vector, list or string SEQUENCE.\n\ | |
122 A byte-code function object is also allowed.") | |
123 (obj) | |
124 register Lisp_Object obj; | |
125 { | |
126 register Lisp_Object tail, val; | |
127 register int i; | |
128 | |
129 retry: | |
130 if (XTYPE (obj) == Lisp_Vector || XTYPE (obj) == Lisp_String | |
131 || XTYPE (obj) == Lisp_Compiled) | |
132 return Farray_length (obj); | |
133 else if (CONSP (obj)) | |
134 { | |
135 for (i = 0, tail = obj; !NULL(tail); i++) | |
136 { | |
137 QUIT; | |
138 tail = Fcdr (tail); | |
139 } | |
140 | |
141 XFASTINT (val) = i; | |
142 return val; | |
143 } | |
144 else if (NULL(obj)) | |
145 { | |
146 XFASTINT (val) = 0; | |
147 return val; | |
148 } | |
149 else | |
150 { | |
151 obj = wrong_type_argument (Qsequencep, obj); | |
152 goto retry; | |
153 } | |
154 } | |
155 | |
156 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0, | |
157 "T if two strings have identical contents.\n\ | |
158 Case is significant.\n\ | |
159 Symbols are also allowed; their print names are used instead.") | |
160 (s1, s2) | |
161 register Lisp_Object s1, s2; | |
162 { | |
163 if (XTYPE (s1) == Lisp_Symbol) | |
164 XSETSTRING (s1, XSYMBOL (s1)->name), XSETTYPE (s1, Lisp_String); | |
165 if (XTYPE (s2) == Lisp_Symbol) | |
166 XSETSTRING (s2, XSYMBOL (s2)->name), XSETTYPE (s2, Lisp_String); | |
167 CHECK_STRING (s1, 0); | |
168 CHECK_STRING (s2, 1); | |
169 | |
170 if (XSTRING (s1)->size != XSTRING (s2)->size || | |
171 bcmp (XSTRING (s1)->data, XSTRING (s2)->data, XSTRING (s1)->size)) | |
172 return Qnil; | |
173 return Qt; | |
174 } | |
175 | |
176 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0, | |
177 "T if first arg string is less than second in lexicographic order.\n\ | |
178 Case is significant.\n\ | |
179 Symbols are also allowed; their print names are used instead.") | |
180 (s1, s2) | |
181 register Lisp_Object s1, s2; | |
182 { | |
183 register int i; | |
184 register unsigned char *p1, *p2; | |
185 register int end; | |
186 | |
187 if (XTYPE (s1) == Lisp_Symbol) | |
188 XSETSTRING (s1, XSYMBOL (s1)->name), XSETTYPE (s1, Lisp_String); | |
189 if (XTYPE (s2) == Lisp_Symbol) | |
190 XSETSTRING (s2, XSYMBOL (s2)->name), XSETTYPE (s2, Lisp_String); | |
191 CHECK_STRING (s1, 0); | |
192 CHECK_STRING (s2, 1); | |
193 | |
194 p1 = XSTRING (s1)->data; | |
195 p2 = XSTRING (s2)->data; | |
196 end = XSTRING (s1)->size; | |
197 if (end > XSTRING (s2)->size) | |
198 end = XSTRING (s2)->size; | |
199 | |
200 for (i = 0; i < end; i++) | |
201 { | |
202 if (p1[i] != p2[i]) | |
203 return p1[i] < p2[i] ? Qt : Qnil; | |
204 } | |
205 return i < XSTRING (s2)->size ? Qt : Qnil; | |
206 } | |
207 | |
208 static Lisp_Object concat (); | |
209 | |
210 /* ARGSUSED */ | |
211 Lisp_Object | |
212 concat2 (s1, s2) | |
213 Lisp_Object s1, s2; | |
214 { | |
215 #ifdef NO_ARG_ARRAY | |
216 Lisp_Object args[2]; | |
217 args[0] = s1; | |
218 args[1] = s2; | |
219 return concat (2, args, Lisp_String, 0); | |
220 #else | |
221 return concat (2, &s1, Lisp_String, 0); | |
222 #endif /* NO_ARG_ARRAY */ | |
223 } | |
224 | |
225 DEFUN ("append", Fappend, Sappend, 0, MANY, 0, | |
226 "Concatenate all the arguments and make the result a list.\n\ | |
227 The result is a list whose elements are the elements of all the arguments.\n\ | |
228 Each argument may be a list, vector or string.\n\ | |
229 The last argument is not copied if it is a list.") | |
230 (nargs, args) | |
231 int nargs; | |
232 Lisp_Object *args; | |
233 { | |
234 return concat (nargs, args, Lisp_Cons, 1); | |
235 } | |
236 | |
237 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0, | |
238 "Concatenate all the arguments and make the result a string.\n\ | |
239 The result is a string whose elements are the elements of all the arguments.\n\ | |
240 Each argument may be a string, a list of numbers, or a vector of numbers.") | |
241 (nargs, args) | |
242 int nargs; | |
243 Lisp_Object *args; | |
244 { | |
245 return concat (nargs, args, Lisp_String, 0); | |
246 } | |
247 | |
248 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0, | |
249 "Concatenate all the arguments and make the result a vector.\n\ | |
250 The result is a vector whose elements are the elements of all the arguments.\n\ | |
251 Each argument may be a list, vector or string.") | |
252 (nargs, args) | |
253 int nargs; | |
254 Lisp_Object *args; | |
255 { | |
256 return concat (nargs, args, Lisp_Vector, 0); | |
257 } | |
258 | |
259 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0, | |
260 "Return a copy of a list, vector or string.\n\ | |
261 The elements of a list or vector are not copied; they are shared\n\ | |
262 with the original.") | |
263 (arg) | |
264 Lisp_Object arg; | |
265 { | |
266 if (NULL (arg)) return arg; | |
267 if (!CONSP (arg) && XTYPE (arg) != Lisp_Vector && XTYPE (arg) != Lisp_String) | |
268 arg = wrong_type_argument (Qsequencep, arg); | |
269 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0); | |
270 } | |
271 | |
272 static Lisp_Object | |
273 concat (nargs, args, target_type, last_special) | |
274 int nargs; | |
275 Lisp_Object *args; | |
276 enum Lisp_Type target_type; | |
277 int last_special; | |
278 { | |
279 Lisp_Object val; | |
280 Lisp_Object len; | |
281 register Lisp_Object tail; | |
282 register Lisp_Object this; | |
283 int toindex; | |
284 register int leni; | |
285 register int argnum; | |
286 Lisp_Object last_tail; | |
287 Lisp_Object prev; | |
288 | |
289 /* In append, the last arg isn't treated like the others */ | |
290 if (last_special && nargs > 0) | |
291 { | |
292 nargs--; | |
293 last_tail = args[nargs]; | |
294 } | |
295 else | |
296 last_tail = Qnil; | |
297 | |
298 for (argnum = 0; argnum < nargs; argnum++) | |
299 { | |
300 this = args[argnum]; | |
301 if (!(CONSP (this) || NULL (this) | |
302 || XTYPE (this) == Lisp_Vector || XTYPE (this) == Lisp_String | |
303 || XTYPE (this) == Lisp_Compiled)) | |
304 { | |
305 if (XTYPE (this) == Lisp_Int) | |
306 args[argnum] = Fint_to_string (this); | |
307 else | |
308 args[argnum] = wrong_type_argument (Qsequencep, this); | |
309 } | |
310 } | |
311 | |
312 for (argnum = 0, leni = 0; argnum < nargs; argnum++) | |
313 { | |
314 this = args[argnum]; | |
315 len = Flength (this); | |
316 leni += XFASTINT (len); | |
317 } | |
318 | |
319 XFASTINT (len) = leni; | |
320 | |
321 if (target_type == Lisp_Cons) | |
322 val = Fmake_list (len, Qnil); | |
323 else if (target_type == Lisp_Vector) | |
324 val = Fmake_vector (len, Qnil); | |
325 else | |
326 val = Fmake_string (len, len); | |
327 | |
328 /* In append, if all but last arg are nil, return last arg */ | |
329 if (target_type == Lisp_Cons && EQ (val, Qnil)) | |
330 return last_tail; | |
331 | |
332 if (CONSP (val)) | |
333 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */ | |
334 else | |
335 toindex = 0; | |
336 | |
337 prev = Qnil; | |
338 | |
339 for (argnum = 0; argnum < nargs; argnum++) | |
340 { | |
341 Lisp_Object thislen; | |
342 int thisleni; | |
343 register int thisindex = 0; | |
344 | |
345 this = args[argnum]; | |
346 if (!CONSP (this)) | |
347 thislen = Flength (this), thisleni = XINT (thislen); | |
348 | |
349 while (1) | |
350 { | |
351 register Lisp_Object elt; | |
352 | |
353 /* Fetch next element of `this' arg into `elt', or break if `this' is exhausted. */ | |
354 if (NULL (this)) break; | |
355 if (CONSP (this)) | |
356 elt = Fcar (this), this = Fcdr (this); | |
357 else | |
358 { | |
359 if (thisindex >= thisleni) break; | |
360 if (XTYPE (this) == Lisp_String) | |
361 XFASTINT (elt) = XSTRING (this)->data[thisindex++]; | |
362 else | |
363 elt = XVECTOR (this)->contents[thisindex++]; | |
364 } | |
365 | |
366 /* Store into result */ | |
367 if (toindex < 0) | |
368 { | |
369 XCONS (tail)->car = elt; | |
370 prev = tail; | |
371 tail = XCONS (tail)->cdr; | |
372 } | |
373 else if (XTYPE (val) == Lisp_Vector) | |
374 XVECTOR (val)->contents[toindex++] = elt; | |
375 else | |
376 { | |
377 while (XTYPE (elt) != Lisp_Int) | |
378 elt = wrong_type_argument (Qintegerp, elt); | |
379 { | |
380 #ifdef MASSC_REGISTER_BUG | |
381 /* Even removing all "register"s doesn't disable this bug! | |
382 Nothing simpler than this seems to work. */ | |
383 unsigned char *p = & XSTRING (val)->data[toindex++]; | |
384 *p = XINT (elt); | |
385 #else | |
386 XSTRING (val)->data[toindex++] = XINT (elt); | |
387 #endif | |
388 } | |
389 } | |
390 } | |
391 } | |
392 if (!NULL (prev)) | |
393 XCONS (prev)->cdr = last_tail; | |
394 | |
395 return val; | |
396 } | |
397 | |
398 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0, | |
399 "Return a copy of ALIST.\n\ | |
400 This is an alist which represents the same mapping from objects to objects,\n\ | |
401 but does not share the alist structure with ALIST.\n\ | |
402 The objects mapped (cars and cdrs of elements of the alist)\n\ | |
403 are shared, however.\n\ | |
404 Elements of ALIST that are not conses are also shared.") | |
405 (alist) | |
406 Lisp_Object alist; | |
407 { | |
408 register Lisp_Object tem; | |
409 | |
410 CHECK_LIST (alist, 0); | |
411 if (NULL (alist)) | |
412 return alist; | |
413 alist = concat (1, &alist, Lisp_Cons, 0); | |
414 for (tem = alist; CONSP (tem); tem = XCONS (tem)->cdr) | |
415 { | |
416 register Lisp_Object car; | |
417 car = XCONS (tem)->car; | |
418 | |
419 if (CONSP (car)) | |
420 XCONS (tem)->car = Fcons (XCONS (car)->car, XCONS (car)->cdr); | |
421 } | |
422 return alist; | |
423 } | |
424 | |
425 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0, | |
426 "Return a substring of STRING, starting at index FROM and ending before TO.\n\ | |
427 TO may be nil or omitted; then the substring runs to the end of STRING.\n\ | |
428 If FROM or TO is negative, it counts from the end.") | |
429 (string, from, to) | |
430 Lisp_Object string; | |
431 register Lisp_Object from, to; | |
432 { | |
433 CHECK_STRING (string, 0); | |
434 CHECK_NUMBER (from, 1); | |
435 if (NULL (to)) | |
436 to = Flength (string); | |
437 else | |
438 CHECK_NUMBER (to, 2); | |
439 | |
440 if (XINT (from) < 0) | |
441 XSETINT (from, XINT (from) + XSTRING (string)->size); | |
442 if (XINT (to) < 0) | |
443 XSETINT (to, XINT (to) + XSTRING (string)->size); | |
444 if (!(0 <= XINT (from) && XINT (from) <= XINT (to) | |
445 && XINT (to) <= XSTRING (string)->size)) | |
446 args_out_of_range_3 (string, from, to); | |
447 | |
448 return make_string (XSTRING (string)->data + XINT (from), | |
449 XINT (to) - XINT (from)); | |
450 } | |
451 | |
452 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0, | |
453 "Take cdr N times on LIST, returns the result.") | |
454 (n, list) | |
455 Lisp_Object n; | |
456 register Lisp_Object list; | |
457 { | |
458 register int i, num; | |
459 CHECK_NUMBER (n, 0); | |
460 num = XINT (n); | |
461 for (i = 0; i < num && !NULL (list); i++) | |
462 { | |
463 QUIT; | |
464 list = Fcdr (list); | |
465 } | |
466 return list; | |
467 } | |
468 | |
469 DEFUN ("nth", Fnth, Snth, 2, 2, 0, | |
470 "Return the Nth element of LIST.\n\ | |
471 N counts from zero. If LIST is not that long, nil is returned.") | |
472 (n, list) | |
473 Lisp_Object n, list; | |
474 { | |
475 return Fcar (Fnthcdr (n, list)); | |
476 } | |
477 | |
478 DEFUN ("elt", Felt, Selt, 2, 2, 0, | |
479 "Return element of SEQUENCE at index N.") | |
480 (seq, n) | |
481 register Lisp_Object seq, n; | |
482 { | |
483 CHECK_NUMBER (n, 0); | |
484 while (1) | |
485 { | |
486 if (XTYPE (seq) == Lisp_Cons || NULL (seq)) | |
487 return Fcar (Fnthcdr (n, seq)); | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
488 else if (XTYPE (seq) == Lisp_String |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
489 || XTYPE (seq) == Lisp_Vector) |
211 | 490 return Faref (seq, n); |
491 else | |
492 seq = wrong_type_argument (Qsequencep, seq); | |
493 } | |
494 } | |
495 | |
496 DEFUN ("member", Fmember, Smember, 2, 2, 0, | |
497 "Return non-nil if ELT is an element of LIST. Comparison done with EQUAL.\n\ | |
498 The value is actually the tail of LIST whose car is ELT.") | |
499 (elt, list) | |
500 register Lisp_Object elt; | |
501 Lisp_Object list; | |
502 { | |
503 register Lisp_Object tail; | |
504 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
505 { | |
506 register Lisp_Object tem; | |
507 tem = Fcar (tail); | |
508 if (! NULL (Fequal (elt, tem))) | |
509 return tail; | |
510 QUIT; | |
511 } | |
512 return Qnil; | |
513 } | |
514 | |
515 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0, | |
516 "Return non-nil if ELT is an element of LIST. Comparison done with EQ.\n\ | |
517 The value is actually the tail of LIST whose car is ELT.") | |
518 (elt, list) | |
519 register Lisp_Object elt; | |
520 Lisp_Object list; | |
521 { | |
522 register Lisp_Object tail; | |
523 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
524 { | |
525 register Lisp_Object tem; | |
526 tem = Fcar (tail); | |
527 if (EQ (elt, tem)) return tail; | |
528 QUIT; | |
529 } | |
530 return Qnil; | |
531 } | |
532 | |
533 DEFUN ("assq", Fassq, Sassq, 2, 2, 0, | |
534 "Return non-nil if ELT is `eq' to the car of an element of LIST.\n\ | |
535 The value is actually the element of LIST whose car is ELT.\n\ | |
536 Elements of LIST that are not conses are ignored.") | |
537 (key, list) | |
538 register Lisp_Object key; | |
539 Lisp_Object list; | |
540 { | |
541 register Lisp_Object tail; | |
542 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
543 { | |
544 register Lisp_Object elt, tem; | |
545 elt = Fcar (tail); | |
546 if (!CONSP (elt)) continue; | |
547 tem = Fcar (elt); | |
548 if (EQ (key, tem)) return elt; | |
549 QUIT; | |
550 } | |
551 return Qnil; | |
552 } | |
553 | |
554 /* Like Fassq but never report an error and do not allow quits. | |
555 Use only on lists known never to be circular. */ | |
556 | |
557 Lisp_Object | |
558 assq_no_quit (key, list) | |
559 register Lisp_Object key; | |
560 Lisp_Object list; | |
561 { | |
562 register Lisp_Object tail; | |
563 for (tail = list; CONSP (tail); tail = Fcdr (tail)) | |
564 { | |
565 register Lisp_Object elt, tem; | |
566 elt = Fcar (tail); | |
567 if (!CONSP (elt)) continue; | |
568 tem = Fcar (elt); | |
569 if (EQ (key, tem)) return elt; | |
570 } | |
571 return Qnil; | |
572 } | |
573 | |
574 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0, | |
575 "Return non-nil if ELT is `equal' to the car of an element of LIST.\n\ | |
576 The value is actually the element of LIST whose car is ELT.") | |
577 (key, list) | |
578 register Lisp_Object key; | |
579 Lisp_Object list; | |
580 { | |
581 register Lisp_Object tail; | |
582 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
583 { | |
584 register Lisp_Object elt, tem; | |
585 elt = Fcar (tail); | |
586 if (!CONSP (elt)) continue; | |
587 tem = Fequal (Fcar (elt), key); | |
588 if (!NULL (tem)) return elt; | |
589 QUIT; | |
590 } | |
591 return Qnil; | |
592 } | |
593 | |
594 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0, | |
595 "Return non-nil if ELT is `eq' to the cdr of an element of LIST.\n\ | |
596 The value is actually the element of LIST whose cdr is ELT.") | |
597 (key, list) | |
598 register Lisp_Object key; | |
599 Lisp_Object list; | |
600 { | |
601 register Lisp_Object tail; | |
602 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
603 { | |
604 register Lisp_Object elt, tem; | |
605 elt = Fcar (tail); | |
606 if (!CONSP (elt)) continue; | |
607 tem = Fcdr (elt); | |
608 if (EQ (key, tem)) return elt; | |
609 QUIT; | |
610 } | |
611 return Qnil; | |
612 } | |
613 | |
614 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0, | |
615 "Delete by side effect any occurrences of ELT as a member of LIST.\n\ | |
616 The modified LIST is returned. Comparison is done with `eq'.\n\ | |
617 If the first member of LIST is ELT, there is no way to remove it by side effect;\n\ | |
618 therefore, write `(setq foo (delq element foo))'\n\ | |
619 to be sure of changing the value of `foo'.") | |
620 (elt, list) | |
621 register Lisp_Object elt; | |
622 Lisp_Object list; | |
623 { | |
624 register Lisp_Object tail, prev; | |
625 register Lisp_Object tem; | |
626 | |
627 tail = list; | |
628 prev = Qnil; | |
629 while (!NULL (tail)) | |
630 { | |
631 tem = Fcar (tail); | |
632 if (EQ (elt, tem)) | |
633 { | |
634 if (NULL (prev)) | |
635 list = Fcdr (tail); | |
636 else | |
637 Fsetcdr (prev, Fcdr (tail)); | |
638 } | |
639 else | |
640 prev = tail; | |
641 tail = Fcdr (tail); | |
642 QUIT; | |
643 } | |
644 return list; | |
645 } | |
646 | |
414 | 647 DEFUN ("delete", Fdelete, Sdelete, 2, 2, 0, |
401 | 648 "Delete by side effect any occurrences of ELT as a member of LIST.\n\ |
649 The modified LIST is returned. Comparison is done with `equal'.\n\ | |
650 If the first member of LIST is ELT, there is no way to remove it by side effect;\n\ | |
651 therefore, write `(setq foo (delete element foo))'\n\ | |
652 to be sure of changing the value of `foo'.") | |
653 (elt, list) | |
654 register Lisp_Object elt; | |
655 Lisp_Object list; | |
656 { | |
657 register Lisp_Object tail, prev; | |
658 register Lisp_Object tem; | |
659 | |
660 tail = list; | |
661 prev = Qnil; | |
662 while (!NULL (tail)) | |
663 { | |
664 tem = Fcar (tail); | |
665 if (Fequal (elt, tem)) | |
666 { | |
667 if (NULL (prev)) | |
668 list = Fcdr (tail); | |
669 else | |
670 Fsetcdr (prev, Fcdr (tail)); | |
671 } | |
672 else | |
673 prev = tail; | |
674 tail = Fcdr (tail); | |
675 QUIT; | |
676 } | |
677 return list; | |
678 } | |
679 | |
211 | 680 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0, |
681 "Reverse LIST by modifying cdr pointers.\n\ | |
682 Returns the beginning of the reversed list.") | |
683 (list) | |
684 Lisp_Object list; | |
685 { | |
686 register Lisp_Object prev, tail, next; | |
687 | |
688 if (NULL (list)) return list; | |
689 prev = Qnil; | |
690 tail = list; | |
691 while (!NULL (tail)) | |
692 { | |
693 QUIT; | |
694 next = Fcdr (tail); | |
695 Fsetcdr (tail, prev); | |
696 prev = tail; | |
697 tail = next; | |
698 } | |
699 return prev; | |
700 } | |
701 | |
702 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0, | |
703 "Reverse LIST, copying. Returns the beginning of the reversed list.\n\ | |
704 See also the function `nreverse', which is used more often.") | |
705 (list) | |
706 Lisp_Object list; | |
707 { | |
708 Lisp_Object length; | |
709 register Lisp_Object *vec; | |
710 register Lisp_Object tail; | |
711 register int i; | |
712 | |
713 length = Flength (list); | |
714 vec = (Lisp_Object *) alloca (XINT (length) * sizeof (Lisp_Object)); | |
715 for (i = XINT (length) - 1, tail = list; i >= 0; i--, tail = Fcdr (tail)) | |
716 vec[i] = Fcar (tail); | |
717 | |
718 return Flist (XINT (length), vec); | |
719 } | |
720 | |
721 Lisp_Object merge (); | |
722 | |
723 DEFUN ("sort", Fsort, Ssort, 2, 2, 0, | |
724 "Sort LIST, stably, comparing elements using PREDICATE.\n\ | |
725 Returns the sorted list. LIST is modified by side effects.\n\ | |
726 PREDICATE is called with two elements of LIST, and should return T\n\ | |
727 if the first element is \"less\" than the second.") | |
728 (list, pred) | |
729 Lisp_Object list, pred; | |
730 { | |
731 Lisp_Object front, back; | |
732 register Lisp_Object len, tem; | |
733 struct gcpro gcpro1, gcpro2; | |
734 register int length; | |
735 | |
736 front = list; | |
737 len = Flength (list); | |
738 length = XINT (len); | |
739 if (length < 2) | |
740 return list; | |
741 | |
742 XSETINT (len, (length / 2) - 1); | |
743 tem = Fnthcdr (len, list); | |
744 back = Fcdr (tem); | |
745 Fsetcdr (tem, Qnil); | |
746 | |
747 GCPRO2 (front, back); | |
748 front = Fsort (front, pred); | |
749 back = Fsort (back, pred); | |
750 UNGCPRO; | |
751 return merge (front, back, pred); | |
752 } | |
753 | |
754 Lisp_Object | |
755 merge (org_l1, org_l2, pred) | |
756 Lisp_Object org_l1, org_l2; | |
757 Lisp_Object pred; | |
758 { | |
759 Lisp_Object value; | |
760 register Lisp_Object tail; | |
761 Lisp_Object tem; | |
762 register Lisp_Object l1, l2; | |
763 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
764 | |
765 l1 = org_l1; | |
766 l2 = org_l2; | |
767 tail = Qnil; | |
768 value = Qnil; | |
769 | |
770 /* It is sufficient to protect org_l1 and org_l2. | |
771 When l1 and l2 are updated, we copy the new values | |
772 back into the org_ vars. */ | |
773 GCPRO4 (org_l1, org_l2, pred, value); | |
774 | |
775 while (1) | |
776 { | |
777 if (NULL (l1)) | |
778 { | |
779 UNGCPRO; | |
780 if (NULL (tail)) | |
781 return l2; | |
782 Fsetcdr (tail, l2); | |
783 return value; | |
784 } | |
785 if (NULL (l2)) | |
786 { | |
787 UNGCPRO; | |
788 if (NULL (tail)) | |
789 return l1; | |
790 Fsetcdr (tail, l1); | |
791 return value; | |
792 } | |
793 tem = call2 (pred, Fcar (l2), Fcar (l1)); | |
794 if (NULL (tem)) | |
795 { | |
796 tem = l1; | |
797 l1 = Fcdr (l1); | |
798 org_l1 = l1; | |
799 } | |
800 else | |
801 { | |
802 tem = l2; | |
803 l2 = Fcdr (l2); | |
804 org_l2 = l2; | |
805 } | |
806 if (NULL (tail)) | |
807 value = tem; | |
808 else | |
809 Fsetcdr (tail, tem); | |
810 tail = tem; | |
811 } | |
812 } | |
813 | |
814 DEFUN ("get", Fget, Sget, 2, 2, 0, | |
815 "Return the value of SYMBOL's PROPNAME property.\n\ | |
816 This is the last VALUE stored with `(put SYMBOL PROPNAME VALUE)'.") | |
817 (sym, prop) | |
818 Lisp_Object sym; | |
819 register Lisp_Object prop; | |
820 { | |
821 register Lisp_Object tail; | |
822 for (tail = Fsymbol_plist (sym); !NULL (tail); tail = Fcdr (Fcdr (tail))) | |
823 { | |
824 register Lisp_Object tem; | |
825 tem = Fcar (tail); | |
826 if (EQ (prop, tem)) | |
827 return Fcar (Fcdr (tail)); | |
828 } | |
829 return Qnil; | |
830 } | |
831 | |
832 DEFUN ("put", Fput, Sput, 3, 3, 0, | |
833 "Store SYMBOL's PROPNAME property with value VALUE.\n\ | |
834 It can be retrieved with `(get SYMBOL PROPNAME)'.") | |
835 (sym, prop, val) | |
836 Lisp_Object sym; | |
837 register Lisp_Object prop; | |
838 Lisp_Object val; | |
839 { | |
840 register Lisp_Object tail, prev; | |
841 Lisp_Object newcell; | |
842 prev = Qnil; | |
843 for (tail = Fsymbol_plist (sym); !NULL (tail); tail = Fcdr (Fcdr (tail))) | |
844 { | |
845 register Lisp_Object tem; | |
846 tem = Fcar (tail); | |
847 if (EQ (prop, tem)) | |
848 return Fsetcar (Fcdr (tail), val); | |
849 prev = tail; | |
850 } | |
851 newcell = Fcons (prop, Fcons (val, Qnil)); | |
852 if (NULL (prev)) | |
853 Fsetplist (sym, newcell); | |
854 else | |
855 Fsetcdr (Fcdr (prev), newcell); | |
856 return val; | |
857 } | |
858 | |
859 DEFUN ("equal", Fequal, Sequal, 2, 2, 0, | |
860 "T if two Lisp objects have similar structure and contents.\n\ | |
861 They must have the same data type.\n\ | |
862 Conses are compared by comparing the cars and the cdrs.\n\ | |
863 Vectors and strings are compared element by element.\n\ | |
864 Numbers are compared by value. Symbols must match exactly.") | |
865 (o1, o2) | |
866 register Lisp_Object o1, o2; | |
867 { | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
868 return internal_equal (o1, o2, 0); |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
869 } |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
870 |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
871 static Lisp_Object |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
872 internal_equal (o1, o2, depth) |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
873 register Lisp_Object o1, o2; |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
874 int depth; |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
875 { |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
876 if (depth > 200) |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
877 error ("Stack overflow in equal"); |
211 | 878 do_cdr: |
879 QUIT; | |
880 if (XTYPE (o1) != XTYPE (o2)) return Qnil; | |
881 if (XINT (o1) == XINT (o2)) return Qt; | |
882 if (XTYPE (o1) == Lisp_Cons) | |
883 { | |
884 Lisp_Object v1; | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
885 v1 = Fequal (Fcar (o1), Fcar (o2), depth + 1); |
211 | 886 if (NULL (v1)) |
887 return v1; | |
888 o1 = Fcdr (o1), o2 = Fcdr (o2); | |
889 goto do_cdr; | |
890 } | |
891 if (XTYPE (o1) == Lisp_Marker) | |
892 { | |
893 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer | |
894 && XMARKER (o1)->bufpos == XMARKER (o2)->bufpos) | |
895 ? Qt : Qnil; | |
896 } | |
897 if (XTYPE (o1) == Lisp_Vector) | |
898 { | |
899 register int index; | |
900 if (XVECTOR (o1)->size != XVECTOR (o2)->size) | |
901 return Qnil; | |
902 for (index = 0; index < XVECTOR (o1)->size; index++) | |
903 { | |
904 Lisp_Object v, v1, v2; | |
905 v1 = XVECTOR (o1)->contents [index]; | |
906 v2 = XVECTOR (o2)->contents [index]; | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
907 v = Fequal (v1, v2, depth + 1); |
211 | 908 if (NULL (v)) return v; |
909 } | |
910 return Qt; | |
911 } | |
912 if (XTYPE (o1) == Lisp_String) | |
913 { | |
914 if (XSTRING (o1)->size != XSTRING (o2)->size) | |
915 return Qnil; | |
916 if (bcmp (XSTRING (o1)->data, XSTRING (o2)->data, XSTRING (o1)->size)) | |
917 return Qnil; | |
918 return Qt; | |
919 } | |
920 return Qnil; | |
921 } | |
922 | |
923 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0, | |
924 "Store each element of ARRAY with ITEM. ARRAY is a vector or string.") | |
925 (array, item) | |
926 Lisp_Object array, item; | |
927 { | |
928 register int size, index, charval; | |
929 retry: | |
930 if (XTYPE (array) == Lisp_Vector) | |
931 { | |
932 register Lisp_Object *p = XVECTOR (array)->contents; | |
933 size = XVECTOR (array)->size; | |
934 for (index = 0; index < size; index++) | |
935 p[index] = item; | |
936 } | |
937 else if (XTYPE (array) == Lisp_String) | |
938 { | |
939 register unsigned char *p = XSTRING (array)->data; | |
940 CHECK_NUMBER (item, 1); | |
941 charval = XINT (item); | |
942 size = XSTRING (array)->size; | |
943 for (index = 0; index < size; index++) | |
944 p[index] = charval; | |
945 } | |
946 else | |
947 { | |
948 array = wrong_type_argument (Qarrayp, array); | |
949 goto retry; | |
950 } | |
951 return array; | |
952 } | |
953 | |
954 /* ARGSUSED */ | |
955 Lisp_Object | |
956 nconc2 (s1, s2) | |
957 Lisp_Object s1, s2; | |
958 { | |
959 #ifdef NO_ARG_ARRAY | |
960 Lisp_Object args[2]; | |
961 args[0] = s1; | |
962 args[1] = s2; | |
963 return Fnconc (2, args); | |
964 #else | |
965 return Fnconc (2, &s1); | |
966 #endif /* NO_ARG_ARRAY */ | |
967 } | |
968 | |
969 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0, | |
970 "Concatenate any number of lists by altering them.\n\ | |
971 Only the last argument is not altered, and need not be a list.") | |
972 (nargs, args) | |
973 int nargs; | |
974 Lisp_Object *args; | |
975 { | |
976 register int argnum; | |
977 register Lisp_Object tail, tem, val; | |
978 | |
979 val = Qnil; | |
980 | |
981 for (argnum = 0; argnum < nargs; argnum++) | |
982 { | |
983 tem = args[argnum]; | |
984 if (NULL (tem)) continue; | |
985 | |
986 if (NULL (val)) | |
987 val = tem; | |
988 | |
989 if (argnum + 1 == nargs) break; | |
990 | |
991 if (!CONSP (tem)) | |
992 tem = wrong_type_argument (Qlistp, tem); | |
993 | |
994 while (CONSP (tem)) | |
995 { | |
996 tail = tem; | |
997 tem = Fcdr (tail); | |
998 QUIT; | |
999 } | |
1000 | |
1001 tem = args[argnum + 1]; | |
1002 Fsetcdr (tail, tem); | |
1003 if (NULL (tem)) | |
1004 args[argnum + 1] = tail; | |
1005 } | |
1006 | |
1007 return val; | |
1008 } | |
1009 | |
1010 /* This is the guts of all mapping functions. | |
1011 Apply fn to each element of seq, one by one, | |
1012 storing the results into elements of vals, a C vector of Lisp_Objects. | |
1013 leni is the length of vals, which should also be the length of seq. */ | |
1014 | |
1015 static void | |
1016 mapcar1 (leni, vals, fn, seq) | |
1017 int leni; | |
1018 Lisp_Object *vals; | |
1019 Lisp_Object fn, seq; | |
1020 { | |
1021 register Lisp_Object tail; | |
1022 Lisp_Object dummy; | |
1023 register int i; | |
1024 struct gcpro gcpro1, gcpro2, gcpro3; | |
1025 | |
1026 /* Don't let vals contain any garbage when GC happens. */ | |
1027 for (i = 0; i < leni; i++) | |
1028 vals[i] = Qnil; | |
1029 | |
1030 GCPRO3 (dummy, fn, seq); | |
1031 gcpro1.var = vals; | |
1032 gcpro1.nvars = leni; | |
1033 /* We need not explicitly protect `tail' because it is used only on lists, and | |
1034 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */ | |
1035 | |
1036 if (XTYPE (seq) == Lisp_Vector) | |
1037 { | |
1038 for (i = 0; i < leni; i++) | |
1039 { | |
1040 dummy = XVECTOR (seq)->contents[i]; | |
1041 vals[i] = call1 (fn, dummy); | |
1042 } | |
1043 } | |
1044 else if (XTYPE (seq) == Lisp_String) | |
1045 { | |
1046 for (i = 0; i < leni; i++) | |
1047 { | |
1048 XFASTINT (dummy) = XSTRING (seq)->data[i]; | |
1049 vals[i] = call1 (fn, dummy); | |
1050 } | |
1051 } | |
1052 else /* Must be a list, since Flength did not get an error */ | |
1053 { | |
1054 tail = seq; | |
1055 for (i = 0; i < leni; i++) | |
1056 { | |
1057 vals[i] = call1 (fn, Fcar (tail)); | |
1058 tail = Fcdr (tail); | |
1059 } | |
1060 } | |
1061 | |
1062 UNGCPRO; | |
1063 } | |
1064 | |
1065 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0, | |
1066 "Apply FN to each element of SEQ, and concat the results as strings.\n\ | |
1067 In between each pair of results, stick in SEP.\n\ | |
1068 Thus, \" \" as SEP results in spaces between the values return by FN.") | |
1069 (fn, seq, sep) | |
1070 Lisp_Object fn, seq, sep; | |
1071 { | |
1072 Lisp_Object len; | |
1073 register int leni; | |
1074 int nargs; | |
1075 register Lisp_Object *args; | |
1076 register int i; | |
1077 struct gcpro gcpro1; | |
1078 | |
1079 len = Flength (seq); | |
1080 leni = XINT (len); | |
1081 nargs = leni + leni - 1; | |
1082 if (nargs < 0) return build_string (""); | |
1083 | |
1084 args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object)); | |
1085 | |
1086 GCPRO1 (sep); | |
1087 mapcar1 (leni, args, fn, seq); | |
1088 UNGCPRO; | |
1089 | |
1090 for (i = leni - 1; i >= 0; i--) | |
1091 args[i + i] = args[i]; | |
1092 | |
1093 for (i = 1; i < nargs; i += 2) | |
1094 args[i] = sep; | |
1095 | |
1096 return Fconcat (nargs, args); | |
1097 } | |
1098 | |
1099 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0, | |
1100 "Apply FUNCTION to each element of SEQUENCE, and make a list of the results.\n\ | |
1101 The result is a list just as long as SEQUENCE.\n\ | |
1102 SEQUENCE may be a list, a vector or a string.") | |
1103 (fn, seq) | |
1104 Lisp_Object fn, seq; | |
1105 { | |
1106 register Lisp_Object len; | |
1107 register int leni; | |
1108 register Lisp_Object *args; | |
1109 | |
1110 len = Flength (seq); | |
1111 leni = XFASTINT (len); | |
1112 args = (Lisp_Object *) alloca (leni * sizeof (Lisp_Object)); | |
1113 | |
1114 mapcar1 (leni, args, fn, seq); | |
1115 | |
1116 return Flist (leni, args); | |
1117 } | |
1118 | |
1119 /* Anything that calls this function must protect from GC! */ | |
1120 | |
1121 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0, | |
1122 "Ask user a \"y or n\" question. Return t if answer is \"y\".\n\ | |
1123 No confirmation of the answer is requested; a single character is enough.\n\ | |
1124 Also accepts Space to mean yes, or Delete to mean no.") | |
1125 (prompt) | |
1126 Lisp_Object prompt; | |
1127 { | |
1128 register Lisp_Object obj; | |
1129 register int ans; | |
1130 Lisp_Object xprompt; | |
1131 Lisp_Object args[2]; | |
1132 int ocech = cursor_in_echo_area; | |
1133 struct gcpro gcpro1, gcpro2; | |
1134 | |
1135 CHECK_STRING (prompt, 0); | |
1136 xprompt = prompt; | |
1137 GCPRO2 (prompt, xprompt); | |
1138 | |
1139 while (1) | |
1140 { | |
1141 message ("%s(y or n) ", XSTRING (xprompt)->data); | |
1142 cursor_in_echo_area = 1; | |
1143 | |
1144 obj = read_char (0); | |
1145 if (XTYPE (obj) == Lisp_Int) | |
1146 ans = XINT (obj); | |
1147 else | |
1148 continue; | |
1149 | |
1150 cursor_in_echo_area = -1; | |
1151 message ("%s(y or n) %c", XSTRING (xprompt)->data, ans); | |
1152 cursor_in_echo_area = ocech; | |
1153 /* Accept a C-g or C-] (abort-recursive-edit) as quit requests. */ | |
1154 if (ans == 7 || ans == '\035') | |
1155 Vquit_flag = Qt; | |
1156 QUIT; | |
1157 if (ans >= 0) | |
1158 ans = DOWNCASE (ans); | |
1159 if (ans == 'y' || ans == ' ') | |
1160 { ans = 'y'; break; } | |
1161 if (ans == 'n' || ans == 127) | |
1162 break; | |
1163 | |
1164 Fding (Qnil); | |
1165 Fdiscard_input (); | |
1166 if (EQ (xprompt, prompt)) | |
1167 { | |
1168 args[0] = build_string ("Please answer y or n. "); | |
1169 args[1] = prompt; | |
1170 xprompt = Fconcat (2, args); | |
1171 } | |
1172 } | |
1173 UNGCPRO; | |
1174 return (ans == 'y' ? Qt : Qnil); | |
1175 } | |
1176 | |
1177 /* This is how C code calls `yes-or-no-p' and allows the user | |
1178 to redefined it. | |
1179 | |
1180 Anything that calls this function must protect from GC! */ | |
1181 | |
1182 Lisp_Object | |
1183 do_yes_or_no_p (prompt) | |
1184 Lisp_Object prompt; | |
1185 { | |
1186 return call1 (intern ("yes-or-no-p"), prompt); | |
1187 } | |
1188 | |
1189 /* Anything that calls this function must protect from GC! */ | |
1190 | |
1191 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0, | |
1192 "Ask user a yes or no question. Return t if answer is yes.\n\ | |
1193 The user must confirm the answer with a newline,\n\ | |
1194 and can rub it out if not confirmed.") | |
1195 (prompt) | |
1196 Lisp_Object prompt; | |
1197 { | |
1198 register Lisp_Object ans; | |
1199 Lisp_Object args[2]; | |
1200 struct gcpro gcpro1; | |
1201 | |
1202 CHECK_STRING (prompt, 0); | |
1203 | |
1204 args[0] = prompt; | |
1205 args[1] = build_string ("(yes or no) "); | |
1206 prompt = Fconcat (2, args); | |
1207 | |
1208 GCPRO1 (prompt); | |
1209 while (1) | |
1210 { | |
1211 ans = Fdowncase (read_minibuf (Vminibuffer_local_map, | |
1212 Qnil, prompt, Qnil, 0)); | |
1213 if (XSTRING (ans)->size == 3 && !strcmp (XSTRING (ans)->data, "yes")) | |
1214 { | |
1215 UNGCPRO; | |
1216 return Qt; | |
1217 } | |
1218 if (XSTRING (ans)->size == 2 && !strcmp (XSTRING (ans)->data, "no")) | |
1219 { | |
1220 UNGCPRO; | |
1221 return Qnil; | |
1222 } | |
1223 | |
1224 Fding (Qnil); | |
1225 Fdiscard_input (); | |
1226 message ("Please answer yes or no."); | |
1227 Fsleep_for (make_number (2)); | |
1228 } | |
1229 UNGCPRO; | |
1230 } | |
1231 | |
1232 /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
350 | 1233 #ifdef DGUX |
1234 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */ | |
1235 | |
1236 #else /* Not DGUX */ | |
1237 | |
211 | 1238 static int ldav_initialized; |
1239 static int ldav_channel; | |
1240 #ifdef LOAD_AVE_TYPE | |
1241 #ifndef VMS | |
1242 static struct nlist ldav_nl[2]; | |
1243 #endif /* VMS */ | |
1244 #endif /* LOAD_AVE_TYPE */ | |
1245 | |
1246 #define channel ldav_channel | |
1247 #define initialized ldav_initialized | |
1248 #define nl ldav_nl | |
350 | 1249 #endif /* Not DGUX */ |
211 | 1250 |
1251 DEFUN ("load-average", Fload_average, Sload_average, 0, 0, 0, | |
1252 "Return list of 1 minute, 5 minute and 15 minute load averages.\n\ | |
1253 Each of the three load averages is multiplied by 100,\n\ | |
1254 then converted to integer.") | |
1255 () | |
1256 { | |
350 | 1257 #ifdef DGUX |
1258 /* perhaps there should be a "sys_load_avg" call in sysdep.c?! - DJB */ | |
1259 load_info.one_minute = 0.0; /* just in case there is an error */ | |
1260 load_info.five_minute = 0.0; | |
1261 load_info.fifteen_minute = 0.0; | |
1262 dg_sys_info (&load_info, DG_SYS_INFO_LOAD_INFO_TYPE, | |
1263 DG_SYS_INFO_LOAD_VERSION_0); | |
1264 | |
1265 return Fcons (make_number ((int)(load_info.one_minute * 100.0)), | |
1266 Fcons (make_number ((int)(load_info.five_minute * 100.0)), | |
1267 Fcons (make_number ((int)(load_info.fifteen_minute * 100.0)), | |
1268 Qnil))); | |
1269 #else /* not DGUX */ | |
211 | 1270 #ifndef LOAD_AVE_TYPE |
1271 error ("load-average not implemented for this operating system"); | |
1272 | |
1273 #else /* LOAD_AVE_TYPE defined */ | |
1274 | |
1275 LOAD_AVE_TYPE load_ave[3]; | |
1276 #ifdef VMS | |
1277 #ifndef eunice | |
1278 #include <iodef.h> | |
1279 #include <descrip.h> | |
1280 #else | |
1281 #include <vms/iodef.h> | |
1282 struct {int dsc$w_length; char *dsc$a_pointer;} descriptor; | |
1283 #endif /* eunice */ | |
1284 #endif /* VMS */ | |
1285 | |
1286 /* If this fails for any reason, we can return (0 0 0) */ | |
1287 load_ave[0] = 0.0; load_ave[1] = 0.0; load_ave[2] = 0.0; | |
1288 | |
1289 #ifdef VMS | |
1290 /* | |
1291 * VMS specific code -- read from the Load Ave driver | |
1292 */ | |
1293 | |
1294 /* | |
1295 * Ensure that there is a channel open to the load ave device | |
1296 */ | |
1297 if (initialized == 0) | |
1298 { | |
1299 /* Attempt to open the channel */ | |
1300 #ifdef eunice | |
1301 descriptor.size = 18; | |
1302 descriptor.ptr = "$$VMS_LOAD_AVERAGE"; | |
1303 #else | |
1304 $DESCRIPTOR(descriptor, "LAV0:"); | |
1305 #endif | |
1306 if (sys$assign (&descriptor, &channel, 0, 0) & 1) | |
1307 initialized = 1; | |
1308 } | |
1309 /* | |
1310 * Read the load average vector | |
1311 */ | |
1312 if (initialized) | |
1313 { | |
1314 if (!(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0, | |
1315 load_ave, 12, 0, 0, 0, 0) | |
1316 & 1)) | |
1317 { | |
1318 sys$dassgn (channel); | |
1319 initialized = 0; | |
1320 } | |
1321 } | |
1322 #else /* not VMS */ | |
1323 /* | |
1324 * 4.2BSD UNIX-specific code -- read _avenrun from /dev/kmem | |
1325 */ | |
1326 | |
1327 /* | |
1328 * Make sure we have the address of _avenrun | |
1329 */ | |
1330 if (nl[0].n_value == 0) | |
1331 { | |
1332 /* | |
1333 * Get the address of _avenrun | |
1334 */ | |
1335 #ifndef NLIST_STRUCT | |
1336 strcpy (nl[0].n_name, LDAV_SYMBOL); | |
1337 nl[1].n_zeroes = 0; | |
1338 #else /* NLIST_STRUCT */ | |
1339 #ifdef convex | |
1340 nl[0].n_un.n_name = LDAV_SYMBOL; | |
1341 nl[1].n_un.n_name = 0; | |
1342 #else /* not convex */ | |
1343 nl[0].n_name = LDAV_SYMBOL; | |
1344 nl[1].n_name = 0; | |
1345 #endif /* not convex */ | |
1346 #endif /* NLIST_STRUCT */ | |
1347 | |
1348 nlist (KERNEL_FILE, nl); | |
1349 | |
1350 #ifdef FIXUP_KERNEL_SYMBOL_ADDR | |
1351 FIXUP_KERNEL_SYMBOL_ADDR (nl); | |
1352 #endif /* FIXUP_KERNEL_SYMBOL_ADDR */ | |
1353 } | |
1354 /* | |
1355 * Make sure we have /dev/kmem open | |
1356 */ | |
1357 if (initialized == 0) | |
1358 { | |
1359 /* | |
1360 * Open /dev/kmem | |
1361 */ | |
1362 channel = open ("/dev/kmem", 0); | |
1363 if (channel >= 0) initialized = 1; | |
1364 } | |
1365 /* | |
1366 * If we can, get the load ave values | |
1367 */ | |
1368 if ((nl[0].n_value != 0) && (initialized != 0)) | |
1369 { | |
1370 /* | |
1371 * Seek to the correct address | |
1372 */ | |
1373 lseek (channel, (long) nl[0].n_value, 0); | |
1374 if (read (channel, load_ave, sizeof load_ave) | |
1375 != sizeof(load_ave)) | |
1376 { | |
1377 close (channel); | |
1378 initialized = 0; | |
1379 } | |
1380 } | |
1381 #endif /* not VMS */ | |
1382 | |
1383 /* | |
1384 * Return the list of load average values | |
1385 */ | |
1386 return Fcons (make_number (LOAD_AVE_CVT (load_ave[0])), | |
1387 Fcons (make_number (LOAD_AVE_CVT (load_ave[1])), | |
1388 Fcons (make_number (LOAD_AVE_CVT (load_ave[2])), | |
1389 Qnil))); | |
1390 #endif /* LOAD_AVE_TYPE */ | |
350 | 1391 #endif /* not DGUX */ |
211 | 1392 } |
1393 | |
1394 #undef channel | |
1395 #undef initialized | |
1396 #undef nl | |
1397 | |
1398 Lisp_Object Vfeatures; | |
1399 | |
1400 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 1, 0, | |
1401 "Returns t if FEATURE is present in this Emacs.\n\ | |
1402 Use this to conditionalize execution of lisp code based on the presence or\n\ | |
1403 absence of emacs or environment extensions.\n\ | |
1404 Use `provide' to declare that a feature is available.\n\ | |
1405 This function looks at the value of the variable `features'.") | |
1406 (feature) | |
1407 Lisp_Object feature; | |
1408 { | |
1409 register Lisp_Object tem; | |
1410 CHECK_SYMBOL (feature, 0); | |
1411 tem = Fmemq (feature, Vfeatures); | |
1412 return (NULL (tem)) ? Qnil : Qt; | |
1413 } | |
1414 | |
1415 DEFUN ("provide", Fprovide, Sprovide, 1, 1, 0, | |
1416 "Announce that FEATURE is a feature of the current Emacs.") | |
1417 (feature) | |
1418 Lisp_Object feature; | |
1419 { | |
1420 register Lisp_Object tem; | |
1421 CHECK_SYMBOL (feature, 0); | |
1422 if (!NULL (Vautoload_queue)) | |
1423 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue); | |
1424 tem = Fmemq (feature, Vfeatures); | |
1425 if (NULL (tem)) | |
1426 Vfeatures = Fcons (feature, Vfeatures); | |
1427 return feature; | |
1428 } | |
1429 | |
1430 DEFUN ("require", Frequire, Srequire, 1, 2, 0, | |
1431 "If feature FEATURE is not loaded, load it from FILENAME.\n\ | |
1432 If FEATURE is not a member of the list `features', then the feature\n\ | |
1433 is not loaded; so load the file FILENAME.\n\ | |
1434 If FILENAME is omitted, the printname of FEATURE is used as the file name.") | |
1435 (feature, file_name) | |
1436 Lisp_Object feature, file_name; | |
1437 { | |
1438 register Lisp_Object tem; | |
1439 CHECK_SYMBOL (feature, 0); | |
1440 tem = Fmemq (feature, Vfeatures); | |
1441 if (NULL (tem)) | |
1442 { | |
1443 int count = specpdl_ptr - specpdl; | |
1444 | |
1445 /* Value saved here is to be restored into Vautoload_queue */ | |
1446 record_unwind_protect (un_autoload, Vautoload_queue); | |
1447 Vautoload_queue = Qt; | |
1448 | |
1449 Fload (NULL (file_name) ? Fsymbol_name (feature) : file_name, | |
1450 Qnil, Qt, Qnil); | |
1451 | |
1452 tem = Fmemq (feature, Vfeatures); | |
1453 if (NULL (tem)) | |
1454 error ("Required feature %s was not provided", | |
1455 XSYMBOL (feature)->name->data ); | |
1456 | |
1457 /* Once loading finishes, don't undo it. */ | |
1458 Vautoload_queue = Qt; | |
1459 feature = unbind_to (count, feature); | |
1460 } | |
1461 return feature; | |
1462 } | |
1463 | |
1464 syms_of_fns () | |
1465 { | |
1466 Qstring_lessp = intern ("string-lessp"); | |
1467 staticpro (&Qstring_lessp); | |
1468 | |
1469 DEFVAR_LISP ("features", &Vfeatures, | |
1470 "A list of symbols which are the features of the executing emacs.\n\ | |
1471 Used by `featurep' and `require', and altered by `provide'."); | |
1472 Vfeatures = Qnil; | |
1473 | |
1474 defsubr (&Sidentity); | |
1475 defsubr (&Srandom); | |
1476 defsubr (&Slength); | |
1477 defsubr (&Sstring_equal); | |
1478 defsubr (&Sstring_lessp); | |
1479 defsubr (&Sappend); | |
1480 defsubr (&Sconcat); | |
1481 defsubr (&Svconcat); | |
1482 defsubr (&Scopy_sequence); | |
1483 defsubr (&Scopy_alist); | |
1484 defsubr (&Ssubstring); | |
1485 defsubr (&Snthcdr); | |
1486 defsubr (&Snth); | |
1487 defsubr (&Selt); | |
1488 defsubr (&Smember); | |
1489 defsubr (&Smemq); | |
1490 defsubr (&Sassq); | |
1491 defsubr (&Sassoc); | |
1492 defsubr (&Srassq); | |
1493 defsubr (&Sdelq); | |
414 | 1494 defsubr (&Sdelete); |
211 | 1495 defsubr (&Snreverse); |
1496 defsubr (&Sreverse); | |
1497 defsubr (&Ssort); | |
1498 defsubr (&Sget); | |
1499 defsubr (&Sput); | |
1500 defsubr (&Sequal); | |
1501 defsubr (&Sfillarray); | |
1502 defsubr (&Snconc); | |
1503 defsubr (&Smapcar); | |
1504 defsubr (&Smapconcat); | |
1505 defsubr (&Sy_or_n_p); | |
1506 defsubr (&Syes_or_no_p); | |
1507 defsubr (&Sload_average); | |
1508 defsubr (&Sfeaturep); | |
1509 defsubr (&Srequire); | |
1510 defsubr (&Sprovide); | |
1511 } |