Add API functions (patch by Eugene Arshinov, thanks):
[geany-mirror.git] / src / plugins.c
blob7e2ba5b2b27457289bbecddcce72e56d278f42fd
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,
126 &editor_get_eol_char_mode,
127 &editor_goto_pos,
128 &editor_find_snippet,
129 &editor_insert_snippet
132 static ScintillaFuncs scintilla_funcs = {
133 &scintilla_send_message,
134 &scintilla_new
137 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
138 #define _scintilla_send_message_macro scintilla_send_message
140 static SciFuncs sci_funcs = {
141 &_scintilla_send_message_macro,
142 &sci_send_command,
143 &sci_start_undo_action,
144 &sci_end_undo_action,
145 &sci_set_text,
146 &sci_insert_text,
147 &sci_get_text,
148 &sci_get_length,
149 &sci_get_current_position,
150 &sci_set_current_position,
151 &sci_get_col_from_position,
152 &sci_get_line_from_position,
153 &sci_get_position_from_line,
154 &sci_replace_sel,
155 &sci_get_selected_text,
156 &sci_get_selected_text_length,
157 &sci_get_selection_start,
158 &sci_get_selection_end,
159 &sci_get_selection_mode,
160 &sci_set_selection_mode,
161 &sci_set_selection_start,
162 &sci_set_selection_end,
163 &sci_get_text_range,
164 &sci_get_line,
165 &sci_get_line_length,
166 &sci_get_line_count,
167 &sci_get_line_is_visible,
168 &sci_ensure_line_is_visible,
169 &sci_scroll_caret,
170 &sci_find_matching_brace,
171 &sci_get_style_at,
172 &sci_get_char_at,
173 &sci_get_current_line,
174 &sci_has_selection,
175 &sci_get_tab_width,
176 &sci_indicator_clear,
177 &sci_indicator_set,
178 &sci_get_contents,
179 &sci_get_contents_range,
180 &sci_get_selection_contents,
181 &sci_set_font,
182 &sci_get_line_end_position,
183 &sci_set_target_start,
184 &sci_set_target_end,
185 &sci_replace_target,
186 &sci_set_marker_at_line,
187 &sci_delete_marker_at_line,
188 &sci_is_marker_set_at_line,
189 &sci_goto_line,
190 &sci_find_text,
191 &sci_set_line_indentation,
192 &sci_get_line_indentation,
193 &sci_get_lexer
196 static TemplateFuncs template_funcs = {
197 &templates_get_template_fileheader
200 static UtilsFuncs utils_funcs = {
201 &utils_str_equal,
202 &utils_string_replace_all,
203 &utils_get_file_list,
204 &utils_write_file,
205 &utils_get_locale_from_utf8,
206 &utils_get_utf8_from_locale,
207 &utils_remove_ext_from_filename,
208 &utils_mkdir,
209 &utils_get_setting_boolean,
210 &utils_get_setting_integer,
211 &utils_get_setting_string,
212 &utils_spawn_sync,
213 &utils_spawn_async,
214 &utils_str_casecmp,
215 &utils_get_date_time,
216 &utils_open_browser,
217 &utils_string_replace_first,
218 &utils_str_middle_truncate,
219 &utils_str_remove_chars,
220 &utils_get_file_list_full,
221 &utils_copy_environment,
222 &utils_find_open_xml_tag
225 static UIUtilsFuncs uiutils_funcs = {
226 &ui_dialog_vbox_new,
227 &ui_frame_new_with_alignment,
228 &ui_set_statusbar,
229 &ui_table_add_row,
230 &ui_path_box_new,
231 &ui_button_new_with_image,
232 &ui_add_document_sensitive,
233 &ui_widget_set_tooltip_text,
234 &ui_image_menu_item_new,
235 &ui_lookup_widget,
236 &ui_progress_bar_start,
237 &ui_progress_bar_stop,
238 &ui_entry_add_clear_icon,
239 &ui_menu_add_document_items,
240 &ui_widget_modify_font_from_string,
241 &ui_is_keyval_enter_or_return,
242 &ui_get_gtk_settings_integer,
243 &ui_combo_box_add_to_history
246 static DialogFuncs dialog_funcs = {
247 &dialogs_show_question,
248 &dialogs_show_msgbox,
249 &dialogs_show_save_as,
250 &dialogs_show_input_numeric
253 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
254 #define _lookup_widget_macro ui_lookup_widget
256 /* deprecated */
257 static SupportFuncs support_funcs = {
258 &_lookup_widget_macro
261 static MsgWinFuncs msgwin_funcs = {
262 &msgwin_status_add,
263 &msgwin_compiler_add,
264 &msgwin_msg_add,
265 &msgwin_clear_tab,
266 &msgwin_switch_tab,
267 &msgwin_set_messages_dir
270 static EncodingFuncs encoding_funcs = {
271 &encodings_convert_to_utf8,
272 &encodings_convert_to_utf8_from_charset,
273 &encodings_get_charset_from_index
276 static KeybindingFuncs keybindings_funcs = {
277 &keybindings_send_command,
278 &keybindings_set_item,
279 &keybindings_get_item
282 static TagManagerFuncs tagmanager_funcs = {
283 &tm_get_real_path,
284 &tm_source_file_new,
285 &tm_workspace_add_object,
286 &tm_source_file_update,
287 &tm_work_object_free,
288 &tm_workspace_remove_object
291 static SearchFuncs search_funcs = {
292 &search_show_find_in_files_dialog
295 static HighlightingFuncs highlighting_funcs = {
296 &highlighting_get_style,
297 &highlighting_set_styles,
298 &highlighting_is_string_style,
299 &highlighting_is_comment_style,
300 &highlighting_is_code_style
303 static FiletypeFuncs filetype_funcs = {
304 &filetypes_detect_from_file,
305 &filetypes_lookup_by_name,
306 &filetypes_index
309 static NavQueueFuncs navqueue_funcs = {
310 &navqueue_goto_line
313 static MainFuncs main_funcs = {
314 &main_reload_configuration,
315 &main_locale_init,
316 &main_is_realized
319 static StashFuncs stash_funcs = {
320 &stash_group_new,
321 &stash_group_add_boolean,
322 &stash_group_add_integer,
323 &stash_group_add_string,
324 &stash_group_add_string_vector,
325 &stash_group_load_from_key_file,
326 &stash_group_save_to_key_file,
327 &stash_group_free,
328 &stash_group_load_from_file,
329 &stash_group_save_to_file,
330 &stash_group_add_toggle_button,
331 &stash_group_add_radio_buttons,
332 &stash_group_add_spin_button_integer,
333 &stash_group_add_combo_box,
334 &stash_group_add_combo_box_entry,
335 &stash_group_add_entry,
336 &stash_group_add_widget_property,
337 &stash_group_display,
338 &stash_group_update
341 static SymbolsFuncs symbols_funcs = {
342 &symbols_get_context_separator
345 static GeanyFunctions geany_functions = {
346 &doc_funcs,
347 &sci_funcs,
348 &template_funcs,
349 &utils_funcs,
350 &uiutils_funcs,
351 &support_funcs,
352 &dialog_funcs,
353 &msgwin_funcs,
354 &encoding_funcs,
355 &keybindings_funcs,
356 &tagmanager_funcs,
357 &search_funcs,
358 &highlighting_funcs,
359 &filetype_funcs,
360 &navqueue_funcs,
361 &editor_funcs,
362 &main_funcs,
363 &plugin_funcs,
364 &scintilla_funcs,
365 &msgwin_funcs,
366 &stash_funcs,
367 &symbols_funcs
370 static GeanyData geany_data;
373 static void
374 geany_data_init(void)
376 GeanyData gd = {
377 app,
378 &main_widgets,
379 documents_array,
380 filetypes_array,
381 &prefs,
382 &interface_prefs,
383 &toolbar_prefs,
384 &editor_prefs,
385 &file_prefs,
386 &search_prefs,
387 &tool_prefs,
388 &template_prefs,
389 &build_info,
390 filetypes_by_title
393 geany_data = gd;
397 /* Prevent the same plugin filename being loaded more than once.
398 * Note: g_module_name always returns the .so name, even when Plugin::filename is an .la file. */
399 static gboolean
400 plugin_loaded(GModule *module)
402 gchar *basename_module, *basename_loaded;
403 GList *item;
405 basename_module = g_path_get_basename(g_module_name(module));
406 for (item = plugin_list; item != NULL; item = g_list_next(item))
408 basename_loaded = g_path_get_basename(
409 g_module_name(((Plugin*)item->data)->module));
411 if (utils_str_equal(basename_module, basename_loaded))
413 g_free(basename_loaded);
414 g_free(basename_module);
415 return TRUE;
417 g_free(basename_loaded);
419 /* Look also through the list of active plugins. This prevents problems when we have the same
420 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
421 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
422 * the plugin manager would list both plugins. Additionally, unloading the active plugin
423 * would cause a crash. */
424 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
426 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
428 if (utils_str_equal(basename_module, basename_loaded))
430 g_free(basename_loaded);
431 g_free(basename_module);
432 return TRUE;
434 g_free(basename_loaded);
436 g_free(basename_module);
437 return FALSE;
441 static Plugin *find_active_plugin_by_name(const gchar *filename)
443 GList *item;
445 g_return_val_if_fail(filename, FALSE);
447 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
449 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
450 return item->data;
453 return NULL;
457 static gboolean
458 plugin_check_version(GModule *module)
460 gint (*version_check)(gint) = NULL;
462 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
464 if (G_UNLIKELY(! version_check))
466 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
467 g_module_name(module));
468 return FALSE;
470 else
472 gint result = version_check(GEANY_ABI_VERSION);
474 if (result < 0)
476 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
477 "release of Geany - please recompile it."), g_module_name(module));
478 geany_debug("Plugin \"%s\" is not binary compatible with this "
479 "release of Geany - recompile it.", g_module_name(module));
480 return FALSE;
482 if (result > GEANY_API_VERSION)
484 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
485 g_module_name(module), result);
486 return FALSE;
489 return TRUE;
493 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
495 PluginCallback *cb;
496 guint i, len = 0;
498 while (TRUE)
500 cb = &callbacks[len];
501 if (!cb->signal_name || !cb->callback)
502 break;
503 len++;
505 if (len == 0)
506 return;
508 for (i = 0; i < len; i++)
510 cb = &callbacks[i];
512 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
513 cb->callback, cb->user_data);
518 static void read_key_group(Plugin *plugin)
520 GeanyKeyGroupInfo *p_key_info;
521 GeanyKeyGroup **p_key_group;
523 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
524 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
525 if (p_key_info && p_key_group)
527 GeanyKeyGroupInfo *key_info = p_key_info;
529 if (*p_key_group)
530 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
531 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
532 plugin->info.name);
533 else
535 if (key_info->count)
537 GeanyKeyGroup *key_group =
538 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
539 if (key_group)
540 *p_key_group = key_group;
542 else
543 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
544 "count field is zero. Maybe use plugin_set_key_group() instead?",
545 plugin->info.name);
548 else if (p_key_info || p_key_group)
549 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
550 "Maybe use plugin_set_key_group() instead?",
551 plugin->info.name);
555 static void
556 plugin_init(Plugin *plugin)
558 GeanyPlugin **p_geany_plugin;
559 PluginCallback *callbacks;
560 PluginInfo **p_info;
561 PluginFields **plugin_fields;
563 /* set these symbols before plugin_init() is called
564 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
565 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
566 if (p_geany_plugin)
567 *p_geany_plugin = &plugin->public;
568 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
569 if (p_info)
570 *p_info = &plugin->info;
571 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
572 if (plugin_fields)
573 *plugin_fields = &plugin->fields;
574 read_key_group(plugin);
576 /* start the plugin */
577 g_return_if_fail(plugin->init);
578 plugin->init(&geany_data);
580 /* store some function pointers for later use */
581 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
582 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
583 if (app->debug_mode && plugin->configure && plugin->configure_single)
584 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
585 "only plugin_configure() will be used!",
586 plugin->info.name);
588 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
589 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
590 if (plugin->cleanup == NULL)
592 if (app->debug_mode)
593 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
594 plugin->info.name);
597 /* now read any plugin-owned data that might have been set in plugin_init() */
599 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
601 ui_add_document_sensitive(plugin->fields.menu_item);
604 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
605 if (callbacks)
606 add_callbacks(plugin, callbacks);
608 /* remember which plugins are active */
609 active_plugin_list = g_list_append(active_plugin_list, plugin);
611 geany_debug("Loaded: %s (%s)", plugin->filename,
612 NVL(plugin->info.name, "<Unknown>"));
616 /* Load and optionally init a plugin.
617 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
618 * called, the plugin will be started, if not the plugin will be read only (for the list of
619 * available plugins in the plugin manager).
620 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
621 static Plugin*
622 plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
624 Plugin *plugin;
625 GModule *module;
626 GeanyData **p_geany_data;
627 GeanyFunctions **p_geany_functions;
628 void (*plugin_set_info)(PluginInfo*);
630 g_return_val_if_fail(fname, NULL);
631 g_return_val_if_fail(g_module_supported(), NULL);
633 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
634 * load the module */
635 plugin = find_active_plugin_by_name(fname);
636 if (plugin != NULL)
638 geany_debug("Plugin \"%s\" already loaded.", fname);
639 if (add_to_list)
640 plugin_list = g_list_append(plugin_list, plugin);
641 return plugin;
644 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
645 * causing a segfault. Without that flag the module will safely fail to load.
646 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
647 * otherwise not be detected due to the shadowing of Geany's app variable.
648 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
649 * function from a plugin will be shadowed. */
650 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
651 if (! module)
653 geany_debug("Can't load plugin: %s", g_module_error());
654 return NULL;
657 if (plugin_loaded(module))
659 geany_debug("Plugin \"%s\" already loaded.", fname);
661 if (! g_module_close(module))
662 g_warning("%s: %s", fname, g_module_error());
663 return NULL;
666 if (! plugin_check_version(module))
668 if (! g_module_close(module))
669 g_warning("%s: %s", fname, g_module_error());
670 return NULL;
673 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
674 if (plugin_set_info == NULL)
676 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
678 if (! g_module_close(module))
679 g_warning("%s: %s", fname, g_module_error());
680 return NULL;
683 plugin = g_new0(Plugin, 1);
685 /* set basic fields here to allow plugins to call Geany functions in set_info() */
686 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
687 if (p_geany_data)
688 *p_geany_data = &geany_data;
689 g_module_symbol(module, "geany_functions", (void *) &p_geany_functions);
690 if (p_geany_functions)
691 *p_geany_functions = &geany_functions;
693 /* read plugin name, etc. */
694 plugin_set_info(&plugin->info);
695 if (!NZV(plugin->info.name))
697 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
698 fname);
700 if (! g_module_close(module))
701 g_warning("%s: %s", fname, g_module_error());
702 g_free(plugin);
703 return NULL;
706 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
707 if (plugin->init == NULL)
709 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
710 plugin->info.name);
712 if (! g_module_close(module))
713 g_warning("%s: %s", fname, g_module_error());
714 g_free(plugin);
715 return NULL;
717 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
719 plugin->filename = g_strdup(fname);
720 plugin->module = module;
721 plugin->public.info = &plugin->info;
722 plugin->public.priv = plugin;
724 if (init_plugin)
725 plugin_init(plugin);
727 if (add_to_list)
728 plugin_list = g_list_append(plugin_list, plugin);
730 return plugin;
734 static void remove_callbacks(Plugin *plugin)
736 GArray *signal_ids = plugin->signal_ids;
737 SignalConnection *sc;
739 if (signal_ids == NULL)
740 return;
742 foreach_array(SignalConnection, sc, signal_ids)
743 g_signal_handler_disconnect(sc->object, sc->handler_id);
745 g_array_free(signal_ids, TRUE);
749 static gboolean is_active_plugin(Plugin *plugin)
751 return (g_list_find(active_plugin_list, plugin) != NULL);
755 /* Clean up anything used by an active plugin */
756 static void
757 plugin_cleanup(Plugin *plugin)
759 GtkWidget *widget;
761 if (plugin->cleanup)
762 plugin->cleanup();
764 remove_callbacks(plugin);
766 if (plugin->key_group)
767 keybindings_free_group(plugin->key_group);
769 widget = plugin->toolbar_separator.widget;
770 if (widget)
771 gtk_widget_destroy(widget);
773 geany_debug("Unloaded: %s", plugin->filename);
777 static void
778 plugin_free(Plugin *plugin)
780 g_return_if_fail(plugin);
781 g_return_if_fail(plugin->module);
783 if (is_active_plugin(plugin))
784 plugin_cleanup(plugin);
786 active_plugin_list = g_list_remove(active_plugin_list, plugin);
788 if (! g_module_close(plugin->module))
789 g_warning("%s: %s", plugin->filename, g_module_error());
791 plugin_list = g_list_remove(plugin_list, plugin);
793 g_free(plugin->filename);
794 g_free(plugin);
795 plugin = NULL;
799 /* load active plugins at startup */
800 static void
801 load_active_plugins(void)
803 guint i, len;
805 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
806 return;
808 for (i = 0; i < len; i++)
810 const gchar *fname = active_plugins_pref[i];
812 if (NZV(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
814 if (plugin_new(fname, TRUE, FALSE) == NULL)
815 failed_plugins_list = g_list_append(failed_plugins_list, g_strdup(fname));
821 static void
822 load_plugins_from_path(const gchar *path)
824 GSList *list, *item;
825 gchar *fname, *tmp;
827 list = utils_get_file_list(path, NULL, NULL);
829 for (item = list; item != NULL; item = g_slist_next(item))
831 tmp = strrchr(item->data, '.');
832 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
833 continue;
835 fname = g_strconcat(path, G_DIR_SEPARATOR_S, item->data, NULL);
836 plugin_new(fname, FALSE, TRUE);
837 g_free(fname);
840 g_slist_foreach(list, (GFunc) g_free, NULL);
841 g_slist_free(list);
845 #ifdef G_OS_WIN32
846 static gchar *get_plugin_path()
848 gchar *install_dir = win32_get_installation_dir();
849 gchar *path;
851 path = g_strconcat(install_dir, "\\lib", NULL);
852 g_free(install_dir);
854 return path;
856 #endif
859 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
860 static void load_all_plugins(void)
862 gchar *path;
864 path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", NULL);
865 /* first load plugins in ~/.config/geany/plugins/ */
866 load_plugins_from_path(path);
867 g_free(path);
869 /* load plugins from a custom path */
870 if (NZV(prefs.custom_plugin_path))
871 load_plugins_from_path(prefs.custom_plugin_path);
873 /* finally load plugins from $prefix/lib/geany */
874 #ifdef G_OS_WIN32
875 path = get_plugin_path();
876 #else
877 path = g_strconcat(GEANY_LIBDIR, G_DIR_SEPARATOR_S "geany", NULL);
878 #endif
879 load_plugins_from_path(path);
880 g_free(path);
884 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
886 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
887 guint i = 0;
888 gboolean have_plugin_menu_items = FALSE;
890 for (item = list; item != NULL; item = g_list_next(item))
892 if (item->data == menu_separator)
894 if (i < g_list_length(list) - 1)
896 have_plugin_menu_items = TRUE;
897 break;
900 i++;
902 g_list_free(list);
904 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
908 /* Calling this starts up plugin support */
909 void plugins_load_active(void)
911 GtkWidget *widget;
913 want_plugins = TRUE;
915 geany_data_init();
917 widget = gtk_separator_menu_item_new();
918 gtk_widget_show(widget);
919 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
921 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
922 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
923 gtk_widget_show(widget);
924 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
926 menu_separator = gtk_separator_menu_item_new();
927 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
928 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
930 load_active_plugins();
934 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
936 const Plugin *pa = a;
937 const Plugin *pb = b;
939 return strcmp(pa->info.name, pb->info.name);
943 static void update_active_plugins_pref(void)
945 gint i = 0;
946 GList *list;
947 gsize count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
949 g_strfreev(active_plugins_pref);
951 if (count == 0)
953 active_plugins_pref = NULL;
954 return;
957 /* sort the list so next time tools menu items are sorted by plugin name
958 * (not ideal to do here, but better than nothing) */
959 active_plugin_list = g_list_sort(active_plugin_list, cmp_plugin_names);
961 active_plugins_pref = g_new0(gchar*, count + 1);
963 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
965 Plugin *plugin = list->data;
967 active_plugins_pref[i] = g_strdup(plugin->filename);
968 i++;
970 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
972 const gchar *fname = list->data;
974 active_plugins_pref[i] = g_strdup(fname);
975 i++;
977 active_plugins_pref[i] = NULL;
981 static void on_save_settings(GKeyFile *config)
983 /* if plugins are disabled, don't clear list of active plugins */
984 if (want_plugins)
985 update_active_plugins_pref();
989 /* called even if plugin support is disabled */
990 void plugins_init(void)
992 StashGroup *group;
994 group = stash_group_new("plugins");
995 configuration_add_pref_group(group, TRUE);
997 stash_group_add_toggle_button(group, &prefs.load_plugins,
998 "load_plugins", TRUE, "check_plugins");
999 stash_group_add_entry(group, &prefs.custom_plugin_path,
1000 "custom_plugin_path", "", "extra_plugin_path_entry");
1002 g_signal_connect(geany_object, "save-settings", G_CALLBACK(on_save_settings), NULL);
1003 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1007 /* called even if plugin support is disabled */
1008 void plugins_finalize(void)
1010 if (failed_plugins_list != NULL)
1012 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1013 g_list_free(failed_plugins_list);
1015 if (active_plugin_list != NULL)
1017 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1018 g_list_free(active_plugin_list);
1020 g_strfreev(active_plugins_pref);
1024 /* Check whether there are any plugins loaded which provide a configure symbol */
1025 gboolean plugins_have_preferences(void)
1027 GList *item;
1029 if (active_plugin_list == NULL)
1030 return FALSE;
1032 foreach_list(item, active_plugin_list)
1034 Plugin *plugin = item->data;
1035 if (plugin->configure != NULL || plugin->configure_single != NULL)
1036 return TRUE;
1039 return FALSE;
1043 /* Plugin Manager */
1045 enum
1047 PLUGIN_COLUMN_CHECK = 0,
1048 PLUGIN_COLUMN_NAME,
1049 PLUGIN_COLUMN_FILE,
1050 PLUGIN_COLUMN_PLUGIN,
1051 PLUGIN_N_COLUMNS,
1052 PM_BUTTON_CONFIGURE,
1053 PM_BUTTON_HELP
1056 typedef struct
1058 GtkWidget *dialog;
1059 GtkWidget *tree;
1060 GtkListStore *store;
1061 GtkWidget *description_label;
1062 GtkWidget *configure_button;
1063 GtkWidget *help_button;
1065 PluginManagerWidgets;
1067 static PluginManagerWidgets pm_widgets;
1070 static void pm_update_buttons(Plugin *p)
1072 gboolean is_active;
1074 is_active = is_active_plugin(p);
1075 gtk_widget_set_sensitive(pm_widgets.configure_button,
1076 (p->configure || p->configure_single) && is_active);
1077 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
1081 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1083 GtkTreeIter iter;
1084 GtkTreeModel *model;
1085 Plugin *p;
1087 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1089 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1091 if (p != NULL)
1093 gchar *text;
1094 PluginInfo *pi;
1096 pi = &p->info;
1097 text = g_strdup_printf(
1098 _("Plugin: %s %s\nDescription: %s\nAuthor(s): %s"),
1099 pi->name, pi->version, pi->description, pi->author);
1101 geany_wrap_label_set_text(GTK_LABEL(pm_widgets.description_label), text);
1102 g_free(text);
1104 pm_update_buttons(p);
1110 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1112 gboolean old_state, state;
1113 gchar *file_name;
1114 GtkTreeIter iter;
1115 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1116 Plugin *p;
1118 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
1119 gtk_tree_path_free(path);
1121 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
1122 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
1124 /* no plugins item */
1125 if (p == NULL)
1126 return;
1128 state = ! old_state; /* toggle the state */
1130 /* save the filename of the plugin */
1131 file_name = g_strdup(p->filename);
1133 /* unload plugin module */
1134 if (!state)
1135 /* save shortcuts (only need this group, but it doesn't take long) */
1136 keybindings_write_to_file();
1138 plugin_free(p);
1140 /* reload plugin module and initialize it if item is checked */
1141 p = plugin_new(file_name, state, TRUE);
1142 if (!p)
1144 /* plugin file may no longer be on disk, or is now incompatible */
1145 gtk_list_store_remove(pm_widgets.store, &iter);
1147 else
1149 if (state)
1150 keybindings_load_keyfile(); /* load shortcuts */
1152 /* update model */
1153 gtk_list_store_set(pm_widgets.store, &iter,
1154 PLUGIN_COLUMN_CHECK, state,
1155 PLUGIN_COLUMN_PLUGIN, p, -1);
1157 /* set again the sensitiveness of the configure and help buttons */
1158 pm_update_buttons(p);
1160 g_free(file_name);
1164 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1166 GtkCellRenderer *text_renderer, *checkbox_renderer;
1167 GtkTreeViewColumn *column;
1168 GtkTreeIter iter;
1169 GList *list;
1170 GtkTreeSelection *sel;
1172 checkbox_renderer = gtk_cell_renderer_toggle_new();
1173 column = gtk_tree_view_column_new_with_attributes(
1174 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1175 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1176 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1178 text_renderer = gtk_cell_renderer_text_new();
1179 column = gtk_tree_view_column_new_with_attributes(
1180 _("Plugin"), text_renderer, "text", PLUGIN_COLUMN_NAME, NULL);
1181 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1183 text_renderer = gtk_cell_renderer_text_new();
1184 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1185 column = gtk_tree_view_column_new_with_attributes(
1186 _("File"), text_renderer, "text", PLUGIN_COLUMN_FILE, NULL);
1187 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1189 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1190 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1191 gtk_tree_sortable_set_sort_column_id(
1192 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_NAME, GTK_SORT_ASCENDING);
1194 /* selection handling */
1195 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1196 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1197 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1199 list = g_list_first(plugin_list);
1200 if (list == NULL)
1202 gtk_list_store_append(store, &iter);
1203 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1204 PLUGIN_COLUMN_NAME, _("No plugins available."),
1205 PLUGIN_COLUMN_FILE, "", PLUGIN_COLUMN_PLUGIN, NULL, -1);
1207 else
1209 Plugin *p;
1210 for (; list != NULL; list = list->next)
1212 p = list->data;
1214 gtk_list_store_append(store, &iter);
1215 gtk_list_store_set(store, &iter,
1216 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1217 PLUGIN_COLUMN_NAME, p->info.name,
1218 PLUGIN_COLUMN_FILE, p->filename,
1219 PLUGIN_COLUMN_PLUGIN, p,
1220 -1);
1223 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1224 g_object_unref(store);
1228 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1230 GtkTreeModel *model;
1231 GtkTreeSelection *selection;
1232 GtkTreeIter iter;
1233 Plugin *p;
1235 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1236 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1238 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1240 if (p != NULL)
1242 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1243 plugin_show_configure(&p->public);
1244 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1245 p->help();
1251 static void
1252 free_non_active_plugin(gpointer data, gpointer user_data)
1254 Plugin *plugin = data;
1256 /* don't do anything when closing the plugin manager and it is an active plugin */
1257 if (is_active_plugin(plugin))
1258 return;
1260 plugin_free(plugin);
1264 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1266 if (plugin_list != NULL)
1268 /* remove all non-active plugins from the list */
1269 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1270 g_list_free(plugin_list);
1271 plugin_list = NULL;
1273 gtk_widget_destroy(GTK_WIDGET(dialog));
1277 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1279 GtkWidget *vbox, *vbox2, *label_vbox, *hbox, *swin, *label, *label2, *desc_win;
1281 /* before showing the dialog, we need to create the list of available plugins */
1282 load_all_plugins();
1284 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1285 GTK_DIALOG_DESTROY_WITH_PARENT,
1286 GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
1287 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1288 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1289 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1291 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1293 pm_widgets.tree = gtk_tree_view_new();
1294 pm_widgets.store = gtk_list_store_new(
1295 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1296 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1298 swin = gtk_scrolled_window_new(NULL, NULL);
1299 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1300 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1301 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1302 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1304 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1306 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1307 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1308 g_signal_connect(pm_widgets.configure_button, "clicked",
1309 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1311 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1312 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1313 g_signal_connect(pm_widgets.help_button, "clicked",
1314 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1316 label2 = gtk_label_new(_("<b>Plugin details:</b>"));
1317 gtk_label_set_use_markup(GTK_LABEL(label2), TRUE);
1318 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
1320 pm_widgets.description_label = geany_wrap_label_new("");
1321 desc_win = gtk_scrolled_window_new(NULL, NULL);
1322 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win),
1323 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1324 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win),
1325 pm_widgets.description_label);
1327 hbox = gtk_hbox_new(FALSE, 0);
1328 gtk_box_pack_start(GTK_BOX(hbox), label2, TRUE, TRUE, 0);
1329 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 4);
1330 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1332 label_vbox = gtk_vbox_new(FALSE, 3);
1333 gtk_box_pack_start(GTK_BOX(label_vbox), hbox, FALSE, FALSE, 0);
1334 gtk_box_pack_start(GTK_BOX(label_vbox), desc_win, FALSE, FALSE, 0);
1336 vbox2 = gtk_vbox_new(FALSE, 3);
1337 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1338 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1339 gtk_box_pack_start(GTK_BOX(vbox2), label_vbox, FALSE, FALSE, 0);
1341 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1343 gtk_container_add(GTK_CONTAINER(vbox), vbox2);
1344 gtk_widget_show_all(pm_widgets.dialog);
1348 #endif