* ls.scm (ls, lls): Don't assume (eq? #f '()).
[bpt/guile.git] / ice-9 / ls.scm
1 ;;;; ls.scm --- functions for browsing modules
2 ;;;;
3 ;;;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
19 ;;;;
20 \f
21 (define-module (ice-9 ls)
22 :use-module (ice-9 common-list))
23
24 ;;;;
25 ;;; local-definitions-in root name
26 ;;; Returns a list of names defined locally in the named
27 ;;; subdirectory of root.
28 ;;; definitions-in root name
29 ;;; Returns a list of all names defined in the named
30 ;;; subdirectory of root. The list includes alll locally
31 ;;; defined names as well as all names inherited from a
32 ;;; member of a use-list.
33 ;;;
34 ;;; A convenient interface for examining the nature of things:
35 ;;;
36 ;;; ls . various-names
37 ;;;
38 ;;; With just one argument, interpret that argument as the
39 ;;; name of a subdirectory of the current module and
40 ;;; return a list of names defined there.
41 ;;;
42 ;;; With more than one argument, still compute
43 ;;; subdirectory lists, but return a list:
44 ;;; ((<subdir-name> . <names-defined-there>)
45 ;;; (<subdir-name> . <names-defined-there>)
46 ;;; ...)
47 ;;;
48
49 (define-public (local-definitions-in root names)
50 (let ((m (nested-ref root names))
51 (answer '()))
52 (if (not (module? m))
53 (set! answer m)
54 (module-for-each (lambda (k v) (set! answer (cons k answer))) m))
55 answer))
56
57 (define-public (definitions-in root names)
58 (let ((m (nested-ref root names)))
59 (if (not (module? m))
60 m
61 (reduce union
62 (cons (local-definitions-in m '())
63 (map (lambda (m2) (definitions-in m2 '()))
64 (module-uses m)))))))
65
66 (define-public (ls . various-refs)
67 (and (pair? various-refs)
68 (if (cdr various-refs)
69 (map (lambda (ref)
70 (cons ref (definitions-in (current-module) ref)))
71 various-refs)
72 (definitions-in (current-module) (car various-refs)))))
73
74 (define-public (lls . various-refs)
75 (and (pair? various-refs)
76 (if (cdr various-refs)
77 (map (lambda (ref)
78 (cons ref (local-definitions-in (current-module) ref)))
79 various-refs)
80 (local-definitions-in (current-module) (car various-refs)))))
81
82 (define-public (recursive-local-define name value)
83 (let ((parent (reverse! (cdr (reverse name)))))
84 (and parent (make-modules-in (current-module) parent))
85 (local-define name value)))