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:pitch?} and @code{ly:duration?}.
153 Suitability of arguments for all other predicates is determined by
154 actually calling the predicate after Lilypond has already converted
155 them into a Scheme expression. As a consequence, the argument can be
156 specified in Scheme syntax if desired (introduced with @code{#} or as
157 the result of calling a scheme function), but Lilypond will also
158 convert a number of Lilypond constructs into Scheme before actually
159 checking the predicate on them. Currently, those include music,
160 simple strings (with or without quotes), full markups and markup
161 lists, score, book, bookpart, context definition and output definition
164 For some kinds of expression (like most music not enclosed in braces)
165 Lilypond needs to look further than the expression itself in order to
166 determine its end. When such an expression is considered for an
167 optional argument by evaluating its predicate, Lilypond can't ``back
168 up'' when it decides the expression does not fit the parameter, and
169 complains. So in some contexts involving optional arguments, you may
170 need extra braces to make them acceptable to Lilypond.
172 This also applies for music arguments preceding @code{ly:duration?}
173 arguments. It may also affect the last argument of a scheme function
174 that is used as the last part of another expression, since otherwise
175 Lilypond won't know whether following postevents or durations apply to
176 the argument of the Scheme function, or to the containing music
179 For a list of available type predicates, see
180 @ruser{Predefined type predicates}.
185 @ruser{Predefined type predicates}.
188 @file{lily/music-scheme.cc},
192 @node Scheme function usage
193 @subsection Scheme function usage
195 Scheme functions can be called pretty much anywhere where a Scheme
196 expression starting with @code{#} can be written. You call a scheme
197 function by writing its name preceded by @code{\}, followed by its
198 arguments. The last argument can't be an optional argument. If there
199 are several optional arguments in a row, they are filled with values
200 left to right. Once an optional argument can't match input, it and all
201 immediately following optional arguments are replaced with their default
202 values, and the matching continues with the next non-optional argument.
204 Apart from places where a Scheme value is required, there are a few
205 places where @code{#} expressions are currently accepted and evaluated
206 for their side effects but otherwise ignored. Mostly those are the
207 places where an assignment would be acceptable as well.
209 Since it is a bad idea to return values that can be misinterpreted in
210 some context, you should use normal scheme functions only for those
211 cases where you always return a useful value, and use void scheme
212 functions (@pxref{Void scheme functions}) otherwise.
214 @node Void scheme functions
215 @subsection Void scheme functions
216 @funindex define-void-function
219 Sometimes a procedure is executed in order to perform an action rather
220 than return a value. Some programming languages (like C and Scheme)
221 use functions for either concept and just discard the returned value
222 (usually by allowing any expression to act as statement, ignoring the
223 result). This is clever but error-prone: most C compilers nowadays
224 offer warnings for various non-``void'' expressions being discarded.
225 For many functions executing an action, the Scheme standards declare
226 the return value to be unspecified. Lilypond's Scheme interpreter
227 Guile has a unique ``unspecified'' value that it usually (such when
228 using @code{set!} directly on a variable) but unfortunately not
229 consistently returns in such cases.
231 Defining a Lilypond function with @code{define-void-function} makes
232 sure that this special value (the only value satisfying the predicate
233 @code{void?}) will be returned.
237 #(define-void-function
240 (ly:set-option 'point-and-click #f))
242 \noPointAndClick % disable point and click
245 If you want to evaluate an expression only for its side-effect and
246 don't want any value it may return interpreted, you can do so by
247 prefixing it with @code{\void}:
250 \void #(hashq-set! some-table some-key some-value)
253 That way, you can be sure that Lilypond will not assign meaning to the
254 returned value regardless of where it encounters it. This will also
255 work for music functions such as @code{\displayMusic}.
257 @node Music functions
258 @section Music functions
260 @cindex music functions
262 @emph{Music functions} are Scheme procedures that can create music
263 expressions automatically, and can be used to greatly simplify the
267 * Music function definitions::
268 * Music function usage::
269 * Simple substitution functions::
270 * Intermediate substitution functions::
271 * Mathematics in functions::
272 * Functions without arguments::
273 * Void music functions::
277 @node Music function definitions
278 @subsection Music function definitions
279 @cindex defining music functions
280 @funindex define-music-function
282 The general form for defining music functions is:
286 #(define-music-function
287 (parser location @var{arg1} @var{arg2} @dots{})
288 (@var{type1?} @var{type2?} @dots{})
293 quite in analogy to @ref{Scheme function definitions}. More often than
294 not, @var{body} will be a @ref{Lilypond code blocks, Lilypond code block}.
296 For a list of available type predicates, see
297 @ruser{Predefined type predicates}.
302 @ruser{Predefined type predicates}.
305 @file{lily/music-scheme.cc},
310 @node Music function usage
311 @subsection Music function usage
312 Music functions may currently be used in three places. Depending on
313 where they are used, restrictions apply in order to be able to parse
314 them unambiguously. The result a music function returns must be
315 compatible with the context in which it is called.
319 At top level in a music expression. There are no special restrictions
320 on the argument list.
323 As a post-event, explicitly started with a direction indicator (one of
324 @code{-}, @code{^}, @w{and @code{_}}). All trailing arguments of the
325 music function with the predicate @code{ly:music?} will get parsed also
326 as post-events (if the last argument is a scheme function, this will
327 hold for trailing @code{ly:music?} arguments of the scheme function
328 instead). Note that returning a post-event will be acceptable for music
329 functions called as normal music, leading to a result roughly equivalent
336 As a chord constituent. All trailing arguments of the music function
337 with the predicate @code{ly:music?} will get parsed also as chord
342 The special rules for trailing arguments make it possible to write
343 polymorphic functions like @code{\tweak} that can be applied to
344 different constructs.
346 @node Simple substitution functions
347 @subsection Simple substitution functions
349 Simple substitution functions are music functions whose output
350 music expression is written in LilyPond format and contains
351 function arguments in the output expression. They are described
352 in @ruser{Substitution function examples}.
355 @node Intermediate substitution functions
356 @subsection Intermediate substitution functions
358 Intermediate substitution functions involve a mix of Scheme code
359 and LilyPond code in the music expression to be returned.
361 Some @code{\override} commands require an argument consisting of
362 a pair of numbers (called a @emph{cons cell} in Scheme).
364 The pair can be directly passed into the music function,
365 using a @code{pair?} variable:
369 #(define-music-function
370 (parser location beg-end)
373 \once \override Beam #'positions = $beg-end
377 \manualBeam #'(3 . 6) c8 d e f
381 Alternatively, the numbers making up the pair can be
382 passed as separate arguments, and the Scheme code
383 used to create the pair can be included in the
386 @lilypond[quote,verbatim,ragged-right]
388 #(define-music-function
389 (parser location beg end)
392 \once \override Beam #'positions = $(cons beg end)
396 \manualBeam #3 #6 c8 d e f
401 @node Mathematics in functions
402 @subsection Mathematics in functions
404 Music functions can involve Scheme programming in
405 addition to simple substitution,
407 @lilypond[quote,verbatim,ragged-right]
409 #(define-music-function
410 (parser location mag)
413 \override Stem #'length = $(* 7.0 mag)
414 \override NoteHead #'font-size =
415 $(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
419 \revert Stem #'length
420 \revert NoteHead #'font-size
425 \AltOn #1.5 c c \AltOff c2
430 This example may be rewritten to pass in music expressions,
432 @lilypond[quote,verbatim,ragged-right]
434 #(define-music-function
435 (parser location mag music)
438 \override Stem #'length = $(* 7.0 mag)
439 \override NoteHead #'font-size =
440 $(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
442 \revert Stem #'length
443 \revert NoteHead #'font-size
447 c2 \withAlt #0.5 { c4 c }
448 \withAlt #1.5 { c c } c2
453 @node Functions without arguments
454 @subsection Functions without arguments
456 In most cases a function without arguments should be written
460 dolce = \markup@{ \italic \bold dolce @}
463 However, in rare cases it may be useful to create a music function
468 #(define-music-function
471 (if (eq? #t (ly:get-option 'display-bar-numbers))
472 #@{ \once \override Score.BarNumber #'break-visibility = ##f #@}
476 To actually display bar numbers where this function is called,
477 invoke @command{lilypond} with
480 lilypond -d display-bar-numbers FILENAME.ly
484 @node Void music functions
485 @subsection Void music functions
487 A music function must return a music expression. If you want to
488 execute a function only for its side effect, you should use
489 @code{define-void-function}. But there may be cases where you
490 sometimes want to produce a music expression, and sometimes not (like
491 in the previous example). Returning a @code{void} music expression
492 via @code{#@{ #@}} will achieve that.
494 @node Event functions
495 @section Event functions
496 @funindex define-event-function
497 @cindex event functions
499 To use a music function in the place of an event, you need to write a
500 direction indicator before it. But sometimes, this does not quite match
501 the syntax of constructs you want to replace. For example, if you want
502 to write dynamics commands, those are usually attached without direction
503 indicator, like @code{c'\pp}. Here is a way to write arbitrary
505 @lilypond[quote,verbatim,raggedright]
506 dyn=#(define-event-function (parser location arg) (markup?)
507 (make-dynamic-script arg))
508 \relative c' { c\dyn pfsss }
510 You could do the same using a music function, but then you always would
511 have to write a direction indicator before calling it, like
512 @code{@w{c-\dyn pfsss}}.
515 @node Markup functions
516 @section Markup functions
518 Markups are implemented as special Scheme functions which produce a
519 @code{Stencil} object given a number of arguments.
522 * Markup construction in Scheme::
523 * How markups work internally::
524 * New markup command definition::
525 * New markup list command definition::
529 @node Markup construction in Scheme
530 @subsection Markup construction in Scheme
532 @cindex defining markup commands
534 The @code{markup} macro builds markup expressions in Scheme while
535 providing a LilyPond-like syntax. For example,
537 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
538 #:larger #:line ("foo" "bar" "baz")))
544 #@{ \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
545 \larger \line @{ foo bar baz @} @} #@}
549 This example demonstrates the main translation rules between regular
550 LilyPond markup syntax and Scheme markup syntax. Using @code{#@{
551 @dots{} #@}} for entering in LilyPond syntax will often be most
552 convenient, but we explain how to use the @code{markup} macro to get a
553 Scheme-only solution.
556 @multitable @columnfractions .3 .3
557 @item @b{LilyPond} @tab @b{Scheme}
558 @item @code{\markup markup1} @tab @code{(markup markup1)}
559 @item @code{\markup @{ markup1 markup2 ... @}} @tab
560 @code{(markup markup1 markup2 ... )}
561 @item @code{\markup-command} @tab @code{#:markup-command}
562 @item @code{\variable} @tab @code{variable}
563 @item @code{\center-column @{ ... @}} @tab @code{#:center-column ( ... )}
564 @item @code{string} @tab @code{"string"}
565 @item @code{#scheme-arg} @tab @code{scheme-arg}
569 The whole Scheme language is accessible inside the
570 @code{markup} macro. For example, You may use function calls inside
571 @code{markup} in order to manipulate character strings. This is
572 useful when defining new markup commands (see
573 @ref{New markup command definition}).
578 The markup-list argument of commands such as @code{#:line},
579 @code{#:center}, and @code{#:column} cannot be a variable or
580 the result of a function call.
583 (markup #:line (function-that-returns-markups))
587 is invalid. One should use the @code{make-line-markup},
588 @code{make-center-markup}, or @code{make-column-markup} functions
592 (markup (make-line-markup (function-that-returns-markups)))
596 @node How markups work internally
597 @subsection How markups work internally
602 \raise #0.5 "text example"
606 @code{\raise} is actually represented by the @code{raise-markup}
607 function. The markup expression is stored as
610 (list raise-markup 0.5 (list simple-markup "text example"))
613 When the markup is converted to printable objects (Stencils), the
614 @code{raise-markup} function is called as
619 @var{list of property alists}
621 @var{the "text example" markup})
624 The @code{raise-markup} function first creates the stencil for the
625 @code{text example} string, and then it raises that Stencil by 0.5
626 staff space. This is a rather simple example; more complex examples
628 of this section, and in @file{scm/define-markup-commands.scm}.
631 @node New markup command definition
632 @subsection New markup command definition
634 This section discusses the definition of new markup commands.
637 * Markup command definition syntax::
639 * A complete example::
640 * Adapting builtin commands::
643 @node Markup command definition syntax
644 @unnumberedsubsubsec Markup command definition syntax
646 New markup commands can be defined using the
647 @code{define-markup-command} Scheme macro, at top-level.
650 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
651 (@var{arg1-type?} @var{arg2-type?} ...)
652 [ #:properties ((@var{property1} @var{default-value1})
660 @item @var{command-name}
661 the markup command name
663 the @q{layout} definition.
665 a list of associative lists, containing all active properties.
667 @var{i}th command argument
668 @item @var{argi-type?}
669 a type predicate for the i@var{th} argument
672 If the command uses properties from the @code{props} arguments,
673 the @code{#:properties} keyword can be used to specify which
674 properties are used along with their default values.
676 Arguments are distinguished according to their type:
678 @item a markup, corresponding to type predicate @code{markup?};
679 @item a list of markups, corresponding to type predicate
681 @item any other scheme object, corresponding to type predicates such as
682 @code{list?}, @code{number?}, @code{boolean?}, etc.
685 There is no limitation on the order of arguments (after the
686 standard @code{layout} and @code{props} arguments). However,
687 markup functions taking a markup as their last argument are
688 somewhat special as you can apply them to a markup list, and the
689 result is a markup list where the markup function (with the
690 specified leading arguments) has been applied to every element of
691 the original markup list.
693 Since replicating the leading arguments for applying a markup
694 function to a markup list is cheap mostly for Scheme arguments,
695 you avoid performance pitfalls by just using Scheme arguments for
696 the leading arguments of markup functions that take a markup as
700 @unnumberedsubsubsec On properties
702 The @code{layout} and @code{props} arguments of markup commands bring a
703 context for the markup interpretation: font size, line width, etc.
705 The @code{layout} argument allows access to properties defined in
706 @code{paper} blocks, using the @code{ly:output-def-lookup} function.
707 For instance, the line width (the same as the one used in scores) is
711 (ly:output-def-lookup layout 'line-width)
714 The @code{props} argument makes some properties accessible to markup
715 commands. For instance, when a book title markup is interpreted, all
716 the variables defined in the @code{\header} block are automatically
717 added to @code{props}, so that the book title markup can access the book
718 title, composer, etc. It is also a way to configure the behaviour of a
719 markup command: for example, when a command uses font size during
720 processing, the font size is read from @code{props} rather than having a
721 @code{font-size} argument. The caller of a markup command may change
722 the value of the font size property in order to change the behaviour.
723 Use the @code{#:properties} keyword of @code{define-markup-command} to
724 specify which properties shall be read from the @code{props} arguments.
726 The example in next section illustrates how to access and override
727 properties in a markup command.
729 @node A complete example
730 @unnumberedsubsubsec A complete example
732 The following example defines a markup command to draw a double box
733 around a piece of text.
735 Firstly, we need to build an approximative result using markups.
736 Consulting the @ruser{Text markup commands} shows us the @code{\box}
739 @lilypond[quote,verbatim,ragged-right]
740 \markup \box \box HELLO
743 Now, we consider that more padding between the text and the boxes is
744 preferable. According to the @code{\box} documentation, this command
745 uses a @code{box-padding} property, which defaults to 0.2. The
746 documentation also mentions how to override it:
748 @lilypond[quote,verbatim,ragged-right]
749 \markup \box \override #'(box-padding . 0.6) \box A
752 Then, the padding between the two boxes is considered too small, so we
755 @lilypond[quote,verbatim,ragged-right]
756 \markup \override #'(box-padding . 0.4) \box
757 \override #'(box-padding . 0.6) \box A
760 Repeating this lengthy markup would be painful. This is where a markup
761 command is needed. Thus, we write a @code{double-box} markup command,
762 taking one argument (the text). This draws the two boxes, with some
766 #(define-markup-command (double-box layout props text) (markup?)
767 "Draw a double box around text."
768 (interpret-markup layout props
769 #@{\markup \override #'(box-padding . 0.4) \box
770 \override #'(box-padding . 0.6) \box @{ $text @}#@}))
776 #(define-markup-command (double-box layout props text) (markup?)
777 "Draw a double box around text."
778 (interpret-markup layout props
779 (markup #:override '(box-padding . 0.4) #:box
780 #:override '(box-padding . 0.6) #:box text)))
783 @code{text} is the name of the command argument, and @code{markup?} its
784 type: it identifies it as a markup. The @code{interpret-markup}
785 function is used in most of markup commands: it builds a stencil, using
786 @code{layout}, @code{props}, and a markup. In the second case, this
787 markup is built using the @code{markup} scheme macro, see @ref{Markup
788 construction in Scheme}. The transformation from @code{\markup}
789 expression to scheme markup expression is straight-forward.
791 The new command can be used as follow:
794 \markup \double-box A
797 It would be nice to make the @code{double-box} command customizable:
798 here, the @code{box-padding} values are hard coded, and cannot be
799 changed by the user. Also, it would be better to distinguish the
800 padding between the two boxes, from the padding between the inner box
801 and the text. So we will introduce a new property,
802 @code{inter-box-padding}, for the padding between the two boxes. The
803 @code{box-padding} will be used for the inner padding. The new code is
807 #(define-markup-command (double-box layout props text) (markup?)
808 #:properties ((inter-box-padding 0.4)
810 "Draw a double box around text."
811 (interpret-markup layout props
812 #@{\markup \override #`(box-padding . ,$inter-box-padding) \box
813 \override #`(box-padding . ,$box-padding) \box
817 Again, the equivalent version using the markup macro would be:
820 #(define-markup-command (double-box layout props text) (markup?)
821 #:properties ((inter-box-padding 0.4)
823 "Draw a double box around text."
824 (interpret-markup layout props
825 (markup #:override `(box-padding . ,inter-box-padding) #:box
826 #:override `(box-padding . ,box-padding) #:box text)))
829 Here, the @code{#:properties} keyword is used so that the
830 @code{inter-box-padding} and @code{box-padding} properties are read from
831 the @code{props} argument, and default values are given to them if the
832 properties are not defined.
834 Then, these values are used to override the @code{box-padding}
835 properties used by the two @code{\box} commands. Note the backquote and
836 the comma in the @code{\override} argument: they allow you to introduce
837 a variable value into a literal expression.
839 Now, the command can be used in a markup, and the boxes padding be
842 @lilypond[quote,verbatim,ragged-right]
843 #(define-markup-command (double-box layout props text) (markup?)
844 #:properties ((inter-box-padding 0.4)
846 "Draw a double box around text."
847 (interpret-markup layout props
848 #{\markup \override #`(box-padding . ,$inter-box-padding) \box
849 \override #`(box-padding . ,$box-padding) \box
852 \markup \double-box A
853 \markup \override #'(inter-box-padding . 0.8) \double-box A
854 \markup \override #'(box-padding . 1.0) \double-box A
857 @node Adapting builtin commands
858 @unnumberedsubsubsec Adapting builtin commands
860 A good way to start writing a new markup command, is to take example on
861 a builtin one. Most of the markup commands provided with LilyPond can be
862 found in file @file{scm/define-markup-commands.scm}.
864 For instance, we would like to adapt the @code{\draw-line} command, to
865 draw a double line instead. The @code{\draw-line} command is defined as
866 follow (documentation stripped):
869 (define-markup-command (draw-line layout props dest)
872 #:properties ((thickness 1))
874 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
878 (make-line-stencil th 0 0 x y)))
881 To define a new command based on an existing one, copy the definition,
882 and change the command name. The @code{#:category} keyword can be
883 safely removed, as it is only used for generating LilyPond
884 documentation, and is of no use for user-defined markup commands.
887 (define-markup-command (draw-double-line layout props dest)
889 #:properties ((thickness 1))
891 (let ((th (* (ly:output-def-lookup layout 'line-thickness)
895 (make-line-stencil th 0 0 x y)))
898 Then, a property for setting the gap between two lines is added, called
899 @code{line-gap}, defaulting e.g. to 0.6:
902 (define-markup-command (draw-double-line layout props dest)
904 #:properties ((thickness 1)
910 Finally, the code for drawing two lines is added. Two calls to
911 @code{make-line-stencil} are used to draw the lines, and the resulting
912 stencils are combined using @code{ly:stencil-add}:
914 @lilypond[quote,verbatim,ragged-right]
915 #(define-markup-command (my-draw-line layout props dest)
917 #:properties ((thickness 1)
920 (let* ((th (* (ly:output-def-lookup layout 'line-thickness)
925 (x (cond ((= dx 0) w)
927 (else (/ w (sqrt (+ 1 (* (/ dx dy) (/ dx dy))))))))
928 (y (* (if (< (* dx dy) 0) 1 -1)
931 (else (/ w (sqrt (+ 1 (* (/ dy dx) (/ dy dx))))))))))
932 (ly:stencil-add (make-line-stencil th x y (+ dx x) (+ dy y))
933 (make-line-stencil th (- x) (- y) (- dx x) (- dy y)))))
935 \markup \my-draw-line #'(4 . 3)
936 \markup \override #'(line-gap . 1.2) \my-draw-line #'(4 . 3)
940 @node New markup list command definition
941 @subsection New markup list command definition
942 Markup list commands are defined with the
943 @code{define-markup-list-command} Scheme macro, which is similar to the
944 @code{define-markup-command} macro described in
945 @ref{New markup command definition}, except that where the latter returns
946 a single stencil, the former returns a list of stencils.
948 In the following example, a @code{\paragraph} markup list command is
949 defined, which returns a list of justified lines, the first one being
950 indented. The indent width is taken from the @code{props} argument.
953 #(define-markup-list-command (paragraph layout props args) (markup-list?)
954 #:properties ((par-indent 2))
955 (interpret-markup-list layout props
956 #@{\markuplist \justified-lines @{ \hspace #$par-indent $args @} #@}))
960 The version using just Scheme is more complex:
962 #(define-markup-list-command (paragraph layout props args) (markup-list?)
963 #:properties ((par-indent 2))
964 (interpret-markup-list layout props
965 (make-justified-lines-markup-list (cons (make-hspace-markup par-indent)
969 Besides the usual @code{layout} and @code{props} arguments, the
970 @code{paragraph} markup list command takes a markup list argument, named
971 @code{args}. The predicate for markup lists is @code{markup-list?}.
973 First, the function gets the indent width, a property here named
974 @code{par-indent}, from the property list @code{props}. If the
975 property is not found, the default value is @code{2}. Then, a
976 list of justified lines is made using the built-in markup list command
977 @code{\justified-lines}, which is related to the
978 @code{make-justified-lines-markup-list} function. A
979 horizontal space is added at the beginning using @code{\hspace} (or the
980 @code{make-hspace-markup} function). Finally, the markup list is
981 interpreted using the @code{interpret-markup-list} function.
983 This new markup list command can be used as follows:
987 The art of music typography is called \italic @{(plate) engraving.@}
988 The term derives from the traditional process of music printing.
989 Just a few decades ago, sheet music was made by cutting and stamping
990 the music into a zinc or pewter plate in mirror image.
992 \override-lines #'(par-indent . 4) \paragraph @{
993 The plate would be inked, the depressions caused by the cutting
994 and stamping would hold ink. An image was formed by pressing paper
995 to the plate. The stamping and cutting was completely done by
1001 @node Contexts for programmers
1002 @section Contexts for programmers
1005 * Context evaluation::
1006 * Running a function on all layout objects::
1009 @node Context evaluation
1010 @subsection Context evaluation
1012 @cindex calling code during interpreting
1013 @funindex \applyContext
1015 Contexts can be modified during interpretation with Scheme code. The
1018 \applyContext @var{function}
1021 @code{@var{function}} should be a Scheme function that takes a
1022 single argument: the context in which the @code{\applyContext}
1023 command is being called. The following code will print the
1024 current bar number on the standard output during the compile:
1029 (format #t "\nWe were called in barnumber ~a.\n"
1030 (ly:context-property x 'currentBarNumber)))
1035 @node Running a function on all layout objects
1036 @subsection Running a function on all layout objects
1039 @cindex calling code on layout objects
1040 @funindex \applyOutput
1043 The most versatile way of tuning an object is @code{\applyOutput} which
1044 works by inserting an event into the specified context
1045 (@rinternals{ApplyOutputEvent}). Its syntax is
1047 \applyOutput @var{context} @var{proc}
1051 where @code{@var{proc}} is a Scheme function, taking three arguments.
1053 When interpreted, the function @code{@var{proc}} is called for
1054 every layout object found in the context @code{@var{context}} at
1055 the current time step, with the following arguments:
1057 @item the layout object itself,
1058 @item the context where the layout object was created, and
1059 @item the context where @code{\applyOutput} is processed.
1063 In addition, the cause of the layout object, i.e., the music
1064 expression or object that was responsible for creating it, is in the
1065 object property @code{cause}. For example, for a note head, this is a
1066 @rinternals{NoteHead} event, and for a stem object,
1067 this is a @rinternals{Stem} object.
1069 Here is a function to use for @code{\applyOutput}; it blanks
1070 note-heads on the center-line and next to it:
1072 @lilypond[quote,verbatim,ragged-right]
1073 #(define (blanker grob grob-origin context)
1074 (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1075 (< (abs (ly:grob-property grob 'staff-position)) 2))
1076 (set! (ly:grob-property grob 'transparent) #t)))
1079 a'4 e8 <<\applyOutput #'Voice #blanker a c d>> b2
1084 @node Callback functions
1085 @section Callback functions
1087 Properties (like @code{thickness}, @code{direction}, etc.) can be
1088 set at fixed values with @code{\override}, e.g.
1091 \override Stem #'thickness = #2.0
1094 Properties can also be set to a Scheme procedure,
1096 @lilypond[fragment,verbatim,quote,relative=2]
1097 \override Stem #'thickness = #(lambda (grob)
1098 (if (= UP (ly:grob-property grob 'direction))
1105 In this case, the procedure is executed as soon as the value of the
1106 property is requested during the formatting process.
1108 Most of the typesetting engine is driven by such callbacks.
1109 Properties that typically use callbacks include
1113 The printing routine, that constructs a drawing for the symbol
1115 The routine that sets the horizontal position
1117 The routine that computes the width of an object
1120 The procedure always takes a single argument, being the grob.
1122 If routines with multiple arguments must be called, the current grob
1123 can be inserted with a grob closure. Here is a setting from
1124 @code{AccidentalSuggestion},
1128 ,(ly:make-simple-closure
1130 ,(ly:make-simple-closure
1131 (list ly:self-alignment-interface::centered-on-x-parent))
1132 ,(ly:make-simple-closure
1133 (list ly:self-alignment-interface::x-aligned-on-self)))))
1137 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
1138 @code{ly:self-alignment-interface::centered-on-x-parent} are called
1139 with the grob as argument. The results are added with the @code{+}
1140 function. To ensure that this addition is properly executed, the whole
1141 thing is enclosed in @code{ly:make-simple-closure}.
1143 In fact, using a single procedure as property value is equivalent to
1146 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
1150 The inner @code{ly:make-simple-closure} supplies the grob as argument
1151 to @var{proc}, the outer ensures that result of the function is
1152 returned, rather than the @code{simple-closure} object.
1154 From within a callback, the easiest method for evaluating a markup is
1155 to use grob-interpret-markup. For example:
1158 my-callback = #(lambda (grob)
1159 (grob-interpret-markup grob (markup "foo")))
1162 @node Inline Scheme code
1163 @section Inline Scheme code
1165 The main disadvantage of @code{\tweak} is its syntactical
1166 inflexibility. For example, the following produces a syntax error.
1169 F = \tweak #'font-size #-3 -\flageolet
1177 In other words, @code{\tweak} doesn't behave like an articulation
1178 regarding the syntax; in particular, it can't be attached with
1179 @code{^} and @code{_}.
1181 Using Scheme, this problem can be avoided. The route to the
1182 result is given in @ref{Adding articulation to notes (example)},
1183 especially how to use @code{\displayMusic} as a helping guide.
1186 F = #(let ((m (make-music 'ArticulationEvent
1187 'articulation-type "flageolet")))
1188 (set! (ly:music-property m 'tweaks)
1189 (acons 'font-size -3
1190 (ly:music-property m 'tweaks)))
1199 Here, the @code{tweaks} properties of the flageolet object
1200 @code{m} (created with @code{make-music}) are extracted with
1201 @code{ly:music-property}, a new key-value pair to change the
1202 font size is prepended to the property list with the
1203 @code{acons} Scheme function, and the result is finally
1204 written back with @code{set!}. The last element of the
1205 @code{let} block is the return value, @code{m} itself.
1209 @node Difficult tweaks
1210 @section Difficult tweaks
1212 There are a few classes of difficult adjustments.
1218 One type of difficult adjustment involves the appearance of
1219 spanner objects, such as slurs and ties. Usually, only one
1220 spanner object is created at a time, and it can be adjusted with
1221 the normal mechanism. However, occasionally a spanner crosses a
1222 line break. When this happens, the object is cloned. A separate
1223 object is created for every system in which the spanner appears.
1224 The new objects are clones of the original object and inherit all
1225 properties, including @code{\override}s.
1228 In other words, an @code{\override} always affects all pieces of a
1229 broken spanner. To change only one part of a spanner at a line break,
1230 it is necessary to hook into the formatting process. The
1231 @code{after-line-breaking} callback contains the Scheme procedure that
1232 is called after the line breaks have been determined and layout
1233 objects have been split over different systems.
1235 In the following example, we define a procedure
1236 @code{my-callback}. This procedure
1240 determines if the spanner has been split across line breaks
1242 if yes, retrieves all the split objects
1244 checks if this grob is the last of the split objects
1246 if yes, it sets @code{extra-offset}.
1249 This procedure is installed into @rinternals{Tie}, so the last part
1250 of the broken tie is repositioned.
1252 @lilypond[quote,verbatim,ragged-right]
1253 #(define (my-callback grob)
1255 ;; have we been split?
1256 (orig (ly:grob-original grob))
1258 ;; if yes, get the split pieces (our siblings)
1259 (siblings (if (ly:grob? orig)
1260 (ly:spanner-broken-into orig)
1263 (if (and (>= (length siblings) 2)
1264 (eq? (car (last-pair siblings)) grob))
1265 (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1268 \override Tie #'after-line-breaking =
1276 When applying this trick, the new @code{after-line-breaking} callback
1277 should also call the old one, if such a default exists. For example,
1278 if using this with @code{Hairpin}, @code{ly:spanner::kill-zero-spanned-time}
1279 should also be called.
1283 Some objects cannot be changed with @code{\override} for
1284 technical reasons. Examples of those are @code{NonMusicalPaperColumn}
1285 and @code{PaperColumn}. They can be changed with the
1286 @code{\overrideProperty} function, which works similar to @code{\once
1287 \override}, but uses a different syntax.
1291 #"Score.NonMusicalPaperColumn" % Grob name
1292 #'line-break-system-details % Property name
1293 #'((next-padding . 20)) % Value
1296 Note, however, that @code{\override}, applied to
1297 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1298 expected within @code{\context} blocks.
1302 @node LilyPond Scheme interfaces
1303 @chapter LilyPond Scheme interfaces
1305 This chapter covers the various tools provided by LilyPond to help
1306 Scheme programmers get information into and out of the music streams.
1308 TODO -- figure out what goes in here and how to organize it