1 ;;;; Copyright (C) 2000,2001, 2002, 2003 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ;; * This module exports:
22 ;; file-commentary -- a procedure that returns a file's "commentary"
24 ;; documentation-files -- a search-list of files using the Guile
25 ;; Documentation Format Version 2.
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'.
33 ;; object-documentation -- a procedure that returns its arg's docstring
35 ;; * Guile Documentation Format
37 ;; Here is the complete and authoritative documentation for the Guile
38 ;; Documentation Format Version 2:
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.
55 ;; (Version 1, corresponding to guile-1.4 and prior, is documented as being
56 ;; not documented anywhere except by this embarrassingly circular comment.)
60 ;; A file's commentary is the body of text found between comments
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.
67 ;; You can specify to `file-commentary' alternate begin and end strings, and
68 ;; scrub procedure. Use #t to get default values. For example:
70 ;; (file-commentary "documentation.scm")
71 ;; You should see this text!
73 ;; (file-commentary "documentation.scm" "^;;; Code:" "ends here$")
74 ;; You should see the rest of this file.
76 ;; (file-commentary "documentation.scm" #t #t string-upcase)
77 ;; You should see this text very loudly (note semicolons untouched).
81 (define-module (ice-9 documentation)
82 :use-module (ice-9 rdelim)
83 :export (file-commentary
84 documentation-files search-documentation-files
86 :autoload (ice-9 regex) (match:suffix)
91 ;; commentary extraction
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 "^;+")))
97 (let ((m (regexp-exec dirt line)))
98 (if m (match:suffix m) line)))))
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))
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))
116 (let ((v (caddr cust)))
117 (cond ((procedure? v) v)
118 (else default-scrub))))))
119 (call-with-input-file filename
121 (let loop ((line (read-delimited "\n" port))
123 (parse-state 'before))
124 (if (or (eof-object? line) (eq? 'after parse-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)
132 (loop (read-delimited "\n" port)
133 (if (and (eq? 'in new-state) (eq? 'in parse-state))
134 (string-append doc (scrub line) "\n")
141 ;; documentation-files is the list of places to look for documentation
143 (define documentation-files
144 (map (lambda (vicinity)
145 (in-vicinity (vicinity) "guile-procedures.txt"))
151 (define entry-delimiter "\f")
153 (define (find-documentation-in-file name file)
154 (and (file-exists? file)
155 (call-with-input-file file
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)
163 ((and ;; large enough?
164 (>= (string-length entry) len)
166 (string=? (substring entry 0 len) name)
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)))))))))))
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)
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)))
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)
190 (and (defmacro? object)
191 (proc-doc (defmacro-transformer object)))
193 (let ((transformer (macro-transformer object)))
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))))
203 (set-procedure-property! object 'documentation docstring))
206 ;;; documentation.scm ends here