Add symbols_get_context_separator() to plugin API (patch by Colomban
[geany-mirror.git] / src / plugins.c
blobbc938ee52883f1b678d64149c7e398f40fbc5ab1
1 /*
2 * plugins.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 * $Id$
25 /* Code to manage, load and unload plugins. */
27 #include "geany.h"
29 #ifdef HAVE_PLUGINS
31 #include <string.h>
33 #include "Scintilla.h"
34 #include "ScintillaWidget.h"
36 #include "prefix.h"
37 #include "plugins.h"
38 #include "plugindata.h"
39 #include "support.h"
40 #include "utils.h"
41 #include "document.h"
42 #include "filetypes.h"
43 #include "templates.h"
44 #include "sciwrappers.h"
45 #include "ui_utils.h"
46 #include "editor.h"
47 #include "dialogs.h"
48 #include "msgwindow.h"
49 #include "prefs.h"
50 #include "geanywraplabel.h"
51 #include "build.h"
52 #include "encodings.h"
53 #include "search.h"
54 #include "highlighting.h"
55 #include "keybindings.h"
56 #include "navqueue.h"
57 #include "main.h"
58 #include "toolbar.h"
59 #include "stash.h"
60 #include "symbols.h"
61 #include "keyfile.h"
62 #include "win32.h"
63 #include "pluginutils.h"
64 #include "pluginprivate.h"
67 GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
70 static gboolean want_plugins = FALSE;
72 /* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
73 * opened, afterwards it will be destroyed */
74 static GList *plugin_list = NULL;
75 static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
76 static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
78 static GtkWidget *menu_separator = NULL;
80 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
83 static PluginFuncs plugin_funcs = {
84 &plugin_add_toolbar_item,
85 &plugin_module_make_resident,
86 &plugin_signal_connect,
87 &plugin_set_key_group,
88 &plugin_show_configure
91 static DocumentFuncs doc_funcs = {
92 &document_new_file,
93 &document_get_current,
94 &document_get_from_page,
95 &document_find_by_filename,
96 &document_find_by_real_path,
97 &document_save_file,
98 &document_open_file,
99 &document_open_files,
100 &document_remove_page,
101 &document_reload_file,
102 &document_set_encoding,
103 &document_set_text_changed,
104 &document_set_filetype,
105 &document_close,
106 &document_index,
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
114 static EditorFuncs editor_funcs = {
115 &editor_get_indent_prefs,
116 &editor_create_widget,
117 &editor_indicator_set_on_range,
118 &editor_indicator_set_on_line,
119 &editor_indicator_clear,
120 &editor_set_indent_type,
121 &editor_get_word_at_pos,
122 &editor_get_eol_char_name,
123 &editor_get_eol_char_len,
124 &editor_get_eol_char,
125 &editor_insert_text_block
128 static ScintillaFuncs scintilla_funcs = {
129 &scintilla_send_message,
130 &scintilla_new
133 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
134 #define _scintilla_send_message_macro scintilla_send_message
136 static SciFuncs sci_funcs = {
137 &_scintilla_send_message_macro,
138 &sci_send_command,
139 &sci_start_undo_action,
140 &sci_end_undo_action,
141 &sci_set_text,
142 &sci_insert_text,
143 &sci_get_text,
144 &sci_get_length,
145 &sci_get_current_position,
146 &sci_set_current_position,
147 &sci_get_col_from_position,
148 &sci_get_line_from_position,
149 &sci_get_position_from_line,
150 &sci_replace_sel,
151 &sci_get_selected_text,
152 &sci_get_selected_text_length,
153 &sci_get_selection_start,
154 &sci_get_selection_end,
155 &sci_get_selection_mode,
156 &sci_set_selection_mode,
157 &sci_set_selection_start,
158 &sci_set_selection_end,
159 &sci_get_text_range,
160 &sci_get_line,
161 &sci_get_line_length,
162 &sci_get_line_count,
163 &sci_get_line_is_visible,
164 &sci_ensure_line_is_visible,
165 &sci_scroll_caret,
166 &sci_find_matching_brace,
167 &sci_get_style_at,
168 &sci_get_char_at,
169 &sci_get_current_line,
170 &sci_has_selection,
171 &sci_get_tab_width,
172 &sci_indicator_clear,
173 &sci_indicator_set,
174 &sci_get_contents,
175 &sci_get_contents_range,
176 &sci_get_selection_contents,
177 &sci_set_font,
178 &sci_get_line_end_position,
179 &sci_set_target_start,
180 &sci_set_target_end,
181 &sci_replace_target,
182 &sci_set_marker_at_line,
183 &sci_delete_marker_at_line,
184 &sci_is_marker_set_at_line,
185 &sci_goto_line,
186 &sci_find_text,
187 &sci_set_line_indentation,
188 &sci_get_line_indentation
191 static TemplateFuncs template_funcs = {
192 &templates_get_template_fileheader
195 static UtilsFuncs utils_funcs = {
196 &utils_str_equal,
197 &utils_string_replace_all,
198 &utils_get_file_list,
199 &utils_write_file,
200 &utils_get_locale_from_utf8,
201 &utils_get_utf8_from_locale,
202 &utils_remove_ext_from_filename,
203 &utils_mkdir,
204 &utils_get_setting_boolean,
205 &utils_get_setting_integer,
206 &utils_get_setting_string,
207 &utils_spawn_sync,
208 &utils_spawn_async,
209 &utils_str_casecmp,
210 &utils_get_date_time,
211 &utils_open_browser,
212 &utils_string_replace_first,
213 &utils_str_middle_truncate,
214 &utils_str_remove_chars,
215 &utils_get_file_list_full,
216 &utils_copy_environment
219 static UIUtilsFuncs uiutils_funcs = {
220 &ui_dialog_vbox_new,
221 &ui_frame_new_with_alignment,
222 &ui_set_statusbar,
223 &ui_table_add_row,
224 &ui_path_box_new,
225 &ui_button_new_with_image,
226 &ui_add_document_sensitive,
227 &ui_widget_set_tooltip_text,
228 &ui_image_menu_item_new,
229 &ui_lookup_widget,
230 &ui_progress_bar_start,
231 &ui_progress_bar_stop,
232 &ui_entry_add_clear_icon,
233 &ui_menu_add_document_items,
234 &ui_widget_modify_font_from_string,
235 &ui_is_keyval_enter_or_return,
236 &ui_get_gtk_settings_integer
239 static DialogFuncs dialog_funcs = {
240 &dialogs_show_question,
241 &dialogs_show_msgbox,
242 &dialogs_show_save_as,
243 &dialogs_show_input_numeric
246 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
247 #define _lookup_widget_macro ui_lookup_widget
249 /* deprecated */
250 static SupportFuncs support_funcs = {
251 &_lookup_widget_macro
254 static MsgWinFuncs msgwin_funcs = {
255 &msgwin_status_add,
256 &msgwin_compiler_add,
257 &msgwin_msg_add,
258 &msgwin_clear_tab,
259 &msgwin_switch_tab
262 static EncodingFuncs encoding_funcs = {
263 &encodings_convert_to_utf8,
264 &encodings_convert_to_utf8_from_charset,
265 &encodings_get_charset_from_index
268 static KeybindingFuncs keybindings_funcs = {
269 &keybindings_send_command,
270 &keybindings_set_item,
271 &keybindings_get_item
274 static TagManagerFuncs tagmanager_funcs = {
275 &tm_get_real_path,
276 &tm_source_file_new,
277 &tm_workspace_add_object,
278 &tm_source_file_update,
279 &tm_work_object_free,
280 &tm_workspace_remove_object
283 static SearchFuncs search_funcs = {
284 &search_show_find_in_files_dialog
287 static HighlightingFuncs highlighting_funcs = {
288 &highlighting_get_style,
289 &highlighting_set_styles
292 static FiletypeFuncs filetype_funcs = {
293 &filetypes_detect_from_file,
294 &filetypes_lookup_by_name,
295 &filetypes_index
298 static NavQueueFuncs navqueue_funcs = {
299 &navqueue_goto_line
302 static MainFuncs main_funcs = {
303 &main_reload_configuration,
304 &main_locale_init,
305 &main_is_realized
308 static StashFuncs stash_funcs = {
309 &stash_group_new,
310 &stash_group_add_boolean,
311 &stash_group_add_integer,
312 &stash_group_add_string,
313 &stash_group_add_string_vector,
314 &stash_group_load_from_key_file,
315 &stash_group_save_to_key_file,
316 &stash_group_free,
317 &stash_group_load_from_file,
318 &stash_group_save_to_file,
319 &stash_group_add_toggle_button,
320 &stash_group_add_radio_buttons,
321 &stash_group_add_spin_button_integer,
322 &stash_group_add_combo_box,
323 &stash_group_add_combo_box_entry,
324 &stash_group_add_entry,
325 &stash_group_add_widget_property,
326 &stash_group_display,
327 &stash_group_update
330 static SymbolsFuncs symbols_funcs = {
331 &symbols_get_context_separator
334 static GeanyFunctions geany_functions = {
335 &doc_funcs,
336 &sci_funcs,
337 &template_funcs,
338 &utils_funcs,
339 &uiutils_funcs,
340 &support_funcs,
341 &dialog_funcs,
342 &msgwin_funcs,
343 &encoding_funcs,
344 &keybindings_funcs,
345 &tagmanager_funcs,
346 &search_funcs,
347 &highlighting_funcs,
348 &filetype_funcs,
349 &navqueue_funcs,
350 &editor_funcs,
351 &main_funcs,
352 &plugin_funcs,
353 &scintilla_funcs,
354 &msgwin_funcs,
355 &stash_funcs,
356 &symbols_funcs
359 static GeanyData geany_data;
362 static void
363 geany_data_init(void)
365 GeanyData gd = {
366 app,
367 &main_widgets,
368 documents_array,
369 filetypes_array,
370 &prefs,
371 &interface_prefs,
372 &toolbar_prefs,
373 &editor_prefs,
374 &file_prefs,
375 &search_prefs,
376 &tool_prefs,
377 &template_prefs,
378 &build_info,
379 filetypes_by_title
381 memcpy(&geany_data, &gd, sizeof(GeanyData));
385 /* Prevent the same plugin filename being loaded more than once.
386 * Note: g_module_name always returns the .so name, even when Plugin::filename is an .la file. */
387 static gboolean
388 plugin_loaded(GModule *module)
390 gchar *basename_module, *basename_loaded;
391 GList *item;
393 basename_module = g_path_get_basename(g_module_name(module));
394 for (item = plugin_list; item != NULL; item = g_list_next(item))
396 basename_loaded = g_path_get_basename(
397 g_module_name(((Plugin*)item->data)->module));
399 if (utils_str_equal(basename_module, basename_loaded))
401 g_free(basename_loaded);
402 g_free(basename_module);
403 return TRUE;
405 g_free(basename_loaded);
407 /* Look also through the list of active plugins. This prevents problems when we have the same
408 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
409 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
410 * the plugin manager would list both plugins. Additionally, unloading the active plugin
411 * would cause a crash. */
412 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
414 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
416 if (utils_str_equal(basename_module, basename_loaded))
418 g_free(basename_loaded);
419 g_free(basename_module);
420 return TRUE;
422 g_free(basename_loaded);
424 g_free(basename_module);
425 return FALSE;
429 static Plugin *find_active_plugin_by_name(const gchar *filename)
431 GList *item;
433 g_return_val_if_fail(filename, FALSE);
435 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
437 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
438 return item->data;
441 return NULL;
445 static gboolean
446 plugin_check_version(GModule *module)
448 gint (*version_check)(gint) = NULL;
450 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
452 if (G_UNLIKELY(! version_check))
454 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
455 g_module_name(module));
456 return FALSE;
458 else
460 gint result = version_check(GEANY_ABI_VERSION);
462 if (result < 0)
464 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
465 "release of Geany - please recompile it."), g_module_name(module));
466 geany_debug("Plugin \"%s\" is not binary compatible with this "
467 "release of Geany - recompile it.", g_module_name(module));
468 return FALSE;
470 if (result > 0)
472 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
473 g_module_name(module), result);
474 return FALSE;
477 return TRUE;
481 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
483 PluginCallback *cb;
484 guint i, len = 0;
486 while (TRUE)
488 cb = &callbacks[len];
489 if (!cb->signal_name || !cb->callback)
490 break;
491 len++;
493 if (len == 0)
494 return;
496 for (i = 0; i < len; i++)
498 cb = &callbacks[i];
500 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
501 cb->callback, cb->user_data);
506 static void read_key_group(Plugin *plugin)
508 GeanyKeyGroupInfo *p_key_info;
509 GeanyKeyGroup **p_key_group;
511 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
512 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
513 if (p_key_info && p_key_group)
515 GeanyKeyGroupInfo *key_info = p_key_info;
517 if (*p_key_group)
518 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
519 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
520 plugin->info.name);
521 else
523 if (key_info->count)
525 GeanyKeyGroup *key_group =
526 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
527 if (key_group)
528 *p_key_group = key_group;
530 else
531 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
532 "count field is zero. Maybe use plugin_set_key_group() instead?",
533 plugin->info.name);
536 else if (p_key_info || p_key_group)
537 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
538 "Maybe use plugin_set_key_group() instead?",
539 plugin->info.name);
543 static void
544 plugin_init(Plugin *plugin)
546 GeanyPlugin **p_geany_plugin;
547 PluginCallback *callbacks;
548 PluginInfo **p_info;
549 PluginFields **plugin_fields;
550 GeanyData **p_geany_data;
551 GeanyFunctions **p_geany_functions;
553 /* set these symbols before plugin_init() is called */
554 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
555 if (p_geany_plugin)
556 *p_geany_plugin = &plugin->public;
557 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
558 if (p_info)
559 *p_info = &plugin->info;
560 g_module_symbol(plugin->module, "geany_data", (void *) &p_geany_data);
561 if (p_geany_data)
562 *p_geany_data = &geany_data;
563 g_module_symbol(plugin->module, "geany_functions", (void *) &p_geany_functions);
564 if (p_geany_functions)
565 *p_geany_functions = &geany_functions;
566 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
567 if (plugin_fields)
568 *plugin_fields = &plugin->fields;
569 read_key_group(plugin);
571 /* start the plugin */
572 g_return_if_fail(plugin->init);
573 plugin->init(&geany_data);
575 /* store some function pointers for later use */
576 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
577 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
578 if (app->debug_mode && plugin->configure && plugin->configure_single)
579 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
580 "only plugin_configure() will be used!",
581 plugin->info.name);
583 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
584 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
585 if (plugin->cleanup == NULL)
587 if (app->debug_mode)
588 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
589 plugin->info.name);
592 /* now read any plugin-owned data that might have been set in plugin_init() */
594 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
596 ui_add_document_sensitive(plugin->fields.menu_item);
599 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
600 if (callbacks)
601 add_callbacks(plugin, callbacks);
603 /* remember which plugins are active */
604 active_plugin_list = g_list_append(active_plugin_list, plugin);
606 geany_debug("Loaded: %s (%s)", plugin->filename,
607 NVL(plugin->info.name, "<Unknown>"));
611 /* Load and optionally init a plugin.
612 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
613 * called, the plugin will be started, if not the plugin will be read only (for the list of
614 * available plugins in the plugin manager).
615 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
616 static Plugin*
617 plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
619 Plugin *plugin;
620 GModule *module;
621 void (*plugin_set_info)(PluginInfo*);
623 g_return_val_if_fail(fname, NULL);
624 g_return_val_if_fail(g_module_supported(), NULL);
626 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
627 * load the module */
628 plugin = find_active_plugin_by_name(fname);
629 if (plugin != NULL)
631 geany_debug("Plugin \"%s\" already loaded.", fname);
632 if (add_to_list)
633 plugin_list = g_list_append(plugin_list, plugin);
634 return plugin;
637 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
638 * causing a segfault. Without that flag the module will safely fail to load.
639 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
640 * otherwise not be detected due to the shadowing of Geany's app variable.
641 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
642 * function from a plugin will be shadowed. */
643 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
644 if (! module)
646 geany_debug("Can't load plugin: %s", g_module_error());
647 return NULL;
650 if (plugin_loaded(module))
652 geany_debug("Plugin \"%s\" already loaded.", fname);
654 if (! g_module_close(module))
655 g_warning("%s: %s", fname, g_module_error());
656 return NULL;
659 if (! plugin_check_version(module))
661 if (! g_module_close(module))
662 g_warning("%s: %s", fname, g_module_error());
663 return NULL;
666 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
667 if (plugin_set_info == NULL)
669 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
671 if (! g_module_close(module))
672 g_warning("%s: %s", fname, g_module_error());
673 return NULL;
676 plugin = g_new0(Plugin, 1);
678 /* read plugin name, etc. */
679 plugin_set_info(&plugin->info);
680 if (!NZV(plugin->info.name))
682 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
683 fname);
685 if (! g_module_close(module))
686 g_warning("%s: %s", fname, g_module_error());
687 g_free(plugin);
688 return NULL;
691 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
692 if (plugin->init == NULL)
694 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
695 plugin->info.name);
697 if (! g_module_close(module))
698 g_warning("%s: %s", fname, g_module_error());
699 g_free(plugin);
700 return NULL;
702 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
704 plugin->filename = g_strdup(fname);
705 plugin->module = module;
706 plugin->public.info = &plugin->info;
707 plugin->public.priv = plugin;
709 if (init_plugin)
710 plugin_init(plugin);
712 if (add_to_list)
713 plugin_list = g_list_append(plugin_list, plugin);
715 return plugin;
719 static void remove_callbacks(Plugin *plugin)
721 GArray *signal_ids = plugin->signal_ids;
722 SignalConnection *sc;
724 if (signal_ids == NULL)
725 return;
727 foreach_array(SignalConnection, sc, signal_ids)
728 g_signal_handler_disconnect(sc->object, sc->handler_id);
730 g_array_free(signal_ids, TRUE);
734 static gboolean is_active_plugin(Plugin *plugin)
736 return (g_list_find(active_plugin_list, plugin) != NULL);
740 /* Clean up anything used by an active plugin */
741 static void
742 plugin_cleanup(Plugin *plugin)
744 GtkWidget *widget;
746 if (plugin->cleanup)
747 plugin->cleanup();
749 remove_callbacks(plugin);
751 if (plugin->key_group)
752 keybindings_free_group(plugin->key_group);
754 widget = plugin->toolbar_separator.widget;
755 if (widget)
756 gtk_widget_destroy(widget);
758 geany_debug("Unloaded: %s", plugin->filename);
762 static void
763 plugin_free(Plugin *plugin)
765 g_return_if_fail(plugin);
766 g_return_if_fail(plugin->module);
768 if (is_active_plugin(plugin))
769 plugin_cleanup(plugin);
771 active_plugin_list = g_list_remove(active_plugin_list, plugin);
773 if (! g_module_close(plugin->module))
774 g_warning("%s: %s", plugin->filename, g_module_error());
776 plugin_list = g_list_remove(plugin_list, plugin);
778 g_free(plugin->filename);
779 g_free(plugin);
780 plugin = NULL;
784 /* load active plugins at startup */
785 static void
786 load_active_plugins(void)
788 guint i, len;
790 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
791 return;
793 for (i = 0; i < len; i++)
795 const gchar *fname = active_plugins_pref[i];
797 if (NZV(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
799 if (plugin_new(fname, TRUE, FALSE) == NULL)
800 failed_plugins_list = g_list_append(failed_plugins_list, g_strdup(fname));
806 static void
807 load_plugins_from_path(const gchar *path)
809 GSList *list, *item;
810 gchar *fname, *tmp;
812 list = utils_get_file_list(path, NULL, NULL);
814 for (item = list; item != NULL; item = g_slist_next(item))
816 tmp = strrchr(item->data, '.');
817 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
818 continue;
820 fname = g_strconcat(path, G_DIR_SEPARATOR_S, item->data, NULL);
821 plugin_new(fname, FALSE, TRUE);
822 g_free(fname);
825 g_slist_foreach(list, (GFunc) g_free, NULL);
826 g_slist_free(list);
830 #ifdef G_OS_WIN32
831 static gchar *get_plugin_path()
833 gchar *install_dir = win32_get_installation_dir();
834 gchar *path;
836 path = g_strconcat(install_dir, "\\lib", NULL);
837 g_free(install_dir);
839 return path;
841 #endif
844 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
845 static void load_all_plugins(void)
847 gchar *path;
849 path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", NULL);
850 /* first load plugins in ~/.config/geany/plugins/ */
851 load_plugins_from_path(path);
852 g_free(path);
854 /* load plugins from a custom path */
855 if (NZV(prefs.custom_plugin_path))
856 load_plugins_from_path(prefs.custom_plugin_path);
858 /* finally load plugins from $prefix/lib/geany */
859 #ifdef G_OS_WIN32
860 path = get_plugin_path();
861 #else
862 path = g_strconcat(GEANY_LIBDIR, G_DIR_SEPARATOR_S "geany", NULL);
863 #endif
864 load_plugins_from_path(path);
865 g_free(path);
869 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
871 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
872 guint i = 0;
873 gboolean have_plugin_menu_items = FALSE;
875 for (item = list; item != NULL; item = g_list_next(item))
877 if (item->data == menu_separator)
879 if (i < g_list_length(list) - 1)
881 have_plugin_menu_items = TRUE;
882 break;
885 i++;
887 g_list_free(list);
889 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
893 /* Calling this starts up plugin support */
894 void plugins_load_active(void)
896 GtkWidget *widget;
898 want_plugins = TRUE;
900 geany_data_init();
902 widget = gtk_separator_menu_item_new();
903 gtk_widget_show(widget);
904 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
906 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
907 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
908 gtk_widget_show(widget);
909 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
911 menu_separator = gtk_separator_menu_item_new();
912 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
913 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
915 load_active_plugins();
919 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
921 const Plugin *pa = a;
922 const Plugin *pb = b;
924 return strcmp(pa->info.name, pb->info.name);
928 static void update_active_plugins_pref(void)
930 gint i = 0;
931 GList *list;
932 gsize count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
934 g_strfreev(active_plugins_pref);
936 if (count == 0)
938 active_plugins_pref = NULL;
939 return;
942 /* sort the list so next time tools menu items are sorted by plugin name
943 * (not ideal to do here, but better than nothing) */
944 active_plugin_list = g_list_sort(active_plugin_list, cmp_plugin_names);
946 active_plugins_pref = g_new0(gchar*, count + 1);
948 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
950 Plugin *plugin = list->data;
952 active_plugins_pref[i] = g_strdup(plugin->filename);
953 i++;
955 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
957 const gchar *fname = list->data;
959 active_plugins_pref[i] = g_strdup(fname);
960 i++;
962 active_plugins_pref[i] = NULL;
966 static void on_save_settings(GKeyFile *config)
968 /* if plugins are disabled, don't clear list of active plugins */
969 if (want_plugins)
970 update_active_plugins_pref();
974 /* called even if plugin support is disabled */
975 void plugins_init(void)
977 StashGroup *group;
979 group = stash_group_new("plugins");
980 configuration_add_pref_group(group, TRUE);
982 stash_group_add_toggle_button(group, &prefs.load_plugins,
983 "load_plugins", TRUE, "check_plugins");
984 stash_group_add_entry(group, &prefs.custom_plugin_path,
985 "custom_plugin_path", "", "extra_plugin_path_entry");
987 g_signal_connect(geany_object, "save-settings", G_CALLBACK(on_save_settings), NULL);
988 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
992 /* called even if plugin support is disabled */
993 void plugins_finalize(void)
995 if (failed_plugins_list != NULL)
997 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
998 g_list_free(failed_plugins_list);
1000 if (active_plugin_list != NULL)
1002 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1003 g_list_free(active_plugin_list);
1005 g_strfreev(active_plugins_pref);
1009 /* Check whether there are any plugins loaded which provide a configure symbol */
1010 gboolean plugins_have_preferences(void)
1012 GList *item;
1014 if (active_plugin_list == NULL)
1015 return FALSE;
1017 foreach_list(item, active_plugin_list)
1019 Plugin *plugin = item->data;
1020 if (plugin->configure != NULL || plugin->configure_single != NULL)
1021 return TRUE;
1024 return FALSE;
1028 /* Plugin Manager */
1030 enum
1032 PLUGIN_COLUMN_CHECK = 0,
1033 PLUGIN_COLUMN_NAME,
1034 PLUGIN_COLUMN_FILE,
1035 PLUGIN_COLUMN_PLUGIN,
1036 PLUGIN_N_COLUMNS,
1037 PM_BUTTON_CONFIGURE,
1038 PM_BUTTON_HELP
1041 typedef struct
1043 GtkWidget *dialog;
1044 GtkWidget *tree;
1045 GtkListStore *store;
1046 GtkWidget *description_label;
1047 GtkWidget *configure_button;
1048 GtkWidget *help_button;
1050 PluginManagerWidgets;
1052 static PluginManagerWidgets pm_widgets;
1055 static void pm_update_buttons(Plugin *p)
1057 gboolean is_active;
1059 is_active = is_active_plugin(p);
1060 gtk_widget_set_sensitive(pm_widgets.configure_button,
1061 (p->configure || p->configure_single) && is_active);
1062 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
1066 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1068 GtkTreeIter iter;
1069 GtkTreeModel *model;
1070 Plugin *p;
1072 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1074 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1076 if (p != NULL)
1078 gchar *text;
1079 PluginInfo *pi;
1081 pi = &p->info;
1082 text = g_strdup_printf(
1083 _("Plugin: %s %s\nDescription: %s\nAuthor(s): %s"),
1084 pi->name, pi->version, pi->description, pi->author);
1086 geany_wrap_label_set_text(GTK_LABEL(pm_widgets.description_label), text);
1087 g_free(text);
1089 pm_update_buttons(p);
1095 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1097 gboolean old_state, state;
1098 gchar *file_name;
1099 GtkTreeIter iter;
1100 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1101 Plugin *p;
1103 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
1104 gtk_tree_path_free(path);
1106 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
1107 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
1109 /* no plugins item */
1110 if (p == NULL)
1111 return;
1113 state = ! old_state; /* toggle the state */
1115 /* save the filename of the plugin */
1116 file_name = g_strdup(p->filename);
1118 /* unload plugin module */
1119 if (!state)
1120 /* save shortcuts (only need this group, but it doesn't take long) */
1121 keybindings_write_to_file();
1123 plugin_free(p);
1125 /* reload plugin module and initialize it if item is checked */
1126 p = plugin_new(file_name, state, TRUE);
1127 if (!p)
1129 /* plugin file may no longer be on disk, or is now incompatible */
1130 gtk_list_store_remove(pm_widgets.store, &iter);
1132 else
1134 if (state)
1135 keybindings_load_keyfile(); /* load shortcuts */
1137 /* update model */
1138 gtk_list_store_set(pm_widgets.store, &iter,
1139 PLUGIN_COLUMN_CHECK, state,
1140 PLUGIN_COLUMN_PLUGIN, p, -1);
1142 /* set again the sensitiveness of the configure and help buttons */
1143 pm_update_buttons(p);
1145 g_free(file_name);
1149 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1151 GtkCellRenderer *text_renderer, *checkbox_renderer;
1152 GtkTreeViewColumn *column;
1153 GtkTreeIter iter;
1154 GList *list;
1155 GtkTreeSelection *sel;
1157 checkbox_renderer = gtk_cell_renderer_toggle_new();
1158 column = gtk_tree_view_column_new_with_attributes(
1159 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1160 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1161 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1163 text_renderer = gtk_cell_renderer_text_new();
1164 column = gtk_tree_view_column_new_with_attributes(
1165 _("Plugin"), text_renderer, "text", PLUGIN_COLUMN_NAME, NULL);
1166 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1168 text_renderer = gtk_cell_renderer_text_new();
1169 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1170 column = gtk_tree_view_column_new_with_attributes(
1171 _("File"), text_renderer, "text", PLUGIN_COLUMN_FILE, NULL);
1172 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1174 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1175 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1176 gtk_tree_sortable_set_sort_column_id(
1177 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_NAME, GTK_SORT_ASCENDING);
1179 /* selection handling */
1180 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1181 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1182 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1184 list = g_list_first(plugin_list);
1185 if (list == NULL)
1187 gtk_list_store_append(store, &iter);
1188 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1189 PLUGIN_COLUMN_NAME, _("No plugins available."),
1190 PLUGIN_COLUMN_FILE, "", PLUGIN_COLUMN_PLUGIN, NULL, -1);
1192 else
1194 Plugin *p;
1195 for (; list != NULL; list = list->next)
1197 p = list->data;
1199 gtk_list_store_append(store, &iter);
1200 gtk_list_store_set(store, &iter,
1201 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1202 PLUGIN_COLUMN_NAME, p->info.name,
1203 PLUGIN_COLUMN_FILE, p->filename,
1204 PLUGIN_COLUMN_PLUGIN, p,
1205 -1);
1208 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1209 g_object_unref(store);
1213 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1215 GtkTreeModel *model;
1216 GtkTreeSelection *selection;
1217 GtkTreeIter iter;
1218 Plugin *p;
1220 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1221 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1223 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1225 if (p != NULL)
1227 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1228 plugin_show_configure(&p->public);
1229 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1230 p->help();
1236 static void
1237 free_non_active_plugin(gpointer data, gpointer user_data)
1239 Plugin *plugin = data;
1241 /* don't do anything when closing the plugin manager and it is an active plugin */
1242 if (is_active_plugin(plugin))
1243 return;
1245 plugin_free(plugin);
1249 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1251 if (plugin_list != NULL)
1253 /* remove all non-active plugins from the list */
1254 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1255 g_list_free(plugin_list);
1256 plugin_list = NULL;
1258 gtk_widget_destroy(GTK_WIDGET(dialog));
1262 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1264 GtkWidget *vbox, *vbox2, *label_vbox, *hbox, *swin, *label, *label2, *desc_win;
1266 /* before showing the dialog, we need to create the list of available plugins */
1267 load_all_plugins();
1269 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1270 GTK_DIALOG_DESTROY_WITH_PARENT,
1271 GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
1272 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1273 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1274 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1276 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1278 pm_widgets.tree = gtk_tree_view_new();
1279 pm_widgets.store = gtk_list_store_new(
1280 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1281 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1283 swin = gtk_scrolled_window_new(NULL, NULL);
1284 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1285 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1286 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1287 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1289 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1291 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1292 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1293 g_signal_connect(pm_widgets.configure_button, "clicked",
1294 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1296 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1297 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1298 g_signal_connect(pm_widgets.help_button, "clicked",
1299 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1301 label2 = gtk_label_new(_("<b>Plugin details:</b>"));
1302 gtk_label_set_use_markup(GTK_LABEL(label2), TRUE);
1303 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
1305 pm_widgets.description_label = geany_wrap_label_new("");
1306 desc_win = gtk_scrolled_window_new(NULL, NULL);
1307 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win),
1308 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1309 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win),
1310 pm_widgets.description_label);
1312 hbox = gtk_hbox_new(FALSE, 0);
1313 gtk_box_pack_start(GTK_BOX(hbox), label2, TRUE, TRUE, 0);
1314 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 4);
1315 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1317 label_vbox = gtk_vbox_new(FALSE, 3);
1318 gtk_box_pack_start(GTK_BOX(label_vbox), hbox, FALSE, FALSE, 0);
1319 gtk_box_pack_start(GTK_BOX(label_vbox), desc_win, FALSE, FALSE, 0);
1321 vbox2 = gtk_vbox_new(FALSE, 3);
1322 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1323 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1324 gtk_box_pack_start(GTK_BOX(vbox2), label_vbox, FALSE, FALSE, 0);
1326 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1328 gtk_container_add(GTK_CONTAINER(vbox), vbox2);
1329 gtk_widget_show_all(pm_widgets.dialog);
1333 #endif