geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

plugins.c
Go to the documentation of this file.
1/*
2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2007 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
21/* Code to manage, load and unload plugins. */
22
23#ifdef HAVE_CONFIG_H
24# include "config.h"
25#endif
26
27#ifdef HAVE_PLUGINS
28
29#include "plugins.h"
30
31#include "app.h"
32#include "dialogs.h"
33#include "documentprivate.h"
34#include "encodings.h"
35#include "geanyobject.h"
36#include "geanywraplabel.h"
37#include "highlighting.h"
38#include "keybindingsprivate.h"
39#include "keyfile.h"
40#include "main.h"
41#include "msgwindow.h"
42#include "navqueue.h"
43#include "plugindata.h"
44#include "pluginprivate.h"
45#include "pluginutils.h"
46#include "prefs.h"
47#include "sciwrappers.h"
48#include "stash.h"
49#include "support.h"
50#include "symbols.h"
51#include "templates.h"
52#include "toolbar.h"
53#include "ui_utils.h"
54#include "utils.h"
55#include "win32.h"
56
57#include <gtk/gtk.h>
58#include <string.h>
59
60
61typedef struct
62{
63 gchar *prefix;
65}
67
68
69GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
70
71
72static gboolean want_plugins = FALSE;
73
74/* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
75 * opened, afterwards it will be destroyed */
76static GList *plugin_list = NULL;
77static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
78static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
79
80static GtkWidget *menu_separator = NULL;
81
82static gchar *get_plugin_path(void);
83static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
84
85typedef struct {
86 gchar extension[8];
87 Plugin *plugin; /* &builtin_so_proxy_plugin for native plugins */
89
90
91static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, const gchar *filename, gpointer pdata);
92static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata);
93
95 .proxy_cbs = {
97 .unload = plugin_unload_gmodule,
98 },
99 /* rest of Plugin can be NULL/0 */
100};
101
103 .extension = G_MODULE_SUFFIX,
104 .plugin = &builtin_so_proxy_plugin,
105};
106
107static GQueue active_proxies = G_QUEUE_INIT;
108
109static void plugin_free(Plugin *plugin);
110
112
113static void
115{
116 GeanyData gd = {
117 app,
121 &prefs,
125 &file_prefs,
127 &tool_prefs,
129 NULL, /* Remove field on next ABI break (abi-todo) */
132 };
133
134 geany_data = gd;
135}
136
137
138/* In order to have nested proxies work the count of dependent plugins must propagate up.
139 * This prevents that any plugin in the tree is unloaded while a leaf plugin is active. */
140static void proxied_count_inc(Plugin *proxy)
141{
142 do
143 {
144 proxy->proxied_count += 1;
145 proxy = proxy->proxy;
146 } while (proxy != NULL);
147}
148
149
150static void proxied_count_dec(Plugin *proxy)
151{
152 g_warn_if_fail(proxy->proxied_count > 0);
153
154 do
155 {
156 proxy->proxied_count -= 1;
157 proxy = proxy->proxy;
158 } while (proxy != NULL);
159}
160
161
162/* Prevent the same plugin filename being loaded more than once.
163 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
164static gboolean
166{
167 gchar *basename_module, *basename_loaded;
168 GList *item;
169
170 basename_module = g_path_get_basename(plugin->filename);
171 for (item = plugin_list; item != NULL; item = g_list_next(item))
172 {
173 basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
174
175 if (utils_str_equal(basename_module, basename_loaded))
176 {
177 g_free(basename_loaded);
178 g_free(basename_module);
179 return TRUE;
180 }
181 g_free(basename_loaded);
182 }
183 /* Look also through the list of active plugins. This prevents problems when we have the same
184 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
185 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
186 * the plugin manager would list both plugins. Additionally, unloading the active plugin
187 * would cause a crash. */
188 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
189 {
190 basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
191
192 if (utils_str_equal(basename_module, basename_loaded))
193 {
194 g_free(basename_loaded);
195 g_free(basename_module);
196 return TRUE;
197 }
198 g_free(basename_loaded);
199 }
200 g_free(basename_module);
201 return FALSE;
202}
203
204
206{
207 GList *item;
208
209 g_return_val_if_fail(filename, FALSE);
210
211 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
212 {
213 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
214 return item->data;
215 }
216
217 return NULL;
218}
219
220
221/* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
222#define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
223
224static gboolean
225plugin_check_version(Plugin *plugin, int plugin_version_code)
226{
227 gboolean ret = TRUE;
228 if (plugin_version_code < 0)
229 {
230 gchar *name = g_path_get_basename(plugin->filename);
231 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
232 "release of Geany - please recompile it."), name);
233 geany_debug("Plugin \"%s\" is not binary compatible with this "
234 "release of Geany - recompile it.", name);
235 ret = FALSE;
236 g_free(name);
237 }
238 else if (plugin_version_code > GEANY_API_VERSION)
239 {
240 gchar *name = g_path_get_basename(plugin->filename);
241 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
242 name, plugin_version_code);
243 ret = FALSE;
244 g_free(name);
245 }
246
247 return ret;
248}
249
250
251static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
252{
253 PluginCallback *cb;
254 guint i, len = 0;
255
256 while (TRUE)
257 {
258 cb = &callbacks[len];
259 if (!cb->signal_name || !cb->callback)
260 break;
261 len++;
262 }
263 if (len == 0)
264 return;
265
266 for (i = 0; i < len; i++)
267 {
268 cb = &callbacks[i];
269
270 /* Pass the callback data as default user_data if none was set by the plugin itself */
272 cb->callback, cb->user_data ? cb->user_data : plugin->cb_data);
273 }
274}
275
276
277static void read_key_group(Plugin *plugin)
278{
279 GeanyKeyGroupInfo *p_key_info;
280 GeanyKeyGroup **p_key_group;
281 GModule *module = plugin->proxy_data;
282
283 g_module_symbol(module, "plugin_key_group_info", (void *) &p_key_info);
284 g_module_symbol(module, "plugin_key_group", (void *) &p_key_group);
285 if (p_key_info && p_key_group)
286 {
287 GeanyKeyGroupInfo *key_info = p_key_info;
288
289 if (*p_key_group)
290 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
291 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
292 plugin->info.name);
293 else
294 {
295 if (key_info->count)
296 {
297 GeanyKeyGroup *key_group =
298 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
299 if (key_group)
300 *p_key_group = key_group;
301 }
302 else
303 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
304 "count field is zero. Maybe use plugin_set_key_group() instead?",
305 plugin->info.name);
306 }
307 }
308 else if (p_key_info || p_key_group)
309 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
310 "Maybe use plugin_set_key_group() instead?",
311 plugin->info.name);
312}
313
314
315static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
316{
317 const Plugin *pa = a;
318 const Plugin *pb = b;
319
320 return strcmp(pa->info.name, pb->info.name);
321}
322
323
324/** Register a plugin to Geany.
325 *
326 * The plugin will show up in the plugin manager. The user can interact with
327 * it based on the functions it provides and installed GUI elements.
328 *
329 * You must initialize the info and funcs fields of @ref GeanyPlugin
330 * appropriately prior to calling this, otherwise registration will fail. For
331 * info at least a valid name must be set (possibly localized). For funcs,
332 * at least init() and cleanup() functions must be implemented and set.
333 *
334 * The return value must be checked. It may be FALSE if the plugin failed to register which can
335 * mainly happen for two reasons (future Geany versions may add new failure conditions):
336 * - Not all mandatory fields of GeanyPlugin have been set.
337 * - The ABI or API versions reported by the plugin are incompatible with the running Geany.
338 *
339 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
340 * handles @a api_version and @a abi_version.
341 *
342 * @param plugin The plugin provided by Geany
343 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
344 * @param min_api_version The minimum API version required by the plugin
345 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
346 *
347 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
348 *
349 * @since 1.26 (API 225)
350 * @see GEANY_PLUGIN_REGISTER()
351 **/
352GEANY_API_SYMBOL
353gboolean geany_plugin_register(GeanyPlugin *plugin, gint api_version, gint min_api_version,
354 gint abi_version)
355{
356 Plugin *p;
357 GeanyPluginFuncs *cbs = plugin->funcs;
358
359 g_return_val_if_fail(plugin != NULL, FALSE);
360
361 p = plugin->priv;
362 /* already registered successfully */
363 g_return_val_if_fail(!PLUGIN_LOADED_OK(p), FALSE);
364
365 /* Prevent registering incompatible plugins. */
366 if (! plugin_check_version(p, PLUGIN_VERSION_CODE(api_version, abi_version)))
367 return FALSE;
368
369 /* Only init and cleanup callbacks are truly mandatory. */
370 if (! cbs->init || ! cbs->cleanup)
371 {
372 gchar *name = g_path_get_basename(p->filename);
373 geany_debug("Plugin '%s' has no %s function - ignoring plugin!", name,
374 cbs->init ? "cleanup" : "init");
375 g_free(name);
376 }
377 else
378 {
379 /* Yes, name is checked again later on, however we want return FALSE here
380 * to signal the error back to the plugin (but we don't print the message twice) */
381 if (! EMPTY(p->info.name))
382 p->flags = LOADED_OK;
383 }
384
385 /* If it ever becomes necessary we can save the api version in Plugin
386 * and apply compat code on a per-plugin basis, because we learn about
387 * the requested API version here. For now it's not necessary. */
388
389 return PLUGIN_LOADED_OK(p);
390}
391
392
393/** Register a plugin to Geany, with plugin-defined data.
394 *
395 * This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
396 * Refer to that function for more details on registering in general.
397 *
398 * @p pdata is the pointer going to be passed to the individual plugin callbacks
399 * of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
400 * @p pdata, which connects the data to the plugin's module life time.
401 *
402 * You cannot use geany_plugin_set_data() after registering with this function. Use
403 * geany_plugin_register() if you need to.
404 *
405 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
406 * handles @p api_version and @p abi_version.
407 *
408 * @param plugin The plugin provided by Geany.
409 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
410 * @param min_api_version The minimum API version required by the plugin.
411 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
412 * @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
413 * @param free_func Function used to deallocate @a pdata, may be @c NULL.
414 *
415 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
416 *
417 * @since 1.26 (API 225)
418 * @see GEANY_PLUGIN_REGISTER_FULL()
419 * @see geany_plugin_register()
420 **/
421GEANY_API_SYMBOL
422gboolean geany_plugin_register_full(GeanyPlugin *plugin, gint api_version, gint min_api_version,
423 gint abi_version, gpointer pdata, GDestroyNotify free_func)
424{
425 if (geany_plugin_register(plugin, api_version, min_api_version, abi_version))
426 {
427 geany_plugin_set_data(plugin, pdata, free_func);
428 /* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
429 * as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
430 * after GeanyPluginFuncs::cleanup() */
431 plugin->priv->flags |= LOAD_DATA;
432 return TRUE;
433 }
434 return FALSE;
435}
436
438{
439 void (*init) (GeanyData *data);
440 GtkWidget* (*configure) (GtkDialog *dialog);
441 void (*help) (void);
442 void (*cleanup) (void);
443};
444
445/* Wrappers to support legacy plugins are below */
446static gboolean legacy_init(GeanyPlugin *plugin, gpointer pdata)
447{
448 struct LegacyRealFuncs *h = pdata;
449 h->init(plugin->geany_data);
450 return TRUE;
451}
452
453static void legacy_cleanup(GeanyPlugin *plugin, gpointer pdata)
454{
455 struct LegacyRealFuncs *h = pdata;
456 /* Can be NULL because it's optional for legacy plugins */
457 if (h->cleanup)
458 h->cleanup();
459}
460
461static void legacy_help(GeanyPlugin *plugin, gpointer pdata)
462{
463 struct LegacyRealFuncs *h = pdata;
464 h->help();
465}
466
467static GtkWidget *legacy_configure(GeanyPlugin *plugin, GtkDialog *parent, gpointer pdata)
468{
469 struct LegacyRealFuncs *h = pdata;
470 return h->configure(parent);
471}
472
473static void free_legacy_cbs(gpointer data)
474{
475 g_slice_free(struct LegacyRealFuncs, data);
476}
477
478/* This function is the equivalent of geany_plugin_register() for legacy-style
479 * plugins which we continue to load for the time being. */
480static void register_legacy_plugin(Plugin *plugin, GModule *module)
481{
482 gint (*p_version_check) (gint abi_version);
483 void (*p_set_info) (PluginInfo *info);
484 void (*p_init) (GeanyData *geany_data);
485 GeanyData **p_geany_data;
486 struct LegacyRealFuncs *h;
487
488#define CHECK_FUNC(__x) \
489 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
490 { \
491 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
492 g_module_name(module)); \
493 return; \
494 }
495 CHECK_FUNC(version_check);
496 CHECK_FUNC(set_info);
498#undef CHECK_FUNC
499
500 /* We must verify the version first. If the plugin has become incompatible any
501 * further actions should be considered invalid and therefore skipped. */
502 if (! plugin_check_version(plugin, p_version_check(GEANY_ABI_VERSION)))
503 return;
504
505 h = g_slice_new(struct LegacyRealFuncs);
506
507 /* Since the version check passed we can proceed with setting basic fields and
508 * calling its set_info() (which might want to call Geany functions already). */
509 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
510 if (p_geany_data)
511 *p_geany_data = &geany_data;
512 /* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
513 p_set_info(&plugin->info);
514
515 /* If all went well we can set the remaining callbacks and let it go for good. */
516 h->init = p_init;
517 g_module_symbol(module, "plugin_configure", (void *) &h->configure);
518 g_module_symbol(module, "plugin_configure_single", (void *) &plugin->configure_single);
519 g_module_symbol(module, "plugin_help", (void *) &h->help);
520 g_module_symbol(module, "plugin_cleanup", (void *) &h->cleanup);
521 /* pointer to callbacks struct can be stored directly, no wrapper necessary */
522 g_module_symbol(module, "plugin_callbacks", (void *) &plugin->cbs.callbacks);
523 if (app->debug_mode)
524 {
525 if (h->configure && plugin->configure_single)
526 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
527 "only plugin_configure() will be used!",
528 plugin->info.name);
529 if (h->cleanup == NULL)
530 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
531 plugin->info.name);
532 }
533
534 plugin->cbs.init = legacy_init;
535 plugin->cbs.cleanup = legacy_cleanup;
537 plugin->cbs.help = h->help ? legacy_help : NULL;
538
539 plugin->flags = LOADED_OK | IS_LEGACY;
541}
542
543
544static gboolean
546{
547 gboolean init_ok = TRUE;
548
549 /* Start the plugin. Legacy plugins require additional cruft. */
550 if (PLUGIN_IS_LEGACY(plugin) && plugin->proxy == &builtin_so_proxy_plugin)
551 {
552 GeanyPlugin **p_geany_plugin;
553 PluginInfo **p_info;
555 GModule *module = plugin->proxy_data;
556 /* set these symbols before plugin_init() is called
557 * we don't set geany_data since it is set directly by plugin_new() */
558 g_module_symbol(module, "geany_plugin", (void *) &p_geany_plugin);
559 if (p_geany_plugin)
560 *p_geany_plugin = &plugin->public;
561 g_module_symbol(module, "plugin_info", (void *) &p_info);
562 if (p_info)
563 *p_info = &plugin->info;
564 g_module_symbol(module, "plugin_fields", (void *) &plugin_fields);
565 if (plugin_fields)
566 *plugin_fields = &plugin->fields;
567 read_key_group(plugin);
568
569 /* Legacy plugin_init() cannot fail. */
570 plugin->cbs.init(&plugin->public, plugin->cb_data);
571
572 /* now read any plugin-owned data that might have been set in plugin_init() */
574 {
576 }
577 }
578 else
579 {
580 init_ok = plugin->cbs.init(&plugin->public, plugin->cb_data);
581 }
582
583 if (! init_ok)
584 return FALSE;
585
586 /* new-style plugins set their callbacks in geany_load_module() */
587 if (plugin->cbs.callbacks)
588 add_callbacks(plugin, plugin->cbs.callbacks);
589
590 /* remember which plugins are active.
591 * keep list sorted so tools menu items and plugin preference tabs are
592 * sorted by plugin name */
593 active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
594 proxied_count_inc(plugin->proxy);
595
596 geany_debug("Loaded: %s (%s)", plugin->filename, plugin->info.name);
597 return TRUE;
598}
599
600
601static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, const gchar *fname, gpointer pdata)
602{
603 GModule *module;
604 void (*p_geany_load_module)(GeanyPlugin *);
605
606 g_return_val_if_fail(g_module_supported(), NULL);
607 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
608 * causing a segfault. Without that flag the module will safely fail to load.
609 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
610 * otherwise not be detected due to the shadowing of Geany's app variable.
611 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
612 * function from a plugin will be shadowed. */
613 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
614 if (!module)
615 {
616 geany_debug("Can't load plugin: %s", g_module_error());
617 return NULL;
618 }
619
620 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
621 g_module_symbol(module, "geany_load_module", (void *) &p_geany_load_module);
622 if (p_geany_load_module)
623 {
624 /* set this here already so plugins can call i.e. plugin_module_make_resident()
625 * right from their geany_load_module() */
626 subplugin->priv->proxy_data = module;
627
628 /* This is a new style plugin. It should fill in plugin->info and then call
629 * geany_plugin_register() in its geany_load_module() to successfully load.
630 * The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
631 * We check the LOADED_OK flag separately to protect us against buggy plugins
632 * who ignore the result of geany_plugin_register() and register anyway */
633 p_geany_load_module(subplugin);
634 }
635 else
636 {
637 /* This is the legacy / deprecated code path. It does roughly the same as
638 * geany_load_module() and geany_plugin_register() together for the new ones */
639 register_legacy_plugin(subplugin->priv, module);
640 }
641 /* We actually check the LOADED_OK flag later */
642 return module;
643}
644
645
646static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, gpointer load_data, gpointer pdata)
647{
648 GModule *module = (GModule *) load_data;
649
650 g_return_if_fail(module != NULL);
651
652 if (! g_module_close(module))
653 g_warning("%s: %s", subplugin->priv->filename, g_module_error());
654}
655
656
657/* Load and optionally init a plugin.
658 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
659 * called, the plugin will be started, if not the plugin will be read only (for the list of
660 * available plugins in the plugin manager).
661 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
662static Plugin*
663plugin_new(Plugin *proxy, const gchar *fname, gboolean load_plugin, gboolean add_to_list)
664{
665 Plugin *plugin;
666
667 g_return_val_if_fail(fname, NULL);
668 g_return_val_if_fail(proxy, NULL);
669
670 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
671 * load the module */
672 plugin = find_active_plugin_by_name(fname);
673 if (plugin != NULL)
674 {
675 geany_debug("Plugin \"%s\" already loaded.", fname);
676 if (add_to_list)
677 {
678 /* do not add to the list twice */
679 if (g_list_find(plugin_list, plugin) != NULL)
680 return NULL;
681
682 plugin_list = g_list_prepend(plugin_list, plugin);
683 }
684 return plugin;
685 }
686
687 plugin = g_new0(Plugin, 1);
688 plugin->filename = g_strdup(fname);
689 plugin->proxy = proxy;
690 plugin->public.geany_data = &geany_data;
691 plugin->public.priv = plugin;
692 /* Fields of plugin->info/funcs must to be initialized by the plugin */
693 plugin->public.info = &plugin->info;
694 plugin->public.funcs = &plugin->cbs;
695 plugin->public.proxy_funcs = &plugin->proxy_cbs;
696
697 if (plugin_loaded(plugin))
698 {
699 geany_debug("Plugin \"%s\" already loaded.", fname);
700 goto err;
701 }
702
703 /* Load plugin, this should read its name etc. It must also call
704 * geany_plugin_register() for the following PLUGIN_LOADED_OK condition */
705 plugin->proxy_data = proxy->proxy_cbs.load(&proxy->public, &plugin->public, fname, proxy->cb_data);
706
707 if (! PLUGIN_LOADED_OK(plugin))
708 {
709 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname);
710 goto err;
711 }
712
713 /* The proxy assumes success, therefore we have to call unload from here
714 * on in case of errors */
715 if (EMPTY(plugin->info.name))
716 {
717 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname);
718 goto err_unload;
719 }
720
721 /* cb_data_destroy() frees plugin->cb_data. If that pointer also passed to unload() afterwards
722 * then that would become a use-after-free. Disallow this combination. If a proxy
723 * needs the same pointer it must not use a destroy func but free manually in its unload(). */
724 if (plugin->proxy_data == proxy->cb_data && plugin->cb_data_destroy)
725 {
726 geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname);
727 plugin->proxy_data = NULL;
728 goto err_unload;
729 }
730
731 if (load_plugin && !plugin_load(plugin))
732 {
733 /* Handle failing init same as failing to load for now. In future we
734 * could present a informational UI or something */
735 geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname);
736 goto err_unload;
737 }
738
739 if (add_to_list)
740 plugin_list = g_list_prepend(plugin_list, plugin);
741
742 return plugin;
743
744err_unload:
745 if (plugin->cb_data_destroy)
746 plugin->cb_data_destroy(plugin->cb_data);
747 proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
748err:
749 g_free(plugin->filename);
750 g_free(plugin);
751 return NULL;
752}
753
754
755static void on_object_weak_notify(gpointer data, GObject *old_ptr)
756{
757 Plugin *plugin = data;
758 guint i = 0;
759
760 g_return_if_fail(plugin && plugin->signal_ids);
761
762 for (i = 0; i < plugin->signal_ids->len; i++)
763 {
764 SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
765
766 if (sc->object == old_ptr)
767 {
768 g_array_remove_index_fast(plugin->signal_ids, i);
769 /* we can break the loop right after finding the first match,
770 * because we will get one notification per connected signal */
771 break;
772 }
773 }
774}
775
776
777/* add an object to watch for destruction, and release pointers to it when destroyed.
778 * this should only be used by plugin_signal_connect() to add a watch on
779 * the object lifetime and nuke out references to it in plugin->signal_ids */
780void plugin_watch_object(Plugin *plugin, gpointer object)
781{
782 g_object_weak_ref(object, on_object_weak_notify, plugin);
783}
784
785
786static void remove_callbacks(Plugin *plugin)
787{
788 GArray *signal_ids = plugin->signal_ids;
790
791 if (signal_ids == NULL)
792 return;
793
794 foreach_array(SignalConnection, sc, signal_ids)
795 {
796 g_signal_handler_disconnect(sc->object, sc->handler_id);
797 g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
798 }
799
800 g_array_free(signal_ids, TRUE);
801}
802
803
804static void remove_sources(Plugin *plugin)
805{
806 GList *item;
807
808 item = plugin->sources;
809 while (item != NULL)
810 {
811 GList *next = item->next; /* cache the next pointer because current item will be freed */
812
813 g_source_destroy(item->data);
814 item = next;
815 }
816 /* don't free the list here, it is allocated inside each source's data */
817}
818
819
820/* Make the GModule backing plugin resident (if it's GModule-backed at all) */
822{
823 if (plugin->proxy == &builtin_so_proxy_plugin)
824 {
825 g_return_if_fail(plugin->proxy_data != NULL);
826 g_module_make_resident(plugin->proxy_data);
827 }
828 else
829 g_warning("Skipping g_module_make_resident() for non-native plugin");
830}
831
832
833/* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
834gpointer plugin_get_module_symbol(Plugin *plugin, const gchar *sym)
835{
836 gpointer symbol;
837
838 if (plugin->proxy == &builtin_so_proxy_plugin)
839 {
840 g_return_val_if_fail(plugin->proxy_data != NULL, NULL);
841 if (g_module_symbol(plugin->proxy_data, sym, &symbol))
842 return symbol;
843 else
844 g_warning("Failed to locate signal handler for '%s': %s",
845 sym, g_module_error());
846 }
847 else /* TODO: Could possibly support this via a new proxy hook */
848 g_warning("Failed to locate signal handler for '%s': Not supported for non-native plugins",
849 sym);
850 return NULL;
851}
852
853
854static gboolean is_active_plugin(Plugin *plugin)
855{
856 return (g_list_find(active_plugin_list, plugin) != NULL);
857}
858
859
860static void remove_each_doc_data(GQuark key_id, gpointer data, gpointer user_data)
861{
862 const ForEachDocData *doc_data = user_data;
863 const gchar *key = g_quark_to_string(key_id);
864 if (g_str_has_prefix(key, doc_data->prefix))
865 g_datalist_remove_data(&doc_data->document->priv->data, key);
866}
867
868
869static void remove_doc_data(Plugin *plugin)
870{
871 ForEachDocData data;
872
873 data.prefix = g_strdup_printf("geany/plugins/%s/", plugin->public.info->name);
874
875 for (guint i = 0; i < documents_array->len; i++)
876 {
877 GeanyDocument *doc = documents_array->pdata[i];
878 if (DOC_VALID(doc))
879 {
880 data.document = doc;
881 g_datalist_foreach(&doc->priv->data, remove_each_doc_data, &data);
882 }
883 }
884
885 g_free(data.prefix);
886}
887
888
889/* Clean up anything used by an active plugin */
890static void
892{
893 GtkWidget *widget;
894
895 /* With geany_register_plugin cleanup is mandatory */
896 plugin->cbs.cleanup(&plugin->public, plugin->cb_data);
897
898 remove_doc_data(plugin);
899 remove_callbacks(plugin);
900 remove_sources(plugin);
901
902 if (plugin->key_group)
904
905 widget = plugin->toolbar_separator.widget;
906 if (widget)
907 gtk_widget_destroy(widget);
908
909 if (!PLUGIN_HAS_LOAD_DATA(plugin) && plugin->cb_data_destroy)
910 {
911 /* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
912 * if it was already set through geany_plugin_register_full() because we couldn't call
913 * its init() anymore (not without completely reloading it anyway). */
914 plugin->cb_data_destroy(plugin->cb_data);
915 plugin->cb_data = NULL;
916 plugin->cb_data_destroy = NULL;
917 }
918
919 proxied_count_dec(plugin->proxy);
920 geany_debug("Unloaded: %s", plugin->filename);
921}
922
923
924/* Remove all plugins that proxy is a proxy for from plugin_list (and free) */
925static void free_subplugins(Plugin *proxy)
926{
927 GList *item;
928
929 item = plugin_list;
930 while (item)
931 {
932 GList *next = g_list_next(item);
933 if (proxy == ((Plugin *) item->data)->proxy)
934 {
935 /* plugin_free modifies plugin_list */
936 plugin_free((Plugin *) item->data);
937 }
938 item = next;
939 }
940}
941
942
943/* Returns true if the removal was successful (=> never for non-proxies) */
944static gboolean unregister_proxy(Plugin *proxy)
945{
946 gboolean is_proxy = FALSE;
947 GList *node;
948
949 /* Remove the proxy from the proxy list first. It might appear more than once (once
950 * for each extension), but if it doesn't appear at all it's not actually a proxy */
952 {
953 PluginProxy *p = node->data;
954 if (p->plugin == proxy)
955 {
956 is_proxy = TRUE;
957 g_queue_delete_link(&active_proxies, node);
958 }
959 }
960 return is_proxy;
961}
962
963
964/* Cleanup a plugin and free all resources allocated on behalf of it.
965 *
966 * If the plugin is a proxy then this also takes special care to unload all
967 * subplugin loaded through it (make sure none of them is active!) */
968static void
970{
971 Plugin *proxy;
972
973 g_return_if_fail(plugin);
974 g_return_if_fail(plugin->proxy);
975 g_return_if_fail(plugin->proxied_count == 0);
976
977 proxy = plugin->proxy;
978 /* If this a proxy remove all depending subplugins. We can assume none of them is *activated*
979 * (but potentially loaded). Note that free_subplugins() might call us through recursion */
980 if (is_active_plugin(plugin))
981 {
982 if (unregister_proxy(plugin))
983 free_subplugins(plugin);
984 plugin_cleanup(plugin);
985 }
986
987 active_plugin_list = g_list_remove(active_plugin_list, plugin);
988 plugin_list = g_list_remove(plugin_list, plugin);
989
990 /* cb_data_destroy might be plugin code and must be called before unloading the module. */
991 if (plugin->cb_data_destroy)
992 plugin->cb_data_destroy(plugin->cb_data);
993 proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
994
995 g_free(plugin->filename);
996 g_free(plugin);
997}
998
999
1000static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
1001 const gchar *plugin_path_system)
1002{
1003 gchar *plugin_path_custom;
1004
1006 return NULL;
1007
1009 utils_tidy_path(plugin_path_custom);
1010
1011 /* check whether the custom plugin path is one of the system or user plugin paths
1012 * and abort if so */
1013 if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
1014 utils_str_equal(plugin_path_custom, plugin_path_system))
1015 {
1016 g_free(plugin_path_custom);
1017 return NULL;
1018 }
1019 return plugin_path_custom;
1020}
1021
1022
1023/* all 3 paths Geany looks for plugins in can change (even system path on Windows)
1024 * so we need to check active plugins are in the right place before loading */
1025static gboolean check_plugin_path(const gchar *fname)
1026{
1027 gchar *plugin_path_config;
1028 gchar *plugin_path_system;
1029 gchar *plugin_path_custom;
1030 gboolean ret = FALSE;
1031
1032 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
1033 if (g_str_has_prefix(fname, plugin_path_config))
1034 ret = TRUE;
1035
1036 plugin_path_system = get_plugin_path();
1037 if (g_str_has_prefix(fname, plugin_path_system))
1038 ret = TRUE;
1039
1040 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
1041 if (plugin_path_custom)
1042 {
1043 if (g_str_has_prefix(fname, plugin_path_custom))
1044 ret = TRUE;
1045
1046 g_free(plugin_path_custom);
1047 }
1048 g_free(plugin_path_config);
1049 g_free(plugin_path_system);
1050 return ret;
1051}
1052
1053
1054/* Returns NULL if this ain't a plugin,
1055 * otherwise it returns the appropriate PluginProxy instance to load it */
1056static PluginProxy* is_plugin(const gchar *file)
1057{
1058 GList *node;
1059 const gchar *ext;
1060
1061 /* extract file extension to avoid g_str_has_suffix() in the loop */
1062 ext = (const gchar *)strrchr(file, '.');
1063 if (ext == NULL)
1064 return FALSE;
1065 /* ensure the dot is really part of the filename */
1066 else if (strchr(ext, G_DIR_SEPARATOR) != NULL)
1067 return FALSE;
1068
1069 ext += 1;
1070 /* O(n*m), (m being extensions per proxy) doesn't scale very well in theory
1071 * but not a problem in practice yet */
1072 foreach_list(node, active_proxies.head)
1073 {
1074 PluginProxy *proxy = node->data;
1075 if (utils_str_casecmp(ext, proxy->extension) == 0)
1076 {
1077 Plugin *p = proxy->plugin;
1078 gint ret = GEANY_PROXY_MATCH;
1079
1080 if (p->proxy_cbs.probe)
1081 ret = p->proxy_cbs.probe(&p->public, file, p->cb_data);
1082 switch (ret)
1083 {
1084 case GEANY_PROXY_MATCH:
1085 return proxy;
1087 return NULL;
1088 case GEANY_PROXY_IGNORE:
1089 continue;
1090 default:
1091 g_warning("Ignoring bogus return value '%d' from "
1092 "proxy plugin '%s' probe() function!", ret,
1093 proxy->plugin->info.name);
1094 continue;
1095 }
1096 }
1097 }
1098 return NULL;
1099}
1100
1101
1102/* load active plugins at startup */
1103static void
1105{
1106 guint i, len, proxies;
1107
1108 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
1109 return;
1110
1111 /* If proxys are loaded we have to restart to load plugins that sort before their proxy */
1112 do
1113 {
1114 proxies = active_proxies.length;
1115 g_list_free_full(failed_plugins_list, (GDestroyNotify) g_free);
1117 for (i = 0; i < len; i++)
1118 {
1119 gchar *fname = active_plugins_pref[i];
1120
1121#ifdef G_OS_WIN32
1122 /* ensure we have canonical paths */
1123 gchar *p = fname;
1124 while ((p = strchr(p, '/')) != NULL)
1125 *p = G_DIR_SEPARATOR;
1126#endif
1127
1128 if (!EMPTY(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
1129 {
1130 PluginProxy *proxy = NULL;
1131 if (check_plugin_path(fname))
1132 proxy = is_plugin(fname);
1133 if (proxy == NULL || plugin_new(proxy->plugin, fname, TRUE, FALSE) == NULL)
1134 failed_plugins_list = g_list_prepend(failed_plugins_list, g_strdup(fname));
1135 }
1136 }
1137 } while (proxies != active_proxies.length);
1138}
1139
1140
1141static void
1142load_plugins_from_path(const gchar *path)
1143{
1144 GSList *list, *item;
1145 gint count = 0;
1146
1148
1149 for (item = list; item != NULL; item = g_slist_next(item))
1150 {
1151 gchar *fname = g_build_filename(path, item->data, NULL);
1152 PluginProxy *proxy = is_plugin(fname);
1153
1154 if (proxy != NULL && plugin_new(proxy->plugin, fname, FALSE, TRUE))
1155 count++;
1156
1157 g_free(fname);
1158 }
1159
1160 g_slist_foreach(list, (GFunc) g_free, NULL);
1161 g_slist_free(list);
1162
1163 if (count)
1164 geany_debug("Added %d plugin(s) in '%s'.", count, path);
1165}
1166
1167
1168static gchar *get_plugin_path(void)
1169{
1170 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN));
1171}
1172
1173
1174/* See load_all_plugins(), this simply sorts items with lower hierarchy level first
1175 * (where hierarchy level == number of intermediate proxies before the builtin so loader) */
1176static gint cmp_plugin_by_proxy(gconstpointer a, gconstpointer b)
1177{
1178 const Plugin *pa = a;
1179 const Plugin *pb = b;
1180
1181 while (TRUE)
1182 {
1183 if (pa->proxy == pb->proxy)
1184 return 0;
1185 else if (pa->proxy == &builtin_so_proxy_plugin)
1186 return -1;
1187 else if (pb->proxy == &builtin_so_proxy_plugin)
1188 return 1;
1189
1190 pa = pa->proxy;
1191 pb = pb->proxy;
1192 }
1193}
1194
1195
1196/* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1197static void load_all_plugins(void)
1198{
1199 gchar *plugin_path_config;
1200 gchar *plugin_path_system;
1201 gchar *plugin_path_custom;
1202
1203 plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
1204 plugin_path_system = get_plugin_path();
1205
1206 /* first load plugins in ~/.config/geany/plugins/ */
1207 load_plugins_from_path(plugin_path_config);
1208
1209 /* load plugins from a custom path */
1210 plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
1211 if (plugin_path_custom)
1212 {
1213 load_plugins_from_path(plugin_path_custom);
1214 g_free(plugin_path_custom);
1215 }
1216
1217 /* finally load plugins from $prefix/lib/geany */
1218 load_plugins_from_path(plugin_path_system);
1219
1220 /* It is important to sort any plugins that are proxied after their proxy because
1221 * pm_populate() needs the proxy to be loaded and active (if selected by user) in order
1222 * to properly set the value for the PLUGIN_COLUMN_CAN_UNCHECK column. The order between
1223 * sub-plugins does not matter, only between sub-plugins and their proxy, thus
1224 * sorting by hierarchy level is perfectly sufficient */
1226
1227 g_free(plugin_path_config);
1228 g_free(plugin_path_system);
1229}
1230
1231
1232static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
1233{
1234 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
1235 guint i = 0;
1236 gboolean have_plugin_menu_items = FALSE;
1237
1238 for (item = list; item != NULL; item = g_list_next(item))
1239 {
1240 if (item->data == menu_separator)
1241 {
1242 if (i < g_list_length(list) - 1)
1243 {
1244 have_plugin_menu_items = TRUE;
1245 break;
1246 }
1247 }
1248 i++;
1249 }
1250 g_list_free(list);
1251
1252 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
1253}
1254
1255
1256/* Calling this starts up plugin support */
1258{
1259 GtkWidget *widget;
1260
1261 want_plugins = TRUE;
1262
1264
1265 widget = gtk_separator_menu_item_new();
1266 gtk_widget_show(widget);
1267 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1268
1269 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1270 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
1271 gtk_widget_show(widget);
1272 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
1273
1274 menu_separator = gtk_separator_menu_item_new();
1276 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
1277
1279}
1280
1281
1282/* Update the global active plugins list so it's up-to-date when configuration
1283 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1285{
1286 gint i = 0;
1287 GList *list;
1288 gsize count;
1289
1290 /* if plugins are disabled, don't clear list of active plugins */
1291 if (!want_plugins)
1292 return;
1293
1294 count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
1295
1296 g_strfreev(active_plugins_pref);
1297
1298 if (count == 0)
1299 {
1301 return;
1302 }
1303
1304 active_plugins_pref = g_new0(gchar*, count + 1);
1305
1306 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
1307 {
1308 Plugin *plugin = list->data;
1309
1310 active_plugins_pref[i] = g_strdup(plugin->filename);
1311 i++;
1312 }
1313 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
1314 {
1315 const gchar *fname = list->data;
1316
1317 active_plugins_pref[i] = g_strdup(fname);
1318 i++;
1319 }
1321}
1322
1323
1324/* called even if plugin support is disabled */
1326{
1328 gchar *path;
1329
1330 path = get_plugin_path();
1331 geany_debug("System plugin path: %s", path);
1332 g_free(path);
1333
1334 group = stash_group_new("plugins");
1336
1338 "load_plugins", TRUE, "check_plugins");
1340 "custom_plugin_path", "", "extra_plugin_path_entry");
1341
1342 g_signal_connect(geany_object, "save-settings", G_CALLBACK(update_active_plugins_pref), NULL);
1344
1345 g_queue_push_head(&active_proxies, &builtin_so_proxy);
1346}
1347
1348
1349/* Same as plugin_free(), except it does nothing for proxies-in-use, to be called on
1350 * finalize in a loop */
1352{
1353 if (p->proxied_count == 0)
1354 plugin_free(p);
1355}
1356
1357
1358/* called even if plugin support is disabled */
1360{
1362 {
1363 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1364 g_list_free(failed_plugins_list);
1365 }
1366 /* Have to loop because proxys cannot be unloaded until after all their
1367 * plugins are unloaded as well (the second loop should should catch all the remaining ones) */
1368 while (active_plugin_list != NULL)
1369 g_list_foreach(active_plugin_list, (GFunc) plugin_free_leaf, NULL);
1370
1371 g_strfreev(active_plugins_pref);
1372}
1373
1374
1375/* Check whether there are any plugins loaded which provide a configure symbol */
1377{
1378 GList *item;
1379
1380 if (active_plugin_list == NULL)
1381 return FALSE;
1382
1384 {
1385 Plugin *plugin = item->data;
1386 if (plugin->configure_single != NULL || plugin->cbs.configure != NULL)
1387 return TRUE;
1388 }
1389
1390 return FALSE;
1391}
1392
1393
1394/* Plugin Manager */
1395
1396enum
1397{
1406
1407typedef struct
1408{
1409 GtkWidget *dialog;
1410 GtkWidget *tree;
1411 GtkTreeStore *store;
1412 GtkWidget *filter_entry;
1415 GtkWidget *help_button;
1416 GtkWidget *popup_menu;
1420}
1422
1424
1425
1427{
1428 gboolean has_configure = FALSE;
1429 gboolean has_help = FALSE;
1430 gboolean has_keybindings = FALSE;
1431
1432 if (p != NULL && is_active_plugin(p))
1433 {
1434 has_configure = p->cbs.configure || p->configure_single;
1435 has_help = p->cbs.help != NULL;
1436 has_keybindings = p->key_group && p->key_group->plugin_key_count;
1437 }
1438
1439 gtk_widget_set_sensitive(pm_widgets.configure_button, has_configure);
1440 gtk_widget_set_sensitive(pm_widgets.help_button, has_help);
1441 gtk_widget_set_sensitive(pm_widgets.keybindings_button, has_keybindings);
1442
1443 gtk_widget_set_sensitive(pm_widgets.popup_configure_menu_item, has_configure);
1444 gtk_widget_set_sensitive(pm_widgets.popup_help_menu_item, has_help);
1445 gtk_widget_set_sensitive(pm_widgets.popup_keybindings_menu_item, has_keybindings);
1446}
1447
1448
1449static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1450{
1451 GtkTreeIter iter;
1452 GtkTreeModel *model;
1453 Plugin *p;
1454
1455 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1456 {
1457 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1458
1459 if (p != NULL)
1461 }
1462}
1463
1464
1465static gboolean find_iter_for_plugin(Plugin *p, GtkTreeModel *model, GtkTreeIter *iter)
1466{
1467 Plugin *pp;
1468 gboolean valid;
1469
1470 for (valid = gtk_tree_model_get_iter_first(model, iter);
1471 valid;
1472 valid = gtk_tree_model_iter_next(model, iter))
1473 {
1474 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &pp, -1);
1475 if (p == pp)
1476 return TRUE;
1477 }
1478
1479 return FALSE;
1480}
1481
1482
1483static void pm_populate(GtkTreeStore *store);
1484
1485
1486static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1487{
1488 gboolean old_state, state;
1489 gchar *file_name;
1490 GtkTreeIter iter;
1491 GtkTreeIter store_iter;
1492 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1493 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1494 Plugin *p;
1495 Plugin *proxy;
1496 guint prev_num_proxies;
1497
1498 gtk_tree_model_get_iter(model, &iter, path);
1499
1500 gtk_tree_model_get(model, &iter,
1501 PLUGIN_COLUMN_CHECK, &old_state,
1502 PLUGIN_COLUMN_PLUGIN, &p, -1);
1503
1504 /* no plugins item */
1505 if (p == NULL)
1506 {
1507 gtk_tree_path_free(path);
1508 return;
1509 }
1510
1511 gtk_tree_model_filter_convert_iter_to_child_iter(
1512 GTK_TREE_MODEL_FILTER(model), &store_iter, &iter);
1513
1514 state = ! old_state; /* toggle the state */
1515
1516 /* save the filename and proxy of the plugin */
1517 file_name = g_strdup(p->filename);
1518 proxy = p->proxy;
1519 prev_num_proxies = active_proxies.length;
1520
1521 /* unload plugin module */
1522 if (!state)
1523 /* save shortcuts (only need this group, but it doesn't take long) */
1525
1526 /* plugin_new() below may cause a tree view refresh with invalid p - set to NULL */
1527 gtk_tree_store_set(pm_widgets.store, &store_iter,
1529 plugin_free(p);
1530
1531 /* reload plugin module and initialize it if item is checked */
1532 p = plugin_new(proxy, file_name, state, TRUE);
1533 if (!p)
1534 {
1535 /* plugin file may no longer be on disk, or is now incompatible */
1536 gtk_tree_store_remove(pm_widgets.store, &store_iter);
1537 }
1538 else
1539 {
1540 if (state)
1541 keybindings_load_keyfile(); /* load shortcuts */
1542
1543 /* update model */
1544 gtk_tree_store_set(pm_widgets.store, &store_iter,
1545 PLUGIN_COLUMN_CHECK, state,
1546 PLUGIN_COLUMN_PLUGIN, p, -1);
1547
1548 /* set again the sensitiveness of the configure and help buttons */
1550
1551 /* Depending on the state disable the checkbox for the proxy of this plugin, and
1552 * only re-enable if the proxy is not used by any other plugin */
1553 if (p->proxy != &builtin_so_proxy_plugin)
1554 {
1555 GtkTreeIter parent;
1556 gboolean can_uncheck;
1557 GtkTreePath *store_path = gtk_tree_model_filter_convert_path_to_child_path(
1558 GTK_TREE_MODEL_FILTER(model), path);
1559
1560 g_warn_if_fail(store_path != NULL);
1561 if (gtk_tree_path_up(store_path))
1562 {
1563 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &parent, store_path);
1564
1565 if (state)
1566 can_uncheck = FALSE;
1567 else
1568 can_uncheck = p->proxy->proxied_count == 0;
1569
1570 gtk_tree_store_set(pm_widgets.store, &parent,
1571 PLUGIN_COLUMN_CAN_UNCHECK, can_uncheck, -1);
1572 }
1573 gtk_tree_path_free(store_path);
1574 }
1575 }
1576 /* We need to find out if a proxy was added or removed because that affects the plugin list
1577 * presented by the plugin manager */
1578 if (prev_num_proxies != active_proxies.length)
1579 {
1580 /* Rescan the plugin list as we now support more. Gives some "already loaded" warnings
1581 * they are unproblematic */
1582 if (prev_num_proxies < active_proxies.length)
1584
1586 gtk_tree_view_expand_row(GTK_TREE_VIEW(pm_widgets.tree), path, FALSE);
1587 }
1588
1589 gtk_tree_path_free(path);
1590 g_free(file_name);
1591}
1592
1593static void pm_populate(GtkTreeStore *store)
1594{
1595 GtkTreeIter iter;
1596 GList *list;
1597
1598 gtk_tree_store_clear(store);
1599 list = g_list_first(plugin_list);
1600 if (list == NULL)
1601 {
1602 gtk_tree_store_append(store, &iter, NULL);
1603 gtk_tree_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1605 }
1606 else
1607 {
1608 for (; list != NULL; list = list->next)
1609 {
1610 Plugin *p = list->data;
1611 GtkTreeIter parent;
1612
1614 && find_iter_for_plugin(p->proxy, GTK_TREE_MODEL(pm_widgets.store), &parent))
1615 gtk_tree_store_append(store, &iter, &parent);
1616 else
1617 gtk_tree_store_append(store, &iter, NULL);
1618
1619 gtk_tree_store_set(store, &iter,
1623 -1);
1624 }
1625 }
1626}
1627
1628static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y,
1629 gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
1630{
1631 GtkTreeModel *model;
1632 GtkTreeIter iter;
1633 GtkTreePath *path;
1634 Plugin *p = NULL;
1635 gboolean can_uncheck = TRUE;
1636
1637 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget), &x, &y, keyboard_mode,
1638 &model, &path, &iter))
1639 return FALSE;
1640
1641 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, PLUGIN_COLUMN_CAN_UNCHECK, &can_uncheck, -1);
1642 if (p != NULL)
1643 {
1644 gchar *prefix, *suffix, *details, *markup;
1645 const gchar *uchk;
1646
1647 uchk = can_uncheck ?
1648 "" : _("\n<i>Other plugins depend on this. Disable them first to allow deactivation.</i>\n");
1649 /* Four allocations is less than ideal but meh */
1650 details = g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1651 p->info.version, p->info.author, p->filename);
1652 prefix = g_markup_printf_escaped("<b>%s</b>\n%s\n", p->info.name, p->info.description);
1653 suffix = g_markup_printf_escaped("<small><i>\n%s</i></small>", details);
1654 markup = g_strconcat(prefix, uchk, suffix, NULL);
1655
1656 gtk_tooltip_set_markup(tooltip, markup);
1657 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget), tooltip, path);
1658
1659 g_free(details);
1660 g_free(suffix);
1661 g_free(prefix);
1662 g_free(markup);
1663 }
1664 gtk_tree_path_free(path);
1665
1666 return p != NULL;
1667}
1668
1669
1670static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell,
1671 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1672{
1673 Plugin *p;
1674
1675 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1676
1677 if (p == NULL)
1678 g_object_set(cell, "text", _("No plugins available."), NULL);
1679 else
1680 {
1681 gchar *markup = g_markup_printf_escaped("<b>%s</b>\n%s", p->info.name, p->info.description);
1682
1683 g_object_set(cell, "markup", markup, NULL);
1684 g_free(markup);
1685 }
1686}
1687
1688
1689static gboolean pm_treeview_button_press_cb(GtkWidget *widget, GdkEventButton *event,
1690 G_GNUC_UNUSED gpointer user_data)
1691{
1692 if (event->button == 3)
1693 {
1694 gtk_menu_popup(GTK_MENU(pm_widgets.popup_menu), NULL, NULL, NULL, NULL,
1695 event->button, event->time);
1696 }
1697 return FALSE;
1698}
1699
1700
1701static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1702 gpointer user_data)
1703{
1704 Plugin *pa, *pb;
1705
1706 gtk_tree_model_get(model, a, PLUGIN_COLUMN_PLUGIN, &pa, -1);
1707 gtk_tree_model_get(model, b, PLUGIN_COLUMN_PLUGIN, &pb, -1);
1708
1709 if (pa && pb)
1710 return strcmp(pa->info.name, pb->info.name);
1711 else
1712 return pa - pb;
1713}
1714
1715
1716static gboolean pm_tree_search(const gchar *key, const gchar *haystack)
1717{
1718 gchar *normalized_string = NULL;
1719 gchar *normalized_key = NULL;
1720 gchar *case_normalized_string = NULL;
1721 gchar *case_normalized_key = NULL;
1722 gboolean matched = TRUE;
1723
1724 normalized_string = g_utf8_normalize(haystack, -1, G_NORMALIZE_ALL);
1725 normalized_key = g_utf8_normalize(key, -1, G_NORMALIZE_ALL);
1726
1727 if (normalized_string != NULL && normalized_key != NULL)
1728 {
1729 GString *stripped_key;
1730 gchar **subkey, **subkeys;
1731
1732 case_normalized_string = g_utf8_casefold(normalized_string, -1);
1733 case_normalized_key = g_utf8_casefold(normalized_key, -1);
1734 stripped_key = g_string_new(case_normalized_key);
1735 do {} while (utils_string_replace_all(stripped_key, " ", " "));
1736 subkeys = g_strsplit(stripped_key->str, " ", -1);
1737 g_string_free(stripped_key, TRUE);
1738 foreach_strv(subkey, subkeys)
1739 {
1740 if (strstr(case_normalized_string, *subkey) == NULL)
1741 {
1742 matched = FALSE;
1743 break;
1744 }
1745 }
1746 g_strfreev(subkeys);
1747 }
1748
1749 g_free(normalized_key);
1750 g_free(normalized_string);
1751 g_free(case_normalized_key);
1752 g_free(case_normalized_string);
1753
1754 return matched;
1755}
1756
1757
1758static gboolean pm_tree_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
1759{
1760 Plugin *plugin;
1761 gboolean matched;
1762 const gchar *key;
1763 gchar *haystack, *filename;
1764
1765 gtk_tree_model_get(model, iter, PLUGIN_COLUMN_PLUGIN, &plugin, -1);
1766
1767 if (!plugin)
1768 return TRUE;
1769 key = gtk_entry_get_text(GTK_ENTRY(pm_widgets.filter_entry));
1770
1771 filename = g_path_get_basename(plugin->filename);
1772 haystack = g_strjoin(" ", plugin->info.name, plugin->info.description,
1773 plugin->info.author, filename, NULL);
1774 matched = pm_tree_search(key, haystack);
1775 g_free(haystack);
1776 g_free(filename);
1777
1778 return matched;
1779}
1780
1781
1782static void on_pm_tree_filter_entry_changed_cb(GtkEntry *entry, gpointer user_data)
1783{
1784 GtkTreeModel *filter_model = gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets.tree));
1785 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model));
1786}
1787
1788
1789static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos,
1790 GdkEvent *event, gpointer user_data)
1791{
1792 if (event->button.button == 1 && icon_pos == GTK_ENTRY_ICON_PRIMARY)
1794}
1795
1796
1797static void pm_prepare_treeview(GtkWidget *tree, GtkTreeStore *store)
1798{
1799 GtkCellRenderer *text_renderer, *checkbox_renderer;
1800 GtkTreeViewColumn *column;
1801 GtkTreeModel *filter_model;
1802 GtkTreeSelection *sel;
1803
1804 g_signal_connect(tree, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip), NULL);
1805 gtk_widget_set_has_tooltip(tree, TRUE);
1806 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1807
1808 checkbox_renderer = gtk_cell_renderer_toggle_new();
1809 column = gtk_tree_view_column_new_with_attributes(
1810 _("Active"), checkbox_renderer,
1811 "active", PLUGIN_COLUMN_CHECK, "activatable", PLUGIN_COLUMN_CAN_UNCHECK, NULL);
1812 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1813 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1814
1815 text_renderer = gtk_cell_renderer_text_new();
1816 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1817 column = gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer, NULL);
1818 gtk_tree_view_column_set_cell_data_func(column, text_renderer,
1820 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1821
1822 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1823 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1824 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN,
1826 gtk_tree_sortable_set_sort_column_id(
1827 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_PLUGIN, GTK_SORT_ASCENDING);
1828
1829 /* selection handling */
1830 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1831 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1832 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1833
1834 g_signal_connect(tree, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb), NULL);
1835
1836 /* filter */
1837 filter_model = gtk_tree_model_filter_new(GTK_TREE_MODEL(store), NULL);
1838 gtk_tree_model_filter_set_visible_func(
1839 GTK_TREE_MODEL_FILTER(filter_model), pm_tree_filter_func, NULL, NULL);
1840
1841 /* set model to tree view */
1842 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), filter_model);
1843 g_object_unref(filter_model);
1844
1845 pm_populate(store);
1846}
1847
1848
1849static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
1850{
1851 GtkTreeModel *model;
1852 GtkTreeSelection *selection;
1853 GtkTreeIter iter;
1854 Plugin *p;
1855
1856 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1857 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1858 {
1859 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1860
1861 if (p != NULL)
1862 {
1863 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1865 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP)
1866 {
1867 g_return_if_fail(p->cbs.help != NULL);
1868 p->cbs.help(&p->public, p->cb_data);
1869 }
1870 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_KEYBINDINGS && p->key_group && p->key_group->plugin_key_count > 0)
1872 }
1873 }
1874}
1875
1876
1877static void
1878free_non_active_plugin(gpointer data, gpointer user_data)
1879{
1880 Plugin *plugin = data;
1881
1882 /* don't do anything when closing the plugin manager and it is an active plugin */
1883 if (is_active_plugin(plugin))
1884 return;
1885
1886 plugin_free(plugin);
1887}
1888
1889
1890/* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1891 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1892static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1893{
1894 switch (response)
1895 {
1896 case GTK_RESPONSE_CLOSE:
1897 case GTK_RESPONSE_DELETE_EVENT:
1898 if (plugin_list != NULL)
1899 {
1900 /* remove all non-active plugins from the list */
1901 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1902 g_list_free(plugin_list);
1903 plugin_list = NULL;
1904 }
1905 gtk_widget_destroy(GTK_WIDGET(dialog));
1907
1909 break;
1911 case PM_BUTTON_HELP:
1913 /* forward event to the generic handler */
1914 pm_on_plugin_button_clicked(NULL, GINT_TO_POINTER(response));
1915 break;
1916 }
1917}
1918
1919
1920static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1921{
1922 GtkWidget *vbox, *vbox2, *swin, *label, *menu_item, *filter_entry;
1923
1924 if (pm_widgets.dialog != NULL)
1925 {
1926 gtk_window_present(GTK_WINDOW(pm_widgets.dialog));
1927 return;
1928 }
1929
1930 /* before showing the dialog, we need to create the list of available plugins */
1932
1933 pm_widgets.dialog = gtk_dialog_new();
1934 gtk_window_set_title(GTK_WINDOW(pm_widgets.dialog), _("Plugins"));
1935 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets.dialog), GTK_WINDOW(main_widgets.window));
1936 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets.dialog), TRUE);
1937
1938 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1939 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1940 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1941
1942 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1943
1944 pm_widgets.help_button = gtk_dialog_add_button(
1945 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_HELP, PM_BUTTON_HELP);
1946 pm_widgets.configure_button = gtk_dialog_add_button(
1947 GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_PREFERENCES, PM_BUTTON_CONFIGURE);
1948 pm_widgets.keybindings_button = gtk_dialog_add_button(
1949 GTK_DIALOG(pm_widgets.dialog), _("Keybindings"), PM_BUTTON_KEYBINDINGS);
1950 gtk_dialog_add_button(GTK_DIALOG(pm_widgets.dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1951 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets.dialog), GTK_RESPONSE_CLOSE);
1952
1953 /* filter */
1954 pm_widgets.filter_entry = filter_entry = gtk_entry_new();
1955 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry), GTK_ENTRY_ICON_PRIMARY, GTK_STOCK_FIND);
1957 g_signal_connect(filter_entry, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb), NULL);
1958 g_signal_connect(filter_entry, "icon-release",
1960
1961 /* prepare treeview */
1962 pm_widgets.tree = gtk_tree_view_new();
1963 pm_widgets.store = gtk_tree_store_new(
1964 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_POINTER);
1966 gtk_tree_view_expand_all(GTK_TREE_VIEW(pm_widgets.tree));
1967 g_object_unref(pm_widgets.store);
1968
1969 swin = gtk_scrolled_window_new(NULL, NULL);
1970 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1971 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1972 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1973 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1974
1975 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1976
1977 /* plugin popup menu */
1978 pm_widgets.popup_menu = gtk_menu_new();
1979
1980 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
1981 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1982 g_signal_connect(menu_item, "activate",
1983 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1985
1986 menu_item = gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1987 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1988 g_signal_connect(menu_item, "activate",
1989 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS));
1991
1992 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
1993 gtk_container_add(GTK_CONTAINER(pm_widgets.popup_menu), menu_item);
1994 g_signal_connect(menu_item, "activate",
1995 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1996 pm_widgets.popup_help_menu_item = menu_item;
1997
1998 /* put it together */
1999 vbox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
2000 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0);
2001 gtk_box_pack_start(GTK_BOX(vbox2), filter_entry, FALSE, FALSE, 0);
2002 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
2003
2004 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
2005
2006 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2009
2010 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
2012 gtk_widget_grab_focus(pm_widgets.filter_entry);
2013}
2014
2015
2016/** Register the plugin as a proxy for other plugins
2017 *
2018 * Proxy plugins register a list of file extensions and a set of callbacks that are called
2019 * appropriately. A plugin can be a proxy for multiple types of sub-plugins by handling
2020 * separate file extensions, however they must share the same set of hooks, because this
2021 * function can only be called at most once per plugin.
2022 *
2023 * Each callback receives the plugin-defined data as parameter (see geany_plugin_register()). The
2024 * callbacks must be set prior to calling this, by assigning to @a plugin->proxy_funcs.
2025 * GeanyProxyFuncs::load and GeanyProxyFuncs::unload must be implemented.
2026 *
2027 * Nested proxies are unsupported at this point (TODO).
2028 *
2029 * @note It is entirely up to the proxy to provide access to Geany's plugin API. Native code
2030 * can naturally call Geany's API directly, for interpreted languages the proxy has to
2031 * implement some kind of bindings that the plugin can use.
2032 *
2033 * @see proxy for detailed documentation and an example.
2034 *
2035 * @param plugin The pointer to the plugin's GeanyPlugin instance
2036 * @param extensions A @c NULL-terminated string array of file extensions, excluding the dot.
2037 * @return @c TRUE if the proxy was successfully registered, otherwise @c FALSE
2038 *
2039 * @since 1.26 (API 226)
2040 */
2041GEANY_API_SYMBOL
2042gboolean geany_plugin_register_proxy(GeanyPlugin *plugin, const gchar **extensions)
2043{
2044 Plugin *p;
2045 const gchar **ext;
2046 PluginProxy *proxy;
2047 GList *node;
2048
2049 g_return_val_if_fail(plugin != NULL, FALSE);
2050 g_return_val_if_fail(extensions != NULL, FALSE);
2051 g_return_val_if_fail(*extensions != NULL, FALSE);
2052 g_return_val_if_fail(plugin->proxy_funcs->load != NULL, FALSE);
2053 g_return_val_if_fail(plugin->proxy_funcs->unload != NULL, FALSE);
2054
2055 p = plugin->priv;
2056 /* Check if this was called already. We want to reserve for the use case of calling
2057 * this again to set new supported extensions (for example, based on proxy configuration). */
2058 foreach_list(node, active_proxies.head)
2059 {
2060 proxy = node->data;
2061 g_return_val_if_fail(p != proxy->plugin, FALSE);
2062 }
2063
2064 foreach_strv(ext, extensions)
2065 {
2066 if (**ext == '.')
2067 {
2068 g_warning(_("Proxy plugin '%s' extension '%s' starts with a dot. "
2069 "Please fix your proxy plugin."), p->info.name, *ext);
2070 }
2071
2072 proxy = g_new(PluginProxy, 1);
2073 g_strlcpy(proxy->extension, *ext, sizeof(proxy->extension));
2074 proxy->plugin = p;
2075 /* prepend, so that plugins automatically override core providers for a given extension */
2076 g_queue_push_head(&active_proxies, proxy);
2077 }
2078
2079 return TRUE;
2080}
2081
2082#endif
Contains the GeanyApp.
const gchar * label
Definition: build.c:2676
File related dialogs, miscellaneous dialogs, font dialog.
const gchar * name
Definition: document.c:3219
GeanyFilePrefs file_prefs
Definition: document.c:86
GPtrArray * documents_array
Definition: document.c:87
#define DOC_VALID(doc_ptr)
Null-safe way to check GeanyDocument::is_valid.
Definition: document.h:162
GeanyEditorPrefs editor_prefs
Definition: editor.c:77
Encoding conversion and Byte Order Mark (BOM) handling.
static GtkWidget * filter_entry
Definition: filebrowser.c:83
unsigned int count
tokenInfo * list
GObject * geany_object
Definition: geanyobject.c:41
GtkWidget * geany_wrap_label_new(const gchar *text)
Syntax highlighting for the different filetypes, using the Scintilla lexers.
void keybindings_free_group(GeanyKeyGroup *group)
Definition: keybindings.c:2685
void keybindings_load_keyfile(void)
Reloads keybinding settings from configuration file.
Definition: keybindings.c:842
void keybindings_write_to_file(void)
Definition: keybindings.c:915
void keybindings_dialog_show_prefs_scroll(const gchar *name)
Definition: keybindings.c:1059
void configuration_save(void)
Definition: keyfile.c:632
void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
Definition: keyfile.c:128
GeanyApp * app
Definition: libmain.c:86
void geany_debug(gchar const *format,...)
Definition: log.c:67
Main program-related commands.
void msgwin_status_add(const gchar *format,...)
Logs a formatted status message without setting the status bar.
Definition: msgwindow.c:520
Message window functions (status, compiler, messages windows).
Simple code navigation.
This file defines the plugin API, the interface between Geany and its plugins.
#define GEANY_API_VERSION
The Application Programming Interface (API) version, incremented whenever any plugin data types are m...
Definition: plugindata.h:61
void geany_plugin_set_data(GeanyPlugin *plugin, gpointer data, GDestroyNotify free_func)
Add additional data that corresponds to the plugin.
Definition: pluginutils.c:613
@ PLUGIN_IS_DOCUMENT_SENSITIVE
Whether a plugin's menu item should be disabled when there are no open documents.
Definition: plugindata.h:442
@ GEANY_PROXY_IGNORE
The proxy is not responsible at all, and Geany or other plugins are free to probe it.
Definition: plugindata.h:350
@ GEANY_PROXY_RELATED
The proxy is does not directly load it, but it's still tied to the proxy.
Definition: plugindata.h:366
@ GEANY_PROXY_MATCH
The proxy is responsible for this file, and creates a plugin for it.
Definition: plugindata.h:355
#define GEANY_ABI_VERSION
The Application Binary Interface (ABI) version, incremented whenever existing fields in the plugin da...
Definition: plugindata.h:72
@ LOADED_OK
Definition: pluginprivate.h:40
@ LOAD_DATA
Definition: pluginprivate.h:42
@ IS_LEGACY
Definition: pluginprivate.h:41
#define PLUGIN_LOADED_OK(p)
Definition: pluginprivate.h:78
#define PLUGIN_HAS_LOAD_DATA(p)
Definition: pluginprivate.h:80
#define PLUGIN_IS_LEGACY(p)
Definition: pluginprivate.h:79
static gboolean check_plugin_path(const gchar *fname)
Definition: plugins.c:1025
static gboolean pm_treeview_query_tooltip(GtkWidget *widget, gint x, gint y, gboolean keyboard_mode, GtkTooltip *tooltip, gpointer user_data)
Definition: plugins.c:1628
static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
Definition: plugins.c:1920
static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton *button, gpointer user_data)
Definition: plugins.c:1849
static gboolean pm_tree_search(const gchar *key, const gchar *haystack)
Definition: plugins.c:1716
static GList * plugin_list
Definition: plugins.c:76
static gboolean legacy_init(GeanyPlugin *plugin, gpointer pdata)
Definition: plugins.c:446
static void pm_update_buttons(Plugin *p)
Definition: plugins.c:1426
static void free_non_active_plugin(gpointer data, gpointer user_data)
Definition: plugins.c:1878
static void remove_callbacks(Plugin *plugin)
Definition: plugins.c:786
static void load_plugins_from_path(const gchar *path)
Definition: plugins.c:1142
static Plugin * plugin_new(Plugin *proxy, const gchar *fname, gboolean load_plugin, gboolean add_to_list)
Definition: plugins.c:663
static void plugin_cleanup(Plugin *plugin)
Definition: plugins.c:891
void plugins_finalize(void)
Definition: plugins.c:1359
void plugin_watch_object(Plugin *plugin, gpointer object)
Definition: plugins.c:780
static void plugin_free(Plugin *plugin)
Definition: plugins.c:969
static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
Definition: plugins.c:1232
gpointer plugin_get_module_symbol(Plugin *plugin, const gchar *sym)
Definition: plugins.c:834
static void register_legacy_plugin(Plugin *plugin, GModule *module)
Definition: plugins.c:480
void plugin_make_resident(Plugin *plugin)
Definition: plugins.c:821
static gboolean find_iter_for_plugin(Plugin *p, GtkTreeModel *model, GtkTreeIter *iter)
Definition: plugins.c:1465
static void load_all_plugins(void)
Definition: plugins.c:1197
static gint cmp_plugin_by_proxy(gconstpointer a, gconstpointer b)
Definition: plugins.c:1176
static void proxied_count_dec(Plugin *proxy)
Definition: plugins.c:150
static gboolean pm_treeview_button_press_cb(GtkWidget *widget, GdkEventButton *event, G_GNUC_UNUSED gpointer user_data)
Definition: plugins.c:1689
static gint pm_tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
Definition: plugins.c:1701
static void proxied_count_inc(Plugin *proxy)
Definition: plugins.c:140
static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
Definition: plugins.c:1892
static gboolean plugin_check_version(Plugin *plugin, int plugin_version_code)
Definition: plugins.c:225
static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, const gchar *filename, gpointer pdata)
Definition: plugins.c:601
static void pm_prepare_treeview(GtkWidget *tree, GtkTreeStore *store)
Definition: plugins.c:1797
static void free_subplugins(Plugin *proxy)
Definition: plugins.c:925
static void free_legacy_cbs(gpointer data)
Definition: plugins.c:473
static void on_object_weak_notify(gpointer data, GObject *old_ptr)
Definition: plugins.c:755
gboolean geany_plugin_register_full(GeanyPlugin *plugin, gint api_version, gint min_api_version, gint abi_version, gpointer pdata, GDestroyNotify free_func)
Register a plugin to Geany, with plugin-defined data.
Definition: plugins.c:422
@ PM_BUTTON_CONFIGURE
Definition: plugins.c:1403
@ PLUGIN_COLUMN_PLUGIN
Definition: plugins.c:1400
@ PLUGIN_COLUMN_CHECK
Definition: plugins.c:1398
@ PLUGIN_N_COLUMNS
Definition: plugins.c:1401
@ PM_BUTTON_HELP
Definition: plugins.c:1404
@ PM_BUTTON_KEYBINDINGS
Definition: plugins.c:1402
@ PLUGIN_COLUMN_CAN_UNCHECK
Definition: plugins.c:1399
static void update_active_plugins_pref(void)
Definition: plugins.c:1284
static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
Definition: plugins.c:1449
static PluginProxy builtin_so_proxy
Definition: plugins.c:102
static void pm_populate(GtkTreeStore *store)
Definition: plugins.c:1593
static void remove_each_doc_data(GQuark key_id, gpointer data, gpointer user_data)
Definition: plugins.c:860
static void read_key_group(Plugin *plugin)
Definition: plugins.c:277
static gboolean unregister_proxy(Plugin *proxy)
Definition: plugins.c:944
static void geany_data_init(void)
Definition: plugins.c:114
static gboolean want_plugins
Definition: plugins.c:72
static void remove_doc_data(Plugin *plugin)
Definition: plugins.c:869
static void remove_sources(Plugin *plugin)
Definition: plugins.c:804
static gboolean pm_tree_filter_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
Definition: plugins.c:1758
static gchar ** active_plugins_pref
Definition: plugins.c:77
static GeanyData geany_data
Definition: plugins.c:111
static gchar * get_custom_plugin_path(const gchar *plugin_path_config, const gchar *plugin_path_system)
Definition: plugins.c:1000
static gchar * get_plugin_path(void)
Definition: plugins.c:1168
static GQueue active_proxies
Definition: plugins.c:107
GList * active_plugin_list
Definition: plugins.c:69
gboolean geany_plugin_register(GeanyPlugin *plugin, gint api_version, gint min_api_version, gint abi_version)
Register a plugin to Geany.
Definition: plugins.c:353
static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
Definition: plugins.c:1486
static gboolean plugin_loaded(Plugin *plugin)
Definition: plugins.c:165
static void load_active_plugins(void)
Definition: plugins.c:1104
void plugins_init(void)
Definition: plugins.c:1325
#define CHECK_FUNC(__x)
static void legacy_help(GeanyPlugin *plugin, gpointer pdata)
Definition: plugins.c:461
static void plugin_free_leaf(Plugin *p)
Definition: plugins.c:1351
static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
Definition: plugins.c:251
#define PLUGIN_VERSION_CODE(api, abi)
Definition: plugins.c:222
static GList * failed_plugins_list
Definition: plugins.c:78
void plugins_load_active(void)
Definition: plugins.c:1257
gboolean plugins_have_preferences(void)
Definition: plugins.c:1376
static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata)
Definition: plugins.c:646
static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer user_data)
Definition: plugins.c:1789
static void pm_treeview_text_cell_data_func(GtkTreeViewColumn *column, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
Definition: plugins.c:1670
static GtkWidget * menu_separator
Definition: plugins.c:80
static void on_pm_tree_filter_entry_changed_cb(GtkEntry *entry, gpointer user_data)
Definition: plugins.c:1782
static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
Definition: plugins.c:315
static PluginManagerWidgets pm_widgets
Definition: plugins.c:1423
static gboolean plugin_load(Plugin *plugin)
Definition: plugins.c:545
static gboolean is_active_plugin(Plugin *plugin)
Definition: plugins.c:854
static Plugin * find_active_plugin_by_name(const gchar *filename)
Definition: plugins.c:205
static GtkWidget * legacy_configure(GeanyPlugin *plugin, GtkDialog *parent, gpointer pdata)
Definition: plugins.c:467
static void legacy_cleanup(GeanyPlugin *plugin, gpointer pdata)
Definition: plugins.c:453
static PluginProxy * is_plugin(const gchar *file)
Definition: plugins.c:1056
static Plugin builtin_so_proxy_plugin
Definition: plugins.c:94
gboolean geany_plugin_register_proxy(GeanyPlugin *plugin, const gchar **extensions)
Register the plugin as a proxy for other plugins.
Definition: plugins.c:2042
PluginFields * plugin_fields
Plugin owned fields, including flags.
Definition: pluginsymbols.c:70
void plugin_show_configure(GeanyPlugin *plugin)
Shows the plugin's configure dialog.
Definition: pluginutils.c:474
GeanyKeyGroup * plugin_set_key_group(GeanyPlugin *plugin, const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
<simplesect kind="geany:skip"></simplesect> Sets up or resizes a keybinding group for the plugin.
Definition: pluginutils.c:331
void plugin_signal_connect(GeanyPlugin *plugin, GObject *object, const gchar *signal_name, gboolean after, GCallback callback, gpointer user_data)
<simplesect kind="geany:skip"></simplesect> Connects a signal which will be disconnected on unloading...
Definition: pluginutils.c:158
Plugin utility functions.
GeanyToolPrefs tool_prefs
Definition: prefs.c:67
GeanyPrefs prefs
Definition: prefs.c:66
#define NULL
Definition: rbtree.h:150
char * strstr(const char *str, const char *substr)
Definition: routines.c:304
Wrapper functions for the Scintilla editor widget SCI_* messages.
GtkWidget * entry
Definition: search.c:118
GeanySearchPrefs search_prefs
Definition: search.c:78
GSList * filetypes_by_title
Definition: filetypes.c:59
GPtrArray * filetypes_array
Definition: filetypes.c:57
StashGroup * group
Definition: stash-example.c:1
const gchar filename[]
Definition: stash-example.c:4
GtkWidget * dialog
stash_group_add_toggle_button(group, &want_handle, "handle", TRUE, "check_handle")
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
gtk_widget_show_all(dialog)
void stash_group_add_string_vector(StashGroup *group, gchar ***setting, const gchar *key_name, const gchar **default_value)
Adds string vector setting (array of strings).
Definition: stash.c:513
StashGroup * stash_group_new(const gchar *name)
Creates a new group.
Definition: stash.c:360
void stash_group_add_entry(StashGroup *group, gchar **setting, const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
Adds a GtkEntry widget pref.
Definition: stash.c:944
Lightweight library for reading/writing GKeyFile settings and synchronizing widgets with C variables.
GeanyDocument * document
Definition: plugins.c:64
gchar * prefix
Definition: plugins.c:63
gchar * configdir
User configuration directory, usually ~/.config/geany.
Definition: app.h:45
gboolean debug_mode
TRUE if debug messages should be printed.
Definition: app.h:39
This contains pointers to global variables owned by Geany for plugins to use.
Definition: plugindata.h:167
Structure for representing an open tab with all its properties.
Definition: document.h:81
struct GeanyDocumentPrivate * priv
Definition: document.h:121
const gchar * name
Group name used in the configuration file, such as "html_chars".
Definition: plugindata.h:414
gsize count
The number of keybindings the group will hold.
Definition: plugindata.h:415
GtkWidget * window
Main window.
Definition: ui_utils.h:80
GtkWidget * tools_menu
Most plugins add menu items to the Tools menu.
Definition: ui_utils.h:85
Callback functions that need to be implemented for every plugin.
Definition: plugindata.h:296
gboolean(* init)(GeanyPlugin *plugin, gpointer pdata)
Called to initialize the plugin, when the user activates it (must not be NULL)
Definition: plugindata.h:300
GtkWidget *(* configure)(GeanyPlugin *plugin, GtkDialog *dialog, gpointer pdata)
plugins configure dialog, optional (can be NULL)
Definition: plugindata.h:302
PluginCallback * callbacks
Array of plugin-provided signal handlers.
Definition: plugindata.h:298
void(* help)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin should show some help, optional (can be NULL)
Definition: plugindata.h:304
void(* cleanup)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin is disabled or when Geany exits (must not be NULL)
Definition: plugindata.h:306
GeanyKeyGroup * key_group
Definition: pluginprivate.h:59
GeanyPlugin public
Definition: pluginprivate.h:52
void(* configure_single)(GtkWidget *parent)
Definition: pluginprivate.h:55
LoadedFlags flags
Definition: pluginprivate.h:66
GeanyPluginFuncs cbs
Definition: pluginprivate.h:54
GeanyProxyFuncs proxy_cbs
Definition: pluginprivate.h:69
PluginFields fields
Definition: pluginprivate.h:58
GeanyAutoSeparator toolbar_separator
Definition: pluginprivate.h:60
GDestroyNotify cb_data_destroy
Definition: pluginprivate.h:65
Basic information for the plugin and identification.
Definition: plugindata.h:233
GeanyData * geany_data
Pointer to global GeanyData intance.
Definition: plugindata.h:235
PluginInfo * info
Fields set in plugin_set_info().
Definition: plugindata.h:234
GeanyProxyFuncs * proxy_funcs
Hooks implemented by the plugin if it wants to act as a proxy Must be set prior to calling geany_plug...
Definition: plugindata.h:237
struct GeanyPluginPrivate * priv
Definition: plugindata.h:239
GeanyPluginFuncs * funcs
Functions implemented by the plugin, set in geany_load_module()
Definition: plugindata.h:236
gchar * custom_plugin_path
Definition: prefs.h:40
gboolean load_plugins
Definition: prefs.h:32
void(* unload)(GeanyPlugin *proxy, GeanyPlugin *subplugin, gpointer load_data, gpointer pdata)
Called when the user initiates unloading of a plugin, e.g.
Definition: plugindata.h:397
gint(* probe)(GeanyPlugin *proxy, const gchar *filename, gpointer pdata)
Called to determine whether the proxy is truly responsible for the requested plugin.
Definition: plugindata.h:393
gpointer(* load)(GeanyPlugin *proxy, GeanyPlugin *subplugin, const gchar *filename, gpointer pdata)
Called after probe(), to perform the actual job of loading the plugin.
Definition: plugindata.h:395
GtkWidget *(* configure)(GtkDialog *dialog)
Definition: plugins.c:440
void(* help)(void)
Definition: plugins.c:441
void(* cleanup)(void)
Definition: plugins.c:442
void(* init)(GeanyData *data)
Definition: plugins.c:439
Callback array entry type used with the plugin_callbacks symbol.
Definition: plugindata.h:148
gpointer user_data
The user data passed to the signal handler.
Definition: plugindata.h:159
GCallback callback
A callback function which is called when the signal is emitted.
Definition: plugindata.h:153
gboolean after
Set to TRUE to connect your handler with g_signal_connect_after().
Definition: plugindata.h:155
const gchar * signal_name
The name of signal, must be an existing signal.
Definition: plugindata.h:151
PluginFlags flags
Bitmask of PluginFlags.
Definition: plugindata.h:451
GtkWidget * menu_item
Pointer to a plugin's menu item which will be automatically enabled/disabled when there are no open d...
Definition: plugindata.h:455
Basic information about a plugin available to Geany without loading the plugin.
Definition: plugindata.h:95
const gchar * version
The version of the plugin.
Definition: plugindata.h:101
const gchar * description
The description of the plugin.
Definition: plugindata.h:99
const gchar * name
The name of the plugin.
Definition: plugindata.h:97
const gchar * author
The author of the plugin.
Definition: plugindata.h:103
GtkWidget * tree
Definition: plugins.c:1410
GtkTreeStore * store
Definition: plugins.c:1411
GtkWidget * popup_help_menu_item
Definition: plugins.c:1419
GtkWidget * help_button
Definition: plugins.c:1415
GtkWidget * filter_entry
Definition: plugins.c:1412
GtkWidget * popup_menu
Definition: plugins.c:1416
GtkWidget * popup_configure_menu_item
Definition: plugins.c:1417
GtkWidget * keybindings_button
Definition: plugins.c:1414
GtkWidget * dialog
Definition: plugins.c:1409
GtkWidget * popup_keybindings_menu_item
Definition: plugins.c:1418
GtkWidget * configure_button
Definition: plugins.c:1413
gchar extension[8]
Definition: plugins.c:86
Plugin * plugin
Definition: plugins.c:87
GObject * object
Definition: pluginprivate.h:34
Defines internationalization macros.
#define _(String)
Definition: support.h:42
Tag-related functions.
GeanyTemplatePrefs template_prefs
Definition: templates.c:50
Templates (prefs).
GeanyToolbarPrefs toolbar_prefs
Definition: toolbar.c:48
Toolbar (prefs).
void ui_widget_show_hide(GtkWidget *widget, gboolean show)
Definition: ui_utils.c:987
void ui_add_document_sensitive(GtkWidget *widget)
Adds a widget to the list of widgets that should be set sensitive/insensitive when some documents are...
Definition: ui_utils.c:976
GtkWidget * ui_dialog_vbox_new(GtkDialog *dialog)
Makes a fixed border for dialogs without increasing the button box border.
Definition: ui_utils.c:1499
GeanyMainWidgets main_widgets
Definition: ui_utils.c:72
void ui_entry_add_clear_icon(GtkEntry *entry)
Adds a small clear icon to the right end of the passed entry.
Definition: ui_utils.c:1604
GeanyInterfacePrefs interface_prefs
Definition: ui_utils.c:71
User Interface general utility functions.
gint utils_str_casecmp(const gchar *s1, const gchar *s2)
A replacement function for g_strncasecmp() to compare strings case-insensitive.
Definition: utils.c:506
guint utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
Replaces all occurrences of needle in haystack with replace.
Definition: utils.c:1529
GSList * utils_get_file_list(const gchar *path, guint *length, GError **error)
Gets a sorted list of files from the specified directory.
Definition: utils.c:1441
void utils_tidy_path(gchar *filename)
Definition: utils.c:1774
const gchar * utils_resource_dir(GeanyResourceDirType type)
Definition: utils.c:2301
gboolean utils_str_equal(const gchar *a, const gchar *b)
NULL-safe string comparison.
Definition: utils.c:599
gchar * utils_get_locale_from_utf8(const gchar *utf8_text)
Converts the given UTF-8 encoded string into locale encoding.
Definition: utils.c:1243
General utility functions, non-GTK related.
#define foreach_list(node, list)
Iterates all the nodes in list.
Definition: utils.h:115
#define foreach_strv(str_ptr, strv)
Iterates a null-terminated string vector.
Definition: utils.h:152
#define EMPTY(ptr)
Returns TRUE if ptr is NULL or *ptr is FALSE.
Definition: utils.h:38
#define foreach_array(type, item, array)
Iterates all items in array.
Definition: utils.h:101
#define foreach_list_safe(node, list)
Definition: utils.h:127