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