Skip [] when performing scope autocompletion
[geany-mirror.git] / doc / plugins.dox
blob19c59b73e37c9cbe28b0156b09b200ddc56dbbb8
1 /*
2  * plugins.dox - this file is part of Geany, a fast and lightweight IDE
3  *
4  * Copyright 2008-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5  * Copyright 2008-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6  * Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
7  * Copyright 2014 Matthew Brush <matt(at)geany(dot)org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * This file contains additional plugin documentation like the signal system
24  * and a small howto. It is best viewed when filetype is set to C or C++.
25  */
28 /**
30 @mainpage Geany Plugin API Documentation
32 @author Enrico Tröger, Nick Treleaven, Frank Lanitz, Matthew Brush
34 @section Intro
35 This is the Geany API documentation. It should be considered work in progress.
36 We will try to document as many functions and structs as possible.
38 @warning Do not use any symbol not in the documentation - it may change.
40 @section pluginsupport Plugin Support
41 - @link howto Plugin HowTo @endlink - get started
42 - @link pluginsymbols.c Plugin Symbols @endlink
43 - @link plugindata.h Plugin Datatypes and Macros @endlink
44 - @link pluginsignals.c Plugin Signals @endlink
45 - @link pluginutils.h Plugin Utility Functions @endlink
46 - @link guidelines Plugin Writing Guidelines @endlink
47 - <b>plugins/demoplugin.c</b> - in Geany's source, bigger than the howto example
49 @section common Common API files
50 - @link dialogs.h @endlink
51 - @link document.h @endlink
52 - @link editor.h @endlink
53 - @link filetypes.h @endlink
54 - @link keybindings.h @endlink
55 - @link msgwindow.h @endlink
56 - @link project.h @endlink
57 - @link sciwrappers.h Scintilla Wrapper Functions @endlink
58 - @link spawn.h Spawning programs @endlink
59 - @link stash.h Stash Pref/Setting Functions @endlink
60 - @link utils.h General Utility Functions @endlink
61 - @link ui_utils.h Widget Utility Functions @endlink
63 @section More
64 - All API functions and types - see <b>Files</b> link at the top
65 - Deprecated symbols - see <b>Related Pages</b> link at the top
67 @note See the HACKING file for information about developing the plugin API and
68 other useful notes.
70 @page guidelines Plugin Writing Guidelines
72 @section intro Introduction
74 The following hints and guidelines are only recommendations. Nobody is forced to follow
75 them at all.
77 @section general General notes
79 @subsection ideas Getting a plugin idea
81 If you want to write a plugin but don't know yet what it should do, have a look at
82 http://www.geany.org/Support/PluginWishlist to get an idea about what users wish.
84 @subsection code Managing the source code
86 For authors of plugins for Geany, we created a dedicated @a geany-plugins project
87 on Sourceforge and GitHub to ease development of plugins and help new authors.
88 All information about this project you can find at http://plugins.geany.org/
90 To add a new plugin to this project, get in touch with the people on the
91 geany-devel-mailing list and create a fork of the geany-plugins project
92 at https://github.com/geany/geany-plugins.
93 Beside of adding a new plugin, geany-devel-mailing list is also the place where
94 to discuss development related questions.
95 However, once you have done your fork of geany-plugins you can develop
96 your plugin until you think its the right time to publish it. At this point,
97 create a pull request for adding your patch set into the master branch of the main
98 geany-plugins repository.
100 Of course, you don't need to use GitHub - any Git is fine. But GitHub
101 is making it way easier for review, merging and get in touch with you for
102 comments.
104 If you don't want your plugin to be part of the geany-plugins project it is also fine.
105 Just skip the part about forking geany-plugins and sending a pull request.
106 In this case it is of course also a good idea to post some kind of announcement
107 to geany-devel and maybe to the main geany mailing list -- it's up to you.
108 You can also ask for your plugin to be listed on the http://plugins.geany.org/
109 website as a third party plugin, helping Geany user to know about your plugin.
111 At time of writing, there are some plugins already available in the
112 repositories. Feel free to use any of these plugins as a start for your own,
113 maybe by copying the directory structure and the autotools files
114 (Makefile.am, configure.in, ...). Most of the available plugins are also ready for
115 i18n support, just for reference.
117 We encourage authors using this service to only commit changes to their
118 own plugin and not to others' plugins. Instead just send patches to
119 geany-devel at uvena.de or the plugin author directly.
121 @section paths Installation paths
123 - The plugin binary (@c pluginname.so) should be installed in Geany's libdir. This is
124   necessary so that Geany can find the plugin.
125   An easy way to retrieve Geany's libdir is to use the pkg-config tool, e.g. @code
126   `$PKG_CONFIG --variable=libdir geany`/ geany
127   @endcode
128 - If your plugin creates other binary files like helper programs or helper libraries,
129   they should go into @c $prefix/bin (for programs, ideally prefixed with @a geany),
130   additional libraries should be installed in Geany's libdir, maybe in a subdirectory.
131 - Plugins should install their documentation files (README, NEWS, ChangeLog, licences and
132   other documentation files) into the common documentation directory
133   @c $prefix/share/doc/geany-plugins/$pluginname/
134 - Translation files should be installed normally into @c $prefix/share/locale. There is no
135   need to use Geany's translation directory. To set up translation support properly and
136   for additional information, see main_locale_init().
137 - Do @a never install anything into a user's home directory like installing
138   the plugin binary in @c ~/.config/geany/plugins/.
141 @page howto Plugin HowTo
143 @section intro Introduction
145 Since Geany 0.12 there is a plugin interface to extend Geany's functionality and
146 add new features. This document gives a brief overview about how to add new
147 plugins by writing a simple "Hello World" plugin in C or C++.
150 @section buildenv Build environment
152 To be able to write plugins for Geany, you need the source code and some development
153 packages for GTK and its dependencies. The following will only describe the way to compile and
154 build plugins on Unix-like systems [1].
155 If you already have the Geany source code and compiled it from them, you can skip the
156 following.
158 First you need to have Geany installed. Then install the development files for GTK
159 and its dependencies. The easiest way to do this is to use your distribution's package
160 management system, e.g. on Debian and Ubuntu systems you can use
161 @code apt-get install libgtk2.0-dev intltool @endcode
162 This will install all necessary files to be able to compile plugins for Geany. On other
163 distributions, the package names and commands to use may differ.
165 Basically, you are done at this point and could continue with writing the plugin code.
167 [1] For Windows, it is basically the same but you might have some more work on setting up
168 the general build environment(compiler, GTK development files, ...). This is described on
169 Geany's website at http://www.geany.org/Support/BuildingOnWin32.
171 @section helloworld "Hello World"
173 When writing a plugin, you will find a couple of functions or macros which are mandatory
174 and some which are free to use for implementing some useful feature once your plugin
175 becomes more powerful like including a configuration or help dialog.
177 You should start your plugin with including some of the needed C header files and defining
178 some basic global variables which will help you to access all needed functions of the plugin
179 API in a more comfortable way.
181 Let's start with the very basic headers and add more later if necessary.
182 @code
183 #include <geanyplugin.h>
184 @endcode
186 @a geanyplugin.h includes all of the Geany API and also the necessary GTK header files,
187 so there is no need to include @a gtk/gtk.h yourself.
189 @note
190 @a plugindata.h contains the biggest part of the plugin API and provides some basic macros.
192 Then you should define two basic variables which will give access to data fields
193 provided by the plugin API.
194 @code
195 GeanyPlugin                     *geany_plugin;
196 GeanyData                       *geany_data;
197 @endcode
199 Now you can go on and write your first lines for your new plugin. As mentioned before,
200 you will need to implement and fill out a couple of functions/macros to make the plugin work.
201 So let's start with PLUGIN_VERSION_CHECK().
203 PLUGIN_VERSION_CHECK() is a convenient way to tell Geany which version of Geany's plugin API
204 is needed at minimum to run your plugin. The value is defined in
205 @a plugindata.h by @a GEANY_API_VERSION. In most cases this should be your minimum.
206 Nevertheless when setting this value, you should choose the lowest possible version here to
207 make the plugin compatible with a bigger number of versions of Geany.
209 For the next step, you will need to tell Geany some basic information about your plugin
210 which will be shown in the plugin manager dialog.
212 To do this you should use the PLUGIN_SET_INFO() macro, which expects 4 parameters:
213 - Plugin name
214 - Short description
215 - Version
216 - Author
218 Based on this, the line could look like:
219 @code
220 PLUGIN_SET_INFO("HelloWorld", "Just another tool to say hello world",
221                                 "1.0", "John Doe <john.doe@example.org>");
222 @endcode
224 Once this is done, you will need to implement the function which will be executed when the
225 plugin is loaded. Part of that function could be adding and removing of an item to
226 Geany's Tools menu, setting up keybindings or registering some callbacks. Also you will
227 need to implement the function that is called when your plugin is unloaded.
228 These functions are called plugin_init() and plugin_cleanup(). Let's see what this
229 looks like:
230 @code
231 PLUGIN_VERSION_CHECK(211)
233 PLUGIN_SET_INFO("HelloWorld", "Just another tool to say hello world",
234                                 "1.0", "Joe Doe <joe.doe@example.org>");
236 void plugin_init(GeanyData *data)
240 void plugin_cleanup(void)
243 @endcode
245 If you think this plugin seems not to implement any functionality right now and only wastes
246 some memory, you are right. But it should compile and load/unload in Geany nicely.
247 Now you have the very basic layout of a new plugin. Great, isn't it?
249 @note
251 If you would rather write the plugin in C++, you can do that by marking the
252 plugin functions that it implements as @c extern @c "C", for example:
254 @code
256 extern "C" void plugin_init(GeanyData *data)
260 extern "C" void plugin_cleanup(void)
263 @endcode
265 @section building Building
267 First make plugin.o:
269 @code gcc -c plugin.c -fPIC `pkg-config --cflags geany` @endcode
271 Then make the plugin library plugin.so (or plugin.dll on Windows):
273 @code gcc plugin.o -o plugin.so -shared `pkg-config --libs geany` @endcode
275 If all went OK, put the library into one of the paths Geany looks for plugins,
276 e.g. $prefix/lib/geany. See @ref paths "Installation paths" for details.
278 @note
280 If you are writing the plugin in C++, then you will need to use your C++
281 compiler here, for example @c g++.
283 @section realfunc Adding functionality
285 Let's go on and implement some real functionality.
287 As mentioned before, plugin_init() will be called when the plugin is loaded in Geany.
288 So it should implement everything that needs to be done during startup. In this case,
289 we'd like to add a menu item to Geany's Tools menu which runs a dialog printing "Hello World".
290 @code
291 void plugin_init(GeanyData *data)
293         GtkWidget *main_menu_item;
295         // Create a new menu item and show it
296         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
297         gtk_widget_show(main_menu_item);
299         // Attach the new menu item to the Tools menu
300         gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu),
301                 main_menu_item);
303         // Connect the menu item with a callback function
304         // which is called when the item is clicked
305         g_signal_connect(main_menu_item, "activate",
306                 G_CALLBACK(item_activate_cb), NULL);
308 @endcode
310 This will add an item to the Tools menu and connect this item to a function which implements
311 what should be done when the menu item is activated by the user.
312 This is done by g_signal_connect(). The Tools menu can be accessed with
313 geany->main_widgets->tools_menu. The structure @a main_widgets contains pointers to the
314 main GUI elements in Geany.
316 Geany has a simple API for showing message dialogs. So our function contains
317 only a few lines:
318 @code
319 void item_activate_cb(GtkMenuItem *menuitem, gpointer user_data)
321         dialogs_show_msgbox(GTK_MESSAGE_INFO, "Hello World");
323 @endcode
325 For the moment you don't need to worry about the parameters of that function.
327 Now we need to clean up properly when the plugin is unloaded.
329 To remove the menu item from the Tools menu, you can use gtk_widget_destroy().
330 gtk_widget_destroy() expects a pointer to a GtkWidget object.
332 First you should add gtk_widget_destroy() to your plugin_cleanup() function.
333 The argument for gtk_widget_destroy() is the widget object you created earlier in
334 plugin_init(). To be able to access this pointer in plugin_cleanup(), you need to move
335 its definition from plugin_init() into the global context so its visibility will increase
336 and it can be accessed in all functions.
337 @code
338 static GtkWidget *main_menu_item = NULL;
340 // ...
341 void plugin_init(GeanyData *data)
343         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
344         gtk_widget_show(main_menu_item);
345 // ...
348 void plugin_cleanup(void)
350         gtk_widget_destroy(main_menu_item);
352 @endcode
354 This will ensure your menu item is removed from the Tools menu as well as from
355 memory once your plugin is unloaded, so you don't leave any memory leaks.
356 Once this is done, your first plugin is ready. Congratulations!
358 @section listing Complete listing (without comments)
360 @code
361 #include <geanyplugin.h>
363 GeanyPlugin             *geany_plugin;
364 GeanyData               *geany_data;
366 PLUGIN_VERSION_CHECK(211)
368 PLUGIN_SET_INFO("HelloWorld", "Just another tool to say hello world",
369                                 "1.0", "John Doe <john.doe@example.org>");
372 static GtkWidget *main_menu_item = NULL;
374 static void item_activate_cb(GtkMenuItem *menuitem, gpointer gdata)
376         dialogs_show_msgbox(GTK_MESSAGE_INFO, "Hello World");
379 void plugin_init(GeanyData *data)
381         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
382         gtk_widget_show(main_menu_item);
383         gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu),
384                 main_menu_item);
385         g_signal_connect(main_menu_item, "activate",
386                 G_CALLBACK(item_activate_cb), NULL);
389 void plugin_cleanup(void)
391         gtk_widget_destroy(main_menu_item);
393  @endcode
396 Now you might like to look at Geany's source code for core plugins such as
397 @a plugins/demoplugin.c.
399 @section furtherimprovements Furter Improvements and next steps
400 @subsection translatable_plugin_information Translatable plugin information
402 After having written our first plugin, there is still room for improvement.
404 By default, PLUGIN_SET_INFO() does not allow translation of the basic plugin
405 information for plugins which are not shipped with Geany's core distribution.
406 Since most plugins are not shipped with Geany's core, it makes sense to
407 enable translation when the plugin is loaded so that it gets translated
408 inside Geany's Plugin Manager.  As of Geany 0.19, the plugin API contains
409 the PLUGIN_SET_TRANSLATABLE_INFO() macro which enables translation of the
410 basic plugin details passed to PLUGIN_SET_INFO() when the plugin is loaded.
412 PLUGIN_SET_TRANSLATABLE_INFO() takes two more parameters than PLUGIN_SET_INFO(),
413 for a total of six parameters.
415  - Localedir
416  - Gettextpackage
417  - Plugin name
418  - Short description
419  - Version
420  - Author
422 The @a Localdir and the @a Gettextpackage parameters are usually set inside
423 the build system.  If this has been done, the call for example HelloWorld
424 plugin could look like:
426 @code
427 PLUGIN_SET_TRANSLATABLE_INFO(
428         LOCALEDIR, GETTEXT_PACKAGE, _("Hello World"),
429         _("Just another tool to say hello world"),
430         "1.0", "John Doe <john.doe@example.org>");
431 @endcode
433 When using this macro, you should use the gettext macro @a _() to mark
434 the strings like name and the short description as translatable as well. You
435 can see how this is done in the above example.
437 As you can see the author's information is not marked as translatable in
438 this example.  The community has agreed that the best practice here is to
439 use, if possible, the latin version of the author's name followed by the
440 native spelling inside parenthesis, where applicable.
442 @subsection plugin_i18n Using i18n/l10n inside Plugin
445 You can (and should) also mark other strings beside the plugin's meta
446 information as translatable.  Strings used in menu entries, information
447 boxes or configuration dialogs should also be translatable as well.  Geany
448 offers a way to enable this in the plugin's code using the main_locale_init()
449 function provided by the plugin API. This function takes the same two
450 parameters discussed in the previous section; @a GETTEXT_PACKAGE and
451 @a LOCALEDIR.
453 The main_locale_init() function is best called during initialization in the
454 plugin's plugin_init() function.  Adding this to the HelloWorld example could
455 look like:
456 @code
457 void plugin_init(GeanyData *data)
459         main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
460         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
461         gtk_widget_show(main_menu_item);
462         gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu),
463                 main_menu_item);
464         g_signal_connect(main_menu_item, "activate",
465                 G_CALLBACK(item_activate_cb), NULL);
467 @endcode
469 @note If you've previously called the PLUGIN_SET_TRANSLATABLE_INFO() you do not
470 need to call main_locale_init() yourself, as this has been already been
471 done for you.