]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/extending/programming-interface.itely
Run scripts/auxiliar/update-with-convert-ly.sh
[lilypond.git] / Documentation / extending / programming-interface.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2
3 @ignore
4     Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
5
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..
9 @end ignore
10
11 @c \version "2.19.21"
12
13 @node Interfaces for programmers
14 @chapter Interfaces for programmers
15
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}.
19
20 @menu
21 * LilyPond code blocks::
22 * Scheme functions::
23 * Music functions::
24 * Event functions::
25 * Markup functions::
26 * Contexts for programmers::
27 * Callback functions::
28 * Difficult tweaks::
29 @end menu
30
31 @node LilyPond code blocks
32 @section LilyPond code blocks
33
34 @cindex LilyPond code blocks
35 @cindex code blocks, LilyPond
36 @funindex #@{ @dots{} #@}
37 @funindex $
38 @funindex #
39
40 Creating music expressions in Scheme can be tedious, as they are
41 heavily nested and the resulting Scheme code is large. For some
42 simple tasks this can be avoided by using LilyPond code blocks,
43 which enable common LilyPond syntax to be used within Scheme.
44
45 LilyPond code blocks look like
46
47 @example
48   #@{ @var{LilyPond code} #@}
49 @end example
50
51 Here is a trivial example:
52
53 @lilypond[verbatim,quote]
54 ritpp = #(define-event-function (parser location) ()
55   #{ ^"rit." \pp #}
56 )
57
58 { c'4 e'4\ritpp g'2 }
59 @end lilypond
60
61 LilyPond code blocks can be used anywhere where you can write Scheme
62 code.  The Scheme reader actually is changed for accommodating
63 LilyPond code blocks and can deal with embedded Scheme expressions
64 starting with @code{$} and@w{ }@code{#}.
65
66 @cindex parser (function argument)
67 @cindex location
68
69 The reader extracts the LilyPond code block and generates a runtime
70 call to the LilyPond @code{parser} to interpret the LilyPond code.
71 Scheme expressions embedded in the LilyPond code are evaluated in the
72 lexical environment of the LilyPond code block, so all local variables
73 and function parameters available at the point the LilyPond code block
74 is written may be accessed.  Variables defined in other Scheme modules,
75 like the modules containing @code{\header} and @code{\layout} blocks,
76 are not accessible as Scheme variables, i.e. prefixed
77 with@tie{}@code{#}, but they are accessible as LilyPond variables, i.e.
78 prefixed with@tie{}@code{\}.
79
80 If @code{location} (see @ref{Scheme functions}) refers to a valid
81 input location (which it usually does inside of music/@/Scheme
82 functions), all music generated inside the code block has its
83 @samp{origin} set to @code{location}.
84
85 A LilyPond code block may contain anything that you can use on the
86 right side of an assignment.  In addition, an empty LilyPond block
87 corresponds to a void music expression, and a LilyPond block
88 containing multiple music events gets turned into a sequential music
89 expression.
90
91 @node Scheme functions
92 @section Scheme functions
93 @cindex Scheme functions (LilyPond syntax)
94
95 @emph{Scheme functions} are Scheme procedures that can create Scheme
96 expressions from input written in LilyPond syntax.  They can be called
97 in pretty much all places where using @code{#} for specifying a value in
98 Scheme syntax is allowed.  While Scheme has functions of its own, this
99 chapter is concerned with @emph{syntactic} functions, functions that
100 receive arguments specified in LilyPond syntax.
101
102 @menu
103 * Scheme function definitions::
104 * Scheme function usage::
105 * Void scheme functions::
106 @end menu
107
108 @node Scheme function definitions
109 @subsection Scheme function definitions
110 @funindex define-scheme-function
111
112 The general form for defining scheme functions is:
113
114 @example
115 function =
116 #(define-scheme-function
117      (parser location @var{arg1} @var{arg2} @dots{})
118      (@var{type1?} @var{type2?} @dots{})
119    @var{body})
120 @end example
121
122 @noindent
123 where
124
125 @multitable @columnfractions .33 .66
126 @item @code{parser}
127 @tab needs to be literally @code{parser} in order to give LilyPond code
128 blocks (@code{#@{}@dots{}@code{#@}}) access to the parser.
129
130 @item @code{location}
131 @tab needs to be literally @code{location} in order to provide access
132 to the input location object, which is used to provide error messages
133 with file names and line numbers.
134
135 @item @code{@var{argN}}
136 @tab @var{n}th argument
137
138 @item @code{@var{typeN?}}
139 @tab a Scheme @emph{type predicate} for which @code{@var{argN}}
140 must return @code{#t}.  There is also a special form
141 @code{(@emph{predicate?} @emph{default})} for specifying optional
142 arguments.  If the actual argument is missing when the function is being
143 called, the default value is substituted instead.  Default values are
144 evaluated at definition time (including LilyPond code blocks!), so if
145 you need a default calculated at runtime, instead write a special value
146 you can easily recognize.  If you write the predicate in parentheses but
147 don't follow it with a default value, @code{#f} is used as the default.
148 Default values are not verified with @emph{predicate?} at either
149 definition or run time: it is your responsibility to deal with the
150 values you specify.  Default values that happen to be music expressions
151 are copied while setting @code{origin} to the @code{location} parameter.
152
153 @item @code{@var{body}}
154 @tab A sequence of Scheme forms evaluated in order, the last one being
155 used as the return value of the scheme function.  It may contain
156 LilyPond code blocks enclosed in hashed braces
157 (@tie{}@w{@code{#@{@dots{}#@}}}@tie{}), like described in
158 @ref{LilyPond code blocks}.  Within LilyPond code blocks, use @code{#}
159 to reference function arguments (eg., @samp{#arg1}) or to start an
160 inline Scheme expression containing function arguments (eg.,
161 @w{@samp{#(cons arg1 arg2)}}).  Where normal Scheme expressions using
162 @code{#} don't do the trick, you might need to revert to immediate
163 Scheme expressions using @code{$}, for example as @samp{$music}.
164
165 If your function returns a music expression, it is given a useful value
166 of @code{origin}.
167 @end multitable
168
169 @noindent
170 Suitability of arguments for the predicates is determined by
171 actually calling the predicate after LilyPond has already converted them
172 into a Scheme expression.  As a consequence, the argument can be
173 specified in Scheme syntax if desired (introduced with @code{#} or as
174 the result of calling a scheme function), but LilyPond will also convert
175 a number of LilyPond constructs into Scheme before actually checking the
176 predicate on them.  Currently, those include music, postevents, simple
177 strings (with or without quotes), numbers, full markups and markup
178 lists, score, book, bookpart, context definition and output definition
179 blocks.
180
181 Some
182 ambiguities LilyPond sorts out by checking with predicate
183 functions: is @samp{-3} a fingering postevent or a negative number?  Is
184 @code{"a" 4} in lyric mode a string followed by a number, or a lyric
185 event of duration @code{4}?  LilyPond tries the argument
186 predicate on successive interpretations until success, with an
187 order designed to minimize inconsistent interpretations and
188 lookahead.
189
190 For example, a predicate accepting both music expressions and
191 pitches will consider @code{c''} to be a pitch rather than a music
192 expression.  Immediately following durations or postevents will
193 change that interpretation.  It's best to avoid overly
194 permissive predicates like @code{scheme?} when the application
195 rather calls for more specific argument types.
196
197 For a list of available predefined type predicates, see
198 @ruser{Predefined type predicates}.
199
200 @seealso
201
202 Notation Reference:
203 @ruser{Predefined type predicates}.
204
205 Installed Files:
206 @file{lily/music-scheme.cc},
207 @file{scm/c++.scm},
208 @file{scm/lily.scm}.
209
210 @node Scheme function usage
211 @subsection Scheme function usage
212
213 Scheme functions can be called pretty much anywhere where a Scheme
214 expression starting with @code{#} can be written.  You call a scheme
215 function by writing its name preceded by @code{\}, followed by its
216 arguments.  Once an optional argument predicate does not match an
217 argument, LilyPond skips this and all following optional arguments,
218 replacing them with their specified default, and @q{backs up} the
219 argument that did not match to the place of the next mandatory argument.
220 Since the backed up argument needs to go somewhere, optional arguments
221 are not actually considered optional unless followed by a mandatory
222 argument.
223
224 There is one exception: if you write @code{\default} in the place of an
225 optional argument, this and all following optional arguments are skipped
226 and replaced by their default.  This works even when no mandatory
227 argument follows since @code{\default} does not need to get backed up.
228 The @code{mark} and @code{key} commands make use of that trick to
229 provide their default behavior when just followed by @code{\default}.
230
231 Apart from places where a Scheme value is required, there are a few
232 places where @code{#} expressions are currently accepted and evaluated
233 for their side effects but otherwise ignored.  Mostly those are the
234 places where an assignment would be acceptable as well.
235
236 Since it is a bad idea to return values that can be misinterpreted in
237 some context, you should use normal scheme functions only for those
238 cases where you always return a useful value, and use void scheme
239 functions (@pxref{Void scheme functions}) otherwise.
240
241 @node Void scheme functions
242 @subsection Void scheme functions
243 @funindex define-void-function
244 @funindex \void
245
246 Sometimes a procedure is executed in order to perform an action rather
247 than return a value.  Some programming languages (like C and Scheme) use
248 functions for either concept and just discard the returned value
249 (usually by allowing any expression to act as statement, ignoring the
250 result).  This is clever but error-prone: most C compilers nowadays
251 offer warnings for various non-``void'' expressions being discarded.
252 For many functions executing an action, the Scheme standards declare the
253 return value to be unspecified.  LilyPond's Scheme interpreter Guile has
254 a unique value @code{*unspecified*} that it usually (such when using
255 @code{set!} directly on a variable) but unfortunately not consistently
256 returns in such cases.
257
258 Defining a LilyPond function with @code{define-void-function} makes
259 sure that this special value (the only value satisfying the predicate
260 @code{void?}) will be returned.
261
262 @example
263 noPointAndClick =
264 #(define-void-function
265      (parser location)
266      ()
267    (ly:set-option 'point-and-click #f))
268 @dots{}
269 \noPointAndClick   % disable point and click
270 @end example
271
272 If you want to evaluate an expression only for its side-effect and
273 don't want any value it may return interpreted, you can do so by
274 prefixing it with @code{\void}:
275
276 @example
277 \void #(hashq-set! some-table some-key some-value)
278 @end example
279
280 That way, you can be sure that LilyPond will not assign meaning to the
281 returned value regardless of where it encounters it.  This will also
282 work for music functions such as @code{\displayMusic}.
283
284 @node Music functions
285 @section Music functions
286
287 @cindex music functions
288
289 @emph{Music functions} are Scheme procedures that can create music
290 expressions automatically, and can be used to greatly simplify the
291 input file.
292
293 @menu
294 * Music function definitions::
295 * Music function usage::
296 * Simple substitution functions::
297 * Intermediate substitution functions::
298 * Mathematics in functions::
299 * Functions without arguments::
300 * Void music functions::
301 @end menu
302
303
304 @node Music function definitions
305 @subsection Music function definitions
306 @cindex defining music functions
307 @funindex define-music-function
308
309 The general form for defining music functions is:
310
311 @example
312 function =
313 #(define-music-function
314      (parser location @var{arg1} @var{arg2} @dots{})
315      (@var{type1?} @var{type2?} @dots{})
316    @var{body})
317 @end example
318
319 @noindent
320 quite in analogy to @ref{Scheme function definitions}.  More often than
321 not, @var{body} will be a @ref{LilyPond code blocks, LilyPond code block}.
322
323 For a list of available type predicates, see
324 @ruser{Predefined type predicates}.
325
326 @seealso
327
328 Notation Reference:
329 @ruser{Predefined type predicates}.
330
331 Installed Files:
332 @file{lily/music-scheme.cc},
333 @file{scm/c++.scm},
334 @file{scm/lily.scm}.
335
336
337 @node Music function usage
338 @subsection Music function usage
339 A @q{music function} has to return an expression matching the predicate
340 @code{ly:music?}.  This makes music function calls suitable as arguments
341 of type @code{ly:music?} for another music function call.
342
343 When using a music function call in other contexts, the context may
344 cause further semantic restrictions.
345
346 @itemize
347 @item
348 At the top level in a music expression a post-event is not accepted.
349
350 @item
351 When a music function (as opposed to an event function) returns an
352 expression of type post-event, LilyPond requires one of the named
353 direction indicators (@code{-}, @code{^}, @w{and @code{_}})) in order to
354 properly integrate the post-event produced by the music function call
355 into the surrounding expression.
356
357 @item
358 As a chord constituent.  The returned expression must be of a
359 @code{rhythmic-event} type, most likely a @code{NoteEvent}.
360 @end itemize
361
362 @noindent
363 @q{Polymorphic} functions, like @code{\tweak}, can be applied to
364 post-events, chord constituent and top level music expressions.
365
366 @node Simple substitution functions
367 @subsection Simple substitution functions
368
369 Simple substitution functions are music functions whose output
370 music expression is written in LilyPond format and contains
371 function arguments in the output expression.  They are described
372 in @ruser{Substitution function examples}.
373
374
375 @node Intermediate substitution functions
376 @subsection Intermediate substitution functions
377
378 Intermediate substitution functions involve a mix of Scheme code
379 and LilyPond code in the music expression to be returned.
380
381 Some @code{\override} commands require an argument consisting of
382 a pair of numbers (called a @emph{cons cell} in Scheme).
383
384 The pair can be directly passed into the music function,
385 using a @code{pair?} variable:
386
387 @example
388 manualBeam =
389 #(define-music-function
390      (parser location beg-end)
391      (pair?)
392    #@{
393      \once \override Beam.positions = #beg-end
394    #@})
395
396 \relative c' @{
397   \manualBeam #'(3 . 6) c8 d e f
398 @}
399 @end example
400
401 Alternatively, the numbers making up the pair can be
402 passed as separate arguments, and the Scheme code
403 used to create the pair can be included in the
404 music expression:
405
406 @lilypond[quote,verbatim,ragged-right]
407 manualBeam =
408 #(define-music-function
409      (parser location beg end)
410      (number? number?)
411    #{
412      \once \override Beam.positions = #(cons beg end)
413    #})
414
415 \relative c' {
416   \manualBeam #3 #6 c8 d e f
417 }
418 @end lilypond
419
420 @funindex \temporary
421 @cindex temporary overrides
422 @cindex overrides, temporary
423 @cindex properties, popping previous value
424
425 Properties are maintained conceptually using one stack per property
426 per grob per context.  Music functions may need to override one or
427 several properties for the duration of the function, restoring them
428 to their previous value before exiting.  However, normal overrides
429 pop and discard the top of the current property stack before
430 pushing to it, so the previous value of the property is lost when it
431 is overridden.  When the previous value must be preserved, prefix the
432 @code{\override} command with @code{\temporary}, like this:
433
434 @example
435 \temporary \override @dots{}
436 @end example
437
438 The use of @code{\temporary} causes the (usually set) @code{pop-first}
439 property in the override to be cleared, so the previous value is not
440 popped off the property stack before pushing the new value onto it.
441 When a subsequent @code{\revert} pops off the temporarily overriden
442 value, the previous value will re-emerge.
443
444 In other words, calling @code{\temporary \override} and @code{\revert}
445 in succession on the same property will have a net effect of zero.
446 Similarly, pairing @code{\temporary} and @code{\undo} on the same
447 music containing overrides will have a net effect of zero.
448
449 Here is an example of a music function which makes use of this.
450 The use of @code{\temporary} ensures the values of the
451 @code{cross-staff} and @code{style} properties are restored on exit
452 to whatever values they had when the @code{crossStaff} function was
453 called.  Without @code{\temporary} the default values would have been
454 set on exit.
455
456 @example
457 crossStaff =
458 #(define-music-function (parser location notes) (ly:music?)
459   (_i "Create cross-staff stems")
460   #@{
461   \temporary \override Stem.cross-staff = #cross-staff-connect
462   \temporary \override Flag.style = #'no-flag
463   #notes
464   \revert Stem.cross-staff
465   \revert Flag.style
466 #@})
467 @end example
468
469
470 @node Mathematics in functions
471 @subsection Mathematics in functions
472
473 Music functions can involve Scheme programming in
474 addition to simple substitution,
475
476 @lilypond[quote,verbatim,ragged-right]
477 AltOn =
478 #(define-music-function
479      (parser location mag)
480      (number?)
481    #{
482      \override Stem.length = #(* 7.0 mag)
483      \override NoteHead.font-size =
484        #(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
485    #})
486
487 AltOff = {
488   \revert Stem.length
489   \revert NoteHead.font-size
490 }
491
492 \relative {
493   c'2 \AltOn #0.5 c4 c
494   \AltOn #1.5 c c \AltOff c2
495 }
496 @end lilypond
497
498 @noindent
499 This example may be rewritten to pass in music expressions,
500
501 @lilypond[quote,verbatim,ragged-right]
502 withAlt =
503 #(define-music-function
504      (parser location mag music)
505      (number? ly:music?)
506    #{
507      \override Stem.length = #(* 7.0 mag)
508      \override NoteHead.font-size =
509        #(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
510      #music
511      \revert Stem.length
512      \revert NoteHead.font-size
513    #})
514
515 \relative {
516   c'2 \withAlt #0.5 { c4 c }
517   \withAlt #1.5 { c c } c2
518 }
519 @end lilypond
520
521
522 @node Functions without arguments
523 @subsection Functions without arguments
524
525 In most cases a function without arguments should be written
526 with a variable,
527
528 @example
529 dolce = \markup@{ \italic \bold dolce @}
530 @end example
531
532 However, in rare cases it may be useful to create a music function
533 without arguments,
534
535 @example
536 displayBarNum =
537 #(define-music-function
538      (parser location)
539      ()
540    (if (eq? #t (ly:get-option 'display-bar-numbers))
541        #@{ \once \override Score.BarNumber.break-visibility = ##f #@}
542        #@{#@}))
543 @end example
544
545 To actually display bar numbers where this function is called,
546 invoke @command{lilypond} with
547
548 @example
549 lilypond -d display-bar-numbers FILENAME.ly
550 @end example
551
552
553 @node Void music functions
554 @subsection Void music functions
555
556 A music function must return a music expression.  If you want to
557 execute a function only for its side effect, you should use
558 @code{define-void-function}.  But there may be cases where you
559 sometimes want to produce a music expression, and sometimes not (like
560 in the previous example).  Returning a @code{void} music expression
561 via @code{#@{ #@}} will achieve that.
562
563 @node Event functions
564 @section Event functions
565 @funindex define-event-function
566 @cindex event functions
567
568 To use a music function in the place of an event, you need to write a
569 direction indicator before it.  But sometimes, this does not quite match
570 the syntax of constructs you want to replace.  For example, if you want
571 to write dynamics commands, those are usually attached without direction
572 indicator, like @code{c'\pp}.  Here is a way to write arbitrary
573 dynamics:
574 @lilypond[quote,verbatim,ragged-right]
575 dyn=#(define-event-function (parser location arg) (markup?)
576          (make-dynamic-script arg))
577 \relative { c'\dyn pfsss }
578 @end lilypond
579 You could do the same using a music function, but then you always would
580 have to write a direction indicator before calling it, like
581 @code{@w{c-\dyn pfsss}}.
582
583
584 @node Markup functions
585 @section Markup functions
586
587 Markups are implemented as special Scheme functions which produce a
588 @code{Stencil} object given a number of arguments.
589
590 @menu
591 * Markup construction in Scheme::
592 * How markups work internally::
593 * New markup command definition::
594 * New markup list command definition::
595 @end menu
596
597
598 @node Markup construction in Scheme
599 @subsection Markup construction in Scheme
600
601 @cindex defining markup commands
602 @funindex \displayScheme
603
604 Markup expressions are internally represented in Scheme using the
605 @code{markup} macro:
606
607 @example
608 (markup @var{expr})
609 @end example
610
611 To see a markup expression in its Scheme form, use the
612 @code{\displayScheme} command:
613
614 @example
615 \displayScheme
616 \markup @{
617   \column @{
618     \line @{ \bold \italic "hello" \raise #0.4 "world" @}
619     \larger \line @{ foo bar baz @}
620   @}
621 @}
622 @end example
623
624 @noindent
625 Compiling the code above will send the following to the display
626 console:
627
628 @example
629 (markup
630   #:line
631   (#:column
632    (#:line
633     (#:bold (#:italic "hello") #:raise 0.4 "world")
634     #:larger
635     (#:line
636      (#:simple "foo" #:simple "bar" #:simple "baz")))))
637 @end example
638
639 To prevent the markup from printing on the page, use
640 @w{@samp{\void \displayScheme @var{markup}}}.  Also, as with the
641 @code{\displayMusic} command, the output of @code{\displayScheme}
642 can be saved to an external file.  See
643 @ref{Displaying music expressions}.
644
645 @noindent
646 This example demonstrates the main translation rules between regular
647 LilyPond markup syntax and Scheme markup syntax.  Using @code{#@{
648 @dots{} #@}} for entering in LilyPond syntax will often be most
649 convenient, but we explain how to use the @code{markup} macro to get a
650 Scheme-only solution.
651
652 @quotation
653 @multitable @columnfractions .3 .3
654 @item @b{LilyPond} @tab @b{Scheme}
655 @item @code{\markup markup1} @tab @code{(markup markup1)}
656 @item @code{\markup @{ markup1 markup2 @dots{} @}} @tab
657         @code{(markup markup1 markup2 @dots{} )}
658 @item @code{\markup-command} @tab @code{#:markup-command}
659 @item @code{\variable} @tab @code{variable}
660 @item @code{\center-column @{ @dots{} @}} @tab
661         @code{#:center-column ( @dots{} )}
662 @item @code{string} @tab @code{"string"}
663 @item @code{#scheme-arg} @tab @code{scheme-arg}
664 @end multitable
665 @end quotation
666
667 The whole Scheme language is accessible inside the
668 @code{markup} macro.  For example, You may use function calls inside
669 @code{markup} in order to manipulate character strings.  This is
670 useful when defining new markup commands (see
671 @ref{New markup command definition}).
672
673
674 @knownissues
675
676 The markup-list argument of commands such as @code{#:line},
677 @code{#:center}, and @code{#:column} cannot be a variable or
678 the result of a function call.
679
680 @lisp
681 (markup #:line (function-that-returns-markups))
682 @end lisp
683
684 @noindent
685 is invalid.  One should use the @code{make-line-markup},
686 @code{make-center-markup}, or @code{make-column-markup} functions
687 instead,
688
689 @lisp
690 (markup (make-line-markup (function-that-returns-markups)))
691 @end lisp
692
693
694 @node How markups work internally
695 @subsection How markups work internally
696
697 In a markup like
698
699 @example
700 \raise #0.5 "text example"
701 @end example
702
703 @noindent
704 @code{\raise} is actually represented by the @code{raise-markup}
705 function.  The markup expression is stored as
706
707 @example
708 (list raise-markup 0.5 (list simple-markup "text example"))
709 @end example
710
711 When the markup is converted to printable objects (Stencils), the
712 @code{raise-markup} function is called as
713
714 @example
715 (apply raise-markup
716        @var{\layout object}
717        @var{list of property alists}
718        0.5
719        @var{the "text example" markup})
720 @end example
721
722 The @code{raise-markup} function first creates the stencil for the
723 @code{text example} string, and then it raises that Stencil by 0.5
724 staff space.  This is a rather simple example; more complex examples
725 are in the rest
726 of this section, and in @file{scm/define-markup-commands.scm}.
727
728
729 @node New markup command definition
730 @subsection New markup command definition
731
732 This section discusses the definition of new markup commands.
733
734 @menu
735 * Markup command definition syntax::
736 * On properties::
737 * A complete example::
738 * Adapting builtin commands::
739 @end menu
740
741 @node Markup command definition syntax
742 @unnumberedsubsubsec Markup command definition syntax
743
744 New markup commands can be defined using the
745 @code{define-markup-command} Scheme macro, at top-level.
746
747 @lisp
748 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} @dots{})
749     (@var{arg1-type?} @var{arg2-type?} @dots{})
750     [ #:properties ((@var{property1} @var{default-value1})
751                     @dots{}) ]
752   @dots{}command body@dots{})
753 @end lisp
754
755 The arguments are
756
757 @table @code
758 @item @var{command-name}
759 the markup command name
760 @item layout
761 the @q{layout} definition.
762 @item props
763 a list of associative lists, containing all active properties.
764 @item @var{argi}
765 @var{i}th command argument
766 @item @var{argi-type?}
767 a type predicate for the i@var{th} argument
768 @end table
769
770 If the command uses properties from the @code{props} arguments,
771 the @code{#:properties} keyword can be used to specify which
772 properties are used along with their default values.
773
774 Arguments are distinguished according to their type:
775 @itemize
776 @item a markup, corresponding to type predicate @code{markup?};
777 @item a list of markups, corresponding to type predicate
778 @code{markup-list?};
779 @item any other scheme object, corresponding to type predicates such as
780 @code{list?}, @code{number?}, @code{boolean?}, etc.
781 @end itemize
782
783 There is no limitation on the order of arguments (after the
784 standard @code{layout} and @code{props} arguments).  However,
785 markup functions taking a markup as their last argument are
786 somewhat special as you can apply them to a markup list, and the
787 result is a markup list where the markup function (with the
788 specified leading arguments) has been applied to every element of
789 the original markup list.
790
791 Since replicating the leading arguments for applying a markup
792 function to a markup list is cheap mostly for Scheme arguments,
793 you avoid performance pitfalls by just using Scheme arguments for
794 the leading arguments of markup functions that take a markup as
795 their last argument.
796
797 @funindex \markup
798 @cindex markup macro
799 @funindex interpret-markup
800 Markup commands have a rather complex life cycle.  The body of a
801 markup command definition is responsible for converting the
802 arguments of the markup command into a stencil expression which is
803 returned.  Quite often this is accomplished by calling the
804 @code{interpret-markup} function on a markup expression, passing
805 the @var{layout} and @var{props} arguments on to it.  Those
806 arguments are usually only known at a very late stage in
807 typesetting.  Markup expressions have their components assembled
808 into markup expressions already when @code{\markup} in a LilyPond
809 expression or the @code{markup} macro in Scheme is expanded.  The
810 evaluation and typechecking of markup command arguments happens at
811 the time @code{\markup}/@code{markup} are interpreted.
812
813 But the actual conversion of markup expressions into stencil
814 expressions by executing the markup function bodies only happens
815 when @code{interpret-markup} is called on a markup expression.
816
817 @node On properties
818 @unnumberedsubsubsec On properties
819
820 The @code{layout} and @code{props} arguments of markup commands bring a
821 context for the markup interpretation: font size, line width, etc.
822
823 The @code{layout} argument allows access to properties defined in
824 @code{paper} blocks, using the @code{ly:output-def-lookup} function.
825 For instance, the line width (the same as the one used in scores) is
826 read using:
827
828 @example
829 (ly:output-def-lookup layout 'line-width)
830 @end example
831
832 The @code{props} argument makes some properties accessible to markup
833 commands.  For instance, when a book title markup is interpreted, all
834 the variables defined in the @code{\header} block are automatically
835 added to @code{props}, so that the book title markup can access the book
836 title, composer, etc.  It is also a way to configure the behaviour of a
837 markup command: for example, when a command uses font size during
838 processing, the font size is read from @code{props} rather than having a
839 @code{font-size} argument.  The caller of a markup command may change
840 the value of the font size property in order to change the behaviour.
841 Use the @code{#:properties} keyword of @code{define-markup-command} to
842 specify which properties shall be read from the @code{props} arguments.
843
844 The example in next section illustrates how to access and override
845 properties in a markup command.
846
847 @node A complete example
848 @unnumberedsubsubsec A complete example
849
850 The following example defines a markup command to draw a double box
851 around a piece of text.
852
853 Firstly, we need to build an approximative result using markups.
854 Consulting the @ruser{Text markup commands} shows us the @code{\box}
855 command is useful:
856
857 @lilypond[quote,verbatim,ragged-right]
858 \markup \box \box HELLO
859 @end lilypond
860
861 Now, we consider that more padding between the text and the boxes is
862 preferable.  According to the @code{\box} documentation, this command
863 uses a @code{box-padding} property, which defaults to 0.2.  The
864 documentation also mentions how to override it:
865
866 @lilypond[quote,verbatim,ragged-right]
867 \markup \box \override #'(box-padding . 0.6) \box A
868 @end lilypond
869
870 Then, the padding between the two boxes is considered too small, so we
871 override it too:
872
873 @lilypond[quote,verbatim,ragged-right]
874 \markup \override #'(box-padding . 0.4) \box
875      \override #'(box-padding . 0.6) \box A
876 @end lilypond
877
878 Repeating this lengthy markup would be painful.  This is where a markup
879 command is needed.  Thus, we write a @code{double-box} markup command,
880 taking one argument (the text).  This draws the two boxes, with some
881 padding.
882
883 @lisp
884 #(define-markup-command (double-box layout props text) (markup?)
885   "Draw a double box around text."
886   (interpret-markup layout props
887     #@{\markup \override #'(box-padding . 0.4) \box
888             \override #'(box-padding . 0.6) \box @{ #text @}#@}))
889 @end lisp
890
891 or, equivalently
892
893 @lisp
894 #(define-markup-command (double-box layout props text) (markup?)
895   "Draw a double box around text."
896   (interpret-markup layout props
897     (markup #:override '(box-padding . 0.4) #:box
898             #:override '(box-padding . 0.6) #:box text)))
899 @end lisp
900
901 @code{text} is the name of the command argument, and @code{markup?} its
902 type: it identifies it as a markup.  The @code{interpret-markup}
903 function is used in most of markup commands: it builds a stencil, using
904 @code{layout}, @code{props}, and a markup.  In the second case, this
905 markup is built using the @code{markup} scheme macro, see @ref{Markup
906 construction in Scheme}.  The transformation from @code{\markup}
907 expression to scheme markup expression is straight-forward.
908
909 The new command can be used as follow:
910
911 @example
912 \markup \double-box A
913 @end example
914
915 It would be nice to make the @code{double-box} command customizable:
916 here, the @code{box-padding} values are hard coded, and cannot be
917 changed by the user.  Also, it would be better to distinguish the
918 padding between the two boxes, from the padding between the inner box
919 and the text.  So we will introduce a new property,
920 @code{inter-box-padding}, for the padding between the two boxes.  The
921 @code{box-padding} will be used for the inner padding.  The new code is
922 now as follows:
923
924 @lisp
925 #(define-markup-command (double-box layout props text) (markup?)
926   #:properties ((inter-box-padding 0.4)
927                 (box-padding 0.6))
928   "Draw a double box around text."
929   (interpret-markup layout props
930     #@{\markup \override #`(box-padding . ,inter-box-padding) \box
931                \override #`(box-padding . ,box-padding) \box
932                @{ #text @} #@}))
933 @end lisp
934
935 Again, the equivalent version using the markup macro would be:
936
937 @lisp
938 #(define-markup-command (double-box layout props text) (markup?)
939   #:properties ((inter-box-padding 0.4)
940                 (box-padding 0.6))
941   "Draw a double box around text."
942   (interpret-markup layout props
943     (markup #:override `(box-padding . ,inter-box-padding) #:box
944             #:override `(box-padding . ,box-padding) #:box text)))
945 @end lisp
946
947 Here, the @code{#:properties} keyword is used so that the
948 @code{inter-box-padding} and @code{box-padding} properties are read from
949 the @code{props} argument, and default values are given to them if the
950 properties are not defined.
951
952 Then, these values are used to override the @code{box-padding}
953 properties used by the two @code{\box} commands.  Note the backquote and
954 the comma in the @code{\override} argument: they allow you to introduce
955 a variable value into a literal expression.
956
957 Now, the command can be used in a markup, and the boxes padding be
958 customized:
959
960 @lilypond[quote,verbatim,ragged-right]
961 #(define-markup-command (double-box layout props text) (markup?)
962   #:properties ((inter-box-padding 0.4)
963                 (box-padding 0.6))
964   "Draw a double box around text."
965   (interpret-markup layout props
966     #{\markup \override #`(box-padding . ,inter-box-padding) \box
967               \override #`(box-padding . ,box-padding) \box
968               { #text } #}))
969
970 \markup \double-box A
971 \markup \override #'(inter-box-padding . 0.8) \double-box A
972 \markup \override #'(box-padding . 1.0) \double-box A
973 @end lilypond
974
975 @node Adapting builtin commands
976 @unnumberedsubsubsec Adapting builtin commands
977
978 A good way to start writing a new markup command, is to take example on
979 a builtin one.  Most of the markup commands provided with LilyPond can be
980 found in file @file{scm/define-markup-commands.scm}.
981
982 For instance, we would like to adapt the @code{\draw-line} command, to
983 draw a double line instead.  The @code{\draw-line} command is defined as
984 follow (documentation stripped):
985
986 @lisp
987 (define-markup-command (draw-line layout props dest)
988   (number-pair?)
989   #:category graphic
990   #:properties ((thickness 1))
991   "@dots{}documentation@dots{}"
992   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
993                thickness))
994         (x (car dest))
995         (y (cdr dest)))
996     (make-line-stencil th 0 0 x y)))
997 @end lisp
998
999 To define a new command based on an existing one, copy the definition,
1000 and change the command name.  The @code{#:category} keyword can be
1001 safely removed, as it is only used for generating LilyPond
1002 documentation, and is of no use for user-defined markup commands.
1003
1004 @lisp
1005 (define-markup-command (draw-double-line layout props dest)
1006   (number-pair?)
1007   #:properties ((thickness 1))
1008   "@dots{}documentation@dots{}"
1009   (let ((th (* (ly:output-def-lookup layout 'line-thickness)
1010                thickness))
1011         (x (car dest))
1012         (y (cdr dest)))
1013     (make-line-stencil th 0 0 x y)))
1014 @end lisp
1015
1016 Then, a property for setting the gap between two lines is added, called
1017 @code{line-gap}, defaulting e.g. to 0.6:
1018
1019 @lisp
1020 (define-markup-command (draw-double-line layout props dest)
1021   (number-pair?)
1022   #:properties ((thickness 1)
1023                 (line-gap 0.6))
1024   "@dots{}documentation@dots{}"
1025   @dots{}
1026 @end lisp
1027
1028 Finally, the code for drawing two lines is added.  Two calls to
1029 @code{make-line-stencil} are used to draw the lines, and the resulting
1030 stencils are combined using @code{ly:stencil-add}:
1031
1032 @lilypond[quote,verbatim,ragged-right]
1033 #(define-markup-command (my-draw-line layout props dest)
1034   (number-pair?)
1035   #:properties ((thickness 1)
1036                 (line-gap 0.6))
1037   "..documentation.."
1038   (let* ((th (* (ly:output-def-lookup layout 'line-thickness)
1039                 thickness))
1040          (dx (car dest))
1041          (dy (cdr dest))
1042          (w (/ line-gap 2.0))
1043          (x (cond ((= dx 0) w)
1044                   ((= dy 0) 0)
1045                   (else (/ w (sqrt (+ 1 (* (/ dx dy) (/ dx dy))))))))
1046          (y (* (if (< (* dx dy) 0) 1 -1)
1047                (cond ((= dy 0) w)
1048                      ((= dx 0) 0)
1049                      (else (/ w (sqrt (+ 1 (* (/ dy dx) (/ dy dx))))))))))
1050      (ly:stencil-add (make-line-stencil th x y (+ dx x) (+ dy y))
1051                      (make-line-stencil th (- x) (- y) (- dx x) (- dy y)))))
1052
1053 \markup \my-draw-line #'(4 . 3)
1054 \markup \override #'(line-gap . 1.2) \my-draw-line #'(4 . 3)
1055 @end lilypond
1056
1057
1058 @node New markup list command definition
1059 @subsection New markup list command definition
1060 @funindex define-markup-list-command
1061 @funindex interpret-markup-list
1062 Markup list commands are defined with the
1063 @code{define-markup-list-command} Scheme macro, which is similar to the
1064 @code{define-markup-command} macro described in
1065 @ref{New markup command definition}, except that where the latter returns
1066 a single stencil, the former returns a list of stencils.
1067
1068 In a similar vein, @code{interpret-markup-list} is used instead of
1069 @code{interpret-markup} for converting a markup list into a list
1070 of stencils.
1071
1072 In the following example, a @code{\paragraph} markup list command is
1073 defined, which returns a list of justified lines, the first one being
1074 indented.  The indent width is taken from the @code{props} argument.
1075
1076 @example
1077 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1078    #:properties ((par-indent 2))
1079    (interpret-markup-list layout props
1080      #@{\markuplist \justified-lines @{ \hspace #par-indent #args @} #@}))
1081 @end example
1082
1083
1084 The version using just Scheme is more complex:
1085 @example
1086 #(define-markup-list-command (paragraph layout props args) (markup-list?)
1087    #:properties ((par-indent 2))
1088    (interpret-markup-list layout props
1089      (make-justified-lines-markup-list (cons (make-hspace-markup par-indent)
1090                                              args))))
1091 @end example
1092
1093 Besides the usual @code{layout} and @code{props} arguments, the
1094 @code{paragraph} markup list command takes a markup list argument, named
1095 @code{args}.  The predicate for markup lists is @code{markup-list?}.
1096
1097 First, the function gets the indent width, a property here named
1098 @code{par-indent}, from the property list @code{props}.  If the
1099 property is not found, the default value is @code{2}.  Then, a
1100 list of justified lines is made using the built-in markup list command
1101 @code{\justified-lines}, which is related to the
1102 @code{make-justified-lines-markup-list} function.  A
1103 horizontal space is added at the beginning using @code{\hspace} (or the
1104 @code{make-hspace-markup} function).  Finally, the markup list is
1105 interpreted using the @code{interpret-markup-list} function.
1106
1107 This new markup list command can be used as follows:
1108 @example
1109 \markuplist @{
1110   \paragraph @{
1111     The art of music typography is called \italic @{(plate) engraving.@}
1112     The term derives from the traditional process of music printing.
1113     Just a few decades ago, sheet music was made by cutting and stamping
1114     the music into a zinc or pewter plate in mirror image.
1115   @}
1116   \override-lines #'(par-indent . 4) \paragraph @{
1117     The plate would be inked, the depressions caused by the cutting
1118     and stamping would hold ink.  An image was formed by pressing paper
1119     to the plate.  The stamping and cutting was completely done by
1120     hand.
1121   @}
1122 @}
1123 @end example
1124
1125 @node Contexts for programmers
1126 @section Contexts for programmers
1127
1128 @menu
1129 * Context evaluation::
1130 * Running a function on all layout objects::
1131 @end menu
1132
1133 @node Context evaluation
1134 @subsection Context evaluation
1135
1136 @cindex calling code during interpreting
1137 @funindex \applyContext
1138 @funindex make-apply-context
1139 @funindex ly:context-property
1140 @funindex ly:context-set-property!
1141 @funindex ly:context-grob-definition
1142 @funindex ly:assoc-get
1143 @funindex ly:context-pushpop-property
1144
1145 Contexts can be modified during interpretation with Scheme code.
1146 In a LilyPond code block, the syntax for this is:
1147
1148 @example
1149 \applyContext @var{function}
1150 @end example
1151
1152 In Scheme code, the syntax is:
1153
1154 @example
1155 (make-apply-context @var{function})
1156 @end example
1157
1158 @code{@var{function}} should be a Scheme function that takes a
1159 single argument: the context in which the @code{\applyContext}
1160 command is being called.  The function can access as well as
1161 override/set grob properties and context properties.  Any actions
1162 taken by the function that depend on the state of the context are
1163 limited to the state of the context @emph{when the function is
1164 called}.  Also, changes effected by a call to @code{\applyContext}
1165 remain in effect until they are directly modified again, or
1166 reverted, even if the initial conditions that they depended on
1167 have changed.
1168
1169 The following scheme functions are useful when using
1170 @code{\applyContext}:
1171
1172 @table @code
1173 @item ly:context-property
1174 look up a context property value
1175
1176 @item ly:context-set-property!
1177 set a context property
1178
1179 @item ly:context-grob-definition
1180 @itemx ly:assoc-get
1181 look up a grob property value
1182
1183 @item ly:context-pushpop-property
1184 do a @code{\temporary@tie{}\override}
1185 or a @code{\revert} on a grob property
1186 @end table
1187
1188
1189 The following example looks up the current @code{fontSize} value, and
1190 then doubles it:
1191
1192 @lilypond[quote,verbatim]
1193 doubleFontSize =
1194 \applyContext
1195   #(lambda (context)
1196      (let ((fontSize (ly:context-property context 'fontSize)))
1197        (ly:context-set-property! context 'fontSize (+ fontSize 6))))
1198
1199 {
1200   \set fontSize = -3
1201   b'4
1202   \doubleFontSize
1203   b'
1204 }
1205 @end lilypond
1206
1207
1208 The following example looks up the current colors of the
1209 @code{NoteHead}, @code{Stem}, and @code{Beam} grobs, and then changes
1210 each to a less saturated shade.
1211
1212 @lilypond[quote,verbatim]
1213 desaturate =
1214 \applyContext
1215   #(lambda (context)
1216      (define (desaturate-grob grob)
1217        (let* ((grob-def (ly:context-grob-definition context grob))
1218               (color (ly:assoc-get 'color grob-def black))
1219               (new-color (map (lambda (x) (min 1 (/ (1+ x) 2))) color)))
1220          (ly:context-pushpop-property context grob 'color new-color)))
1221      (for-each desaturate-grob '(NoteHead Stem Beam)))
1222
1223 \relative {
1224   \time 3/4
1225   g'8[ g] \desaturate g[ g] \desaturate g[ g]
1226   \override NoteHead.color = #darkred
1227   \override Stem.color = #darkred
1228   \override Beam.color = #darkred
1229   g[ g] \desaturate g[ g] \desaturate g[ g]
1230 }
1231 @end lilypond
1232
1233
1234 This also could be implemented as a music function, in order to
1235 restrict the modifications to a single music block.  Notice how
1236 @code{ly:context-pushpop-property} is used both as a
1237 @code{\temporary@tie{}\override} and as a @code{\revert}:
1238
1239 @lilypond[quote,verbatim]
1240 desaturate =
1241 #(define-music-function
1242    (parser location music) (ly:music?)
1243    #{
1244      \applyContext
1245      #(lambda (context)
1246         (define (desaturate-grob grob)
1247           (let* ((grob-def (ly:context-grob-definition context grob))
1248                  (color (ly:assoc-get 'color grob-def black))
1249                  (new-color (map (lambda (x) (min 1 (/ (1+ x) 2))) color)))
1250             (ly:context-pushpop-property context grob 'color new-color)))
1251         (for-each desaturate-grob '(NoteHead Stem Beam)))
1252      #music
1253      \applyContext
1254      #(lambda (context)
1255         (define (revert-color grob)
1256           (ly:context-pushpop-property context grob 'color))
1257         (for-each revert-color '(NoteHead Stem Beam)))
1258    #})
1259
1260 \relative g' {
1261   \override NoteHead.color = #darkblue
1262   \override Stem.color = #darkblue
1263   \override Beam.color = #darkblue
1264   g8 a b c
1265   \desaturate { d c b a }
1266   g b d b g2
1267 }
1268 @end lilypond
1269
1270
1271 @node Running a function on all layout objects
1272 @subsection Running a function on all layout objects
1273
1274
1275 @cindex calling code on layout objects
1276 @funindex \applyOutput
1277
1278 The most versatile way of tuning an object is @code{\applyOutput} which
1279 works by inserting an event into the specified context
1280 (@rinternals{ApplyOutputEvent}).  Its syntax is
1281 @example
1282 \applyOutput @var{Context} @var{proc}
1283 @end example
1284
1285 @noindent
1286 where @code{@var{proc}} is a Scheme function, taking three arguments.
1287
1288 When interpreted, the function @code{@var{proc}} is called for
1289 every layout object found in the context @code{@var{Context}} at
1290 the current time step, with the following arguments:
1291 @itemize
1292 @item the layout object itself,
1293 @item the context where the layout object was created, and
1294 @item the context where @code{\applyOutput} is processed.
1295 @end itemize
1296
1297
1298 In addition, the cause of the layout object, i.e., the music
1299 expression or object that was responsible for creating it, is in the
1300 object property @code{cause}.  For example, for a note head, this is a
1301 @rinternals{NoteHead} event, and for a stem object,
1302 this is a @rinternals{Stem} object.
1303
1304 Here is a function to use for @code{\applyOutput}; it blanks
1305 note-heads on the center-line and next to it:
1306
1307 @lilypond[quote,verbatim,ragged-right]
1308 #(define (blanker grob grob-origin context)
1309    (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
1310             (< (abs (ly:grob-property grob 'staff-position)) 2))
1311        (set! (ly:grob-property grob 'transparent) #t)))
1312
1313 \relative {
1314   a'4 e8 <<\applyOutput #'Voice #blanker a c d>> b2
1315 }
1316 @end lilypond
1317
1318 To have @var{function} interpreted at the @code{Score} or @code{Staff}
1319 level use these forms
1320
1321 @example
1322 \applyOutput #'Score #@var{function}
1323 \applyOutput #'Staff #@var{function}
1324 @end example
1325
1326
1327 @node Callback functions
1328 @section Callback functions
1329
1330 Properties (like @code{thickness}, @code{direction}, etc.) can be
1331 set at fixed values with @code{\override}, e.g.
1332
1333 @example
1334 \override Stem.thickness = #2.0
1335 @end example
1336
1337 Properties can also be set to a Scheme procedure,
1338
1339 @lilypond[fragment,verbatim,quote,relative=2]
1340 \override Stem.thickness = #(lambda (grob)
1341     (if (= UP (ly:grob-property grob 'direction))
1342         2.0
1343         7.0))
1344 c b a g b a g b
1345 @end lilypond
1346
1347 @noindent
1348 In this case, the procedure is executed as soon as the value of the
1349 property is requested during the formatting process.
1350
1351 Most of the typesetting engine is driven by such callbacks.
1352 Properties that typically use callbacks include
1353
1354 @table @code
1355 @item stencil
1356   The printing routine, that constructs a drawing for the symbol
1357 @item X-offset
1358   The routine that sets the horizontal position
1359 @item X-extent
1360   The routine that computes the width of an object
1361 @end table
1362
1363 The procedure always takes a single argument, being the grob.
1364
1365 That procedure may access the usual value of the property, by first
1366 calling the function that is the usual callback for that property, which
1367 can by found in the Internals Reference or the file 'define-grobs.scm':
1368
1369 @example
1370 \relative c'' @{
1371   \override Flag.X-offset = #(lambda (flag)
1372     (let ((default (ly:flag::calc-x-offset flag)))
1373       (* default 4.0)))
1374   c4. d8 a4. g8
1375 @}
1376 @end example
1377
1378 From within a callback, the easiest method for evaluating a markup is
1379 to use grob-interpret-markup.  For example:
1380
1381 @example
1382 my-callback = #(lambda (grob)
1383                  (grob-interpret-markup grob (markup "foo")))
1384 @end example
1385
1386 @ignore
1387
1388 @n ode Inline Scheme code
1389 @s ection Inline Scheme code
1390
1391 TODO: after this section had been written, LilyPond has improved
1392 to the point that finding a @emph{simple} example where one would
1393 @emph{have} to revert to Scheme has become rather hard.
1394
1395 Until this section gets a rewrite, let's pretend we don't know.
1396
1397 The main disadvantage of @code{\tweak} is its syntactical
1398 inflexibility.  For example, the following produces a syntax error
1399 (or rather, it did so at some point in the past).
1400
1401 @example
1402 F = \tweak font-size #-3 -\flageolet
1403
1404 \relative @{
1405   c''4^\F c4_\F
1406 @}
1407 @end example
1408
1409 @noindent
1410 Using Scheme, this problem can be avoided.  The route to the
1411 result is given in @ref{Adding articulation to notes (example)},
1412 especially how to use @code{\displayMusic} as a helping guide.
1413
1414 @example
1415 F = #(let ((m (make-music 'ArticulationEvent
1416                           'articulation-type "flageolet")))
1417        (set! (ly:music-property m 'tweaks)
1418              (acons 'font-size -3
1419                     (ly:music-property m 'tweaks)))
1420        m)
1421
1422 \relative @{
1423   c''4^\F c4_\F
1424 @}
1425 @end example
1426
1427 @noindent
1428 Here, the @code{tweaks} properties of the flageolet object
1429 @code{m} (created with @code{make-music}) are extracted with
1430 @code{ly:music-property}, a new key-value pair to change the
1431 font size is prepended to the property list with the
1432 @code{acons} Scheme function, and the result is finally
1433 written back with @code{set!}.  The last element of the
1434 @code{let} block is the return value, @code{m} itself.
1435
1436 @end ignore
1437
1438
1439 @node Difficult tweaks
1440 @section Difficult tweaks
1441
1442 There are a few classes of difficult adjustments.
1443
1444 @itemize
1445
1446
1447 @item
1448 One type of difficult adjustment involves the appearance of
1449 spanner objects, such as slurs and ties.  Usually, only one
1450 spanner object is created at a time, and it can be adjusted with
1451 the normal mechanism.  However, occasionally a spanner crosses a
1452 line break.  When this happens, the object is cloned.  A separate
1453 object is created for every system in which the spanner appears.
1454 The new objects are clones of the original object and inherit all
1455 properties, including @code{\override}s.
1456
1457
1458 In other words, an @code{\override} always affects all pieces of a
1459 broken spanner.  To change only one part of a spanner at a line break,
1460 it is necessary to hook into the formatting process.  The
1461 @code{after-line-breaking} callback contains the Scheme procedure that
1462 is called after the line breaks have been determined and layout
1463 objects have been split over different systems.
1464
1465 In the following example, we define a procedure
1466 @code{my-callback}.  This procedure
1467
1468 @itemize
1469 @item
1470 determines if the spanner has been split across line breaks
1471 @item
1472 if yes, retrieves all the split objects
1473 @item
1474 checks if this grob is the last of the split objects
1475 @item
1476 if yes, it sets @code{extra-offset}.
1477 @end itemize
1478
1479 This procedure is installed into @rinternals{Tie}, so the last part
1480 of the broken tie is repositioned.
1481
1482 @lilypond[quote,verbatim,ragged-right]
1483 #(define (my-callback grob)
1484    (let* (
1485           ;; have we been split?
1486           (orig (ly:grob-original grob))
1487
1488           ;; if yes, get the split pieces (our siblings)
1489           (siblings (if (ly:grob? orig)
1490                         (ly:spanner-broken-into orig)
1491                         '())))
1492
1493      (if (and (>= (length siblings) 2)
1494               (eq? (car (last-pair siblings)) grob))
1495          (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
1496
1497 \relative c'' {
1498   \override Tie.after-line-breaking =
1499   #my-callback
1500   c1 ~ \break
1501   c2 ~ 2
1502 }
1503 @end lilypond
1504
1505 @noindent
1506 When applying this trick, the new @code{after-line-breaking} callback
1507 should also call the old one, if such a default exists.  For example,
1508 if using this with @code{Hairpin}, @code{ly:spanner::kill-zero-spanned-time}
1509 should also be called.
1510
1511
1512 @item
1513 Some objects cannot be changed with @code{\override} for
1514 technical reasons.  Examples of those are @code{NonMusicalPaperColumn}
1515 and @code{PaperColumn}.  They can be changed with the
1516 @code{\overrideProperty} function, which works similar to @code{\once
1517 \override}, but uses a different syntax.
1518
1519 @example
1520 \overrideProperty
1521 Score.NonMusicalPaperColumn       % Grob name
1522   . line-break-system-details     % Property name
1523   . next-padding                  % Optional subproperty name
1524   #20                             % Value
1525 @end example
1526
1527 Note, however, that @code{\override}, applied to
1528 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
1529 expected within @code{\context} blocks.
1530
1531 @end itemize
1532
1533 @node LilyPond Scheme interfaces
1534 @chapter LilyPond Scheme interfaces
1535
1536 This chapter covers the various tools provided by LilyPond to help
1537 Scheme programmers get information into and out of the music streams.
1538
1539 TODO -- figure out what goes in here and how to organize it
1540