* gnus-clfns.el (find-cl-run-time-functions): New implementation.
[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 ;; This module is for mainly avoiding cl runtime functions in FSF
27 ;; Emacsen.  Function should also be defined as an ordinary function
28 ;; if it will not be provided in cl.
29
30 ;;; Code:
31
32 (if (featurep 'xemacs)
33     nil
34   (eval-when-compile (require 'cl))
35   (require 'pym)
36
37   (define-compiler-macro butlast (&whole form x &optional n)
38     (if (and (fboundp 'butlast)
39              (subrp (symbol-function 'butlast)))
40         form
41       (if n
42           `(let ((x ,x)
43                  (n ,n))
44              (if (and n (<= n 0))
45                  x
46                (let ((m (length x)))
47                  (or n (setq n 1))
48                  (and (< n m)
49                       (progn
50                         (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
51                         x)))))
52         `(let* ((x ,x)
53                 (m (length x)))
54            (and (< 1 m)
55                 (progn
56                   (setcdr (nthcdr (- m 2) x) nil)
57                   x))))))
58
59   (define-compiler-macro coerce (&whole form x type)
60     (if (and (fboundp 'coerce)
61              (subrp (symbol-function 'coerce)))
62         form
63       `(let ((x ,x)
64              (type ,type))
65          (cond ((eq type 'list) (if (listp x) x (append x nil)))
66                ((eq type 'vector) (if (vectorp x) x (vconcat x)))
67                ((eq type 'string) (if (stringp x) x (concat x)))
68                ((eq type 'array) (if (arrayp x) x (vconcat x)))
69                ((and (eq type 'character) (stringp x) (= (length x) 1))
70                 (aref x 0))
71                ((and (eq type 'character) (symbolp x)
72                      (= (length (symbol-name x)) 1))
73                 (aref (symbol-name x) 0))
74                ((eq type 'float) (float x))
75                ((typep x type) x)
76                (t (error "Can't coerce %s to type %s" x type))))))
77
78   (define-compiler-macro last (&whole form x &optional n)
79     (if (and (fboundp 'last)
80              (subrp (symbol-function 'last)))
81         form
82       (if n
83           `(let* ((x ,x)
84                   (n ,n)
85                   (m 0)
86                   (p x))
87              (while (consp p)
88                (incf m)
89                (pop p))
90              (if (<= n 0)
91                  p
92                (if (< n m)
93                    (nthcdr (- m n) x)
94                  x)))
95         `(let ((x ,x))
96            (while (consp (cdr x))
97              (pop x))
98            x))))
99
100   (define-compiler-macro merge (&whole form type seq1 seq2 pred &rest keys)
101     (if (and (fboundp 'merge)
102              (subrp (symbol-function 'merge)))
103         form
104       `(let ((type ,type)
105              (seq1 ,seq1)
106              (seq2 ,seq2)
107              (pred ,pred))
108          (or (listp seq1) (setq seq1 (append seq1 nil)))
109          (or (listp seq2) (setq seq2 (append seq2 nil)))
110          (let ((res nil))
111            (while (and seq1 seq2)
112              (if (funcall pred (car seq2) (car seq1))
113                  (push (pop seq2) res)
114                (push (pop seq1) res)))
115            (coerce (nconc (nreverse res) seq1 seq2) type)))))
116
117   (define-compiler-macro string (&whole form &rest args)
118     (if (and (fboundp 'string)
119              (subrp (symbol-function 'string)))
120         form
121       (list 'concat (cons 'list args))))
122
123   (defun-maybe string (&rest args)
124     "Concatenate all the argument characters and make the result a string."
125     (concat args))
126
127   (define-compiler-macro subseq (&whole form seq start &optional end)
128     (if (and (fboundp 'subseq)
129              (subrp (symbol-function 'subseq)))
130         form
131       (if end
132           `(let ((seq ,seq)
133                  (start ,start)
134                  (end ,end))
135              (if (stringp seq)
136                  (substring seq start end)
137                (let (len)
138                  (if (< end 0)
139                      (setq end (+ end (setq len (length seq)))))
140                  (if (< start 0)
141                      (setq start (+ start (or len (setq len (length seq))))))
142                  (cond ((listp seq)
143                         (if (> start 0)
144                             (setq seq (nthcdr start seq)))
145                         (let ((res nil))
146                           (while (>= (setq end (1- end)) start)
147                             (push (pop seq) res))
148                           (nreverse res)))
149                        (t
150                         (let ((res (make-vector (max (- end start) 0) nil))
151                               (i 0))
152                           (while (< start end)
153                             (aset res i (aref seq start))
154                             (setq i (1+ i)
155                                   start (1+ start)))
156                           res))))))
157         `(let ((seq ,seq)
158                (start ,start))
159            (if (stringp seq)
160                (substring seq start)
161              (let (len)
162                (if (< start 0)
163                    (setq start (+ start (or len (setq len (length seq))))))
164                (cond ((listp seq)
165                       (if (> start 0)
166                           (setq seq (nthcdr start seq)))
167                       (copy-sequence seq))
168                      (t
169                       (let* ((end (or len (length seq)))
170                              (res (make-vector (max (- end start) 0) nil))
171                              (i 0))
172                         (while (< start end)
173                           (aset res i (aref seq start))
174                           (setq i (1+ i)
175                                 start (1+ start)))
176                         res)))))))))
177   )
178
179 ;; A tool for the developers.
180
181 (defvar cl-run-time-functions
182   '(Values
183     Values-list acons assoc-if assoc-if-not build-klist butlast ceiling*
184     coerce common-lisp-indent-function compiler-macroexpand concatenate
185     copy-list count count-if count-if-not delete* delete-duplicates delete-if
186     delete-if-not duplicate-symbols-p elt-satisfies-test-p equalp evenp every
187     extract-from-klist fill find find-if find-if-not floatp-safe floor* gcd
188     gensym gentemp get-setf-method getf hash-table-count hash-table-p
189     intersection isqrt keyword-argument-supplied-p keyword-of keywordp last
190     lcm ldiff lisp-indent-259 lisp-indent-do lisp-indent-function-lambda-hack
191     lisp-indent-report-bad-format lisp-indent-tagbody list-length
192     make-hash-table make-random-state map mapc mapcan mapcar* mapcon mapl
193     maplist member-if member-if-not merge mismatch mod* nbutlast nintersection
194     notany notevery nreconc nset-difference nset-exclusive-or nsublis nsubst
195     nsubst-if nsubst-if-not nsubstitute nsubstitute-if nsubstitute-if-not
196     nunion oddp pair-with-newsyms pairlis position position-if position-if-not
197     proclaim random* random-state-p rassoc* rassoc-if rassoc-if-not
198     reassemble-argslists reduce rem* remove remove* remove-duplicates
199     remove-if remove-if-not remq replace revappend round* safe-idiv search
200     set-difference set-exclusive-or setelt setnth setnthcdr signum some sort*
201     stable-sort sublis subseq subsetp subst subst-if subst-if-not substitute
202     substitute-if substitute-if-not tailp tree-equal truncate* union
203     unzip-lists zip-lists)
204   "A list of CL run-time functions.  Some functions were built-in, nowadays.")
205
206 ;;;###autoload
207 (defun find-cl-run-time-functions (file-or-directory arg)
208   "Find CL run-time functions in the FILE-OR-DIRECTORY.  You can alter
209 the behavior of this command with the prefix ARG as described below.
210
211 By default, it searches for all the CL run-time functions listed in
212  the variable `cl-run-time-functions'.
213 With 1 or 3 \\[universal-argument]'s, the built-in functions in this Emacs\
214  will not be
215  reported.
216 With 2 or 3 \\[universal-argument]'s, just the symbols will also be reported.
217
218 You can use the `digit-argument' 1, 2 or 3 instead of\
219  \\[universal-argument]'s."
220   (interactive (list (read-file-name "Find CL run-time functions in: "
221                                      nil default-directory t)
222                      current-prefix-arg))
223   (unless (interactive-p)
224     (error "You should invoke `M-x find-cl-run-time-functions' interactively"))
225   (let ((report-symbols (member arg '((16) (64) 2 3)))
226         files clfns working file lines form forms fns fn newform buffer
227         window scroll
228         buffer-file-format format-alist
229         insert-file-contents-post-hook insert-file-contents-pre-hook)
230     (cond ((file-directory-p file-or-directory)
231            (setq files (directory-files file-or-directory t "\\.el$"))
232            (dolist (file files)
233              (unless (file-exists-p file)
234                (setq files (delete file files))))
235            (unless files
236              (message "No files found in: %s" file-or-directory))
237            files)
238           ((file-exists-p file-or-directory)
239            (setq files (list file-or-directory)))
240           (t
241            (message "No such file or directory: %s" file-or-directory)))
242     (when files
243       (if (member arg '((4) (64) 1 3))
244           (dolist (fn cl-run-time-functions)
245             (unless (and (fboundp fn)
246                          (subrp (symbol-function fn)))
247               (push fn clfns)))
248         (setq clfns cl-run-time-functions))
249       (set-buffer (setq working
250                         (get-buffer-create
251                          " *Searching for CL run-time functions*")))
252       (let (emacs-lisp-mode-hook)
253         (emacs-lisp-mode))
254       (while files
255         (setq file (pop files)
256               lines (list nil nil))
257         (message "Searching for CL run-time functions in: %s..."
258                  (file-name-nondirectory file))
259         (insert-file-contents file nil nil nil t)
260         ;; XEmacs moves point to the beginning of the buffer after
261         ;; inserting a file, FSFmacs doesn't so if the fifth argument
262         ;; of `insert-file-contents' is specified.
263         (goto-char (point-min))
264         ;;
265         (while (progn
266                  (while (and (looking-at "[\t\v\f\r ]*\\(;.*\\)?$")
267                              (zerop (forward-line 1))))
268                  (not (eobp)))
269           (setcar lines (if (bolp)
270                             (1+ (count-lines (point-min) (point)))
271                           (count-lines (point-min) (point))))
272           (when (consp;; Ignore stand-alone symbols, strings, etc.
273                  (setq form (condition-case nil
274                                 (read working)
275                               (error nil))))
276             (setcdr lines (list (count-lines (point-min) (point))))
277             (setq forms (list form)
278                   fns nil)
279             (while forms
280               (setq form (pop forms))
281               (when (consp form)
282                 (setq fn (pop form))
283                 (cond ((memq fn '(apply mapatoms mapcar mapconcat
284                                         mapextent symbol-function))
285                        (if (consp (car form))
286                            (when (memq (caar form) '(\` backquote quote))
287                              (setcar form (cdar form)))
288                          (setq form (cdr form))))
289                       ((memq fn '(\` backquote quote))
290                        (if report-symbols
291                            (progn
292                              (setq form (car form)
293                                    newform nil)
294                              (while form
295                                (push (list (or (car-safe form) form))
296                                      newform)
297                                (setq form (cdr-safe form)))
298                              (setq form (nreverse newform)))
299                          (setq form nil)))
300                       ((memq fn '(defadvice
301                                    defmacro defsubst defun
302                                    defmacro-maybe defmacro-maybe-cond
303                                    defsubst-maybe defun-maybe
304                                    defun-maybe-cond))
305                        (setq form (cddr form)))
306                       ((memq fn '(defalias lambda fset))
307                        (setq form (cdr form)))
308                       ((eq fn 'define-compiler-macro)
309                        (setq form nil))
310                       ((eq fn 'dolist)
311                        (setcar form (cadar form)))
312                       ((memq fn '(let let*))
313                        (setq form
314                              (append
315                               (delq nil
316                                     (mapcar
317                                      (lambda (element)
318                                        (when (and (consp element)
319                                                   (consp (cadr element)))
320                                          (cadr element)))
321                                      (car form)))
322                               (cdr form))))
323                       ((eq fn 'sort)
324                        (when (and (consp (cadr form))
325                                   (memq (caadr form) '(\` backquote quote)))
326                          (setcdr form (list (cdadr form)))))
327                       ((and (memq fn clfns)
328                             (listp form))
329                        (push fn fns)))
330                 (setq forms (append form forms))))
331             (when fns
332               (if buffer
333                   (set-buffer buffer)
334                 (display-buffer
335                  (setq buffer (get-buffer-create
336                                (concat "*CL run-time functions in: "
337                                        file-or-directory "*"))))
338                 (set-buffer buffer)
339                 (erase-buffer)
340                 (setq window (get-buffer-window buffer t)
341                       scroll (- 2 (window-height window))
342                       fill-column (max 16 (- (window-width window) 2))
343                       fill-prefix "               "))
344               (when file
345                 (insert file "\n")
346                 (setq file nil))
347               (narrow-to-region
348                (point)
349                (progn
350                  (insert fill-prefix
351                          (mapconcat (lambda (fn) (format "%s" fn))
352                                     (nreverse fns) " "))
353                  (point)))
354               (fill-region (point-min) (point-max))
355               (goto-char (point-min))
356               (widen)
357               (delete-char 14)
358               (insert (format "%5d - %5d:" (car lines) (cadr lines)))
359               (goto-char (point-max))
360               (forward-line scroll)
361               (set-window-start window (point))
362               (goto-char (point-max))
363               (sit-for 0)
364               (set-buffer working)))))
365       (kill-buffer working)
366       (if buffer
367           (message "Done")
368         (message "No CL run-time functions found in: %s"
369                  file-or-directory)))))
370
371 (provide 'gnus-clfns)
372
373 ;;; gnus-clfns.el ends here