]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/hacking.texi
release: 1.2.11
[lilypond.git] / Documentation / hacking.texi
1 \input texinfo @c -*-texinfo-*-
2 @setfilename internals.info
3 @settitle LilyPond internals
4
5 @node Top, LilyPond internals, (dir), (dir)
6 @top
7
8
9 @menu
10 * LilyPond internals::
11 * Overview::
12 * mudela::                      
13 * Request_engraver::            
14 * Items and spanners::          
15 * Dependencies::                
16 * Breaking::                    
17 * Coding standards::
18 * Making patches::
19 @end menu
20
21 @node LilyPond internals,  , Top, Top
22 @menu
23 * Overview::                      Overview
24 * mudela::                        mudela
25 * Request_engraver::              Request_engraver
26 * Items and spanners::            Items and spanners
27 * Dependencies::                  Dependencies
28 * Breaking::                      Breaking
29 @end menu
30
31 @chapter LilyPond internals
32
33
34 This documents some aspects of the internals of GNU LilyPond. Some of
35 this stuff comes from e-mail I wrote, some from e-mail others wrote,
36 some are large comments taken away from the headers. This page may be
37 a little incoherent.  Unfortunately, it is also quite outdated.  A
38 more thorough  and understandable document is in the works.
39
40 You should use @code{doc++} to take a peek at the sources.
41
42 @node Overview, mudela, Top, Top
43 @section Overview
44
45 GNU LilyPond is a "multi-pass" system. The different passes have been
46 created so that they do not depend on each other. In a later stage
47 some parts may be moved into libraries, or seperate programs, or they
48 might be integrated in larger systems.
49
50 @table @samp
51
52 @item Parsing:
53
54 No difficult algorithms. The .ly file is read, and converted to a list
55 of @code{Scores}, which each contain @code{Music} and paper/midi-definitions.
56
57 @item Interpreting music
58
59 The music is walked through in time-order. The iterators which do the
60 walking report Music to Translators which use this information to
61 create elements, either MIDI or "visual" elements. The translators
62 form a hierarchy; the ones for paper output are Engravers, for MIDI
63 Performers.
64
65 The translators swallow Music (mostly atomic gobs called Requests),
66 create elements, broadcast them to other translators on higher or same
67 level in the hierarchy:
68
69 The stem of a voice A is broadcast to the staff which contains A, but
70 not to the stems, beams and noteheads of a different voice (say B) or
71 a different staff. The stem and noteheads of A are coupled, because
72 the the Note_heads_engraver broadcasts its heads, and the Stem_engraver catches
73 these.
74
75 The engraver which agrees to handle a request decides whether to to
76 honor the request, ignore it, or merge it with other requests. Merging
77 of requests is preferably done with other requests done by members of
78 the same voicegroups (beams, brackets, stems). In this way you can put
79 the voices of 2 instruments in a conductor's score so they make chords
80 (the Beam requests of both instruments will be merged).
81
82 @item Prebreaking
83
84 Breakable stuff (eg. clefs and bars) are copied into pre and
85 postbreaks.
86
87 @item Preprocessing
88
89 Some dependencies are resolved, such as the direction of stems, beams,
90 and "horizontal" placement issues (the order of clefs,  keys etc,
91 placement of chords in multi-voice music), 
92
93 @item Break calculation:
94
95 The lines and horizontal positions of the columns are determined.
96
97 @item Breaking
98
99 Through some magical interactions with Line_of_score and Super_elem
100 (check out the source) the "lines" are produced.
101
102 All other spanners can figure across which lines they are spread. If
103 applicable, they break themselves into pieces. After this, each piece
104 (or, if there are no pieces, the original spanner itself) throws out
105 any dependencies which are in the wrong line.
106
107 @item Postprocesing:
108
109 Some items and all spanners need computation after the Paper_column
110 positions are determined. Examples: slurs, vertical positions of
111 staffs.
112
113 @item Output paper
114
115 @end table
116
117 @node mudela, Request_engraver, Overview, Top
118 @section mudela
119
120 Most information is stored in the form of a request.  In music
121 typesetting, the user might want to cram a lot more symbols on the
122 paper than actually fits. To reflect this idea (the user asks more
123 than we can do), the container for this data is called Request.
124
125 In a lot of other formats this would be called an 'Event'
126
127 @table @samp
128 @item @code{Barcheck_req}
129     Checks during music processing if start of this voice element
130     coincides with the start of a measure. Handy to check if you left out
131     some voice elts.
132 @item @code{Note_req}
133     LilyPond has to decide if the ball should be hanging left or
134     right. This influences the horizontal dimensions of a column, and this
135     is why request processing should be done before horizontal spacing.
136     Other voices' frivolities may cause the need for accidentals, so this
137     is also for the to decide. The engraver can decide on positioning based on
138     ottava commands and the appropriate clef.
139 @item @code{Rest_req}
140     Typeset a rest.
141 @item @code{Span_req}
142     This type of request typically results in the creation of a @code{Spanner}
143 @item @code{Beam_req}
144     Start/stop a beam.
145     Engraver has to combine this request with the stem_request, since the
146     number of flags that a stem wants to carry will determine the
147     number of beams.
148 @item @code{Dynamic}
149     Each dynamic is bound to one note (a crescendo spanning multiple
150     notes is thought to be made of two "dynamics": a start and a stop).
151     Dynamic changes can occur in a smaller time than the length of its
152     note, therefore fore each  @code{Dynamic} request carries a time, measured
153     from the start of its note.
154 @end table
155
156 @node Request_engraver, Items and spanners, mudela, Top
157 @section Request_engraver
158
159 In the previous section the idea of Request has been explained, but
160 this only solves one half of the problem. The other half is deciding
161 which requests should be honored, which should merged with other
162 requests, and which should be ignored. Consider this input
163
164 @example 
165
166         \type Staff < % chord
167                 @{ \meter 2/4; [c8 c8] @}
168                 @{\meter 2/4;  [e8 e8] @}
169         >
170  
171 @end example 
172
173 Both the cs and es are part of a staff (they are in the same
174 Voice_group), so they should share meters, but the two [ ] pairs
175 should be merged.
176
177 The judge in this "allocation" problem a set of brokers: the requests
178 are transmitted to so-called engravers which respond if they want to
179 accept a request eg, the @code{Notehead_engraver} will accept
180 @code{Note_req}s, and turn down @code{Slur_req}s. If the Music_iterator
181 cannot find a engraver that wants the request, it is junked (with a
182 warning message).
183
184 After all requests have been either assigned, or junked, the Engraver
185 will process the requests (which usually means creating an @code{Item}
186 or @code{Spanner}). If a @code{Request_engraver} creates something, it
187 tells the enclosing context. If all items/spanners have been created,
188 then each Engraver is notified of any created Score_element, via a
189 broadcasting system.
190
191 @unnumberedsubsec example:
192
193 @example 
194
195         c4
196  
197 @end example 
198
199 produces:
200
201 @example 
202
203         Note_request (duration 1/4)
204         Stem_request (duration 1/4)
205  
206 @end example 
207
208 Note_request will be taken by a @code{Notehead_engraver}, stem_request
209 will be taken by a @code{Stem_beam_engraver}. @code{Notehead_engraver}
210 creates a @code{Notehead}, @code{Stem_beam_engraver} creates a
211 @code{Stem}. Both announce this to the Staff_engraver. Staff_engraver
212 will tell @code{Stem_beam_engraver} about the @code{Notehead}, which
213 will add the @code{Notehead} to the @code{Stem} it just created.
214
215 To decide on merging, several engravers have been grouped. Please
216 check @file{init/engraver.ly}.
217
218 @node Items and spanners, Dependencies, Request_engraver, Top
219 @section Items and spanners
220
221 The symbols that are printed, are generated by items and spanners
222 (staff-elements). An item has one horizontal position, whereas a
223 spanner spans several columns.
224
225 @node Dependencies, Breaking, Items and spanners, Top
226 @section Dependencies
227
228 In music symbols depend on each other: the stems of a beam should
229 point in the same direction as the beam itself, so the stems of a beam
230 depend on the beam. In the same way do scripts depend on the direction
231 of the stem. To reflect this, LilyPond has the notion of dependency.
232 It works in the same fashion that @code{make} uses to build programs:
233 before a stem is calculated, its dependencies (the beam) should be
234 calculated. Before a slur is calculated, its dependencies (stems,
235 noteheads) should be calculated.
236
237 @node Breaking, Making patches, Dependencies, Top
238 @section Breaking
239
240 So what is this PREBREAK and POSTBREAK stuff?
241
242 Let's take text as an example. In German some compound
243 words change their spelling if they are broken: "backen" becomes
244 "bak-ken".  TeX has a mechanism to deal with this, you would define
245 the spelling of "backen" in TeX in this way
246
247         \discretionary@{bak-@}@{ken@}@{backen@}
248
249 These 3 arguments are called "prebreak", "postbreak" and "nobreak"
250 text.
251
252 The same problem exists when typesetting music. If a line of music is
253 broken, the next line usually gets a clef. So in TeX terms, the clef
254 is a postbreak. The same thing happens with meter signs: Normally the
255 meter follows the bar. If a line is broken at that bar, the bar along
256 with the meter stays on the "last" line, but the next line also gets a
257 meter sign after the clef. Using the previous notation,
258
259         \discretionary@{bar meter@}@{clef meter@}@{ bar meter @}
260
261 In GNU Lilypond, we have the same concepts (and the same
262 terminology). Each (nonrhythmic) symbol is typeset in  a nonrhythmic column
263 At a breakpoint, multiple symbols are printed; symbols to be printed
264 if the line is not broken, symbols to appear on the previous line, and
265 on the next line if it is broken.
266
267 @node Coding standards,  , Top, Top
268
269 @chapter CodingStyle - standards while programming for GNU
270 LilyPond
271
272 We use these standards while doing programming for GNU LilyPond.  If
273 you do some hacking, we appreciate it if you would follow this rules,
274 but if you don't, we still like you.
275
276 Functions and methods do not return errorcodes.
277
278 @quotation
279
280 A program should be light and agile, its subroutines
281 connected like a string of pearls.  The spirit and intent of
282 the program should be retained throughout.  There should be
283 neither too little nor too much, neither needless loops nor
284 useless variables, neither lack of structure nor overwhelming
285 rigidity.
286
287 A program should follow the 'Law of Least
288 Astonishment'.  What is this law?  It is simply that the
289 program should always respond to the user in the way that
290 astonishes him least.
291
292 A program, no matter how complex, should act as a
293 single unit.  The program should be directed by the logic
294 within rather than by outward appearances.
295
296 If the program fails in these requirements, it will be
297 in a state of disorder and confusion.  The only way to correct
298 this is to rewrite the program.
299
300 -- Geoffrey James, "The Tao of Programming"
301 @end quotation
302
303
304 @unnumberedsubsec Languages
305
306 C++ and Python are preferred.  Perl is not.  Python code should use an
307 indent of 8, using TAB characters.
308
309 @unnumberedsubsec Filenames
310
311 Definitions of classes that are only accessed via pointers
312 (*) or references (&) shall not be included as include files.
313
314 filenames
315
316 @example 
317
318         ".hh"   Include files
319         ".cc"   Implementation files
320         ".icc"  Inline definition files
321         ".tcc"  non inline Template defs
322  
323 @end example 
324
325 in emacs:
326
327 @example 
328
329         (setq auto-mode-alist
330               (append '(("\\.make$" . makefile-mode)
331                         ("\\.cc$" . c++-mode)
332                         ("\\.icc$" . c++-mode)
333                         ("\\.tcc$" . c++-mode)
334                         ("\\.hh$" . c++-mode)
335                         ("\\.pod$" . text-mode)         
336                         )
337                       auto-mode-alist))
338  
339 @end example 
340
341
342 The class Class_name_abbreviation is coded in @file{class-name-abbr.*}
343
344 @unnumberedsubsec Indentation
345
346 Standard GNU coding style is used.   In emacs:
347
348 @example 
349
350         (add-hook 'c++-mode-hook
351                   '(lambda() (c-set-style "gnu")
352                      )
353                   )
354  
355 @end example 
356
357 If you like using font-lock, you can also add this to your @file{.emacs}:
358
359 @example 
360
361         (setq font-lock-maximum-decoration t)
362         (setq c++-font-lock-keywords-3 
363               (append
364                c++-font-lock-keywords-3
365                '(("\\b\\([a-zA-Z_]+_\\)\\b" 1 font-lock-variable-name-face)
366                ("\\b\\([A-Z]+[a-z_]+\\)\\b" 1 font-lock-type-face))
367                ))
368  
369 @end example 
370
371 @unnumberedsubsec Classes and Types
372
373 @example 
374
375         This_is_a_class
376  
377 @end example 
378
379 @unnumberedsubsec Members
380
381 @example 
382
383         Class::member ()
384         Type Class::member_type_
385         Type Class::member_type ()
386  
387 @end example 
388
389 the @code{type} is a Hungarian notation postfix for @code{Type}. See below
390
391 @unnumberedsubsec Macros
392
393 Macros should be written completely in uppercase
394
395 The code should not be compilable if proper macro declarations are not
396 included. 
397
398 Don't laugh. It took us a whole evening/night to figure out one of
399 these bugs, because we had a macro that looked like
400 @code{DECLARE_VIRTUAL_FUNCTIONS ()}. 
401
402 @unnumberedsubsec Broken code
403
404 Broken code (hardwired dependencies, hardwired constants, slow
405 algorithms and obvious limitations) should be marked as such: either
406 with a verbose TODO, or with a short "ugh" comment.
407
408 @unnumberedsubsec Comments
409
410 The source is commented in the DOC++ style.  Check out doc++ at
411 @uref{http://www.zib.de/Visual/software/doc++/index.html}
412
413 @example 
414
415         /*
416                 C style comments for multiline comments.
417                 They come before the thing to document.
418                 [...]
419         */
420
421         /**
422                 short description.
423                 Long class documentation.
424                 (Hungarian postfix)
425
426                 TODO Fix boring_member ()
427         */
428         class Class @{
429                 /**
430                   short description.
431                   long description
432                 */
433
434                 Data data_member_;
435
436                 /**
437                         short memo. long doco of member ()
438                         @@param description of arguments
439                         @@return Rettype
440                 */
441                 Rettype member (Argtype);
442
443                 /// memo only
444                 boring_member () @{
445                         data_member_ = 121; // ugh
446                 @}
447         @};
448  
449 @end example 
450
451         
452 Unfortunately most of the code isn't really documented that good.
453
454 @unnumberedsubsec Members (2)
455
456 Standard methods:
457
458 @example 
459
460         ///check that *this satisfies its invariants, abort if not.
461         void OK () const
462
463         /// print *this (and substructures) to debugging log
464         void print () const
465
466         /**
467         protected member. Usually invoked by non-virtual XXXX ()
468         */
469         virtual do_XXXX ()
470
471         /**add some data to *this.
472         Presence of these methods usually imply that it is not feasible to this
473         via  a constructor
474         */
475         add (..)
476
477         /// replace some data of *this
478         set (..)
479  
480 @end example 
481
482 @unnumberedsubsec Constructor
483
484 Every class should have a default constructor.  
485
486 Don't use non-default constructors if this can be avoided:
487
488 @example 
489
490         Foo f(1)
491  
492 @end example 
493
494 is less readable than
495
496 @example 
497
498         Foo f;
499         f.x = 1
500  
501 @end example 
502
503 or 
504
505 @example 
506
507         Foo f(Foo_convert::int_to_foo (1))
508  
509 @end example 
510
511 @unnumberedsec Hungarian notation naming convention
512
513 Proposed is a naming convention derived from the so-called
514 @emph{Hungarian Notation}.  Macros, @code{enum}s and @code{const}s are all
515 uppercase, with the parts of the names separated by underscores.
516
517 @unnumberedsubsec Types
518
519 @table @samp
520 @item @code{byte}
521     unsigned char. (The postfix _by is ambiguous)
522 @item @code{b}
523     bool
524 @item @code{bi}
525     bit
526 @item @code{ch}
527     char
528 @item @code{f}
529     float
530 @item @code{i}
531     signed integer
532 @item @code{str}
533     string class
534 @item @code{sz}
535     Zero terminated c string
536 @item @code{u}
537     unsigned integer
538 @end table
539
540 @unnumberedsubsec User defined types
541
542 @example 
543
544         /** Slur blah. blah.
545         (slur)
546         */
547         class Slur @{@};
548         Slur* slur_p = new Slur;
549  
550 @end example 
551
552 @unnumberedsubsec Modifiers
553
554 The following types modify the meaning of the prefix. 
555 These are preceded by the prefixes:
556
557 @table @samp
558 @item @code{a}
559     array
560 @item @code{array}
561     user built array.
562 @item @code{c}
563     const. Note that the proper order is @code{Type const}
564     i.s.o. @code{const Type}
565 @item @code{C}
566     A const pointer. This would be equivalent to @code{_c_l}, but since any
567     "const" pointer has to be a link (you can't delete a const pointer),
568     it is superfluous.
569 @item @code{l}
570     temporary pointer to object (link)
571 @item @code{p}
572     pointer to newed object
573 @item @code{r}
574     reference
575 @end table
576
577 @unnumberedsubsec Adjective
578
579 Adjectives such as global and static should be spelled out in full.
580 They come before the noun that they refer to, just as in normal english.
581
582 @example 
583
584 foo_global_i: a global variable of type int commonly called "foo".
585  
586 @end example 
587
588 static class members do not need the static_ prefix in the name (the
589 Class::var notation usually makes it clear that it is static)
590
591 @table @samp
592 @item @code{loop_i}
593     Variable loop: an integer
594 @item @code{u}
595     Temporary variable: an unsigned integer
596 @item @code{test_ch}
597     Variable test: a character
598 @item @code{first_name_str}
599     Variable first_name: a String class object
600 @item @code{last_name_ch_a}
601     Variable last_name: a @code{char} array
602 @item @code{foo_i_p}
603     Variable foo: an @code{Int*} that you must delete
604 @item @code{bar_i_l}
605     Variable bar: an @code{Int*} that you must not delete
606 @end table
607
608 Generally default arguments are taboo, except for nil pointers.
609
610 The naming convention can be quite conveniently memorised, by
611 expressing the type in english, and abbreviating it
612
613 @example 
614
615         static Array<int*> foo
616  
617 @end example 
618
619 @code{foo} can be described as "the static int-pointer user-array", so you get
620
621 @example 
622
623         foo_static_l_arr
624  
625 @end example 
626
627
628 @unnumberedsec Miscellaneous
629     
630
631 For some tasks, some scripts are supplied, notably creating patches, a
632 mirror of the website, generating the header to put over cc and hh
633 files, doing a release.
634
635 Use them.
636
637 @node Making patches,  , Breaking, Top
638
639
640 @unnumberedsec name
641     
642
643 PATCHES - track and distribute your code changes
644
645 This page documents how to distribute your changes to GNU lilypond
646     
647 We would like to have unified context diffs with full pathnames.  A
648 script automating supplied with Lily.
649
650  Distributing a change normally goes like this:
651
652 @itemize @bullet
653 @item make your fix/add your code 
654 @item Add changes to NEWS, and add yourself to Documentation/topdocs/AUTHORS.yo
655 @item generate a patch, 
656 @item e-mail your patch to one of the mailing lists
657     gnu-music-discuss@@gnu.org or bug-gnu-music@@gnu.org
658 @end itemize
659
660 Please do not send entire files, even if the patch is bigger than the
661 original.  A patch makes it clear what is changed, and it won't
662 overwrite previous not yet released changes.
663
664 @unnumberedsec Generating a patch
665     
666
667 In @file{VERSION}, set MY_PATCH_LEVEL:
668
669 @example 
670
671     VERSION:
672         ...
673         MY_PATCH_LEVEL=jcn1
674  
675 @end example 
676
677 In @file{CHANGES}, enter a summary of changes:
678
679 @example 
680         pl 0.1.73.jcn1
681                 - added PATCHES.texi
682 @end example 
683
684 Then, from the top of Lily's source tree, type
685
686 @example 
687
688     make dist
689     make diff
690  
691 @end example 
692
693 which rolls the tarball @file{../releases/lilypond-0.1.73.tar.gz}
694 and leaves your patch as @file{./lilypond-0.1.73.jcn1.diff}.
695 @footnote{'Make diff' generates a patch between two tarballs.  For 
696 more info type 'make diff help=='.}  We assume that there is a tarball 
697 @file{lilypond-0.1.73.tar.gz} in the directory @file{../releases}.
698
699 If you didn't configure Lily using --srcdir, you can do:
700
701 @example 
702
703     make release
704
705     tar-ball: ../patches/lilypond-0.1.73.jcn1.gz
706     patch: ../patches/lilypond-0.1.73.jcn1.gz
707 @end example 
708
709 @unnumberedsec Prerequisites
710     
711
712 For creating a patch you need
713
714 @itemize @bullet
715 @item All items mentioned in @file{INSTALL}.  You're not going to send a patch
716     that you haven't even built, right?
717 @item GNU diff
718 @example 
719     make distclean
720     cd ..
721     diff -urN lilypond-0.1.73 lilypond-0.1.73.jcn1 > lilypond-0.1.73.jcn1
722 @end example 
723
724 but there are handy python scripts available.  If you're doing development,
725 you'll need Python for other LilyPond scripts anyway.
726
727 @item The Lily directory structure, which looks like:
728
729     @example 
730
731     doos/                        # gnu/windows32 build and binary releases
732     harmonia -> harmonia-x.y.z 
733     harmonia-x.y.z/
734     lilypond -> lilypond-x.y.z   # symlink to development directory
735     lilypond-x.y.z/              # current development
736     patches/                     # patches between different releases
737     RedHat/BUILD                 # RedHat build and binary releases
738     RedHat/RPMS
739     RedHat/SPECS
740     releases/                    # .tar.gz releases
741     test/                        # tarballs and diffs from current version
742
743  
744 @end example 
745
746 with prefix @file{$HOME/usr/src}
747 and (for building rpms only) in @file{$HOME/.rpmrc}:
748 @example 
749
750     topdir: /home/fred/usr/src/RedHat
751  
752 @end example 
753
754 @end itemize
755         
756 @unnumberedsec Applying patches
757     
758
759 If you're following LilyPond development regularly, you probably want to
760 download just the patch for each subsequent release.
761 After downloading the patch (into the patches directory, of course), simply 
762 apply it:
763
764 @example 
765
766     gzip -dc ../patches/lilypond-0.1.74.diff.gz | patch -p1 -E
767  
768 @end example 
769
770 and don't forget to make automatically generated files:
771
772 @example 
773
774     autoconf footnote(patches don't include automatically generated files, 
775     i.e. file(configure) and files generated by file(configure).)
776
777     configure
778  
779 @end example 
780
781 @unnumberedsec Synchronise
782     
783
784 If you're not very quick with sending your patch, there's a good
785 chance that an new release of LilyPond comes available.  In such a
786 case, the maintainer will probably ask you to make a new patch against
787 the latest release.  Your best bet is to download the latest release,
788 and apply your patch against this new source tree:
789
790 @example 
791
792     cd lilypond-0.1.74
793     gzip -dc ../patches/lilypond-0.1.73.jcn1.diff.gz | patch -p1 -E
794     autoconf
795     configure
796  
797 @end example 
798
799 Then, make a patch as shown above.
800
801 @unnumberedsec See also
802     
803
804 @file{stepmake/INSTALL.txt}
805
806 @unnumberedsec maintainer
807     
808
809 @email{hanwen@@cs.uu.nl, Han-Wen Nienhuys}
810
811 Just keep on sending those patches!
812
813
814 @bye
815
816