2

I have a plugin for detecting and syntax highlighting *.k - source files in a given programming language. My ~/.vimrc contains only:

se nocp rtp+=~/k/vim-k   " the language plugin
sy on

And there isn't any ~/.vim directory.

After opening a *.k file, :scriptnames shows kwt.vim, cpp.vim, and c.vim from $VIMRUNTIME/syntax before the k.vim file from my language plugin. So, Vim must be doing filetype detection twice - initially through its built-in plugins as a "kwt" file (which leads to other problems; but to keep the question focused, I won't go into detail), and then again through my plugin.

How can I suppress the initial filetype detection?

2
  • Do you still have somethinf open in your question? How can we help you further? Otherwise maybe could you accept your answer using the v button next to the arrow voting buttons. It allow the question to rest :-) Sep 19, 2023 at 3:18
  • 1
    @VivianDeSmedt done
    – ngn
    Sep 19, 2023 at 9:22

3 Answers 3

3

The *.k extension is detected by $VIMRUNTIME/filetype.vim that comes before the ~/.vim/ftdetect/*.vim in the execution list.

That is why you have these scripts being evaluated (kwd.vim calling cpp.vim calling c.vim)

You can by pass that by making your detection in ~/.vim/filetype.vim that is executed before.

But as a plugin there is no way to by pass the detection of $VIMRUNTIME/filetype.vim.

But you can:

  1. Override the filetype using: set ft=k instead of setf k
    autocmd BufNew,BufRead *.k set ft=k
    
  2. Override the détection by using (as found by @nhn):
    aitocmd! BufNew,BufRead *.k setf k
    

The first don't prevent the scripts to be called but their effect should be undone by Vim

The second override the detection and the scripts will not be called

3
  • so, it's better to have loaded and unloaded a .vim file than never to have loaded it at all? :P
    – ngn
    Sep 16, 2023 at 17:34
  • making your detection in ~/.vim/filetype.vim - The problem is that users of my language plugin would have to do that manually. But it can work as a last resort. Thanks
    – ngn
    Sep 16, 2023 at 17:34
  • @ngn you can add a filetype detection script to your vim-k directory, e.g. vim-k/ftdetect/vim-k.vim alongside your existing vim-k/plugin/vim-k.vim and presumably also vim-k/syntax/vim-k.vim and vim-k/indent/vim-k.vim. This is conventionally how language/filetype plugins are written and distributed. Sep 17, 2023 at 13:21
2

After a lot of trial and error, I found a solution.

It turns out that the order of entries in runtimepath doesn't matter.

All I had to do was use au! instead of au in the plugin's ftdetect/k.vim. That clears the kwt autocommand from $VIMRUNTIME/filetype.vim and prevents the loading of irrelevant files.

1
  • You are right that is the good solution :-) I miss it. I have corrected my solution crediting you ;-) Sep 17, 2023 at 5:01
2
  1. You don't generally need to suppress the filetype detection, because (proper) filetype plugins set b:undo_ftplugin and other variables, which Vim uses automatically to "undo" the results of previous filetype plugins. Similar logic applies to syntax files, which is what you mention, except that Vim automatically runs :syntax clear when a syntax changes (see $VIMRUNTIME/synload.vim and :help syntax-loading).
  2. I can't find a good way to skip the default checks: :filetype on runs $VIMRUNTIME/filetype.vim, which doesn't do :runtime! ftdetect/*.vim until the default detections are installed. But see above: there should be no need to skip it.

You wrote in the comments:

The problem was caused by my own custom highlighting for c, which was loaded after the kwt -> cpp -> c chain, so vim is not to blame here (apart from the faulty filetype detection).

It sounds like something in your C highlighting is not playing nicely with standard Vim mechanisms to make sure changes to 'syntax' or 'filetype' start from a clean slate.

3
  • 2. Strange - even if I prepend with let &rtp='~/k/vim-k,'.&rtp, vim still loads its own kwt.vim cpp.vim c.vim
    – ngn
    Sep 16, 2023 at 17:49
  • 3. The problem was caused by my own custom highlighting for c, which was loaded after the kwt -> cpp -> c chain, so vim is not to blame here (apart from the faulty filetype detection).
    – ngn
    Sep 16, 2023 at 17:52
  • I’ll have to double-check some bits on (2) based on your comment and answer. I think I misremembered how filetype on works wrt detection orders.
    – D. Ben Knoble
    Sep 16, 2023 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.