3 ;;;; A docstring extractor for the sbcl manual. Creates
4 ;;;; @include-ready documentation from the docstrings of exported
5 ;;;; symbols of specified packages.
7 ;;;; This software is part of the SBCL software system. SBCL is in the
8 ;;;; public domain and is provided with absolutely no warranty. See
9 ;;;; the COPYING file for more information.
11 ;;;; Written by Rudi Schlatte <rudi@constantly.at>, mangled
12 ;;;; by Nikodemus Siivola.
17 ;;;; * Method documentation untested
18 ;;;; * Method sorting, somehow
19 ;;;; * Index for macros & constants?
20 ;;;; * This is getting complicated enough that tests would be good
21 ;;;; * Nesting (currently only nested itemizations work)
22 ;;;; * doc -> internal form -> texinfo (so that non-texinfo format are also
23 ;;;; easily generated)
25 ;;;; FIXME: The description below is no longer complete. This
26 ;;;; should possibly be turned into a contrib with proper documentation.
28 ;;;; Formatting heuristics (tweaked to format SAVE-LISP-AND-DIE sanely):
30 ;;;; Formats SYMBOL as @code{symbol}, or @var{symbol} if symbol is in
31 ;;;; the argument list of the defun / defmacro.
33 ;;;; Lines starting with * or - that are followed by intented lines
34 ;;;; are marked up with @itemize.
36 ;;;; Lines containing only a SYMBOL that are followed by indented
37 ;;;; lines are marked up as @table @code, with the SYMBOL as the item.
39 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
40 (require 'sb-introspect
))
42 (defpackage :sb-texinfo
44 (:shadow
#:documentation
)
45 (:export
#:generate-includes
#:document-package
)
47 "Tools to generate TexInfo documentation from docstrings."))
49 (in-package :sb-texinfo
)
51 ;;;; various specials and parameters
53 (defvar *texinfo-output
*)
54 (defvar *texinfo-variables
*)
55 (defvar *documentation-package
*)
57 (defparameter *undocumented-packages
* '(sb-pcl sb-int sb-kernel sb-sys sb-c
))
59 (defparameter *documentation-types
*
64 ;;structure ; also handled by `type'
67 "A list of symbols accepted as second argument of `documentation'")
69 (defparameter *character-replacements
*
70 '((#\
* .
"star") (#\
/ .
"slash") (#\
+ .
"plus")
71 (#\
< .
"lt") (#\
> .
"gt"))
72 "Characters and their replacement names that `alphanumize' uses. If
73 the replacements contain any of the chars they're supposed to replace,
74 you deserve to lose.")
76 (defparameter *characters-to-drop
* '(#\\ #\
` #\')
77 "Characters that should be removed by `alphanumize'.")
79 (defparameter *texinfo-escaped-chars
* "@{}"
80 "Characters that must be escaped with #\@ for Texinfo.")
82 (defparameter *itemize-start-characters
* '(#\
* #\-
)
83 "Characters that might start an itemization in docstrings when
84 at the start of a line.")
86 (defparameter *symbol-characters
* "ABCDEFGHIJKLMNOPQRSTUVWXYZ*:-+&#'"
87 "List of characters that make up symbols in a docstring.")
89 (defparameter *symbol-delimiters
* " ,.!?;()")
91 (defparameter *ordered-documentation-kinds
*
92 '(package type structure condition class macro
))
100 (nconc (flatten (car list
)) (flatten (cdr list
))))
102 (cons (car list
) nil
))
104 (cons (car list
) (flatten (cdr list
))))))
106 (defun whitespacep (char)
107 (find char
#(#\tab
#\space
#\page
)))
109 (defun setf-name-p (name)
111 (and (listp name
) (= 2 (length name
)) (eq (car name
) 'setf
))))
113 (defgeneric specializer-name
(specializer))
115 (defmethod specializer-name ((specializer eql-specializer
))
116 (list 'eql
(eql-specializer-object specializer
)))
118 (defmethod specializer-name ((specializer class
))
119 (class-name specializer
))
121 (defun ensure-class-precedence-list (class)
122 (unless (class-finalized-p class
)
123 (finalize-inheritance class
))
124 (class-precedence-list class
))
126 (defun specialized-lambda-list (method)
127 ;; courtesy of AMOP p. 61
128 (let* ((specializers (method-specializers method
))
129 (lambda-list (method-lambda-list method
))
130 (n-required (length specializers
)))
131 (append (mapcar (lambda (arg specializer
)
132 (if (eq specializer
(find-class 't
))
134 `(,arg
,(specializer-name specializer
))))
135 (subseq lambda-list
0 n-required
)
137 (subseq lambda-list n-required
))))
139 (defun string-lines (string)
140 "Lines in STRING as a vector."
141 (coerce (with-input-from-string (s string
)
142 (loop for line
= (read-line s nil nil
)
143 while line collect line
))
146 (defun indentation (line)
147 "Position of first non-SPACE character in LINE."
148 (position-if-not (lambda (c) (char= c
#\Space
)) line
))
150 (defun docstring (x doc-type
)
151 (cl:documentation x doc-type
))
153 (defun flatten-to-string (list)
154 (format nil
"~{~A~^-~}" (flatten list
)))
156 (defun alphanumize (original)
157 "Construct a string without characters like *`' that will f-star-ck
158 up filename handling. See `*character-replacements*' and
159 `*characters-to-drop*' for customization."
160 (let ((name (remove-if (lambda (x) (member x
*characters-to-drop
*))
162 (flatten-to-string original
)
164 (chars-to-replace (mapcar #'car
*character-replacements
*)))
165 (flet ((replacement-delimiter (index)
166 (cond ((or (< index
0) (>= index
(length name
))) "")
167 ((alphanumericp (char name index
)) "-")
169 (loop for index
= (position-if #'(lambda (x) (member x chars-to-replace
))
172 do
(setf name
(concatenate 'string
(subseq name
0 index
)
173 (replacement-delimiter (1- index
))
174 (cdr (assoc (aref name index
)
175 *character-replacements
*))
176 (replacement-delimiter (1+ index
))
177 (subseq name
(1+ index
))))))
180 ;;;; generating various names
182 (defgeneric name
(thing)
183 (:documentation
"Name for a documented thing. Names are either
184 symbols or lists of symbols."))
186 (defmethod name ((symbol symbol
))
189 (defmethod name ((cons cons
))
192 (defmethod name ((package package
))
193 (package-name package
))
195 (defmethod name ((method method
))
197 (generic-function-name (method-generic-function method
))
198 (method-qualifiers method
)
199 (specialized-lambda-list method
)))
201 ;;; Node names for DOCUMENTATION instances
203 (defgeneric name-using-kind
/name
(kind name doc
))
205 (defmethod name-using-kind/name
(kind (name string
) doc
)
206 (declare (ignore kind doc
))
209 (defmethod name-using-kind/name
(kind (name symbol
) doc
)
210 (declare (ignore kind
))
211 (format nil
"~A ~A" (package-name (get-package doc
)) name
))
213 (defmethod name-using-kind/name
(kind (name list
) doc
)
214 (declare (ignore kind
))
215 (assert (setf-name-p name
))
216 (format nil
"(setf ~A ~A)" (package-name (get-package doc
)) (second name
)))
218 (defmethod name-using-kind/name
((kind (eql 'method
)) name doc
)
219 (flet ((specializers (ll)
223 ((member arg lambda-list-keywords
) (return))
224 ((atom arg
) (push t result
))
225 (t (push (second arg
) result
))))
227 (format nil
"~A~{ ~A~} ~A"
228 (name-using-kind/name nil
(first name
) doc
)
230 (specializers (third name
)))))
232 (defun node-name (doc)
233 "Returns TexInfo node name as a string for a DOCUMENTATION instance."
234 (let ((kind (get-kind doc
)))
235 (format nil
"~:(~A~) ~(~A~)" kind
(name-using-kind/name kind
(get-name doc
) doc
))))
237 (defun package-shortest-name (package)
238 (let* ((names (cons (package-name package
) (package-nicknames package
)))
239 (sorted (sort (copy-list names
) #'< :key
#'length
)))
242 (defun package-macro-name (package)
243 (let ((short-name (package-shortest-name package
)))
244 (remove-if-not #'alpha-char-p
(string-downcase short-name
))))
246 ;;; Definition titles for DOCUMENTATION instances
248 (defgeneric title-using-kind
/name
(kind name doc
))
250 (defmethod title-using-kind/name
(kind (name string
) doc
)
251 (declare (ignore kind doc
))
254 (defmethod title-using-kind/name
(kind (name symbol
) doc
)
255 (declare (ignore kind
))
256 (let* ((symbol-name (symbol-name name
))
257 (earmuffsp (and (char= (char symbol-name
0) #\
*)
258 (char= (char symbol-name
(1- (length symbol-name
))) #\
*)
259 (some #'alpha-char-p symbol-name
))))
261 (format nil
"@~A{@earmuffs{~A}}" (package-macro-name (get-package doc
)) (subseq symbol-name
1 (1- (length symbol-name
))))
262 (format nil
"@~A{~A}" (package-macro-name (get-package doc
)) name
))))
264 (defmethod title-using-kind/name
(kind (name list
) doc
)
265 (declare (ignore kind
))
266 (assert (setf-name-p name
))
267 (format nil
"@setf{@~A{~A}}" (package-macro-name (get-package doc
)) (second name
)))
269 (defmethod title-using-kind/name
((kind (eql 'method
)) name doc
)
270 (format nil
"~{~A ~}~A"
272 (title-using-kind/name nil
(first name
) doc
)))
274 (defun title-name (doc)
275 "Returns a string to be used as name of the definition."
276 (string-downcase (title-using-kind/name
(get-kind doc
) (get-name doc
) doc
)))
278 (defun include-pathname (doc)
279 (let* ((kind (get-kind doc
))
280 (name (nstring-downcase
281 (if (eq 'package kind
)
282 (format nil
"package-~A" (alphanumize (get-name doc
)))
283 (format nil
"~A-~A-~A"
285 ((function generic-function
) "fun")
288 (otherwise (symbol-name (get-kind doc
))))
289 (alphanumize (package-name (get-package doc
)))
290 (alphanumize (get-name doc
)))))))
291 (make-pathname :name name
:type
"texinfo")))
293 ;;;; documentation class and related methods
295 (defclass documentation
()
296 ((name :initarg
:name
:reader get-name
)
297 (kind :initarg
:kind
:reader get-kind
)
298 (string :initarg
:string
:reader get-string
)
299 (children :initarg
:children
:initform nil
:reader get-children
)
300 (package :initform
*documentation-package
* :reader get-package
)))
302 (defmethod print-object ((documentation documentation
) stream
)
303 (print-unreadable-object (documentation stream
:type t
)
304 (princ (list (get-kind documentation
) (get-name documentation
)) stream
)))
306 (defgeneric make-documentation
(x doc-type string
))
308 (defmethod make-documentation ((x package
) doc-type string
)
309 (declare (ignore doc-type
))
310 (make-instance 'documentation
315 (defmethod make-documentation (x (doc-type (eql 'function
)) string
)
316 (declare (ignore doc-type
))
317 (let* ((fdef (and (fboundp x
) (fdefinition x
)))
319 (kind (cond ((and (symbolp x
) (special-operator-p x
))
321 ((and (symbolp x
) (macro-function x
))
323 ((typep fdef
'generic-function
)
324 (assert (or (symbolp name
) (setf-name-p name
)))
327 (assert (or (symbolp name
) (setf-name-p name
)))
329 (children (when (eq kind
'generic-function
)
330 (collect-gf-documentation fdef
))))
331 (make-instance 'documentation
335 :children children
)))
337 (defmethod make-documentation ((x method
) doc-type string
)
338 (declare (ignore doc-type
))
339 (make-instance 'documentation
344 (defmethod make-documentation (x (doc-type (eql 'type
)) string
)
345 (make-instance 'documentation
348 :kind
(etypecase (find-class x nil
)
349 (structure-class 'structure
)
350 (standard-class 'class
)
351 (sb-pcl::condition-class
'condition
)
352 ((or built-in-class null
) 'type
))))
354 (defmethod make-documentation (x (doc-type (eql 'variable
)) string
)
355 (make-instance 'documentation
358 :kind
(if (constantp x
)
362 (defmethod make-documentation (x (doc-type (eql 'setf
)) string
)
363 (declare (ignore doc-type
))
364 (make-instance 'documentation
369 (defmethod make-documentation (x doc-type string
)
370 (make-instance 'documentation
375 (defun maybe-documentation (x doc-type
)
376 "Returns a DOCUMENTATION instance for X and DOC-TYPE, or NIL if
377 there is no corresponding docstring."
378 (let ((docstring (docstring x doc-type
)))
380 (make-documentation x doc-type docstring
))))
382 (defun lambda-list (doc)
384 ((package constant variable type structure class condition nil
)
387 (third (get-name doc
)))
391 ;; believe it or not, the above comment was written before CSR
392 ;; came along and obfuscated this. (2005-07-04)
393 (when (symbolp (get-name doc
))
394 (labels ((clean (x &key optional key
)
397 ((cons (member &optional
))
398 (cons (car x
) (clean (cdr x
) :optional t
)))
399 ((cons (member &key
))
400 (cons (car x
) (clean (cdr x
) :key t
)))
401 ((cons (member &whole
&environment
))
403 (clean (cdr x
) :optional optional
:key key
))
406 (cond (key (if (consp (caar x
))
411 (clean (cdr x
) :key key
:optional optional
)))
414 (cond ((or key optional
) (car x
))
416 (clean (cdr x
) :key key
:optional optional
))))))
417 (multiple-value-bind (ll unknown
) (sb-introspect:function-lambda-list
(get-name doc
))
422 (defun get-string-name (x)
423 (let ((name (get-name x
)))
424 (cond ((symbolp name
)
426 ((and (consp name
) (eq 'setf
(car name
)))
427 (symbol-name (second name
)))
431 (error "Don't know which symbol to use for name ~S" name
)))))
433 (defun documentation< (x y
)
434 (let ((p1 (position (get-kind x
) *ordered-documentation-kinds
*))
435 (p2 (position (get-kind y
) *ordered-documentation-kinds
*)))
436 (if (or (not (and p1 p2
)) (= p1 p2
))
437 (string< (get-string-name x
) (get-string-name y
))
440 ;;;; turning text into texinfo
442 (defun escape-for-texinfo (string &optional downcasep
)
443 "Return STRING with characters in *TEXINFO-ESCAPED-CHARS* escaped
444 with #\@. Optionally downcase the result."
445 (let ((result (with-output-to-string (s)
446 (loop for char across string
447 when
(find char
*texinfo-escaped-chars
*)
448 do
(write-char #\
@ s
)
449 do
(write-char char s
)))))
450 (if downcasep
(nstring-downcase result
) result
)))
452 (defun empty-p (line-number lines
)
453 (and (< -
1 line-number
(length lines
))
454 (not (indentation (svref lines line-number
)))))
458 (defvar *not-symbols
* '("ANSI" "CLHS" "UNIX"))
460 (defun locate-symbols (line)
461 "Return a list of index pairs of symbol-like parts of LINE."
462 ;; This would be a good application for a regex ...
464 (flet ((grab (start end
)
465 (unless (member (subseq line start end
) *not-symbols
*)
466 (push (list start end
) result
)))
467 (got-symbol-p (start)
468 (let ((end (when (< start
(length line
))
469 (position #\space line
:start start
))))
471 (every (lambda (char) (find char
*symbol-characters
*))
472 (subseq line start end
))))))
476 ((>= i
(length line
))
477 ;; symbol at end of line
478 (when (and begin
(or (> i
(1+ begin
))
479 (not (member (char line begin
) '(#\A
#\I
)))))
483 ((and begin
(find (char line i
) *symbol-delimiters
*))
484 ;; symbol end; remember it if it's not "A" or "I"
485 (when (or (> i
(1+ begin
)) (not (member (char line begin
) '(#\A
#\I
))))
489 ((and begin
(not (find (char line i
) *symbol-characters
*)))
490 ;; Not a symbol: abort
492 ((and maybe-begin
(not begin
) (find (char line i
) *symbol-characters
*))
493 ;; potential symbol begin at this position
496 ((find (char line i
) *symbol-delimiters
*)
497 ;; potential symbol begin after this position
498 (setf maybe-begin t
))
499 ((and (eql #\
( (char line i
)) (got-symbol-p (1+ i
)))
500 ;; a type designator, or a function call as part of the text?
501 (multiple-value-bind (exp end
)
502 (let ((*package
* (find-package :cl-user
)))
503 (ignore-errors (read-from-string line nil nil
:start i
)))
510 ;; Not reading a symbol, not at potential start of symbol
511 (setf maybe-begin nil
)))))))
513 (defun texinfo-line (line)
514 "Format symbols in LINE texinfo-style: either as code or as
515 variables if the symbol in question is contained in symbols
516 *TEXINFO-VARIABLES*."
517 (with-output-to-string (result)
519 (dolist (symbol/index
(locate-symbols line
))
520 (write-string (subseq line last
(first symbol
/index
)) result
)
521 (let ((symbol-name (apply #'subseq line symbol
/index
)))
522 (format result
(if (member symbol-name
*texinfo-variables
*
526 (string-downcase symbol-name
)))
527 (setf last
(second symbol
/index
)))
528 (write-string (subseq line last
) result
))))
532 (defun lisp-section-p (line line-number lines
)
533 "Returns T if the given LINE looks like start of lisp code --
534 ie. if it starts with whitespace followed by a paren or
535 semicolon, and the previous line is empty"
536 (let ((offset (indentation line
)))
539 (find (find-if-not #'whitespacep line
) "(;")
540 (empty-p (1- line-number
) lines
))))
542 (defun collect-lisp-section (lines line-number
)
543 (flet ((maybe-line (index)
544 (and (< index
(length lines
)) (svref lines index
))))
545 (let ((lisp (loop for index
= line-number then
(1+ index
)
546 for line
= (maybe-line index
)
547 while
(or (indentation line
)
548 ;; Allow empty lines in middle of lisp sections.
549 (let ((next (1+ index
)))
550 (lisp-section-p (maybe-line next
) next lines
)))
552 (values (length lisp
) `("@lisp" ,@lisp
"@end lisp")))))
554 ;;; itemized sections
556 (defun maybe-itemize-offset (line)
557 "Return NIL or the indentation offset if LINE looks like it starts
558 an item in an itemization."
559 (let* ((offset (indentation line
))
560 (char (when offset
(char line offset
))))
562 (member char
*itemize-start-characters
* :test
#'char
=)
563 (char= #\Space
(find-if-not (lambda (c) (char= c char
))
567 (defun collect-maybe-itemized-section (lines starting-line
)
568 ;; Return index of next line to be processed outside
569 (let ((this-offset (maybe-itemize-offset (svref lines starting-line
)))
572 (loop for line-number from starting-line below
(length lines
)
573 for line
= (svref lines line-number
)
574 for indentation
= (indentation line
)
575 for offset
= (maybe-itemize-offset line
)
578 ;; empty line -- inserts paragraph.
580 (incf lines-consumed
))
581 ((and offset
(> indentation this-offset
))
582 ;; nested itemization -- handle recursively
583 ;; FIXME: tables in itemizations go wrong
584 (multiple-value-bind (sub-lines-consumed sub-itemization
)
585 (collect-maybe-itemized-section lines line-number
)
586 (when sub-lines-consumed
587 (incf line-number
(1- sub-lines-consumed
)) ; +1 on next loop
588 (incf lines-consumed sub-lines-consumed
)
589 (setf result
(append (reverse sub-itemization
) result
)))))
590 ((and offset
(= indentation this-offset
))
592 (push (format nil
"@item ~A"
593 (texinfo-line (subseq line
(1+ offset
))))
595 (incf lines-consumed
))
596 ((and (not offset
) (> indentation this-offset
))
597 ;; continued item from previous line
598 (push (texinfo-line line
) result
)
599 (incf lines-consumed
))
601 ;; end of itemization
603 ;; a single-line itemization isn't.
604 (if (> (count-if (lambda (line) (> (length line
) 0)) result
) 1)
605 (values lines-consumed
`("@itemize" ,@(reverse result
) "@end itemize"))
610 (defun tabulation-body-p (offset line-number lines
)
611 (when (< line-number
(length lines
))
612 (let ((offset2 (indentation (svref lines line-number
))))
613 (and offset2
(< offset offset2
)))))
615 (defun tabulation-p (offset line-number lines direction
)
616 (let ((step (ecase direction
617 (:backwards
(1- line-number
))
618 (:forwards
(1+ line-number
)))))
619 (when (and (plusp line-number
) (< line-number
(length lines
)))
620 (and (eql offset
(indentation (svref lines line-number
)))
621 (or (when (eq direction
:backwards
)
622 (empty-p step lines
))
623 (tabulation-p offset step lines direction
)
624 (tabulation-body-p offset step lines
))))))
626 (defun maybe-table-offset (line-number lines
)
627 "Return NIL or the indentation offset if LINE looks like it starts
628 an item in a tabulation. Ie, if it is (1) indented, (2) preceded by an
629 empty line, another tabulation label, or a tabulation body, (3) and
630 followed another tabulation label or a tabulation body."
631 (let* ((line (svref lines line-number
))
632 (offset (indentation line
))
633 (prev (1- line-number
))
634 (next (1+ line-number
)))
635 (when (and offset
(plusp offset
))
636 (and (or (empty-p prev lines
)
637 (tabulation-body-p offset prev lines
)
638 (tabulation-p offset prev lines
:backwards
))
639 (or (tabulation-body-p offset next lines
)
640 (tabulation-p offset next lines
:forwards
))
643 ;;; FIXME: This and itemization are very similar: could they share
644 ;;; some code, mayhap?
646 (defun collect-maybe-table-section (lines starting-line
)
647 ;; Return index of next line to be processed outside
648 (let ((this-offset (maybe-table-offset starting-line lines
))
651 (loop for line-number from starting-line below
(length lines
)
652 for line
= (svref lines line-number
)
653 for indentation
= (indentation line
)
654 for offset
= (maybe-table-offset line-number lines
)
657 ;; empty line -- inserts paragraph.
659 (incf lines-consumed
))
660 ((and offset
(= indentation this-offset
))
661 ;; start of new item, or continuation of previous item
662 (if (and result
(search "@item" (car result
) :test
#'char
=))
663 (push (format nil
"@itemx ~A" (texinfo-line line
))
667 (push (format nil
"@item ~A" (texinfo-line line
))
669 (incf lines-consumed
))
670 ((> indentation this-offset
)
671 ;; continued item from previous line
672 (push (texinfo-line line
) result
)
673 (incf lines-consumed
))
675 ;; end of itemization
677 ;; a single-line table isn't.
678 (if (> (count-if (lambda (line) (> (length line
) 0)) result
) 1)
679 (values lines-consumed
680 `("" "@table @emph" ,@(reverse result
) "@end table" ""))
685 (defmacro with-maybe-section
(index &rest forms
)
686 `(multiple-value-bind (count collected
) (progn ,@forms
)
688 (dolist (line collected
)
689 (write-line line
*texinfo-output
*))
690 (incf ,index
(1- count
)))))
692 (defun write-texinfo-string (string &optional lambda-list
)
693 "Try to guess as much formatting for a raw docstring as possible."
694 (let ((*texinfo-variables
* (flatten lambda-list
))
695 (lines (string-lines (escape-for-texinfo string nil
))))
696 (loop for line-number from
0 below
(length lines
)
697 for line
= (svref lines line-number
)
699 ((with-maybe-section line-number
700 (and (lisp-section-p line line-number lines
)
701 (collect-lisp-section lines line-number
))))
702 ((with-maybe-section line-number
703 (and (maybe-itemize-offset line
)
704 (collect-maybe-itemized-section lines line-number
))))
705 ((with-maybe-section line-number
706 (and (maybe-table-offset line-number lines
)
707 (collect-maybe-table-section lines line-number
))))
709 (write-line (texinfo-line line
) *texinfo-output
*))))))
711 ;;;; texinfo formatting tools
713 (defun hide-superclass-p (class-name super-name
)
714 (let ((super-package (symbol-package super-name
)))
716 ;; KLUDGE: We assume that we don't want to advertise internal
717 ;; classes in CP-lists, unless the symbol we're documenting is
719 (and (member super-package
#.
'(mapcar #'find-package
*undocumented-packages
*))
720 (not (eq super-package
(symbol-package class-name
))))
721 ;; KLUDGE: We don't generally want to advertise SIMPLE-ERROR or
722 ;; SIMPLE-CONDITION in the CPLs of conditions that inherit them
723 ;; simply as a matter of convenience. The assumption here is that
724 ;; the inheritance is incidental unless the name of the condition
725 ;; begins with SIMPLE-.
726 (and (member super-name
'(simple-error simple-condition
))
727 (let ((prefix "SIMPLE-"))
728 (mismatch prefix
(string class-name
) :end2
(length prefix
)))
729 t
; don't return number from MISMATCH
732 (defun hide-slot-p (symbol slot
)
733 ;; FIXME: There is no pricipal reason to avoid the slot docs fo
734 ;; structures and conditions, but their DOCUMENTATION T doesn't
735 ;; currently work with them the way we'd like.
736 (not (and (typep (find-class symbol nil
) 'standard-class
)
737 (docstring slot t
))))
739 (defun texinfo-anchor (doc &aux
*print-pretty
*)
740 (format *texinfo-output
* "@anchor{~A}~%" (node-name doc
)))
742 ;;; KLUDGE: &AUX *PRINT-PRETTY* here means "no linebreaks please"
743 (defun texinfo-begin (doc &aux
*print-pretty
*)
744 (let ((kind (get-kind doc
)))
745 (format *texinfo-output
* "@~A {~:(~A~)} ~(~A~)"
747 ((package constant variable
)
749 ((structure class condition type
)
753 (map 'string
(lambda (char) (if (eql char
#\-
) #\Space char
)) (string kind
))
755 (multiple-value-bind (lambda-list unknown
) (lambda-list doc
)
757 (format *texinfo-output
* " @emph{lambda list not known}"))
760 ;; &foo would be amusingly bold in the pdf thanks to
761 ;; TeX/Texinfo interactions,so we escape the ampersand --
762 ;; amusingly for TeX. sbcl.texinfo defines macros that
763 ;; expand @andkey and friends to &key.
764 (format *texinfo-output
* " ~(~{~A~^ ~}~)"
765 (mapcar (lambda (name)
766 (if (member name lambda-list-keywords
)
767 (format nil
"@and~A{}"
768 (remove #\-
(subseq (string name
) 1)))
771 (format *texinfo-output
* "~%")))
773 (defun texinfo-inferred-body (doc)
774 (when (member (get-kind doc
) '(class structure condition
))
775 (let ((name (get-name doc
)))
776 ;; class precedence list
777 (format *texinfo-output
* "@raggedright~%Class precedence list: ~(~{@code{@w{~A}}~^, ~}~)~%@end raggedright~%~%"
778 (remove-if (lambda (class) (hide-superclass-p name class
))
779 (mapcar #'class-name
(ensure-class-precedence-list (find-class name
)))))
781 (let ((slots (remove-if (lambda (slot) (hide-slot-p name slot
))
782 (class-direct-slots (find-class name
)))))
784 (format *texinfo-output
* "Slots:~%@itemize~%")
786 (format *texinfo-output
*
787 "@item ~(@code{~A}~#[~:; --- ~]~
788 ~:{~2*~@[~2:*~A~P: ~{@code{@w{~S}}~^, ~}~]~:^; ~}~)~%~%"
789 (slot-definition-name slot
)
793 (lambda (name things
)
795 (list name
(length things
) things
)))
796 '("initarg" "reader" "writer")
798 (slot-definition-initargs slot
)
799 (slot-definition-readers slot
)
800 (slot-definition-writers slot
)))))
801 ;; FIXME: Would be neater to handler as children
802 (write-texinfo-string (docstring slot t
)))
803 (format *texinfo-output
* "@end itemize~%~%"))))))
805 (defun texinfo-body (doc)
806 (write-texinfo-string (get-string doc
)))
808 (defun texinfo-end (doc)
809 (write-line (case (get-kind doc
)
810 ((package variable constant
) "@end defvr")
811 ((structure type class condition
) "@end deftp")
815 (defun write-texinfo (doc)
816 "Writes TexInfo for a DOCUMENTATION instance to *TEXINFO-OUTPUT*."
819 (texinfo-inferred-body doc
)
822 ;; FIXME: Children should be sorted one way or another
823 (mapc #'write-texinfo
(get-children doc
)))
827 (defun collect-gf-documentation (gf)
828 "Collects method documentation for the generic function GF"
829 (loop for method in
(generic-function-methods gf
)
830 for doc
= (maybe-documentation method t
)
834 (defun collect-name-documentation (name)
835 (loop for type in
*documentation-types
*
836 for doc
= (maybe-documentation name type
)
840 (defun collect-symbol-documentation (symbol)
841 "Collects all docs for a SYMBOL and (SETF SYMBOL), returns a list of
842 the form DOC instances. See `*documentation-types*' for the possible
844 (nconc (collect-name-documentation symbol
)
845 (collect-name-documentation (list 'setf symbol
))))
847 (defun collect-documentation (package)
848 "Collects all documentation for all external symbols of the given
849 package, as well as for the package itself."
850 (let* ((*documentation-package
* (find-package package
))
852 (check-type package package
)
853 (do-external-symbols (symbol package
)
854 (setf docs
(nconc (collect-symbol-documentation symbol
) docs
)))
855 (let ((doc (maybe-documentation *documentation-package
* t
)))
860 (defmacro with-texinfo-file
(pathname &body forms
)
861 `(with-open-file (*texinfo-output
* ,pathname
863 :if-does-not-exist
:create
864 :if-exists
:supersede
)
867 (defun write-package-macro (package)
868 (let* ((package-name (package-shortest-name package
))
869 (macro-name (package-macro-name package
)))
870 ;; KLUDGE: SB-SEQUENCE has a shorter nickname SEQUENCE, but we
871 ;; want to document the SB- variant.
872 (when (eql (find-package "SB-SEQUENCE") (find-package package
))
873 (setf package-name
"SB-SEQUENCE"))
874 (write-packageish-macro package-name macro-name
)))
876 (defun write-packageish-macro (package-name macro-name
)
877 ;; a word of explanation about the iftex branch here is probably
878 ;; warranted. The package information should be present for
879 ;; clarity, because these produce body text as well as index
880 ;; entries (though in info output it's more important to use a
881 ;; very restricted character set because the info reader parses
882 ;; the link, and colon is a special character). In TeX output we
883 ;; make the package name unconditionally small, and arrange such
884 ;; that the start of the symbol name is at a constant horizontal
885 ;; offset, that offset being such that the longest package names
886 ;; have the "sb-" extending into the left margin. (At the moment,
887 ;; the length of the longest package name, sb-concurrency, is
889 (format *texinfo-output
* "~
892 {@smallertt@phantom{concurrency:}~@[@llap{~(~A~):}~]}\\name\\
903 \\name\\ ~@[[~(~A~)]~]
907 macro-name package-name
))
909 (defun generate-includes (directory &rest packages
)
910 "Create files in `directory' containing Texinfo markup of all
911 docstrings of each exported symbol in `packages'. `directory' is
912 created if necessary. If you supply a namestring that doesn't end in a
913 slash, you lose. The generated files are of the form
914 \"<doc-type>_<packagename>_<symbol-name>.texinfo\" and can be included
915 via @include statements. Texinfo syntax-significant characters are
916 escaped in symbol names, but if a docstring contains invalid Texinfo
918 (handler-bind ((warning #'muffle-warning
))
919 (let ((directory (merge-pathnames (pathname directory
))))
920 (ensure-directories-exist directory
)
921 (dolist (package packages
)
922 (dolist (doc (collect-documentation (find-package package
)))
923 (with-texinfo-file (merge-pathnames (include-pathname doc
) directory
)
924 (write-texinfo doc
))))
925 (with-texinfo-file (merge-pathnames "package-macros.texinfo" directory
)
926 (dolist (package packages
)
927 (write-package-macro package
))
928 (write-packageish-macro nil
"nopkg"))
931 (defun document-package (package &optional filename
)
932 "Create a file containing all available documentation for the
933 exported symbols of `package' in Texinfo format. If `filename' is not
934 supplied, a file \"<packagename>.texinfo\" is generated.
936 The definitions can be referenced using Texinfo statements like
937 @ref{<doc-type>_<packagename>_<symbol-name>.texinfo}. Texinfo
938 syntax-significant characters are escaped in symbol names, but if a
939 docstring contains invalid Texinfo markup, you lose."
940 (handler-bind ((warning #'muffle-warning
))
941 (let* ((package (find-package package
))
942 (filename (or filename
(make-pathname
943 :name
(string-downcase (package-name package
))
945 (docs (sort (collect-documentation package
) #'documentation
<)))
946 (with-texinfo-file filename
948 (write-texinfo doc
)))