redapple2008 发表于 2021-4-16 09:09:13

SciTE设置UTF8BOM格式

本帖最后由 redapple2008 于 2021-4-16 10:30 编辑

https://www.autoitscript.com/forum/topic/205631-scite-utf8bom-only-for-au3-files/
如果设置了属性 NewFileEncoding = UTF8BOM将在SciTE中创建的每个文件都将获得此编码。如果仅使用au3-文件,则可以。但是,如果您还使用例如lua文件,则必须删除BOM。因此,我提出了另一种解决方案:<pre class="ipsCode prettyprint lang-c prettyprinted" style="box-sizing: border-box; font-family: Consolas, &quot;Courier New&quot;, &quot;Andale Mono&quot;, monospace; font-size: 12px; overflow: auto; tab-size: 4; white-space: pre-wrap; line-height: 13.2px; text-rendering: optimizespeed; background: rgb(248, 248, 248); clear: both; direction: ltr; overflow-wrap: normal; padding: 5px; border: 1px solid rgb(201, 201, 201); margin-left: 10px; max-height: 500px; color: rgb(53, 60, 65);">-- TIME_STAMP   2021-04-15 14:02:19


--[[
After a file was saved, the event OnSave will fired.
Now will checked:
- needs this file typ the BOM?
- If Yes: Has this file already the BOM?
-                  If No: Write the BOM sequence at the beginning of the file


If you want register other types as "au3" for set BOM use this property in SciTEUser.properties:

#~ File types, that needs byte order mark
#~ "au3" is predefined and does not need to be set here
BOM.File.Types=extension_1 extension_2
]]


CheckBOM = EventClass:new(Common)

CheckBOM.OnSave = function(self, _file)
      if not self:NeedsBOM(props['FileExt']) then return nil end
      if not self:StartsWithBOM(_file) then scite.MenuCommand(153) end
      return nil
end

CheckBOM.StartsWithBOM = function(self, _file)
      local ToHex = function(_s)
                if _s == nil then return "DEAD" end
                return (_s:gsub('.', function(_c) return ('%02X'):format(_c:byte()) end))
      end
      local fh = io.open(_file, "rb")
      local read = fh:read(3)
      fh:close()
      return (ToHex(read) == "EFBBBF")
end

CheckBOM.NeedsBOM = function(self, _ext)
      local extensions = props['BOM.File.Types']:lower()..' au3'
      if extensions:find(_ext:lower()) then return true else return false end
end</pre>
用“ SciTEStartup.lua”加载“ CheckBOM.lua”,并根据需要创建属性“ BOM.File.Types”以添加其他文件类型以使用BOM。

afan 发表于 2021-4-16 10:18:34

转帖注明出处,代码也不完整
CheckBOM.NeedsBOM = function(self, _ext)
      local extensions = props['BOM.File.Types']:lower()..' au3'
      if extensions:find(_ext:lower()) then r
这明显不完整

CheckBOM.NeedsBOM = function(self, _ext)
        local extensions = props['BOM.File.Types']:lower()..' au3'
        if extensions:find(_ext:lower()) then return true else return false end
end

redapple2008 发表于 2021-4-16 10:30:35

afan 发表于 2021-4-16 10:18
转帖注明出处,代码也不完整
这明显不完整



地址加上了。

afan 发表于 2021-4-16 10:40:26

redapple2008 发表于 2021-4-16 10:30
地址加上了。

代码编辑后又多了两处 html 标签,唉
页: [1]
查看完整版本: SciTE设置UTF8BOM格式