Add sci_get_lexer() to plugin API.
[geany-mirror.git] / src / plugins.c
blobf2657b8f021685f222d2a1d849d2e0abc2a5aed4
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
130 static ScintillaFuncs scintilla_funcs = {
131 &scintilla_send_message,
132 &scintilla_new
135 /* Macro to prevent a duplicate macro being generated in geanyfunctions.h */
136 #define _scintilla_send_message_macro scintilla_send_message
138 static SciFuncs sci_funcs = {
139 &_scintilla_send_message_macro,
140 &sci_send_command,
141 &sci_start_undo_action,
142 &sci_end_undo_action,
143 &sci_set_text,
144 &sci_insert_text,
145 &sci_get_text,
146 &sci_get_length,
147 &sci_get_current_position,
148 &sci_set_current_position,
149 &sci_get_col_from_position,
150 &sci_get_line_from_position,
151 &sci_get_position_from_line,
152 &sci_replace_sel,
153 &sci_get_selected_text,
154 &sci_get_selected_text_length,
155 &sci_get_selection_start,
156 &sci_get_selection_end,
157 &sci_get_selection_mode,
158 &sci_set_selection_mode,
159 &sci_set_selection_start,
160 &sci_set_selection_end,
161 &sci_get_text_range,
162 &sci_get_line,
163 &sci_get_line_length,
164 &sci_get_line_count,
165 &sci_get_line_is_visible,
166 &sci_ensure_line_is_visible,
167 &sci_scroll_caret,
168 &sci_find_matching_brace,
169 &sci_get_style_at,
170 &sci_get_char_at,
171 &sci_get_current_line,
172 &sci_has_selection,
173 &sci_get_tab_width,
174 &sci_indicator_clear,
175 &sci_indicator_set,
176 &sci_get_contents,
177 &sci_get_contents_range,
178 &sci_get_selection_contents,
179 &sci_set_font,
180 &sci_get_line_end_position,
181 &sci_set_target_start,
182 &sci_set_target_end,
183 &sci_replace_target,
184 &sci_set_marker_at_line,
185 &sci_delete_marker_at_line,
186 &sci_is_marker_set_at_line,
187 &sci_goto_line,
188 &sci_find_text,
189 &sci_set_line_indentation,
190 &sci_get_line_indentation,
191 &sci_get_lexer
194 static TemplateFuncs template_funcs = {
195 &templates_get_template_fileheader
198 static UtilsFuncs utils_funcs = {
199 &utils_str_equal,
200 &utils_string_replace_all,
201 &utils_get_file_list,
202 &utils_write_file,
203 &utils_get_locale_from_utf8,
204 &utils_get_utf8_from_locale,
205 &utils_remove_ext_from_filename,
206 &utils_mkdir,
207 &utils_get_setting_boolean,
208 &utils_get_setting_integer,
209 &utils_get_setting_string,
210 &utils_spawn_sync,
211 &utils_spawn_async,
212 &utils_str_casecmp,
213 &utils_get_date_time,
214 &utils_open_browser,
215 &utils_string_replace_first,
216 &utils_str_middle_truncate,
217 &utils_str_remove_chars,
218 &utils_get_file_list_full,
219 &utils_copy_environment
222 static UIUtilsFuncs uiutils_funcs = {
223 &ui_dialog_vbox_new,
224 &ui_frame_new_with_alignment,
225 &ui_set_statusbar,
226 &ui_table_add_row,
227 &ui_path_box_new,
228 &ui_button_new_with_image,
229 &ui_add_document_sensitive,
230 &ui_widget_set_tooltip_text,
231 &ui_image_menu_item_new,
232 &ui_lookup_widget,
233 &ui_progress_bar_start,
234 &ui_progress_bar_stop,
235 &ui_entry_add_clear_icon,
236 &ui_menu_add_document_items,
237 &ui_widget_modify_font_from_string,
238 &ui_is_keyval_enter_or_return,
239 &ui_get_gtk_settings_integer,
240 &ui_combo_box_add_to_history
243 static DialogFuncs dialog_funcs = {
244 &dialogs_show_question,
245 &dialogs_show_msgbox,
246 &dialogs_show_save_as,
247 &dialogs_show_input_numeric
250 /* Macro to prevent confusing macro being generated in geanyfunctions.h */
251 #define _lookup_widget_macro ui_lookup_widget
253 /* deprecated */
254 static SupportFuncs support_funcs = {
255 &_lookup_widget_macro
258 static MsgWinFuncs msgwin_funcs = {
259 &msgwin_status_add,
260 &msgwin_compiler_add,
261 &msgwin_msg_add,
262 &msgwin_clear_tab,
263 &msgwin_switch_tab,
264 &msgwin_set_messages_dir
267 static EncodingFuncs encoding_funcs = {
268 &encodings_convert_to_utf8,
269 &encodings_convert_to_utf8_from_charset,
270 &encodings_get_charset_from_index
273 static KeybindingFuncs keybindings_funcs = {
274 &keybindings_send_command,
275 &keybindings_set_item,
276 &keybindings_get_item
279 static TagManagerFuncs tagmanager_funcs = {
280 &tm_get_real_path,
281 &tm_source_file_new,
282 &tm_workspace_add_object,
283 &tm_source_file_update,
284 &tm_work_object_free,
285 &tm_workspace_remove_object
288 static SearchFuncs search_funcs = {
289 &search_show_find_in_files_dialog
292 static HighlightingFuncs highlighting_funcs = {
293 &highlighting_get_style,
294 &highlighting_set_styles
297 static FiletypeFuncs filetype_funcs = {
298 &filetypes_detect_from_file,
299 &filetypes_lookup_by_name,
300 &filetypes_index
303 static NavQueueFuncs navqueue_funcs = {
304 &navqueue_goto_line
307 static MainFuncs main_funcs = {
308 &main_reload_configuration,
309 &main_locale_init,
310 &main_is_realized
313 static StashFuncs stash_funcs = {
314 &stash_group_new,
315 &stash_group_add_boolean,
316 &stash_group_add_integer,
317 &stash_group_add_string,
318 &stash_group_add_string_vector,
319 &stash_group_load_from_key_file,
320 &stash_group_save_to_key_file,
321 &stash_group_free,
322 &stash_group_load_from_file,
323 &stash_group_save_to_file,
324 &stash_group_add_toggle_button,
325 &stash_group_add_radio_buttons,
326 &stash_group_add_spin_button_integer,
327 &stash_group_add_combo_box,
328 &stash_group_add_combo_box_entry,
329 &stash_group_add_entry,
330 &stash_group_add_widget_property,
331 &stash_group_display,
332 &stash_group_update
335 static SymbolsFuncs symbols_funcs = {
336 &symbols_get_context_separator
339 static GeanyFunctions geany_functions = {
340 &doc_funcs,
341 &sci_funcs,
342 &template_funcs,
343 &utils_funcs,
344 &uiutils_funcs,
345 &support_funcs,
346 &dialog_funcs,
347 &msgwin_funcs,
348 &encoding_funcs,
349 &keybindings_funcs,
350 &tagmanager_funcs,
351 &search_funcs,
352 &highlighting_funcs,
353 &filetype_funcs,
354 &navqueue_funcs,
355 &editor_funcs,
356 &main_funcs,
357 &plugin_funcs,
358 &scintilla_funcs,
359 &msgwin_funcs,
360 &stash_funcs,
361 &symbols_funcs
364 static GeanyData geany_data;
367 static void
368 geany_data_init(void)
370 GeanyData gd = {
371 app,
372 &main_widgets,
373 documents_array,
374 filetypes_array,
375 &prefs,
376 &interface_prefs,
377 &toolbar_prefs,
378 &editor_prefs,
379 &file_prefs,
380 &search_prefs,
381 &tool_prefs,
382 &template_prefs,
383 &build_info,
384 filetypes_by_title
387 geany_data = gd;
391 /* Prevent the same plugin filename being loaded more than once.
392 * Note: g_module_name always returns the .so name, even when Plugin::filename is an .la file. */
393 static gboolean
394 plugin_loaded(GModule *module)
396 gchar *basename_module, *basename_loaded;
397 GList *item;
399 basename_module = g_path_get_basename(g_module_name(module));
400 for (item = plugin_list; item != NULL; item = g_list_next(item))
402 basename_loaded = g_path_get_basename(
403 g_module_name(((Plugin*)item->data)->module));
405 if (utils_str_equal(basename_module, basename_loaded))
407 g_free(basename_loaded);
408 g_free(basename_module);
409 return TRUE;
411 g_free(basename_loaded);
413 /* Look also through the list of active plugins. This prevents problems when we have the same
414 * plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
415 * as active plugin. The plugin manager list would only take the one in configdir/geany/ and
416 * the plugin manager would list both plugins. Additionally, unloading the active plugin
417 * would cause a crash. */
418 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
420 basename_loaded = g_path_get_basename(g_module_name(((Plugin*)item->data)->module));
422 if (utils_str_equal(basename_module, basename_loaded))
424 g_free(basename_loaded);
425 g_free(basename_module);
426 return TRUE;
428 g_free(basename_loaded);
430 g_free(basename_module);
431 return FALSE;
435 static Plugin *find_active_plugin_by_name(const gchar *filename)
437 GList *item;
439 g_return_val_if_fail(filename, FALSE);
441 for (item = active_plugin_list; item != NULL; item = g_list_next(item))
443 if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
444 return item->data;
447 return NULL;
451 static gboolean
452 plugin_check_version(GModule *module)
454 gint (*version_check)(gint) = NULL;
456 g_module_symbol(module, "plugin_version_check", (void *) &version_check);
458 if (G_UNLIKELY(! version_check))
460 geany_debug("Plugin \"%s\" has no plugin_version_check() function - ignoring plugin!",
461 g_module_name(module));
462 return FALSE;
464 else
466 gint result = version_check(GEANY_ABI_VERSION);
468 if (result < 0)
470 msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
471 "release of Geany - please recompile it."), g_module_name(module));
472 geany_debug("Plugin \"%s\" is not binary compatible with this "
473 "release of Geany - recompile it.", g_module_name(module));
474 return FALSE;
476 if (result > GEANY_API_VERSION)
478 geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
479 g_module_name(module), result);
480 return FALSE;
483 return TRUE;
487 static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
489 PluginCallback *cb;
490 guint i, len = 0;
492 while (TRUE)
494 cb = &callbacks[len];
495 if (!cb->signal_name || !cb->callback)
496 break;
497 len++;
499 if (len == 0)
500 return;
502 for (i = 0; i < len; i++)
504 cb = &callbacks[i];
506 plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
507 cb->callback, cb->user_data);
512 static void read_key_group(Plugin *plugin)
514 GeanyKeyGroupInfo *p_key_info;
515 GeanyKeyGroup **p_key_group;
517 g_module_symbol(plugin->module, "plugin_key_group_info", (void *) &p_key_info);
518 g_module_symbol(plugin->module, "plugin_key_group", (void *) &p_key_group);
519 if (p_key_info && p_key_group)
521 GeanyKeyGroupInfo *key_info = p_key_info;
523 if (*p_key_group)
524 geany_debug("Ignoring plugin_key_group symbol for plugin '%s' - "
525 "use plugin_set_key_group() instead to allocate keybindings dynamically.",
526 plugin->info.name);
527 else
529 if (key_info->count)
531 GeanyKeyGroup *key_group =
532 plugin_set_key_group(&plugin->public, key_info->name, key_info->count, NULL);
533 if (key_group)
534 *p_key_group = key_group;
536 else
537 geany_debug("Ignoring plugin_key_group_info symbol for plugin '%s' - "
538 "count field is zero. Maybe use plugin_set_key_group() instead?",
539 plugin->info.name);
542 else if (p_key_info || p_key_group)
543 geany_debug("Ignoring only one of plugin_key_group[_info] symbols defined for plugin '%s'. "
544 "Maybe use plugin_set_key_group() instead?",
545 plugin->info.name);
549 static void
550 plugin_init(Plugin *plugin)
552 GeanyPlugin **p_geany_plugin;
553 PluginCallback *callbacks;
554 PluginInfo **p_info;
555 PluginFields **plugin_fields;
557 /* set these symbols before plugin_init() is called
558 * we don't set geany_functions and geany_data since they are set directly by plugin_new() */
559 g_module_symbol(plugin->module, "geany_plugin", (void *) &p_geany_plugin);
560 if (p_geany_plugin)
561 *p_geany_plugin = &plugin->public;
562 g_module_symbol(plugin->module, "plugin_info", (void *) &p_info);
563 if (p_info)
564 *p_info = &plugin->info;
565 g_module_symbol(plugin->module, "plugin_fields", (void *) &plugin_fields);
566 if (plugin_fields)
567 *plugin_fields = &plugin->fields;
568 read_key_group(plugin);
570 /* start the plugin */
571 g_return_if_fail(plugin->init);
572 plugin->init(&geany_data);
574 /* store some function pointers for later use */
575 g_module_symbol(plugin->module, "plugin_configure", (void *) &plugin->configure);
576 g_module_symbol(plugin->module, "plugin_configure_single", (void *) &plugin->configure_single);
577 if (app->debug_mode && plugin->configure && plugin->configure_single)
578 g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
579 "only plugin_configure() will be used!",
580 plugin->info.name);
582 g_module_symbol(plugin->module, "plugin_help", (void *) &plugin->help);
583 g_module_symbol(plugin->module, "plugin_cleanup", (void *) &plugin->cleanup);
584 if (plugin->cleanup == NULL)
586 if (app->debug_mode)
587 g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
588 plugin->info.name);
591 /* now read any plugin-owned data that might have been set in plugin_init() */
593 if (plugin->fields.flags & PLUGIN_IS_DOCUMENT_SENSITIVE)
595 ui_add_document_sensitive(plugin->fields.menu_item);
598 g_module_symbol(plugin->module, "plugin_callbacks", (void *) &callbacks);
599 if (callbacks)
600 add_callbacks(plugin, callbacks);
602 /* remember which plugins are active */
603 active_plugin_list = g_list_append(active_plugin_list, plugin);
605 geany_debug("Loaded: %s (%s)", plugin->filename,
606 NVL(plugin->info.name, "<Unknown>"));
610 /* Load and optionally init a plugin.
611 * init_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
612 * called, the plugin will be started, if not the plugin will be read only (for the list of
613 * available plugins in the plugin manager).
614 * When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
615 static Plugin*
616 plugin_new(const gchar *fname, gboolean init_plugin, gboolean add_to_list)
618 Plugin *plugin;
619 GModule *module;
620 GeanyData **p_geany_data;
621 GeanyFunctions **p_geany_functions;
622 void (*plugin_set_info)(PluginInfo*);
624 g_return_val_if_fail(fname, NULL);
625 g_return_val_if_fail(g_module_supported(), NULL);
627 /* find the plugin in the list of already loaded, active plugins and use it, otherwise
628 * load the module */
629 plugin = find_active_plugin_by_name(fname);
630 if (plugin != NULL)
632 geany_debug("Plugin \"%s\" already loaded.", fname);
633 if (add_to_list)
634 plugin_list = g_list_append(plugin_list, plugin);
635 return plugin;
638 /* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
639 * causing a segfault. Without that flag the module will safely fail to load.
640 * G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
641 * otherwise not be detected due to the shadowing of Geany's app variable.
642 * Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
643 * function from a plugin will be shadowed. */
644 module = g_module_open(fname, G_MODULE_BIND_LOCAL);
645 if (! module)
647 geany_debug("Can't load plugin: %s", g_module_error());
648 return NULL;
651 if (plugin_loaded(module))
653 geany_debug("Plugin \"%s\" already loaded.", fname);
655 if (! g_module_close(module))
656 g_warning("%s: %s", fname, g_module_error());
657 return NULL;
660 if (! plugin_check_version(module))
662 if (! g_module_close(module))
663 g_warning("%s: %s", fname, g_module_error());
664 return NULL;
667 g_module_symbol(module, "plugin_set_info", (void *) &plugin_set_info);
668 if (plugin_set_info == NULL)
670 geany_debug("No plugin_set_info() defined for \"%s\" - ignoring plugin!", fname);
672 if (! g_module_close(module))
673 g_warning("%s: %s", fname, g_module_error());
674 return NULL;
677 plugin = g_new0(Plugin, 1);
679 /* set basic fields here to allow plugins to call Geany functions in set_info() */
680 g_module_symbol(module, "geany_data", (void *) &p_geany_data);
681 if (p_geany_data)
682 *p_geany_data = &geany_data;
683 g_module_symbol(module, "geany_functions", (void *) &p_geany_functions);
684 if (p_geany_functions)
685 *p_geany_functions = &geany_functions;
687 /* read plugin name, etc. */
688 plugin_set_info(&plugin->info);
689 if (!NZV(plugin->info.name))
691 geany_debug("No plugin name set in plugin_set_info() for \"%s\" - ignoring plugin!",
692 fname);
694 if (! g_module_close(module))
695 g_warning("%s: %s", fname, g_module_error());
696 g_free(plugin);
697 return NULL;
700 g_module_symbol(module, "plugin_init", (void *) &plugin->init);
701 if (plugin->init == NULL)
703 geany_debug("Plugin '%s' has no plugin_init() function - ignoring plugin!",
704 plugin->info.name);
706 if (! g_module_close(module))
707 g_warning("%s: %s", fname, g_module_error());
708 g_free(plugin);
709 return NULL;
711 /*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
713 plugin->filename = g_strdup(fname);
714 plugin->module = module;
715 plugin->public.info = &plugin->info;
716 plugin->public.priv = plugin;
718 if (init_plugin)
719 plugin_init(plugin);
721 if (add_to_list)
722 plugin_list = g_list_append(plugin_list, plugin);
724 return plugin;
728 static void remove_callbacks(Plugin *plugin)
730 GArray *signal_ids = plugin->signal_ids;
731 SignalConnection *sc;
733 if (signal_ids == NULL)
734 return;
736 foreach_array(SignalConnection, sc, signal_ids)
737 g_signal_handler_disconnect(sc->object, sc->handler_id);
739 g_array_free(signal_ids, TRUE);
743 static gboolean is_active_plugin(Plugin *plugin)
745 return (g_list_find(active_plugin_list, plugin) != NULL);
749 /* Clean up anything used by an active plugin */
750 static void
751 plugin_cleanup(Plugin *plugin)
753 GtkWidget *widget;
755 if (plugin->cleanup)
756 plugin->cleanup();
758 remove_callbacks(plugin);
760 if (plugin->key_group)
761 keybindings_free_group(plugin->key_group);
763 widget = plugin->toolbar_separator.widget;
764 if (widget)
765 gtk_widget_destroy(widget);
767 geany_debug("Unloaded: %s", plugin->filename);
771 static void
772 plugin_free(Plugin *plugin)
774 g_return_if_fail(plugin);
775 g_return_if_fail(plugin->module);
777 if (is_active_plugin(plugin))
778 plugin_cleanup(plugin);
780 active_plugin_list = g_list_remove(active_plugin_list, plugin);
782 if (! g_module_close(plugin->module))
783 g_warning("%s: %s", plugin->filename, g_module_error());
785 plugin_list = g_list_remove(plugin_list, plugin);
787 g_free(plugin->filename);
788 g_free(plugin);
789 plugin = NULL;
793 /* load active plugins at startup */
794 static void
795 load_active_plugins(void)
797 guint i, len;
799 if (active_plugins_pref == NULL || (len = g_strv_length(active_plugins_pref)) == 0)
800 return;
802 for (i = 0; i < len; i++)
804 const gchar *fname = active_plugins_pref[i];
806 if (NZV(fname) && g_file_test(fname, G_FILE_TEST_EXISTS))
808 if (plugin_new(fname, TRUE, FALSE) == NULL)
809 failed_plugins_list = g_list_append(failed_plugins_list, g_strdup(fname));
815 static void
816 load_plugins_from_path(const gchar *path)
818 GSList *list, *item;
819 gchar *fname, *tmp;
821 list = utils_get_file_list(path, NULL, NULL);
823 for (item = list; item != NULL; item = g_slist_next(item))
825 tmp = strrchr(item->data, '.');
826 if (tmp == NULL || utils_str_casecmp(tmp, "." G_MODULE_SUFFIX) != 0)
827 continue;
829 fname = g_strconcat(path, G_DIR_SEPARATOR_S, item->data, NULL);
830 plugin_new(fname, FALSE, TRUE);
831 g_free(fname);
834 g_slist_foreach(list, (GFunc) g_free, NULL);
835 g_slist_free(list);
839 #ifdef G_OS_WIN32
840 static gchar *get_plugin_path()
842 gchar *install_dir = win32_get_installation_dir();
843 gchar *path;
845 path = g_strconcat(install_dir, "\\lib", NULL);
846 g_free(install_dir);
848 return path;
850 #endif
853 /* Load (but don't initialize) all plugins for the Plugin Manager dialog */
854 static void load_all_plugins(void)
856 gchar *path;
858 path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", NULL);
859 /* first load plugins in ~/.config/geany/plugins/ */
860 load_plugins_from_path(path);
861 g_free(path);
863 /* load plugins from a custom path */
864 if (NZV(prefs.custom_plugin_path))
865 load_plugins_from_path(prefs.custom_plugin_path);
867 /* finally load plugins from $prefix/lib/geany */
868 #ifdef G_OS_WIN32
869 path = get_plugin_path();
870 #else
871 path = g_strconcat(GEANY_LIBDIR, G_DIR_SEPARATOR_S "geany", NULL);
872 #endif
873 load_plugins_from_path(path);
874 g_free(path);
878 static void on_tools_menu_show(GtkWidget *menu_item, G_GNUC_UNUSED gpointer user_data)
880 GList *item, *list = gtk_container_get_children(GTK_CONTAINER(menu_item));
881 guint i = 0;
882 gboolean have_plugin_menu_items = FALSE;
884 for (item = list; item != NULL; item = g_list_next(item))
886 if (item->data == menu_separator)
888 if (i < g_list_length(list) - 1)
890 have_plugin_menu_items = TRUE;
891 break;
894 i++;
896 g_list_free(list);
898 ui_widget_show_hide(menu_separator, have_plugin_menu_items);
902 /* Calling this starts up plugin support */
903 void plugins_load_active(void)
905 GtkWidget *widget;
907 want_plugins = TRUE;
909 geany_data_init();
911 widget = gtk_separator_menu_item_new();
912 gtk_widget_show(widget);
913 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
915 widget = gtk_menu_item_new_with_mnemonic(_("_Plugin Manager"));
916 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), widget);
917 gtk_widget_show(widget);
918 g_signal_connect(widget, "activate", G_CALLBACK(pm_show_dialog), NULL);
920 menu_separator = gtk_separator_menu_item_new();
921 gtk_container_add(GTK_CONTAINER(main_widgets.tools_menu), menu_separator);
922 g_signal_connect(main_widgets.tools_menu, "show", G_CALLBACK(on_tools_menu_show), NULL);
924 load_active_plugins();
928 static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
930 const Plugin *pa = a;
931 const Plugin *pb = b;
933 return strcmp(pa->info.name, pb->info.name);
937 static void update_active_plugins_pref(void)
939 gint i = 0;
940 GList *list;
941 gsize count = g_list_length(active_plugin_list) + g_list_length(failed_plugins_list);
943 g_strfreev(active_plugins_pref);
945 if (count == 0)
947 active_plugins_pref = NULL;
948 return;
951 /* sort the list so next time tools menu items are sorted by plugin name
952 * (not ideal to do here, but better than nothing) */
953 active_plugin_list = g_list_sort(active_plugin_list, cmp_plugin_names);
955 active_plugins_pref = g_new0(gchar*, count + 1);
957 for (list = g_list_first(active_plugin_list); list != NULL; list = list->next)
959 Plugin *plugin = list->data;
961 active_plugins_pref[i] = g_strdup(plugin->filename);
962 i++;
964 for (list = g_list_first(failed_plugins_list); list != NULL; list = list->next)
966 const gchar *fname = list->data;
968 active_plugins_pref[i] = g_strdup(fname);
969 i++;
971 active_plugins_pref[i] = NULL;
975 static void on_save_settings(GKeyFile *config)
977 /* if plugins are disabled, don't clear list of active plugins */
978 if (want_plugins)
979 update_active_plugins_pref();
983 /* called even if plugin support is disabled */
984 void plugins_init(void)
986 StashGroup *group;
988 group = stash_group_new("plugins");
989 configuration_add_pref_group(group, TRUE);
991 stash_group_add_toggle_button(group, &prefs.load_plugins,
992 "load_plugins", TRUE, "check_plugins");
993 stash_group_add_entry(group, &prefs.custom_plugin_path,
994 "custom_plugin_path", "", "extra_plugin_path_entry");
996 g_signal_connect(geany_object, "save-settings", G_CALLBACK(on_save_settings), NULL);
997 stash_group_add_string_vector(group, &active_plugins_pref, "active_plugins", NULL);
1001 /* called even if plugin support is disabled */
1002 void plugins_finalize(void)
1004 if (failed_plugins_list != NULL)
1006 g_list_foreach(failed_plugins_list, (GFunc) g_free, NULL);
1007 g_list_free(failed_plugins_list);
1009 if (active_plugin_list != NULL)
1011 g_list_foreach(active_plugin_list, (GFunc) plugin_free, NULL);
1012 g_list_free(active_plugin_list);
1014 g_strfreev(active_plugins_pref);
1018 /* Check whether there are any plugins loaded which provide a configure symbol */
1019 gboolean plugins_have_preferences(void)
1021 GList *item;
1023 if (active_plugin_list == NULL)
1024 return FALSE;
1026 foreach_list(item, active_plugin_list)
1028 Plugin *plugin = item->data;
1029 if (plugin->configure != NULL || plugin->configure_single != NULL)
1030 return TRUE;
1033 return FALSE;
1037 /* Plugin Manager */
1039 enum
1041 PLUGIN_COLUMN_CHECK = 0,
1042 PLUGIN_COLUMN_NAME,
1043 PLUGIN_COLUMN_FILE,
1044 PLUGIN_COLUMN_PLUGIN,
1045 PLUGIN_N_COLUMNS,
1046 PM_BUTTON_CONFIGURE,
1047 PM_BUTTON_HELP
1050 typedef struct
1052 GtkWidget *dialog;
1053 GtkWidget *tree;
1054 GtkListStore *store;
1055 GtkWidget *description_label;
1056 GtkWidget *configure_button;
1057 GtkWidget *help_button;
1059 PluginManagerWidgets;
1061 static PluginManagerWidgets pm_widgets;
1064 static void pm_update_buttons(Plugin *p)
1066 gboolean is_active;
1068 is_active = is_active_plugin(p);
1069 gtk_widget_set_sensitive(pm_widgets.configure_button,
1070 (p->configure || p->configure_single) && is_active);
1071 gtk_widget_set_sensitive(pm_widgets.help_button, p->help != NULL && is_active);
1075 static void pm_selection_changed(GtkTreeSelection *selection, gpointer user_data)
1077 GtkTreeIter iter;
1078 GtkTreeModel *model;
1079 Plugin *p;
1081 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1083 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1085 if (p != NULL)
1087 gchar *text;
1088 PluginInfo *pi;
1090 pi = &p->info;
1091 text = g_strdup_printf(
1092 _("Plugin: %s %s\nDescription: %s\nAuthor(s): %s"),
1093 pi->name, pi->version, pi->description, pi->author);
1095 geany_wrap_label_set_text(GTK_LABEL(pm_widgets.description_label), text);
1096 g_free(text);
1098 pm_update_buttons(p);
1104 static void pm_plugin_toggled(GtkCellRendererToggle *cell, gchar *pth, gpointer data)
1106 gboolean old_state, state;
1107 gchar *file_name;
1108 GtkTreeIter iter;
1109 GtkTreePath *path = gtk_tree_path_new_from_string(pth);
1110 Plugin *p;
1112 gtk_tree_model_get_iter(GTK_TREE_MODEL(pm_widgets.store), &iter, path);
1113 gtk_tree_path_free(path);
1115 gtk_tree_model_get(GTK_TREE_MODEL(pm_widgets.store), &iter,
1116 PLUGIN_COLUMN_CHECK, &old_state, PLUGIN_COLUMN_PLUGIN, &p, -1);
1118 /* no plugins item */
1119 if (p == NULL)
1120 return;
1122 state = ! old_state; /* toggle the state */
1124 /* save the filename of the plugin */
1125 file_name = g_strdup(p->filename);
1127 /* unload plugin module */
1128 if (!state)
1129 /* save shortcuts (only need this group, but it doesn't take long) */
1130 keybindings_write_to_file();
1132 plugin_free(p);
1134 /* reload plugin module and initialize it if item is checked */
1135 p = plugin_new(file_name, state, TRUE);
1136 if (!p)
1138 /* plugin file may no longer be on disk, or is now incompatible */
1139 gtk_list_store_remove(pm_widgets.store, &iter);
1141 else
1143 if (state)
1144 keybindings_load_keyfile(); /* load shortcuts */
1146 /* update model */
1147 gtk_list_store_set(pm_widgets.store, &iter,
1148 PLUGIN_COLUMN_CHECK, state,
1149 PLUGIN_COLUMN_PLUGIN, p, -1);
1151 /* set again the sensitiveness of the configure and help buttons */
1152 pm_update_buttons(p);
1154 g_free(file_name);
1158 static void pm_prepare_treeview(GtkWidget *tree, GtkListStore *store)
1160 GtkCellRenderer *text_renderer, *checkbox_renderer;
1161 GtkTreeViewColumn *column;
1162 GtkTreeIter iter;
1163 GList *list;
1164 GtkTreeSelection *sel;
1166 checkbox_renderer = gtk_cell_renderer_toggle_new();
1167 column = gtk_tree_view_column_new_with_attributes(
1168 _("Active"), checkbox_renderer, "active", PLUGIN_COLUMN_CHECK, NULL);
1169 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1170 g_signal_connect(checkbox_renderer, "toggled", G_CALLBACK(pm_plugin_toggled), NULL);
1172 text_renderer = gtk_cell_renderer_text_new();
1173 column = gtk_tree_view_column_new_with_attributes(
1174 _("Plugin"), text_renderer, "text", PLUGIN_COLUMN_NAME, NULL);
1175 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1177 text_renderer = gtk_cell_renderer_text_new();
1178 g_object_set(text_renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1179 column = gtk_tree_view_column_new_with_attributes(
1180 _("File"), text_renderer, "text", PLUGIN_COLUMN_FILE, NULL);
1181 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1183 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1184 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree), FALSE);
1185 gtk_tree_sortable_set_sort_column_id(
1186 GTK_TREE_SORTABLE(store), PLUGIN_COLUMN_NAME, GTK_SORT_ASCENDING);
1188 /* selection handling */
1189 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1190 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
1191 g_signal_connect(sel, "changed", G_CALLBACK(pm_selection_changed), NULL);
1193 list = g_list_first(plugin_list);
1194 if (list == NULL)
1196 gtk_list_store_append(store, &iter);
1197 gtk_list_store_set(store, &iter, PLUGIN_COLUMN_CHECK, FALSE,
1198 PLUGIN_COLUMN_NAME, _("No plugins available."),
1199 PLUGIN_COLUMN_FILE, "", PLUGIN_COLUMN_PLUGIN, NULL, -1);
1201 else
1203 Plugin *p;
1204 for (; list != NULL; list = list->next)
1206 p = list->data;
1208 gtk_list_store_append(store, &iter);
1209 gtk_list_store_set(store, &iter,
1210 PLUGIN_COLUMN_CHECK, is_active_plugin(p),
1211 PLUGIN_COLUMN_NAME, p->info.name,
1212 PLUGIN_COLUMN_FILE, p->filename,
1213 PLUGIN_COLUMN_PLUGIN, p,
1214 -1);
1217 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
1218 g_object_unref(store);
1222 static void pm_on_plugin_button_clicked(GtkButton *button, gpointer user_data)
1224 GtkTreeModel *model;
1225 GtkTreeSelection *selection;
1226 GtkTreeIter iter;
1227 Plugin *p;
1229 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pm_widgets.tree));
1230 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1232 gtk_tree_model_get(model, &iter, PLUGIN_COLUMN_PLUGIN, &p, -1);
1234 if (p != NULL)
1236 if (GPOINTER_TO_INT(user_data) == PM_BUTTON_CONFIGURE)
1237 plugin_show_configure(&p->public);
1238 else if (GPOINTER_TO_INT(user_data) == PM_BUTTON_HELP && p->help != NULL)
1239 p->help();
1245 static void
1246 free_non_active_plugin(gpointer data, gpointer user_data)
1248 Plugin *plugin = data;
1250 /* don't do anything when closing the plugin manager and it is an active plugin */
1251 if (is_active_plugin(plugin))
1252 return;
1254 plugin_free(plugin);
1258 static void pm_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1260 if (plugin_list != NULL)
1262 /* remove all non-active plugins from the list */
1263 g_list_foreach(plugin_list, free_non_active_plugin, NULL);
1264 g_list_free(plugin_list);
1265 plugin_list = NULL;
1267 gtk_widget_destroy(GTK_WIDGET(dialog));
1271 static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data)
1273 GtkWidget *vbox, *vbox2, *label_vbox, *hbox, *swin, *label, *label2, *desc_win;
1275 /* before showing the dialog, we need to create the list of available plugins */
1276 load_all_plugins();
1278 pm_widgets.dialog = gtk_dialog_new_with_buttons(_("Plugins"), GTK_WINDOW(main_widgets.window),
1279 GTK_DIALOG_DESTROY_WITH_PARENT,
1280 GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
1281 vbox = ui_dialog_vbox_new(GTK_DIALOG(pm_widgets.dialog));
1282 gtk_widget_set_name(pm_widgets.dialog, "GeanyDialog");
1283 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1285 gtk_window_set_default_size(GTK_WINDOW(pm_widgets.dialog), 500, 450);
1287 pm_widgets.tree = gtk_tree_view_new();
1288 pm_widgets.store = gtk_list_store_new(
1289 PLUGIN_N_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1290 pm_prepare_treeview(pm_widgets.tree, pm_widgets.store);
1292 swin = gtk_scrolled_window_new(NULL, NULL);
1293 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1294 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1295 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1296 gtk_container_add(GTK_CONTAINER(swin), pm_widgets.tree);
1298 label = geany_wrap_label_new(_("Choose which plugins should be loaded at startup:"));
1300 pm_widgets.configure_button = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
1301 gtk_widget_set_sensitive(pm_widgets.configure_button, FALSE);
1302 g_signal_connect(pm_widgets.configure_button, "clicked",
1303 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_CONFIGURE));
1305 pm_widgets.help_button = gtk_button_new_from_stock(GTK_STOCK_HELP);
1306 gtk_widget_set_sensitive(pm_widgets.help_button, FALSE);
1307 g_signal_connect(pm_widgets.help_button, "clicked",
1308 G_CALLBACK(pm_on_plugin_button_clicked), GINT_TO_POINTER(PM_BUTTON_HELP));
1310 label2 = gtk_label_new(_("<b>Plugin details:</b>"));
1311 gtk_label_set_use_markup(GTK_LABEL(label2), TRUE);
1312 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
1314 pm_widgets.description_label = geany_wrap_label_new("");
1315 desc_win = gtk_scrolled_window_new(NULL, NULL);
1316 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(desc_win),
1317 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1318 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(desc_win),
1319 pm_widgets.description_label);
1321 hbox = gtk_hbox_new(FALSE, 0);
1322 gtk_box_pack_start(GTK_BOX(hbox), label2, TRUE, TRUE, 0);
1323 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.help_button, FALSE, FALSE, 4);
1324 gtk_box_pack_start(GTK_BOX(hbox), pm_widgets.configure_button, FALSE, FALSE, 0);
1326 label_vbox = gtk_vbox_new(FALSE, 3);
1327 gtk_box_pack_start(GTK_BOX(label_vbox), hbox, FALSE, FALSE, 0);
1328 gtk_box_pack_start(GTK_BOX(label_vbox), desc_win, FALSE, FALSE, 0);
1330 vbox2 = gtk_vbox_new(FALSE, 3);
1331 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 5);
1332 gtk_box_pack_start(GTK_BOX(vbox2), swin, TRUE, TRUE, 0);
1333 gtk_box_pack_start(GTK_BOX(vbox2), label_vbox, FALSE, FALSE, 0);
1335 g_signal_connect(pm_widgets.dialog, "response", G_CALLBACK(pm_dialog_response), NULL);
1337 gtk_container_add(GTK_CONTAINER(vbox), vbox2);
1338 gtk_widget_show_all(pm_widgets.dialog);
1342 #endif