Loading ...
Sorry, an error occurred while loading the content.
 

Re: filetype detection within files

Expand Messages
  • Tony Mechelynck
    ... To know which filetype has been detected for the current editfile, see ... if &filetype == no filetype has been detected (NoName buffer?) elseif
    Message 1 of 5 , Dec 5, 2012
      On 05/12/12 09:09, Marc Weber wrote:
      > There are quite a lot of files supporting sub filetypes such as
      > .haml
      > .php
      > and whatnot
      >
      > Some plugins such as snippet and commenting plugins could benefit from
      > knowing which filetype the current cursor is at.
      > Do any implemantions of this beside the one contained in tcomment
      > already exist?
      >
      > Marc Weber
      >

      To know which 'filetype' has been detected for the current editfile, see
      :help expr-option

      if &filetype == ""
      " no filetype has been detected (NoName buffer?)
      elseif &filetype == "php"
      " do something
      elseif &filetype == "phtml"
      " do something else
      else
      " this is probably not PHP
      endif

      Or did I misunderstand your question? If I did, please explain more clearly.


      Best regards,
      Tony.
      --
      User: I'm having problems with my text editor.
      Help desk: Which editor are you using?
      User: I don't know, but it's version VI (pronounced: 6).
      Help desk: Oh, then you should upgrade to version VIM (pronounced: 994).

      --
      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
    • Marc Weber
      I know about &ft. Talking about == a .html file == CURSOR_HERE == Now I want a function which returnns js if cursor is
      Message 2 of 5 , Dec 5, 2012
        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.

        Marc Weber

        --
        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
      • Tony Mechelynck
        ... Ah, that doesn t look so easy. Maybe by examining the syntax group at the cursor position and checking the part of the group name before the first
        Message 3 of 5 , Dec 5, 2012
          On 05/12/12 18:23, 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.
          >
          > Marc Weber
          >

          Ah, that doesn't look so easy.

          Maybe by examining the syntax group at the cursor position and checking
          the part of the group name before the first uppercase letter? You would
          of course need to have syntax highlighting enabled (and compiled-in).

          See
          :help synID()
          :help synIDattr()
          :help synIDtrans()

          There are however places (such as in "ordinary" HTML text) where no
          syntax group is defined, defaulting to the Normal highlight.

          Also, in the value of a style= attribute within an HTML tag, the syntax
          group is htmlString (highlight linked to Constant), not anything
          CSS-related.


          Best regards,
          Tony.
          --
          "Beware of the man who works hard to learn something, learns it, and
          finds himself no wiser than before," Bokonon tells us. "He is full of
          murderous resentment of people who are ignorant without having come by
          their ignorance the hard way."
          -- Kurt Vonnegut, "Cat's Cradle"

          --
          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
        • Charles Campbell
          ... 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
          Message 4 of 5 , Dec 6, 2012
            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
          Your message has been successfully submitted and would be delivered to recipients shortly.