1 ;;;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
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.
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.
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
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
18 ;;;; As a special exception, the Free Software Foundation gives permission
19 ;;;; for additional uses of the text contained in its release of GUILE.
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.
27 ;;;; This exception does not however invalidate any other reasons why
28 ;;;; the executable file might be covered by the GNU General Public License.
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.
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.
44 (define-module (ice-9 session)
45 :use-module (ice-9 documentation)
46 :use-module (ice-9 regex)
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))
60 Prints useful information. Try `(help)'."
61 (cond ((not (= (length exp) 2))
63 ((not (provided? 'regex))
64 (display "`help' depends on the `regex' feature.
65 You don't seem to have regular expressions installed.\n"))
67 (let ((name (cadr exp))
68 (not-found (lambda (type x)
69 (simple-format #t "No ~A found for ~A\n"
78 (regexp-quote (symbol->string name)))))
87 (eq? (car name) 'unquote))
88 (cond ((object-documentation
89 (local-eval (cadr name) env))
91 (else (not-found 'documentation (cadr name)))))
96 (eq? (car name) 'quote)
97 (symbol? (cadr name)))
98 (cond ((search-documentation-files (cadr name))
100 (else (not-found 'documentation (cadr name)))))
104 (and-map symbol? name)
106 (not (eq? (car name) 'quote)))
107 (cond ((module-commentary name)
109 (display name) (write-line " commentary:")
111 (else (not-found 'commentary name))))
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
125 (string-append elt "/"))
126 dir-hint-module-name))))
127 (%search-load-path (in-vicinity dir-hint leaf))))
129 (define (module-commentary name)
130 (cond ((module-filename name) => file-commentary)
133 (define (help-doc term regexp)
134 (let ((entries (apropos-fold (lambda (module name object data)
137 (object-documentation object)
138 (cond ((closure? object)
141 "a primitive procedure")
147 apropos-fold-exported))
152 (cond ((not (null? entries))
154 (undocumented-entries '())
155 (documented-entries '())
156 (documentations '()))
158 (for-each (lambda (entry)
159 (let ((entry-summary (simple-format
161 (module-name (module entry))
165 (set! documented-entries
166 (cons entry-summary documented-entries))
167 ;; *fixme*: Use `describe' when we have GOOPS?
170 #f "`~S' is ~A in the ~S module.\n\n~A\n"
173 (module-name (module entry))
176 (set! undocumented-entries
178 undocumented-entries)))))
181 (if (and (not (null? documented-entries))
182 (or (> (length documented-entries) 1)
183 (not (null? undocumented-entries))))
185 (display "Documentation found for:\n")
186 (for-each (lambda (entry) (display entry))
190 (for-each (lambda (entry)
197 (if (not (null? undocumented-entries))
202 (display "No documentation found for:\n")
203 (for-each (lambda (entry) (display entry))
204 undocumented-entries)))))
205 ((search-documentation-files term)
207 (write-line "Documentation from file:")
211 (display "Did not find any object ")
215 "matching regexp \"~A\"\n")
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)
221 (help 'NAME) gives documentation for NAME, even if it is not an object
222 (help ,EXPR) gives documentation for object returned by EXPR
223 (help (my module)) gives module commentary for `(my module)'
224 (help) gives this text
226 `help' searches among bindings exported from loaded modules, while
227 `apropos' searches among bindings visible from the \"current\" module.
229 Examples: (help help)
231 (help \"output-string\")
233 Other useful sources of helpful information:
237 (name PROCEDURE-OR-MACRO)
238 (source PROCEDURE-OR-MACRO)
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)
247 (OPTIONSET-options 'full) ;display option information
248 (OPTIONSET-enable 'OPTION)
249 (OPTIONSET-disable 'OPTION)
250 (OPTIONSET-set! OPTION VALUE)
252 where OPTIONSET is one of debug, read, eval, print
258 ;;; Author: Roland Orre <orre@nada.kth.se>
261 (define (apropos rgx . options)
262 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
263 (if (zero? (string-length rgx))
264 "Empty string not allowed"
265 (let* ((match (make-regexp rgx))
266 (modules (cons (current-module)
267 (module-uses (current-module))))
269 (shadow (member 'shadow options))
270 (value (member 'value options)))
271 (cond ((member 'full options)
276 (let* ((name (module-name module))
277 (obarray (module-obarray module)))
278 ;; XXX - should use hash-fold here
283 (cond ((regexp-exec match (symbol->string (car x)))
287 (cond ((variable-bound? (cdr x))
288 (let ((val (variable-ref (cdr x))))
289 (cond ((or (procedure? val) value)
294 (display "(unbound)")))
296 (not (eq? (module-ref module
298 (module-ref (current-module)
300 (display " shadowed"))
306 (define (apropos-internal rgx)
307 "Return a list of accessible variable names."
308 (apropos-fold (lambda (module name var data)
312 (apropos-fold-accessible (current-module))))
314 (define (apropos-fold proc init rgx folder)
315 "Folds PROCEDURE over bindings matching third arg REGEXP.
319 (PROCEDURE MODULE1 NAME1 VALUE1
320 (PROCEDURE MODULE2 NAME2 VALUE2
322 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
324 where INIT is the second arg to `apropos-fold'.
326 Fourth arg FOLDER is one of
328 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
329 apropos-fold-exported ;fold over all exported bindings
330 apropos-fold-all ;fold over all bindings"
331 (let ((match (make-regexp rgx))
332 (recorded (make-vector 61 '())))
334 (lambda (module data)
335 (let* ((obarray-filter
336 (lambda (name val data)
337 (if (and (regexp-exec match (symbol->string name))
338 (not (hashq-get-handle recorded name)))
340 (hashq-set! recorded name #t)
341 (proc module name val data))
344 (lambda (name var data)
345 (if (variable-bound? var)
346 (obarray-filter name (variable-ref var) data)
348 (cond (module (hash-fold module-filter
350 (module-obarray module)))
352 (folder fold-module init))))
354 (define (make-fold-modules init-thunk traverse extract)
355 "Return procedure capable of traversing a forest of modules.
356 The forest traversed is the image of the forest generated by root
357 modules returned by INIT-THUNK and the generator TRAVERSE.
358 It is an image under the mapping EXTRACT."
359 (lambda (fold-module init)
360 (let* ((table (make-hash-table 31))
361 (first? (lambda (obj)
362 (let* ((handle (hash-create-handle! table obj #t))
363 (first? (cdr handle)))
366 (let rec ((data init)
367 (modules (init-thunk)))
368 (do ((modules modules (cdr modules))
369 (data data (if (first? (car modules))
370 (rec (fold-module (extract (car modules)) data)
371 (traverse (car modules)))
373 ((null? modules) data))))))
375 (define (apropos-fold-accessible module)
376 (make-fold-modules (lambda () (list module))
380 (define (root-modules)
381 (cons the-root-module
382 (submodules (nested-ref the-root-module '(app modules)))))
384 (define (submodules m)
385 (hash-fold (lambda (name var data)
386 (let ((obj (and (variable-bound? var) (variable-ref var))))
387 (if (and (module? obj)
388 (eq? (module-kind obj) 'directory))
394 (define apropos-fold-exported
395 (make-fold-modules root-modules submodules module-public-interface))
397 (define apropos-fold-all
398 (make-fold-modules root-modules submodules identity))
401 (cond ((procedure? obj) (procedure-source obj))
402 ((macro? obj) (procedure-source (macro-transformer obj)))
406 (define (display-arg-list arg-list)
408 (display (car arg-list))
409 (let loop ((ls (cdr arg-list)))
413 (display "', the rest in `")
422 (define (display-arg-list/summary arg-list type)
423 (let ((len (length arg-list)))
428 (display " arguments: ")
429 (display " argument: "))
430 (display-arg-list arg-list)))
432 ((procedure-property obj 'arglist)
434 (let ((required-args (car arglist))
435 (optional-args (cadr arglist))
436 (keyword-args (caddr arglist))
437 (allow-other-keys? (cadddr arglist))
438 (rest-arg (car (cddddr arglist)))
439 (need-punctuation #f))
440 (cond ((not (null? required-args))
441 (display-arg-list/summary required-args "required")
442 (set! need-punctuation #t)))
443 (cond ((not (null? optional-args))
444 (if need-punctuation (display ", "))
445 (display-arg-list/summary optional-args "optional")
446 (set! need-punctuation #t)))
447 (cond ((not (null? keyword-args))
448 (if need-punctuation (display ", "))
449 (display-arg-list/summary keyword-args "keyword")
450 (set! need-punctuation #t)))
451 (cond (allow-other-keys?
452 (if need-punctuation (display ", "))
453 (display "other keywords allowed")
454 (set! need-punctuation #t)))
456 (if need-punctuation (display ", "))
457 (display "the rest in `")
461 (let ((arity (procedure-property obj 'arity)))
462 (display (car arity))
464 (display " or more"))
465 ((not (zero? (cadr arity)))
466 (display " required and ")
467 (display (cadr arity))
468 (display " optional")))
469 (if (and (not (caddr arity))
472 (display " argument")
473 (display " arguments"))
475 (let ((formals (cadr (procedure-source obj))))
479 (display-arg-list formals))
486 (define system-module
489 (let* ((m (nested-ref the-root-module
490 (append '(app modules) (cadr exp)))))
492 (error "Couldn't find any module named" (cadr exp)))
493 (let ((s (not (procedure-property (module-eval-closure m)
495 (set-system-module! m s)
496 (string-append "Module " (symbol->string (module-name m))
497 " is now a " (if s "system" "user") " module."))))))
499 ;;; session.scm ends here