Thursday, April 19, 2018

Re: modeline: syntax or filetype?

On Wed, Apr 18, 2018 at 9:33 AM, Alexander Dahl <post@lespocky.de> wrote:
Hei hei,

in the ptxdist project [1] we have files of type kconfig and make,
where the filetype is not detected by default, so since the very first
commits in the project vim modelines were used. Over time the options
used there evolved and are not consistent anymore:

    % grep -h 'vim:' rules/*.make | sort | uniq
    # vim: ft=make noet
    # vim: ft=make noet ts=8 sw=8
    # vim: ft=make ts=8 noet tw=80
    # vim: ft=make ts=8 tw=80
    # vim: syntax=make
    # vim: syntax=make tabstop=8

    % grep -h 'vim:' rules/*.in | sort | uniq
    # vim: ft=kconfig noet tw=72
    # vim: ft=kconfig ts=8 noet tw=80
    # vim: set ft=kconfig:
    # vim: set ft=kconfig sw=8 ts=8 noet:
    # vim: set sw=8 ts=8 noet ft=kconfig:
    # vim: set ts=8 sw=8 noet ft=kconfig:
    # vim: syntax=kconfig

Before taking some effort to make this consistent again over existing
files and templates for new files, I wanted to ask about the
difference between 'syntax' and 'filetype'? From the documentation I
understood, filetype sets syntax and additional options. Also from
digging around I learned there is one set of definition files
distributed with Vim for filetype and another set for syntax, not
necessarily both for a given type of file? Can that cause problems,
e.g. if the modeline tries to set a filetype for which no definition
exists in the distributed Vim?

'syntax' defines how to do syntax highlighting. 'filetype' sets other things, for instance buffer-local options and/or mappings. 'filetype' also sets 'syntax', usually to the same name.

If there is no filetype-plugin for the relevant filetype in any directory in 'runtimepath', the result is the same as if there had been an empty one, except that depending on the 'verbose' setting you may get a warning message telling you that no ftplugin script was found. This is not an error, and (if there is a syntax script) the proper syntax highlighting will be set.

My problem with the current files: I have expandtab set in my personal
vimrc as a default. When I open a file called 'Makefile' Vim
recognizes it as filetype=make and automatically sets 'noexpandtab'
which you need in a makefile to not produce non working files.

Yes, in makefiles tabs and spaces don't have the same meaning so it's important not to replace tabs by spaces when editing them.

Now with makefiles not automatically recognized by Vim (different
name/extension) this happens:

* Without modeline: ft=conf, expandtab can be anything: bad
* With modeline 'syntax=make': syntax highlighting correct, but
expandtab set, while noexpandtab would be needed: bad
* With modeline 'syntax=make noet': fine
* With modeline 'ft=make': fine
 
This is the expected behaviour if Vim fails to recognize the filetype automatically. The ":setlocal noet" command is found in the ftplugin, not in the syntax script, which busies itself with highlighting and nothing else.

Now it seems to me, setting ft in the modeline would be better, but
can it have undesired side effects? Perhaps with options I did not
consider?

You can find that out by reading $VIMRUNTIME/ftplugin/make.vim plus (if there are any) ftplugin/make.vim files under any other directories in 'runtimepath'. Normally there won't be any, unless you or your systems administrator have put them there.

What would you recommend to set in the modeline in general? Does it
make sense to set 'syntax' instead of 'filetype'? Or should 'filetype'
be preferred, because the options would set things right, which are
maybe set unfavorable by e.g. vimrc?

Look what is done in the modelines at the bottom of help files, IMHO they are a good example.

Should 'noet' be set additionally to 'ft' or 'syntax' in case some
definition files for syntax and/or filetype are missing? Or rely on ft
only?

I wouldn't put in modelines settings redundant with what is already set in the relevant $VIMRUNTIME/ftplugin/*.vim script, and if there are settings I want to set for _all_ files of a given filetype, I would put them (on Unix) in the relevant ~/.vim/ftplugin/*.vim or (on Windows) in the relevant ~/vimfiles/ftplugin/*.vim (where I give the path in Vim language).

Thanks for reading and in the hope of some helpful reply
Alex

[1] https://www.ptxdist.org/

Best regards,
Tony.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: