Undo the last change.
[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 (defun liece-clfns-subr-fboundp (symbol)
34   "Return t if SYMBOL's function definition is a basic function."
35   (and (fboundp symbol)
36        (or (subrp (symbol-function symbol-function))
37            (string-equal (symbol-file symbol) "subr"))))
38
39 (if (featurep 'xemacs)
40     nil
41   (require 'cl)
42
43   (define-compiler-macro last (&whole form x &optional n)
44     (if (liece-clfns-subr-fboundp 'last)
45         form
46       (if n
47           `(let* ((x ,x) (n ,n) (m 0) (p x))
48              (while (consp p)
49                (incf m)
50                (pop p))
51              (if (<= n 0)
52                  p
53                (if (< n m)
54                    (nthcdr (- m n) x)
55                  x)))
56         `(let ((x ,x))
57            (while (consp (cdr x))
58              (pop x))
59            x))))
60
61   (define-compiler-macro member-if (&whole form pred list)
62     (if (liece-clfns-subr-fboundp 'member-if)
63         form
64       `(let ((fn ,pred)
65              (seq ,list))
66          (while (and seq (not (funcall fn (car seq))))
67            (pop seq))
68          seq)))
69
70   (define-compiler-macro member-if-not (&whole form pred list)
71     (if (liece-clfns-subr-fboundp 'member-if-not)
72         form
73       `(let ((fn ,pred)
74              (seq ,list))
75          (while (and seq (funcall fn (car seq)))
76            (pop seq))
77          seq)))
78   
79   (define-compiler-macro delete-if (&whole form pred list)
80     (if (liece-clfns-subr-fboundp 'delete-if)
81         form
82       `(let* ((fn ,pred) (seq ,list) (p seq))
83          (while (and p (not (funcall fn (car p))))
84            (pop p))
85          (if p (delq (car p) seq)))))
86
87   (define-compiler-macro remove-if (&whole form pred list)
88     (if (liece-clfns-subr-fboundp 'remove-if)
89         form
90       `(let* ((fn ,pred) (seq (copy-sequence ,list)) (p seq))
91          (while (and p (not (funcall fn (car p))))
92            (pop p))
93          (if p (delq (car p) seq) seq))))
94
95   (define-compiler-macro remove-if-not (&whole form pred list)
96     (if (liece-clfns-subr-fboundp '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 (liece-clfns-subr-fboundp 'assoc-if)
105         form
106       `(let ((fn ,pred) (seq ,list))
107          (while (and seq (not (funcall fn (caar seq))))
108            (pop seq))
109          (car seq))))
110
111   (define-compiler-macro rassoc-if (&whole form pred list)
112     (if (liece-clfns-subr-fboundp 'rassoc-if)
113         form
114       `(let ((fn ,pred) (seq ,list))
115          (while (and seq (not (funcall fn (cdar seq))))
116            (pop seq))
117          (car seq)))))
118
119 (provide 'liece-clfns)
120
121 ;;; liece-clfns.el ends here