1 ;;; gnus-clfns.el --- compiler macros for emulating cl functions
3 ;; Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Kastsumi Yamaoka <yamaoka@jpl.org>
6 ;; Keywords: cl, compile
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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)
15 ;; GNU Emacs 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.
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.
27 ;; This module is for mainly avoiding cl runtime functions in FSF
28 ;; Emacsen. Function should also be defined as an ordinary function
29 ;; if it will not be provided in cl.
33 (if (featurep 'xemacs)
35 (eval-when-compile (require 'cl))
38 (define-compiler-macro assq-delete-all (&whole form key alist)
39 (if (>= emacs-major-version 21)
45 (if (and (consp (car tail)) (eq (car (car tail)) key))
46 (setq alist (delq (car tail) alist)))
47 (setq tail (cdr tail)))
50 (define-compiler-macro butlast (&whole form x &optional n)
51 (if (>= emacs-major-version 21)
64 (setq x (copy-sequence x))
65 (setcdr (nthcdr (- (1- m) n) x) nil)))
71 (setq x (copy-sequence x))
72 (setcdr (nthcdr (- m 2) x) nil)
75 ;; (define-compiler-macro coerce (&whole form x type)
76 ;; (if (and (fboundp 'coerce)
77 ;; (subrp (symbol-function 'coerce)))
81 ;; (cond ((eq type 'list) (if (listp x) x (append x nil)))
82 ;; ((eq type 'vector) (if (vectorp x) x (vconcat x)))
83 ;; ((eq type 'string) (if (stringp x) x (concat x)))
84 ;; ((eq type 'array) (if (arrayp x) x (vconcat x)))
85 ;; ((and (eq type 'character) (stringp x) (= (length x) 1))
87 ;; ((and (eq type 'character) (symbolp x)
88 ;; (= (length (symbol-name x)) 1))
89 ;; (aref (symbol-name x) 0))
90 ;; ((eq type 'float) (float x))
92 ;; (t (error "Can't coerce %s to type %s" x type))))))
94 ;; (define-compiler-macro copy-list (&whole form list)
95 ;; (if (and (fboundp 'copy-list)
96 ;; (subrp (symbol-function 'copy-list)))
98 ;; `(let ((list ,list))
101 ;; (while (consp list) (push (pop list) res))
102 ;; (prog1 (nreverse res) (setcdr res list)))
105 (define-compiler-macro last (&whole form x &optional n)
106 (if (>= emacs-major-version 20)
122 (while (consp (cdr x))
126 (define-compiler-macro mapc (&whole form fn seq &rest rest)
127 (if (>= emacs-major-version 21)
132 (args (list seq ,@rest))
133 (m (apply (function min) (mapcar (function length) args)))
136 (apply fn (mapcar (function (lambda (arg) (nth n arg))) args))
143 ;; (define-compiler-macro merge (&whole form type seq1 seq2 pred &rest keys)
144 ;; (if (and (fboundp 'merge)
145 ;; (subrp (symbol-function 'merge)))
147 ;; `(let ((type ,type)
151 ;; (or (listp seq1) (setq seq1 (append seq1 nil)))
152 ;; (or (listp seq2) (setq seq2 (append seq2 nil)))
154 ;; (while (and seq1 seq2)
155 ;; (if (funcall pred (car seq2) (car seq1))
156 ;; (push (pop seq2) res)
157 ;; (push (pop seq1) res)))
158 ;; (coerce (nconc (nreverse res) seq1 seq2) type)))))
160 ;; (define-compiler-macro string (&whole form &rest args)
161 ;; (if (>= emacs-major-version 20)
163 ;; (list 'concat (cons 'list args))))
165 ;; (defun-maybe string (&rest args)
166 ;; "Concatenate all the argument characters and make the result a string."
169 (define-compiler-macro string-to-list (&whole form string)
170 (cond ((fboundp 'string-to-list)
172 ((fboundp 'string-to-char-list)
173 (list 'string-to-char-list string))
175 `(let* ((str ,string)
180 (setq c (sref str idx))
181 (setq idx (+ idx (char-bytes c)))
185 ;; 92.7.2 by K.Handa (imported from Mule 2.3)
186 (defun-maybe string-to-list (str)
187 (let ((len (length str))
191 (setq c (sref str idx))
192 (setq idx (+ idx (char-bytes c)))
196 ;; (define-compiler-macro subseq (&whole form seq start &optional end)
197 ;; (if (and (fboundp 'subseq)
198 ;; (subrp (symbol-function 'subseq)))
205 ;; (substring seq start end)
208 ;; (setq end (+ end (setq len (length seq)))))
210 ;; (setq start (+ start (or len (setq len (length seq))))))
211 ;; (cond ((listp seq)
213 ;; (setq seq (nthcdr start seq)))
215 ;; (while (>= (setq end (1- end)) start)
216 ;; (push (pop seq) res))
219 ;; (let ((res (make-vector (max (- end start) 0) nil))
221 ;; (while (< start end)
222 ;; (aset res i (aref seq start))
224 ;; start (1+ start)))
229 ;; (substring seq start)
232 ;; (setq start (+ start (or len (setq len (length seq))))))
233 ;; (cond ((listp seq)
235 ;; (setq seq (nthcdr start seq)))
236 ;; (copy-sequence seq))
238 ;; (let* ((end (or len (length seq)))
239 ;; (res (make-vector (max (- end start) 0) nil))
241 ;; (while (< start end)
242 ;; (aset res i (aref seq start))
244 ;; start (1+ start)))
248 ;; A tool for the developers.
250 (defvar cl-run-time-functions
252 Values-list acons assoc-if assoc-if-not build-klist butlast ceiling*
253 coerce common-lisp-indent-function compiler-macroexpand concatenate
254 copy-list count count-if count-if-not delete* delete-duplicates delete-if
255 delete-if-not duplicate-symbols-p elt-satisfies-test-p equalp evenp every
256 extract-from-klist fill find find-if find-if-not floatp-safe floor* gcd
257 gensym gentemp get-setf-method getf hash-table-count hash-table-p
258 intersection isqrt keyword-argument-supplied-p keyword-of keywordp last
259 lcm ldiff lisp-indent-259 lisp-indent-do lisp-indent-function-lambda-hack
260 lisp-indent-report-bad-format lisp-indent-tagbody list-length
261 make-hash-table make-random-state map mapc mapcan mapcar* mapcon mapl
262 maplist member-if member-if-not merge mismatch mod* nbutlast nintersection
263 notany notevery nreconc nset-difference nset-exclusive-or nsublis nsubst
264 nsubst-if nsubst-if-not nsubstitute nsubstitute-if nsubstitute-if-not
265 nunion oddp pair-with-newsyms pairlis position position-if position-if-not
266 proclaim random* random-state-p rassoc* rassoc-if rassoc-if-not
267 reassemble-argslists reduce rem* remove remove* remove-duplicates
268 remove-if remove-if-not remq replace revappend round* safe-idiv search
269 set-difference set-exclusive-or setelt setnth setnthcdr signum some sort*
270 stable-sort sublis subseq subsetp subst subst-if subst-if-not substitute
271 substitute-if substitute-if-not tailp tree-equal truncate* union
272 unzip-lists zip-lists)
273 "A list of CL run-time functions. Some functions were built-in, nowadays.")
276 (defun find-cl-run-time-functions (file-or-directory arg)
277 "Find CL run-time functions in the FILE-OR-DIRECTORY. You can alter
278 the behavior of this command with the prefix ARG as described below.
280 By default, it searches for all the CL run-time functions listed in
281 the variable `cl-run-time-functions'.
282 With 1 or 3 \\[universal-argument]'s, the built-in functions in this Emacs\
285 With 2 or 3 \\[universal-argument]'s, just the symbols will also be reported.
287 You can use the `digit-argument' 1, 2 or 3 instead of\
288 \\[universal-argument]'s."
289 (interactive (list (read-file-name "Find CL run-time functions in: "
290 nil default-directory t)
292 (unless (interactive-p)
293 (error "You should invoke `M-x find-cl-run-time-functions' interactively"))
294 (let ((report-symbols (member arg '((16) (64) 2 3)))
295 files clfns working file lines form forms fns fn newform buffer
297 buffer-file-format format-alist
298 insert-file-contents-post-hook insert-file-contents-pre-hook)
299 (cond ((file-directory-p file-or-directory)
300 (setq files (directory-files file-or-directory t "\\.el$"))
302 (unless (file-exists-p file)
303 (setq files (delete file files))))
305 (message "No files found in: %s" file-or-directory))
307 ((file-exists-p file-or-directory)
308 (setq files (list file-or-directory)))
310 (message "No such file or directory: %s" file-or-directory)))
312 (if (member arg '((4) (64) 1 3))
313 (dolist (fn cl-run-time-functions)
314 (unless (and (fboundp fn)
315 (subrp (symbol-function fn)))
317 (setq clfns cl-run-time-functions))
318 (set-buffer (setq working
320 " *Searching for CL run-time functions*")))
321 (let (emacs-lisp-mode-hook)
324 (setq file (pop files)
325 lines (list nil nil))
326 (message "Searching for CL run-time functions in: %s..."
327 (file-name-nondirectory file))
328 (insert-file-contents file nil nil nil t)
329 ;; XEmacs moves point to the beginning of the buffer after
330 ;; inserting a file, FSFmacs doesn't so if the fifth argument
331 ;; of `insert-file-contents' is specified.
332 (goto-char (point-min))
335 (while (and (looking-at "[\t\v\f\r ]*\\(;.*\\)?$")
336 (zerop (forward-line 1))))
338 (setcar lines (if (bolp)
339 (1+ (count-lines (point-min) (point)))
340 (count-lines (point-min) (point))))
341 (when (consp;; Ignore stand-alone symbols, strings, etc.
342 (setq form (condition-case nil
345 (setcdr lines (list (count-lines (point-min) (point))))
346 (setq forms (list form)
349 (setq form (pop forms))
352 (cond ((memq fn '(apply mapatoms mapcar mapconcat
353 mapextent symbol-function))
354 (if (consp (car form))
355 (when (memq (caar form) '(\` backquote quote))
356 (setcar form (cdar form)))
357 (setq form (cdr form))))
358 ((memq fn '(\` backquote quote))
361 (setq form (car form)
364 (push (list (or (car-safe form) form))
366 (setq form (cdr-safe form)))
367 (setq form (nreverse newform)))
369 ((memq fn '(defadvice
370 defmacro defsubst defun
371 defmacro-maybe defmacro-maybe-cond
372 defsubst-maybe defun-maybe
374 (setq form (cddr form)))
375 ((memq fn '(defalias lambda fset))
376 (setq form (cdr form)))
377 ((eq fn 'define-compiler-macro)
380 (setcar form (cadar form)))
381 ((memq fn '(let let*))
387 (when (and (consp element)
388 (consp (cadr element)))
393 (when (and (consp (cadr form))
394 (memq (caadr form) '(\` backquote quote)))
395 (setcdr form (list (cdadr form)))))
396 ((and (memq fn clfns)
400 (setq forms (append form forms)))))
405 (setq buffer (get-buffer-create
406 (concat "*CL run-time functions in: "
407 file-or-directory "*"))))
410 (setq window (get-buffer-window buffer t)
411 scroll (- 2 (window-height window))
412 fill-column (max 16 (- (window-width window) 2))
421 (mapconcat (lambda (fn) (format "%s" fn))
425 (fill-region (point-min) (point-max))
426 (goto-char (point-min))
429 (insert (format "%5d - %5d:" (car lines) (cadr lines)))
430 (goto-char (point-max))
431 (forward-line scroll)
432 (set-window-start window (point))
433 (goto-char (point-max))
435 (set-buffer working)))))
436 (kill-buffer working)
439 (message "No CL run-time functions found in: %s"
440 file-or-directory)))))
442 (provide 'gnus-clfns)
444 ;;; gnus-clfns.el ends here