1 ;;;; Copyright (C) 1997, 2000, 2001, 2003, 2006, 2009, 2010, 2011,
2 ;;;; 2012 Free Software Foundation, Inc.
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the GNU Lesser General Public
6 ;;;; License as published by the Free Software Foundation; either
7 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;; This library is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;;; Lesser General Public License for more details.
14 ;;;; You should have received a copy of the GNU Lesser General Public
15 ;;;; License along with this library; if not, write to the Free Software
16 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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)
24 #:use-module (ice-9 match)
26 add-value-help-handler! remove-value-help-handler!
27 add-name-help-handler! remove-name-help-handler!
29 apropos apropos-internal apropos-fold apropos-fold-accessible
30 apropos-fold-exported apropos-fold-all source arity
36 (define *value-help-handlers*
37 `(,(lambda (name value)
38 (object-documentation value))))
40 (define (add-value-help-handler! proc)
41 "Adds a handler for performing `help' on a value.
43 `proc' will be called as (PROC NAME VALUE). `proc' should return #t to
44 indicate that it has performed help, a string to override the default
45 object documentation, or #f to try the other handlers, potentially
46 falling back on the normal behavior for `help'."
47 (set! *value-help-handlers* (cons proc *value-help-handlers*)))
49 (define (remove-value-help-handler! proc)
50 "Removes a handler for performing `help' on a value."
51 (set! *value-help-handlers* (delete! proc *value-help-handlers*)))
53 (define (try-value-help name value)
54 (or-map (lambda (proc) (proc name value)) *value-help-handlers*))
57 (define *name-help-handlers* '())
59 (define (add-name-help-handler! proc)
60 "Adds a handler for performing `help' on a name.
62 `proc' will be called with the unevaluated name as its argument. That is
63 to say, when the user calls `(help FOO)', the name is FOO, exactly as
66 `proc' should return #t to indicate that it has performed help, a string
67 to override the default object documentation, or #f to try the other
68 handlers, potentially falling back on the normal behavior for `help'."
69 (set! *name-help-handlers* (cons proc *name-help-handlers*)))
71 (define (remove-name-help-handler! proc)
72 "Removes a handler for performing `help' on a name."
73 (set! *name-help-handlers* (delete! proc *name-help-handlers*)))
75 (define (try-name-help name)
76 (or-map (lambda (proc) (proc name)) *name-help-handlers*))
81 (define-macro (help . exp)
83 Prints useful information. Try `(help)'."
84 (cond ((not (= (length exp) 1))
87 ((not (provided? 'regex))
88 (display "`help' depends on the `regex' feature.
89 You don't seem to have regular expressions installed.\n")
92 (let ((name (car exp))
93 (not-found (lambda (type x)
94 (simple-format #t "No ~A found for ~A\n"
100 => (lambda (x) (if (not (eq? x #t)) (display x))))
107 (regexp-quote (symbol->string name)))))
111 (help-doc name name))
116 (eq? (car name) 'unquote))
117 (let ((doc (try-value-help (cadr name)
118 (module-ref (current-module)
120 (cond ((not doc) (not-found 'documentation (cadr name)))
121 ((eq? doc #t)) ;; pass
122 (else (write-line doc)))))
127 (eq? (car name) 'quote)
128 (symbol? (cadr name)))
129 (cond ((search-documentation-files (cadr name))
131 (else (not-found 'documentation (cadr name)))))
135 (and-map symbol? name)
137 (not (eq? (car name) 'quote)))
138 (cond ((module-commentary name)
140 (display name) (write-line " commentary:")
142 (else (not-found 'commentary name))))
149 (define (module-filename name) ; fixme: better way? / done elsewhere?
150 (let* ((name (map symbol->string name))
151 (reverse-name (reverse name))
152 (leaf (car reverse-name))
153 (dir-hint-module-name (reverse (cdr reverse-name)))
154 (dir-hint (apply string-append
156 (string-append elt "/"))
157 dir-hint-module-name))))
158 (%search-load-path (in-vicinity dir-hint leaf))))
160 (define (module-commentary name)
161 (cond ((module-filename name) => file-commentary)
164 (define (help-doc term regexp)
165 (let ((entries (apropos-fold (lambda (module name object data)
168 (try-value-help name object)
169 (cond ((procedure? object)
176 apropos-fold-exported))
181 (cond ((not (null? entries))
183 (undocumented-entries '())
184 (documented-entries '())
185 (documentations '()))
187 (for-each (lambda (entry)
188 (let ((entry-summary (simple-format
190 (module-name (module entry))
194 (set! documented-entries
195 (cons entry-summary documented-entries))
196 ;; *fixme*: Use `describe' when we have GOOPS?
199 #f "`~S' is ~A in the ~S module.\n\n~A\n"
202 (module-name (module entry))
205 (set! undocumented-entries
207 undocumented-entries)))))
210 (if (and (not (null? documented-entries))
211 (or (> (length documented-entries) 1)
212 (not (null? undocumented-entries))))
214 (display "Documentation found for:\n")
215 (for-each (lambda (entry) (display entry))
219 (for-each (lambda (entry)
226 (if (not (null? undocumented-entries))
231 (display "No documentation found for:\n")
232 (for-each (lambda (entry) (display entry))
233 undocumented-entries)))))
234 ((search-documentation-files term)
236 (write-line "Documentation from file:")
240 (display "Did not find any object ")
244 "matching regexp \"~A\"\n")
248 (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
249 (help REGEXP) ditto for objects with names matching REGEXP (a string)
250 (help 'NAME) gives documentation for NAME, even if it is not an object
251 (help ,EXPR) gives documentation for object returned by EXPR
252 (help (my module)) gives module commentary for `(my module)'
253 (help) gives this text
255 `help' searches among bindings exported from loaded modules, while
256 `apropos' searches among bindings visible from the \"current\" module.
258 Examples: (help help)
260 (help \"output-string\")
262 Other useful sources of helpful information:
266 (name PROCEDURE-OR-MACRO)
267 (source PROCEDURE-OR-MACRO)
271 (backtrace) ;show backtrace from last error
272 (debug) ;enter the debugger
273 (trace [PROCEDURE]) ;trace procedure (no arg => show)
274 (untrace [PROCEDURE]) ;untrace (no arg => untrace all)
276 (OPTIONSET-options 'full) ;display option information
277 (OPTIONSET-enable 'OPTION)
278 (OPTIONSET-disable 'OPTION)
279 (OPTIONSET-set! OPTION VALUE)
281 where OPTIONSET is one of debug, read, eval, print
287 ;;; Author: Roland Orre <orre@nada.kth.se>
290 ;; Two arguments: the module, and the pattern, as a string.
292 (define apropos-hook (make-hook 2))
294 (define (apropos rgx . options)
295 "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
296 (run-hook apropos-hook (current-module) rgx)
297 (if (zero? (string-length rgx))
298 "Empty string not allowed"
299 (let* ((match (make-regexp rgx))
300 (uses (module-uses (current-module)))
301 (modules (cons (current-module)
302 (if (and (not (null? uses))
303 (eq? (module-name (car uses))
308 (shadow (member 'shadow options))
309 (value (member 'value options)))
310 (cond ((member 'full options)
315 (let* ((name (module-name module))
316 (obarray (module-obarray module)))
317 ;; XXX - should use hash-fold here
319 (lambda (symbol variable)
320 (cond ((regexp-exec match (symbol->string symbol))
324 (cond ((variable-bound? variable)
325 (let ((val (variable-ref variable)))
326 (cond ((or (procedure? val) value)
331 (display "(unbound)")))
333 (not (eq? (module-ref module symbol)
334 (module-ref (current-module) symbol))))
335 (display " shadowed"))
340 (define (apropos-internal rgx)
341 "Return a list of accessible variable names."
342 (apropos-fold (lambda (module name var data)
346 (apropos-fold-accessible (current-module))))
348 (define (apropos-fold proc init rgx folder)
349 "Folds PROCEDURE over bindings matching third arg REGEXP.
353 (PROCEDURE MODULE1 NAME1 VALUE1
354 (PROCEDURE MODULE2 NAME2 VALUE2
356 (PROCEDURE MODULEn NAMEn VALUEn INIT)))
358 where INIT is the second arg to `apropos-fold'.
360 Fourth arg FOLDER is one of
362 (apropos-fold-accessible MODULE) ;fold over bindings accessible in MODULE
363 apropos-fold-exported ;fold over all exported bindings
364 apropos-fold-all ;fold over all bindings"
365 (run-hook apropos-hook (current-module) rgx)
366 (let ((match (make-regexp rgx))
367 (recorded (make-hash-table)))
369 (lambda (module data)
370 (let* ((obarray-filter
371 (lambda (name val data)
372 (if (and (regexp-exec match (symbol->string name))
373 (not (hashq-get-handle recorded name)))
375 (hashq-set! recorded name #t)
376 (proc module name val data))
379 (lambda (name var data)
380 (if (variable-bound? var)
381 (obarray-filter name (variable-ref var) data)
383 (cond (module (hash-fold module-filter
385 (module-obarray module)))
387 (folder fold-module init))))
389 (define (make-fold-modules init-thunk traverse extract)
390 "Return procedure capable of traversing a forest of modules.
391 The forest traversed is the image of the forest generated by root
392 modules returned by INIT-THUNK and the generator TRAVERSE.
393 It is an image under the mapping EXTRACT."
394 (lambda (fold-module init)
395 (let* ((table (make-hash-table 31))
396 (first? (lambda (obj)
397 (let* ((handle (hash-create-handle! table obj #t))
398 (first? (cdr handle)))
401 (let rec ((data init)
402 (modules (init-thunk)))
403 (do ((modules modules (cdr modules))
404 (data data (if (first? (car modules))
405 (rec (fold-module (extract (car modules)) data)
406 (traverse (car modules)))
408 ((null? modules) data))))))
410 (define (apropos-fold-accessible module)
411 (make-fold-modules (lambda () (list module))
415 (define (root-modules)
416 (submodules (resolve-module '() #f)))
418 (define (submodules mod)
419 (hash-map->list (lambda (k v) v) (module-submodules mod)))
421 (define apropos-fold-exported
422 (make-fold-modules root-modules submodules module-public-interface))
424 (define apropos-fold-all
425 (make-fold-modules root-modules submodules identity))
428 (cond ((procedure? obj) (procedure-source obj))
429 ((macro? obj) (procedure-source (macro-transformer obj)))
433 (define (display-arg-list arg-list)
435 (display (car arg-list))
436 (let loop ((ls (cdr arg-list)))
440 (display "', the rest in `")
449 (define (display-arg-list/summary arg-list type)
450 (let ((len (length arg-list)))
455 (display " arguments: ")
456 (display " argument: "))
457 (display-arg-list arg-list)))
459 ((procedure-property obj 'arglist)
461 (let ((required-args (car arglist))
462 (optional-args (cadr arglist))
463 (keyword-args (caddr arglist))
464 (allow-other-keys? (cadddr arglist))
465 (rest-arg (car (cddddr arglist)))
466 (need-punctuation #f))
467 (cond ((not (null? required-args))
468 (display-arg-list/summary required-args "required")
469 (set! need-punctuation #t)))
470 (cond ((not (null? optional-args))
471 (if need-punctuation (display ", "))
472 (display-arg-list/summary optional-args "optional")
473 (set! need-punctuation #t)))
474 (cond ((not (null? keyword-args))
475 (if need-punctuation (display ", "))
476 (display-arg-list/summary keyword-args "keyword")
477 (set! need-punctuation #t)))
478 (cond (allow-other-keys?
479 (if need-punctuation (display ", "))
480 (display "other keywords allowed")
481 (set! need-punctuation #t)))
483 (if need-punctuation (display ", "))
484 (display "the rest in `")
488 (let ((arity (procedure-minimum-arity obj)))
489 (display (car arity))
491 (display " or more"))
492 ((not (zero? (cadr arity)))
493 (display " required and ")
494 (display (cadr arity))
495 (display " optional")))
496 (if (and (not (caddr arity))
499 (display " argument")
500 (display " arguments")))))
504 (define (procedure-arguments proc)
505 "Return an alist describing the arguments that `proc' accepts, or `#f'
506 if the information cannot be obtained.
508 The alist keys that are currently defined are `required', `optional',
509 `keyword', `allow-other-keys?', and `rest'."
511 ((procedure-property proc 'arglist)
513 ((req opt keyword aok? rest)
514 `((required . ,(if (number? req)
517 (optional . ,(if (number? opt)
521 (allow-other-keys? . ,aok?)
523 ((procedure-source proc)
525 (((@ (system vm program) program?) proc)
526 ((@ (system vm program) program-arguments-alist) proc))
530 ;;; session.scm ends here