diff mbox series

contrib: vim: ftdetect: don't conflict with other filetypes

Message ID 20250303125502.2939507-1-rhi@pengutronix.de
State Accepted, archived
Commit e8efbba5d7bb4b685ed0a9b970e042ad99be8afb
Headers show
Series contrib: vim: ftdetect: don't conflict with other filetypes | expand

Commit Message

Roland Hieber March 3, 2025, 12:55 p.m. UTC
Use :setfiletype instead of :set filetype. The former only sets the
'filetype' option if it has not been set before, which makes it possible
to override the syntax of certain *.inc files in autocommands from e.g.
.vimrc or modelines. All other ftdetect plugins in upstream vim also use
:setfiletype for this reason.

The detection for bitbake *.inc files is now upstream since Vim 9.0
patch 0055 [1]. If we're running an earlier Vim, use the detection
heuristic from upstream [2] to overwrite the filetype explicitely if we
find bitbake code. But don't always assuming that *.inc files are
bitbake files so as not to break Perl, PHP, Assembly, Povray, etc.

[1]: https://github.com/vim/vim/commit/fa49eb482729
[2]: https://github.com/vim/vim/blob/fb49e3cde79d/runtime/autoload/dist/ft.vim#L715

Signed-off-by: Roland Hieber <rhi@pengutronix.de>
---
 contrib/vim/ftdetect/bitbake.vim | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/contrib/vim/ftdetect/bitbake.vim b/contrib/vim/ftdetect/bitbake.vim
index 09fc4dc74c25..427ab5b987c3 100644
--- a/contrib/vim/ftdetect/bitbake.vim
+++ b/contrib/vim/ftdetect/bitbake.vim
@@ -11,10 +11,18 @@  if &compatible || version < 600 || exists("b:loaded_bitbake_plugin")
 endif
 
 " .bb, .bbappend and .bbclass
-au BufNewFile,BufRead *.{bb,bbappend,bbclass}  set filetype=bitbake
+au BufNewFile,BufRead *.{bb,bbappend,bbclass}  setfiletype bitbake
 
-" .inc
-au BufNewFile,BufRead *.inc		set filetype=bitbake
+" .inc -- meanwhile included upstream
+if !has("patch-9.0.0055")
+    au BufNewFile,BufRead *.inc                call s:BBIncDetect()
+    def s:BBIncDetect()
+        l:lines = getline(1) .. getline(2) .. getline(3)
+        if l:lines =~# '\<\%(require\|inherit\)\>' || lines =~# '[A-Z][A-Za-z0-9_:${}]*\s\+\%(??\|[?:+]\)\?= '
+            set filetype bitbake
+        endif
+    enddef
+endif
 
 " .conf
 au BufNewFile,BufRead *.conf