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 Lilypond code blocks look like
37 #@{ @var{Lilypond code} #@}
39 They can be used anywhere where you can write Scheme code: the Scheme
40 reader actually is changed for accommodating LilyPond code blocks. When
41 the LilyPond code block is being read, it is parsed superficially and
42 replaced by a call to the LilyPond @code{parser} which is executed at
43 runtime to interpret the LilyPond code block.
45 The point of the superficial parsing is the interpretation of @code{$}
46 signs which can be used for splicing in expressions from the surrounding
47 lexical Scheme context (like @code{let} variables and function
48 parameters). @code{$} can be used in the following ways:
52 just passes a single @code{$} to the LilyPond parser.
54 will evaluate the Scheme form at runtime and splice its value as an
55 identifier @code{\form} into the LilyPond parser. Depending on the
56 value type, it may be interpreted as several different syntactic
59 will evaluate the Scheme form at runtime and splice its value as a
60 Scheme expression into the LilyPond parser.
62 Forms in Scheme expressions started with @code{#} are read and parsed
63 recursively for @code{$} signs. Those are treated as follows:
64 @item #@dots{}$@var{variable}
65 splices the value of the variable into the surrounding expression.
66 @item #@dots{}($ @var{form} @dots{})
67 splices the value of the form into the surrounding expression. As
68 opposed to a LilyPond level @code{$@var{form}}, you need to separate the
69 form with a blank, making @code{$} be recognizable as a separate Scheme
73 A LilyPond code block may contain anything that you can use on the right
74 side of an assignment. In addition, an empty LilyPond block corresponds
75 to a void music expression, and a LilyPond block containing multiple
76 music events gets turned into a sequential music expression.
78 @node Scheme functions
79 @section Scheme functions
80 @cindex Scheme functions (LilyPond syntax)
82 @emph{Scheme functions} are Scheme procedures that can create Scheme
83 expressions from input written in LilyPond syntax. They can be called
84 in pretty much all places where using @code{#} for specifying a value in
85 Scheme syntax is allowed. While Scheme has functions of its own, this
86 chapter is concerned with @emph{syntactic} functions, functions that
87 receive arguments specified in LilyPond syntax.
90 * Scheme function definitions::
91 * Scheme function usage::
92 * Void scheme functions::
95 @node Scheme function definitions
96 @subsection Scheme function definitions
97 @funindex define-scheme-function
99 The general form for defining scheme functions is:
103 #(define-scheme-function
104 (parser location @var{arg1} @var{arg2} @dots{})
105 (@var{type1?} @var{type2?} @dots{})
112 @multitable @columnfractions .33 .66
114 @tab needs to be literally @code{parser} in order to give LilyPond code
115 blocks (@code{#@{}@dots{}@code{#@}}) access to the parser.
117 @item @code{@var{argN}}
118 @tab @var{n}th argument
120 @item @code{@var{typeN?}}
121 @tab a Scheme @emph{type predicate} for which @code{@var{argN}}
122 must return @code{#t}. Some of these predicates are specially
123 recognized by the parser, see below. There is also a special form
124 @code{(@emph{predicate?} @emph{default})} for specifying optional
125 arguments. If the actual argument is missing when the function is being
126 called, the default value is substituted instead. Default values are
127 evaluated at definition time (including LilyPond code blocks!), so if
128 you need a default calculated at runtime, instead write a special value
129 you can easily recognize. If you write the predicate in parentheses but
130 don't follow it with a default value, @code{#f} is used as the default.
131 Default values are not verified with @emph{predicate?} at either
132 definition or run time: it is your responsibility to deal with the
133 values you specify. Default values that happen to be music expressions
134 are copied while setting @code{origin} to the @code{location} parameter.
136 @item @code{@var{body}}
137 @tab A sequence of Scheme forms evaluated in order, the last one being
138 used as the return value of the scheme function. It may contain
139 LilyPond code blocks enclosed in hashed braces
140 (@tie{}@w{@code{#@{@dots{}#@}}}@tie{}), like described in @ref{Lilypond
141 code blocks}. Within LilyPond code blocks, use @code{$} to reference
142 function arguments (eg., @samp{$arg1}) or to start an inline Scheme
143 expression containing function arguments (eg., @w{@samp{$(cons arg1
144 arg2)}}). If your function returns a music expression, it is cloned and
145 given the correct @code{origin}.
149 Some type predicates are specially handled by the parser since it
150 can't recognize the arguments reliably otherwise. Currently these are
151 @code{ly:music?}, @code{ly:pitch?}, and @code{ly:duration?}.
153 All other predicates can be specified in Scheme syntax if desired
154 (introduced with @code{#} or as the result of calling a scheme
155 function), but Lilypond will also accept a number of Lilypond
156 constructs it can recognize fully without lookahead. Currently, those
157 include simple strings (with or without quotes), context
158 modifications, full markups and markup lists.
160 If you really want to input one of the special items as a Scheme rather
161 than a LilyPond expression, you may write them as a Scheme expression
162 that calls @code{ly:export} at its outermost level.
164 For a list of available type predicates, see
165 @ruser{Predefined type predicates}.
170 @ruser{Predefined type predicates}.
173 @file{lily/music-scheme.cc},
177 @node Scheme function usage
178 @subsection Scheme function usage
180 Scheme functions can be called pretty much anywhere where a Scheme
181 expression starting with @code{#} can be written. You call a scheme
182 function by writing its name preceded by @code{\}, followed by its
183 arguments. The last argument can't be an optional argument. If there
184 are several optional arguments in a row, they are filled with values
185 left to right. Once an optional argument can't match input, it and all
186 immediately following optional arguments are replaced with their default
187 values, and the matching continues with the next non-optional argument.
189 Apart from places where a Scheme value is required, there are a few
190 places where @code{#} expressions are accepted and evaluated for their
191 side effects but otherwise ignored. Mostly those are the places where
192 an assignment would be acceptable as well.
194 There are a few special places where an argument matching
195 @code{ly:music?} has to be either a music identifier or a music
196 expression enclosed in @code{@{}@dots{}@code{@}} or
197 @code{<<}@dots{}@code{>>} explicitly, so that possibly following
198 optional durations or postevents can't be confused with additional
199 arguments. One obvious place is before a @code{ly:duration?}
200 predicate. Another is as the last argument of a scheme function when it
201 is used in a place where such optional parts could be considered either
202 part of the music argument or not.
204 In those rare cases, you have to delimit your music arguments
205 appropriately to spare LilyPond from getting confused.
207 @node Void scheme functions
208 @subsection Void scheme functions
209 @funindex define-void-function
211 Sometimes a procedure is executed in order to perform an action rather
212 than return a value. Some programming languages (like C and Scheme)
213 use functions for either concept and just discard the returned value
214 (usually by allowing any expression to act as statement, ignoring the
215 result). This is clever but error-prone: most C compilers nowadays
216 offer warnings for various non-``void'' expressions being discarded.
217 For many functions executing an action, the Scheme standards declare
218 the return value to be unspecified. Lilypond's Scheme interpreter
219 Guile has a unique ``unspecified'' value that it usually (such when
220 using @code{set!} directly on a variable) but unfortunately not
221 consistently returns in such cases.
223 Defining a Lilypond function with @code{define-void-function} makes
224 sure that this special value (the only value satisfying the predicate
225 @code{void?}) will be returned.
229 #(define-void-function
232 (ly:set-option 'point-and-click #f))
234 \noPointAndClick % disable point and click
237 @node Music functions
238 @section Music functions
240 @cindex music functions
242 @emph{Music functions} are Scheme procedures that can create music
243 expressions automatically, and can be used to greatly simplify the
247 * Music function definitions::
248 * Music function usage::
249 * Simple substitution functions::
250 * Intermediate substitution functions::
251 * Mathematics in functions::
252 * Functions without arguments::
253 * Void music functions::
257 @node Music function definitions
258 @subsection Music function definitions
259 @cindex defining music functions
260 @funindex define-music-function
262 The general form for defining music functions is:
266 #(define-music-function
267 (parser location @var{arg1} @var{arg2} @dots{})
268 (@var{type1?} @var{type2?} @dots{})
273 quite in analogy to @ref{Scheme function definitions}. More often than
274 not, @var{body} will be a @ref{Lilypond code blocks, Lilypond code block}.
276 For a list of available type predicates, see
277 @ruser{Predefined type predicates}.
282 @ruser{Predefined type predicates}.
285 @file{lily/music-scheme.cc},
290 @node Music function usage
291 @subsection Music function usage
292 Music functions may currently be used in three places. Depending on
293 where they are used, restrictions apply in order to be able to parse
294 them unambiguously. The result a music function returns must be
295 compatible with the context in which it is called.
299 At top level in a music expression. There are no special restrictions
300 on the argument list.
303 As a post-event, explicitly started with a direction indicator (one of
304 @code{-}, @code{^}, @w{and @code{_}}). All trailing arguments of the
305 music function with the predicate @code{ly:music?} will get parsed also
306 as post-events (if the last argument is a scheme function, this will
307 hold for trailing @code{ly:music?} arguments of the scheme function
308 instead). Note that returning a post-event will be acceptable for music
309 functions called as normal music, leading to a result roughly equivalent
316 As a chord constituent. All trailing arguments of the music function
317 with the predicate @code{ly:music?} will get parsed also as chord
322 The special rules for trailing arguments make it possible to write
323 polymorphic functions like @code{\tweak} that can be applied to
324 different constructs.
326 @node Simple substitution functions
327 @subsection Simple substitution functions
329 Simple substitution functions are music functions whose output
330 music expression is written in LilyPond format and contains
331 function arguments in the output expression. They are described
332 in @ruser{Substitution function examples}.
335 @node Intermediate substitution functions
336 @subsection Intermediate substitution functions
338 Intermediate substitution functions involve a mix of Scheme code
339 and LilyPond code in the music expression to be returned.
341 Some @code{\override} commands require an argument consisting of
342 a pair of numbers (called a @emph{cons cell} in Scheme).
344 The pair can be directly passed into the music function,
345 using a @code{pair?} variable:
349 #(define-music-function
350 (parser location beg-end)
353 \once \override Beam #'positions = $beg-end
357 \manualBeam #'(3 . 6) c8 d e f
361 Alternatively, the numbers making up the pair can be
362 passed as separate arguments, and the Scheme code
363 used to create the pair can be included in the
366 @lilypond[quote,verbatim,ragged-right]
368 #(define-music-function
369 (parser location beg end)
372 \once \override Beam #'positions = $(cons beg end)
376 \manualBeam #3 #6 c8 d e f
381 @node Mathematics in functions
382 @subsection Mathematics in functions
384 Music functions can involve Scheme programming in
385 addition to simple substitution,
387 @lilypond[quote,verbatim,ragged-right]
389 #(define-music-function
390 (parser location mag)
393 \override Stem #'length = $(* 7.0 mag)
394 \override NoteHead #'font-size =
395 $(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
399 \revert Stem #'length
400 \revert NoteHead #'font-size
405 \AltOn #1.5 c c \AltOff c2
410 This example may be rewritten to pass in music expressions,
412 @lilypond[quote,verbatim,ragged-right]
414 #(define-music-function
415 (parser location mag music)
418 \override Stem #'length = $(* 7.0 mag)
419 \override NoteHead #'font-size =
420 $(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
422 \revert Stem #'length
423 \revert NoteHead #'font-size
427 c2 \withAlt #0.5 { c4 c }
428 \withAlt #1.5 { c c } c2
433 @node Functions without arguments
434 @subsection Functions without arguments
436 In most cases a function without arguments should be written
440 dolce = \markup@{ \italic \bold dolce @}
443 However, in rare cases it may be useful to create a music function
448 #(define-music-function
451 (if (eq? #t (ly:get-option 'display-bar-numbers))
452 #@{ \once \override Score.BarNumber #'break-visibility = ##f #@}
456 To actually display bar numbers where this function is called,
457 invoke @command{lilypond} with
460 lilypond -d display-bar-numbers FILENAME.ly
464 @node Void music functions
465 @subsection Void music functions
467 A music function must return a music expression. If you want to
468 execute a function only for its side effect, you should use
469 @code{define-void-function}. But there may be cases where you
470 sometimes want to produce a music expression, and sometimes not (like
471 in the previous example). Returning a @code{void} music expression
472 via @code{#@{ #@}} will achieve that.
474 @node Event functions
475 @section Event functions
476 @funindex define-event-function
477 @cindex event functions
479 To use a music function in the place of an event, you need to write a
480 direction indicator before it. But sometimes, this does not quite match
481 the syntax of constructs you want to replace. For example, if you want
482 to write dynamics commands, those are usually attached without direction
483 indicator, like @code{c'\pp}. Here is a way to write arbitrary
485 @lilypond[quote,verbatim,raggedright]
486 dyn=#(define-event-function (parser location arg) (markup?)
487 (make-dynamic-script arg))
488 \relative c' { c\dyn pfsss }
490 You could do the same using a music function, but then you always would
491 have to write a direction indicator before calling it, like
492 @code{@w{c-\dyn pfsss}}.
495 @node Markup functions
496 @section Markup functions
498 Markups are implemented as special Scheme functions which produce a
499 @code{Stencil} object given a number of arguments.
502 * Markup construction in Scheme::
503 * How markups work internally::
504 * New markup command definition::
505 * New markup list command definition::
509 @node Markup construction in Scheme
510 @subsection Markup construction in Scheme
512 @cindex defining markup commands
514 The @code{markup} macro builds markup expressions in Scheme while
515 providing a LilyPond-like syntax. For example,
517 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
518 #:larger #:line ("foo" "bar" "baz")))
524 #@{ \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
525 \larger \line @{ foo bar baz @} @} #@}
529 This example demonstrates the main translation rules between regular
530 LilyPond markup syntax and Scheme markup syntax. Using @code{#@{
531 @dots{} #@}} for entering in LilyPond syntax will often be most
532 convenient, but we explain how to use the @code{markup} macro to get a
533 Scheme-only solution.
536 @multitable @columnfractions .3 .3
537 @item @b{LilyPond} @tab @b{Scheme}
538 @item @code{\markup markup1} @tab @code{(markup markup1)}
539 @item @code{\markup @{ markup1 markup2 ... @}} @tab
540 @code{(markup markup1 markup2 ... )}
541 @item @code{\markup-command} @tab @code{#:markup-command}
542 @item @code{\variable} @tab @code{variable}
543 @item @code{\center-column @{ ... @}} @tab @code{#:center-column ( ... )}
544 @item @code{string} @tab @code{"string"}
545 @item @code{#scheme-arg} @tab @code{scheme-arg}
549 The whole Scheme language is accessible inside the
550 @code{markup} macro. For example, You may use function calls inside
551 @code{markup} in order to manipulate character strings. This is
552 useful when defining new markup commands (see
553 @ref{New markup command definition}).
558 The markup-list argument of commands such as @code{#:line},
559 @code{#:center}, and @code{#:column} cannot be a variable or
560 the result of a function call.
563 (markup #:line (function-that-returns-markups))
567 is invalid. One should use the @code{make-line-markup},
568 @code{make-center-markup}, or @code{make-column-markup} functions
572 (markup (make-line-markup (function-that-returns-markups)))
576 @node How markups work internally
577 @subsection How markups work internally
582 \raise #0.5 "text example"
586 @code{\raise} is actually represented by the @code{raise-markup}
587 function. The markup expression is stored as
590 (list raise-markup 0.5 (list simple-markup "text example"))
593 When the markup is converted to printable objects (Stencils), the
594 @code{raise-markup} function is called as
599 @var{list of property alists}
601 @var{the "text example" markup})
604 The @code{raise-markup} function first creates the stencil for the
605 @code{text example} string, and then it raises that Stencil by 0.5
606 staff space. This is a rather simple example; more complex examples
608 of this section, and in @file{scm/define-markup-commands.scm}.
611 @node New markup command definition
612 @subsection New markup command definition
614 This section discusses the definition of new markup commands.
617 * Markup command definition syntax::
619 * A complete example::
620 * Adapting builtin commands::
623 @node Markup command definition syntax
624 @unnumberedsubsubsec Markup command definition syntax
626 New markup commands can be defined using the
627 @code{define-markup-command} Scheme macro, at top-level.
630 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
631 (@var{arg1-type?} @var{arg2-type?} ...)
632 [ #:properties ((@var{property1} @var{default-value1})
640 @item @var{command-name}
641 the markup command name
643 the @q{layout} definition.
645 a list of associative lists, containing all active properties.
647 @var{i}th command argument
648 @item @var{argi-type?}
649 a type predicate for the i@var{th} argument
652 If the command uses properties from the @code{props} arguments,
653 the @code{#:properties} keyword can be used to specify which
654 properties are used along with their default values.
656 Arguments are distinguished according to their type:
658 @item a markup, corresponding to type predicate @code{markup?};
659 @item a list of markups, corresponding to type predicate
661 @item any other scheme object, corresponding to type predicates such as
662 @code{list?}, @code{number?}, @code{boolean?}, etc.
665 There is no limitation on the order of arguments (after the
666 standard @code{layout} and @code{props} arguments). However,
667 markup functions taking a markup as their last argument are
668 somewhat special as you can apply them to a markup list, and the
669 result is a markup list where the markup function (with the
670 specified leading arguments) has been applied to every element of
671 the original markup list.
673 Since replicating the leading arguments for applying a markup
674 function to a markup list is cheap mostly for Scheme arguments,
675 you avoid performance pitfalls by just using Scheme arguments for
676 the leading arguments of markup functions that take a markup as
680 @unnumberedsubsubsec On properties
682 The @code{layout} and @code{props} arguments of markup commands bring a
683 context for the markup interpretation: font size, line width, etc.
685 The @code{layout} argument allows access to properties defined in
686 @code{paper} blocks, using the @code{ly:output-def-lookup} function.
687 For instance, the line width (the same as the one used in scores) is
691 (ly:output-def-lookup layout 'line-width)
694 The @code{props} argument makes some properties accessible to markup
695 commands. For instance, when a book title markup is interpreted, all
696 the variables defined in the @code{\header} block are automatically
697 added to @code{props}, so that the book title markup can access the book
698 title, composer, etc. It is also a way to configure the behaviour of a
699 markup command: for example, when a command uses font size during
700 processing, the font size is read from @code{props} rather than having a
701 @code{font-size} argument. The caller of a markup command may change
702 the value of the font size property in order to change the behaviour.
703 Use the @code{#:properties} keyword of @code{define-markup-command} to
704 specify which properties shall be read from the @code{props} arguments.
706 The example in next section illustrates how to access and override
707 properties in a markup command.
709 @node A complete example
710 @unnumberedsubsubsec A complete example
712 The following example defines a markup command to draw a double box
713 around a piece of text.
715 Firstly, we need to build an approximative result using markups.
716 Consulting the @ruser{Text markup commands} shows us the @code{\box}
719 @lilypond[quote,verbatim,ragged-right]
720 \markup \box \box HELLO
723 Now, we consider that more padding between the text and the boxes is
724 preferable. According to the @code{\box} documentation, this command
725 uses a @code{box-padding} property, which defaults to 0.2. The
726 documentation also mentions how to override it:
728 @lilypond[quote,verbatim,ragged-right]
729 \markup \box \override #'(box-padding . 0.6) \box A
732 Then, the padding between the two boxes is considered too small, so we
735 @lilypond[quote,verbatim,ragged-right]
736 \markup \override #'(box-padding . 0.4) \box
737 \override #'(box-padding . 0.6) \box A
740 Repeating this lengthy markup would be painful. This is where a markup
741 command is needed. Thus, we write a @code{double-box} markup command,
742 taking one argument (the text). This draws the two boxes, with some
746 #(define-markup-command (double-box layout props text) (markup?)
747 "Draw a double box around text."
748 (interpret-markup layout props
749 #@{\markup \override #'(box-padding . 0.4) \box
750 \override #'(box-padding . 0.6) \box @{ $text @}#@}))
756 #(define-markup-command (double-box layout props text) (markup?)
757 "Draw a double box around text."
758 (interpret-markup layout props
759 (markup #:override '(box-padding . 0.4) #:box
760 #:override '(box-padding . 0.6) #:box text)))
763 @code{text} is the name of the command argument, and @code{markup?} its
764 type: it identifies it as a markup. The @code{interpret-markup}
765 function is used in most of markup commands: it builds a stencil, using
766 @code{layout}, @code{props}, and a markup. In the second case, this
767 markup is built using the @code{markup} scheme macro, see @ref{Markup
768 construction in Scheme}. The transformation from @code{\markup}
769 expression to scheme markup expression is straight-forward.
771 The new command can be used as follow:
774 \markup \double-box A
777 It would be nice to make the @code{double-box} command customizable:
778 here, the @code{box-padding} values are hard coded, and cannot be
779 changed by the user. Also, it would be better to distinguish the
780 padding between the two boxes, from the padding between the inner box
781 and the text. So we will introduce a new property,
782 @code{inter-box-padding}, for the padding between the two boxes. The
783 @code{box-padding} will be used for the inner padding. The new code is
787 #(define-markup-command (double-box layout props text) (markup?)
788 #:properties ((inter-box-padding 0.4)
790 "Draw a double box around text."
791 (interpret-markup layout props
792 #@{\markup \override #`(box-padding . ,$inter-box-padding) \box
793 \override #`(box-padding . ,$box-padding) \box
797 Again, the equivalent version using the markup macro would be:
800 #(define-markup-command (double-box layout props text) (markup?)
801 #:properties ((inter-box-padding 0.4)
803 "Draw a double box around text."
804 (interpret-markup layout props
805 (markup #:override `(box-padding . ,inter-box-padding) #:box
806 #:override `(box-padding . ,box-padding) #:box text)))
809 Here, the @code{#:properties} keyword is used so that the
810 @code{inter-box-padding} and @code{box-padding} properties are read from
811 the @code{props} argument, and default values are given to them if the
812 properties are not defined.
814 Then, these values are used to override the @code{box-padding}
815 properties used by the two @code{\box} commands. Note the backquote and
816 the comma in the @code{\override} argument: they allow you to introduce
817 a variable value into a literal expression.
819 Now, the command can be used in a markup, and the boxes padding be
822 @lilypond[quote,verbatim,ragged-right]
823 #(define-markup-command (double-box layout props text) (markup?)
824 #:properties ((inter-box-padding 0.4)
826 "Draw a double box around text."
827 (interpret-markup layout props
828 #{\markup \override #`(box-padding . ,$inter-box-padding) \box
829 \override #`(box-padding . ,$box-padding) \box
832 \markup \double-box A
833 \markup \override #'(inter-box-padding . 0.8) \double-box A
834 \markup \override #'(box-padding . 1.0) \double-box A
837 @node Adapting builtin commands
838 @unnumberedsubsubsec Adapting builtin commands
840 A good way to start writing a new markup command, is to take example on
841 a builtin one. Most of the markup commands provided with LilyPond can be
842 found in file @file{scm/define-markup-commands.scm}.
844 For instance, we would like to adapt the @code{\draw-line} command, to
845 draw a double line instead. The @code{\draw-line} command is defined as
846 follow (documentation stripped):
849 (define-markup-command (draw-line layout props dest)
852 #:properties ((thickness 1))
854 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
858 (make-line-stencil th 0 0 x y)))
861 To define a new command based on an existing one, copy the definition,
862 and change the command name. The @code{#:category} keyword can be
863 safely removed, as it is only used for generating LilyPond
864 documentation, and is of no use for user-defined markup commands.
867 (define-markup-command (draw-double-line layout props dest)
869 #:properties ((thickness 1))
871 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
875 (make-line-stencil th 0 0 x y)))
878 Then, a property for setting the gap between two lines is added, called
879 @code{line-gap}, defaulting e.g. to 0.6:
882 (define-markup-command (draw-double-line layout props dest)
884 #:properties ((thickness 1)
890 Finally, the code for drawing two lines is added. Two calls to
891 @code{make-line-stencil} are used to draw the lines, and the resulting
892 stencils are combined using @code{ly:stencil-add}:
894 @lilypond[quote,verbatim,ragged-right]
895 #(define-markup-command (my-draw-line layout props dest)
897 #:properties ((thickness 1)
900 (let* ((th (* (ly:output-def-lookup layout 'line-thickness)
905 (x (cond ((= dx 0) w)
907 (else (/ w (sqrt (+ 1 (* (/ dx dy) (/ dx dy))))))))
908 (y (* (if (< (* dx dy) 0) 1 -1)
911 (else (/ w (sqrt (+ 1 (* (/ dy dx) (/ dy dx))))))))))
912 (ly:stencil-add (make-line-stencil th x y (+ dx x) (+ dy y))
913 (make-line-stencil th (- x) (- y) (- dx x) (- dy y)))))
915 \markup \my-draw-line #'(4 . 3)
916 \markup \override #'(line-gap . 1.2) \my-draw-line #'(4 . 3)
920 @node New markup list command definition
921 @subsection New markup list command definition
922 Markup list commands are defined with the
923 @code{define-markup-list-command} Scheme macro, which is similar to the
924 @code{define-markup-command} macro described in
925 @ref{New markup command definition}, except that where the latter returns
926 a single stencil, the former returns a list of stencils.
928 In the following example, a @code{\paragraph} markup list command is
929 defined, which returns a list of justified lines, the first one being
930 indented. The indent width is taken from the @code{props} argument.
933 #(define-markup-list-command (paragraph layout props args) (markup-list?)
934 #:properties ((par-indent 2))
935 (interpret-markup-list layout props
936 #@{\markuplines \justified-lines @{ \hspace #$par-indent $args @} #@}))
940 The version using just Scheme is more complex:
942 #(define-markup-list-command (paragraph layout props args) (markup-list?)
943 #:properties ((par-indent 2))
944 (interpret-markup-list layout props
945 (make-justified-lines-markup-list (cons (make-hspace-markup par-indent)
949 Besides the usual @code{layout} and @code{props} arguments, the
950 @code{paragraph} markup list command takes a markup list argument, named
951 @code{args}. The predicate for markup lists is @code{markup-list?}.
953 First, the function gets the indent width, a property here named
954 @code{par-indent}, from the property list @code{props}. If the
955 property is not found, the default value is @code{2}. Then, a
956 list of justified lines is made using the built-in markup list command
957 @code{\justified-lines}, which is related to the
958 @code{make-justified-lines-markup-list} function. A
959 horizontal space is added at the beginning using @code{\hspace} (or the
960 @code{make-hspace-markup} function). Finally, the markup list is
961 interpreted using the @code{interpret-markup-list} function.
963 This new markup list command can be used as follows:
967 The art of music typography is called \italic @{(plate) engraving.@}
968 The term derives from the traditional process of music printing.
969 Just a few decades ago, sheet music was made by cutting and stamping
970 the music into a zinc or pewter plate in mirror image.
972 \override-lines #'(par-indent . 4) \paragraph @{
973 The plate would be inked, the depressions caused by the cutting
974 and stamping would hold ink. An image was formed by pressing paper
975 to the plate. The stamping and cutting was completely done by
981 @node Contexts for programmers
982 @section Contexts for programmers
985 * Context evaluation::
986 * Running a function on all layout objects::
989 @node Context evaluation
990 @subsection Context evaluation
992 @cindex calling code during interpreting
993 @funindex \applyContext
995 Contexts can be modified during interpretation with Scheme code. The
998 \applyContext @var{function}
1001 @code{@var{function}} should be a Scheme function that takes a
1002 single argument: the context in which the @code{\applyContext}
1003 command is being called. The following code will print the
1004 current bar number on the standard output during the compile:
1009 (format #t "\nWe were called in barnumber ~a.\n"
1010 (ly:context-property x 'currentBarNumber)))
1015 @node Running a function on all layout objects
1016 @subsection Running a function on all layout objects
1019 @cindex calling code on layout objects
1020 @funindex \applyOutput
1023 The most versatile way of tuning an object is @code{\applyOutput} which
1024 works by inserting an event into the specified context
1025 (@rinternals{ApplyOutputEvent}). Its syntax is
1027 \applyOutput @var{context} @var{proc}
1031 where @code{@var{proc}} is a Scheme function, taking three arguments.
1033 When interpreted, the function @code{@var{proc}} is called for
1034 every layout object found in the context @code{@var{context}} at
1035 the current time step, with the following arguments:
1037 @item the layout object itself,
1038 @item the context where the layout object was created, and
1039 @item the context where @code{\applyOutput} is processed.
1043 In addition, the cause of the layout object, i.e., the music
1044 expression or object that was responsible for creating it, is in the
1045 object property @code{cause}. For example, for a note head, this is a
1046 @rinternals{NoteHead} event, and for a stem object,
1047 this is a @rinternals{Stem} object.
1049 Here is a function to use for @code{\applyOutput}; it blanks
1050 note-heads on the center-line and next to it:
1052 @lilypond[quote,verbatim,ragged-right]
1053 #(define (blanker grob grob-origin context)
1054 (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1055 (< (abs (ly:grob-property grob 'staff-position)) 2))
1056 (set! (ly:grob-property grob 'transparent) #t)))
1059 a'4 e8 <<\applyOutput #'Voice #blanker a c d>> b2
1064 @node Callback functions
1065 @section Callback functions
1067 Properties (like @code{thickness}, @code{direction}, etc.) can be
1068 set at fixed values with @code{\override}, e.g.
1071 \override Stem #'thickness = #2.0
1074 Properties can also be set to a Scheme procedure,
1076 @lilypond[fragment,verbatim,quote,relative=2]
1077 \override Stem #'thickness = #(lambda (grob)
1078 (if (= UP (ly:grob-property grob 'direction))
1085 In this case, the procedure is executed as soon as the value of the
1086 property is requested during the formatting process.
1088 Most of the typesetting engine is driven by such callbacks.
1089 Properties that typically use callbacks include
1093 The printing routine, that constructs a drawing for the symbol
1095 The routine that sets the horizontal position
1097 The routine that computes the width of an object
1100 The procedure always takes a single argument, being the grob.
1102 If routines with multiple arguments must be called, the current grob
1103 can be inserted with a grob closure. Here is a setting from
1104 @code{AccidentalSuggestion},
1108 ,(ly:make-simple-closure
1110 ,(ly:make-simple-closure
1111 (list ly:self-alignment-interface::centered-on-x-parent))
1112 ,(ly:make-simple-closure
1113 (list ly:self-alignment-interface::x-aligned-on-self)))))
1117 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
1118 @code{ly:self-alignment-interface::centered-on-x-parent} are called
1119 with the grob as argument. The results are added with the @code{+}
1120 function. To ensure that this addition is properly executed, the whole
1121 thing is enclosed in @code{ly:make-simple-closure}.
1123 In fact, using a single procedure as property value is equivalent to
1126 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
1130 The inner @code{ly:make-simple-closure} supplies the grob as argument
1131 to @var{proc}, the outer ensures that result of the function is
1132 returned, rather than the @code{simple-closure} object.
1134 From within a callback, the easiest method for evaluating a markup is
1135 to use grob-interpret-markup. For example:
1138 my-callback = #(lambda (grob)
1139 (grob-interpret-markup grob (markup "foo")))
1142 @node Inline Scheme code
1143 @section Inline Scheme code
1145 The main disadvantage of @code{\tweak} is its syntactical
1146 inflexibility. For example, the following produces a syntax error.
1149 F = \tweak #'font-size #-3 -\flageolet
1157 In other words, @code{\tweak} doesn't behave like an articulation
1158 regarding the syntax; in particular, it can't be attached with
1159 @code{^} and @code{_}.
1161 Using Scheme, this problem can be avoided. The route to the
1162 result is given in @ref{Adding articulation to notes (example)},
1163 especially how to use @code{\displayMusic} as a helping guide.
1166 F = #(let ((m (make-music 'ArticulationEvent
1167 'articulation-type "flageolet")))
1168 (set! (ly:music-property m 'tweaks)
1169 (acons 'font-size -3
1170 (ly:music-property m 'tweaks)))
1179 Here, the @code{tweaks} properties of the flageolet object
1180 @code{m} (created with @code{make-music}) are extracted with
1181 @code{ly:music-property}, a new key-value pair to change the
1182 font size is prepended to the property list with the
1183 @code{acons} Scheme function, and the result is finally
1184 written back with @code{set!}. The last element of the
1185 @code{let} block is the return value, @code{m} itself.
1189 @node Difficult tweaks
1190 @section Difficult tweaks
1192 There are a few classes of difficult adjustments.
1198 One type of difficult adjustment involves the appearance of
1199 spanner objects, such as slurs and ties. Usually, only one
1200 spanner object is created at a time, and it can be adjusted with
1201 the normal mechanism. However, occasionally a spanner crosses a
1202 line break. When this happens, the object is cloned. A separate
1203 object is created for every system in which the spanner appears.
1204 The new objects are clones of the original object and inherit all
1205 properties, including @code{\override}s.
1208 In other words, an @code{\override} always affects all pieces of a
1209 broken spanner. To change only one part of a spanner at a line break,
1210 it is necessary to hook into the formatting process. The
1211 @code{after-line-breaking} callback contains the Scheme procedure that
1212 is called after the line breaks have been determined and layout
1213 objects have been split over different systems.
1215 In the following example, we define a procedure
1216 @code{my-callback}. This procedure
1220 determines if the spanner has been split across line breaks
1222 if yes, retrieves all the split objects
1224 checks if this grob is the last of the split objects
1226 if yes, it sets @code{extra-offset}.
1229 This procedure is installed into @rinternals{Tie}, so the last part
1230 of the broken tie is repositioned.
1232 @lilypond[quote,verbatim,ragged-right]
1233 #(define (my-callback grob)
1235 ;; have we been split?
1236 (orig (ly:grob-original grob))
1238 ;; if yes, get the split pieces (our siblings)
1239 (siblings (if (ly:grob? orig)
1240 (ly:spanner-broken-into orig)
1243 (if (and (>= (length siblings) 2)
1244 (eq? (car (last-pair siblings)) grob))
1245 (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1248 \override Tie #'after-line-breaking =
1256 When applying this trick, the new @code{after-line-breaking} callback
1257 should also call the old one, if such a default exists. For example,
1258 if using this with @code{Hairpin}, @code{ly:spanner::kill-zero-spanned-time}
1259 should also be called.
1263 Some objects cannot be changed with @code{\override} for
1264 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1265 and @code{PaperColumn}. They can be changed with the
1266 @code{\overrideProperty} function, which works similar to @code{\once
1267 \override}, but uses a different syntax.
1271 #"Score.NonMusicalPaperColumn" % Grob name
1272 #'line-break-system-details % Property name
1273 #'((next-padding . 20)) % Value
1276 Note, however, that @code{\override}, applied to
1277 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1278 expected within @code{\context} blocks.
1282 @node LilyPond Scheme interfaces
1283 @chapter LilyPond Scheme interfaces
1285 This chapter covers the various tools provided by LilyPond to help
1286 Scheme programmers get information into and out of the music streams.
1288 TODO -- figure out what goes in here and how to organize it