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