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

insert space after comma based on context

Expand Messages
  • Zhang Le
    Hi, Most of time I want a space after a comma, so I use imap , , The problem is, sometime I do not want a comma inside square brackets in some programming
    Message 1 of 7 , Jun 29, 2006
    • 0 Attachment
      Hi,
      Most of time I want a space after a comma, so I use "imap , , "
      The problem is, sometime I do not want a comma inside square
      brackets in some programming language such python and matlab:
      a[10,:] or a(10,:)

      Is there a way to not insert a space based on context around the
      cursor so that if the text before cursor is [xxx, or (xxx, no space
      will be inserted?
      Any tips?
      Zhang Le
    • Luc Hermitte
      Hello, ... I see two ways: 1- The first is to rely on ftplugins and :map- . This way, you can specify mappings for specific filetypes. However, it won t
      Message 2 of 7 , Jun 29, 2006
      • 0 Attachment
        Hello,

        * On Thu, Jun 29, 2006 at 02:45:16PM +0100, Zhang Le <zhang.le.misc@...> wrote:
        > Most of time I want a space after a comma, so I use "imap , , "
        > The problem is, sometime I do not want a comma inside square
        > brackets in some programming language such python and matlab:
        > a[10,:] or a(10,:)
        >
        > Is there a way to not insert a space based on context around the
        > cursor so that if the text before cursor is [xxx, or (xxx, no space
        > will be inserted?

        I see two ways:
        1- The first is to rely on ftplugins and :map-<buffer>. This way, you
        can specify mappings for specific filetypes. However, it won't be able
        to distinguish normal commas, from commas used in arrays.

        2- The second is to define context dependent mapping. You can search for
        the discussion I had with Dr Bean last week on this topic.
        You can search for the following Message-ID on gmane:
        <20060624160651.GD6748@...>

        He used the function InsertIfNotAfter() I define in my C++ ftplugin
        cpp_set.vim available in lh-cpp.tar.gz [1].
        In your case, I guess it will look like:

        inoremap <silent> ,
        \ <c-r>=InsertIfNotAfter(',', ', ', '[[(][^]]\+')<cr>

        HTH,


        [1] http://hermitte.free.fr/vim/ressources/lh-cpp.tar.gz
        --
        Luc Hermitte
        http://hermitte.free.fr/vim/
      • Dr Bean
        ... Just for my own benefit, and to refresh my memory about what this means: It s a mapping to add a space after the comma, but only when the comma is not
        Message 3 of 7 , Jun 29, 2006
        • 0 Attachment
          On Fri, 30 Jun 2006, Luc Hermitte wrote:

          > * On Thu, Jun 29, 2006 at 02:45:16PM +0100, Zhang Le <zhang.le.misc@...> wrote:
          > > Most of time I want a space after a comma, so I use "imap , , "
          > > The problem is, sometime I do not want a comma inside square
          > > brackets in some programming language such python and matlab:
          > > a[10,:] or a(10,:)

          > > Is there a way to not insert a space based on context around the
          > > cursor so that if the text before cursor is [xxx, or (xxx, no space
          > > will be inserted?

          > I see two ways:

          > 2- The second is to define context dependent mapping. You can search for
          > the discussion I had with Dr Bean last week on this topic.
          > You can search for the following Message-ID on gmane:
          > <20060624160651.GD6748@...>

          > He used the function InsertIfNotAfter() I define in my C++ ftplugin
          > cpp_set.vim available in lh-cpp.tar.gz [1].
          > In your case, I guess it will look like:

          > inoremap <silent> ,
          > \ <c-r>=InsertIfNotAfter(',', ', ', '[[(][^]]\+')<cr>

          Just for my own benefit, and to refresh my memory about what this means:

          It's a mapping to add a space after the comma, but only when the
          comma is not preceded by something the regex matches.

          What the regex matches is an opening bracket or parenthesis followed by
          a number of non-closing-bracket characters.

          Shouldn't the closing parenthesis be included also in the regex?

          '[[(][^])]\+'

          --
          Dr Bean People don't know what they say or
          what they think they ought to say.
          --Leonard Bloomfield, quoted by Virginia McDavid
        • Luc Hermitte
          Hello, ... I must admit I never remember which parameter is expanded when. If the pattern is matched, we expand the first parameter. The second otherwise. ...
          Message 4 of 7 , Jun 30, 2006
          • 0 Attachment
            Hello,

            * On Fri, Jun 30, 2006 at 09:03:57AM +0800, Dr Bean <lang@...> wrote:
            > > > Is there a way to not insert a space based on context around the
            > > > cursor so that if the text before cursor is [xxx, or (xxx, no space
            > > > will be inserted?
            >
            > > He used the function InsertIfNotAfter() I define in my C++ ftplugin
            > > cpp_set.vim available in lh-cpp.tar.gz [1].
            > > In your case, I guess it will look like:
            >
            > > inoremap <silent> ,
            > > \ <c-r>=InsertIfNotAfter(',', ', ', '[[(][^]]\+')<cr>
            >
            > Just for my own benefit, and to refresh my memory about what this
            > means:
            >
            > It's a mapping to add a space after the comma, but only when the comma
            > is not preceded by something the regex matches.

            I must admit I never remember which parameter is expanded when.
            If the pattern is matched, we expand the first parameter. The second
            otherwise.

            > What the regex matches is an opening bracket or parenthesis followed
            > by a number of non-closing-bracket characters.
            >
            > Shouldn't the closing parenthesis be included also in the regex?
            > '[[(][^])]\+'

            Of course. You're right.

            --
            Luc Hermitte
            http://hermitte.free.fr/vim/
          • Dr Bean
            ... The function, InsertIfNotAfter, is defined as function! InsertIfNotAfter(key, what, pattern) let c = col( . ) - 1 let l = getline( . ) let l = strpart(l,
            Message 5 of 7 , Jul 1, 2006
            • 0 Attachment
              On Fri, 30 Jun 2006, Luc Hermitte wrote:

              > Hello,

              > > > He used the function InsertIfNotAfter() I define in my C++ ftplugin
              > > > cpp_set.vim available in lh-cpp.tar.gz [1].
              > > > In your case, I guess it will look like:

              > > > inoremap <silent> ,
              > > > \ <c-r>=InsertIfNotAfter(',', ', ', '[[(][^]]\+')<cr>

              > > It's a mapping to add a space after the comma, but only when the comma
              > > is not preceded by something the regex matches.

              > I must admit I never remember which parameter is expanded when.
              > If the pattern is matched, we expand the first parameter. The second
              > otherwise.

              The function, InsertIfNotAfter, is defined as

              function! InsertIfNotAfter(key, what, pattern)
              let c = col('.') - 1
              let l = getline('.')
              let l = strpart(l, 0, c)
              if l =~ a:pattern.'\s*$'
              return a:key
              else
              return Def_AbbrC(a:key, a:what)
              endif
              endfunction

              I thought that if a:pattern matches, then a:key is not expanded.
              And if it is not matched, it is expanded, to a:what.

              Oh, wait, I see. This function is part of a mapping. As you said,
              if the pattern matches, the comma is expanded to whatever the
              first parameter of InsertIfNotAfter is. If it doesn't match, it
              is expanded to the second parameter of InsertIfNotAfter.

              --
              Dr Bean More Pricks than Kicks.
              --Samuel Beckett
            • Vigil
              You can also ctrl-v, which will prevent the , map. ... -- .
              Message 6 of 7 , Jul 3, 2006
              • 0 Attachment
                You can also ctrl-v, which will prevent the , map.

                On Thu, 29 Jun 2006, Zhang Le wrote:

                > Hi,
                > Most of time I want a space after a comma, so I use "imap , , "
                > The problem is, sometime I do not want a comma inside square
                > brackets in some programming language such python and matlab:
                > a[10,:] or a(10,:)
                >
                > Is there a way to not insert a space based on context around the
                > cursor so that if the text before cursor is [xxx, or (xxx, no space
                > will be inserted?
                > Any tips?
                > Zhang Le
                >

                --

                .
              • A.J.Mechelynck
                ... I suppose there is: see :help pattern.txt Then maybe you ll want not to replace it between digits, as in 123,456,789.01 (decimal notation with thousands,
                Message 7 of 7 , Jul 14, 2006
                • 0 Attachment
                  Zhang Le wrote:
                  > Hi,
                  > Most of time I want a space after a comma, so I use "imap , , "
                  > The problem is, sometime I do not want a comma inside square
                  > brackets in some programming language such python and matlab:
                  > a[10,:] or a(10,:)
                  >
                  > Is there a way to not insert a space based on context around the
                  > cursor so that if the text before cursor is [xxx, or (xxx, no space
                  > will be inserted?
                  > Any tips?
                  > Zhang Le
                  >
                  >

                  I suppose there is: see :help pattern.txt

                  Then maybe you'll want not to replace it between digits, as in
                  123,456,789.01 (decimal notation with thousands, Anglo-Saxon style) or
                  123.456.789,01 (the same, Latin style). (BTW, I don't know whether the
                  Chinese people use a comma or a period for the decimal point, and I'm
                  not sure whether they separate thousands, millions, etc., or maybe
                  tens-of-thousands [aka myriads], myriads of myriads, etc., in large
                  numbers written with the characters 0123456789).


                  Best regards,
                  Tony.
                Your message has been successfully submitted and would be delivered to recipients shortly.