Fix gendocs_template
[texinfo-docstrings.git] / docstrings.lisp
blob9aa301dea3c3aadc66de0550441f39b16113b52f
1 ;;; -*- lisp -*-
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.
10 ;;;;
11 ;;;; Written by Rudi Schlatte <rudi@constantly.at>, mangled
12 ;;;; by Nikodemus Siivola.
14 ;;;; TODO
15 ;;;; * Verbatim text
16 ;;;; * Quotations
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):
29 ;;;;
30 ;;;; Formats SYMBOL as @code{symbol}, or @var{symbol} if symbol is in
31 ;;;; the argument list of the defun / defmacro.
32 ;;;;
33 ;;;; Lines starting with * or - that are followed by intented lines
34 ;;;; are marked up with @itemize.
35 ;;;;
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 #+sbcl
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (require 'sb-introspect))
43 (defpackage #:texinfo-docstrings
44 (:use #:cl #-sbcl #:c2mop #+sbcl #:sb-mop)
45 (:shadow #:documentation)
46 (:export #:generate-includes #:document-package)
47 (:documentation
48 "Tools to generate TexInfo documentation from docstrings."))
50 (in-package #:texinfo-docstrings)
52 (defun function-arglist (function)
53 #+sbcl (sb-introspect:function-arglist function)
54 #-sbcl (error "function-arglist unimplemented"))
56 ;;;; various specials and parameters
58 (defvar *texinfo-output*)
59 (defvar *texinfo-variables*)
60 (defvar *documentation-package*)
61 (defvar *documentation-package-name*)
63 ;;; If T, package names are prepended in the documentation. This
64 ;;; doesn't affect filenames. For now this value is sort of hardcoded
65 ;;; in GENERATE-INCLUDES. Fix that.
66 (defvar *prepend-package-names*)
68 (defparameter *undocumented-packages*
69 #+sbcl '(sb-pcl sb-int sb-kernel sb-sys sb-c)
70 #-sbcl nil)
72 (defparameter *documentation-types*
73 '(compiler-macro
74 function
75 method-combination
76 setf
77 ;;structure ; also handled by `type'
78 type
79 variable)
80 "A list of symbols accepted as second argument of `documentation'")
82 (defparameter *character-replacements*
83 '((#\* . "star") (#\/ . "slash") (#\+ . "plus")
84 (#\< . "lt") (#\> . "gt"))
85 "Characters and their replacement names that `alphanumize' uses. If
86 the replacements contain any of the chars they're supposed to replace,
87 you deserve to lose.")
89 (defparameter *characters-to-drop* '(#\\ #\` #\')
90 "Characters that should be removed by `alphanumize'.")
92 (defparameter *texinfo-escaped-chars* "@{}"
93 "Characters that must be escaped with #\@ for Texinfo.")
95 (defparameter *itemize-start-characters* '(#\* #\-)
96 "Characters that might start an itemization in docstrings when
97 at the start of a line.")
99 (defparameter *symbol-characters* "ABCDEFGHIJKLMNOPQRSTUVWXYZ*:-+&"
100 "List of characters that make up symbols in a docstring.")
102 (defparameter *symbol-delimiters* " ,.!?;")
104 (defparameter *ordered-documentation-kinds*
105 '(package type structure condition class macro))
107 ;;;; utilities
109 (defun flatten (list)
110 (cond ((null list)
111 nil)
112 ((consp (car list))
113 (nconc (flatten (car list)) (flatten (cdr list))))
114 ((null (cdr list))
115 (cons (car list) nil))
117 (cons (car list) (flatten (cdr list))))))
119 (defun whitespacep (char)
120 (find char #(#\tab #\space #\page)))
122 (defun setf-name-p (name)
123 (or (symbolp name)
124 (and (listp name) (= 2 (length name)) (eq (car name) 'setf))))
126 (defgeneric specializer-name (specializer))
128 (defmethod specializer-name ((specializer eql-specializer))
129 (list 'eql (eql-specializer-object specializer)))
131 (defmethod specializer-name ((specializer class))
132 (class-name specializer))
134 (defun ensure-class-precedence-list (class)
135 (unless (class-finalized-p class)
136 (finalize-inheritance class))
137 (class-precedence-list class))
139 (defun specialized-lambda-list (method)
140 ;; courtecy of AMOP p. 61
141 (let* ((specializers (method-specializers method))
142 (lambda-list (method-lambda-list method))
143 (n-required (length specializers)))
144 (append (mapcar (lambda (arg specializer)
145 (if (eq specializer (find-class 't))
147 `(,arg ,(specializer-name specializer))))
148 (subseq lambda-list 0 n-required)
149 specializers)
150 (subseq lambda-list n-required))))
152 (defun string-lines (string)
153 "Lines in STRING as a vector."
154 (coerce (with-input-from-string (s string)
155 (loop for line = (read-line s nil nil)
156 while line collect line))
157 'vector))
159 (defun indentation (line)
160 "Position of first non-SPACE character in LINE."
161 (position-if-not (lambda (c) (char= c #\Space)) line))
163 (defun docstring (x doc-type)
164 (cl:documentation x doc-type))
166 (defun flatten-to-string (list)
167 (format nil "~{~A~^-~}" (flatten list)))
169 (defun alphanumize (original)
170 "Construct a string without characters like *`' that will f-star-ck
171 up filename handling. See `*character-replacements*' and
172 `*characters-to-drop*' for customization."
173 (let ((name (remove-if (lambda (x) (member x *characters-to-drop*))
174 (if (listp original)
175 (flatten-to-string original)
176 (string original))))
177 (chars-to-replace (mapcar #'car *character-replacements*)))
178 (flet ((replacement-delimiter (index)
179 (cond ((or (< index 0) (>= index (length name))) "")
180 ((alphanumericp (char name index)) "-")
181 (t ""))))
182 (loop for index = (position-if #'(lambda (x) (member x chars-to-replace))
183 name)
184 while index
185 do (setf name (concatenate 'string (subseq name 0 index)
186 (replacement-delimiter (1- index))
187 (cdr (assoc (aref name index)
188 *character-replacements*))
189 (replacement-delimiter (1+ index))
190 (subseq name (1+ index))))))
191 name))
193 ;;;; generating various names
195 (defgeneric name (thing)
196 (:documentation "Name for a documented thing. Names are either
197 symbols or lists of symbols."))
199 (defmethod name ((symbol symbol))
200 symbol)
202 (defmethod name ((cons cons))
203 cons)
205 (defmethod name ((package package))
206 (package-name package))
208 (defmethod name ((method method))
209 (list
210 (generic-function-name (method-generic-function method))
211 (method-qualifiers method)
212 (specialized-lambda-list method)))
214 ;;; Node names for DOCUMENTATION instances
216 (defun package-name-prefix (doc)
217 (format nil "~@[~A:~]" (and *prepend-package-names* (get-package-name doc))))
219 (defgeneric name-using-kind/name (kind name doc))
221 (defmethod name-using-kind/name (kind (name string) doc)
222 (declare (ignore kind doc))
223 name)
225 (defmethod name-using-kind/name (kind (name symbol) doc)
226 (declare (ignore kind))
227 (format nil "~A~A" (package-name-prefix doc) name))
229 (defmethod name-using-kind/name (kind (name list) doc)
230 (declare (ignore kind))
231 (assert (setf-name-p name))
232 (format nil "(setf ~A~A)" (package-name-prefix doc) (second name)))
234 (defmethod name-using-kind/name ((kind (eql 'method)) name doc)
235 (format nil "~A~{ ~A~} ~A"
236 (name-using-kind/name nil (first name) doc)
237 (second name)
238 (third name)))
240 (defun node-name (doc)
241 "Returns TexInfo node name as a string for a DOCUMENTATION instance."
242 (let ((kind (get-kind doc)))
243 (format nil "~:(~A~) ~(~A~)"
244 kind (name-using-kind/name kind (get-name doc) doc))))
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))
252 name)
254 (defmethod title-using-kind/name (kind (name symbol) doc)
255 (declare (ignore kind))
256 (format nil "~A~A" (package-name-prefix doc) name))
258 (defmethod title-using-kind/name (kind (name list) doc)
259 (declare (ignore kind))
260 (assert (setf-name-p name))
261 (format nil "(setf ~A~A)" (package-name-prefix doc) (second name)))
263 (defmethod title-using-kind/name ((kind (eql 'method)) name doc)
264 (format nil "~{~A ~}~A"
265 (second name)
266 (title-using-kind/name nil (first name) doc)))
268 (defun title-name (doc)
269 "Returns a string to be used as name of the definition."
270 (string-downcase (title-using-kind/name (get-kind doc) (get-name doc) doc)))
272 (defun include-pathname (doc)
273 (let* ((kind (get-kind doc))
274 (name (nstring-downcase
275 (if (eq 'package kind)
276 (format nil "package-~A" (alphanumize (get-name doc)))
277 (format nil "~A-~A-~A"
278 (case (get-kind doc)
279 ((function generic-function) "fun")
280 (structure "struct")
281 (variable "var")
282 (otherwise (symbol-name (get-kind doc))))
283 (alphanumize (get-package-name doc))
284 (alphanumize (get-name doc)))))))
285 (make-pathname :name name :type "texinfo")))
287 ;;;; documentation class and related methods
289 (defclass documentation ()
290 ((name :initarg :name :reader get-name)
291 (kind :initarg :kind :reader get-kind)
292 (string :initarg :string :reader get-string)
293 (children :initarg :children :initform nil :reader get-children)
294 (package :initform *documentation-package* :reader get-package)
295 (package-name :initform *documentation-package-name*
296 :reader get-package-name)))
298 (defmethod print-object ((documentation documentation) stream)
299 (print-unreadable-object (documentation stream :type t)
300 (princ (list (get-kind documentation) (get-name documentation)) stream)))
302 (defgeneric make-documentation (x doc-type string))
304 (defmethod make-documentation ((x package) doc-type string)
305 (declare (ignore doc-type))
306 (make-instance 'documentation
307 :name (name x)
308 :kind 'package
309 :string string))
311 (defmethod make-documentation (x (doc-type (eql 'function)) string)
312 (declare (ignore doc-type))
313 (let* ((fdef (and (fboundp x) (fdefinition x)))
314 (name x)
315 (kind (cond ((and (symbolp x) (special-operator-p x))
316 'special-operator)
317 ((and (symbolp x) (macro-function x))
318 'macro)
319 ((typep fdef 'generic-function)
320 (assert (or (symbolp name) (setf-name-p name)))
321 'generic-function)
322 (fdef
323 (assert (or (symbolp name) (setf-name-p name)))
324 'function)))
325 (children (when (eq kind 'generic-function)
326 (collect-gf-documentation fdef))))
327 (make-instance 'documentation
328 :name (name x)
329 :string string
330 :kind kind
331 :children children)))
333 (defmethod make-documentation ((x method) doc-type string)
334 (declare (ignore doc-type))
335 (make-instance 'documentation
336 :name (name x)
337 :kind 'method
338 :string string))
340 (defmethod make-documentation (x (doc-type (eql 'type)) string)
341 (make-instance 'documentation
342 :name (name x)
343 :string string
344 :kind (etypecase (find-class x nil)
345 (structure-class 'structure)
346 (standard-class 'class)
347 (sb-pcl::condition-class 'condition)
348 ((or built-in-class null) 'type))))
350 (defmethod make-documentation (x (doc-type (eql 'variable)) string)
351 (make-instance 'documentation
352 :name (name x)
353 :string string
354 :kind (if (constantp x)
355 'constant
356 'variable)))
358 (defmethod make-documentation (x (doc-type (eql 'setf)) string)
359 (declare (ignore doc-type))
360 (make-instance 'documentation
361 :name (name x)
362 :kind 'setf-expander
363 :string string))
365 (defmethod make-documentation (x doc-type string)
366 (make-instance 'documentation
367 :name (name x)
368 :kind doc-type
369 :string string))
371 (defun maybe-documentation (x doc-type)
372 "Returns a DOCUMENTATION instance for X and DOC-TYPE, or NIL if
373 there is no corresponding docstring."
374 (let ((docstring (docstring x doc-type)))
375 (when docstring
376 (make-documentation x doc-type docstring))))
378 (defun lambda-list (doc)
379 (case (get-kind doc)
380 ((package constant variable type structure class condition nil)
381 nil)
382 (method
383 (third (get-name doc)))
385 ;; KLUDGE: Eugh.
387 ;; believe it or not, the above comment was written before CSR
388 ;; came along and obfuscated this. (2005-07-04)
389 (when (symbolp (get-name doc))
390 (labels ((clean (x &key optional key)
391 (typecase x
392 (atom x)
393 ((cons (member &optional))
394 (cons (car x) (clean (cdr x) :optional t)))
395 ((cons (member &key))
396 (cons (car x) (clean (cdr x) :key t)))
397 ((cons cons)
398 (cons
399 (cond (key (if (consp (caar x))
400 (caaar x)
401 (caar x)))
402 (optional (caar x))
403 (t (clean (car x))))
404 (clean (cdr x) :key key :optional optional)))
405 (cons
406 (cons
407 (cond ((or key optional) (car x))
408 (t (clean (car x))))
409 (clean (cdr x) :key key :optional optional))))))
410 (clean (function-arglist (get-name doc))))))))
412 (defun documentation< (x y)
413 (let ((p1 (position (get-kind x) *ordered-documentation-kinds*))
414 (p2 (position (get-kind y) *ordered-documentation-kinds*)))
415 (if (or (not (and p1 p2)) (= p1 p2))
416 (string< (string (get-name x)) (string (get-name y)))
417 (< p1 p2))))
419 ;;;; turning text into texinfo
421 (defun escape-for-texinfo (string &optional downcasep)
422 "Return STRING with characters in *TEXINFO-ESCAPED-CHARS* escaped
423 with #\@. Optionally downcase the result."
424 (let ((result (with-output-to-string (s)
425 (loop for char across string
426 when (find char *texinfo-escaped-chars*)
427 do (write-char #\@ s)
428 do (write-char char s)))))
429 (if downcasep (nstring-downcase result) result)))
431 (defun empty-p (line-number lines)
432 (and (< -1 line-number (length lines))
433 (not (indentation (svref lines line-number)))))
435 ;;; line markups
437 (defun locate-symbols (line)
438 "Return a list of index pairs of symbol-like parts of LINE."
439 ;; This would be a good application for a regex ...
440 (do ((result nil)
441 (begin nil)
442 (maybe-begin t)
443 (i 0 (1+ i)))
444 ((= i (length line))
445 ;; symbol at end of line
446 (when (and begin (or (> i (1+ begin))
447 (not (member (char line begin) '(#\A #\I)))))
448 (push (list begin i) result))
449 (nreverse result))
450 (cond
451 ((and begin (find (char line i) *symbol-delimiters*))
452 ;; symbol end; remember it if it's not "A" or "I"
453 (when (or (> i (1+ begin)) (not (member (char line begin) '(#\A #\I))))
454 (push (list begin i) result))
455 (setf begin nil
456 maybe-begin t))
457 ((and begin (not (find (char line i) *symbol-characters*)))
458 ;; Not a symbol: abort
459 (setf begin nil))
460 ((and maybe-begin (not begin) (find (char line i) *symbol-characters*))
461 ;; potential symbol begin at this position
462 (setf begin i
463 maybe-begin nil))
464 ((find (char line i) *symbol-delimiters*)
465 ;; potential symbol begin after this position
466 (setf maybe-begin t))
468 ;; Not reading a symbol, not at potential start of symbol
469 (setf maybe-begin nil)))))
471 (defun texinfo-line (line)
472 "Format symbols in LINE texinfo-style: either as code or as
473 variables if the symbol in question is contained in symbols
474 *TEXINFO-VARIABLES*."
475 (with-output-to-string (result)
476 (let ((last 0))
477 (dolist (symbol/index (locate-symbols line))
478 (write-string (subseq line last (first symbol/index)) result)
479 (let ((symbol-name (apply #'subseq line symbol/index)))
480 (format result (if (member symbol-name *texinfo-variables*
481 :test #'string=)
482 "@var{~A}"
483 "@code{~A}")
484 (string-downcase symbol-name)))
485 (setf last (second symbol/index)))
486 (write-string (subseq line last) result))))
488 ;;; lisp sections
490 (defun lisp-section-p (line line-number lines)
491 "Returns T if the given LINE looks like start of lisp code --
492 ie. if it starts with whitespace followed by a paren or
493 semicolon, and the previous line is empty"
494 (let ((offset (indentation line)))
495 (and offset
496 (plusp offset)
497 (find (find-if-not #'whitespacep line) "(;")
498 (empty-p (1- line-number) lines))))
500 (defun collect-lisp-section (lines line-number)
501 (let ((lisp (loop for index = line-number then (1+ index)
502 for line = (and (< index (length lines))
503 (svref lines index))
504 while (indentation line)
505 collect line)))
506 (values (length lisp) `("@lisp" ,@lisp "@end lisp"))))
508 ;;; itemized sections
510 (defun maybe-itemize-offset (line)
511 "Return NIL or the indentation offset if LINE looks like it starts
512 an item in an itemization."
513 (let* ((offset (indentation line))
514 (char (when offset (char line offset))))
515 (and offset
516 (member char *itemize-start-characters* :test #'char=)
517 (char= #\Space (find-if-not (lambda (c) (char= c char))
518 line :start offset))
519 offset)))
521 (defun collect-maybe-itemized-section (lines starting-line)
522 ;; Return index of next line to be processed outside
523 (let ((this-offset (maybe-itemize-offset (svref lines starting-line)))
524 (result nil)
525 (lines-consumed 0))
526 (loop for line-number from starting-line below (length lines)
527 for line = (svref lines line-number)
528 for indentation = (indentation line)
529 for offset = (maybe-itemize-offset line)
530 do (cond
531 ((not indentation)
532 ;; empty line -- inserts paragraph.
533 (push "" result)
534 (incf lines-consumed))
535 ((and offset (> indentation this-offset))
536 ;; nested itemization -- handle recursively
537 ;; FIXME: tables in itemizations go wrong
538 (multiple-value-bind (sub-lines-consumed sub-itemization)
539 (collect-maybe-itemized-section lines line-number)
540 (when sub-lines-consumed
541 (incf line-number (1- sub-lines-consumed)) ; +1 on next loop
542 (incf lines-consumed sub-lines-consumed)
543 (setf result (nconc (nreverse sub-itemization) result)))))
544 ((and offset (= indentation this-offset))
545 ;; start of new item
546 (push (format nil "@item ~A"
547 (texinfo-line (subseq line (1+ offset))))
548 result)
549 (incf lines-consumed))
550 ((and (not offset) (> indentation this-offset))
551 ;; continued item from previous line
552 (push (texinfo-line line) result)
553 (incf lines-consumed))
555 ;; end of itemization
556 (loop-finish))))
557 ;; a single-line itemization isn't.
558 (if (> (count-if (lambda (line) (> (length line) 0)) result) 1)
559 (values lines-consumed `("@itemize" ,@(reverse result) "@end itemize"))
560 nil)))
562 ;;; table sections
564 (defun tabulation-body-p (offset line-number lines)
565 (when (< line-number (length lines))
566 (let ((offset2 (indentation (svref lines line-number))))
567 (and offset2 (< offset offset2)))))
569 (defun tabulation-p (offset line-number lines direction)
570 (let ((step (ecase direction
571 (:backwards (1- line-number))
572 (:forwards (1+ line-number)))))
573 (when (and (plusp line-number) (< line-number (length lines)))
574 (and (eql offset (indentation (svref lines line-number)))
575 (or (when (eq direction :backwards)
576 (empty-p step lines))
577 (tabulation-p offset step lines direction)
578 (tabulation-body-p offset step lines))))))
580 (defun maybe-table-offset (line-number lines)
581 "Return NIL or the indentation offset if LINE looks like it starts
582 an item in a tabulation. Ie, if it is (1) indented, (2) preceded by an
583 empty line, another tabulation label, or a tabulation body, (3) and
584 followed another tabulation label or a tabulation body."
585 (let* ((line (svref lines line-number))
586 (offset (indentation line))
587 (prev (1- line-number))
588 (next (1+ line-number)))
589 (when (and offset (plusp offset))
590 (and (or (empty-p prev lines)
591 (tabulation-body-p offset prev lines)
592 (tabulation-p offset prev lines :backwards))
593 (or (tabulation-body-p offset next lines)
594 (tabulation-p offset next lines :forwards))
595 offset))))
597 ;;; FIXME: This and itemization are very similar: could they share
598 ;;; some code, mayhap?
600 (defun collect-maybe-table-section (lines starting-line)
601 ;; Return index of next line to be processed outside
602 (let ((this-offset (maybe-table-offset starting-line lines))
603 (result nil)
604 (lines-consumed 0))
605 (loop for line-number from starting-line below (length lines)
606 for line = (svref lines line-number)
607 for indentation = (indentation line)
608 for offset = (maybe-table-offset line-number lines)
609 do (cond
610 ((not indentation)
611 ;; empty line -- inserts paragraph.
612 (push "" result)
613 (incf lines-consumed))
614 ((and offset (= indentation this-offset))
615 ;; start of new item, or continuation of previous item
616 (if (and result (search "@item" (car result) :test #'char=))
617 (push (format nil "@itemx ~A" (texinfo-line line))
618 result)
619 (progn
620 (push "" result)
621 (push (format nil "@item ~A" (texinfo-line line))
622 result)))
623 (incf lines-consumed))
624 ((> indentation this-offset)
625 ;; continued item from previous line
626 (push (texinfo-line line) result)
627 (incf lines-consumed))
629 ;; end of itemization
630 (loop-finish))))
631 ;; a single-line table isn't.
632 (if (> (count-if (lambda (line) (> (length line) 0)) result) 1)
633 (values lines-consumed
634 `("" "@table @emph" ,@(reverse result) "@end table" ""))
635 nil)))
637 ;;; section markup
639 (defmacro with-maybe-section (index &rest forms)
640 `(multiple-value-bind (count collected) (progn ,@forms)
641 (when count
642 (dolist (line collected)
643 (write-line line *texinfo-output*))
644 (incf ,index (1- count)))))
646 (defun write-texinfo-string (string &optional lambda-list)
647 "Try to guess as much formatting for a raw docstring as possible."
648 (let ((*texinfo-variables* (flatten lambda-list))
649 (lines (string-lines (escape-for-texinfo string nil))))
650 (loop for line-number from 0 below (length lines)
651 for line = (svref lines line-number)
652 do (cond
653 ((with-maybe-section line-number
654 (and (lisp-section-p line line-number lines)
655 (collect-lisp-section lines line-number))))
656 ((with-maybe-section line-number
657 (and (maybe-itemize-offset line)
658 (collect-maybe-itemized-section lines line-number))))
659 ((with-maybe-section line-number
660 (and (maybe-table-offset line-number lines)
661 (collect-maybe-table-section lines line-number))))
663 (write-line (texinfo-line line) *texinfo-output*))))))
665 ;;;; texinfo formatting tools
667 (defun hide-superclass-p (class-name super-name)
668 (let ((super-package (symbol-package super-name)))
670 ;; KLUDGE: We assume that we don't want to advertise internal
671 ;; classes in CP-lists, unless the symbol we're documenting is
672 ;; internal as well.
673 (and (member super-package
674 #.'(mapcar #'find-package *undocumented-packages*))
675 (not (eq super-package (symbol-package class-name))))
676 ;; KLUDGE: We don't generally want to advertise SIMPLE-ERROR or
677 ;; SIMPLE-CONDITION in the CPLs of conditions that inherit them
678 ;; simply as a matter of convenience. The assumption here is that
679 ;; the inheritance is incidental unless the name of the condition
680 ;; begins with SIMPLE-.
681 (and (member super-name '(simple-error simple-condition))
682 (let ((prefix "SIMPLE-"))
683 (mismatch prefix (string class-name) :end2 (length prefix)))
684 t ; don't return number from MISMATCH
685 ))))
687 (defun hide-slot-p (symbol slot)
688 ;; FIXME: There is no pricipal reason to avoid the slot docs fo
689 ;; structures and conditions, but their DOCUMENTATION T doesn't
690 ;; currently work with them the way we'd like.
691 (not (and (typep (find-class symbol nil) 'standard-class)
692 (docstring slot t))))
694 (defun texinfo-anchor (doc)
695 (format *texinfo-output* "@anchor{~A}~%" (node-name doc)))
697 ;;; KLUDGE: &AUX *PRINT-PRETTY* here means "no linebreaks please"
698 (defun texinfo-begin (doc &aux *print-pretty*)
699 (let ((kind (get-kind doc)))
700 (format *texinfo-output* "@~A {~:(~A~)} ~(~A~@[ ~{~A~^ ~}~]~)~%"
701 (case kind
702 ((package constant variable)
703 "defvr")
704 ((structure class condition type)
705 "deftp")
707 "deffn"))
708 (map 'string (lambda (char) (if (eql char #\-) #\Space char))
709 (string kind))
710 (title-name doc)
711 (lambda-list doc))))
713 (defun texinfo-index (doc)
714 (let ((title (title-name doc)))
715 (case (get-kind doc)
716 ((structure type class condition)
717 (format *texinfo-output* "@tindex ~A~%" title))
718 ((variable constant)
719 (format *texinfo-output* "@vindex ~A~%" title))
720 ((compiler-macro function method-combination macro generic-function)
721 (format *texinfo-output* "@findex ~A~%" title)))))
723 (defun texinfo-inferred-body (doc)
724 (when (member (get-kind doc) '(class structure condition))
725 (let ((name (get-name doc)))
726 ;; class precedence list
727 (format *texinfo-output*
728 "Class precedence list: @code{~(~{@lw{~A}~^, ~}~)}~%~%"
729 (remove-if (lambda (class) (hide-superclass-p name class))
730 (mapcar #'class-name (ensure-class-precedence-list
731 (find-class name)))))
732 ;; slots
733 (let ((slots (remove-if (lambda (slot) (hide-slot-p name slot))
734 (class-direct-slots (find-class name)))))
735 (when slots
736 (format *texinfo-output* "Slots:~%@itemize~%")
737 (dolist (slot slots)
738 (format *texinfo-output*
739 "@item ~(@code{~A}~#[~:; --- ~]~
740 ~:{~2*~@[~2:*~A~P: ~{@code{@w{~A}}~^, ~}~]~:^; ~}~)~%~%"
741 (slot-definition-name slot)
742 (remove
744 (mapcar
745 (lambda (name things)
746 (if things
747 (list name (length things) things)))
748 '("initarg" "reader" "writer")
749 ;; because I couldn't grok that format string
750 (flet ((symbol-names (list)
751 (mapcar (lambda (x)
752 (if (or *prepend-package-names*
753 (keywordp x))
754 (format nil "~(~S~)" x)
755 (format nil "~(~A~)" x)))
756 list)))
757 (mapcar #'symbol-names
758 (list (slot-definition-initargs slot)
759 (slot-definition-readers slot)
760 (slot-definition-writers slot)))))))
761 ;; FIXME: Would be neater to handler as children
762 (write-texinfo-string (docstring slot t)))
763 (format *texinfo-output* "@end itemize~%~%"))))))
765 (defun texinfo-body (doc)
766 (write-texinfo-string (get-string doc)))
768 (defun texinfo-end (doc)
769 (write-line (case (get-kind doc)
770 ((package variable constant) "@end defvr")
771 ((structure type class condition) "@end deftp")
772 (t "@end deffn"))
773 *texinfo-output*))
775 (defun write-texinfo (doc)
776 "Writes TexInfo for a DOCUMENTATION instance to *TEXINFO-OUTPUT*."
777 (texinfo-anchor doc)
778 (texinfo-begin doc)
779 (texinfo-index doc)
780 (texinfo-inferred-body doc)
781 (texinfo-body doc)
782 (texinfo-end doc)
783 ;; FIXME: Children should be sorted one way or another
784 (mapc #'write-texinfo (get-children doc)))
786 ;;;; main logic
788 (defun collect-gf-documentation (gf)
789 "Collects method documentation for the generic function GF"
790 (loop for method in (generic-function-methods gf)
791 for doc = (maybe-documentation method t)
792 when doc
793 collect doc))
795 (defun collect-name-documentation (name)
796 (loop for type in *documentation-types*
797 for doc = (maybe-documentation name type)
798 when doc
799 collect doc))
801 (defun collect-symbol-documentation (symbol)
802 "Collects all docs for a SYMBOL and (SETF SYMBOL), returns a list of
803 the form DOC instances. See `*documentation-types*' for the possible
804 values of doc-type."
805 (nconc (collect-name-documentation symbol)
806 (collect-name-documentation (list 'setf symbol))))
808 (defun collect-documentation (package &optional package-name)
809 "Collects all documentation for all external symbols of the given
810 package, as well as for the package itself."
811 (let* ((*documentation-package* (find-package package))
812 (*documentation-package-name*
813 (or package-name (package-name *documentation-package*)))
814 (docs nil))
815 (check-type package package)
816 (do-external-symbols (symbol package)
817 (setf docs (nconc (collect-symbol-documentation symbol) docs)))
818 (let ((doc (maybe-documentation *documentation-package* t)))
819 (when doc
820 (push doc docs)))
821 docs))
823 (defmacro with-texinfo-file (pathname &body forms)
824 `(with-open-file (*texinfo-output* ,pathname
825 :direction :output
826 :if-does-not-exist :create
827 :if-exists :supersede)
828 ,@forms))
830 (defun generate-includes (directory &rest packages)
831 "Create files in `directory' containing Texinfo markup of all
832 docstrings of each exported symbol in `packages'. `directory' is
833 created if necessary. If you supply a namestring that doesn't end in a
834 slash, you lose. The generated files are of the form
835 \"<doc-type>_<packagename>_<symbol-name>.texinfo\" and can be included
836 via @include statements. Texinfo syntax-significant characters are
837 escaped in symbol names, but if a docstring contains invalid Texinfo
838 markup, you lose."
839 (handler-bind ((warning #'muffle-warning))
840 (let ((directory (merge-pathnames (pathname directory))))
841 (ensure-directories-exist directory)
842 (let ((*prepend-package-names* (> (length packages) 1)))
843 (dolist (package packages)
844 (dolist (doc (collect-documentation
845 (find-package package)
846 (string-downcase (etypecase package
847 (symbol (symbol-name package))
848 (string package)))))
849 (with-texinfo-file
850 (merge-pathnames (include-pathname doc) directory)
851 (write-texinfo doc)))))
852 directory)))
854 (defun document-package (package &optional filename)
855 "Create a file containing all available documentation for the
856 exported symbols of `package' in Texinfo format. If `filename' is not
857 supplied, a file \"<packagename>.texinfo\" is generated.
859 The definitions can be referenced using Texinfo statements like
860 @ref{<doc-type>_<packagename>_<symbol-name>.texinfo}. Texinfo
861 syntax-significant characters are escaped in symbol names, but if a
862 docstring contains invalid Texinfo markup, you lose."
863 (handler-bind ((warning #'muffle-warning))
864 (let* ((package (find-package package))
865 (filename (or filename
866 (make-pathname
867 :name (string-downcase (package-name package))
868 :type "texinfo")))
869 (docs (sort (collect-documentation package) #'documentation<)))
870 (with-texinfo-file filename
871 (dolist (doc docs)
872 (write-texinfo doc)))
873 filename)))