changeset 32582:c777d71cf397 v9.0.1623

patch 9.0.1623: the program to filetype translation is not exported Commit: https://github.com/vim/vim/commit/f07d1a7108f29068efeb61d91454c4120ab5ae6c Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jun 9 21:01:47 2023 +0100 patch 9.0.1623: the program to filetype translation is not exported Problem: The program to filetype translation is not exported. Solution: Export Exe2filetype().
author Bram Moolenaar <Bram@vim.org>
date Fri, 09 Jun 2023 22:15:03 +0200
parents fa05c32c24ca
children 3a10160f2242
files runtime/autoload/dist/ft.vim runtime/autoload/dist/script.vim src/version.c
diffstat 3 files changed, 93 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/autoload/dist/ft.vim
+++ b/runtime/autoload/dist/ft.vim
@@ -3,7 +3,7 @@ vim9script
 # Vim functions for file type detection
 #
 # Maintainer:	Bram Moolenaar <Bram@vim.org>
-# Last Change:	2023 Apr 22
+# Last Change:	2023 Jun 09
 
 # These functions are moved here from runtime/filetype.vim to make startup
 # faster.
@@ -362,8 +362,8 @@ export def ProtoCheck(default: string)
   else
     # recognize Prolog by specific text in the first non-empty line
     # require a blank after the '%' because Perl uses "%list" and "%translate"
-    var l = getline(nextnonblank(1))
-    if l =~ '\<prolog\>' || l =~ '^\s*\(%\+\(\s\|$\)\|/\*\)' || l =~ ':-'
+    var lnum = getline(nextnonblank(1))
+    if lnum =~ '\<prolog\>' || lnum =~ '^\s*\(%\+\(\s\|$\)\|/\*\)' || lnum =~ ':-'
       setf prolog
     else
       exe 'setf ' .. default
@@ -472,12 +472,12 @@ enddef
 def IsLProlog(): bool
   # skip apparent comments and blank lines, what looks like
   # LambdaProlog comment may be RAPID header
-  var l: number = nextnonblank(1)
-  while l > 0 && l < line('$') && getline(l) =~ '^\s*%' # LambdaProlog comment
-    l = nextnonblank(l + 1)
+  var lnum: number = nextnonblank(1)
+  while lnum > 0 && lnum < line('$') && getline(lnum) =~ '^\s*%' # LambdaProlog comment
+    lnum = nextnonblank(lnum + 1)
   endwhile
   # this pattern must not catch a go.mod file
-  return getline(l) =~ '\<module\s\+\w\+\s*\.\s*\(%\|$\)'
+  return getline(lnum) =~ '\<module\s\+\w\+\s*\.\s*\(%\|$\)'
 enddef
 
 # Determine if *.mod is ABB RAPID, LambdaProlog, Modula-2, Modsim III or go.mod
@@ -504,8 +504,8 @@ export def FTpl()
   else
     # recognize Prolog by specific text in the first non-empty line
     # require a blank after the '%' because Perl uses "%list" and "%translate"
-    var l = getline(nextnonblank(1))
-    if l =~ '\<prolog\>' || l =~ '^\s*\(%\+\(\s\|$\)\|/\*\)' || l =~ ':-'
+    var line = getline(nextnonblank(1))
+    if line =~ '\<prolog\>' || line =~ '^\s*\(%\+\(\s\|$\)\|/\*\)' || line =~ ':-'
       setf prolog
     else
       setf perl
@@ -678,26 +678,24 @@ export def McSetf()
 enddef
 
 # Called from filetype.vim and scripts.vim.
-export def SetFileTypeSH(name: string)
-  if did_filetype()
+# When "setft" is passed and false then the 'filetype' option is not set.
+export def SetFileTypeSH(name: string, setft = true): string
+  if setft && did_filetype()
     # Filetype was already detected
-    return
+    return ''
   endif
-  if expand("<amatch>") =~ g:ft_ignore_pat
-    return
+  if setft && expand("<amatch>") =~ g:ft_ignore_pat
+    return ''
   endif
   if name =~ '\<csh\>'
     # Some .sh scripts contain #!/bin/csh.
-    SetFileTypeShell("csh")
-    return
+    return SetFileTypeShell("csh", setft)
   elseif name =~ '\<tcsh\>'
     # Some .sh scripts contain #!/bin/tcsh.
