1 @c -*- coding: utf-8; mode: texinfo; -*-
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. See TRANSLATION for details.
12 @node Interfaces for programmers
13 @chapter Interfaces for programmers
15 Advanced tweaks may be performed by using Scheme. If you are
16 not familiar with Scheme, you may wish to read our
17 @rlearning{Scheme tutorial}.
21 * Programmer interfaces::
22 * Building complicated functions::
23 * Markup programmer interface::
24 * Contexts for programmers::
25 * Scheme procedures as properties::
26 * Using Scheme code instead of \tweak::
32 @section Music functions
34 This section discusses how to create music functions within LilyPond.
37 * Overview of music functions::
38 * Simple substitution functions::
39 * Paired substitution functions::
40 * Mathematics in functions::
42 * Functions without arguments::
45 @node Overview of music functions
46 @subsection Overview of music functions
48 Making a function which substitutes a variable into LilyPond
49 code is easy. The general form of these functions is
53 #(define-music-function (parser location @var{var1} @var{var2}...@var{vari}... )
54 (@var{var1-type?} @var{var2-type?}...@var{vari-type?}...)
63 @multitable @columnfractions .33 .66
64 @item @var{vari} @tab @var{i}th variable
65 @item @var{vari-type?} @tab type of @var{i}th variable
66 @item @var{...music...} @tab normal LilyPond input, using
67 variables as @code{#$var1}, etc.
70 There following input types may be used as variables
71 in a music function. This list is not exhaustive; see
72 other documentation specifically about Scheme for more
75 @multitable @columnfractions .33 .66
76 @headitem Input type @tab @var{vari-type?} notation
77 @item Integer @tab @code{integer?}
78 @item Float (decimal number) @tab @code{number?}
79 @item Text string @tab @code{string?}
80 @item Markup @tab @code{markup?}
81 @item Music expression @tab @code{ly:music?}
82 @item A pair of variables @tab @code{pair?}
85 The @code{parser} and @code{location} arguments are mandatory,
86 and are used in some advanced situations. The @code{parser}
87 argument is used to gain access to the value of another LilyPond
88 variable. The @code{location} argument
89 is used to set the @q{origin} of the music expression that is built
90 by the music function, so that in case of a syntax error LilyPond
91 can tell the user an appropriate place to look in the input file.
94 @node Simple substitution functions
95 @subsection Simple substitution functions
97 Here is a simple example,
99 @lilypond[quote,verbatim,ragged-right]
100 padText = #(define-music-function (parser location padding) (number?)
102 \once \override TextScript #'padding = #$padding
110 c4^"piu mosso" fis a g
114 Music expressions may be substituted as well,
116 @lilypond[quote,verbatim,ragged-right]
117 custosNote = #(define-music-function (parser location note)
120 \once \override Voice.NoteHead #'stencil =
121 #ly:text-interface::print
122 \once \override Voice.NoteHead #'text =
123 \markup \musicglyph #"custodes.mensural.u0"
124 \once \override Voice.Stem #'stencil = ##f
128 { c' d' e' f' \custosNote g' }
131 Multiple variables may be used,
133 @lilypond[quote,verbatim,ragged-right]
134 tempoPadded = #(define-music-function (parser location padding tempotext)
137 \once \override Score.MetronomeMark #'padding = $padding
138 \tempo \markup { \bold $tempotext }
142 \tempo \markup { "Low tempo" }
144 \tempoPadded #4.0 #"High tempo"
150 @node Paired substitution functions
151 @subsection Paired substitution functions
153 Some @code{\override} commands require a pair of numbers
154 (called a @code{cons cell} in Scheme). To pass these numbers
155 into a function, either use a @code{pair?} variable, or
156 insert the @code{cons} into the music function.
161 #(define-music-function (parser location beg-end)
164 \once \override Beam #'positions = #$beg-end
168 \manualBeam #'(3 . 6) c8 d e f
176 @lilypond[quote,verbatim,ragged-right]
178 #(define-music-function (parser location beg end)
181 \once \override Beam #'positions = #(cons $beg $end)
185 \manualBeam #3 #6 c8 d e f
190 @node Mathematics in functions
191 @subsection Mathematics in functions
193 Music functions can involve Scheme programming in
194 addition to simple substitution,
196 @lilypond[quote,verbatim,ragged-right]
197 AltOn = #(define-music-function (parser location mag) (number?)
198 #{ \override Stem #'length = #$(* 7.0 mag)
199 \override NoteHead #'font-size =
200 #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag))) #})
203 \revert Stem #'length
204 \revert NoteHead #'font-size
207 { c'2 \AltOn #0.5 c'4 c'
208 \AltOn #1.5 c' c' \AltOff c'2 }
212 This example may be rewritten to pass in music expressions,
214 @lilypond[quote,verbatim,ragged-right]
215 withAlt = #(define-music-function (parser location mag music) (number? ly:music?)
216 #{ \override Stem #'length = #$(* 7.0 mag)
217 \override NoteHead #'font-size =
218 #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
220 \revert Stem #'length
221 \revert NoteHead #'font-size #})
223 { c'2 \withAlt #0.5 {c'4 c'}
224 \withAlt #1.5 {c' c'} c'2 }
228 @subsection Void functions
230 A music function must return a music expression, but sometimes we
231 may want to have a function which does not involve music (such as
232 turning off Point and Click). To do this, we return a @code{void}
236 that is returned is the @code{(make-music ...)}. With the
237 @code{'void} property set to @code{#t}, the parser is told to
238 actually disregard this returned music
239 expression. Thus the important part of the void music function is the
240 processing done by the function, not the music expression that is
245 #(define-music-function (parser location) ()
246 (ly:set-option 'point-and-click #f)
247 (make-music 'SequentialMusic 'void #t))
249 \noPointAndClick % disable point and click
253 @node Functions without arguments
254 @subsection Functions without arguments
256 In most cases a function without arguments should be written
260 dolce = \markup@{ \italic \bold dolce @}
263 However, in rare cases it may be useful to create a music function
268 #(define-music-function (parser location) ()
269 (if (eq? #t (ly:get-option 'display-bar-numbers))
270 #@{ \once \override Score.BarNumber #'break-visibility = ##f #@}
274 To actually display bar numbers where this function is called,
275 invoke @command{lilypond} with
278 lilypond -d display-bar-numbers FILENAME.ly
282 @node Programmer interfaces
283 @section Programmer interfaces
285 This section contains information about mixing LilyPond
289 * Input variables and Scheme::
290 * Internal music representation::
294 @node Input variables and Scheme
295 @subsection Input variables and Scheme
297 The input format supports the notion of variables: in the following
298 example, a music expression is assigned to a variable with the name
302 traLaLa = @{ c'4 d'4 @}
307 There is also a form of scoping: in the following example, the
308 @code{\layout} block also contains a @code{traLaLa} variable, which is
309 independent of the outer @code{\traLaLa}.
311 traLaLa = @{ c'4 d'4 @}
312 \layout @{ traLaLa = 1.0 @}
315 In effect, each input file is a scope, and all @code{\header},
316 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
319 Both variables and scoping are implemented in the GUILE module system.
320 An anonymous Scheme module is attached to each scope. An assignment of
323 traLaLa = @{ c'4 d'4 @}
327 is internally converted to a Scheme definition
329 (define traLaLa @var{Scheme value of `@code{... }'})
332 This means that input variables and Scheme variables may be freely
333 mixed. In the following example, a music fragment is stored in the
334 variable @code{traLaLa}, and duplicated using Scheme. The result is
335 imported in a @code{\score} block by means of a second variable
339 traLaLa = { c'4 d'4 }
341 %% dummy action to deal with parser lookahead
342 #(display "this needs to be here, sorry!")
344 #(define newLa (map ly:music-deep-copy
345 (list traLaLa traLaLa)))
347 (make-sequential-music newLa))
352 @c Due to parser lookahead
354 In this example, the assignment happens after parser has verified that
355 nothing interesting happens after @code{traLaLa = @{ ... @}}. Without
356 the dummy statement in the above example, the @code{newLa} definition
357 is executed before @code{traLaLa} is defined, leading to a syntax
360 The above example shows how to @q{export} music expressions from the
361 input to the Scheme interpreter. The opposite is also possible. By
362 wrapping a Scheme value in the function @code{ly:export}, a Scheme
363 value is interpreted as if it were entered in LilyPond syntax.
364 Instead of defining @code{\twice}, the example above could also have
369 @{ #(ly:export (make-sequential-music (list newLa))) @}
372 Scheme code is evaluated as soon as the parser encounters it. To
373 define some Scheme code in a macro (to be called later), use
374 @ref{Void functions}, or
378 (ly:set-option 'point-and-click #f))
388 Mixing Scheme and LilyPond variables is not possible with the
389 @code{--safe} option.
392 @node Internal music representation
393 @subsection Internal music representation
395 When a music expression is parsed, it is converted into a set of
396 Scheme music objects. The defining property of a music object is that
397 it takes up time. Time is a rational number that measures the length
398 of a piece of music in whole notes.
400 A music object has three kinds of types:
403 music name: Each music expression has a name. For example, a note
404 leads to a @rinternals{NoteEvent}, and @code{\simultaneous} leads to
405 a @rinternals{SimultaneousMusic}. A list of all expressions
406 available is in the Internals Reference manual, under
407 @rinternals{Music expressions}.
410 @q{type} or interface: Each music name has several @q{types} or
411 interfaces, for example, a note is an @code{event}, but it is also a
412 @code{note-event}, a @code{rhythmic-event}, and a
413 @code{melodic-event}. All classes of music are listed in the
414 Internals Reference, under
415 @rinternals{Music classes}.
418 C++ object: Each music object is represented by an object of the C++
422 The actual information of a music expression is stored in properties.
423 For example, a @rinternals{NoteEvent} has @code{pitch} and
424 @code{duration} properties that store the pitch and duration of that
425 note. A list of all properties available is in the internals manual,
426 under @rinternals{Music properties}.
428 A compound music expression is a music object that contains other
429 music objects in its properties. A list of objects can be stored in
430 the @code{elements} property of a music object, or a single @q{child}
431 music object in the @code{element} property. For example,
432 @rinternals{SequentialMusic} has its children in @code{elements},
433 and @rinternals{GraceMusic} has its single argument in
434 @code{element}. The body of a repeat is stored in the @code{element}
435 property of @rinternals{RepeatedMusic}, and the alternatives in
440 @node Building complicated functions
441 @section Building complicated functions
443 This section explains how to gather the information necessary
444 to create complicated music functions.
447 * Displaying music expressions::
449 * Doubling a note with slurs (example)::
450 * Adding articulation to notes (example)::
454 @node Displaying music expressions
455 @subsection Displaying music expressions
457 @cindex internal storage
458 @cindex displaying music expressions
459 @cindex internal representation, displaying
461 @funindex \displayMusic
463 When writing a music function it is often instructive to inspect how
464 a music expression is stored internally. This can be done with the
465 music function @code{\displayMusic}
469 \displayMusic @{ c'4\f @}
486 (ly:make-duration 2 0 1 1)
488 (ly:make-pitch 0 0 0))
490 'AbsoluteDynamicEvent
495 By default, LilyPond will print these messages to the console along
496 with all the other messages. To split up these messages and save
497 the results of @code{\display@{STUFF@}}, redirect the output to
501 lilypond file.ly >display.txt
504 With a bit of reformatting, the above information is
508 (make-music 'SequentialMusic
509 'elements (list (make-music 'EventChord
510 'elements (list (make-music 'NoteEvent
511 'duration (ly:make-duration 2 0 1 1)
512 'pitch (ly:make-pitch 0 0 0))
513 (make-music 'AbsoluteDynamicEvent
517 A @code{@{ ... @}} music sequence has the name @code{SequentialMusic},
518 and its inner expressions are stored as a list in its @code{'elements}
519 property. A note is represented as an @code{EventChord} expression,
520 containing a @code{NoteEvent} object (storing the duration and
521 pitch properties) and any extra information (in this case, an
522 @code{AbsoluteDynamicEvent} with a @code{"f"} text property.
525 @node Music properties
526 @subsection Music properties
528 The @code{NoteEvent} object is the first object of the
529 @code{'elements} property of @code{someNote}.
533 \displayMusic \someNote
541 (ly:make-duration 2 0 1 1)
543 (ly:make-pitch 0 0 0))))
546 The @code{display-scheme-music} function is the function used by
547 @code{\displayMusic} to display the Scheme representation of a music
551 #(display-scheme-music (first (ly:music-property someNote 'elements)))
556 (ly:make-duration 2 0 1 1)
558 (ly:make-pitch 0 0 0))
561 Then the note pitch is accessed through the @code{'pitch} property
562 of the @code{NoteEvent} object,
565 #(display-scheme-music
566 (ly:music-property (first (ly:music-property someNote 'elements))
569 (ly:make-pitch 0 0 0)
572 The note pitch can be changed by setting this 'pitch property,
574 @funindex \displayLilyMusic
577 #(set! (ly:music-property (first (ly:music-property someNote 'elements))
579 (ly:make-pitch 0 1 0)) ;; set the pitch to d'.
580 \displayLilyMusic \someNote
586 @node Doubling a note with slurs (example)
587 @subsection Doubling a note with slurs (example)
589 Suppose we want to create a function which translates
590 input like @code{a} into @code{a( a)}. We begin
591 by examining the internal representation of the music
592 we want to end up with.
595 \displayMusic@{ a'( a') @}
606 (ly:make-duration 2 0 1 1)
608 (ly:make-pitch 0 5 0))
619 (ly:make-duration 2 0 1 1)
621 (ly:make-pitch 0 5 0))
628 The bad news is that the @code{SlurEvent} expressions
629 must be added @q{inside} the note (or more precisely,
630 inside the @code{EventChord} expression).
632 Now we examine the input,
644 (ly:make-duration 2 0 1 1)
646 (ly:make-pitch 0 5 0))))))
649 So in our function, we need to clone this expression (so that we
650 have two notes to build the sequence), add @code{SlurEvents} to the
651 @code{'elements} property of each one, and finally make a
652 @code{SequentialMusic} with the two @code{EventChords}.
655 doubleSlur = #(define-music-function (parser location note) (ly:music?)
656 "Return: @{ note ( note ) @}.
657 `note' is supposed to be an EventChord."
658 (let ((note2 (ly:music-deep-copy note)))
659 (set! (ly:music-property note 'elements)
660 (cons (make-music 'SlurEvent 'span-direction -1)
661 (ly:music-property note 'elements)))
662 (set! (ly:music-property note2 'elements)
663 (cons (make-music 'SlurEvent 'span-direction 1)
664 (ly:music-property note2 'elements)))
665 (make-music 'SequentialMusic 'elements (list note note2))))
669 @node Adding articulation to notes (example)
670 @subsection Adding articulation to notes (example)
672 The easy way to add articulation to notes is to merge two music
673 expressions into one context, as explained in
674 @ref{Creating contexts}. However, suppose that we want to write
675 a music function which does this.
677 A @code{$variable} inside the @code{#@{...#@}} notation is like
678 using a regular @code{\variable} in classical LilyPond
679 notation. We know that
686 will not work in LilyPond. We could avoid this problem by attaching
687 the articulation to a fake note,
690 @{ << \music s1*0-.-> @}
694 but for the sake of this example, we will learn how to do this in
695 Scheme. We begin by examining our input and desired output,
707 (ly:make-duration 2 0 1 1)
709 (ly:make-pitch -1 0 0))))
720 (ly:make-duration 2 0 1 1)
722 (ly:make-pitch -1 0 0))
729 We see that a note (@code{c4}) is represented as an @code{EventChord}
730 expression, with a @code{NoteEvent} expression in its elements list. To
731 add a marcato articulation, an @code{ArticulationEvent} expression must
732 be added to the elements property of the @code{EventChord}
735 To build this function, we begin with
738 (define (add-marcato event-chord)
739 "Add a marcato ArticulationEvent to the elements of `event-chord',
740 which is supposed to be an EventChord expression."
741 (let ((result-event-chord (ly:music-deep-copy event-chord)))
742 (set! (ly:music-property result-event-chord 'elements)
743 (cons (make-music 'ArticulationEvent
744 'articulation-type "marcato")
745 (ly:music-property result-event-chord 'elements)))
749 The first line is the way to define a function in Scheme: the function
750 name is @code{add-marcato}, and has one variable called
751 @code{event-chord}. In Scheme, the type of variable is often clear
752 from its name. (this is good practice in other programming languages,
760 is a description of what the function does. This is not strictly
761 necessary, but just like clear variable names, it is good practice.
764 (let ((result-event-chord (ly:music-deep-copy event-chord)))
767 @code{let} is used to declare local variables. Here we use one local
768 variable, named @code{result-event-chord}, to which we give the value
769 @code{(ly:music-deep-copy event-chord)}. @code{ly:music-deep-copy} is
770 a function specific to LilyPond, like all functions prefixed by
771 @code{ly:}. It is use to make a copy of a music
772 expression. Here we copy @code{event-chord} (the parameter of the
773 function). Recall that our purpose is to add a marcato to an
774 @code{EventChord} expression. It is better to not modify the
775 @code{EventChord} which was given as an argument, because it may be
778 Now we have a @code{result-event-chord}, which is a
779 @code{NoteEventChord} expression and is a copy of @code{event-chord}. We
780 add the marcato to its elements list property.
783 (set! place new-value)
786 Here, what we want to set (the @q{place}) is the @q{elements} property of
787 @code{result-event-chord} expression.
790 (ly:music-property result-event-chord 'elements)
793 @code{ly:music-property} is the function used to access music properties
794 (the @code{'elements}, @code{'duration}, @code{'pitch}, etc, that we
795 see in the @code{\displayMusic} output above). The new value is the
796 former elements property, with an extra item: the
797 @code{ArticulationEvent} expression, which we copy from the
798 @code{\displayMusic} output,
801 (cons (make-music 'ArticulationEvent
802 'articulation-type "marcato")
803 (ly:music-property result-event-chord 'elements))
806 @code{cons} is used to add an element to a list without modifying the
807 original list. This is what we
808 want: the same list as before, plus the new @code{ArticulationEvent}
809 expression. The order inside the elements property is not important here.
811 Finally, once we have added the marcato articulation to its @code{elements}
812 property, we can return @code{result-event-chord}, hence the last line of
815 Now we transform the @code{add-marcato} function into a music
819 addMarcato = #(define-music-function (parser location event-chord)
821 "Add a marcato ArticulationEvent to the elements of `event-chord',
822 which is supposed to be an EventChord expression."
823 (let ((result-event-chord (ly:music-deep-copy event-chord)))
824 (set! (ly:music-property result-event-chord 'elements)
825 (cons (make-music 'ArticulationEvent
826 'articulation-type "marcato")
827 (ly:music-property result-event-chord 'elements)))
831 We may verify that this music function works correctly,
834 \displayMusic \addMarcato c4
838 @node Markup programmer interface
839 @section Markup programmer interface
841 Markups are implemented as special Scheme functions which produce a
842 Stencil object given a number of arguments.
845 * Markup construction in Scheme::
846 * How markups work internally::
847 * New markup command definition::
848 * New markup list command definition::
852 @node Markup construction in Scheme
853 @subsection Markup construction in Scheme
855 @cindex defining markup commands
857 The @code{markup} macro builds markup expressions in Scheme while
858 providing a LilyPond-like syntax. For example,
860 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
861 #:larger #:line ("foo" "bar" "baz")))
867 \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
868 \larger \line @{ foo bar baz @} @}
872 This example demonstrates the main translation rules between regular
873 LilyPond markup syntax and Scheme markup syntax.
876 @multitable @columnfractions .3 .3
877 @item @b{LilyPond} @tab @b{Scheme}
878 @item @code{\markup markup1} @tab @code{(markup markup1)}
879 @item @code{\markup @{ markup1 markup2 ... @}} @tab
880 @code{(markup markup1 markup2 ... )}
881 @item @code{\command} @tab @code{#:command}
882 @item @code{\variable} @tab @code{variable}
883 @item @code{\center-column @{ ... @}} @tab @code{#:center-column ( ... )}
884 @item @code{string} @tab @code{"string"}
885 @item @code{#scheme-arg} @tab @code{scheme-arg}
889 The whole Scheme language is accessible inside the
890 @code{markup} macro. For example, You may use function calls inside
891 @code{markup} in order to manipulate character strings. This is
892 useful when defining new markup commands (see
893 @ref{New markup command definition}).
898 The markup-list argument of commands such as @code{#:line},
899 @code{#:center}, and @code{#:column} cannot be a variable or
900 the result of a function call.
903 (markup #:line (function-that-returns-markups))
907 is invalid. One should use the @code{make-line-markup},
908 @code{make-center-markup}, or @code{make-column-markup} functions
912 (markup (make-line-markup (function-that-returns-markups)))
916 @node How markups work internally
917 @subsection How markups work internally
922 \raise #0.5 "text example"
926 @code{\raise} is actually represented by the @code{raise-markup}
927 function. The markup expression is stored as
930 (list raise-markup 0.5 (list simple-markup "text example"))
933 When the markup is converted to printable objects (Stencils), the
934 @code{raise-markup} function is called as
939 @var{list of property alists}
941 @var{the "text example" markup})
944 The @code{raise-markup} function first creates the stencil for the
945 @code{text example} string, and then it raises that Stencil by 0.5
946 staff space. This is a rather simple example; more complex examples
948 of this section, and in @file{scm/@/define@/-markup@/-commands@/.scm}.
951 @node New markup command definition
952 @subsection New markup command definition
954 New markup commands can be defined
955 with the @code{define-markup-command} Scheme macro.
958 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
959 (@var{arg1-type?} @var{arg2-type?} ...)
967 @var{i}th command argument
969 a type predicate for the i@var{th} argument
971 the @q{layout} definition
973 a list of alists, containing all active properties.
976 As a simple example, we show how to add a @code{\smallcaps} command,
977 which selects a small caps font. Normally we could select the
981 \markup @{ \override #'(font-shape . caps) Text-in-caps @}
985 This selects the caps font by setting the @code{font-shape} property to
986 @code{#'caps} for interpreting @code{Text-in-caps}.
988 To make the above available as @code{\smallcaps} command, we must
989 define a function using @code{define-markup-command}. The command should
990 take a single argument of type @code{markup}. Therefore the start of the
991 definition should read
994 (define-markup-command (smallcaps layout props argument) (markup?)
999 What follows is the content of the command: we should interpret
1000 the @code{argument} as a markup, i.e.,
1003 (interpret-markup layout @dots{} argument)
1007 This interpretation should add @code{'(font-shape . caps)} to the active
1008 properties, so we substitute the following for the @dots{} in the
1012 (cons (list '(font-shape . caps) ) props)
1016 The variable @code{props} is a list of alists, and we prepend to it by
1017 cons'ing a list with the extra setting.
1020 Suppose that we are typesetting a recitative in an opera and
1021 we would like to define a command that will show character names in a
1022 custom manner. Names should be printed with small caps and moved a
1023 bit to the left and top. We will define a @code{\character} command
1024 which takes into account the necessary translation and uses the newly
1025 defined @code{\smallcaps} command:
1028 #(define-markup-command (character layout props name) (string?)
1029 "Print the character name in small caps, translated to the left and
1030 top. Syntax: \\character #\"name\""
1031 (interpret-markup layout props
1032 (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
1035 There is one complication that needs explanation: texts above and below
1036 the staff are moved vertically to be at a certain distance (the
1037 @code{padding} property) from the staff and the notes. To make sure
1038 that this mechanism does not annihilate the vertical effect of our
1039 @code{#:translate}, we add an empty string (@code{#:hspace 0}) before the
1040 translated text. Now the @code{#:hspace 0} will be put above the notes,
1042 @code{name} is moved in relation to that empty string. The net effect is
1043 that the text is moved to the upper left.
1045 The final result is as follows:
1049 c''^\markup \character #"Cleopatra"
1050 e'^\markup \character #"Giulio Cesare"
1054 @lilypond[quote,ragged-right]
1055 #(define-markup-command (smallcaps layout props str) (string?)
1056 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
1057 (interpret-markup layout props
1060 (if (= (string-length s) 0)
1062 (markup #:large (string-upcase (substring s 0 1))
1063 #:translate (cons -0.6 0)
1064 #:tiny (string-upcase (substring s 1)))))
1065 (string-split str #\Space)))))
1067 #(define-markup-command (character layout props name) (string?)
1068 "Print the character name in small caps, translated to the left and
1069 top. Syntax: \\character #\"name\""
1070 (interpret-markup layout props
1071 (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
1074 c''^\markup \character #"Cleopatra" c'' c'' c''
1075 e'^\markup \character #"Giulio Cesare" e' e' e'
1079 We have used the @code{caps} font shape, but suppose that our font
1080 does not have a small-caps variant. In that case we have to fake
1081 the small caps font by setting a string in upcase with the first
1082 letter a little larger:
1085 #(define-markup-command (smallcaps layout props str) (string?)
1086 "Print the string argument in small caps."
1087 (interpret-markup layout props
1090 (if (= (string-length s) 0)
1092 (markup #:large (string-upcase (substring s 0 1))
1093 #:translate (cons -0.6 0)
1094 #:tiny (string-upcase (substring s 1)))))
1095 (string-split str #\Space)))))
1098 The @code{smallcaps} command first splits its string argument into
1099 tokens separated by spaces (@code{(string-split str #\Space)}); for
1100 each token, a markup is built with the first letter made large and
1101 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
1102 second markup built with the following letters made tiny and upcased
1103 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
1104 introduces a space between markups on a line, the second markup is
1105 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
1106 the markups built for each token are put in a line by
1107 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
1108 to the @code{interpret-markup} function, with the @code{layout} and
1109 @code{props} arguments.
1111 Note: there is now an internal command @code{\smallCaps} which can
1112 be used to set text in small caps. See
1113 @ref{Text markup commands}, for details.
1117 Currently, the available combinations of arguments (after the standard
1118 @var{layout} and @var{props} arguments) to a markup command defined with
1119 @code{define-markup-command} are limited as follows.
1125 @itemx @var{markup markup}
1127 @itemx @var{scm markup}
1128 @itemx @var{scm scm}
1129 @itemx @var{scm scm markup}
1130 @itemx @var{scm scm markup markup}
1131 @itemx @var{scm markup markup}
1132 @itemx @var{scm scm scm}
1136 In the above table, @var{scm} represents native Scheme data types like
1137 @q{number} or @q{string}.
1139 As an example, it is not possible to use a markup command @code{foo} with
1140 four arguments defined as
1143 #(define-markup-command (foo layout props
1144 num1 str1 num2 str2)
1145 (number? string? number? string?)
1150 If you apply it as, say,
1153 \markup \foo #1 #"bar" #2 #"baz"
1156 @cindex Scheme signature
1157 @cindex signature, Scheme
1159 @command{lilypond} complains that it cannot parse @code{foo} due to its
1160 unknown Scheme signature.
1163 @node New markup list command definition
1164 @subsection New markup list command definition
1165 Markup list commands are defined with the
1166 @code{define-markup-list-command} Scheme macro, which is similar to the
1167 @code{define-markup-command} macro described in
1168 @ref{New markup command definition}, except that where the latter returns
1169 a single stencil, the former returns a list stencils.
1171 In the following example, a @code{\paragraph} markup list command is
1172 defined, which returns a list of justified lines, the first one being
1173 indented. The indent width is taken from the @code{props} argument.
1175 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1176 (let ((indent (chain-assoc-get 'par-indent props 2)))
1177 (interpret-markup-list layout props
1178 (make-justified-lines-markup-list (cons (make-hspace-markup indent)
1182 Besides the usual @code{layout} and @code{props} arguments, the
1183 @code{paragraph} markup list command takes a markup list argument, named
1184 @code{args}. The predicate for markup lists is @code{markup-list?}.
1186 First, the function gets the indent width, a property here named
1187 @code{par-indent}, from the property list @code{props} If the property
1188 is not found, the default value is @code{2}. Then, a list of justified
1189 lines is made using the @code{make-justified-lines-markup-list}
1190 function, which is related to the @code{\justified-lines}
1191 built-in markup list command. An horizontal space is added at the
1192 beginning using the @code{make-hspace-markup} function. Finally, the
1193 markup list is interpreted using the @code{interpret-markup-list}
1196 This new markup list command can be used as follows:
1200 The art of music typography is called \italic @{(plate) engraving.@}
1201 The term derives from the traditional process of music printing.
1202 Just a few decades ago, sheet music was made by cutting and stamping
1203 the music into a zinc or pewter plate in mirror image.
1205 \override-lines #'(par-indent . 4) \paragraph @{
1206 The plate would be inked, the depressions caused by the cutting
1207 and stamping would hold ink. An image was formed by pressing paper
1208 to the plate. The stamping and cutting was completely done by
1214 @node Contexts for programmers
1215 @section Contexts for programmers
1218 * Context evaluation::
1219 * Running a function on all layout objects::
1222 @node Context evaluation
1223 @subsection Context evaluation
1225 @cindex calling code during interpreting
1226 @funindex \applyContext
1228 Contexts can be modified during interpretation with Scheme code. The
1231 \applyContext @var{function}
1234 @var{function} should be a Scheme function taking a single argument,
1235 being the context to apply it to. The following code will print the
1236 current bar number on the standard output during the compile:
1241 (format #t "\nWe were called in barnumber ~a.\n"
1242 (ly:context-property x 'currentBarNumber)))
1247 @node Running a function on all layout objects
1248 @subsection Running a function on all layout objects
1251 @cindex calling code on layout objects
1252 @funindex \applyOutput
1255 The most versatile way of tuning an object is @code{\applyOutput}. Its
1258 \applyOutput @var{context} @var{proc}
1262 where @var{proc} is a Scheme function, taking three arguments.
1264 When interpreted, the function @var{proc} is called for every layout
1265 object found in the context @var{context}, with the following
1268 @item the layout object itself,
1269 @item the context where the layout object was created, and
1270 @item the context where @code{\applyOutput} is processed.
1274 In addition, the cause of the layout object, i.e., the music
1275 expression or object that was responsible for creating it, is in the
1276 object property @code{cause}. For example, for a note head, this is a
1277 @rinternals{NoteHead} event, and for a @rinternals{Stem} object,
1278 this is a @rinternals{NoteHead} object.
1280 Here is a function to use for @code{\applyOutput}; it blanks
1281 note-heads on the center-line:
1283 @lilypond[quote,verbatim,ragged-right]
1284 #(define (blanker grob grob-origin context)
1285 (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1286 (eq? (ly:grob-property grob 'staff-position) 0))
1287 (set! (ly:grob-property grob 'transparent) #t)))
1290 e4 g8 \applyOutput #'Voice #blanker b d2
1295 @node Scheme procedures as properties
1296 @section Scheme procedures as properties
1298 Properties (like thickness, direction, etc.) can be set at fixed values
1299 with \override, e.g.
1302 \override Stem #'thickness = #2.0
1305 Properties can also be set to a Scheme procedure,
1307 @lilypond[fragment,verbatim,quote,relative=2]
1308 \override Stem #'thickness = #(lambda (grob)
1309 (if (= UP (ly:grob-property grob 'direction))
1316 In this case, the procedure is executed as soon as the value of the
1317 property is requested during the formatting process.
1319 Most of the typesetting engine is driven by such callbacks.
1320 Properties that typically use callbacks include
1324 The printing routine, that constructs a drawing for the symbol
1326 The routine that sets the horizontal position
1328 The routine that computes the width of an object
1331 The procedure always takes a single argument, being the grob.
1333 If routines with multiple arguments must be called, the current grob
1334 can be inserted with a grob closure. Here is a setting from
1335 @code{AccidentalSuggestion},
1339 ,(ly:make-simple-closure
1341 ,(ly:make-simple-closure
1342 (list ly:self-alignment-interface::centered-on-x-parent))
1343 ,(ly:make-simple-closure
1344 (list ly:self-alignment-interface::x-aligned-on-self)))))
1348 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
1349 @code{ly:self-alignment-interface::centered-on-x-parent} are called
1350 with the grob as argument. The results are added with the @code{+}
1351 function. To ensure that this addition is properly executed, the whole
1352 thing is enclosed in @code{ly:make-simple-closure}.
1354 In fact, using a single procedure as property value is equivalent to
1357 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
1361 The inner @code{ly:make-simple-closure} supplies the grob as argument
1362 to @var{proc}, the outer ensures that result of the function is
1363 returned, rather than the @code{simple-closure} object.
1366 @node Using Scheme code instead of \tweak
1367 @section Using Scheme code instead of @code{\tweak}
1369 The main disadvantage of @code{\tweak} is its syntactical
1370 inflexibility. For example, the following produces a syntax error.
1373 F = \tweak #'font-size #-3 -\flageolet
1381 With other words, @code{\tweak} doesn't behave like an articulation
1382 regarding the syntax; in particular, it can't be attached with
1383 @code{^} and @code{_}.
1385 Using Scheme, this problem can be circumvented. The route to the
1386 result is given in @ref{Adding articulation to notes (example)},
1387 especially how to use @code{\displayMusic} as a helping guide.
1390 F = #(let ((m (make-music 'ArticulationEvent
1391 'articulation-type "flageolet")))
1392 (set! (ly:music-property m 'tweaks)
1393 (acons 'font-size -3
1394 (ly:music-property m 'tweaks)))
1403 Here, the @code{tweaks} properties of the flageolet object
1404 @code{m} (created with @code{make-music}) are extracted with
1405 @code{ly:music-property}, a new key-value pair to change the
1406 font size is prepended to the property list with the
1407 @code{acons} Scheme function, and the result is finally
1408 written back with @code{set!}. The last element of the
1409 @code{let} block is the return value, @code{m} itself.
1413 @node Difficult tweaks
1414 @section Difficult tweaks
1416 There are a few classes of difficult adjustments.
1422 One type of difficult adjustment is the appearance of spanner objects,
1423 such as slur and tie. Initially, only one of these objects is created,
1424 and they can be adjusted with the normal mechanism. However, in some
1425 cases the spanners cross line breaks. If this happens, these objects
1426 are cloned. A separate object is created for every system that it is
1427 in. These are clones of the original object and inherit all
1428 properties, including @code{\override}s.
1431 In other words, an @code{\override} always affects all pieces of a
1432 broken spanner. To change only one part of a spanner at a line break,
1433 it is necessary to hook into the formatting process. The
1434 @code{after-line-breaking} callback contains the Scheme procedure that
1435 is called after the line breaks have been determined, and layout
1436 objects have been split over different systems.
1438 In the following example, we define a procedure
1439 @code{my-callback}. This procedure
1443 determines if we have been split across line breaks
1445 if yes, retrieves all the split objects
1447 checks if we are the last of the split objects
1449 if yes, it sets @code{extra-offset}.
1452 This procedure is installed into @rinternals{Tie}, so the last part
1453 of the broken tie is translated up.
1455 @lilypond[quote,verbatim,ragged-right]
1456 #(define (my-callback grob)
1458 ; have we been split?
1459 (orig (ly:grob-original grob))
1461 ; if yes, get the split pieces (our siblings)
1462 (siblings (if (ly:grob? orig)
1463 (ly:spanner-broken-into orig) '() )))
1465 (if (and (>= (length siblings) 2)
1466 (eq? (car (last-pair siblings)) grob))
1467 (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1470 \override Tie #'after-line-breaking =
1477 When applying this trick, the new @code{after-line-breaking} callback
1478 should also call the old one @code{after-line-breaking}, if there is
1479 one. For example, if using this with @code{Hairpin},
1480 @code{ly:hairpin::after-line-breaking} should also be called.
1483 @item Some objects cannot be changed with @code{\override} for
1484 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1485 and @code{PaperColumn}. They can be changed with the
1486 @code{\overrideProperty} function, which works similar to @code{\once
1487 \override}, but uses a different syntax.
1491 #"Score.NonMusicalPaperColumn" % Grob name
1492 #'line-break-system-details % Property name
1493 #'((next-padding . 20)) % Value
1496 Note, however, that @code{\override}, applied to
1497 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1498 expected within @code{\context} blocks.