patch 9.1.1687: filetype: autoconf filetype not always correct

Commit: 
https://github.com/vim/vim/commit/2b55474f0a78bd252290578a5774316dc5f60180
Author: Damien Lejay <[email protected]>
Date:   Tue Aug 26 17:09:47 2025 +0200

    patch 9.1.1687: filetype: autoconf filetype not always correct
    
    Problem:  filetype: autoconf filetype not always correct
    Solution: Detect aclocal.m4 as config filetype, detect configure.ac as
              config filetype, fall back to POSIX m4 (Damien Lejay).
    
    closes: #18065
    
    Signed-off-by: Damien Lejay <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim
index 380b97bf4..2172626dc 100644
--- a/runtime/autoload/dist/ft.vim
+++ b/runtime/autoload/dist/ft.vim
@@ -3,7 +3,7 @@ vim9script
 # Vim functions for file type detection
 #
 # Maintainer:          The Vim Project <https://github.com/vim/vim>
-# Last Change:         2025 Jul 09
+# Last Change:         2025 Aug 26
 # Former Maintainer:   Bram Moolenaar <[email protected]>
 
 # These functions are moved here from runtime/filetype.vim to make startup
@@ -599,6 +599,50 @@ export def FTm()
   endif
 enddef
 
+# For files ending in *.m4, distinguish:
+#  – *.html.m4 files
+#  – files in the Autoconf M4 dialect
+#  – files in POSIX M4
+export def FTm4()
+  var fname = expand('%:t')
+  var path  = expand('%:p:h')
+
+  # Case 0: canonical Autoconf file
+  if fname ==# 'aclocal.m4'
+    setf config
+    return
+  endif
+
+  # Case 1: html.m4
+  if fname =~# 'html\.m4$'
+    setf htmlm4
+    return
+  endif
+
+  # Case 2: repo heuristic (nearby configure.ac)
+  if filereadable(path .. '/../configure.ac') || filereadable(path .. 
'/configure.ac')
+    setf config
+    return
+  endif
+
+  # Case 3: content heuristic (scan first ~200 lines)
+  # Signals:
+  #   - Autoconf macro prefixes: AC_/AM_/AS_/AU_/AT_
+  var n = 1
+  var max = min([200, line('$')])
+  while n <= max
+    var line = getline(n)
+    if line =~# '^\s*A[CMSUT]_'
+      setf config
+      return
+    endif
+    n += 1
+  endwhile
+
+  # Case 4: default to POSIX M4
+  setf m4
+enddef
+
 export def FTmake()
   # Check if it is a BSD, GNU, or Microsoft Makefile
   unlet! b:make_flavor
diff --git a/runtime/filetype.vim b/runtime/filetype.vim
index 1b0e4183f..af21a47fb 100644
--- a/runtime/filetype.vim
+++ b/runtime/filetype.vim
@@ -1,7 +1,7 @@
 " Vim support file to detect file types
 "
 " Maintainer:          The Vim Project <https://github.com/vim/vim>
-" Last Change:         2025 Aug 10
+" Last Change:         2025 Aug 26
 " Former Maintainer:   Bram Moolenaar <[email protected]>
 
 " Listen very carefully, I will say this only once
@@ -219,8 +219,8 @@ au BufNewFile,BufRead *.au3                 setf autoit
 " Autohotkey
 au BufNewFile,BufRead *.ahk                    setf autohotkey
 
-" Autotest .at files are actually m4
-au BufNewFile,BufRead *.at                     setf m4
+" Autotest .at files are actually Autoconf M4
+au BufNewFile,BufRead *.at                     setf config
 
 " Avenue
 au BufNewFile,BufRead *.ave                    setf ave
@@ -1157,9 +1157,6 @@ au BufNewFile,BufRead *.http                      setf 
http
 " HTML with Ruby - eRuby
 au BufNewFile,BufRead *.erb,*.rhtml            setf eruby
 
-" HTML with M4
-au BufNewFile,BufRead *.html.m4                        setf htmlm4
-
 " Some template.  Used to be HTML Cheetah.
 au BufNewFile,BufRead *.tmpl                   setf template
 
@@ -1519,8 +1516,8 @@ au BufNewFile,BufRead *.lsl                       call 
dist#ft#FTlsl()
 au BufNewFile,BufRead *.lss                    setf lss
 
 " M4
-au BufNewFile,BufRead *.m4
-       \ if expand("<afile>") !~? 'html.m4$\|fvwm2rc' | setf m4 | endif
+au BufNewFile,BufRead *.m4                      call dist#ft#FTm4()
+
 au BufNewFile,BufRead .m4_history              setf m4
 
 " MaGic Point
diff --git a/src/testdir/test_filetype.vim b/src/testdir/test_filetype.vim
index 8ac4f54ee..42e4cbcde 100644
--- a/src/testdir/test_filetype.vim
+++ b/src/testdir/test_filetype.vim
@@ -1,5 +1,10 @@
 " Test :setfiletype
 
+" Make VIMRUNTIME and &rtp absolute.
+" Otherwise, a :lcd inside a test would break the relative ../../runtime path.
+let $VIMRUNTIME = fnamemodify($VIMRUNTIME, ':p')
+let &rtp = join(map(split(&rtp, ','), 'fnamemodify(v:val, ":p")'), ',')
+
 func Test_backup_strip()
   filetype on
   let fname = 'Xdetect.js~~~~~~~~~~~'
