aboutsummaryrefslogtreecommitdiff
path: root/lua/plugins/filetype.lua
diff options
context:
space:
mode:
authorkhwerz <khwerz@gmail.com>2022-06-21 00:01:14 -0400
committerFelix Van der Jeugt <felix.vanderjeugt@posteo.net>2022-07-23 10:23:06 +0200
commit1ca937518ed5ca00e18e5e59e1fef0d9bd628e16 (patch)
treea253079d8eebc14dc4757b4009b57266c0609012 /lua/plugins/filetype.lua
parentcbeda11801c95687498e2deb6a0144b562581d16 (diff)
downloadvis-1ca937518ed5ca00e18e5e59e1fef0d9bd628e16.tar.gz
vis-1ca937518ed5ca00e18e5e59e1fef0d9bd628e16.tar.xz
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.
Diffstat (limited to 'lua/plugins/filetype.lua')
-rw-r--r--lua/plugins/filetype.lua50
1 files 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()