ZeroBraneStudio之支持GBK文件编码

费了好大劲终于搞定了让ZBS支持打开GBK文件了。记录下过程:

看源码发现ZBS打开文件时会调用src\editor\commands.lua中的LoadFile函数,代码如下:

1 local file_text = FileRead(filePath)
2   if file_text then
3     if GetConfigIOFilter("input") then
4       file_text = GetConfigIOFilter("input")(filePath,file_text)
5     end
6     
7   elseif file_must_exist then
8     return nil
9   end

发现其中会读取配置文件GetConfigIOFilter,再在cfg\user-sample.lua中看看有没有和iofilter相关的配置项,找到如下代码:

-- to fix an issue with 0d0d0a line endings in MOAI examples,
-- which may negatively affect breakpoints during debugging
editor.iofilter = "0d0d0aFix"

  OK,找到之后再在所有文件中搜下“0d0d0aFix”,呵呵,终于在src\iofilters.lua中找到了:

ide.iofilters["0d0d0aFix"] = {
  -- this function converts 0d0d0a line ending to 0d0a
  input = function(fpath, content)
    return content:gsub("\013\013\010","\013\010")
  end,
}

找到位置就好办了,照着他的例子照葫芦画瓢,写个将GBK转换成UTF8的例子就行了。

local encode = require("encode")

ide.iofilters["GbkToUtf8"] = {
  -- this function converts 0d0d0a line ending to 0d0a
  input = function(fpath, content)
    return encode.GbkToUtf8(content)
  end,
  output = function (fpath, content)
    return encode.Utf8ToGbk(content)
  end
}

其中需要用到2个文件encode.lua、bitOp.lua,将这2个文件放到lualibs目录中。

再配置一下就行了,在Edit->Preferences->Settings:User中加入下面代码:

language = "cn"
editor.iofilter='GbkToUtf8'

posted on 2014-12-04 18:21  没事就晕  阅读(966)  评论(0)    收藏  举报

导航