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. For details, see the Contributors'
8 Guide, node Updating translation committishes..
13 @node Interfaces for programmers
14 @chapter Interfaces for programmers
16 Advanced tweaks may be performed by using Scheme. If you are
17 not familiar with Scheme, you may wish to read our
18 @ref{Scheme tutorial}.
21 * LilyPond code blocks::
26 * Contexts for programmers::
27 * Callback functions::
28 * Inline Scheme code::
32 @node LilyPond code blocks
33 @section LilyPond code blocks
35 @cindex LilyPond code blocks
36 @cindex code blocks, LilyPond
37 @funindex #@{ @dots{} #@}
41 Creating music expressions in Scheme can be tedious, as they are
42 heavily nested and the resulting Scheme code is large. For some
43 simple tasks this can be avoided by using LilyPond code blocks,
44 which enable common LilyPond syntax to be used within Scheme.
46 LilyPond code blocks look like
49 #@{ @var{LilyPond code} #@}
52 Here is a trivial example:
54 @lilypond[verbatim,quote]
55 ritpp = #(define-event-function (parser location) ()
62 LilyPond code blocks can be used anywhere where you can write Scheme
63 code. The Scheme reader actually is changed for accommodating
64 LilyPond code blocks and can deal with embedded Scheme expressions
65 starting with @code{$} and@w{ }@code{#}.
67 @cindex parser (function argument)
70 The reader extracts the LilyPond code block and generates a runtime
71 call to the LilyPond @code{parser} to interpret the LilyPond code.
72 Scheme expressions embedded in the LilyPond code are evaluated in the
73 lexical environment of the LilyPond code block, so all local variables
74 and function parameters available at the point the LilyPond code block
75 is written may be accessed. Variables defined in other Scheme modules,
76 like the modules containing @code{\header} and @code{\layout} blocks,
77 are not accessible as Scheme variables, i.e. prefixed
78 with@tie{}@code{#}, but they are accessible as LilyPond variables, i.e.
79 prefixed with@tie{}@code{\}.
81 If @code{location} (see @ref{Scheme functions}) refers to a valid
82 input location (which it usually does inside of music/@/Scheme
83 functions), all music generated inside the code block has its
84 @samp{origin} set to @code{location}.
86 A LilyPond code block may contain anything that you can use on the
87 right side of an assignment. In addition, an empty LilyPond block
88 corresponds to a void music expression, and a LilyPond block
89 containing multiple music events gets turned into a sequential music
92 @node Scheme functions
93 @section Scheme functions
94 @cindex Scheme functions (LilyPond syntax)
96 @emph{Scheme functions} are Scheme procedures that can create Scheme
97 expressions from input written in LilyPond syntax. They can be called
98 in pretty much all places where using @code{#} for specifying a value in
99 Scheme syntax is allowed. While Scheme has functions of its own, this
100 chapter is concerned with @emph{syntactic} functions, functions that
101 receive arguments specified in LilyPond syntax.
104 * Scheme function definitions::
105 * Scheme function usage::
106 * Void scheme functions::
109 @node Scheme function definitions
110 @subsection Scheme function definitions
111 @funindex define-scheme-function
113 The general form for defining scheme functions is:
117 #(define-scheme-function
118 (parser location @var{arg1} @var{arg2} @dots{})
119 (@var{type1?} @var{type2?} @dots{})
126 @multitable @columnfractions .33 .66
128 @tab needs to be literally @code{parser} in order to give LilyPond code
129 blocks (@code{#@{}@dots{}@code{#@}}) access to the parser.
131 @item @code{location}
132 @tab needs to be literally @code{location} in order to provide access
133 to the input location object, which is used to provide error messages
134 with file names and line numbers.
136 @item @code{@var{argN}}
137 @tab @var{n}th argument
139 @item @code{@var{typeN?}}
140 @tab a Scheme @emph{type predicate} for which @code{@var{argN}}
141 must return @code{#t}. Some of these predicates are specially
142 recognized by the parser, see below. There is also a special form
143 @code{(@emph{predicate?} @emph{default})} for specifying optional
144 arguments. If the actual argument is missing when the function is being
145 called, the default value is substituted instead. Default values are
146 evaluated at definition time (including LilyPond code blocks!), so if
147 you need a default calculated at runtime, instead write a special value
148 you can easily recognize. If you write the predicate in parentheses but
149 don't follow it with a default value, @code{#f} is used as the default.
150 Default values are not verified with @emph{predicate?} at either
151 definition or run time: it is your responsibility to deal with the
152 values you specify. Default values that happen to be music expressions
153 are copied while setting @code{origin} to the @code{location} parameter.
155 @item @code{@var{body}}
156 @tab A sequence of Scheme forms evaluated in order, the last one being
157 used as the return value of the scheme function. It may contain
158 LilyPond code blocks enclosed in hashed braces
159 (@tie{}@w{@code{#@{@dots{}#@}}}@tie{}), like described in
160 @ref{LilyPond code blocks}. Within LilyPond code blocks, use @code{#}
161 to reference function arguments (eg., @samp{#arg1}) or to start an
162 inline Scheme expression containing function arguments (eg.,
163 @w{@samp{#(cons arg1 arg2)}}). Where normal Scheme expressions using
164 @code{#} don't do the trick, you might need to revert to immediate
165 Scheme expressions using @code{$}, for example as @samp{$music}.
167 If your function returns a music expression, it is given a useful value
172 Some type predicates are specially handled by the parser since it
173 can't recognize the arguments reliably otherwise. Currently these are
174 @code{ly:pitch?} and @code{ly:duration?}.
176 Suitability of arguments for all other predicates is determined by
177 actually calling the predicate after LilyPond has already converted them
178 into a Scheme expression. As a consequence, the argument can be
179 specified in Scheme syntax if desired (introduced with @code{#} or as
180 the result of calling a scheme function), but LilyPond will also convert
181 a number of LilyPond constructs into Scheme before actually checking the
182 predicate on them. Currently, those include music, postevents, simple
183 strings (with or without quotes), numbers, full markups and markup
184 lists, score, book, bookpart, context definition and output definition
187 For some kinds of expression (like most music not enclosed in braces)
188 LilyPond needs to look further than the expression itself in order to
189 determine its end. If such an expression were considered for an
190 optional argument by evaluating its predicate, LilyPond would not be
191 able to ``backup'' when it decides the expression does not fit the
192 parameter. So some forms of music might need to be enclosed in braces
193 to make them acceptable to LilyPond. There are also some other
194 ambiguities that LilyPond sorts out by checking with predicate
195 functions: is @samp{-3} a fingering postevent or a negative number? Is
196 @code{"a" 4} in lyric mode a string followed by a number, or a lyric
197 event of duration @code{4}? LilyPond decides by asking the predicates.
198 That means that you should avoid permissive predicates like
199 @code{scheme?} if you have a particular use in mind instead of a general
202 For a list of available predefined type predicates, see
203 @ruser{Predefined type predicates}.
208 @ruser{Predefined type predicates}.
211 @file{lily/music-scheme.cc},
215 @node Scheme function usage
216 @subsection Scheme function usage
218 Scheme functions can be called pretty much anywhere where a Scheme
219 expression starting with @code{#} can be written. You call a scheme
220 function by writing its name preceded by @code{\}, followed by its
221 arguments. Once an optional argument predicate does not match an
222 argument, LilyPond skips this and all following optional arguments,
223 replacing them with their specified default, and @q{backs up} the
224 argument that did not match to the place of the next mandatory argument.
225 Since the backed up argument needs to go somewhere, optional arguments
226 are not actually considered optional unless followed by a mandatory
229 There is one exception: if you write @code{\default} in the place of an
230 optional argument, this and all following optional arguments are skipped
231 and replaced by their default. This works even when no mandatory
232 argument follows since @code{\default} does not need to get backed up.
233 The @code{mark} and @code{key} commands make use of that trick to
234 provide their default behavior when just followed by @code{\default}.
236 Apart from places where a Scheme value is required, there are a few
237 places where @code{#} expressions are currently accepted and evaluated
238 for their side effects but otherwise ignored. Mostly those are the
239 places where an assignment would be acceptable as well.
241 Since it is a bad idea to return values that can be misinterpreted in
242 some context, you should use normal scheme functions only for those
243 cases where you always return a useful value, and use void scheme
244 functions (@pxref{Void scheme functions}) otherwise.
246 @node Void scheme functions
247 @subsection Void scheme functions
248 @funindex define-void-function
251 Sometimes a procedure is executed in order to perform an action rather
252 than return a value. Some programming languages (like C and Scheme) use
253 functions for either concept and just discard the returned value
254 (usually by allowing any expression to act as statement, ignoring the
255 result). This is clever but error-prone: most C compilers nowadays
256 offer warnings for various non-``void'' expressions being discarded.
257 For many functions executing an action, the Scheme standards declare the
258 return value to be unspecified. LilyPond's Scheme interpreter Guile has
259 a unique value @code{*unspecified*} that it usually (such when using
260 @code{set!} directly on a variable) but unfortunately not consistently
261 returns in such cases.
263 Defining a LilyPond function with @code{define-void-function} makes
264 sure that this special value (the only value satisfying the predicate
265 @code{void?}) will be returned.
269 #(define-void-function
272 (ly:set-option 'point-and-click #f))
274 \noPointAndClick % disable point and click
277 If you want to evaluate an expression only for its side-effect and
278 don't want any value it may return interpreted, you can do so by
279 prefixing it with @code{\void}:
282 \void #(hashq-set! some-table some-key some-value)
285 That way, you can be sure that LilyPond will not assign meaning to the
286 returned value regardless of where it encounters it. This will also
287 work for music functions such as @code{\displayMusic}.
289 @node Music functions
290 @section Music functions
292 @cindex music functions
294 @emph{Music functions} are Scheme procedures that can create music
295 expressions automatically, and can be used to greatly simplify the
299 * Music function definitions::
300 * Music function usage::
301 * Simple substitution functions::
302 * Intermediate substitution functions::
303 * Mathematics in functions::
304 * Functions without arguments::
305 * Void music functions::
309 @node Music function definitions
310 @subsection Music function definitions
311 @cindex defining music functions
312 @funindex define-music-function
314 The general form for defining music functions is:
318 #(define-music-function
319 (parser location @var{arg1} @var{arg2} @dots{})
320 (@var{type1?} @var{type2?} @dots{})
325 quite in analogy to @ref{Scheme function definitions}. More often than
326 not, @var{body} will be a @ref{LilyPond code blocks, LilyPond code block}.
328 For a list of available type predicates, see
329 @ruser{Predefined type predicates}.
334 @ruser{Predefined type predicates}.
337 @file{lily/music-scheme.cc},
342 @node Music function usage
343 @subsection Music function usage
344 Music functions may currently be used in several places. Depending on
345 where they are used, restrictions apply in order to be able to parse
346 them unambiguously. The result a music function returns must be
347 compatible with the context in which it is called.
351 At top level in a music expression. No restriction apply here.
354 As a post-event, explicitly started with a direction indicator (one of
355 @code{-}, @code{^}, @w{and @code{_}}). Note that returning a post-event
356 will be acceptable for music functions called as normal music, leading
357 to a result roughly equivalent to
362 In this case, you can't use an @emph{open} music expression as the last
363 argument, one that would end with a music expression able to accept
364 additional postevents.
367 As a chord constituent. The returned expression must be of
368 @code{rhythmic-event} type, most likely a @code{NoteEvent}.
372 The special rules for trailing arguments make it possible to write
373 polymorphic functions like @code{\tweak} that can be applied to
374 different constructs.
376 @node Simple substitution functions
377 @subsection Simple substitution functions
379 Simple substitution functions are music functions whose output
380 music expression is written in LilyPond format and contains
381 function arguments in the output expression. They are described
382 in @ruser{Substitution function examples}.
385 @node Intermediate substitution functions
386 @subsection Intermediate substitution functions
388 Intermediate substitution functions involve a mix of Scheme code
389 and LilyPond code in the music expression to be returned.
391 Some @code{\override} commands require an argument consisting of
392 a pair of numbers (called a @emph{cons cell} in Scheme).
394 The pair can be directly passed into the music function,
395 using a @code{pair?} variable:
399 #(define-music-function
400 (parser location beg-end)
403 \once \override Beam.positions = #beg-end
407 \manualBeam #'(3 . 6) c8 d e f
411 Alternatively, the numbers making up the pair can be
412 passed as separate arguments, and the Scheme code
413 used to create the pair can be included in the
416 @lilypond[quote,verbatim,ragged-right]
418 #(define-music-function
419 (parser location beg end)
422 \once \override Beam.positions = #(cons beg end)
426 \manualBeam #3 #6 c8 d e f
431 @cindex temporary overrides
432 @cindex overrides, temporary
433 @cindex properties, popping previous value
435 Properties are maintained conceptually using one stack per property
436 per grob per context. Music functions may need to override one or
437 several properties for the duration of the function, restoring them
438 to their previous value before exiting. However, normal overrides
439 pop and discard the top of the current property stack before
440 pushing to it, so the previous value of the property is lost when it
441 is overridden. When the previous value must be preserved, prefix the
442 @code{\override} command with @code{\temporary}, like this:
445 \temporary \override @dots{}
448 The use of @code{\temporary} causes the (usually set) @code{pop-first}
449 property in the override to be cleared, so the previous value is not
450 popped off the property stack before pushing the new value onto it.
451 When a subsequent @code{\revert} pops off the temporarily overriden
452 value, the previous value will re-emerge.
454 In other words, calling @code{\temporary \override} and @code{\revert}
455 in succession on the same property will have a net effect of zero.
456 Similarly, pairing @code{\temporary} and @code{\undo} on the same
457 music containing overrides will have a net effect of zero.
459 Here is an example of a music function which makes use of this.
460 The use of @code{\temporary} ensures the values of the
461 @code{cross-staff} and @code{style} properties are restored on exit
462 to whatever values they had when the @code{crossStaff} function was
463 called. Without @code{\temporary} the default values would have been
468 #(define-music-function (parser location notes) (ly:music?)
469 (_i "Create cross-staff stems")
471 \temporary \override Stem.cross-staff = #cross-staff-connect
472 \temporary \override Flag.style = #'no-flag
474 \revert Stem.cross-staff
480 @node Mathematics in functions
481 @subsection Mathematics in functions
483 Music functions can involve Scheme programming in
484 addition to simple substitution,
486 @lilypond[quote,verbatim,ragged-right]
488 #(define-music-function
489 (parser location mag)
492 \override Stem.length = #(* 7.0 mag)
493 \override NoteHead.font-size =
494 #(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
499 \revert NoteHead.font-size
504 \AltOn #1.5 c c \AltOff c2
509 This example may be rewritten to pass in music expressions,
511 @lilypond[quote,verbatim,ragged-right]
513 #(define-music-function
514 (parser location mag music)
517 \override Stem.length = #(* 7.0 mag)
518 \override NoteHead.font-size =
519 #(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
522 \revert NoteHead.font-size
526 c2 \withAlt #0.5 { c4 c }
527 \withAlt #1.5 { c c } c2
532 @node Functions without arguments
533 @subsection Functions without arguments
535 In most cases a function without arguments should be written
539 dolce = \markup@{ \italic \bold dolce @}
542 However, in rare cases it may be useful to create a music function
547 #(define-music-function
550 (if (eq? #t (ly:get-option 'display-bar-numbers))
551 #@{ \once \override Score.BarNumber.break-visibility = ##f #@}
555 To actually display bar numbers where this function is called,
556 invoke @command{lilypond} with
559 lilypond -d display-bar-numbers FILENAME.ly
563 @node Void music functions
564 @subsection Void music functions
566 A music function must return a music expression. If you want to
567 execute a function only for its side effect, you should use
568 @code{define-void-function}. But there may be cases where you
569 sometimes want to produce a music expression, and sometimes not (like
570 in the previous example). Returning a @code{void} music expression
571 via @code{#@{ #@}} will achieve that.
573 @node Event functions
574 @section Event functions
575 @funindex define-event-function
576 @cindex event functions
578 To use a music function in the place of an event, you need to write a
579 direction indicator before it. But sometimes, this does not quite match
580 the syntax of constructs you want to replace. For example, if you want
581 to write dynamics commands, those are usually attached without direction
582 indicator, like @code{c'\pp}. Here is a way to write arbitrary
584 @lilypond[quote,verbatim,ragged-right]
585 dyn=#(define-event-function (parser location arg) (markup?)
586 (make-dynamic-script arg))
587 \relative c' { c\dyn pfsss }
589 You could do the same using a music function, but then you always would
590 have to write a direction indicator before calling it, like
591 @code{@w{c-\dyn pfsss}}.
594 @node Markup functions
595 @section Markup functions
597 Markups are implemented as special Scheme functions which produce a
598 @code{Stencil} object given a number of arguments.
601 * Markup construction in Scheme::
602 * How markups work internally::
603 * New markup command definition::
604 * New markup list command definition::
608 @node Markup construction in Scheme
609 @subsection Markup construction in Scheme
611 @cindex defining markup commands
613 The @code{markup} macro builds markup expressions in Scheme while
614 providing a LilyPond-like syntax. For example,
616 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
617 #:larger #:line ("foo" "bar" "baz")))
623 #@{ \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
624 \larger \line @{ foo bar baz @} @} #@}
628 This example demonstrates the main translation rules between regular
629 LilyPond markup syntax and Scheme markup syntax. Using @code{#@{
630 @dots{} #@}} for entering in LilyPond syntax will often be most
631 convenient, but we explain how to use the @code{markup} macro to get a
632 Scheme-only solution.
635 @multitable @columnfractions .3 .3
636 @item @b{LilyPond} @tab @b{Scheme}
637 @item @code{\markup markup1} @tab @code{(markup markup1)}
638 @item @code{\markup @{ markup1 markup2 @dots{} @}} @tab
639 @code{(markup markup1 markup2 @dots{} )}
640 @item @code{\markup-command} @tab @code{#:markup-command}
641 @item @code{\variable} @tab @code{variable}
642 @item @code{\center-column @{ @dots{} @}} @tab
643 @code{#:center-column ( @dots{} )}
644 @item @code{string} @tab @code{"string"}
645 @item @code{#scheme-arg} @tab @code{scheme-arg}
649 The whole Scheme language is accessible inside the
650 @code{markup} macro. For example, You may use function calls inside
651 @code{markup} in order to manipulate character strings. This is
652 useful when defining new markup commands (see
653 @ref{New markup command definition}).
658 The markup-list argument of commands such as @code{#:line},
659 @code{#:center}, and @code{#:column} cannot be a variable or
660 the result of a function call.
663 (markup #:line (function-that-returns-markups))
667 is invalid. One should use the @code{make-line-markup},
668 @code{make-center-markup}, or @code{make-column-markup} functions
672 (markup (make-line-markup (function-that-returns-markups)))
676 @node How markups work internally
677 @subsection How markups work internally
682 \raise #0.5 "text example"
686 @code{\raise} is actually represented by the @code{raise-markup}
687 function. The markup expression is stored as
690 (list raise-markup 0.5 (list simple-markup "text example"))
693 When the markup is converted to printable objects (Stencils), the
694 @code{raise-markup} function is called as
699 @var{list of property alists}
701 @var{the "text example" markup})
704 The @code{raise-markup} function first creates the stencil for the
705 @code{text example} string, and then it raises that Stencil by 0.5
706 staff space. This is a rather simple example; more complex examples
708 of this section, and in @file{scm/define-markup-commands.scm}.
711 @node New markup command definition
712 @subsection New markup command definition
714 This section discusses the definition of new markup commands.
717 * Markup command definition syntax::
719 * A complete example::
720 * Adapting builtin commands::
723 @node Markup command definition syntax
724 @unnumberedsubsubsec Markup command definition syntax
726 New markup commands can be defined using the
727 @code{define-markup-command} Scheme macro, at top-level.
730 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} @dots{})
731 (@var{arg1-type?} @var{arg2-type?} @dots{})
732 [ #:properties ((@var{property1} @var{default-value1})
734 @dots{}command body@dots{})
740 @item @var{command-name}
741 the markup command name
743 the @q{layout} definition.
745 a list of associative lists, containing all active properties.
747 @var{i}th command argument
748 @item @var{argi-type?}
749 a type predicate for the i@var{th} argument
752 If the command uses properties from the @code{props} arguments,
753 the @code{#:properties} keyword can be used to specify which
754 properties are used along with their default values.
756 Arguments are distinguished according to their type:
758 @item a markup, corresponding to type predicate @code{markup?};
759 @item a list of markups, corresponding to type predicate
761 @item any other scheme object, corresponding to type predicates such as
762 @code{list?}, @code{number?}, @code{boolean?}, etc.
765 There is no limitation on the order of arguments (after the
766 standard @code{layout} and @code{props} arguments). However,
767 markup functions taking a markup as their last argument are
768 somewhat special as you can apply them to a markup list, and the
769 result is a markup list where the markup function (with the
770 specified leading arguments) has been applied to every element of
771 the original markup list.
773 Since replicating the leading arguments for applying a markup
774 function to a markup list is cheap mostly for Scheme arguments,
775 you avoid performance pitfalls by just using Scheme arguments for
776 the leading arguments of markup functions that take a markup as
780 @unnumberedsubsubsec On properties
782 The @code{layout} and @code{props} arguments of markup commands bring a
783 context for the markup interpretation: font size, line width, etc.
785 The @code{layout} argument allows access to properties defined in
786 @code{paper} blocks, using the @code{ly:output-def-lookup} function.
787 For instance, the line width (the same as the one used in scores) is
791 (ly:output-def-lookup layout 'line-width)
794 The @code{props} argument makes some properties accessible to markup
795 commands. For instance, when a book title markup is interpreted, all
796 the variables defined in the @code{\header} block are automatically
797 added to @code{props}, so that the book title markup can access the book
798 title, composer, etc. It is also a way to configure the behaviour of a
799 markup command: for example, when a command uses font size during
800 processing, the font size is read from @code{props} rather than having a
801 @code{font-size} argument. The caller of a markup command may change
802 the value of the font size property in order to change the behaviour.
803 Use the @code{#:properties} keyword of @code{define-markup-command} to
804 specify which properties shall be read from the @code{props} arguments.
806 The example in next section illustrates how to access and override
807 properties in a markup command.
809 @node A complete example
810 @unnumberedsubsubsec A complete example
812 The following example defines a markup command to draw a double box
813 around a piece of text.
815 Firstly, we need to build an approximative result using markups.
816 Consulting the @ruser{Text markup commands} shows us the @code{\box}
819 @lilypond[quote,verbatim,ragged-right]
820 \markup \box \box HELLO
823 Now, we consider that more padding between the text and the boxes is
824 preferable. According to the @code{\box} documentation, this command
825 uses a @code{box-padding} property, which defaults to 0.2. The
826 documentation also mentions how to override it:
828 @lilypond[quote,verbatim,ragged-right]
829 \markup \box \override #'(box-padding . 0.6) \box A
832 Then, the padding between the two boxes is considered too small, so we
835 @lilypond[quote,verbatim,ragged-right]
836 \markup \override #'(box-padding . 0.4) \box
837 \override #'(box-padding . 0.6) \box A
840 Repeating this lengthy markup would be painful. This is where a markup
841 command is needed. Thus, we write a @code{double-box} markup command,
842 taking one argument (the text). This draws the two boxes, with some
846 #(define-markup-command (double-box layout props text) (markup?)
847 "Draw a double box around text."
848 (interpret-markup layout props
849 #@{\markup \override #'(box-padding . 0.4) \box
850 \override #'(box-padding . 0.6) \box @{ #text @}#@}))
856 #(define-markup-command (double-box layout props text) (markup?)
857 "Draw a double box around text."
858 (interpret-markup layout props
859 (markup #:override '(box-padding . 0.4) #:box
860 #:override '(box-padding . 0.6) #:box text)))
863 @code{text} is the name of the command argument, and @code{markup?} its
864 type: it identifies it as a markup. The @code{interpret-markup}
865 function is used in most of markup commands: it builds a stencil, using
866 @code{layout}, @code{props}, and a markup. In the second case, this
867 markup is built using the @code{markup} scheme macro, see @ref{Markup
868 construction in Scheme}. The transformation from @code{\markup}
869 expression to scheme markup expression is straight-forward.
871 The new command can be used as follow:
874 \markup \double-box A
877 It would be nice to make the @code{double-box} command customizable:
878 here, the @code{box-padding} values are hard coded, and cannot be
879 changed by the user. Also, it would be better to distinguish the
880 padding between the two boxes, from the padding between the inner box
881 and the text. So we will introduce a new property,
882 @code{inter-box-padding}, for the padding between the two boxes. The
883 @code{box-padding} will be used for the inner padding. The new code is
887 #(define-markup-command (double-box layout props text) (markup?)
888 #:properties ((inter-box-padding 0.4)
890 "Draw a double box around text."
891 (interpret-markup layout props
892 #@{\markup \override #`(box-padding . ,inter-box-padding) \box
893 \override #`(box-padding . ,box-padding) \box
897 Again, the equivalent version using the markup macro would be:
900 #(define-markup-command (double-box layout props text) (markup?)
901 #:properties ((inter-box-padding 0.4)
903 "Draw a double box around text."
904 (interpret-markup layout props
905 (markup #:override `(box-padding . ,inter-box-padding) #:box
906 #:override `(box-padding . ,box-padding) #:box text)))
909 Here, the @code{#:properties} keyword is used so that the
910 @code{inter-box-padding} and @code{box-padding} properties are read from
911 the @code{props} argument, and default values are given to them if the
912 properties are not defined.
914 Then, these values are used to override the @code{box-padding}
915 properties used by the two @code{\box} commands. Note the backquote and
916 the comma in the @code{\override} argument: they allow you to introduce
917 a variable value into a literal expression.
919 Now, the command can be used in a markup, and the boxes padding be
922 @lilypond[quote,verbatim,ragged-right]
923 #(define-markup-command (double-box layout props text) (markup?)
924 #:properties ((inter-box-padding 0.4)
926 "Draw a double box around text."
927 (interpret-markup layout props
928 #{\markup \override #`(box-padding . ,inter-box-padding) \box
929 \override #`(box-padding . ,box-padding) \box
932 \markup \double-box A
933 \markup \override #'(inter-box-padding . 0.8) \double-box A
934 \markup \override #'(box-padding . 1.0) \double-box A
937 @node Adapting builtin commands
938 @unnumberedsubsubsec Adapting builtin commands
940 A good way to start writing a new markup command, is to take example on
941 a builtin one. Most of the markup commands provided with LilyPond can be
942 found in file @file{scm/define-markup-commands.scm}.
944 For instance, we would like to adapt the @code{\draw-line} command, to
945 draw a double line instead. The @code{\draw-line} command is defined as
946 follow (documentation stripped):
949 (define-markup-command (draw-line layout props dest)
952 #:properties ((thickness 1))
953 "@dots{}documentation@dots{}"
954 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
958 (make-line-stencil th 0 0 x y)))
961 To define a new command based on an existing one, copy the definition,
962 and change the command name. The @code{#:category} keyword can be
963 safely removed, as it is only used for generating LilyPond
964 documentation, and is of no use for user-defined markup commands.
967 (define-markup-command (draw-double-line layout props dest)
969 #:properties ((thickness 1))
970 "@dots{}documentation@dots{}"
971 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
975 (make-line-stencil th 0 0 x y)))
978 Then, a property for setting the gap between two lines is added, called
979 @code{line-gap}, defaulting e.g. to 0.6:
982 (define-markup-command (draw-double-line layout props dest)
984 #:properties ((thickness 1)
986 "@dots{}documentation@dots{}"
990 Finally, the code for drawing two lines is added. Two calls to
991 @code{make-line-stencil} are used to draw the lines, and the resulting
992 stencils are combined using @code{ly:stencil-add}:
994 @lilypond[quote,verbatim,ragged-right]
995 #(define-markup-command (my-draw-line layout props dest)
997 #:properties ((thickness 1)
1000 (let* ((th (* (ly:output-def-lookup layout 'line-thickness)
1004 (w (/ line-gap 2.0))
1005 (x (cond ((= dx 0) w)
1007 (else (/ w (sqrt (+ 1 (* (/ dx dy) (/ dx dy))))))))
1008 (y (* (if (< (* dx dy) 0) 1 -1)
1011 (else (/ w (sqrt (+ 1 (* (/ dy dx) (/ dy dx))))))))))
1012 (ly:stencil-add (make-line-stencil th x y (+ dx x) (+ dy y))
1013 (make-line-stencil th (- x) (- y) (- dx x) (- dy y)))))
1015 \markup \my-draw-line #'(4 . 3)
1016 \markup \override #'(line-gap . 1.2) \my-draw-line #'(4 . 3)
1020 @node New markup list command definition
1021 @subsection New markup list command definition
1022 Markup list commands are defined with the
1023 @code{define-markup-list-command} Scheme macro, which is similar to the
1024 @code{define-markup-command} macro described in
1025 @ref{New markup command definition}, except that where the latter returns
1026 a single stencil, the former returns a list of stencils.
1028 In the following example, a @code{\paragraph} markup list command is
1029 defined, which returns a list of justified lines, the first one being
1030 indented. The indent width is taken from the @code{props} argument.
1033 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1034 #:properties ((par-indent 2))
1035 (interpret-markup-list layout props
1036 #@{\markuplist \justified-lines @{ \hspace #par-indent #args @} #@}))
1040 The version using just Scheme is more complex:
1042 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1043 #:properties ((par-indent 2))
1044 (interpret-markup-list layout props
1045 (make-justified-lines-markup-list (cons (make-hspace-markup par-indent)
1049 Besides the usual @code{layout} and @code{props} arguments, the
1050 @code{paragraph} markup list command takes a markup list argument, named
1051 @code{args}. The predicate for markup lists is @code{markup-list?}.
1053 First, the function gets the indent width, a property here named
1054 @code{par-indent}, from the property list @code{props}. If the
1055 property is not found, the default value is @code{2}. Then, a
1056 list of justified lines is made using the built-in markup list command
1057 @code{\justified-lines}, which is related to the
1058 @code{make-justified-lines-markup-list} function. A
1059 horizontal space is added at the beginning using @code{\hspace} (or the
1060 @code{make-hspace-markup} function). Finally, the markup list is
1061 interpreted using the @code{interpret-markup-list} function.
1063 This new markup list command can be used as follows:
1067 The art of music typography is called \italic @{(plate) engraving.@}
1068 The term derives from the traditional process of music printing.
1069 Just a few decades ago, sheet music was made by cutting and stamping
1070 the music into a zinc or pewter plate in mirror image.
1072 \override-lines #'(par-indent . 4) \paragraph @{
1073 The plate would be inked, the depressions caused by the cutting
1074 and stamping would hold ink. An image was formed by pressing paper
1075 to the plate. The stamping and cutting was completely done by
1081 @node Contexts for programmers
1082 @section Contexts for programmers
1085 * Context evaluation::
1086 * Running a function on all layout objects::
1089 @node Context evaluation
1090 @subsection Context evaluation
1092 @cindex calling code during interpreting
1093 @funindex \applyContext
1095 Contexts can be modified during interpretation with Scheme code. The
1098 \applyContext @var{function}
1101 @code{@var{function}} should be a Scheme function that takes a
1102 single argument: the context in which the @code{\applyContext}
1103 command is being called. The following code will print the
1104 current bar number on the standard output during the compile:
1109 (format #t "\nWe were called in barnumber ~a.\n"
1110 (ly:context-property x 'currentBarNumber)))
1115 @node Running a function on all layout objects
1116 @subsection Running a function on all layout objects
1119 @cindex calling code on layout objects
1120 @funindex \applyOutput
1122 The most versatile way of tuning an object is @code{\applyOutput} which
1123 works by inserting an event into the specified context
1124 (@rinternals{ApplyOutputEvent}). Its syntax is
1126 \applyOutput @var{Context} @var{proc}
1130 where @code{@var{proc}} is a Scheme function, taking three arguments.
1132 When interpreted, the function @code{@var{proc}} is called for
1133 every layout object found in the context @code{@var{Context}} at
1134 the current time step, with the following arguments:
1136 @item the layout object itself,
1137 @item the context where the layout object was created, and
1138 @item the context where @code{\applyOutput} is processed.
1142 In addition, the cause of the layout object, i.e., the music
1143 expression or object that was responsible for creating it, is in the
1144 object property @code{cause}. For example, for a note head, this is a
1145 @rinternals{NoteHead} event, and for a stem object,
1146 this is a @rinternals{Stem} object.
1148 Here is a function to use for @code{\applyOutput}; it blanks
1149 note-heads on the center-line and next to it:
1151 @lilypond[quote,verbatim,ragged-right]
1152 #(define (blanker grob grob-origin context)
1153 (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1154 (< (abs (ly:grob-property grob 'staff-position)) 2))
1155 (set! (ly:grob-property grob 'transparent) #t)))
1158 a'4 e8 <<\applyOutput #'Voice #blanker a c d>> b2
1162 To have @var{function} interpreted at the @code{Score} or @code{Staff}
1163 level use these forms
1166 \applyOutput #'Score #@var{function}
1167 \applyOutput #'Staff #@var{function}
1171 @node Callback functions
1172 @section Callback functions
1174 Properties (like @code{thickness}, @code{direction}, etc.) can be
1175 set at fixed values with @code{\override}, e.g.
1178 \override Stem.thickness = #2.0
1181 Properties can also be set to a Scheme procedure,
1183 @lilypond[fragment,verbatim,quote,relative=2]
1184 \override Stem.thickness = #(lambda (grob)
1185 (if (= UP (ly:grob-property grob 'direction))
1192 In this case, the procedure is executed as soon as the value of the
1193 property is requested during the formatting process.
1195 Most of the typesetting engine is driven by such callbacks.
1196 Properties that typically use callbacks include
1200 The printing routine, that constructs a drawing for the symbol
1202 The routine that sets the horizontal position
1204 The routine that computes the width of an object
1207 The procedure always takes a single argument, being the grob.
1209 If routines with multiple arguments must be called, the current grob
1210 can be inserted with a grob closure. Here is a setting from
1211 @code{AccidentalSuggestion},
1215 ,(ly:make-simple-closure
1217 ,(ly:make-simple-closure
1218 (list ly:self-alignment-interface::centered-on-x-parent))
1219 ,(ly:make-simple-closure
1220 (list ly:self-alignment-interface::x-aligned-on-self)))))
1224 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
1225 @code{ly:self-alignment-interface::centered-on-x-parent} are called
1226 with the grob as argument. The results are added with the @code{+}
1227 function. To ensure that this addition is properly executed, the whole
1228 thing is enclosed in @code{ly:make-simple-closure}.
1230 In fact, using a single procedure as property value is equivalent to
1233 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
1237 The inner @code{ly:make-simple-closure} supplies the grob as argument
1238 to @var{proc}, the outer ensures that result of the function is
1239 returned, rather than the @code{simple-closure} object.
1241 From within a callback, the easiest method for evaluating a markup is
1242 to use grob-interpret-markup. For example:
1245 my-callback = #(lambda (grob)
1246 (grob-interpret-markup grob (markup "foo")))
1249 @node Inline Scheme code
1250 @section Inline Scheme code
1252 TODO: the example for this section is ill-chosen since
1254 F = -\tweak font-size #-3 -\flageolet
1256 (note the @samp{-} marking it as a post event) will actually work fine
1257 for the stated purpose. Until this section gets a rewrite, let's
1258 pretend we don't know.
1260 The main disadvantage of @code{\tweak} is its syntactical
1261 inflexibility. For example, the following produces a syntax error.
1264 F = \tweak font-size #-3 -\flageolet
1272 Using Scheme, this problem can be avoided. The route to the
1273 result is given in @ref{Adding articulation to notes (example)},
1274 especially how to use @code{\displayMusic} as a helping guide.
1277 F = #(let ((m (make-music 'ArticulationEvent
1278 'articulation-type "flageolet")))
1279 (set! (ly:music-property m 'tweaks)
1280 (acons 'font-size -3
1281 (ly:music-property m 'tweaks)))
1290 Here, the @code{tweaks} properties of the flageolet object
1291 @code{m} (created with @code{make-music}) are extracted with
1292 @code{ly:music-property}, a new key-value pair to change the
1293 font size is prepended to the property list with the
1294 @code{acons} Scheme function, and the result is finally
1295 written back with @code{set!}. The last element of the
1296 @code{let} block is the return value, @code{m} itself.
1300 @node Difficult tweaks
1301 @section Difficult tweaks
1303 There are a few classes of difficult adjustments.
1309 One type of difficult adjustment involves the appearance of
1310 spanner objects, such as slurs and ties. Usually, only one
1311 spanner object is created at a time, and it can be adjusted with
1312 the normal mechanism. However, occasionally a spanner crosses a
1313 line break. When this happens, the object is cloned. A separate
1314 object is created for every system in which the spanner appears.
1315 The new objects are clones of the original object and inherit all
1316 properties, including @code{\override}s.
1319 In other words, an @code{\override} always affects all pieces of a
1320 broken spanner. To change only one part of a spanner at a line break,
1321 it is necessary to hook into the formatting process. The
1322 @code{after-line-breaking} callback contains the Scheme procedure that
1323 is called after the line breaks have been determined and layout
1324 objects have been split over different systems.
1326 In the following example, we define a procedure
1327 @code{my-callback}. This procedure
1331 determines if the spanner has been split across line breaks
1333 if yes, retrieves all the split objects
1335 checks if this grob is the last of the split objects
1337 if yes, it sets @code{extra-offset}.
1340 This procedure is installed into @rinternals{Tie}, so the last part
1341 of the broken tie is repositioned.
1343 @lilypond[quote,verbatim,ragged-right]
1344 #(define (my-callback grob)
1346 ;; have we been split?
1347 (orig (ly:grob-original grob))
1349 ;; if yes, get the split pieces (our siblings)
1350 (siblings (if (ly:grob? orig)
1351 (ly:spanner-broken-into orig)
1354 (if (and (>= (length siblings) 2)
1355 (eq? (car (last-pair siblings)) grob))
1356 (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1359 \override Tie.after-line-breaking =
1367 When applying this trick, the new @code{after-line-breaking} callback
1368 should also call the old one, if such a default exists. For example,
1369 if using this with @code{Hairpin}, @code{ly:spanner::kill-zero-spanned-time}
1370 should also be called.
1374 Some objects cannot be changed with @code{\override} for
1375 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1376 and @code{PaperColumn}. They can be changed with the
1377 @code{\overrideProperty} function, which works similar to @code{\once
1378 \override}, but uses a different syntax.
1382 Score.NonMusicalPaperColumn % Grob name
1383 . line-break-system-details % Property name
1384 . next-padding % Optional subproperty name
1388 Note, however, that @code{\override}, applied to
1389 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1390 expected within @code{\context} blocks.
1394 @node LilyPond Scheme interfaces
1395 @chapter LilyPond Scheme interfaces
1397 This chapter covers the various tools provided by LilyPond to help
1398 Scheme programmers get information into and out of the music streams.
1400 TODO -- figure out what goes in here and how to organize it