]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/scheme-tutorial.itely
18f79c2d15bfa021aedd3565c9e8100e91fabaf2
[lilypond.git] / Documentation / user / scheme-tutorial.itely
1 @c -*- coding: utf-8; mode: texinfo; -*-
2 @c This file is part of lilypond.tely
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 @appendix 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 The LilyPond installation also includes the Guile implementation of
38 Scheme.  On most systems you can experiment in a Scheme sandbox by
39 opening a terminal window and typing @q{guile}.  On some systems,
40 notably Windows, you may need to set the environment variable
41 @code{GUILE_LOAD_PATH} to the directory @code{../usr/shr/guile/1.8}
42 in the LilyPond installation (for the full path to this directory
43 see @ref{Other sources of information}).  Alternatively, Windows
44 users may simply choose @q{Run} from the Start menu and enter
45 @q{guile}.
46
47 The most basic concept in a language is data typing: numbers, character
48 strings, lists, etc.  Here is a list of data types that are relevant to
49 LilyPond input.
50
51 @table @asis
52 @item Booleans
53 Boolean values are True or False.  The Scheme for True is @code{#t}
54 and False is @code{#f}.
55 @funindex ##t
56 @funindex ##f
57
58 @item Numbers
59 Numbers are entered in the standard fashion,
60 @code{1} is the (integer) number one, while @code{-1.5} is a
61 floating point number (a non-integer number).
62
63 @item Strings
64 Strings are enclosed in double quotes,
65
66 @example
67 "this is a string"
68 @end example
69
70 Strings may span several lines
71
72 @example
73 "this
74 is
75 a string"
76 @end example
77
78 Quotation marks and newlines can also be added with so-called escape
79 sequences.  The string @code{a said "b"} is entered as
80
81 @example
82 "a said \"b\""
83 @end example
84
85 Newlines and backslashes are escaped with @code{\n} and @code{\\}
86 respectively.
87 @end table
88
89
90 In a music file, snippets of Scheme code are introduced with the hash
91 mark @code{#}.  So, the previous examples translated to LilyPond are
92
93 @example
94 ##t ##f
95 #1 #-1.5
96 #"this is a string"
97 #"this
98 is
99 a string"
100 @end example
101
102 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
103 be used within Scheme code.  Comments in Guile Scheme are entered
104 as follows:
105
106 @example
107 ; this is a single-line comment
108
109 #!
110   This a (non-nestable) Guile-style block comment
111   But these are rarely used by Schemers and never in
112   LilyPond source code
113 !#
114 @end example
115
116 Multiple consecutive scheme expressions in a music file can be
117 combined using the @code{begin} operator. This permits the number
118 of hash marks to be reduced to one.
119
120 @example
121 #(begin
122   (define foo 0)
123   (define bar 1))
124 @end example
125
126 If @code{#} is followed by an opening bracket, @code{(}, as in
127 the example above, the parser will remain in Scheme mode until
128 a matching closing bracket, @code{)}, is found, so further
129 @code{#} symbols to introduce a Scheme section are not required.
130
131 For the rest of this section, we will assume that the data is entered
132 in a music file, so we add @code{#}s everywhere.
133
134 Scheme can be used to do calculations.  It uses @emph{prefix}
135 syntax.  Adding 1 and@tie{}2 is written as @code{(+ 1 2)} rather than the
136 traditional @math{1+2}.
137
138 @lisp
139 #(+ 1 2)
140   @result{} #3
141 @end lisp
142
143 The arrow @result{} shows that the result of evaluating @code{(+ 1 2)}
144 is@tie{}@code{3}.  Calculations may be nested; the result of a function may
145 be used for another calculation.
146
147 @lisp
148 #(+ 1 (* 3 4))
149   @result{} #(+ 1 12)
150   @result{} #13
151 @end lisp
152
153 These calculations are examples of evaluations; an expression like
154 @code{(* 3 4)} is replaced by its value @code{12}.  A similar thing
155 happens with variables.  After defining a variable
156
157 @example
158 twelve = #12
159 @end example
160
161 @noindent
162 variables can also be used in expressions, here
163
164 @example
165 twentyFour = #(* 2 twelve)
166 @end example
167
168 @noindent
169 the number 24 is stored in the variable @code{twentyFour}.
170 The same assignment can be done in completely in Scheme as well,
171
172 @example
173 #(define twentyFour (* 2 twelve))
174 @end example
175
176 The @emph{name} of a variable is also an expression, similar to a
177 number or a string.  It is entered as
178
179 @example
180 #'twentyFour
181 @end example
182
183 @funindex #'symbol
184 @cindex quoting in Scheme
185
186 The quote mark @code{'} prevents the Scheme interpreter from substituting
187 @code{24} for the @code{twentyFour}.  Instead, we get the name
188 @code{twentyFour}.
189
190 This syntax will be used very frequently, since many of the layout
191 tweaks involve assigning (Scheme) values to internal variables, for
192 example
193
194 @example
195 \override Stem #'thickness = #2.6
196 @end example
197
198 This instruction adjusts the appearance of stems.  The value @code{2.6}
199 is put into the @code{thickness} variable of a @code{Stem}
200 object.  @code{thickness} is measured relative to the thickness of
201 staff lines, so these stem lines will be @code{2.6} times the
202 width of staff lines.  This makes stems almost twice as thick as their
203 normal size.  To distinguish between variables defined in input files (like
204 @code{twentyFour} in the example above) and variables of internal
205 objects, we will call the latter @q{properties} and the former
206 @q{variables.}  So, the stem object has a @code{thickness} property,
207 while @code{twentyFour} is an variable.
208
209 @cindex properties vs. variables
210 @cindex variables vs. properties
211
212 Two-dimensional offsets (X and Y coordinates) as well as object sizes
213 (intervals with a left and right point) are entered as @code{pairs}.  A
214 pair@footnote{In Scheme terminology, the pair is called @code{cons},
215 and its two elements are called @code{car} and @code{cdr} respectively.}
216 is entered as @code{(first . second)} and, like symbols, they must be quoted,
217
218 @example
219 \override TextScript #'extra-offset = #'(1 . 2)
220 @end example
221
222 This assigns the pair (1, 2) to the @code{extra-offset} property of the
223 TextScript object.  These numbers are measured in staff-spaces, so
224 this command moves the object 1 staff space to the right, and 2 spaces up.
225
226 The two elements of a pair may be arbitrary values, for example
227
228 @example
229 #'(1 . 2)
230 #'(#t . #f)
231 #'("blah-blah" . 3.14159265)
232 @end example
233
234 A list is entered by enclosing its elements in parentheses, and adding
235 a quote.  For example,
236
237 @example
238 #'(1 2 3)
239 #'(1 2 "string" #f)
240 @end example
241
242 We have been using lists all along.  A calculation, like @code{(+ 1 2)}
243 is also a list (containing the symbol @code{+} and the numbers 1
244 and@tie{}2).  Normally lists are interpreted as calculations, and the
245 Scheme interpreter substitutes the outcome of the calculation.  To enter a
246 list, we stop the evaluation.  This is done by quoting the list with a
247 quote @code{'} symbol.  So, for calculations do not use a quote.
248
249 Inside a quoted list or pair, there is no need to quote anymore.  The
250 following is a pair of symbols, a list of symbols and a list of lists
251 respectively,
252
253 @example
254 #'(stem . head)
255 #'(staff clef key-signature)
256 #'((1) (2))
257 @end example
258
259
260 @menu
261 * Tweaking with Scheme::
262 @end menu
263
264 @node Tweaking with Scheme
265 @appendixsec Tweaking with Scheme
266
267 We have seen how LilyPond output can be heavily modified using
268 commands like
269 @code{\override TextScript #'extra-offset = ( 1 . -1)}.  But
270 we have even more power if we use Scheme.  For a full explanation
271 of this, see the @ref{Scheme tutorial}, and
272 @ruser{Interfaces for programmers}.
273
274 We can use Scheme to simply @code{\override} commands,
275
276 TODO Find a simple example
277 @c This isn't a valid example with skylining
278 @c It works fine without padText  -td
279
280 @ignore
281 @lilypond[quote,verbatim,ragged-right]
282 padText = #(define-music-function (parser location padding) (number?)
283 #{
284   \once \override TextScript #'padding = #$padding
285 #})
286
287 \relative c''' {
288   c4^"piu mosso" b a b
289   \padText #1.8
290   c4^"piu mosso" d e f
291   \padText #2.6
292   c4^"piu mosso" fis a g
293 }
294 @end lilypond
295 @end ignore
296
297 We can use it to create new commands:
298
299 @c Check this is a valid example with skylining
300 @c It is - 'padding still works
301
302
303 @lilypond[quote,verbatim,ragged-right]
304 tempoPadded = #(define-music-function (parser location padding tempotext)
305   (number? string?)
306 #{
307   \once \override Score.MetronomeMark #'padding = $padding
308   \tempo \markup { \bold $tempotext }
309 #})
310
311 \relative c'' {
312   \tempo \markup { "Low tempo" }
313   c4 d e f g1
314   \tempoPadded #4.0 #"High tempo"
315   g4 f e d c1
316 }
317 @end lilypond
318
319
320 Even music expressions can be passed in:
321
322 @lilypond[quote,verbatim,ragged-right]
323 pattern = #(define-music-function (parser location x y) (ly:music? ly:music?)
324 #{
325   $x e8 a b $y b a e
326 #})
327
328 \relative c''{
329   \pattern c8 c8\f
330   \pattern {d16 dis} { ais16-> b\p }
331 }
332 @end lilypond
333
334
335
336
337