* gnus-clfns.el (subseq, merge, coerce, butlast): New compiler macros.
[elisp/gnus.git-] / lisp / gnus-clfns.el
1 ;;; gnus-clfns.el --- compiler macros for emulating cl functions
2 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3
4 ;; Author: Kastsumi Yamaoka <yamaoka@jpl.org>
5 ;; Keywords: cl, compile
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Avoid cl runtime functions for FSF Emacsen.
27
28 ;;; Code:
29
30 (if (featurep 'xemacs)
31     nil
32   (require 'cl)
33
34   (define-compiler-macro butlast (&whole form x &optional n)
35     (if (and (fboundp 'butlast)
36              (subrp (symbol-function 'butlast)))
37         form
38       (if n
39           `(let ((x ,x)
40                  (n ,n))
41              (if (and n (<= n 0))
42                  x
43                (let ((m (length x)))
44                  (or n (setq n 1))
45                  (and (< n m)
46                       (progn
47                         (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
48                         x)))))
49         `(let* ((x ,x)
50                 (m (length x)))
51            (and (< 1 m)
52                 (progn
53                   (setcdr (nthcdr (- m 2) x) nil)
54                   x))))))
55
56   (define-compiler-macro coerce (&whole form x type)
57     (if (and (fboundp 'coerce)
58              (subrp (symbol-function 'coerce)))
59         form
60       `(let ((x ,x)
61              (type ,type))
62          (cond ((eq type 'list) (if (listp x) x (append x nil)))
63                ((eq type 'vector) (if (vectorp x) x (vconcat x)))
64                ((eq type 'string) (if (stringp x) x (concat x)))
65                ((eq type 'array) (if (arrayp x) x (vconcat x)))
66                ((and (eq type 'character) (stringp x) (= (length x) 1))
67                 (aref x 0))
68                ((and (eq type 'character) (symbolp x)
69                      (= (length (symbol-name x)) 1))
70                 (aref (symbol-name x) 0))
71                ((eq type 'float) (float x))
72                ((typep x type) x)
73                (t (error "Can't coerce %s to type %s" x type))))))
74
75   (define-compiler-macro last (&whole form x &optional n)
76     (if (and (fboundp 'last)
77              (subrp (symbol-function 'last)))
78         form
79       (if n
80           `(let* ((x ,x)
81                   (n ,n)
82                   (m 0)
83                   (p x))
84              (while (consp p)
85                (incf m)
86                (pop p))
87              (if (<= n 0)
88                  p
89                (if (< n m)
90                    (nthcdr (- m n) x)
91                  x)))
92         `(let ((x ,x))
93            (while (consp (cdr x))
94              (pop x))
95            x))))
96
97   (define-compiler-macro merge (&whole form type seq1 seq2 pred &rest keys)
98     (if (and (fboundp 'merge)
99              (subrp (symbol-function 'merge)))
100         form
101       `(let ((type ,type)
102              (seq1 ,seq1)
103              (seq2 ,seq2)
104              (pred ,pred))
105          (or (listp seq1) (setq seq1 (append seq1 nil)))
106          (or (listp seq2) (setq seq2 (append seq2 nil)))
107          (let ((res nil))
108            (while (and seq1 seq2)
109              (if (funcall pred (car seq2) (car seq1))
110                  (push (pop seq2) res)
111                (push (pop seq1) res)))
112            (coerce (nconc (nreverse res) seq1 seq2) type)))))
113
114   (define-compiler-macro subseq (&whole form seq start &optional end)
115     (if (and (fboundp 'subseq)
116              (subrp (symbol-function 'subseq)))
117         form
118       (if end
119           `(let ((seq ,seq)
120                  (start ,start)
121                  (end ,end))
122              (if (stringp seq)
123                  (substring seq start end)
124                (let (len)
125                  (if (< end 0)
126                      (setq end (+ end (setq len (length seq)))))
127                  (if (< start 0)
128                      (setq start (+ start (or len (setq len (length seq))))))
129                  (cond ((listp seq)
130                         (if (> start 0)
131                             (setq seq (nthcdr start seq)))
132                         (let ((res nil))
133                           (while (>= (setq end (1- end)) start)
134                             (push (pop seq) res))
135                           (nreverse res)))
136                        (t
137                         (let ((res (make-vector (max (- end start) 0) nil))
138                               (i 0))
139                           (while (< start end)
140                             (aset res i (aref seq start))
141                             (setq i (1+ i)
142                                   start (1+ start)))
143                           res))))))
144         `(let ((seq ,seq)
145                (start ,start))
146            (if (stringp seq)
147                (substring seq start)
148              (let (len)
149                (if (< start 0)
150                    (setq start (+ start (or len (setq len (length seq))))))
151                (cond ((listp seq)
152                       (if (> start 0)
153                           (setq seq (nthcdr start seq)))
154                       (copy-sequence seq))
155                      (t
156                       (let* ((end (or len (length seq)))
157                              (res (make-vector (max (- end start) 0) nil))
158                              (i 0))
159                         (while (< start end)
160                           (aset res i (aref seq start))
161                           (setq i (1+ i)
162                                 start (1+ start)))
163                         res)))))))))
164   )
165
166 (provide 'gnus-clfns)
167
168 ;;; gnus-clfns.el ends here