Encoding in statusbar (gedit plugin) - Display document encoding in statusbar

Motivation: gedit is a smart generic text editor, but it does not indicate the character encoding of the file being edited.

Pretty frequently I have to deal with files of various character encodings. By default, gedit attempts to detect encoding while opening a file - should this auto-detection fail, you can still select the proper charset manually. When saving a file (save as...) you can specify the line-ending (CR/LF) and encoding to be used.

I was missing two features related to the topic: On one hand I would have preferred some visual feedback of the encoding the editor has opened the file with - handy especially in case of auto-detection. On the other hand users might need a quick way to change the character encoding used for the current file. Here I do not refer to converting the file itself to a different charset, but rather to re-displaying the current file using a different encoding.

The second issue is a known gap for which others have already created a gedit-plugin that lists character sets in a menu, and once one of the menu items is clicked, re-opens the active document with that encoding. This solved one part of my issues...

Unfortunately, we still do not receive any visual indication of the encoding used to display the current document, so after some howto reading I grabbed my keyboard... My goal was to author a plugin that enables the status bar to display the encoding used for the document in the currently active tab.

Update

Due to the transition to Gedit3 I had to rewrite the plugin from scratch. I have enhanced the initial quick and version to make use of signals in order to ensure the GUI update logic is only triggered when necessary.

Installation

gedit 2

The plugin consists of two files: a descriptor (encoding-in-statusbar.gedit-plugin) and the python source itself (encoding-in-statusbar.py). These need to be dropped into ~/.gnome2/gedit/plugins, and after [re]starting gedit, the plugin can be enabled from Edit / Preferences / Plugins.

gedit 3

The internal architecture of gedit3 has received significant changes, rendering most gedit2 plugins unusable with gedit3. The descriptor (encoding-in-statusbar.plugin) and the python source (encoding-in-statusbar.py) have to be dropped into ~/.local/share/gedit/plugins as of the new version, and after [re]starting gedit, the plugin can be enabled from Edit / Preferences / Plugins as usual.

Source

gedit 2


#
# Encoding in statusbar (gedit plugin) - Display document encoding in statusbar
# 
# Copyright 2011 Tibor Bősze <tibor.boesze@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import gtk
import gedit

class EncodingInStatusBarWindowHelper:
    def __init__(self, plugin, window):
        self._window = window
        self._plugin = plugin
        self._frame = gtk.Frame()
        self._label = gtk.Label()
        self._frame.add(self._label)
        self.update_ui()
        window.get_statusbar().pack_end(self._frame, False, False)
        self._frame.show_all()

    def deactivate(self):
        gtk.HBox.remove(self._window.get_statusbar(), self._frame)
        self._window = None
        self._plugin = None
        self._frame = None
        self._label = None

    def update_ui(self):
        doc = self._window.get_active_document()
        if doc:
            self._label.set_text(doc.get_encoding().to_string())
        else:
            self._label.set_text('(no encoding)');

class EncodingInStatusBarPlugin(gedit.Plugin):
    def __init__(self):
        super(gedit.Plugin, self).__init__()
        self._instances = {}

    def activate(self, window):
        self._instances[window] = EncodingInStatusBarWindowHelper(self, window)

    def deactivate(self, window):
        self._instances[window].deactivate()
        del self._instances[window]

    def update_ui(self, window):
        self._instances[window].update_ui()

[Gedit Plugin]
Loader=python
Module=encoding-in-statusbar
IAge=2
Name=Encoding in statusbar
Description=Display document encoding in statusbar
Authors=Tibor Bősze <tibor.boesze@gmail.com>
Copyright=Copyright © 2011 Tibor Bősze <tibor.boesze@gmail.com>
Website=http://lithium.io7.org/~shanq/bitsnbytes/encoding-in-statusbar.html
Icon=gucharmap
Version=0.1

gedit 3


# -*- coding: UTF-8 -*-
#
# Encoding in statusbar (gedit plugin) - Display document encoding in statusbar
# 
# Copyright 2012 Tibor Bősze <tibor.boesze@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from gi.repository import GObject, Gtk, Gedit

class EncodingInStatusbar(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "EncodingInStatusbar"
    window = GObject.property(type=Gedit.Window)
   
    def __init__(self):
        GObject.Object.__init__(self)
        
    def do_activate(self):
        self._label = Gtk.Label()
        self.window.get_statusbar().pack_end(self._label, False, False, 5)
        self._label.show()
        handlers = []        
        handlers.append(self.window.connect("active_tab_changed", self._on_active_tab_change))
        handlers.append(self.window.connect("active_tab_state_changed", self._on_active_tab_state_change))
        self.window.set_data(self.__gtype_name__, handlers);
        self._update_via_doc(self.window.get_active_document())

    def do_deactivate(self):
        Gtk.Container.remove(self.window.get_statusbar(), self._label)
        del self._label
        for handler in self.window.get_data(self.__gtype_name__):
            self.window.disconnect(handler)

    def do_update_state(self):
        pass

    def _update_via_doc(self, doc):
        if doc:
            self._label.set_text(doc.get_encoding().to_string())            
        else:
            self._label.set_text('(no encoding)');

    def _on_active_tab_change(self, window, tab):
        self._update_via_doc(tab.get_document())

    def _on_active_tab_state_change(self, window):
        if Gedit.TabState.STATE_NORMAL == window.get_active_tab().get_state():
            self._update_via_doc(self.window.get_active_document())


[Plugin]
Loader=python
Module=encoding-in-statusbar
IAge=3
Name=Encoding in statusbar
Description=Display document encoding in statusbar
Authors=Tibor Bősze <tibor.boesze@gmail.com>
Copyright=Copyright © 2012 Tibor Bősze <tibor.boesze@gmail.com>
Website=http://lithium.io7.org/~shanq/bitsnbytes/encoding-in-statusbar.html
Icon=gucharmap
Version=0.3
© 2003-2020 lithium.io7.org
Content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.