Synch with Gnus.
[elisp/gnus.git-] / lisp / gnus-clfns.el
1 ;;; gnus-clfns.el --- compiler macros for emulating cl functions
2 ;; Copyright (C) 2000 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 last (&whole form x &optional n)
35     (if (and (fboundp 'last)
36              (subrp (symbol-function 'last)))
37         form
38       (if n
39           `(let* ((x ,x)
40                   (n ,n)
41                   (m 0)
42                   (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 mapc (&whole form fn seq &rest rest)
57     (if (and (fboundp 'mapc)
58              (subrp (symbol-function 'mapc)))
59         form
60       (if rest
61           `(let* ((fn ,fn)
62                   (seq ,seq)
63                   (args (list seq ,@rest))
64                   (m (apply (function min) (mapcar (function length) args)))
65                   (n 0))
66              (while (< n m)
67                (apply fn (mapcar (function (lambda (arg) (nth n arg))) args))
68                (setq n (1+ n)))
69              seq)
70         `(let ((seq ,seq))
71            (mapcar ,fn seq)
72            seq))))
73   )
74
75 (provide 'gnus-clfns)
76
77 ;;; gnus-clfns.el ends here