Fix potential NULL dereference
[geany-mirror.git] / HACKING
blobdf7bd1f24d61150871262e62de1f8f59222ed574
1 =============
2 Hacking Geany
3 =============
4 .. contents::
6 General
7 =======
9 About this file
10 ---------------
11 This file contains information for anyone wanting to work on the Geany
12 codebase. You should be aware of the open source licenses used - see
13 the README file or the documentation. It is reStructuredText; the
14 source file is HACKING. You can generate hacking.html by running ``make
15 hacking-doc`` from the doc/ subdirectory.
17 Writing plugins
18 ---------------
19 * src/plugindata.h contains the plugin API data types.
20 * See plugins/demoplugin.c for a very basic example plugin.
21 * src/plugins.c loads and unloads plugins (you shouldn't need to read
22   this really).
23 * The API documentation contains a few basic guidelines and hints to
24   write plugins.
26 You should generate and read the plugin API documentation, see below.
28 Plugin API documentation
29 ^^^^^^^^^^^^^^^^^^^^^^^^
30 You can generate documentation for the plugin API using the doxygen
31 tool. Run ``make api-doc`` in the doc subdirectory. The documentation
32 will be output to doc/reference/index.html.
33 Alternatively you can view the API documentation online at
34 http://www.geany.org/manual/reference/.
36 Patches
37 -------
38 We are happy to receive patches, but it's best to check with us by email
39 or mailing list whether a new feature is appropriate, and whether someone
40 is already working on similar code.
42 In general it's best to provide git-formatted patches made from the
43 current Git (see `Committing`_)::
45     $ git commit -a
46     $ git format-patch HEAD^
48 We also accept patches against other releases, but it's more work for us.
50 If you're not using Git, although you're strongly suggested to used it,
51 you can use the diff command::
53     $ diff -u originalpath modifiedpath > new-feature.patch
55 However, such a patch won't contain the authoring information nor the
56 patch's description.
58 .. note::
59     Please make sure patches follow the style of existing code - In
60     particular, use tabs for indentation. See `Coding`_.
62 Windows tools
63 -------------
64 * Git: http://git-scm.com/ and http://code.google.com/p/msysgit/
65 * diff, grep, etc: http://mingw.org/ or http://unxutils.sourceforge.net/
67 See also the 'Building on Windows' document on the website.
69 File organization
70 -----------------
71 callbacks.c is just for Glade callbacks.
72 Avoid adding code to geany.h if it will fit better elsewhere.
73 See the top of each ``src/*.c`` file for a brief description of what
74 it's for.
76 Plugin API code
77 ---------------
78 Please be aware that anything with a doc-comment (a comment with an
79 extra asterix: ``/**``) is something in the plugin API. Things like
80 enums and structs can usually still be appended to, ensuring that all
81 the existing elements stay in place - this will keep the ABI stable.
83 .. warning::
85     Some structs like GeanyCallback cannot be appended to without
86     breaking the ABI because they are used to declare structs by
87     plugins, not just for accessing struct members through a pointer.
88     Normally structs should never be allocated by plugins.
90 Keeping the plugin ABI stable
91 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92 Before the 1.0 release series, the ABI can change when necessary, and
93 even the API can change. An ABI change just means that all plugins will
94 not load and they must be rebuilt. An API change means that some plugins
95 might not build correctly.
97 If you're reordering or changing existing elements of structs that are
98 used as part of the plugin API, you must increment GEANY_ABI_VERSION
99 in plugindata.h. This is usually not needed if you're just appending
100 fields to structs. The GEANY_API_VERSION value should be incremented
101 for any changes to the plugin API, including appending elements.
103 If you're in any doubt when making changes to plugin API code, just ask us.
105 Plugin API/ABI design
106 ^^^^^^^^^^^^^^^^^^^^^
107 You should not make plugins rely on the size of a struct. This means:
109 * Don't let plugins allocate any structs (stack or heap).
110 * Don't let plugins index any arrays of structs.
111 * Don't add any array fields to structs in case we want to change the
112   array size later.
114 Doc-comments
115 ^^^^^^^^^^^^
116 * The @file tag can go in the source .c file, but use the .h header name so
117   it appears normally in the generated documentation. See ui_utils.c for an
118   example.
119 * Function doc-comments should always go in the source file, not the
120   header, so they can be updated if/when the implementation changes.
122 Glade
123 -----
124 Use the code generation features of Glade instead of editing interface.c
125 or support.c. Glade 2.12 is required as later Glade versions do not
126 have the code generation features anymore. At some point we'll switch to
127 GtkBuilder, probably.
129 You can build Glade 2.12 and run the binary in place, without installing
130 it - this should work fine even if you have another version of Glade
131 installed on the system.
133 You can download Glade 2.12.2 here:
134 http://download.geany.org/glade-2.12.2.tar.gz
136 On recent GTK versions, you need a small patch to make it compile.
137 You can get the patch from:
138 http://download.geany.org/glade-2.12.2-build-fixes.patch
140 And then simply apply it like so::
142     $ /tmp/glade-2.12.2% patch -p1 < glade-2.12.2-build-fixes.patch
145 GTK versions & API documentation
146 --------------------------------
147 Geany requires GTK >= 2.16 and GLib >= 2.20. API symbols from newer
148 GTK/GLib versions should be avoided or made optional to keep the source
149 code building on older systems.
151 The official GTK 2.16 API documentation may not be available online
152 anymore, so we put it on http://www.geany.org/manual/gtk/. There
153 is also a tarball with all available files for download and use with
154 devhelp.
156 Using the 2.16 API documentation of the GTK libs (including GLib, GDK
157 and Pango) has the advantages that you don't get confused by any
158 newer API additions and you don't have to take care about whether
159 you can use them or not.
161 Coding
162 ------
163 * Don't write long functions with a lot of variables and/or scopes - break
164   them down into smaller static functions where possible. This makes code
165   much easier to read and maintain.
166 * Use GLib types and functions - gint not int, g_free() not free().
167 * Your code should build against GLib 2.20 and GTK 2.16. At least for the
168   moment, we want to keep the minimum requirement for GTK at 2.16 (of
169   course, you can use the GTK_CHECK_VERSION macro to protect code using
170   later versions).
171 * Variables should be declared before statements. You can use
172   gcc's -Wdeclaration-after-statement to warn about this.
173 * Don't let variable names shadow outer variables - use gcc's -Wshadow
174   option.
175 * Do not use G_LIKELY or G_UNLIKELY (except in critical loops). These
176   add noise to the code with little real benefit.
178 Compiler options & warnings
179 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
180 Use ``CFLAGS='-Wfoo' ./configure`` or ``CFLAGS='-Wfoo' ./autogen.sh``
181 to set warning options (as well as anything else e.g. -g -O2).
183 * Enable warnings - for gcc use '-Wall -W' (and optionally
184   -Wno-unused-parameter to avoid unused parameter warnings in Glade
185   callbacks).
186 * You should try to write ISO C90 code for portability, so always
187   use C ``/* */`` comments and function_name(void) instead of
188   function_name(). This is for compatibility with various Unix-like
189   compilers. You should use -ansi to help check this.
191 .. tip::
192     Remember for gcc you need to enable optimization to get certain
193     warnings like uninitialized variables, but for debugging it's
194     better to have no optimization on.
196 Style
197 ^^^^^
198 * We use a tab width of 4 and indent completely with tabs not spaces.
199   Note the documentation files use (4) spaces instead, so you may want
200   to use the 'Detect from file' indent pref.
201 * Do not add whitespace at the end of lines, this adds to commit noise.
202   When editing with Geany set preference files->Strip trailing spaces
203   and tabs.
204 * Use the multiline comment ``/* */`` to comment small blocks of code,
205   functions descriptions or longer explanations of code, etc. C++ single
206   line comments will cause portability issues. The more comments are in
207   your code the better. (See also ``scripts/fix-cxx-comments.pl`` in Git).
208 * Lines should not be longer than about 100 characters and after 100
209   characters the lines should be wrapped and indented once more to
210   show that the line is continued.
211 * We don't put spaces between function names and the opening brace for
212   argument lists.
213 * Variable declarations come first after an opening brace, then one
214   newline to separate declarations and code.
215 * 2-operand operators should have a space each side.
216 * Function bodies should have 2 blank newlines after them.
217 * Align braces together on separate lines.
218 * Don't put assignments in 'if/while/etc' expressions.
219 * if statements without brace bodies should have the code on a separate
220   line, then a blank line afterwards.
221 * Use braces after if/while statements if the body uses another
222   if/while statement.
223 * Try to fit in with the existing code style.
225 .. note::
226     A few of the above can be done with the Git
227     ``scripts/fix-alignment.pl``, but it is quite dumb and it's much better
228     to write it correctly in the first place.
229     ``scripts/rstrip-whitespace.py`` just removes trailing whitespace.
232 .. below tabs should be used, but spaces are required for reST.
234 Example::
236     struct Bar;
238     typedef struct Foo  /* struct names normally are the same as typedef names */
239     {
240         gint    foo;    /* names are somewhat aligned visually */
241         gint    bar;    /* fields don't share the same line */
242         SomeLongTypeName baz; /* alignment is not strict */
243         gchar   *ptr;   /* pointer symbol must go next to variable name, not type */
244         Bar     public; /**< only plugin API fields have a doc-comment */
245     }
246     Foo;
249     gint some_func(void);
251     gint some_other_func(void);
254     /* optional function comment explains something important */
255     gint function_long_name(gchar arg1, <too many args to fit on this line>,
256             gchar argN)
257     {
258         /* variable declarations always go before code in each scope */
259         /* variable names should NOT be aligned at all */
260         gint foo, bar;  /* variables can go on the same line */
261         gint baz;       /* but often don't */
262         gchar *ptr;     /* pointer symbol must go next to variable name, not type */
263         gchar *another; /* pointers should normally go on separate lines */
265         /* Some long comment block
266          * taking several different
267          * lines to explain */
268         if (foo)
269         {
270             /* variables only used in one scope should normally be declared there */
271             gint dir = -1;
273             bar = foo;
274             if ((bar & (guint)dir) != 7)
275                 some_code(arg1, <too many args to fit on this line>,
276                     argN - 1, argN);
278             some_func();
279         }
280     }
283     /** Explains using doc-comments for plugin API functions.
284      * First line should be short and use the third person tense - 'explains',
285      * not 'explain'.
286      *
287      * @return Some number.
288      * @since 1.22. */
289     gint another_function(void)
290     {
291         ...
294 Committing
295 ----------
297 * Commit one thing at a time, do small commits.  Commits should be
298   meaningful and not too big when possible; multiple small commits are
299   good if there is no good reason to group them.
300 * Use meaningful name and email in the Author and Committer fields.
301   This helps knowing who did what and allows to contact the author if
302   there is a good reason to do so (unlikely, but can happen).
303 * When working on a new feature, create a new branch for it.  When
304   merging it, use the --no-ff option to make sure a merge commit will
305   be created to better track what happened.  However, if the feature
306   only took one commit you might merge it fast-forward since there is
307   not history to keep together.
309 Commit messages
310 ^^^^^^^^^^^^^^^
311 Follow the standard Git formatting:
313 * No line should use more than about 80 characters (around 72 is best).
314 * The first line is the commit's summary and is followed by an empty
315   line.  This summary should be one line and one line only, thus less
316   than 80 characters.  This summary should not include any punctuation
317   unless really needed.  See it as the subject of an email: keep it
318   concise and as precise as you can, but not tool long.
319 * Following lines are optional detailed commit information, with
320   paragraphs separated by blank lines.  This part should be as long as
321   needed to describe the commit in depth, should use proper
322   punctuation and should include any useful information, like the
323   motivation for the patch and/or any valuable details the diff itself
324   don't provide or don't make clear.  Make it as complete as you think
325   it makes sense, but don't include an information that is better
326   explained by the commit's diff.
328   It is OK to use ASCII formatting like bullet list using "*" or "-",
329   etc. if useful, but emphasis (bold, italic, underline) should be
330   avoided.
332 Example::
334     Ask the user if spawn fails in utils_open_browser()
336     Ask the user to configure a valid browser command if spawning it
337     fails rather than falling back to some arbitrary hardcoded defaults.
339     This avoid spawning an unexpected browser when the configured one is
340     wrong, and gives the user a chance to correctly fix the preference.
343 Testing
344 -------
345 * Run with ``-v`` to print any debug messages.
346 * You can use a second instance (``geany -i``).
347 * To check first-run behaviour, use an alternate config directory by
348   passing ``-c some_dir`` (but make sure the directory is clean first).
349 * For debugging tips, see `GDB`_.
351 Bugs to watch out for
352 ---------------------
353 * Forgetting to check *doc->is_valid* when looping through
354   *documents_array* - instead use *foreach_document()*.
355 * Inserting fields into structs in the plugin API instead of appending.
356 * Not breaking the plugin ABI when necessary.
357 * Using an idle callback that doesn't check main_status.quitting.
358 * Forgetting CRLF line endings on Windows.
359 * Not handling Tabs & Spaces indent mode.
361 Libraries
362 ---------
363 We try to use an unmodified version of Scintilla - any new lexers or
364 other changes should be passed on to the maintainers at
365 http://scintilla.org. We normally update to a new Scintilla release
366 shortly after one is made. See also scintilla/README.
368 Tagmanager was originally taken from Anjuta 1.2.2, and parts of it
369 (notably c.c) have been merged from later versions of Anjuta and
370 CTags. The independent Tagmanager library itself ceased development
371 before Geany was started. It's source code parsing is mostly taken from
372 Exuberant CTags (see http://ctags.sf.net). If appropriate it's good to
373 pass language parser changes back to the CTags project.
376 Notes
377 =====
378 Some of these notes below are brief (or maybe incomplete) - please
379 contact the geany-devel mailing list for more information.
381 Using pre-defined autotools values
382 ----------------------------------
383 When you are use macros supplied by the autotools like GEANY_PREFIX,
384 GEANY_LIBDIR, GEANY_DATADIR and GEANY_LOCALEDIR be aware that these
385 might not be static strings when Geany is configured with
386 --enable-binreloc. Then these macros will be replaced by function calls
387 (in src/prefix.h). So, don't use anything like
388 printf("Prefix: " GEANY_PREFIX); but instead use
389 printf("Prefix: %s", GEANY_PREFIX);
391 Adding a source file foo.[hc] in src/ or plugins/
392 -------------------------------------------------
393 * Add foo.c, foo.h to SRCS in path/Makefile.am.
394 * Add foo.o to OBJS in path/makefile.win32.
395 * Add path/foo.c to geany_sources in wscript.
396 * Add path/foo.c to po/POTFILES.in (for string translation).
398 Adding a filetype
399 -----------------
400 You can add a filetype without syntax highlighting or tag parsing, but
401 check to see if those features have been written in other projects first.
403 * Add GEANY_FILETYPES_FOO to filetypes.h.
404 * Initialize GEANY_FILETYPES_FOO in init_builtin_filetypes() of
405   filetypes.c. You should use filetype_make_title() to avoid a
406   translation whenever possible.
407 * Update data/filetype_extensions.conf.
409 filetypes.* configuration file
410 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
411 All languages need a data/filetypes.foo configuration file. See
412 the "Filetype definition files" section in the manual and/or
413 data/filetypes.c for an example.
415 Programming languages should have:
417 * [keywords] if the lexer supports it.
418 * [settings] mostly for comment settings.
419 * [build_settings] for commands to run.
421 For languages with a Scintilla lexer, there should be a [styling] section,
422 to correspond to the styles used in styleset_foo() in highlighting.c -
423 see below.
425 Syntax highlighting
426 ^^^^^^^^^^^^^^^^^^^
427 It may be possible to use an existing Scintilla lexer in the scintilla/
428 subdirectory - if not, you will need to find (or write) one,
429 LexFoo.cxx. Try the official Scintilla project first.
431 .. warning::
432     We won't accept adding a lexer that conflicts with one in
433     Scintilla. All new lexers should be submitted back to the Scintilla
434     project to save duplication of work.
436 When adding a lexer, update:
438 * scintilla/Makefile.am
439 * scintilla/makefile.win32
440 * wscript
441 * scintilla/KeyWords.cxx - add a LINK_LEXER command *manually*
443 For syntax highlighting, you will need to edit highlighting.c and add
444 the following things:
446 1. Write styleset_foo_init() to setup lexer styles and load style
447    settings from the filetypes.foo configuration file. You should probably
448    start by copying and adapting another filetype's initialization, such
449    as styleset_tcl_init(). You may want to use load_style_entries().
450 2. Write styleset_foo() to apply styles when a new scintilla widget
451    is created. Again you could copy and adapt a function like
452    styleset_tcl(). You may want to use apply_style_entries().
453 3. In highlighting_init_styles(), add
454    ``init_styleset_case(GEANY_FILETYPES_FOO, styleset_foo_init);``.
455 4. In highlighting_set_styles(), add
456    ``styleset_case(GEANY_FILETYPES_FOO, styleset_foo);``.
457 5. Write data/filetypes.foo configuration file [styling] section. See
458    the manual and see data/filetypes.d for a named style example.
460 .. note::
461     Please try to make your styles fit in with the other filetypes'
462     default colors, and to use named styles where possible (e.g.
463     "commentline=comment"). Filetypes that share a lexer should have
464     the same colors. If not using named styles, leave the background color
465     empty to match the default color.
467 Error message parsing
468 ^^^^^^^^^^^^^^^^^^^^^
469 New-style error message parsing is done with an extended GNU-style regex
470 stored in the filetypes.foo file - see the [build_settings] information
471 in the manual for details.
473 Old-style error message parsing is done in
474 msgwin_parse_compiler_error_line() of msgwindow.c - see the ParseData
475 typedef for more information.
477 Other features
478 ^^^^^^^^^^^^^^
479 If the lexer has comment styles, you should add them in
480 highlighting_is_comment_style(). You should also update
481 highlighting_is_string_style() for string/character styles. For now,
482 this prevents calltips and autocompletion when typing in a comment
483 (but it can still be forced by the user).
485 For brace indentation, update lexer_has_braces() in editor.c;
486 indentation after ':' is done from on_new_line_added().
488 If the Scintilla lexer supports user type keyword highlighting (e.g.
489 SCLEX_CPP), update document_update_type_keywords() in document.c.
491 Adding a TagManager parser
492 ^^^^^^^^^^^^^^^^^^^^^^^^^^
493 This assumes the filetype for Geany already exists.
495 First write or find a CTags compatible parser, foo.c. Note that there
496 are some language patches for CTags at:
497 http://sf.net/projects/ctags - see the tracker.
499 (You can also try the Anjuta project's tagmanager codebase.)
501 .. note::
502     From Geany 1.22 GLib's GRegex engine is used instead of POSIX
503     regex, unlike CTags. It should be close enough to POSIX to work
504     OK.
505     We no longer support regex parsers with the "b" regex flag
506     option set and Geany will print debug warnings if it's used.
507     CTags supports it but doesn't currently (2011) include any
508     parsers that use it. It should be easy to convert to extended
509     regex syntax anyway.
511 Method
512 ``````
513 * Add foo.c to SRCS in Makefile.am.
514 * Add foo.o to OBJS in makefile.win32.
515 * Add path/foo.c to geany_sources in wscript.
516 * Add Foo to parsers.h & fill in comment with parser number for foo.
518 In foo.c:
519 Edit FooKinds 3rd column to match a s_tag_type_names string in tm_tag.c.
520 (You may want to make the symbols.c change before doing this).
522 In filetypes.c, init_builtin_filetypes():
523 Set filetypes[GEANY_FILETYPES_FOO].lang = foo's parser number.
525 In symbols.c:
526 Unless your parser uses C-like tag type kinds, update
527 add_top_level_items() for foo, calling tag_list_add_groups(). See
528 get_tag_type_iter() for which tv_iters fields to use.
534 Stop on warnings
535 ^^^^^^^^^^^^^^^^
536 When a GLib or GTK warning is printed, often you want to get a
537 backtrace to find out what code caused them. You can do that with the
538 ``--g-fatal-warnings`` argument, which will abort Geany on the first
539 warning it receives.
541 But for ordinary testing, you don't always want your editor to abort
542 just because of a warning - use::
544     (gdb) b handler_log if level <= G_LOG_LEVEL_WARNING
547 Running with batch commands
548 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
549 Use::
551     $ gdb src/geany -x gdb-commands
553 Where ``gdb-commands`` is a file with the following lines::
555     set pagination off
556     b handler_log if level <= G_LOG_LEVEL_WARNING
557     r -v
560 Loading a plugin
561 ^^^^^^^^^^^^^^^^
562 This is useful so you can load plugins without installing them first.
563 Alternatively you can use a symlink in ~/.config/geany/plugins or
564 $prefix/lib/geany (where $prefix is /usr/local by default).
566 The gdb session below was run from the toplevel Geany source directory.
567 Start normally with e.g. "gdb src/geany".
568 Type 'r' to run.
569 Press Ctrl-C from the gdb window to interrupt program execution.
571 Example::
573     Program received signal SIGINT, Interrupt.
574     0x00d16402 in __kernel_vsyscall ()
575     (gdb) call plugin_new("./plugins/.libs/demoplugin.so")
576     ** INFO: Loaded:   ./plugins/.libs/demoplugin.so (Demo)
577     $1 = (Plugin *) 0x905a890
578     (gdb) c
579     Continuing.
581     Program received signal SIGINT, Interrupt.
582     0x00d16402 in __kernel_vsyscall ()
583     (gdb) call plugin_free(0x905a890)
584     ** INFO: Unloaded: ./plugins/.libs/demoplugin.so
585     (gdb) c
586     Continuing.
588 Building Plugins
589 ^^^^^^^^^^^^^^^^
591 The geany-plugins autotools script automatically detects the
592 installed system Geany and builds the plugins against that.
594 To use plugins with a development version of Geany built with
595 a different prefix, the plugins will need to be compiled against
596 that version if the ABI has changed.
598 To do this you need to specify both --prefix and --with-geany-libdir
599 to the plugin configure.  Normally the plugin prefix is the
600 same as the Geany prefix to keep plugins with the version of Geany
601 that they are compiled against, and with-geany-libdir is the Geany
602 prefix/lib.
604 Whilst it is possible for the plugin prefix to be different to
605 the prefix of the libdir (which is why there are two settings),
606 it is probably better to keep the version of Geany and its plugins
607 together.