]> git.donarmstrong.com Git - lilypond.git/blob - scm/document-translation.scm
Run scripts/auxiliar/fixscm.sh scm/*.scm
[lilypond.git] / scm / document-translation.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2000--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;                 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;;
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
10 ;;;;
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;;; GNU General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define (engraver-makes-grob? name-symbol grav)
20   (memq name-symbol (assoc 'grobs-created (ly:translator-description grav))))
21
22 (define (engraver-accepts-music-type? name-symbol grav)
23   (memq name-symbol (assoc 'events-accepted (ly:translator-description grav))))
24
25 (define (engraver-accepts-music-types? types grav)
26   (if (null? types)
27       #f
28       (or
29        (engraver-accepts-music-type? (car types) grav)
30        (engraver-accepts-music-types? (cdr types) grav))))
31
32 (define (engraver-doc-string engraver in-which-contexts)
33   (let* ((propsr (assoc-get 'properties-read (ly:translator-description engraver)))
34          (propsw (assoc-get 'properties-written (ly:translator-description engraver)))
35          (accepted  (assoc-get 'events-accepted (ly:translator-description engraver)))
36          (name-sym  (ly:translator-name engraver))
37          (name-str (symbol->string name-sym))
38          (desc (assoc-get 'description (ly:translator-description engraver)))
39          (grobs (engraver-grobs engraver)))
40
41     (string-append
42      desc
43      "\n\n"
44      (if (pair? accepted)
45          (string-append
46           "Music types accepted:\n\n"
47           (human-listify
48            (map ref-ify (sort (map symbol->string accepted) ly:string-ci<?))))
49          "")
50      "\n\n"
51      (if (pair? propsr)
52          (string-append
53           "Properties (read)"
54           (description-list->texi
55            (map (lambda (x) (property->texi 'translation x '()))
56                 (sort propsr ly:symbol-ci<?))
57            #t))
58          "")
59
60      (if (null? propsw)
61          ""
62          (string-append
63           "Properties (write)"
64           (description-list->texi
65            (map (lambda (x) (property->texi 'translation x '()))
66                 (sort propsw ly:symbol-ci<?))
67            #t)))
68      (if  (null? grobs)
69           ""
70           (string-append
71            "\n\nThis engraver creates the following layout object(s):\n\n"
72            (human-listify (map ref-ify (uniq-list (sort grobs ly:string-ci<?))))
73            "."))
74
75      "\n\n"
76
77      (if in-which-contexts
78          (let* ((layout-alist (ly:output-description $defaultlayout))
79                 (context-description-alist (map cdr layout-alist))
80                 (contexts
81                  (apply append
82                         (map
83                          (lambda (x)
84                            (let* ((context (assoc-get 'context-name x))
85                                   (group (assq-ref x 'group-type))
86                                   (consists (append
87                                              (if group
88                                                  (list group)
89                                                  '())
90                                              (assoc-get 'consists x))))
91                              (if (member name-sym consists)
92                                  (list context)
93                                  '())))
94                          context-description-alist)))
95                 (context-list (human-listify (map ref-ify
96                                                   (sort
97                                                    (map symbol->string contexts)
98                                                    ly:string-ci<?)))))
99            (string-append
100             "@code{" name-str "} "
101             (if (equal? context-list "none")
102                 "is not part of any context"
103                 (string-append
104                  "is part of the following context(s): "
105                  context-list))
106             "."))
107          ""))))
108
109 ;; First level Engraver description
110 (define (engraver-doc grav)
111   (make <texi-node>
112     #:name (symbol->string (ly:translator-name grav))
113     #:text (engraver-doc-string grav #t)))
114
115 ;; Second level, part of Context description
116 (define name->engraver-table (make-hash-table 61))
117 (map
118  (lambda (x)
119    (hash-set! name->engraver-table (ly:translator-name x) x))
120  (ly:get-all-translators))
121
122 (define (find-engraver-by-name name)
123   "NAME is a symbol."
124   (hash-ref name->engraver-table name #f))
125
126 (define (document-engraver-by-name name)
127   "NAME is a symbol."
128
129   (let* ((eg (find-engraver-by-name name)))
130
131     (cons (string-append "@code{" (ref-ify (symbol->string name)) "}")
132           (engraver-doc-string eg #f))))
133
134 (define (document-property-operation op)
135   (let ((tag (car op))
136         (context-sym (cadr op))
137         (args (cddr op))
138         )
139
140     (cond
141      ((equal?  tag 'push)
142       (let*
143           ((value (car args))
144            (path (cdr args)))
145
146         (string-append
147          "@item Set "
148          (format #f "grob-property @code{~a} "
149                  (string-join (map symbol->string path) " "))
150          (format #f "in @ref{~a} to ~a."
151                  context-sym (scm->texi value))
152          "\n")))
153      ((equal? (object-property context-sym 'is-grob?) #t) "")
154      ((equal? tag 'assign)
155       (format #f "@item Set translator property @code{~a} to ~a.\n"
156               context-sym
157               (scm->texi (car args))))
158      )))
159
160
161 (define (context-doc context-desc)
162   (let* ((name-sym (assoc-get 'context-name context-desc))
163          (name (symbol->string name-sym))
164          (aliases (map symbol->string (assoc-get 'aliases context-desc)))
165          (desc (assoc-get 'description context-desc "(not documented"))
166          (accepts (assoc-get 'accepts context-desc))
167          (consists (assoc-get 'consists context-desc))
168          (props (assoc-get 'property-ops context-desc))
169          (grobs  (context-grobs context-desc))
170          (grob-refs (map ref-ify (sort grobs ly:string-ci<?))))
171
172     (make <texi-node>
173       #:name name
174       #:text
175       (string-append
176        desc
177        (if (pair? aliases)
178            (string-append
179             "\n\nThis context also accepts commands for the following context(s):\n\n"
180             (human-listify (sort aliases ly:string-ci<?))
181             ".")
182            "")
183
184        "\n\nThis context creates the following layout object(s):\n\n"
185        (human-listify (uniq-list grob-refs))
186        "."
187
188        (if (and (pair? props) (not (null? props)))
189            (let ((str (apply string-append
190                              (sort (map document-property-operation props)
191                                    ly:string-ci<?))))
192              (if (string-null? str)
193                  ""
194                  (string-append
195                   "\n\nThis context sets the following properties:\n\n"
196                   "@itemize @bullet\n"
197                   str
198                   "@end itemize\n")))
199            "")
200
201        (if (null? accepts)
202            "\n\nThis context is a `bottom' context; it cannot contain other contexts."
203            (string-append
204             "\n\nContext "
205             name
206             " can contain\n"
207             (human-listify (map ref-ify (sort (map symbol->string accepts)
208                                               ly:string-ci<?)))
209             "."))
210
211        (if (null? consists)
212            ""
213            (string-append
214             "\n\nThis context is built from the following engraver(s):"
215             (description-list->texi
216              (map document-engraver-by-name (sort consists ly:symbol-ci<?))
217              #t)))))))
218
219 (define (engraver-grobs grav)
220   (let* ((eg (if (symbol? grav)
221                  (find-engraver-by-name grav)
222                  grav)))
223     (if (eq? eg #f)
224         '()
225         (map symbol->string (assoc-get 'grobs-created (ly:translator-description eg))))))
226
227 (define (context-grobs context-desc)
228   (let* ((group (assq-ref context-desc 'group-type))
229          (consists (append
230                     (if group
231                         (list group)
232                         '())
233                     (assoc-get 'consists context-desc)))
234          (grobs  (apply append
235                         (map engraver-grobs consists))))
236     grobs))
237
238 (define (all-contexts-doc)
239   (let* ((layout-alist
240           (sort (ly:output-description $defaultlayout)
241                 (lambda (x y) (ly:symbol-ci<? (car x) (car y)))))
242          (names (sort (map symbol->string (map car layout-alist)) ly:string-ci<?))
243          (contexts (map cdr layout-alist)))
244
245     (make <texi-node>
246       #:name "Contexts"
247       #:desc "Complete descriptions of all contexts."
248       #:children
249       (map context-doc contexts))))
250
251 (define all-engravers-list  (ly:get-all-translators))
252 (set! all-engravers-list
253       (sort all-engravers-list
254             (lambda (a b) (ly:string-ci<? (symbol->string (ly:translator-name a))
255                                           (symbol->string (ly:translator-name b))))))
256
257 (define (all-engravers-doc)
258   (make <texi-node>
259     #:name "Engravers and Performers"
260     #:desc "All separate engravers and performers."
261     #:text "See @ruser{Modifying context plug-ins}."
262     #:children
263     (map engraver-doc all-engravers-list)))
264
265 (define (translation-properties-doc-string lst)
266   (let* ((ps (sort (map symbol->string lst) ly:string-ci<?))
267          (sortedsyms (map string->symbol ps))
268          (propdescs
269           (map
270            (lambda (x) (property->texi 'translation  x '()))
271            sortedsyms))
272          (texi (description-list->texi propdescs #f)))
273     texi))
274
275 (define (translation-doc-node)
276   (make <texi-node>
277     #:name "Translation"
278     #:desc "From music to layout."
279     #:children
280     (list
281      (all-contexts-doc)
282      (all-engravers-doc)
283      (make <texi-node>
284        #:name "Tunable context properties"
285        #:desc "All tunable context properties."
286        #:text (translation-properties-doc-string
287                all-user-translation-properties))
288
289      (make <texi-node>
290        #:name "Internal context properties"
291        #:desc "All internal context properties."
292        #:text (translation-properties-doc-string
293                all-internal-translation-properties)))))