session.scm 18 KB

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