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::
43 * Overview of available music functions::
46 @node Overview of music functions
47 @subsection Overview of music functions
49 Making a function which substitutes a variable into LilyPond
50 code is easy. The general form of these functions is
54 #(define-music-function (parser location @var{var1} @var{var2}...@var{vari}... )
55 (@var{var1-type?} @var{var2-type?}...@var{vari-type?}...)
64 @multitable @columnfractions .33 .66
65 @item @var{vari} @tab @var{i}th variable
66 @item @var{vari-type?} @tab type of @var{i}th variable
67 @item @var{...music...} @tab normal LilyPond input, using
68 variables as @code{#$var1}, etc.
71 There following input types may be used as variables
72 in a music function. This list is not exhaustive; see
73 other documentation specifically about Scheme for more
76 @multitable @columnfractions .33 .66
77 @headitem Input type @tab @var{vari-type?} notation
78 @item Integer @tab @code{integer?}
79 @item Float (decimal number) @tab @code{number?}
80 @item Text string @tab @code{string?}
81 @item Markup @tab @code{markup?}
82 @item Music expression @tab @code{ly:music?}
83 @item A pair of variables @tab @code{pair?}
86 The @code{parser} and @code{location} arguments are mandatory,
87 and are used in some advanced situations. The @code{parser}
88 argument is used to gain access to the value of another LilyPond
89 variable. The @code{location} argument
90 is used to set the @q{origin} of the music expression that is built
91 by the music function, so that in case of a syntax error LilyPond
92 can tell the user an appropriate place to look in the input file.
95 @node Simple substitution functions
96 @subsection Simple substitution functions
98 Here is a simple example,
100 @lilypond[quote,verbatim,ragged-right]
101 padText = #(define-music-function (parser location padding) (number?)
103 \once \override TextScript #'padding = #$padding
111 c4^"piu mosso" fis a g
115 Music expressions may be substituted as well,
117 @lilypond[quote,verbatim,ragged-right]
118 custosNote = #(define-music-function (parser location note)
121 \once \override Voice.NoteHead #'stencil =
122 #ly:text-interface::print
123 \once \override Voice.NoteHead #'text =
124 \markup \musicglyph #"custodes.mensural.u0"
125 \once \override Voice.Stem #'stencil = ##f
129 { c' d' e' f' \custosNote g' }
132 Multiple variables may be used,
134 @lilypond[quote,verbatim,ragged-right]
135 tempoPadded = #(define-music-function (parser location padding tempotext)
138 \once \override Score.MetronomeMark #'padding = $padding
139 \tempo \markup { \bold $tempotext }
143 \tempo \markup { "Low tempo" }
145 \tempoPadded #4.0 #"High tempo"
151 @node Paired substitution functions
152 @subsection Paired substitution functions
154 Some @code{\override} commands require a pair of numbers
155 (called a @code{cons cell} in Scheme). To pass these numbers
156 into a function, either use a @code{pair?} variable, or
157 insert the @code{cons} into the music function.
162 #(define-music-function (parser location beg-end)
165 \once \override Beam #'positions = #$beg-end
169 \manualBeam #'(3 . 6) c8 d e f
177 @lilypond[quote,verbatim,ragged-right]
179 #(define-music-function (parser location beg end)
182 \once \override Beam #'positions = #(cons $beg $end)
186 \manualBeam #3 #6 c8 d e f
191 @node Mathematics in functions
192 @subsection Mathematics in functions
194 Music functions can involve Scheme programming in
195 addition to simple substitution,
197 @lilypond[quote,verbatim,ragged-right]
198 AltOn = #(define-music-function (parser location mag) (number?)
199 #{ \override Stem #'length = #$(* 7.0 mag)
200 \override NoteHead #'font-size =
201 #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag))) #})
204 \revert Stem #'length
205 \revert NoteHead #'font-size
208 { c'2 \AltOn #0.5 c'4 c'
209 \AltOn #1.5 c' c' \AltOff c'2 }
213 This example may be rewritten to pass in music expressions,
215 @lilypond[quote,verbatim,ragged-right]
216 withAlt = #(define-music-function (parser location mag music) (number? ly:music?)
217 #{ \override Stem #'length = #$(* 7.0 mag)
218 \override NoteHead #'font-size =
219 #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
221 \revert Stem #'length
222 \revert NoteHead #'font-size #})
224 { c'2 \withAlt #0.5 {c'4 c'}
225 \withAlt #1.5 {c' c'} c'2 }
229 @subsection Void functions
231 A music function must return a music expression, but sometimes we
232 may want to have a function which does not involve music (such as
233 turning off Point and Click). To do this, we return a @code{void}
237 that is returned is the @code{(make-music ...)}. With the
238 @code{'void} property set to @code{#t}, the parser is told to
239 actually disregard this returned music
240 expression. Thus the important part of the void music function is the
241 processing done by the function, not the music expression that is
246 #(define-music-function (parser location) ()
247 (ly:set-option 'point-and-click #f)
248 (make-music 'SequentialMusic 'void #t))
250 \noPointAndClick % disable point and click
254 @node Functions without arguments
255 @subsection Functions without arguments
257 In most cases a function without arguments should be written
261 dolce = \markup@{ \italic \bold dolce @}
264 However, in rare cases it may be useful to create a music function
269 #(define-music-function (parser location) ()
270 (if (eq? #t (ly:get-option 'display-bar-numbers))
271 #@{ \once \override Score.BarNumber #'break-visibility = ##f #@}
275 To actually display bar numbers where this function is called,
276 invoke @command{lilypond} with
279 lilypond -d display-bar-numbers FILENAME.ly
283 @node Overview of available music functions
284 @subsection Overview of available music functions
286 @c fixme ; this should be move somewhere else?
287 The following commands are music functions
289 @include identifiers.tely
293 @node Programmer interfaces
294 @section Programmer interfaces
296 This section contains information about mixing LilyPond
300 * Input variables and Scheme::
301 * Internal music representation::
305 @node Input variables and Scheme
306 @subsection Input variables and Scheme
308 The input format supports the notion of variables: in the following
309 example, a music expression is assigned to a variable with the name
313 traLaLa = @{ c'4 d'4 @}
318 There is also a form of scoping: in the following example, the
319 @code{\layout} block also contains a @code{traLaLa} variable, which is
320 independent of the outer @code{\traLaLa}.
322 traLaLa = @{ c'4 d'4 @}
323 \layout @{ traLaLa = 1.0 @}
326 In effect, each input file is a scope, and all @code{\header},
327 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
330 Both variables and scoping are implemented in the GUILE module system.
331 An anonymous Scheme module is attached to each scope. An assignment of
334 traLaLa = @{ c'4 d'4 @}
338 is internally converted to a Scheme definition
340 (define traLaLa @var{Scheme value of `@code{... }'})
343 This means that input variables and Scheme variables may be freely
344 mixed. In the following example, a music fragment is stored in the
345 variable @code{traLaLa}, and duplicated using Scheme. The result is
346 imported in a @code{\score} block by means of a second variable
350 traLaLa = { c'4 d'4 }
352 %% dummy action to deal with parser lookahead
353 #(display "this needs to be here, sorry!")
355 #(define newLa (map ly:music-deep-copy
356 (list traLaLa traLaLa)))
358 (make-sequential-music newLa))
363 @c Due to parser lookahead
365 In this example, the assignment happens after parser has verified that
366 nothing interesting happens after @code{traLaLa = @{ ... @}}. Without
367 the dummy statement in the above example, the @code{newLa} definition
368 is executed before @code{traLaLa} is defined, leading to a syntax
371 The above example shows how to @q{export} music expressions from the
372 input to the Scheme interpreter. The opposite is also possible. By
373 wrapping a Scheme value in the function @code{ly:export}, a Scheme
374 value is interpreted as if it were entered in LilyPond syntax.
375 Instead of defining @code{\twice}, the example above could also have
380 @{ #(ly:export (make-sequential-music (list newLa))) @}
383 Scheme code is evaluated as soon as the parser encounters it. To
384 define some Scheme code in a macro (to be called later), use
385 @ref{Void functions}, or
389 (ly:set-option 'point-and-click #f))
399 Mixing Scheme and LilyPond variables is not possible with the
400 @code{--safe} option.
403 @node Internal music representation
404 @subsection Internal music representation
406 When a music expression is parsed, it is converted into a set of
407 Scheme music objects. The defining property of a music object is that
408 it takes up time. Time is a rational number that measures the length
409 of a piece of music in whole notes.
411 A music object has three kinds of types:
414 music name: Each music expression has a name. For example, a note
415 leads to a @rinternals{NoteEvent}, and @code{\simultaneous} leads to
416 a @rinternals{SimultaneousMusic}. A list of all expressions
417 available is in the Internals Reference manual, under
418 @rinternals{Music expressions}.
421 @q{type} or interface: Each music name has several @q{types} or
422 interfaces, for example, a note is an @code{event}, but it is also a
423 @code{note-event}, a @code{rhythmic-event}, and a
424 @code{melodic-event}. All classes of music are listed in the
425 Internals Reference, under
426 @rinternals{Music classes}.
429 C++ object: Each music object is represented by an object of the C++
433 The actual information of a music expression is stored in properties.
434 For example, a @rinternals{NoteEvent} has @code{pitch} and
435 @code{duration} properties that store the pitch and duration of that
436 note. A list of all properties available is in the internals manual,
437 under @rinternals{Music properties}.
439 A compound music expression is a music object that contains other
440 music objects in its properties. A list of objects can be stored in
441 the @code{elements} property of a music object, or a single @q{child}
442 music object in the @code{element} property. For example,
443 @rinternals{SequentialMusic} has its children in @code{elements},
444 and @rinternals{GraceMusic} has its single argument in
445 @code{element}. The body of a repeat is stored in the @code{element}
446 property of @rinternals{RepeatedMusic}, and the alternatives in
451 @node Building complicated functions
452 @section Building complicated functions
454 This section explains how to gather the information necessary
455 to create complicated music functions.
458 * Displaying music expressions::
460 * Doubling a note with slurs (example)::
461 * Adding articulation to notes (example)::
465 @node Displaying music expressions
466 @subsection Displaying music expressions
468 @cindex internal storage
469 @cindex displaying music expressions
470 @cindex internal representation, displaying
472 @funindex \displayMusic
474 When writing a music function it is often instructive to inspect how
475 a music expression is stored internally. This can be done with the
476 music function @code{\displayMusic}
480 \displayMusic @{ c'4\f @}
497 (ly:make-duration 2 0 1 1)
499 (ly:make-pitch 0 0 0))
501 'AbsoluteDynamicEvent
506 By default, LilyPond will print these messages to the console along
507 with all the other messages. To split up these messages and save
508 the results of @code{\display@{STUFF@}}, redirect the output to
512 lilypond file.ly >display.txt
515 With a bit of reformatting, the above information is
519 (make-music 'SequentialMusic
520 'elements (list (make-music 'EventChord
521 'elements (list (make-music 'NoteEvent
522 'duration (ly:make-duration 2 0 1 1)
523 'pitch (ly:make-pitch 0 0 0))
524 (make-music 'AbsoluteDynamicEvent
528 A @code{@{ ... @}} music sequence has the name @code{SequentialMusic},
529 and its inner expressions are stored as a list in its @code{'elements}
530 property. A note is represented as an @code{EventChord} expression,
531 containing a @code{NoteEvent} object (storing the duration and
532 pitch properties) and any extra information (in this case, an
533 @code{AbsoluteDynamicEvent} with a @code{"f"} text property.
536 @node Music properties
537 @subsection Music properties
539 The @code{NoteEvent} object is the first object of the
540 @code{'elements} property of @code{someNote}.
544 \displayMusic \someNote
552 (ly:make-duration 2 0 1 1)
554 (ly:make-pitch 0 0 0))))
557 The @code{display-scheme-music} function is the function used by
558 @code{\displayMusic} to display the Scheme representation of a music
562 #(display-scheme-music (first (ly:music-property someNote 'elements)))
567 (ly:make-duration 2 0 1 1)
569 (ly:make-pitch 0 0 0))
572 Then the note pitch is accessed through the @code{'pitch} property
573 of the @code{NoteEvent} object,
576 #(display-scheme-music
577 (ly:music-property (first (ly:music-property someNote 'elements))
580 (ly:make-pitch 0 0 0)
583 The note pitch can be changed by setting this 'pitch property,
585 @funindex \displayLilyMusic
588 #(set! (ly:music-property (first (ly:music-property someNote 'elements))
590 (ly:make-pitch 0 1 0)) ;; set the pitch to d'.
591 \displayLilyMusic \someNote
597 @node Doubling a note with slurs (example)
598 @subsection Doubling a note with slurs (example)
600 Suppose we want to create a function which translates
601 input like @code{a} into @code{a( a)}. We begin
602 by examining the internal representation of the music
603 we want to end up with.
606 \displayMusic@{ a'( a') @}
617 (ly:make-duration 2 0 1 1)
619 (ly:make-pitch 0 5 0))
630 (ly:make-duration 2 0 1 1)
632 (ly:make-pitch 0 5 0))
639 The bad news is that the @code{SlurEvent} expressions
640 must be added @q{inside} the note (or more precisely,
641 inside the @code{EventChord} expression).
643 Now we examine the input,
655 (ly:make-duration 2 0 1 1)
657 (ly:make-pitch 0 5 0))))))
660 So in our function, we need to clone this expression (so that we
661 have two notes to build the sequence), add @code{SlurEvents} to the
662 @code{'elements} property of each one, and finally make a
663 @code{SequentialMusic} with the two @code{EventChords}.
666 doubleSlur = #(define-music-function (parser location note) (ly:music?)
667 "Return: @{ note ( note ) @}.
668 `note' is supposed to be an EventChord."
669 (let ((note2 (ly:music-deep-copy note)))
670 (set! (ly:music-property note 'elements)
671 (cons (make-music 'SlurEvent 'span-direction -1)
672 (ly:music-property note 'elements)))
673 (set! (ly:music-property note2 'elements)
674 (cons (make-music 'SlurEvent 'span-direction 1)
675 (ly:music-property note2 'elements)))
676 (make-music 'SequentialMusic 'elements (list note note2))))
680 @node Adding articulation to notes (example)
681 @subsection Adding articulation to notes (example)
683 The easy way to add articulation to notes is to merge two music
684 expressions into one context, as explained in
685 @ref{Creating contexts}. However, suppose that we want to write
686 a music function which does this.
688 A @code{$variable} inside the @code{#@{...#@}} notation is like
689 using a regular @code{\variable} in classical LilyPond
690 notation. We know that
697 will not work in LilyPond. We could avoid this problem by attaching
698 the articulation to a fake note,
701 @{ << \music s1*0-.-> @}
705 but for the sake of this example, we will learn how to do this in
706 Scheme. We begin by examining our input and desired output,
718 (ly:make-duration 2 0 1 1)
720 (ly:make-pitch -1 0 0))))
731 (ly:make-duration 2 0 1 1)
733 (ly:make-pitch -1 0 0))
740 We see that a note (@code{c4}) is represented as an @code{EventChord}
741 expression, with a @code{NoteEvent} expression in its elements list. To
742 add a marcato articulation, an @code{ArticulationEvent} expression must
743 be added to the elements property of the @code{EventChord}
746 To build this function, we begin with
749 (define (add-marcato event-chord)
750 "Add a marcato ArticulationEvent to the elements of `event-chord',
751 which is supposed to be an EventChord expression."
752 (let ((result-event-chord (ly:music-deep-copy event-chord)))
753 (set! (ly:music-property result-event-chord 'elements)
754 (cons (make-music 'ArticulationEvent
755 'articulation-type "marcato")
756 (ly:music-property result-event-chord 'elements)))
760 The first line is the way to define a function in Scheme: the function
761 name is @code{add-marcato}, and has one variable called
762 @code{event-chord}. In Scheme, the type of variable is often clear
763 from its name. (this is good practice in other programming languages,
771 is a description of what the function does. This is not strictly
772 necessary, but just like clear variable names, it is good practice.
775 (let ((result-event-chord (ly:music-deep-copy event-chord)))
778 @code{let} is used to declare local variables. Here we use one local
779 variable, named @code{result-event-chord}, to which we give the value
780 @code{(ly:music-deep-copy event-chord)}. @code{ly:music-deep-copy} is
781 a function specific to LilyPond, like all functions prefixed by
782 @code{ly:}. It is use to make a copy of a music
783 expression. Here we copy @code{event-chord} (the parameter of the
784 function). Recall that our purpose is to add a marcato to an
785 @code{EventChord} expression. It is better to not modify the
786 @code{EventChord} which was given as an argument, because it may be
789 Now we have a @code{result-event-chord}, which is a
790 @code{NoteEventChord} expression and is a copy of @code{event-chord}. We
791 add the marcato to its elements list property.
794 (set! place new-value)
797 Here, what we want to set (the @q{place}) is the @q{elements} property of
798 @code{result-event-chord} expression.
801 (ly:music-property result-event-chord 'elements)
804 @code{ly:music-property} is the function used to access music properties
805 (the @code{'elements}, @code{'duration}, @code{'pitch}, etc, that we
806 see in the @code{\displayMusic} output above). The new value is the
807 former elements property, with an extra item: the
808 @code{ArticulationEvent} expression, which we copy from the
809 @code{\displayMusic} output,
812 (cons (make-music 'ArticulationEvent
813 'articulation-type "marcato")
814 (ly:music-property result-event-chord 'elements))
817 @code{cons} is used to add an element to a list without modifying the
818 original list. This is what we
819 want: the same list as before, plus the new @code{ArticulationEvent}
820 expression. The order inside the elements property is not important here.
822 Finally, once we have added the marcato articulation to its @code{elements}
823 property, we can return @code{result-event-chord}, hence the last line of
826 Now we transform the @code{add-marcato} function into a music
830 addMarcato = #(define-music-function (parser location event-chord)
832 "Add a marcato ArticulationEvent to the elements of `event-chord',
833 which is supposed to be an EventChord expression."
834 (let ((result-event-chord (ly:music-deep-copy event-chord)))
835 (set! (ly:music-property result-event-chord 'elements)
836 (cons (make-music 'ArticulationEvent
837 'articulation-type "marcato")
838 (ly:music-property result-event-chord 'elements)))
842 We may verify that this music function works correctly,
845 \displayMusic \addMarcato c4
849 @node Markup programmer interface
850 @section Markup programmer interface
852 Markups are implemented as special Scheme functions which produce a
853 Stencil object given a number of arguments.
856 * Markup construction in Scheme::
857 * How markups work internally::
858 * New markup command definition::
859 * New markup list command definition::
863 @node Markup construction in Scheme
864 @subsection Markup construction in Scheme
866 @cindex defining markup commands
868 The @code{markup} macro builds markup expressions in Scheme while
869 providing a LilyPond-like syntax. For example,
871 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
872 #:larger #:line ("foo" "bar" "baz")))
878 \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
879 \larger \line @{ foo bar baz @} @}
883 This example demonstrates the main translation rules between regular
884 LilyPond markup syntax and Scheme markup syntax.
887 @multitable @columnfractions .3 .3
888 @item @b{LilyPond} @tab @b{Scheme}
889 @item @code{\markup markup1} @tab @code{(markup markup1)}
890 @item @code{\markup @{ markup1 markup2 ... @}} @tab
891 @code{(markup markup1 markup2 ... )}
892 @item @code{\command} @tab @code{#:command}
893 @item @code{\variable} @tab @code{variable}
894 @item @code{\center-column @{ ... @}} @tab @code{#:center-column ( ... )}
895 @item @code{string} @tab @code{"string"}
896 @item @code{#scheme-arg} @tab @code{scheme-arg}
900 The whole Scheme language is accessible inside the
901 @code{markup} macro. For example, You may use function calls inside
902 @code{markup} in order to manipulate character strings. This is
903 useful when defining new markup commands (see
904 @ref{New markup command definition}).
909 The markup-list argument of commands such as @code{#:line},
910 @code{#:center}, and @code{#:column} cannot be a variable or
911 the result of a function call.
914 (markup #:line (function-that-returns-markups))
918 is invalid. One should use the @code{make-line-markup},
919 @code{make-center-markup}, or @code{make-column-markup} functions
923 (markup (make-line-markup (function-that-returns-markups)))
927 @node How markups work internally
928 @subsection How markups work internally
933 \raise #0.5 "text example"
937 @code{\raise} is actually represented by the @code{raise-markup}
938 function. The markup expression is stored as
941 (list raise-markup 0.5 (list simple-markup "text example"))
944 When the markup is converted to printable objects (Stencils), the
945 @code{raise-markup} function is called as
950 @var{list of property alists}
952 @var{the "text example" markup})
955 The @code{raise-markup} function first creates the stencil for the
956 @code{text example} string, and then it raises that Stencil by 0.5
957 staff space. This is a rather simple example; more complex examples
959 of this section, and in @file{scm/@/define@/-markup@/-commands@/.scm}.
962 @node New markup command definition
963 @subsection New markup command definition
965 New markup commands can be defined
966 with the @code{define-markup-command} Scheme macro.
969 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
970 (@var{arg1-type?} @var{arg2-type?} ...)
978 @var{i}th command argument
980 a type predicate for the i@var{th} argument
982 the @q{layout} definition
984 a list of alists, containing all active properties.
987 As a simple example, we show how to add a @code{\smallcaps} command,
988 which selects a small caps font. Normally we could select the
992 \markup @{ \override #'(font-shape . caps) Text-in-caps @}
996 This selects the caps font by setting the @code{font-shape} property to
997 @code{#'caps} for interpreting @code{Text-in-caps}.
999 To make the above available as @code{\smallcaps} command, we must
1000 define a function using @code{define-markup-command}. The command should
1001 take a single argument of type @code{markup}. Therefore the start of the
1002 definition should read
1005 (define-markup-command (smallcaps layout props argument) (markup?)
1010 What follows is the content of the command: we should interpret
1011 the @code{argument} as a markup, i.e.,
1014 (interpret-markup layout @dots{} argument)
1018 This interpretation should add @code{'(font-shape . caps)} to the active
1019 properties, so we substitute the following for the @dots{} in the
1023 (cons (list '(font-shape . caps) ) props)
1027 The variable @code{props} is a list of alists, and we prepend to it by
1028 cons'ing a list with the extra setting.
1031 Suppose that we are typesetting a recitative in an opera and
1032 we would like to define a command that will show character names in a
1033 custom manner. Names should be printed with small caps and moved a
1034 bit to the left and top. We will define a @code{\character} command
1035 which takes into account the necessary translation and uses the newly
1036 defined @code{\smallcaps} command:
1039 #(define-markup-command (character layout props name) (string?)
1040 "Print the character name in small caps, translated to the left and
1041 top. Syntax: \\character #\"name\""
1042 (interpret-markup layout props
1043 (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
1046 There is one complication that needs explanation: texts above and below
1047 the staff are moved vertically to be at a certain distance (the
1048 @code{padding} property) from the staff and the notes. To make sure
1049 that this mechanism does not annihilate the vertical effect of our
1050 @code{#:translate}, we add an empty string (@code{#:hspace 0}) before the
1051 translated text. Now the @code{#:hspace 0} will be put above the notes,
1053 @code{name} is moved in relation to that empty string. The net effect is
1054 that the text is moved to the upper left.
1056 The final result is as follows:
1060 c''^\markup \character #"Cleopatra"
1061 e'^\markup \character #"Giulio Cesare"
1065 @lilypond[quote,ragged-right]
1066 #(define-markup-command (smallcaps layout props str) (string?)
1067 "Print the string argument in small caps. Syntax: \\smallcaps #\"string\""
1068 (interpret-markup layout props
1071 (if (= (string-length s) 0)
1073 (markup #:large (string-upcase (substring s 0 1))
1074 #:translate (cons -0.6 0)
1075 #:tiny (string-upcase (substring s 1)))))
1076 (string-split str #\Space)))))
1078 #(define-markup-command (character layout props name) (string?)
1079 "Print the character name in small caps, translated to the left and
1080 top. Syntax: \\character #\"name\""
1081 (interpret-markup layout props
1082 (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
1085 c''^\markup \character #"Cleopatra" c'' c'' c''
1086 e'^\markup \character #"Giulio Cesare" e' e' e'
1090 We have used the @code{caps} font shape, but suppose that our font
1091 does not have a small-caps variant. In that case we have to fake
1092 the small caps font by setting a string in upcase with the first
1093 letter a little larger:
1096 #(define-markup-command (smallcaps layout props str) (string?)
1097 "Print the string argument in small caps."
1098 (interpret-markup layout props
1101 (if (= (string-length s) 0)
1103 (markup #:large (string-upcase (substring s 0 1))
1104 #:translate (cons -0.6 0)
1105 #:tiny (string-upcase (substring s 1)))))
1106 (string-split str #\Space)))))
1109 The @code{smallcaps} command first splits its string argument into
1110 tokens separated by spaces (@code{(string-split str #\Space)}); for
1111 each token, a markup is built with the first letter made large and
1112 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
1113 second markup built with the following letters made tiny and upcased
1114 (@code{#:tiny (string-upcase (substring s 1))}). As LilyPond
1115 introduces a space between markups on a line, the second markup is
1116 translated to the left (@code{#:translate (cons -0.6 0) ...}). Then,
1117 the markups built for each token are put in a line by
1118 @code{(make-line-markup ...)}. Finally, the resulting markup is passed
1119 to the @code{interpret-markup} function, with the @code{layout} and
1120 @code{props} arguments.
1122 Note: there is now an internal command @code{\smallCaps} which can
1123 be used to set text in small caps. See
1124 @ref{Text markup commands}, for details.
1128 Currently, the available combinations of arguments (after the standard
1129 @var{layout} and @var{props} arguments) to a markup command defined with
1130 @code{define-markup-command} are limited as follows.
1136 @itemx @var{markup markup}
1138 @itemx @var{scm markup}
1139 @itemx @var{scm scm}
1140 @itemx @var{scm scm markup}
1141 @itemx @var{scm scm markup markup}
1142 @itemx @var{scm markup markup}
1143 @itemx @var{scm scm scm}
1147 In the above table, @var{scm} represents native Scheme data types like
1148 @q{number} or @q{string}.
1150 As an example, it is not possible to use a markup command @code{foo} with
1151 four arguments defined as
1154 #(define-markup-command (foo layout props
1155 num1 str1 num2 str2)
1156 (number? string? number? string?)
1161 If you apply it as, say,
1164 \markup \foo #1 #"bar" #2 #"baz"
1167 @cindex Scheme signature
1168 @cindex signature, Scheme
1170 @command{lilypond} complains that it cannot parse @code{foo} due to its
1171 unknown Scheme signature.
1174 @node New markup list command definition
1175 @subsection New markup list command definition
1176 Markup list commands are defined with the
1177 @code{define-markup-list-command} Scheme macro, which is similar to the
1178 @code{define-markup-command} macro described in
1179 @ref{New markup command definition}, except that where the latter returns
1180 a single stencil, the former returns a list stencils.
1182 In the following example, a @code{\paragraph} markup list command is
1183 defined, which returns a list of justified lines, the first one being
1184 indented. The indent width is taken from the @code{props} argument.
1186 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1187 (let ((indent (chain-assoc-get 'par-indent props 2)))
1188 (interpret-markup-list layout props
1189 (make-justified-lines-markup-list (cons (make-hspace-markup indent)
1193 Besides the usual @code{layout} and @code{props} arguments, the
1194 @code{paragraph} markup list command takes a markup list argument, named
1195 @code{args}. The predicate for markup lists is @code{markup-list?}.
1197 First, the function gets the indent width, a property here named
1198 @code{par-indent}, from the property list @code{props} If the property
1199 is not found, the default value is @code{2}. Then, a list of justified
1200 lines is made using the @code{make-justified-lines-markup-list}
1201 function, which is related to the @code{\justified-lines}
1202 built-in markup list command. An horizontal space is added at the
1203 beginning using the @code{make-hspace-markup} function. Finally, the
1204 markup list is interpreted using the @code{interpret-markup-list}
1207 This new markup list command can be used as follows:
1211 The art of music typography is called \italic @{(plate) engraving.@}
1212 The term derives from the traditional process of music printing.
1213 Just a few decades ago, sheet music was made by cutting and stamping
1214 the music into a zinc or pewter plate in mirror image.
1216 \override-lines #'(par-indent . 4) \paragraph @{
1217 The plate would be inked, the depressions caused by the cutting
1218 and stamping would hold ink. An image was formed by pressing paper
1219 to the plate. The stamping and cutting was completely done by
1225 @node Contexts for programmers
1226 @section Contexts for programmers
1229 * Context evaluation::
1230 * Running a function on all layout objects::
1233 @node Context evaluation
1234 @subsection Context evaluation
1236 @cindex calling code during interpreting
1237 @funindex \applyContext
1239 Contexts can be modified during interpretation with Scheme code. The
1242 \applyContext @var{function}
1245 @var{function} should be a Scheme function taking a single argument,
1246 being the context to apply it to. The following code will print the
1247 current bar number on the standard output during the compile:
1252 (format #t "\nWe were called in barnumber ~a.\n"
1253 (ly:context-property x 'currentBarNumber)))
1258 @node Running a function on all layout objects
1259 @subsection Running a function on all layout objects
1262 @cindex calling code on layout objects
1263 @funindex \applyOutput
1266 The most versatile way of tuning an object is @code{\applyOutput}. Its
1269 \applyOutput @var{context} @var{proc}
1273 where @var{proc} is a Scheme function, taking three arguments.
1275 When interpreted, the function @var{proc} is called for every layout
1276 object found in the context @var{context}, with the following
1279 @item the layout object itself,
1280 @item the context where the layout object was created, and
1281 @item the context where @code{\applyOutput} is processed.
1285 In addition, the cause of the layout object, i.e., the music
1286 expression or object that was responsible for creating it, is in the
1287 object property @code{cause}. For example, for a note head, this is a
1288 @rinternals{NoteHead} event, and for a @rinternals{Stem} object,
1289 this is a @rinternals{NoteHead} object.
1291 Here is a function to use for @code{\applyOutput}; it blanks
1292 note-heads on the center-line:
1294 @lilypond[quote,verbatim,ragged-right]
1295 #(define (blanker grob grob-origin context)
1296 (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1297 (eq? (ly:grob-property grob 'staff-position) 0))
1298 (set! (ly:grob-property grob 'transparent) #t)))
1301 e4 g8 \applyOutput #'Voice #blanker b d2
1306 @node Scheme procedures as properties
1307 @section Scheme procedures as properties
1309 Properties (like thickness, direction, etc.) can be set at fixed values
1310 with \override, e.g.
1313 \override Stem #'thickness = #2.0
1316 Properties can also be set to a Scheme procedure,
1318 @lilypond[fragment,verbatim,quote,relative=2]
1319 \override Stem #'thickness = #(lambda (grob)
1320 (if (= UP (ly:grob-property grob 'direction))
1327 In this case, the procedure is executed as soon as the value of the
1328 property is requested during the formatting process.
1330 Most of the typesetting engine is driven by such callbacks.
1331 Properties that typically use callbacks include
1335 The printing routine, that constructs a drawing for the symbol
1337 The routine that sets the horizontal position
1339 The routine that computes the width of an object
1342 The procedure always takes a single argument, being the grob.
1344 If routines with multiple arguments must be called, the current grob
1345 can be inserted with a grob closure. Here is a setting from
1346 @code{AccidentalSuggestion},
1350 ,(ly:make-simple-closure
1352 ,(ly:make-simple-closure
1353 (list ly:self-alignment-interface::centered-on-x-parent))
1354 ,(ly:make-simple-closure
1355 (list ly:self-alignment-interface::x-aligned-on-self)))))
1359 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
1360 @code{ly:self-alignment-interface::centered-on-x-parent} are called
1361 with the grob as argument. The results are added with the @code{+}
1362 function. To ensure that this addition is properly executed, the whole
1363 thing is enclosed in @code{ly:make-simple-closure}.
1365 In fact, using a single procedure as property value is equivalent to
1368 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
1372 The inner @code{ly:make-simple-closure} supplies the grob as argument
1373 to @var{proc}, the outer ensures that result of the function is
1374 returned, rather than the @code{simple-closure} object.
1377 @node Using Scheme code instead of \tweak
1378 @section Using Scheme code instead of @code{\tweak}
1380 The main disadvantage of @code{\tweak} is its syntactical
1381 inflexibility. For example, the following produces a syntax error.
1384 F = \tweak #'font-size #-3 -\flageolet
1392 With other words, @code{\tweak} doesn't behave like an articulation
1393 regarding the syntax; in particular, it can't be attached with
1394 @code{^} and @code{_}.
1396 Using Scheme, this problem can be circumvented. The route to the
1397 result is given in @ref{Adding articulation to notes (example)},
1398 especially how to use @code{\displayMusic} as a helping guide.
1401 F = #(let ((m (make-music 'ArticulationEvent
1402 'articulation-type "flageolet")))
1403 (set! (ly:music-property m 'tweaks)
1404 (acons 'font-size -3
1405 (ly:music-property m 'tweaks)))
1414 Here, the @code{tweaks} properties of the flageolet object
1415 @code{m} (created with @code{make-music}) are extracted with
1416 @code{ly:music-property}, a new key-value pair to change the
1417 font size is prepended to the property list with the
1418 @code{acons} Scheme function, and the result is finally
1419 written back with @code{set!}. The last element of the
1420 @code{let} block is the return value, @code{m} itself.
1424 @node Difficult tweaks
1425 @section Difficult tweaks
1427 There are a few classes of difficult adjustments.
1433 One type of difficult adjustment is the appearance of spanner objects,
1434 such as slur and tie. Initially, only one of these objects is created,
1435 and they can be adjusted with the normal mechanism. However, in some
1436 cases the spanners cross line breaks. If this happens, these objects
1437 are cloned. A separate object is created for every system that it is
1438 in. These are clones of the original object and inherit all
1439 properties, including @code{\override}s.
1442 In other words, an @code{\override} always affects all pieces of a
1443 broken spanner. To change only one part of a spanner at a line break,
1444 it is necessary to hook into the formatting process. The
1445 @code{after-line-breaking} callback contains the Scheme procedure that
1446 is called after the line breaks have been determined, and layout
1447 objects have been split over different systems.
1449 In the following example, we define a procedure
1450 @code{my-callback}. This procedure
1454 determines if we have been split across line breaks
1456 if yes, retrieves all the split objects
1458 checks if we are the last of the split objects
1460 if yes, it sets @code{extra-offset}.
1463 This procedure is installed into @rinternals{Tie}, so the last part
1464 of the broken tie is translated up.
1466 @lilypond[quote,verbatim,ragged-right]
1467 #(define (my-callback grob)
1469 ; have we been split?
1470 (orig (ly:grob-original grob))
1472 ; if yes, get the split pieces (our siblings)
1473 (siblings (if (ly:grob? orig)
1474 (ly:spanner-broken-into orig) '() )))
1476 (if (and (>= (length siblings) 2)
1477 (eq? (car (last-pair siblings)) grob))
1478 (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1481 \override Tie #'after-line-breaking =
1488 When applying this trick, the new @code{after-line-breaking} callback
1489 should also call the old one @code{after-line-breaking}, if there is
1490 one. For example, if using this with @code{Hairpin},
1491 @code{ly:hairpin::after-line-breaking} should also be called.
1494 @item Some objects cannot be changed with @code{\override} for
1495 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1496 and @code{PaperColumn}. They can be changed with the
1497 @code{\overrideProperty} function, which works similar to @code{\once
1498 \override}, but uses a different syntax.
1502 #"Score.NonMusicalPaperColumn" % Grob name
1503 #'line-break-system-details % Property name
1504 #'((next-padding . 20)) % Value
1507 Note, however, that @code{\override}, applied to
1508 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1509 expected within @code{\context} blocks.