More incorrect next_free_page usage.
[sbcl.git] / doc / manual / docstrings.lisp
blobede40cfa6dbcd2c83065ae17ee9b182536fdd57b
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 ;; 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)
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 (flet ((specializers (ll)
220 (let (result)
221 (dolist (arg ll)
222 (cond
223 ((member arg lambda-list-keywords) (return))
224 ((atom arg) (push t result))
225 (t (push (second arg) result))))
226 (nreverse result))))
227 (format nil "~A~{ ~A~} ~A"
228 (name-using-kind/name nil (first name) doc)
229 (second name)
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)))
240 (car sorted)))
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))
252 name)
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))))
260 (if earmuffsp
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"
271 (second name)
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"
284 (case (get-kind doc)
285 ((function generic-function) "fun")
286 (structure "struct")
287 (variable "var")
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
311 :name (name x)
312 :kind 'package
313 :string string))
315 (defmethod make-documentation (x (doc-type (eql 'function)) string)
316 (declare (ignore doc-type))
317 (let* ((fdef (and (fboundp x) (fdefinition x)))
318 (name x)
319 (kind (cond ((and (symbolp x) (special-operator-p x))
320 'special-operator)
321 ((and (symbolp x) (macro-function x))
322 'macro)
323 ((typep fdef 'generic-function)
324 (assert (or (symbolp name) (setf-name-p name)))
325 'generic-function)
326 (fdef
327 (assert (or (symbolp name) (setf-name-p name)))
328 'function)))
329 (children (when (eq kind 'generic-function)
330 (collect-gf-documentation fdef))))
331 (make-instance 'documentation
332 :name (name x)
333 :string string
334 :kind kind
335 :children children)))
337 (defmethod make-documentation ((x method) doc-type string)
338 (declare (ignore doc-type))
339 (make-instance 'documentation
340 :name (name x)
341 :kind 'method
342 :string string))
344 (defmethod make-documentation (x (doc-type (eql 'type)) string)
345 (make-instance 'documentation
346 :name (name x)
347 :string string
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
356 :name (name x)
357 :string string
358 :kind (if (constantp x)
359 'constant
360 'variable)))
362 (defmethod make-documentation (x (doc-type (eql 'setf)) string)
363 (declare (ignore doc-type))
364 (make-instance 'documentation
365 :name (name x)
366 :kind 'setf-expander
367 :string string))
369 (defmethod make-documentation (x doc-type string)
370 (make-instance 'documentation
371 :name (name x)
372 :kind doc-type
373 :string string))
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)))
379 (when docstring
380 (make-documentation x doc-type docstring))))
382 (defun lambda-list (doc)
383 (case (get-kind doc)
384 ((package constant variable type structure class condition nil)
385 nil)
386 (method
387 (third (get-name doc)))
389 ;; KLUDGE: Eugh.
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)
395 (typecase x
396 (atom x)
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))
402 ;; Skip these
403 (clean (cdr x) :optional optional :key key))
404 ((cons cons)
405 (cons
406 (cond (key (if (consp (caar x))
407 (caaar x)
408 (caar x)))
409 (optional (caar x))
410 (t (clean (car x))))
411 (clean (cdr x) :key key :optional optional)))
412 (cons
413 (cons
414 (cond ((or key optional) (car x))
415 (t (clean (car x))))
416 (clean (cdr x) :key key :optional optional))))))
417 (multiple-value-bind (ll unknown) (sb-introspect:function-lambda-list (get-name doc))
418 (if unknown
419 (values nil t)
420 (clean ll))))))))
422 (defun get-string-name (x)
423 (let ((name (get-name x)))
424 (cond ((symbolp name)
425 (symbol-name name))
426 ((and (consp name) (eq 'setf (car name)))
427 (symbol-name (second name)))
428 ((stringp name)
429 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))
438 (< p1 p2))))
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)))))
456 ;;; line markups
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 ...
463 (let (result)
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))))
470 (when end
471 (every (lambda (char) (find char *symbol-characters*))
472 (subseq line start end))))))
473 (do ((begin nil)
474 (maybe-begin t)
475 (i 0 (1+ i)))
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)))))
480 (grab begin i))
481 (nreverse result))
482 (cond
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))))
486 (grab begin i))
487 (setf begin nil
488 maybe-begin t))
489 ((and begin (not (find (char line i) *symbol-characters*)))
490 ;; Not a symbol: abort
491 (setf begin nil))
492 ((and maybe-begin (not begin) (find (char line i) *symbol-characters*))
493 ;; potential symbol begin at this position
494 (setf begin i
495 maybe-begin nil))
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)))
504 (when exp
505 (grab i end)
506 (setf begin nil
507 maybe-begin nil
508 i end))))
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)
518 (let ((last 0))
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*
523 :test #'string=)
524 "@var{~A}"
525 "@code{~A}")
526 (string-downcase symbol-name)))
527 (setf last (second symbol/index)))
528 (write-string (subseq line last) result))))
530 ;;; lisp sections
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)))
537 (and offset
538 (plusp offset)
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)))
551 collect line)))
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))))
561 (and offset
562 (member char *itemize-start-characters* :test #'char=)
563 (char= #\Space (find-if-not (lambda (c) (char= c char))
564 line :start offset))
565 offset)))
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)))
570 (result nil)
571 (lines-consumed 0))
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)
576 do (cond
577 ((not indentation)
578 ;; empty line -- inserts paragraph.
579 (push "" result)
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))
591 ;; start of new item
592 (push (format nil "@item ~A"
593 (texinfo-line (subseq line (1+ offset))))
594 result)
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
602 (loop-finish))))
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"))
606 nil)))
608 ;;; table sections
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))
641 offset))))
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))
649 (result nil)
650 (lines-consumed 0))
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)
655 do (cond
656 ((not indentation)
657 ;; empty line -- inserts paragraph.
658 (push "" result)
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))
664 result)
665 (progn
666 (push "" result)
667 (push (format nil "@item ~A" (texinfo-line line))
668 result)))
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
676 (loop-finish))))
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" ""))
681 nil)))
683 ;;; section markup
685 (defmacro with-maybe-section (index &rest forms)
686 `(multiple-value-bind (count collected) (progn ,@forms)
687 (when count
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)
698 do (cond
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
718 ;; internal as well.
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
730 ))))
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~)"
746 (case kind
747 ((package constant variable)
748 "defvr")
749 ((structure class condition type)
750 "deftp")
752 "deffn"))
753 (map 'string (lambda (char) (if (eql char #\-) #\Space char)) (string kind))
754 (title-name doc))
755 (multiple-value-bind (lambda-list unknown) (lambda-list doc)
756 (cond (unknown
757 (format *texinfo-output* " @emph{lambda list not known}"))
758 ((not lambda-list))
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)))
769 name))
770 lambda-list)))))
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)))))
780 ;; slots
781 (let ((slots (remove-if (lambda (slot) (hide-slot-p name slot))
782 (class-direct-slots (find-class name)))))
783 (when slots
784 (format *texinfo-output* "Slots:~%@itemize~%")
785 (dolist (slot slots)
786 (format *texinfo-output*
787 "@item ~(@code{~A}~#[~:; --- ~]~
788 ~:{~2*~@[~2:*~A~P: ~{@code{@w{~S}}~^, ~}~]~:^; ~}~)~%~%"
789 (slot-definition-name slot)
790 (remove
792 (mapcar
793 (lambda (name things)
794 (if things
795 (list name (length things) things)))
796 '("initarg" "reader" "writer")
797 (list
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")
812 (t "@end deffn"))
813 *texinfo-output*))
815 (defun write-texinfo (doc)
816 "Writes TexInfo for a DOCUMENTATION instance to *TEXINFO-OUTPUT*."
817 (texinfo-anchor doc)
818 (texinfo-begin doc)
819 (texinfo-inferred-body doc)
820 (texinfo-body doc)
821 (texinfo-end doc)
822 ;; FIXME: Children should be sorted one way or another
823 (mapc #'write-texinfo (get-children doc)))
825 ;;;; main logic
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)
831 when doc
832 collect doc))
834 (defun collect-name-documentation (name)
835 (loop for type in *documentation-types*
836 for doc = (maybe-documentation name type)
837 when doc
838 collect doc))
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
843 values of doc-type."
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))
851 (docs nil))
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)))
856 (when doc
857 (push doc docs)))
858 docs))
860 (defmacro with-texinfo-file (pathname &body forms)
861 `(with-open-file (*texinfo-output* ,pathname
862 :direction :output
863 :if-does-not-exist :create
864 :if-exists :supersede)
865 ,@forms))
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
888 ;; hard-coded).
889 (format *texinfo-output* "~
890 @iftex
891 @macro ~A{name}
892 {@smallertt@phantom{concurrency:}~@[@llap{~(~A~):}~]}\\name\\
893 @end macro
894 @end iftex
895 @ifinfo
896 @macro ~2:*~A{name}
897 \\name\\
898 @end macro
899 @end ifinfo
900 @ifnottex
901 @ifnotinfo
902 @macro ~:*~A{name}
903 \\name\\ ~@[[~(~A~)]~]
904 @end macro
905 @end ifnotinfo
906 @end ifnottex~%"
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
917 markup, you lose."
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"))
929 directory)))
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))
944 :type "texinfo")))
945 (docs (sort (collect-documentation package) #'documentation<)))
946 (with-texinfo-file filename
947 (dolist (doc docs)
948 (write-texinfo doc)))
949 filename)))