* boot-9.scm (duplicate-handlers): Make sure the merge-generics
[bpt/guile.git] / ice-9 / session.scm
CommitLineData
1798b73d 1;;;; Copyright (C) 1997, 2000, 2001, 2003 Free Software Foundation, Inc.
7bb1bfc2 2;;;;
0e81dabd
MD
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.
7bb1bfc2 7;;;;
0e81dabd
MD
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.
7bb1bfc2 12;;;;
0e81dabd
MD
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
7bb1bfc2 17;;;;
a482f2cc
MV
18;;;; As a special exception, the Free Software Foundation gives permission
19;;;; for additional uses of the text contained in its release of GUILE.
20;;;;
21;;;; The exception is that, if you link the GUILE library with other files
22;;;; to produce an executable, this does not by itself cause the
23;;;; resulting executable to be covered by the GNU General Public License.
24;;;; Your use of that executable is in no way restricted on account of
25;;;; linking the GUILE library code into it.
26;;;;
27;;;; This exception does not however invalidate any other reasons why
28;;;; the executable file might be covered by the GNU General Public License.
29;;;;
30;;;; This exception applies only to the code released by the
31;;;; Free Software Foundation under the name GUILE. If you copy
32;;;; code from other Free Software Foundation releases into a copy of
33;;;; GUILE, as the General Public License permits, the exception does
34;;;; not apply to the code that you add in this way. To avoid misleading
35;;;; anyone as to the status of such modified files, you must delete
36;;;; this exception notice from them.
37;;;;
38;;;; If you write modifications of your own for GUILE, it is your choice
39;;;; whether to permit this exception to apply to your modifications.
40;;;; If you do not wish that, delete this exception notice.
41;;;;
0e81dabd
MD
42\f
43
bbefd480 44(define-module (ice-9 session)
3bdca000 45 :use-module (ice-9 documentation)
3510b484 46 :use-module (ice-9 regex)
1a179b03
MD
47 :use-module (ice-9 rdelim)
48 :export (help apropos apropos-internal apropos-fold
49 apropos-fold-accessible apropos-fold-exported apropos-fold-all
50 source arity system-module))
0e81dabd
MD
51
52\f
53
13ae9151
MD
54;;; Documentation
55;;;
1a179b03 56(define help
13ae9151
MD
57 (procedure->syntax
58 (lambda (exp env)
59 "(help [NAME])
60Prints useful information. Try `(help)'."
9f0eee46 61 (cond ((not (= (length exp) 2))
de25f281
TTN
62 (help-usage))
63 ((not (provided? 'regex))
64 (display "`help' depends on the `regex' feature.
9f0eee46 65You don't seem to have regular expressions installed.\n"))
de25f281
TTN
66 (else
67 (let ((name (cadr exp))
68 (not-found (lambda (type x)
69 (simple-format #t "No ~A found for ~A\n"
70 type x))))
71 (cond
72
73 ;; SYMBOL
74 ((symbol? name)
75 (help-doc name
76 (simple-format
77 #f "^~A$"
78 (regexp-quote (symbol->string name)))))
79
80 ;; "STRING"
81 ((string? name)
82 (help-doc name name))
83
84 ;; (unquote SYMBOL)
85 ((and (list? name)
86 (= (length name) 2)
87 (eq? (car name) 'unquote))
88 (cond ((object-documentation
89 (local-eval (cadr name) env))
90 => write-line)
91 (else (not-found 'documentation (cadr name)))))
92
93 ;; (quote SYMBOL)
94 ((and (list? name)
95 (= (length name) 2)
96 (eq? (car name) 'quote)
97 (symbol? (cadr name)))
98 (cond ((search-documentation-files (cadr name))
99 => write-line)
100 (else (not-found 'documentation (cadr name)))))
101
102 ;; (SYM1 SYM2 ...)
103 ((and (list? name)
104 (and-map symbol? name)
105 (not (null? name))
106 (not (eq? (car name) 'quote)))
107 (cond ((module-commentary name)
108 => (lambda (doc)
109 (display name) (write-line " commentary:")
110 (write-line doc)))
111 (else (not-found 'commentary name))))
112
113 ;; unrecognized
114 (else
115 (help-usage)))
116 *unspecified*))))))
13ae9151 117
7bb1bfc2
TTN
118(define (module-filename name) ; fixme: better way? / done elsewhere?
119 (let* ((name (map symbol->string name))
120 (reverse-name (reverse name))
121 (leaf (car reverse-name))
122 (dir-hint-module-name (reverse (cdr reverse-name)))
123 (dir-hint (apply string-append
124 (map (lambda (elt)
125 (string-append elt "/"))
126 dir-hint-module-name))))
127 (%search-load-path (in-vicinity dir-hint leaf))))
128
129(define (module-commentary name)
130 (cond ((module-filename name) => file-commentary)
131 (else #f)))
132
3bdca000
MD
133(define (help-doc term regexp)
134 (let ((entries (apropos-fold (lambda (module name object data)
135 (cons (list module
136 name
db611983
NJ
137 (object-documentation object)
138 (cond ((closure? object)
139 "a procedure")
140 ((procedure? object)
141 "a primitive procedure")
142 (else
143 "an object")))
3bdca000
MD
144 data))
145 '()
146 regexp
147 apropos-fold-exported))
148 (module car)
149 (name cadr)
db611983
NJ
150 (doc caddr)
151 (type cadddr))
de25f281
TTN
152 (cond ((not (null? entries))
153 (let ((first? #t)
154 (undocumented-entries '())
155 (documented-entries '())
156 (documentations '()))
157
158 (for-each (lambda (entry)
159 (let ((entry-summary (simple-format
160 #f "~S: ~S\n"
161 (module-name (module entry))
162 (name entry))))
163 (if (doc entry)
164 (begin
165 (set! documented-entries
166 (cons entry-summary documented-entries))
167 ;; *fixme*: Use `describe' when we have GOOPS?
168 (set! documentations
169 (cons (simple-format
170 #f "`~S' is ~A in the ~S module.\n\n~A\n"
171 (name entry)
172 (type entry)
173 (module-name (module entry))
174 (doc entry))
175 documentations)))
176 (set! undocumented-entries
177 (cons entry-summary
178 undocumented-entries)))))
179 entries)
180
181 (if (and (not (null? documented-entries))
182 (or (> (length documented-entries) 1)
183 (not (null? undocumented-entries))))
184 (begin
185 (display "Documentation found for:\n")
186 (for-each (lambda (entry) (display entry))
187 documented-entries)
188 (set! first? #f)))
189
190 (for-each (lambda (entry)
191 (if first?
192 (set! first? #f)
193 (newline))
194 (display entry))
195 documentations)
196
197 (if (not (null? undocumented-entries))
198 (begin
199 (if first?
200 (set! first? #f)
201 (newline))
202 (display "No documentation found for:\n")
203 (for-each (lambda (entry) (display entry))
204 undocumented-entries)))))
205 ((search-documentation-files term)
206 => (lambda (doc)
207 (write-line "Documentation from file:")
208 (write-line doc)))
209 (else
210 ;; no matches
211 (display "Did not find any object ")
212 (simple-format #t
213 (if (symbol? term)
214 "named `~A'\n"
215 "matching regexp \"~A\"\n")
216 term)))))
3bdca000 217
13ae9151 218(define (help-usage)
3bdca000
MD
219 (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
220 (help REGEXP) ditto for objects with names matching REGEXP (a string)
1de3b33b 221 (help 'NAME) gives documentation for NAME, even if it is not an object
d1c50f73 222 (help ,EXPR) gives documentation for object returned by EXPR
8bbe4c82 223 (help (my module)) gives module commentary for `(my module)'
13ae9151
MD
224 (help) gives this text
225
3bdca000
MD
226`help' searches among bindings exported from loaded modules, while
227`apropos' searches among bindings visible from the \"current\" module.
228
2f52380c
MD
229Examples: (help help)
230 (help cons)
3bdca000 231 (help \"output-string\")
13ae9151
MD
232
233Other useful sources of helpful information:
234
235(apropos STRING)
236(arity PROCEDURE)
237(name PROCEDURE-OR-MACRO)
238(source PROCEDURE-OR-MACRO)
239
240Tools:
241
2f52380c
MD
242(backtrace) ;show backtrace from last error
243(debug) ;enter the debugger
244(trace [PROCEDURE]) ;trace procedure (no arg => show)
245(untrace [PROCEDURE]) ;untrace (no arg => untrace all)
13ae9151
MD
246
247(OPTIONSET-options 'full) ;display option information
248(OPTIONSET-enable 'OPTION)
249(OPTIONSET-disable 'OPTION)
250(OPTIONSET-set! OPTION VALUE)
251
252where OPTIONSET is one of debug, read, eval, print
253
254"))
255
0e81dabd
MD
256;;; {Apropos}
257;;;
258;;; Author: Roland Orre <orre@nada.kth.se>
259;;;
260
1a179b03 261(define (apropos rgx . options)
0e81dabd
MD
262 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
263 (if (zero? (string-length rgx))
264 "Empty string not allowed"
4f161c5c 265 (let* ((match (make-regexp rgx))
0e81dabd
MD
266 (modules (cons (current-module)
267 (module-uses (current-module))))
268 (separator #\tab)
269 (shadow (member 'shadow options))
270 (value (member 'value options)))
271 (cond ((member 'full options)
272 (set! shadow #t)
273 (set! value #t)))
274 (for-each
275 (lambda (module)
296ff5e7
MV
276 (let* ((name (module-name module))
277 (obarray (module-obarray module)))
278 ;; XXX - should use hash-fold here
1798b73d
MD
279 (hash-for-each
280 (lambda (symbol variable)
281 (cond ((regexp-exec match (symbol->string symbol))
282 (display name)
283 (display ": ")
284 (display symbol)
285 (cond ((variable-bound? variable)
286 (let ((val (variable-ref variable)))
287 (cond ((or (procedure? val) value)
288 (display separator)
289 (display val)))))
290 (else
291 (display separator)
292 (display "(unbound)")))
293 (if (and shadow
294 (not (eq? (module-ref module symbol)
295 (module-ref (current-module) symbol))))
296 (display " shadowed"))
297 (newline))))
10764e3c 298 obarray)))
0e81dabd 299 modules))))
68aed3ea 300
1a179b03 301(define (apropos-internal rgx)
68aed3ea 302 "Return a list of accessible variable names."
3bdca000
MD
303 (apropos-fold (lambda (module name var data)
304 (cons name data))
305 '()
306 rgx
307 (apropos-fold-accessible (current-module))))
308
1a179b03 309(define (apropos-fold proc init rgx folder)
3bdca000
MD
310 "Folds PROCEDURE over bindings matching third arg REGEXP.
311
312Result is
313
314 (PROCEDURE MODULE1 NAME1 VALUE1
315 (PROCEDURE MODULE2 NAME2 VALUE2
316 ...
317 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
318
319where INIT is the second arg to `apropos-fold'.
320
321Fourth arg FOLDER is one of
322
323 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
324 apropos-fold-exported ;fold over all exported bindings
325 apropos-fold-all ;fold over all bindings"
326 (let ((match (make-regexp rgx))
327 (recorded (make-vector 61 '())))
328 (let ((fold-module
329 (lambda (module data)
330 (let* ((obarray-filter
331 (lambda (name val data)
4adc3028 332 (if (and (regexp-exec match (symbol->string name))
3bdca000
MD
333 (not (hashq-get-handle recorded name)))
334 (begin
335 (hashq-set! recorded name #t)
336 (proc module name val data))
337 data)))
338 (module-filter
339 (lambda (name var data)
aef9dd65
MV
340 (if (variable-bound? var)
341 (obarray-filter name (variable-ref var) data)
342 data))))
296ff5e7 343 (cond (module (hash-fold module-filter
3bdca000
MD
344 data
345 (module-obarray module)))
346 (else data))))))
347 (folder fold-module init))))
348
349(define (make-fold-modules init-thunk traverse extract)
350 "Return procedure capable of traversing a forest of modules.
351The forest traversed is the image of the forest generated by root
352modules returned by INIT-THUNK and the generator TRAVERSE.
353It is an image under the mapping EXTRACT."
354 (lambda (fold-module init)
9aec4751
MD
355 (let* ((table (make-hash-table 31))
356 (first? (lambda (obj)
8d627248
MD
357 (let* ((handle (hash-create-handle! table obj #t))
358 (first? (cdr handle)))
359 (set-cdr! handle #f)
360 first?))))
9aec4751
MD
361 (let rec ((data init)
362 (modules (init-thunk)))
363 (do ((modules modules (cdr modules))
364 (data data (if (first? (car modules))
365 (rec (fold-module (extract (car modules)) data)
366 (traverse (car modules)))
367 data)))
368 ((null? modules) data))))))
3bdca000 369
1a179b03 370(define (apropos-fold-accessible module)
3bdca000
MD
371 (make-fold-modules (lambda () (list module))
372 module-uses
de25f281 373 identity))
3bdca000
MD
374
375(define (root-modules)
376 (cons the-root-module
377 (submodules (nested-ref the-root-module '(app modules)))))
378
379(define (submodules m)
380 (hash-fold (lambda (name var data)
aef9dd65 381 (let ((obj (and (variable-bound? var) (variable-ref var))))
3bdca000
MD
382 (if (and (module? obj)
383 (eq? (module-kind obj) 'directory))
384 (cons obj data)
385 data)))
386 '()
387 (module-obarray m)))
388
1a179b03 389(define apropos-fold-exported
3bdca000
MD
390 (make-fold-modules root-modules submodules module-public-interface))
391
1a179b03 392(define apropos-fold-all
de25f281 393 (make-fold-modules root-modules submodules identity))
7cfae7e6 394
1a179b03 395(define (source obj)
7cfae7e6
MD
396 (cond ((procedure? obj) (procedure-source obj))
397 ((macro? obj) (procedure-source (macro-transformer obj)))
398 (else #f)))
4a9f464e 399
1a179b03 400(define (arity obj)
c7bb434f
TTN
401 (define (display-arg-list arg-list)
402 (display #\`)
403 (display (car arg-list))
404 (let loop ((ls (cdr arg-list)))
405 (cond ((null? ls)
406 (display #\'))
407 ((not (pair? ls))
408 (display "', the rest in `")
409 (display ls)
410 (display #\'))
411 (else
412 (if (pair? (cdr ls))
413 (display "', `")
414 (display "' and `"))
415 (display (car ls))
416 (loop (cdr ls))))))
417 (define (display-arg-list/summary arg-list type)
418 (let ((len (length arg-list)))
419 (display len)
420 (display " ")
421 (display type)
422 (if (> len 1)
423 (display " arguments: ")
424 (display " argument: "))
425 (display-arg-list arg-list)))
426 (cond
427 ((procedure-property obj 'arglist)
428 => (lambda (arglist)
429 (let ((required-args (car arglist))
430 (optional-args (cadr arglist))
431 (keyword-args (caddr arglist))
432 (allow-other-keys? (cadddr arglist))
433 (rest-arg (car (cddddr arglist)))
434 (need-punctuation #f))
435 (cond ((not (null? required-args))
436 (display-arg-list/summary required-args "required")
437 (set! need-punctuation #t)))
438 (cond ((not (null? optional-args))
439 (if need-punctuation (display ", "))
440 (display-arg-list/summary optional-args "optional")
441 (set! need-punctuation #t)))
442 (cond ((not (null? keyword-args))
443 (if need-punctuation (display ", "))
444 (display-arg-list/summary keyword-args "keyword")
445 (set! need-punctuation #t)))
446 (cond (allow-other-keys?
447 (if need-punctuation (display ", "))
448 (display "other keywords allowed")
449 (set! need-punctuation #t)))
450 (cond (rest-arg
451 (if need-punctuation (display ", "))
452 (display "the rest in `")
453 (display rest-arg)
454 (display "'"))))))
455 (else
456 (let ((arity (procedure-property obj 'arity)))
457 (display (car arity))
458 (cond ((caddr arity)
459 (display " or more"))
460 ((not (zero? (cadr arity)))
461 (display " required and ")
462 (display (cadr arity))
463 (display " optional")))
464 (if (and (not (caddr arity))
465 (= (car arity) 1)
466 (<= (cadr arity) 1))
467 (display " argument")
468 (display " arguments"))
469 (if (closure? obj)
470 (let ((formals (cadr (procedure-source obj))))
471 (cond
472 ((pair? formals)
473 (display ": ")
474 (display-arg-list formals))
475 (else
476 (display " in `")
477 (display formals)
478 (display #\'))))))))
479 (display ".\n"))
6ae34994 480
1a179b03 481(define system-module
6ae34994
MD
482 (procedure->syntax
483 (lambda (exp env)
484 (let* ((m (nested-ref the-root-module
485 (append '(app modules) (cadr exp)))))
486 (if (not m)
487 (error "Couldn't find any module named" (cadr exp)))
488 (let ((s (not (procedure-property (module-eval-closure m)
489 'system-module))))
490 (set-system-module! m s)
491 (string-append "Module " (symbol->string (module-name m))
492 " is now a " (if s "system" "user") " module."))))))
de25f281
TTN
493
494;;; session.scm ends here