2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* Code to manage, load and unload plugins. */
34 #include "documentprivate.h"
35 #include "encodings.h"
36 #include "geanyobject.h"
37 #include "geanywraplabel.h"
38 #include "highlighting.h"
39 #include "keybindingsprivate.h"
42 #include "msgwindow.h"
44 #include "plugindata.h"
45 #include "pluginprivate.h"
46 #include "pluginutils.h"
48 #include "sciwrappers.h"
52 #include "templates.h"
58 #include "gtkcompat.h"
66 GeanyDocument
*document
;
71 GList
*active_plugin_list
= NULL
; /* list of only actually loaded plugins, always valid */
74 static gboolean want_plugins
= FALSE
;
76 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
77 * opened, afterwards it will be destroyed */
78 static GList
*plugin_list
= NULL
;
79 static gchar
**active_plugins_pref
= NULL
; /* list of plugin filenames to load at startup */
80 static GList
*failed_plugins_list
= NULL
; /* plugins the user wants active but can't be used */
82 static GtkWidget
*menu_separator
= NULL
;
84 static gchar
*get_plugin_path(void);
85 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
);
89 Plugin
*plugin
; /* &builtin_so_proxy_plugin for native plugins */
93 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, const gchar
*filename
, gpointer pdata
);
94 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, gpointer load_data
, gpointer pdata
);
96 static Plugin builtin_so_proxy_plugin
= {
98 .load
= plugin_load_gmodule
,
99 .unload
= plugin_unload_gmodule
,
101 /* rest of Plugin can be NULL/0 */
104 static PluginProxy builtin_so_proxy
= {
105 .extension
= G_MODULE_SUFFIX
,
106 .plugin
= &builtin_so_proxy_plugin
,
109 static GQueue active_proxies
= G_QUEUE_INIT
;
111 static void plugin_free(Plugin
*plugin
);
113 static GeanyData geany_data
;
116 geany_data_init(void)
131 NULL
, /* Remove field on next ABI break (abi-todo) */
140 /* In order to have nested proxies work the count of dependent plugins must propagate up.
141 * This prevents that any plugin in the tree is unloaded while a leaf plugin is active. */
142 static void proxied_count_inc(Plugin
*proxy
)
146 proxy
->proxied_count
+= 1;
147 proxy
= proxy
->proxy
;
148 } while (proxy
!= NULL
);
152 static void proxied_count_dec(Plugin
*proxy
)
154 g_warn_if_fail(proxy
->proxied_count
> 0);
158 proxy
->proxied_count
-= 1;
159 proxy
= proxy
->proxy
;
160 } while (proxy
!= NULL
);
164 /* Prevent the same plugin filename being loaded more than once.
165 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
167 plugin_loaded(Plugin
*plugin
)
169 gchar
*basename_module
, *basename_loaded
;
172 basename_module
= g_path_get_basename(plugin
->filename
);
173 for (item
= plugin_list
; item
!= NULL
; item
= g_list_next(item
))
175 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
177 if (utils_str_equal(basename_module
, basename_loaded
))
179 g_free(basename_loaded
);
180 g_free(basename_module
);
183 g_free(basename_loaded
);
185 /* Look also through the list of active plugins. This prevents problems when we have the same
186 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
187 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
188 * the plugin manager would list both plugins. Additionally, unloading the active plugin
189 * would cause a crash. */
190 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
192 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
194 if (utils_str_equal(basename_module
, basename_loaded
))
196 g_free(basename_loaded
);
197 g_free(basename_module
);
200 g_free(basename_loaded
);
202 g_free(basename_module
);
207 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
211 g_return_val_if_fail(filename
, FALSE
);
213 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
215 if (utils_str_equal(filename
, ((Plugin
*)item
->data
)->filename
))
223 /* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
224 #define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
227 plugin_check_version(Plugin
*plugin
, int plugin_version_code
)
230 if (plugin_version_code
< 0)
232 gchar
*name
= g_path_get_basename(plugin
->filename
);
233 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
234 "release of Geany - please recompile it."), name
);
235 geany_debug("Plugin \"%s\" is not binary compatible with this "
236 "release of Geany - recompile it.", name
);
240 else if (plugin_version_code
> GEANY_API_VERSION
)
242 gchar
*name
= g_path_get_basename(plugin
->filename
);
243 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
244 name
, plugin_version_code
);
253 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
260 cb
= &callbacks
[len
];
261 if (!cb
->signal_name
|| !cb
->callback
)
268 for (i
= 0; i
< len
; i
++)
272 /* Pass the callback data as default user_data if none was set by the plugin itself */
273 plugin_signal_connect(&plugin
->public, NULL
, cb
->signal_name
, cb
->after
,
274 cb
->callback
, cb
->user_data
? cb
->user_data
: plugin
->cb_data
);
279 static void read_key_group(Plugin
*plugin
)
281 GeanyKeyGroupInfo
*p_key_info
;
282 GeanyKeyGroup
**p_key_group
;
283 GModule
*module
= plugin
->proxy_data
;
285 g_module_symbol(module
, "plugin_key_group_info", (void *) &p_key_info
);
286 g_module_symbol(module
, "plugin_key_group", (void *) &p_key_group
);
287 if (p_key_info
&& p_key_group
)
289 GeanyKeyGroupInfo
*key_info
= p_key_info
;
292 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
293 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
299 GeanyKeyGroup
*key_group
=
300 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
302 *p_key_group
= key_group
;
305 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
306 "count field is zero. Maybe use plugin_set_key_group() instead?",
310 else if (p_key_info
|| p_key_group
)
311 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
312 "Maybe use plugin_set_key_group() instead?",
317 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
319 const Plugin
*pa
= a
;
320 const Plugin
*pb
= b
;
322 return strcmp(pa
->info
.name
, pb
->info
.name
);
326 /** Register a plugin to Geany.
328 * The plugin will show up in the plugin manager. The user can interact with
329 * it based on the functions it provides and installed GUI elements.
331 * You must initialize the info and funcs fields of @ref GeanyPlugin
332 * appropriately prior to calling this, otherwise registration will fail. For
333 * info at least a valid name must be set (possibly localized). For funcs,
334 * at least init() and cleanup() functions must be implemented and set.
336 * The return value must be checked. It may be FALSE if the plugin failed to register which can
337 * mainly happen for two reasons (future Geany versions may add new failure conditions):
338 * - Not all mandatory fields of GeanyPlugin have been set.
339 * - The ABI or API versions reported by the plugin are incompatible with the running Geany.
341 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
342 * handles @a api_version and @a abi_version.
344 * @param plugin The plugin provided by Geany
345 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
346 * @param min_api_version The minimum API version required by the plugin
347 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
349 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
351 * @since 1.26 (API 225)
352 * @see GEANY_PLUGIN_REGISTER()
355 gboolean
geany_plugin_register(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
359 GeanyPluginFuncs
*cbs
= plugin
->funcs
;
361 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
364 /* already registered successfully */
365 g_return_val_if_fail(!PLUGIN_LOADED_OK(p
), FALSE
);
367 /* Prevent registering incompatible plugins. */
368 if (! plugin_check_version(p
, PLUGIN_VERSION_CODE(api_version
, abi_version
)))
371 /* Only init and cleanup callbacks are truly mandatory. */
372 if (! cbs
->init
|| ! cbs
->cleanup
)
374 gchar
*name
= g_path_get_basename(p
->filename
);
375 geany_debug("Plugin '%s' has no %s function - ignoring plugin!", name
,
376 cbs
->init
? "cleanup" : "init");
381 /* Yes, name is checked again later on, however we want return FALSE here
382 * to signal the error back to the plugin (but we don't print the message twice) */
383 if (! EMPTY(p
->info
.name
))
384 p
->flags
= LOADED_OK
;
387 /* If it ever becomes necessary we can save the api version in Plugin
388 * and apply compat code on a per-plugin basis, because we learn about
389 * the requested API version here. For now it's not necessary. */
391 return PLUGIN_LOADED_OK(p
);
395 /** Register a plugin to Geany, with plugin-defined data.
397 * This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
398 * Refer to that function for more details on registering in general.
400 * @p pdata is the pointer going to be passed to the individual plugin callbacks
401 * of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
402 * @p pdata, which connects the data to the plugin's module life time.
404 * You cannot use geany_plugin_set_data() after registering with this function. Use
405 * geany_plugin_register() if you need to.
407 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
408 * handles @p api_version and @p abi_version.
410 * @param plugin The plugin provided by Geany.
411 * @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
412 * @param min_api_version The minimum API version required by the plugin.
413 * @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
414 * @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
415 * @param free_func Function used to deallocate @a pdata, may be @c NULL.
417 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
419 * @since 1.26 (API 225)
420 * @see GEANY_PLUGIN_REGISTER_FULL()
421 * @see geany_plugin_register()
424 gboolean
geany_plugin_register_full(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
425 gint abi_version
, gpointer pdata
, GDestroyNotify free_func
)
427 if (geany_plugin_register(plugin
, api_version
, min_api_version
, abi_version
))
429 geany_plugin_set_data(plugin
, pdata
, free_func
);
430 /* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
431 * as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
432 * after GeanyPluginFuncs::cleanup() */
433 plugin
->priv
->flags
|= LOAD_DATA
;
439 struct LegacyRealFuncs
441 void (*init
) (GeanyData
*data
);
442 GtkWidget
* (*configure
) (GtkDialog
*dialog
);
444 void (*cleanup
) (void);
447 /* Wrappers to support legacy plugins are below */
448 static gboolean
legacy_init(GeanyPlugin
*plugin
, gpointer pdata
)
450 struct LegacyRealFuncs
*h
= pdata
;
451 h
->init(plugin
->geany_data
);
455 static void legacy_cleanup(GeanyPlugin
*plugin
, gpointer pdata
)
457 struct LegacyRealFuncs
*h
= pdata
;
458 /* Can be NULL because it's optional for legacy plugins */
463 static void legacy_help(GeanyPlugin
*plugin
, gpointer pdata
)
465 struct LegacyRealFuncs
*h
= pdata
;
469 static GtkWidget
*legacy_configure(GeanyPlugin
*plugin
, GtkDialog
*parent
, gpointer pdata
)
471 struct LegacyRealFuncs
*h
= pdata
;
472 return h
->configure(parent
);
475 static void free_legacy_cbs(gpointer data
)
477 g_slice_free(struct LegacyRealFuncs
, data
);
480 /* This function is the equivalent of geany_plugin_register() for legacy-style
481 * plugins which we continue to load for the time being. */
482 static void register_legacy_plugin(Plugin
*plugin
, GModule
*module
)
484 gint (*p_version_check
) (gint abi_version
);
485 void (*p_set_info
) (PluginInfo
*info
);
486 void (*p_init
) (GeanyData
*geany_data
);
487 GeanyData
**p_geany_data
;
488 struct LegacyRealFuncs
*h
;
490 #define CHECK_FUNC(__x) \
491 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
493 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
494 g_module_name(module)); \
497 CHECK_FUNC(version_check
);
498 CHECK_FUNC(set_info
);
502 /* We must verify the version first. If the plugin has become incompatible any
503 * further actions should be considered invalid and therefore skipped. */
504 if (! plugin_check_version(plugin
, p_version_check(GEANY_ABI_VERSION
)))
507 h
= g_slice_new(struct LegacyRealFuncs
);
509 /* Since the version check passed we can proceed with setting basic fields and
510 * calling its set_info() (which might want to call Geany functions already). */
511 g_module_symbol(module
, "geany_data", (void *) &p_geany_data
);
513 *p_geany_data
= &geany_data
;
514 /* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
515 p_set_info(&plugin
->info
);
517 /* If all went well we can set the remaining callbacks and let it go for good. */
519 g_module_symbol(module
, "plugin_configure", (void *) &h
->configure
);
520 g_module_symbol(module
, "plugin_configure_single", (void *) &plugin
->configure_single
);
521 g_module_symbol(module
, "plugin_help", (void *) &h
->help
);
522 g_module_symbol(module
, "plugin_cleanup", (void *) &h
->cleanup
);
523 /* pointer to callbacks struct can be stored directly, no wrapper necessary */
524 g_module_symbol(module
, "plugin_callbacks", (void *) &plugin
->cbs
.callbacks
);
527 if (h
->configure
&& plugin
->configure_single
)
528 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
529 "only plugin_configure() will be used!",
531 if (h
->cleanup
== NULL
)
532 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
536 plugin
->cbs
.init
= legacy_init
;
537 plugin
->cbs
.cleanup
= legacy_cleanup
;
538 plugin
->cbs
.configure
= h
->configure
? legacy_configure
: NULL
;
539 plugin
->cbs
.help
= h
->help
? legacy_help
: NULL
;
541 plugin
->flags
= LOADED_OK
| IS_LEGACY
;
542 geany_plugin_set_data(&plugin
->public, h
, free_legacy_cbs
);
547 plugin_load(Plugin
*plugin
)
549 gboolean init_ok
= TRUE
;
551 /* Start the plugin. Legacy plugins require additional cruft. */
552 if (PLUGIN_IS_LEGACY(plugin
) && plugin
->proxy
== &builtin_so_proxy_plugin
)
554 GeanyPlugin
**p_geany_plugin
;
556 PluginFields
**plugin_fields
;
557 GModule
*module
= plugin
->proxy_data
;
558 /* set these symbols before plugin_init() is called
559 * we don't set geany_data since it is set directly by plugin_new() */
560 g_module_symbol(module
, "geany_plugin", (void *) &p_geany_plugin
);
562 *p_geany_plugin
= &plugin
->public;
563 g_module_symbol(module
, "plugin_info", (void *) &p_info
);
565 *p_info
= &plugin
->info
;
566 g_module_symbol(module
, "plugin_fields", (void *) &plugin_fields
);
568 *plugin_fields
= &plugin
->fields
;
569 read_key_group(plugin
);
571 /* Legacy plugin_init() cannot fail. */
572 plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
574 /* now read any plugin-owned data that might have been set in plugin_init() */
575 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
577 ui_add_document_sensitive(plugin
->fields
.menu_item
);
582 init_ok
= plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
588 /* new-style plugins set their callbacks in geany_load_module() */
589 if (plugin
->cbs
.callbacks
)
590 add_callbacks(plugin
, plugin
->cbs
.callbacks
);
592 /* remember which plugins are active.
593 * keep list sorted so tools menu items and plugin preference tabs are
594 * sorted by plugin name */
595 active_plugin_list
= g_list_insert_sorted(active_plugin_list
, plugin
, cmp_plugin_names
);
596 proxied_count_inc(plugin
->proxy
);
598 geany_debug("Loaded: %s (%s)", plugin
->filename
, plugin
->info
.name
);
603 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, const gchar
*fname
, gpointer pdata
)
606 void (*p_geany_load_module
)(GeanyPlugin
*);
608 g_return_val_if_fail(g_module_supported(), NULL
);
609 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
610 * causing a segfault. Without that flag the module will safely fail to load.
611 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
612 * otherwise not be detected due to the shadowing of Geany's app variable.
613 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
614 * function from a plugin will be shadowed. */
615 module
= g_module_open(fname
, G_MODULE_BIND_LOCAL
);
618 geany_debug("Can't load plugin: %s", g_module_error());
622 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
623 g_module_symbol(module
, "geany_load_module", (void *) &p_geany_load_module
);
624 if (p_geany_load_module
)
626 /* set this here already so plugins can call i.e. plugin_module_make_resident()
627 * right from their geany_load_module() */
628 subplugin
->priv
->proxy_data
= module
;
630 /* This is a new style plugin. It should fill in plugin->info and then call
631 * geany_plugin_register() in its geany_load_module() to successfully load.
632 * The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
633 * We check the LOADED_OK flag separately to protect us against buggy plugins
634 * who ignore the result of geany_plugin_register() and register anyway */
635 p_geany_load_module(subplugin
);
639 /* This is the legacy / deprecated code path. It does roughly the same as
640 * geany_load_module() and geany_plugin_register() together for the new ones */
641 register_legacy_plugin(subplugin
->priv
, module
);
643 /* We actually check the LOADED_OK flag later */
648 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, gpointer load_data
, gpointer pdata
)
650 GModule
*module
= (GModule
*) load_data
;
652 g_return_if_fail(module
!= NULL
);
654 if (! g_module_close(module
))
655 g_warning("%s: %s", subplugin
->priv
->filename
, g_module_error());
659 /* Load and optionally init a plugin.
660 * load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
661 * called, the plugin will be started, if not the plugin will be read only (for the list of
662 * available plugins in the plugin manager).
663 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
665 plugin_new(Plugin
*proxy
, const gchar
*fname
, gboolean load_plugin
, gboolean add_to_list
)
669 g_return_val_if_fail(fname
, NULL
);
670 g_return_val_if_fail(proxy
, NULL
);
672 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
674 plugin
= find_active_plugin_by_name(fname
);
677 geany_debug("Plugin \"%s\" already loaded.", fname
);
680 /* do not add to the list twice */
681 if (g_list_find(plugin_list
, plugin
) != NULL
)
684 plugin_list
= g_list_prepend(plugin_list
, plugin
);
689 plugin
= g_new0(Plugin
, 1);
690 plugin
->filename
= g_strdup(fname
);
691 plugin
->proxy
= proxy
;
692 plugin
->public.geany_data
= &geany_data
;
693 plugin
->public.priv
= plugin
;
694 /* Fields of plugin->info/funcs must to be initialized by the plugin */
695 plugin
->public.info
= &plugin
->info
;
696 plugin
->public.funcs
= &plugin
->cbs
;
697 plugin
->public.proxy_funcs
= &plugin
->proxy_cbs
;
699 if (plugin_loaded(plugin
))
701 geany_debug("Plugin \"%s\" already loaded.", fname
);
705 /* Load plugin, this should read its name etc. It must also call
706 * geany_plugin_register() for the following PLUGIN_LOADED_OK condition */
707 plugin
->proxy_data
= proxy
->proxy_cbs
.load(&proxy
->public, &plugin
->public, fname
, proxy
->cb_data
);
709 if (! PLUGIN_LOADED_OK(plugin
))
711 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname
);
715 /* The proxy assumes success, therefore we have to call unload from here
716 * on in case of errors */
717 if (EMPTY(plugin
->info
.name
))
719 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname
);
723 /* cb_data_destroy() frees plugin->cb_data. If that pointer also passed to unload() afterwards
724 * then that would become a use-after-free. Disallow this combination. If a proxy
725 * needs the same pointer it must not use a destroy func but free manually in its unload(). */
726 if (plugin
->proxy_data
== proxy
->cb_data
&& plugin
->cb_data_destroy
)
728 geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname
);
729 plugin
->proxy_data
= NULL
;
733 if (load_plugin
&& !plugin_load(plugin
))
735 /* Handle failing init same as failing to load for now. In future we
736 * could present a informational UI or something */
737 geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname
);
742 plugin_list
= g_list_prepend(plugin_list
, plugin
);
747 if (plugin
->cb_data_destroy
)
748 plugin
->cb_data_destroy(plugin
->cb_data
);
749 proxy
->proxy_cbs
.unload(&proxy
->public, &plugin
->public, plugin
->proxy_data
, proxy
->cb_data
);
751 g_free(plugin
->filename
);
757 static void on_object_weak_notify(gpointer data
, GObject
*old_ptr
)
759 Plugin
*plugin
= data
;
762 g_return_if_fail(plugin
&& plugin
->signal_ids
);
764 for (i
= 0; i
< plugin
->signal_ids
->len
; i
++)
766 SignalConnection
*sc
= &g_array_index(plugin
->signal_ids
, SignalConnection
, i
);
768 if (sc
->object
== old_ptr
)
770 g_array_remove_index_fast(plugin
->signal_ids
, i
);
771 /* we can break the loop right after finding the first match,
772 * because we will get one notification per connected signal */
779 /* add an object to watch for destruction, and release pointers to it when destroyed.
780 * this should only be used by plugin_signal_connect() to add a watch on
781 * the object lifetime and nuke out references to it in plugin->signal_ids */
782 void plugin_watch_object(Plugin
*plugin
, gpointer object
)
784 g_object_weak_ref(object
, on_object_weak_notify
, plugin
);
788 static void remove_callbacks(Plugin
*plugin
)
790 GArray
*signal_ids
= plugin
->signal_ids
;
791 SignalConnection
*sc
;
793 if (signal_ids
== NULL
)
796 foreach_array(SignalConnection
, sc
, signal_ids
)
798 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
799 g_object_weak_unref(sc
->object
, on_object_weak_notify
, plugin
);
802 g_array_free(signal_ids
, TRUE
);
806 static void remove_sources(Plugin
*plugin
)
810 item
= plugin
->sources
;
813 GList
*next
= item
->next
; /* cache the next pointer because current item will be freed */
815 g_source_destroy(item
->data
);
818 /* don't free the list here, it is allocated inside each source's data */
822 /* Make the GModule backing plugin resident (if it's GModule-backed at all) */
823 void plugin_make_resident(Plugin
*plugin
)
825 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
827 g_return_if_fail(plugin
->proxy_data
!= NULL
);
828 g_module_make_resident(plugin
->proxy_data
);
831 g_warning("Skipping g_module_make_resident() for non-native plugin");
835 /* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
836 gpointer
plugin_get_module_symbol(Plugin
*plugin
, const gchar
*sym
)
840 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
842 g_return_val_if_fail(plugin
->proxy_data
!= NULL
, NULL
);
843 if (g_module_symbol(plugin
->proxy_data
, sym
, &symbol
))
846 g_warning("Failed to locate signal handler for '%s': %s",
847 sym
, g_module_error());
849 else /* TODO: Could possibly support this via a new proxy hook */
850 g_warning("Failed to locate signal handler for '%s': Not supported for non-native plugins",
856 static gboolean
is_active_plugin(Plugin
*plugin
)
858 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
862 static void remove_each_doc_data(GQuark key_id
, gpointer data
, gpointer user_data
)
864 const ForEachDocData
*doc_data
= user_data
;
865 const gchar
*key
= g_quark_to_string(key_id
);
866 if (g_str_has_prefix(key
, doc_data
->prefix
))
867 g_datalist_remove_data(&doc_data
->document
->priv
->data
, key
);
871 static void remove_doc_data(Plugin
*plugin
)
875 data
.prefix
= g_strdup_printf("geany/plugins/%s/", plugin
->public.info
->name
);
877 for (guint i
= 0; i
< documents_array
->len
; i
++)
879 GeanyDocument
*doc
= documents_array
->pdata
[i
];
883 g_datalist_foreach(&doc
->priv
->data
, remove_each_doc_data
, &data
);
891 /* Clean up anything used by an active plugin */
893 plugin_cleanup(Plugin
*plugin
)
897 /* With geany_register_plugin cleanup is mandatory */
898 plugin
->cbs
.cleanup(&plugin
->public, plugin
->cb_data
);
900 remove_doc_data(plugin
);
901 remove_callbacks(plugin
);
902 remove_sources(plugin
);
904 if (plugin
->key_group
)
905 keybindings_free_group(plugin
->key_group
);
907 widget
= plugin
->toolbar_separator
.widget
;
909 gtk_widget_destroy(widget
);
911 if (!PLUGIN_HAS_LOAD_DATA(plugin
) && plugin
->cb_data_destroy
)
913 /* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
914 * if it was already set through geany_plugin_register_full() because we couldn't call
915 * its init() anymore (not without completely reloading it anyway). */
916 plugin
->cb_data_destroy(plugin
->cb_data
);
917 plugin
->cb_data
= NULL
;
918 plugin
->cb_data_destroy
= NULL
;
921 proxied_count_dec(plugin
->proxy
);
922 geany_debug("Unloaded: %s", plugin
->filename
);
926 /* Remove all plugins that proxy is a proxy for from plugin_list (and free) */
927 static void free_subplugins(Plugin
*proxy
)
934 GList
*next
= g_list_next(item
);
935 if (proxy
== ((Plugin
*) item
->data
)->proxy
)
937 /* plugin_free modifies plugin_list */
938 plugin_free((Plugin
*) item
->data
);
945 /* Returns true if the removal was successful (=> never for non-proxies) */
946 static gboolean
unregister_proxy(Plugin
*proxy
)
948 gboolean is_proxy
= FALSE
;
951 /* Remove the proxy from the proxy list first. It might appear more than once (once
952 * for each extension), but if it doesn't appear at all it's not actually a proxy */
953 foreach_list_safe(node
, active_proxies
.head
)
955 PluginProxy
*p
= node
->data
;
956 if (p
->plugin
== proxy
)
959 g_queue_delete_link(&active_proxies
, node
);
966 /* Cleanup a plugin and free all resources allocated on behalf of it.
968 * If the plugin is a proxy then this also takes special care to unload all
969 * subplugin loaded through it (make sure none of them is active!) */
971 plugin_free(Plugin
*plugin
)
975 g_return_if_fail(plugin
);
976 g_return_if_fail(plugin
->proxy
);
977 g_return_if_fail(plugin
->proxied_count
== 0);
979 proxy
= plugin
->proxy
;
980 /* If this a proxy remove all depending subplugins. We can assume none of them is *activated*
981 * (but potentially loaded). Note that free_subplugins() might call us through recursion */
982 if (is_active_plugin(plugin
))
984 if (unregister_proxy(plugin
))
985 free_subplugins(plugin
);
986 plugin_cleanup(plugin
);
989 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
990 plugin_list
= g_list_remove(plugin_list
, plugin
);
992 /* cb_data_destroy might be plugin code and must be called before unloading the module. */
993 if (plugin
->cb_data_destroy
)
994 plugin
->cb_data_destroy(plugin
->cb_data
);
995 proxy
->proxy_cbs
.unload(&proxy
->public, &plugin
->public, plugin
->proxy_data
, proxy
->cb_data
);
997 g_free(plugin
->filename
);
1002 static gchar
*get_custom_plugin_path(const gchar
*plugin_path_config
,
1003 const gchar
*plugin_path_system
)
1005 gchar
*plugin_path_custom
;
1007 if (EMPTY(prefs
.custom_plugin_path
))
1010 plugin_path_custom
= utils_get_locale_from_utf8(prefs
.custom_plugin_path
);
1011 utils_tidy_path(plugin_path_custom
);
1013 /* check whether the custom plugin path is one of the system or user plugin paths
1014 * and abort if so */
1015 if (utils_str_equal(plugin_path_custom
, plugin_path_config
) ||
1016 utils_str_equal(plugin_path_custom
, plugin_path_system
))
1018 g_free(plugin_path_custom
);
1021 return plugin_path_custom
;
1025 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
1026 * so we need to check active plugins are in the right place before loading */
1027 static gboolean
check_plugin_path(const gchar
*fname
)
1029 gchar
*plugin_path_config
;
1030 gchar
*plugin_path_system
;
1031 gchar
*plugin_path_custom
;
1032 gboolean ret
= FALSE
;
1034 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1035 if (g_str_has_prefix(fname
, plugin_path_config
))
1038 plugin_path_system
= get_plugin_path();
1039 if (g_str_has_prefix(fname
, plugin_path_system
))
1042 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1043 if (plugin_path_custom
)
1045 if (g_str_has_prefix(fname
, plugin_path_custom
))
1048 g_free(plugin_path_custom
);
1050 g_free(plugin_path_config
);
1051 g_free(plugin_path_system
);
1056 /* Returns NULL if this ain't a plugin,
1057 * otherwise it returns the appropriate PluginProxy instance to load it */
1058 static PluginProxy
* is_plugin(const gchar
*file
)
1063 /* extract file extension to avoid g_str_has_suffix() in the loop */
1064 ext
= (const gchar
*)strrchr(file
, '.');
1067 /* ensure the dot is really part of the filename */
1068 else if (strchr(ext
, G_DIR_SEPARATOR
) != NULL
)
1072 /* O(n*m), (m being extensions per proxy) doesn't scale very well in theory
1073 * but not a problem in practice yet */
1074 foreach_list(node
, active_proxies
.head
)
1076 PluginProxy
*proxy
= node
->data
;
1077 if (utils_str_casecmp(ext
, proxy
->extension
) == 0)
1079 Plugin
*p
= proxy
->plugin
;
1080 gint ret
= GEANY_PROXY_MATCH
;
1082 if (p
->proxy_cbs
.probe
)
1083 ret
= p
->proxy_cbs
.probe(&p
->public, file
, p
->cb_data
);
1086 case GEANY_PROXY_MATCH
:
1088 case GEANY_PROXY_RELATED
:
1090 case GEANY_PROXY_IGNORE
:
1093 g_warning("Ignoring bogus return value '%d' from "
1094 "proxy plugin '%s' probe() function!", ret
,
1095 proxy
->plugin
->info
.name
);
1104 /* load active plugins at startup */
1106 load_active_plugins(void)
1108 guint i
, len
, proxies
;
1110 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
1113 /* If proxys are loaded we have to restart to load plugins that sort before their proxy */
1116 proxies
= active_proxies
.length
;
1117 g_list_free_full(failed_plugins_list
, (GDestroyNotify
) g_free
);
1118 failed_plugins_list
= NULL
;
1119 for (i
= 0; i
< len
; i
++)
1121 gchar
*fname
= active_plugins_pref
[i
];
1124 /* ensure we have canonical paths */
1126 while ((p
= strchr(p
, '/')) != NULL
)
1127 *p
= G_DIR_SEPARATOR
;
1130 if (!EMPTY(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
1132 PluginProxy
*proxy
= NULL
;
1133 if (check_plugin_path(fname
))
1134 proxy
= is_plugin(fname
);
1135 if (proxy
== NULL
|| plugin_new(proxy
->plugin
, fname
, TRUE
, FALSE
) == NULL
)
1136 failed_plugins_list
= g_list_prepend(failed_plugins_list
, g_strdup(fname
));
1139 } while (proxies
!= active_proxies
.length
);
1144 load_plugins_from_path(const gchar
*path
)
1146 GSList
*list
, *item
;
1149 list
= utils_get_file_list(path
, NULL
, NULL
);
1151 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
1153 gchar
*fname
= g_build_filename(path
, item
->data
, NULL
);
1154 PluginProxy
*proxy
= is_plugin(fname
);
1156 if (proxy
!= NULL
&& plugin_new(proxy
->plugin
, fname
, FALSE
, TRUE
))
1162 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
1166 geany_debug("Added %d plugin(s) in '%s'.", count
, path
);
1170 static gchar
*get_plugin_path(void)
1172 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN
));
1176 /* See load_all_plugins(), this simply sorts items with lower hierarchy level first
1177 * (where hierarchy level == number of intermediate proxies before the builtin so loader) */
1178 static gint
cmp_plugin_by_proxy(gconstpointer a
, gconstpointer b
)
1180 const Plugin
*pa
= a
;
1181 const Plugin
*pb
= b
;
1185 if (pa
->proxy
== pb
->proxy
)
1187 else if (pa
->proxy
== &builtin_so_proxy_plugin
)
1189 else if (pb
->proxy
== &builtin_so_proxy_plugin
)
1198 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1199 static void load_all_plugins(void)
1201 gchar
*plugin_path_config
;
1202 gchar
*plugin_path_system
;
1203 gchar
*plugin_path_custom
;
1205 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1206 plugin_path_system
= get_plugin_path();
1208 /* first load plugins in ~/.config/geany/plugins/ */
1209 load_plugins_from_path(plugin_path_config
);
1211 /* load plugins from a custom path */
1212 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1213 if (plugin_path_custom
)
1215 load_plugins_from_path(plugin_path_custom
);
1216 g_free(plugin_path_custom
);
1219 /* finally load plugins from $prefix/lib/geany */
1220 load_plugins_from_path(plugin_path_system
);
1222 /* It is important to sort any plugins that are proxied after their proxy because
1223 * pm_populate() needs the proxy to be loaded and active (if selected by user) in order
1224 * to properly set the value for the PLUGIN_COLUMN_CAN_UNCHECK column. The order between
1225 * sub-plugins does not matter, only between sub-plugins and their proxy, thus
1226 * sorting by hierarchy level is perfectly sufficient */
1227 plugin_list
= g_list_sort(plugin_list
, cmp_plugin_by_proxy
);
1229 g_free(plugin_path_config
);
1230 g_free(plugin_path_system
);
1234 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
1236 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
1238 gboolean have_plugin_menu_items
= FALSE
;
1240 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
1242 if (item
->data
== menu_separator
)
1244 if (i
< g_list_length(list
) - 1)
1246 have_plugin_menu_items
= TRUE
;
1254 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
1258 /* Calling this starts up plugin support */
1259 void plugins_load_active(void)
1263 want_plugins
= TRUE
;
1267 widget
= gtk_separator_menu_item_new();
1268 gtk_widget_show(widget
);
1269 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1271 widget
= gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1272 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1273 gtk_widget_show(widget
);
1274 g_signal_connect(widget
, "activate", G_CALLBACK(pm_show_dialog
), NULL
);
1276 menu_separator
= gtk_separator_menu_item_new();
1277 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
1278 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
1280 load_active_plugins();
1284 /* Update the global active plugins list so it's up-to-date when configuration
1285 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1286 static void update_active_plugins_pref(void)
1292 /* if plugins are disabled, don't clear list of active plugins */
1296 count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
1298 g_strfreev(active_plugins_pref
);
1302 active_plugins_pref
= NULL
;
1306 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
1308 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
1310 Plugin
*plugin
= list
->data
;
1312 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
1315 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
1317 const gchar
*fname
= list
->data
;
1319 active_plugins_pref
[i
] = g_strdup(fname
);
1322 active_plugins_pref
[i
] = NULL
;
1326 /* called even if plugin support is disabled */
1327 void plugins_init(void)
1332 path
= get_plugin_path();
1333 geany_debug("System plugin path: %s", path
);
1336 group
= stash_group_new("plugins");
1337 configuration_add_pref_group(group
, TRUE
);
1339 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
1340 "load_plugins", TRUE
, "check_plugins");
1341 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
1342 "custom_plugin_path", "", "extra_plugin_path_entry");
1344 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(update_active_plugins_pref
), NULL
);
1345 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
1347 g_queue_push_head(&active_proxies
, &builtin_so_proxy
);
1351 /* Same as plugin_free(), except it does nothing for proxies-in-use, to be called on
1352 * finalize in a loop */
1353 static void plugin_free_leaf(Plugin
*p
)
1355 if (p
->proxied_count
== 0)
1360 /* called even if plugin support is disabled */
1361 void plugins_finalize(void)
1363 if (failed_plugins_list
!= NULL
)
1365 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
1366 g_list_free(failed_plugins_list
);
1368 /* Have to loop because proxys cannot be unloaded until after all their
1369 * plugins are unloaded as well (the second loop should should catch all the remaining ones) */
1370 while (active_plugin_list
!= NULL
)
1371 g_list_foreach(active_plugin_list
, (GFunc
) plugin_free_leaf
, NULL
);
1373 g_strfreev(active_plugins_pref
);
1377 /* Check whether there are any plugins loaded which provide a configure symbol */
1378 gboolean
plugins_have_preferences(void)
1382 if (active_plugin_list
== NULL
)
1385 foreach_list(item
, active_plugin_list
)
1387 Plugin
*plugin
= item
->data
;
1388 if (plugin
->configure_single
!= NULL
|| plugin
->cbs
.configure
!= NULL
)
1396 /* Plugin Manager */
1400 PLUGIN_COLUMN_CHECK
= 0,
1401 PLUGIN_COLUMN_CAN_UNCHECK
,
1402 PLUGIN_COLUMN_PLUGIN
,
1404 PM_BUTTON_KEYBINDINGS
,
1405 PM_BUTTON_CONFIGURE
,
1413 GtkTreeStore
*store
;
1414 GtkWidget
*filter_entry
;
1415 GtkWidget
*configure_button
;
1416 GtkWidget
*keybindings_button
;
1417 GtkWidget
*help_button
;
1418 GtkWidget
*popup_menu
;
1419 GtkWidget
*popup_configure_menu_item
;
1420 GtkWidget
*popup_keybindings_menu_item
;
1421 GtkWidget
*popup_help_menu_item
;
1423 PluginManagerWidgets
;
1425 static PluginManagerWidgets pm_widgets
;
1428 static void pm_update_buttons(Plugin
*p
)
1430 gboolean has_configure
= FALSE
;
1431 gboolean has_help
= FALSE
;
1432 gboolean has_keybindings
= FALSE
;
1434 if (p
!= NULL
&& is_active_plugin(p
))
1436 has_configure
= p
->cbs
.configure
|| p
->configure_single
;
1437 has_help
= p
->cbs
.help
!= NULL
;
1438 has_keybindings
= p
->key_group
&& p
->key_group
->plugin_key_count
;
1441 gtk_widget_set_sensitive(pm_widgets
.configure_button
, has_configure
);
1442 gtk_widget_set_sensitive(pm_widgets
.help_button
, has_help
);
1443 gtk_widget_set_sensitive(pm_widgets
.keybindings_button
, has_keybindings
);
1445 gtk_widget_set_sensitive(pm_widgets
.popup_configure_menu_item
, has_configure
);
1446 gtk_widget_set_sensitive(pm_widgets
.popup_help_menu_item
, has_help
);
1447 gtk_widget_set_sensitive(pm_widgets
.popup_keybindings_menu_item
, has_keybindings
);
1451 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
1454 GtkTreeModel
*model
;
1457 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1459 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1462 pm_update_buttons(p
);
1467 static gboolean
find_iter_for_plugin(Plugin
*p
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
1472 for (valid
= gtk_tree_model_get_iter_first(model
, iter
);
1474 valid
= gtk_tree_model_iter_next(model
, iter
))
1476 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &pp
, -1);
1485 static void pm_populate(GtkTreeStore
*store
);
1488 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
1490 gboolean old_state
, state
;
1493 GtkTreeIter store_iter
;
1494 GtkTreePath
*path
= gtk_tree_path_new_from_string(pth
);
1495 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
1498 guint prev_num_proxies
;
1500 gtk_tree_model_get_iter(model
, &iter
, path
);
1502 gtk_tree_model_get(model
, &iter
,
1503 PLUGIN_COLUMN_CHECK
, &old_state
,
1504 PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1506 /* no plugins item */
1509 gtk_tree_path_free(path
);
1513 gtk_tree_model_filter_convert_iter_to_child_iter(
1514 GTK_TREE_MODEL_FILTER(model
), &store_iter
, &iter
);
1516 state
= ! old_state
; /* toggle the state */
1518 /* save the filename and proxy of the plugin */
1519 file_name
= g_strdup(p
->filename
);
1521 prev_num_proxies
= active_proxies
.length
;
1523 /* unload plugin module */
1525 /* save shortcuts (only need this group, but it doesn't take long) */
1526 keybindings_write_to_file();
1528 /* plugin_new() below may cause a tree view refresh with invalid p - set to NULL */
1529 gtk_tree_store_set(pm_widgets
.store
, &store_iter
,
1530 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1533 /* reload plugin module and initialize it if item is checked */
1534 p
= plugin_new(proxy
, file_name
, state
, TRUE
);
1537 /* plugin file may no longer be on disk, or is now incompatible */
1538 gtk_tree_store_remove(pm_widgets
.store
, &store_iter
);
1543 keybindings_load_keyfile(); /* load shortcuts */
1546 gtk_tree_store_set(pm_widgets
.store
, &store_iter
,
1547 PLUGIN_COLUMN_CHECK
, state
,
1548 PLUGIN_COLUMN_PLUGIN
, p
, -1);
1550 /* set again the sensitiveness of the configure and help buttons */
1551 pm_update_buttons(p
);
1553 /* Depending on the state disable the checkbox for the proxy of this plugin, and
1554 * only re-enable if the proxy is not used by any other plugin */
1555 if (p
->proxy
!= &builtin_so_proxy_plugin
)
1558 gboolean can_uncheck
;
1559 GtkTreePath
*store_path
= gtk_tree_model_filter_convert_path_to_child_path(
1560 GTK_TREE_MODEL_FILTER(model
), path
);
1562 g_warn_if_fail(store_path
!= NULL
);
1563 if (gtk_tree_path_up(store_path
))
1565 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets
.store
), &parent
, store_path
);
1568 can_uncheck
= FALSE
;
1570 can_uncheck
= p
->proxy
->proxied_count
== 0;
1572 gtk_tree_store_set(pm_widgets
.store
, &parent
,
1573 PLUGIN_COLUMN_CAN_UNCHECK
, can_uncheck
, -1);
1575 gtk_tree_path_free(store_path
);
1578 /* We need to find out if a proxy was added or removed because that affects the plugin list
1579 * presented by the plugin manager */
1580 if (prev_num_proxies
!= active_proxies
.length
)
1582 /* Rescan the plugin list as we now support more. Gives some "already loaded" warnings
1583 * they are unproblematic */
1584 if (prev_num_proxies
< active_proxies
.length
)
1587 pm_populate(pm_widgets
.store
);
1588 gtk_tree_view_expand_row(GTK_TREE_VIEW(pm_widgets
.tree
), path
, FALSE
);
1591 gtk_tree_path_free(path
);
1595 static void pm_populate(GtkTreeStore
*store
)
1600 gtk_tree_store_clear(store
);
1601 list
= g_list_first(plugin_list
);
1604 gtk_tree_store_append(store
, &iter
, NULL
);
1605 gtk_tree_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1606 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1610 for (; list
!= NULL
; list
= list
->next
)
1612 Plugin
*p
= list
->data
;
1615 if (p
->proxy
!= &builtin_so_proxy_plugin
1616 && find_iter_for_plugin(p
->proxy
, GTK_TREE_MODEL(pm_widgets
.store
), &parent
))
1617 gtk_tree_store_append(store
, &iter
, &parent
);
1619 gtk_tree_store_append(store
, &iter
, NULL
);
1621 gtk_tree_store_set(store
, &iter
,
1622 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1623 PLUGIN_COLUMN_PLUGIN
, p
,
1624 PLUGIN_COLUMN_CAN_UNCHECK
, (p
->proxied_count
== 0),
1630 static gboolean
pm_treeview_query_tooltip(GtkWidget
*widget
, gint x
, gint y
,
1631 gboolean keyboard_mode
, GtkTooltip
*tooltip
, gpointer user_data
)
1633 GtkTreeModel
*model
;
1637 gboolean can_uncheck
= TRUE
;
1639 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget
), &x
, &y
, keyboard_mode
,
1640 &model
, &path
, &iter
))
1643 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, PLUGIN_COLUMN_CAN_UNCHECK
, &can_uncheck
, -1);
1646 gchar
*prefix
, *suffix
, *details
, *markup
;
1649 uchk
= can_uncheck
?
1650 "" : _("\n<i>Other plugins depend on this. Disable them first to allow deactivation.</i>\n");
1651 /* Four allocations is less than ideal but meh */
1652 details
= g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1653 p
->info
.version
, p
->info
.author
, p
->filename
);
1654 prefix
= g_markup_printf_escaped("<b>%s</b>\n%s\n", p
->info
.name
, p
->info
.description
);
1655 suffix
= g_markup_printf_escaped("<small><i>\n%s</i></small>", details
);
1656 markup
= g_strconcat(prefix
, uchk
, suffix
, NULL
);
1658 gtk_tooltip_set_markup(tooltip
, markup
);
1659 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget
), tooltip
, path
);
1666 gtk_tree_path_free(path
);
1672 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn
*column
, GtkCellRenderer
*cell
,
1673 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer data
)
1677 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1680 g_object_set(cell
, "text", _("No plugins available."), NULL
);
1683 gchar
*markup
= g_markup_printf_escaped("<b>%s</b>\n%s", p
->info
.name
, p
->info
.description
);
1685 g_object_set(cell
, "markup", markup
, NULL
);
1691 static gboolean
pm_treeview_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
1692 G_GNUC_UNUSED gpointer user_data
)
1694 if (event
->button
== 3)
1696 gtk_menu_popup(GTK_MENU(pm_widgets
.popup_menu
), NULL
, NULL
, NULL
, NULL
,
1697 event
->button
, event
->time
);
1703 static gint
pm_tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1708 gtk_tree_model_get(model
, a
, PLUGIN_COLUMN_PLUGIN
, &pa
, -1);
1709 gtk_tree_model_get(model
, b
, PLUGIN_COLUMN_PLUGIN
, &pb
, -1);
1712 return strcmp(pa
->info
.name
, pb
->info
.name
);
1718 static gboolean
pm_tree_search(const gchar
*key
, const gchar
*haystack
)
1720 gchar
*normalized_string
= NULL
;
1721 gchar
*normalized_key
= NULL
;
1722 gchar
*case_normalized_string
= NULL
;
1723 gchar
*case_normalized_key
= NULL
;
1724 gboolean matched
= TRUE
;
1726 normalized_string
= g_utf8_normalize(haystack
, -1, G_NORMALIZE_ALL
);
1727 normalized_key
= g_utf8_normalize(key
, -1, G_NORMALIZE_ALL
);
1729 if (normalized_string
!= NULL
&& normalized_key
!= NULL
)
1731 GString
*stripped_key
;
1732 gchar
**subkey
, **subkeys
;
1734 case_normalized_string
= g_utf8_casefold(normalized_string
, -1);
1735 case_normalized_key
= g_utf8_casefold(normalized_key
, -1);
1736 stripped_key
= g_string_new(case_normalized_key
);
1737 do {} while (utils_string_replace_all(stripped_key
, " ", " "));
1738 subkeys
= g_strsplit(stripped_key
->str
, " ", -1);
1739 g_string_free(stripped_key
, TRUE
);
1740 foreach_strv(subkey
, subkeys
)
1742 if (strstr(case_normalized_string
, *subkey
) == NULL
)
1748 g_strfreev(subkeys
);
1751 g_free(normalized_key
);
1752 g_free(normalized_string
);
1753 g_free(case_normalized_key
);
1754 g_free(case_normalized_string
);
1760 static gboolean
pm_tree_filter_func(GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1765 gchar
*haystack
, *filename
;
1767 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &plugin
, -1);
1771 key
= gtk_entry_get_text(GTK_ENTRY(pm_widgets
.filter_entry
));
1773 filename
= g_path_get_basename(plugin
->filename
);
1774 haystack
= g_strjoin(" ", plugin
->info
.name
, plugin
->info
.description
,
1775 plugin
->info
.author
, filename
, NULL
);
1776 matched
= pm_tree_search(key
, haystack
);
1784 static void on_pm_tree_filter_entry_changed_cb(GtkEntry
*entry
, gpointer user_data
)
1786 GtkTreeModel
*filter_model
= gtk_tree_view_get_model(GTK_TREE_VIEW(pm_widgets
.tree
));
1787 gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(filter_model
));
1791 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry
*entry
, GtkEntryIconPosition icon_pos
,
1792 GdkEvent
*event
, gpointer user_data
)
1794 if (event
->button
.button
== 1 && icon_pos
== GTK_ENTRY_ICON_PRIMARY
)
1795 on_pm_tree_filter_entry_changed_cb(entry
, user_data
);
1799 static void pm_prepare_treeview(GtkWidget
*tree
, GtkTreeStore
*store
)
1801 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1802 GtkTreeViewColumn
*column
;
1803 GtkTreeModel
*filter_model
;
1804 GtkTreeSelection
*sel
;
1806 g_signal_connect(tree
, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip
), NULL
);
1807 gtk_widget_set_has_tooltip(tree
, TRUE
);
1808 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
1810 checkbox_renderer
= gtk_cell_renderer_toggle_new();
1811 column
= gtk_tree_view_column_new_with_attributes(
1812 _("Active"), checkbox_renderer
,
1813 "active", PLUGIN_COLUMN_CHECK
, "activatable", PLUGIN_COLUMN_CAN_UNCHECK
, NULL
);
1814 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1815 g_signal_connect(checkbox_renderer
, "toggled", G_CALLBACK(pm_plugin_toggled
), NULL
);
1817 text_renderer
= gtk_cell_renderer_text_new();
1818 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1819 column
= gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer
, NULL
);
1820 gtk_tree_view_column_set_cell_data_func(column
, text_renderer
,
1821 pm_treeview_text_cell_data_func
, NULL
, NULL
);
1822 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1824 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
1825 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree
), FALSE
);
1826 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
,
1827 pm_tree_sort_func
, NULL
, NULL
);
1828 gtk_tree_sortable_set_sort_column_id(
1829 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
, GTK_SORT_ASCENDING
);
1831 /* selection handling */
1832 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
1833 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_SINGLE
);
1834 g_signal_connect(sel
, "changed", G_CALLBACK(pm_selection_changed
), NULL
);
1836 g_signal_connect(tree
, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb
), NULL
);
1839 filter_model
= gtk_tree_model_filter_new(GTK_TREE_MODEL(store
), NULL
);
1840 gtk_tree_model_filter_set_visible_func(
1841 GTK_TREE_MODEL_FILTER(filter_model
), pm_tree_filter_func
, NULL
, NULL
);
1843 /* set model to tree view */
1844 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), filter_model
);
1845 g_object_unref(filter_model
);
1851 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton
*button
, gpointer user_data
)
1853 GtkTreeModel
*model
;
1854 GtkTreeSelection
*selection
;
1858 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1859 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1861 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1865 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1866 plugin_show_configure(&p
->public);
1867 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
)
1868 p
->cbs
.help(&p
->public, p
->cb_data
);
1869 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_KEYBINDINGS
&& p
->key_group
&& p
->key_group
->plugin_key_count
> 0)
1870 keybindings_dialog_show_prefs_scroll(p
->info
.name
);
1877 free_non_active_plugin(gpointer data
, gpointer user_data
)
1879 Plugin
*plugin
= data
;
1881 /* don't do anything when closing the plugin manager and it is an active plugin */
1882 if (is_active_plugin(plugin
))
1885 plugin_free(plugin
);
1889 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1890 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1891 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1895 case GTK_RESPONSE_CLOSE
:
1896 case GTK_RESPONSE_DELETE_EVENT
:
1897 if (plugin_list
!= NULL
)
1899 /* remove all non-active plugins from the list */
1900 g_list_foreach(plugin_list
, free_non_active_plugin
, NULL
);
1901 g_list_free(plugin_list
);
1904 gtk_widget_destroy(GTK_WIDGET(dialog
));
1905 pm_widgets
.dialog
= NULL
;
1907 configuration_save();
1909 case PM_BUTTON_CONFIGURE
:
1910 case PM_BUTTON_HELP
:
1911 case PM_BUTTON_KEYBINDINGS
:
1912 /* forward event to the generic handler */
1913 pm_on_plugin_button_clicked(NULL
, GINT_TO_POINTER(response
));
1919 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1921 GtkWidget
*vbox
, *vbox2
, *swin
, *label
, *menu_item
, *filter_entry
;
1923 if (pm_widgets
.dialog
!= NULL
)
1925 gtk_window_present(GTK_WINDOW(pm_widgets
.dialog
));
1929 /* before showing the dialog, we need to create the list of available plugins */
1932 pm_widgets
.dialog
= gtk_dialog_new();
1933 gtk_window_set_title(GTK_WINDOW(pm_widgets
.dialog
), _("Plugins"));
1934 gtk_window_set_transient_for(GTK_WINDOW(pm_widgets
.dialog
), GTK_WINDOW(main_widgets
.window
));
1935 gtk_window_set_destroy_with_parent(GTK_WINDOW(pm_widgets
.dialog
), TRUE
);
1937 vbox
= ui_dialog_vbox_new(GTK_DIALOG(pm_widgets
.dialog
));
1938 gtk_widget_set_name(pm_widgets
.dialog
, "GeanyDialog");
1939 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
1941 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
1943 pm_widgets
.help_button
= gtk_dialog_add_button(
1944 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_HELP
, PM_BUTTON_HELP
);
1945 pm_widgets
.configure_button
= gtk_dialog_add_button(
1946 GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_PREFERENCES
, PM_BUTTON_CONFIGURE
);
1947 pm_widgets
.keybindings_button
= gtk_dialog_add_button(
1948 GTK_DIALOG(pm_widgets
.dialog
), _("Keybindings"), PM_BUTTON_KEYBINDINGS
);
1949 gtk_dialog_add_button(GTK_DIALOG(pm_widgets
.dialog
), GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
);
1950 gtk_dialog_set_default_response(GTK_DIALOG(pm_widgets
.dialog
), GTK_RESPONSE_CLOSE
);
1953 pm_widgets
.filter_entry
= filter_entry
= gtk_entry_new();
1954 gtk_entry_set_icon_from_stock(GTK_ENTRY(filter_entry
), GTK_ENTRY_ICON_PRIMARY
, GTK_STOCK_FIND
);
1955 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry
));
1956 g_signal_connect(filter_entry
, "changed", G_CALLBACK(on_pm_tree_filter_entry_changed_cb
), NULL
);
1957 g_signal_connect(filter_entry
, "icon-release",
1958 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb
), NULL
);
1960 /* prepare treeview */
1961 pm_widgets
.tree
= gtk_tree_view_new();
1962 pm_widgets
.store
= gtk_tree_store_new(
1963 PLUGIN_N_COLUMNS
, G_TYPE_BOOLEAN
, G_TYPE_BOOLEAN
, G_TYPE_POINTER
);
1964 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1965 gtk_tree_view_expand_all(GTK_TREE_VIEW(pm_widgets
.tree
));
1966 g_object_unref(pm_widgets
.store
);
1968 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1969 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
1970 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1971 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1972 gtk_container_add(GTK_CONTAINER(swin
), pm_widgets
.tree
);
1974 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1976 /* plugin popup menu */
1977 pm_widgets
.popup_menu
= gtk_menu_new();
1979 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, NULL
);
1980 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1981 g_signal_connect(menu_item
, "activate",
1982 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_CONFIGURE
));
1983 pm_widgets
.popup_configure_menu_item
= menu_item
;
1985 menu_item
= gtk_image_menu_item_new_with_mnemonic(_("Keybindings"));
1986 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1987 g_signal_connect(menu_item
, "activate",
1988 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS
));
1989 pm_widgets
.popup_keybindings_menu_item
= menu_item
;
1991 menu_item
= gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP
, NULL
);
1992 gtk_container_add(GTK_CONTAINER(pm_widgets
.popup_menu
), menu_item
);
1993 g_signal_connect(menu_item
, "activate",
1994 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_HELP
));
1995 pm_widgets
.popup_help_menu_item
= menu_item
;
1997 /* put it together */
1998 vbox2
= gtk_vbox_new(FALSE
, 6);
1999 gtk_box_pack_start(GTK_BOX(vbox2
), label
, FALSE
, FALSE
, 0);
2000 gtk_box_pack_start(GTK_BOX(vbox2
), filter_entry
, FALSE
, FALSE
, 0);
2001 gtk_box_pack_start(GTK_BOX(vbox2
), swin
, TRUE
, TRUE
, 0);
2003 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
2005 gtk_box_pack_start(GTK_BOX(vbox
), vbox2
, TRUE
, TRUE
, 0);
2006 gtk_widget_show_all(pm_widgets
.dialog
);
2007 gtk_widget_show_all(pm_widgets
.popup_menu
);
2009 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
2010 pm_update_buttons(NULL
);
2011 gtk_widget_grab_focus(pm_widgets
.filter_entry
);
2015 /** Register the plugin as a proxy for other plugins
2017 * Proxy plugins register a list of file extensions and a set of callbacks that are called
2018 * appropriately. A plugin can be a proxy for multiple types of sub-plugins by handling
2019 * separate file extensions, however they must share the same set of hooks, because this
2020 * function can only be called at most once per plugin.
2022 * Each callback receives the plugin-defined data as parameter (see geany_plugin_register()). The
2023 * callbacks must be set prior to calling this, by assigning to @a plugin->proxy_funcs.
2024 * GeanyProxyFuncs::load and GeanyProxyFuncs::unload must be implemented.
2026 * Nested proxies are unsupported at this point (TODO).
2028 * @note It is entirely up to the proxy to provide access to Geany's plugin API. Native code
2029 * can naturally call Geany's API directly, for interpreted languages the proxy has to
2030 * implement some kind of bindings that the plugin can use.
2032 * @see proxy for detailed documentation and an example.
2034 * @param plugin The pointer to the plugin's GeanyPlugin instance
2035 * @param extensions A @c NULL-terminated string array of file extensions, excluding the dot.
2036 * @return @c TRUE if the proxy was successfully registered, otherwise @c FALSE
2038 * @since 1.26 (API 226)
2041 gboolean
geany_plugin_register_proxy(GeanyPlugin
*plugin
, const gchar
**extensions
)
2048 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
2049 g_return_val_if_fail(extensions
!= NULL
, FALSE
);
2050 g_return_val_if_fail(*extensions
!= NULL
, FALSE
);
2051 g_return_val_if_fail(plugin
->proxy_funcs
->load
!= NULL
, FALSE
);
2052 g_return_val_if_fail(plugin
->proxy_funcs
->unload
!= NULL
, FALSE
);
2055 /* Check if this was called already. We want to reserve for the use case of calling
2056 * this again to set new supported extensions (for example, based on proxy configuration). */
2057 foreach_list(node
, active_proxies
.head
)
2060 g_return_val_if_fail(p
!= proxy
->plugin
, FALSE
);
2063 foreach_strv(ext
, extensions
)
2067 g_warning(_("Proxy plugin '%s' extension '%s' starts with a dot. "
2068 "Please fix your proxy plugin."), p
->info
.name
, *ext
);
2071 proxy
= g_new(PluginProxy
, 1);
2072 g_strlcpy(proxy
->extension
, *ext
, sizeof(proxy
->extension
));
2074 /* prepend, so that plugins automatically override core providers for a given extension */
2075 g_queue_push_head(&active_proxies
, proxy
);