gedit 片段插件不适用于除 UTF-8 之外的其他编码

gedit 片段插件不适用于除 UTF-8 之外的其他编码

我刚刚遇到了在 Ubuntu 12.04 上运行的 gedit 3.4.1 的 snippets 插件的问题。问题是,当文件的编码不是 UTF-8(例如 ISO-8859-1)时,snippets 插件会停止工作(即,不会对键盘快捷键和制表符完成做出反应)。在这种情况下,snippets 插件不再起作用,控制台上的错误输出显示:

Traceback (most recent call last):
  File "/usr/lib/gedit/plugins/snippets/library.py", line 678, in accelerator_activated
    ret = cb(group, obj, keyval, mod)
  File "/usr/lib/gedit/plugins/snippets/windowactivatable.py", line 216, in accelerator_activated
    return controller.accelerator_activate(keyval, mod)
  File "/usr/lib/gedit/plugins/snippets/document.py", line 148, in accelerator_activate
    self.apply_snippet(snippets[0])
  File "/usr/lib/gedit/plugins/snippets/document.py", line 519, in apply_snippet
    env = self.get_environment()
  File "/usr/lib/gedit/plugins/snippets/document.py", line 483, in get_environment
    v = variables[var](buf)
  File "/usr/lib/gedit/plugins/snippets/document.py", line 348, in env_get_current_line
    return {'utf8': u8, 'noenc': self.string_in_native_doc_encoding(buf, u8)}
  File "/usr/lib/gedit/plugins/snippets/document.py", line 320, in string_in_native_doc_encoding
    except GLib.GError:
NameError: global name 'GLib' is not defined

显然,文件 /usr/lib/gedit/plugins/snippets/document.py 中存在一个问题,当文档编码不是 UTF-8 时,就会调用函数 GLib.convert。因此,如果我将同一个文件保存为 UTF-8,一切正常。

我不太懂 Python,所以如果能提供任何关于如何修复这个问题的指示我将非常感激。

答案1

显然,这个错误将在 Ubuntu 的下一个版本中得到修复。要立即修复,只需编辑/usr/lib/gedit/plugins/snippets/document.py,将第 22 行从

from gi.repository import Gtk, Gdk, Gio, GtkSource, Gedit

from gi.repository import Gtk, Gdk, Gio, GLib, GtkSource, Gedit

(即包括GLib导入)。别忘了重新启动 gedit :-)

相关内容