Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / obsolete / cl-compat.el
CommitLineData
be010748 1;;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility)
fcd73769 2
2f1cc336 3;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
49f70d46 4;; 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
fcd73769
RS
5
6;; Author: Dave Gillespie <daveg@synaptics.com>
7;; Version: 2.02
8;; Keywords: extensions
2f1cc336 9;; Obsolete-since: 23.3
fcd73769
RS
10
11;; This file is part of GNU Emacs.
12
d6cba7ae 13;; GNU Emacs is free software: you can redistribute it and/or modify
fcd73769 14;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
fcd73769
RS
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
d6cba7ae 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fcd73769 25
07b3798c 26;;; Commentary:
fcd73769 27
2f1cc336
GM
28;; This file has been obsolete since Emacs 23.3.
29
fcd73769
RS
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 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
07b3798c 48;;; Code:
fcd73769 49
e3d95497
GM
50;; This used to be:
51;; (or (featurep 'cl) (require 'cl))
52;; which just has the effect of fooling the byte-compiler into not
53;; loading cl when compiling. However, that leads to some bogus
54;; compiler warnings. Loading cl when compiling cannot do any harm,
55;; because for a long time bootstrap-emacs contained 'cl, due to being
56;; dumped from uncompiled files that eval-when-compile'd cl. So every
57;; file was compiled with 'cl loaded.
58(require 'cl)
fcd73769
RS
59
60
61;;; Keyword routines not supported by new package.
62
63(defmacro defkeyword (x &optional doc)
64 (list* 'defconst x (list 'quote x) (and doc (list doc))))
65
fcd73769
RS
66(defun keyword-of (sym)
67 (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
68
69
70;;; Multiple values. Note that the new package uses a different
71;;; convention for multiple values. The following definitions
72;;; emulate the old convention; all function names have been changed
73;;; by capitalizing the first letter: Values, Multiple-value-*,
74;;; to avoid conflict with the new-style definitions in cl-macs.
75
76(put 'Multiple-value-bind 'lisp-indent-function 2)
77(put 'Multiple-value-setq 'lisp-indent-function 2)
78(put 'Multiple-value-call 'lisp-indent-function 1)
79(put 'Multiple-value-prog1 'lisp-indent-function 1)
80
81(defvar *mvalues-values* nil)
82
83(defun Values (&rest val-forms)
84 (setq *mvalues-values* val-forms)
85 (car val-forms))
86
87(defun Values-list (val-forms)
88 (apply 'values val-forms))
89
90(defmacro Multiple-value-list (form)
91 (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
92 '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
93 (list *mvalues-temp*))))
94
95(defmacro Multiple-value-call (function &rest args)
96 (list 'apply function
97 (cons 'append
98 (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
99 args))))
100
101(defmacro Multiple-value-bind (vars form &rest body)
102 (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
103
104(defmacro Multiple-value-setq (vars form)
105 (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
106
107(defmacro Multiple-value-prog1 (form &rest body)
108 (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
109
110
111;;; Routines for parsing keyword arguments.
112
113(defun build-klist (arglist keys &optional allow-others)
114 (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
115 (or allow-others
116 (let ((bad (set-difference (mapcar 'car res) keys)))
117 (if bad (error "Bad keywords: %s not in %s" bad keys))))
118 res))
119
120(defun extract-from-klist (klist key &optional def)
121 (let ((res (assq key klist))) (if res (cdr res) def)))
122
123(defun keyword-argument-supplied-p (klist key)
124 (assq key klist))
125
126(defun elt-satisfies-test-p (item elt klist)
127 (let ((test-not (cdr (assq ':test-not klist)))
128 (test (cdr (assq ':test klist)))
129 (key (cdr (assq ':key klist))))
130 (if key (setq elt (funcall key elt)))
131 (if test-not (not (funcall test-not item elt))
132 (funcall (or test 'eql) item elt))))
133
134
135;;; Rounding functions with old-style multiple value returns.
136
137(defun cl-floor (a &optional b) (Values-list (floor* a b)))
138(defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
139(defun cl-round (a &optional b) (Values-list (round* a b)))
140(defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
141
142(defun safe-idiv (a b)
143 (let* ((q (/ (abs a) (abs b)))
144 (s (* (signum a) (signum b))))
145 (Values q (- a (* s q b)) s)))
146
147
148;; Internal routines.
149
150(defun pair-with-newsyms (oldforms)
ab3d4bb2 151 (let ((newsyms (mapcar (lambda (x) (make-symbol "--cl-var--")) oldforms)))
fcd73769
RS
152 (Values (mapcar* 'list newsyms oldforms) newsyms)))
153
154(defun zip-lists (evens odds)
155 (mapcan 'list evens odds))
156
157(defun unzip-lists (list)
158 (let ((e nil) (o nil))
159 (while list
160 (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
161 (Values (nreverse e) (nreverse o))))
162
163(defun reassemble-argslists (list)
164 (let ((n (apply 'min (mapcar 'length list))) (res nil))
165 (while (>= (setq n (1- n)) 0)
166 (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
167 res))
168
169(defun duplicate-symbols-p (list)
170 (let ((res nil))
171 (while list
172 (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
173 (setq list (cdr list)))
174 res))
175
176
177;;; Setf internals.
178
179(defun setnth (n list x)
180 (setcar (nthcdr n list) x))
181
182(defun setnthcdr (n list x)
183 (setcdr (nthcdr (1- n) list) x))
184
185(defun setelt (seq n x)
186 (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
187
188
189;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
190;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
191;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
192;;; all names with embedded `$'.
193
194
195(provide 'cl-compat)
196
7187be8b
GM
197;; Local variables:
198;; byte-compile-warnings: (not cl-functions)
199;; End:
200
fcd73769 201;;; cl-compat.el ends here