1 # -*- coding: utf-8 -*-
2 # Open the document in a different encoding
3 # Dependence: python >=2.5, pygtk
5 # Install: copy encoding.gedit-plugin and encodingpy.py to ~/.gnome2/gedit/plugins/
7 # Copyright (C) 2008 Vladislav Gorbunov
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.
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
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())
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"/>
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
):
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()
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
)
104 GObject
.Object
.__init
__(self
)
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()