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

vim7: bug - NL, substitute() and submatch()

Expand Messages
  • Mikolaj Machowski
    Hello, Cannot place new line character into string using substitute() and submatch() combination. When working on string asdf1234asdf
    Message 1 of 7 , Jun 3, 2005
    • 0 Attachment
      Hello,

      Cannot place new line character into string using substitute() and
      submatch() combination.

      When working on string
      asdf1234asdf
      s/\d*/\=substitute(submatch(0), '.', '\n', 'g')/

      works as intended returning
      asdf



      asdf

      But when doing
      let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0), ".",
      "\n", "g")')
      echo a
      asdfasdf

      For all regular characters it works. Number of \ or \r or ^M doesn't
      help.

      m.

      --
      LaTeX + Vim = http://vim-latex.sourceforge.net/
      Vim-list(s) Users Map: (last change 15 May)
      http://skawina.eu.org/mikolaj/vimlist
      CLEWN - http://clewn.sf.net
    • A. J. Mechelynck
      ... To break a line using :substitute, you need to insert r , not n . One of the implications of this is that (IIUC) ... actually counts line breaks,
      Message 2 of 7 , Jun 3, 2005
      • 0 Attachment
        Mikolaj Machowski wrote:
        > Hello,
        >
        > Cannot place new line character into string using substitute() and
        > submatch() combination.
        >
        > When working on string
        > asdf1234asdf
        > s/\d*/\=substitute(submatch(0), '.', '\n', 'g')/
        >
        > works as intended returning
        > asdf
        >
        >
        >
        > asdf
        >
        > But when doing
        > let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0), ".",
        > "\n", "g")')
        > echo a
        > asdfasdf
        >
        > For all regular characters it works. Number of \ or \r or ^M doesn't
        > help.
        >
        > m.
        >

        To break a line using :substitute, you need to insert "\r", not "\n".
        One of the implications of this is that (IIUC)

        :1,$s/\n/\r/g

        actually counts line breaks, replacing them by themselves (but it's not
        the most economical way to do that). That is one of the quirks of Vim; I
        believe that altering it would break too many existing scripts.

        Best regards,
        Tony.
      • Mikolaj Machowski
        ... Did you read what I wrote? Besides, this is about substitute() (where n is accepted), not ... m.
        Message 3 of 7 , Jun 3, 2005
        • 0 Attachment
          Dnia piątek 03 czerwiec 2005 20:52, A. J. Mechelynck napisał:
          > > But when doing
          > > let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0),
          > > ".", "\n", "g")')
          > > echo a
          > > asdfasdf
          > >
          > > For all regular characters it works. Number of \ or \r or ^M doesn't
          ^^^
          > > help.
          >
          > To break a line using :substitute, you need to insert "\r", not "\n".

          Did you read what I wrote?

          Besides, this is about substitute() (where \n is accepted), not
          :substitute.

          m.
        • A. J. Mechelynck
          ... substitute( asdf1234asdf , d* , =substitute(submatch(0), . , n , g ) ) E119: Not enough arguments for function: substitute E15: Invalid expression:
          Message 4 of 7 , Jun 4, 2005
          • 0 Attachment
            Mikolaj Machowski wrote:
            > Dnia piątek 03 czerwiec 2005 20:52, A. J. Mechelynck napisał:
            >
            >>>But when doing
            >>>let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0),
            >>>".", "\n", "g")')
            >>>echo a
            >>>asdfasdf
            >>>
            >>>For all regular characters it works. Number of \ or \r or ^M doesn't
            >
            > ^^^
            >
            >>>help.
            >>
            >>To break a line using :substitute, you need to insert "\r", not "\n".
            >
            >
            > Did you read what I wrote?
            >
            > Besides, this is about substitute() (where \n is accepted), not
            > :substitute.
            >
            > m.
            >
            >
            >
            >

            Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:

            :echo
            substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")')
            E119: Not enough arguments for function: substitute
            E15: Invalid expression:
            substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")')

            :echo
            substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")','g')
            asdf

            :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
            "'" . '\n' . "'" . ',"g")','g')
            asdf
            asdf

            In other words: it works for me, when I make sure that only single
            quotes are used around the \n


            Best regards,
            Tony.





            Best regards,
            Tony.
          • Mikolaj Machowski
            ... That is expected. substitute() requires flag element, even if empty. ... That is my problem. ... Doesn t work for me. Today CVS, Linux, Mandriva 10.2. Note
            Message 5 of 7 , Jun 4, 2005
            • 0 Attachment
              Dnia sobota 04 czerwiec 2005 19:41, A. J. Mechelynck napisał:
              >
              > Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:
              > :echo
              >
              > substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
              >) E119: Not enough arguments for function: substitute

              That is expected. substitute() requires flag element, even if empty.
              > :echo
              >
              > substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
              >,'g') asdf

              That is my problem.
              >
              > :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
              >
              > "'" . '\n' . "'" . ',"g")','g')
              > asdf
              > asdf
              >
              Doesn't work for me. Today CVS, Linux, Mandriva 10.2.

              Note that when replacing echo with echomsg result is:
              asdf^M^M^M^Masdf

              So, replacement was successful (even with Tony's second example equal to
              my try) just interpretation of character is borked.

              m.
            • A. J. Mechelynck
              ... [...] Replacing echo with echomsg in either my second or my third example gives me asdf^M^M^M^Masdf too. With ... etc., followed by ... I also get
              Message 6 of 7 , Jun 4, 2005
              • 0 Attachment
                Mikolaj Machowski wrote:
                > Dnia sobota 04 czerwiec 2005 19:41, A. J. Mechelynck napisał:
                >
                >>Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:
                >> :echo
                >>
                >>substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
                >>) E119: Not enough arguments for function: substitute
                >
                >
                > That is expected. substitute() requires flag element, even if empty.
                >
                >> :echo
                >>
                >>substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
                >>,'g') asdf
                >
                >
                > That is my problem.
                >
                >> :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
                >>
                >>"'" . '\n' . "'" . ',"g")','g')
                >>asdf
                >>asdf
                >>
                >
                > Doesn't work for me. Today CVS, Linux, Mandriva 10.2.
                >
                > Note that when replacing echo with echomsg result is:
                > asdf^M^M^M^Masdf
                [...]

                Replacing echo with echomsg in either my second or my third example
                gives me asdf^M^M^M^Masdf too. With

                :let @* = substitute(

                etc., followed by

                :reg

                I also get asdf^M^M^M^Masdf -- apparently the problem is with ":echo",
                not with substitute().


                Best regards,
                Tony.
              • Mikolaj Machowski
                ... No. echo and echomsg behaviour is correct. echo tries to interpret special characters and remove them when cannot do this. While echomsg show everything
                Message 7 of 7 , Jun 5, 2005
                • 0 Attachment
                  Dnia niedziela 05 czerwiec 2005 00:33, A. J. Mechelynck napisał:
                  > >
                  > > Doesn't work for me. Today CVS, Linux, Mandriva 10.2.
                  > >
                  > > Note that when replacing echo with echomsg result is:
                  > > asdf^M^M^M^Masdf
                  >
                  > [...]
                  >
                  > Replacing echo with echomsg in either my second or my third example
                  > gives me asdf^M^M^M^Masdf too. With
                  >
                  > :let @* = substitute(
                  >
                  > etc., followed by
                  >
                  > :reg
                  >
                  > I also get asdf^M^M^M^Masdf -- apparently the problem is with ":echo",
                  > not with substitute().

                  No. echo and echomsg behaviour is correct. echo tries to interpret
                  special characters and remove them when cannot do this. While echomsg
                  show everything without interpretation.

                  Try
                  echo "asdf\nasdf"
                  echomsg "asdf\nasdf"

                  What I posted is dumbed down example of real behaviour with assigning
                  variables and so on. Everywhere else \n was inserted properly as
                  newline. Here is passed as ^M on Unix in DOS-ended files. I think here
                  is solution of mystery. On MS-Windows ^M is treated as newline (part of
                  it?), while on Unix (Linux in this case) it is left as is. Somewhere
                  should be put test on 'fformat'.

                  m.
                Your message has been successfully submitted and would be delivered to recipients shortly.