| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2008,2009 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
(library (ikarus.apropos) |
| 19 |
(export apropos) |
| 20 |
(import |
| 21 |
(except (ikarus) apropos) |
| 22 |
(only (psyntax library-manager) library-subst library-name)) |
| 23 |
|
| 24 |
(define (compose f g) |
| 25 |
(lambda (x) (f (g x)))) |
| 26 |
|
| 27 |
(define (match-maker s1) |
| 28 |
(let ([n1 (string-length s1)]) |
| 29 |
(lambda (s2) |
| 30 |
(let ([m (fx- (string-length s2) n1)]) |
| 31 |
(let f ([i2 0]) |
| 32 |
(and (fx<=? i2 m) |
| 33 |
(or (let g ([i1 0] [i2 i2]) |
| 34 |
(or (fx= i1 n1) |
| 35 |
(and (char=? (string-ref s1 i1) |
| 36 |
(string-ref s2 i2)) |
| 37 |
(g (fx+ i1 1) (fx+ i2 1))))) |
| 38 |
(f (fx+ i2 1))))))))) |
| 39 |
|
| 40 |
(define ($apropos-list name who) |
| 41 |
(let ([name |
| 42 |
(cond |
| 43 |
[(string? name) name] |
| 44 |
[(symbol? name) (symbol->string name)] |
| 45 |
[else |
| 46 |
(die who "not a string or symbol" name)])]) |
| 47 |
(define matcher |
| 48 |
(compose (match-maker name) |
| 49 |
(compose symbol->string car))) |
| 50 |
(fold-right |
| 51 |
(lambda (lib rest) |
| 52 |
(define (symbol<? s1 s2) |
| 53 |
(string<? (symbol->string s1) (symbol->string s2))) |
| 54 |
(let ([ls (filter matcher (library-subst lib))]) |
| 55 |
(if (null? ls) |
| 56 |
rest |
| 57 |
(let ([ls (list-sort symbol<? (map car ls))]) |
| 58 |
(cons (cons (library-name lib) ls) rest))))) |
| 59 |
'() |
| 60 |
(list-sort |
| 61 |
(lambda (lib1 lib2) |
| 62 |
(let f ([ls1 (library-name lib1)] [ls2 (library-name lib2)]) |
| 63 |
(and (pair? ls2) |
| 64 |
(or (null? ls1) |
| 65 |
(let ([s1 (symbol->string (car ls1))] |
| 66 |
[s2 (symbol->string (car ls2))]) |
| 67 |
(or (string<? s1 s2) |
| 68 |
(and (string=? s1 s2) |
| 69 |
(f (cdr ls1) (cdr ls2))))))))) |
| 70 |
(installed-libraries))))) |
| 71 |
|
| 72 |
(define (apropos name) |
| 73 |
(for-each |
| 74 |
(lambda (x) (printf "~a:\n ~a\n" (car x) (cdr x))) |
| 75 |
($apropos-list name 'apropos)))) |
| 76 |
|
| 77 |
|