*** empty log message ***
[bpt/guile.git] / ice-9 / documentation.scm
CommitLineData
a3e01368 1;;;; Copyright (C) 2000,2001, 2002, 2003 Free Software Foundation, Inc.
70afc25b 2;;;;
73be1d9e
MV
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,
245dfe7f 9;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
92205699 15;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a482f2cc 16;;;;
70afc25b
TTN
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;;
115d80dc
TTN
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;;
70afc25b
TTN
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
115d80dc 43;;
70afc25b
TTN
44;; ^LPROC2
45;; DOCUMENTATION2
115d80dc
TTN
46;;
47;; ^L...
70afc25b
TTN
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
115d80dc
TTN
52;; formatting. Note that there are two newlines before the next formfeed;
53;; these are discarded when the documentation is read in.
70afc25b
TTN
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:
245dfe7f
MD
80
81(define-module (ice-9 documentation)
6d36532c 82 :use-module (ice-9 rdelim)
115d80dc
TTN
83 :export (file-commentary
84 documentation-files search-documentation-files
85 object-documentation)
70afc25b 86 :autoload (ice-9 regex) (match:suffix)
245dfe7f
MD
87 :no-backtrace)
88
89\f
70afc25b
TTN
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)
a3e01368
KR
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)))))))))
70afc25b
TTN
137
138\f
245dfe7f
MD
139
140;;
141;; documentation-files is the list of places to look for documentation
142;;
70afc25b 143(define documentation-files
245dfe7f
MD
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
245dfe7f 151(define entry-delimiter "\f")
245dfe7f
MD
152
153(define (find-documentation-in-file name file)
154 (and (file-exists? file)
a3e01368
KR
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?
db611983 164 (>= (string-length entry) len)
245dfe7f 165 ;; matching name?
db611983 166 (string=? (substring entry 0 len) name)
245dfe7f 167 ;; terminated?
db611983 168 (memq (string-ref entry len) '(#\newline)))
a3e01368
KR
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)))))))))))
245dfe7f 172
115d80dc
TTN
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
245dfe7f
MD
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
70afc25b
TTN
184(define (object-documentation object)
185 "Return the docstring for OBJECT.
186OBJECT can be a procedure, macro or any object that has its
187`documentation' property set."
245dfe7f
MD
188 (or (and (procedure? object)
189 (proc-doc object))
abce330c
MD
190 (and (defmacro? object)
191 (proc-doc (defmacro-transformer object)))
245dfe7f
MD
192 (and (macro? object)
193 (let ((transformer (macro-transformer object)))
194 (and transformer
195 (proc-doc transformer))))
196 (object-property object 'documentation)
245dfe7f
MD
197 (and (procedure? object)
198 (not (closure? object))
199 (procedure-name object)
115d80dc
TTN
200 (let ((docstring (search-documentation-files
201 (procedure-name object))))
245dfe7f
MD
202 (if docstring
203 (set-procedure-property! object 'documentation docstring))
204 docstring))))
70afc25b
TTN
205
206;;; documentation.scm ends here