Thursday, December 6, 2012

Re: filetype detection within files

Marc Weber wrote:
> I know about &ft.
>
> Talking about
>
> == a .html file ==
>
> <script type="javaoscript">
> CURSOR_HERE
> </script>
>
> ==
>
> Now I want a function which returnns "js" if cursor is in such a
> javascript section. That would be useful for both: commenting plugins
> and snippet plugins.
>
> Same for any kind of "nested" languages such as php, haml, jsp etc.

If I understand correctly, you want to know the currently active
effective filetype when a syntax group has been "included" in.

For example, from html:

syn include @htmlJavaScript syntax/javascript.vim
syn region javaScript start=+<script[^>]*>+ keepend
end=+</script>+me=s-1
contains=@htmlJavaScript,htmlCssStyleComment,htmlScriptTag,@htmlPreproc

which interprets the <script type="javascript">... snippet above.

In your example, the following vimL...

let idlist = synstack(line("."),col("."))
for id in idlist
echo synIDattr(id,"name")
endfor

will show "javaScript". Any syntax inclusion must be in a region. You
could manually extract a list of such names and set up a dictionary
mapping them to the filetypes you want (ie. converting javaScript ->
javascript (js is just one of several suffices that map to a filetype of
javascript). Alternatively, you could have something crawl through the
syntax files and build such a list, but I'd think that'd be
unnecessarily time consuming (as the syntax-include region names are
likely to be fairly static).

Regards,
C Campbell





--
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

No comments: