]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/devel/programming-work.itexi
Doc: Add stub for LilyPond architecture to CG
[lilypond.git] / Documentation / devel / programming-work.itexi
1 @c -*- coding: us-ascii; mode: texinfo; -*-
2 @node Programming work
3 @chapter Programming work
4
5 @menu
6 * Overview of LilyPond architecture::
7 * LilyPond programming languages::
8 * Programming without compiling::  
9 * Finding functions::           
10 * Code style::                  
11 * Debugging LilyPond::          
12 @end menu
13
14 @node Overview of LilyPond architecture::
15 @section Overview of LilyPond architecture::
16
17 TODO -- put in brief description along with link to Erik Sandberg's
18 thesis.
19
20 Key concepts:
21
22 Parsing converts input file to scheme music expressions.
23
24 Iterating converts scheme music expressions into a context tree.
25
26 Translation converts the context tree into graphical and/or midi output.
27
28 Music expressions provide relative timing for music events.
29
30 Contexts provide an evaluation environment within which an engraver converts
31 the music event to output.
32
33 Question:  What is an engraver?  I think an engraver handles both iteration
34 and translation, but I need to check on that.
35
36 Sandberg says that "translator" and "engraver" are synonymous for his thesis;
37 the distinction between them is not relevant for that document.  What is the
38 difference?
39
40 @node LilyPond programming languages
41 @section LilyPond programming languages
42
43 Programming in LilyPond is done in a variety of programming languages.  Each
44 language is used for a specific purpose or purposes.  This section describes
45 the languages used and provides links to reference manuals and tutorials for
46 the relevant language.
47
48 @subsection C++
49
50 The core functionality of LilyPond is implemented in C++.
51
52 C++ is so ubiquitous that it is difficult to identify either a reference
53 manual or a tutorial.  Programmers unfamiliar with C++ will need to spend some
54 time to learn the language before attempting to modify the C++ code.
55
56 The C++ code calls Scheme/GUILE through the GUILE interface, which is
57 documented in the
58 @uref{http://www.gnu.org/software/guile/manual/html_node/index.html, GUILE
59   Reference Manual}.
60
61 @subsection GNU Bison
62
63 The LilyPond parser is implemented in Bison, a GNU parser generator.  The
64 Bison homepage is found at @uref{http://www.gnu.org/software/bison/,
65 gnu.org}.  The manual (which includes both a reference and tutorial) is
66 @uref{http://www.gnu.org/software/bison/manual/index.html, available} in a
67 variety of formats.
68
69 @subsection GNU Make
70
71 GNU Make is used to control the compiling process and to build the
72 documentation and the website.  GNU Make documentation is available at
73 @uref{http://www.gnu.org/software/make/manual/, the GNU website}.
74
75 @subsection GUILE or Scheme
76
77 GUILE is the dialect of Scheme that is used as LilyPond's extension language.  Many extensions to LilyPond are written entirely in GUILE.  The 
78 @uref{http://www.gnu.org/software/guile/manual/html_node/index.html,
79 GUILE Reference Manual} is available online.
80
81 @uref{http://mitpress.mit.edu/sicp/full-text/book/book.html, Structure and
82 Interpretation of Computer Programs}, a popular textbook used to teach
83 programming in Scheme is available in its entirety online.
84
85 @subsection MetaFont
86
87 MetaFont is used to create the music fonts used by LilyPond.  A MetaFont
88 tutorial is available at @uref{http://metafont.tutorial.free.fr/, the
89 METAFONT tutorial page}.
90
91 @subsection PostScript
92
93 PostScript is used to generate graphical output.  A brief PostScript tutorial
94 is @uref{http://local.wasp.uwa.edu.au/~pbourke/dataformats/postscript/,
95 available online}.  The
96 @uref{http://www.adobe.com/devnet/postscript/pdfs/PLRM.pdf, PostScript Lanugage
97 Reference} is available online in PDF format.
98
99 @subsection Python
100
101 Python is used for XML2ly and is used for buillding the documentation and the
102 website.
103
104 Python documentation is available at @uref{http://www.python.org/doc/,
105 python.org}.
106
107 @node Programming without compiling
108 @section Programming without compiling
109
110 Much of the development work in LilyPond takes place by changing *.ly or
111 *.scm files.  These changes can be made without compiling LilyPond.  Such
112 changes are described in this section.
113
114
115 @subsection Modifying distribution files
116
117 Much of LilyPond is written in Scheme or LilyPond input files.  These
118 files are interpreted when the program is run, rather than being compiled
119 when the program is built, and are present in all LilyPond distributions.
120 You will find .ly files in the ly/ directory and the Scheme files in the
121 scm/ directory.  Both Scheme files and .ly files can be modified and
122 saved with any text editor.  It's probably wise to make a backup copy of
123 your files before you modify them, although you can reinstall if the
124 files become corrupted.
125
126 Once you've modified the files, you can test the changes just by running
127 LilyPond on some input file.  It's a good idea to create a file that
128 demonstrates the feature you're trying to add.  This file will eventually
129 become a regression test and will be part of the LilyPond distribution.
130
131 @subsection Desired file formatting
132
133 Files that are part of the LilyPond distribution have Unix-style line
134 endings (LF), rather than DOS (CR+LF) or MacOS 9 and earlier (CR).  Make
135 sure you use the necessary tools to ensure that Unix-style line endings are
136 preserved in the patches you create.
137
138 Tab characters should not be included in files for distribution.  All
139 indentation should be done with spaces.  Most editors have settings to
140 allow the setting of tab stops and ensuring that no tab characters are
141 included in the file.
142
143 Scheme files and LilyPond files should be written according to standard
144 style guidelines.  Scheme file guidelines can be found at
145 @uref{http://community.schemewiki.org/?scheme-style}.  Following these
146 guidelines will make your code easier to read.  Both you and others that
147 work on your code will be glad you followed these guidelines.
148
149 For LilyPond files, you should follow the guidelines for LilyPond snippets
150 in the documentation.  You can find these guidelines at
151 @ref{Texinfo introduction and usage policy}.
152
153 @node Finding functions
154 @section Finding functions
155
156 When making changes or fixing bugs in LilyPond, one of the initial
157 challenges is finding out where in the code tree the functions to
158 be modified live.  With nearly 3000 files in the source tree,
159 trial-and-error searching is generally ineffective. This section
160 describes a process for finding interesting code.
161
162 @subsection Using the ROADMAP
163
164 The file ROADMAP is located in the main directory of the lilypond source.
165 ROADMAP lists all of the directories in the LilPond source tree, along
166 with a brief description of the kind of files found in each directory.
167 This can be a very helpful tool for deciding which directories to search
168 when looking for a function.
169
170
171 @subsection Using grep to search
172
173 Having identified a likely subdirectory to search, the grep utility can
174 be used to search for a function name.  The format of the grep command is
175
176 @example 
177 grep -i functionName subdirectory/*
178 @end example
179
180 This command will search all the contents of the directory subdirectory/
181 and display every line in any of the files that contains
182 functionName.  The @code{-i} option makes @command{grep} ignore
183 case -- this can be very useful if you are not yet familiar with
184 our capitalization conventions.
185
186 The most likely directories to grep for function names are scm/ for
187 scheme files, ly/ for lilypond input (*.ly) files, and lily/ for C++
188 files.  
189
190
191 @subsection Using git grep to search
192
193 If you have used git to obtain the source, you have access to a
194 powerful tool to search for functions.  The command:
195
196 @example
197 git grep functionName
198 @end example
199
200 will search through all of the files that are present in the git
201 repository looking for functionName.  It also presents the results
202 of the search using @code{less}, so the results are displayed one page
203 at a time.
204
205 @subsection Searching on the git repository at Savannah
206
207 You can also use the equivalent of git grep on the Savannah server.
208
209 @itemize
210
211 @item
212 Go to http://git.sv.gnu.org/gitweb/?p=lilypond.git
213
214 @item
215 In the pulldown box that says commit, select grep.
216
217 @item
218 Type functionName in the search box, and hit enter/return
219
220 @end itemize
221
222 This will initiate a search of the remote git repository.
223
224
225 @node Code style
226 @section Code style 
227 @c email to wl@gnu.org when I get here.
228
229 @menu
230 @end menu
231
232 @subsection Handling errors
233
234 As a general rule, you should always try to continue computations,
235 even if there is some kind of error.  When the program stops, it
236 is often very hard for a user to pinpoint what part of the input
237 causes an error.  Finding the culprit is much easier if there is
238 some viewable output.
239
240 So functions and methods do not return errorcodes, they never
241 crash, but report a programming_error and try to carry on.
242
243 @subsection Languages
244
245 C++ and Python are preferred.  Python code should use PEP 8.
246
247 @subsection Filenames
248
249 Definitions of classes that are only accessed via pointers (*) or
250 references (&) shall not be included as include files.
251
252 @verbatim
253    filenames
254
255         ".hh"   Include files
256              ".cc"      Implementation files
257              ".icc"     Inline definition files
258              ".tcc"     non inline Template defs
259
260    in emacs:
261
262              (setq auto-mode-alist
263                    (append '(("\\.make$" . makefile-mode)
264                         ("\\.cc$" . c++-mode)
265                         ("\\.icc$" . c++-mode)
266                         ("\\.tcc$" . c++-mode)
267                         ("\\.hh$" . c++-mode)
268                         ("\\.pod$" . text-mode)
269                         )
270                       auto-mode-alist))
271 @end verbatim
272
273 The class Class_name is coded in @q{class-name.*}
274
275 @subsection Indentation
276
277 Standard GNU coding style is used. In emacs:
278
279 @verbatim
280              (add-hook 'c++-mode-hook
281                   '(lambda() (c-set-style "gnu")
282                      ))
283 @end verbatim
284
285 If you like using font-lock, you can also add this to your
286 @q{.emacs}:
287
288 @verbatim
289              (setq font-lock-maximum-decoration t)
290              (setq c++-font-lock-keywords-3
291                    (append
292                     c++-font-lock-keywords-3
293                     '(("\\b\\(a-zA-Z_?+_\\)\\b" 1 font-lock-variable-name-face) ("\\b\\(A-Z?+a-z_?+\\)\\b" 1 font-lock-type-face))
294                     ))
295 @end verbatim
296
297
298 @subsection Classes and Types
299
300 @verbatim
301 This_is_a_class
302 @end verbatim
303
304
305 @subsection Members
306
307 Member variable names end with an underscore:
308
309 @verbatim
310 Type Class::member_
311 @end verbatim
312
313
314 @subsection Macros
315
316 Macro names should be written in uppercase completely.
317
318
319 @subsection Broken code
320
321 Do not write broken code.  This includes hardwired dependencies,
322 hardwired constants, slow algorithms and obvious limitations.  If
323 you can not avoid it, mark the place clearly, and add a comment
324 explaining shortcomings of the code.
325
326 We reject broken-in-advance on principle.
327
328 @subsection Naming
329
330
331 @subsection Messages
332
333 Messages need to follow Localization.
334
335
336 @subsection Localization
337
338 This document provides some guidelines for programmers write user
339 messages.  To help translations, user messages must follow
340 uniform conventions.  Follow these rules when coding for LilyPond.
341 Hopefully, this can be replaced by general GNU guidelines in the
342 future.  Even better would be to have an English (en_BR, en_AM)
343 guide helping programmers writing consistent messages for all GNU
344 programs.
345
346 Non-preferred messages are marked with `+'. By convention,
347 ungrammatical examples are marked with `*'.  However, such ungrammatical
348 examples may still be preferred.
349
350 @itemize
351
352 @item
353 Every message to the user should be localized (and thus be marked
354 for localization). This includes warning and error messages.
355
356 @item
357 Don't localize/gettextify:
358
359 @itemize
360 @item
361 `programming_error ()'s
362
363 @item
364 `programming_warning ()'s
365
366 @item
367 debug strings
368
369 @item
370 output strings (PostScript, TeX, etc.)
371
372 @end itemize
373
374 @item
375 Messages to be localised must be encapsulated in `_ (STRING)' or
376 `_f (FORMAT, ...)'. E.g.:
377
378 @example
379 warning (_ ("need music in a score"));
380 error (_f ("cannot open file: `%s'", file_name));
381 @end example
382     
383 In some rare cases you may need to call `gettext ()' by hand. This
384 happens when you pre-define (a list of) string constants for later
385 use. In that case, you'll probably also need to mark these string
386 constants for translation, using `_i (STRING)'. The `_i' macro is
387 a no-op, it only serves as a marker for `xgettext'.
388
389 @example
390 char const* messages[] = @{
391   _i ("enable debugging output"),
392   _i ("ignore lilypond version"),
393   0
394 @};
395
396 void
397 foo (int i)
398 @{
399   puts (gettext (messages i));
400 @}
401 @end example
402     
403 See also `flower/getopt-long.cc' and `lily/main.cc'.
404
405 @item
406 Do not use leading or trailing whitespace in messages. If you need
407 whitespace to be printed, prepend or append it to the translated
408 message
409
410 @example
411 message ("Calculating line breaks..." + " ");
412 @end example
413
414 @item
415 Error or warning messages displayed with a file name and line
416 number never start with a capital, eg,
417
418 @example
419 foo.ly: 12: not a duration: 3
420 @end example
421       
422 Messages containing a final verb, or a gerund (`-ing'-form) always
423 start with a capital. Other (simpler) messages start with a
424 lowercase letter
425
426 @example
427 Processing foo.ly...
428 `foo': not declared.
429 Not declaring: `foo'.
430 @end example
431     
432 @item
433 Avoid abbreviations or short forms, use `cannot' and `do not'
434 rather than `can't' or `don't'
435 To avoid having a number of different messages for the same
436 situation, we'll use quoting like this `"message: `%s'"' for all
437 strings. Numbers are not quoted:
438
439 @example
440 _f ("cannot open file: `%s'", name_str)
441 _f ("cannot find character number: %d", i)
442 @end example
443     
444 @item
445 Think about translation issues. In a lot of cases, it is better to
446 translate a whole message. The english grammar mustn't be imposed
447 on the translator. So, instead of
448
449 @example
450 stem at  + moment.str () +  does not fit in beam
451 @end example
452     
453 have
454
455 @example
456 _f ("stem at %s does not fit in beam", moment.str ())
457 @end example
458     
459 @item
460 Split up multi-sentence messages, whenever possible. Instead of
461
462 @example
463 warning (_f ("out of tune!  Can't find: `%s'", "Key_engraver"));
464 warning (_f ("cannot find font `%s', loading default", font_name));
465 @end example
466     
467 rather say:
468
469 @example
470 warning (_ ("out of tune:"));
471 warning (_f ("cannot find: `%s', "Key_engraver"));
472 warning (_f ("cannot find font: `%s', font_name));
473 warning (_f ("Loading default font"));
474 @end example
475     
476 @item
477 If you must have multiple-sentence messages, use full punctuation.
478 Use two spaces after end of sentence punctuation. No punctuation
479 (esp. period) is used at the end of simple messages.
480
481 @example
482 _f ("Non-matching braces in text `%s', adding braces", text)
483 _ ("Debug output disabled.  Compiled with NPRINT.")
484 _f ("Huh?  Not a Request: `%s'.  Ignoring.", request)
485 @end example
486     
487 @item
488 Do not modularise too much; words frequently cannot be translated
489 without context. It's probably safe to treat most occurences of
490 words like stem, beam, crescendo as separately translatable words.
491
492 @item
493 When translating, it is preferable to put interesting information
494 at the end of the message, rather than embedded in the middle.
495 This especially applies to frequently used messages, even if this
496 would mean sacrificing a bit of eloquency. This holds for original
497 messages too, of course.
498
499 @example
500 en: cannot open: `foo.ly'
501 +   nl: kan `foo.ly' niet openen (1)
502 kan niet openen: `foo.ly'*   (2)
503 niet te openen: `foo.ly'*    (3)
504 @end example
505
506     
507 The first nl message, although grammatically and stylistically
508 correct, is not friendly for parsing by humans (even if they speak
509 dutch). I guess we'd prefer something like (2) or (3).
510
511 @item
512 Do not run make po/po-update with GNU gettext < 0.10.35
513
514 @end itemize
515
516
517
518 @node Debugging LilyPond
519 @section Debugging LilyPond
520
521 The most commonly used tool for debugging LilyPond is the GNU debugger
522 gdb.  Use of gdb is described in this section.
523
524 @subsection Debugging overview
525
526 Using a debugger simplifies troubleshooting in at least two ways.
527
528 First, breakpoints can be set to pause execution at any desired point.
529 Then, when execution has paused, debugger commands can be issued to 
530 explore the values of various variables or to execute functions.
531
532 Second, the debugger allows the display of a stack trace, which shows
533 the sequence in which functions are called and the arguments to the
534 various function calls.
535
536
537 @subsection Compiling with debugging information
538
539 In order to use a debugger with LilyPond, it is necessary to compile
540 LilyPond with debugging information.  This is accomplished by ...
541
542 TODO -- get good description here, or perhaps add debugging compile
543 to AU1.1 as it comes to CG and just use a reference here.
544
545 TODO -- Test the following to make sure it is true.
546
547 If you want to be able to set breakpoints in Scheme functions, it is
548 necessary to compile guile with debugging information.  This is done
549 by ...
550
551 TODO -- get compiling description for guile here.
552
553 @subsection Typical gdb usage
554
555 @subsection Typical .gdbinit files
556
557 The behavior of gdb can be readily customized through the use of
558 @var{.gdbinit} files.  A @var{.gdbinit} file is a file named
559 @var{.gdbinit} (notice the @qq{.} at the beginning of the file name)
560   that is placed in a user's home directory.
561
562 The @var{.gdbinit} file below is from Han-Wen.  It sets breakpoints
563 for all errors and defines functions for displaying scheme objects
564 (ps), grobs (pgrob), and parsed music expressions (pmusic).
565
566 @example
567 file lily/out/lilypond 
568 b scm_error 
569 b programming_error 
570 b Grob::programming_error 
571
572 define ps 
573    print ly_display_scm($arg0) 
574   end 
575   define pgrob 
576      print ly_display_scm($arg0->self_scm_) 
577      print ly_display_scm($arg0->mutable_property_alist_) 
578      print ly_display_scm($arg0->immutable_property_alist_) 
579      print ly_display_scm($arg0->object_alist_) 
580   end 
581   define pmusic 
582      print ly_display_scm($arg0->self_scm_) 
583      print ly_display_scm($arg0->mutable_property_alist_) 
584      print ly_display_scm($arg0->immutable_property_alist_) 
585   end 
586 @end example
587
588 @subsection Using Guile interactively with LilyPond
589
590 In order to experiment with Scheme programming in the LilyPond
591 environment, it is convenient to have a Guile interpreter that
592 has all the LilyPond modules loaded.  This requires the following
593 steps.
594
595 First, define a Scheme symbol for the active module
596 in the .ly file:
597
598 @example
599 #(module-define! (resolve-module '(guile-user))
600     'lilypond-module (current-module))
601 @end example
602
603 Second, place a Scheme function in the .ly file that gives an interactive Guile
604 prompt:
605
606 @example
607 #(top-repl)
608 @end example
609
610 When the .ly file is compiled, this causes the compilation to be interrupted
611 and an interactive guile prompt to appear.  When the guile prompt appears,
612 the LilyPond active module must be set as the current guile module:
613
614 @example
615 guile> (set-current-module lilypond-module)
616 @end example
617
618 Proper operation of these commands can be demonstrated by typing the name
619 of a LilyPond public scheme function to see if it's properly defined:
620
621 @example
622 guile> fret-diagram-verbose-markup
623 #<procedure fret-diagram-verbose-markup (layout props marking-list)>
624 @end example
625
626 If the LilyPond module has not been correctly loaded, an error
627 message will be generated:
628
629 @example
630 guile> fret-diagram-verbose-markup
631 ERROR: Unbound variable: fret-diagram-verbose-markup
632 ABORT: (unbound-variable)
633 @end example
634
635 Once the module is properly loaded, any valid LilyPond Scheme expression
636 can be entered at the interactive prompt.
637
638 After the investigation is complete, the interactive guile interpreter
639 can be exited:
640
641 @example
642 guile> (quit)
643 @end example
644
645 The compilation of the .ly file will then continue.
646