updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / gedit-encoding-plugin / encodingpy.py
blob87d388c803a6190e56ad72061e707269a57793f5
1 # -*- coding: utf-8 -*-
2 # Open the document in a different encoding
3 # Dependence: python >=2.5, pygtk
4 #
5 # Install: copy encoding.gedit-plugin and encodingpy.py to ~/.gnome2/gedit/plugins/
6 #
7 # Copyright (C) 2008 Vladislav Gorbunov
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330,
22 # Boston, MA 02111-1307, USA.
24 # TODO:
25 # fix document_loaded: assertion `(tab->priv->state == GEDIT_TAB_STATE_LOADING) || (tab->priv->state == GEDIT_TAB_STATE_REVERTING)' failed
27 from gettext import gettext as _
29 from gi.repository import GObject, Gtk, Gio, Gedit
30 import functools
32 # All encodings names
33 enclist_func = lambda i=0: [Gedit.encoding_get_from_index(i)] + enclist_func(i+1) if Gedit.encoding_get_from_index(i) else []
35 shown_enc = Gio.Settings.new("org.gnome.gedit.preferences.encodings").get_strv("shown-in-menu")
36 # show the full list of encodings if not they not configured in the Open/Save Dialog
37 enclist = sorted(([Gedit.encoding_get_from_charset(encname) for encname in shown_enc]
38 if shown_enc else enclist_func())
39 + [Gedit.encoding_get_utf8()], key=lambda enc: enc.to_string())
41 ui_str = """<ui>
42 <menubar name="MenuBar">
43 <menu name="FileMenu" action="File">
44 <placeholder name="FileOps_2">
45 <menu name="FileEncodingMenu" action="FileEncoding">
46 <placeholder name="EncodingListHolder"/>
47 <separator/>
49 </menu>
50 </placeholder>
51 </menu>
52 </menubar>
53 </ui>
54 """ % "\n".join(["<menuitem name=\"Encoding%i\" action=\"Encoding%i\"/>" % (i, i) for i in range(len(enclist))])
56 class EncodingWindowHelper:
57 def __init__(self, plugin, window):
58 self._window = window
59 self._plugin = plugin
60 self._insert_menu()
62 def deactivate(self):
63 self._remove_menu()
64 self._window = None
65 self._plugin = None
66 self._action_group = None
68 def _insert_menu(self):
69 manager = self._window.get_ui_manager()
70 self._action_group = Gtk.ActionGroup("EncodingPyPluginActions")
71 self._action_group.add_actions([("FileEncoding", None, _("Encoding"))] + \
72 [("Encoding%i" % i, None, enclist[i].to_string(), None,
73 _("Reopen the document in")+" "+enclist[i].to_string(),
74 functools.partial(self.reopen_document, enc=enclist[i])) \
75 for i in range(len(enclist))])
76 manager.insert_action_group(self._action_group, -1)
77 self._ui_id = manager.add_ui_from_string(ui_str)
79 def _remove_menu(self):
80 manager = self._window.get_ui_manager()
81 manager.remove_ui(self._ui_id)
82 manager.remove_action_group(self._action_group)
83 manager.ensure_update()
85 def update_ui(self):
86 self._action_group.set_sensitive(self._window.get_active_document() != None)
88 def reopen_document(self, action, _dummy = None, enc = None):
89 doc = self._window.get_active_document()
90 if doc and doc.get_location():
91 line_pos = doc.get_iter_at_mark(doc.get_insert()).get_line()
92 location = doc.get_location()
93 doc.load(location, enc, line_pos+1, 1, False)
94 # Can fix the 'document_loaded: assertion' if replace doc.load() to this two lines:
95 #self._window.close_tab(self._window.get_active_tab())
96 #self._window.create_tab_from_uri(uri, enc, line_pos+1, False, True)
99 class EncodingPlugin(GObject.Object, Gedit.WindowActivatable):
101 window = GObject.property(type=Gedit.Window)
103 def __init__(self):
104 GObject.Object.__init__(self)
105 self._instances = {}
107 def do_activate(self):
108 self._instances[self.window] = EncodingWindowHelper(self, self.window)
110 def do_deactivate(self):
111 self._instances[self.window].deactivate()
112 del self._instances[self.window]
114 def do_update_state(self):
115 self._instances[self.window].update_ui()