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 = \notes @{ 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 = \notes @{ 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 = \notes @{ c'4 d'4 @}
57 is internally converted to a Scheme definition
59 (define traLaLa @var{Scheme value of ``@code{\notes ... }''})
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 = \notes @{ 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 \score @{ #(ly:export (make-sequential-music newLa)) @}
91 Mixing Scheme and lily identifiers is not possible with @code{--safe}.
93 @node Internal music representation
94 @subsection Internal music representation
96 When a music expression is parsed, it is converted into a set of
97 Scheme music objects. The defining property of a music object is that
98 it takes up time. Time is a rational number that measures the length
99 of a piece of music, in whole notes.
101 A music object has three kinds of types:
104 music name: Each music expression has a name, for example, a note
105 leads to a @internalsref{NoteEvent}, and @code{\simultaneous} leads to
106 a @internalsref{SimultaneousMusic}. A list of all expressions
107 available is in the internals manual, under
108 @hyphenatedinternalsref{Music expressions,Music-expressions}.
111 `type' or interface: Each music name has several `types' or
112 interfaces, for example, a note is an @code{event}, but it is also a
113 @code{note-event}, a @code{rhythmic-event} and a @code{melodic-event}.
115 All classes of music are listed in the internals manual, under
116 @hyphenatedinternalsref{Music classes,Music-classes}.
119 C++ object: Each music object is represented by a C++ object. For
120 technical reasons, different music objects may be represented by
121 different C++ object types. For example, a note is @code{Event}
122 object, while @code{\grace} creates a @code{Grace_music} object.
124 We expect that distinctions between different C++ types will disappear
128 The actual information of a music expression is stored in properties.
129 For example, a @internalsref{NoteEvent} has @code{pitch} and
130 @code{duration} properties that store the pitch and duration of that
131 note. A list of all properties available is in the internals manual,
132 under @internalsref{Music properties}.
134 A compound music expression is a music object that contains other
135 music objects in its properties. A list of objects can be stored in
136 the @code{elements} property of a music object, or a single `child'
137 music object in the @code{element} object. For example,
138 @internalsref{SequentialMusic} has its children in @code{elements},
139 and @internalsref{GraceMusic} has its single argument in
140 @code{element}. The body of a repeat is in @code{element} property of
141 @internalsref{RepeatedMusic}, and the alternatives in @code{elements}.
146 @node Extending music syntax
147 @subsection Extending music syntax
149 The syntax of composite music expressions, like
150 @code{\repeat}, @code{\transpose} and @code{\context}
151 follows the general form of
154 \@code{keyword} @var{non-music-arguments} @var{music-arguments}
157 Such syntax can also be defined as user code. To do this, it is
158 necessary to create a @emph{music function}. This is a specially marked
159 Scheme function. For example, the music function @code{\applymusic} applies
160 a user-defined function to a music expression. Its syntax is
163 \applymusic #@var{func} @var{music}
166 A music function is created with @code{ly:make-music-function},
169 (ly:make-music-function
172 @code{\applymusic} takes a Scheme function and a Music expression as
173 argument. This is encoded in its first argument,
176 (list procedure? ly:music?)
179 The function itself takes another argument, an Input location
180 object. That object is used to provide error messages with file names
181 and line numbers. The definition is the second argument of
182 @code{ly:make-music-function}. The body is function simply calls the
186 (lambda (where func music)
190 The above Scheme code only defines the functionality. The tag
191 @code{\applymusic} is selected by defining
194 applymusic = #(ly:make-music-function
195 (list procedure? ly:music?)
196 (lambda (location func music)
200 A @code{def-music-function} macro is introduced on top of
201 @code{ly:make-music-function} to ease the definition of music
205 applymusic = #(def-music-function (location func music) (procedure? ly:music?)
209 Examples of the use of @code{\applymusic} are in the next section.
212 @file{ly/music-functions-init.ly}.
214 @node Manipulating music expressions
215 @subsection Manipulating music expressions
217 Music objects and their properties can be accessed and manipulated
218 directly, through the @code{\applymusic} mechanism.
219 The syntax for @code{\applymusic} is
221 \applymusic #@var{func} @var{music}
225 This means that the scheme function @var{func} is called with
226 @var{music} as its argument. The return value of @var{func} is the
227 result of the entire expression. @var{func} may read and write music
228 properties using the functions @code{ly:music-property} and
229 @code{ly:music-set-property!}.
231 An example is a function that reverses the order of elements in
233 @lilypond[verbatim,raggedright]
234 #(define (rev-music-1 m)
235 (ly:music-set-property! m 'elements (reverse
236 (ly:music-property m 'elements)))
238 \score { \notes \applymusic #rev-music-1 { c4 d4 } }
241 The use of such a function is very limited. The effect of this
242 function is void when applied to an argument which is does not have
243 multiple children. The following function application has no effect:
246 \applymusic #rev-music-1 \grace @{ c4 d4 @}
250 In this case, @code{\grace} is stored as @internalsref{GraceMusic}, which has no
251 @code{elements}, only a single @code{element}. Every generally
252 applicable function for @code{\applymusic} must -- like music expressions
253 themselves -- be recursive.
255 The following example is such a recursive function: It first extracts
256 the @code{elements} of an expression, reverses them and puts them
257 back. Then it recurses, both on @code{elements} and @code{element}
260 #(define (reverse-music music)
261 (let* ((elements (ly:music-property music 'elements))
262 (child (ly:music-property music 'element))
263 (reversed (reverse elements)))
266 (ly:music-set-property! music 'elements reversed)
269 (if (ly:music? child) (reverse-music child))
270 (map reverse-music reversed)
275 A slightly more elaborate example is in
276 @inputfileref{input/test,reverse-music.ly}.
278 Some of the input syntax is also implemented as recursive music
279 functions. For example, the syntax for polyphony
285 is actually implemented as a recursive function that replaces the
286 above by the internal equivalent of
288 << \context Voice = "1" @{ \voiceOne a @}
289 \context Voice = "2" @{ \voiceTwo b @} >>
292 Other applications of @code{\applymusic} are writing out repeats
293 automatically (@inputfileref{input/test,unfold-all-repeats.ly}),
294 saving keystrokes (@inputfileref{input/test,music-box.ly}) and
296 LilyPond input to other formats (@inputfileref{input/test,to-xml.ly})
300 @file{scm/music-functions.scm}, @file{scm/music-types.scm},
301 @inputfileref{input/test,add-staccato.ly},
302 @inputfileref{input/test,unfold-all-repeats.ly}, and
303 @inputfileref{input/test,music-box.ly}.
306 @node Using LilyPond syntax inside Scheme
307 @subsection Using LilyPond syntax inside Scheme
309 Creating music expressions in scheme can be tedious, as they are
310 heavily nested and the resulting scheme code is large. For some
311 simple tasks, this can be avoided, using LilyPond usual syntax inside
312 scheme, with the dedicated @code{#@{ ... #@}} syntax.
314 The following two expressions give equivalent music expressions:
316 mynotes = @{ \override Stem #'thickness = #4
317 \notes @{ c'8 d' @} @}
319 #(define mynotes #@{ \override Stem #'thickness = #4
320 \notes @{ c'8 d' @} #@})
323 The content of @code{#@{ ... #@}} is enclosed in an implicit @code{@{
324 ... @}} block, which is parsed. The resulting music expression, a
325 @code{SequentialMusic} music object, is then returned and usable in scheme.
327 Arbitrary scheme forms, including variables, can be used in @code{#@{ ... #@}}
328 expressions with the @code{$} character (@code{$$} can be used to
329 produce a single $ character). This makes the creation of simple
330 functions straightforward. In the following example, a function
331 setting the TextScript's padding is defined:
333 @lilypond[verbatim,raggedright]
334 #(use-modules (ice-9 optargs))
335 #(define* (textpad padding #:optional once?)
336 (ly:export ; this is necessary for using the expression
337 ; directly inside a \notes block
339 #{ \once \override TextScript #'padding = #$padding #}
340 #{ \override TextScript #'padding = #$padding #})))
345 #(textpad 3.0 #t) % only once
356 Here, the variable @code{padding} is a number; music expression
357 variables may also be used in a similar fashion, as in the following
360 @lilypond[verbatim,raggedright]
361 #(define (with-padding padding)
363 #{ \override TextScript #'padding = #$padding
365 \revert TextScript #'padding #}))
370 \applymusic #(with-padding 3)
377 The function created by @code{(with-padding 3)} adds @code{\override} and
378 @code{\revert} statements around the music given as an argument, and returns
379 this new expression. Thus, this example is equivalent to:
385 @{ \override TextScript #'padding = #3
387 \revert TextScript #'padding
394 This function may also be defined as a music function:
396 @lilypond[verbatim,raggedright]
397 withPadding = #(def-music-function (location padding music) (number? ly:music?)
398 #{ \override TextScript #'padding = #$padding
400 \revert TextScript #'padding #})
413 @node Markup programmer interface
414 @section Markup programmer interface
418 * Markup construction in scheme::
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 Markup command definition
477 @subsection Markup command definition
479 New markup commands can be defined
480 with the @code{def-markup-command} scheme macro.
482 (def-markup-command (@var{command-name} @var{paper} @var{props} @var{arg1} @var{arg2} ...)
483 (@var{arg1-type?} @var{arg2-type?} ...)
487 The arguments signify
491 @var{i}th command argument
493 a type predicate for the i@var{th} argument
495 the `paper' definition
497 a list of alists, containing all active properties.
500 As a simple example, we show how to add a @code{\smallcaps} command,
501 which selects @TeX{}'s small caps font. Normally, we could select the
502 small caps font as follows:
505 \markup { \override #'(font-shape . caps) Text-in-caps }
508 This selects the caps font by setting the @code{font-shape} property to
509 @code{#'caps} for interpreting @code{Text-in-caps}.
511 To make the above available as @code{\smallcaps} command, we have to
512 define a function using @code{def-markup-command}. The command should
513 take a single argument, of markup type. Therefore, the start of the
514 definition should read
516 (def-markup-command (smallcaps paper props argument) (markup?)
521 What follows is the content of the command: we should interpret
522 the @code{argument} as a markup, i.e.
525 (interpret-markup paper @dots{} argument)
529 This interpretation should add @code{'(font-shape . caps)} to the active
530 properties, so we substitute the following for the @dots{} in the
534 (cons (list '(font-shape . caps) ) props)
538 The variable @code{props} is a list of alists, and we prepend to it by
539 consing a list with the extra setting.
542 Suppose that we are typesetting a recitative in an opera, and
543 we would like to define a command that will show character names in a
544 custom manner. Names should be printed with small caps and translated a
545 bit to the left and top. We will define a @code{\character} command
546 that takes into account the needed translation, and uses the newly
547 defined @code{\smallcaps} command:
550 #(def-markup-command (character paper props name) (string?)
551 "Print the character name in small caps, translated to the left and
552 top. Syntax: \\character #\"name\""
553 (interpret-markup paper props
554 (markup "" #:translate (cons -4 2) #:smallcaps name)))
557 There is one complication that needs explanation: texts above and below
558 the staff are moved vertically to be at a certain distance (the
559 @code{padding} property) from the staff and the notes. To make sure
560 that this mechanism does not annihilate the vertical effect of our
561 @code{#:translate}, we add an empty string (@code{""}) before the
562 translated text. Now the @code{""} will be put above the notes, and the
563 @code{name} is moved in relation to that empty string. The net effect is
564 that the text is moved to the upper left.
566 The final result is as follows:
570 c''^\markup \character #"Cleopatra"
571 e'^\markup \character #"Giulio Cesare"
576 @lilypond[raggedright]
577 #(def-markup-command (smallcaps paper props str) (string?)
578 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
579 (interpret-markup paper props
582 (if (= (string-length s) 0)
584 (markup #:large (string-upcase (substring s 0 1))
585 #:translate (cons -0.6 0)
586 #:tiny (string-upcase (substring s 1)))))
587 (string-split str #\Space)))))
589 #(def-markup-command (character paper props name) (string?)
590 "Print the character name in small caps, translated to the left and
591 top. Syntax: \\character #\"name\""
592 (interpret-markup paper props
593 (markup "" #:translate (cons -4 0) #:smallcaps name)))
597 c''^\markup \character #"Cleopatra"
598 e'^\markup \character #"Giulio Cesare"
603 We have used the @code{caps} font shape, but suppose that our font
604 that does not have a small-caps variant. In that case, we have to fake
605 the small caps font, by setting a string in upcase, with the first
606 letter a little larger:
609 #(def-markup-command (smallcaps paper props str) (string?)
610 "Print the string argument in small caps."
611 (interpret-markup paper props
614 (if (= (string-length s) 0)
616 (markup #:large (string-upcase (substring s 0 1))
617 #:translate (cons -0.6 0)
618 #:tiny (string-upcase (substring s 1)))))
619 (string-split str #\Space)))))
622 The @code{smallcaps} command first splits its string argument into
623 tokens separated by spaces (@code{(string-split str #\Space)}); for
624 each token, a markup is built with the first letter made large and
625 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
626 second markup built with the following letters made tiny and upcased
627 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
628 introduces a space between markups on a line, the second markup is
629 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
630 the markups built for each token are put in a line by
631 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
632 to the @code{interpret-markup} function, with the @code{paper} and
633 @code{props} arguments.
637 @node Contexts for programmers
638 @section Contexts for programmers
642 * Context evaluation::
643 * Running a function on all layout objects::
646 @node Context evaluation
647 @subsection Context evaluation
649 @cindex calling code during interpreting
650 @cindex @code{\applycontext}
652 Contexts can be modified during interpretation with Scheme code. The
655 \applycontext @var{function}
658 @var{function} should be a Scheme function taking a single argument,
659 being the context to apply it to. The following code will print the
660 current bar number on the standard output during the compile:
665 (format #t "\nWe were called in barnumber ~a.\n"
666 (ly:context-property x 'currentBarNumber)))
671 @node Running a function on all layout objects
672 @subsection Running a function on all layout objects
675 @cindex calling code on layout objects
676 @cindex @code{\applyoutput}
679 The most versatile way of tuning an object is @code{\applyoutput}. Its
682 \applyoutput @var{proc}
686 where @var{proc} is a Scheme function, taking three arguments.
688 When interpreted, the function @var{proc} is called for every layout
689 object found in the context, with the following arguments:
691 @item the layout object itself,
692 @item the context where the layout object was created, and
693 @item the context where @code{\applyoutput} is processed.
697 In addition, the cause of the layout object, i.e. the music
698 expression or object that was responsible for creating it, is in the
699 object property @code{cause}. For example, for a note head, this is a
700 @internalsref{NoteHead} event, and for a @internalsref{Stem} object,
701 this is a @internalsref{NoteHead} object.
703 Here is a function to use for @code{\applyoutput}; it blanks
704 note-heads on the center-line:
707 (define (blanker grob grob-origin context)
708 (if (and (memq (ly:grob-property grob 'interfaces)
710 (eq? (ly:grob-property grob 'staff-position) 0))
712 (set! (ly:grob-property grob 'transparent) #t)))