hinokiyoの手順書

自分のためにも手順書チックなものを残していければなと思っています。

Ubuntuのgeditに文字コードを表示する方法

Ubuntuテキストエディタは何がいいんですかね?おすすめって何かあるんでしょうか。
とりあえず、普段はwindowsサクラエディタを使っているので
サクラエディタUbuntu版がないかなと調べたところ、wineを使ってサクラエディタを動かすというのが出てきました。
これでもいいんですが、せっかくUbuntu使っているのでGeditをしばらく使ってみようということになりました。

行番号表示とか、スペース表示・改行コード表示はググると出てくるんですが、「文字コードはどうやって表示するの?」となりました。

調べていると有効なWikiNameではありません - DebugIto'sが出てきたんですが、古くてgeditの3.10には使えず。

更に調べるとありました。
Encoding in statusbar (gedit plugin) - Display document encoding in statusbar
ハンガリーの方ありがとう!!。
下記に手順を書きます。

ソースファイル作成まで

  1. 下記Geditのプラグインディレクトリがなければ自分で作成します。
    /home/username/.local/share/gedit/plugins
    ※1.usernameは自分の環境に合わせて書き換えてください。
    ※2.ここは自分で作ったプラグインを入れる専用のディレクトリみたいです。
  2. 上記で作成したディレクトリ内に下記2つのファイルを作成する。
    1. encoding-in-statusbar.plugin
    2. encoding-in-statusbar.py

ソースファイルの中身

作成した各ファイルの内容を下記のようにして保存。

  • encoding-in-statusbar.plugin
[Plugin]
Loader=python3
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.homenet.org/~shanq/bitsnbytes/encoding-in-statusbar.html
Icon=gucharmap
Version=0.3
  • encoding-in-statusbar.py
# -*- 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())

プラグインを有効にして確認

  1. geditを再起動して編集→設定のプラグインタブを表示。
  2. “Encoding in statusbar” のチェックをONにして有効にする。
  3. 再度、geditを再起動。

うまく行くとgeditの画面下部に文字コードが出ます。 f:id:hinokiyo:20151012020104p:plain