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