@@ -1291,7 +1291,6 @@ local filename = {
12911291WORKSPACE = ' bzl' ,
12921292BUILD = ' bzl' ,
12931293 [' cabal.project' ]= ' cabalproject' ,
1294- [vim .env .HOME .. ' /cabal.config' ]= ' cabalconfig' ,
12951294 [' cabal.config' ]= ' cabalconfig' ,
12961295calendar = ' calendar' ,
12971296catalog = ' catalog' ,
@@ -1699,6 +1698,7 @@ local pattern = {
16991698 [' .*/meta%-.*/conf/.*%.conf' ]= ' bitbake' ,
17001699 [' bzr_log%..*' ]= ' bzr' ,
17011700 [' .*enlightenment/.*%.cfg' ]= ' c' ,
1701+ [' ${HOME}/cabal%.config' ]= ' cabalconfig' ,
17021702 [' cabal%.project%..*' ]= starsetf (' cabalproject' ),
17031703 [' .*/%.calendar/.*' ]= starsetf (' calendar' ),
17041704 [' .*/share/calendar/.*/calendar%..*' ]= starsetf (' calendar' ),
@@ -1823,41 +1823,21 @@ local pattern = {
18231823 [' .*/%.config/git/config' ]= ' gitconfig' ,
18241824 [' .*%.git/config%.worktree' ]= ' gitconfig' ,
18251825 [' .*%.git/worktrees/.*/config%.worktree' ]= ' gitconfig' ,
1826- [' .*/git/config' ]= function (path ,bufnr )
1827- if vim .env .XDG_CONFIG_HOME and path :find (vim .env .XDG_CONFIG_HOME .. ' /git/config' )then
1828- return ' gitconfig'
1829- end
1830- end ,
1826+ [' ${XDG_CONFIG_HOME}/git/config' ]= ' gitconfig' ,
18311827 [' .*%.git/info/attributes' ]= ' gitattributes' ,
18321828 [' .*/etc/gitattributes' ]= ' gitattributes' ,
18331829 [' .*/%.config/git/attributes' ]= ' gitattributes' ,
1834- [' .*/git/attributes' ]= function (path ,bufnr )
1835- if vim .env .XDG_CONFIG_HOME and path :find (vim .env .XDG_CONFIG_HOME .. ' /git/attributes' )then
1836- return ' gitattributes'
1837- end
1838- end ,
1830+ [' ${XDG_CONFIG_HOME}/git/attributes' ]= ' gitattributes' ,
18391831 [' .*%.git/info/exclude' ]= ' gitignore' ,
18401832 [' .*/%.config/git/ignore' ]= ' gitignore' ,
1841- [' .*/git/ignore' ]= function (path ,bufnr )
1842- if vim .env .XDG_CONFIG_HOME and path :find (vim .env .XDG_CONFIG_HOME .. ' /git/ignore' )then
1843- return ' gitignore'
1844- end
1845- end ,
1833+ [' ${XDG_CONFIG_HOME}/git/ignore' ]= ' gitignore' ,
18461834 [' %.gitsendemail%.msg%.......' ]= ' gitsendemail' ,
18471835 [' gkrellmrc_.' ]= ' gkrellmrc' ,
18481836 [' .*/usr/.*/gnupg/options%.skel' ]= ' gpg' ,
18491837 [' .*/%.gnupg/options' ]= ' gpg' ,
18501838 [' .*/%.gnupg/gpg%.conf' ]= ' gpg' ,
1851- [' .*/options' ]= function (path ,bufnr )
1852- if vim .env .GNUPGHOME and path :find (vim .env .GNUPGHOME .. ' /options' )then
1853- return ' gpg'
1854- end
1855- end ,
1856- [' .*/gpg%.conf' ]= function (path ,bufnr )
1857- if vim .env .GNUPGHOME and path :find (vim .env .GNUPGHOME .. ' /gpg%.conf' )then
1858- return ' gpg'
1859- end
1860- end ,
1839+ [' ${GNUPGHOME}/options' ]= ' gpg' ,
1840+ [' ${GNUPGHOME}/gpg%.conf' ]= ' gpg' ,
18611841 [' .*/etc/group' ]= ' group' ,
18621842 [' .*/etc/gshadow' ]= ' group' ,
18631843 [' .*/etc/group%.edit' ]= ' group' ,
@@ -1871,7 +1851,7 @@ local pattern = {
18711851 [' .*/etc/grub%.conf' ]= ' grub' ,
18721852-- gtkrc* and .gtkrc*
18731853 [' %.?gtkrc.*' ]= starsetf (' gtkrc' ),
1874- [vim . env . VIMRUNTIME .. ' /doc/.*%.txt' ]= ' help' ,
1854+ [' ${VIMRUNTIME} /doc/.*%.txt' ]= ' help' ,
18751855 [' hg%-editor%-.*%.txt' ]= ' hgcommit' ,
18761856 [' .*/etc/host%.conf' ]= ' hostconf' ,
18771857 [' .*/etc/hosts%.deny' ]= ' hostsaccess' ,
@@ -2275,6 +2255,9 @@ end
22752255--- Filename patterns can specify an optional priority to resolve cases when a
22762256--- file path matches multiple patterns. Higher priorities are matched first.
22772257--- When omitted, the priority defaults to 0.
2258+ --- A pattern can contain environment variables of the form "${ENV}" that will be
2259+ --- automatically expanded. If the environment variable is not set, the pattern
2260+ --- won't be matched.
22782261---
22792262--- See $VIMRUNTIME/lua/vim/filetype.lua for more examples.
22802263---
@@ -2303,6 +2286,8 @@ end
23032286--- ['.*/etc/foo/.*'] = 'fooscript',
23042287--- -- Using an optional priority
23052288--- ['.*/etc/foo/.*%.conf'] = { 'dosini', { priority = 10 } },
2289+ --- -- A pattern containing an environment variable
2290+ --- ['${XDG_CONFIG_HOME}/foo/git'] = 'git',
23062291--- ['README.(%a+)$'] = function(path, bufnr, ext)
23072292--- if ext == 'md' then
23082293--- return 'markdown'
@@ -2376,8 +2361,28 @@ local function dispatch(ft, path, bufnr, ...)
23762361end
23772362end
23782363
2364+ -- Caches calls to string.find
2365+ local expand_env_lookup = {}
2366+
23792367--- @private
23802368local function match_pattern (name ,path ,tail ,pat )
2369+ if expand_env_lookup [pat ]== nil then
2370+ expand_env_lookup [pat ]= pat :find (' %$' )~= nil
2371+ end
2372+ if expand_env_lookup [pat ]then
2373+ local return_early
2374+ pat = pat :gsub (' %${(%S-)}' ,function (env )
2375+ -- If an environment variable is present in the pattern but not set, there is no match
2376+ if not vim .env [env ]then
2377+ return_early = true
2378+ return nil
2379+ end
2380+ return vim .env [env ]
2381+ end )
2382+ if return_early then
2383+ return false
2384+ end
2385+ end
23812386-- If the pattern contains a / match against the full path, otherwise just the tail
23822387local fullpat = ' ^' .. pat .. ' $'
23832388local matches