1.0.33.9: LDB stability improvements.
[sbcl/llvm.git] / doc / manual / docstrings.lisp
blobc258d20a18f7fb5d96ba231568c24f2d5ba0e7e0
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 (eval-when (:compile-toplevel :load-toplevel :execute)
40 (require 'sb-introspect))
42 (defpackage :sb-texinfo
43 (:use :cl :sb-mop)
44 (:shadow #:documentation)
45 (:export #:generate-includes #:document-package)
46 (:documentation
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*
60 '(compiler-macro
61 function
62 method-combination
63 setf
64 ;;structure ; also handled by `type'
65 type
66 variable)
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))
94 ;;;; utilities
96 (defun flatten (list)
97 (cond ((null list)
98 nil)
99 ((consp (car list))
100 (nconc (flatten (car list)) (flatten (cdr list))))
101 ((null (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)
110 (or (symbolp 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 ;; courtecy 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)
136 specializers)
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))
144 'vector))
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*))
161 (if (listp original)
162 (flatten-to-string original)
163 (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)) "-")
168 (t ""))))
169 (loop for index = (position-if #'(lambda (x) (member x chars-to-replace))
170 name)
171 while index
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))))))
178 name))
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))
187 symbol)
189 (defmethod name ((cons cons))
190 cons)
192 (defmethod name ((package package))
193 (package-name package))
195 (defmethod name ((method method))
196 (list
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))
207 name)
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 (format nil "~A~{ ~A~} ~A"
220 (name-using-kind/name nil (first name) doc)
221 (second name)
222 (third name)))
224 (defun node-name (doc)
225 "Returns TexInfo node name as a string for a DOCUMENTATION instance."
226 (let ((kind (get-kind doc)))
227 (format nil "~:(~A~) ~(~A~)" kind (name-using-kind/name kind (get-name doc) doc))))
229 (defun short-package-name (package)
230 (car (sort (copy-list (cons (package-name package) (package-nicknames package)))
231 #'< :key #'length)))
233 ;;; Definition titles for DOCUMENTATION instances
235 (defgeneric title-using-kind/name (kind name doc))
237 (defmethod title-using-kind/name (kind (name string) doc)
238 (declare (ignore kind doc))
239 name)
241 (defmethod title-using-kind/name (kind (name symbol) doc)
242 (declare (ignore kind))
243 (format nil "~A:~A" (short-package-name (get-package doc)) name))
245 (defmethod title-using-kind/name (kind (name list) doc)
246 (declare (ignore kind))
247 (assert (setf-name-p name))
248 (format nil "(setf ~A:~A)" (short-package-name (get-package doc)) (second name)))
250 (defmethod title-using-kind/name ((kind (eql 'method)) name doc)
251 (format nil "~{~A ~}~A"
252 (second name)
253 (title-using-kind/name nil (first name) doc)))
255 (defun title-name (doc)
256 "Returns a string to be used as name of the definition."
257 (string-downcase (title-using-kind/name (get-kind doc) (get-name doc) doc)))
259 (defun include-pathname (doc)
260 (let* ((kind (get-kind doc))
261 (name (nstring-downcase
262 (if (eq 'package kind)
263 (format nil "package-~A" (alphanumize (get-name doc)))
264 (format nil "~A-~A-~A"
265 (case (get-kind doc)
266 ((function generic-function) "fun")
267 (structure "struct")
268 (variable "var")
269 (otherwise (symbol-name (get-kind doc))))
270 (alphanumize (package-name (get-package doc)))
271 (alphanumize (get-name doc)))))))
272 (make-pathname :name name :type "texinfo")))
274 ;;;; documentation class and related methods
276 (defclass documentation ()
277 ((name :initarg :name :reader get-name)
278 (kind :initarg :kind :reader get-kind)
279 (string :initarg :string :reader get-string)
280 (children :initarg :children :initform nil :reader get-children)
281 (package :initform *documentation-package* :reader get-package)))
283 (defmethod print-object ((documentation documentation) stream)
284 (print-unreadable-object (documentation stream :type t)
285 (princ (list (get-kind documentation) (get-name documentation)) stream)))
287 (defgeneric make-documentation (x doc-type string))
289 (defmethod make-documentation ((x package) doc-type string)
290 (declare (ignore doc-type))
291 (make-instance 'documentation
292 :name (name x)
293 :kind 'package
294 :string string))
296 (defmethod make-documentation (x (doc-type (eql 'function)) string)
297 (declare (ignore doc-type))
298 (let* ((fdef (and (fboundp x) (fdefinition x)))
299 (name x)
300 (kind (cond ((and (symbolp x) (special-operator-p x))
301 'special-operator)
302 ((and (symbolp x) (macro-function x))
303 'macro)
304 ((typep fdef 'generic-function)
305 (assert (or (symbolp name) (setf-name-p name)))
306 'generic-function)
307 (fdef
308 (assert (or (symbolp name) (setf-name-p name)))
309 'function)))
310 (children (when (eq kind 'generic-function)
311 (collect-gf-documentation fdef))))
312 (make-instance 'documentation
313 :name (name x)
314 :string string
315 :kind kind
316 :children children)))
318 (defmethod make-documentation ((x method) doc-type string)
319 (declare (ignore doc-type))
320 (make-instance 'documentation
321 :name (name x)
322 :kind 'method
323 :string string))
325 (defmethod make-documentation (x (doc-type (eql 'type)) string)
326 (make-instance 'documentation
327 :name (name x)
328 :string string
329 :kind (etypecase (find-class x nil)
330 (structure-class 'structure)
331 (standard-class 'class)
332 (sb-pcl::condition-class 'condition)
333 ((or built-in-class null) 'type))))
335 (defmethod make-documentation (x (doc-type (eql 'variable)) string)
336 (make-instance 'documentation
337 :name (name x)
338 :string string
339 :kind (if (constantp x)
340 'constant
341 'variable)))
343 (defmethod make-documentation (x (doc-type (eql 'setf)) string)
344 (declare (ignore doc-type))
345 (make-instance 'documentation
346 :name (name x)
347 :kind 'setf-expander
348 :string string))
350 (defmethod make-documentation (x doc-type string)
351 (make-instance 'documentation
352 :name (name x)
353 :kind doc-type
354 :string string))
356 (defun maybe-documentation (x doc-type)
357 "Returns a DOCUMENTATION instance for X and DOC-TYPE, or NIL if
358 there is no corresponding docstring."
359 (let ((docstring (docstring x doc-type)))
360 (when docstring
361 (make-documentation x doc-type docstring))))
363 (defun lambda-list (doc)
364 (case (get-kind doc)
365 ((package constant variable type structure class condition nil)
366 nil)
367 (method
368 (third (get-name doc)))
370 ;; KLUDGE: Eugh.
372 ;; believe it or not, the above comment was written before CSR
373 ;; came along and obfuscated this. (2005-07-04)
374 (when (symbolp (get-name doc))
375 (labels ((clean (x &key optional key)
376 (typecase x
377 (atom x)
378 ((cons (member &optional))
379 (cons (car x) (clean (cdr x) :optional t)))
380 ((cons (member &key))
381 (cons (car x) (clean (cdr x) :key t)))
382 ((cons cons)
383 (cons
384 (cond (key (if (consp (caar x))
385 (caaar x)
386 (caar x)))
387 (optional (caar x))
388 (t (clean (car x))))
389 (clean (cdr x) :key key :optional optional)))
390 (cons
391 (cons
392 (cond ((or key optional) (car x))
393 (t (clean (car x))))
394 (clean (cdr x) :key key :optional optional))))))
395 (clean (sb-introspect:function-lambda-list (get-name doc))))))))
397 (defun get-string-name (x)
398 (let ((name (get-name x)))
399 (cond ((symbolp name)
400 (symbol-name name))
401 ((and (consp name) (eq 'setf (car name)))
402 (symbol-name (second name)))
403 ((stringp name)
404 name)
406 (error "Don't know which symbol to use for name ~S" name)))))
408 (defun documentation< (x y)
409 (let ((p1 (position (get-kind x) *ordered-documentation-kinds*))
410 (p2 (position (get-kind y) *ordered-documentation-kinds*)))
411 (if (or (not (and p1 p2)) (= p1 p2))
412 (string< (get-string-name x) (get-string-name y))
413 (< p1 p2))))
415 ;;;; turning text into texinfo
417 (defun escape-for-texinfo (string &optional downcasep)
418 "Return STRING with characters in *TEXINFO-ESCAPED-CHARS* escaped
419 with #\@. Optionally downcase the result."
420 (let ((result (with-output-to-string (s)
421 (loop for char across string
422 when (find char *texinfo-escaped-chars*)
423 do (write-char #\@ s)
424 do (write-char char s)))))
425 (if downcasep (nstring-downcase result) result)))
427 (defun empty-p (line-number lines)
428 (and (< -1 line-number (length lines))
429 (not (indentation (svref lines line-number)))))
431 ;;; line markups
433 (defvar *not-symbols* '("ANSI" "CLHS"))
435 (defun locate-symbols (line)
436 "Return a list of index pairs of symbol-like parts of LINE."
437 ;; This would be a good application for a regex ...
438 (let (result)
439 (flet ((grab (start end)
440 (unless (member (subseq line start end) '("ANSI" "CLHS"))
441 (push (list start end) result))))
442 (do ((begin nil)
443 (maybe-begin t)
444 (i 0 (1+ i)))
445 ((= i (length line))
446 ;; symbol at end of line
447 (when (and begin (or (> i (1+ begin))
448 (not (member (char line begin) '(#\A #\I)))))
449 (grab begin i))
450 (nreverse result))
451 (cond
452 ((and begin (find (char line i) *symbol-delimiters*))
453 ;; symbol end; remember it if it's not "A" or "I"
454 (when (or (> i (1+ begin)) (not (member (char line begin) '(#\A #\I))))
455 (grab begin i))
456 (setf begin nil
457 maybe-begin t))
458 ((and begin (not (find (char line i) *symbol-characters*)))
459 ;; Not a symbol: abort
460 (setf begin nil))
461 ((and maybe-begin (not begin) (find (char line i) *symbol-characters*))
462 ;; potential symbol begin at this position
463 (setf begin i
464 maybe-begin nil))
465 ((find (char line i) *symbol-delimiters*)
466 ;; potential symbol begin after this position
467 (setf maybe-begin t))
469 ;; Not reading a symbol, not at potential start of symbol
470 (setf maybe-begin nil)))))))
472 (defun texinfo-line (line)
473 "Format symbols in LINE texinfo-style: either as code or as
474 variables if the symbol in question is contained in symbols
475 *TEXINFO-VARIABLES*."
476 (with-output-to-string (result)
477 (let ((last 0))
478 (dolist (symbol/index (locate-symbols line))
479 (write-string (subseq line last (first symbol/index)) result)
480 (let ((symbol-name (apply #'subseq line symbol/index)))
481 (format result (if (member symbol-name *texinfo-variables*
482 :test #'string=)
483 "@var{~A}"
484 "@code{~A}")
485 (string-downcase symbol-name)))
486 (setf last (second symbol/index)))
487 (write-string (subseq line last) result))))
489 ;;; lisp sections
491 (defun lisp-section-p (line line-number lines)
492 "Returns T if the given LINE looks like start of lisp code --
493 ie. if it starts with whitespace followed by a paren or
494 semicolon, and the previous line is empty"
495 (let ((offset (indentation line)))
496 (and offset
497 (plusp offset)
498 (find (find-if-not #'whitespacep line) "(;")
499 (empty-p (1- line-number) lines))))
501 (defun collect-lisp-section (lines line-number)
502 (let ((lisp (loop for index = line-number then (1+ index)
503 for line = (and (< index (length lines)) (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 #.'(mapcar #'find-package *undocumented-packages*))
674 (not (eq super-package (symbol-package class-name))))
675 ;; KLUDGE: We don't generally want to advertise SIMPLE-ERROR or
676 ;; SIMPLE-CONDITION in the CPLs of conditions that inherit them
677 ;; simply as a matter of convenience. The assumption here is that
678 ;; the inheritance is incidental unless the name of the condition
679 ;; begins with SIMPLE-.
680 (and (member super-name '(simple-error simple-condition))
681 (let ((prefix "SIMPLE-"))
682 (mismatch prefix (string class-name) :end2 (length prefix)))
683 t ; don't return number from MISMATCH
684 ))))
686 (defun hide-slot-p (symbol slot)
687 ;; FIXME: There is no pricipal reason to avoid the slot docs fo
688 ;; structures and conditions, but their DOCUMENTATION T doesn't
689 ;; currently work with them the way we'd like.
690 (not (and (typep (find-class symbol nil) 'standard-class)
691 (docstring slot t))))
693 (defun texinfo-anchor (doc)
694 (format *texinfo-output* "@anchor{~A}~%" (node-name doc)))
696 ;;; KLUDGE: &AUX *PRINT-PRETTY* here means "no linebreaks please"
697 (defun texinfo-begin (doc &aux *print-pretty*)
698 (let ((kind (get-kind doc)))
699 (format *texinfo-output* "@~A {~:(~A~)} ~(~A~@[ ~{~A~^ ~}~]~)~%"
700 (case kind
701 ((package constant variable)
702 "defvr")
703 ((structure class condition type)
704 "deftp")
706 "deffn"))
707 (map 'string (lambda (char) (if (eql char #\-) #\Space char)) (string kind))
708 (title-name doc)
709 ;; &foo would be amusingly bold in the pdf thanks to TeX/Texinfo
710 ;; interactions,so we escape the ampersand -- amusingly for TeX.
711 ;; sbcl.texinfo defines macros that expand @&key and friends to &key.
712 (mapcar (lambda (name)
713 (if (member name lambda-list-keywords)
714 (format nil "@~A" name)
715 name))
716 (lambda-list doc)))))
718 (defun texinfo-index (doc)
719 (let ((title (title-name doc)))
720 (case (get-kind doc)
721 ((structure type class condition)
722 (format *texinfo-output* "@tindex ~A~%" title))
723 ((variable constant)
724 (format *texinfo-output* "@vindex ~A~%" title))
725 ((compiler-macro function method-combination macro generic-function)
726 (format *texinfo-output* "@findex ~A~%" title)))))
728 (defun texinfo-inferred-body (doc)
729 (when (member (get-kind doc) '(class structure condition))
730 (let ((name (get-name doc)))
731 ;; class precedence list
732 (format *texinfo-output* "Class precedence list: @code{~(~{@lw{~A}~^, ~}~)}~%~%"
733 (remove-if (lambda (class) (hide-superclass-p name class))
734 (mapcar #'class-name (ensure-class-precedence-list (find-class name)))))
735 ;; slots
736 (let ((slots (remove-if (lambda (slot) (hide-slot-p name slot))
737 (class-direct-slots (find-class name)))))
738 (when slots
739 (format *texinfo-output* "Slots:~%@itemize~%")
740 (dolist (slot slots)
741 (format *texinfo-output*
742 "@item ~(@code{~A}~#[~:; --- ~]~
743 ~:{~2*~@[~2:*~A~P: ~{@code{@w{~S}}~^, ~}~]~:^; ~}~)~%~%"
744 (slot-definition-name slot)
745 (remove
747 (mapcar
748 (lambda (name things)
749 (if things
750 (list name (length things) things)))
751 '("initarg" "reader" "writer")
752 (list
753 (slot-definition-initargs slot)
754 (slot-definition-readers slot)
755 (slot-definition-writers slot)))))
756 ;; FIXME: Would be neater to handler as children
757 (write-texinfo-string (docstring slot t)))
758 (format *texinfo-output* "@end itemize~%~%"))))))
760 (defun texinfo-body (doc)
761 (write-texinfo-string (get-string doc)))
763 (defun texinfo-end (doc)
764 (write-line (case (get-kind doc)
765 ((package variable constant) "@end defvr")
766 ((structure type class condition) "@end deftp")
767 (t "@end deffn"))
768 *texinfo-output*))
770 (defun write-texinfo (doc)
771 "Writes TexInfo for a DOCUMENTATION instance to *TEXINFO-OUTPUT*."
772 (texinfo-anchor doc)
773 (texinfo-begin doc)
774 (texinfo-index doc)
775 (texinfo-inferred-body doc)
776 (texinfo-body doc)
777 (texinfo-end doc)
778 ;; FIXME: Children should be sorted one way or another
779 (mapc #'write-texinfo (get-children doc)))
781 ;;;; main logic
783 (defun collect-gf-documentation (gf)
784 "Collects method documentation for the generic function GF"
785 (loop for method in (generic-function-methods gf)
786 for doc = (maybe-documentation method t)
787 when doc
788 collect doc))
790 (defun collect-name-documentation (name)
791 (loop for type in *documentation-types*
792 for doc = (maybe-documentation name type)
793 when doc
794 collect doc))
796 (defun collect-symbol-documentation (symbol)
797 "Collects all docs for a SYMBOL and (SETF SYMBOL), returns a list of
798 the form DOC instances. See `*documentation-types*' for the possible
799 values of doc-type."
800 (nconc (collect-name-documentation symbol)
801 (collect-name-documentation (list 'setf symbol))))
803 (defun collect-documentation (package)
804 "Collects all documentation for all external symbols of the given
805 package, as well as for the package itself."
806 (let* ((*documentation-package* (find-package package))
807 (docs nil))
808 (check-type package package)
809 (do-external-symbols (symbol package)
810 (setf docs (nconc (collect-symbol-documentation symbol) docs)))
811 (let ((doc (maybe-documentation *documentation-package* t)))
812 (when doc
813 (push doc docs)))
814 docs))
816 (defmacro with-texinfo-file (pathname &body forms)
817 `(with-open-file (*texinfo-output* ,pathname
818 :direction :output
819 :if-does-not-exist :create
820 :if-exists :supersede)
821 ,@forms))
823 (defun generate-includes (directory &rest packages)
824 "Create files in `directory' containing Texinfo markup of all
825 docstrings of each exported symbol in `packages'. `directory' is
826 created if necessary. If you supply a namestring that doesn't end in a
827 slash, you lose. The generated files are of the form
828 \"<doc-type>_<packagename>_<symbol-name>.texinfo\" and can be included
829 via @include statements. Texinfo syntax-significant characters are
830 escaped in symbol names, but if a docstring contains invalid Texinfo
831 markup, you lose."
832 (handler-bind ((warning #'muffle-warning))
833 (let ((directory (merge-pathnames (pathname directory))))
834 (ensure-directories-exist directory)
835 (dolist (package packages)
836 (dolist (doc (collect-documentation (find-package package)))
837 (with-texinfo-file (merge-pathnames (include-pathname doc) directory)
838 (write-texinfo doc))))
839 directory)))
841 (defun document-package (package &optional filename)
842 "Create a file containing all available documentation for the
843 exported symbols of `package' in Texinfo format. If `filename' is not
844 supplied, a file \"<packagename>.texinfo\" is generated.
846 The definitions can be referenced using Texinfo statements like
847 @ref{<doc-type>_<packagename>_<symbol-name>.texinfo}. Texinfo
848 syntax-significant characters are escaped in symbol names, but if a
849 docstring contains invalid Texinfo markup, you lose."
850 (handler-bind ((warning #'muffle-warning))
851 (let* ((package (find-package package))
852 (filename (or filename (make-pathname
853 :name (string-downcase (package-name package))
854 :type "texinfo")))
855 (docs (sort (collect-documentation package) #'documentation<)))
856 (with-texinfo-file filename
857 (dolist (doc docs)
858 (write-texinfo doc)))
859 filename)))