Mercurial > hg > xemacs-beta
comparison lisp/cl.el @ 2153:393039450288
[xemacs-hg @ 2004-06-26 21:25:23 by james]
Synch with Emacs 21.3.
author | james |
---|---|
date | Sat, 26 Jun 2004 21:25:24 +0000 |
parents | 9d6ec778e1e8 |
children | ecf1ebac70d8 |
comparison
equal
deleted
inserted
replaced
2152:d93fedcbf6be | 2153:393039450288 |
---|---|
22 ;; You should have received a copy of the GNU General Public License | 22 ;; You should have received a copy of the GNU General Public License |
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free | 23 ;; along with XEmacs; see the file COPYING. If not, write to the Free |
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | 24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
25 ;; 02111-1307, USA. | 25 ;; 02111-1307, USA. |
26 | 26 |
27 ;;; Synched up with: FSF 19.34. | 27 ;;; Synched up with: FSF 21.3. |
28 | 28 |
29 ;;; Commentary: | 29 ;;; Commentary: |
30 | 30 |
31 ;; This file is dumped with XEmacs. | 31 ;; This file is dumped with XEmacs. |
32 | 32 |
34 ;; Common Lisp compatibility, beyond what is already built-in | 34 ;; Common Lisp compatibility, beyond what is already built-in |
35 ;; in Emacs Lisp. | 35 ;; in Emacs Lisp. |
36 ;; | 36 ;; |
37 ;; This package was written by Dave Gillespie; it is a complete | 37 ;; This package was written by Dave Gillespie; it is a complete |
38 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | 38 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. |
39 ;; | |
40 ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19. | |
41 ;; | 39 ;; |
42 ;; Bug reports, comments, and suggestions are welcome! | 40 ;; Bug reports, comments, and suggestions are welcome! |
43 | 41 |
44 ;; This file contains the portions of the Common Lisp extensions | 42 ;; This file contains the portions of the Common Lisp extensions |
45 ;; package which should always be present. | 43 ;; package which should always be present. |
105 (string-lessp emacs-version "19")) 18) | 103 (string-lessp emacs-version "19")) 18) |
106 ((string-match "XEmacs" emacs-version) | 104 ((string-match "XEmacs" emacs-version) |
107 'lucid) | 105 'lucid) |
108 (t 19))) | 106 (t 19))) |
109 | 107 |
110 (or (fboundp 'defalias) (fset 'defalias 'fset)) | |
111 | |
112 (defvar cl-optimize-speed 1) | 108 (defvar cl-optimize-speed 1) |
113 (defvar cl-optimize-safety 1) | 109 (defvar cl-optimize-safety 1) |
114 | |
115 | |
116 ;;; Keywords used in this package. | |
117 | |
118 ;;; XEmacs - keywords are done in Fintern(). | |
119 ;;; | |
120 ;;; (defconst :test ':test) | |
121 ;;; (defconst :test-not ':test-not) | |
122 ;;; (defconst :key ':key) | |
123 ;;; (defconst :start ':start) | |
124 ;;; (defconst :start1 ':start1) | |
125 ;;; (defconst :start2 ':start2) | |
126 ;;; (defconst :end ':end) | |
127 ;;; (defconst :end1 ':end1) | |
128 ;;; (defconst :end2 ':end2) | |
129 ;;; (defconst :count ':count) | |
130 ;;; (defconst :initial-value ':initial-value) | |
131 ;;; (defconst :size ':size) | |
132 ;;; (defconst :from-end ':from-end) | |
133 ;;; (defconst :rehash-size ':rehash-size) | |
134 ;;; (defconst :rehash-threshold ':rehash-threshold) | |
135 ;;; (defconst :allow-other-keys ':allow-other-keys) | |
136 | 110 |
137 | 111 |
138 (defvar custom-print-functions nil | 112 (defvar custom-print-functions nil |
139 "This is a list of functions that format user objects for printing. | 113 "This is a list of functions that format user objects for printing. |
140 Each function is called in turn with three arguments: the object, the | 114 Each function is called in turn with three arguments: the object, the |
225 | 199 |
226 ;; The macros `when' and `unless' are so useful that we want them to | 200 ;; The macros `when' and `unless' are so useful that we want them to |
227 ;; ALWAYS be available. So they've been moved from cl.el to eval.c. | 201 ;; ALWAYS be available. So they've been moved from cl.el to eval.c. |
228 ;; Note: FSF Emacs moved them to subr.el in FSF 20. | 202 ;; Note: FSF Emacs moved them to subr.el in FSF 20. |
229 | 203 |
230 (defun cl-map-extents (&rest cl-args) | 204 (defalias 'cl-map-extents 'map-extents) |
231 ;; XEmacs: This used to check for overlays first, but that's wrong | |
232 ;; because of the new compatibility library. *duh* | |
233 (cond ((fboundp 'map-extents) | |
234 (apply 'map-extents cl-args)) | |
235 ((fboundp 'next-overlay-at) | |
236 (apply 'cl-map-overlays cl-args)))) | |
237 | 205 |
238 | 206 |
239 ;;; Blocks and exits. | 207 ;;; Blocks and exits. |
240 | 208 |
241 (defalias 'cl-block-wrapper 'identity) | 209 (defalias 'cl-block-wrapper 'identity) |
244 | 212 |
245 ;;; Multiple values. True multiple values are not supported, or even | 213 ;;; Multiple values. True multiple values are not supported, or even |
246 ;;; simulated. Instead, multiple-value-bind and friends simply expect | 214 ;;; simulated. Instead, multiple-value-bind and friends simply expect |
247 ;;; the target form to return the values as a list. | 215 ;;; the target form to return the values as a list. |
248 | 216 |
249 (defalias 'values 'list) | 217 (defsubst values (&rest values) |
250 (defalias 'values-list 'identity) | 218 "Return multiple values, Common Lisp style. |
251 (defalias 'multiple-value-list 'identity) | 219 The arguments of `values' are the values |
220 that the containing function should return." | |
221 values) | |
222 | |
223 (defsubst values-list (list) | |
224 "Return multiple values, Common Lisp style, taken from a list. | |
225 LIST specifies the list of values | |
226 that the containing function should return." | |
227 list) | |
228 | |
229 (defsubst multiple-value-list (expression) | |
230 "Return a list of the multiple values produced by EXPRESSION. | |
231 This handles multiple values in Common Lisp style, but it does not | |
232 work right when EXPRESSION calls an ordinary Emacs Lisp function | |
233 that returns just one value." | |
234 expression) | |
235 | |
236 (defsubst multiple-value-apply (function expression) | |
237 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them. | |
238 This handles multiple values in Common Lisp style, but it does not work | |
239 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just | |
240 one value." | |
241 (apply function expression)) | |
242 | |
252 (defalias 'multiple-value-call 'apply) ; only works for one arg | 243 (defalias 'multiple-value-call 'apply) ; only works for one arg |
253 (defalias 'nth-value 'nth) | 244 |
254 | 245 (defsubst nth-value (n expression) |
246 "Evaluate EXPRESSION to get multiple values and return the Nth one. | |
247 This handles multiple values in Common Lisp style, but it does not work | |
248 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just | |
249 one value." | |
250 (nth n expression)) | |
255 | 251 |
256 ;;; Macros. | 252 ;;; Macros. |
257 | 253 |
258 (defvar cl-macro-environment nil) | 254 (defvar cl-macro-environment nil) |
259 ;; XEmacs: we renamed the internal function to macroexpand-internal | 255 ;; XEmacs: we renamed the internal function to macroexpand-internal |
315 (coerce-number v 'fixnum) | 311 (coerce-number v 'fixnum) |
316 v))) | 312 v))) |
317 | 313 |
318 (defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100)) | 314 (defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100)) |
319 | 315 |
316 ;; XEmacs change: gensym and gentemp moved here from cl-macs.el | |
320 (defun gensym (&optional arg) | 317 (defun gensym (&optional arg) |
321 "Generate a new uninterned symbol. | 318 "Generate a new uninterned symbol. |
322 The name is made by appending a number to a prefix. If ARG is a string, it | 319 The name is made by appending a number to a prefix. If ARG is a string, it |
323 is the prefix, otherwise the prefix defaults to \"G\". If ARG is an integer, | 320 is the prefix, otherwise the prefix defaults to \"G\". If ARG is an integer, |
324 the internal counter is reset to that number before creating the name. | 321 the internal counter is reset to that number before creating the name. |
339 (setq *gensym-counter* (1+ *gensym-counter*))) | 336 (setq *gensym-counter* (1+ *gensym-counter*))) |
340 (intern name))) | 337 (intern name))) |
341 | 338 |
342 ;;; Numbers. | 339 ;;; Numbers. |
343 | 340 |
341 ;; XEmacs change: use floatp, which is right even in the presence of ratios | |
342 ;; and bigfloats | |
344 (defun floatp-safe (object) | 343 (defun floatp-safe (object) |
345 "Return t if OBJECT is a floating point number." | 344 "Return t if OBJECT is a floating point number." |
346 (floatp object)) | 345 (floatp object)) |
347 | 346 |
348 (defun plusp (number) | 347 (defun plusp (number) |
359 | 358 |
360 (defun evenp (integer) | 359 (defun evenp (integer) |
361 "Return t if INTEGER is even." | 360 "Return t if INTEGER is even." |
362 (eq (logand integer 1) 0)) | 361 (eq (logand integer 1) 0)) |
363 | 362 |
364 (defun cl-abs (number) | 363 ;; XEmacs addition |
365 "Return the absolute value of NUMBER." | 364 (defalias 'cl-abs 'abs) |
366 (if (>= number 0) number (- number))) | |
367 (or (fboundp 'abs) (defalias 'abs 'cl-abs)) ; This is built-in to Emacs 19 | |
368 | 365 |
369 (defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time))) | 366 (defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time))) |
370 | 367 |
371 ;; These constants are defined in C when 'number-types is provided. | 368 ;; XEmacs: These constants are defined in C when 'number-types is provided. |
369 ;; They are always defined in C on Emacs. Maybe we should, too. | |
372 (unless (featurep 'number-types) | 370 (unless (featurep 'number-types) |
373 ;;; We use `eval' in case VALBITS differs from compile-time to load-time. | 371 ;;; We use `eval' in case VALBITS differs from compile-time to load-time. |
374 (defconst most-positive-fixnum (eval '(lsh -1 -1)) | 372 (defconst most-positive-fixnum (eval '(lsh -1 -1)) |
375 "The integer closest in value to positive infinity.") | 373 "The integer closest in value to positive infinity.") |
376 (defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1))) | 374 (defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1))) |
408 (while (and cl-x cl-y) | 406 (while (and cl-x cl-y) |
409 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res)) | 407 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res)) |
410 (nreverse cl-res))) | 408 (nreverse cl-res))) |
411 (mapcar cl-func cl-x))) | 409 (mapcar cl-func cl-x))) |
412 | 410 |
411 (defalias 'svref 'aref) | |
413 | 412 |
414 ;;; List functions. | 413 ;;; List functions. |
415 | 414 |
416 ;; These functions are made known to the byte-compiler by cl-macs.el | 415 ;; These functions are made known to the byte-compiler by cl-macs.el |
417 ;; and turned into efficient car and cdr bytecodes. | 416 ;; and turned into efficient car and cdr bytecodes. |
418 | 417 |
419 (defalias 'first 'car) | 418 (defalias 'first 'car) |
420 (defalias 'rest 'cdr) | 419 (defalias 'rest 'cdr) |
421 (defalias 'endp 'null) | 420 (defalias 'endp 'null) |
422 | 421 |
422 ;; XEmacs change: make it a real function | |
423 (defun second (x) | 423 (defun second (x) |
424 "Return the second element of the list LIST." | 424 "Return the second element of the list LIST." |
425 (car (cdr x))) | 425 (car (cdr x))) |
426 | 426 |
427 (defun third (x) | 427 (defun third (x) |
428 "Return the third element of the list LIST." | 428 "Return the third element of the list X." |
429 (car (cdr (cdr x)))) | 429 (car (cdr (cdr x)))) |
430 | 430 |
431 (defun fourth (x) | 431 (defun fourth (x) |
432 "Return the fourth element of the list LIST." | 432 "Return the fourth element of the list X." |
433 (nth 3 x)) | 433 (nth 3 x)) |
434 | 434 |
435 (defun fifth (x) | 435 (defun fifth (x) |
436 "Return the fifth element of the list LIST." | 436 "Return the fifth element of the list X." |
437 (nth 4 x)) | 437 (nth 4 x)) |
438 | 438 |
439 (defun sixth (x) | 439 (defun sixth (x) |
440 "Return the sixth element of the list LIST." | 440 "Return the sixth element of the list X." |
441 (nth 5 x)) | 441 (nth 5 x)) |
442 | 442 |
443 (defun seventh (x) | 443 (defun seventh (x) |
444 "Return the seventh element of the list LIST." | 444 "Return the seventh element of the list X." |
445 (nth 6 x)) | 445 (nth 6 x)) |
446 | 446 |
447 (defun eighth (x) | 447 (defun eighth (x) |
448 "Return the eighth element of the list LIST." | 448 "Return the eighth element of the list X." |
449 (nth 7 x)) | 449 (nth 7 x)) |
450 | 450 |
451 (defun ninth (x) | 451 (defun ninth (x) |
452 "Return the ninth element of the list LIST." | 452 "Return the ninth element of the list X." |
453 (nth 8 x)) | 453 (nth 8 x)) |
454 | 454 |
455 (defun tenth (x) | 455 (defun tenth (x) |
456 "Return the tenth element of the list LIST." | 456 "Return the tenth element of the list X." |
457 (nth 9 x)) | 457 (nth 9 x)) |
458 | 458 |
459 ;; XEmacs change: Emacs defines caar, cadr, cdar, and cddr in subr.el. | |
459 (defun caar (x) | 460 (defun caar (x) |
460 "Return the `car' of the `car' of X." | 461 "Return the `car' of the `car' of X." |
461 (car (car x))) | 462 (car (car x))) |
462 | 463 |
463 (defun cadr (x) | 464 (defun cadr (x) |
567 (defun cddddr (x) | 568 (defun cddddr (x) |
568 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X." | 569 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X." |
569 (cdr (cdr (cdr (cdr x))))) | 570 (cdr (cdr (cdr (cdr x))))) |
570 | 571 |
571 ;;; `last' is implemented as a C primitive, as of 1998-11 | 572 ;;; `last' is implemented as a C primitive, as of 1998-11 |
572 | 573 ;;(defun last* (x &optional n) |
573 ;(defun last (x &optional n) | 574 ;; "Returns the last link in the list LIST. |
574 ; "Return the last link in the list LIST. | 575 ;;With optional argument N, returns Nth-to-last link (default 1)." |
575 ;With optional argument N, return Nth-to-last link (default 1)." | 576 ;; (if n |
576 ; (if n | 577 ;; (let ((m 0) (p x)) |
577 ; (let ((m 0) (p x)) | 578 ;; (while (consp p) (incf m) (pop p)) |
578 ; (while (consp p) (incf m) (pop p)) | 579 ;; (if (<= n 0) p |
579 ; (if (<= n 0) p | 580 ;; (if (< n m) (nthcdr (- m n) x) x))) |
580 ; (if (< n m) (nthcdr (- m n) x) x))) | 581 ;; (while (consp (cdr x)) (pop x)) |
581 ; (while (consp (cdr x)) (pop x)) | 582 ;; x)) |
582 ; x)) | |
583 | |
584 ;;; `butlast' is implemented as a C primitive, as of 1998-11 | |
585 ;;; `nbutlast' is implemented as a C primitive, as of 1998-11 | |
586 | |
587 ;(defun butlast (x &optional n) | |
588 ; "Return a copy of LIST with the last N elements removed." | |
589 ; (if (and n (<= n 0)) x | |
590 ; (nbutlast (copy-sequence x) n))) | |
591 | |
592 ;(defun nbutlast (x &optional n) | |
593 ; "Modify LIST to remove the last N elements." | |
594 ; (let ((m (length x))) | |
595 ; (or n (setq n 1)) | |
596 ; (and (< n m) | |
597 ; (progn | |
598 ; (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil)) | |
599 ; x)))) | |
600 | 583 |
601 (defun list* (arg &rest rest) ; See compiler macro in cl-macs.el | 584 (defun list* (arg &rest rest) ; See compiler macro in cl-macs.el |
602 "Return a new list with specified args as elements, cons'd to last arg. | 585 "Return a new list with specified args as elements, cons'd to last arg. |
603 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to | 586 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to |
604 `(cons A (cons B (cons C D)))'." | 587 `(cons A (cons B (cons C D)))'." |
630 | 613 |
631 (defun cl-maclisp-member (item list) | 614 (defun cl-maclisp-member (item list) |
632 (while (and list (not (equal item (car list)))) (setq list (cdr list))) | 615 (while (and list (not (equal item (car list)))) (setq list (cdr list))) |
633 list) | 616 list) |
634 | 617 |
635 ;;; Define an Emacs 19-compatible `member' for the benefit of Emacs 18 users. | |
636 (or (and (fboundp 'member) (subrp (symbol-function 'member))) | |
637 (defalias 'member 'cl-maclisp-member)) | |
638 | |
639 (defalias 'cl-member 'memq) ; for compatibility with old CL package | 618 (defalias 'cl-member 'memq) ; for compatibility with old CL package |
640 (defalias 'cl-floor 'floor*) | 619 (defalias 'cl-floor 'floor*) |
641 (defalias 'cl-ceiling 'ceiling*) | 620 (defalias 'cl-ceiling 'ceiling*) |
642 (defalias 'cl-truncate 'truncate*) | 621 (defalias 'cl-truncate 'truncate*) |
643 (defalias 'cl-round 'round*) | 622 (defalias 'cl-round 'round*) |
681 ;;; Miscellaneous. | 660 ;;; Miscellaneous. |
682 | 661 |
683 ;; XEmacs change | 662 ;; XEmacs change |
684 (define-error 'cl-assertion-failed "Assertion failed") | 663 (define-error 'cl-assertion-failed "Assertion failed") |
685 | 664 |
686 ;;; This is defined in Emacs 19; define it here for Emacs 18 users. | 665 ;; XEmacs change: omit the autoload rules; we handle those a different way |
687 (defun cl-add-hook (hook func &optional append) | |
688 "Add to hook variable HOOK the function FUNC. | |
689 FUNC is not added if it already appears on the list stored in HOOK." | |
690 (let ((old (and (boundp hook) (symbol-value hook)))) | |
691 (and (listp old) (not (eq (car old) 'lambda)) | |
692 (setq old (list old))) | |
693 (and (not (member func old)) | |
694 (set hook (if append (nconc old (list func)) (cons func old)))))) | |
695 (or (fboundp 'add-hook) (defalias 'add-hook 'cl-add-hook)) | |
696 | |
697 ;; XEmacs change | |
698 ;(load "cl-defs") | |
699 | 666 |
700 ;;; Define data for indentation and edebug. | 667 ;;; Define data for indentation and edebug. |
701 (mapcar | 668 (mapcar |
702 #'(lambda (entry) | 669 #'(lambda (entry) |
703 (mapcar | 670 (mapcar |
749 | 716 |
750 (defvar cl-hacked-flag nil) | 717 (defvar cl-hacked-flag nil) |
751 (defun cl-hack-byte-compiler () | 718 (defun cl-hack-byte-compiler () |
752 (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)) | 719 (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)) |
753 (progn | 720 (progn |
721 (setq cl-hacked-flag t) ; Do it first, to prevent recursion. | |
754 (when (not (fboundp 'cl-compile-time-init)) | 722 (when (not (fboundp 'cl-compile-time-init)) |
755 (load "cl-macs" nil t)) | 723 (load "cl-macs" nil t)) |
756 (cl-compile-time-init) ; in cl-macs.el | 724 (cl-compile-time-init)))) ; In cl-macs.el. |
757 (setq cl-hacked-flag t)))) | |
758 | 725 |
759 ;;; Try it now in case the compiler has already been loaded. | 726 ;;; Try it now in case the compiler has already been loaded. |
760 (cl-hack-byte-compiler) | 727 (cl-hack-byte-compiler) |
761 | 728 |
762 ;;; Also make a hook in case compiler is loaded after this file. | 729 ;;; Also make a hook in case compiler is loaded after this file. |
763 ;;; The compiler doesn't call any hooks when it loads or runs, but | 730 ;;; The compiler doesn't call any hooks when it loads or runs, but |
764 ;;; we can take advantage of the fact that emacs-lisp-mode will be | 731 ;;; we can take advantage of the fact that emacs-lisp-mode will be |
765 ;;; called when the compiler reads in the file to be compiled. | 732 ;;; called when the compiler reads in the file to be compiled. |
766 ;;; BUG: If the first compilation is `byte-compile' rather than | 733 ;;; BUG: If the first compilation is `byte-compile' rather than |
767 ;;; `byte-compile-file', we lose. Oh, well. | 734 ;;; `byte-compile-file', we lose. Emacs has fixed this by hanging it |
735 ;;; on `bytecomp-load-hook' instead, which we do not have. | |
768 (add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler) | 736 (add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler) |
769 | 737 |
770 | 738 |
771 ;;; The following ensures that packages which expect the old-style cl.el | 739 ;;; The following ensures that packages which expect the old-style cl.el |
772 ;;; will be happy with this one. | 740 ;;; will be happy with this one. |
773 | 741 |
774 (provide 'cl) | 742 (provide 'cl) |
775 | 743 |
776 (provide 'mini-cl) ; for Epoch | |
777 | |
778 (run-hooks 'cl-load-hook) | 744 (run-hooks 'cl-load-hook) |
779 | 745 |
746 ;;; arch-tag: 5f07fa74-f153-4524-9303-21f5be125851 | |
780 ;;; cl.el ends here | 747 ;;; cl.el ends here |