Invoking Ruby script for selected range in Vimscript...

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Invoking Ruby script for selected range in Vimscript...

Sonny Chee
Hey Guys,

When I attempt to invoke a Ruby script (within  a Vimscript) over a selected range, my Rubyscript gets invoked once for each line in the selected range. This is the documented behavior:

:[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
                        [range], with $_ being set to the text of each line in
                        turn, without a trailing <EOL>.  Setting $_ will change
                        the text, but note that it is not possible to add or
                        delete lines using this command.
                        The default for [range] is the whole file: "1,$".


Is it possible to bypass this behavior and have my Ruby script invoked just once?

Any suggestions would be welcome.

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Invoking Ruby script for selected range in Vimscript...

Reid Thompson
On Tue, 2015-10-13 at 10:57 -0700, Sonny Chee wrote:

> Hey Guys,
>
> When I attempt to invoke a Ruby script (within  a Vimscript) over a selected
> range, my Rubyscript gets invoked once for each line in the selected range.
> This is the documented behavior:
>
> :[range]rubyd[o] {cmd} Evaluate Ruby command {cmd} for each line in the
>  [range], with $_ being set to the text of each line in
>  turn, without a trailing <EOL>.  Setting $_ will
change

>  the text, but note that it is not possible to add or
>  delete lines using this command.
>  The default for [range] is the whole file: "1,$".
>
>
> Is it possible to bypass this behavior and have my Ruby script invoked just
> once?
>
> Any suggestions would be welcome.
>
> --

just as info
i've used a variant of this that i found on the web for a while now
executes on the current line, or the current visual selection

http://starshine.org/xteddy/thomas/~n6tadam/vim-ruby/ruby_xmp.vim

where xmpvim.rb is

$ cat /usr/bin/xmpvim.rb
# xmpvim.rb
# Based on xmp.rb by Gotoken
module Kernel
  XMPVIM_VERSION = "2004-11-24"

  def xmp(arg, fmt = "%l%s# => %r\n", out = " /// output: '%o'", sep= "\t\t")
    devnil = ""
    def devnil.write(str)
      nil
    end
    $stderr = devnil
    if fmt
      fmt = fmt.to_s
      buff = ""
      def buff.write(str)
        self << str.to_s
      end
      $stdout = buff
      sep = "" if arg =~ /^\s*\z/
      begin
        res = eval(arg, TOPLEVEL_BINDING).inspect
        res += out.gsub('%o', buff.gsub("\n",'\n')) unless buff == ""
        fmt.gsub!('%l', arg.chomp).gsub!('%r', res).gsub!('%s',sep)
        STDOUT.print fmt
      rescue Exception => res
        res = "ERROR: " + res.to_s.gsub("\n", "...   ")
        res += out.gsub('%o', buff.gsub("\n",'\n')) unless buff == ""
        fmt.gsub!('%l', arg.chomp).gsub!('%r', res).gsub!('%s',sep)
        STDOUT.print fmt
      end
    else
      begin
        print arg
        $stdout = devnil
        eval(arg, TOPLEVEL_BINDING)
      rescue Exception
        STDOUT.puts "#" + $!.to_s.gsub("\n", "...   ")
      end
    end
    $stdout = STDOUT
    $stderr = STDERR
  end
  def xmpvim(begv, endv)
    lines = $stdin.to_a
    firstpart = lines[0..begv-2].join
    secondpart = lines[begv-1..-1].join
    xmp(firstpart, false) if begv > 1
    xmp(secondpart)
  end

  def xmpvim_simple
    lines = $stdin.to_a
    xmp(lines.join)
  end

  alias __xmp__ xmp
end

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.