From 1ca937518ed5ca00e18e5e59e1fef0d9bd628e16 Mon Sep 17 00:00:00 2001 From: khwerz Date: Tue, 21 Jun 2022 00:01:14 -0400 Subject: simplify loop to sanitize filename and read extension This uses gsub instead of find+sub. Also changes an ignoresuffix pattern from /~$/ to /~+$/ which should reduce iterations. --- lua/plugins/filetype.lua | 50 ++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/lua/plugins/filetype.lua b/lua/plugins/filetype.lua index 88ae9f2..b8640fc 100644 --- a/lua/plugins/filetype.lua +++ b/lua/plugins/filetype.lua @@ -1,7 +1,7 @@ vis.ftdetect = {} vis.ftdetect.ignoresuffixes = { - "~$", "%.orig$", "%.bak$", "%.old$", "%.new$" + "~+$", "%.orig$", "%.bak$", "%.old$", "%.new$" } vis.ftdetect.filetypes = { @@ -484,39 +484,35 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) win:set_syntax(syntax) end - local name = win.file.name - -- remove ignored suffixes from filename - local sanitizedfn = name - if sanitizedfn ~= nil then - sanitizedfn = sanitizedfn:gsub('^.*/', '') - repeat - local changed = false - for _, pattern in pairs(vis.ftdetect.ignoresuffixes) do - local start = sanitizedfn:find(pattern) - if start then - sanitizedfn = sanitizedfn:sub(1, start-1) - changed = true + local path = win.file.name -- filepath + local mime + + if path and #path > 0 then + local name = path:match("[^/]+") -- filename + if name then + local unchanged + while #name > 0 and name ~= unchanged do + unchanged = name + for _, pattern in ipairs(vis.ftdetect.ignoresuffixes) do + name = name:gsub(pattern, "") end end - until not changed - end + end - -- detect filetype by filename ending with a configured extension - if sanitizedfn ~= nil then - for lang, ft in pairs(vis.ftdetect.filetypes) do - for _, pattern in pairs(ft.ext or {}) do - if sanitizedfn:match(pattern) then - set_filetype(lang, ft) - return + if name and #name > 0 then + -- detect filetype by filename ending with a configured extension + for lang, ft in pairs(vis.ftdetect.filetypes) do + for _, pattern in pairs(ft.ext or {}) do + if name:match(pattern) then + set_filetype(lang, ft) + return + end end end end - end - -- run file(1) to determine mime type - local mime - if name ~= nil then - local file = io.popen(string.format("file -bL --mime-type -- '%s'", name:gsub("'", "'\\''"))) + -- run file(1) to determine mime type + local file = io.popen(string.format("file -bL --mime-type -- '%s'", path:gsub("'", "'\\''"))) if file then mime = file:read('*all') file:close() -- cgit v1.2.3