-    SetFileTypeShell("tcsh")
-    return
+    return SetFileTypeShell("tcsh", setft)
   elseif name =~ '\<zsh\>'
     # Some .sh scripts contain #!/bin/zsh.
-    SetFileTypeShell("zsh")
-    return
+    return SetFileTypeShell("zsh", setft)
   elseif name =~ '\<ksh\>'
     b:is_kornshell = 1
     if exists("b:is_bash")
@@ -724,34 +722,43 @@ export def SetFileTypeSH(name: string)
       unlet b:is_bash
     endif
   endif
-  SetFileTypeShell("sh")
+
+  return SetFileTypeShell("sh", setft)
 enddef
 
 # For shell-like file types, check for an "exec" command hidden in a comment,
 # as used for Tcl.
+# When "setft" is passed and false then the 'filetype' option is not set.
 # Also called from scripts.vim, thus can't be local to this script.
-export def SetFileTypeShell(name: string)
-  if did_filetype()
+export def SetFileTypeShell(name: string, setft = true): string
+  if setft && did_filetype()
     # Filetype was already detected
-    return
+    return ''
   endif
-  if expand("<amatch>") =~ g:ft_ignore_pat
-    return
+  if setft && expand("<amatch>") =~ g:ft_ignore_pat
+    return ''
   endif
-  var l = 2
-  while l < 20 && l < line("$") && getline(l) =~ '^\s*\(#\|$\)'
+
+  var lnum = 2
+  while lnum < 20 && lnum < line("$") && getline(lnum) =~ '^\s*\(#\|$\)'
     # Skip empty and comment lines.
-    l += 1
+    lnum += 1
   endwhile
-  if l < line("$") && getline(l) =~ '\s*exec\s' && getline(l - 1) =~ '^\s*#.*\\$'
+  if lnum < line("$") && getline(lnum) =~ '\s*exec\s' && getline(lnum - 1) =~ '^\s*#.*\\$'
     # Found an "exec" line after a comment with continuation
-    var n = substitute(getline(l), '\s*exec\s\+\([^ ]*/\)\=', '', '')
+    var n = substitute(getline(lnum), '\s*exec\s\+\([^ ]*/\)\=', '', '')
     if n =~ '\<tclsh\|\<wish'
-      setf tcl
-      return
+      if setft
+	setf tcl
+      endif
+      return 'tcl'
     endif
   endif
-  exe "setf " .. name
+
+  if setft
+    exe "setf " .. name
+  endif
+  return name
 enddef
 
 export def CSH()
--- a/runtime/autoload/dist/script.vim
+++ b/runtime/autoload/dist/script.vim
@@ -4,7 +4,7 @@ vim9script
 # Invoked from "scripts.vim" in 'runtimepath'
 #
 # Maintainer:	Bram Moolenaar <Bram@vim.org>
-# Last Change:	2023 Jun 08
+# Last Change:	2023 Jun 09
 
 export def DetectFiletype()
   var line1 = getline(1)
