Mercurial > emacs
annotate lisp/emacs-lisp/cl-compat.el @ 98139:5e46a10b86b5
*** empty log message ***
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 11 Sep 2008 06:16:59 +0000 |
parents | be35d9783436 |
children | a9dc0e7c3f2b |
rev | line source |
---|---|
13337 | 1 ;;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility) |
4355 | 2 |
74466 | 3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005, |
79704 | 4 ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
4355 | 5 |
6 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
7 ;; Version: 2.02 | |
8 ;; Keywords: extensions | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
4355 | 13 ;; it under the terms of the GNU General Public License as published by |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
15 ;; (at your option) any later version. |
4355 | 16 |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
94655
90a2847062be
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
4355 | 24 |
7942 | 25 ;;; Commentary: |
4355 | 26 |
27 ;; These are extensions to Emacs Lisp that provide a degree of | |
28 ;; Common Lisp compatibility, beyond what is already built-in | |
29 ;; in Emacs Lisp. | |
30 ;; | |
31 ;; This package was written by Dave Gillespie; it is a complete | |
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
33 ;; | |
34 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. | |
35 ;; | |
36 ;; Bug reports, comments, and suggestions are welcome! | |
37 | |
38 ;; This file contains emulations of internal routines of the older | |
39 ;; CL package which users may have called directly from their code. | |
40 ;; Use (require 'cl-compat) to get these routines. | |
41 | |
42 ;; See cl.el for Change Log. | |
43 | |
44 | |
7942 | 45 ;;; Code: |
4355 | 46 |
96711
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
47 ;; This used to be: |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
48 ;; (or (featurep 'cl) (require 'cl)) |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
49 ;; which just has the effect of fooling the byte-compiler into not |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
50 ;; loading cl when compiling. However, that leads to some bogus |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
51 ;; compiler warnings. Loading cl when compiling cannot do any harm, |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
52 ;; because for a long time bootstrap-emacs contained 'cl, due to being |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
53 ;; dumped from uncompiled files that eval-when-compile'd cl. So every |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
54 ;; file was compiled with 'cl loaded. |
be35d9783436
Simply require 'cl (silences spurious warnings).
Glenn Morris <rgm@gnu.org>
parents:
94655
diff
changeset
|
55 (require 'cl) |
4355 | 56 |
57 | |
58 ;;; Keyword routines not supported by new package. | |
59 | |
60 (defmacro defkeyword (x &optional doc) | |
61 (list* 'defconst x (list 'quote x) (and doc (list doc)))) | |
62 | |
63 (defun keyword-of (sym) | |
64 (or (keywordp sym) (keywordp (intern (format ":%s" sym))))) | |
65 | |
66 | |
67 ;;; Multiple values. Note that the new package uses a different | |
68 ;;; convention for multiple values. The following definitions | |
69 ;;; emulate the old convention; all function names have been changed | |
70 ;;; by capitalizing the first letter: Values, Multiple-value-*, | |
71 ;;; to avoid conflict with the new-style definitions in cl-macs. | |
72 | |
73 (put 'Multiple-value-bind 'lisp-indent-function 2) | |
74 (put 'Multiple-value-setq 'lisp-indent-function 2) | |
75 (put 'Multiple-value-call 'lisp-indent-function 1) | |
76 (put 'Multiple-value-prog1 'lisp-indent-function 1) | |
77 | |
78 (defvar *mvalues-values* nil) | |
79 | |
80 (defun Values (&rest val-forms) | |
81 (setq *mvalues-values* val-forms) | |
82 (car val-forms)) | |
83 | |
84 (defun Values-list (val-forms) | |
85 (apply 'values val-forms)) | |
86 | |
87 (defmacro Multiple-value-list (form) | |
88 (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form)) | |
89 '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*) | |
90 (list *mvalues-temp*)))) | |
91 | |
92 (defmacro Multiple-value-call (function &rest args) | |
93 (list 'apply function | |
94 (cons 'append | |
95 (mapcar (function (lambda (x) (list 'Multiple-value-list x))) | |
96 args)))) | |
97 | |
98 (defmacro Multiple-value-bind (vars form &rest body) | |
99 (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body)) | |
100 | |
101 (defmacro Multiple-value-setq (vars form) | |
102 (list 'multiple-value-setq vars (list 'Multiple-value-list form))) | |
103 | |
104 (defmacro Multiple-value-prog1 (form &rest body) | |
105 (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body))) | |
106 | |
107 | |
108 ;;; Routines for parsing keyword arguments. | |
109 | |
110 (defun build-klist (arglist keys &optional allow-others) | |
111 (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist)))) | |
112 (or allow-others | |
113 (let ((bad (set-difference (mapcar 'car res) keys))) | |
114 (if bad (error "Bad keywords: %s not in %s" bad keys)))) | |
115 res)) | |
116 | |
117 (defun extract-from-klist (klist key &optional def) | |
118 (let ((res (assq key klist))) (if res (cdr res) def))) | |
119 | |
120 (defun keyword-argument-supplied-p (klist key) | |
121 (assq key klist)) | |
122 | |
123 (defun elt-satisfies-test-p (item elt klist) | |
124 (let ((test-not (cdr (assq ':test-not klist))) | |
125 (test (cdr (assq ':test klist))) | |
126 (key (cdr (assq ':key klist)))) | |
127 (if key (setq elt (funcall key elt))) | |
128 (if test-not (not (funcall test-not item elt)) | |
129 (funcall (or test 'eql) item elt)))) | |
130 | |
131 | |
132 ;;; Rounding functions with old-style multiple value returns. | |
133 | |
134 (defun cl-floor (a &optional b) (Values-list (floor* a b))) | |
135 (defun cl-ceiling (a &optional b) (Values-list (ceiling* a b))) | |
136 (defun cl-round (a &optional b) (Values-list (round* a b))) | |
137 (defun cl-truncate (a &optional b) (Values-list (truncate* a b))) | |
138 | |
139 (defun safe-idiv (a b) | |
140 (let* ((q (/ (abs a) (abs b))) | |
141 (s (* (signum a) (signum b)))) | |
142 (Values q (- a (* s q b)) s))) | |
143 | |
144 | |
145 ;; Internal routines. | |
146 | |
147 (defun pair-with-newsyms (oldforms) | |
58254
e64002f85cf6
(pair-with-newsyms): Use make-symbol.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
148 (let ((newsyms (mapcar (lambda (x) (make-symbol "--cl-var--")) oldforms))) |
4355 | 149 (Values (mapcar* 'list newsyms oldforms) newsyms))) |
150 | |
151 (defun zip-lists (evens odds) | |
152 (mapcan 'list evens odds)) | |
153 | |
154 (defun unzip-lists (list) | |
155 (let ((e nil) (o nil)) | |
156 (while list | |
157 (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list))) | |
158 (Values (nreverse e) (nreverse o)))) | |
159 | |
160 (defun reassemble-argslists (list) | |
161 (let ((n (apply 'min (mapcar 'length list))) (res nil)) | |
162 (while (>= (setq n (1- n)) 0) | |
163 (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res))) | |
164 res)) | |
165 | |
166 (defun duplicate-symbols-p (list) | |
167 (let ((res nil)) | |
168 (while list | |
169 (if (memq (car list) (cdr list)) (setq res (cons (car list) res))) | |
170 (setq list (cdr list))) | |
171 res)) | |
172 | |
173 | |
174 ;;; Setf internals. | |
175 | |
176 (defun setnth (n list x) | |
177 (setcar (nthcdr n list) x)) | |
178 | |
179 (defun setnthcdr (n list x) | |
180 (setcdr (nthcdr (1- n) list) x)) | |
181 | |
182 (defun setelt (seq n x) | |
183 (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x))) | |
184 | |
185 | |
186 ;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms, | |
187 ;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms, | |
188 ;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify, | |
189 ;;; all names with embedded `$'. | |
190 | |
191 | |
192 (provide 'cl-compat) | |
193 | |
85728
2797206f47b8
Add a local variables section, and set
Glenn Morris <rgm@gnu.org>
parents:
78217
diff
changeset
|
194 ;; Local variables: |
2797206f47b8
Add a local variables section, and set
Glenn Morris <rgm@gnu.org>
parents:
78217
diff
changeset
|
195 ;; byte-compile-warnings: (not cl-functions) |
2797206f47b8
Add a local variables section, and set
Glenn Morris <rgm@gnu.org>
parents:
78217
diff
changeset
|
196 ;; End: |
2797206f47b8
Add a local variables section, and set
Glenn Morris <rgm@gnu.org>
parents:
78217
diff
changeset
|
197 |
58254
e64002f85cf6
(pair-with-newsyms): Use make-symbol.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
198 ;; arch-tag: 9996bb4f-aaf5-4592-b436-bf64759a3163 |
4355 | 199 ;;; cl-compat.el ends here |