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
20 (define-module (ice-9 session)
21 :use-module (ice-9 documentation)
22 :use-module (ice-9 regex)
23 :use-module (ice-9 rdelim))
33 Prints useful information. Try `(help)'."
34 (cond ((not (= (length exp) 2))
36 ((not (feature? 'regex))
37 (display "`help' depends on the `regex' feature.
38 You don't seem to have regular expressions installed.\n"))
40 (let ((name (cadr exp)))
45 (symbol->string name))
51 (eq? (car name) 'unquote))
52 (let ((doc (object-documentation (local-eval (cadr name)
55 (simple-format #t "No documentation found for ~S\n"
59 (and-map symbol? name)
61 (not (eq? (car name) 'quote)))
62 (let ((doc (module-commentary name)))
65 #t "No commentary found for module ~S\n" name)
67 (display name) (write-line " commentary:")
73 (define (module-filename name) ; fixme: better way? / done elsewhere?
74 (let* ((name (map symbol->string name))
75 (reverse-name (reverse name))
76 (leaf (car reverse-name))
77 (dir-hint-module-name (reverse (cdr reverse-name)))
78 (dir-hint (apply string-append
80 (string-append elt "/"))
81 dir-hint-module-name))))
82 (%search-load-path (in-vicinity dir-hint leaf))))
84 (define (module-commentary name)
85 (cond ((module-filename name) => file-commentary)
88 (define (help-doc term regexp)
89 (let ((entries (apropos-fold (lambda (module name object data)
92 (object-documentation object)
93 (cond ((closure? object)
96 "a primitive procedure")
102 apropos-fold-exported))
110 (display "Did not find any object ")
114 "matching regexp \"~A\"\n")
117 (undocumented-entries '())
118 (documented-entries '())
119 (documentations '()))
121 (for-each (lambda (entry)
122 (let ((entry-summary (simple-format #f
124 (module-name (module entry))
128 (set! documented-entries
129 (cons entry-summary documented-entries))
130 ;; *fixme*: Use `describe' when we have GOOPS?
132 (cons (simple-format #f
133 "`~S' is ~A in the ~S module.\n\n~A\n"
136 (module-name (module entry))
139 (set! undocumented-entries
140 (cons entry-summary undocumented-entries)))))
143 (if (and (not (null? documented-entries))
144 (or (> (length documented-entries) 1)
145 (not (null? undocumented-entries))))
147 (display "Documentation found for:\n")
148 (for-each (lambda (entry) (display entry)) documented-entries)
151 (for-each (lambda (entry)
158 (if (not (null? undocumented-entries))
163 (display "No documentation found for:\n")
164 (for-each (lambda (entry) (display entry)) undocumented-entries)))))))
167 (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
168 (help REGEXP) ditto for objects with names matching REGEXP (a string)
169 (help ,EXPR) gives documentation for object returned by EXPR
170 (help (my module)) gives module commentary for `(my module)'
171 (help) gives this text
173 `help' searches among bindings exported from loaded modules, while
174 `apropos' searches among bindings visible from the \"current\" module.
176 Examples: (help help)
178 (help \"output-string\")
180 Other useful sources of helpful information:
184 (name PROCEDURE-OR-MACRO)
185 (source PROCEDURE-OR-MACRO)
189 (backtrace) ;show backtrace from last error
190 (debug) ;enter the debugger
191 (trace [PROCEDURE]) ;trace procedure (no arg => show)
192 (untrace [PROCEDURE]) ;untrace (no arg => untrace all)
194 (OPTIONSET-options 'full) ;display option information
195 (OPTIONSET-enable 'OPTION)
196 (OPTIONSET-disable 'OPTION)
197 (OPTIONSET-set! OPTION VALUE)
199 where OPTIONSET is one of debug, read, eval, print
205 ;;; Author: Roland Orre <orre@nada.kth.se>
208 (define-public (apropos rgx . options)
209 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
210 (if (zero? (string-length rgx))
211 "Empty string not allowed"
212 (let* ((match (make-regexp rgx))
213 (modules (cons (current-module)
214 (module-uses (current-module))))
216 (shadow (member 'shadow options))
217 (value (member 'value options)))
218 (cond ((member 'full options)
223 (let* ((builtin (or (eq? module the-scm-module)
224 (eq? module the-root-module)))
225 (name (module-name module))
228 (module-obarray module)))
236 (cond ((regexp-exec match (symbol->string (car x)))
240 (cond ((procedure? (get-ref (cdr x)))
242 (display (get-ref (cdr x))))
245 (display (get-ref (cdr x)))))
247 (not (eq? (module-ref module
249 (module-ref (current-module)
251 (display " shadowed"))
258 (define-public (apropos-internal rgx)
259 "Return a list of accessible variable names."
260 (apropos-fold (lambda (module name var data)
264 (apropos-fold-accessible (current-module))))
266 (define-public (apropos-fold proc init rgx folder)
267 "Folds PROCEDURE over bindings matching third arg REGEXP.
271 (PROCEDURE MODULE1 NAME1 VALUE1
272 (PROCEDURE MODULE2 NAME2 VALUE2
274 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
276 where INIT is the second arg to `apropos-fold'.
278 Fourth arg FOLDER is one of
280 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
281 apropos-fold-exported ;fold over all exported bindings
282 apropos-fold-all ;fold over all bindings"
283 (let ((match (make-regexp rgx))
284 (recorded (make-vector 61 '())))
286 (lambda (module data)
287 (let* ((obarray-filter
288 (lambda (name val data)
289 (if (and (regexp-exec match (symbol->string name))
290 (not (hashq-get-handle recorded name)))
292 (hashq-set! recorded name #t)
293 (proc module name val data))
296 (lambda (name var data)
297 (obarray-filter name (variable-ref var) data))))
298 (cond ((or (eq? module the-scm-module)
299 (eq? module the-root-module))
300 (hash-fold obarray-filter
303 (module (hash-fold module-filter
305 (module-obarray module)))
307 (folder fold-module init))))
309 (define (make-fold-modules init-thunk traverse extract)
310 "Return procedure capable of traversing a forest of modules.
311 The forest traversed is the image of the forest generated by root
312 modules returned by INIT-THUNK and the generator TRAVERSE.
313 It is an image under the mapping EXTRACT."
314 (lambda (fold-module init)
315 (let* ((table (make-hash-table 31))
316 (first? (lambda (obj)
317 (let* ((handle (hash-create-handle! table obj #t))
318 (first? (cdr handle)))
321 (let rec ((data init)
322 (modules (init-thunk)))
323 (do ((modules modules (cdr modules))
324 (data data (if (first? (car modules))
325 (rec (fold-module (extract (car modules)) data)
326 (traverse (car modules)))
328 ((null? modules) data))))))
330 (define-public (apropos-fold-accessible module)
331 (make-fold-modules (lambda () (list module))
335 (define (root-modules)
336 (cons the-root-module
337 (submodules (nested-ref the-root-module '(app modules)))))
339 (define (submodules m)
340 (hash-fold (lambda (name var data)
341 (let ((obj (variable-ref var)))
342 (if (and (module? obj)
343 (eq? (module-kind obj) 'directory))
349 (define-public apropos-fold-exported
350 (make-fold-modules root-modules submodules module-public-interface))
352 (define-public apropos-fold-all
353 (make-fold-modules root-modules submodules (lambda (x) x)))
355 (define-public (source obj)
356 (cond ((procedure? obj) (procedure-source obj))
357 ((macro? obj) (procedure-source (macro-transformer obj)))
360 (define-public (arity obj)
361 (let ((arity (procedure-property obj 'arity)))
362 (display (car arity))
364 (display " or more"))
365 ((not (zero? (cadr arity)))
366 (display " required and ")
367 (display (cadr arity))
368 (display " optional")))
369 (if (and (not (caddr arity))
372 (display " argument")
373 (display " arguments"))
375 (let ((formals (cadr (procedure-source obj))))
379 (display (car formals))
380 (let loop ((ls (cdr formals)))
384 (display "', the rest in `")
399 (define-public system-module
402 (let* ((m (nested-ref the-root-module
403 (append '(app modules) (cadr exp)))))
405 (error "Couldn't find any module named" (cadr exp)))
406 (let ((s (not (procedure-property (module-eval-closure m)
408 (set-system-module! m s)
409 (string-append "Module " (symbol->string (module-name m))
410 " is now a " (if s "system" "user") " module."))))))