@@ -53,155 +53,164 @@ def DetectFromHashBang(firstline: string
     name = 'wish'
   endif
 
+  var ft = Exe2filetype(name, line1)
+  if ft != ''
+    exe 'setl ft=' .. ft
+  endif
+enddef
+
+# Returns the filetype name associated with program "name".
+# "line1" is the #! line at the top of the file.  Use the same as "name" if
+# not available.
+# Returns an empty string when not recognized.
+export def Exe2filetype(name: string, line1: string): string
     # Bourne-like shell scripts: bash bash2 dash ksh ksh93 sh
   if name =~ '^\(bash\d*\|dash\|ksh\d*\|sh\)\>'
-    call dist#ft#SetFileTypeSH(line1)
+    return dist#ft#SetFileTypeSH(line1, false)
 
     # csh scripts
   elseif name =~ '^csh\>'
-    if exists("g:filetype_csh")
-      call dist#ft#SetFileTypeShell(g:filetype_csh)
-    else
-      call dist#ft#SetFileTypeShell("csh")
-    endif
+    return dist#ft#SetFileTypeShell(exists("g:filetype_csh") ? g:filetype_csh : 'csh', false)
 
     # tcsh scripts
   elseif name =~ '^tcsh\>'
-    call dist#ft#SetFileTypeShell("tcsh")
+    return dist#ft#SetFileTypeShell("tcsh", false)
 
     # Z shell scripts
   elseif name =~ '^zsh\>'
-    setl ft=zsh
+    return 'zsh'
 
     # TCL scripts
   elseif name =~ '^\(tclsh\|wish\|expectk\|itclsh\|itkwish\)\>'
-    setl ft=tcl
+    return 'tcl'
 
     # Expect scripts
   elseif name =~ '^expect\>'
-    setl ft=expect
+    return 'expect'
 
     # Gnuplot scripts
   elseif name =~ '^gnuplot\>'
-    setl ft=gnuplot
+    return 'gnuplot'
 
     # Makefiles
   elseif name =~ 'make\>'
-    setl ft=make
+    return 'make'
 
     # Pike
   elseif name =~ '^pike\%(\>\|[0-9]\)'
-    setl ft=pike
+    return 'pike'
 
     # Lua
   elseif name =~ 'lua'
-    setl ft=lua
+    return 'lua'
 
     # Perl
   elseif name =~ 'perl'
-    setl ft=perl
+    return 'perl'
 
     # PHP
   elseif name =~ 'php'
-    setl ft=php
+    return 'php'
 
     # Python
   elseif name =~ 'python'
-    setl ft=python
+    return 'python'
 
     # Groovy
   elseif name =~ '^groovy\>'
-    setl ft=groovy
+    return 'groovy'
 
     # Raku
   elseif name =~ 'raku'
-    setl ft=raku
+    return 'raku'
 
     # Ruby
   elseif name =~ 'ruby'
-    setl ft=ruby
+    return 'ruby'
 
     # JavaScript
   elseif name =~ 'node\(js\)\=\>\|js\>' || name =~ 'rhino\>'
-    setl ft=javascript
+    return 'javascript'
 
     # BC calculator
   elseif name =~ '^bc\>'
-    setl ft=bc
+    return 'bc'
 
     # sed
   elseif name =~ 'sed\>'
-    setl ft=sed
+    return 'sed'
 
     # OCaml-scripts
   elseif name =~ 'ocaml'
-    setl ft=ocaml
+    return 'ocaml'
 
     # Awk scripts; also finds "gawk"
   elseif name =~ 'awk\>'
-    setl ft=awk
+    return 'awk'
 
     # Website MetaLanguage
   elseif name =~ 'wml'
-    setl ft=wml
+    return 'wml'
 
     # Scheme scripts
   elseif name =~ 'scheme'
-    setl ft=scheme
+    return 'scheme'
 
     # CFEngine scripts
   elseif name =~ 'cfengine'
-    setl ft=cfengine
+    return 'cfengine'
 
     # Erlang scripts
   elseif name =~ 'escript'
-    setl ft=erlang
+    return 'erlang'
 
     # Haskell
   elseif name =~ 'haskell'
-    setl ft=haskell
+    return 'haskell'
 
     # Scala
   elseif name =~ 'scala\>'
-    setl ft=scala
+    return 'scala'
 
     # Clojure
   elseif name =~ 'clojure'
-    setl ft=clojure
+    return 'clojure'
 
     # Free Pascal
   elseif name =~ 'instantfpc\>'
-    setl ft=pascal
+    return 'pascal'
 
     # Fennel
   elseif name =~ 'fennel\>'
-    setl ft=fennel
+    return 'fennel'
 
     # MikroTik RouterOS script
   elseif name =~ 'rsc\>'
-    setl ft=routeros
+    return 'routeros'
 
     # Fish shell
   elseif name =~ 'fish\>'
-    setl ft=fish
+    return 'fish'
 
     # Gforth
   elseif name =~ 'gforth\>'
-    setl ft=forth
+    return 'forth'
 
     # Icon
   elseif name =~ 'icon\>'
-    setl ft=icon
+    return 'icon'
 
     # Guile
   elseif name =~ 'guile'
-    setl ft=scheme
+    return 'scheme'
 
     # Nix
   elseif name =~ 'nix-shell'
-    setl ft=nix
+    return 'nix'
 
   endif
+
+  return ''
 enddef
 
 
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1623,
+/**/
     1622,
 /**/
     1621,