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

Re: using :g/\(\w\+\)/ to feed python function?

Expand Messages
  • Reckoner
    ... ( @x );g=foo(i);current.line= %d;%d;%s===; %(g.order(),g.size(),i) it s ugly, but reliable. I agree we should have a pyeval or a special compilation of
    Message 1 of 5 , Sep 30, 2009
    • 0 Attachment
      Thanks for all the hints. The following seems to work:

      :g/^==/s/^==\+ \(\w\+\).*/\=setreg('x',submatch(1))/ | :py i=eval
      ('@x');g=foo(i);current.line='%d;%d;%s===;' %(g.order(),g.size(),i)

      it's ugly, but reliable. I agree we should have a pyeval or a special
      compilation of vim to more easily handle this kind of thing.

      Thanks!



      On Sep 29, 11:55 pm, "Christian Brabandt" <cbli...@...> wrote:
      > On Tue, September 29, 2009 11:55 pm, Reckoner wrote:
      > > Is it possible to do something like:
      >
      > > :g/\(\w+\) .*/python foo(\1)
      >
      > > and then have all the matching lines changed to the output of the
      > > python foo() function?
      >
      > > For example,
      >
      > > +--------------------------------
      > > we are on line one
      > > we are on line two
      > > +--------------------------------
      >
      > > and given
      >
      > > def foo(x): return len(x)
      >
      > > we get
      >
      > > 2 are on line one
      > > 2 are on line two
      >
      > > since the matching part ("we") has two letters.
      >
      > Well, I am no python expert, but in this special case I would use
      > :%s/^\(\w\+\)/\=strlen(submatch(0))// which would use vims scripting
      > language to do what you want. For more complex things, you can probably
      > do something like this:
      > :%s@^\(\w\+\)@\=substitute(system("echo " . shellescape(submatch(1)) . "|
      > perl -pe 's/^(.*)/length $1/e' "), '\n', '', '')@
      > (one line)
      >
      > Someone can probably suggest a similar command line for python. For more
      > complex functions, you can write some code, that will be called
      > when calling the interpreter.
      >
      > regards,
      > Christian
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    Your message has been successfully submitted and would be delivered to recipients shortly.