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 (provided? '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))
41 (not-found (lambda (type x)
42 (simple-format #t "No ~A found for ~A\n"
51 (regexp-quote (symbol->string name)))))
60 (eq? (car name) 'unquote))
61 (cond ((object-documentation
62 (local-eval (cadr name) env))
64 (else (not-found 'documentation (cadr name)))))
69 (eq? (car name) 'quote)
70 (symbol? (cadr name)))
71 (cond ((search-documentation-files (cadr name))
73 (else (not-found 'documentation (cadr name)))))
77 (and-map symbol? name)
79 (not (eq? (car name) 'quote)))
80 (cond ((module-commentary name)
82 (display name) (write-line " commentary:")
84 (else (not-found 'commentary name))))
91 (define (module-filename name) ; fixme: better way? / done elsewhere?
92 (let* ((name (map symbol->string name))
93 (reverse-name (reverse name))
94 (leaf (car reverse-name))
95 (dir-hint-module-name (reverse (cdr reverse-name)))
96 (dir-hint (apply string-append
98 (string-append elt "/"))
99 dir-hint-module-name))))
100 (%search-load-path (in-vicinity dir-hint leaf))))
102 (define (module-commentary name)
103 (cond ((module-filename name) => file-commentary)
106 (define (help-doc term regexp)
107 (let ((entries (apropos-fold (lambda (module name object data)
110 (object-documentation object)
111 (cond ((closure? object)
114 "a primitive procedure")
120 apropos-fold-exported))
125 (cond ((not (null? entries))
127 (undocumented-entries '())
128 (documented-entries '())
129 (documentations '()))
131 (for-each (lambda (entry)
132 (let ((entry-summary (simple-format
134 (module-name (module entry))
138 (set! documented-entries
139 (cons entry-summary documented-entries))
140 ;; *fixme*: Use `describe' when we have GOOPS?
143 #f "`~S' is ~A in the ~S module.\n\n~A\n"
146 (module-name (module entry))
149 (set! undocumented-entries
151 undocumented-entries)))))
154 (if (and (not (null? documented-entries))
155 (or (> (length documented-entries) 1)
156 (not (null? undocumented-entries))))
158 (display "Documentation found for:\n")
159 (for-each (lambda (entry) (display entry))
163 (for-each (lambda (entry)
170 (if (not (null? undocumented-entries))
175 (display "No documentation found for:\n")
176 (for-each (lambda (entry) (display entry))
177 undocumented-entries)))))
178 ((search-documentation-files term)
180 (write-line "Documentation from file:")
184 (display "Did not find any object ")
188 "matching regexp \"~A\"\n")
192 (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
193 (help REGEXP) ditto for objects with names matching REGEXP (a string)
194 (help ,EXPR) gives documentation for object returned by EXPR
195 (help (my module)) gives module commentary for `(my module)'
196 (help) gives this text
198 `help' searches among bindings exported from loaded modules, while
199 `apropos' searches among bindings visible from the \"current\" module.
201 Examples: (help help)
203 (help \"output-string\")
205 Other useful sources of helpful information:
209 (name PROCEDURE-OR-MACRO)
210 (source PROCEDURE-OR-MACRO)
214 (backtrace) ;show backtrace from last error
215 (debug) ;enter the debugger
216 (trace [PROCEDURE]) ;trace procedure (no arg => show)
217 (untrace [PROCEDURE]) ;untrace (no arg => untrace all)
219 (OPTIONSET-options 'full) ;display option information
220 (OPTIONSET-enable 'OPTION)
221 (OPTIONSET-disable 'OPTION)
222 (OPTIONSET-set! OPTION VALUE)
224 where OPTIONSET is one of debug, read, eval, print
230 ;;; Author: Roland Orre <orre@nada.kth.se>
233 (define-public (apropos rgx . options)
234 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
235 (if (zero? (string-length rgx))
236 "Empty string not allowed"
237 (let* ((match (make-regexp rgx))
238 (modules (cons (current-module)
239 (module-uses (current-module))))
241 (shadow (member 'shadow options))
242 (value (member 'value options)))
243 (cond ((member 'full options)
248 (let* ((name (module-name module))
249 (obarray (module-obarray module)))
250 ;; XXX - should use hash-fold here
255 (cond ((regexp-exec match (symbol->string (car x)))
259 (cond ((procedure? (variable-ref (cdr x)))
261 (display (variable-ref (cdr x))))
264 (display (variable-ref (cdr x)))))
266 (not (eq? (module-ref module
268 (module-ref (current-module)
270 (display " shadowed"))
276 (define-public (apropos-internal rgx)
277 "Return a list of accessible variable names."
278 (apropos-fold (lambda (module name var data)
282 (apropos-fold-accessible (current-module))))
284 (define-public (apropos-fold proc init rgx folder)
285 "Folds PROCEDURE over bindings matching third arg REGEXP.
289 (PROCEDURE MODULE1 NAME1 VALUE1
290 (PROCEDURE MODULE2 NAME2 VALUE2
292 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
294 where INIT is the second arg to `apropos-fold'.
296 Fourth arg FOLDER is one of
298 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
299 apropos-fold-exported ;fold over all exported bindings
300 apropos-fold-all ;fold over all bindings"
301 (let ((match (make-regexp rgx))
302 (recorded (make-vector 61 '())))
304 (lambda (module data)
305 (let* ((obarray-filter
306 (lambda (name val data)
307 (if (and (regexp-exec match (symbol->string name))
308 (not (hashq-get-handle recorded name)))
310 (hashq-set! recorded name #t)
311 (proc module name val data))
314 (lambda (name var data)
315 (obarray-filter name (variable-ref var) data))))
316 (cond (module (hash-fold module-filter
318 (module-obarray module)))
320 (folder fold-module init))))
322 (define (make-fold-modules init-thunk traverse extract)
323 "Return procedure capable of traversing a forest of modules.
324 The forest traversed is the image of the forest generated by root
325 modules returned by INIT-THUNK and the generator TRAVERSE.
326 It is an image under the mapping EXTRACT."
327 (lambda (fold-module init)
328 (let* ((table (make-hash-table 31))
329 (first? (lambda (obj)
330 (let* ((handle (hash-create-handle! table obj #t))
331 (first? (cdr handle)))
334 (let rec ((data init)
335 (modules (init-thunk)))
336 (do ((modules modules (cdr modules))
337 (data data (if (first? (car modules))
338 (rec (fold-module (extract (car modules)) data)
339 (traverse (car modules)))
341 ((null? modules) data))))))
343 (define-public (apropos-fold-accessible module)
344 (make-fold-modules (lambda () (list module))
348 (define (root-modules)
349 (cons the-root-module
350 (submodules (nested-ref the-root-module '(app modules)))))
352 (define (submodules m)
353 (hash-fold (lambda (name var data)
354 (let ((obj (variable-ref var)))
355 (if (and (module? obj)
356 (eq? (module-kind obj) 'directory))
362 (define-public apropos-fold-exported
363 (make-fold-modules root-modules submodules module-public-interface))
365 (define-public apropos-fold-all
366 (make-fold-modules root-modules submodules identity))
368 (define-public (source obj)
369 (cond ((procedure? obj) (procedure-source obj))
370 ((macro? obj) (procedure-source (macro-transformer obj)))
373 (define-public (arity obj)
374 (let ((arity (procedure-property obj 'arity)))
375 (display (car arity))
377 (display " or more"))
378 ((not (zero? (cadr arity)))
379 (display " required and ")
380 (display (cadr arity))
381 (display " optional")))
382 (if (and (not (caddr arity))
385 (display " argument")
386 (display " arguments"))
388 (let ((formals (cadr (procedure-source obj))))
392 (display (car formals))
393 (let loop ((ls (cdr formals)))
397 (display "', the rest in `")
412 (define-public system-module
415 (let* ((m (nested-ref the-root-module
416 (append '(app modules) (cadr exp)))))
418 (error "Couldn't find any module named" (cadr exp)))
419 (let ((s (not (procedure-property (module-eval-closure m)
421 (set-system-module! m s)
422 (string-append "Module " (symbol->string (module-name m))
423 " is now a " (if s "system" "user") " module."))))))
425 ;;; session.scm ends here