Allow editing of formerly hidden preferences in the preferences dialog
[geany-mirror.git] / HACKING
blobcf1b0605bbe8d9092584cf0e086d2785230d1c40
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 work from the current SVN, but we accept patches
43 against other releases::
45     $ svn diff > fix-some-bug.patch
47 If you're not using SVN, you can use the diff command::
49     $ diff -u originalpath modifiedpath > new-feature.patch
51 .. note::
52     Please make sure patches follow the style of existing code - In
53     particular, use tabs for indentation. See `Coding`_.
55 Windows tools
56 -------------
57 * Subversion (SVN): http://subversion.tigris.org/
58 * diff, grep, etc: http://mingw.org/ or http://unxutils.sourceforge.net/
60 See also the 'Building on Windows' document on the website.
62 File organization
63 -----------------
64 callbacks.c is just for Glade callbacks.
65 Avoid adding code to geany.h if it will fit better elsewhere.
66 See the top of each ``src/*.c`` file for a brief description of what
67 it's for.
69 Plugin API code
70 ---------------
71 Please be aware that anything with a doc-comment (a comment with an
72 extra asterix: ``/**``) is something in the plugin API. Things like
73 enums and structs can usually still be appended to, ensuring that all
74 the existing elements stay in place - this will keep the ABI stable.
76 .. warning::
78     Some structs like GeanyCallback cannot be appended to without
79     breaking the ABI because they are used to declare structs by
80     plugins, not just for accessing struct members through a pointer.
81     Normally structs should never be allocated by plugins.
83 Keeping the plugin ABI stable
84 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85 Before the 1.0 release series, the ABI can change when necessary, and
86 even the API can change. An ABI change just means that all plugins will
87 not load and they must be rebuilt. An API change means that some plugins
88 might not build correctly.
90 If you're reordering or changing existing elements of structs that are
91 used as part of the plugin API, you must increment GEANY_ABI_VERSION
92 in plugindata.h. This is usually not needed if you're just appending
93 fields to structs. The GEANY_API_VERSION value should be incremented
94 for any changes to the plugin API, including appending elements.
96 If you're in any doubt when making changes to plugin API code, just ask us.
98 Plugin API/ABI design
99 ^^^^^^^^^^^^^^^^^^^^^
100 You should not make plugins rely on the size of a struct. This means:
102 * Don't let plugins allocate any structs (stack or heap).
103 * Don't let plugins index any arrays of structs.
104 * Don't add any array fields to structs in case we want to change the
105   array size later.
107 Doc-comments
108 ^^^^^^^^^^^^
109 * The @file tag can go in the source .c file, but use the .h header name so
110   it appears normally in the generated documentation. See ui_utils.c for an
111   example.
112 * Function doc-comments should always go in the source file, not the
113   header, so they can be updated if/when the implementation changes.
115 Glade
116 -----
117 Use the code generation features of Glade instead of editing interface.c
118 or support.c. Glade 2.12 is required as later Glade versions do not
119 have the code generation features anymore. At some point we'll switch to
120 GtkBuilder, probably.
122 You can build Glade 2.12 and run the binary in place, without installing
123 it - this should work fine even if you have another version of Glade
124 installed on the system.
126 You can download Glade 2.12.2 here:
127 http://download.geany.org/glade-2.12.2.tar.gz
129 On recent GTK versions, you need a small patch to make it compile.
130 You can get the patch from:
131 http://download.geany.org/glade-2.12.2-build-fixes.patch
133 And then simply apply it like so::
135     $ /tmp/glade-2.12.2% patch -p1 < glade-2.12.2-build-fixes.patch
138 GTK API documentation
139 ---------------------
140 The official GTK 2.8 API documentation is not available online anymore,
141 so we put them on http://www.geany.org/manual/gtk/.
142 There is also a tarball with all available files for download and use
143 with devhelp.
145 Using the 2.8 API documentation of the GTK libs (including GLib, GDK and
146 Pango) has the advantages that you don't get confused by any newer API
147 additions and you don't have to take care about whether you can use
148 them or not.
149 This is because Geany depends on GTK 2.8. API symbols from newer
150 GTK/GLib versions should be avoided to keep the source code building
151 against GTK 2.8.
153 Coding
154 ------
155 * Don't write long functions with a lot of variables and/or scopes - break
156   them down into smaller static functions where possible. This makes code
157   much easier to read and maintain.
158 * Use GLib types and functions - gint not int, g_free() not free().
159 * Your code should build against GLib 2.16 and GTK 2.12. At least for the
160   moment, we want to keep the minimum requirement for GTK at 2.12 (of
161   course, you can use the GTK_CHECK_VERSION macro to protect code using
162   later versions).
163 * Variables should be declared before statements. You can use
164   gcc's -Wdeclaration-after-statement to warn about this.
165 * Don't let variable names shadow outer variables - use gcc's -Wshadow
166   option.
168 Compiler options & warnings
169 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
170 Use ``CFLAGS='-Wfoo' ./configure`` or ``CFLAGS='-Wfoo' ./autogen.sh``
171 to set warning options (as well as anything else e.g. -g -O2).
173 * Enable warnings - for gcc use '-Wall -W' (and optionally
174   -Wno-unused-parameter to avoid unused parameter warnings in Glade
175   callbacks).
176 * You should try to write ISO C90 code for portability, so always
177   use C ``/* */`` comments and function_name(void) instead of
178   function_name(). This is for compatibility with various Unix-like
179   compilers. You should use -ansi to help check this.
181 .. tip::
182     Remember for gcc you need to enable optimization to get certain
183     warnings like uninitialized variables, but for debugging it's
184     better to have no optimization on.
186 Style
187 ^^^^^
188 * We use a tab width of 4 and indent completely with tabs not spaces.
189   Note the documentation files use (4) spaces instead, so you may want
190   to use the 'Detect from file' indent pref.
191 * Use the multiline comment ``/* */`` to comment small blocks of code,
192   functions descriptions or longer explanations of code, etc. C++ single
193   line comments will cause portability issues. The more comments are in
194   your code the better. (See also ``scripts/fix-cxx-comments.pl`` in SVN).
195 * Lines should not be longer than about 100 characters and after 100
196   characters the lines should be wrapped and indented once more to
197   show that the line is continued.
198 * We don't put spaces between function names and the opening brace for
199   argument lists.
200 * Variable declarations come first after an opening brace, then one
201   newline to separate declarations and code.
202 * 2-operand operators should have a space each side.
203 * Function bodies should have 2 blank newlines after them.
204 * Align braces together on separate lines.
205 * Don't put assignments in 'if/while/etc' expressions.
206 * if statements without brace bodies should have the code on a separate
207   line, then a blank line afterwards.
208 * Use braces after if/while statements if the body uses another
209   if/while statement.
210 * Try to fit in with the existing code style.
212 .. note::
213     A few of the above can be done with the SVN
214     ``scripts/fix-alignment.pl``, but it is quite dumb and it's much better
215     to write it correctly in the first place.
217 .. below tabs should be used, but spaces are required for reST.
219 Example::
221     gint some_func(void);
224     gint function_long_name(gchar arg1, <too many args to fit on this line>,
225             gchar argN)
226     {
227         /* variable declarations go before code in each scope */
228         gint foo, bar;  /* variables can go on the same line */
229         gchar *ptr;     /* pointer symbol must go next to variable name, not type */
230         gchar *another; /* pointers should normally go on separate lines */
232         /* Some long comment block
233          * taking several different
234          * lines to explain */
235         if (foo)
236         {
237             gint dir = -1;    /* -1 to search backwards */
239             bar = foo;
240             if ((bar & (guint)dir) != 7)
241                 some_code(arg1, <too many args to fit on this line>,
242                     argN - 1, argN);
244             some_func();
245         }
246     }
249     gint another_function(void)
250     {
251         ...
254 Testing
255 -------
256 * Run with ``-v`` to print any debug messages.
257 * You can use a second instance (``geany -i``).
258 * To check first-run behaviour, use an alternate config directory by
259   passing ``-c some_dir`` (but make sure the directory is clean first).
260 * For debugging tips, see `GDB`_.
262 Bugs to watch out for
263 ---------------------
264 * Forgetting to check *doc->is_valid* when looping through
265   *documents_array* - instead use *foreach_document()*.
266 * Inserting fields into structs in the plugin API instead of appending.
267 * Not breaking the plugin ABI when necessary.
268 * Using an idle callback that doesn't check main_status.quitting.
269 * Forgetting CRLF line endings on Windows.
270 * Not handling Tabs & Spaces indent mode.
272 Libraries
273 ---------
274 We try to use an unmodified version of Scintilla - any new lexers or
275 other changes should be passed on to the maintainers at
276 http://scintilla.org. We normally update to a new Scintilla release
277 shortly after one is made. See also scintilla/README.
279 Tagmanager was originally taken from Anjuta 1.2.2, and parts of it
280 (notably c.c) have been merged from later versions of Anjuta and
281 CTags. The independent Tagmanager library itself ceased development
282 before Geany was started. It's source code parsing is mostly taken from
283 Exuberant CTags (see http://ctags.sf.net). If appropriate it's good to
284 pass language parser changes back to the CTags project.
287 Notes
288 =====
289 Some of these notes below are brief (or maybe incomplete) - please
290 contact the geany-devel mailing list for more information.
292 Using pre-defined autotools values
293 ----------------------------------
294 When you are use macros supplied by the autotools like GEANY_PREFIX,
295 GEANY_LIBDIR, GEANY_DATADIR and GEANY_LOCALEDIR be aware that these
296 might not be static strings when Geany is configured with
297 --enable-binreloc. Then these macros will be replaced by function calls
298 (in src/prefix.h). So, don't use anything like
299 printf("Prefix: " GEANY_PREFIX); but instead use
300 printf("Prefix: %s", GEANY_PREFIX);
302 Adding a source file foo.[hc] in src/ or plugins/
303 -------------------------------------------------
304 * Add foo.c, foo.h to SRCS in path/Makefile.am.
305 * Add foo.o to OBJS in path/makefile.win32.
306 * Add path/foo.c to geany_sources in wscript.
307 * Add path/foo.c to po/POTFILES.in (for string translation).
309 Adding a filetype
310 -----------------
311 You can add a filetype without syntax highlighting or tag parsing, but
312 check to see if those features have been written in other projects first.
314 * Add GEANY_FILETYPES_FOO to filetypes.h.
315 * Initialize GEANY_FILETYPES_FOO in init_builtin_filetypes() of
316   filetypes.c. You should use filetype_make_title() to avoid a
317   translation whenever possible.
318 * Update data/filetype_extensions.conf.
320 filetypes.* configuration file
321 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322 All languages need a data/filetypes.foo configuration file. See
323 the "Filetype definition files" section in the manual and/or
324 data/filetypes.c for an example.
326 Programming languages should have:
328 * [keywords] if the lexer supports it.
329 * [settings] mostly for comment settings.
330 * [build_settings] for commands to run.
332 For languages with a Scintilla lexer, there should be a [styling] section,
333 to correspond to the styles used in styleset_foo() in highlighting.c -
334 see below.
336 Syntax highlighting
337 ^^^^^^^^^^^^^^^^^^^
338 It may be possible to use an existing Scintilla lexer in the scintilla/
339 subdirectory - if not, you will need to find (or write) one,
340 LexFoo.cxx. Try the official Scintilla project first.
342 .. warning::
343     We won't accept adding a lexer that conflicts with one in
344     Scintilla. All new lexers should be submitted back to the Scintilla
345     project to save duplication of work.
347 When adding a lexer, update:
349 * scintilla/Makefile.am
350 * scintilla/makefile.win32
351 * wscript
352 * scintilla/KeyWords.cxx - add a LINK_LEXER command *manually*
354 For syntax highlighting, you will need to edit highlighting.c and add
355 the following things:
357 1. Write styleset_foo_init() to setup lexer styles and load style
358    settings from the filetypes.foo configuration file. You should probably
359    start by copying and adapting another filetype's initialization, such
360    as styleset_tcl_init(). You may want to use load_style_entries().
361 2. Write styleset_foo() to apply styles when a new scintilla widget
362    is created. Again you could copy and adapt a function like
363    styleset_tcl(). You may want to use apply_style_entries().
364 3. In highlighting_init_styles(), add
365    ``init_styleset_case(GEANY_FILETYPES_FOO, styleset_foo_init);``.
366 4. In highlighting_set_styles(), add
367    ``styleset_case(GEANY_FILETYPES_FOO, styleset_foo);``.
368 5. Write data/filetypes.foo configuration file [styling] section. See
369    the manual and see data/filetypes.d for a named style example.
371 .. note::
372     Please try to make your styles fit in with the other filetypes'
373     default colors, and to use named styles where possible (e.g.
374     "commentline=comment"). Filetypes that share a lexer should have
375     the same colors. If not using named styles, leave the background color
376     empty to match the default color.
378 Error message parsing
379 ^^^^^^^^^^^^^^^^^^^^^
380 New-style error message parsing is done with an extended GNU-style regex
381 stored in the filetypes.foo file - see the [build_settings] information
382 in the manual for details.
384 Old-style error message parsing is done in
385 msgwin_parse_compiler_error_line() of msgwindow.c - see the ParseData
386 typedef for more information.
388 Other features
389 ^^^^^^^^^^^^^^
390 If the lexer has comment styles, you should add them in
391 highlighting_is_comment_style(). You should also update
392 highlighting_is_string_style() for string/character styles. For now,
393 this prevents calltips and autocompletion when typing in a comment
394 (but it can still be forced by the user).
396 For brace indentation, update lexer_has_braces() in editor.c;
397 indentation after ':' is done from on_new_line_added().
399 If the Scintilla lexer supports user type keyword highlighting (e.g.
400 SCLEX_CPP), update editor_lexer_get_type_keyword_idx() in editor.c.
402 Adding a TagManager parser
403 ^^^^^^^^^^^^^^^^^^^^^^^^^^
404 This assumes the filetype for Geany already exists.
406 First write or find a CTags compatible parser, foo.c. Note that there
407 are some language patches for CTags at:
408 http://sf.net/projects/ctags - see the tracker.
410 (You can also try the Anjuta project's tagmanager codebase.)
412 * Add foo.c to SRCS in Makefile.am.
413 * Add foo.o to OBJS in makefile.win32.
414 * Add path/foo.c to geany_sources in wscript.
415 * Add Foo to parsers.h & fill in comment with parser number for foo.
417 In foo.c:
418 Edit FooKinds 3rd column to match a s_tag_type_names string in tm_tag.c.
419 (You may want to make the symbols.c change before doing this).
421 In filetypes.c, init_builtin_filetypes():
422 Set filetypes[GEANY_FILETYPES_FOO].lang = foo's parser number.
424 In symbols.c:
425 Unless your parser uses C-like tag type kinds, update
426 add_top_level_items() for foo, calling tag_list_add_groups(). See
427 get_tag_type_iter() for which tv_iters fields to use.
433 Stop on warnings
434 ^^^^^^^^^^^^^^^^
435 When a GLib or GTK warning is printed, often you want to get a
436 backtrace to find out what code caused them. You can do that with the
437 ``--g-fatal-warnings`` argument, which will abort Geany on the first
438 warning it receives.
440 But for ordinary testing, you don't always want your editor to abort
441 just because of a warning - use::
443     (gdb) b handler_log if level <= G_LOG_LEVEL_WARNING
446 Running with batch commands
447 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
448 Use::
450     $ gdb src/geany -x gdb-commands
452 Where ``gdb-commands`` is a file with the following lines::
454     set pagination off
455     b handler_log if level <= G_LOG_LEVEL_WARNING
456     r -v
459 Loading a plugin
460 ^^^^^^^^^^^^^^^^
461 This is useful so you can load plugins without installing them first.
462 Alternatively you can use a symlink in ~/.config/geany/plugins or
463 $prefix/lib/geany (where $prefix is /usr/local by default).
465 The gdb session below was run from the toplevel Geany source directory.
466 Start normally with e.g. "gdb src/geany".
467 Type 'r' to run.
468 Press Ctrl-C from the gdb window to interrupt program execution.
470 Example::
472     Program received signal SIGINT, Interrupt.
473     0x00d16402 in __kernel_vsyscall ()
474     (gdb) call plugin_new("./plugins/.libs/demoplugin.so")
475     ** INFO: Loaded:   ./plugins/.libs/demoplugin.so (Demo)
476     $1 = (Plugin *) 0x905a890
477     (gdb) c
478     Continuing.
480     Program received signal SIGINT, Interrupt.
481     0x00d16402 in __kernel_vsyscall ()
482     (gdb) call plugin_free(0x905a890)
483     ** INFO: Unloaded: ./plugins/.libs/demoplugin.so
484     (gdb) c
485     Continuing.