Merge remote-tracking branch 'origin/master' into for-steve
[sxemacs] / 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 SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs 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
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Synched up with: FSF 19.34.
25
26 ;;; Commentary:
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 XEmacs/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
46 ;;; Code:
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
57 ;; XEmacs change.
58 ;; We have built-in function.
59 ;;(defun keywordp (sym)
60 ;;  (and (symbolp sym) (eq (aref (symbol-name sym) 0) ?\:) (set sym sym)))
61
62 (defun keyword-of (sym)
63   (or (keywordp sym) (keywordp (intern (format ":%s" sym)))))
64
65
66 ;;; Multiple values.  Note that the new package uses a different
67 ;;; convention for multiple values.  The following definitions
68 ;;; emulate the old convention; all function names have been changed
69 ;;; by capitalizing the first letter: Values, Multiple-value-*,
70 ;;; to avoid conflict with the new-style definitions in cl-macs.
71
72 (put 'Multiple-value-bind  'lisp-indent-function 2)
73 (put 'Multiple-value-setq  'lisp-indent-function 2)
74 (put 'Multiple-value-call  'lisp-indent-function 1)
75 (put 'Multiple-value-prog1 'lisp-indent-function 1)
76
77 (defvar *mvalues-values* nil)
78
79 (defun Values (&rest val-forms)
80   (setq *mvalues-values* val-forms)
81   (car val-forms))
82
83 (defun Values-list (val-forms)
84   (apply 'values val-forms))
85
86 (defmacro Multiple-value-list (form)
87   (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form))
88         '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*)
89              (list *mvalues-temp*))))
90
91 (defmacro Multiple-value-call (function &rest args)
92   (list 'apply function
93         (cons 'append
94               (mapcar (function (lambda (x) (list 'Multiple-value-list x)))
95                       args))))
96
97 (defmacro Multiple-value-bind (vars form &rest body)
98   (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body))
99
100 (defmacro Multiple-value-setq (vars form)
101   (list 'multiple-value-setq vars (list 'Multiple-value-list form)))
102
103 (defmacro Multiple-value-prog1 (form &rest body)
104   (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body)))
105
106
107 ;;; Routines for parsing keyword arguments.
108
109 (defun build-klist (arglist keys &optional allow-others)
110   (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist))))
111     (or allow-others
112         (let ((bad (set-difference (mapcar 'car res) keys)))
113           (if bad (error "Bad keywords: %s not in %s" bad keys))))
114     res))
115
116 (defun extract-from-klist (klist key &optional def)
117   (let ((res (assq key klist))) (if res (cdr res) def)))
118
119 (defun keyword-argument-supplied-p (klist key)
120   (assq key klist))
121
122 (defun elt-satisfies-test-p (item elt klist)
123   (let ((test-not (cdr (assq ':test-not klist)))
124         (test (cdr (assq ':test klist)))
125         (key (cdr (assq ':key klist))))
126     (if key (setq elt (funcall key elt)))
127     (if test-not (not (funcall test-not item elt))
128       (funcall (or test 'eql) item elt))))
129
130
131 ;;; Rounding functions with old-style multiple value returns.
132
133 (defun cl-floor (a &optional b) (Values-list (floor* a b)))
134 (defun cl-ceiling (a &optional b) (Values-list (ceiling* a b)))
135 (defun cl-round (a &optional b) (Values-list (round* a b)))
136 (defun cl-truncate (a &optional b) (Values-list (truncate* a b)))
137
138 (defun safe-idiv (a b)
139   (let* ((q (/ (abs a) (abs b)))
140          (s (* (signum a) (signum b))))
141     (Values q (- a (* s q b)) s)))
142
143
144 ;; Internal routines.
145
146 (defun pair-with-newsyms (oldforms)
147   (let ((newsyms (mapcar (function (lambda (x) (gensym))) oldforms)))
148     (Values (mapcar* 'list newsyms oldforms) newsyms)))
149
150 (defun zip-lists (evens odds)
151   (mapcan 'list evens odds))
152
153 (defun unzip-lists (list)
154   (let ((e nil) (o nil))
155     (while list
156       (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list)))
157     (Values (nreverse e) (nreverse o))))
158
159 (defun reassemble-argslists (list)
160   (let ((n (apply 'min (mapcar 'length list))) (res nil))
161     (while (>= (setq n (1- n)) 0)
162       (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res)))
163     res))
164
165 (defun duplicate-symbols-p (list)
166   (let ((res nil))
167     (while list
168       (if (memq (car list) (cdr list)) (setq res (cons (car list) res)))
169       (setq list (cdr list)))
170     res))
171
172
173 ;;; Setf internals.
174
175 (defun setnth (n list x)
176   (setcar (nthcdr n list) x))
177
178 (defun setnthcdr (n list x)
179   (setcdr (nthcdr (1- n) list) x))
180
181 (defun setelt (seq n x)
182   (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x)))
183
184
185 ;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms,
186 ;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms,
187 ;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify,
188 ;;; all names with embedded `$'.
189
190
191 (provide 'cl-compat)
192
193 ;;; cl-compat.el ends here