1 @c -*- coding: us-ascii; mode: texinfo; -*-
3 @chapter Programming work
6 * Overview of LilyPond architecture::
7 * LilyPond programming languages::
8 * Programming without compiling::
11 * Debugging LilyPond::
12 * Adding or modifying features::
15 @node Overview of LilyPond architecture
16 @section Overview of LilyPond architecture
18 LilyPond processes the input file into graphical and musical output in a
19 number of stages. This process, along with the types of routines that
20 accomplish the various stages of the process, is described in this section. A
21 more complete description of the LilyPond architecture and internal program
22 execution is found in Erik Sandberg's
23 @uref{http://lilypond.org/web/images/thesis-erik-sandberg.pdf, master's
27 The first stage of LilyPond processing is @emph{parsing}. In the parsing
28 process, music expressions in LilyPond input format are converted to music
29 expressions in Scheme format. In Scheme format, a music expression is a list
30 in tree form, with nodes that indicate the relationships between various music
31 events. The LilyPond parser is written in Bison.
33 The second stage of LilyPond processing is @emph{iterating}. Iterating
34 assigns each music event to a context, which is the environment in which the
35 music will be finally engraved. The context is responsible for all further
36 processing of the music. It is during the iteration stage that contexts are
37 created as necessary to ensure that every note has a Voice type context (e.g.
38 Voice, TabVoice, DrumVoice, CueVoice, MensuralVoice, VaticanaVoice,
39 GregorianTranscriptionVoice), that the Voice type contexts exist in
40 appropriate Staff type contexts, and that parallel Staff type contexts exist
41 in StaffGroup type contexts. In addition, during the iteration stage each
42 music event is assigned a moment, or a time in the music when the event
45 Each type of music event has an associated iterator. Iterators are defined in
46 *-iterator.cc. During iteration, an
47 event's iterator is called to deliver that music event to the appropriate
50 The final stage of LilyPond processing is @emph{translation}. During
51 translation, music events are prepared for graphical or midi output. The
52 translation step is accomplished by translators or engravers (the distinction
55 Translators are defined in C++ files named *-engraver.cc. In *-engraver.cc, a
56 C++ class of Engraver type is created. The Engraver is also declared as a
57 translator. Much of the work of translating is handled by Scheme functions,
58 which is one of the keys to LilyPond's exceptional flexibility.
61 @node LilyPond programming languages
62 @section LilyPond programming languages
64 Programming in LilyPond is done in a variety of programming languages. Each
65 language is used for a specific purpose or purposes. This section describes
66 the languages used and provides links to reference manuals and tutorials for
67 the relevant language.
71 The core functionality of LilyPond is implemented in C++.
73 C++ is so ubiquitous that it is difficult to identify either a reference
74 manual or a tutorial. Programmers unfamiliar with C++ will need to spend some
75 time to learn the language before attempting to modify the C++ code.
77 The C++ code calls Scheme/GUILE through the GUILE interface, which is
79 @uref{http://www.gnu.org/software/guile/manual/html_node/index.html, GUILE
84 The LilyPond parser is implemented in Bison, a GNU parser generator. The
85 Bison homepage is found at @uref{http://www.gnu.org/software/bison/,
86 gnu.org}. The manual (which includes both a reference and tutorial) is
87 @uref{http://www.gnu.org/software/bison/manual/index.html, available} in a
92 GNU Make is used to control the compiling process and to build the
93 documentation and the website. GNU Make documentation is available at
94 @uref{http://www.gnu.org/software/make/manual/, the GNU website}.
96 @subsection GUILE or Scheme
98 GUILE is the dialect of Scheme that is used as LilyPond's extension language. Many extensions to LilyPond are written entirely in GUILE. The
99 @uref{http://www.gnu.org/software/guile/manual/html_node/index.html,
100 GUILE Reference Manual} is available online.
102 @uref{http://mitpress.mit.edu/sicp/full-text/book/book.html, Structure and
103 Interpretation of Computer Programs}, a popular textbook used to teach
104 programming in Scheme is available in its entirety online.
106 An introduction to Guile/Scheme as used in LilyPond can be found in the
107 Learning Manual, see @rlearning{Scheme tutorial}.
111 MetaFont is used to create the music fonts used by LilyPond. A MetaFont
112 tutorial is available at @uref{http://metafont.tutorial.free.fr/, the
113 METAFONT tutorial page}.
115 @subsection PostScript
117 PostScript is used to generate graphical output. A brief PostScript tutorial
118 is @uref{http://local.wasp.uwa.edu.au/~pbourke/dataformats/postscript/,
119 available online}. The
120 @uref{http://www.adobe.com/devnet/postscript/pdfs/PLRM.pdf, PostScript Lanugage
121 Reference} is available online in PDF format.
125 Python is used for XML2ly and is used for buillding the documentation and the
128 Python documentation is available at @uref{http://www.python.org/doc/,
131 @node Programming without compiling
132 @section Programming without compiling
134 Much of the development work in LilyPond takes place by changing *.ly or
135 *.scm files. These changes can be made without compiling LilyPond. Such
136 changes are described in this section.
139 @subsection Modifying distribution files
141 Much of LilyPond is written in Scheme or LilyPond input files. These
142 files are interpreted when the program is run, rather than being compiled
143 when the program is built, and are present in all LilyPond distributions.
144 You will find .ly files in the ly/ directory and the Scheme files in the
145 scm/ directory. Both Scheme files and .ly files can be modified and
146 saved with any text editor. It's probably wise to make a backup copy of
147 your files before you modify them, although you can reinstall if the
148 files become corrupted.
150 Once you've modified the files, you can test the changes just by running
151 LilyPond on some input file. It's a good idea to create a file that
152 demonstrates the feature you're trying to add. This file will eventually
153 become a regression test and will be part of the LilyPond distribution.
155 @subsection Desired file formatting
157 Files that are part of the LilyPond distribution have Unix-style line
158 endings (LF), rather than DOS (CR+LF) or MacOS 9 and earlier (CR). Make
159 sure you use the necessary tools to ensure that Unix-style line endings are
160 preserved in the patches you create.
162 Tab characters should not be included in files for distribution. All
163 indentation should be done with spaces. Most editors have settings to
164 allow the setting of tab stops and ensuring that no tab characters are
165 included in the file.
167 Scheme files and LilyPond files should be written according to standard
168 style guidelines. Scheme file guidelines can be found at
169 @uref{http://community.schemewiki.org/?scheme-style}. Following these
170 guidelines will make your code easier to read. Both you and others that
171 work on your code will be glad you followed these guidelines.
173 For LilyPond files, you should follow the guidelines for LilyPond snippets
174 in the documentation. You can find these guidelines at
175 @ref{Texinfo introduction and usage policy}.
177 @node Finding functions
178 @section Finding functions
180 When making changes or fixing bugs in LilyPond, one of the initial
181 challenges is finding out where in the code tree the functions to
182 be modified live. With nearly 3000 files in the source tree,
183 trial-and-error searching is generally ineffective. This section
184 describes a process for finding interesting code.
186 @subsection Using the ROADMAP
188 The file ROADMAP is located in the main directory of the lilypond source.
189 ROADMAP lists all of the directories in the LilPond source tree, along
190 with a brief description of the kind of files found in each directory.
191 This can be a very helpful tool for deciding which directories to search
192 when looking for a function.
195 @subsection Using grep to search
197 Having identified a likely subdirectory to search, the grep utility can
198 be used to search for a function name. The format of the grep command is
201 grep -i functionName subdirectory/*
204 This command will search all the contents of the directory subdirectory/
205 and display every line in any of the files that contains
206 functionName. The @code{-i} option makes @command{grep} ignore
207 case -- this can be very useful if you are not yet familiar with
208 our capitalization conventions.
210 The most likely directories to grep for function names are scm/ for
211 scheme files, ly/ for lilypond input (*.ly) files, and lily/ for C++
215 @subsection Using git grep to search
217 If you have used git to obtain the source, you have access to a
218 powerful tool to search for functions. The command:
221 git grep functionName
224 will search through all of the files that are present in the git
225 repository looking for functionName. It also presents the results
226 of the search using @code{less}, so the results are displayed one page
229 @subsection Searching on the git repository at Savannah
231 You can also use the equivalent of git grep on the Savannah server.
236 Go to http://git.sv.gnu.org/gitweb/?p=lilypond.git
239 In the pulldown box that says commit, select grep.
242 Type functionName in the search box, and hit enter/return
246 This will initiate a search of the remote git repository.
251 @c email to wl@gnu.org when I get here.
256 @subsection Handling errors
258 As a general rule, you should always try to continue computations,
259 even if there is some kind of error. When the program stops, it
260 is often very hard for a user to pinpoint what part of the input
261 causes an error. Finding the culprit is much easier if there is
262 some viewable output.
264 So functions and methods do not return errorcodes, they never
265 crash, but report a programming_error and try to carry on.
267 @subsection Languages
269 C++ and Python are preferred. Python code should use PEP 8.
271 @subsection Filenames
273 Definitions of classes that are only accessed via pointers (*) or
274 references (&) shall not be included as include files.
280 ".cc" Implementation files
281 ".icc" Inline definition files
282 ".tcc" non inline Template defs
286 (setq auto-mode-alist
287 (append '(("\\.make$" . makefile-mode)
288 ("\\.cc$" . c++-mode)
289 ("\\.icc$" . c++-mode)
290 ("\\.tcc$" . c++-mode)
291 ("\\.hh$" . c++-mode)
292 ("\\.pod$" . text-mode)
297 The class Class_name is coded in @q{class-name.*}
299 @subsection Indentation
301 Standard GNU coding style is used. In emacs:
304 (add-hook 'c++-mode-hook
305 '(lambda() (c-set-style "gnu")
309 If you like using font-lock, you can also add this to your
313 (setq font-lock-maximum-decoration t)
314 (setq c++-font-lock-keywords-3
316 c++-font-lock-keywords-3
317 '(("\\b\\(a-zA-Z_?+_\\)\\b" 1 font-lock-variable-name-face) ("\\b\\(A-Z?+a-z_?+\\)\\b" 1 font-lock-type-face))
322 @subsection Classes and Types
331 Member variable names end with an underscore:
340 Macro names should be written in uppercase completely.
343 @subsection Broken code
345 Do not write broken code. This includes hardwired dependencies,
346 hardwired constants, slow algorithms and obvious limitations. If
347 you can not avoid it, mark the place clearly, and add a comment
348 explaining shortcomings of the code.
350 We reject broken-in-advance on principle.
357 Messages need to follow Localization.
360 @subsection Localization
362 This document provides some guidelines for programmers write user
363 messages. To help translations, user messages must follow
364 uniform conventions. Follow these rules when coding for LilyPond.
365 Hopefully, this can be replaced by general GNU guidelines in the
366 future. Even better would be to have an English (en_BR, en_AM)
367 guide helping programmers writing consistent messages for all GNU
370 Non-preferred messages are marked with `+'. By convention,
371 ungrammatical examples are marked with `*'. However, such ungrammatical
372 examples may still be preferred.
377 Every message to the user should be localized (and thus be marked
378 for localization). This includes warning and error messages.
381 Don't localize/gettextify:
385 `programming_error ()'s
388 `programming_warning ()'s
394 output strings (PostScript, TeX, etc.)
399 Messages to be localised must be encapsulated in `_ (STRING)' or
400 `_f (FORMAT, ...)'. E.g.:
403 warning (_ ("need music in a score"));
404 error (_f ("cannot open file: `%s'", file_name));
407 In some rare cases you may need to call `gettext ()' by hand. This
408 happens when you pre-define (a list of) string constants for later
409 use. In that case, you'll probably also need to mark these string
410 constants for translation, using `_i (STRING)'. The `_i' macro is
411 a no-op, it only serves as a marker for `xgettext'.
414 char const* messages[] = @{
415 _i ("enable debugging output"),
416 _i ("ignore lilypond version"),
423 puts (gettext (messages i));
427 See also `flower/getopt-long.cc' and `lily/main.cc'.
430 Do not use leading or trailing whitespace in messages. If you need
431 whitespace to be printed, prepend or append it to the translated
435 message ("Calculating line breaks..." + " ");
439 Error or warning messages displayed with a file name and line
440 number never start with a capital, eg,
443 foo.ly: 12: not a duration: 3
446 Messages containing a final verb, or a gerund (`-ing'-form) always
447 start with a capital. Other (simpler) messages start with a
453 Not declaring: `foo'.
457 Avoid abbreviations or short forms, use `cannot' and `do not'
458 rather than `can't' or `don't'
459 To avoid having a number of different messages for the same
460 situation, well will use quoting like this `"message: `%s'"' for all
461 strings. Numbers are not quoted:
464 _f ("cannot open file: `%s'", name_str)
465 _f ("cannot find character number: %d", i)
469 Think about translation issues. In a lot of cases, it is better to
470 translate a whole message. The english grammar must not be imposed
471 on the translator. So, instead of
474 stem at + moment.str () + does not fit in beam
480 _f ("stem at %s does not fit in beam", moment.str ())
484 Split up multi-sentence messages, whenever possible. Instead of
487 warning (_f ("out of tune! Can't find: `%s'", "Key_engraver"));
488 warning (_f ("cannot find font `%s', loading default", font_name));
494 warning (_ ("out of tune:"));
495 warning (_f ("cannot find: `%s', "Key_engraver"));
496 warning (_f ("cannot find font: `%s', font_name));
497 warning (_f ("Loading default font"));
501 If you must have multiple-sentence messages, use full punctuation.
502 Use two spaces after end of sentence punctuation. No punctuation
503 (esp. period) is used at the end of simple messages.
506 _f ("Non-matching braces in text `%s', adding braces", text)
507 _ ("Debug output disabled. Compiled with NPRINT.")
508 _f ("Huh? Not a Request: `%s'. Ignoring.", request)
512 Do not modularise too much; words frequently cannot be translated
513 without context. It is probably safe to treat most occurences of
514 words like stem, beam, crescendo as separately translatable words.
517 When translating, it is preferable to put interesting information
518 at the end of the message, rather than embedded in the middle.
519 This especially applies to frequently used messages, even if this
520 would mean sacrificing a bit of eloquency. This holds for original
521 messages too, of course.
524 en: cannot open: `foo.ly'
525 + nl: kan `foo.ly' niet openen (1)
526 kan niet openen: `foo.ly'* (2)
527 niet te openen: `foo.ly'* (3)
531 The first nl message, although grammatically and stylistically
532 correct, is not friendly for parsing by humans (even if they speak
533 dutch). I guess we would prefer something like (2) or (3).
536 Do not run make po/po-update with GNU gettext < 0.10.35
542 @node Debugging LilyPond
543 @section Debugging LilyPond
545 The most commonly used tool for debugging LilyPond is the GNU debugger
546 gdb. Use of gdb is described in this section.
548 @subsection Debugging overview
550 Using a debugger simplifies troubleshooting in at least two ways.
552 First, breakpoints can be set to pause execution at any desired point.
553 Then, when execution has paused, debugger commands can be issued to
554 explore the values of various variables or to execute functions.
556 Second, the debugger allows the display of a stack trace, which shows
557 the sequence in which functions are called and the arguments to the
558 various function calls.
561 @subsection Compiling with debugging information
563 In order to use a debugger with LilyPond, it is necessary to compile
564 LilyPond with debugging information. This is accomplished by running
565 the following commands in the main LilyPond source directory.
568 ./configure --disable-optimising
573 This will create a version of LilyPond that contains the debugging
574 information that will allow the debugger to tie the source code
575 to the compiled code.
577 You should not do @var{make install} if you want to use a debugger
578 with LilyPond. @var{make install} will strip the debugging information
579 from the LilyPond binary.
581 To set breakpoints in Scheme functions, put
584 \include "guile-debugger.ly"
587 in your input file after any scheme procedures you have defined in
588 that file. When your input file is processed, a guile prompt
589 will be displayed. At the guile prompt, you can set breakpoints with
590 the @code{break!} procedure:
593 guile> (break! my-scheme-procedure)
596 Once you have set the desired breakpoints, you exit the guile repl frame
603 When one of the scheme routines for which you have set breakpoints is
604 entered, guile will interrupt execution in a debug frame. At this point,
605 you will have access to guile debugging commands. For a listing of these
612 @subsection Typical gdb usage
614 @subsection Typical .gdbinit files
616 The behavior of gdb can be readily customized through the use of
617 @var{.gdbinit} files. A @var{.gdbinit} file is a file named
618 @var{.gdbinit} (notice the @qq{.} at the beginning of the file name)
619 that is placed in a user's home directory.
621 The @var{.gdbinit} file below is from Han-Wen. It sets breakpoints
622 for all errors and defines functions for displaying scheme objects
623 (ps), grobs (pgrob), and parsed music expressions (pmusic).
626 file lily/out/lilypond
628 b Grob::programming_error
631 print ly_display_scm($arg0)
634 print ly_display_scm($arg0->self_scm_)
635 print ly_display_scm($arg0->mutable_property_alist_)
636 print ly_display_scm($arg0->immutable_property_alist_)
637 print ly_display_scm($arg0->object_alist_)
640 print ly_display_scm($arg0->self_scm_)
641 print ly_display_scm($arg0->mutable_property_alist_)
642 print ly_display_scm($arg0->immutable_property_alist_)
646 @subsection Using Guile interactively with LilyPond
648 In order to experiment with Scheme programming in the LilyPond
649 environment, it is convenient to have a Guile interpreter that
650 has all the LilyPond modules loaded. This requires the following
653 First, define a Scheme symbol for the active module
657 #(module-define! (resolve-module '(guile-user))
658 'lilypond-module (current-module))
661 Second, place a Scheme function in the .ly file that gives an interactive Guile
668 When the .ly file is compiled, this causes the compilation to be interrupted
669 and an interactive guile prompt to appear. When the guile prompt appears,
670 the LilyPond active module must be set as the current guile module:
673 guile> (set-current-module lilypond-module)
676 Proper operation of these commands can be demonstrated by typing the name
677 of a LilyPond public scheme function to see if it's properly defined:
680 guile> fret-diagram-verbose-markup
681 #<procedure fret-diagram-verbose-markup (layout props marking-list)>
684 If the LilyPond module has not been correctly loaded, an error
685 message will be generated:
688 guile> fret-diagram-verbose-markup
689 ERROR: Unbound variable: fret-diagram-verbose-markup
690 ABORT: (unbound-variable)
693 Once the module is properly loaded, any valid LilyPond Scheme expression
694 can be entered at the interactive prompt.
696 After the investigation is complete, the interactive guile interpreter
703 The compilation of the .ly file will then continue.
705 @node Adding or modifying features
706 @section Adding or modifying features
708 When a new feature is to be added to LilyPond, it is necessary to
709 ensure that the feature is properly integrated to maintain
710 its long-term support. This section describes the steps necessary
711 for feature addition.
713 @subsection Write the code
715 You should create a new git branch for writing the code, as that
716 will separate it from the master branch and allow you to continue
717 to work on small projects related to master.
719 Please be sure to follow the rules for programming style discussed
720 earlier in this chapter.
722 @subsection Write regression tests
724 In order to demonstrate that the code works properly, you will
725 need to write one or more regression tests. These tests are
726 typically .ly files that are found in input/regression.
728 Regression tests should be as brief as possible to demonstrate the
729 functionality of the code.
731 Regression tests should generally cover one issue per test. Several
732 short, single-issue regression tests are preferred to a single, long,
733 multiple-issue regression test.
735 Use existing regression tests as templates to demonstrate the type of
736 header information that should be included in a regression test.
738 @subsection Write documentation
740 Although it is not required, it is helpful if the developer can
741 write relevant material for inclusion in the Notation Reference.
742 If the developer does not feel qualified to write the documentation,
743 a documentation editor will be able to write it from the regression
746 If the modification changes the input syntax so that inline snippets in
747 the documentation need to be changed, you will need to change the
748 snippets in both the english version of the documentation and any
749 translated versions. If you do not change the snippets in all
750 translations, older versions of the snippet may be included
751 when the documentation is built.
753 If lsr snippets need to be changed, the snippet should be copied to
754 input/new and modified there. The portions of the snippet that
755 are added by makelsr.py should be removed. The changed snippet
756 will then be included in all versions of the documentation.
758 If non-snippet text is changed in the english documentation, no
759 corresponding changes should be made in the translated documentation.
761 @subsection Write convert-ly rule
763 If the modification changes the input syntax, a convert-ly rule
764 should be written to automatically update input files from older
767 convert-ly rules are found in python/convertrules.py
769 If possible, the convert-ly rule should allow automatic updating
770 of the file. In some cases, this will not be possible, so the
771 rule will simply point out to the user that the feature needs
774 @subsection Write NEWS entry
776 An entry should be added to the NEWS file to describe the feature
777 changes to be implemented. This is especially important for changes
778 that change input file syntax.
780 Hints for NEWS file entries are given at the top of the NEWS file.
782 New entries in NEWS go at the top of the file.
784 The NEWS entry should be written to show how the new change
785 improves LilyPond, if possible.
787 @subsection Verify regression test
789 In order to avoid breaking LilyPond, it is important to verify that
790 the regression tests all succeed. This process is described in
791 @ref{Regression tests}.
793 @subsection Post patch for comments
795 For any change other than a minor change, a patch set should be
796 posted on Rietveld for comment.
798 The patch set is posted by issuing the following command, after
799 first committing all changes:
802 git-cl upload <reference SHA1 ID>
806 where <reference SHA1 ID> is the SHA1 ID of the commit to be used
807 as a reference source for the patch (generally, this will be the
808 SHA1 ID of origin/master).
810 After prompting for an email and a password, the patch set will be
813 An email should then be sent to lilypond-devel, with a subject line
814 starting with PATCH:, asking for comments on the patch.
816 As revisions are made in response to comments, successive patch sets
817 for the same issue can be uploaded by reissuing the git-cl command.
819 @subsection Push patch
821 Once all the comments have been addressed, the patch can be pushed.
823 If the author has push privileges, the author will push the patch.
824 Otherwise, a developer with push privileges will push the patch.
826 @subsection Closing the issues
828 Once the patch has been pushed, all the relevant issues should be
831 On Rietveld, the author should log in an close the issue either by
832 using the @q{Edit Issue} link, or by clicking the circled x icon
833 to the left of the issue name.
835 If the changes were in response to a feature request on the Google
836 issue tracker for LilyPond, the author should change the status to
837 @q{Fixed_x_y_z} where the patch was fixed in version x.y.z. If
838 the author does not have privileges to change the status, an email
839 should be sent to bug-lilypond requesting the BugMeister to change