]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/extending/programming-interface.itely
Doc -- reorganize Extending
[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.  See TRANSLATION for details.
8 @end ignore
9
10 @c \version "2.12.0"
11
12 @node Interfaces for programmers
13 @chapter Interfaces for programmers
14
15 Advanced tweaks may be performed by using Scheme.  If you are
16 not familiar with Scheme, you may wish to read our
17 @ref{Scheme tutorial}.
18
19 @menu
20 * Music functions::
21 * Markup functions::
22 * Contexts for programmers::
23 * Callback functions::
24 * Inline Scheme code::
25 * Difficult tweaks::
26 @end menu
27
28
29 @node Music functions
30 @section Music functions
31
32 Music functions are scheme functions that are used to
33 automatically create music expressions.  They can be used to
34 greatly simplify the input file.
35
36 @menu
37 * Music function syntax::
38 * Simple substitution functions::
39 * Intermediate substitution functions::
40 * Mathematics in functions::
41 * Void functions::
42 * Functions without arguments::
43 @end menu
44
45 @node Music function syntax
46 @subsection Music function syntax
47
48 The general syntax of a music function is:
49
50 @example
51 myFunction =
52 #(define-music-function (parser location @var{var_1} @var{var_2}...@var{var_n})
53                         (@var{var_1-type?} @var{var_2-type?}...@var{var_n-type?})
54     @var{...valid music expression...})
55 @end example
56
57 @noindent
58 where
59
60 @multitable @columnfractions .33 .66
61 @item @var{var_i}         @tab @var{i}th variable
62 @item @var{var_i-type?}   @tab type of @var{i}th variable
63 @item @var{...valid music expression...}  @tab expression that returns
64 valid music, generally in the form of a Scheme expression.  There is
65 also special syntax that allows LilyPond input code in this music
66 expression.
67 @end multitable
68
69 The variable type checkers are scheme procedures that will return
70 @code{#t} if a variable is of a given type.  Some common types
71 are shown in the table below.  Other types can be found in the files
72 @file{lily/music-scheme.cc} and @file{scm/c++.scm}.
73
74 @multitable @columnfractions .33 .66
75 @headitem Input type          @tab @var{vari-type?} notation
76 @item Integer                 @tab @code{integer?}
77 @item Float (decimal number)  @tab @code{number?}
78 @item Text string             @tab @code{string?}
79 @item Markup                  @tab @code{markup?}
80 @item Music expression        @tab @code{ly:music?}
81 @item A pair of variables     @tab @code{pair?}
82 @end multitable
83
84 The @code{parser} and @code{location} arguments are mandatory.
85 The @code{parser} argument is used in the body of the function
86 to gain access to the value of another LilyPond variable.
87 The @code{location} argument is used to set the @q{origin}
88 of the music expression that is built by the music function,
89 so that in case of a syntax error LilyPond
90 can tell the user an appropriate place to look in the input file.
91
92 @node Simple substitution functions
93 @subsection Simple substitution functions
94
95 A simple substitution function is a music function whose output music
96 expression is written in LilyPond code, but with an input variable
97 substituted into the LilyPond code.  The general form of these functions is
98
99 @example
100 myFunction =
101 #(define-music-function (parser location @var{var1})
102                         (@var{var1-type?})
103   #@{
104     @emph{... LilyPond input code with} @code{#$var1} @emph{for substition ...}
105   #@})
106 @end example
107
108 Note that the special characters @code{#@{} and @code{#@}} surround the
109 LilyPond music.
110
111 @multitable @columnfractions .33 .66
112 @item @var{vari}         @tab @var{i}th variable
113 @item @var{vari-type?}   @tab type of @var{i}th variable
114 @item @var{...music...}  @tab normal LilyPond input, using
115  variables as @code{#$var1}, etc.
116 @end multitable
117
118 For example, a function can be defined that simplifies
119 setting the padding of a TextScript:
120
121 @lilypond[quote,verbatim,ragged-right]
122 padText = #(define-music-function (parser location padding) (number?)
123   #{
124     \once \override TextScript #'padding = #$padding
125   #})
126
127 \relative c''' {
128   c4^"piu mosso" b a b
129   \padText #1.8
130   c4^"piu mosso" d e f
131   \padText #2.6
132   c4^"piu mosso" fis a g
133 }
134 @end lilypond
135
136 In addition to numbers, we can use music expressions such
137 as notes for arguments to music functions:
138
139 @lilypond[quote,verbatim,ragged-right]
140 custosNote = #(define-music-function (parser location note)
141                                      (ly:music?)
142   #{
143     \once \override Voice.NoteHead #'stencil =
144       #ly:text-interface::print
145     \once \override Voice.NoteHead #'text =
146       \markup \musicglyph #"custodes.mensural.u0"
147     \once \override Voice.Stem #'stencil = ##f
148     $note
149   #})
150 @end lilypond
151
152 @node Intermediate substitution functions
153 @subsection Intermediate substitution functions
154
155 Slightly more complicated than simple substitution function,
156 intermediate substitution functions involve a mix of Scheme code and
157 LilyPond code in the music expression to be
158 returned.
159
160 Some @code{\override} commands require an argument consisting of
161 a pair of numbers (called a @code{cons cell} in Scheme).
162
163 The pair can be directly passed into the music function,
164 using a @code{pair?} variable:
165
166 @quotation
167 @example
168 manualBeam =
169 #(define-music-function (parser location beg-end)
170                         (pair?)
171 #@{
172   \once \override Beam #'positions = #$beg-end
173 #@})
174
175 \relative @{
176   \manualBeam #'(3 . 6) c8 d e f
177 @}
178 @end example
179 @end quotation
180
181 Alternatively, the numbers making up the pair can be
182 passed as separate arguments, and the Scheme code
183 used to create the pair can be included in the
184 music expression:
185
186 @lilypond[quote,verbatim,ragged-right]
187 manualBeam =
188 #(define-music-function (parser location beg end)
189                         (number? number?)
190 #{
191   \once \override Beam #'positions = #(cons $beg $end)
192 #})
193
194 \relative {
195   \manualBeam #3 #6 c8 d e f
196 }
197 @end lilypond
198
199
200 @node Mathematics in functions
201 @subsection Mathematics in functions
202
203 Music functions can involve Scheme programming in
204 addition to simple substitution,
205
206 @lilypond[quote,verbatim,ragged-right]
207 AltOn = #(define-music-function (parser location mag) (number?)
208   #{ \override Stem #'length = #$(* 7.0 mag)
209      \override NoteHead #'font-size =
210        #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag))) #})
211
212 AltOff = {
213   \revert Stem #'length
214   \revert NoteHead #'font-size
215 }
216
217 { c'2 \AltOn #0.5 c'4 c'
218   \AltOn #1.5 c' c' \AltOff c'2 }
219 @end lilypond
220
221 @noindent
222 This example may be rewritten to pass in music expressions,
223
224 @lilypond[quote,verbatim,ragged-right]
225 withAlt = #(define-music-function (parser location mag music) (number? ly:music?)
226   #{ \override Stem #'length = #$(* 7.0 mag)
227      \override NoteHead #'font-size =
228        #$(inexact->exact (* (/ 6.0 (log 2.0)) (log mag)))
229      $music
230      \revert Stem #'length
231      \revert NoteHead #'font-size #})
232
233 { c'2 \withAlt #0.5 {c'4 c'}
234   \withAlt #1.5 {c' c'} c'2 }
235 @end lilypond
236
237 @node Void functions
238 @subsection Void functions
239
240 A music function must return a music expression, but sometimes we
241 may want to have a function that does not involve music (such as
242 turning off Point and Click).  To do this, we return a @code{void}
243 music expression.
244
245 That is why the form
246 that is returned is the @code{(make-music ...)}.  With the
247 @code{'void} property set to @code{#t}, the parser is told to
248 actually disregard this returned music
249 expression.  Thus the important part of the void music function is the
250 processing done by the function, not the music expression that is
251 returned.
252
253 @example
254 noPointAndClick =
255 #(define-music-function (parser location) ()
256    (ly:set-option 'point-and-click #f)
257    (make-music 'SequentialMusic 'void #t))
258 ...
259 \noPointAndClick   % disable point and click
260 @end example
261
262
263 @node Functions without arguments
264 @subsection Functions without arguments
265
266 In most cases a function without arguments should be written
267 with a variable,
268
269 @example
270 dolce = \markup@{ \italic \bold dolce @}
271 @end example
272
273 However, in rare cases it may be useful to create a music function
274 without arguments,
275
276 @example
277 displayBarNum =
278 #(define-music-function (parser location) ()
279    (if (eq? #t (ly:get-option 'display-bar-numbers))
280        #@{ \once \override Score.BarNumber #'break-visibility = ##f #@}
281        #@{#@}))
282 @end example
283
284 To actually display bar numbers where this function is called,
285 invoke @command{lilypond} with
286
287 @example
288 lilypond -d display-bar-numbers FILENAME.ly
289 @end example
290
291
292
293 @node Markup functions
294 @section Markup functions
295
296 Markups are implemented as special Scheme functions which produce a
297 @code{Stencil} object given a number of arguments.
298
299 @menu
300 * Markup construction in Scheme::
301 * How markups work internally::
302 * New markup command definition::
303 * New markup list command definition::
304 @end menu
305
306
307 @node Markup construction in Scheme
308 @subsection Markup construction in Scheme
309
310 @cindex defining markup commands
311
312 The @code{markup} macro builds markup expressions in Scheme while
313 providing a LilyPond-like syntax.  For example,
314 @example
315 (markup #:column (#:line (#:bold #:italic "hello" #:raise 0.4 "world")
316                   #:larger #:line ("foo" "bar" "baz")))
317 @end example
318
319 @noindent
320 is equivalent to:
321 @example
322 \markup \column @{ \line @{ \bold \italic "hello" \raise #0.4 "world" @}
323                   \larger \line @{ foo bar baz @} @}
324 @end example
325
326 @noindent
327 This example demonstrates the main translation rules between regular
328 LilyPond markup syntax and Scheme markup syntax.
329
330 @quotation
331 @multitable @columnfractions .3 .3
332 @item @b{LilyPond} @tab @b{Scheme}
333 @item @code{\markup markup1} @tab @code{(markup markup1)}
334 @item @code{\markup @{ markup1 markup2 ... @}} @tab
335         @code{(markup markup1 markup2 ... )}
336 @item @code{\markup-command} @tab @code{#:markup-command}
337 @item @code{\variable} @tab @code{variable}
338 @item @code{\center-column @{ ... @}} @tab @code{#:center-column ( ... )}
339 @item @code{string} @tab @code{"string"}
340 @item @code{#scheme-arg} @tab @code{scheme-arg}
341 @end multitable
342 @end quotation
343
344 The whole Scheme language is accessible inside the
345 @code{markup} macro.  For example, You may use function calls inside
346 @code{markup} in order to manipulate character strings.  This is
347 useful when defining new markup commands (see
348 @ref{New markup command definition}).
349
350
351 @knownissues
352
353 The markup-list argument of commands such as @code{#:line},
354 @code{#:center}, and @code{#:column} cannot be a variable or
355 the result of a function call.
356
357 @lisp
358 (markup #:line (function-that-returns-markups))
359 @end lisp
360
361 @noindent
362 is invalid.  One should use the @code{make-line-markup},
363 @code{make-center-markup}, or @code{make-column-markup} functions
364 instead,
365
366 @lisp
367 (markup (make-line-markup (function-that-returns-markups)))
368 @end lisp
369
370
371 @node How markups work internally
372 @subsection How markups work internally
373
374 In a markup like
375
376 @example
377 \raise #0.5 "text example"
378 @end example
379
380 @noindent
381 @code{\raise} is actually represented by the @code{raise-markup}
382 function.  The markup expression is stored as
383
384 @example
385 (list raise-markup 0.5 (list simple-markup "text example"))
386 @end example
387
388 When the markup is converted to printable objects (Stencils), the
389 @code{raise-markup} function is called as
390
391 @example
392 (apply raise-markup
393        @var{\layout object}
394        @var{list of property alists}
395        0.5
396        @var{the "text example" markup})
397 @end example
398
399 The @code{raise-markup} function first creates the stencil for the
400 @code{text example} string, and then it raises that Stencil by 0.5
401 staff space.  This is a rather simple example; more complex examples
402 are in the rest
403 of this section, and in @file{scm/@/define@/-markup@/-commands@/.scm}.
404
405
406 @node New markup command definition
407 @subsection New markup command definition
408
409 New markup commands can be defined
410 with the @code{define-markup-command} Scheme macro.
411
412 @lisp
413 (define-markup-command (@var{command-name} @var{layout} @var{props} @var{arg1} @var{arg2} ...)
414             (@var{arg1-type?} @var{arg2-type?} ...)
415   ..command body..)
416 @end lisp
417
418 The arguments are
419
420 @table @var
421 @item argi
422 @var{i}th command argument
423 @item argi-type?
424 a type predicate for the i@var{th} argument
425 @item layout
426 the @q{layout} definition
427 @item props
428 a list of alists, containing all active properties.
429 @end table
430
431 As a simple example, we show how to add a @code{\smallcaps} command,
432 which selects a small caps font.  Normally we could select the
433 small caps font,
434
435 @example
436 \markup @{ \override #'(font-shape . caps) Text-in-caps @}
437 @end example
438
439 @noindent
440 This selects the caps font by setting the @code{font-shape} property to
441 @code{#'caps} for interpreting @code{Text-in-caps}.
442
443 To make the above available as @code{\smallcaps} command, we must
444 define a function using @code{define-markup-command}.  The command should
445 take a single argument of type @code{markup}.  Therefore the start of the
446 definition should read
447
448 @example
449 (define-markup-command (smallcaps layout props argument) (markup?)
450 @end example
451
452 @noindent
453
454 What follows is the content of the command: we should interpret
455 the @code{argument} as a markup, i.e.,
456
457 @example
458 (interpret-markup layout @dots{} argument)
459 @end example
460
461 @noindent
462 This interpretation should add @code{'(font-shape . caps)} to the active
463 properties, so we substitute the following for the @dots{} in the
464 above example:
465
466 @example
467 (cons (list '(font-shape . caps) ) props)
468 @end example
469
470 @noindent
471 The variable @code{props} is a list of alists, and we prepend to it by
472 cons'ing a list with the extra setting.
473
474
475 Suppose that we are typesetting a recitative in an opera and
476 we would like to define a command that will show character names in a
477 custom manner.  Names should be printed with small caps and moved a
478 bit up and to the left.  We will define a @code{\character} command
479 which takes into account the necessary translation and uses the newly
480 defined @code{\smallcaps} command:
481
482 @example
483 #(define-markup-command (character layout props name) (string?)
484   "Print the character name in small caps, translated to the left and
485   top.  Syntax: \\character #\"name\""
486   (interpret-markup layout props
487    (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
488 @end example
489
490 There is one complication that needs explanation: texts above and below
491 the staff are moved vertically to be at a certain distance (the
492 @code{padding} property) from the staff and the notes.  To make sure
493 that this mechanism does not annihilate the vertical effect of our
494 @code{#:translate}, we add an empty string (@code{#:hspace 0}) before the
495 translated text.  Now the @code{#:hspace 0} will be put above the notes,
496 and the
497 @code{name} is moved in relation to that empty string.  The net effect is
498 that the text is moved to the upper left.
499
500 The final result is as follows:
501
502 @example
503 @{
504   c''^\markup \character #"Cleopatra"
505   e'^\markup \character #"Giulio Cesare"
506 @}
507 @end example
508
509 @lilypond[quote,ragged-right]
510 #(define-markup-command (smallcaps layout props str) (string?)
511   "Print the string argument in small caps.  Syntax: \\smallcaps #\"string\""
512   (interpret-markup layout props
513    (make-line-markup
514     (map (lambda (s)
515           (if (= (string-length s) 0)
516               s
517               (markup #:large (string-upcase (substring s 0 1))
518                       #:translate (cons -0.6 0)
519                       #:tiny (string-upcase (substring s 1)))))
520          (string-split str #\Space)))))
521
522 #(define-markup-command (character layout props name) (string?)
523   "Print the character name in small caps, translated to the left and
524   top.  Syntax: \\character #\"name\""
525   (interpret-markup layout props
526    (markup #:hspace 0 #:translate (cons -3 1) #:smallcaps name)))
527
528 {
529   c''^\markup \character #"Cleopatra" c'' c'' c''
530   e'^\markup \character #"Giulio Cesare" e' e' e'
531 }
532 @end lilypond
533
534 We have used the @code{caps} font shape, but suppose that our font
535 does not have a small-caps variant.  In that case we have to fake
536 the small caps font by setting a string in uppercase with the
537 first letter a little larger:
538
539 @example
540 #(define-markup-command (smallcaps layout props str) (string?)
541   "Print the string argument in small caps."
542   (interpret-markup layout props
543    (make-line-markup
544     (map (lambda (s)
545           (if (= (string-length s) 0)
546               s
547               (markup #:large (string-upcase (substring s 0 1))
548                       #:translate (cons -0.6 0)
549                       #:tiny (string-upcase (substring s 1)))))
550          (string-split str #\Space)))))
551 @end example
552
553 The @code{smallcaps} command first splits its string argument into
554 tokens separated by spaces (@code{(string-split str #\Space)}); for
555 each token, a markup is built with the first letter made large and
556 upcased (@code{#:large (string-upcase (substring s 0 1))}), and a
557 second markup built with the following letters made tiny and upcased
558 (@code{#:tiny (string-upcase (substring s 1))}).  As LilyPond
559 introduces a space between markups on a line, the second markup is
560 translated to the left (@code{#:translate (cons -0.6 0) ...}).  Then,
561 the markups built for each token are put in a line by
562 @code{(make-line-markup ...)}.  Finally, the resulting markup is passed
563 to the @code{interpret-markup} function, with the @code{layout} and
564 @code{props} arguments.
565
566 Note: there is now an internal command @code{\smallCaps} which can
567 be used to set text in small caps.  See
568 @ruser{Text markup commands}, for details.
569
570 @knownissues
571
572 Currently, the available combinations of arguments (after the standard
573 @var{layout} and @var{props} arguments) to a markup command defined with
574 @code{define-markup-command} are limited as follows.
575
576 @table @asis
577 @item (no argument)
578 @itemx @var{list}
579 @itemx @var{markup}
580 @itemx @var{markup markup}
581 @itemx @var{scm}
582 @itemx @var{scm markup}
583 @itemx @var{scm scm}
584 @itemx @var{scm scm markup}
585 @itemx @var{scm scm markup markup}
586 @itemx @var{scm markup markup}
587 @itemx @var{scm scm scm}
588 @end table
589
590 @noindent
591 In the above table, @var{scm} represents native Scheme data types like
592 @q{number} or @q{string}.
593
594 As an example, it is not possible to use a markup command @code{foo} with
595 four arguments defined as
596
597 @example
598 #(define-markup-command (foo layout props
599                          num1    str1    num2    str2)
600                         (number? string? number? string?)
601   ...)
602 @end example
603
604 @noindent
605 If you apply it as, say,
606
607 @example
608 \markup \foo #1 #"bar" #2 #"baz"
609 @end example
610
611 @cindex Scheme signature
612 @cindex signature, Scheme
613 @noindent
614 @command{lilypond} complains that it cannot parse @code{foo} due to its
615 unknown Scheme signature.
616
617
618 @node New markup list command definition
619 @subsection New markup list command definition
620 Markup list commands are defined with the
621 @code{define-markup-list-command} Scheme macro, which is similar to the
622 @code{define-markup-command} macro described in
623 @ref{New markup command definition}, except that where the latter returns
624 a single stencil, the former returns a list of stencils.
625
626 In the following example, a @code{\paragraph} markup list command is
627 defined, which returns a list of justified lines, the first one being
628 indented.  The indent width is taken from the @code{props} argument.
629 @example
630 #(define-markup-list-command (paragraph layout props args) (markup-list?)
631    (let ((indent (chain-assoc-get 'par-indent props 2)))
632      (interpret-markup-list layout props
633        (make-justified-lines-markup-list (cons (make-hspace-markup indent)
634                                                args)))))
635 @end example
636
637 Besides the usual @code{layout} and @code{props} arguments, the
638 @code{paragraph} markup list command takes a markup list argument, named
639 @code{args}.  The predicate for markup lists is @code{markup-list?}.
640
641 First, the function gets the indent width, a property here named
642 @code{par-indent}, from the property list @code{props}.  If the
643 property is not found, the default value is @code{2}.  Then, a
644 list of justified lines is made using the
645 @code{make-justified-lines-markup-list} function, which is related
646 to the @code{\justified-lines} built-in markup list command.  A
647 horizontal space is added at the beginning using the
648 @code{make-hspace-markup} function.  Finally, the markup list is
649 interpreted using the @code{interpret-markup-list} function.
650
651 This new markup list command can be used as follows:
652 @example
653 \markuplines @{
654   \paragraph @{
655     The art of music typography is called \italic @{(plate) engraving.@}
656     The term derives from the traditional process of music printing.
657     Just a few decades ago, sheet music was made by cutting and stamping
658     the music into a zinc or pewter plate in mirror image.
659   @}
660   \override-lines #'(par-indent . 4) \paragraph @{
661     The plate would be inked, the depressions caused by the cutting
662     and stamping would hold ink.  An image was formed by pressing paper
663     to the plate.  The stamping and cutting was completely done by
664     hand.
665   @}
666 @}
667 @end example
668
669 @node Contexts for programmers
670 @section Contexts for programmers
671
672 @menu
673 * Context evaluation::
674 * Running a function on all layout objects::
675 @end menu
676
677 @node Context evaluation
678 @subsection Context evaluation
679
680 @cindex calling code during interpreting
681 @funindex \applyContext
682
683 Contexts can be modified during interpretation with Scheme code.  The
684 syntax for this is
685 @example
686 \applyContext @var{function}
687 @end example
688
689 @var{function} should be a Scheme function that takes a single
690 argument: the context in which the @code{\applyContext} command is
691 being called.  The following code will print the current bar
692 number on the standard output during the compile:
693
694 @example
695 \applyContext
696   #(lambda (x)
697     (format #t "\nWe were called in barnumber ~a.\n"
698      (ly:context-property x 'currentBarNumber)))
699 @end example
700
701
702
703 @node Running a function on all layout objects
704 @subsection Running a function on all layout objects
705
706
707 @cindex calling code on layout objects
708 @funindex \applyOutput
709
710
711 The most versatile way of tuning an object is @code{\applyOutput}.  Its
712 syntax is
713 @example
714 \applyOutput @var{context} @var{proc}
715 @end example
716
717 @noindent
718 where @var{proc} is a Scheme function, taking three arguments.
719
720 When interpreted, the function @var{proc} is called for every layout
721 object found in the context @var{context}, with the following
722 arguments:
723 @itemize
724 @item the layout object itself,
725 @item the context where the layout object was created, and
726 @item the context where @code{\applyOutput} is processed.
727 @end itemize
728
729
730 In addition, the cause of the layout object, i.e., the music
731 expression or object that was responsible for creating it, is in the
732 object property @code{cause}.  For example, for a note head, this is a
733 @rinternals{NoteHead} event, and for a @rinternals{Stem} object,
734 this is a @rinternals{NoteHead} object.
735
736 Here is a function to use for @code{\applyOutput}; it blanks
737 note-heads on the center-line:
738
739 @lilypond[quote,verbatim,ragged-right]
740 #(define (blanker grob grob-origin context)
741    (if (and (memq 'note-head-interface (ly:grob-interfaces grob))
742             (eq? (ly:grob-property grob 'staff-position) 0))
743        (set! (ly:grob-property grob 'transparent) #t)))
744
745 \relative {
746   e4 g8 \applyOutput #'Voice #blanker b d2
747 }
748 @end lilypond
749
750
751 @node Callback functions
752 @section Callback functions
753
754 Properties (like @code{thickness}, @code{direction}, etc.) can be
755 set at fixed values with @code{\override}, e.g.
756
757 @example
758 \override Stem #'thickness = #2.0
759 @end example
760
761 Properties can also be set to a Scheme procedure,
762
763 @lilypond[fragment,verbatim,quote,relative=2]
764 \override Stem #'thickness = #(lambda (grob)
765     (if (= UP (ly:grob-property grob 'direction))
766         2.0
767         7.0))
768 c b a g b a g b
769 @end lilypond
770
771 @noindent
772 In this case, the procedure is executed as soon as the value of the
773 property is requested during the formatting process.
774
775 Most of the typesetting engine is driven by such callbacks.
776 Properties that typically use callbacks include
777
778 @table @code
779 @item stencil
780   The printing routine, that constructs a drawing for the symbol
781 @item X-offset
782   The routine that sets the horizontal position
783 @item X-extent
784   The routine that computes the width of an object
785 @end table
786
787 The procedure always takes a single argument, being the grob.
788
789 If routines with multiple arguments must be called, the current grob
790 can be inserted with a grob closure.  Here is a setting from
791 @code{AccidentalSuggestion},
792
793 @example
794 (X-offset .
795   ,(ly:make-simple-closure
796     `(,+
797         ,(ly:make-simple-closure
798            (list ly:self-alignment-interface::centered-on-x-parent))
799       ,(ly:make-simple-closure
800            (list ly:self-alignment-interface::x-aligned-on-self)))))
801 @end example
802
803 @noindent
804 In this example, both @code{ly:self-alignment-interface::x-aligned-on-self} and
805 @code{ly:self-alignment-interface::centered-on-x-parent} are called
806 with the grob as argument.  The results are added with the @code{+}
807 function.  To ensure that this addition is properly executed, the whole
808 thing is enclosed in @code{ly:make-simple-closure}.
809
810 In fact, using a single procedure as property value is equivalent to
811
812 @example
813 (ly:make-simple-closure (ly:make-simple-closure (list @var{proc})))
814 @end example
815
816 @noindent
817 The inner @code{ly:make-simple-closure} supplies the grob as argument
818 to @var{proc}, the outer ensures that result of the function is
819 returned, rather than the @code{simple-closure} object.
820
821
822 @node Inline Scheme code
823 @section Inline Scheme code
824
825 The main disadvantage of @code{\tweak} is its syntactical
826 inflexibility.  For example, the following produces a syntax error.
827
828 @example
829 F = \tweak #'font-size #-3 -\flageolet
830
831 \relative c'' @{
832   c4^\F c4_\F
833 @}
834 @end example
835
836 @noindent
837 In other words, @code{\tweak} doesn't behave like an articulation
838 regarding the syntax; in particular, it can't be attached with
839 @code{^} and @code{_}.
840
841 Using Scheme, this problem can be avoided.  The route to the
842 result is given in @ref{Adding articulation to notes (example)},
843 especially how to use @code{\displayMusic} as a helping guide.
844
845 @example
846 F = #(let ((m (make-music 'ArticulationEvent
847                           'articulation-type "flageolet")))
848        (set! (ly:music-property m 'tweaks)
849              (acons 'font-size -3
850                     (ly:music-property m 'tweaks)))
851        m)
852
853 \relative c'' @{
854   c4^\F c4_\F
855 @}
856 @end example
857
858 @noindent
859 Here, the @code{tweaks} properties of the flageolet object
860 @code{m} (created with @code{make-music}) are extracted with
861 @code{ly:music-property}, a new key-value pair to change the
862 font size is prepended to the property list with the
863 @code{acons} Scheme function, and the result is finally
864 written back with @code{set!}.  The last element of the
865 @code{let} block is the return value, @code{m} itself.
866
867
868
869 @node Difficult tweaks
870 @section Difficult tweaks
871
872 There are a few classes of difficult adjustments.
873
874 @itemize
875
876
877 @item
878 One type of difficult adjustment involves the appearance of
879 spanner objects, such as slurs and ties.  Usually, only one
880 spanner object is created at a time, and it can be adjusted with
881 the normal mechanism.  However, occasionally a spanner crosses a
882 line break.  When this happens, the object is cloned.  A separate
883 object is created for every system in which the spanner appears.
884 The new objects are clones of the original object and inherit all
885 properties, including @code{\override}s.
886
887
888 In other words, an @code{\override} always affects all pieces of a
889 broken spanner.  To change only one part of a spanner at a line break,
890 it is necessary to hook into the formatting process.  The
891 @code{after-line-breaking} callback contains the Scheme procedure that
892 is called after the line breaks have been determined and layout
893 objects have been split over different systems.
894
895 In the following example, we define a procedure
896 @code{my-callback}.  This procedure
897
898 @itemize
899 @item
900 determines if the spanner has been split across line breaks
901 @item
902 if yes, retrieves all the split objects
903 @item
904 checks if this grob is the last of the split objects
905 @item
906 if yes, it sets @code{extra-offset}.
907 @end itemize
908
909 This procedure is installed into @rinternals{Tie}, so the last part
910 of the broken tie is repositioned.
911
912 @lilypond[quote,verbatim,ragged-right]
913 #(define (my-callback grob)
914   (let* (
915          ; have we been split?
916          (orig (ly:grob-original grob))
917
918          ; if yes, get the split pieces (our siblings)
919          (siblings (if (ly:grob? orig)
920                      (ly:spanner-broken-into orig) '() )))
921
922    (if (and (>= (length siblings) 2)
923              (eq? (car (last-pair siblings)) grob))
924      (ly:grob-set-property! grob 'extra-offset '(-2 . 5)))))
925
926 \relative c'' {
927   \override Tie #'after-line-breaking =
928   #my-callback
929   c1 ~ \break c2 ~ c
930 }
931 @end lilypond
932
933 @noindent
934 When applying this trick, the new @code{after-line-breaking} callback
935 should also call the old one @code{after-line-breaking}, if there is
936 one.  For example, if using this with @code{Hairpin},
937 @code{ly:hairpin::after-line-breaking} should also be called.
938
939
940 @item Some objects cannot be changed with @code{\override} for
941 technical reasons.  Examples of those are @code{NonMusicalPaperColumn}
942 and @code{PaperColumn}.  They can be changed with the
943 @code{\overrideProperty} function, which works similar to @code{\once
944 \override}, but uses a different syntax.
945
946 @example
947 \overrideProperty
948 #"Score.NonMusicalPaperColumn"  % Grob name
949 #'line-break-system-details     % Property name
950 #'((next-padding . 20))         % Value
951 @end example
952
953 Note, however, that @code{\override}, applied to
954 @code{NonMusicalPaperColumn} and @code{PaperColumn}, still works as
955 expected within @code{\context} blocks.
956
957 @end itemize
958
959 @node LilyPond Scheme interfaces
960 @chapter LilyPond Scheme interfaces
961
962 This chapter covers the various tools provided by LilyPond to help
963 Scheme programmers get information into and out of the music streams.
964
965 TODO -- figure out what goes in here and how to organize it
966