From 3aa833e0d90204102bb8a90a5d7dc1323b882bfd Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 11 Apr 2025 16:45:14 +0800 Subject: [PATCH] vim-patch:5c84d12: runtime(filetype): make shell filetype detection more robust (#33421) closes: vim/vim#17063 https://github.com/vim/vim/commit/5c84d12df104e976e8f56c43f9f975e54d9fd779 Co-authored-by: Aliaksei Budavei <0x000c70@gmail.com> --- runtime/lua/vim/filetype/detect.lua | 17 ++++++++++------- runtime/syntax/sh.vim | 11 ++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index fc0b45ecd8..96be7edbae 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -1502,33 +1502,36 @@ local function sh(path, contents, name) -- Get the name from the first line if not specified name = name or contents[1] - if matchregex(name, [[\]]) then + if name:find('^csh$') or matchregex(name, [[^#!.\{-2,}\]]) then -- Some .sh scripts contain #!/bin/csh. return M.shell(path, contents, 'csh') + elseif name:find('^tcsh$') or matchregex(name, [[^#!.\{-2,}\]]) then -- Some .sh scripts contain #!/bin/tcsh. - elseif matchregex(name, [[\]]) then return M.shell(path, contents, 'tcsh') + elseif name:find('^zsh$') or matchregex(name, [[^#!.\{-2,}\]]) then -- Some .sh scripts contain #!/bin/zsh. - elseif matchregex(name, [[\]]) then return M.shell(path, contents, 'zsh') end local on_detect --- @type fun(b: integer)? - if matchregex(name, [[\]]) then + if name:find('^ksh$') or matchregex(name, [[^#!.\{-2,}\]]) then on_detect = function(b) vim.b[b].is_kornshell = 1 vim.b[b].is_bash = nil vim.b[b].is_sh = nil end - elseif vim.g.bash_is_sh or matchregex(name, [[\<\(bash\|bash2\)\>]]) then + elseif + vim.g.bash_is_sh + or name:find('^bash2?$') + or matchregex(name, [[^#!.\{-2,}\]]) + then on_detect = function(b) vim.b[b].is_bash = 1 vim.b[b].is_kornshell = nil vim.b[b].is_sh = nil end - -- Ubuntu links sh to dash - elseif matchregex(name, [[\<\(sh\|dash\)\>]]) then + elseif findany(name, { '^sh$', '^dash$' }) or matchregex(name, [[^#!.\{-2,}\<\%(da\)\=sh\>]]) then -- Ubuntu links "sh" to "dash" on_detect = function(b) vim.b[b].is_sh = 1 vim.b[b].is_kornshell = nil diff --git a/runtime/syntax/sh.vim b/runtime/syntax/sh.vim index 298198274e..9e5320f1ce 100644 --- a/runtime/syntax/sh.vim +++ b/runtime/syntax/sh.vim @@ -9,6 +9,7 @@ " 2025 Jan 18 add bash coproc, remove duplicate syn keywords (#16467) " 2025 Mar 21 update shell capability detection (#16939) " 2025 Apr 03 command substitution opening paren at EOL (#17026) +" 2025 Apr 10 improve shell detection (#17084) " Version: 208 " Former URL: http://www.drchip.org/astronaut/vim/index.html#SYNTAX_SH " For options and settings, please use: :help ft-sh-syntax @@ -23,11 +24,13 @@ endif let b:is_sh = 1 " If the shell script itself specifies which shell to use, use it -if getline(1) =~ '\' +let s:shebang = getline(1) + +if s:shebang =~ '^#!.\{-2,}\' let b:is_kornshell = 1 -elseif getline(1) =~ '\' +elseif s:shebang =~ '^#!.\{-2,}\' let b:is_bash = 1 -elseif getline(1) =~ '\' +elseif s:shebang =~ '^#!.\{-2,}\' let b:is_dash = 1 " handling /bin/sh with is_kornshell/is_sh {{{1 " b:is_sh will be set when "#! /bin/sh" is found; @@ -70,6 +73,8 @@ elseif !exists("b:is_kornshell") && !exists("b:is_bash") && !exists("b:is_posix" endif endif +unlet s:shebang + " if b:is_dash, set b:is_posix too if exists("b:is_dash") let b:is_posix= 1