-
Notifications
You must be signed in to change notification settings - Fork 266
/
manual.lisp
212 lines (199 loc) · 9.68 KB
/
manual.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
;; Copyright (C) 2007-2008 Shawn Betts
;;
;; This file is part of stumpwm.
;;
;; stumpwm is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; stumpwm is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this software; see the file COPYING. If not, see
;; <http://www.gnu.org/licenses/>.
;; Commentary:
;;
;; Generate the texinfo manual from docstrings in the source.
;;
;; Code:
(in-package #:stumpwm)
(require :sb-introspect)
(defun format-lambda-list (texinfo-output list)
"Print the lambda list LIST to the stream TEXINFO-OUTPUT. The lambda list is
printed with only the argument names and types, followed by a list of default
arguments when applicable. This function assumes that it is printing within the
context of a @defun, @deffn, @defmac, or similar. It is also assumed that 80 is
the maximum line width."
(let ((*print-pretty* nil)
(format-string " ~A")
(stream texinfo-output)
optionals rest keys aux)
(declare (ignorable optionals rest keys aux))
(macrolet ((with-bind-check
((var list &optional (argtypes '(&optional &rest &key &aux))
(dispatcher 'dispatch-format))
&body body)
(let ((l (gensym)))
`(let* ((,l ,list)
(,var (car ,l)))
(cond ((member ,var ',argtypes)
(,dispatcher ,var (cdr ,l)))
((null ,var)
nil)
(t ,@body))))))
(labels ((dispatch-format (type list)
(when type
(format stream format-string type)
(case type
((&optional) (format-optional list))
((&rest) (format-rest list))
((&key) (format-key list))
((&aux) nil)
(otherwise (format-unknown list)))))
(format-key-optional (arg)
(destructuring-bind (name &optional default provided)
(if (atom arg) (list arg) arg)
(format stream format-string name)
(list name default provided)))
(format-normal (list &optional in-list)
(with-bind-check (arg list)
(if (listp arg)
(progn
(format stream "(")
(format-normal arg t)
(format stream ")")
(format-normal (cdr list)))
(progn
(format stream
(if in-list
"~A"
format-string)
arg)
(format-normal (cdr list))))))
(format-optional (list)
(with-bind-check (arg list '(&rest &key &aux))
(push (format-key-optional arg) optionals)
(format-optional (cdr list))))
(format-rest (list)
(with-bind-check (arg list '(&key &aux))
(format stream format-string arg)
(format-rest (cdr list))))
(format-key (list)
(with-bind-check (arg list '(&aux))
(push (format-key-optional arg) keys)
(format-key (cdr list))))
(format-unknown (list)
(format stream format-string (car list))
(dispatch-format (cadr list) (cddr list))))
(format-normal list)
(let* ((opts-keys (append (reverse optionals) (reverse keys)))
(len (length (string (caar (sort (copy-seq opts-keys)
(lambda (a b)
(> (length (string (car a)))
(length (string (car b))))))))))
(argstr (concatenate 'string
" ~A~"
(format nil "~D" (+ 4 len))
"T"))
(valstr (if (> len 46)
"~% ~S~%"
"~S~%")))
(terpri stream)
(when opts-keys
(format stream "Default Values:~%@verbatim~%")
(let ((*print-right-margin* (- 80 (+ 4 len))))
(dolist (var opts-keys)
(destructuring-bind (name default provided) var
(declare (ignore provided))
(format stream argstr name)
(let ((*print-pretty* t))
(format stream valstr default)))))
(format stream "@end verbatim~%")))))))
(defun generate-function-doc (s line)
(ppcre:register-groups-bind (name) ("^@@@ (.*)" line)
(let ((fn-name (with-standard-io-syntax
(let ((*package* (find-package :stumpwm)))
(read-from-string name)))))
(if (fboundp fn-name)
(let ((fn (fdefinition fn-name))
(*print-pretty* nil))
(format s "@defun {~A} " name)
(format-lambda-list s (sb-introspect:function-lambda-list fn))
(format s "~A~&@end defun~%~%" (documentation fn 'function))
t)
(warn "Function ~A not found." fn-name)))))
(defun generate-macro-doc (s line)
(ppcre:register-groups-bind (name) ("^%%% (.*)" line)
(let* ((symbol (find-symbol (string-upcase name) :stumpwm))
(*print-pretty* nil))
(format s "@defmac {~A} " name)
(format-lambda-list s (sb-introspect:function-lambda-list
(macro-function symbol)))
(format s "~A~&@end defmac~%~%" (documentation symbol 'function))
t)))
(defun generate-variable-doc (s line)
(ppcre:register-groups-bind (name) ("^### (.*)" line)
(let ((sym (find-symbol (string-upcase name) :stumpwm)))
(format s "@defvar ~a~%~a~&@end defvar~%~%"
name (documentation sym 'variable))
t)))
(defun generate-hook-doc (s line)
(ppcre:register-groups-bind (name) ("^\\$\\$\\$ (.*)" line)
(let ((sym (find-symbol (string-upcase name) :stumpwm)))
(format s "@defvr {Hook} ~a~%~a~&@end defvr~%~%"
name (documentation sym 'variable))
t)))
(defun generate-command-doc (s line)
(ppcre:register-groups-bind (name) ("^!!! (.*)" line)
(if-let (symbol (find-symbol (string-upcase name) :stumpwm))
(let ((cmd (symbol-function symbol))
(*print-pretty* nil))
(format s "@deffn {Command} ~A " name)
(format-lambda-list s (sb-introspect:function-lambda-list cmd))
(format s "~A~&@end deffn~%~%" (documentation cmd 'function))
t)
(warn "Symbol ~A not found in package STUMPWM" name))))
(defun generate-class-doc (s line)
(ppcre:register-groups-bind (name) ("^€€€ (.*)" line)
(let ((sym (find-symbol (string-upcase name) :stumpwm)))
(if sym
(let ((class (find-class sym)))
(if class
(progn
(format s "@deftp {Class} ~A ~{~A~^ ~}~%~ADirect Superclasses: ~{~A~^, ~}@*~&Direct Subclasses: ~{~A~^, ~}@*~&Direct Slots: @*@ @ ~{~{~A~^@ -@ ~}~^@*@ @ ~}@*~&@end deftp~%~%"
sym
(mapcar #'sb-mop:slot-definition-name
(sb-mop:class-direct-slots class))
(let ((doc (documentation class t)))
(if doc
(concatenate 'string doc "@*")
""))
(mapcar #'sb-mop:class-name
(sb-mop:class-direct-superclasses class))
(mapcar #'sb-mop:class-name
(sb-mop:class-direct-subclasses class))
(mapcar (lambda (slot)
(let ((name (sb-mop:slot-definition-name slot))
(docs (documentation slot t)))
(if docs
(list name docs)
(list name))))
(sb-mop:class-direct-slots class)))
t)
(warn "Symbol ~A does not denote a class" sym)))
(warn "Symbol ~A not found in package STUMPWM" sym)))))
(defun generate-manual (&key (in #p"stumpwm.texi.in") (out #p"stumpwm.texi"))
(let ((*print-case* :downcase))
(with-open-file (os out :direction :output :if-exists :supersede)
(with-open-file (is in :direction :input)
(loop for line = (read-line is nil is)
until (eq line is) do
(or (generate-function-doc os line)
(generate-macro-doc os line)
(generate-hook-doc os line)
(generate-variable-doc os line)
(generate-command-doc os line)
(generate-class-doc os line)
(write-line line os)))))))