2000-06-30 Akira Ohashi <bg66@luck.gr.jp>
[elisp/liece.git] / lisp / liece-clfns.el
1 ;;; liece-clfns.el --- compiler macros for emulating cl functions
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 2000-03-19
6 ;; Keywords: cl, compile
7
8 ;; This file is part of Liece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25
26 ;;; Commentary:
27
28 ;; This file is borrowed from `gnus-clfns.el' from T-gnus.
29 ;; Avoid cl runtime functions for FSF Emacsen.
30
31 ;;; Code:
32
33 (if (featurep 'xemacs)
34     nil
35   (require 'cl)
36
37   (define-compiler-macro last (&whole form x &optional n)
38     (if (and (fboundp 'last)
39              (subrp (symbol-function 'last)))
40         form
41       (if n
42           `(let* ((x ,x) (n ,n) (m 0) (p x))
43              (while (consp p)
44                (incf m)
45                (pop p))
46              (if (<= n 0)
47                  p
48                (if (< n m)
49                    (nthcdr (- m n) x)
50                  x)))
51         `(let ((x ,x))
52            (while (consp (cdr x))
53              (pop x))
54            x))))
55
56   (define-compiler-macro member-if (&whole form pred list)
57     (if (and (fboundp 'member-if)
58              (subrp (symbol-function 'member-if)))
59         form
60       `(let ((fn ,pred)
61              (seq ,list))
62          (while (and seq (not (funcall fn (car seq))))
63            (pop seq))
64          seq)))
65
66   (define-compiler-macro member-if-not (&whole form pred list)
67     (if (and (fboundp 'member-if-not)
68              (subrp (symbol-function 'member-if-not)))
69         form
70       `(let ((fn ,pred)
71              (seq ,list))
72          (while (and seq (funcall fn (car seq)))
73            (pop seq))
74          seq)))
75   
76   (define-compiler-macro delete-if (&whole form pred list)
77     (if (and (fboundp 'delete-if)
78              (subrp (symbol-function 'delete-if)))
79         form
80       `(let* ((fn ,pred) (seq ,list) (p seq))
81          (while (and p (not (funcall fn (car p))))
82            (pop p))
83          (if p (delq (car p) seq)))))
84
85   (define-compiler-macro remove-if (&whole form pred list)
86     (if (and (fboundp 'remove-if)
87              (subrp (symbol-function 'remove-if)))
88         form
89       `(let* ((fn ,pred) (seq (copy-sequence ,list)) (p seq))
90          (while (and p (not (funcall fn (car p))))
91            (pop p))
92          (if p (delq (car p) seq) seq))))
93
94   (define-compiler-macro remove-if-not (&whole form pred list)
95     (if (and (fboundp 'remove-if-not)
96              (subrp (symbol-function 'remove-if-not)))
97         form
98       `(let* ((fn ,pred) (seq (copy-sequence ,list)) (p seq))
99          (while (and p (funcall fn (car p)))
100            (pop p))
101          (if p (delq (car p) seq) seq))))
102
103   (define-compiler-macro assoc-if (&whole form pred list)
104     (if (and (fboundp 'assoc-if)
105              (subrp (symbol-function 'assoc-if)))
106         form
107       `(let ((fn ,pred) (seq ,list))
108          (while (and seq (not (funcall fn (caar seq))))
109            (pop seq))
110          (car seq))))
111
112   (define-compiler-macro rassoc-if (&whole form pred list)
113     (if (and (fboundp 'rassoc-if)
114              (subrp (symbol-function 'rassoc-if)))
115         form
116       `(let ((fn ,pred) (seq ,list))
117          (while (and seq (not (funcall fn (cdar seq))))
118            (pop seq))
119          (car seq)))))
120
121 (provide 'liece-clfns)
122
123 ;;; liece-clfns.el ends here