PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edwin/docstr.scm

#
Scheme | 203 lines | 163 code | 13 blank | 27 comment | 0 complexity | c672be70d907f349a1c8c8acc12d29f5 MD5 | raw file
Possible License(s): GPL-2.0
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Documentation Strings
  21. (declare (usual-integrations))
  22. (define *external-doc-strings?* #t)
  23. (define *external-doc-strings-file* #f)
  24. (define *doc-strings* #f)
  25. (define *doc-string-posn* 0)
  26. (define *doc-string-channel* #f)
  27. (define *doc-string-buffer* #f)
  28. (define (doc-string->posn name str)
  29. (if (and *external-doc-strings?* (string? str))
  30. (let ((nlen (string-length name))
  31. (dslen (string-length str))
  32. (slen (if (not *doc-strings*)
  33. 0
  34. (string-length *doc-strings*)))
  35. (posn *doc-string-posn*))
  36. (let* ((next (fix:+ posn nlen))
  37. (end (fix:+ next (fix:+ dslen 6))))
  38. (if (> end slen)
  39. (let ((new (string-allocate
  40. (max end
  41. (if (fix:zero? slen)
  42. 4096
  43. (fix:+ slen (fix:quotient slen 2)))))))
  44. (if *doc-strings*
  45. (substring-move-right! *doc-strings* 0 posn new 0))
  46. (set! *doc-strings* new)))
  47. (let ((doc-strings *doc-strings*))
  48. (vector-8b-set! doc-strings posn (fix:remainder dslen 256))
  49. (vector-8b-set! doc-strings
  50. (fix:+ posn 1)
  51. (fix:quotient dslen 256))
  52. (string-set! doc-strings (fix:+ posn 2) #\Newline)
  53. (substring-move-right! name 0 nlen doc-strings (fix:+ posn 3))
  54. (string-set! doc-strings (fix:+ next 3) #\Newline)
  55. (substring-move-right! str 0 dslen doc-strings (fix:+ next 4))
  56. (string-set! doc-strings (fix:- end 2) #\Newline)
  57. (string-set! doc-strings (fix:- end 1) #\Newline)
  58. (set! *doc-string-posn* end)
  59. posn)))
  60. str))
  61. (define-integrable doc-string-buffer-length 512)
  62. (define (->doc-string name posn)
  63. (define (out-of-range)
  64. (editor-error "->doc-string: Out of range argument" posn))
  65. (define (fill-buffer channel buffer posn blen)
  66. (let fill-loop ((posn posn))
  67. (if (fix:< posn blen)
  68. (let ((n (channel-read-block channel buffer posn blen)))
  69. (fill-loop (fix:+ posn n))))))
  70. (define (verify-and-extract buffer nlen dslen nposn)
  71. (let ((nend (fix:+ nposn nlen)))
  72. (if (not (string=? (substring buffer nposn nend) name))
  73. (editor-error "->doc-string: Inconsistency" posn)
  74. (let ((dstart (fix:+ nend 1)))
  75. (substring buffer dstart (fix:+ dstart dslen))))))
  76. (cond ((string? posn)
  77. posn)
  78. ((not (fix:fixnum? posn))
  79. (editor-error "->doc-string: Wrong type argument" posn))
  80. ((fix:< posn 0)
  81. (out-of-range))
  82. (*doc-strings*
  83. (let ((slen (string-length *doc-strings*))
  84. (nlen (string-length name)))
  85. (if (fix:> (fix:+ posn 2) slen)
  86. (out-of-range))
  87. (let ((dslen
  88. (fix:+ (vector-8b-ref *doc-strings* posn)
  89. (fix:lsh (vector-8b-ref *doc-strings* (fix:+ posn 1))
  90. 8))))
  91. (if (fix:> (fix:+ (fix:+ posn 6) (fix:+ nlen dslen)) slen)
  92. (out-of-range)
  93. (verify-and-extract *doc-strings* nlen dslen
  94. (fix:+ posn 3))))))
  95. (else
  96. (guarantee-doc-string-state)
  97. (let* ((channel *doc-string-channel*)
  98. (buffer *doc-string-buffer*)
  99. (flen (channel-file-length channel))
  100. (nlen (string-length name))
  101. (delta (fix:- flen (fix:+ posn 2))))
  102. (if (fix:< delta 0)
  103. (out-of-range))
  104. (channel-file-set-position channel posn)
  105. (let ((blen (min doc-string-buffer-length delta)))
  106. (fill-buffer channel buffer 0 blen)
  107. (let* ((dslen (fix:+ (vector-8b-ref buffer 0)
  108. (fix:lsh (vector-8b-ref buffer 1)
  109. 8)))
  110. (end (fix:+ (fix:+ dslen nlen) 6)))
  111. (cond ((not (fix:> end blen))
  112. (verify-and-extract buffer nlen dslen 3))
  113. ((fix:> (fix:+ end posn) flen)
  114. (out-of-range))
  115. (else
  116. (let* ((rlen (fix:+ (fix:+ nlen dslen) 1))
  117. (result (string-allocate rlen)))
  118. (substring-move-right! buffer 3 blen result 0)
  119. (fill-buffer channel result (fix:- blen 3) rlen)
  120. (verify-and-extract result nlen dslen 0))))))))))
  121. (define (dump-doc-strings output #!optional permanent)
  122. (if (not *doc-strings*)
  123. (error "dump-doc-strings: No doc strings to dump!"))
  124. (set! *external-doc-strings-file*
  125. (if (or (default-object? permanent)
  126. (not permanent))
  127. output
  128. permanent))
  129. (set-string-length! *doc-strings* *doc-string-posn*)
  130. (call-with-binary-output-file
  131. output
  132. (lambda (port)
  133. (output-port/write-string port *doc-strings*)))
  134. (set! *external-doc-strings?* #f)
  135. (set! *doc-string-posn* 0)
  136. (set! *doc-strings* #f)
  137. unspecific)
  138. (define (guarantee-doc-string-state)
  139. (if (not *doc-string-buffer*)
  140. (set! *doc-string-buffer* (string-allocate doc-string-buffer-length)))
  141. (cond (*doc-string-channel*)
  142. ((not *external-doc-strings-file*)
  143. (editor-error
  144. "guarantee-doc-string-channel: Undeclared doc-string file"))
  145. (else
  146. (let ((doc-strings
  147. (if (or (pathname-absolute? *external-doc-strings-file*)
  148. (file-exists? *external-doc-strings-file*))
  149. *external-doc-strings-file*
  150. (merge-pathnames *external-doc-strings-file*
  151. (edwin-etc-directory)))))
  152. (if (not (file-exists? doc-strings))
  153. (editor-error
  154. "guarantee-doc-string-channel: Non-existent doc-string file")
  155. (begin
  156. (set! *doc-string-channel*
  157. (file-open-input-channel
  158. (->namestring doc-strings)))
  159. unspecific))))))
  160. (add-event-receiver! event:after-restart
  161. (lambda () (set! *doc-string-channel* #f)))
  162. ;;;; Abstraction of help descriptions
  163. (define (description? description)
  164. (or (string? description)
  165. (and (procedure? description)
  166. (procedure-arity-valid? description 0))))
  167. (define (description->string description)
  168. (cond ((string? description) description)
  169. ((procedure? description) (description))
  170. (else
  171. (error:wrong-type-argument description "description"
  172. 'DESCRIPTION->STRING))))
  173. (define (description-first-line description)
  174. (let ((string (description->string description)))
  175. (let ((index (string-find-next-char string #\newline)))
  176. (if index
  177. (substring string 0 index)
  178. string))))
  179. (define (description-append . descriptions)
  180. (lambda () (apply string-append (map description->string descriptions))))