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 * Using LilyPond syntax inside Scheme::
24 @node Input variables and Scheme
25 @subsection Input variables and Scheme
28 The input format supports the notion of variable: in the following
29 example, a music expression is assigned to a variable with the name
32 traLaLa = @{ c'4 d'4 @}
37 There is also a form of scoping: in the following example, the
38 @code{\paper} block also contains a @code{traLaLa} variable, which is
39 independent of the outer @code{\traLaLa}.
41 traLaLa = @{ c'4 d'4 @}
42 \paper @{ traLaLa = 1.0 @}
45 In effect, each input file is a scope, and all @code{\header},
46 @code{\midi} and @code{\paper} blocks are scopes nested inside that
49 Both variables and scoping are implemented in the GUILE module system.
50 An anonymous Scheme module is attached to each scope. An assignment of
53 traLaLa = @{ c'4 d'4 @}
57 is internally converted to a Scheme definition
59 (define traLaLa @var{Scheme value of ``@code{... }''})
62 This means that input variables and Scheme variables may be freely
63 mixed. In the following example, a music fragment is stored in the
64 variable @code{traLaLa}, and duplicated using Scheme. The result is
65 imported in a @code{\score} by means of a second variable
68 traLaLa = @{ c'4 d'4 @}
70 #(define newLa (map ly:music-deep-copy
71 (list traLaLa traLaLa)))
73 (make-sequential-music newLa))
78 In the above example, music expressions can be `exported' from the
79 input to the Scheme interpreter. The opposite is also possible. By
80 wrapping a Scheme value in the function @code{ly:export}, a Scheme
81 value is interpreted as if it were entered in LilyPond syntax. Instead
82 of defining @code{\twice}, the example above could also have been
86 @{ #(ly:export (make-sequential-music newLa)) @}
91 Mixing Scheme and LilyPond identifiers is not possible with the
94 @node Internal music representation
95 @subsection Internal music representation
97 When a music expression is parsed, it is converted into a set of
98 Scheme music objects. The defining property of a music object is that
99 it takes up time. Time is a rational number that measures the length
100 of a piece of music, in whole notes.
102 A music object has three kinds of types:
105 music name: Each music expression has a name, for example, a note
106 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
107 a @internalsref{SimultaneousMusic}. A list of all expressions
108 available is in the internals manual, under
109 @hyphenatedinternalsref{Music expressions,Music-expressions}.
112 `type' or interface: Each music name has several `types' or
113 interfaces, for example, a note is an @code{event}, but it is also a
114 @code{note-event}, a @code{rhythmic-event} and a @code{melodic-event}.
116 All classes of music are listed in the internals manual, under
117 @hyphenatedinternalsref{Music classes,Music-classes}.
120 C++ object: Each music object is represented by a C++ object. For
121 technical reasons, different music objects may be represented by
122 different C++ object types. For example, a note is @code{Event}
123 object, while @code{\grace} creates a @code{Grace_music} object.
125 We expect that distinctions between different C++ types will disappear
129 The actual information of a music expression is stored in properties.
130 For example, a @internalsref{NoteEvent} has @code{pitch} and
131 @code{duration} properties that store the pitch and duration of that
132 note. A list of all properties available is in the internals manual,
133 under @internalsref{Music properties}.
135 A compound music expression is a music object that contains other
136 music objects in its properties. A list of objects can be stored in
137 the @code{elements} property of a music object, or a single `child'
138 music object in the @code{element} object. For example,
139 @internalsref{SequentialMusic} has its children in @code{elements},
140 and @internalsref{GraceMusic} has its single argument in
141 @code{element}. The body of a repeat is stored in the @code{element}
142 property of @internalsref{RepeatedMusic}, and the alternatives in
148 @node Extending music syntax
149 @subsection Extending music syntax
151 The syntax of composite music expressions, like
152 @code{\repeat}, @code{\transpose} and @code{\context}
153 follows the general form of
156 \@code{keyword} @var{non-music-arguments} @var{music-arguments}
159 Such syntax can also be defined as user code. To do this, it is
160 necessary to create a @emph{music function}. This is a specially marked
161 Scheme function. For example, the music function @code{\applymusic} applies
162 a user-defined function to a music expression. Its syntax is
165 \applymusic #@var{func} @var{music}
168 A music function is created with @code{ly:make-music-function},
171 (ly:make-music-function
174 @code{\applymusic} takes a Scheme function and a Music expression as
175 argument. This is encoded in its first argument,
178 (list procedure? ly:music?)
181 The function itself takes another argument, an Input location
182 object. That object is used to provide error messages with file names
183 and line numbers. The definition is the second argument of
184 @code{ly:make-music-function}. The body is function simply calls the
188 (lambda (where func music)
192 The above Scheme code only defines the functionality. The tag
193 @code{\applymusic} is selected by defining
196 applymusic = #(ly:make-music-function
197 (list procedure? ly:music?)
198 (lambda (location func music)
202 A @code{def-music-function} macro is introduced on top of
203 @code{ly:make-music-function} to ease the definition of music
207 applymusic = #(def-music-function (location func music)
208 (procedure? ly:music?)
212 Examples of the use of @code{\applymusic} are in the next section.
215 @file{ly/music-functions-init.ly}.
217 @node Manipulating music expressions
218 @subsection Manipulating music expressions
220 Music objects and their properties can be accessed and manipulated
221 directly, through the @code{\applymusic} mechanism.
222 The syntax for @code{\applymusic} is
224 \applymusic #@var{func} @var{music}
228 This means that the scheme function @var{func} is called with
229 @var{music} as its argument. The return value of @var{func} is the
230 result of the entire expression. @var{func} may read and write music
231 properties using the functions @code{ly:music-property} and
232 @code{ly:music-set-property!}.
234 An example is a function that reverses the order of elements in
236 @lilypond[verbatim,raggedright]
237 #(define (rev-music-1 m)
238 (ly:music-set-property! m 'elements (reverse
239 (ly:music-property m 'elements)))
241 \applymusic #rev-music-1 { c4 d4 }
244 The use of such a function is very limited. The effect of this
245 function is void when applied to an argument which is does not have
246 multiple children. The following function application has no effect
249 \applymusic #rev-music-1 \grace @{ c4 d4 @}
253 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
254 @code{elements}, only a single @code{element}. Every generally
255 applicable function for @code{\applymusic} must---like music expressions
256 themselves -- be recursive.
258 The following example is such a recursive function: It first extracts
259 the @code{elements} of an expression, reverses them and puts them
260 back. Then it recurses, both on @code{elements} and @code{element}
263 #(define (reverse-music music)
264 (let* ((elements (ly:music-property music 'elements))
265 (child (ly:music-property music 'element))
266 (reversed (reverse elements)))
269 (ly:music-set-property! music 'elements reversed)
272 (if (ly:music? child) (reverse-music child))
273 (map reverse-music reversed)
278 A slightly more elaborate example is in
279 @inputfileref{input/test,reverse-music.ly}.
281 Some of the input syntax is also implemented as recursive music
282 functions. For example, the syntax for polyphony
288 is actually implemented as a recursive function that replaces the
289 above by the internal equivalent of
291 << \context Voice = "1" @{ \voiceOne a @}
292 \context Voice = "2" @{ \voiceTwo b @} >>
295 Other applications of @code{\applymusic} are writing out repeats
296 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
297 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
298 exporting LilyPond input to other formats
299 (@inputfileref{input/test,to-xml.ly})
303 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
304 @inputfileref{input/test,add-staccato.ly},
305 @inputfileref{input/test,unfold-all-repeats.ly}, and
306 @inputfileref{input/test,music-box.ly}.
309 @node Using LilyPond syntax inside Scheme
310 @subsection Using LilyPond syntax inside Scheme
312 Creating music expressions in scheme can be tedious, as they are
313 heavily nested and the resulting scheme code is large. For some
314 simple tasks, this can be avoided, using LilyPond usual syntax inside
315 scheme, with the dedicated @code{#@{ ... #@}} syntax.
317 The following two expressions give equivalent music expressions:
319 mynotes = @{ \override Stem #'thickness = #4
322 #(define mynotes #@{ \override Stem #'thickness = #4
326 The content of @code{#@{ ... #@}} is enclosed in an implicit @code{@{
327 ... @}} block, which is parsed. The resulting music expression, a
328 @code{SequentialMusic} music object, is then returned and usable in scheme.
330 Arbitrary scheme forms, including variables, can be used in @code{#@{ ... #@}}
331 expressions with the @code{$} character (@code{$$} can be used to
332 produce a single $ character). This makes the creation of simple
333 functions straightforward. In the following example, a function
334 setting the TextScript's padding is defined:
336 @lilypond[verbatim,raggedright]
337 #(use-modules (ice-9 optargs))
338 #(define* (textpad padding #:optional once?)
339 (ly:export ; this is necessary for using the expression
340 ; directly inside a block
342 #{ \once \override TextScript #'padding = #$padding #}
343 #{ \override TextScript #'padding = #$padding #})))
347 #(textpad 3.0 #t) % only once
357 Here, the variable @code{padding} is a number; music expression
358 variables may also be used in a similar fashion, as in the following
361 @lilypond[verbatim,raggedright]
362 #(define (with-padding padding)
364 #{ \override TextScript #'padding = #$padding
366 \revert TextScript #'padding #}))
370 \applymusic #(with-padding 3)
376 The function created by @code{(with-padding 3)} adds @code{\override} and
377 @code{\revert} statements around the music given as an argument, and returns
378 this new expression. Thus, this example is equivalent to:
383 @{ \override TextScript #'padding = #3
385 \revert TextScript #'padding
391 This function may also be defined as a music function:
393 @lilypond[verbatim,raggedright]
394 withPadding = #(def-music-function (location padding music) (number? ly:music?)
395 #{ \override TextScript #'padding = #$padding
397 \revert TextScript #'padding #})
408 @node Markup programmer interface
409 @section Markup programmer interface
411 Markups implemented as special Scheme functions. When applied with as
412 arguments an output definition (@code{\paper} or @code{\bookpaper}),
413 and a list of properties and other arguments, produce a Stencil
417 * Markup construction in scheme::
418 * How markups work internally ::
419 * Markup command definition::
422 @node Markup construction in scheme
423 @subsection Markup construction in scheme
425 @cindex defining markup commands
427 The @code{markup} macro builds markup expressions in Scheme while
428 providing a LilyPond-like syntax. For example,
430 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
431 #:bigger #:line ("foo" "bar" "baz")))
437 \markup \column < @{ \bold \italic "hello" \raise #0.4 "world" @}
438 \bigger @{ foo bar baz @} >
442 This example exposes the main translation rules between regular
443 LilyPond markup syntax and scheme markup syntax, which are summed up
445 @multitable @columnfractions .5 .5
446 @item @b{LilyPond} @tab @b{Scheme}
447 @item @code{\command} @tab @code{#:command}
448 @item @code{\variable} @tab @code{variable}
449 @item @code{@{ ... @}} @tab @code{#:line ( ... )}
450 @item @code{\center-align < ... >} @tab @code{#:center ( ... )}
451 @item @code{string} @tab @code{"string"}
452 @item @code{#scheme-arg} @tab @code{scheme-arg}
455 Besides, the whole scheme language is accessible inside the
456 @code{markup} macro: thus, one may use function calls inside
457 @code{markup} in order to manipulate character strings for
458 instance. This proves useful when defining new markup commands (see
459 @ref{Markup command definition}).
463 One can not feed the @code{#:line} (resp @code{#:center},
464 @code{#:column}) command with a variable or the result of a function
467 (markup #:line (fun-that-returns-markups))
469 is illegal. One should use the @code{make-line-markup} (resp
470 @code{make-center-markup}, @code{make-column-markup}) function
473 (markup (make-line-markup (fun-that-returns-markups)))
476 @node How markups work internally
477 @subsection How markups work internally
486 @code{\raise} is actually represented by the @code{raise-markup}
487 function. The markup expression is stored as
490 (list raise-markup 0.5 (list simple-markup 'latin1 "foo"))
494 In this case, @code{latin1} is the input encoding, which is set with
495 the @code{\encoding} command.
497 When the markup is converted to printable objects (Stencils), the
498 raise markup is called as
503 @var{list of property alists}
505 @var{the "foo" markup})
508 The @code{raise-markup} first creates the stencil for the @code{foo}
509 string, and then it raises that Stencil by 0.5 staff space. This is a
510 rather simple example; more complex examples are in the rest of this
511 section, and in @file{scm/define-markup-commands.scm}.
513 @node Markup command definition
514 @subsection Markup command definition
516 New markup commands can be defined
517 with the @code{def-markup-command} scheme macro.
519 (def-markup-command (@var{command-name} @var{paper} @var{props} @var{arg1} @var{arg2} ...)
520 (@var{arg1-type?} @var{arg2-type?} ...)
524 The arguments signify
528 @var{i}th command argument
530 a type predicate for the i@var{th} argument
532 the `paper' definition
534 a list of alists, containing all active properties.
537 As a simple example, we show how to add a @code{\smallcaps} command,
538 which selects @TeX{}'s small caps font. Normally, we could select the
539 small caps font as follows:
542 \markup { \override #'(font-shape . caps) Text-in-caps }
545 This selects the caps font by setting the @code{font-shape} property to
546 @code{#'caps} for interpreting @code{Text-in-caps}.
548 To make the above available as @code{\smallcaps} command, we have to
549 define a function using @code{def-markup-command}. The command should
550 take a single argument, of markup type. Therefore, the start of the
551 definition should read
553 (def-markup-command (smallcaps paper props argument) (markup?)
558 What follows is the content of the command: we should interpret
559 the @code{argument} as a markup, i.e.
562 (interpret-markup paper @dots{} argument)
566 This interpretation should add @code{'(font-shape . caps)} to the active
567 properties, so we substitute the following for the @dots{} in the
571 (cons (list '(font-shape . caps) ) props)
575 The variable @code{props} is a list of alists, and we prepend to it by
576 consing a list with the extra setting.
579 Suppose that we are typesetting a recitative in an opera, and
580 we would like to define a command that will show character names in a
581 custom manner. Names should be printed with small caps and translated a
582 bit to the left and top. We will define a @code{\character} command
583 that takes into account the needed translation, and uses the newly
584 defined @code{\smallcaps} command:
587 #(def-markup-command (character paper props name) (string?)
588 "Print the character name in small caps, translated to the left and
589 top. Syntax: \\character #\"name\""
590 (interpret-markup paper props
591 (markup "" #:translate (cons -4 2) #:smallcaps name)))
594 There is one complication that needs explanation: texts above and below
595 the staff are moved vertically to be at a certain distance (the
596 @code{padding} property) from the staff and the notes. To make sure
597 that this mechanism does not annihilate the vertical effect of our
598 @code{#:translate}, we add an empty string (@code{""}) before the
599 translated text. Now the @code{""} will be put above the notes, and the
600 @code{name} is moved in relation to that empty string. The net effect is
601 that the text is moved to the upper left.
603 The final result is as follows:
606 c''^\markup \character #"Cleopatra"
607 e'^\markup \character #"Giulio Cesare"
611 @lilypond[raggedright]
612 #(def-markup-command (smallcaps paper props str) (string?)
613 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
614 (interpret-markup paper props
617 (if (= (string-length s) 0)
619 (markup #:large (string-upcase (substring s 0 1))
620 #:translate (cons -0.6 0)
621 #:tiny (string-upcase (substring s 1)))))
622 (string-split str #\Space)))))
624 #(def-markup-command (character paper props name) (string?)
625 "Print the character name in small caps, translated to the left and
626 top. Syntax: \\character #\"name\""
627 (interpret-markup paper props
628 (markup "" #:translate (cons -4 0) #:smallcaps name)))
631 c''^\markup \character #"Cleopatra"
632 e'^\markup \character #"Giulio Cesare"
636 We have used the @code{caps} font shape, but suppose that our font
637 that does not have a small-caps variant. In that case, we have to fake
638 the small caps font, by setting a string in upcase, with the first
639 letter a little larger:
642 #(def-markup-command (smallcaps paper props str) (string?)
643 "Print the string argument in small caps."
644 (interpret-markup paper props
647 (if (= (string-length s) 0)
649 (markup #:large (string-upcase (substring s 0 1))
650 #:translate (cons -0.6 0)
651 #:tiny (string-upcase (substring s 1)))))
652 (string-split str #\Space)))))
655 The @code{smallcaps} command first splits its string argument into
656 tokens separated by spaces (@code{(string-split str #\Space)}); for
657 each token, a markup is built with the first letter made large and
658 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
659 second markup built with the following letters made tiny and upcased
660 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
661 introduces a space between markups on a line, the second markup is
662 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
663 the markups built for each token are put in a line by
664 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
665 to the @code{interpret-markup} function, with the @code{paper} and
666 @code{props} arguments.
670 @node Contexts for programmers
671 @section Contexts for programmers
675 * Context evaluation::
676 * Running a function on all layout objects::
679 @node Context evaluation
680 @subsection Context evaluation
682 @cindex calling code during interpreting
683 @cindex @code{\applycontext}
685 Contexts can be modified during interpretation with Scheme code. The
688 \applycontext @var{function}
691 @var{function} should be a Scheme function taking a single argument,
692 being the context to apply it to. The following code will print the
693 current bar number on the standard output during the compile:
698 (format #t "\nWe were called in barnumber ~a.\n"
699 (ly:context-property x 'currentBarNumber)))
704 @node Running a function on all layout objects
705 @subsection Running a function on all layout objects
708 @cindex calling code on layout objects
709 @cindex @code{\applyoutput}
712 The most versatile way of tuning an object is @code{\applyoutput}. Its
715 \applyoutput @var{proc}
719 where @var{proc} is a Scheme function, taking three arguments.
721 When interpreted, the function @var{proc} is called for every layout
722 object found in the context, with the following arguments:
724 @item the layout object itself,
725 @item the context where the layout object was created, and
726 @item the context where @code{\applyoutput} is processed.
730 In addition, the cause of the layout object, i.e. the music
731 expression or object that was responsible for creating it, is in the
732 object property @code{cause}. For example, for a note head, this is a
733 @internalsref{NoteHead} event, and for a @internalsref{Stem} object,
734 this is a @internalsref{NoteHead} object.
736 Here is a function to use for @code{\applyoutput}; it blanks
737 note-heads on the center-line:
740 (define (blanker grob grob-origin context)
741 (if (and (memq (ly:grob-property grob 'interfaces)
743 (eq? (ly:grob-property grob 'staff-position) 0))
745 (set! (ly:grob-property grob 'transparent) #t)))