comparison lisp/cl/cl-compat.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility)
2
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Version: 2.02
7 ;; Keywords: extensions
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Synched up with: FSF 19.30.
26
27 ;;; Commentary:
28
29 ;; These are extensions to Emacs Lisp that provide a degree of
30 ;; Common Lisp compatibility, beyond what is already built-in
31 ;; in Emacs Lisp.
32 ;;
33 ;; This package was written by Dave Gillespie; it is a complete
34 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
35 ;;
36 ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19.
37 ;;
38 ;; Bug reports, comments, and suggestions are welcome!
39
40 ;; This file contains emulations of internal routines of the older
41 ;; CL package which users may have called directly from their code.
42 ;; Use (require 'cl-compat) to get these routines.
43
44 ;; See cl.el for Change Log.
45
46
47 ;;; Code:
48
49 ;; Require at load-time, but not when compiling cl-compat.
50 (or (featurep 'cl) (require 'cl))
51
52
53 ;;; Keyword routines not supported by new package.
54
55 (defmacro defkeyword (x &optional doc)
56 (list* 'defconst x (list 'quote x) (and doc (list doc))))
57
58 (defun keywordp (sym)
59 (and (symbolp sym) (eq (aref (symbol-name sym) 0) ?\:) (set sym sym)))
60
61 (defun keyword-of (sym)
62 (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
63
64
65 ;;; Multiple values. Note that the new package uses a different
66 ;;; convention for multiple values. The following definitions
67 ;;; emulate the old convention; all function names have been changed
68 ;;; by capitalizing the first letter: Values, Multiple-value-*,
69 ;;; to avoid conflict with the new-style definitions in cl-macs.
70
71 (put 'Multiple-value-bind 'lisp-indent-function 2)
72 (put 'Multiple-value-setq 'lisp-indent-function 2)
73 (put 'Multiple-value-call 'lisp-indent-function 1)
74 (put 'Multiple-value-prog1 'lisp-indent-function 1)
75
76 (defvar *mvalues-values* nil)
77
78 (defun Values (&rest val-forms)
79 (setq *mvalues-values* val-forms)
80 (car val-forms))
81
82 (defun Values-list (val-forms)
83 (apply 'values val-forms))
84
85 (defmacro Multiple-value-list (form)
86 (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
87 '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
88 (list *mvalues-temp*))))
89
90 (defmacro Multiple-value-call (function &rest args)
91 (list 'apply function
92 (cons 'append
93 (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
94 args))))
95
96 (defmacro Multiple-value-bind (vars form &rest body)
97 (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
98
99 (defmacro Multiple-value-setq (vars form)
100 (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
101
102 (defmacro Multiple-value-prog1 (form &rest body)
103 (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
104
105
106 ;;; Routines for parsing keyword arguments.
107
108 (defun build-klist (arglist keys &optional allow-others)
109 (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
110 (or allow-others
111 (let ((bad (set-difference (mapcar 'car res) keys)))
112 (if bad (error "Bad keywords: %s not in %s" bad keys))))
113 res))
114
115 (defun extract-from-klist (klist key &optional def)
116 (let ((res (assq key klist))) (if res (cdr res) def)))
117
118 (defun keyword-argument-supplied-p (klist key)
119 (assq key klist))
120
121 (defun elt-satisfies-test-p (item elt klist)
122 (let ((test-not (cdr (assq ':test-not klist)))
123 (test (cdr (assq ':test klist)))
124 (key (cdr (assq ':key klist))))
125 (if key (setq elt (funcall key elt)))
126 (if test-not (not (funcall test-not item elt))
127 (funcall (or test 'eql) item elt))))
128
129
130 ;;; Rounding functions with old-style multiple value returns.
131
132 (defun cl-floor (a &optional b) (Values-list (floor* a b)))
133 (defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
134 (defun cl-round (a &optional b) (Values-list (round* a b)))
135 (defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
136
137 (defun safe-idiv (a b)
138 (let* ((q (/ (abs a) (abs b)))
139 (s (* (signum a) (signum b))))
140 (Values q (- a (* s q b)) s)))
141
142
143 ;; Internal routines.
144
145 (defun pair-with-newsyms (oldforms)
146 (let ((newsyms (mapcar (function (lambda (x) (gensym))) oldforms)))
147 (Values (mapcar* 'list newsyms oldforms) newsyms)))
148
149 (defun zip-lists (evens odds)
150 (mapcan 'list evens odds))
151
152 (defun unzip-lists (list)
153 (let ((e nil) (o nil))
154 (while list
155 (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
156 (Values (nreverse e) (nreverse o))))
157
158 (defun reassemble-argslists (list)
159 (let ((n (apply 'min (mapcar 'length list))) (res nil))
160 (while (>= (setq n (1- n)) 0)
161 (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
162 res))
163
164 (defun duplicate-symbols-p (list)
165 (let ((res nil))
166 (while list
167 (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
168 (setq list (cdr list)))
169 res))
170
171
172 ;;; Setf internals.
173
174 (defun setnth (n list x)
175 (setcar (nthcdr n list) x))
176
177 (defun setnthcdr (n list x)
178 (setcdr (nthcdr (1- n) list) x))
179
180 (defun setelt (seq n x)
181 (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
182
183
184 ;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
185 ;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
186 ;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
187 ;;; all names with embedded `$'.
188
189
190 (provide 'cl-compat)
191
192 ;;; cl-compat.el ends here
193