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

search for next occurence that is not commented out

Expand Messages
  • Eric Smith
    How do I find the next occurance of a pattern which is not preceeded in the same line by a comment token? The identity of the comment token should be derived
    Message 1 of 5 , Nov 19, 2011
      How do I find the next occurance of a pattern which is not preceeded
      in the same line by a comment token? The identity of the comment token
      should be derived from the current filetype.

      (Without rollng and mintaining my own recipes using \@<! )

      --
      - Eric Smith

      --
      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
    • Tim Chase
      ... A couple ideas occur to me, but each involves merely automating the addition of @
      Message 2 of 5 , Nov 19, 2011
        On 11/19/11 05:05, Eric Smith wrote:
        > How do I find the next occurance of a pattern which is not
        > preceeded in the same line by a comment token? The identity of
        > the comment token should be derived from the current
        > filetype.
        >
        > (Without rollng and mintaining my own recipes using \@<! )

        A couple ideas occur to me, but each involves merely automating
        the addition of "\@<!" or "\@!". I don't know where you're
        getting your comment leader, but my stock vim doesn't have the
        info specific to given files (just generic info in 'comments'
        which doesn't seem to adjust for file-type). For the below, I'm
        assuming you can access that information in the variable
        "b:comment_leader", but you'd have to adjust accordingly.

        The first way that came to mind was to map something that
        auto-inserted the comment-leader into a search pattern when you
        tried to search, saving you the trouble:

        :nnoremap <f4> /\%(^\s*<c-r>=escape(b:comment_leader,
        '*\./')<cr>.*\)\@<!

        which would start a forward-search, insert (assuming your
        comment_leader was "#")

        /\%(^\s*#.*\)\@<!

        whereupon you could begin your search query.

        The alternative would be to have something that added that to the
        beginning of an existing search, something like

        :nnoremap <f4> :let @/='\%(^\s*'.escape(b:comment_leader,
        '.').'.*\)\@<!'.@/<cr>

        So you could search for text, then hit <f4> and continue to use
        n/N to search forwards/backwards excluding the comment-leader.

        Nothing stops you from foolishly running the 2nd one multiple
        times and creating a uselessly redundant pattern.

        Hope this gives you some hacks to get closer to what you want.

        -tim


        --
        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
      • Andy Wokula
        ... I think in place of b:comment_leader, you can (rather) almost count on ... Not a complete solution, just one step further. -- Andy -- You received this
        Message 3 of 5 , Nov 19, 2011
          Am 19.11.2011 13:27, schrieb Tim Chase:
          > On 11/19/11 05:05, Eric Smith wrote:
          >> How do I find the next occurance of a pattern which is not
          >> preceeded in the same line by a comment token? The identity of
          >> the comment token should be derived from the current
          >> filetype.
          >>
          >> (Without rollng and mintaining my own recipes using \@<! )
          >
          > A couple ideas occur to me, but each involves merely automating
          > the addition of "\@<!" or "\@!". I don't know where you're
          > getting your comment leader, but my stock vim doesn't have the
          > info specific to given files (just generic info in 'comments'
          > which doesn't seem to adjust for file-type). For the below, I'm
          > assuming you can access that information in the variable
          > "b:comment_leader", but you'd have to adjust accordingly.
          >
          > The first way that came to mind was to map something that
          > auto-inserted the comment-leader into a search pattern when you
          > tried to search, saving you the trouble:
          >
          > :nnoremap <f4> /\%(^\s*<c-r>=escape(b:comment_leader, '*\./')<cr>.*\)\@<!
          >
          > which would start a forward-search, insert (assuming your
          > comment_leader was "#")
          >
          > /\%(^\s*#.*\)\@<!
          >
          > whereupon you could begin your search query.
          >
          > The alternative would be to have something that added that to the
          > beginning of an existing search, something like
          >
          > :nnoremap <f4> :let @/='\%(^\s*'.escape(b:comment_leader, '.').'.*\)\@<!'.@/<cr>
          >
          > So you could search for text, then hit <f4> and continue to use n/N to
          > search forwards/backwards excluding the comment-leader.
          >
          > Nothing stops you from foolishly running the 2nd one multiple times
          > and creating a uselessly redundant pattern.
          >
          > Hope this gives you some hacks to get closer to what you want.
          >
          > -tim

          I think in place of b:comment_leader, you can (rather) almost count on
          'cms'/'commentstring':

          :let comment_leader = substitute(split(&cms, '%s')[0], '^\s*', '', '')
          :let comlead_pat = escape(comment_leader, '\.*$^~[')

          Not a complete solution, just one step further.

          --
          Andy

          --
          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
        • Tim Chase
          ... I m not sure how reliable that is either, as I just opened a .py file and the cms was set to /*%s*/ which isn t a valid comment leader for Python.
          Message 4 of 5 , Nov 19, 2011
            On 11/19/11 07:32, Andy Wokula wrote:
            > Am 19.11.2011 13:27, schrieb Tim Chase:
            >> On 11/19/11 05:05, Eric Smith wrote:
            >>> How do I find the next occurance of a pattern which is not
            >>> preceeded in the same line by a comment token? The identity of
            >>> the comment token should be derived from the current
            >>> filetype.
            >>
            >> For the below, I'm
            >> assuming you can access that information in the variable
            >> "b:comment_leader"
            >
            > I think in place of b:comment_leader, you can (rather) almost count on
            > 'cms'/'commentstring':

            I'm not sure how reliable that is either, as I just opened a .py
            file and the 'cms' was set to "/*%s*/" which isn't a valid
            comment leader for Python.

            Additionally, some C-like languages support blockwise comments
            (such as "/* ... */" or even "#ifdef 0 ... #endif") in addition
            to line-wise comments (using "//"), and it sounds like the OP
            wants to be able to insert "//" for C-like languages, "#" for
            many scripting-type languages (bash, Python, Perl perhaps, etc?),
            or a double-quote for Vim scripts, etc.

            Nothing in my Vim seems to set those upon FT detection, though
            that may be because (1) I'm running 7.2 which comes stock on
            Debian Stable or (2) I don't have some aspect of ft-detection
            turned on.

            -tim


            --
            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
          • Andy Wokula
            ... Right, it s not as common as I thought. I just remembered a few plugins (commenter plugins like tComment ...) making use of that option. Now I see that
            Message 5 of 5 , Dec 2, 2011
              Am 19.11.2011 15:13, schrieb Tim Chase:

              > On 11/19/11 07:32, Andy Wokula wrote:

              >> Am 19.11.2011 13:27, schrieb Tim Chase:

              >>> On 11/19/11 05:05, Eric Smith wrote:

              >>>> How do I find the next occurance of a pattern which is not

              >>>> preceeded in the same line by a comment token? The identity of

              >>>> the comment token should be derived from the current

              >>>> filetype.

              >>>

              >>> For the below, I'm assuming you can access that information in the

              >>> variable "b:comment_leader"

              >>

              >> I think in place of b:comment_leader, you can (rather) almost count on

              >> 'cms'/'commentstring':

              >

              > I'm not sure how reliable that is either, as I just opened a .py file

              > and the 'cms' was set to "/*%s*/" which isn't a valid comment leader

              > for Python.

              >

              > Additionally, some C-like languages support blockwise comments (such

              > as "/* ... */" or even "#ifdef 0 ... #endif") in addition to line-wise

              > comments (using "//"), and it sounds like the OP wants to be able to

              > insert "//" for C-like languages, "#" for many scripting-type

              > languages (bash, Python, Perl perhaps, etc?), or a double-quote for

              > Vim scripts, etc.

              >

              > Nothing in my Vim seems to set those upon FT detection, though that

              > may be because (1) I'm running 7.2 which comes stock on Debian Stable

              > or (2) I don't have some aspect of ft-detection turned on.

              >

              > -tim



              Right, it's not as common as I thought. I just remembered a few plugins

              (commenter plugins like tComment ...) making use of that option. Now I

              see that they "try to make use of it" -- they don't "rely" on it, ok.


              --

              Andy

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