2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
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.
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.
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.
21 /* Code to manage, load and unload plugins. */
33 #include "documentprivate.h"
34 #include "encodings.h"
35 #include "geanyobject.h"
36 #include "geanywraplabel.h"
37 #include "highlighting.h"
38 #include "keybindingsprivate.h"
41 #include "msgwindow.h"
43 #include "plugindata.h"
44 #include "pluginprivate.h"
45 #include "pluginutils.h"
47 #include "sciwrappers.h"
51 #include "templates.h"
64 GeanyDocument
*document
;
69 GList
*active_plugin_list
= NULL
; /* list of only actually loaded plugins, always valid */
72 static gboolean want_plugins
= FALSE
;
74 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
75 * opened, afterwards it will be destroyed */
76 static GList
*plugin_list
= NULL
;
77 static gchar
**active_plugins_pref
= NULL
; /* list of plugin filenames to load at startup */
78 static GList
*failed_plugins_list
= NULL
; /* plugins the user wants active but can't be used */
80 static GtkWidget
*menu_separator
= NULL
;
82 static gchar
*get_plugin_path(void);
83 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
);
87 Plugin
*plugin
; /* &builtin_so_proxy_plugin for native plugins */
91 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, const gchar
*filename
, gpointer pdata
);
92 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*plugin
, gpointer load_data
, gpointer pdata
);
94 static Plugin builtin_so_proxy_plugin
= {
96 .load
= plugin_load_gmodule
,
97 .unload
= plugin_unload_gmodule
,
99 /* rest of Plugin can be NULL/0 */
102 static PluginProxy builtin_so_proxy
= {
103 .extension
= G_MODULE_SUFFIX
,
104 .plugin
= &builtin_so_proxy_plugin
,
107 static GQueue active_proxies
= G_QUEUE_INIT
;
109 static void plugin_free(Plugin
*plugin
);
111 static GeanyData geany_data
;
114 geany_data_init(void)
129 NULL
, /* Remove field on next ABI break (abi-todo) */
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. */
140 static void proxied_count_inc(Plugin
*proxy
)
144 proxy
->proxied_count
+= 1;
145 proxy
= proxy
->proxy
;
146 } while (proxy
!= NULL
);
150 static void proxied_count_dec(Plugin
*proxy
)
152 g_warn_if_fail(proxy
->proxied_count
> 0);
156 proxy
->proxied_count
-= 1;
157 proxy
= proxy
->proxy
;
158 } while (proxy
!= NULL
);
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. */
165 plugin_loaded(Plugin
*plugin
)
167 gchar
*basename_module
, *basename_loaded
;
170 basename_module
= g_path_get_basename(plugin
->filename
);
171 for (item
= plugin_list
; item
!= NULL
; item
= g_list_next(item
))
173 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
175 if (utils_str_equal(basename_module
, basename_loaded
))
177 g_free(basename_loaded
);
178 g_free(basename_module
);
181 g_free(basename_loaded
);
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
))
190 basename_loaded
= g_path_get_basename(((Plugin
*)item
->data
)->filename
);
192 if (utils_str_equal(basename_module
, basename_loaded
))
194 g_free(basename_loaded
);
195 g_free(basename_module
);
198 g_free(basename_loaded
);
200 g_free(basename_module
);
205 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
209 g_return_val_if_fail(filename
, FALSE
);
211 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
213 if (utils_str_equal(filename
, ((Plugin
*)item
->data
)->filename
))
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))
225 plugin_check_version(Plugin
*plugin
, int plugin_version_code
)
228 if (plugin_version_code
< 0)
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
);
238 else if (plugin_version_code
> GEANY_API_VERSION
)
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
);
251 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
258 cb
= &callbacks
[len
];
259 if (!cb
->signal_name
|| !cb
->callback
)
266 for (i
= 0; i
< len
; i
++)
270 /* Pass the callback data as default user_data if none was set by the plugin itself */
271 plugin_signal_connect(&plugin
->public, NULL
, cb
->signal_name
, cb
->after
,
272 cb
->callback
, cb
->user_data
? cb
->user_data
: plugin
->cb_data
);
277 static void read_key_group(Plugin
*plugin
)
279 GeanyKeyGroupInfo
*p_key_info
;
280 GeanyKeyGroup
**p_key_group
;
281 GModule
*module
= plugin
->proxy_data
;
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
)
287 GeanyKeyGroupInfo
*key_info
= p_key_info
;
290 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
291 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
297 GeanyKeyGroup
*key_group
=
298 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
300 *p_key_group
= key_group
;
303 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
304 "count field is zero. Maybe use plugin_set_key_group() instead?",
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?",
315 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
317 const Plugin
*pa
= a
;
318 const Plugin
*pb
= b
;
320 return strcmp(pa
->info
.name
, pb
->info
.name
);
324 /** Register a plugin to Geany.
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.
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.
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.
339 * Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
340 * handles @a api_version and @a abi_version.
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)
347 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
349 * @since 1.26 (API 225)
350 * @see GEANY_PLUGIN_REGISTER()
353 gboolean
geany_plugin_register(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
357 GeanyPluginFuncs
*cbs
= plugin
->funcs
;
359 g_return_val_if_fail(plugin
!= NULL
, FALSE
);
362 /* already registered successfully */
363 g_return_val_if_fail(!PLUGIN_LOADED_OK(p
), FALSE
);
365 /* Prevent registering incompatible plugins. */
366 if (! plugin_check_version(p
, PLUGIN_VERSION_CODE(api_version
, abi_version
)))
369 /* Only init and cleanup callbacks are truly mandatory. */
370 if (! cbs
->init
|| ! cbs
->cleanup
)
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");
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
;
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. */
389 return PLUGIN_LOADED_OK(p
);
393 /** Register a plugin to Geany, with plugin-defined data.
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.
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.
402 * You cannot use geany_plugin_set_data() after registering with this function. Use
403 * geany_plugin_register() if you need to.
405 * Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
406 * handles @p api_version and @p abi_version.
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.
415 * @return TRUE if the plugin was successfully registered. Otherwise FALSE.
417 * @since 1.26 (API 225)
418 * @see GEANY_PLUGIN_REGISTER_FULL()
419 * @see geany_plugin_register()
422 gboolean
geany_plugin_register_full(GeanyPlugin
*plugin
, gint api_version
, gint min_api_version
,
423 gint abi_version
, gpointer pdata
, GDestroyNotify free_func
)
425 if (geany_plugin_register(plugin
, api_version
, min_api_version
, abi_version
))
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
;
437 struct LegacyRealFuncs
439 void (*init
) (GeanyData
*data
);
440 GtkWidget
* (*configure
) (GtkDialog
*dialog
);
442 void (*cleanup
) (void);
445 /* Wrappers to support legacy plugins are below */
446 static gboolean
legacy_init(GeanyPlugin
*plugin
, gpointer pdata
)
448 struct LegacyRealFuncs
*h
= pdata
;
449 h
->init(plugin
->geany_data
);
453 static void legacy_cleanup(GeanyPlugin
*plugin
, gpointer pdata
)
455 struct LegacyRealFuncs
*h
= pdata
;
456 /* Can be NULL because it's optional for legacy plugins */
461 static void legacy_help(GeanyPlugin
*plugin
, gpointer pdata
)
463 struct LegacyRealFuncs
*h
= pdata
;
467 static GtkWidget
*legacy_configure(GeanyPlugin
*plugin
, GtkDialog
*parent
, gpointer pdata
)
469 struct LegacyRealFuncs
*h
= pdata
;
470 return h
->configure(parent
);
473 static void free_legacy_cbs(gpointer data
)
475 g_slice_free(struct LegacyRealFuncs
, data
);
478 /* This function is the equivalent of geany_plugin_register() for legacy-style
479 * plugins which we continue to load for the time being. */
480 static void register_legacy_plugin(Plugin
*plugin
, GModule
*module
)
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
;
488 #define CHECK_FUNC(__x) \
489 if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
491 geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
492 g_module_name(module)); \
495 CHECK_FUNC(version_check
);
496 CHECK_FUNC(set_info
);
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
)))
505 h
= g_slice_new(struct LegacyRealFuncs
);
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
);
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
);
515 /* If all went well we can set the remaining callbacks and let it go for good. */
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
);
525 if (h
->configure
&& plugin
->configure_single
)
526 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
527 "only plugin_configure() will be used!",
529 if (h
->cleanup
== NULL
)
530 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
534 plugin
->cbs
.init
= legacy_init
;
535 plugin
->cbs
.cleanup
= legacy_cleanup
;
536 plugin
->cbs
.configure
= h
->configure
? legacy_configure
: NULL
;
537 plugin
->cbs
.help
= h
->help
? legacy_help
: NULL
;
539 plugin
->flags
= LOADED_OK
| IS_LEGACY
;
540 geany_plugin_set_data(&plugin
->public, h
, free_legacy_cbs
);
545 plugin_load(Plugin
*plugin
)
547 gboolean init_ok
= TRUE
;
549 /* Start the plugin. Legacy plugins require additional cruft. */
550 if (PLUGIN_IS_LEGACY(plugin
) && plugin
->proxy
== &builtin_so_proxy_plugin
)
552 GeanyPlugin
**p_geany_plugin
;
554 PluginFields
**plugin_fields
;
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
);
560 *p_geany_plugin
= &plugin
->public;
561 g_module_symbol(module
, "plugin_info", (void *) &p_info
);
563 *p_info
= &plugin
->info
;
564 g_module_symbol(module
, "plugin_fields", (void *) &plugin_fields
);
566 *plugin_fields
= &plugin
->fields
;
567 read_key_group(plugin
);
569 /* Legacy plugin_init() cannot fail. */
570 plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
572 /* now read any plugin-owned data that might have been set in plugin_init() */
573 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
575 ui_add_document_sensitive(plugin
->fields
.menu_item
);
580 init_ok
= plugin
->cbs
.init(&plugin
->public, plugin
->cb_data
);
586 /* new-style plugins set their callbacks in geany_load_module() */
587 if (plugin
->cbs
.callbacks
)
588 add_callbacks(plugin
, plugin
->cbs
.callbacks
);
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
);
596 geany_debug("Loaded: %s (%s)", plugin
->filename
, plugin
->info
.name
);
601 static gpointer
plugin_load_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, const gchar
*fname
, gpointer pdata
)
604 void (*p_geany_load_module
)(GeanyPlugin
*);
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
);
616 geany_debug("Can't load plugin: %s", g_module_error());
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
)
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
;
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
);
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
);
641 /* We actually check the LOADED_OK flag later */
646 static void plugin_unload_gmodule(GeanyPlugin
*proxy
, GeanyPlugin
*subplugin
, gpointer load_data
, gpointer pdata
)
648 GModule
*module
= (GModule
*) load_data
;
650 g_return_if_fail(module
!= NULL
);
652 if (! g_module_close(module
))
653 g_warning("%s: %s", subplugin
->priv
->filename
, g_module_error());
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. */
663 plugin_new(Plugin
*proxy
, const gchar
*fname
, gboolean load_plugin
, gboolean add_to_list
)
667 g_return_val_if_fail(fname
, NULL
);
668 g_return_val_if_fail(proxy
, NULL
);
670 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
672 plugin
= find_active_plugin_by_name(fname
);
675 geany_debug("Plugin \"%s\" already loaded.", fname
);
678 /* do not add to the list twice */
679 if (g_list_find(plugin_list
, plugin
) != NULL
)
682 plugin_list
= g_list_prepend(plugin_list
, plugin
);
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
;
697 if (plugin_loaded(plugin
))
699 geany_debug("Plugin \"%s\" already loaded.", fname
);
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
);
707 if (! PLUGIN_LOADED_OK(plugin
))
709 geany_debug("Failed to load \"%s\" - ignoring plugin!", fname
);
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
))
717 geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname
);
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
)
726 geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname
);
727 plugin
->proxy_data
= NULL
;
731 if (load_plugin
&& !plugin_load(plugin
))
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
);
740 plugin_list
= g_list_prepend(plugin_list
, plugin
);
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
);
749 g_free(plugin
->filename
);
755 static void on_object_weak_notify(gpointer data
, GObject
*old_ptr
)
757 Plugin
*plugin
= data
;
760 g_return_if_fail(plugin
&& plugin
->signal_ids
);
762 for (i
= 0; i
< plugin
->signal_ids
->len
; i
++)
764 SignalConnection
*sc
= &g_array_index(plugin
->signal_ids
, SignalConnection
, i
);
766 if (sc
->object
== old_ptr
)
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 */
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 */
780 void plugin_watch_object(Plugin
*plugin
, gpointer object
)
782 g_object_weak_ref(object
, on_object_weak_notify
, plugin
);
786 static void remove_callbacks(Plugin
*plugin
)
788 GArray
*signal_ids
= plugin
->signal_ids
;
789 SignalConnection
*sc
;
791 if (signal_ids
== NULL
)
794 foreach_array(SignalConnection
, sc
, signal_ids
)
796 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
797 g_object_weak_unref(sc
->object
, on_object_weak_notify
, plugin
);
800 g_array_free(signal_ids
, TRUE
);
804 static void remove_sources(Plugin
*plugin
)
808 item
= plugin
->sources
;
811 GList
*next
= item
->next
; /* cache the next pointer because current item will be freed */
813 g_source_destroy(item
->data
);
816 /* don't free the list here, it is allocated inside each source's data */
820 /* Make the GModule backing plugin resident (if it's GModule-backed at all) */
821 void plugin_make_resident(Plugin
*plugin
)
823 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
825 g_return_if_fail(plugin
->proxy_data
!= NULL
);
826 g_module_make_resident(plugin
->proxy_data
);
829 g_warning("Skipping g_module_make_resident() for non-native plugin");
833 /* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
834 gpointer
plugin_get_module_symbol(Plugin
*plugin
, const gchar
*sym
)
838 if (plugin
->proxy
== &builtin_so_proxy_plugin
)
840 g_return_val_if_fail(plugin
->proxy_data
!= NULL
, NULL
);
841 if (g_module_symbol(plugin
->proxy_data
, sym
, &symbol
))
844 g_warning("Failed to locate signal handler for '%s': %s",
845 sym
, g_module_error());
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",
854 static gboolean
is_active_plugin(Plugin
*plugin
)
856 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
860 static void remove_each_doc_data(GQuark key_id
, gpointer data
, gpointer user_data
)
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
);
869 static void remove_doc_data(Plugin
*plugin
)
873 data
.prefix
= g_strdup_printf("geany/plugins/%s/", plugin
->public.info
->name
);
875 for (guint i
= 0; i
< documents_array
->len
; i
++)
877 GeanyDocument
*doc
= documents_array
->pdata
[i
];
881 g_datalist_foreach(&doc
->priv
->data
, remove_each_doc_data
, &data
);
889 /* Clean up anything used by an active plugin */
891 plugin_cleanup(Plugin
*plugin
)
895 /* With geany_register_plugin cleanup is mandatory */
896 plugin
->cbs
.cleanup(&plugin
->public, plugin
->cb_data
);
898 remove_doc_data(plugin
);
899 remove_callbacks(plugin
);
900 remove_sources(plugin
);
902 if (plugin
->key_group
)
903 keybindings_free_group(plugin
->key_group
);
905 widget
= plugin
->toolbar_separator
.widget
;
907 gtk_widget_destroy(widget
);
909 if (!PLUGIN_HAS_LOAD_DATA(plugin
) && plugin
->cb_data_destroy
)
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
;
919 proxied_count_dec(plugin
->proxy
);
920 geany_debug("Unloaded: %s", plugin
->filename
);
924 /* Remove all plugins that proxy is a proxy for from plugin_list (and free) */
925 static void free_subplugins(Plugin
*proxy
)
932 GList
*next
= g_list_next(item
);
933 if (proxy
== ((Plugin
*) item
->data
)->proxy
)
935 /* plugin_free modifies plugin_list */
936 plugin_free((Plugin
*) item
->data
);
943 /* Returns true if the removal was successful (=> never for non-proxies) */
944 static gboolean
unregister_proxy(Plugin
*proxy
)
946 gboolean is_proxy
= FALSE
;
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 */
951 foreach_list_safe(node
, active_proxies
.head
)
953 PluginProxy
*p
= node
->data
;
954 if (p
->plugin
== proxy
)
957 g_queue_delete_link(&active_proxies
, node
);
964 /* Cleanup a plugin and free all resources allocated on behalf of it.
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!) */
969 plugin_free(Plugin
*plugin
)
973 g_return_if_fail(plugin
);
974 g_return_if_fail(plugin
->proxy
);
975 g_return_if_fail(plugin
->proxied_count
== 0);
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
))
982 if (unregister_proxy(plugin
))
983 free_subplugins(plugin
);
984 plugin_cleanup(plugin
);
987 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
988 plugin_list
= g_list_remove(plugin_list
, plugin
);
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
);
995 g_free(plugin
->filename
);
1000 static gchar
*get_custom_plugin_path(const gchar
*plugin_path_config
,
1001 const gchar
*plugin_path_system
)
1003 gchar
*plugin_path_custom
;
1005 if (EMPTY(prefs
.custom_plugin_path
))
1008 plugin_path_custom
= utils_get_locale_from_utf8(prefs
.custom_plugin_path
);
1009 utils_tidy_path(plugin_path_custom
);
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
))
1016 g_free(plugin_path_custom
);
1019 return plugin_path_custom
;
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 */
1025 static gboolean
check_plugin_path(const gchar
*fname
)
1027 gchar
*plugin_path_config
;
1028 gchar
*plugin_path_system
;
1029 gchar
*plugin_path_custom
;
1030 gboolean ret
= FALSE
;
1032 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1033 if (g_str_has_prefix(fname
, plugin_path_config
))
1036 plugin_path_system
= get_plugin_path();
1037 if (g_str_has_prefix(fname
, plugin_path_system
))
1040 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1041 if (plugin_path_custom
)
1043 if (g_str_has_prefix(fname
, plugin_path_custom
))
1046 g_free(plugin_path_custom
);
1048 g_free(plugin_path_config
);
1049 g_free(plugin_path_system
);
1054 /* Returns NULL if this ain't a plugin,
1055 * otherwise it returns the appropriate PluginProxy instance to load it */
1056 static PluginProxy
* is_plugin(const gchar
*file
)
1061 /* extract file extension to avoid g_str_has_suffix() in the loop */
1062 ext
= (const gchar
*)strrchr(file
, '.');
1065 /* ensure the dot is really part of the filename */
1066 else if (strchr(ext
, G_DIR_SEPARATOR
) != NULL
)
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
)
1074 PluginProxy
*proxy
= node
->data
;
1075 if (utils_str_casecmp(ext
, proxy
->extension
) == 0)
1077 Plugin
*p
= proxy
->plugin
;
1078 gint ret
= GEANY_PROXY_MATCH
;
1080 if (p
->proxy_cbs
.probe
)
1081 ret
= p
->proxy_cbs
.probe(&p
->public, file
, p
->cb_data
);
1084 case GEANY_PROXY_MATCH
:
1086 case GEANY_PROXY_RELATED
:
1088 case GEANY_PROXY_IGNORE
:
1091 g_warning("Ignoring bogus return value '%d' from "
1092 "proxy plugin '%s' probe() function!", ret
,
1093 proxy
->plugin
->info
.name
);
1102 /* load active plugins at startup */
1104 load_active_plugins(void)
1106 guint i
, len
, proxies
;
1108 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
1111 /* If proxys are loaded we have to restart to load plugins that sort before their proxy */
1114 proxies
= active_proxies
.length
;
1115 g_list_free_full(failed_plugins_list
, (GDestroyNotify
) g_free
);
1116 failed_plugins_list
= NULL
;
1117 for (i
= 0; i
< len
; i
++)
1119 gchar
*fname
= active_plugins_pref
[i
];
1122 /* ensure we have canonical paths */
1124 while ((p
= strchr(p
, '/')) != NULL
)
1125 *p
= G_DIR_SEPARATOR
;
1128 if (!EMPTY(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
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
));
1137 } while (proxies
!= active_proxies
.length
);
1142 load_plugins_from_path(const gchar
*path
)
1144 GSList
*list
, *item
;
1147 list
= utils_get_file_list(path
, NULL
, NULL
);
1149 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
1151 gchar
*fname
= g_build_filename(path
, item
->data
, NULL
);
1152 PluginProxy
*proxy
= is_plugin(fname
);
1154 if (proxy
!= NULL
&& plugin_new(proxy
->plugin
, fname
, FALSE
, TRUE
))
1160 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
1164 geany_debug("Added %d plugin(s) in '%s'.", count
, path
);
1168 static gchar
*get_plugin_path(void)
1170 return g_strdup(utils_resource_dir(RESOURCE_DIR_PLUGIN
));
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) */
1176 static gint
cmp_plugin_by_proxy(gconstpointer a
, gconstpointer b
)
1178 const Plugin
*pa
= a
;
1179 const Plugin
*pb
= b
;
1183 if (pa
->proxy
== pb
->proxy
)
1185 else if (pa
->proxy
== &builtin_so_proxy_plugin
)
1187 else if (pb
->proxy
== &builtin_so_proxy_plugin
)
1196 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1197 static void load_all_plugins(void)
1199 gchar
*plugin_path_config
;
1200 gchar
*plugin_path_system
;
1201 gchar
*plugin_path_custom
;
1203 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1204 plugin_path_system
= get_plugin_path();
1206 /* first load plugins in ~/.config/geany/plugins/ */
1207 load_plugins_from_path(plugin_path_config
);
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
)
1213 load_plugins_from_path(plugin_path_custom
);
1214 g_free(plugin_path_custom
);
1217 /* finally load plugins from $prefix/lib/geany */
1218 load_plugins_from_path(plugin_path_system
);
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 */
1225 plugin_list
= g_list_sort(plugin_list
, cmp_plugin_by_proxy
);
1227 g_free(plugin_path_config
);
1228 g_free(plugin_path_system
);
1232 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
1234 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
1236 gboolean have_plugin_menu_items
= FALSE
;
1238 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
1240 if (item
->data
== menu_separator
)
1242 if (i
< g_list_length(list
) - 1)
1244 have_plugin_menu_items
= TRUE
;
1252 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
1256 /* Calling this starts up plugin support */
1257 void plugins_load_active(void)
1261 want_plugins
= TRUE
;
1265 widget
= gtk_separator_menu_item_new();
1266 gtk_widget_show(widget
);
1267 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
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
);
1274 menu_separator
= gtk_separator_menu_item_new();
1275 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
1276 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
1278 load_active_plugins();
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. */
1284 static void update_active_plugins_pref(void)
1290 /* if plugins are disabled, don't clear list of active plugins */
1294 count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
1296 g_strfreev(active_plugins_pref
);
1300 active_plugins_pref
= NULL
;
1304 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
1306 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
1308 Plugin
*plugin
= list
->data
;
1310 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
1313 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
1315 const gchar
*fname
= list
->data
;
1317 active_plugins_pref
[i
] = g_strdup(fname
);
1320 active_plugins_pref
[i
] = NULL
;
1324 /* called even if plugin support is disabled */
1325 void plugins_init(void)
1330 path
= get_plugin_path();
1331 geany_debug("System plugin path: %s", path
);
1334 group
= stash_group_new("plugins");
1335 configuration_add_pref_group(group
, TRUE
);
1337 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
1338 "load_plugins", TRUE
, "check_plugins");
1339 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
1340 "custom_plugin_path", "", "extra_plugin_path_entry");
1342 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(update_active_plugins_pref
), NULL
);
1343 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
1345 g_queue_push_head(&active_proxies
, &builtin_so_proxy
);
1349 /* Same as plugin_free(), except it does nothing for proxies-in-use, to be called on
1350 * finalize in a loop */
1351 static void plugin_free_leaf(Plugin
*p
)
1353 if (p
->proxied_count
== 0)
1358 /* called even if plugin support is disabled */
1359 void plugins_finalize(void)
1361 if (failed_plugins_list
!= NULL
)
1363 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
1364 g_list_free(failed_plugins_list
);
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
);
1371 g_strfreev(active_plugins_pref
);
1375 /* Check whether there are any plugins loaded which provide a configure symbol */
1376 gboolean
plugins_have_preferences(void)
1380 if (active_plugin_list
== NULL
)
1383 foreach_list(item
, active_plugin_list
)
1385 Plugin
*plugin
= item
->data
;
1386 if (plugin
->configure_single
!= NULL
|| plugin
->cbs
.configure
!= NULL
)
1394 /* Plugin Manager */
1398 PLUGIN_COLUMN_CHECK
= 0,
1399 PLUGIN_COLUMN_CAN_UNCHECK
,
1400 PLUGIN_COLUMN_PLUGIN
,
1402 PM_BUTTON_KEYBINDINGS
,
1403 PM_BUTTON_CONFIGURE
,
1411 GtkTreeStore
*store
;
1412 GtkWidget
*filter_entry
;
1413 GtkWidget
*configure_button
;
1414 GtkWidget
*keybindings_button
;
1415 GtkWidget
*help_button
;
1416 GtkWidget
*popup_menu
;
1417 GtkWidget
*popup_configure_menu_item
;
1418 GtkWidget
*popup_keybindings_menu_item
;
1419 GtkWidget
*popup_help_menu_item
;
1421 PluginManagerWidgets
;
1423 static PluginManagerWidgets pm_widgets
;
1426 static void pm_update_buttons(Plugin
*p
)
1428 gboolean has_configure
= FALSE
;
1429 gboolean has_help
= FALSE
;
1430 gboolean has_keybindings
= FALSE
;
1432 if (p
!= NULL
&& is_active_plugin(p
))
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
;
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
);
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
);
1449 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
1452 GtkTreeModel
*model
;
1455 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1457 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1460 pm_update_buttons(p
);
1465 static gboolean
find_iter_for_plugin(Plugin
*p
, GtkTreeModel
*model
, GtkTreeIter
*iter
)
1470 for (valid
= gtk_tree_model_get_iter_first(model
, iter
);
1472 valid
= gtk_tree_model_iter_next(model
, iter
))
1474 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &pp
, -1);
1483 static void pm_populate(GtkTreeStore
*store
);
1486 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
1488 gboolean old_state
, state
;
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
));
1496 guint prev_num_proxies
;
1498 gtk_tree_model_get_iter(model
, &iter
, path
);
1500 gtk_tree_model_get(model
, &iter
,
1501 PLUGIN_COLUMN_CHECK
, &old_state
,
1502 PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1504 /* no plugins item */
1507 gtk_tree_path_free(path
);
1511 gtk_tree_model_filter_convert_iter_to_child_iter(
1512 GTK_TREE_MODEL_FILTER(model
), &store_iter
, &iter
);
1514 state
= ! old_state
; /* toggle the state */
1516 /* save the filename and proxy of the plugin */
1517 file_name
= g_strdup(p
->filename
);
1519 prev_num_proxies
= active_proxies
.length
;
1521 /* unload plugin module */
1523 /* save shortcuts (only need this group, but it doesn't take long) */
1524 keybindings_write_to_file();
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
,
1528 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1531 /* reload plugin module and initialize it if item is checked */
1532 p
= plugin_new(proxy
, file_name
, state
, TRUE
);
1535 /* plugin file may no longer be on disk, or is now incompatible */
1536 gtk_tree_store_remove(pm_widgets
.store
, &store_iter
);
1541 keybindings_load_keyfile(); /* load shortcuts */
1544 gtk_tree_store_set(pm_widgets
.store
, &store_iter
,
1545 PLUGIN_COLUMN_CHECK
, state
,
1546 PLUGIN_COLUMN_PLUGIN
, p
, -1);
1548 /* set again the sensitiveness of the configure and help buttons */
1549 pm_update_buttons(p
);
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
)
1556 gboolean can_uncheck
;
1557 GtkTreePath
*store_path
= gtk_tree_model_filter_convert_path_to_child_path(
1558 GTK_TREE_MODEL_FILTER(model
), path
);
1560 g_warn_if_fail(store_path
!= NULL
);
1561 if (gtk_tree_path_up(store_path
))
1563 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets
.store
), &parent
, store_path
);
1566 can_uncheck
= FALSE
;
1568 can_uncheck
= p
->proxy
->proxied_count
== 0;
1570 gtk_tree_store_set(pm_widgets
.store
, &parent
,
1571 PLUGIN_COLUMN_CAN_UNCHECK
, can_uncheck
, -1);
1573 gtk_tree_path_free(store_path
);
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
)
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
)
1585 pm_populate(pm_widgets
.store
);
1586 gtk_tree_view_expand_row(GTK_TREE_VIEW(pm_widgets
.tree
), path
, FALSE
);
1589 gtk_tree_path_free(path
);
1593 static void pm_populate(GtkTreeStore
*store
)
1598 gtk_tree_store_clear(store
);
1599 list
= g_list_first(plugin_list
);
1602 gtk_tree_store_append(store
, &iter
, NULL
);
1603 gtk_tree_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1604 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1608 for (; list
!= NULL
; list
= list
->next
)
1610 Plugin
*p
= list
->data
;
1613 if (p
->proxy
!= &builtin_so_proxy_plugin
1614 && find_iter_for_plugin(p
->proxy
, GTK_TREE_MODEL(pm_widgets
.store
), &parent
))
1615 gtk_tree_store_append(store
, &iter
, &parent
);
1617 gtk_tree_store_append(store
, &iter
, NULL
);
1619 gtk_tree_store_set(store
, &iter
,
1620 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1621 PLUGIN_COLUMN_PLUGIN
, p
,
1622 PLUGIN_COLUMN_CAN_UNCHECK
, (p
->proxied_count
== 0),
1628 static gboolean
pm_treeview_query_tooltip(GtkWidget
*widget
, gint x
, gint y
,
1629 gboolean keyboard_mode
, GtkTooltip
*tooltip
, gpointer user_data
)
1631 GtkTreeModel
*model
;
1635 gboolean can_uncheck
= TRUE
;
1637 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget
), &x
, &y
, keyboard_mode
,
1638 &model
, &path
, &iter
))
1641 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, PLUGIN_COLUMN_CAN_UNCHECK
, &can_uncheck
, -1);
1644 gchar
*prefix
, *suffix
, *details
, *markup
;
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
);
1656 gtk_tooltip_set_markup(tooltip
, markup
);
1657 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget
), tooltip
, path
);
1664 gtk_tree_path_free(path
);
1670 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn
*column
, GtkCellRenderer
*cell
,
1671 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer data
)
1675 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1678 g_object_set(cell
, "text", _("No plugins available."), NULL
);
1681 gchar
*markup
= g_markup_printf_escaped("<b>%s</b>\n%s", p
->info
.name
, p
->info
.description
);
1683 g_object_set(cell
, "markup", markup
, NULL
);
1689 static gboolean
pm_treeview_button_press_cb(GtkWidget
*widget
, GdkEventButton
*event
,
1690 G_GNUC_UNUSED gpointer user_data
)
1692 if (event
->button
== 3)
1694 gtk_menu_popup(GTK_MENU(pm_widgets
.popup_menu
), NULL
, NULL
, NULL
, NULL
,
1695 event
->button
, event
->time
);
1701 static gint
pm_tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1706 gtk_tree_model_get(model
, a
, PLUGIN_COLUMN_PLUGIN
, &pa
, -1);
1707 gtk_tree_model_get(model
, b
, PLUGIN_COLUMN_PLUGIN
, &pb
, -1);
1710 return strcmp(pa
->info
.name
, pb
->info
.name
);
1716 static gboolean
pm_tree_search(const gchar
*key
, const gchar
*haystack
)
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
;
1724 normalized_string
= g_utf8_normalize(haystack
, -1, G_NORMALIZE_ALL
);
1725 normalized_key
= g_utf8_normalize(key
, -1, G_NORMALIZE_ALL
);
1727 if (normalized_string
!= NULL
&& normalized_key
!= NULL
)
1729 GString
*stripped_key
;
1730 gchar
**subkey
, **subkeys
;
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
)
1740 if (strstr(case_normalized_string
, *subkey
) == NULL
)
1746 g_strfreev(subkeys
);
1749 g_free(normalized_key
);
1750 g_free(normalized_string
);
1751 g_free(case_normalized_key
);
1752 g_free(case_normalized_string
);
1758 static gboolean
pm_tree_filter_func(GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1763 gchar
*haystack
, *filename
;
1765 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &plugin
, -1);
1769 key
= gtk_entry_get_text(GTK_ENTRY(pm_widgets
.filter_entry
));
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
);
1782 static void on_pm_tree_filter_entry_changed_cb(GtkEntry
*entry
, gpointer user_data
)
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
));
1789 static void on_pm_tree_filter_entry_icon_release_cb(GtkEntry
*entry
, GtkEntryIconPosition icon_pos
,
1790 GdkEvent
*event
, gpointer user_data
)
1792 if (event
->button
.button
== 1 && icon_pos
== GTK_ENTRY_ICON_PRIMARY
)
1793 on_pm_tree_filter_entry_changed_cb(entry
, user_data
);
1797 static void pm_prepare_treeview(GtkWidget
*tree
, GtkTreeStore
*store
)
1799 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1800 GtkTreeViewColumn
*column
;
1801 GtkTreeModel
*filter_model
;
1802 GtkTreeSelection
*sel
;
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
);
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
);
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
,
1819 pm_treeview_text_cell_data_func
, NULL
, NULL
);
1820 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
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
,
1825 pm_tree_sort_func
, NULL
, NULL
);
1826 gtk_tree_sortable_set_sort_column_id(
1827 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
, GTK_SORT_ASCENDING
);
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
);
1834 g_signal_connect(tree
, "button-press-event", G_CALLBACK(pm_treeview_button_press_cb
), NULL
);
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
);
1841 /* set model to tree view */
1842 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), filter_model
);
1843 g_object_unref(filter_model
);
1849 static void pm_on_plugin_button_clicked(G_GNUC_UNUSED GtkButton
*button
, gpointer user_data
)
1851 GtkTreeModel
*model
;
1852 GtkTreeSelection
*selection
;
1856 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1857 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1859 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1863 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1864 plugin_show_configure(&p
->public);
1865 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
)
1867 g_return_if_fail(p
->cbs
.help
!= NULL
);
1868 p
->cbs
.help(&p
->public, p
->cb_data
);
1870 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_KEYBINDINGS
&& p
->key_group
&& p
->key_group
->plugin_key_count
> 0)
1871 keybindings_dialog_show_prefs_scroll(p
->info
.name
);
1878 free_non_active_plugin(gpointer data
, gpointer user_data
)
1880 Plugin
*plugin
= data
;
1882 /* don't do anything when closing the plugin manager and it is an active plugin */
1883 if (is_active_plugin(plugin
))
1886 plugin_free(plugin
);
1890 /* Callback when plugin manager dialog closes, responses GTK_RESPONSE_CLOSE and
1891 * GTK_RESPONSE_DELETE_EVENT are treated the same. */
1892 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1896 case GTK_RESPONSE_CLOSE
:
1897 case GTK_RESPONSE_DELETE_EVENT
:
1898 if (plugin_list
!= NULL
)
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
);
1905 gtk_widget_destroy(GTK_WIDGET(dialog
));
1906 pm_widgets
.dialog
= NULL
;
1908 configuration_save();
1910 case PM_BUTTON_CONFIGURE
:
1911 case PM_BUTTON_HELP
:
1912 case PM_BUTTON_KEYBINDINGS
:
1913 /* forward event to the generic handler */
1914 pm_on_plugin_button_clicked(NULL
, GINT_TO_POINTER(response
));
1920 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1922 GtkWidget
*vbox
, *vbox2
, *swin
, *label
, *menu_item
, *filter_entry
;
1924 if (pm_widgets
.dialog
!= NULL
)
1926 gtk_window_present(GTK_WINDOW(pm_widgets
.dialog
));
1930 /* before showing the dialog, we need to create the list of available plugins */
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
);
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);
1942 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
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
);
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
);
1956 ui_entry_add_clear_icon(GTK_ENTRY(filter_entry
));
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",
1959 G_CALLBACK(on_pm_tree_filter_entry_icon_release_cb
), NULL
);
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
);
1965 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1966 gtk_tree_view_expand_all(GTK_TREE_VIEW(pm_widgets
.tree
));
1967 g_object_unref(pm_widgets
.store
);
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
);
1975 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1977 /* plugin popup menu */
1978 pm_widgets
.popup_menu
= gtk_menu_new();
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
));
1984 pm_widgets
.popup_configure_menu_item
= menu_item
;
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
));
1990 pm_widgets
.popup_keybindings_menu_item
= menu_item
;
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
;
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);
2004 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
2006 gtk_box_pack_start(GTK_BOX(vbox
), vbox2
, TRUE
, TRUE
, 0);
2007 gtk_widget_show_all(pm_widgets
.dialog
);
2008 gtk_widget_show_all(pm_widgets
.popup_menu
);
2010 /* set initial plugin buttons state, pass NULL as no plugin is selected by default */
2011 pm_update_buttons(NULL
);
2012 gtk_widget_grab_focus(pm_widgets
.filter_entry
);
2016 /** Register the plugin as a proxy for other plugins
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.
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.
2027 * Nested proxies are unsupported at this point (TODO).
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.
2033 * @see proxy for detailed documentation and an example.
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
2039 * @since 1.26 (API 226)
2042 gboolean
geany_plugin_register_proxy(GeanyPlugin
*plugin
, const gchar
**extensions
)
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
);
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
)
2061 g_return_val_if_fail(p
!= proxy
->plugin
, FALSE
);
2064 foreach_strv(ext
, extensions
)
2068 g_warning(_("Proxy plugin '%s' extension '%s' starts with a dot. "
2069 "Please fix your proxy plugin."), p
->info
.name
, *ext
);
2072 proxy
= g_new(PluginProxy
, 1);
2073 g_strlcpy(proxy
->extension
, *ext
, sizeof(proxy
->extension
));
2075 /* prepend, so that plugins automatically override core providers for a given extension */
2076 g_queue_push_head(&active_proxies
, proxy
);