1 ;;; gnus-clfns.el --- compiler macros for emulating cl functions
2 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Kastsumi Yamaoka <yamaoka@jpl.org>
5 ;; Keywords: cl, compile
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
32 (if (featurep 'xemacs)
34 (eval-when-compile (require 'cl))
37 (define-compiler-macro butlast (&whole form x &optional n)
38 (if (and (fboundp 'butlast)
39 (subrp (symbol-function 'butlast)))
50 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
56 (setcdr (nthcdr (- m 2) x) nil)
59 (define-compiler-macro coerce (&whole form x type)
60 (if (and (fboundp 'coerce)
61 (subrp (symbol-function 'coerce)))
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))
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))
76 (t (error "Can't coerce %s to type %s" x type))))))
78 (define-compiler-macro copy-list (&whole form list)
79 (if (and (fboundp 'copy-list)
80 (subrp (symbol-function 'copy-list)))
85 (while (consp list) (push (pop list) res))
86 (prog1 (nreverse res) (setcdr res list)))
89 (define-compiler-macro last (&whole form x &optional n)
90 (if (and (fboundp 'last)
91 (subrp (symbol-function 'last)))
107 (while (consp (cdr x))
111 (define-compiler-macro merge (&whole form type seq1 seq2 pred &rest keys)
112 (if (and (fboundp 'merge)
113 (subrp (symbol-function 'merge)))
119 (or (listp seq1) (setq seq1 (append seq1 nil)))
120 (or (listp seq2) (setq seq2 (append seq2 nil)))
122 (while (and seq1 seq2)
123 (if (funcall pred (car seq2) (car seq1))
124 (push (pop seq2) res)
125 (push (pop seq1) res)))
126 (coerce (nconc (nreverse res) seq1 seq2) type)))))
128 (define-compiler-macro string (&whole form &rest args)
129 (if (and (fboundp 'string)
130 (subrp (symbol-function 'string)))
132 (list 'concat (cons 'list args))))
134 (defun-maybe string (&rest args)
135 "Concatenate all the argument characters and make the result a string."
138 (define-compiler-macro string-to-list (&whole form string)
139 (cond ((fboundp 'string-to-list)
141 ((fboundp 'string-to-char-list)
142 (list 'string-to-char-list string))
144 `(let* ((str ,string)
149 (setq c (sref str idx))
150 (setq idx (+ idx (char-bytes c)))
154 ;; 92.7.2 by K.Handa (imported from Mule 2.3)
155 (defun-maybe string-to-list (str)
156 (let ((len (length str))
160 (setq c (sref str idx))
161 (setq idx (+ idx (char-bytes c)))
165 (define-compiler-macro subseq (&whole form seq start &optional end)
166 (if (and (fboundp 'subseq)
167 (subrp (symbol-function 'subseq)))
174 (substring seq start end)
177 (setq end (+ end (setq len (length seq)))))
179 (setq start (+ start (or len (setq len (length seq))))))
182 (setq seq (nthcdr start seq)))
184 (while (>= (setq end (1- end)) start)
185 (push (pop seq) res))
188 (let ((res (make-vector (max (- end start) 0) nil))
191 (aset res i (aref seq start))
198 (substring seq start)
201 (setq start (+ start (or len (setq len (length seq))))))
204 (setq seq (nthcdr start seq)))
207 (let* ((end (or len (length seq)))
208 (res (make-vector (max (- end start) 0) nil))
211 (aset res i (aref seq start))
217 ;; A tool for the developers.
219 (defvar cl-run-time-functions
221 Values-list acons assoc-if assoc-if-not build-klist butlast ceiling*
222 coerce common-lisp-indent-function compiler-macroexpand concatenate
223 copy-list count count-if count-if-not delete* delete-duplicates delete-if
224 delete-if-not duplicate-symbols-p elt-satisfies-test-p equalp evenp every
225 extract-from-klist fill find find-if find-if-not floatp-safe floor* gcd
226 gensym gentemp get-setf-method getf hash-table-count hash-table-p
227 intersection isqrt keyword-argument-supplied-p keyword-of keywordp last
228 lcm ldiff lisp-indent-259 lisp-indent-do lisp-indent-function-lambda-hack
229 lisp-indent-report-bad-format lisp-indent-tagbody list-length
230 make-hash-table make-random-state map mapc mapcan mapcar* mapcon mapl
231 maplist member-if member-if-not merge mismatch mod* nbutlast nintersection
232 notany notevery nreconc nset-difference nset-exclusive-or nsublis nsubst
233 nsubst-if nsubst-if-not nsubstitute nsubstitute-if nsubstitute-if-not
234 nunion oddp pair-with-newsyms pairlis position position-if position-if-not
235 proclaim random* random-state-p rassoc* rassoc-if rassoc-if-not
236 reassemble-argslists reduce rem* remove remove* remove-duplicates
237 remove-if remove-if-not remq replace revappend round* safe-idiv search
238 set-difference set-exclusive-or setelt setnth setnthcdr signum some sort*
239 stable-sort sublis subseq subsetp subst subst-if subst-if-not substitute
240 substitute-if substitute-if-not tailp tree-equal truncate* union
241 unzip-lists zip-lists)
242 "A list of CL run-time functions. Some functions were built-in, nowadays.")
245 (defun find-cl-run-time-functions (file-or-directory arg)
246 "Find CL run-time functions in the FILE-OR-DIRECTORY. You can alter
247 the behavior of this command with the prefix ARG as described below.
249 By default, it searches for all the CL run-time functions listed in
250 the variable `cl-run-time-functions'.
251 With 1 or 3 \\[universal-argument]'s, the built-in functions in this Emacs\
254 With 2 or 3 \\[universal-argument]'s, just the symbols will also be reported.
256 You can use the `digit-argument' 1, 2 or 3 instead of\
257 \\[universal-argument]'s."
258 (interactive (list (read-file-name "Find CL run-time functions in: "
259 nil default-directory t)
261 (unless (interactive-p)
262 (error "You should invoke `M-x find-cl-run-time-functions' interactively"))
263 (let ((report-symbols (member arg '((16) (64) 2 3)))
264 files clfns working file lines form forms fns fn newform buffer
266 buffer-file-format format-alist
267 insert-file-contents-post-hook insert-file-contents-pre-hook)
268 (cond ((file-directory-p file-or-directory)
269 (setq files (directory-files file-or-directory t "\\.el$"))
271 (unless (file-exists-p file)
272 (setq files (delete file files))))
274 (message "No files found in: %s" file-or-directory))
276 ((file-exists-p file-or-directory)
277 (setq files (list file-or-directory)))
279 (message "No such file or directory: %s" file-or-directory)))
281 (if (member arg '((4) (64) 1 3))
282 (dolist (fn cl-run-time-functions)
283 (unless (and (fboundp fn)
284 (subrp (symbol-function fn)))
286 (setq clfns cl-run-time-functions))
287 (set-buffer (setq working
289 " *Searching for CL run-time functions*")))
290 (let (emacs-lisp-mode-hook)
293 (setq file (pop files)
294 lines (list nil nil))
295 (message "Searching for CL run-time functions in: %s..."
296 (file-name-nondirectory file))
297 (insert-file-contents file nil nil nil t)
298 ;; XEmacs moves point to the beginning of the buffer after
299 ;; inserting a file, FSFmacs doesn't so if the fifth argument
300 ;; of `insert-file-contents' is specified.
301 (goto-char (point-min))
304 (while (and (looking-at "[\t\v\f\r ]*\\(;.*\\)?$")
305 (zerop (forward-line 1))))
307 (setcar lines (if (bolp)
308 (1+ (count-lines (point-min) (point)))
309 (count-lines (point-min) (point))))
310 (when (consp;; Ignore stand-alone symbols, strings, etc.
311 (setq form (condition-case nil
314 (setcdr lines (list (count-lines (point-min) (point))))
315 (setq forms (list form)
318 (setq form (pop forms))
321 (cond ((memq fn '(apply mapatoms mapcar mapconcat
322 mapextent symbol-function))
323 (if (consp (car form))
324 (when (memq (caar form) '(\` backquote quote))
325 (setcar form (cdar form)))
326 (setq form (cdr form))))
327 ((memq fn '(\` backquote quote))
330 (setq form (car form)
333 (push (list (or (car-safe form) form))
335 (setq form (cdr-safe form)))
336 (setq form (nreverse newform)))
338 ((memq fn '(defadvice
339 defmacro defsubst defun
340 defmacro-maybe defmacro-maybe-cond
341 defsubst-maybe defun-maybe
343 (setq form (cddr form)))
344 ((memq fn '(defalias lambda fset))
345 (setq form (cdr form)))
346 ((eq fn 'define-compiler-macro)
349 (setcar form (cadar form)))
350 ((memq fn '(let let*))
356 (when (and (consp element)
357 (consp (cadr element)))
362 (when (and (consp (cadr form))
363 (memq (caadr form) '(\` backquote quote)))
364 (setcdr form (list (cdadr form)))))
365 ((and (memq fn clfns)
368 (setq forms (append form forms))))
373 (setq buffer (get-buffer-create
374 (concat "*CL run-time functions in: "
375 file-or-directory "*"))))
378 (setq window (get-buffer-window buffer t)
379 scroll (- 2 (window-height window))
380 fill-column (max 16 (- (window-width window) 2))
389 (mapconcat (lambda (fn) (format "%s" fn))
392 (fill-region (point-min) (point-max))
393 (goto-char (point-min))
396 (insert (format "%5d - %5d:" (car lines) (cadr lines)))
397 (goto-char (point-max))
398 (forward-line scroll)
399 (set-window-start window (point))
400 (goto-char (point-max))
402 (set-buffer working)))))
403 (kill-buffer working)
406 (message "No CL run-time functions found in: %s"
407 file-or-directory)))))
409 (provide 'gnus-clfns)
411 ;;; gnus-clfns.el ends here