merge from 1.8 branch
[bpt/guile.git] / ice-9 / documentation.scm
1 ;;;; Copyright (C) 2000,2001, 2002, 2003 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
18 ;;; Commentary:
19
20 ;; * This module exports:
21 ;;
22 ;; file-commentary -- a procedure that returns a file's "commentary"
23 ;;
24 ;; documentation-files -- a search-list of files using the Guile
25 ;; Documentation Format Version 2.
26 ;;
27 ;; search-documentation-files -- a procedure that takes NAME (a symbol)
28 ;; and searches `documentation-files' for
29 ;; associated documentation. optional
30 ;; arg FILES is a list of filenames to use
31 ;; instead of `documentation-files'.
32 ;;
33 ;; object-documentation -- a procedure that returns its arg's docstring
34 ;;
35 ;; * Guile Documentation Format
36 ;;
37 ;; Here is the complete and authoritative documentation for the Guile
38 ;; Documentation Format Version 2:
39 ;;
40 ;; HEADER
41 ;; ^LPROC1
42 ;; DOCUMENTATION1
43 ;;
44 ;; ^LPROC2
45 ;; DOCUMENTATION2
46 ;;
47 ;; ^L...
48 ;;
49 ;; The HEADER is completely ignored. The "^L" are formfeeds. PROC1, PROC2
50 ;; and so on are symbols that name the element documented. DOCUMENTATION1,
51 ;; DOCUMENTATION2 and so on are the related documentation, w/o any further
52 ;; formatting. Note that there are two newlines before the next formfeed;
53 ;; these are discarded when the documentation is read in.
54 ;;
55 ;; (Version 1, corresponding to guile-1.4 and prior, is documented as being
56 ;; not documented anywhere except by this embarrassingly circular comment.)
57 ;;
58 ;; * File Commentary
59 ;;
60 ;; A file's commentary is the body of text found between comments
61 ;; ;;; Commentary:
62 ;; and
63 ;; ;;; Code:
64 ;; both of which must be at the beginning of the line. In the result string,
65 ;; semicolons at the beginning of each line are discarded.
66 ;;
67 ;; You can specify to `file-commentary' alternate begin and end strings, and
68 ;; scrub procedure. Use #t to get default values. For example:
69 ;;
70 ;; (file-commentary "documentation.scm")
71 ;; You should see this text!
72 ;;
73 ;; (file-commentary "documentation.scm" "^;;; Code:" "ends here$")
74 ;; You should see the rest of this file.
75 ;;
76 ;; (file-commentary "documentation.scm" #t #t string-upcase)
77 ;; You should see this text very loudly (note semicolons untouched).
78
79 ;;; Code:
80
81 (define-module (ice-9 documentation)
82 :use-module (ice-9 rdelim)
83 :export (file-commentary
84 documentation-files search-documentation-files
85 object-documentation)
86 :autoload (ice-9 regex) (match:suffix)
87 :no-backtrace)
88
89 \f
90 ;;
91 ;; commentary extraction
92 ;;
93 (define default-in-line-re (make-regexp "^;;; Commentary:"))
94 (define default-after-line-re (make-regexp "^;;; Code:"))
95 (define default-scrub (let ((dirt (make-regexp "^;+")))
96 (lambda (line)
97 (let ((m (regexp-exec dirt line)))
98 (if m (match:suffix m) line)))))
99
100 (define (file-commentary filename . cust) ; (IN-LINE-RE AFTER-LINE-RE SCRUB)
101 ;; fixme: might be cleaner to use optargs here...
102 (let ((in-line-re (if (> 1 (length cust))
103 default-in-line-re
104 (let ((v (car cust)))
105 (cond ((regexp? v) v)
106 ((string? v) (make-regexp v))
107 (else default-in-line-re)))))
108 (after-line-re (if (> 2 (length cust))
109 default-after-line-re
110 (let ((v (cadr cust)))
111 (cond ((regexp? v) v)
112 ((string? v) (make-regexp v))
113 (else default-after-line-re)))))
114 (scrub (if (> 3 (length cust))
115 default-scrub
116 (let ((v (caddr cust)))
117 (cond ((procedure? v) v)
118 (else default-scrub))))))
119 (call-with-input-file filename
120 (lambda (port)
121 (let loop ((line (read-delimited "\n" port))
122 (doc "")
123 (parse-state 'before))
124 (if (or (eof-object? line) (eq? 'after parse-state))
125 doc
126 (let ((new-state
127 (cond ((regexp-exec in-line-re line) 'in)
128 ((regexp-exec after-line-re line) 'after)
129 (else parse-state))))
130 (if (eq? 'after new-state)
131 doc
132 (loop (read-delimited "\n" port)
133 (if (and (eq? 'in new-state) (eq? 'in parse-state))
134 (string-append doc (scrub line) "\n")
135 doc)
136 new-state)))))))))
137
138 \f
139
140 ;;
141 ;; documentation-files is the list of places to look for documentation
142 ;;
143 (define documentation-files
144 (map (lambda (vicinity)
145 (in-vicinity (vicinity) "guile-procedures.txt"))
146 (list %library-dir
147 %package-data-dir
148 %site-dir
149 (lambda () "."))))
150
151 (define entry-delimiter "\f")
152
153 (define (find-documentation-in-file name file)
154 (and (file-exists? file)
155 (call-with-input-file file
156 (lambda (port)
157 (let ((name (symbol->string name)))
158 (let ((len (string-length name)))
159 (read-delimited entry-delimiter port) ;skip to first entry
160 (let loop ((entry (read-delimited entry-delimiter port)))
161 (cond ((eof-object? entry) #f)
162 ;; match?
163 ((and ;; large enough?
164 (>= (string-length entry) len)
165 ;; matching name?
166 (string=? (substring entry 0 len) name)
167 ;; terminated?
168 (memq (string-ref entry len) '(#\newline)))
169 ;; cut away name tag and extra surrounding newlines
170 (substring entry (+ len 2) (- (string-length entry) 2)))
171 (else (loop (read-delimited entry-delimiter port)))))))))))
172
173 (define (search-documentation-files name . files)
174 (or-map (lambda (file)
175 (find-documentation-in-file name file))
176 (cond ((null? files) documentation-files)
177 (else files))))
178
179 ;; helper until the procedure documentation property is cleaned up
180 (define (proc-doc proc)
181 (or (procedure-documentation proc)
182 (procedure-property proc 'documentation)))
183
184 (define (object-documentation object)
185 "Return the docstring for OBJECT.
186 OBJECT can be a procedure, macro or any object that has its
187 `documentation' property set."
188 (or (and (procedure? object)
189 (proc-doc object))
190 (and (defmacro? object)
191 (proc-doc (defmacro-transformer object)))
192 (and (macro? object)
193 (let ((transformer (macro-transformer object)))
194 (and transformer
195 (proc-doc transformer))))
196 (object-property object 'documentation)
197 (and (procedure? object)
198 (not (closure? object))
199 (procedure-name object)
200 (let ((docstring (search-documentation-files
201 (procedure-name object))))
202 (if docstring
203 (set-procedure-property! object 'documentation docstring))
204 docstring))))
205
206 ;;; documentation.scm ends here