]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup-macros.scm
6c50ec51280e614796229fd565401b9474d16d07
[lilypond.git] / scm / markup-macros.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2003--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 "
19 Internally markup is stored as lists, whose head is a function.
20
21   (FUNCTION ARG1 ARG2 ... )
22
23 When the markup is formatted, then FUNCTION is called as follows
24
25   (FUNCTION GROB PROPS ARG1 ARG2 ... )
26
27 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
28 the rest of the arguments.
29
30 The function should return a stencil (i.e. a formatted, ready to
31 print object).
32
33
34 To add a markup command, use the define-markup-command utility.
35
36   (define-markup-command (mycommand layout prop arg1 ...) (arg1-type? ...)
37     \"my command usage and description\"
38     ...function body...)
39
40 The command is now available in markup mode, e.g.
41
42   \\markup { .... \\MYCOMMAND #1 argument ... }
43
44 " ; "
45
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 ;;; markup definer utilities
48
49 ;; For documentation purposes
50 ;; category -> markup functions
51 (define-public markup-functions-by-category (make-hash-table 150))
52 ;; markup function -> used properties
53 (define-public markup-functions-properties (make-weak-key-hash-table 151))
54 ;; List of markup list functions
55 (define-public markup-list-functions (make-weak-key-hash-table 151))
56
57 (use-modules (ice-9 optargs))
58
59 (defmacro*-public define-markup-command
60   (command-and-args signature
61                     #:key (category '()) (properties '())
62                     #:rest body)
63   "
64 * Define a COMMAND-markup function after command-and-args and body,
65 register COMMAND-markup and its signature,
66
67 * add COMMAND-markup to markup-functions-by-category,
68
69 * sets COMMAND-markup markup-signature object property,
70
71 * define a make-COMMAND-markup function.
72
73 Syntax:
74   (define-markup-command (COMMAND layout props . arguments)
75                                  argument-types
76                                  [ #:properties properties ]
77     \"documentation string\"
78     ...command body...)
79
80 where:
81   `argument-types' is a list of type predicates for arguments
82   `properties' a list of (property default-value) lists
83
84 The specified properties are available as let-bound variables in the
85 command body, using the respective `default-value' as fallback in case
86 `property' is not found in `props'.  `props' itself is left unchanged:
87 if you want defaults specified in that manner passed down into other
88 markup functions, you need to adjust `props' yourself.
89
90 The autogenerated documentation makes use of some optional
91 specifications that are otherwise ignored:
92
93 After `argument-types', you may also specify
94                                  [ #:category category ]
95 where:
96   `category' is either a symbol or a symbol list specifying the
97              category for this markup command in the docs.
98
99 As an element of the `properties' list, you may directly use a
100 COMMANDx-markup symbol instead of a `(prop value)' list to indicate
101 that this markup command is called by the newly defined command,
102 adding its properties to the documented properties of the new
103 command.  There is no protection against circular definitions.
104 "
105   (let* ((command (car command-and-args))
106          (args (cdr command-and-args))
107          (command-name (string->symbol (format #f "~a-markup" command)))
108          (make-markup-name (string->symbol (format #f "make-~a-markup" command))))
109     (while (and (pair? body) (keyword? (car body)))
110            (set! body (cddr body)))
111     `(begin
112        ;; define the COMMAND-markup function
113        ,(let* ((documentation
114                 (format #f "~a\n~a" (cddr args)
115                         (if (string? (car body)) (car body) "")))
116                (real-body (if (or (not (string? (car body)))
117                                   (null? (cdr body)))
118                               body (cdr body))))
119           `(define-public (,command-name ,@args)
120              ,documentation
121              (let ,(map (lambda (prop-spec)
122                           (let ((prop (car prop-spec))
123                                 (default-value (if (null? (cdr prop-spec))
124                                                    #f
125                                                    (cadr prop-spec)))
126                                 (props (cadr args)))
127                             `(,prop (chain-assoc-get ',prop ,props ,default-value))))
128                         (filter pair? properties))
129                ,@real-body)))
130        (set! (markup-command-signature ,command-name) (list ,@signature))
131        ;; Register the new function, for markup documentation
132        ,@(map (lambda (category)
133                 `(hashq-set!
134                   (or (hashq-ref markup-functions-by-category ',category)
135                       (let ((hash (make-weak-key-hash-table 151)))
136                         (hashq-set! markup-functions-by-category ',category
137                                     hash)
138                         hash))
139                   ,command-name #t))
140               (if (list? category) category (list category)))
141        ;; Used properties, for markup documentation
142        (hashq-set! markup-functions-properties
143                    ,command-name
144                    (list ,@(map (lambda (prop-spec)
145                                   (cond ((symbol? prop-spec)
146                                          prop-spec)
147                                         ((not (null? (cdr prop-spec)))
148                                          `(list ',(car prop-spec) ,(cadr prop-spec)))
149                                         (else
150                                          `(list ',(car prop-spec)))))
151                                 (if (pair? args)
152                                     properties
153                                     (list)))))
154        ;; define the make-COMMAND-markup function
155        (define-public (,make-markup-name . args)
156          (let ((sig (list ,@signature)))
157            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
158
159 (defmacro*-public define-markup-list-command
160   (command-and-args signature #:key (properties '()) #:rest body)
161   "Same as `define-markup-command', but defines a command that, when
162 interpreted, returns a list of stencils instead of a single one"
163   (let* ((command (car command-and-args))
164          (args (cdr command-and-args))
165          (command-name (string->symbol (format #f "~a-markup-list" command)))
166          (make-markup-name (string->symbol (format #f "make-~a-markup-list" command))))
167     (while (and (pair? body) (keyword? (car body)))
168            (set! body (cddr body)))
169     `(begin
170        ;; define the COMMAND-markup-list function
171        ,(let* ((documentation
172                 (format #f "~a\n~a" (cddr args)
173                         (if (string? (car body)) (car body) "")))
174                (real-body (if (or (not (string? (car body)))
175                                   (null? (cdr body)))
176                               body (cdr body))))
177           `(define-public (,command-name ,@args)
178              ,documentation
179              (let ,(map (lambda (prop-spec)
180                           (let ((prop (car prop-spec))
181                                 (default-value (if (null? (cdr prop-spec))
182                                                    #f
183                                                    (cadr prop-spec)))
184                                 (props (cadr args)))
185                             `(,prop (chain-assoc-get ',prop ,props ,default-value))))
186                         (filter pair? properties))
187                ,@real-body)))
188        (set! (markup-command-signature ,command-name) (list ,@signature))
189        ;; add the command to markup-list-function-list, for markup documentation
190        (hashq-set! markup-list-functions ,command-name #t)
191        ;; Used properties, for markup documentation
192        (hashq-set! markup-functions-properties
193                    ,command-name
194                    (list ,@(map (lambda (prop-spec)
195                                   (cond ((symbol? prop-spec)
196                                          prop-spec)
197                                         ((not (null? (cdr prop-spec)))
198                                          `(list ',(car prop-spec) ,(cadr prop-spec)))
199                                         (else
200                                          `(list ',(car prop-spec)))))
201                                 (if (pair? args)
202                                     properties
203                                     (list)))))
204        ;; it's a markup-list command:
205        (set-object-property! ,command-name 'markup-list-command #t)
206        ;; define the make-COMMAND-markup-list function
207        (define-public (,make-markup-name . args)
208          (let ((sig (list ,@signature)))
209            (list (make-markup ,command-name
210                               ,(symbol->string make-markup-name) sig args)))))))
211
212 ;;;;;;;;;;;;;;;
213 ;;; Utilities for storing and accessing markup commands signature
214 ;;; Examples:
215 ;;;
216 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
217 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
218 ;;;
219 ;;; (markup-command-signature raise-markup)
220 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
221 ;;;
222
223 (define-public (markup-command-signature-ref markup-command)
224   "Return markup-command's signature (the 'markup-signature object property)"
225   (object-property markup-command 'markup-signature))
226
227 (define-public (markup-command-signature-set! markup-command signature)
228   "Set markup-command's signature (as object property)"
229   (set-object-property! markup-command 'markup-signature signature)
230   signature)
231
232 (define-public markup-command-signature
233   (make-procedure-with-setter markup-command-signature-ref
234                               markup-command-signature-set!))
235
236 ;;;;;;;;;;;;;;;;;;;;;;
237 ;;; markup type predicates
238
239 (define-public (markup-function? x)
240   (and (markup-command-signature x)
241        (not (object-property x 'markup-list-command))))
242
243 (define-public (markup-list-function? x)
244   (and (markup-command-signature x)
245        (object-property x 'markup-list-command)))
246
247 (define-public (markup-command-list? x)
248   "Determine if `x' is a markup command list, ie. a list composed of
249 a markup list function and its arguments."
250   (and (pair? x) (markup-list-function? (car x))))
251
252 (define-public (markup-list? arg)
253   "Return a true value if `x' is a list of markups or markup command lists."
254   (define (markup-list-inner? lst)
255     (or (null? lst)
256         (and (or (markup? (car lst)) (markup-command-list? (car lst)))
257              (markup-list-inner? (cdr lst)))))
258   (not (not (and (list? arg) (markup-list-inner? arg)))))
259
260 (define (markup-argument-list? signature arguments)
261   "Typecheck argument list."
262   (if (and (pair? signature) (pair? arguments))
263       (and ((car signature) (car arguments))
264            (markup-argument-list? (cdr signature) (cdr arguments)))
265       (and (null? signature) (null? arguments))))
266
267
268 (define (markup-argument-list-error signature arguments number)
269   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
270 #f is no error found.
271 "
272   (if (and (pair? signature) (pair? arguments))
273       (if (not ((car signature) (car arguments)))
274           (list number (type-name (car signature)) (car arguments))
275           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
276       #f))
277
278 ;;
279 ;; full recursive typecheck.
280 ;;
281 (define (markup-typecheck? arg)
282   (or (string? arg)
283       (and (pair? arg)
284            (markup-function? (car arg))
285            (markup-argument-list? (markup-command-signature (car arg))
286                                   (cdr arg)))))
287
288 ;;
289 ;;
290 ;;
291 ;;
292 (define (markup-thrower-typecheck arg)
293   "typecheck, and throw an error when something amiss.
294
295 Uncovered - cheap-markup? is used."
296
297   (cond ((string? arg) #t)
298         ((not (pair? arg))
299          (throw 'markup-format "Not a pair" arg))
300         ((not (markup-function? (car arg)))
301          (throw 'markup-format "Not a markup function " (car arg)))
302         ((not (markup-argument-list? (markup-command-signature (car arg))
303                                      (cdr arg)))
304          (throw 'markup-format "Arguments failed  typecheck for " arg)))
305   #t)
306
307 ;;
308 ;; good enough if you only  use make-XXX-markup functions.
309 ;;
310 (define (cheap-markup? x)
311   (or (string? x)
312       (and (pair? x)
313            (markup-function? (car x)))))
314
315 ;;
316 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
317 ;;
318 (define-public markup? cheap-markup?)
319
320 (define-public (make-markup markup-function make-name signature args)
321   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
322 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
323 "
324   (let* ((arglen (length args))
325          (siglen (length signature))
326          (error-msg (if (and (> siglen 0) (> arglen 0))
327                         (markup-argument-list-error signature args 1)
328                         #f)))
329     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
330         (ly:error (string-append make-name ": "
331                                  (_ "Wrong number of arguments.  Expect: ~A, found ~A: ~S"))
332                   siglen arglen args))
333     (if error-msg
334         (ly:error
335          (string-append
336           make-name ": "
337           (_ "Invalid argument in position ~A.  Expect: ~A, found: ~S."))
338          (car error-msg) (cadr error-msg)(caddr error-msg))
339         (cons markup-function args))))
340
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;; markup constructors
343 ;;; lilypond-like syntax for markup construction in scheme.
344
345 (use-modules (ice-9 receive))
346
347 (define (compile-all-markup-expressions expr)
348   "Return a list of canonical markups expressions, e.g.:
349   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
350   ===>
351   ((make-COMMAND1-markup arg11 arg12)
352    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
353   (do ((rest expr rest)
354        (markps '() markps))
355       ((null? rest) (reverse markps))
356     (receive (m r) (compile-markup-expression rest)
357              (set! markps (cons m markps))
358              (set! rest r))))
359
360 (define (keyword->make-markup key)
361   "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
362   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
363
364 (define (compile-markup-expression expr)
365   "Return two values: the first complete canonical markup expression
366    found in `expr', e.g. (make-COMMAND-markup arg1 arg2 ...),
367    and the rest expression."
368   (cond ((and (pair? expr)
369               (keyword? (car expr)))
370          ;; expr === (#:COMMAND arg1 ...)
371          (let ((command (symbol->string (keyword->symbol (car expr)))))
372            (if (not (pair? (lookup-markup-command command)))
373                (ly:error (_ "Not a markup command: ~A") command))
374            (let* ((sig (markup-command-signature
375                         (car (lookup-markup-command command))))
376                   (sig-len (length sig)))
377              (do ((i 0 (1+ i))
378                   (args '() args)
379                   (rest (cdr expr) rest))
380                  ((>= i sig-len)
381                   (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
382                (cond ((eqv? (list-ref sig i) markup-list?)
383                       ;; (car rest) is a markup list
384                       (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
385                       (set! rest (cdr rest)))
386                      (else
387                       ;; pick up one arg in `rest'
388                       (receive (a r) (compile-markup-arg rest)
389                                (set! args (cons a args))
390                                (set! rest r))))))))
391         ((and (pair? expr)
392               (pair? (car expr))
393               (keyword? (caar expr)))
394          ;; expr === ((#:COMMAND arg1 ...) ...)
395          (receive (m r) (compile-markup-expression (car expr))
396                   (values m (cdr expr))))
397         ((and (pair? expr)
398               (string? (car expr))) ;; expr === ("string" ...)
399          (values `(make-simple-markup ,(car expr)) (cdr expr)))
400         (else
401          ;; expr === (symbol ...) or ((funcall ...) ...)
402          (values (car expr)
403                  (cdr expr)))))
404
405 (define (compile-all-markup-args expr)
406   "Transform `expr' into markup arguments"
407   (do ((rest expr rest)
408        (args '() args))
409       ((null? rest) (reverse args))
410     (receive (a r) (compile-markup-arg rest)
411              (set! args (cons a args))
412              (set! rest r))))
413
414 (define (compile-markup-arg expr)
415   "Return two values: the desired markup argument, and the rest arguments"
416   (cond ((null? expr)
417          ;; no more args
418          (values '() '()))
419         ((keyword? (car expr))
420          ;; expr === (#:COMMAND ...)
421          ;; ==> build and return the whole markup expression
422          (compile-markup-expression expr))
423         ((and (pair? (car expr))
424               (keyword? (caar expr)))
425          ;; expr === ((#:COMMAND ...) ...)
426          ;; ==> build and return the whole markup expression(s)
427          ;; found in (car expr)
428          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
429                   (if (null? rest-expr)
430                       (values markup-expr (cdr expr))
431                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
432                               (cdr expr)))))
433         ((and (pair? (car expr))
434               (pair? (caar expr)))
435          ;; expr === (((foo ...) ...) ...)
436          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
437         (else (values (car expr) (cdr expr)))))
438
439 (define (lookup-markup-command-aux symbol)
440   (let ((proc (catch 'misc-error
441                      (lambda ()
442                        (module-ref (current-module) symbol))
443                      (lambda (key . args) #f))))
444     (and (procedure? proc) proc)))
445
446 (define-public (lookup-markup-command code)
447   (let ((proc (lookup-markup-command-aux
448                (string->symbol (format #f "~a-markup" code)))))
449     (and proc (markup-function? proc)
450          (cons proc (markup-command-signature proc)))))
451
452 (define-public (lookup-markup-list-command code)
453   (let ((proc (lookup-markup-command-aux
454                (string->symbol (format #f "~a-markup-list" code)))))
455     (and proc (markup-list-function? proc)
456          (cons proc (markup-command-signature proc)))))