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 "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"
57 #include "gtkcompat.h"
62 GList
*active_plugin_list
= NULL
; /* list of only actually loaded plugins, always valid */
65 static gboolean want_plugins
= FALSE
;
67 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
68 * opened, afterwards it will be destroyed */
69 static GList
*plugin_list
= NULL
;
70 static gchar
**active_plugins_pref
= NULL
; /* list of plugin filenames to load at startup */
71 static GList
*failed_plugins_list
= NULL
; /* plugins the user wants active but can't be used */
73 static GtkWidget
*menu_separator
= NULL
;
75 static gchar
*get_plugin_path(void);
76 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
);
79 static PluginFuncs plugin_funcs
= {
80 &plugin_add_toolbar_item
,
81 &plugin_module_make_resident
,
82 &plugin_signal_connect
,
83 &plugin_set_key_group
,
84 &plugin_show_configure
,
86 &plugin_timeout_add_seconds
,
88 &plugin_builder_connect_signals
91 static DocumentFuncs doc_funcs
= {
93 &document_get_current
,
94 &document_get_from_page
,
95 &document_find_by_filename
,
96 &document_find_by_real_path
,
100 &document_remove_page
,
101 &document_reload_file
,
102 &document_set_encoding
,
103 &document_set_text_changed
,
104 &document_set_filetype
,
107 &document_save_file_as
,
108 &document_rename_file
,
109 &document_get_status_color
,
110 &document_get_basename_for_display
,
111 &document_get_notebook_page
,
112 &document_compare_by_display_name
,
113 &document_compare_by_tab_order
,
114 &document_compare_by_tab_order_reverse
117 static EditorFuncs editor_funcs
= {
118 &editor_get_indent_prefs
,
119 &editor_create_widget
,
120 &editor_indicator_set_on_range
,
121 &editor_indicator_set_on_line
,
122 &editor_indicator_clear
,
123 &editor_set_indent_type
,
124 &editor_get_word_at_pos
,
125 &editor_get_eol_char_name
,
126 &editor_get_eol_char_len
,
127 &editor_get_eol_char
,
128 &editor_insert_text_block
,
129 &editor_get_eol_char_mode
,
131 &editor_find_snippet
,
132 &editor_insert_snippet
135 static ScintillaFuncs scintilla_funcs
= {
136 &scintilla_send_message
,
140 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
141 #define _scintilla_send_message_macro scintilla_send_message
143 static SciFuncs sci_funcs
= {
144 &_scintilla_send_message_macro
,
146 &sci_start_undo_action
,
147 &sci_end_undo_action
,
152 &sci_get_current_position
,
153 &sci_set_current_position
,
154 &sci_get_col_from_position
,
155 &sci_get_line_from_position
,
156 &sci_get_position_from_line
,
158 &sci_get_selected_text
,
159 &sci_get_selected_text_length
,
160 &sci_get_selection_start
,
161 &sci_get_selection_end
,
162 &sci_get_selection_mode
,
163 &sci_set_selection_mode
,
164 &sci_set_selection_start
,
165 &sci_set_selection_end
,
168 &sci_get_line_length
,
170 &sci_get_line_is_visible
,
171 &sci_ensure_line_is_visible
,
173 &sci_find_matching_brace
,
176 &sci_get_current_line
,
179 &sci_indicator_clear
,
182 &sci_get_contents_range
,
183 &sci_get_selection_contents
,
185 &sci_get_line_end_position
,
186 &sci_set_target_start
,
189 &sci_set_marker_at_line
,
190 &sci_delete_marker_at_line
,
191 &sci_is_marker_set_at_line
,
194 &sci_set_line_indentation
,
195 &sci_get_line_indentation
,
199 static TemplateFuncs template_funcs
= {
200 &templates_get_template_fileheader
203 static UtilsFuncs utils_funcs
= {
205 &utils_string_replace_all
,
206 &utils_get_file_list
,
208 &utils_get_locale_from_utf8
,
209 &utils_get_utf8_from_locale
,
210 &utils_remove_ext_from_filename
,
212 &utils_get_setting_boolean
,
213 &utils_get_setting_integer
,
214 &utils_get_setting_string
,
218 &utils_get_date_time
,
220 &utils_string_replace_first
,
221 &utils_str_middle_truncate
,
222 &utils_str_remove_chars
,
223 &utils_get_file_list_full
,
224 &utils_copy_environment
,
225 &utils_find_open_xml_tag
,
226 &utils_find_open_xml_tag_pos
229 static UIUtilsFuncs uiutils_funcs
= {
231 &ui_frame_new_with_alignment
,
235 &ui_button_new_with_image
,
236 &ui_add_document_sensitive
,
237 &ui_widget_set_tooltip_text
,
238 &ui_image_menu_item_new
,
240 &ui_progress_bar_start
,
241 &ui_progress_bar_stop
,
242 &ui_entry_add_clear_icon
,
243 &ui_menu_add_document_items
,
244 &ui_widget_modify_font_from_string
,
245 &ui_is_keyval_enter_or_return
,
246 &ui_get_gtk_settings_integer
,
247 &ui_combo_box_add_to_history
,
248 &ui_menu_add_document_items_sorted
,
249 &ui_lookup_stock_label
252 static DialogFuncs dialog_funcs
= {
253 &dialogs_show_question
,
254 &dialogs_show_msgbox
,
255 &dialogs_show_save_as
,
256 &dialogs_show_input_numeric
,
260 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
261 #define _lookup_widget_macro ui_lookup_widget
264 static SupportFuncs support_funcs
= {
265 &_lookup_widget_macro
268 static MsgWinFuncs msgwin_funcs
= {
270 &msgwin_compiler_add
,
274 &msgwin_set_messages_dir
277 static EncodingFuncs encoding_funcs
= {
278 &encodings_convert_to_utf8
,
279 &encodings_convert_to_utf8_from_charset
,
280 &encodings_get_charset_from_index
283 static KeybindingFuncs keybindings_funcs
= {
284 &keybindings_send_command
,
285 &keybindings_set_item
,
286 &keybindings_get_item
289 static TagManagerFuncs tagmanager_funcs
= {
292 &tm_workspace_add_object
,
293 &tm_source_file_update
,
294 &tm_work_object_free
,
295 &tm_workspace_remove_object
298 static SearchFuncs search_funcs
= {
299 &search_show_find_in_files_dialog
302 static HighlightingFuncs highlighting_funcs
= {
303 &highlighting_get_style
,
304 &highlighting_set_styles
,
305 &highlighting_is_string_style
,
306 &highlighting_is_comment_style
,
307 &highlighting_is_code_style
310 static FiletypeFuncs filetype_funcs
= {
311 &filetypes_detect_from_file
,
312 &filetypes_lookup_by_name
,
314 &filetypes_get_display_name
,
315 &filetypes_get_sorted_by_name
318 static NavQueueFuncs navqueue_funcs
= {
322 static MainFuncs main_funcs
= {
323 &main_reload_configuration
,
328 static StashFuncs stash_funcs
= {
330 &stash_group_add_boolean
,
331 &stash_group_add_integer
,
332 &stash_group_add_string
,
333 &stash_group_add_string_vector
,
334 &stash_group_load_from_key_file
,
335 &stash_group_save_to_key_file
,
337 &stash_group_load_from_file
,
338 &stash_group_save_to_file
,
339 &stash_group_add_toggle_button
,
340 &stash_group_add_radio_buttons
,
341 &stash_group_add_spin_button_integer
,
342 &stash_group_add_combo_box
,
343 &stash_group_add_combo_box_entry
,
344 &stash_group_add_entry
,
345 &stash_group_add_widget_property
,
346 &stash_group_display
,
348 &stash_group_free_settings
351 static SymbolsFuncs symbols_funcs
= {
352 &symbols_get_context_separator
355 static BuildFuncs build_funcs
= {
356 &build_activate_menu_item
,
357 &build_get_current_menu_item
,
358 &build_remove_menu_item
,
359 &build_set_menu_item
,
360 &build_get_group_count
363 static GeanyFunctions geany_functions
= {
389 static GeanyData geany_data
;
393 geany_data_init(void)
416 /* Prevent the same plugin filename being loaded more than once.
417 * Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
419 plugin_loaded(GModule
*module
)
421 gchar
*basename_module
, *basename_loaded
;
424 basename_module
= g_path_get_basename(g_module_name(module
));
425 for (item
= plugin_list
; item
!= NULL
; item
= g_list_next(item
))
427 basename_loaded
= g_path_get_basename(
428 g_module_name(((Plugin
*)item
->data
)->module
));
430 if (utils_str_equal(basename_module
, basename_loaded
))
432 g_free(basename_loaded
);
433 g_free(basename_module
);
436 g_free(basename_loaded
);
438 /* Look also through the list of active plugins. This prevents problems when we have the same
439 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
440 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
441 * the plugin manager would list both plugins. Additionally, unloading the active plugin
442 * would cause a crash. */
443 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
445 basename_loaded
= g_path_get_basename(g_module_name(((Plugin
*)item
->data
)->module
));
447 if (utils_str_equal(basename_module
, basename_loaded
))
449 g_free(basename_loaded
);
450 g_free(basename_module
);
453 g_free(basename_loaded
);
455 g_free(basename_module
);
460 static Plugin
*find_active_plugin_by_name(const gchar
*filename
)
464 g_return_val_if_fail(filename
, FALSE
);
466 for (item
= active_plugin_list
; item
!= NULL
; item
= g_list_next(item
))
468 if (utils_str_equal(filename
, ((Plugin
*)item
->data
)->filename
))
477 plugin_check_version(GModule
*module
)
479 gint (*version_check
)(gint
) = NULL
;
481 g_module_symbol(module
, "plugin_version_check", (void *) &version_check
);
483 if (G_UNLIKELY(! version_check
))
485 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
486 g_module_name(module
));
491 gint result
= version_check(GEANY_ABI_VERSION
);
495 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
496 "release of Geany - please recompile it."), g_module_name(module
));
497 geany_debug("Plugin \"%s\" is not binary compatible with this "
498 "release of Geany - recompile it.", g_module_name(module
));
501 if (result
> GEANY_API_VERSION
)
503 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
504 g_module_name(module
), result
);
512 static void add_callbacks(Plugin
*plugin
, PluginCallback
*callbacks
)
519 cb
= &callbacks
[len
];
520 if (!cb
->signal_name
|| !cb
->callback
)
527 for (i
= 0; i
< len
; i
++)
531 plugin_signal_connect(&plugin
->public, NULL
, cb
->signal_name
, cb
->after
,
532 cb
->callback
, cb
->user_data
);
537 static void read_key_group(Plugin
*plugin
)
539 GeanyKeyGroupInfo
*p_key_info
;
540 GeanyKeyGroup
**p_key_group
;
542 g_module_symbol(plugin
->module
, "plugin_key_group_info", (void *) &p_key_info
);
543 g_module_symbol(plugin
->module
, "plugin_key_group", (void *) &p_key_group
);
544 if (p_key_info
&& p_key_group
)
546 GeanyKeyGroupInfo
*key_info
= p_key_info
;
549 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
550 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
556 GeanyKeyGroup
*key_group
=
557 plugin_set_key_group(&plugin
->public, key_info
->name
, key_info
->count
, NULL
);
559 *p_key_group
= key_group
;
562 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
563 "count field is zero. Maybe use plugin_set_key_group() instead?",
567 else if (p_key_info
|| p_key_group
)
568 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
569 "Maybe use plugin_set_key_group() instead?",
574 static gint
cmp_plugin_names(gconstpointer a
, gconstpointer b
)
576 const Plugin
*pa
= a
;
577 const Plugin
*pb
= b
;
579 return strcmp(pa
->info
.name
, pb
->info
.name
);
584 plugin_init(Plugin
*plugin
)
586 GeanyPlugin
**p_geany_plugin
;
587 PluginCallback
*callbacks
;
589 PluginFields
**plugin_fields
;
591 /* set these symbols before plugin_init() is called
592 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
593 g_module_symbol(plugin
->module
, "geany_plugin", (void *) &p_geany_plugin
);
595 *p_geany_plugin
= &plugin
->public;
596 g_module_symbol(plugin
->module
, "plugin_info", (void *) &p_info
);
598 *p_info
= &plugin
->info
;
599 g_module_symbol(plugin
->module
, "plugin_fields", (void *) &plugin_fields
);
601 *plugin_fields
= &plugin
->fields
;
602 read_key_group(plugin
);
604 /* start the plugin */
605 g_return_if_fail(plugin
->init
);
606 plugin
->init(&geany_data
);
608 /* store some function pointers for later use */
609 g_module_symbol(plugin
->module
, "plugin_configure", (void *) &plugin
->configure
);
610 g_module_symbol(plugin
->module
, "plugin_configure_single", (void *) &plugin
->configure_single
);
611 if (app
->debug_mode
&& plugin
->configure
&& plugin
->configure_single
)
612 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
613 "only plugin_configure() will be used!",
616 g_module_symbol(plugin
->module
, "plugin_help", (void *) &plugin
->help
);
617 g_module_symbol(plugin
->module
, "plugin_cleanup", (void *) &plugin
->cleanup
);
618 if (plugin
->cleanup
== NULL
)
621 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
625 /* now read any plugin-owned data that might have been set in plugin_init() */
627 if (plugin
->fields
.flags
& PLUGIN_IS_DOCUMENT_SENSITIVE
)
629 ui_add_document_sensitive(plugin
->fields
.menu_item
);
632 g_module_symbol(plugin
->module
, "plugin_callbacks", (void *) &callbacks
);
634 add_callbacks(plugin
, callbacks
);
636 /* remember which plugins are active.
637 * keep list sorted so tools menu items and plugin preference tabs are
638 * sorted by plugin name */
639 active_plugin_list
= g_list_insert_sorted(active_plugin_list
, plugin
, cmp_plugin_names
);
641 geany_debug("Loaded: %s (%s)", plugin
->filename
,
642 FALLBACK(plugin
->info
.name
, "<Unknown>"));
646 /* Load and optionally init a plugin.
647 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
648 * called, the plugin will be started, if not the plugin will be read only (for the list of
649 * available plugins in the plugin manager).
650 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
652 plugin_new(const gchar
*fname
, gboolean init_plugin
, gboolean add_to_list
)
656 GeanyData
**p_geany_data
;
657 GeanyFunctions
**p_geany_functions
;
658 void (*plugin_set_info
)(PluginInfo
*);
660 g_return_val_if_fail(fname
, NULL
);
661 g_return_val_if_fail(g_module_supported(), NULL
);
663 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
665 plugin
= find_active_plugin_by_name(fname
);
668 geany_debug("Plugin \"%s\" already loaded.", fname
);
671 /* do not add to the list twice */
672 if (g_list_find(plugin_list
, plugin
) != NULL
)
675 plugin_list
= g_list_prepend(plugin_list
, plugin
);
680 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
681 * causing a segfault. Without that flag the module will safely fail to load.
682 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
683 * otherwise not be detected due to the shadowing of Geany's app variable.
684 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
685 * function from a plugin will be shadowed. */
686 module
= g_module_open(fname
, G_MODULE_BIND_LOCAL
);
689 geany_debug("Can't load plugin: %s", g_module_error());
693 if (plugin_loaded(module
))
695 geany_debug("Plugin \"%s\" already loaded.", fname
);
697 if (! g_module_close(module
))
698 g_warning("%s: %s", fname
, g_module_error());
702 if (! plugin_check_version(module
))
704 if (! g_module_close(module
))
705 g_warning("%s: %s", fname
, g_module_error());
709 g_module_symbol(module
, "plugin_set_info", (void *) &plugin_set_info
);
710 if (plugin_set_info
== NULL
)
712 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname
);
714 if (! g_module_close(module
))
715 g_warning("%s: %s", fname
, g_module_error());
719 plugin
= g_new0(Plugin
, 1);
721 /* set basic fields here to allow plugins to call Geany functions in set_info() */
722 g_module_symbol(module
, "geany_data", (void *) &p_geany_data
);
724 *p_geany_data
= &geany_data
;
725 g_module_symbol(module
, "geany_functions", (void *) &p_geany_functions
);
726 if (p_geany_functions
)
727 *p_geany_functions
= &geany_functions
;
729 /* read plugin name, etc. */
730 plugin_set_info(&plugin
->info
);
731 if (G_UNLIKELY(EMPTY(plugin
->info
.name
)))
733 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
736 if (! g_module_close(module
))
737 g_warning("%s: %s", fname
, g_module_error());
742 g_module_symbol(module
, "plugin_init", (void *) &plugin
->init
);
743 if (plugin
->init
== NULL
)
745 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
748 if (! g_module_close(module
))
749 g_warning("%s: %s", fname
, g_module_error());
753 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
755 plugin
->filename
= g_strdup(fname
);
756 plugin
->module
= module
;
757 plugin
->public.info
= &plugin
->info
;
758 plugin
->public.priv
= plugin
;
764 plugin_list
= g_list_prepend(plugin_list
, plugin
);
770 static void on_object_weak_notify(gpointer data
, GObject
*old_ptr
)
772 Plugin
*plugin
= data
;
775 g_return_if_fail(plugin
&& plugin
->signal_ids
);
777 for (i
= 0; i
< plugin
->signal_ids
->len
; i
++)
779 SignalConnection
*sc
= &g_array_index(plugin
->signal_ids
, SignalConnection
, i
);
781 if (sc
->object
== old_ptr
)
783 g_array_remove_index_fast(plugin
->signal_ids
, i
);
784 /* we can break the loop right after finding the first match,
785 * because we will get one notification per connected signal */
792 /* add an object to watch for destruction, and release pointers to it when destroyed.
793 * this should only be used by plugin_signal_connect() to add a watch on
794 * the object lifetime and nuke out references to it in plugin->signal_ids */
795 void plugin_watch_object(Plugin
*plugin
, gpointer object
)
797 g_object_weak_ref(object
, on_object_weak_notify
, plugin
);
801 static void remove_callbacks(Plugin
*plugin
)
803 GArray
*signal_ids
= plugin
->signal_ids
;
804 SignalConnection
*sc
;
806 if (signal_ids
== NULL
)
809 foreach_array(SignalConnection
, sc
, signal_ids
)
811 g_signal_handler_disconnect(sc
->object
, sc
->handler_id
);
812 g_object_weak_unref(sc
->object
, on_object_weak_notify
, plugin
);
815 g_array_free(signal_ids
, TRUE
);
819 static void remove_sources(Plugin
*plugin
)
823 item
= plugin
->sources
;
826 GList
*next
= item
->next
; /* cache the next pointer because current item will be freed */
828 g_source_destroy(item
->data
);
831 /* don't free the list here, it is allocated inside each source's data */
835 static gboolean
is_active_plugin(Plugin
*plugin
)
837 return (g_list_find(active_plugin_list
, plugin
) != NULL
);
841 /* Clean up anything used by an active plugin */
843 plugin_cleanup(Plugin
*plugin
)
850 remove_callbacks(plugin
);
851 remove_sources(plugin
);
853 if (plugin
->key_group
)
854 keybindings_free_group(plugin
->key_group
);
856 widget
= plugin
->toolbar_separator
.widget
;
858 gtk_widget_destroy(widget
);
860 geany_debug("Unloaded: %s", plugin
->filename
);
865 plugin_free(Plugin
*plugin
)
867 g_return_if_fail(plugin
);
868 g_return_if_fail(plugin
->module
);
870 if (is_active_plugin(plugin
))
871 plugin_cleanup(plugin
);
873 active_plugin_list
= g_list_remove(active_plugin_list
, plugin
);
875 if (! g_module_close(plugin
->module
))
876 g_warning("%s: %s", plugin
->filename
, g_module_error());
878 plugin_list
= g_list_remove(plugin_list
, plugin
);
880 g_free(plugin
->filename
);
886 static gchar
*get_custom_plugin_path(const gchar
*plugin_path_config
,
887 const gchar
*plugin_path_system
)
889 gchar
*plugin_path_custom
;
891 if (EMPTY(prefs
.custom_plugin_path
))
894 plugin_path_custom
= utils_get_locale_from_utf8(prefs
.custom_plugin_path
);
895 utils_tidy_path(plugin_path_custom
);
897 /* check whether the custom plugin path is one of the system or user plugin paths
899 if (utils_str_equal(plugin_path_custom
, plugin_path_config
) ||
900 utils_str_equal(plugin_path_custom
, plugin_path_system
))
902 g_free(plugin_path_custom
);
905 return plugin_path_custom
;
909 /* all 3 paths Geany looks for plugins in can change (even system path on Windows)
910 * so we need to check active plugins are in the right place before loading */
911 static gboolean
check_plugin_path(const gchar
*fname
)
913 gchar
*plugin_path_config
;
914 gchar
*plugin_path_system
;
915 gchar
*plugin_path_custom
;
916 gboolean ret
= FALSE
;
918 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
919 if (g_str_has_prefix(fname
, plugin_path_config
))
922 plugin_path_system
= get_plugin_path();
923 if (g_str_has_prefix(fname
, plugin_path_system
))
926 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
927 if (plugin_path_custom
)
929 if (g_str_has_prefix(fname
, plugin_path_custom
))
932 g_free(plugin_path_custom
);
934 g_free(plugin_path_config
);
935 g_free(plugin_path_system
);
940 /* load active plugins at startup */
942 load_active_plugins(void)
946 if (active_plugins_pref
== NULL
|| (len
= g_strv_length(active_plugins_pref
)) == 0)
949 for (i
= 0; i
< len
; i
++)
951 const gchar
*fname
= active_plugins_pref
[i
];
953 if (!EMPTY(fname
) && g_file_test(fname
, G_FILE_TEST_EXISTS
))
955 if (!check_plugin_path(fname
) || plugin_new(fname
, TRUE
, FALSE
) == NULL
)
956 failed_plugins_list
= g_list_prepend(failed_plugins_list
, g_strdup(fname
));
963 load_plugins_from_path(const gchar
*path
)
969 list
= utils_get_file_list(path
, NULL
, NULL
);
971 for (item
= list
; item
!= NULL
; item
= g_slist_next(item
))
973 tmp
= strrchr(item
->data
, '.');
974 if (tmp
== NULL
|| utils_str_casecmp(tmp
, "." G_MODULE_SUFFIX
) != 0)
977 fname
= g_build_filename(path
, item
->data
, NULL
);
978 if (plugin_new(fname
, FALSE
, TRUE
))
983 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
987 geany_debug("Added %d plugin(s) in '%s'.", count
, path
);
991 static gchar
*get_plugin_path(void)
995 gchar
*install_dir
= win32_get_installation_dir();
997 path
= g_build_filename(install_dir
, "lib", NULL
);
1002 return g_build_filename(GEANY_LIBDIR
, "geany", NULL
);
1007 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
1008 static void load_all_plugins(void)
1010 gchar
*plugin_path_config
;
1011 gchar
*plugin_path_system
;
1012 gchar
*plugin_path_custom
;
1014 plugin_path_config
= g_build_filename(app
->configdir
, "plugins", NULL
);
1015 plugin_path_system
= get_plugin_path();
1017 /* first load plugins in ~/.config/geany/plugins/ */
1018 load_plugins_from_path(plugin_path_config
);
1020 /* load plugins from a custom path */
1021 plugin_path_custom
= get_custom_plugin_path(plugin_path_config
, plugin_path_system
);
1022 if (plugin_path_custom
)
1024 load_plugins_from_path(plugin_path_custom
);
1025 g_free(plugin_path_custom
);
1028 /* finally load plugins from $prefix/lib/geany */
1029 load_plugins_from_path(plugin_path_system
);
1031 g_free(plugin_path_config
);
1032 g_free(plugin_path_system
);
1036 static void on_tools_menu_show(GtkWidget
*menu_item
, G_GNUC_UNUSED gpointer user_data
)
1038 GList
*item
, *list
= gtk_container_get_children(GTK_CONTAINER(menu_item
));
1040 gboolean have_plugin_menu_items
= FALSE
;
1042 for (item
= list
; item
!= NULL
; item
= g_list_next(item
))
1044 if (item
->data
== menu_separator
)
1046 if (i
< g_list_length(list
) - 1)
1048 have_plugin_menu_items
= TRUE
;
1056 ui_widget_show_hide(menu_separator
, have_plugin_menu_items
);
1060 /* Calling this starts up plugin support */
1061 void plugins_load_active(void)
1065 want_plugins
= TRUE
;
1069 widget
= gtk_separator_menu_item_new();
1070 gtk_widget_show(widget
);
1071 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1073 widget
= gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
1074 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), widget
);
1075 gtk_widget_show(widget
);
1076 g_signal_connect(widget
, "activate", G_CALLBACK(pm_show_dialog
), NULL
);
1078 menu_separator
= gtk_separator_menu_item_new();
1079 gtk_container_add(GTK_CONTAINER(main_widgets
.tools_menu
), menu_separator
);
1080 g_signal_connect(main_widgets
.tools_menu
, "show", G_CALLBACK(on_tools_menu_show
), NULL
);
1082 load_active_plugins();
1086 /* Update the global active plugins list so it's up-to-date when configuration
1087 * is saved. Called in response to GeanyObject's "save-settings" signal. */
1088 static void update_active_plugins_pref(void)
1094 /* if plugins are disabled, don't clear list of active plugins */
1098 count
= g_list_length(active_plugin_list
) + g_list_length(failed_plugins_list
);
1100 g_strfreev(active_plugins_pref
);
1104 active_plugins_pref
= NULL
;
1108 active_plugins_pref
= g_new0(gchar
*, count
+ 1);
1110 for (list
= g_list_first(active_plugin_list
); list
!= NULL
; list
= list
->next
)
1112 Plugin
*plugin
= list
->data
;
1114 active_plugins_pref
[i
] = g_strdup(plugin
->filename
);
1117 for (list
= g_list_first(failed_plugins_list
); list
!= NULL
; list
= list
->next
)
1119 const gchar
*fname
= list
->data
;
1121 active_plugins_pref
[i
] = g_strdup(fname
);
1124 active_plugins_pref
[i
] = NULL
;
1128 /* called even if plugin support is disabled */
1129 void plugins_init(void)
1134 path
= get_plugin_path();
1135 geany_debug("System plugin path: %s", path
);
1138 group
= stash_group_new("plugins");
1139 configuration_add_pref_group(group
, TRUE
);
1141 stash_group_add_toggle_button(group
, &prefs
.load_plugins
,
1142 "load_plugins", TRUE
, "check_plugins");
1143 stash_group_add_entry(group
, &prefs
.custom_plugin_path
,
1144 "custom_plugin_path", "", "extra_plugin_path_entry");
1146 g_signal_connect(geany_object
, "save-settings", G_CALLBACK(update_active_plugins_pref
), NULL
);
1147 stash_group_add_string_vector(group
, &active_plugins_pref
, "active_plugins", NULL
);
1151 /* called even if plugin support is disabled */
1152 void plugins_finalize(void)
1154 if (failed_plugins_list
!= NULL
)
1156 g_list_foreach(failed_plugins_list
, (GFunc
) g_free
, NULL
);
1157 g_list_free(failed_plugins_list
);
1159 if (active_plugin_list
!= NULL
)
1161 g_list_foreach(active_plugin_list
, (GFunc
) plugin_free
, NULL
);
1162 g_list_free(active_plugin_list
);
1164 g_strfreev(active_plugins_pref
);
1168 /* Check whether there are any plugins loaded which provide a configure symbol */
1169 gboolean
plugins_have_preferences(void)
1173 if (active_plugin_list
== NULL
)
1176 foreach_list(item
, active_plugin_list
)
1178 Plugin
*plugin
= item
->data
;
1179 if (plugin
->configure
!= NULL
|| plugin
->configure_single
!= NULL
)
1187 /* Plugin Manager */
1191 PLUGIN_COLUMN_CHECK
= 0,
1192 PLUGIN_COLUMN_PLUGIN
,
1194 PM_BUTTON_KEYBINDINGS
,
1195 PM_BUTTON_CONFIGURE
,
1203 GtkListStore
*store
;
1204 GtkWidget
*configure_button
;
1205 GtkWidget
*keybindings_button
;
1206 GtkWidget
*help_button
;
1208 PluginManagerWidgets
;
1210 static PluginManagerWidgets pm_widgets
;
1213 static void pm_update_buttons(Plugin
*p
)
1217 is_active
= is_active_plugin(p
);
1218 gtk_widget_set_sensitive(pm_widgets
.configure_button
,
1219 (p
->configure
|| p
->configure_single
) && is_active
);
1220 gtk_widget_set_sensitive(pm_widgets
.help_button
, p
->help
!= NULL
&& is_active
);
1221 gtk_widget_set_sensitive(pm_widgets
.keybindings_button
,
1222 p
->key_group
&& p
->key_group
->plugin_key_count
> 0 && is_active
);
1226 static void pm_selection_changed(GtkTreeSelection
*selection
, gpointer user_data
)
1229 GtkTreeModel
*model
;
1232 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1234 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1237 pm_update_buttons(p
);
1242 static void pm_plugin_toggled(GtkCellRendererToggle
*cell
, gchar
*pth
, gpointer data
)
1244 gboolean old_state
, state
;
1247 GtkTreePath
*path
= gtk_tree_path_new_from_string(pth
);
1250 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets
.store
), &iter
, path
);
1251 gtk_tree_path_free(path
);
1253 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets
.store
), &iter
,
1254 PLUGIN_COLUMN_CHECK
, &old_state
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1256 /* no plugins item */
1260 state
= ! old_state
; /* toggle the state */
1262 /* save the filename of the plugin */
1263 file_name
= g_strdup(p
->filename
);
1265 /* unload plugin module */
1267 /* save shortcuts (only need this group, but it doesn't take long) */
1268 keybindings_write_to_file();
1272 /* reload plugin module and initialize it if item is checked */
1273 p
= plugin_new(file_name
, state
, TRUE
);
1276 /* plugin file may no longer be on disk, or is now incompatible */
1277 gtk_list_store_remove(pm_widgets
.store
, &iter
);
1282 keybindings_load_keyfile(); /* load shortcuts */
1285 gtk_list_store_set(pm_widgets
.store
, &iter
,
1286 PLUGIN_COLUMN_CHECK
, state
,
1287 PLUGIN_COLUMN_PLUGIN
, p
, -1);
1289 /* set again the sensitiveness of the configure and help buttons */
1290 pm_update_buttons(p
);
1296 static gboolean
pm_treeview_query_tooltip(GtkWidget
*widget
, gint x
, gint y
,
1297 gboolean keyboard_mode
, GtkTooltip
*tooltip
, gpointer user_data
)
1299 GtkTreeModel
*model
;
1304 if (! gtk_tree_view_get_tooltip_context(GTK_TREE_VIEW(widget
), &x
, &y
, keyboard_mode
,
1305 &model
, &path
, &iter
))
1308 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1314 details
= g_strdup_printf(_("Version:\t%s\nAuthor(s):\t%s\nFilename:\t%s"),
1315 p
->info
.version
, p
->info
.author
, p
->filename
);
1316 markup
= g_markup_printf_escaped("<b>%s</b>\n%s\n<small><i>\n%s</i></small>",
1317 p
->info
.name
, p
->info
.description
, details
);
1319 gtk_tooltip_set_markup(tooltip
, markup
);
1320 gtk_tree_view_set_tooltip_row(GTK_TREE_VIEW(widget
), tooltip
, path
);
1325 gtk_tree_path_free(path
);
1331 static void pm_treeview_text_cell_data_func(GtkTreeViewColumn
*column
, GtkCellRenderer
*cell
,
1332 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer data
)
1336 gtk_tree_model_get(model
, iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1339 g_object_set(cell
, "text", _("No plugins available."), NULL
);
1342 gchar
*markup
= g_markup_printf_escaped("<b>%s</b>\n%s", p
->info
.name
, p
->info
.description
);
1344 g_object_set(cell
, "markup", markup
, NULL
);
1350 static gint
pm_tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1355 gtk_tree_model_get(model
, a
, PLUGIN_COLUMN_PLUGIN
, &pa
, -1);
1356 gtk_tree_model_get(model
, b
, PLUGIN_COLUMN_PLUGIN
, &pb
, -1);
1359 return strcmp(pa
->info
.name
, pb
->info
.name
);
1365 static void pm_prepare_treeview(GtkWidget
*tree
, GtkListStore
*store
)
1367 GtkCellRenderer
*text_renderer
, *checkbox_renderer
;
1368 GtkTreeViewColumn
*column
;
1371 GtkTreeSelection
*sel
;
1373 g_signal_connect(tree
, "query-tooltip", G_CALLBACK(pm_treeview_query_tooltip
), NULL
);
1374 gtk_widget_set_has_tooltip(tree
, TRUE
);
1375 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
1377 checkbox_renderer
= gtk_cell_renderer_toggle_new();
1378 column
= gtk_tree_view_column_new_with_attributes(
1379 _("Active"), checkbox_renderer
, "active", PLUGIN_COLUMN_CHECK
, NULL
);
1380 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1381 g_signal_connect(checkbox_renderer
, "toggled", G_CALLBACK(pm_plugin_toggled
), NULL
);
1383 text_renderer
= gtk_cell_renderer_text_new();
1384 g_object_set(text_renderer
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1385 column
= gtk_tree_view_column_new_with_attributes(_("Plugin"), text_renderer
, NULL
);
1386 gtk_tree_view_column_set_cell_data_func(column
, text_renderer
,
1387 pm_treeview_text_cell_data_func
, NULL
, NULL
);
1388 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1390 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
1391 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree
), FALSE
);
1392 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
,
1393 pm_tree_sort_func
, NULL
, NULL
);
1394 gtk_tree_sortable_set_sort_column_id(
1395 GTK_TREE_SORTABLE(store
), PLUGIN_COLUMN_PLUGIN
, GTK_SORT_ASCENDING
);
1397 /* selection handling */
1398 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
1399 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_SINGLE
);
1400 g_signal_connect(sel
, "changed", G_CALLBACK(pm_selection_changed
), NULL
);
1402 list
= g_list_first(plugin_list
);
1405 gtk_list_store_append(store
, &iter
);
1406 gtk_list_store_set(store
, &iter
, PLUGIN_COLUMN_CHECK
, FALSE
,
1407 PLUGIN_COLUMN_PLUGIN
, NULL
, -1);
1412 for (; list
!= NULL
; list
= list
->next
)
1416 gtk_list_store_append(store
, &iter
);
1417 gtk_list_store_set(store
, &iter
,
1418 PLUGIN_COLUMN_CHECK
, is_active_plugin(p
),
1419 PLUGIN_COLUMN_PLUGIN
, p
,
1423 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), GTK_TREE_MODEL(store
));
1424 g_object_unref(store
);
1428 static void pm_on_plugin_button_clicked(GtkButton
*button
, gpointer user_data
)
1430 GtkTreeModel
*model
;
1431 GtkTreeSelection
*selection
;
1435 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets
.tree
));
1436 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1438 gtk_tree_model_get(model
, &iter
, PLUGIN_COLUMN_PLUGIN
, &p
, -1);
1442 if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_CONFIGURE
)
1443 plugin_show_configure(&p
->public);
1444 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_HELP
&& p
->help
!= NULL
)
1446 else if (GPOINTER_TO_INT(user_data
) == PM_BUTTON_KEYBINDINGS
&& p
->key_group
&& p
->key_group
->plugin_key_count
> 0)
1447 keybindings_dialog_show_prefs_scroll(p
->info
.name
);
1454 free_non_active_plugin(gpointer data
, gpointer user_data
)
1456 Plugin
*plugin
= data
;
1458 /* don't do anything when closing the plugin manager and it is an active plugin */
1459 if (is_active_plugin(plugin
))
1462 plugin_free(plugin
);
1466 /* Callback when plugin manager dialog closes, only ever has response of
1467 * GTK_RESPONSE_OK or GTK_RESPONSE_DELETE_EVENT and both are treated the same. */
1468 static void pm_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1470 if (plugin_list
!= NULL
)
1472 /* remove all non-active plugins from the list */
1473 g_list_foreach(plugin_list
, free_non_active_plugin
, NULL
);
1474 g_list_free(plugin_list
);
1477 gtk_widget_destroy(GTK_WIDGET(dialog
));
1479 configuration_save();
1483 static void pm_show_dialog(GtkMenuItem
*menuitem
, gpointer user_data
)
1485 GtkWidget
*vbox
, *vbox2
, *hbox
, *swin
, *label
;
1487 /* before showing the dialog, we need to create the list of available plugins */
1490 pm_widgets
.dialog
= gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets
.window
),
1491 GTK_DIALOG_DESTROY_WITH_PARENT
,
1492 GTK_STOCK_OK
, GTK_RESPONSE_OK
, NULL
);
1493 vbox
= ui_dialog_vbox_new(GTK_DIALOG(pm_widgets
.dialog
));
1494 gtk_widget_set_name(pm_widgets
.dialog
, "GeanyDialog");
1495 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
1497 gtk_window_set_default_size(GTK_WINDOW(pm_widgets
.dialog
), 500, 450);
1499 pm_widgets
.tree
= gtk_tree_view_new();
1500 pm_widgets
.store
= gtk_list_store_new(
1501 PLUGIN_N_COLUMNS
, G_TYPE_BOOLEAN
, G_TYPE_POINTER
);
1502 pm_prepare_treeview(pm_widgets
.tree
, pm_widgets
.store
);
1504 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1505 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
1506 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
1507 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1508 gtk_container_add(GTK_CONTAINER(swin
), pm_widgets
.tree
);
1510 label
= geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1512 pm_widgets
.keybindings_button
= gtk_button_new_with_label(_("Keybindings"));
1513 gtk_widget_set_sensitive(pm_widgets
.keybindings_button
, FALSE
);
1514 g_signal_connect(pm_widgets
.keybindings_button
, "clicked",
1515 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_KEYBINDINGS
));
1517 pm_widgets
.configure_button
= gtk_button_new_from_stock(GTK_STOCK_PREFERENCES
);
1518 gtk_widget_set_sensitive(pm_widgets
.configure_button
, FALSE
);
1519 g_signal_connect(pm_widgets
.configure_button
, "clicked",
1520 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_CONFIGURE
));
1522 pm_widgets
.help_button
= gtk_button_new_from_stock(GTK_STOCK_HELP
);
1523 gtk_widget_set_sensitive(pm_widgets
.help_button
, FALSE
);
1524 g_signal_connect(pm_widgets
.help_button
, "clicked",
1525 G_CALLBACK(pm_on_plugin_button_clicked
), GINT_TO_POINTER(PM_BUTTON_HELP
));
1527 hbox
= gtk_hbox_new(FALSE
, 0);
1528 gtk_box_set_spacing(GTK_BOX(hbox
), 6);
1529 gtk_box_pack_end(GTK_BOX(hbox
), pm_widgets
.keybindings_button
, FALSE
, FALSE
, 0);
1530 gtk_box_pack_end(GTK_BOX(hbox
), pm_widgets
.configure_button
, FALSE
, FALSE
, 0);
1531 gtk_box_pack_end(GTK_BOX(hbox
), pm_widgets
.help_button
, FALSE
, FALSE
, 0);
1533 vbox2
= gtk_vbox_new(FALSE
, 3);
1534 gtk_box_pack_start(GTK_BOX(vbox2
), label
, FALSE
, FALSE
, 5);
1535 gtk_box_pack_start(GTK_BOX(vbox2
), swin
, TRUE
, TRUE
, 0);
1536 gtk_box_pack_start(GTK_BOX(vbox2
), hbox
, FALSE
, TRUE
, 0);
1538 g_signal_connect(pm_widgets
.dialog
, "response", G_CALLBACK(pm_dialog_response
), NULL
);
1540 gtk_box_pack_start(GTK_BOX(vbox
), vbox2
, TRUE
, TRUE
, 0);
1541 gtk_widget_show_all(pm_widgets
.dialog
);