-- Which files the agent showed may be opened: only kinds that are merely looked at or listened -- to, and only when the file's first bytes are that kind and its name ends as that kind does (the -- system picks its program by the name, so a name the bytes do not back is never handed to it). -- The same rule as the VS Code extension's (vscode/src/media.js); tests/rules/openable.json holds -- the cases both must answer alike. local M = {} local TEXT_ENDINGS = {} for _, e in ipairs({ "txt", "text", "log", "md", "markdown", "csv", "tsv", "json", "jsonl", "yaml", "yml", "toml", "ini", "diff", "patch" }) do TEXT_ENDINGS[e] = true end local function has(list, v) return vim.tbl_contains(list, v) end local function picture(b) if #b < 12 then return nil end if b:sub(1, 6) == "\137PNG\r\n" then return { "png" } end if b:sub(1, 3) == "\255\216\255" then return { "jpg", "jpeg" } end if b:sub(1, 6) == "GIF87a" or b:sub(1, 6) == "GIF89a" then return { "gif" } end if b:sub(1, 4) == "RIFF" and b:sub(9, 12) == "WEBP" then return { "webp" } end if b:sub(5, 8) == "ftyp" and (b:sub(9, 12) == "avif" or b:sub(9, 12) == "avis") then return { "avif" } end return nil end local function brand(b) return b:sub(5, 8) == "ftyp" and b:sub(9, 12) or "" end local function one_of(v, list) return has(list, v) end local KINDS = { { "pdf", { "pdf" }, function(b) return b:sub(1, 5) == "%PDF-" end }, { "audio", { "mp3" }, function(b) return b:sub(1, 3) == "ID3" or (b:byte(1) == 0xff and bit.band(b:byte(2), 0xe6) == 0xe2) end }, { "audio", { "wav" }, function(b) return b:sub(1, 4) == "RIFF" and b:sub(9, 12) == "WAVE" end }, { "audio", { "ogg", "oga", "opus" }, function(b) return b:sub(1, 4) == "OggS" end }, { "audio", { "flac" }, function(b) return b:sub(1, 4) == "fLaC" end }, { "audio", { "m4a" }, function(b) return one_of(brand(b), { "M4A ", "M4B ", "mp42", "isom" }) end }, { "video", { "mp4", "m4v" }, function(b) local br = brand(b) return one_of(br, { "isom", "mp41", "mp42", "avc1", "M4V ", "dash" }) or br:match("^iso[2-9]$") ~= nil end }, { "video", { "mov" }, function(b) return brand(b) == "qt " end }, { "video", { "webm", "mkv" }, function(b) return b:sub(1, 4) == "\26\69\223\163" end }, } local function looks_like_text(b) if #b == 0 then return false end for i = 1, #b do local c = b:byte(i) if c == 0 or (c < 0x20 and c ~= 0x09 and c ~= 0x0a and c ~= 0x0d and c ~= 0x0c) then return false end end -- The window may end inside a character; up to three bytes are let go before judging. for cut = 0, math.min(3, #b - 1) do if M._valid_utf8(b:sub(1, #b - cut)) then return true end end return false end --- Whether `s` is well-formed UTF-8. function M._valid_utf8(s) local i, n = 1, #s while i <= n do local c = s:byte(i) local more, low if c < 0x80 then more = 0 elseif c >= 0xc2 and c <= 0xdf then more = 1 elseif c >= 0xe0 and c <= 0xef then more = 2 elseif c >= 0xf0 and c <= 0xf4 then more = 3 else return false end if i + more > n then return false end for k = 1, more do low = s:byte(i + k) if low < 0x80 or low > 0xbf then return false end end -- No overlong forms, no surrogates, nothing past U+10FFFF. local second = more > 0 and s:byte(i + 1) or 0 if (c == 0xe0 and second < 0xa0) or (c == 0xed and second > 0x9f) or (c == 0xf0 and second < 0x90) or (c == 0xf4 and second > 0x8f) then return false end i = i + more + 1 end return true end --- What a shown file is, from its first bytes (`head`, a string) and its name: "image", "pdf", --- "audio", "video" or "text" when it may be opened, nil when it may only be named. function M.openable(head, name) local ending = (tostring(name or ""):match("%.([%w]+)$") or ""):lower() if type(head) ~= "string" or ending == "" then return nil end local pic = picture(head) if pic then return has(pic, ending) and "image" or nil end local known = false for _, k in ipairs(KINDS) do if #head >= 12 and k[3](head) then if has(k[2], ending) then return k[1] end known = true end end if known then return nil end -- the bytes are a kind the name does not say if TEXT_ENDINGS[ending] and looks_like_text(head) then return "text" end return nil end --- The first bytes of the file at `path`, or nil. function M.head(path) local f = io.open(path, "rb") if not f then return nil end local b = f:read(4096) f:close() return b or "" end return M