Updated Spanish translation
[anjuta.git] / plugins / indentation-python-style / plugin.c
blobdb69dd646877d31c88ad3d013d82c69891cd6772
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) Ishan Chattopadhyaya 2009 <ichattopadhyaya@gmail.com>
6 * plugin.c is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * plugin.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <config.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <libanjuta/anjuta-shell.h>
29 #include <libanjuta/anjuta-debug.h>
30 #include <libanjuta/interfaces/ianjuta-iterable.h>
31 #include <libanjuta/interfaces/ianjuta-document.h>
32 #include <libanjuta/interfaces/ianjuta-document-manager.h>
33 #include <libanjuta/interfaces/ianjuta-editor.h>
34 #include <libanjuta/interfaces/ianjuta-file.h>
35 #include <libanjuta/interfaces/ianjuta-editor-cell.h>
36 #include <libanjuta/interfaces/ianjuta-editor-language.h>
37 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
38 #include <libanjuta/interfaces/ianjuta-preferences.h>
39 #include <libanjuta/interfaces/ianjuta-language.h>
40 #include <libanjuta/interfaces/ianjuta-indenter.h>
42 #include "plugin.h"
43 #include "python-indentation.h"
45 /* Pixmaps */
46 #define ANJUTA_STOCK_AUTOINDENT "anjuta-indent"
48 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-indentation-python-style.xml"
49 #define PROPERTIES_FILE_UI PACKAGE_DATA_DIR"/glade/anjuta-indentation-python-style.ui"
50 #define ICON_FILE "anjuta-indentation-python-style-plugin.png"
52 /* Preferences keys */
53 #define ANJUTA_PREF_SCHEMA_PREFIX "org.gnome.anjuta."
54 #define PREF_SCHEMA "org.gnome.anjuta.plugins.indent-python"
56 static gpointer parent_class;
58 static void
59 on_editor_char_inserted_python (IAnjutaEditor *editor,
60 IAnjutaIterable *insert_pos,
61 gchar ch,
62 IndentPythonPlugin *plugin)
64 python_indent (plugin, editor, insert_pos, ch);
67 static void
68 install_support (IndentPythonPlugin *lang_plugin)
70 IAnjutaLanguage* lang_manager =
71 anjuta_shell_get_interface (ANJUTA_PLUGIN (lang_plugin)->shell,
72 IAnjutaLanguage, NULL);
74 if (!lang_manager)
75 return;
77 if (lang_plugin->support_installed)
78 return;
80 lang_plugin->current_language =
81 ianjuta_language_get_name_from_editor (lang_manager,
82 IANJUTA_EDITOR_LANGUAGE (lang_plugin->current_editor), NULL);
84 if (lang_plugin->current_language &&
85 (g_str_equal (lang_plugin->current_language, "Python")))
87 g_signal_connect (lang_plugin->current_editor,
88 "char-added",
89 G_CALLBACK (on_editor_char_inserted_python),
90 lang_plugin);
92 else
94 return;
97 python_indent_init (lang_plugin);
98 /* Disable editor intern auto-indent */
99 ianjuta_editor_set_auto_indent (IANJUTA_EDITOR(lang_plugin->current_editor),
100 FALSE, NULL);
102 lang_plugin->support_installed = TRUE;
105 static void
106 uninstall_support (IndentPythonPlugin *lang_plugin)
108 if (!lang_plugin->support_installed)
109 return;
111 if (lang_plugin->current_language &&
112 (g_str_equal (lang_plugin->current_language, "Python")))
114 g_signal_handlers_disconnect_by_func (lang_plugin->current_editor,
115 G_CALLBACK (on_editor_char_inserted_python),
116 lang_plugin);
119 lang_plugin->support_installed = FALSE;
122 static void
123 on_editor_language_changed (IAnjutaEditor *editor,
124 const gchar *new_language,
125 IndentPythonPlugin *plugin)
127 uninstall_support (plugin);
128 install_support (plugin);
131 static void
132 on_editor_added (AnjutaPlugin *plugin, const gchar *name,
133 const GValue *value, gpointer data)
135 IndentPythonPlugin *lang_plugin;
136 IAnjutaDocument* doc = IANJUTA_DOCUMENT(g_value_get_object (value));
137 lang_plugin = ANJUTA_PLUGIN_INDENT_PYTHON(plugin);
140 if (IANJUTA_IS_EDITOR(doc))
142 lang_plugin->current_editor = G_OBJECT(doc);
144 else
146 lang_plugin->current_editor = NULL;
147 return;
149 if (lang_plugin->current_editor)
151 IAnjutaEditor* editor = IANJUTA_EDITOR (lang_plugin->current_editor);
152 GFile* current_editor_file = ianjuta_file_get_file (IANJUTA_FILE (editor),
153 NULL);
155 if (current_editor_file)
157 lang_plugin->current_editor_filename = g_file_get_path (current_editor_file);
158 g_object_unref (current_editor_file);
161 install_support (lang_plugin);
162 g_signal_connect (lang_plugin->current_editor, "language-changed",
163 G_CALLBACK (on_editor_language_changed),
164 plugin);
168 static void
169 on_editor_removed (AnjutaPlugin *plugin, const gchar *name,
170 gpointer data)
172 IndentPythonPlugin *lang_plugin;
173 lang_plugin = ANJUTA_PLUGIN_INDENT_PYTHON (plugin);
175 if (lang_plugin->current_editor)
176 g_signal_handlers_disconnect_by_func (lang_plugin->current_editor,
177 G_CALLBACK (on_editor_language_changed),
178 plugin);
180 uninstall_support (lang_plugin);
183 g_free (lang_plugin->current_editor_filename);
184 lang_plugin->current_editor_filename = NULL;
185 lang_plugin->current_editor = NULL;
186 lang_plugin->current_language = NULL;
189 static void
190 on_auto_indent (GtkAction *action, gpointer data)
192 IndentPythonPlugin *lang_plugin = ANJUTA_PLUGIN_INDENT_PYTHON (data);
194 python_indent_auto (lang_plugin, NULL, NULL);
197 static GtkActionEntry actions[] = {
199 "ActionMenuEdit",
200 NULL, N_("_Edit"),
201 NULL, NULL, NULL
204 "ActionEditAutoindent",
205 GTK_STOCK_NEW, //ANJUTA_STOCK_AUTOINDENT,
206 N_("Auto-Indent"), "<control>i",
207 N_("Auto-indent current line or selection based on indentation settings"),
208 G_CALLBACK (on_auto_indent)
212 static gboolean
213 indent_python_plugin_activate (AnjutaPlugin *plugin)
215 AnjutaUI *ui;
217 IndentPythonPlugin *python_plugin;
218 python_plugin = (IndentPythonPlugin*) plugin;
220 python_plugin->prefs = anjuta_shell_get_preferences (plugin->shell, NULL);
222 /* Add all UI actions and merge UI */
223 ui = anjuta_shell_get_ui (plugin->shell, NULL);
225 python_plugin->action_group =
226 anjuta_ui_add_action_group_entries (ui, "ActionGroupPythonIndent",
227 _("Python Indentation"),
228 actions,
229 G_N_ELEMENTS (actions),
230 GETTEXT_PACKAGE, TRUE,
231 plugin);
232 python_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
234 /* Add watches */
235 python_plugin->editor_watch_id = anjuta_plugin_add_watch (plugin,
236 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
237 on_editor_added,
238 on_editor_removed,
239 NULL);
240 return TRUE;
243 static gboolean
244 indent_python_plugin_deactivate (AnjutaPlugin *plugin)
247 AnjutaUI *ui;
248 IndentPythonPlugin *lang_plugin;
249 lang_plugin = (IndentPythonPlugin*) (plugin);
251 anjuta_plugin_remove_watch (plugin,
252 lang_plugin->editor_watch_id,
253 TRUE);
255 ui = anjuta_shell_get_ui (plugin->shell, NULL);
256 anjuta_ui_remove_action_group (ui, ANJUTA_PLUGIN_INDENT_PYTHON(plugin)->action_group);
257 anjuta_ui_unmerge (ui, ANJUTA_PLUGIN_INDENT_PYTHON(plugin)->uiid);
259 return TRUE;
262 static void
263 indent_python_plugin_finalize (GObject *obj)
265 /* Finalization codes here */
266 G_OBJECT_CLASS (parent_class)->finalize (obj);
269 static void
270 indent_python_plugin_dispose (GObject *obj)
272 /* Disposition codes */
273 IndentPythonPlugin *plugin = (IndentPythonPlugin*)obj;
275 if (plugin->settings)
276 g_object_unref (plugin->settings);
277 plugin->settings = NULL;
279 G_OBJECT_CLASS (parent_class)->dispose (obj);
282 static void
283 indent_python_plugin_instance_init (GObject *obj)
285 IndentPythonPlugin *plugin = (IndentPythonPlugin*)obj;
286 plugin->action_group = NULL;
287 plugin->current_editor = NULL;
288 plugin->current_language = NULL;
289 plugin->editor_watch_id = 0;
290 plugin->uiid = 0;
291 plugin->settings = g_settings_new (PREF_SCHEMA);
294 static void
295 indent_python_plugin_class_init (GObjectClass *klass)
297 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
299 parent_class = g_type_class_peek_parent (klass);
301 plugin_class->activate = indent_python_plugin_activate;
302 plugin_class->deactivate = indent_python_plugin_deactivate;
303 klass->finalize = indent_python_plugin_finalize;
304 klass->dispose = indent_python_plugin_dispose;
308 static void
309 ipreferences_merge (IAnjutaPreferences* ipref, AnjutaPreferences* prefs,
310 GError** e)
312 GError* error = NULL;
313 IndentPythonPlugin* plugin = ANJUTA_PLUGIN_INDENT_PYTHON (ipref);
314 plugin->bxml = gtk_builder_new ();
316 /* Add preferences */
317 if (!gtk_builder_add_from_file (plugin->bxml, PROPERTIES_FILE_UI, &error))
319 g_warning ("Couldn't load builder file: %s", error->message);
320 g_error_free (error);
322 anjuta_preferences_add_from_builder (prefs,
323 plugin->bxml, plugin->settings,
324 "preferences", _("Indentation"),
325 ICON_FILE);
328 static void
329 ipreferences_unmerge (IAnjutaPreferences* ipref, AnjutaPreferences* prefs,
330 GError** e)
332 IndentPythonPlugin* plugin = ANJUTA_PLUGIN_INDENT_PYTHON (ipref);
333 anjuta_preferences_remove_page(prefs, _("Indentation"));
334 g_object_unref (plugin->bxml);
337 static void
338 ipreferences_iface_init (IAnjutaPreferencesIface* iface)
340 iface->merge = ipreferences_merge;
341 iface->unmerge = ipreferences_unmerge;
344 static void
345 iindenter_indent (IAnjutaIndenter* indenter,
346 IAnjutaIterable* start,
347 IAnjutaIterable* end,
348 GError** e)
350 IndentPythonPlugin* plugin = ANJUTA_PLUGIN_INDENT_PYTHON (indenter);
352 python_indent_auto (plugin,
353 start, end);
356 static void
357 iindenter_iface_init (IAnjutaIndenterIface* iface)
359 iface->indent = iindenter_indent;
362 ANJUTA_PLUGIN_BEGIN (IndentPythonPlugin, indent_python_plugin);
363 ANJUTA_PLUGIN_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
364 ANJUTA_PLUGIN_ADD_INTERFACE(iindenter, IANJUTA_TYPE_INDENTER);
365 ANJUTA_PLUGIN_END;
367 ANJUTA_SIMPLE_PLUGIN (IndentPythonPlugin, indent_python_plugin);