@@ -3,7 +3,7 @@ vim9script
33# Vim functions for file type detection
44#
55# Maintainer: Bram Moolenaar
<[email protected] > 6- # Last Change: 2023 Apr 22
6+ # Last Change: 2023 Jun 09
77
88# These functions are moved here from runtime /filetype .vim to make startup
99# faster.
@@ -362,8 +362,8 @@ export def ProtoCheck(default: string)
362362 else
363363 # recognize Prolog by specific text in the first non- empty line
364364 # require a blank after the ' %' because Perl uses " %list" and " %translate"
365- var l = getline (nextnonblank (1 ))
366- if l = ~ ' \<prolog\>' || l = ~ ' ^\s*\(%\+\(\s\|$\)\|/\*\)' || l = ~ ' :-'
365+ var lnum = getline (nextnonblank (1 ))
366+ if lnum = ~ ' \<prolog\>' || lnum = ~ ' ^\s*\(%\+\(\s\|$\)\|/\*\)' || lnum = ~ ' :-'
367367 setf prolog
368368 else
369369 exe ' setf ' .. default
@@ -472,12 +472,12 @@ enddef
472472def IsLProlog (): bool
473473 # skip apparent comments and blank lines , what looks like
474474 # LambdaProlog comment may be RAPID header
475- var l : number = nextnonblank (1 )
476- while l > 0 && l < line (' $' ) && getline (l ) = ~ ' ^\s*%' # LambdaProlog comment
477- l = nextnonblank (l + 1 )
475+ var lnum : number = nextnonblank (1 )
476+ while lnum > 0 && lnum < line (' $' ) && getline (lnum ) = ~ ' ^\s*%' # LambdaProlog comment
477+ lnum = nextnonblank (lnum + 1 )
478478 endwhile
479479 # this pattern must not catch a go .mod file
480- return getline (l ) = ~ ' \<module\s\+\w\+\s*\.\s*\(%\|$\)'
480+ return getline (lnum ) = ~ ' \<module\s\+\w\+\s*\.\s*\(%\|$\)'
481481enddef
482482
483483# Determine if * .mod is ABB RAPID, LambdaProlog, Modula- 2 , Modsim III or go .mod
@@ -504,8 +504,8 @@ export def FTpl()
504504 else
505505 # recognize Prolog by specific text in the first non- empty line
506506 # require a blank after the ' %' because Perl uses " %list" and " %translate"
507- var l = getline (nextnonblank (1 ))
508- if l = ~ ' \<prolog\>' || l = ~ ' ^\s*\(%\+\(\s\|$\)\|/\*\)' || l = ~ ' :-'
507+ var line = getline (nextnonblank (1 ))
508+ if line = ~ ' \<prolog\>' || line = ~ ' ^\s*\(%\+\(\s\|$\)\|/\*\)' || line = ~ ' :-'
509509 setf prolog
510510 else
511511 setf perl
@@ -678,26 +678,24 @@ export def McSetf()
678678enddef
679679
680680# Called from filetype .vim and scripts.vim .
681- export def SetFileTypeSH (name: string )
682- if did_filetype ()
681+ # When " setft" is passed and false then the ' filetype' option is not set .
682+ export def SetFileTypeSH (name: string , setft = true): string
683+ if setft && did_filetype ()
683684 # Filetype was already detected
684- return
685+ return ' '
685686 endif
686- if expand (" <amatch>" ) = ~ g: ft_ignore_pat
687- return
687+ if setft && expand (" <amatch>" ) = ~ g: ft_ignore_pat
688+ return ' '
688689 endif
689690 if name = ~ ' \<csh\>'
690691 # Some .sh scripts contain #! /bin/ csh.
691- SetFileTypeShell (" csh" )
692- return
692+ return SetFileTypeShell (" csh" , setft)
693693 elseif name = ~ ' \<tcsh\>'
694694 # Some .sh scripts contain #! /bin/ tcsh.
695- SetFileTypeShell (" tcsh" )
696- return
695+ return SetFileTypeShell (" tcsh" , setft)
697696 elseif name = ~ ' \<zsh\>'
698697 # Some .sh scripts contain #! /bin/ zsh.
699- SetFileTypeShell (" zsh" )
700- return
698+ return SetFileTypeShell (" zsh" , setft)
701699 elseif name = ~ ' \<ksh\>'
702700 b: is_kornshell = 1
703701 if exists (" b:is_bash" )
@@ -724,34 +722,43 @@ export def SetFileTypeSH(name: string)
724722 unlet b: is_bash
725723 endif
726724 endif
727- SetFileTypeShell (" sh" )
725+
726+ return SetFileTypeShell (" sh" , setft)
728727enddef
729728
730729# For shell - like file types, check for an " exec" command hidden in a comment ,
731730# as used for Tcl.
731+ # When " setft" is passed and false then the ' filetype' option is not set .
732732# Also called from scripts.vim , thus can't be local to this script .
733- export def SetFileTypeShell (name: string )
734- if did_filetype ()
733+ export def SetFileTypeShell (name: string , setft = true): string
734+ if setft && did_filetype ()
735735 # Filetype was already detected
736- return
736+ return ' '
737737 endif
738- if expand (" <amatch>" ) = ~ g: ft_ignore_pat
739- return
738+ if setft && expand (" <amatch>" ) = ~ g: ft_ignore_pat
739+ return ' '
740740 endif
741- var l = 2
742- while l < 20 && l < line (" $" ) && getline (l ) = ~ ' ^\s*\(#\|$\)'
741+
742+ var lnum = 2
743+ while lnum < 20 && lnum < line (" $" ) && getline (lnum) = ~ ' ^\s*\(#\|$\)'
743744 # Skip empty and comment lines .
744- l += 1
745+ lnum += 1
745746 endwhile
746- if l < line (" $" ) && getline (l ) = ~ ' \s*exec\s' && getline (l - 1 ) = ~ ' ^\s*#.*\\$'
747+ if lnum < line (" $" ) && getline (lnum ) = ~ ' \s*exec\s' && getline (lnum - 1 ) = ~ ' ^\s*#.*\\$'
747748 # Found an " exec" line after a comment with continuation
748- var n = substitute (getline (l ), ' \s*exec\s\+\([^ ]*/\)\=' , ' ' , ' ' )
749+ var n = substitute (getline (lnum ), ' \s*exec\s\+\([^ ]*/\)\=' , ' ' , ' ' )
749750 if n = ~ ' \<tclsh\|\<wish'
750- setf tcl
751- return
751+ if setft
752+ setf tcl
753+ endif
754+ return ' tcl'
752755 endif
753756 endif
754- exe " setf " .. name
757+
758+ if setft
759+ exe " setf " .. name
760+ endif
761+ return name
755762enddef
756763
757764export def CSH ()
0 commit comments