1 ;;;; Copyright (C) 1997, 2000, 2001, 2003 Free Software Foundation, Inc.
3 ;;;; This library is free software; you can redistribute it and/or
4 ;;;; modify it under the terms of the GNU Lesser General Public
5 ;;;; License as published by the Free Software Foundation; either
6 ;;;; version 2.1 of the License, or (at your option) any later version.
8 ;;;; This library 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 GNU
11 ;;;; Lesser General Public License for more details.
13 ;;;; You should have received a copy of the GNU Lesser General Public
14 ;;;; License along with this library; if not, write to the Free Software
15 ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 (define-module (ice-9 session)
20 :use-module (ice-9 documentation)
21 :use-module (ice-9 regex)
22 :use-module (ice-9 rdelim)
23 :export (help apropos apropos-internal apropos-fold
24 apropos-fold-accessible apropos-fold-exported apropos-fold-all
25 source arity system-module))
35 Prints useful information. Try `(help)'."
36 (cond ((not (= (length exp) 2))
38 ((not (provided? 'regex))
39 (display "`help' depends on the `regex' feature.
40 You don't seem to have regular expressions installed.\n"))
42 (let ((name (cadr exp))
43 (not-found (lambda (type x)
44 (simple-format #t "No ~A found for ~A\n"
53 (regexp-quote (symbol->string name)))))
62 (eq? (car name) 'unquote))
63 (cond ((object-documentation
64 (local-eval (cadr name) env))
66 (else (not-found 'documentation (cadr name)))))
71 (eq? (car name) 'quote)
72 (symbol? (cadr name)))
73 (cond ((search-documentation-files (cadr name))
75 (else (not-found 'documentation (cadr name)))))
79 (and-map symbol? name)
81 (not (eq? (car name) 'quote)))
82 (cond ((module-commentary name)
84 (display name) (write-line " commentary:")
86 (else (not-found 'commentary name))))
93 (define (module-filename name) ; fixme: better way? / done elsewhere?
94 (let* ((name (map symbol->string name))
95 (reverse-name (reverse name))
96 (leaf (car reverse-name))
97 (dir-hint-module-name (reverse (cdr reverse-name)))
98 (dir-hint (apply string-append
100 (string-append elt "/"))
101 dir-hint-module-name))))
102 (%search-load-path (in-vicinity dir-hint leaf))))
104 (define (module-commentary name)
105 (cond ((module-filename name) => file-commentary)
108 (define (help-doc term regexp)
109 (let ((entries (apropos-fold (lambda (module name object data)
112 (object-documentation object)
113 (cond ((closure? object)
116 "a primitive procedure")
122 apropos-fold-exported))
127 (cond ((not (null? entries))
129 (undocumented-entries '())
130 (documented-entries '())
131 (documentations '()))
133 (for-each (lambda (entry)
134 (let ((entry-summary (simple-format
136 (module-name (module entry))
140 (set! documented-entries
141 (cons entry-summary documented-entries))
142 ;; *fixme*: Use `describe' when we have GOOPS?
145 #f "`~S' is ~A in the ~S module.\n\n~A\n"
148 (module-name (module entry))
151 (set! undocumented-entries
153 undocumented-entries)))))
156 (if (and (not (null? documented-entries))
157 (or (> (length documented-entries) 1)
158 (not (null? undocumented-entries))))
160 (display "Documentation found for:\n")
161 (for-each (lambda (entry) (display entry))
165 (for-each (lambda (entry)
172 (if (not (null? undocumented-entries))
177 (display "No documentation found for:\n")
178 (for-each (lambda (entry) (display entry))
179 undocumented-entries)))))
180 ((search-documentation-files term)
182 (write-line "Documentation from file:")
186 (display "Did not find any object ")
190 "matching regexp \"~A\"\n")
194 (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
195 (help REGEXP) ditto for objects with names matching REGEXP (a string)
196 (help 'NAME) gives documentation for NAME, even if it is not an object
197 (help ,EXPR) gives documentation for object returned by EXPR
198 (help (my module)) gives module commentary for `(my module)'
199 (help) gives this text
201 `help' searches among bindings exported from loaded modules, while
202 `apropos' searches among bindings visible from the \"current\" module.
204 Examples: (help help)
206 (help \"output-string\")
208 Other useful sources of helpful information:
212 (name PROCEDURE-OR-MACRO)
213 (source PROCEDURE-OR-MACRO)
217 (backtrace) ;show backtrace from last error
218 (debug) ;enter the debugger
219 (trace [PROCEDURE]) ;trace procedure (no arg => show)
220 (untrace [PROCEDURE]) ;untrace (no arg => untrace all)
222 (OPTIONSET-options 'full) ;display option information
223 (OPTIONSET-enable 'OPTION)
224 (OPTIONSET-disable 'OPTION)
225 (OPTIONSET-set! OPTION VALUE)
227 where OPTIONSET is one of debug, read, eval, print
233 ;;; Author: Roland Orre <orre@nada.kth.se>
236 (define (apropos rgx . options)
237 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
238 (if (zero? (string-length rgx))
239 "Empty string not allowed"
240 (let* ((match (make-regexp rgx))
241 (uses (module-uses (current-module)))
242 (modules (cons (current-module)
243 (if (and (not (null? uses))
244 (eq? (module-name (car uses))
249 (shadow (member 'shadow options))
250 (value (member 'value options)))
251 (cond ((member 'full options)
256 (let* ((name (module-name module))
257 (obarray (module-obarray module)))
258 ;; XXX - should use hash-fold here
260 (lambda (symbol variable)
261 (cond ((regexp-exec match (symbol->string symbol))
265 (cond ((variable-bound? variable)
266 (let ((val (variable-ref variable)))
267 (cond ((or (procedure? val) value)
272 (display "(unbound)")))
274 (not (eq? (module-ref module symbol)
275 (module-ref (current-module) symbol))))
276 (display " shadowed"))
281 (define (apropos-internal rgx)
282 "Return a list of accessible variable names."
283 (apropos-fold (lambda (module name var data)
287 (apropos-fold-accessible (current-module))))
289 (define (apropos-fold proc init rgx folder)
290 "Folds PROCEDURE over bindings matching third arg REGEXP.
294 (PROCEDURE MODULE1 NAME1 VALUE1
295 (PROCEDURE MODULE2 NAME2 VALUE2
297 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
299 where INIT is the second arg to `apropos-fold'.
301 Fourth arg FOLDER is one of
303 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
304 apropos-fold-exported ;fold over all exported bindings
305 apropos-fold-all ;fold over all bindings"
306 (let ((match (make-regexp rgx))
307 (recorded (make-vector 61 '())))
309 (lambda (module data)
310 (let* ((obarray-filter
311 (lambda (name val data)
312 (if (and (regexp-exec match (symbol->string name))
313 (not (hashq-get-handle recorded name)))
315 (hashq-set! recorded name #t)
316 (proc module name val data))
319 (lambda (name var data)
320 (if (variable-bound? var)
321 (obarray-filter name (variable-ref var) data)
323 (cond (module (hash-fold module-filter
325 (module-obarray module)))
327 (folder fold-module init))))
329 (define (make-fold-modules init-thunk traverse extract)
330 "Return procedure capable of traversing a forest of modules.
331 The forest traversed is the image of the forest generated by root
332 modules returned by INIT-THUNK and the generator TRAVERSE.
333 It is an image under the mapping EXTRACT."
334 (lambda (fold-module init)
335 (let* ((table (make-hash-table 31))
336 (first? (lambda (obj)
337 (let* ((handle (hash-create-handle! table obj #t))
338 (first? (cdr handle)))
341 (let rec ((data init)
342 (modules (init-thunk)))
343 (do ((modules modules (cdr modules))
344 (data data (if (first? (car modules))
345 (rec (fold-module (extract (car modules)) data)
346 (traverse (car modules)))
348 ((null? modules) data))))))
350 (define (apropos-fold-accessible module)
351 (make-fold-modules (lambda () (list module))
355 (define (root-modules)
356 (cons the-root-module
357 (submodules (nested-ref the-root-module '(app modules)))))
359 (define (submodules m)
360 (hash-fold (lambda (name var data)
361 (let ((obj (and (variable-bound? var) (variable-ref var))))
362 (if (and (module? obj)
363 (eq? (module-kind obj) 'directory))
369 (define apropos-fold-exported
370 (make-fold-modules root-modules submodules module-public-interface))
372 (define apropos-fold-all
373 (make-fold-modules root-modules submodules identity))
376 (cond ((procedure? obj) (procedure-source obj))
377 ((macro? obj) (procedure-source (macro-transformer obj)))
381 (define (display-arg-list arg-list)
383 (display (car arg-list))
384 (let loop ((ls (cdr arg-list)))
388 (display "', the rest in `")
397 (define (display-arg-list/summary arg-list type)
398 (let ((len (length arg-list)))
403 (display " arguments: ")
404 (display " argument: "))
405 (display-arg-list arg-list)))
407 ((procedure-property obj 'arglist)
409 (let ((required-args (car arglist))
410 (optional-args (cadr arglist))
411 (keyword-args (caddr arglist))
412 (allow-other-keys? (cadddr arglist))
413 (rest-arg (car (cddddr arglist)))
414 (need-punctuation #f))
415 (cond ((not (null? required-args))
416 (display-arg-list/summary required-args "required")
417 (set! need-punctuation #t)))
418 (cond ((not (null? optional-args))
419 (if need-punctuation (display ", "))
420 (display-arg-list/summary optional-args "optional")
421 (set! need-punctuation #t)))
422 (cond ((not (null? keyword-args))
423 (if need-punctuation (display ", "))
424 (display-arg-list/summary keyword-args "keyword")
425 (set! need-punctuation #t)))
426 (cond (allow-other-keys?
427 (if need-punctuation (display ", "))
428 (display "other keywords allowed")
429 (set! need-punctuation #t)))
431 (if need-punctuation (display ", "))
432 (display "the rest in `")
436 (let ((arity (procedure-property obj 'arity)))
437 (display (car arity))
439 (display " or more"))
440 ((not (zero? (cadr arity)))
441 (display " required and ")
442 (display (cadr arity))
443 (display " optional")))
444 (if (and (not (caddr arity))
447 (display " argument")
448 (display " arguments"))
450 (let ((formals (cadr (procedure-source obj))))
454 (display-arg-list formals))
461 (define system-module
464 (let* ((m (nested-ref the-root-module
465 (append '(app modules) (cadr exp)))))
467 (error "Couldn't find any module named" (cadr exp)))
468 (let ((s (not (procedure-property (module-eval-closure m)
470 (set-system-module! m s)
471 (string-append "Module " (symbol->string (module-name m))
472 " is now a " (if s "system" "user") " module."))))))
474 ;;; session.scm ends here