* Makefile.am (EXTRA_DIST): Add liece.xbm and liece.xpm.
[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))
37            (and (fboundp 'symbol-file)
38                 (string-equal (symbol-file symbol) "subr")))))
39
40 (if (featurep 'xemacs)
41     nil
42   (require 'cl)
43
44   (define-compiler-macro last (&whole form x &optional n)
45     (if (liece-clfns-subr-fboundp 'last)
46         form
47       (if n
48           `(let* ((x ,x) (n ,n) (m 0) (p x))
49              (while (consp p)
50                (incf m)
51                (pop p))
52              (if (<= n 0)
53                  p
54                (if (< n m)
55                    (nthcdr (- m n) x)
56                  x)))
57         `(let ((x ,x))
58            (while (consp (cdr x))
59              (pop x))
60            x))))
61
62   (define-compiler-macro remq (&whole form elt list)
63     (if (liece-clfns-subr-fboundp 'remq)
64         form
65       `(let ((elt ,elt)
66              (list ,list))
67          (if (memq elt list)
68              (delq elt (copy-sequence list))
69            list))))
70
71   (define-compiler-macro member-if (&whole form pred list)
72     (if (liece-clfns-subr-fboundp 'member-if)
73         form
74       `(let ((fn ,pred)
75              (seq ,list))
76          (while (and seq (not (funcall fn (car seq))))
77            (pop seq))
78          seq)))
79
80   (define-compiler-macro member-if-not (&whole form pred list)
81     (if (liece-clfns-subr-fboundp 'member-if-not)
82         form
83       `(let ((fn ,pred)
84              (seq ,list))
85          (while (and seq (funcall fn (car seq)))
86            (pop seq))
87          seq)))
88   
89   (define-compiler-macro delete-if (&whole form pred list)
90     (if (liece-clfns-subr-fboundp 'delete-if)
91         form
92       `(let* ((fn ,pred) (seq ,list) (p seq))
93          (while (and p (not (funcall fn (car p))))
94            (pop p))
95          (if p (delq (car p) seq)))))
96
97   (define-compiler-macro remove-if (&whole form pred list)
98     (if (liece-clfns-subr-fboundp 'remove-if)
99         form
100       `(let* ((fn ,pred) (seq (copy-sequence ,list)) (p seq))
101          (while (and p (not (funcall fn (car p))))
102            (pop p))
103          (if p (delq (car p) seq) seq))))
104
105   (define-compiler-macro remove-if-not (&whole form pred list)
106     (if (liece-clfns-subr-fboundp 'remove-if-not)
107         form
108       `(let* ((fn ,pred) (seq (copy-sequence ,list)) (p seq))
109          (while (and p (funcall fn (car p)))
110            (pop p))
111          (if p (delq (car p) seq) seq))))
112
113   (define-compiler-macro assoc-if (&whole form pred list)
114     (if (liece-clfns-subr-fboundp 'assoc-if)
115         form
116       `(let ((fn ,pred) (seq ,list))
117          (while (and seq (not (funcall fn (caar seq))))
118            (pop seq))
119          (car seq))))
120
121   (define-compiler-macro rassoc-if (&whole form pred list)
122     (if (liece-clfns-subr-fboundp 'rassoc-if)
123         form
124       `(let ((fn ,pred) (seq ,list))
125          (while (and seq (not (funcall fn (cdar seq))))
126            (pop seq))
127          (car seq)))))
128
129 (provide 'liece-clfns)
130
131 ;;; liece-clfns.el ends here