Add insults.
[bpt/guile.git] / ice-9 / session.scm
CommitLineData
0e81dabd
MD
1;;;; Copyright (C) 1997 Free Software Foundation, Inc.
2;;;;
3;;;; This program is free software; you can redistribute it and/or modify
4;;;; it under the terms of the GNU General Public License as published by
5;;;; the Free Software Foundation; either version 2, or (at your option)
6;;;; any later version.
7;;;;
8;;;; This program is distributed in the hope that it will be useful,
9;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11;;;; GNU General Public License for more details.
12;;;;
13;;;; You should have received a copy of the GNU General Public License
14;;;; along with this software; see the file COPYING. If not, write to
c6e23ea2
JB
15;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16;;;; Boston, MA 02111-1307 USA
0e81dabd
MD
17;;;;
18\f
19
bbefd480
MD
20(define-module (ice-9 session)
21 :no-backtrace)
0e81dabd
MD
22
23\f
24
25;;; {Apropos}
26;;;
27;;; Author: Roland Orre <orre@nada.kth.se>
28;;;
29
30(define (id x) x)
31
0e81dabd
MD
32(define-public (apropos rgx . options)
33 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
34 (if (zero? (string-length rgx))
35 "Empty string not allowed"
4f161c5c 36 (let* ((match (make-regexp rgx))
0e81dabd
MD
37 (modules (cons (current-module)
38 (module-uses (current-module))))
39 (separator #\tab)
40 (shadow (member 'shadow options))
41 (value (member 'value options)))
42 (cond ((member 'full options)
43 (set! shadow #t)
44 (set! value #t)))
45 (for-each
46 (lambda (module)
47 (let* ((builtin (or (eq? module the-scm-module)
48 (eq? module the-root-module)))
49 (name (module-name module))
50 (obarrays (if builtin
51 (list (builtin-weak-bindings)
52 (builtin-bindings))
53 (list (module-obarray module))))
54 (get-refs (if builtin
55 (list id id)
56 (list variable-ref)))
57 )
58 (for-each
59 (lambda (obarray get-ref)
68aed3ea 60 (array-for-each
0e81dabd
MD
61 (lambda (oblist)
62 (for-each
63 (lambda (x)
4f161c5c 64 (cond ((regexp-exec match (car x))
0e81dabd
MD
65 (display name)
66 (display ": ")
67 (display (car x))
68 (cond ((procedure? (get-ref (cdr x)))
69 (display separator)
70 (display (get-ref (cdr x))))
71 (value
72 (display separator)
73 (display (get-ref (cdr x)))))
74 (if (and shadow
75 (not (eq? (module-ref module
76 (car x))
77 (module-ref (current-module)
78 (car x)))))
79 (display " shadowed"))
80 (newline)
81 )))
82 oblist))
83 obarray))
84 obarrays get-refs)))
85 modules))))
68aed3ea
MD
86
87(define-public (apropos-internal rgx)
88 "Return a list of accessible variable names."
bbefd480
MD
89 (letrec ((match (make-regexp rgx))
90 (recorded (make-vector 61 '()))
91 (obarray-names
92 (lambda (obarray names)
70558403 93 (hash-fold (lambda (name var vars)
bbefd480
MD
94 (if (and (regexp-exec match name)
95 (not (hashq-get-handle recorded name)))
96 (begin
97 (hashq-set! recorded name #t)
98 (cons name vars))
99 vars))
70558403
MD
100 names
101 obarray))))
bbefd480
MD
102 (do ((modules (cons (current-module) (module-uses (current-module)))
103 (cdr modules))
104 (names '()
105 (if (or (eq? (car modules) the-scm-module)
106 (eq? (car modules) the-root-module))
107 (obarray-names (builtin-weak-bindings)
108 (obarray-names (builtin-bindings)
109 names))
110 (obarray-names (module-obarray (car modules))
111 names))))
112 ((null? modules) names))))
7cfae7e6
MD
113
114(define-public (name obj)
115 (cond ((procedure? obj) (procedure-name obj))
116 ((macro? obj) (macro-name obj))
117 (else #f)))
118
119(define-public (source obj)
120 (cond ((procedure? obj) (procedure-source obj))
121 ((macro? obj) (procedure-source (macro-transformer obj)))
122 (else #f)))
4a9f464e
MD
123
124(define-public (arity obj)
125 (let ((arity (procedure-property obj 'arity)))
126 (display (car arity))
127 (cond ((caddr arity)
128 (display " or more"))
129 ((not (zero? (cadr arity)))
130 (display " required and ")
131 (display (cadr arity))
132 (display " optional")))
133 (if (and (not (caddr arity))
134 (= (car arity) 1)
135 (<= (cadr arity) 1))
136 (display " argument")
137 (display " arguments"))
138 (if (closure? obj)
139 (let ((formals (cadr (procedure-source obj))))
140 (if (pair? formals)
141 (begin
142 (display ": `")
143 (display (car formals))
144 (let loop ((ls (cdr formals)))
145 (cond ((null? ls)
146 (display #\'))
147 ((not (pair? ls))
148 (display "', the rest in `")
149 (display ls)
150 (display #\'))
151 (else
152 (if (pair? (cdr ls))
153 (display "', `")
154 (display "' and `"))
155 (display (car ls))
156 (loop (cdr ls))))))
157 (begin
158 (display " in `")
159 (display formals)
160 (display #\')))))
161 (display ".\n")))
6ae34994
MD
162
163(define-public system-module
164 (procedure->syntax
165 (lambda (exp env)
166 (let* ((m (nested-ref the-root-module
167 (append '(app modules) (cadr exp)))))
168 (if (not m)
169 (error "Couldn't find any module named" (cadr exp)))
170 (let ((s (not (procedure-property (module-eval-closure m)
171 'system-module))))
172 (set-system-module! m s)
173 (string-append "Module " (symbol->string (module-name m))
174 " is now a " (if s "system" "user") " module."))))))