1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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)
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.
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>
43 #include "python-indentation.h"
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
;
59 on_editor_char_inserted_python (IAnjutaEditor
*editor
,
60 IAnjutaIterable
*insert_pos
,
62 IndentPythonPlugin
*plugin
)
64 python_indent (plugin
, editor
, insert_pos
, ch
);
68 install_support (IndentPythonPlugin
*lang_plugin
)
70 IAnjutaLanguage
* lang_manager
=
71 anjuta_shell_get_interface (ANJUTA_PLUGIN (lang_plugin
)->shell
,
72 IAnjutaLanguage
, NULL
);
77 if (lang_plugin
->support_installed
)
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
,
89 G_CALLBACK (on_editor_char_inserted_python
),
97 python_indent_init (lang_plugin
);
98 /* Disable editor intern auto-indent */
99 ianjuta_editor_set_auto_indent (IANJUTA_EDITOR(lang_plugin
->current_editor
),
102 lang_plugin
->support_installed
= TRUE
;
106 uninstall_support (IndentPythonPlugin
*lang_plugin
)
108 if (!lang_plugin
->support_installed
)
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
),
119 lang_plugin
->support_installed
= FALSE
;
123 on_editor_language_changed (IAnjutaEditor
*editor
,
124 const gchar
*new_language
,
125 IndentPythonPlugin
*plugin
)
127 uninstall_support (plugin
);
128 install_support (plugin
);
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
);
146 lang_plugin
->current_editor
= NULL
;
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
),
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
),
169 on_editor_removed (AnjutaPlugin
*plugin
, const gchar
*name
,
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
),
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
;
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
[] = {
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
)
213 indent_python_plugin_activate (AnjutaPlugin
*plugin
)
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"),
229 G_N_ELEMENTS (actions
),
230 GETTEXT_PACKAGE
, TRUE
,
232 python_plugin
->uiid
= anjuta_ui_merge (ui
, UI_FILE
);
235 python_plugin
->editor_watch_id
= anjuta_plugin_add_watch (plugin
,
236 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT
,
244 indent_python_plugin_deactivate (AnjutaPlugin
*plugin
)
248 IndentPythonPlugin
*lang_plugin
;
249 lang_plugin
= (IndentPythonPlugin
*) (plugin
);
251 anjuta_plugin_remove_watch (plugin
,
252 lang_plugin
->editor_watch_id
,
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
);
263 indent_python_plugin_finalize (GObject
*obj
)
265 /* Finalization codes here */
266 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
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
);
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;
291 plugin
->settings
= g_settings_new (PREF_SCHEMA
);
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
;
309 ipreferences_merge (IAnjutaPreferences
* ipref
, AnjutaPreferences
* prefs
,
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"),
329 ipreferences_unmerge (IAnjutaPreferences
* ipref
, AnjutaPreferences
* prefs
,
332 IndentPythonPlugin
* plugin
= ANJUTA_PLUGIN_INDENT_PYTHON (ipref
);
333 anjuta_preferences_remove_page(prefs
, _("Indentation"));
334 g_object_unref (plugin
->bxml
);
338 ipreferences_iface_init (IAnjutaPreferencesIface
* iface
)
340 iface
->merge
= ipreferences_merge
;
341 iface
->unmerge
= ipreferences_unmerge
;
345 iindenter_indent (IAnjutaIndenter
* indenter
,
346 IAnjutaIterable
* start
,
347 IAnjutaIterable
* end
,
350 IndentPythonPlugin
* plugin
= ANJUTA_PLUGIN_INDENT_PYTHON (indenter
);
352 python_indent_auto (plugin
,
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
);
367 ANJUTA_SIMPLE_PLUGIN (IndentPythonPlugin
, indent_python_plugin
);