update.
[chise/xemacs-chise.git.1] / lisp / cl-compat.el
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, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Synched up with: FSF 19.34.
27
28 ;;; Commentary:
29
30 ;; These are extensions to Emacs Lisp that provide a degree of
31 ;; Common Lisp compatibility, beyond what is already built-in
32 ;; in Emacs Lisp.
33 ;;
34 ;; This package was written by Dave Gillespie; it is a complete
35 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
36 ;;
37 ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19.
38 ;;
39 ;; Bug reports, comments, and suggestions are welcome!
40
41 ;; This file contains emulations of internal routines of the older
42 ;; CL package which users may have called directly from their code.
43 ;; Use (require 'cl-compat) to get these routines.
44
45 ;; See cl.el for Change Log.
46
47
48 ;;; Code:
49
50 ;; Require at load-time, but not when compiling cl-compat.
51 (or (featurep 'cl) (require 'cl))
52
53
54 ;;; Keyword routines not supported by new package.
55
56 (defmacro defkeyword (x &optional doc)
57   (list* 'defconst x (list 'quote x) (and doc (list doc))))
58
59 ;; XEmacs change.
60 ;; We have built-in function.
61 ;;(defun keywordp (sym)
62 ;;  (and (symbolp sym) (eq (aref (symbol-name sym) 0) ?\:) (set sym sym)))
63
64 (defun keyword-of (sym)
65   (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
66
67
68 ;;; Multiple values.  Note that the new package uses a different
69 ;;; convention for multiple values.  The following definitions
70 ;;; emulate the old convention; all function names have been changed
71 ;;; by capitalizing the first letter: Values, Multiple-value-*,
72 ;;; to avoid conflict with the new-style definitions in cl-macs.
73
74 (put 'Multiple-value-bind  'lisp-indent-function 2)
75 (put 'Multiple-value-setq  'lisp-indent-function 2)
76 (put 'Multiple-value-call  'lisp-indent-function 1)
77 (put 'Multiple-value-prog1 'lisp-indent-function 1)
78
79 (defvar *mvalues-values* nil)
80
81 (defun Values (&rest val-forms)
82   (setq *mvalues-values* val-forms)
83   (car val-forms))
84
85 (defun Values-list (val-forms)
86   (apply 'values val-forms))
87
88 (defmacro Multiple-value-list (form)
89   (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
90         '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
91              (list *mvalues-temp*))))
92
93 (defmacro Multiple-value-call (function &rest args)
94   (list 'apply function
95         (cons 'append
96               (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
97                       args))))
98
99 (defmacro Multiple-value-bind (vars form &rest body)
100   (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
101
102 (defmacro Multiple-value-setq (vars form)
103   (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
104
105 (defmacro Multiple-value-prog1 (form &rest body)
106   (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
107
108
109 ;;; Routines for parsing keyword arguments.
110
111 (defun build-klist (arglist keys &optional allow-others)
112   (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
113     (or allow-others
114         (let ((bad (set-difference (mapcar 'car res) keys)))
115           (if bad (error "Bad keywords: %s not in %s" bad keys))))
116     res))
117
118 (defun extract-from-klist (klist key &optional def)
119   (let ((res (assq key klist))) (if res (cdr res) def)))
120
121 (defun keyword-argument-supplied-p (klist key)
122   (assq key klist))
123
124 (defun elt-satisfies-test-p (item elt klist)
125   (let ((test-not (cdr (assq ':test-not klist)))
126         (test (cdr (assq ':test klist)))
127         (key (cdr (assq ':key klist))))
128     (if key (setq elt (funcall key elt)))
129     (if test-not (not (funcall test-not item elt))
130       (funcall (or test 'eql) item elt))))
131
132
133 ;;; Rounding functions with old-style multiple value returns.
134
135 (defun cl-floor (a &optional b) (Values-list (floor* a b)))
136 (defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
137 (defun cl-round (a &optional b) (Values-list (round* a b)))
138 (defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
139
140 (defun safe-idiv (a b)
141   (let* ((q (/ (abs a) (abs b)))
142          (s (* (signum a) (signum b))))
143     (Values q (- a (* s q b)) s)))
144
145
146 ;; Internal routines.
147
148 (defun pair-with-newsyms (oldforms)
149   (let ((newsyms (mapcar (function (lambda (x) (gensym))) oldforms)))
150     (Values (mapcar* 'list newsyms oldforms) newsyms)))
151
152 (defun zip-lists (evens odds)
153   (mapcan 'list evens odds))
154
155 (defun unzip-lists (list)
156   (let ((e nil) (o nil))
157     (while list
158       (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
159     (Values (nreverse e) (nreverse o))))
160
161 (defun reassemble-argslists (list)
162   (let ((n (apply 'min (mapcar 'length list))) (res nil))
163     (while (>= (setq n (1- n)) 0)
164       (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
165     res))
166
167 (defun duplicate-symbols-p (list)
168   (let ((res nil))
169     (while list
170       (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
171       (setq list (cdr list)))
172     res))
173
174
175 ;;; Setf internals.
176
177 (defun setnth (n list x)
178   (setcar (nthcdr n list) x))
179
180 (defun setnthcdr (n list x)
181   (setcdr (nthcdr (1- n) list) x))
182
183 (defun setelt (seq n x)
184   (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
185
186
187 ;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
188 ;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
189 ;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
190 ;;; all names with embedded `$'.
191
192
193 (provide 'cl-compat)
194
195 ;;; cl-compat.el ends here
196