session.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. ;;;; Copyright (C) 1997, 2000, 2001, 2003, 2006, 2009, 2010, 2011 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 3 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. procedure-arguments
  27. module-commentary))
  28. (define *value-help-handlers*
  29. `(,(lambda (name value)
  30. (object-documentation value))))
  31. (define (add-value-help-handler! proc)
  32. "Adds a handler for performing `help' on a value.
  33. `proc' will be called as (PROC NAME VALUE). `proc' should return #t to
  34. indicate that it has performed help, a string to override the default
  35. object documentation, or #f to try the other handlers, potentially
  36. falling back on the normal behavior for `help'."
  37. (set! *value-help-handlers* (cons proc *value-help-handlers*)))
  38. (define (remove-value-help-handler! proc)
  39. "Removes a handler for performing `help' on a value."
  40. (set! *value-help-handlers* (delete! proc *value-help-handlers*)))
  41. (define (try-value-help name value)
  42. (or-map (lambda (proc) (proc name value)) *value-help-handlers*))
  43. (define *name-help-handlers* '())
  44. (define (add-name-help-handler! proc)
  45. "Adds a handler for performing `help' on a name.
  46. `proc' will be called with the unevaluated name as its argument. That is
  47. to say, when the user calls `(help FOO)', the name is FOO, exactly as
  48. the user types it.
  49. `proc' should return #t to indicate that it has performed help, a string
  50. to override the default object documentation, or #f to try the other
  51. handlers, potentially falling back on the normal behavior for `help'."
  52. (set! *name-help-handlers* (cons proc *name-help-handlers*)))
  53. (define (remove-name-help-handler! proc)
  54. "Removes a handler for performing `help' on a name."
  55. (set! *name-help-handlers* (delete! proc *name-help-handlers*)))
  56. (define (try-name-help name)
  57. (or-map (lambda (proc) (proc name)) *name-help-handlers*))
  58. ;;; Documentation
  59. ;;;
  60. (define-macro (help . exp)
  61. "(help [NAME])
  62. Prints useful information. Try `(help)'."
  63. (cond ((not (= (length exp) 1))
  64. (help-usage)
  65. '(begin))
  66. ((not (provided? 'regex))
  67. (display "`help' depends on the `regex' feature.
  68. You don't seem to have regular expressions installed.\n")
  69. '(begin))
  70. (else
  71. (let ((name (car exp))
  72. (not-found (lambda (type x)
  73. (simple-format #t "No ~A found for ~A\n"
  74. type x))))
  75. (cond
  76. ;; User-specified
  77. ((try-name-help name)
  78. => (lambda (x) (if (not (eq? x #t)) (display x))))
  79. ;; SYMBOL
  80. ((symbol? name)
  81. (help-doc name
  82. (simple-format
  83. #f "^~A$"
  84. (regexp-quote (symbol->string name)))))
  85. ;; "STRING"
  86. ((string? name)
  87. (help-doc name name))
  88. ;; (unquote SYMBOL)
  89. ((and (list? name)
  90. (= (length name) 2)
  91. (eq? (car name) 'unquote))
  92. (let ((doc (try-value-help (cadr name)
  93. (module-ref (current-module)
  94. (cadr name)))))
  95. (cond ((not doc) (not-found 'documentation (cadr name)))
  96. ((eq? doc #t)) ;; pass
  97. (else (write-line doc)))))
  98. ;; (quote SYMBOL)
  99. ((and (list? name)
  100. (= (length name) 2)
  101. (eq? (car name) 'quote)
  102. (symbol? (cadr name)))
  103. (cond ((search-documentation-files (cadr name))
  104. => write-line)
  105. (else (not-found 'documentation (cadr name)))))
  106. ;; (SYM1 SYM2 ...)
  107. ((and (list? name)
  108. (and-map symbol? name)
  109. (not (null? name))
  110. (not (eq? (car name) 'quote)))
  111. (cond ((module-commentary name)
  112. => (lambda (doc)
  113. (display name) (write-line " commentary:")
  114. (write-line doc)))
  115. (else (not-found 'commentary name))))
  116. ;; unrecognized
  117. (else
  118. (help-usage)))
  119. '(begin)))))
  120. (define (module-filename name) ; fixme: better way? / done elsewhere?
  121. (let* ((name (map symbol->string name))
  122. (reverse-name (reverse name))
  123. (leaf (car reverse-name))
  124. (dir-hint-module-name (reverse (cdr reverse-name)))
  125. (dir-hint (apply string-append
  126. (map (lambda (elt)
  127. (string-append elt "/"))
  128. dir-hint-module-name))))
  129. (%search-load-path (in-vicinity dir-hint leaf))))
  130. (define (module-commentary name)
  131. (cond ((module-filename name) => file-commentary)
  132. (else #f)))
  133. (define (help-doc term regexp)
  134. (let ((entries (apropos-fold (lambda (module name object data)
  135. (cons (list module
  136. name
  137. (try-value-help name object)
  138. (cond ((procedure? object)
  139. "a 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-hash-table)))
  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. (submodules (resolve-module '() #f)))
  354. (define (submodules mod)
  355. (hash-map->list (lambda (k v) v) (module-submodules mod)))
  356. (define apropos-fold-exported
  357. (make-fold-modules root-modules submodules module-public-interface))
  358. (define apropos-fold-all
  359. (make-fold-modules root-modules submodules identity))
  360. (define (source obj)
  361. (cond ((procedure? obj) (procedure-source obj))
  362. ((macro? obj) (procedure-source (macro-transformer obj)))
  363. (else #f)))
  364. (define (arity obj)
  365. (define (display-arg-list arg-list)
  366. (display #\`)
  367. (display (car arg-list))
  368. (let loop ((ls (cdr arg-list)))
  369. (cond ((null? ls)
  370. (display #\'))
  371. ((not (pair? ls))
  372. (display "', the rest in `")
  373. (display ls)
  374. (display #\'))
  375. (else
  376. (if (pair? (cdr ls))
  377. (display "', `")
  378. (display "' and `"))
  379. (display (car ls))
  380. (loop (cdr ls))))))
  381. (define (display-arg-list/summary arg-list type)
  382. (let ((len (length arg-list)))
  383. (display len)
  384. (display " ")
  385. (display type)
  386. (if (> len 1)
  387. (display " arguments: ")
  388. (display " argument: "))
  389. (display-arg-list arg-list)))
  390. (cond
  391. ((procedure-property obj 'arglist)
  392. => (lambda (arglist)
  393. (let ((required-args (car arglist))
  394. (optional-args (cadr arglist))
  395. (keyword-args (caddr arglist))
  396. (allow-other-keys? (cadddr arglist))
  397. (rest-arg (car (cddddr arglist)))
  398. (need-punctuation #f))
  399. (cond ((not (null? required-args))
  400. (display-arg-list/summary required-args "required")
  401. (set! need-punctuation #t)))
  402. (cond ((not (null? optional-args))
  403. (if need-punctuation (display ", "))
  404. (display-arg-list/summary optional-args "optional")
  405. (set! need-punctuation #t)))
  406. (cond ((not (null? keyword-args))
  407. (if need-punctuation (display ", "))
  408. (display-arg-list/summary keyword-args "keyword")
  409. (set! need-punctuation #t)))
  410. (cond (allow-other-keys?
  411. (if need-punctuation (display ", "))
  412. (display "other keywords allowed")
  413. (set! need-punctuation #t)))
  414. (cond (rest-arg
  415. (if need-punctuation (display ", "))
  416. (display "the rest in `")
  417. (display rest-arg)
  418. (display "'"))))))
  419. (else
  420. (let ((arity (procedure-minimum-arity obj)))
  421. (display (car arity))
  422. (cond ((caddr arity)
  423. (display " or more"))
  424. ((not (zero? (cadr arity)))
  425. (display " required and ")
  426. (display (cadr arity))
  427. (display " optional")))
  428. (if (and (not (caddr arity))
  429. (= (car arity) 1)
  430. (<= (cadr arity) 1))
  431. (display " argument")
  432. (display " arguments")))))
  433. (display ".\n"))
  434. (define (procedure-arguments proc)
  435. "Return an alist describing the arguments that `proc' accepts, or `#f'
  436. if the information cannot be obtained.
  437. The alist keys that are currently defined are `required', `optional',
  438. `keyword', and `rest'."
  439. (cond
  440. ((procedure-property proc 'arglist)
  441. => (lambda (arglist)
  442. `((required . ,(car arglist))
  443. (optional . ,(cadr arglist))
  444. (keyword . ,(caddr arglist))
  445. (rest . ,(car (cddddr arglist))))))
  446. ((procedure-source proc)
  447. => cadr)
  448. (((@ (system vm program) program?) proc)
  449. ((@ (system vm program) program-arguments-alist) proc))
  450. (else #f)))
  451. ;;; session.scm ends here