]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/extending/scheme-tutorial.itely
Doc -- work on Scheme tutorial
[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.  See TRANSLATION for details.
8 @end ignore
9
10 @c \version "2.12.0"
11
12 @node Scheme tutorial
13 @chapter Scheme tutorial
14
15 @funindex #
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/shr/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 Once the guile sandbox is running, you will received a guile prompt:
76
77 @lisp
78 guile>
79 @end lisp
80
81 You can enter Scheme expressions at this prompt to experiment with Scheme.
82
83 @node Scheme variables
84 @subsection Scheme variables
85
86 Scheme variables can have any valid scheme value, including a Scheme
87 procedure.
88
89 Scheme variables are created with @code{define}:
90
91 @lisp
92 guile> (define a 2)
93 guile>
94 @end lisp
95
96 Scheme variables can be evaluated at the guile prompt simply by
97 typing the variable name:
98
99 @lisp
100 guile> a
101 2
102 guile>
103 @end lisp
104
105 Scheme variables can be printed on the display by use of the display function:
106
107 @lisp
108 guile> (display a)
109 2guile>
110 @end lisp
111
112 @noindent
113 Note that the value @code{2} and the guile prompt @code{guile} both
114 showed up on the same line.  This can be avoided by calling the newline
115 procedure or displaying a newline character.
116
117 @lisp
118 guile> (display a)(newline)
119 2
120 guile> (display a)(display "\n")
121 2
122 guile>
123 @end lisp
124
125 Once a variable has been created, its value can be changed with @code{set!}:
126
127 @lisp
128 guile> (set! a 12345)
129 guile> a
130 12345
131 guile>
132 @end lisp
133
134 @node Scheme simple data types
135 @subsection Scheme simple data types
136
137 The most basic concept in a language is data typing: numbers, character
138 strings, lists, etc.  Here is a list of simple Scheme data types that are
139 often used with LilyPond.
140
141 @table @asis
142 @item Booleans
143 Boolean values are True or False.  The Scheme for True is @code{#t}
144 and False is @code{#f}.
145 @funindex ##t
146 @funindex ##f
147
148 @item Numbers
149 Numbers are entered in the standard fashion,
150 @code{1} is the (integer) number one, while @code{-1.5} is a
151 floating point number (a non-integer number).
152
153 @item Strings
154 Strings are enclosed in double quotes,
155
156 @example
157 "this is a string"
158 @end example
159
160 Strings may span several lines:
161
162 @example
163 "this
164 is
165 a string"
166 @end example
167
168 @noindent
169 and the newline characters at the end of each line will be included
170 in the string.
171
172 Newline characters can also be added by including @code{\n} in the
173 string.
174
175 @example
176 "this\nis a\nmultiline string"
177 @end example
178
179
180 Quotation marks and backslashes are added to strings
181 by preceding them with a backslash.
182 The string @code{\a said "b"} is entered as
183
184 @example
185 "\\a said \"b\""
186 @end example
187
188 @end table
189
190 There are additional Scheme data types that are not discussed here.
191 For a complete listing see the Guile reference guide,
192 @uref{http://www.gnu.org/software/guile/manual/html_node/Simple-Data-Types.html}.
193
194 @node Scheme compound data types
195 @subsection Scheme compound data types
196
197 There are also compound data types in Scheme.  The  types commonly used in
198 LilyPond programming include pairs, lists, alists, and hash tables.
199
200 @unnumberedsubsubsec Pairs
201
202 The foundational compound data type of Scheme is the @code{pair}.  As
203 might be expected from its name, a pair is two values glued together.
204 The operator used to form a pair is called @code{cons}.
205
206 @lisp
207 guile> (cons 4 5)
208 (4 . 5)
209 guile>
210 @end lisp
211
212 Note that the pair is displayed as two items surrounded by
213 parentheses and separated by whitespace, a period (@code{.}), and
214 more whitespace.  The period is @emph{not} a decimal point, but
215 rather an indicator of the pair.
216
217 Pairs can also be entered as literal values by preceding them with
218 a single quote character.
219
220 @lisp
221 guile> '(4 . 5)
222 (4 . 5)
223 guile>
224 @end lisp
225
226 The two elements of a pair may be any valid Scheme value:
227
228 @lisp
229 guile> (cons #t #f)
230 (#t . #f)
231 guile> '("blah-blah" . 3.1415926535)
232 ("blah-blah" . 3.1415926535)
233 guile>
234 @end lisp
235
236 The first and second elements of the pair can be accessed by the
237 Scheme procedures @code{car} and @code{cdr}, respectively.
238
239 @lisp
240 guile> (define mypair (cons 123 "hello there")
241 ... )
242 guile> (car mypair)
243 123
244 guile> (cdr mypair)
245 "hello there"
246 guile>
247 @end lisp
248
249 @noindent
250
251 Note:  @code{cdr} is pronounced "could-er", according Sussman and
252 Abelson, see
253 @uref{http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#footnote_Temp_133}
254
255
256 @unnumberedsubsubsec Lists
257
258 TODO -- write about lists
259
260 A list is entered by enclosing its elements in parentheses, and adding
261 a quote.  For example,
262
263 @example
264 #'(1 2 3)
265 #'(1 2 "string" #f)
266 @end example
267
268 We have been using lists all along.  A calculation, like @code{(+ 1 2)}
269 is also a list (containing the symbol @code{+} and the numbers 1
270 and@tie{}2).  Normally lists are interpreted as calculations, and the
271 Scheme interpreter substitutes the outcome of the calculation.  To enter a
272 list, we stop the evaluation.  This is done by quoting the list with a
273 quote @code{'} symbol.  So, for calculations do not use a quote.
274
275 Inside a quoted list or pair, there is no need to quote anymore.  The
276 following is a pair of symbols, a list of symbols and a list of lists
277 respectively,
278
279 @example
280 #'(stem . head)
281 #'(staff clef key-signature)
282 #'((1) (2))
283 @end example
284
285 @unnumberedsubsubsec Hash tables
286
287 TODO -- write about hash tables
288
289
290 @node Calculations in Scheme
291 @subsection Calculations in Scheme
292
293 Scheme can be used to do calculations.  It uses @emph{prefix}
294 syntax.  Adding 1 and@tie{}2 is written as @code{(+ 1 2)} rather than the
295 traditional @math{1+2}.
296
297 @lisp
298 (+ 1 2)
299   @result{} 3
300 @end lisp
301
302 The arrow @result{} shows that the result of evaluating @code{(+ 1 2)}
303 is@tie{}@code{3}.  Calculations may be nested; the result of a function may
304 be used for another calculation.
305
306 @lisp
307 (+ 1 (* 3 4))
308   @result{} (+ 1 12)
309   @result{} 13
310 @end lisp
311
312 These calculations are examples of evaluations; an expression like
313 @code{(* 3 4)} is replaced by its value @code{12}.
314
315 The same assignment can be done in completely in Scheme as well,
316
317 @example
318 #(define twentyFour (* 2 twelve))
319 @end example
320
321 @c this next section is confusing -- need to rewrite
322
323 The @emph{name} of a variable is also an expression, similar to a
324 number or a string.  It is entered as
325
326 @example
327 #'twentyFour
328 @end example
329
330 @funindex #'symbol
331 @cindex quoting in Scheme
332
333 The quote mark @code{'} prevents the Scheme interpreter from substituting
334 @code{24} for the @code{twentyFour}.  Instead, we get the name
335 @code{twentyFour}.
336
337 @node Scheme procedures
338 @subsection Scheme procedures
339
340 @unnumberedsubsubsec Predicates
341
342 @unnumberedsubsubsec Return values
343
344 TODO -- write about scheme procedures
345
346 @node Scheme conditionals
347 @subsection Scheme conditionals
348
349 @unnumberedsubsubsec if
350
351 @unnumberedsubsubsec cond
352
353
354 @node Scheme in LilyPond
355 @section Scheme in LilyPond
356
357
358 @menu
359 * LilyPond Scheme syntax::
360 * LilyPond variables::
361 * Input variables and Scheme::
362 * Object properties::
363 * LilyPond compound variables::
364 * Internal music representation::
365 @end menu
366
367 @node LilyPond Scheme syntax
368 @subsection LilyPond Scheme syntax
369
370 In a music file, snippets of Scheme code are introduced with the hash
371 mark @code{#}.  So, the previous examples translated to LilyPond are
372
373 @example
374 ##t ##f
375 #1 #-1.5
376 #"this is a string"
377 #"this
378 is
379 a string"
380 @end example
381
382 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
383 be used within Scheme code.  Comments in Guile Scheme are entered
384 as follows:
385
386 @example
387 ; this is a single-line comment
388
389 #!
390   This a (non-nestable) Guile-style block comment
391   But these are rarely used by Schemers and never in
392   LilyPond source code
393 !#
394 @end example
395
396 Multiple consecutive scheme expressions in a music file can be
397 combined using the @code{begin} operator. This permits the number
398 of hash marks to be reduced to one.
399
400 @example
401 #(begin
402   (define foo 0)
403   (define bar 1))
404 @end example
405
406 @c todo -- # introduces a scheme *expression*
407 @c         need the concept of an expression
408
409 If @code{#} is followed by an opening parenthesis, @code{(}, as in
410 the example above, the parser will remain in Scheme mode until
411 a matching closing parenthesis, @code{)}, is found, so further
412 @code{#} symbols to introduce a Scheme section are not required.
413
414 For the rest of this section, we will assume that the data is entered
415 in a music file, so we add @code{#}s everywhere.
416
417 @node LilyPond variables
418 @subsection LilyPond variables
419
420
421 TODO -- make this read right
422
423 A similar thing
424 happens with variables.  After defining a variable
425
426 @example
427 twelve = 12
428 @end example
429
430 @noindent
431 variables can also be used in expressions, here
432
433 @example
434 twentyFour = (* 2 twelve)
435 @end example
436
437 @noindent
438 the number 24 is stored in the variable @code{twentyFour}.
439
440 @node Input variables and Scheme
441 @subsection Input variables and Scheme
442
443 The input format supports the notion of variables: in the following
444 example, a music expression is assigned to a variable with the name
445 @code{traLaLa}.
446
447 @example
448 traLaLa = @{ c'4 d'4 @}
449 @end example
450
451 @noindent
452
453 There is also a form of scoping: in the following example, the
454 @code{\layout} block also contains a @code{traLaLa} variable, which is
455 independent of the outer @code{\traLaLa}.
456 @example
457 traLaLa = @{ c'4 d'4 @}
458 \layout @{ traLaLa = 1.0 @}
459 @end example
460 @c
461 In effect, each input file is a scope, and all @code{\header},
462 @code{\midi}, and @code{\layout} blocks are scopes nested inside that
463 toplevel scope.
464
465 Both variables and scoping are implemented in the GUILE module system.
466 An anonymous Scheme module is attached to each scope.  An assignment of
467 the form
468 @example
469 traLaLa = @{ c'4 d'4 @}
470 @end example
471
472 @noindent
473 is internally converted to a Scheme definition
474 @example
475 (define traLaLa @var{Scheme value of `@code{... }'})
476 @end example
477
478 This means that input variables and Scheme variables may be freely
479 mixed.  In the following example, a music fragment is stored in the
480 variable @code{traLaLa}, and duplicated using Scheme.  The result is
481 imported in a @code{\score} block by means of a second variable
482 @code{twice}:
483
484 @lilypond[verbatim]
485 traLaLa = { c'4 d'4 }
486
487 %% dummy action to deal with parser lookahead
488 #(display "this needs to be here, sorry!")
489
490 #(define newLa (map ly:music-deep-copy
491   (list traLaLa traLaLa)))
492 #(define twice
493   (make-sequential-music newLa))
494
495 { \twice }
496 @end lilypond
497
498 @c Due to parser lookahead
499
500 In this example, the assignment happens after the parser has
501 verified that nothing interesting happens after
502 @code{traLaLa = @{ ... @}}.  Without the dummy statement in the
503 above example, the @code{newLa} definition is executed before
504 @code{traLaLa} is defined, leading to a syntax error.
505
506 The above example shows how to @q{export} music expressions from the
507 input to the Scheme interpreter.  The opposite is also possible.  By
508 wrapping a Scheme value in the function @code{ly:export}, a Scheme
509 value is interpreted as if it were entered in LilyPond syntax.
510 Instead of defining @code{\twice}, the example above could also have
511 been written as
512
513 @example
514 ...
515 @{ #(ly:export (make-sequential-music (list newLa))) @}
516 @end example
517
518 Scheme code is evaluated as soon as the parser encounters it.  To
519 define some Scheme code in a macro (to be called later), use
520 @ref{Void functions}, or
521
522 @example
523 #(define (nopc)
524   (ly:set-option 'point-and-click #f))
525
526 ...
527 #(nopc)
528 @{ c'4 @}
529 @end example
530
531 @knownissues
532
533 Mixing Scheme and LilyPond variables is not possible with the
534 @code{--safe} option.
535
536
537
538
539 @node Object properties
540 @subsection Object properties
541
542 This syntax will be used very frequently, since many of the layout
543 tweaks involve assigning (Scheme) values to internal variables, for
544 example
545
546 @example
547 \override Stem #'thickness = #2.6
548 @end example
549
550 This instruction adjusts the appearance of stems.  The value @code{2.6}
551 is put into the @code{thickness} variable of a @code{Stem}
552 object.  @code{thickness} is measured relative to the thickness of
553 staff lines, so these stem lines will be @code{2.6} times the
554 width of staff lines.  This makes stems almost twice as thick as their
555 normal size.  To distinguish between variables defined in input files (like
556 @code{twentyFour} in the example above) and variables of internal
557 objects, we will call the latter @q{properties} and the former
558 @q{variables.}  So, the stem object has a @code{thickness} property,
559 while @code{twentyFour} is an variable.
560
561 @cindex properties vs. variables
562 @cindex variables vs. properties
563
564 @c  todo -- here we're getting interesting.  We're now introducing
565 @c  LilyPond variable types.  I think this deserves a section all
566 @c  its own
567
568 @node LilyPond compound variables
569 @subsection LilyPond compound variables
570
571 @unnumberedsubsubsec Offsets
572
573 Two-dimensional offsets (X and Y coordinates) as well as object sizes
574 (intervals with a left and right point) are entered as @code{pairs}.  A
575 pair@footnote{In Scheme terminology, the pair is called @code{cons},
576 and its two elements are called @code{car} and @code{cdr} respectively.}
577 is entered as @code{(first . second)} and, like symbols, they must be quoted,
578
579 @example
580 \override TextScript #'extra-offset = #'(1 . 2)
581 @end example
582
583 This assigns the pair (1, 2) to the @code{extra-offset} property of the
584 TextScript object.  These numbers are measured in staff-spaces, so
585 this command moves the object 1 staff space to the right, and 2 spaces up.
586
587 @unnumberedsubsubsec Extents
588
589 todo -- write something about extents
590
591 @unnumberedsubsubsec Property alists
592
593 todo -- write something about property alists
594
595 @unnumberedsubsubsec Alist chains
596
597 todo -- write something about alist chains
598
599 @node Internal music representation
600 @subsection Internal music representation
601
602 When a music expression is parsed, it is converted into a set of
603 Scheme music objects.  The defining property of a music object is that
604 it takes up time.  Time is a rational number that measures the length
605 of a piece of music in whole notes.
606
607 A music object has three kinds of types:
608 @itemize
609 @item
610 music name: Each music expression has a name.  For example, a note
611 leads to a @rinternals{NoteEvent}, and @code{\simultaneous} leads to
612 a @rinternals{SimultaneousMusic}.  A list of all expressions
613 available is in the Internals Reference manual, under
614 @rinternals{Music expressions}.
615
616 @item
617 @q{type} or interface: Each music name has several @q{types} or
618 interfaces, for example, a note is an @code{event}, but it is also a
619 @code{note-event}, a @code{rhythmic-event}, and a
620 @code{melodic-event}.  All classes of music are listed in the
621 Internals Reference, under
622 @rinternals{Music classes}.
623
624 @item
625 C++ object: Each music object is represented by an object of the C++
626 class @code{Music}.
627 @end itemize
628
629 The actual information of a music expression is stored in properties.
630 For example, a @rinternals{NoteEvent} has @code{pitch} and
631 @code{duration} properties that store the pitch and duration of that
632 note.  A list of all properties available can be found in the
633 Internals Reference, under @rinternals{Music properties}.
634
635 A compound music expression is a music object that contains other
636 music objects in its properties.  A list of objects can be stored in
637 the @code{elements} property of a music object, or a single @q{child}
638 music object in the @code{element} property.  For example,
639 @rinternals{SequentialMusic} has its children in @code{elements},
640 and @rinternals{GraceMusic} has its single argument in
641 @code{element}.  The body of a repeat is stored in the @code{element}
642 property of @rinternals{RepeatedMusic}, and the alternatives in
643 @code{elements}.
644
645 @node Building complicated functions
646 @section Building complicated functions
647
648 This section explains how to gather the information necessary
649 to create complicated music functions.
650
651 @menu
652 * Displaying music expressions::
653 * Music properties::
654 * Doubling a note with slurs (example)::
655 * Adding articulation to notes (example)::
656 @end menu
657
658
659 @node Displaying music expressions
660 @subsection Displaying music expressions
661
662 @cindex internal storage
663 @cindex displaying music expressions
664 @cindex internal representation, displaying
665 @cindex displayMusic
666 @funindex \displayMusic
667
668 When writing a music function it is often instructive to inspect how
669 a music expression is stored internally.  This can be done with the
670 music function @code{\displayMusic}
671
672 @example
673 @{
674   \displayMusic @{ c'4\f @}
675 @}
676 @end example
677
678 @noindent
679 will display
680
681 @example
682 (make-music
683   'SequentialMusic
684   'elements
685   (list (make-music
686           'EventChord
687           'elements
688           (list (make-music
689                   'NoteEvent
690                   'duration
691                   (ly:make-duration 2 0 1 1)
692                   'pitch
693                   (ly:make-pitch 0 0 0))
694                 (make-music
695                   'AbsoluteDynamicEvent
696                   'text
697                   "f")))))
698 @end example
699
700 By default, LilyPond will print these messages to the console along
701 with all the other messages.  To split up these messages and save
702 the results of @code{\display@{STUFF@}}, redirect the output to
703 a file.
704
705 @example
706 lilypond file.ly >display.txt
707 @end example
708
709 With a bit of reformatting, the above information is
710 easier to read,
711
712 @example
713 (make-music 'SequentialMusic
714   'elements (list (make-music 'EventChord
715                     'elements (list (make-music 'NoteEvent
716                                       'duration (ly:make-duration 2 0 1 1)
717                                       'pitch (ly:make-pitch 0 0 0))
718                                     (make-music 'AbsoluteDynamicEvent
719                                       'text "f")))))
720 @end example
721
722 A @code{@{ ... @}} music sequence has the name @code{SequentialMusic},
723 and its inner expressions are stored as a list in its @code{'elements}
724 property.  A note is represented as an @code{EventChord} expression,
725 containing a @code{NoteEvent} object (storing the duration and
726 pitch properties) and any extra information (in this case, an
727 @code{AbsoluteDynamicEvent} with a @code{"f"} text property.
728
729
730 @node Music properties
731 @subsection Music properties
732
733 The @code{NoteEvent} object is the first object of the
734 @code{'elements} property of @code{someNote}.
735
736 @example
737 someNote = c'
738 \displayMusic \someNote
739 ===>
740 (make-music
741   'EventChord
742   'elements
743   (list (make-music
744           'NoteEvent
745           'duration
746           (ly:make-duration 2 0 1 1)
747           'pitch
748           (ly:make-pitch 0 0 0))))
749 @end example
750
751 The @code{display-scheme-music} function is the function used by
752 @code{\displayMusic} to display the Scheme representation of a music
753 expression.
754
755 @example
756 #(display-scheme-music (first (ly:music-property someNote 'elements)))
757 ===>
758 (make-music
759   'NoteEvent
760   'duration
761   (ly:make-duration 2 0 1 1)
762   'pitch
763   (ly:make-pitch 0 0 0))
764 @end example
765
766 Then the note pitch is accessed through the @code{'pitch} property
767 of the @code{NoteEvent} object,
768
769 @example
770 #(display-scheme-music
771    (ly:music-property (first (ly:music-property someNote 'elements))
772                       'pitch))
773 ===>
774 (ly:make-pitch 0 0 0)
775 @end example
776
777 The note pitch can be changed by setting this @code{'pitch} property,
778
779 @funindex \displayLilyMusic
780
781 @example
782 #(set! (ly:music-property (first (ly:music-property someNote 'elements))
783                           'pitch)
784        (ly:make-pitch 0 1 0)) ;; set the pitch to d'.
785 \displayLilyMusic \someNote
786 ===>
787 d'
788 @end example
789
790
791 @node Doubling a note with slurs (example)
792 @subsection Doubling a note with slurs (example)
793
794 Suppose we want to create a function that translates input like
795 @code{a} into @code{a( a)}.  We begin by examining the internal
796 representation of the desired result.
797
798 @example
799 \displayMusic@{ a'( a') @}
800 ===>
801 (make-music
802   'SequentialMusic
803   'elements
804   (list (make-music
805           'EventChord
806           'elements
807           (list (make-music
808                   'NoteEvent
809                   'duration
810                   (ly:make-duration 2 0 1 1)
811                   'pitch
812                   (ly:make-pitch 0 5 0))
813                 (make-music
814                   'SlurEvent
815                   'span-direction
816                   -1)))
817         (make-music
818           'EventChord
819           'elements
820           (list (make-music
821                   'NoteEvent
822                   'duration
823                   (ly:make-duration 2 0 1 1)
824                   'pitch
825                   (ly:make-pitch 0 5 0))
826                 (make-music
827                   'SlurEvent
828                   'span-direction
829                   1)))))
830 @end example
831
832 The bad news is that the @code{SlurEvent} expressions
833 must be added @q{inside} the note (or more precisely,
834 inside the @code{EventChord} expression).
835
836 Now we examine the input,
837
838 @example
839 (make-music
840   'SequentialMusic
841   'elements
842   (list (make-music
843           'EventChord
844           'elements
845           (list (make-music
846                   'NoteEvent
847                   'duration
848                   (ly:make-duration 2 0 1 1)
849                   'pitch
850                   (ly:make-pitch 0 5 0))))))
851 @end example
852
853 So in our function, we need to clone this expression (so that we
854 have two notes to build the sequence), add @code{SlurEvents} to the
855 @code{'elements} property of each one, and finally make a
856 @code{SequentialMusic} with the two @code{EventChords}.
857
858 @example
859 doubleSlur = #(define-music-function (parser location note) (ly:music?)
860          "Return: @{ note ( note ) @}.
861          `note' is supposed to be an EventChord."
862          (let ((note2 (ly:music-deep-copy note)))
863            (set! (ly:music-property note 'elements)
864                  (cons (make-music 'SlurEvent 'span-direction -1)
865                        (ly:music-property note 'elements)))
866            (set! (ly:music-property note2 'elements)
867                  (cons (make-music 'SlurEvent 'span-direction 1)
868                        (ly:music-property note2 'elements)))
869            (make-music 'SequentialMusic 'elements (list note note2))))
870 @end example
871
872
873 @node Adding articulation to notes (example)
874 @subsection Adding articulation to notes (example)
875
876 The easy way to add articulation to notes is to merge two music
877 expressions into one context, as explained in
878 @ruser{Creating contexts}.  However, suppose that we want to write
879 a music function that does this.
880
881 A @code{$variable} inside the @code{#@{...#@}} notation is like
882 a regular @code{\variable} in classical LilyPond notation.  We
883 know that
884
885 @example
886 @{ \music -. -> @}
887 @end example
888
889 @noindent
890 will not work in LilyPond.  We could avoid this problem by attaching
891 the articulation to a fake note,
892
893 @example
894 @{ << \music s1*0-.-> @}
895 @end example
896
897 @noindent
898 but for the sake of this example, we will learn how to do this in
899 Scheme.  We begin by examining our input and desired output,
900
901 @example
902 %  input
903 \displayMusic c4
904 ===>
905 (make-music
906   'EventChord
907   'elements
908   (list (make-music
909           'NoteEvent
910           'duration
911           (ly:make-duration 2 0 1 1)
912           'pitch
913           (ly:make-pitch -1 0 0))))
914 =====
915 %  desired output
916 \displayMusic c4->
917 ===>
918 (make-music
919   'EventChord
920   'elements
921   (list (make-music
922           'NoteEvent
923           'duration
924           (ly:make-duration 2 0 1 1)
925           'pitch
926           (ly:make-pitch -1 0 0))
927         (make-music
928           'ArticulationEvent
929           'articulation-type
930           "marcato")))
931 @end example
932
933 We see that a note (@code{c4}) is represented as an @code{EventChord}
934 expression, with a @code{NoteEvent} expression in its elements list.  To
935 add a marcato articulation, an @code{ArticulationEvent} expression must
936 be added to the elements property of the @code{EventChord}
937 expression.
938
939 To build this function, we begin with
940
941 @example
942 (define (add-marcato event-chord)
943   "Add a marcato ArticulationEvent to the elements of `event-chord',
944   which is supposed to be an EventChord expression."
945   (let ((result-event-chord (ly:music-deep-copy event-chord)))
946     (set! (ly:music-property result-event-chord 'elements)
947           (cons (make-music 'ArticulationEvent
948                   'articulation-type "marcato")
949                 (ly:music-property result-event-chord 'elements)))
950     result-event-chord))
951 @end example
952
953 The first line is the way to define a function in Scheme: the function
954 name is @code{add-marcato}, and has one variable called
955 @code{event-chord}.  In Scheme, the type of variable is often clear
956 from its name.  (this is good practice in other programming languages,
957 too!)
958
959 @example
960 "Add a marcato..."
961 @end example
962
963 @noindent
964 is a description of what the function does.  This is not strictly
965 necessary, but just like clear variable names, it is good practice.
966
967 @example
968 (let ((result-event-chord (ly:music-deep-copy event-chord)))
969 @end example
970
971 @code{let} is used to declare local variables.  Here we use one local
972 variable, named @code{result-event-chord}, to which we give the value
973 @code{(ly:music-deep-copy event-chord)}.  @code{ly:music-deep-copy} is
974 a function specific to LilyPond, like all functions prefixed by
975 @code{ly:}.  It is use to make a copy of a music
976 expression.  Here we copy @code{event-chord} (the parameter of the
977 function).  Recall that our purpose is to add a marcato to an
978 @code{EventChord} expression.  It is better to not modify the
979 @code{EventChord} which was given as an argument, because it may be
980 used elsewhere.
981
982 Now we have a @code{result-event-chord}, which is a
983 @code{NoteEventChord} expression and is a copy of
984 @code{event-chord}.  We add the marcato to its @code{'elements}
985 list property.
986
987 @example
988 (set! place new-value)
989 @end example
990
991 Here, what we want to set (the @q{place}) is the @code{'elements}
992 property of @code{result-event-chord} expression.
993
994 @example
995 (ly:music-property result-event-chord 'elements)
996 @end example
997
998 @code{ly:music-property} is the function used to access music properties
999 (the @code{'elements}, @code{'duration}, @code{'pitch}, etc, that we
1000 see in the @code{\displayMusic} output above).  The new value is the
1001 former @code{'elements} property, with an extra item: the
1002 @code{ArticulationEvent} expression, which we copy from the
1003 @code{\displayMusic} output,
1004
1005 @example
1006 (cons (make-music 'ArticulationEvent
1007         'articulation-type "marcato")
1008       (ly:music-property result-event-chord 'elements))
1009 @end example
1010
1011 @code{cons} is used to add an element to a list without modifying
1012 the original list.  This is what we want: the same list as before,
1013 plus the new @code{ArticulationEvent} expression.  The order
1014 inside the @code{'elements} property is not important here.
1015
1016 Finally, once we have added the marcato articulation to its @code{elements}
1017 property, we can return @code{result-event-chord}, hence the last line of
1018 the function.
1019
1020 Now we transform the @code{add-marcato} function into a music
1021 function,
1022
1023 @example
1024 addMarcato = #(define-music-function (parser location event-chord)
1025                                      (ly:music?)
1026     "Add a marcato ArticulationEvent to the elements of `event-chord',
1027     which is supposed to be an EventChord expression."
1028     (let ((result-event-chord (ly:music-deep-copy event-chord)))
1029       (set! (ly:music-property result-event-chord 'elements)
1030             (cons (make-music 'ArticulationEvent
1031                     'articulation-type "marcato")
1032                   (ly:music-property result-event-chord 'elements)))
1033       result-event-chord))
1034 @end example
1035
1036 We may verify that this music function works correctly,
1037
1038 @example
1039 \displayMusic \addMarcato c4
1040 @end example
1041
1042
1043
1044
1045
1046
1047 @ignore
1048 @menu
1049 * Tweaking with Scheme::
1050 @end menu
1051
1052 @c @node Tweaking with Scheme
1053 @c @section Tweaking with Scheme
1054
1055 We have seen how LilyPond output can be heavily modified using
1056 commands like
1057 @code{\override TextScript #'extra-offset = ( 1 . -1)}.  But
1058 we have even more power if we use Scheme.  For a full explanation
1059 of this, see the @ref{Scheme tutorial}, and
1060 @ref{Interfaces for programmers}.
1061
1062 We can use Scheme to simply @code{\override} commands,
1063
1064 TODO Find a simple example
1065 @c This isn't a valid example with skylining
1066 @c It works fine without padText  -td
1067 @end ignore
1068
1069 @ignore
1070 @lilypond[quote,verbatim,ragged-right]
1071 padText = #(define-music-function (parser location padding) (number?)
1072 #{
1073   \once \override TextScript #'padding = #$padding
1074 #})
1075
1076 \relative c''' {
1077   c4^"piu mosso" b a b
1078   \padText #1.8
1079   c4^"piu mosso" d e f
1080   \padText #2.6
1081   c4^"piu mosso" fis a g
1082 }
1083 @end lilypond
1084 @end ignore
1085
1086 @ignore
1087 We can use it to create new commands:
1088
1089 @c Check this is a valid example with skylining
1090 @c It is - 'padding still works
1091
1092
1093 @lilypond[quote,verbatim,ragged-right]
1094 tempoPadded = #(define-music-function (parser location padding tempotext)
1095   (number? string?)
1096 #{
1097   \once \override Score.MetronomeMark #'padding = $padding
1098   \tempo \markup { \bold $tempotext }
1099 #})
1100
1101 \relative c'' {
1102   \tempo \markup { "Low tempo" }
1103   c4 d e f g1
1104   \tempoPadded #4.0 #"High tempo"
1105   g4 f e d c1
1106 }
1107 @end lilypond
1108
1109
1110 Even music expressions can be passed in:
1111
1112 @lilypond[quote,verbatim,ragged-right]
1113 pattern = #(define-music-function (parser location x y) (ly:music? ly:music?)
1114 #{
1115   $x e8 a b $y b a e
1116 #})
1117
1118 \relative c''{
1119   \pattern c8 c8\f
1120   \pattern {d16 dis} { ais16-> b\p }
1121 }
1122 @end lilypond
1123 @end ignore