root/trunk/lisp/emacs-lisp/cl-compat.el

Revision 4220, 6.0 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

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