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

`match()` with branches

Expand Messages
  • Nico Weber
    Hi, `:echo match( ip , i |p )` results in -1 (ie. no match). I d expect that I get 0 as the pattern matches right at the start of the string ( i is i
    Message 1 of 6 , Dec 1, 2007
    • 0 Attachment
      Hi,

      `:echo match("ip", "i\|p")` results in "-1" (ie. no match). I'd expect
      that I get "0" as the pattern matches right at the start of the string
      ("i" is "i" or "p"). The documentation for `match()` claims that
      ordinary regex patterns with 'magic' set are accepted. Everything
      except branches seems to work (I tried character classes and
      wildcards), but the above does not. Is this a bug, or am I missing
      something?

      Thanks,
      Nico

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_dev" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Bill McCarthy
      ... If you choose to place the regex in a string, you ll need to ... or ... -- Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You
      Message 2 of 6 , Dec 1, 2007
      • 0 Attachment
        On Sat 1-Dec-07 5:23am -0600, Nico Weber wrote:

        > `:echo match("ip", "i\|p")` results in "-1" (ie. no match). I'd expect
        > that I get "0" as the pattern matches right at the start of the string
        > ("i" is "i" or "p"). The documentation for `match()` claims that
        > ordinary regex patterns with 'magic' set are accepted. Everything
        > except branches seems to work (I tried character classes and
        > wildcards), but the above does not. Is this a bug, or am I missing
        > something?

        If you choose to place the regex in a string, you'll need to
        escape that '\' character. Use either:

        :echo match("ip", "i\\|p")
        or
        :echo match("ip", 'i\|p')

        --
        Best regards,
        Bill


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_dev" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Nico Weber
        ... Thanks :-) Attached is a patch to filetype.vim that adds objc detection for .h files. I don t know if this is of general interest, but I think so. Nico
        Message 3 of 6 , Dec 1, 2007
        • 0 Attachment
          > If you choose to place the regex in a string, you'll need to
          > escape that '\' character. Use either:
          >
          > :echo match("ip", "i\\|p")
          > or
          > :echo match("ip", 'i\|p')

          Thanks :-)

          Attached is a patch to filetype.vim that adds objc detection for .h
          files. I don't know if this is of general interest, but I think so.

          Nico


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_dev" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Bram Moolenaar
          ... I wonder how often the detection will fail. @interface and @end could appear in a comment of a C or C++ file, at least. Esp. when using something
          Message 4 of 6 , Dec 2, 2007
          • 0 Attachment
            Nico Weber wrote:

            > Attached is a patch to filetype.vim that adds objc detection for .h
            > files. I don't know if this is of general interest, but I think so.

            I wonder how often the detection will fail. "@interface" and "@end"
            could appear in a comment of a C or C++ file, at least. Esp. when using
            something like doxygen.

            Is there any way to make it a bit more specific?

            The check could be turned into a one liner:

            " .h files can be C, Ch C++, ObjC or ObjC++.
            " Set c_syntax_for_h if you want C, ch_syntax_for_h if you want Ch. ObjC is
            " detected automatically.
            au BufNewFile,BufRead *.h call s:FTheader()

            func! s:FTheader()
            if match(getline(1, min([line("$"), 200])), '@interface\|@end') > -1
            setf objc
            elseif exists("c_syntax_for_h")
            setf c
            elseif exists("ch_syntax_for_h")
            setf ch
            else
            setf cpp
            endif
            endfunc


            --
            hundred-and-one symptoms of being an internet addict:
            169. You hire a housekeeper for your home page.

            /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
            /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
            \\\ download, build and distribute -- http://www.A-A-P.org ///
            \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_dev" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Nico Weber
            ... What about ^@interface |^@end (and maybe throw in a |^@class at the end for good measure)? ... If we add the ^ s and |^@class , this line would be
            Message 5 of 6 , Dec 2, 2007
            • 0 Attachment
              > I wonder how often the detection will fail. "@interface" and "@end"
              > could appear in a comment of a C or C++ file, at least. Esp. when
              > using
              > something like doxygen.

              What about '^@interface\|^@end' (and maybe throw in a '\|^@class' at
              the end for good measure)?

              > The check could be turned into a one liner:
              >
              > " .h files can be C, Ch C++, ObjC or ObjC++.
              > " Set c_syntax_for_h if you want C, ch_syntax_for_h if you want Ch.
              > ObjC is
              > " detected automatically.
              > au BufNewFile,BufRead *.h call s:FTheader()
              >
              > func! s:FTheader()
              > if match(getline(1, min([line("$"), 200])), '@interface\|@end') > -1

              If we add the '^'s and '\|^@class', this line would be > 80 chars.
              >
              > setf objc
              > elseif exists("c_syntax_for_h")
              > setf c
              > elseif exists("ch_syntax_for_h")
              > setf ch
              > else
              > setf cpp
              > endif
              > endfunc

              Nico

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_dev" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Bram Moolenaar
              ... That s a good restriction. Suppose a .h file is C++ and recognized as objective C, how bad would the effect be? I assume that objective C is quite similar
              Message 6 of 6 , Dec 2, 2007
              • 0 Attachment
                Nico Weber wrote:

                > > I wonder how often the detection will fail. "@interface" and "@end"
                > > could appear in a comment of a C or C++ file, at least. Esp. when
                > > using
                > > something like doxygen.
                >
                > What about '^@interface\|^@end' (and maybe throw in a '\|^@class' at
                > the end for good measure)?

                That's a good restriction.

                Suppose a .h file is C++ and recognized as objective C, how bad would
                the effect be? I assume that objective C is quite similar to C++.

                There are a few mistakes, you can use this to try out:

                http://www.google.com/codesearch?hl=en&q=+file:%5C.h+%5E%40(interface%7Cclass%7Cend)&sa=N

                Note that you need to look at the file to verify the type. Google Code
                Search also makes mistakes.

                --
                hundred-and-one symptoms of being an internet addict:
                173. You keep tracking down the email addresses of all your friends
                (even childhood friends).

                /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                \\\ download, build and distribute -- http://www.A-A-P.org ///
                \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_dev" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              Your message has been successfully submitted and would be delivered to recipients shortly.