1 @c -*- coding: utf-8; mode: texinfo; -*-
4 Translation of GIT committish: FILL-IN-HEAD-COMMITTISH
6 When revising a translation, copy the HEAD committish of the
7 version that you are working on. See TRANSLATION for details.
13 @chapter Scheme tutorial
18 @cindex Scheme, in-line code
19 @cindex accessing Scheme
20 @cindex evaluating Scheme
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}.
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/}.
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 @rlearning{Other sources of information}). Alternatively, Windows
44 users may simply choose @q{Run} from the Start menu and enter
48 * Introduction to Scheme::
49 * Scheme in LilyPond::
52 @node Introduction to Scheme
53 @section Introduction to Scheme
55 We begin with an introduction to Scheme. For this brief introduction,
56 we will use the GUILE interpreter to explore how the language works.
57 Once we are familiar with Scheme, we will show how the language can
58 be integrated in LilyPond files.
62 * Scheme simple data types::
63 * Scheme compound data types::
64 * Calculations in Scheme::
66 * Scheme conditionals::
69 @node Scheme simple data types
70 @subsection Scheme simple data types
72 The most basic concept in a language is data typing: numbers, character
73 strings, lists, etc. Here is a list of data types that are relevant to
78 Boolean values are True or False. The Scheme for True is @code{#t}
79 and False is @code{#f}.
84 Numbers are entered in the standard fashion,
85 @code{1} is the (integer) number one, while @code{-1.5} is a
86 floating point number (a non-integer number).
89 Strings are enclosed in double quotes,
95 Strings may span several lines
103 Quotation marks and newlines can also be added with so-called escape
104 sequences. The string @code{a said "b"} is entered as
110 Newlines and backslashes are escaped with @code{\n} and @code{\\}
115 @node Scheme compound data types
116 @subsection Scheme compound data types
118 @unnumberedsubsubsec Pairs
120 TODO -- write about pairs
122 The two elements of a pair may be arbitrary values, for example
127 #'("blah-blah" . 3.14159265)
131 @unnumberedsubsubsec Lists
133 TODO -- write about lists
135 A list is entered by enclosing its elements in parentheses, and adding
136 a quote. For example,
143 We have been using lists all along. A calculation, like @code{(+ 1 2)}
144 is also a list (containing the symbol @code{+} and the numbers 1
145 and@tie{}2). Normally lists are interpreted as calculations, and the
146 Scheme interpreter substitutes the outcome of the calculation. To enter a
147 list, we stop the evaluation. This is done by quoting the list with a
148 quote @code{'} symbol. So, for calculations do not use a quote.
150 Inside a quoted list or pair, there is no need to quote anymore. The
151 following is a pair of symbols, a list of symbols and a list of lists
156 #'(staff clef key-signature)
160 @unnumberedsubsubsec Hash tables
162 TODO -- write about hash tables
165 @node Calculations in Scheme
166 @subsection Calculations in Scheme
168 Scheme can be used to do calculations. It uses @emph{prefix}
169 syntax. Adding 1 and@tie{}2 is written as @code{(+ 1 2)} rather than the
170 traditional @math{1+2}.
177 The arrow @result{} shows that the result of evaluating @code{(+ 1 2)}
178 is@tie{}@code{3}. Calculations may be nested; the result of a function may
179 be used for another calculation.
187 These calculations are examples of evaluations; an expression like
188 @code{(* 3 4)} is replaced by its value @code{12}.
190 The same assignment can be done in completely in Scheme as well,
193 #(define twentyFour (* 2 twelve))
196 @c this next section is confusing -- need to rewrite
198 The @emph{name} of a variable is also an expression, similar to a
199 number or a string. It is entered as
206 @cindex quoting in Scheme
208 The quote mark @code{'} prevents the Scheme interpreter from substituting
209 @code{24} for the @code{twentyFour}. Instead, we get the name
212 @node Scheme procedures
213 @subsection Scheme procedures
215 @unnumberedsubsubsec Predicates
217 @unnumberedsubsubsec Return values
219 TODO -- write about scheme procedures
221 @node Scheme conditionals
222 @subsection Scheme conditionals
224 @unnumberedsubsubsec if
226 @unnumberedsubsubsec cond
229 @node Scheme in LilyPond
230 @section Scheme in LilyPond
234 * LilyPond Scheme syntax::
235 * LilyPond variables::
236 * Object properties::
237 * LilyPond compound variables::
240 @node LilyPond Scheme syntax
241 @subsection LilyPond Scheme syntax
243 In a music file, snippets of Scheme code are introduced with the hash
244 mark @code{#}. So, the previous examples translated to LilyPond are
255 Note that LilyPond comments (@code{%} and @code{%@{ %@}}) cannot
256 be used within Scheme code. Comments in Guile Scheme are entered
260 ; this is a single-line comment
263 This a (non-nestable) Guile-style block comment
264 But these are rarely used by Schemers and never in
269 Multiple consecutive scheme expressions in a music file can be
270 combined using the @code{begin} operator. This permits the number
271 of hash marks to be reduced to one.
279 @c todo -- # introduces a scheme *expression*
280 @c need the concept of an expression
282 If @code{#} is followed by an opening parenthesis, @code{(}, as in
283 the example above, the parser will remain in Scheme mode until
284 a matching closing parenthesis, @code{)}, is found, so further
285 @code{#} symbols to introduce a Scheme section are not required.
287 For the rest of this section, we will assume that the data is entered
288 in a music file, so we add @code{#}s everywhere.
290 @node LilyPond variables
291 @subsection LilyPond variables
294 TODO -- make this read right
297 happens with variables. After defining a variable
304 variables can also be used in expressions, here
307 twentyFour = (* 2 twelve)
311 the number 24 is stored in the variable @code{twentyFour}.
313 @node Object properties
314 @subsection Object properties
316 This syntax will be used very frequently, since many of the layout
317 tweaks involve assigning (Scheme) values to internal variables, for
321 \override Stem #'thickness = #2.6
324 This instruction adjusts the appearance of stems. The value @code{2.6}
325 is put into the @code{thickness} variable of a @code{Stem}
326 object. @code{thickness} is measured relative to the thickness of
327 staff lines, so these stem lines will be @code{2.6} times the
328 width of staff lines. This makes stems almost twice as thick as their
329 normal size. To distinguish between variables defined in input files (like
330 @code{twentyFour} in the example above) and variables of internal
331 objects, we will call the latter @q{properties} and the former
332 @q{variables.} So, the stem object has a @code{thickness} property,
333 while @code{twentyFour} is an variable.
335 @cindex properties vs. variables
336 @cindex variables vs. properties
338 @c todo -- here we're getting interesting. We're now introducing
339 @c LilyPond variable types. I think this deserves a section all
342 @node LilyPond compound variables
343 @subsection LilyPond compound variables
345 @unnumberedsubsubsec Offsets
347 Two-dimensional offsets (X and Y coordinates) as well as object sizes
348 (intervals with a left and right point) are entered as @code{pairs}. A
349 pair@footnote{In Scheme terminology, the pair is called @code{cons},
350 and its two elements are called @code{car} and @code{cdr} respectively.}
351 is entered as @code{(first . second)} and, like symbols, they must be quoted,
354 \override TextScript #'extra-offset = #'(1 . 2)
357 This assigns the pair (1, 2) to the @code{extra-offset} property of the
358 TextScript object. These numbers are measured in staff-spaces, so
359 this command moves the object 1 staff space to the right, and 2 spaces up.
361 @unnumberedsubsubsec Extents
363 todo -- write something about extents
365 @unnumberedsubsubsec Property alists
367 todo -- write something about property alists
369 @unnumberedsubsubsec Alist chains
371 todo -- write something about alist chains
375 * Tweaking with Scheme::
378 @node Tweaking with Scheme
379 @section Tweaking with Scheme
381 We have seen how LilyPond output can be heavily modified using
383 @code{\override TextScript #'extra-offset = ( 1 . -1)}. But
384 we have even more power if we use Scheme. For a full explanation
385 of this, see the @ref{Scheme tutorial}, and
386 @ref{Interfaces for programmers}.
388 We can use Scheme to simply @code{\override} commands,
390 TODO Find a simple example
391 @c This isn't a valid example with skylining
392 @c It works fine without padText -td
396 @lilypond[quote,verbatim,ragged-right]
397 padText = #(define-music-function (parser location padding) (number?)
399 \once \override TextScript #'padding = #$padding
407 c4^"piu mosso" fis a g
413 We can use it to create new commands:
415 @c Check this is a valid example with skylining
416 @c It is - 'padding still works
419 @lilypond[quote,verbatim,ragged-right]
420 tempoPadded = #(define-music-function (parser location padding tempotext)
423 \once \override Score.MetronomeMark #'padding = $padding
424 \tempo \markup { \bold $tempotext }
428 \tempo \markup { "Low tempo" }
430 \tempoPadded #4.0 #"High tempo"
436 Even music expressions can be passed in:
438 @lilypond[quote,verbatim,ragged-right]
439 pattern = #(define-music-function (parser location x y) (ly:music? ly:music?)
446 \pattern {d16 dis} { ais16-> b\p }