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

delete lines from while loop

Expand Messages
  • Andrei Popescu
    Hello, I m trying to add new features to the po.vim plugin[1], first one being the removal of the previous msgid in fuzzy strings: #, fuzzy #| msgid #| The
    Message 1 of 6 , May 31, 2010
    • 0 Attachment
      Hello,

      I'm trying to add new features to the po.vim plugin[1], first one being
      the removal of the previous msgid in fuzzy strings:

      #, fuzzy
      #| msgid ""
      #| "The following disk access storage devices (DASD) are available. Please "
      #| "select each device you want to use one at a time."
      msgid ""
      "The following direct access storage devices (DASD) are available. Please "
      "select each device you want to use one at a time."
      msgstr ""
      "Următoarele Dispozitive de stocare cu acces la disc (DASD) sunt disponibile. "
      "Vă rugăm să alegeți pe rând fiecare dispozitiv pe care doriți să-l folosiți."

      So far I have:

      " Remove previous msgid and fuzzy description from the translation.
      if !hasmapto('<Plug>RemoveFuzzy')
      if gui
      imap <buffer> <unique> <S-F8> <Plug>RemoveFuzzy
      nmap <buffer> <unique> <S-F8> <Plug>RemoveFuzzy
      else
      imap <buffer> <unique> <LocalLeader>r <Plug>RemoveFuzzy
      nmap <buffer> <unique> <LocalLeader>r <Plug>RemoveFuzzy
      endif
      endif
      inoremap <buffer> <unique> <Plug>RemoveFuzzy <ESC>{vap:call <SID>RemoveFuzzy()<CR>gv<ESC>}i
      nnoremap <buffer> <unique> <Plug>RemoveFuzzy {vap:call <SID>RemoveFuzzy()<CR>gv<ESC>}

      fu! <SID>RemoveFuzzy() range
      for linenum in range(a:firstline, a:lastline)
      let line = getline(linenum)
      if line =~ '^#,.*fuzzy'
      exe "normal! dd"
      "call setline(linenum, substitute(line, '^.*$','',''))
      elseif line =~ '^#|\smsgid\s".*"$'
      exe "normal! dd"
      "call setline(linenum, substitute(line, '^.*$','',''))
      elseif line =~ '^#|\s".*"$'
      exe "normal! dd"
      "call setline(linenum, substitute(line, '^.*$','',''))
      endif
      endfor
      endf

      but it doesn't work and I don't understand why. As it is now it only
      deletes the empty line separating this "paragraph" from the previous
      one. I'm guessing the problem is with the "exe" command, because
      replacing it with the commented out "call" the lines are emptied as
      expected.

      I also tried other variations, like

      exe ":delete"
      exe "1d"

      There must be a detail I'm missing, but I read the docs and searched the
      web and couldn't find the answer. Any hints?

      [1] http://www.vim.org/scripts/script.php?script_id=695

      Regards,
      Andrei
      --
      Offtopic discussions among Debian users and developers:
      http://lists.alioth.debian.org/mailman/listinfo/d-community-offtopic
    • Christian Brabandt
      Hi Andrei! ... I am not sure, I understand your problem. Anyway, your function has one problem. You are deleting lines (including the end-of-line), so you
      Message 2 of 6 , May 31, 2010
      • 0 Attachment
        Hi Andrei!

        On Mo, 31 Mai 2010, Andrei Popescu wrote:

        > Hello,
        >
        > I'm trying to add new features to the po.vim plugin[1], first one being
        > the removal of the previous msgid in fuzzy strings:
        >
        > #, fuzzy
        > #| msgid ""
        > #| "The following disk access storage devices (DASD) are available. Please "
        > #| "select each device you want to use one at a time."
        > msgid ""
        > "The following direct access storage devices (DASD) are available. Please "
        > "select each device you want to use one at a time."
        > msgstr ""
        > "Următoarele Dispozitive de stocare cu acces la disc (DASD) sunt disponibile. "
        > "Vă rugăm să alegeți pe rând fiecare dispozitiv pe care doriți să-l folosiți."
        >
        > So far I have:
        >
        > " Remove previous msgid and fuzzy description from the translation.
        > if !hasmapto('<Plug>RemoveFuzzy')
        > if gui
        > imap <buffer> <unique> <S-F8> <Plug>RemoveFuzzy
        > nmap <buffer> <unique> <S-F8> <Plug>RemoveFuzzy
        > else
        > imap <buffer> <unique> <LocalLeader>r <Plug>RemoveFuzzy
        > nmap <buffer> <unique> <LocalLeader>r <Plug>RemoveFuzzy
        > endif
        > endif
        > inoremap <buffer> <unique> <Plug>RemoveFuzzy <ESC>{vap:call <SID>RemoveFuzzy()<CR>gv<ESC>}i
        > nnoremap <buffer> <unique> <Plug>RemoveFuzzy {vap:call <SID>RemoveFuzzy()<CR>gv<ESC>}
        >
        > fu! <SID>RemoveFuzzy() range
        > for linenum in range(a:firstline, a:lastline)
        > let line = getline(linenum)
        > if line =~ '^#,.*fuzzy'
        > exe "normal! dd"
        > "call setline(linenum, substitute(line, '^.*$','',''))
        > elseif line =~ '^#|\smsgid\s".*"$'
        > exe "normal! dd"
        > "call setline(linenum, substitute(line, '^.*$','',''))
        > elseif line =~ '^#|\s".*"$'
        > exe "normal! dd"
        > "call setline(linenum, substitute(line, '^.*$','',''))
        > endif
        > endfor
        > endf

        I am not sure, I understand your problem. Anyway, your function has one
        problem. You are deleting lines (including the end-of-line), so you
        after the exe "normal! dd" command your file has actually one line less
        then before. And the next time you run getline(linenum) you get actually
        the wrong line. (which would now be equivalent to linenum+1 and each
        time you do normal! dd you add another one)


        regards,
        Christian
        --
        Was ist der Unterschied zwischen Männern und Schweinen?
        Schweine verwandeln sich nicht in Männer, wenn sie betrunken sind.

        --
        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
      • Andrei Popescu
        ... Thanks, I have rewritten the function like this: fu! RemoveFuzzy() range for linenum in range(a:firstline, a:lastline) let line = getline(linenum) if
        Message 3 of 6 , Jun 1, 2010
        • 0 Attachment
          On Lu, 31 mai 10, 23:09:58, Christian Brabandt wrote:
          > Hi Andrei!
          >
          > I am not sure, I understand your problem. Anyway, your function has one
          > problem. You are deleting lines (including the end-of-line), so you
          > after the exe "normal! dd" command your file has actually one line less
          > then before. And the next time you run getline(linenum) you get actually
          > the wrong line. (which would now be equivalent to linenum+1 and each
          > time you do normal! dd you add another one)

          Thanks, I have rewritten the function like this:

          fu! <SID>RemoveFuzzy() range
          for linenum in range(a:firstline, a:lastline)
          let line = getline(linenum)
          if line =~ '^#,\sfuzzy$'
          let startline = linenum
          let endline = linenum
          elseif line =~ '^#|\s.*".*"$'
          let endline = endline + 1
          endif
          endfor
          exe startline.",".endline."d"
          endf

          This works because .po files have a strict format I can rely on, but I
          have a feeling it could be shorter...

          Regards,
          Andrei
          --
          http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
        • Tom Link
          ... You might want to delete the lines in reverse order, i.e. from the bottom to the top. I also wonder if you could make use of the :g command. Regards, Tom
          Message 4 of 6 , Jun 1, 2010
          • 0 Attachment
            >    for linenum in range(a:firstline, a:lastline)

            You might want to delete the lines in reverse order, i.e. from the
            bottom to the top. I also wonder if you could make use of the :g
            command.

            Regards,
            Tom

            --
            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
          • Andrei Popescu
            ... Tried that, but couldn t find the right incantation, this is my first try at vimscript ;) ... Thanks a lot, :global is perfect! I m hitting another issue
            Message 5 of 6 , Jun 2, 2010
            • 0 Attachment
              On Ma, 01 iun 10, 21:32:01, Tom Link wrote:
              > >    for linenum in range(a:firstline, a:lastline)
              >
              > You might want to delete the lines in reverse order, i.e. from the
              > bottom to the top.

              Tried that, but couldn't find the right incantation, this is my first
              try at vimscript ;)

              > I also wonder if you could make use of the :g
              > command.

              Thanks a lot, :global is perfect! I'm hitting another issue now, with a
              part of code not included so far (hence the subject change). Now I have:

              fu! <SID>RemoveFuzzy() range
              execute ':''<,''>global/^#,\sfuzzy$\|^#|\s.*".*"$/d'
              for linenum in range(a:firstline, a:lastline)
              let line = getline(linenum)
              if line =~ '^#,\(.*,\)\=\s*fuzzy'
              call setline(linenum, substitute(line, ',\s*fuzzy', '', ""))
              endif
              endfor
              endf

              I tried (no pun intended) to replace the 'for' loop with:

              try
              execute ':''<,''>global/^#,\(.*,\)\=\s*fuzzy/s/,\s*fuzzy//'
              catch /^Vim\%((\a\+)\)\=:E486/ " based on example from :help catch
              endtry

              but I still get:

              E486: Pattern not found: ^#,\(.*,\)\=\s*fuzzy

              I can work around it by reversing the order, let the substitute global
              do its work and then remove any '^#$' lines left behind in the deleting
              global, but according to :help it should work.

              Regards,
              Andrei
              --
              http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
            • Andrei Popescu
              [sorry for replying to myself] ... Much simpler: execute : s/, s*fuzzy//e but I m still curious why the :catch doesn t work... Regards, Andrei --
              Message 6 of 6 , Jun 2, 2010
              • 0 Attachment
                [sorry for replying to myself]

                On Mi, 02 iun 10, 11:32:03, Andrei Popescu wrote:
                >
                > fu! <SID>RemoveFuzzy() range
                > execute ':''<,''>global/^#,\sfuzzy$\|^#|\s.*".*"$/d'
                > for linenum in range(a:firstline, a:lastline)
                > let line = getline(linenum)
                > if line =~ '^#,\(.*,\)\=\s*fuzzy'
                > call setline(linenum, substitute(line, ',\s*fuzzy', '', ""))
                > endif
                > endfor
                > endf
                >
                > I tried (no pun intended) to replace the 'for' loop with:
                >
                > try
                > execute ':''<,''>global/^#,\(.*,\)\=\s*fuzzy/s/,\s*fuzzy//'
                > catch /^Vim\%((\a\+)\)\=:E486/ " based on example from :help catch
                > endtry
                >
                > but I still get:
                >
                > E486: Pattern not found: ^#,\(.*,\)\=\s*fuzzy

                Much simpler:

                execute ':''<,''>s/,\s*fuzzy//e'

                but I'm still curious why the :catch doesn't work...

                Regards,
                Andrei
                --
                http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
              Your message has been successfully submitted and would be delivered to recipients shortly.