Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / emacs-lisp / cl-compat.el
CommitLineData
be010748 1;;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility)
fcd73769 2
d59c3137 3;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
8b72699e 4;; 2006, 2007, 2008 Free Software Foundation, Inc.
fcd73769
RS
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
d6cba7ae 12;; GNU Emacs is free software: you can redistribute it and/or modify
fcd73769 13;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
fcd73769
RS
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
d6cba7ae 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fcd73769 24
07b3798c 25;;; Commentary:
fcd73769
RS
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
07b3798c 45;;; Code:
fcd73769
RS
46
47;; Require at load-time, but not when compiling cl-compat.
48(or (featurep 'cl) (require 'cl))
49
50
51;;; Keyword routines not supported by new package.
52
53(defmacro defkeyword (x &optional doc)
54 (list* 'defconst x (list 'quote x) (and doc (list doc))))
55
fcd73769
RS
56(defun keyword-of (sym)
57 (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
58
59
60;;; Multiple values. Note that the new package uses a different
61;;; convention for multiple values. The following definitions
62;;; emulate the old convention; all function names have been changed
63;;; by capitalizing the first letter: Values, Multiple-value-*,
64;;; to avoid conflict with the new-style definitions in cl-macs.
65
66(put 'Multiple-value-bind 'lisp-indent-function 2)
67(put 'Multiple-value-setq 'lisp-indent-function 2)
68(put 'Multiple-value-call 'lisp-indent-function 1)
69(put 'Multiple-value-prog1 'lisp-indent-function 1)
70
71(defvar *mvalues-values* nil)
72
73(defun Values (&rest val-forms)
74 (setq *mvalues-values* val-forms)
75 (car val-forms))
76
77(defun Values-list (val-forms)
78 (apply 'values val-forms))
79
80(defmacro Multiple-value-list (form)
81 (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
82 '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
83 (list *mvalues-temp*))))
84
85(defmacro Multiple-value-call (function &rest args)
86 (list 'apply function
87 (cons 'append
88 (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
89 args))))
90
91(defmacro Multiple-value-bind (vars form &rest body)
92 (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
93
94(defmacro Multiple-value-setq (vars form)
95 (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
96
97(defmacro Multiple-value-prog1 (form &rest body)
98 (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
99
100
101;;; Routines for parsing keyword arguments.
102
103(defun build-klist (arglist keys &optional allow-others)
104 (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
105 (or allow-others
106 (let ((bad (set-difference (mapcar 'car res) keys)))
107 (if bad (error "Bad keywords: %s not in %s" bad keys))))
108 res))
109
110(defun extract-from-klist (klist key &optional def)
111 (let ((res (assq key klist))) (if res (cdr res) def)))
112
113(defun keyword-argument-supplied-p (klist key)
114 (assq key klist))
115
116(defun elt-satisfies-test-p (item elt klist)
117 (let ((test-not (cdr (assq ':test-not klist)))
118 (test (cdr (assq ':test klist)))
119 (key (cdr (assq ':key klist))))
120 (if key (setq elt (funcall key elt)))
121 (if test-not (not (funcall test-not item elt))
122 (funcall (or test 'eql) item elt))))
123
124
125;;; Rounding functions with old-style multiple value returns.
126
127(defun cl-floor (a &optional b) (Values-list (floor* a b)))
128(defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
129(defun cl-round (a &optional b) (Values-list (round* a b)))
130(defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
131
132(defun safe-idiv (a b)
133 (let* ((q (/ (abs a) (abs b)))
134 (s (* (signum a) (signum b))))
135 (Values q (- a (* s q b)) s)))
136
137
138;; Internal routines.
139
140(defun pair-with-newsyms (oldforms)
ab3d4bb2 141 (let ((newsyms (mapcar (lambda (x) (make-symbol "--cl-var--")) oldforms)))
fcd73769
RS
142 (Values (mapcar* 'list newsyms oldforms) newsyms)))
143
144(defun zip-lists (evens odds)
145 (mapcan 'list evens odds))
146
147(defun unzip-lists (list)
148 (let ((e nil) (o nil))
149 (while list
150 (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
151 (Values (nreverse e) (nreverse o))))
152
153(defun reassemble-argslists (list)
154 (let ((n (apply 'min (mapcar 'length list))) (res nil))
155 (while (>= (setq n (1- n)) 0)
156 (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
157 res))
158
159(defun duplicate-symbols-p (list)
160 (let ((res nil))
161 (while list
162 (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
163 (setq list (cdr list)))
164 res))
165
166
167;;; Setf internals.
168
169(defun setnth (n list x)
170 (setcar (nthcdr n list) x))
171
172(defun setnthcdr (n list x)
173 (setcdr (nthcdr (1- n) list) x))
174
175(defun setelt (seq n x)
176 (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
177
178
179;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
180;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
181;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
182;;; all names with embedded `$'.
183
184
185(provide 'cl-compat)
186
7187be8b
GM
187;; Local variables:
188;; byte-compile-warnings: (not cl-functions)
189;; End:
190
ab3d4bb2 191;; arch-tag: 9996bb4f-aaf5-4592-b436-bf64759a3163
fcd73769 192;;; cl-compat.el ends here