-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathencawdx.lua
60 lines (54 loc) · 2 KB
/
encawdx.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- requires enca
local cmd = "enca"
local params = ''
local cacheoutput = false
local output = ''
local filename = ''
local values = { -- return values
{"UTF-8", "CP1251", "IBM866", "KOI8-R", "ISO-8859-5", "UTF-7", "CP1252", "ASCII", "???"},
}
local fields = { -- field name, command line parameters, pattern, return values
{"iconv name", "-g -i", "([^\n]+)", nil},
{"iconv name(multichoice)", "-g -i", "([^\n]+)", values[1]},
{"enca's encoding name", "-g -e", "([^\n]+)", nil},
{"lang ru", "-L ru -g", "(.+)\n$", nil},
}
function ContentGetSupportedField(FieldIndex)
if (fields[FieldIndex + 1] ~= nil) then
if (fields[FieldIndex + 1][4] ~= nil) then
local units = '';
for i = 1 , #fields[FieldIndex + 1][4] do
if (i > 1) then
units = units .. '|';
end
units = units .. fields[FieldIndex + 1][4][i];
end
return fields[FieldIndex + 1][1], units, 7;
else
return fields[FieldIndex + 1][1], "", 8;
end
end
return '', '', 0; -- ft_nomorefields
end
function ContentGetDetectString()
return nil; -- return detect string
end
function ContentGetValue(FileName, FieldIndex, UnitIndex, flags)
if (filename ~= FileName) or (params ~= fields[FieldIndex + 1][2]) then
local attr = SysUtils.FileGetAttr(FileName);
if (attr < 0) or (math.floor(attr / 0x00000004) % 2 ~= 0) or (math.floor(attr / 0x00000010) % 2 ~= 0) then
return nil;
end
params = fields[FieldIndex + 1][2];
local handle = io.popen(cmd .. ' ' .. params .. ' "' .. FileName:gsub('"', '\\"') .. '"');
output = handle:read("*a");
handle:close();
if (cacheoutput == true) then
filename = FileName;
end
end
if (fields[FieldIndex + 1][3] ~= nil) then
return output:match(fields[FieldIndex + 1][3]);
end
return nil;
end