@@ -186,7 +191,7 @@ def s:GetFilenameChecks(): dict<list<string>>
     coco: ['file.atg'],
     conaryrecipe: ['file.recipe'],
     conf: ['auto.master', 'file.conf', 'texdoc.cnf', '.x11vncrc', '.chktexrc', 
'.ripgreprc', 'ripgreprc', 'file.ctags'],
-    config: ['configure.in', 'configure.ac', '/etc/hostname.file', 
'any/etc/hostname.file'],
+    config: ['/etc/hostname.file', 'any/etc/hostname.file', 'configure.in', 
'configure.ac', 'alocal.m4', 'file.at'],
     confini: ['pacman.conf', 'paru.conf', 'mpv.conf', 'any/.aws/config', 
'any/.aws/credentials', 'file.nmconnection',
               'any/.gnuradio/grc.conf', 'any/gnuradio/config.conf', 
'any/gnuradio/conf.d/modtool.conf'],
     context: ['tex/context/any/file.tex', 'file.mkii', 'file.mkiv', 
'file.mkvi', 'file.mkxl', 'file.mklx'],
@@ -466,7 +471,7 @@ def s:GetFilenameChecks(): dict<list<string>>
              'any/m17n-db/file.ali', 'any/m17n-db/file.cs', 
'any/m17n-db/file.dir', 'any/m17n-db/FLT/file.flt', 'any/m17n-db/file.fst', 
'any/m17n-db/LANGDATA/file.lnm', 'any/m17n-db/file.mic', 
'any/m17n-db/MIM/file.mim', 'any/m17n-db/file.tbl'],
     m3build: ['m3makefile', 'm3overrides'],
     m3quake: ['file.quake', 'cm3.cfg'],
-    m4: ['file.at', '.m4_history'],
+    m4: ['.m4_history'],
     mail: ['snd.123', '.letter', '.letter.123', '.followup', '.article', 
'.article.123', 'pico.123', 'mutt-xx-xxx', 'muttng-xx-xxx', 'ae123.txt', 
'file.eml', 'reportbug-file'],
     mailaliases: ['/etc/mail/aliases', '/etc/aliases', 'any/etc/aliases', 
'any/etc/mail/aliases'],
     mailcap: ['.mailcap', 'mailcap'],
@@ -1180,6 +1185,43 @@ endfunc
 " Keep sorted.
 """""""""""""""""""""""""""""""""""""""""""""""""
 
+" Since dist#ft#FTm4() looks around for configure.ac
+" the test needs to isolate itself in a fresh temporary project tree,
+" so that no configure.ac from another test (or from the repo root)
+" accidentally influences detection.
+func Test_autoconf_file()
+  filetype on
+  " Make a fresh sandbox far away from any configure.ac
+  let save_cwd = getcwd()
+  call mkdir('Xproj_autoconf/a/b', 'p')
+  execute 'lcd Xproj_autoconf/a/b'
+
+  try
+    call writefile(['AC_CHECK_HEADERS([stdio.h])'], 'foo.m4', 'D')
+    split foo.m4
+    call assert_equal('config', &filetype)
+    bwipe!
+
+    call writefile(['AS_IF([true], [:])'], 'bar.m4', 'D')
+    split bar.m4
+    call assert_equal('config', &filetype)
+    bwipe!
+
+    call writefile(['AC_INIT([foo],[1.0])'], 'configure.ac')
+    call mkdir('m4', 'p')
+    call writefile([], 'm4/empty.m4', 'D')
+    split m4/empty.m4
+    call assert_equal('config', &filetype)
+    bwipe!
+  finally
+    call delete('m4', 'rf')
+    call delete('configure.ac')
+    execute 'lcd' fnameescape(save_cwd)
+    call delete('Xproj_autoconf', 'rf')
+    filetype off
+  endtry
+endfunc
+
 func Test_bas_file()
   filetype on
 
@@ -1925,6 +1967,30 @@ func Test_m_file()
   filetype off
 endfunc
 
+" Since dist#ft#FTm4() looks around for configure.ac
+" the test needs to isolate itself in a fresh temporary project tree,
+" so that no configure.ac from another test (or from the repo root)
+" accidentally influences detection.
+func Test_m4_file()
+  filetype on
+  let save_cwd = getcwd()
+
+  " Make a fresh sandbox far away from any configure.ac
+  call mkdir('Xsandbox/level1/level2', 'p')
+  execute 'lcd Xsandbox/level1/level2'
+
+  try
+    call writefile(["define(`FOO', `bar')", "FOO"], 'plain.m4', 'D')
+    split plain.m4
+    call assert_equal('m4', &filetype)
+    bwipe!
+  finally
+    execute 'lcd' fnameescape(save_cwd)
+    call delete('Xsandbox', 'rf')
+    filetype off
+  endtry
+endfunc
+
 func Test_mod_file()
   filetype on
 
diff --git a/src/version.c b/src/version.c
index 4f47ec268..309ddf7f7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -724,6 +724,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1687,
 /**/
     1686,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1uqvO5-003Tsq-QR%40256bit.org.

Raspunde prin e-mail lui