--[[

encoding

for menu.cfg

&Auto Detect{
luamacro(AutoDetect(),(O&ff))
luamacro(AutoDetect(universal_charset_detector),Uni&versal)
luamacro(AutoDetect(zh_parallel_state_machine),Ch&inese)
luamacro(AutoDetect(ja_parallel_state_machine),J&apanese)
luamacro(AutoDetect(ko_parallel_state_machine),K&orean)
luamacro(AutoDetect(cjk_parallel_state_machine),&East Asian)
luamacro(AutoDetect(ruprob),Ru&ssian)
luamacro(AutoDetect(ukprob),U&krainian)
}

En&coding{
:&Auto Detect
-
luamacro(ForceCharset(iso-8859-1),&Western (ISO-8859-1))
-
luamacro(ForceCharset(iso-8859-2),&Central European (ISO-8859-2))
luamacro(ForceCharset(windows-1250),C&entral European (Windows-1250))
-
luamacro(ForceCharset(utf-8),&Unicode (UTF-8))
luamacro(ForceCharset(utf-7),U&nicode (UTF-7))
}

end of menu.cfg

]]

require "hook"

local pref_charset_det = "intl.charset.detector"
local last_detector, last_encoding = "", ""

local function detect_sync(arg)
	km.SetCheck("luamacro(AutoDetect(" .. last_detector .. "))", false)
	last_detector = arg or km.getpref(TYPE_STRING, pref_charset_det) or ""
	km.SetCheck("luamacro(AutoDetect(" .. last_detector .. "))", true)
end

local function encoding_sync(arg)
	km.SetCheck("luamacro(ForceCharset(" .. last_encoding .. "))", false)
	last_encoding = arg or km.GetGlobalVar(TYPE_STRING, "CHARSET") or ""
	km.SetCheck("luamacro(ForceCharset(" .. last_encoding .. "))", true)
end

function ForceCharset(arg)
	km.SetForceCharset(arg or "")
	encoding_sync(arg)
end

function AutoDetect(arg)
	local det = arg or ""
	km.setpref(TYPE_STRING, pref_charset_det, det)
	detect_sync(det)
	ForceCharset()
end

hook.add(StartupHook, detect_sync)
hook.add(LoadHook, encoding_sync)
hook.add(ActivateWindowHook, encoding_sync)
