1 @c -*- coding: latin-1; mode: texinfo; -*-
2 @node Interfaces for programmers
3 @chapter Interfaces for programmers
8 * Programmer interfaces for input ::
9 * Markup programmer interface::
10 * Contexts for programmers::
13 @node Programmer interfaces for input
14 @section Programmer interfaces for input
17 * Input variables and Scheme::
18 * Internal music representation::
19 * Extending music syntax::
20 * Manipulating music expressions::
21 * Displaying music expressions::
22 * Using LilyPond syntax inside Scheme::
25 @node Input variables and Scheme
26 @subsection Input variables and Scheme
29 The input format supports the notion of variables: in the following
30 example, a music expression is assigned to a variable with the name
33 traLaLa = @{ c'4 d'4 @}
38 There is also a form of scoping: in the following example, the
39 @code{\layout} block also contains a @code{traLaLa} variable, which is
40 independent of the outer @code{\traLaLa}.
42 traLaLa = @{ c'4 d'4 @}
43 \layout @{ traLaLa = 1.0 @}
46 In effect, each input file is a scope, and all @code{\header},
47 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
50 Both variables and scoping are implemented in the GUILE module system.
51 An anonymous Scheme module is attached to each scope. An assignment of
54 traLaLa = @{ c'4 d'4 @}
58 is internally converted to a Scheme definition
60 (define traLaLa @var{Scheme value of ``@code{... }''})
63 This means that input variables and Scheme variables may be freely
64 mixed. In the following example, a music fragment is stored in the
65 variable @code{traLaLa}, and duplicated using Scheme. The result is
66 imported in a @code{\score} block by means of a second variable
69 traLaLa = @{ c'4 d'4 @}
71 #(define newLa (map ly:music-deep-copy
72 (list traLaLa traLaLa)))
74 (make-sequential-music newLa))
79 In the above example, music expressions can be `exported' from the
80 input to the Scheme interpreter. The opposite is also possible. By
81 wrapping a Scheme value in the function @code{ly:export}, a Scheme
82 value is interpreted as if it were entered in LilyPond syntax. Instead
83 of defining @code{\twice}, the example above could also have been
87 @{ #(ly:export (make-sequential-music (list newLa))) @}
92 Mixing Scheme and LilyPond identifiers is not possible with the
95 @node Internal music representation
96 @subsection Internal music representation
98 When a music expression is parsed, it is converted into a set of
99 Scheme music objects. The defining property of a music object is that
100 it takes up time. Time is a rational number that measures the length
101 of a piece of music, in whole notes.
103 A music object has three kinds of types:
106 music name: Each music expression has a name, for example, a note
107 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
108 a @internalsref{SimultaneousMusic}. A list of all expressions
109 available is in the internals manual, under
110 @internalsref{Music expressions}.
113 `type' or interface: Each music name has several `types' or
114 interfaces, for example, a note is an @code{event}, but it is also a
115 @code{note-event}, a @code{rhythmic-event}, and a @code{melodic-event}.
117 All classes of music are listed in the internals manual, under
118 @internalsref{Music classes}.
121 C++ object: Each music object is represented by a C++ object. For
122 technical reasons, different music objects may be represented by
123 different C++ object types. For example, a note is @code{Event}
124 object, while @code{\grace} creates a @code{Grace_music} object.
126 We expect that distinctions between different C++ types will disappear
130 The actual information of a music expression is stored in properties.
131 For example, a @internalsref{NoteEvent} has @code{pitch} and
132 @code{duration} properties that store the pitch and duration of that
133 note. A list of all properties available is in the internals manual,
134 under @internalsref{Music properties}.
136 A compound music expression is a music object that contains other
137 music objects in its properties. A list of objects can be stored in
138 the @code{elements} property of a music object, or a single `child'
139 music object in the @code{element} object. For example,
140 @internalsref{SequentialMusic} has its children in @code{elements},
141 and @internalsref{GraceMusic} has its single argument in
142 @code{element}. The body of a repeat is stored in the @code{element}
143 property of @internalsref{RepeatedMusic}, and the alternatives in
149 @node Extending music syntax
150 @subsection Extending music syntax
152 @c TODO: rewrite example.
153 @c The use of FUNC as example argument is rather confusing.
155 The syntax of composite music expressions, like @code{\repeat},
156 @code{\transpose}, and @code{\context} follows the general form of
159 \@code{keyword} @var{non-music-arguments} @var{music-arguments}
162 Such syntax can also be defined as user code. To do this, it is
163 necessary to create a @emph{music function}. This is a specially marked
164 Scheme function. For example, the music function @code{\applymusic} applies
165 a user-defined function to a music expression. Its syntax is
168 \applymusic #@var{func} @var{music}
171 A music function is created with @code{ly:make-music-function},
174 (ly:make-music-function
177 @code{\applymusic} takes a Scheme function and a Music expression as
178 arguments. This is encoded in its parameter list,
181 (list procedure? ly:music?)
184 The function itself takes another argument, an Input location
185 object. That object is used to provide error messages with file names
186 and line numbers. The definition is the second argument of
187 @code{ly:make-music-function}. The body simply calls the function
190 (lambda (where func music)
194 The above Scheme code only defines the functionality. The tag
195 @code{\applymusic} is selected by defining
198 applymusic = #(ly:make-music-function
199 (list procedure? ly:music?)
200 (lambda (parser location func music)
204 A @code{def-music-function} macro is introduced on top of
205 @code{ly:make-music-function} to ease the definition of music
209 applymusic = #(def-music-function (parser location func music)
210 (procedure? ly:music?)
214 Examples of the use of @code{\applymusic} are in the next section.
217 @file{ly/@/music@/-functions@/-init@/.ly}.
219 @node Manipulating music expressions
220 @subsection Manipulating music expressions
222 Music objects and their properties can be accessed and manipulated
223 directly, through the @code{\applymusic} mechanism.
224 The syntax for @code{\applymusic} is
226 \applymusic #@var{func} @var{music}
230 This means that the Scheme function @var{func} is called with
231 @var{music} as its argument. The return value of @var{func} is the
232 result of the entire expression. @var{func} may read and write music
233 properties using the functions @code{ly:music-property} and
234 @code{ly:music-set-property!}.
236 An example is a function that reverses the order of elements in
238 @lilypond[quote,verbatim,raggedright]
239 #(define (rev-music-1 m)
240 (ly:music-set-property! m 'elements
241 (reverse (ly:music-property m 'elements)))
244 \applymusic #rev-music-1 { c'4 d'4 }
247 The use of such a function is very limited. The effect of this
248 function is void when applied to an argument that does not have
249 multiple children. The following function application has no effect
252 \applymusic #rev-music-1 \grace @{ c4 d4 @}
256 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which
257 has no @code{elements}, only a single @code{element}. Every generally
258 applicable function for @code{\applymusic} must -- like music expressions
259 themselves -- be recursive.
261 The following example is such a recursive function: It first extracts
262 the @code{elements} of an expression, reverses them and puts them
263 back. Then it recurses, both on @code{elements} and @code{element}
266 #(define (reverse-music music)
267 (let* ((elements (ly:music-property music 'elements))
268 (child (ly:music-property music 'element))
269 (reversed (reverse elements)))
272 (ly:music-set-property! music 'elements reversed)
275 (if (ly:music? child) (reverse-music child))
276 (map reverse-music reversed)
281 A slightly more elaborate example is in
282 @inputfileref{input/@/test,reverse@/-music@/.ly}.
284 Some of the input syntax is also implemented as recursive music
285 functions. For example, the syntax for polyphony
291 is actually implemented as a recursive function that replaces the
292 above by the internal equivalent of
294 << \context Voice = "1" @{ \voiceOne a @}
295 \context Voice = "2" @{ \voiceTwo b @} >>
298 Other applications of @code{\applymusic} are writing out repeats
299 automatically (@inputfileref{input/@/test,unfold@/-all@/-repeats@/.ly}),
300 saving keystrokes (@inputfileref{input/@/test,music@/-box@/.ly}) and
301 exporting LilyPond input to other formats
302 @c no @inputfileref{} here
303 (eg. @file{input/@/no@/-notation/@/to@/-xml@/.ly}).
307 @file{scm/@/music@/-functions@/.scm}, @file{scm/@/music@/-types@/.scm},
308 @inputfileref{input/@/test,add@/-staccato@/.ly},
309 @inputfileref{input/@/test,unfold@/-all@/-repeats@/.ly}, and
310 @inputfileref{input/@/test,music@/-box@/.ly}.
313 @node Displaying music expressions
314 @subsection Displaying music expressions
316 @cindex internal storage
317 @cindex @code{\displayMusic}
319 When writing a music function, it is often instructive to inspect how
320 a music expression is stored internally. This can be done with the
321 music function @code{\displayMusic}.
325 \displayMusic @{ c'4\f @}
330 @node Using LilyPond syntax inside Scheme
331 @subsection Using LilyPond syntax inside Scheme
333 Creating music expressions in Scheme can be tedious, as they are
334 heavily nested and the resulting Scheme code is large. For some
335 simple tasks, this can be avoided, using common LilyPond syntax inside
336 Scheme, with the dedicated @code{#@{ ... #@}} syntax.
338 The following two expressions give equivalent music expressions:
340 mynotes = @{ \override Stem #'thickness = #4
343 #(define mynotes #@{ \override Stem #'thickness = #4
347 The content of @code{#@{ ... #@}} is enclosed in an implicit @code{@{
348 ... @}} block, which is parsed. The resulting music expression, a
349 @code{SequentialMusic} music object, is then returned and usable in Scheme.
351 Arbitrary Scheme forms, including variables, can be used in @code{#@{ ... #@}}
352 expressions with the @code{$} character (@code{$$} can be used to
353 produce a single @code{$} character). This makes the creation of simple
354 functions straightforward. In the following example, a function
355 setting the TextScript's padding is defined:
357 @lilypond[quote,verbatim,raggedright]
358 #(use-modules (ice-9 optargs))
359 #(define* (textpad padding #:optional once?)
360 (ly:export ; this is necessary for using the expression
361 ; directly inside a block
363 #{ \once \override TextScript #'padding = #$padding #}
364 #{ \override TextScript #'padding = #$padding #})))
368 #(textpad 3.0 #t) % only once
377 Here, the variable @code{padding} is a number; music expression
378 variables may also be used in a similar fashion, as in the following
381 @lilypond[quote,verbatim,raggedright]
382 #(define (with-padding padding)
384 #{ \override TextScript #'padding = #$padding
386 \revert TextScript #'padding #}))
390 \applymusic #(with-padding 3) { c'^"2" c'^"3" }
395 The function created by @code{(with-padding 3)} adds @code{\override} and
396 @code{\revert} statements around the music given as an argument, and returns
397 this new expression. Thus, this example is equivalent to:
402 @{ \override TextScript #'padding = #3
404 \revert TextScript #'padding
410 This function may also be defined as a music function:
412 @lilypond[quote,verbatim,raggedright]
414 #(def-music-function (parser location padding music) (number? ly:music?)
415 #{ \override TextScript #'padding = #$padding
417 \revert TextScript #'padding #})
421 \withPadding #3 { c'^"2" c'^"3"}
427 @node Markup programmer interface
428 @section Markup programmer interface
430 @c Please rewrite the second sentence; I don't understand its meaning. AS
432 Markups are implemented as special Scheme functions. When applied
433 with as arguments an output definition (@code{\layout} or
434 @code{\paper}), and a list of properties and other arguments, produce
438 * Markup construction in Scheme::
439 * How markups work internally ::
440 * Markup command definition::
443 @node Markup construction in Scheme
444 @subsection Markup construction in Scheme
446 @cindex defining markup commands
448 The @code{markup} macro builds markup expressions in Scheme while
449 providing a LilyPond-like syntax. For example,
451 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
452 #:bigger #:line ("foo" "bar" "baz")))
458 \markup \column { \line @{ \bold \italic "hello" \raise #0.4 "world" @}
459 \bigger \line @{ foo bar baz @} }
463 This example exposes the main translation rules between regular
464 LilyPond markup syntax and Scheme markup syntax, which are summed up
468 @multitable @columnfractions .3 .3
469 @item @b{LilyPond} @tab @b{Scheme}
470 @item @code{\markup@{ ... @}} @tab @code{( markup ... )}
471 @item @code{\markup@{ markup1 markup2 ... @}} @tab
472 @code{(markup #:line ( markup1 markup2 ... ))}
473 @item @code{\command} @tab @code{#:command}
474 @item @code{\variable} @tab @code{variable}
475 @item @code{\center-align @{ ... @}} @tab @code{#:center-align ( ... )}
476 @item @code{string} @tab @code{"string"}
477 @item @code{#scheme-arg} @tab @code{scheme-arg}
481 Besides, the whole scheme language is accessible inside the
482 @code{markup} macro: thus, one may use function calls inside
483 @code{markup} in order to manipulate character strings for
484 instance. This proves useful when defining new markup commands (see
485 @ref{Markup command definition}).
489 One can not feed the @code{#:line} (resp @code{#:center},
490 @code{#:column}) command with a variable or the result of a function
494 (markup #:line (fun-that-returns-markups))
498 is invalid. One should use the @code{make-line-markup} (resp.,
499 @code{make-center-markup} or @code{make-column-markup}) function
502 (markup (make-line-markup (fun-that-returns-markups)))
505 @node How markups work internally
506 @subsection How markups work internally
515 @code{\raise} is actually represented by the @code{raise-markup}
516 function. The markup expression is stored as
519 (list raise-markup 0.5 (list simple-markup "foo"))
522 When the markup is converted to printable objects (Stencils), the
523 @code{raise-markup} function is called as
528 @var{list of property alists}
530 @var{the "foo" markup})
533 The @code{raise-markup} function first creates the stencil for the
534 @code{foo} string, and then it raises that Stencil by 0.5 staff space.
535 This is a rather simple example; more complex examples are in the rest
536 of this section, and in @file{scm/@/define@/-markup@/-commands@/.scm}.
538 @node Markup command definition
539 @subsection Markup command definition
541 New markup commands can be defined
542 with the @code{def-markup-command} scheme macro.
544 (def-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
545 (@var{arg1-type?} @var{arg2-type?} ...)
549 The arguments signify
553 @var{i}th command argument
555 a type predicate for the i@var{th} argument
557 the `layout' definition
559 a list of alists, containing all active properties.
562 As a simple example, we show how to add a @code{\smallcaps} command,
563 which selects @TeX{}'s small caps font. Normally, we could select the
564 small caps font as follows:
567 \markup @{ \override #'(font-shape . caps) Text-in-caps @}
570 This selects the caps font by setting the @code{font-shape} property to
571 @code{#'caps} for interpreting @code{Text-in-caps}.
573 To make the above available as @code{\smallcaps} command, we have to
574 define a function using @code{def-markup-command}. The command should
575 take a single argument, of type markup. Therefore, the start of the
576 definition should read
578 (def-markup-command (smallcaps layout props argument) (markup?)
583 What follows is the content of the command: we should interpret
584 the @code{argument} as a markup, i.e.,
587 (interpret-markup layout @dots{} argument)
591 This interpretation should add @code{'(font-shape . caps)} to the active
592 properties, so we substitute the following for the @dots{} in the
596 (cons (list '(font-shape . caps) ) props)
600 The variable @code{props} is a list of alists, and we prepend to it by
601 cons'ing a list with the extra setting.
604 Suppose that we are typesetting a recitative in an opera, and
605 we would like to define a command that will show character names in a
606 custom manner. Names should be printed with small caps and translated a
607 bit to the left and top. We will define a @code{\character} command
608 that takes into account the necessary translation, and uses the newly
609 defined @code{\smallcaps} command:
612 #(def-markup-command (character layout props name) (string?)
613 "Print the character name in small caps, translated to the left and
614 top. Syntax: \\character #\"name\""
615 (interpret-markup layout props
616 (markup "" #:translate (cons -3 1) #:smallcaps name)))
619 There is one complication that needs explanation: texts above and below
620 the staff are moved vertically to be at a certain distance (the
621 @code{padding} property) from the staff and the notes. To make sure
622 that this mechanism does not annihilate the vertical effect of our
623 @code{#:translate}, we add an empty string (@code{""}) before the
624 translated text. Now the @code{""} will be put above the notes, and the
625 @code{name} is moved in relation to that empty string. The net effect is
626 that the text is moved to the upper left.
628 The final result is as follows:
631 c''^\markup \character #"Cleopatra"
632 e'^\markup \character #"Giulio Cesare"
636 @lilypond[quote,raggedright]
637 #(def-markup-command (smallcaps layout props str) (string?)
638 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
639 (interpret-markup layout props
642 (if (= (string-length s) 0)
644 (markup #:large (string-upcase (substring s 0 1))
645 #:translate (cons -0.6 0)
646 #:tiny (string-upcase (substring s 1)))))
647 (string-split str #\Space)))))
649 #(def-markup-command (character layout props name) (string?)
650 "Print the character name in small caps, translated to the left and
651 top. Syntax: \\character #\"name\""
652 (interpret-markup layout props
653 (markup "" #:translate (cons -3 1) #:smallcaps name)))
656 c''^\markup \character #"Cleopatra" c'' c'' c''
657 e'^\markup \character #"Giulio Cesare" e' e' e'
661 We have used the @code{caps} font shape, but suppose that our font
662 does not have a small-caps variant. In that case we have to fake
663 the small caps font by setting a string in upcase with the first
664 letter a little larger:
667 #(def-markup-command (smallcaps layout props str) (string?)
668 "Print the string argument in small caps."
669 (interpret-markup layout props
672 (if (= (string-length s) 0)
674 (markup #:large (string-upcase (substring s 0 1))
675 #:translate (cons -0.6 0)
676 #:tiny (string-upcase (substring s 1)))))
677 (string-split str #\Space)))))
680 The @code{smallcaps} command first splits its string argument into
681 tokens separated by spaces (@code{(string-split str #\Space)}); for
682 each token, a markup is built with the first letter made large and
683 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
684 second markup built with the following letters made tiny and upcased
685 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
686 introduces a space between markups on a line, the second markup is
687 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
688 the markups built for each token are put in a line by
689 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
690 to the @code{interpret-markup} function, with the @code{layout} and
691 @code{props} arguments.
695 @node Contexts for programmers
696 @section Contexts for programmers
700 * Context evaluation::
701 * Running a function on all layout objects::
704 @node Context evaluation
705 @subsection Context evaluation
707 @cindex calling code during interpreting
708 @cindex @code{\applycontext}
710 Contexts can be modified during interpretation with Scheme code. The
713 \applycontext @var{function}
716 @var{function} should be a Scheme function taking a single argument,
717 being the context to apply it to. The following code will print the
718 current bar number on the standard output during the compile:
723 (format #t "\nWe were called in barnumber ~a.\n"
724 (ly:context-property x 'currentBarNumber)))
729 @node Running a function on all layout objects
730 @subsection Running a function on all layout objects
733 @cindex calling code on layout objects
734 @cindex @code{\applyoutput}
737 The most versatile way of tuning an object is @code{\applyoutput}. Its
740 \applyoutput @var{proc}
744 where @var{proc} is a Scheme function, taking three arguments.
746 When interpreted, the function @var{proc} is called for every layout
747 object found in the context, with the following arguments:
749 @item the layout object itself,
750 @item the context where the layout object was created, and
751 @item the context where @code{\applyoutput} is processed.
755 In addition, the cause of the layout object, i.e., the music
756 expression or object that was responsible for creating it, is in the
757 object property @code{cause}. For example, for a note head, this is a
758 @internalsref{NoteHead} event, and for a @internalsref{Stem} object,
759 this is a @internalsref{NoteHead} object.
761 Here is a function to use for @code{\applyoutput}; it blanks
762 note-heads on the center-line:
765 (define (blanker grob grob-origin context)
766 (if (and (memq (ly:grob-property grob 'interfaces)
768 (eq? (ly:grob-property grob 'staff-position) 0))
769 (set! (ly:grob-property grob 'transparent) #t)))