| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes style guidelines for LilyPond source code.
| 10.5.1 Languages | ||
| 10.5.2 Filenames | ||
| 10.5.3 Indentation | ||
| 10.5.4 Naming Conventions | ||
| 10.5.5 Broken code | ||
| 10.5.6 Code comments | ||
| 10.5.7 Handling errors | ||
| 10.5.8 Localization |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
C++ and Python are preferred. Python code should use PEP 8.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Definitions of classes that are only accessed via pointers (*) or references (&) shall not be included as include files.
filenames
".hh" Include files
".cc" Implementation files
".icc" Inline definition files
".tcc" non inline Template defs
in emacs:
(setq auto-mode-alist
(append '(("\\.make$" . makefile-mode)
("\\.cc$" . c++-mode)
("\\.icc$" . c++-mode)
("\\.tcc$" . c++-mode)
("\\.hh$" . c++-mode)
("\\.pod$" . text-mode)
)
auto-mode-alist))
The class Class_name is coded in ‘class-name.*’
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Standard GNU coding style is used.
fixcc.py (recommended)LilyPond provides a python script that will adjust the indentation
and spacing on a .cc or .hh file to very near the
GNU standard:
scripts/auxiliar/fixcc.py FILENAME |
This can be run on all files at once, but this is not recommended for normal contributors or developers.
scripts/auxiliar/fixcc.py \ $(find flower lily -name '*cc' -o -name '*hh' | grep -v /out) |
The following hooks will produce indentation which is similar to
our official indentation as produced with fixcc.py.
(add-hook 'c++-mode-hook
'(lambda ()
(c-set-style "gnu")
(setq indent-tabs-mode nil))
|
If you like using font-lock, you can also add this to your ‘.emacs’:
(setq font-lock-maximum-decoration t)
(setq c++-font-lock-keywords-3
(append
c++-font-lock-keywords-3
'(("\\b\\(a-zA-Z_?+_\\)\\b" 1 font-lock-variable-name-face) ("\\b\\(A-Z?+a-z_?+\\)\\b" 1 font-lock-type-face))
))
|
Although emacs indentation is the GNU standard, correct indentation for C++ files can be achieved by using the settings recommended in the GNU GCC Wiki. Save the following in ‘~/.vim/after/ftplugin/cpp.vim’:
setlocal cindent
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal textwidth=79
setlocal fo-=ro fo+=cql
" use spaces instead of tabs
setlocal expandtab
" remove trailing whitespace on write
autocmd BufWritePre * :%s/\s\+$//e
|
With these settings, files can be reindented automatically by
highlighting the lines to be indented in visual mode (use V to
enter visual mode) and pressing =, or a single line
correctly indented in normal mode by pressing ==.
A ‘scheme.vim’ file will help improve the indentation of Scheme code. This one was suggested by Patrick McCarty. It should be saved in ‘~/.vim/after/syntax/scheme.vim’.
" Additional Guile-specific 'forms' syn keyword schemeSyntax define-public define*-public syn keyword schemeSyntax define* lambda* let-keywords* syn keyword schemeSyntax defmacro defmacro* define-macro syn keyword schemeSyntax defmacro-public defmacro*-public syn keyword schemeSyntax use-modules define-module syn keyword schemeSyntax define-method define-class " Additional LilyPond-specific 'forms' syn keyword schemeSyntax define-markup-command define-markup-list-command syn keyword schemeSyntax define-safe-public define-music-function syn keyword schemeSyntax def-grace-function " All of the above should influence indenting too setlocal lw+=define-public,define*-public setlocal lw+=define*,lambda*,let-keywords* setlocal lw+=defmacro,defmacro*,define-macro setlocal lw+=defmacro-public,defmacro*-public setlocal lw+=use-modules,define-module setlocal lw+=define-method,define-class setlocal lw+=define-markup-command,define-markup-list-command setlocal lw+=define-safe-public,define-music-function setlocal lw+=def-grace-function " These forms should not influence indenting setlocal lw-=if setlocal lw-=set! " Try to highlight all ly: procedures syn match schemeFunc "ly:[^) ]\+" |
For documentation work on texinfo files, identify the file extensions used as texinfo files in your ‘.vim/filetype.vim’:
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
au! BufRead,BufNewFile *.itely setfiletype texinfo
au! BufRead,BufNewFile *.itexi setfiletype texinfo
au! BufRead,BufNewFile *.tely setfiletype texinfo
augroup END
|
and add these settings in ‘.vim/after/ftplugin/texinfo.vim’:
setlocal expandtab setlocal shiftwidth=2 setlocal textwidth=66 |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Naming conventions have been established for LilyPond source code.
Classes begin with an uppercase letter, and words
in class names are separated with _:
This_is_a_class
Member variable names end with an underscore:
Type Class::member_
Macro names should be written in uppercase completely,
with words separated by _:
THIS_IS_A_MACRO
Variable names should be complete words, rather than abbreviations.
For example, it is preferred to use thickness rather than
th or t.
Multi-word variable names in C++ should have the words separated by the underscore character (‘_’):
cxx_multiword_variable
Multi-word variable names in Scheme should have the words separated by a hyphen (‘-’):
scheme-multiword-variable
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Do not write broken code. This includes hardwired dependencies, hardwired constants, slow algorithms and obvious limitations. If you can not avoid it, mark the place clearly, and add a comment explaining shortcomings of the code.
Ideally, the comment marking the shortcoming would include TODO, so that it is marked for future fixing.
We reject broken-in-advance on principle.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Comments may not be needed if descriptive variable names are used in the code and the logic is straightforward. However, if the logic is difficult to follow, and particularly if non-obvious code has been included to resolve a bug, a comment describing the logic and/or the need for the non-obvious code should be included.
There are instances where the current code could be commented better. If significant time is required to understand the code as part of preparing a patch, it would be wise to add comments reflecting your understanding to make future work easier.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
As a general rule, you should always try to continue computations, even if there is some kind of error. When the program stops, it is often very hard for a user to pinpoint what part of the input causes an error. Finding the culprit is much easier if there is some viewable output.
So functions and methods do not return errorcodes, they never crash, but report a programming_error and try to carry on.
Error and warning messages need to be localized.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document provides some guidelines to help programmers write proper user messages. To help translations, user messages must follow uniform conventions. Follow these rules when coding for LilyPond. Hopefully, this can be replaced by general GNU guidelines in the future. Even better would be to have an English (en_GB, en_US) guide helping programmers writing consistent messages for all GNU programs.
Non-preferred messages are marked with ‘+’. By convention, ungrammatical examples are marked with ‘*’. However, such ungrammatical examples may still be preferred.
warning (_ ("need music in a score"));
error (_f ("cannot open file: `%s'", file_name));
|
In some rare cases you may need to call ‘gettext ()’ by hand. This happens when you pre-define (a list of) string constants for later use. In that case, you’ll probably also need to mark these string constants for translation, using ‘_i (STRING)’. The ‘_i’ macro is a no-op, it only serves as a marker for ‘xgettext’.
char const* messages[] = {
_i ("enable debugging output"),
_i ("ignore lilypond version"),
0
};
void
foo (int i)
{
puts (gettext (messages i));
}
|
See also ‘flower/getopt-long.cc’ and ‘lily/main.cc’.
message ("Calculating line breaks..." + " ");
|
foo.ly: 12: not a duration: 3 |
Messages containing a final verb, or a gerund (‘-ing’-form) always start with a capital. Other (simpler) messages start with a lowercase letter
Processing foo.ly... `foo': not declared. Not declaring: `foo'. |
_f ("cannot open file: `%s'", name_str)
_f ("cannot find character number: %d", i)
|
stem at + moment.str () + does not fit in beam |
have
_f ("stem at %s does not fit in beam", moment.str ())
|
warning (_f ("out of tune! Can't find: `%s'", "Key_engraver"));
warning (_f ("cannot find font `%s', loading default", font_name));
|
rather say:
warning (_ ("out of tune:"));
warning (_f ("cannot find: `%s', "Key_engraver"));
warning (_f ("cannot find font: `%s', font_name));
warning (_f ("Loading default font"));
|
_f ("Non-matching braces in text `%s', adding braces", text)
_ ("Debug output disabled. Compiled with NPRINT.")
_f ("Huh? Not a Request: `%s'. Ignoring.", request)
|
en: cannot open: `foo.ly' + nl: kan `foo.ly' niet openen (1) kan niet openen: `foo.ly'* (2) niet te openen: `foo.ly'* (3) |
The first nl message, although grammatically and stylistically correct, is not friendly for parsing by humans (even if they speak dutch). I guess we would prefer something like (2) or (3).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by gub on July 13, 2020 using texi2html 1.82.