Merge pull request #3720 from b4n/encodings-ui-improvements
[geany-mirror.git] / doc / plugins.dox
blob4d0591c34d1fcd549e830958e09c42fb0c2f606b
1 /*
2  * plugins.dox - this file is part of Geany, a fast and lightweight IDE
3  *
4  * Copyright 2008 The Geany contributors
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * This file contains additional plugin documentation like the signal system
21  * and a small howto. It is best viewed when filetype is set to C or C++.
22  */
25 /**
27 @mainpage Geany Plugin API Documentation
29 @author Enrico Tröger, Nick Treleaven, Frank Lanitz, Matthew Brush
31 @section Intro
32 This is the Geany API documentation. It should be considered work in progress.
33 We will try to document as many functions and structs as possible.
35 @warning Do not use any symbol not in the documentation - it may change.
36 @warning Except for exceptions stated in the documentation for geany_load_module(), no API function
37 may be called if the plugin is not enabled (between the calls to their GeanyFuncs::init and
38 GeanyFuncs::cleanup functions).
40 @section pluginsupport Plugin Support
41 - @link howto Plugin HowTo @endlink - get started
42 - @ref proxy
43 - @ref legacy
44 - @link plugindata.h Plugin Datatypes and Macros @endlink
45 - @link pluginsignals.c Plugin Signals @endlink
46 - @link pluginutils.h Plugin Utility Functions @endlink
47 - @link guidelines Plugin Writing Guidelines @endlink
48 - <b>plugins/demoplugin.c</b> - in Geany's source, bigger than the howto example
50 @section common Common API files
51 - @link dialogs.h @endlink
52 - @link document.h @endlink
53 - @link editor.h @endlink
54 - @link filetypes.h @endlink
55 - @link keybindings.h @endlink
56 - @link msgwindow.h @endlink
57 - @link project.h @endlink
58 - @link sciwrappers.h Scintilla Wrapper Functions @endlink
59 - @link spawn.h Spawning programs @endlink
60 - @link stash.h Stash Pref/Setting Functions @endlink
61 - @link utils.h General Utility Functions @endlink
62 - @link ui_utils.h Widget Utility Functions @endlink
64 @section More
65 - All API functions and types - see <b>Files</b> link at the top
66 - Deprecated symbols - see <b>Related Pages</b> link at the top
68 @note See the HACKING file for information about developing the plugin API and
69 other useful notes.
71 @page guidelines Plugin Writing Guidelines
73 @section intro_guidelines Introduction
75 The following hints and guidelines are only recommendations. Nobody is forced to follow
76 them at all.
78 @section general General notes
80 @subsection ideas Getting a plugin idea
82 If you want to write a plugin but don't know yet what it should do, have a look at
83 https://www.geany.org/Support/PluginWishlist to get an idea about what users wish.
85 @subsection code Managing the source code
87 For authors of plugins for Geany, we created a dedicated @a geany-plugins project
88 on Sourceforge and GitHub to ease development of plugins and help new authors.
89 All information about this project you can find at https://plugins.geany.org/
91 To add a new plugin to this project, get in touch with the people on the
92 geany-devel-mailing list and create a fork of the geany-plugins project
93 at https://github.com/geany/geany-plugins.
94 Beside of adding a new plugin, geany-devel-mailing list is also the place
95 to discuss development related questions.
96 However, once you have done your fork of geany-plugins you can develop
97 your plugin until you think it is the right time to publish it. At this point,
98 create a pull request for adding your patch set into the master branch of the main
99 geany-plugins repository.
101 Of course, you don't need to use GitHub - any Git is fine. But GitHub
102 is making it way easier for review, merging and get in touch with you for
103 comments.
105 If you don't want your plugin to be part of the geany-plugins project it is also fine.
106 Just skip the part about forking geany-plugins and sending a pull request.
107 In this case it is of course also a good idea to post some kind of announcement
108 to geany-devel and maybe to the main geany mailing list -- it's up to you.
109 You can also ask for your plugin to be listed on the https://plugins.geany.org/
110 website as a third party plugin, helping Geany user to know about your plugin.
112 At time of writing, there are some plugins already available in the
113 repositories. Feel free to use any of these plugins as a start for your own,
114 maybe by copying the directory structure and the autotools files
115 (Makefile.am, configure.in, ...). Most of the available plugins are also ready for
116 i18n support, just for reference.
118 We encourage authors using this service to only commit changes to their
119 own plugin and not to others' plugins. Instead just send patches to
120 geany-devel at uvena.de or the plugin author directly.
122 @section paths Installation paths
124 - The plugin binary (@c pluginname.so) should be installed in Geany's libdir. This is
125   necessary so that Geany can find the plugin.
126   An easy way to retrieve Geany's libdir is to use the pkg-config tool, e.g. @code
127   `$PKG_CONFIG --variable=libdir geany`/ geany
128   @endcode
129 - If your plugin creates other binary files like helper programs or helper libraries,
130   they should go into @c $prefix/bin (for programs, ideally prefixed with @a geany),
131   additional libraries should be installed in Geany's libdir, maybe in a subdirectory.
132 - Plugins should install their documentation files (README, NEWS, ChangeLog, licences and
133   other documentation files) into the common documentation directory
134   @c $prefix/share/doc/geany-plugins/$pluginname/
135 - Translation files should be installed normally into @c $prefix/share/locale. There is no
136   need to use Geany's translation directory. To set up translation support properly and
137   for additional information, see main_locale_init().
138 - Do @a never install anything into a user's home directory like installing
139   the plugin binary in @c ~/.config/geany/plugins/.
142 @page howto Plugin HowTo
144 @section intro_howto Introduction
146 Since Geany 0.12 there is a plugin interface to extend Geany's functionality and
147 add new features. This document gives a brief overview about how to add new
148 plugins by writing a simple "Hello World" plugin in C or C++.
151 @section buildenv Build environment
153 To be able to write plugins for Geany, you need the source code and some development
154 packages for GTK and its dependencies. The following will only describe the way to compile and
155 build plugins on Unix-like systems [1].
156 If you already have the Geany source code and compiled it from them, you can skip the
157 following.
159 First you need to have Geany installed. Then install the development files for GTK
160 and its dependencies. The easiest way to do this is to use your distribution's package
161 management system, e.g. on Debian and Ubuntu systems you can use
162 @code apt-get install libgtk-3-dev autoconf automake autopoint gettext @endcode
163 This will install all necessary files to be able to compile plugins for Geany. On other
164 distributions, the package names and commands to use may differ.
166 Basically, you are done at this point and could continue with writing the plugin code.
168 [1] For Windows, it is basically the same but you might have some more work on setting up
169 the general build environment (compiler, GTK development files, ...). This is described on
170 Geany's website at https://www.geany.org/Support/BuildingOnWin32.
172 @section helloworld "Hello World"
174 @note This section describes the new entry points for plugins introduced with Geany 1.26. A short
175 summary of the legacy entry points is given at page @ref legacy but they are deprecated should not
176 be used any more.
178 When writing a plugin you will find a couple of functions which are mandatory and some which can be
179 implemented optionally for implementing some useful features once your plugin becomes more
180 powerful. For example to provide a configuration or help dialog.
182 @subsection beginning First steps for any Plugin
184 You should start your plugin with including <geanyplugin.h> and exporting a function named @a
185 geany_load_module(). In this function you must fill in basic information that Geany uses to learn
186 more about your plugin and present it to the user. You also must define some hooks that enable
187 Geany to actually execute your code.
189 Please also do not forget about license headers which are by convention at the start of source
190 files. You can use templates provided by Geany to get started. Without a proper license it will be
191 difficult for packagers to pick up and distribute your plugin.
193 As mentioned above, start with the very fundamental header that gets you all goodies of Geany's
194 plugin API. @a geanyplugin.h includes all of the Geany API and also the necessary GTK header
195 files so there is no need to include @a gtk/gtk.h yourself. In fact it includes a utility header
196 that helps supporting GTK+2 and GTK+3 in the same source.
198 @code
199 #include <geanyplugin.h>
200 @endcode
202 @note If you use autoconf then config.h must be included even before that as usual.
204 Now you can go on and write your first lines for your new plugin. As mentioned before, you will
205 need to implement a couple of functions. The first mandatory one is @a geany_load_module(). Geany
206 uses the presence of this function to identify a library as a plugin. When Geany scans the
207 pre-defined and user-configured plugin directories, it will take a look at each shared library (or
208 DLL on Windows) to see if it exports a @a geany_load_module() symbol. Files lacking these will be
209 ignored. The second mandatory one is an initialization function that is only called when the plugin
210 becomes actually enabled (by the user or at startup).
212 @subsection register Registering a Plugin
214 Geany will always invoke this geany_load_module(), regardless of whether the user activates your
215 plugin. In fact its purpose to probe if the plugin should even be presented to the user. Therefore
216 you must use this function to register your plugin. Geany will pass a pointer to a GeanyPlugin
217 instance which acts as a unique handle to your plugin. Use this pointer for registering and later
218 API calls. It won't change for the life time of the plugin. Registering the plugin consists of a
219 number of steps:
221   1. Filling in GeanyPlugin::info with metadata that is shown to the user.
222     - @ref PluginInfo::name : The name of your plugin
223     - @ref PluginInfo::description : A brief description.
224     - @ref PluginInfo::version : The plugin's version.
225     - @ref PluginInfo::author : Your contact information, preferably in the form "Name <email>".
226     .
227     Filling in all of them is recommended to provide the best user experience, but only the name is
228     truly mandatory. Since all of the strings are shown to the user they should be human readable.
229   2. Filling in GeanyPlugin::funcs with function pointers that are called by Geany.
230     - @ref GeanyPluginFuncs::init : an initialization function
231     - @ref GeanyPluginFuncs::cleanup : a finalization function
232     - @ref GeanyPluginFuncs::configure : a function that provides configuration (optional)
233     - @ref GeanyPluginFuncs::help : a function that provides help (optional)
234     - @ref GeanyPluginFuncs::callbacks : a pointer to an array of PluginCallback (optional).
235     .
236     @a init and @a cleanup are mandatory, the other ones depend on how advanced your plugin is.
237     Furthermore, @a init is called on startup and when the user activates your plugin in the Plugin
238     Manager, and @a cleanup is called on exit and when the user deactivates it. So use these to do
239     advanced initialization and teardown as to not waste resources when the plugin is not even
240     enabled.
241   3. Actually registering by calling GEANY_PLUGIN_REGISTER() or GEANY_PLUGIN_REGISTER_FULL().
242    - Usually you should use GEANY_PLUGIN_REGISTER() to register your plugin, passing the
243     GeanyPlugin pointer that you received and filled out as above. GEANY_PLUGIN_REGISTER() also
244     takes the minimum API version number you want to support (see @ref versions for details). Please
245     also <b>check the return value</b>. Geany may refuse to load your plugin due to
246     incompatibilities, you should then abort any extra setup. GEANY_PLUGIN_REGISTER() is a macro
247     wrapping geany_plugin_register() which takes additional the API and ABI that you should not pass
248     manually.
249    - If you require a plugin-specific context or state to be passed to your GeanyPlugin::funcs then
250     use GEANY_PLUGIN_REGISTER_FULL() to register. This one takes additional parameters for adding
251     user data to your plugin. That user data pointer is subsequently passed back to your functions.
252     It allows, for example, to set instance pointer to objects in case your plugin isn't written in
253     pure C, enabling you to use member functions as plugin functions. You may also set such data
254     later on, for example in your @ref GeanyPluginFuncs::init routine to defer costly allocations
255     to when the plugin is actually activated by the user. However, you then have to call
256     geany_plugin_set_data().
259 @subsection versions On API and ABI Versions
260 As previously mentioned @a geany_plugin_register() takes a number of versions as arguments:
261   1. api_version
262   2. min_api_version
263   3. abi_version
265 These refer to Geany's versioning scheme to manage plugin compatibility. The following rules apply:
266   - Plugins are compiled against a specific Geany version on the build machine. This version of
267     Geany has specific ABI and API versions, which will be compiled into the plugin. Both are
268     managed automatically, by calling GEANY_PLUGIN_REGISTER().
269   - The Geany version that loads the plugin may be different, possibly even have different API and
270     ABI versions.
271   - The ABI version is the primary plugin compatibility criteria. The ABI version of the running
272     Geany and the one that's compiled into the plugin must match exactly (==). In case of mismatch,
273     the affected plugins need to be recompiled (generally without source code changes) against the
274     running Geany. The ABI is usually stable even across multiple releases of Geany.
275   - The API version is secondary. It doesn't have to match exactly, however a plugin can report
276     a minimum API version that it requires to run. Geany will check if its own API is larger than
277     that (>=) and will otherwise refuse to load the plugin. The API version is incremented when
278     functions or variables are added to the API which often happens more than once within a release
279     cycle.
280   - The API version the plugin is compiled against is still relevant for enabling compatibility
281     code inside Geany (for cases where incrementing the ABI version could be avoided).
283 Instead of calling geany_plugin_register() directly it is very highly recommended to use
284 GEANY_PLUGIN_REGISTER(). This is a convenient way to pass Geany's current API and ABI versions
285 without requiring future code changes whenever either one changes. In fact, the promise that
286 plugins need to be just recompiled on ABI change can hold if the plugins use this macro. You still
287 want to pass the API version needed at minimum to run your plugin. The value is defined in
288 plugindata.h by @ref GEANY_API_VERSION. In most cases this should be your minimum. Nevertheless when
289 setting this value, you should choose the lowest possible version here to make the plugin
290 compatible with a bigger number of versions of Geany. The absolute minimum is 225 which introduced
291 the new plugin entry points.
293 To increase your flexibility the API version of the running Geany is passed to geany_load_module().
294 You can use this information to toggle API-specific code. This comes handy, for example to enable
295 optional code that requires a recent API version without raising your minimum required API version.
296 This enables running the plugin against more Geany versions, although perhaps at reduced
297 functionality.
299 @subsection example Example
301 Going back to our "Hello World" plugin here is example code that properly adds the HelloWorld
302 plugin to Geany.
304 @code
305 /* License blob */
307 #include <geanyplugin.h>
310 static gboolean hello_init(GeanyPlugin *plugin, gpointer pdata)
312         printf("Hello World from plugin!\n");
314         /* Perform advanced set up here */
316         return TRUE;
320 static void hello_cleanup(GeanyPlugin *plugin, gpointer pdata)
322         printf("Bye World :-(\n");
326 G_MODULE_EXPORT
327 void geany_load_module(GeanyPlugin *plugin)
329         /* Step 1: Set metadata */
330         plugin->info->name = "HelloWorld";
331         plugin->info->description = "Just another tool to say hello world";
332         plugin->info->version = "1.0";
333         plugin->info->author = "John Doe <john.doe@example.org>";
335         /* Step 2: Set functions */
336         plugin->funcs->init = hello_init;
337         plugin->funcs->cleanup = hello_cleanup;
339         /* Step 3: Register! */
340         GEANY_PLUGIN_REGISTER(plugin, 225);
341         /* alternatively:
342         GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, free_func); */
344 @endcode
346 If you think this plugin seems not to implement any functionality right now and only wastes
347 some memory, you are right. But it should compile and load/unload in Geany nicely.
348 Now you have the very basic layout of a new plugin. Great, isn't it?
350 If you would rather write the plugin in C++, you can do that by marking @a geany_load_module()
351 as <c> extern "C" </c>, for example:
353 @code
355 extern "C" void geany_load_module(GeanyPlugin *plugin)
359 @endcode
361 You can also create an instance of a class and set that as data pointer (with
362 GEANY_PLUGIN_REGISTER_FULL()). With small wrappers that shuffle the parameters you can even use
363 member functions for @ref GeanyPlugin::funcs etc.
365 @section building Building
367 First make plugin.o:
369 @code gcc -c plugin.c -fPIC `pkg-config --cflags geany` @endcode
371 Then make the plugin library plugin.so (or plugin.dll on Windows):
373 @code gcc plugin.o -o plugin.so -shared `pkg-config --libs geany` @endcode
375 If all went OK, put the library into one of the paths Geany looks for plugins,
376 e.g. $prefix/lib/geany. See @ref paths "Installation paths" for details.
378 If you are writing the plugin in C++, then you will need to use your C++
379 compiler here, for example @c g++.
381 @section realfunc Adding functionality
383 Let's go on and implement some real functionality.
385 As mentioned before, GeanyPluginFuncs::init() will be called when the plugin is activated by
386 Geany. So it should implement everything that needs to be done during startup. In this case, we'd
387 like to add a menu item to Geany's Tools menu which runs a dialog printing "Hello World".
389 @code
390 static gboolean hello_init(GeanyPlugin *plugin, gpointer pdata)
392         GtkWidget *main_menu_item;
394         // Create a new menu item and show it
395         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
396         gtk_widget_show(main_menu_item);
398         // Attach the new menu item to the Tools menu
399         gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu),
400                 main_menu_item);
402         // Connect the menu item with a callback function
403         // which is called when the item is clicked
404         g_signal_connect(main_menu_item, "activate",
405                 G_CALLBACK(item_activate_cb), NULL);
407         return TRUE;
409 @endcode
411 This will add an item to the Tools menu and connect this item to a function which implements what
412 should be done when the menu item is activated by the user. This is done by g_signal_connect(). The
413 Tools menu can be accessed with plugin->geany_data->main_widgets->tools_menu. The structure
414 GeanyMainWidgets contains pointers to all main GUI elements in Geany.
416 Geany has a simple API for showing message dialogs. So our function contains
417 only a few lines:
418 @code
419 static void item_activate_cb(GtkMenuItem *menuitem, gpointer user_data)
421         dialogs_show_msgbox(GTK_MESSAGE_INFO, "Hello World");
423 @endcode
425 For the moment you don't need to worry about the parameters of that function.
427 Now we need to clean up properly when the plugin is unloaded.
429 To remove the menu item from the Tools menu you can use gtk_widget_destroy().
431 First you should add gtk_widget_destroy() to your GeanyPluginFuncs::cleanup() function. The
432 argument for gtk_widget_destroy() is the widget object you created earlier in
433 GeanyPluginFuncs::init(). To be able to access this pointer in GeanyPluginFuncs::cleanup() you can
434 use geany_plugin_set_data() to set plugin-defined data pointer to the widget. Alternatively, you
435 can store the pointer in some global variable so its visibility will increase and it can be
436 accessed in all functions.
438 @code
439 /* alternative: global variable:
440 static GtkWidget *main_menu_item;
442 // ...
443 static gboolean hello_init(GeanyPlugin *plugin, gpointer pdata)
445         GtkWidget *main_menu_item;
447         // Create a new menu item and show it
448         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
449         gtk_widget_show(main_menu_item);
450 // ...
451         geany_plugin_set_data(plugin, main_menu_item, NULL);
452         return TRUE;
455 static void hello_cleanup(GeanyPlugin *plugin, gpointer pdata)
457         GtkWidget *main_menu_item = (GtkWidget *) pdata;
458 // ...
459         gtk_widget_destroy(main_menu_item);
461 @endcode
463 This will ensure your menu item is removed from the Tools menu as well as from
464 memory once your plugin is unloaded, so you don't leave any memory leaks.
465 Once this is done, your first plugin is ready. Congratulations!
467 @section listing Complete listing (without comments)
469 @code
471 #include <geanyplugin.h>
473 static void item_activate_cb(GtkMenuItem *menuitem, gpointer user_data)
475         dialogs_show_msgbox(GTK_MESSAGE_INFO, "Hello World");
479 static gboolean hello_init(GeanyPlugin *plugin, gpointer pdata)
481         GtkWidget *main_menu_item;
483         // Create a new menu item and show it
484         main_menu_item = gtk_menu_item_new_with_mnemonic("Hello World");
485         gtk_widget_show(main_menu_item);
486         gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu),
487                         main_menu_item);
488         g_signal_connect(main_menu_item, "activate",
489                         G_CALLBACK(item_activate_cb), NULL);
491         geany_plugin_set_data(plugin, main_menu_item, NULL);
492         return TRUE;
496 static void hello_cleanup(GeanyPlugin *plugin, gpointer pdata)
498         GtkWidget *main_menu_item = (GtkWidget *) pdata;
500         gtk_widget_destroy(main_menu_item);
504 G_MODULE_EXPORT
505 void geany_load_module(GeanyPlugin *plugin)
507         plugin->info->name = "HelloWorld";
508         plugin->info->description = "Just another tool to say hello world";
509         plugin->info->version = "1.0";
510         plugin->info->author = "John Doe <john.doe@example.org>";
512         plugin->funcs->init = hello_init;
513         plugin->funcs->cleanup = hello_cleanup;
515         GEANY_PLUGIN_REGISTER(plugin, 225);
517 @endcode
520 Now you might like to look at Geany's source code for core plugins such as
521 @a plugins/demoplugin.c.
523 @section furtherimprovements Further Improvements and next steps
524 @subsection translatable_plugin_information Translatable plugin information
526 After having written our first plugin, there is still room for improvement.
528 By default, @ref geany_load_module() is not prepared to allow translation of the basic plugin
529 information, except plugins which are shipped with Geany's core distribution, because custom
530 gettext catalogs are not setup. Since most plugins are not shipped with Geany's core, it makes
531 sense to setup gettext when the plugin is loaded so that it gets translated inside Geany's Plugin
532 Manager. The solution is to call the API function main_locale_init() inside @ref
533 geany_load_module() and then use gettext's _() as usual.
535 The invocation will most probably look similar to this:
536 @code
537 // ...
538         main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
539         plugin->info->name = _("HelloWorld");
540         plugin->info->description = _("Just another tool to say hello world");
541         plugin->info->version = "1.0";
542         plugin->info->author = "John Doe <john.doe@example.org>";
543 @endcode
545 The @a LOCALEDIR and the @a GETTEXT_PACKAGE parameters are usually set inside the build system.
547 As you can see the author's information is not marked as translatable in
548 this example.  The community has agreed that the best practice here is to
549 use, if possible, the latin version of the author's name followed by the
550 native spelling inside parenthesis, where applicable.
552 @subsection plugin_i18n Using i18n/l10n inside Plugin
555 You can (and should) also mark other strings beside the plugin's meta information as translatable.
556 Strings used in menu entries, information boxes or configuration dialogs should be translatable as
557 well.
559 @code
560 static gboolean hello_init(GeanyPlugin *plugin, gpointer pdata)
562         main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
563         main_menu_item = gtk_menu_item_new_with_mnemonic(_("Hello World"));
564 // ...
566 @endcode
568 @page legacy Porting guide from legacy entry points to the current ones
570 @section intro_legacy Introduction
572 This page briefly describes the deprecated, legacy plugin entry points. These have been in place
573 prior to Geany 1.26 and are still loadable and working for the time being. However, do not create
574 new plugins against these. For this reason, the actual description here is rather minimalistic and
575 concentrates on porting legacy plugins to the new interface. Basically its main purpose
576 is to give newcomers an idea of what they are looking at if they come across a legacy plugin.
578 @section overview Overview
580 The legacy entry points consist of a number of pre-defined symbols (functions and variables)
581 exported by plugins. There is no active registration procedure. It is implicit simply by exporting
582 the mandatory symbols. The entirety of the symbols is described at the page @link pluginsymbols.c
583 Plugin Symbols @endlink.
585 At the very least plugins must define the functions @a plugin_init(GeanyData *) and @a
586 plugin_version_check(gint). Additionally, an instance of the struct PluginInfo named plugin_info
587 must be exported as well, this contains the same metadata already known from GeanyPlugin::info. The
588 functions plugin_cleanup(), plugin_help(), plugin_configure(GtkDialog *) and
589 plugin_configure_single(GtkWidget *) are optional, however Geany prints a warning if
590 plugin_cleanup() is missing and only one of plugin_configure(GtkDialog *) and
591 plugin_configure_single(GtkWidget *) is used for any single plugin.
593 By convention, plugin_version_check() is implicitly defined through the use of PLUGIN_VERSION_CHECK(),
594 and similarly plugin_info is defined through PLUGIN_SET_INFO() or PLUGIN_SET_TRANSLATABLE_INFO().
596 The functions should generally perform the same tasks as their equivalents in GeanyPlugin::funcs.
598 Geany also recognized numerous variable fields if the plugin exported them globally, and actually
599 set a few of them inside the plugins data section.
601 @section porting Porting a Legacy Plugin
603 Given a legacy plugin it can be modified to use the new entry points without much effort. This
604 section gives a basic recipe that should work for most existing plugins. The transition should
605 be easy and painless so it is recommended that you adapt your plugin as soon as possible.
607 @note This guide is intentionally minimalistic (in terms of suggested code changes) in order to
608 allow adaption to the current entry points as quickly as possible and without a lot effort. It
609 should also work even for rather complex plugins comprising multiple source files. On the other hand
610 it does not make use of new features such as geany_plugin_set_data().
612 @subsection functions Functions
614 Probably the biggest hurdle is the dropped support of the long-deprecated
615 plugin_configure_single(). This means you first have to port the configuration dialog (if any) to
616 the combined plugin dialog. While you previously created a custom dialog you now attach the main
617 widget of that dialog to the combined plugin dialog simply by returning it from
618 GeanyPluginFuncs::configure. You don't actually add it, Geany will do that for you. The pointer to
619 the dialog is passed to @a configure simply to allow you to connect to its "response" or "close"
620 signals.
622 The following lists the function mapping of previous @a plugin_* functions to the new @a
623 GeanyPlugin::funcs. They are semantically the same, however the new functions receive more
624 parameters which you may use or not.
626   - plugin_init() => GeanyPlugin->funcs->init
627   - plugin_cleanup() => GeanyPlugin->funcs->cleanup
628   - plugin_help() => GeanyPlugin->funcs->help
629   - plugin_configure() => GeanyPlugin->funcs->configure
631 @note @ref GeanyPluginFuncs::init() should return a boolean value: whether or not the plugin loaded
632 successfully. Since legacy plugins couldn't fail in plugin_init() you should return @c TRUE
633 unconditionally.
635 @note Again, plugin_configure_single() is not supported anymore.
637 @subsection Variables
639 Exported global variables are not recognized anymore. They are replaced in the following ways:
641 @ref plugin_info is simply removed. Instead, you have to assign the values to GeanyPlugin::info
642 yourself, and it must be done inside your @a geany_load_module().
644 Example:
646 @code
647 PLUGIN_SET_INFO(
648         "HelloWorld",
649         "Just another tool to say hello world",
650         "1.0", "John Doe <john.doe@example.org>");
651 @endcode
653 becomes
655 @code
656 G_MODULE_EXPORT
657 void geany_load_module(GeanyPlugin *plugin)
659 // ...
660         plugin->info->name = "HelloWorld";
661         plugin->info->description = "Just another tool to say hello world";
662         plugin->info->version = "1.0";
663         plugin->info->author = "John Doe <john.doe@example.org>";
664 // ...
666 @endcode
667 @note Refer to @ref translatable_plugin_information for i18n support for the metadata.
670 The @ref plugin_callbacks array is supported by assigning the GeanyPluginFuncs::callbacks to
671 the array.
673 @ref plugin_fields is not supported anymore. Use ui_add_document_sensitive() instead.
674 @c PLUGIN_KEY_GROUP and @ref plugin_key_group are also not supported anymore. Use
675 plugin_set_key_group() and keybindings_set_item() respectively.
677 Additionally, Geany traditionally set a few variables. This is not the case anymore. @ref
678 geany_functions has been removed in 1.25 and since then existed only for compatibility and has been
679 empty. You can simply remove its declaration from your source code. @ref geany_plugin is passed to
680 each @ref GeanyPluginFuncs function. You need to store it yourself somewhere if you need it
681 elsewhere. @ref geany_data is now available as a member of GeanyPlugin.
683 @code
684 GeanyPlugin *geany_plugin;
685 GeanyData *geany_data;
687 static gboolean my_init(GeanyPlugin *plugin, gpointer pdata)
689 // ...
690         geany_plugin = plugin;
691         geany_data = plugin->geany_data;
692         return TRUE;
694 @endcode
696 @ref geany_plugin is now also passed by default to the PluginCallback signal handlers as data
697 pointer if it's set to NULL.
699 @code
700 static PluginCallback plugin_callbacks[] = {
701         { "editor-notify", (GCallback) &on_editor_notify_cb, FALSE, NULL },
702 // ...
705 static gboolean on_editor_notify_cb(GObject *object, GeanyEditor *editor,
706                                                                         SCNotification *nt, gpointer data)
708         GeanyPlugin *plugin = data;
709 //...
713 G_MODULE_EXPORT
714 void geany_load_module(GeanyPlugin *plugin)
716 // ...
717         plugin->funcs->callbacks = plugin_callbacks;
718 // ...
721 @endcode
724 @page proxy Proxy Plugin HowTo
726 @section proxy_intro Introduction
728 Geany has built-in support for plugins. These plugins can alter the way Geany operates in many
729 imaginable ways which leaves little to be desired.
731 However, there is one significant short-coming. Due to the infrastructure, Geany's built-in support
732 only covers plugins written in C, perhaps C++ and Vala. Basically all languages which can be
733 compiled into native shared libraries and can link GTK libraries. This excludes dynamic languages
734 such as Python.
736 Geany provides a mechanism to enable support for those languages. Native plugins can register as
737 proxy plugins by being a normal plugin to the Geany-side and by providing a bridge to write plugins
738 in another language on the other side.
740 These plugins are also called sub-plugins. This refers to the relation to their proxy.
741 To Geany they are first-class citizens.
743 @section proxy_protocol Writing a Proxy Plugin
745 The basic idea is that a proxy plugin provides methods to match, load and unload one or more
746 sub-plugin plugins in an abstract manner:
748  - Matching consists of providing a list of supported file extensions for the sub-plugins and
749    a mechanism to resolve file extension uncertainty or ambiguity. The matching makes the plugin
750    visible to the user within the Plugin Manager.
751  - Loading consists of loading the sub-plugin's file, passing the file to some form of interpreter
752    and calling GEANY_PLUGIN_REGISTER() or GEANY_PLUGIN_REGISTER_FULL() on behalf of the sub-plugin
753    at some point.
754  - Unloading simply reverses the effect of loading.
756 For providing these methods, GeanyPlugin has a field GeanyProxyFuncs which contains three function
757 pointers which must be initialized prior to calling geany_plugin_register_proxy(). This should be
758 done in the GeanyPluginFuncs::init function of the proxy plugin.
760  - In the call to geany_plugin_register_proxy() the proxy plugin passes a list of file extensions.
761    When Geany scans through its plugin directories as usual it will also look for files with
762    that extensions and consider found files as plugin candidate.
763  - GeanyProxyFuncs::probe may be implemented to probe if a plugin candidate (that has one of the
764    provided file extensions) is actually a plugin. This may depend on the plugin file itself in
765    case of ambiguity or availability of runtime dependencies or even configuration.
766    @ref GeanyProxyProbeResults constants should be returned. Not implementing GeanyProxyFuncs::probe
767    at all is equivalent to always returning @ref GEANY_PROXY_MATCH.
768  - GeanyProxyFuncs::load must be implemented to actually load the plugin. It is called by Geany
769    when the user enables the sub-plugin. What "loading" means is entirely up to the proxy plugin and
770    probably depends on the interpreter of the dynamic language that shall be supported. After
771    setting everything up as necessary GEANY_PLUGIN_REGISTER() or GEANY_PLUGIN_REGISTER_FULL() must
772    be called to register the sub-plugin.
773  - GeanyProxyFuncs::unload must be implemented and is called when the user unchecks the sub-plugin
774    or when Geany exits. Here, the proxy should release any references or memory associated to the
775    sub-plugin. Note that if GeanyProxyFuncs::load didn't succeed, i.e. didn't successfully register
776    the sub-plugin, then this function won't be called.
778 GeanyProxyFuncs::load and GeanyProxyFuncs::unload receive two GeanyPlugin pointers: One that
779 corresponds to the proxy itself and another that corresponds to the sub-plugin. The sub-plugin's
780 one may be used to call various API functions on behalf of the sub-plugin, including
781 GEANY_PLUGIN_REGISTER() and GEANY_PLUGIN_REGISTER_FULL().
783 GeanyProxyFuncs::load may return a pointer that is passed back to GeanyProxyFuncs::unload. This can
784 be used to store proxy-defined but sub-plugin-specific data required for unloading. However, this
785 pointer is not passed to the sub-plugin's GeanyPluginFuncs. To arrange for that, you want to call
786 GEANY_PLUGIN_REGISTER_FULL(). This method is the key to enable proxy plugins to wrap the
787 GeanyPluginFuncs of all sub-plugins and yet multiplex between multiple sub-plugin, for example by
788 storing a per-sub-plugin interpreter context.
790 @note If the pointer returned from GeanyProxyFuncs::load is the same that is passed to
791 GEANY_PLUGIN_REGISTER_FULL() then you must pass NULL as free_func, because that would be invoked
792 prior to unloading. Insert the corresponding code into GeanyProxyFuncs::unload.
794 @section proxy_compat_guideline Guideline for Checking Compatibility
796 Determining if a plugin candidate is compatible is not a single test. There are multiple levels and
797 each should be handled differently in order to give the user a consistent feedback.
799 Consider the 5 basic cases:
801 1) A candidate comes with a suitable file extension but is not a workable plugin file at all. For
802 example, your proxy supports plugins written in a shell script (.sh) but the shebang of that script
803 points to an incompatible shell (or even lacks a shebang). You should check for this in
804 GeanyProxyFuncs::probe() and return @ref GEANY_PROXY_IGNORE which hides that script from the Plugin
805 Manager and allows other enabled proxy plugins to pick it up. GeanyProxyFuncs::probe() returning
806 @ref GEANY_PROXY_IGNORE is an indication that the candidate is meant for another proxy, or the user
807 placed the file by accident in one of Geany's plugin directories. In other words the candidate
808 simply doesn't correspond to your proxy. Thus any noise by debug messages for this case is
809 undesirable.
811 2) A proxy plugin provides its own, versioned API to sub-plugin. The API version of the sub-plugin
812 is not compatible with the API exposed by the proxy. GeanyProxyFuncs::probe() should never perform
813 a version check because its sole purpose is to indicate a proxy's correspondence to a given
814 candidate. It should return @ref GEANY_PROXY_MATCH instead. Later, Geany will invoke the
815 GeanyProxyFuncs::load(), and this function is the right place for a version check. If it fails then
816 you simply do not call GEANY_PLUGIN_REGISTER(), but rather print a debug message. The result is
817 that the sub-plugin is not shown in the Plugin Manager at all. This is consistent with the
818 treatment of native plugins by Geany.
820 3) The sub-plugin is also depending on Geany's API version (whether it is or not depends on the
821 design of the proxy). In this case do not do anything special but forward the API version the
822 sub-plugin is written/compiled against to GEANY_PLUGIN_REGISTER(). Here, Geany will perform its own
823 compatibility check, allowing for a consistent user feedback. The result is again that the
824 sub-plugin is hidden from the Plugin Manager, like in case 2. But Geany will print a debug message
825 so you can skip that.
828 If you have even more cases try to fit it into case 1 or 2, depending on whether other proxy
829 plugins should get a chance to load the candidate or not.
831 @section proxy_dep_guideline Guideline for Runtime Errors
833 A sub-plugin might not be able to run even if it's perfectly compatible with its proxy. This
834 includes the case when it lacks certain runtime dependencies such as programs or modules but also
835 syntactic problems or other errors.
837 There are two basic classes:
839 1) Runtime errors that can be determined at load time. For example, the shebang of a script
840 indicates a specific interpreter version but that version is not installed on the system. Your proxy
841 should respond the same way as for version-incompatible plugins: don't register the plugin at
842 all, but leave a message the user suggesting what has to be installed in order to work. Handle
843 syntax errors in the scripts of sub-plugins the same way if possible.
845 2) Runtime errors that cannot be determined without actually running the plugin. An example would
846 be missing modules in Python scripts. If your proxy has no way of foreseeing the problem the plugin
847 will be registered normally. However, you can catch runtime errors by implementing
848 GeanyPluginFuncs::init() on the plugin's behalf. This is called after user activation and allows to
849 indicate errors by returning @c FALSE. However, allowing the user to enable a plugin and then
850 disabling anyway is a poor user experience.
852 Therefore, if possible, try to fail fast and disallow registration.
854 @section Proxy Plugin Example
856 In this section a dumb example proxy plugin is shown in order to give a practical starting point.
857 The sub-plugin are not actually code but rather a ini-style description of one or more menu items
858 that are added to Geany's tools menu and a help dialog. Real world sub-plugins would contain actual
859 code, usually written in a scripting language.
861 A sub-plugin file looks like this:
863 @code{.ini}
864 #!!PROXY_MAGIC!!
866 [Init]
867 item0 = Bam
868 item1 = Foo
869 item2 = Bar
871 [Help]
872 text = I'm a simple test. Nothing to see!
874 [Info]
875 name = Demo Proxy Tester
876 description = I'm a simple test. Nothing to see!
877 version = 0.1
878 author = The Geany developer team
879 @endcode
881 The first line acts as a verification that this file is truly a sub-plugin. Within the [Init] section
882 there is the menu items for Geany's tools menu. The [Help] section declares the sub-plugins help
883 text which is shown in its help dialog (via GeanyPluginFuncs::help). The [Info] section is
884 used as-is for filling the sub-plugins PluginInfo fields.
886 That's it, this dumb format is purely declarative and contains no logic. Yet we will create plugins
887 from it.
889 We start by registering the proxy plugin to Geany. There is nothing special to it compared to
890 normal plugins. A proxy plugin must also fill its own @ref PluginInfo and @ref GeanyPluginFuncs,
891 followed by registering through GEANY_PLUGIN_REGISTER().
894 @code{.c}
896 /* Called by Geany to initialize the plugin. */
897 static gboolean demoproxy_init(GeanyPlugin *plugin, gpointer pdata)
899         // ...
903 /* Called by Geany before unloading the plugin. */
904 static void demoproxy_cleanup(GeanyPlugin *plugin, gpointer data)
906         // ...
910 G_MODULE_EXPORT
911 void geany_load_module(GeanyPlugin *plugin)
913         plugin->info->name = _("Demo Proxy");
914         plugin->info->description = _("Example Proxy.");
915         plugin->info->version = "0.1";
916         plugin->info->author = _("The Geany developer team");
918         plugin->funcs->init = demoproxy_init;
919         plugin->funcs->cleanup = demoproxy_cleanup;
921         GEANY_PLUGIN_REGISTER(plugin, 225);
924 @endcode
926 The next step is to actually register as a proxy plugin. This is done in demoproxy_init().
927 As previously mentioned, it needs a list of accepted file extensions and a set of callback
928 functions.
930 @code{.c}
931 static gboolean demoproxy_init(GeanyPlugin *plugin, gpointer pdata)
933         const gchar *extensions[] = { "ini", "px", NULL };
935         plugin->proxy_funcs->probe  = demoproxy_probe;
936         plugin->proxy_funcs->load   = demoproxy_load;
937         plugin->proxy_funcs->unload = demoproxy_unload;
939         return geany_plugin_register_proxy(plugin, extensions);
942 @endcode
944 The callback functions deserve a closer look.
946 As already mentioned the file format includes a magic first line which must be present.
947 GeanyProxyFuncs::probe() verifies that it's present and avoids showing the sub-plugin in the
948 Plugin Manager if not.
950 @code{.c}
951 static gint demoproxy_probe(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
953         /* We know the extension is right (Geany checks that). For demo purposes we perform an
954          * additional check. This is not necessary when the extension is unique enough. */
955         gboolean match = FALSE;
956         gchar linebuf[128];
957         FILE *f = fopen(filename, "r");
958         if (f != NULL)
959         {
960                 if (fgets(linebuf, sizeof(linebuf), f) != NULL)
961                         match = utils_str_equal(linebuf, "#!!PROXY_MAGIC!!\n");
962                 fclose(f);
963         }
964         return match ? GEANY_PROXY_MATCH : GEANY_PROXY_IGNORE;
966 @endcode
968 GeanyProxyFuncs::load is a bit more complex. It reads the file, fills the sub-plugin's PluginInfo
969 fields and calls GEANY_PLUGIN_REGISTER_FULL(). Additionally, it creates a per-plugin context that
970 holds GKeyFile instance (a poor man's interpreter context). You can also see that it does not call
971 GEANY_PLUGIN_REGISTER_FULL() if g_key_file_load_from_file() found an error (probably a syntax
972 problem) which means the sub-plugin cannot be enabled.
974 It also installs wrapper functions for the sub-plugin's GeanyPluginFuncs as ini files aren't code.
975 It's very likely that your proxy needs something similar because you can only install function
976 pointers to native code.
978 @code{.c}
979 typedef struct {
980         GKeyFile       *file;
981         gchar          *help_text;
982         GSList         *menu_items;
984 PluginContext;
987 static gboolean proxy_init(GeanyPlugin *plugin, gpointer pdata);
988 static void proxy_help(GeanyPlugin *plugin, gpointer pdata);
989 static void proxy_cleanup(GeanyPlugin *plugin, gpointer pdata);
992 static gpointer demoproxy_load(GeanyPlugin *proxy, GeanyPlugin *plugin,
993                                const gchar *filename, gpointer pdata)
995         GKeyFile *file;
996         gboolean result;
998         file = g_key_file_new();
999         result = g_key_file_load_from_file(file, filename, 0, NULL);
1001         if (result)
1002         {
1003                 PluginContext *data = g_new0(PluginContext, 1);
1004                 data->file = file;
1006                 plugin->info->name = g_key_file_get_locale_string(data->file, "Info", "name", NULL, NULL);
1007                 plugin->info->description = g_key_file_get_locale_string(data->file, "Info", "description", NULL, NULL);
1008                 plugin->info->version = g_key_file_get_locale_string(data->file, "Info", "version", NULL, NULL);
1009                 plugin->info->author = g_key_file_get_locale_string(data->file, "Info", "author", NULL, NULL);
1011                 plugin->funcs->init = proxy_init;
1012                 plugin->funcs->help = proxy_help;
1013                 plugin->funcs->cleanup = proxy_cleanup;
1015                 /* Cannot pass g_free as free_func be Geany calls it before unloading, and since
1016                  * demoproxy_unload() accesses the data this would be catastrophic */
1017                 GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, NULL);
1018                 return data;
1019         }
1021         g_key_file_free(file);
1022         return NULL;
1024 @endcode
1026 demoproxy_unload() simply releases all resources acquired in demoproxy_load(). It does not have to
1027 do anything else in for unloading.
1029 @code{.c}
1030 static void demoproxy_unload(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata)
1032         PluginContext *data = load_data;
1034         g_free((gchar *)plugin->info->name);
1035         g_free((gchar *)plugin->info->description);
1036         g_free((gchar *)plugin->info->version);
1037         g_free((gchar *)plugin->info->author);
1039         g_key_file_free(data->file);
1040         g_free(data);
1042 @endcode
1044 Finally the demo_proxy's wrapper GeanyPluginFuncs. They are called for each possible sub-plugin and
1045 therefore have to multiplex between each using the plugin-defined data pointer. Each is called by
1046 Geany as if it were an ordinary, native plugin.
1048 proxy_init() actually reads the sub-plugin's file using GKeyFile APIs. It prepares for the help
1049 dialog and installs the menu items. proxy_help() is called when the user clicks the help button in
1050 the Plugin Manager. Consequently, this fires up a suitable dialog, although with a dummy message.
1051 proxy_cleanup() frees all memory allocated in proxy_init().
1053 @code{.c}
1054 static gboolean proxy_init(GeanyPlugin *plugin, gpointer pdata)
1056         PluginContext *data;
1057         gint i = 0;
1058         gchar *text;
1060         data = (PluginContext *) pdata;
1062         /* Normally, you would instruct the VM/interpreter to call into the actual plugin. The
1063          * plugin would be identified by pdata. Because there is no interpreter for
1064          * .ini files we do it inline, as this is just a demo */
1065         data->help_text = g_key_file_get_locale_string(data->file, "Help", "text", NULL, NULL);
1066         while (TRUE)
1067         {
1068                 GtkWidget *item;
1069                 gchar *key = g_strdup_printf("item%d", i++);
1070                 text = g_key_file_get_locale_string(data->file, "Init", key, NULL, NULL);
1071                 g_free(key);
1073                 if (!text)
1074                         break;
1076                 item = gtk_menu_item_new_with_label(text);
1077                 gtk_widget_show(item);
1078                 gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu), item);
1079                 gtk_widget_set_sensitive(item, FALSE);
1080                 data->menu_items = g_slist_prepend(data->menu_items, (gpointer) item);
1081                 g_free(text);
1082         }
1084         return TRUE;
1088 static void proxy_help(GeanyPlugin *plugin, gpointer pdata)
1090         PluginContext *data;
1091         GtkWidget *dialog;
1093         data = (PluginContext *) pdata;
1095         dialog = gtk_message_dialog_new(
1096                 GTK_WINDOW(plugin->geany_data->main_widgets->window),
1097                 GTK_DIALOG_DESTROY_WITH_PARENT,
1098                 GTK_MESSAGE_INFO,
1099                 GTK_BUTTONS_OK,
1100                 "%s", data->help_text);
1101         gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
1102                 _("(From the %s plugin)"), plugin->info->name);
1104         gtk_dialog_run(GTK_DIALOG(dialog));
1105         gtk_widget_destroy(dialog);
1109 static void proxy_cleanup(GeanyPlugin *plugin, gpointer pdata)
1111         PluginContext *data = (PluginContext *) pdata;
1113         g_slist_free_full(data->menu_items, (GDestroyNotify) gtk_widget_destroy);
1114         g_free(data->help_text);
1116 @endcode