session.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. ;;;; Copyright (C) 1997, 2000, 2001, 2003, 2006 Free Software Foundation, Inc.
  2. ;;;;
  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.
  7. ;;;;
  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.
  12. ;;;;
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (ice-9 session)
  18. :use-module (ice-9 documentation)
  19. :use-module (ice-9 regex)
  20. :use-module (ice-9 rdelim)
  21. :export (help
  22. add-value-help-handler! remove-value-help-handler!
  23. add-name-help-handler! remove-name-help-handler!
  24. apropos apropos-internal apropos-fold apropos-fold-accessible
  25. apropos-fold-exported apropos-fold-all source arity
  26. system-module module-commentary))
  27. (define *value-help-handlers*
  28. `(,(lambda (name value)
  29. (object-documentation value))))
  30. (define (add-value-help-handler! proc)
  31. "Adds a handler for performing `help' on a value.
  32. `proc' will be called as (PROC NAME VALUE). `proc' should return #t to
  33. indicate that it has performed help, a string to override the default
  34. object documentation, or #f to try the other handlers, potentially
  35. falling back on the normal behavior for `help'."
  36. (set! *value-help-handlers* (cons proc *value-help-handlers*)))
  37. (define (remove-value-help-handler! proc)
  38. "Removes a handler for performing `help' on a value."
  39. (set! *value-help-handlers* (delete! proc *value-help-handlers*)))
  40. (define (try-value-help name value)
  41. (or-map (lambda (proc) (proc name value)) *value-help-handlers*))
  42. (define *name-help-handlers* '())
  43. (define (add-name-help-handler! proc)
  44. "Adds a handler for performing `help' on a name.
  45. `proc' will be called with the unevaluated name as its argument. That is
  46. to say, when the user calls `(help FOO)', the name is FOO, exactly as
  47. the user types it.
  48. `proc' should return #t to indicate that it has performed help, a string
  49. to override the default object documentation, or #f to try the other
  50. handlers, potentially falling back on the normal behavior for `help'."
  51. (set! *name-help-handlers* (cons proc *name-help-handlers*)))
  52. (define (remove-name-help-handler! proc)
  53. "Removes a handler for performing `help' on a name."
  54. (set! *name-help-handlers* (delete! proc *name-help-handlers*)))
  55. (define (try-name-help name)
  56. (or-map (lambda (proc) (proc name)) *name-help-handlers*))
  57. ;;; Documentation
  58. ;;;
  59. (define help
  60. (procedure->syntax
  61. (lambda (exp env)
  62. "(help [NAME])
  63. Prints useful information. Try `(help)'."
  64. (cond ((not (= (length exp) 2))
  65. (help-usage))
  66. ((not (provided? 'regex))
  67. (display "`help' depends on the `regex' feature.
  68. You don't seem to have regular expressions installed.\n"))
  69. (else
  70. (let ((name (cadr exp))
  71. (not-found (lambda (type x)
  72. (simple-format #t "No ~A found for ~A\n"
  73. type x))))
  74. (cond
  75. ;; User-specified
  76. ((try-name-help name)
  77. => (lambda (x) (if (not (eq? x #t)) (display x))))
  78. ;; SYMBOL
  79. ((symbol? name)
  80. (help-doc name
  81. (simple-format
  82. #f "^~A$"
  83. (regexp-quote (symbol->string name)))))
  84. ;; "STRING"
  85. ((string? name)
  86. (help-doc name name))
  87. ;; (unquote SYMBOL)
  88. ((and (list? name)
  89. (= (length name) 2)
  90. (eq? (car name) 'unquote))
  91. (let ((doc (try-value-help (cadr name)
  92. (local-eval (cadr name) env))))
  93. (cond ((not doc) (not-found 'documentation (cadr name)))
  94. ((eq? doc #t)) ;; pass
  95. (else (write-line doc)))))
  96. ;; (quote SYMBOL)
  97. ((and (list? name)
  98. (= (length name) 2)
  99. (eq? (car name) 'quote)
  100. (symbol? (cadr name)))
  101. (cond ((search-documentation-files (cadr name))
  102. => write-line)
  103. (else (not-found 'documentation (cadr name)))))
  104. ;; (SYM1 SYM2 ...)
  105. ((and (list? name)
  106. (and-map symbol? name)
  107. (not (null? name))
  108. (not (eq? (car name) 'quote)))
  109. (cond ((module-commentary name)
  110. => (lambda (doc)
  111. (display name) (write-line " commentary:")
  112. (write-line doc)))
  113. (else (not-found 'commentary name))))
  114. ;; unrecognized
  115. (else
  116. (help-usage)))
  117. *unspecified*))))))
  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
  124. (map (lambda (elt)
  125. (string-append elt "/"))
  126. dir-hint-module-name))))
  127. (%search-load-path (in-vicinity dir-hint leaf))))
  128. (define (module-commentary name)
  129. (cond ((module-filename name) => file-commentary)
  130. (else #f)))
  131. (define (help-doc term regexp)
  132. (let ((entries (apropos-fold (lambda (module name object data)
  133. (cons (list module
  134. name
  135. (try-value-help name object)
  136. (cond ((closure? object)
  137. "a procedure")
  138. ((procedure? object)
  139. "a primitive procedure")
  140. (else
  141. "an object")))
  142. data))
  143. '()
  144. regexp
  145. apropos-fold-exported))
  146. (module car)
  147. (name cadr)
  148. (doc caddr)
  149. (type cadddr))
  150. (cond ((not (null? entries))
  151. (let ((first? #t)
  152. (undocumented-entries '())
  153. (documented-entries '())
  154. (documentations '()))
  155. (for-each (lambda (entry)
  156. (let ((entry-summary (simple-format
  157. #f "~S: ~S\n"
  158. (module-name (module entry))
  159. (name entry))))
  160. (if (doc entry)
  161. (begin
  162. (set! documented-entries
  163. (cons entry-summary documented-entries))
  164. ;; *fixme*: Use `describe' when we have GOOPS?
  165. (set! documentations
  166. (cons (simple-format
  167. #f "`~S' is ~A in the ~S module.\n\n~A\n"
  168. (name entry)
  169. (type entry)
  170. (module-name (module entry))
  171. (doc entry))
  172. documentations)))
  173. (set! undocumented-entries
  174. (cons entry-summary
  175. undocumented-entries)))))
  176. entries)
  177. (if (and (not (null? documented-entries))
  178. (or (> (length documented-entries) 1)
  179. (not (null? undocumented-entries))))
  180. (begin
  181. (display "Documentation found for:\n")
  182. (for-each (lambda (entry) (display entry))
  183. documented-entries)
  184. (set! first? #f)))
  185. (for-each (lambda (entry)
  186. (if first?
  187. (set! first? #f)
  188. (newline))
  189. (display entry))
  190. documentations)
  191. (if (not (null? undocumented-entries))
  192. (begin
  193. (if first?
  194. (set! first? #f)
  195. (newline))
  196. (display "No documentation found for:\n")
  197. (for-each (lambda (entry) (display entry))
  198. undocumented-entries)))))
  199. ((search-documentation-files term)
  200. => (lambda (doc)
  201. (write-line "Documentation from file:")
  202. (write-line doc)))
  203. (else
  204. ;; no matches
  205. (display "Did not find any object ")
  206. (simple-format #t
  207. (if (symbol? term)
  208. "named `~A'\n"
  209. "matching regexp \"~A\"\n")
  210. term)))))
  211. (define (help-usage)
  212. (display "Usage: (help NAME) gives documentation about objects named NAME (a symbol)
  213. (help REGEXP) ditto for objects with names matching REGEXP (a string)
  214. (help 'NAME) gives documentation for NAME, even if it is not an object
  215. (help ,EXPR) gives documentation for object returned by EXPR
  216. (help (my module)) gives module commentary for `(my module)'
  217. (help) gives this text
  218. `help' searches among bindings exported from loaded modules, while
  219. `apropos' searches among bindings visible from the \"current\" module.
  220. Examples: (help help)
  221. (help cons)
  222. (help \"output-string\")
  223. Other useful sources of helpful information:
  224. (apropos STRING)
  225. (arity PROCEDURE)
  226. (name PROCEDURE-OR-MACRO)
  227. (source PROCEDURE-OR-MACRO)
  228. Tools:
  229. (backtrace) ;show backtrace from last error
  230. (debug) ;enter the debugger
  231. (trace [PROCEDURE]) ;trace procedure (no arg => show)
  232. (untrace [PROCEDURE]) ;untrace (no arg => untrace all)
  233. (OPTIONSET-options 'full) ;display option information
  234. (OPTIONSET-enable 'OPTION)
  235. (OPTIONSET-disable 'OPTION)
  236. (OPTIONSET-set! OPTION VALUE)
  237. where OPTIONSET is one of debug, read, eval, print
  238. "))
  239. ;;; {Apropos}
  240. ;;;
  241. ;;; Author: Roland Orre <orre@nada.kth.se>
  242. ;;;
  243. (define (apropos rgx . options)
  244. "Search for bindings: apropos regexp {options= 'full 'shadow 'value}"
  245. (if (zero? (string-length rgx))
  246. "Empty string not allowed"
  247. (let* ((match (make-regexp rgx))
  248. (uses (module-uses (current-module)))
  249. (modules (cons (current-module)
  250. (if (and (not (null? uses))
  251. (eq? (module-name (car uses))
  252. 'duplicates))
  253. (cdr uses)
  254. uses)))
  255. (separator #\tab)
  256. (shadow (member 'shadow options))
  257. (value (member 'value options)))
  258. (cond ((member 'full options)
  259. (set! shadow #t)
  260. (set! value #t)))
  261. (for-each
  262. (lambda (module)
  263. (let* ((name (module-name module))
  264. (obarray (module-obarray module)))
  265. ;; XXX - should use hash-fold here
  266. (hash-for-each
  267. (lambda (symbol variable)
  268. (cond ((regexp-exec match (symbol->string symbol))
  269. (display name)
  270. (display ": ")
  271. (display symbol)
  272. (cond ((variable-bound? variable)
  273. (let ((val (variable-ref variable)))
  274. (cond ((or (procedure? val) value)
  275. (display separator)
  276. (display val)))))
  277. (else
  278. (display separator)
  279. (display "(unbound)")))
  280. (if (and shadow
  281. (not (eq? (module-ref module symbol)
  282. (module-ref (current-module) symbol))))
  283. (display " shadowed"))
  284. (newline))))
  285. obarray)))
  286. modules))))
  287. (define (apropos-internal rgx)
  288. "Return a list of accessible variable names."
  289. (apropos-fold (lambda (module name var data)
  290. (cons name data))
  291. '()
  292. rgx
  293. (apropos-fold-accessible (current-module))))
  294. (define (apropos-fold proc init rgx folder)
  295. "Folds PROCEDURE over bindings matching third arg REGEXP.
  296. Result is
  297. (PROCEDURE MODULE1 NAME1 VALUE1
  298. (PROCEDURE MODULE2 NAME2 VALUE2
  299. ...
  300. (PROCEDURE MODULEn NAMEn VALUEn INIT)))
  301. where INIT is the second arg to `apropos-fold'.
  302. 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 '())))
  308. (let ((fold-module
  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)))
  314. (begin
  315. (hashq-set! recorded name #t)
  316. (proc module name val data))
  317. data)))
  318. (module-filter
  319. (lambda (name var data)
  320. (if (variable-bound? var)
  321. (obarray-filter name (variable-ref var) data)
  322. data))))
  323. (cond (module (hash-fold module-filter
  324. data
  325. (module-obarray module)))
  326. (else data))))))
  327. (folder fold-module init))))
  328. (define (make-fold-modules init-thunk traverse extract)
  329. "Return procedure capable of traversing a forest of modules.
  330. The forest traversed is the image of the forest generated by root
  331. modules returned by INIT-THUNK and the generator TRAVERSE.
  332. It is an image under the mapping EXTRACT."
  333. (lambda (fold-module init)
  334. (let* ((table (make-hash-table 31))
  335. (first? (lambda (obj)
  336. (let* ((handle (hash-create-handle! table obj #t))
  337. (first? (cdr handle)))
  338. (set-cdr! handle #f)
  339. first?))))
  340. (let rec ((data init)
  341. (modules (init-thunk)))
  342. (do ((modules modules (cdr modules))
  343. (data data (if (first? (car modules))
  344. (rec (fold-module (extract (car modules)) data)
  345. (traverse (car modules)))
  346. data)))
  347. ((null? modules) data))))))
  348. (define (apropos-fold-accessible module)
  349. (make-fold-modules (lambda () (list module))
  350. module-uses
  351. identity))
  352. (define (root-modules)
  353. (cons the-root-module
  354. (submodules (nested-ref the-root-module '(app modules)))))
  355. (define (submodules m)
  356. (hash-fold (lambda (name var data)
  357. (let ((obj (and (variable-bound? var) (variable-ref var))))
  358. (if (and (module? obj)
  359. (eq? (module-kind obj) 'directory))
  360. (cons obj data)
  361. data)))
  362. '()
  363. (module-obarray m)))
  364. (define apropos-fold-exported
  365. (make-fold-modules root-modules submodules module-public-interface))
  366. (define apropos-fold-all
  367. (make-fold-modules root-modules submodules identity))
  368. (define (source obj)
  369. (cond ((procedure? obj) (procedure-source obj))
  370. ((macro? obj) (procedure-source (macro-transformer obj)))
  371. (else #f)))
  372. (define (arity obj)
  373. (define (display-arg-list arg-list)
  374. (display #\`)
  375. (display (car arg-list))
  376. (let loop ((ls (cdr arg-list)))
  377. (cond ((null? ls)
  378. (display #\'))
  379. ((not (pair? ls))
  380. (display "', the rest in `")
  381. (display ls)
  382. (display #\'))
  383. (else
  384. (if (pair? (cdr ls))
  385. (display "', `")
  386. (display "' and `"))
  387. (display (car ls))
  388. (loop (cdr ls))))))
  389. (define (display-arg-list/summary arg-list type)
  390. (let ((len (length arg-list)))
  391. (display len)
  392. (display " ")
  393. (display type)
  394. (if (> len 1)
  395. (display " arguments: ")
  396. (display " argument: "))
  397. (display-arg-list arg-list)))
  398. (cond
  399. ((procedure-property obj 'arglist)
  400. => (lambda (arglist)
  401. (let ((required-args (car arglist))
  402. (optional-args (cadr arglist))
  403. (keyword-args (caddr arglist))
  404. (allow-other-keys? (cadddr arglist))
  405. (rest-arg (car (cddddr arglist)))
  406. (need-punctuation #f))
  407. (cond ((not (null? required-args))
  408. (display-arg-list/summary required-args "required")
  409. (set! need-punctuation #t)))
  410. (cond ((not (null? optional-args))
  411. (if need-punctuation (display ", "))
  412. (display-arg-list/summary optional-args "optional")
  413. (set! need-punctuation #t)))
  414. (cond ((not (null? keyword-args))
  415. (if need-punctuation (display ", "))
  416. (display-arg-list/summary keyword-args "keyword")
  417. (set! need-punctuation #t)))
  418. (cond (allow-other-keys?
  419. (if need-punctuation (display ", "))
  420. (display "other keywords allowed")
  421. (set! need-punctuation #t)))
  422. (cond (rest-arg
  423. (if need-punctuation (display ", "))
  424. (display "the rest in `")
  425. (display rest-arg)
  426. (display "'"))))))
  427. (else
  428. (let ((arity (procedure-property obj 'arity)))
  429. (display (car arity))
  430. (cond ((caddr arity)
  431. (display " or more"))
  432. ((not (zero? (cadr arity)))
  433. (display " required and ")
  434. (display (cadr arity))
  435. (display " optional")))
  436. (if (and (not (caddr arity))
  437. (= (car arity) 1)
  438. (<= (cadr arity) 1))
  439. (display " argument")
  440. (display " arguments"))
  441. (if (closure? obj)
  442. (let ((formals (cadr (procedure-source obj))))
  443. (cond
  444. ((pair? formals)
  445. (display ": ")
  446. (display-arg-list formals))
  447. (else
  448. (display " in `")
  449. (display formals)
  450. (display #\'))))))))
  451. (display ".\n"))
  452. (define system-module
  453. (procedure->syntax
  454. (lambda (exp env)
  455. (let* ((m (nested-ref the-root-module
  456. (append '(app modules) (cadr exp)))))
  457. (if (not m)
  458. (error "Couldn't find any module named" (cadr exp)))
  459. (let ((s (not (procedure-property (module-eval-closure m)
  460. 'system-module))))
  461. (set-system-module! m s)
  462. (string-append "Module " (symbol->string (module-name m))
  463. " is now a " (if s "system" "user") " module."))))))
  464. ;;; session.scm ends here