1 ;;;; Copyright (C) 2000,2001 Free Software Foundation, Inc.
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program 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
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
21 ;; * This module exports:
23 ;; file-commentary -- a procedure that returns a file's "commentary"
25 ;; documentation-files -- a search-list of files using the Guile
26 ;; Documentation Format Version 2.
28 ;; object-documentation -- a procedure that returns its arg's docstring
30 ;; * Guile Documentation Format
32 ;; Here is the complete and authoritative documentation for the Guile
33 ;; Documentation Format Version 2:
42 ;; The HEADER is completely ignored. The "^L" are formfeeds. PROC1, PROC2
43 ;; and so on are symbols that name the element documented. DOCUMENTATION1,
44 ;; DOCUMENTATION2 and so on are the related documentation, w/o any further
47 ;; (Version 1, corresponding to guile-1.4 and prior, is documented as being
48 ;; not documented anywhere except by this embarrassingly circular comment.)
52 ;; A file's commentary is the body of text found between comments
56 ;; both of which must be at the beginning of the line. In the result string,
57 ;; semicolons at the beginning of each line are discarded.
59 ;; You can specify to `file-commentary' alternate begin and end strings, and
60 ;; scrub procedure. Use #t to get default values. For example:
62 ;; (file-commentary "documentation.scm")
63 ;; You should see this text!
65 ;; (file-commentary "documentation.scm" "^;;; Code:" "ends here$")
66 ;; You should see the rest of this file.
68 ;; (file-commentary "documentation.scm" #t #t string-upcase)
69 ;; You should see this text very loudly (note semicolons untouched).
73 (define-module (ice-9 documentation)
74 :use-module (ice-9 rdelim)
75 :export (file-commentary documentation-files object-documentation)
76 :autoload (ice-9 regex) (match:suffix)
81 ;; commentary extraction
83 (define default-in-line-re (make-regexp "^;;; Commentary:"))
84 (define default-after-line-re (make-regexp "^;;; Code:"))
85 (define default-scrub (let ((dirt (make-regexp "^;+")))
87 (let ((m (regexp-exec dirt line)))
88 (if m (match:suffix m) line)))))
90 (define (file-commentary filename . cust) ; (IN-LINE-RE AFTER-LINE-RE SCRUB)
91 ;; fixme: might be cleaner to use optargs here...
92 (let ((in-line-re (if (> 1 (length cust))
96 ((string? v) (make-regexp v))
97 (else default-in-line-re)))))
98 (after-line-re (if (> 2 (length cust))
100 (let ((v (cadr cust)))
101 (cond ((regexp? v) v)
102 ((string? v) (make-regexp v))
103 (else default-after-line-re)))))
104 (scrub (if (> 3 (length cust))
106 (let ((v (caddr cust)))
107 (cond ((procedure? v) v)
108 (else default-scrub)))))
109 (port (open-input-file filename)))
110 (let loop ((line (read-delimited "\n" port))
112 (parse-state 'before))
113 (if (or (eof-object? line) (eq? 'after parse-state))
116 (cond ((regexp-exec in-line-re line) 'in)
117 ((regexp-exec after-line-re line) 'after)
118 (else parse-state))))
119 (if (eq? 'after new-state)
121 (loop (read-delimited "\n" port)
122 (if (and (eq? 'in new-state) (eq? 'in parse-state))
123 (string-append doc (scrub line) "\n")
130 ;; documentation-files is the list of places to look for documentation
132 (define documentation-files
133 (map (lambda (vicinity)
134 (in-vicinity (vicinity) "guile-procedures.txt"))
140 (define (find-documentation name)
141 (or-map (lambda (file)
142 (find-documentation-in-file name file))
143 documentation-files))
145 (define entry-delimiter "\f")
147 (define (find-documentation-in-file name file)
148 (and (file-exists? file)
149 (let ((port (open-input-file file))
150 (name (symbol->string name)))
151 (let ((len (string-length name)))
152 (read-delimited entry-delimiter port) ;skip to first entry
153 (let loop ((entry (read-delimited entry-delimiter port)))
154 (cond ((eof-object? entry) #f)
156 ((and ;; large enough?
157 (>= (string-length entry) len)
159 (string=? (substring entry 0 len) name)
161 (memq (string-ref entry len) '(#\newline)))
162 ;; cut away name tag and extra surrounding newlines
163 (substring entry (+ len 2) (- (string-length entry) 2)))
164 (else (loop (read-delimited entry-delimiter port)))))))))
166 ;; helper until the procedure documentation property is cleaned up
167 (define (proc-doc proc)
168 (or (procedure-documentation proc)
169 (procedure-property proc 'documentation)))
171 (define (object-documentation object)
172 "Return the docstring for OBJECT.
173 OBJECT can be a procedure, macro or any object that has its
174 `documentation' property set."
175 (or (and (procedure? object)
178 (let ((transformer (macro-transformer object)))
180 (proc-doc transformer))))
181 (object-property object 'documentation)
182 ;; find-documentation currently only works for builtin primitives
183 (and (procedure? object)
184 (not (closure? object))
185 (procedure-name object)
186 (let ((docstring (find-documentation (procedure-name object))))
188 (set-procedure-property! object 'documentation docstring))
191 ;;; documentation.scm ends here