]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/extending/scheme-tutorial.itely
9b055dcba6475654ce457bc1fcaaf3860d04de01
[lilypond.git] / Documentation / extending / scheme-tutorial.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.15.20"
12
13 @node Scheme tutorial
14 @chapter Scheme tutorial
15
16 @cindex Scheme
17 @cindex GUILE
18 @cindex Scheme, in-line code
19 @cindex accessing Scheme
20 @cindex evaluating Scheme
21 @cindex LISP
22
23 LilyPond uses the Scheme programming language, both as part of the
24 input syntax, and as internal mechanism to glue modules of the program
25 together.  This section is a very brief overview of entering data in
26 Scheme.  If you want to know more about Scheme, see
27 @uref{http://@/www@/.schemers@/.org}.
28
29 LilyPond uses the GNU Guile implementation of Scheme, which is
30 based on the Scheme @qq{R5RS} standard.  If you are learning Scheme
31 to use with LilyPond, working with a different implementation (or
32 referring to a different standard) is not recommended.  Information
33 on guile can be found at @uref{http://www.gnu.org/software/guile/}.
34 The @qq{R5RS} Scheme standard is located at
35 @uref{http://www.schemers.org/Documents/Standards/R5RS/}.
36
37 @menu
38 * Introduction to Scheme::
39 * Scheme in LilyPond::
40 * Building complicated functions::
41 @end menu
42
43 @node Introduction to Scheme
44 @section Introduction to Scheme
45
46 We begin with an introduction to Scheme.  For this brief introduction,
47 we will use the GUILE interpreter to explore how the language works.
48 Once we are familiar with Scheme, we will show how the language can
49 be integrated in LilyPond files.
50
51
52 @menu
53 * Scheme sandbox::
54 * Scheme variables::
55 * Scheme simple data types::
56 * Scheme compound data types::
57 * Calculations in Scheme::
58 * Scheme procedures::
59 * Scheme conditionals::
60 @end menu
61
62 @node Scheme sandbox
63 @subsection Scheme sandbox
64
65 The LilyPond installation includes the Guile implementation of
66 Scheme.  On most systems you can experiment in a Scheme sandbox by
67 opening a terminal window and typing @q{guile}.  On some systems,
68 notably Windows, you may need to set the environment variable
69 @code{GUILE_LOAD_PATH} to the directory @code{../usr/share/guile/1.8}
70 in the LilyPond installation.  For the full path to this directory
71 see @rlearning{Other sources of information}.  Alternatively, Windows
72 users may simply choose @q{Run} from the Start menu and enter
73 @q{guile}.
74
75 However, a hands-on Scheme sandbox with all of Lilypond loaded is
76 available with this command line:
77 @example
78 lilypond scheme-sandbox
79 @end example
80
81 @noindent
82 Once the sandbox is running, you will receive a guile prompt:
83
84 @lisp
85 guile>
86 @end lisp
87
88 You can enter Scheme expressions at this prompt to experiment with
89 Scheme.  If you want to be able to use the GNU readline library for
90 nicer editing of the Scheme command line, check the file
91 @file{ly/scheme-sandbox.ly} for more information.  If you already have
92 enabled the readline library for your interactive Guile sessions outside
93 of LilyPond, this should work in the sandbox as well.
94
95 @node Scheme variables
96 @subsection Scheme variables
97
98 Scheme variables can have any valid scheme value, including a Scheme
99 procedure.
100
101 Scheme variables are created with @code{define}:
102
103 @lisp
104 guile> (define a 2)
105 guile>
106 @end lisp
107
108 Scheme variables can be evaluated at the guile prompt simply by
109 typing the variable name:
110
111 @lisp
112 guile> a
113 2
114 guile>
115 @end lisp
116
117 Scheme variables can be printed on the display by using the display function:
118
119 @lisp
120 guile> (display a)
121 2guile>
122 @end lisp
123
124 @noindent
125 Note that both the value @code{2} and the guile prompt @code{guile}
126 showed up on the same line.  This can be avoided by calling the
127 newline procedure or displaying a newline character.
128
129 @lisp
130 guile> (display a)(newline)
131 2
132 guile> (display a)(display "\n")
133 2
134 guile>
135 @end lisp
136
137 Once a variable has been created, its value can be changed with @code{set!}:
138
139 @lisp
140 guile> (set! a 12345)
141 guile> a
142 12345
143 guile>
144 @end lisp
145
146 @node Scheme simple data types
147 @subsection Scheme simple data types
148
149 The most basic concept in a language is data typing: numbers, character
150 strings, lists, etc.  Here is a list of simple Scheme data types that are
151 often used with LilyPond.
152
153 @table @asis
154 @item Booleans
155 Boolean values are True or False.  The Scheme for True is @code{#t}
156 and False is @code{#f}.
157 @funindex ##t
158 @funindex ##f
159
160 @item Numbers
161 Numbers are entered in the standard fashion,
162 @code{1} is the (integer) number one, while @w{@code{-1.5}} is a
163 floating point number (a non-integer number).
164
165 @item Strings
166 Strings are enclosed in double quotes:
167
168 @example
169 "this is a string"
170 @end example
171
172 Strings may span several lines:
173
174 @example
175 "this
176 is
177 a string"
178 @end example
179
180 @noindent
181 and the newline characters at the end of each line will be included
182 in the string.
183
184 Newline characters can also be added by including @code{\n} in the
185 string.
186
187 @example
188 "this\nis a\nmultiline string"
189 @end example
190
191
192 Quotation marks and backslashes are added to strings
193 by preceding them with a backslash.
194 The string @code{\a said "b"} is entered as
195
196 @example
197 "\\a said \"b\""
198 @end example
199
200 @end table
201
202 There are additional Scheme data types that are not discussed here.
203 For a complete listing see the Guile reference guide,
204 @uref{http://www.gnu.org/software/guile/manual/html_node/Simple-Data-Types.html}.
205
206 @node Scheme compound data types
207 @subsection Scheme compound data types
208
209 There are also compound data types in Scheme.  The  types commonly used in
210 LilyPond programming include pairs, lists, alists, and hash tables.
211
212 @subheading Pairs
213
214 The foundational compound data type of Scheme is the @code{pair}.  As
215 might be expected from its name, a pair is two values glued together.
216 The operator used to form a pair is called @code{cons}.
217
218 @lisp
219 guile> (cons 4 5)
220 (4 . 5)
221 guile>
222 @end lisp
223
224 Note that the pair is displayed as two items surrounded by
225 parentheses and separated by whitespace, a period (@code{.}), and
226 more whitespace.  The period is @emph{not} a decimal point, but
227 rather an indicator of the pair.
228
229 Pairs can also be entered as literal values by preceding them with
230 a single quote character.
231
232 @lisp
233 guile> '(4 . 5)
234 (4 . 5)
235 guile>
236 @end lisp
237
238 The two elements of a pair may be any valid Scheme value:
239
240 @lisp
241 guile> (cons #t #f)
242 (#t . #f)
243 guile> '("blah-blah" . 3.1415926535)
244 ("blah-blah" . 3.1415926535)
245 guile>
246 @end lisp
247
248 The first and second elements of the pair can be accessed by the
249 Scheme procedures @code{car} and @code{cdr}, respectively.
250
251 @lisp
252 guile> (define mypair (cons 123 "hello there")
253 ... )
254 guile> (car mypair)
255 123
256 guile> (cdr mypair)
257 "hello there"
258 guile>
259 @end lisp
260
261 @noindent
262
263 Note:  @code{cdr} is pronounced "could-er", according Sussman and
264 Abelson, see
265 @uref{http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#footnote_Temp_133}
266
267 @subheading Lists
268
269 A very common Scheme data structure is the @emph{list}.  Formally, a
270 list is defined as either the empty list (represented as @code{'()},
271 or a pair whose @code{cdr} is a list.
272
273 There are many ways of creating lists.  Perhaps the most common is
274 with the @code{list} procedure:
275
276 @lisp
277 guile> (list 1 2 3 "abc" 17.5)
278 (1 2 3 "abc" 17.5)
279 @end lisp
280
281 As can be seen, a list is displayed in the form of individual elements
282 separated by whitespace and enclosed in parentheses.  Unlike a pair,
283 there is no period between the elements.
284
285 A list can also be entered as a literal list by enclosing its
286 elements in parentheses, and adding a quote:
287
288 @lisp
289 guile> '(17 23 "foo" "bar" "bazzle")
290 (17 23 "foo" "bar" "bazzle")
291 @end lisp
292
293 Lists are a central part of Scheme.  In, fact, Scheme is considered
294 a dialect of lisp, where @q{lisp} is an abbreviation for
295 @q{List Processing}.  Scheme expressions are all lists.
296
297 @subheading Association lists (alists)
298
299 A special type of list is an @emph{association list} or @emph{alist}.
300 An alist is used to store data for easy retrieval.
301
302 Alists are lists whose elements are pairs.  The @code{car} of each
303 element is called the @emph{key}, and the @code{cdr} of each element
304 is called the @emph{value}.  The Scheme procedure @code{assoc} is
305 used to retrieve an entry from the alist, and @code{cdr} is used to
306 retrieve the value:
307
308 @lisp
309 guile> (define my-alist '((1  . "A") (2 . "B") (3 . "C")))
310 guile> my-alist
311 ((1 . "A") (2 . "B") (3 . "C"))
312 guile> (assoc 2 my-alist)
313 (2 . "B")
314 guile> (cdr (assoc 2 my-alist))
315 "B"
316 guile>
317 @end lisp
318
319 Alists are widely used in LilyPond to store properties and other data.
320
321 @subheading Hash tables
322
323 A data structure that is used occasionally in LilyPond.  A hash table
324 is similar to an array, but the indexes to the array can be any type
325 of Scheme value, not just integers.
326
327 Hash tables are more efficient than alists if there is a lot of data
328 to store and the data changes very infrequently.
329
330 The syntax to create hash tables is a bit complex, but you
331 can see examples of it in the LilyPond source.
332
333 @lisp
334 guile> (define h (make-hash-table 10))
335 guile> h
336 #<hash-table 0/31>
337 guile> (hashq-set! h 'key1 "val1")
338 "val1"
339 guile> (hashq-set! h 'key2 "val2")
340 "val2"
341 guile> (hashq-set! h 3 "val3")
342 "val3"
343 @end lisp
344
345 Values are retrieved from hash tables with @code{hashq-ref}.
346
347 @lisp
348 guile> (hashq-ref h 3)
349 "val3"
350 guile> (hashq-ref h 'key2)
351 "val2"
352 guile>
353 @end lisp
354
355 Keys and values are retrieved as a pair with @code{hashq-get-handle}.
356 This is a preferred way, because it will return @code{#f} if a key is
357 not found.
358
359 @lisp
360 guile> (hashq-get-handle h 'key1)
361 (key1 . "val1")
362 guile> (hashq-get-handle h 'frob)
363 #f
364 guile>
365 @end lisp
366
367 @node Calculations in Scheme
368 @subsection Calculations in Scheme
369
370 @ignore
371 We have been using lists all along.  A calculation, like @code{(+ 1 2)}
372 is also a list (containing the symbol @code{+} and the numbers 1
373 and@tie{}2).  Normally lists are interpreted as calculations, and the
374 Scheme interpreter substitutes the outcome of the calculation.  To enter a
375 list, we stop the evaluation.  This is done by quoting the list with a
376 quote @code{'} symbol.  So, for calculations do not use a quote.
377
378 Inside a quoted list or pair, there is no need to quote anymore.  The
379 following is a pair of symbols, a list of symbols and a list of lists
380 respectively,
381
382 @example
383 #'(stem . head)
384 #'(staff clef key-signature)
385 #'((1) (2))
386 @end example
387 @end ignore
388
389 Scheme can be used to do calculations.  It uses @emph{prefix}
390 syntax.  Adding 1 and@tie{}2 is written as @code{(+ 1 2)} rather than the
391 traditional @math{1+2}.
392
393 @lisp
394 guile> (+ 1 2)
395 3
396 @end lisp
397
398 Calculations may be nested; the result of a function may
399 be used for another calculation.
400
401 @lisp
402 guile> (+ 1 (* 3 4))
403 13
404 @end lisp
405
406 These calculations are examples of evaluations; an expression like
407 @code{(* 3 4)} is replaced by its value @code{12}.
408
409 Scheme calculations are sensitive to the differences between integers
410 and non-integers.  Integer calculations are exact, while non-integers
411 are calculated to the appropriate limits of precision:
412
413 @lisp
414 guile> (/ 7 3)
415 7/3
416 guile> (/ 7.0 3.0)
417 2.33333333333333
418 @end lisp
419
420 When the scheme interpreter encounters an expression that is a list,
421 the first element of the list is treated as a procedure to be
422 evaluated with the arguments of the remainder of the list.  Therefore,
423 all operators in Scheme are prefix operators.
424
425 If the first element of a Scheme expression that is a list passed to
426 the interpreter is @emph{not} an operator or procedure, an error will
427 occur:
428
429 @lisp
430 guile> (1 2 3)
431
432 Backtrace:
433 In current input:
434   52: 0* [1 2 3]
435
436 <unnamed port>:52:1: In expression (1 2 3):
437 <unnamed port>:52:1: Wrong type to apply: 1
438 ABORT: (misc-error)
439 guile>
440 @end lisp
441
442 Here you can see that the interpreter was trying to treat 1 as an
443 operator or procedure, and it couldn't.  Hence the error is "Wrong
444 type to apply: 1".
445
446 Therefore, to create a list we need to use the list operator, or to
447 quote the list so that the interpreter will not try to evaluate it.
448
449 @lisp
450 guile> (list 1 2 3)
451 (1 2 3)
452 guile> '(1 2 3)
453 (1 2 3)
454 guile>
455 @end lisp
456
457 This is an error that can appear as you are working with Scheme in LilyPond.
458
459 @ignore
460 The same assignment can be done in completely in Scheme as well,
461
462 @example
463 #(define twentyFour (* 2 twelve))
464 @end example
465
466 @c this next section is confusing -- need to rewrite
467
468 The @emph{name} of a variable is also an expression, similar to a
469 number or a string.  It is entered as
470
471 @example
472 #'twentyFour
473 @end example
474
475 @funindex #'symbol
476 @cindex quoting in Scheme
477
478 The quote mark @code{'} prevents the Scheme interpreter from substituting
479 @code{24} for the @code{twentyFour}.  Instead, we get the name
480 @code{twentyFour}.
481 @end ignore
482
483
484 @node Scheme procedures
485 @subsection Scheme procedures
486
487 Scheme procedures are executable scheme expressions that return a
488 value resulting from their execution.  They can also manipulate
489 variables defined outside of the procedure.
490
491 @subheading Defining procedures
492
493 Procedures are defined in Scheme with define
494
495 @example
496 (define (function-name arg1 arg2 ... argn)
497  scheme-expression-that-gives-a-return-value)
498 @end example
499
500 For example, we could define a procedure to calculate the average:
501
502 @lisp
503 guile> (define (average x y) (/ (+ x y) 2))
504 guile> average
505 #<procedure average (x y)>
506 @end lisp
507
508 Once a procedure is defined, it is called by putting the procedure
509 name and the arguments in a list.  For example, we can calculate
510 the average of 3 and 12:
511
512 @lisp
513 guile> (average 3 12)
514 15/2
515 @end lisp
516
517 @subheading Predicates
518
519 Scheme procedures that return boolean values are often called
520 @emph{predicates}.  By convention (but not necessity), predicate names
521 typically end in a question mark:
522
523 @lisp
524 guile> (define (less-than-ten? x) (< x 10))
525 guile> (less-than-ten? 9)
526 #t
527 guile> (less-than-ten? 15)
528 #f
529 @end lisp
530
531 @subheading Return values
532
533 Scheme procedures always return a return value, which is the value
534 of the last expression executed in the procedure.  The return
535 value can be any valid Scheme value, including a complex data
536 structure or a procedure.
537
538 Sometimes the user would like to have multiple Scheme expressions in
539 a procedure.  There are two ways that multiple expressions can be
540 combined.  The first is the @code{begin} procedure, which allows
541 multiple expressions to be evaluated, and returns the value of
542 the last expression.
543
544 @lisp
545 guile> (begin (+ 1 2) (- 5 8) (* 2 2))
546 4
547 @end lisp
548
549 The second way to combine multiple expressions is in a @code{let} block.
550 In a let block, a series of bindings are created, and then a sequence
551 of expressions that can include those bindings is evaluated.  The
552 return value of the let block is the return value of the last
553 statement in the let block:
554
555 @lisp
556 guile> (let ((x 2) (y 3) (z 4)) (display (+ x y)) (display (- z 4))
557 ... (+ (* x y) (/ z x)))
558 508
559 @end lisp
560
561 @node Scheme conditionals
562 @subsection Scheme conditionals
563
564 @subheading if
565
566 Scheme has an @code{if} procedure:
567
568 @example
569 (if test-expression true-expression false-expression)
570 @end example
571
572 @var{test-expression} is an expression that returns a boolean
573 value.  If @var{test-expression} returns @code{#t}, the if
574 procedure returns the value of @var{true-expression}, otherwise
575 it returns the value of @var{false-expression}.
576
577 @lisp
578 guile> (define a 3)
579 guile> (define b 5)
580 guile> (if (> a b) "a is greater than b" "a is not greater than b")
581 "a is not greater than b"
582 @end lisp
583
584 @subheading cond
585
586 Another conditional procedure in scheme is @code{cond}:
587
588 @example
589 (cond (test-expression-1 result-expression-sequence-1)
590       (test-expression-2 result-expression-sequence-2)
591       ...
592       (test-expression-n result-expression-sequence-n))
593 @end example
594
595 For example:
596
597 @lisp
598 guile> (define a 6)
599 guile> (define b 8)
600 guile> (cond ((< a b) "a is less than b")
601 ...          ((= a b) "a equals b")
602 ...          ((> a b) "a is greater than b"))
603 "a is less than b"
604 @end lisp
605
606 @node Scheme in LilyPond
607 @section Scheme in LilyPond
608
609
610 @menu
611 * LilyPond Scheme syntax::
612 * LilyPond variables::
613 * Input variables and Scheme::
614 * Importing Scheme in LilyPond::
615 * Object properties::
616 * LilyPond compound variables::
617 * Internal music representation::
618 @end menu
619
620 @node LilyPond Scheme syntax
621 @subsection LilyPond Scheme syntax
622 @funindex $
623 @funindex #
624
625 The Guile interpreter is part of LilyPond, which means that
626 Scheme can be included in LilyPond input files.  There are several
627 methods for including Scheme in LilyPond.
628
629 The simplest way is to use a hash mark@tie{}@code{#} before a Scheme
630 expression.
631
632 Now LilyPond's input is structured into tokens and expressions, much
633 like human language is structured into words and sentences.  LilyPond
634 has a lexer that recognizes tokens (literal numbers, strings, Scheme
635 elements, pitches and so on), and a parser that understands the syntax,
636 @ruser{LilyPond grammar}.  Once it knows that a particular syntax rule
637 applies, it executes actions associated with it.
638
639 The hash mark@tie{}@code{#} method of embedding Scheme is a natural fit
640 for this system.  Once the lexer sees a hash mark, it calls the Scheme
641 reader to read one full Scheme expression (this can be an identifier, an
642 expression enclosed in parentheses, or several other things).  After the
643 Scheme expression is read, it is stored away as the value for an
644 @code{SCM_TOKEN} in the grammar.  Once the parser knows how to make use
645 of this token, it calls Guile for evaluating the Scheme expression.
646 Since the parser usually requires a bit of lookahead from the lexer to
647 make its parsing decisions, this separation of reading and evaluation
648 between lexer and parser is exactly what is needed to keep the execution
649 of LilyPond and Scheme expressions in sync.  For this reason, you should
650 use the hash mark@tie{}@code{#} for calling Scheme whenever this is
651 feasible.
652
653 Another way to call the Scheme interpreter from LilyPond is the use of
654 dollar@tie{}@code{$} instead of a hash mark for introducing Scheme
655 expressions.  In this case, Lilypond evaluates the code right after the
656 lexer has read it.  It checks the resulting type of the Scheme
657 expression and then picks a token type (one of several
658 @code{xxx_IDENTIFIER} in the syntax) for it.  It creates a @emph{copy}
659 of the value and uses that for the value of the token.  If the value of
660 the expression is void (Guile's value of @code{*unspecified*}), nothing
661 at all is passed to the parser.
662
663 This is, in fact, exactly the same mechanism that Lilypond employs when
664 you call any variable or music function by name, as @code{\name}, with
665 the only difference that the name is determined by the Lilypond lexer
666 without consulting the Scheme reader, and thus only variable names
667 consistent with the current Lilypond mode are accepted.
668
669 The immediate action of @code{$} can lead to surprises, @ref{Input
670 variables and Scheme}.  Using @code{#} where the parser supports it is
671 usually preferable.
672
673 @funindex $@@
674 @funindex #@@
675 There are also @q{list splicing} operators @code{$@@} and @code{#@@}
676 that insert all elements of a list in the surrounding context.
677
678 Now let's take a look at some actual Scheme code.  Scheme procedures can
679 be defined in LilyPond input files:
680
681 @example
682 #(define (average a b c) (/ (+ a b c) 3))
683 @end example
684
685 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
686 be used within Scheme code, even in a LilyPond input file, because
687 the Guile interpreter, not the LilyPond lexer, is reading
688 the Scheme expression.  Comments in Guile Scheme are entered
689 as follows:
690
691 @example
692 ; this is a single-line comment
693
694 #!
695   This a (non-nestable) Guile-style block comment
696   But these are rarely used by Schemers and never in
697   LilyPond source code
698 !#
699 @end example
700
701 For the rest of this section, we will assume that the data is entered
702 in a music file, so we add@tie{}@code{#}s at the beginning of each Scheme
703 expression.
704
705 All of the top-level Scheme expressions in a LilyPond input file can
706 be combined into a single Scheme expression by the use of the
707 @code{begin} statement:
708
709 @example
710 #(begin
711   (define foo 0)
712   (define bar 1))
713 @end example
714
715
716 @node LilyPond variables
717 @subsection LilyPond variables
718
719 LilyPond variables are stored internally in the form of Scheme
720 variables.  Thus,
721
722 @example
723 twelve = 12
724 @end example
725
726 @noindent
727 is equivalent to
728
729 @example
730 #(define twelve 12)
731 @end example
732
733 This means that LilyPond variables are available
734 for use in Scheme expressions.  For example, we could use
735
736 @example
737 twentyFour = #(* 2 twelve)
738 @end example
739
740 @noindent
741 which would result in the number 24 being stored in the
742 LilyPond (and Scheme) variable @code{twentyFour}.
743
744 The usual way to refer to Lilypond variables, @ref{LilyPond Scheme
745 syntax}, is to call them using a backslash, i.e., @code{\twentyFour}.
746 Since this creates a copy of the value for most of LilyPond's internal
747 types, in particular music expressions, music functions don't usually
748 create copies of material they change.  For this reason, music
749 expressions given with @code{#} should usually not contain material that
750 is not either created from scratch or explicitly copied rather than
751 directly referenced.
752
753 @node Input variables and Scheme
754 @subsection Input variables and Scheme
755
756 The input format supports the notion of variables: in the following
757 example, a music expression is assigned to a variable with the name
758 @code{traLaLa}.
759
760 @example
761 traLaLa = @{ c'4 d'4 @}
762 @end example
763
764 @noindent
765
766 There is also a form of scoping: in the following example, the
767 @code{\layout} block also contains a @code{traLaLa} variable, which is
768 independent of the outer @code{\traLaLa}.
769
770 @example
771 traLaLa = @{ c'4 d'4 @}
772 \layout @{ traLaLa = 1.0 @}
773 @end example
774
775 @c
776 In effect, each input file is a scope, and all @code{\header},
777 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
778 toplevel scope.
779
780 Both variables and scoping are implemented in the GUILE module system.
781 An anonymous Scheme module is attached to each scope.  An assignment of
782 the form:
783
784 @example
785 traLaLa = @{ c'4 d'4 @}
786 @end example
787
788 @noindent
789 is internally converted to a Scheme definition:
790
791 @example
792 (define traLaLa @var{Scheme value of `@code{... }'})
793 @end example
794
795 This means that LilyPond variables and Scheme variables may be freely
796 mixed.  In the following example, a music fragment is stored in the
797 variable @code{traLaLa}, and duplicated using Scheme.  The result is
798 imported in a @code{\score} block by means of a second variable
799 @code{twice}:
800
801 @lilypond[verbatim]
802 traLaLa = { c'4 d'4 }
803
804 #(define newLa (map ly:music-deep-copy
805   (list traLaLa traLaLa)))
806 #(define twice
807   (make-sequential-music newLa))
808
809 \twice
810 @end lilypond
811
812 @c Due to parser lookahead
813
814 This is actually a rather interesting example.  The assignment will only
815 take place after the parser has ascertained that nothing akin to
816 @code{\addlyrics} follows, so it needs to check what comes next.  It
817 reads @code{#} and the following Scheme expression @emph{without}
818 evaluating it, so it can go ahead with the assignment, and
819 @emph{afterwards} execute the Scheme code without problem.
820
821 @node Importing Scheme in LilyPond
822 @subsection Importing Scheme in LilyPond
823 @funindex $
824 @funindex #
825
826 The above example shows how to @q{export} music expressions from the
827 input to the Scheme interpreter.  The opposite is also possible.  By
828 placing it after @code{$}, a Scheme
829 value is interpreted as if it were entered in LilyPond syntax.
830 Instead of defining @code{\twice}, the example above could also have
831 been written as
832
833 @example
834 ...
835 $(make-sequential-music newLa)
836 @end example
837
838 You can use @code{$} with a Scheme expression anywhere you could use
839 @code{\@var{name}} after having assigned the Scheme expression to a
840 variable @var{name}.  This replacement happens in the @q{lexer}, so
841 Lilypond is not even aware of the difference.
842
843 One drawback, however, is that of timing.  If we had been using @code{$}
844 instead of @code{#} for defining @code{newLa} in the above example, the
845 following Scheme definition would have failed because @code{traLaLa}
846 would not yet have been defined.  For an explanation of this timing
847 problem, @ref{LilyPond Scheme syntax}.
848
849 @funindex $@@
850 @funindex #@@
851 A further convenience can be the @q{list splicing} operators @code{$@@}
852 and @code{#@@} for inserting the elements of a list in the surrounding
853 context.  Using those, the last part of the example could have been
854 written as
855
856 @example
857 ...
858 @{ $@@newLa @}
859 @end example
860
861 Here, every element of the list stored in @code{newLa} is taken in
862 sequence and inserted into the list, as if we had written
863
864 @example
865 @{ $(first newLa) $(second newLa) @}
866 @end example
867
868 Now in all of these forms, the Scheme code is evaluated while the
869 input is still being consumed, either in the lexer or in the parser.
870 If you need it to be executed at a later point of time, check out
871 @ref{Void scheme functions}, or store it in a procedure:
872
873 @example
874 #(define (nopc)
875   (ly:set-option 'point-and-click #f))
876
877 ...
878 #(nopc)
879 @{ c'4 @}
880 @end example
881
882 @knownissues
883
884 Mixing Scheme and LilyPond variables is not possible with the
885 @option{--safe} option.
886
887
888 @node Object properties
889 @subsection Object properties
890
891 Object properties are stored in LilyPond in the form of alist-chains,
892 which are lists of alists.  Properties are set by adding values at
893 the beginning of the property list.  Properties are read by retrieving
894 values from the alists.
895
896 Setting a new value for a property requires assigning a value to
897 the alist with both a key and a value.  The LilyPond syntax for doing
898 this is:
899
900 @example
901 \override Stem #'thickness = #2.6
902 @end example
903
904 This instruction adjusts the appearance of stems.  An alist entry
905 @code{'(thickness . 2.6)} is added to the property list of the
906 @code{Stem}
907 object.  @code{thickness} is measured relative to the thickness of
908 staff lines, so these stem lines will be @code{2.6} times the
909 width of staff lines.  This makes stems almost twice as thick as their
910 normal size.  To distinguish between variables defined in input files (like
911 @code{twentyFour} in the example above) and variables of internal
912 objects, we will call the latter @q{properties} and the former
913 @q{variables.}  So, the stem object has a @code{thickness} property,
914 while @code{twentyFour} is a variable.
915
916 @cindex properties vs. variables
917 @cindex variables vs. properties
918
919 @c  todo -- here we're getting interesting.  We're now introducing
920 @c  LilyPond variable types.  I think this deserves a section all
921 @c  its own
922
923 @node LilyPond compound variables
924 @subsection LilyPond compound variables
925
926 @subheading Offsets
927
928 Two-dimensional offsets (X and Y coordinates) are stored as @emph{pairs}.
929 The @code{car} of the offset is the X coordinate, and the @code{cdr} is
930 the Y coordinate.
931
932 @example
933 \override TextScript #'extra-offset = #'(1 . 2)
934 @end example
935
936 This assigns the pair @code{(1 . 2)} to the @code{extra-offset}
937 property of the
938 TextScript object.  These numbers are measured in staff-spaces, so
939 this command moves the object 1 staff space to the right, and 2 spaces up.
940
941 Procedures for working with offsets are found in @file{scm/lily-library.scm}.
942
943 @subheading Fractions
944
945 Fractions as used by LilyPond are again stored as @emph{pairs}, this
946 time of unsigned integers.  While Scheme can represent rational numbers
947 as a native type, musically @samp{2/4} and @samp{1/2} are not the same,
948 and we need to be able to distinguish between them.  Similarly there are
949 no negative @q{fractions} in LilyPond's mind.  So @code{2/4} in LilyPond
950 means @code{(2 . 4)} in Scheme, and @code{#2/4} in LilyPond means
951 @code{1/2} in Scheme.
952
953 @subheading Extents
954
955 Pairs are also used to store intervals, which represent a range of numbers
956 from the minimum (the @code{car}) to the maximum (the @code{cdr}).
957 Intervals are used to store the X- and Y- extents of printable objects.
958 For X extents, the @code{car} is the left hand X coordinate, and the
959 @code{cdr} is the right hand X coordinate.  For Y extents, the @code{car}
960 is the bottom coordinate, and the @code{cdr} is the top coordinate.
961
962 Procedures for working with intervals are found in
963 @file{scm/lily-library.scm}.  These procedures should be used when possible
964 to ensure consistency of code.
965
966 @subheading Property alists
967
968 A property alist is a LilyPond data structure that is an alist whose
969 keys are properties and whose values are Scheme expressions that give
970 the desired value for the property.
971
972 LilyPond properties are Scheme symbols, such as @code{'thickness}.
973
974 @subheading Alist chains
975
976 An alist chain is a list containing property alists.
977
978 The set of all properties that will apply to a grob is typically
979 stored as an alist chain.  In order to find the value for a particular
980 property that a grob should have, each alist in the chain is searched in
981 order, looking for an entry containing the property key.  The first alist
982 entry found is returned, and the value is the property value.
983
984 The Scheme procedure @code{chain-assoc-get} is normally used to get
985 grob property values.
986
987 @node Internal music representation
988 @subsection Internal music representation
989
990 Internally, music is represented as a Scheme list.  The list contains
991 various elements that affect the printed output.  Parsing is the process
992 of converting music from the LilyPond input representation to the
993 internal Scheme representation.
994
995 When a music expression is parsed, it is converted into a set of
996 Scheme music objects.  The defining property of a music object is that
997 it takes up time.  The time it takes up is called its @emph{duration}.
998 Durations are expressed as a rational number that measures the length
999 of the music object in whole notes.
1000
1001 A music object has three kinds of types:
1002 @itemize
1003 @item
1004 music name: Each music expression has a name.  For example, a note
1005 leads to a @rinternals{NoteEvent}, and @code{\simultaneous} leads to
1006 a @rinternals{SimultaneousMusic}.  A list of all expressions
1007 available is in the Internals Reference manual, under
1008 @rinternals{Music expressions}.
1009
1010 @item
1011 @q{type} or interface: Each music name has several @q{types} or
1012 interfaces, for example, a note is an @code{event}, but it is also a
1013 @code{note-event}, a @code{rhythmic-event}, and a
1014 @code{melodic-event}.  All classes of music are listed in the
1015 Internals Reference, under
1016 @rinternals{Music classes}.
1017
1018 @item
1019 C++ object: Each music object is represented by an object of the C++
1020 class @code{Music}.
1021 @end itemize
1022
1023 The actual information of a music expression is stored in properties.
1024 For example, a @rinternals{NoteEvent} has @code{pitch} and
1025 @code{duration} properties that store the pitch and duration of that
1026 note.  A list of all properties available can be found in the
1027 Internals Reference, under @rinternals{Music properties}.
1028
1029 A compound music expression is a music object that contains other
1030 music objects in its properties.  A list of objects can be stored in
1031 the @code{elements} property of a music object, or a single @q{child}
1032 music object in the @code{element} property.  For example,
1033 @rinternals{SequentialMusic} has its children in @code{elements},
1034 and @rinternals{GraceMusic} has its single argument in
1035 @code{element}.  The body of a repeat is stored in the @code{element}
1036 property of @rinternals{RepeatedMusic}, and the alternatives in
1037 @code{elements}.
1038
1039 @node Building complicated functions
1040 @section Building complicated functions
1041
1042 This section explains how to gather the information necessary
1043 to create complicated music functions.
1044
1045 @menu
1046 * Displaying music expressions::
1047 * Music properties::
1048 * Doubling a note with slurs (example)::
1049 * Adding articulation to notes (example)::
1050 @end menu
1051
1052 @node Displaying music expressions
1053 @subsection Displaying music expressions
1054
1055 @cindex internal storage
1056 @cindex displaying music expressions
1057 @cindex internal representation, displaying
1058 @cindex displayMusic
1059 @funindex \displayMusic
1060
1061 When writing a music function it is often instructive to inspect how
1062 a music expression is stored internally.  This can be done with the
1063 music function @code{\displayMusic}
1064
1065 @example
1066 @{
1067   \displayMusic @{ c'4\f @}
1068 @}
1069 @end example
1070
1071 @noindent
1072 will display
1073
1074 @example
1075 (make-music
1076   'SequentialMusic
1077   'elements
1078   (list (make-music
1079           'NoteEvent
1080           'articulations
1081           (list (make-music
1082                   'AbsoluteDynamicEvent
1083                   'text
1084                   "f"))
1085           'duration
1086           (ly:make-duration 2 0 1 1)
1087           'pitch
1088           (ly:make-pitch 0 0 0))))
1089 @end example
1090
1091 By default, LilyPond will print these messages to the console along
1092 with all the other messages.  To split up these messages and save
1093 the results of @code{\display@{STUFF@}}, redirect the output to
1094 a file.
1095
1096 @example
1097 lilypond file.ly >display.txt
1098 @end example
1099
1100 With a combined bit of Lilypond and Scheme magic, you can actually
1101 let Lilypond direct just this output to a file of its own:
1102
1103 @example
1104 @{
1105   $(with-output-to-file "display.txt"
1106       (lambda () #@{ \displayMusic @{ c'4\f @} #@}))
1107 @}
1108 @end example
1109
1110
1111 A bit of reformatting makes the above information easier to read:
1112
1113 @example
1114 (make-music 'SequentialMusic
1115   'elements (list
1116              (make-music 'NoteEvent
1117                'articulations (list
1118                                (make-music 'AbsoluteDynamicEvent
1119                                  'text
1120                                  "f"))
1121                'duration (ly:make-duration 2 0 1 1)
1122                'pitch    (ly:make-pitch 0 0 0))))
1123 @end example
1124
1125 A @code{@{ ... @}} music sequence has the name @code{SequentialMusic},
1126 and its inner expressions are stored as a list in its @code{'elements}
1127 property.  A note is represented as a @code{NoteEvent} object (storing
1128 the duration and pitch properties) with attached information (in this
1129 case, an @code{AbsoluteDynamicEvent} with a @code{"f"} text property)
1130 stored in its @code{articulations} property.
1131
1132 @funindex{\void}
1133 @code{\displayMusic} returns the music it displays, so it will get
1134 interpreted as well as displayed.  To avoid interpretation, write
1135 @code{\void} before @code{\displayMusic}.
1136
1137 @node Music properties
1138 @subsection Music properties
1139
1140 TODO -- make sure we delineate between @emph{music} properties,
1141 @emph{context} properties, and @emph{layout} properties.  These
1142 are potentially confusing.
1143
1144 Let's look at an example:
1145
1146 @example
1147 someNote = c'
1148 \displayMusic \someNote
1149 ===>
1150 (make-music
1151   'NoteEvent
1152   'duration
1153   (ly:make-duration 2 0 1 1)
1154   'pitch
1155   (ly:make-pitch 0 0 0))
1156 @end example
1157
1158 The @code{NoteEvent} object is the representation of @code{someNote}.
1159 Straightforward.  How about putting c' in a chord?
1160
1161 @example
1162 someNote = <c'>
1163 \displayMusic \someNote
1164 ===>
1165 (make-music
1166   'EventChord
1167   'elements
1168   (list (make-music
1169           'NoteEvent
1170           'duration
1171           (ly:make-duration 2 0 1 1)
1172           'pitch
1173           (ly:make-pitch 0 0 0))))
1174 @end example
1175
1176 Now the @code{NoteEvent} object is the first object of the
1177 @code{'elements} property of @code{someNote}.
1178
1179 The @code{display-scheme-music} function is the function used by
1180 @code{\displayMusic} to display the Scheme representation of a music
1181 expression.
1182
1183 @example
1184 #(display-scheme-music (first (ly:music-property someNote 'elements)))
1185 ===>
1186 (make-music
1187   'NoteEvent
1188   'duration
1189   (ly:make-duration 2 0 1 1)
1190   'pitch
1191   (ly:make-pitch 0 0 0))
1192 @end example
1193
1194 Then the note pitch is accessed through the @code{'pitch} property
1195 of the @code{NoteEvent} object,
1196
1197 @example
1198 #(display-scheme-music
1199    (ly:music-property (first (ly:music-property someNote 'elements))
1200                       'pitch))
1201 ===>
1202 (ly:make-pitch 0 0 0)
1203 @end example
1204
1205 The note pitch can be changed by setting this @code{'pitch} property,
1206
1207 @funindex \displayLilyMusic
1208
1209 @example
1210 #(set! (ly:music-property (first (ly:music-property someNote 'elements))
1211                           'pitch)
1212        (ly:make-pitch 0 1 0)) ;; set the pitch to d'.
1213 \displayLilyMusic \someNote
1214 ===>
1215 d'
1216 @end example
1217
1218
1219 @node Doubling a note with slurs (example)
1220 @subsection Doubling a note with slurs (example)
1221
1222 Suppose we want to create a function that translates input like
1223 @code{a} into @code{@{ a( a) @}}.  We begin by examining the internal
1224 representation of the desired result.
1225
1226 @example
1227 \displayMusic@{ a'( a') @}
1228 ===>
1229 (make-music
1230   'SequentialMusic
1231   'elements
1232   (list (make-music
1233           'NoteEvent
1234           'articulations
1235           (list (make-music
1236                   'SlurEvent
1237                   'span-direction
1238                   -1))
1239           'duration
1240           (ly:make-duration 2 0 1 1)
1241           'pitch
1242           (ly:make-pitch 0 5 0))
1243         (make-music
1244           'NoteEvent
1245           'articulations
1246           (list (make-music
1247                   'SlurEvent
1248                   'span-direction
1249                   1))
1250           'duration
1251           (ly:make-duration 2 0 1 1)
1252           'pitch
1253           (ly:make-pitch 0 5 0))))
1254 @end example
1255
1256 The bad news is that the @code{SlurEvent} expressions
1257 must be added @q{inside} the note (in its @code{articulations}
1258 property).
1259
1260 Now we examine the input,
1261
1262 @example
1263 \displayMusic a'
1264 ===>
1265 (make-music
1266   'NoteEvent
1267   'duration
1268   (ly:make-duration 2 0 1 1)
1269   'pitch
1270   (ly:make-pitch 0 5 0))))
1271 @end example
1272
1273 So in our function, we need to clone this expression (so that we have
1274 two notes to build the sequence), add a @code{SlurEvent} to the
1275 @code{'articulations} property of each one, and finally make a
1276 @code{SequentialMusic} with the two @code{EventChords}.  For adding to a
1277 property, it is useful to know that an unset property is read out as
1278 @code{'()}, the empty list, so no special checks are required before we
1279 put another element at the front of the @code{articulations} property.
1280
1281 @example
1282 doubleSlur = #(define-music-function (parser location note) (ly:music?)
1283          "Return: @{ note ( note ) @}.
1284          `note' is supposed to be a single note."
1285          (let ((note2 (ly:music-deep-copy note)))
1286            (set! (ly:music-property note 'articulations)
1287                  (cons (make-music 'SlurEvent 'span-direction -1)
1288                        (ly:music-property note 'articulations)))
1289            (set! (ly:music-property note2 'articulations)
1290                  (cons (make-music 'SlurEvent 'span-direction 1)
1291                        (ly:music-property note2 'articulations)))
1292            (make-music 'SequentialMusic 'elements (list note note2))))
1293 @end example
1294
1295
1296 @node Adding articulation to notes (example)
1297 @subsection Adding articulation to notes (example)
1298
1299 The easy way to add articulation to notes is to merge two music
1300 expressions into one context, as explained in @ruser{Creating contexts}.
1301 However, suppose that we want to write a music function that does this.
1302 This will have the additional advantage that we can use that music
1303 function to add an articulation (like a fingering instruction) to a
1304 single note inside of a chord which is not possible if we just merge
1305 independent music.
1306
1307 A @code{$variable} inside the @code{#@{...#@}} notation is like
1308 a regular @code{\variable} in classical LilyPond notation.  We
1309 know that
1310
1311 @example
1312 @{ \music -. -> @}
1313 @end example
1314
1315 @noindent
1316 will not work in LilyPond.  We could avoid this problem by attaching
1317 the articulation to an empty chord,
1318
1319 @example
1320 @{ << \music <> -. -> >> @}
1321 @end example
1322
1323 @noindent
1324 but for the sake of this example, we will learn how to do this in
1325 Scheme.  We begin by examining our input and desired output,
1326
1327 @example
1328 %  input
1329 \displayMusic c4
1330 ===>
1331 (make-music
1332   'NoteEvent
1333   'duration
1334   (ly:make-duration 2 0 1 1)
1335   'pitch
1336   (ly:make-pitch -1 0 0))))
1337 =====
1338 %  desired output
1339 \displayMusic c4->
1340 ===>
1341 (make-music
1342   'NoteEvent
1343   'articulations
1344   (list (make-music
1345           'ArticulationEvent
1346           'articulation-type
1347           "accent"))
1348   'duration
1349   (ly:make-duration 2 0 1 1)
1350   'pitch
1351   (ly:make-pitch -1 0 0))
1352 @end example
1353
1354 We see that a note (@code{c4}) is represented as an @code{NoteEvent}
1355 expression.  To add an accent articulation, an @code{ArticulationEvent}
1356 expression must be added to the @code{articulations} property of the
1357 @code{NoteEvent} expression.
1358
1359 To build this function, we begin with
1360
1361 @example
1362 (define (add-accent note-event)
1363   "Add an accent ArticulationEvent to the articulations of `note-event',
1364   which is supposed to be a NoteEvent expression."
1365   (set! (ly:music-property note-event 'articulations)
1366         (cons (make-music 'ArticulationEvent
1367                 'articulation-type "accent")
1368               (ly:music-property note-event 'articulations)))
1369   note-event)
1370 @end example
1371
1372 The first line is the way to define a function in Scheme: the function
1373 name is @code{add-accent}, and has one variable called
1374 @code{note-event}.  In Scheme, the type of variable is often clear
1375 from its name.  (this is good practice in other programming languages,
1376 too!)
1377
1378 @example
1379 "Add an accent..."
1380 @end example
1381
1382 @noindent
1383 is a description of what the function does.  This is not strictly
1384 necessary, but just like clear variable names, it is good practice.
1385
1386 You may wonder why we modify the note event directly instead of working
1387 on a copy (@code{ly:music-deep-copy} can be used for that).  The reason
1388 is a silent contract: music functions are allowed to modify their
1389 arguments: they are either generated from scratch (like user input) or
1390 are already copied (referencing a music variable with @samp{\name} or
1391 music from immediate Scheme expressions @samp{$(@dots{})} provides a
1392 copy).  Since it would be inefficient to create unnecessary copies, the
1393 return value from a music function is @emph{not} copied.  So to heed
1394 that contract, you must not use any arguments more than once, and
1395 returning it counts as one use.
1396
1397 In an earlier example, we constructed music by repeating a given music
1398 argument.  In that case, at least one repetition had to be a copy of its
1399 own.  If it weren't, strange things may happen.  For example, if you use
1400 @code{\relative} or @code{\transpose} on the resulting music containing
1401 the same elements multiple times, those will be subjected to
1402 relativation or transposition multiple times.  If you assign them to a
1403 music variable, the curse is broken since referencing @samp{\name} will
1404 again create a copy which does not retain the identity of the repeated
1405 elements.
1406
1407 Now while the above function is not a music function, it will normally
1408 be used within music functions.  So it makes sense to heed the same
1409 contract we use for music functions: the input may be modified for
1410 producing the output, and the caller is responsible for creating copies
1411 if it still needs the unchanged argument itself.  If you take a look at
1412 LilyPond's own functions like @code{music-map}, you'll find that they
1413 stick with the same principles.
1414
1415 Where were we?  Now we have a @code{note-event} we may modify, not
1416 because of using @code{ly:music-deep-copy} but because of a long-winded
1417 explanation.  We add the accent to its @code{'articulations} list
1418 property.
1419
1420 @example
1421 (set! place new-value)
1422 @end example
1423
1424 Here, what we want to set (the @q{place}) is the @code{'articulations}
1425 property of @code{note-event} expression.
1426
1427 @example
1428 (ly:music-property note-event 'articulations)
1429 @end example
1430
1431 @code{ly:music-property} is the function used to access music properties
1432 (the @code{'articulations}, @code{'duration}, @code{'pitch}, etc, that we
1433 see in the @code{\displayMusic} output above).  The new value is the
1434 former @code{'articulations} property, with an extra item: the
1435 @code{ArticulationEvent} expression, which we copy from the
1436 @code{\displayMusic} output,
1437
1438 @example
1439 (cons (make-music 'ArticulationEvent
1440         'articulation-type "accent")
1441       (ly:music-property result-event-chord 'articulations))
1442 @end example
1443
1444 @code{cons} is used to add an element to the front of a list without
1445 modifying the original list.  This is what we want: the same list as
1446 before, plus the new @code{ArticulationEvent} expression.  The order
1447 inside the @code{'articulations} property is not important here.
1448
1449 Finally, once we have added the accent articulation to its
1450 @code{articulations} property, we can return @code{note-event}, hence
1451 the last line of the function.
1452
1453 Now we transform the @code{add-accent} function into a music
1454 function (a matter of some syntactic sugar and a declaration of the type
1455 of its sole @q{real} argument).
1456
1457 @example
1458 addAccent = #(define-music-function (parser location note-event)
1459                                      (ly:music?)
1460   "Add an accent ArticulationEvent to the articulations of `note-event',
1461   which is supposed to be a NoteEvent expression."
1462   (set! (ly:music-property note-event 'articulations)
1463         (cons (make-music 'ArticulationEvent
1464                 'articulation-type "accent")
1465               (ly:music-property note-event 'articulations)))
1466   note-event)
1467 @end example
1468
1469 We may verify that this music function works correctly,
1470
1471 @example
1472 \displayMusic \addAccent c4
1473 @end example
1474
1475
1476
1477
1478
1479 @ignore
1480 @menu
1481 * Tweaking with Scheme::
1482 @end menu
1483
1484 @c @nod e Tweaking with Scheme
1485 @c @sectio n Tweaking with Scheme
1486
1487 We have seen how LilyPond output can be heavily modified using
1488 commands like
1489 @code{\override TextScript #'extra-offset = ( 1 . -1)}.  But
1490 we have even more power if we use Scheme.  For a full explanation
1491 of this, see the @ref{Scheme tutorial}, and
1492 @ref{Interfaces for programmers}.
1493
1494 We can use Scheme to simply @code{\override} commands,
1495
1496 TODO Find a simple example
1497 @c This isn't a valid example with skylining
1498 @c It works fine without padText  -td
1499 @end ignore
1500
1501 @ignore
1502 @lilypond[quote,verbatim,ragged-right]
1503 padText = #(define-music-function (parser location padding) (number?)
1504 #{
1505   \once \override TextScript #'padding = #padding
1506 #})
1507
1508 \relative c''' {
1509   c4^"piu mosso" b a b
1510   \padText #1.8
1511   c4^"piu mosso" d e f
1512   \padText #2.6
1513   c4^"piu mosso" fis a g
1514 }
1515 @end lilypond
1516 @end ignore
1517
1518 @ignore
1519 We can use it to create new commands:
1520
1521 @c Check this is a valid example with skylining
1522 @c It is - 'padding still works
1523
1524
1525 @lilypond[quote,verbatim,ragged-right]
1526 tempoPadded = #(define-music-function (parser location padding tempotext)
1527   (number? string?)
1528 #{
1529   \once \override Score.MetronomeMark #'padding = $padding
1530   \tempo \markup { \bold #tempotext }
1531 #})
1532
1533 \relative c'' {
1534   \tempo \markup { "Low tempo" }
1535   c4 d e f g1
1536   \tempoPadded #4.0 #"High tempo"
1537   g4 f e d c1
1538 }
1539 @end lilypond
1540
1541
1542 Even music expressions can be passed in:
1543
1544 @lilypond[quote,verbatim,ragged-right]
1545 pattern = #(define-music-function (parser location x y) (ly:music? ly:music?)
1546 #{
1547   $x e8 a b $y b a e
1548 #})
1549
1550 \relative c''{
1551   \pattern c8 c8\f
1552   \pattern {d16 dis} { ais16-> b\p }
1553 }
1554 @end lilypond
1555 @end ignore