On Wed, 25 Mar 2009 02:24:44 -0700, anton.hornquist wrote:

> Hi,
> 
> I've checked the docs on the ruby scripting features in vim but I cannot
> find a way to retrieve the visual selection area in either VIM::window
> or VIM::buffer. Is there any way to do this or any easy workaround for
> this?
> 
> As an example I would like to be able to visually select an area and
> invoke a ruby script that would add a comment line to the top and bottom
> of the selection (while of course preserving undo history). Also I would
> like to be able to select an arbitrary area and execute a ruby script
> that could search in for certain characters in the area.
> 
> /Anton

I already shared this answer by private email, but I decided today to 
subscribe to the group so I'm going to share my answer with this group 
too.

I recently (just last week) decided to implement the same sort of plugin. 
After examining the visswap.vim plugin, I concluded that the best way to 
do this sort of thing is as follows:


old_x=VIM.evaluate('@x') #first save the old x register
VIM.command(':normal! vgv"xy') #then yank selection into the x register
selection=VIM.evaluate('@x') #then get the contents of x register

#do ruby stuff on the selection

#set the value of the X register
VIM::evaluate("setreg('x',\"#{result.vim_escape}\")")
VIM::command(':normal! vgv"xP') #paste x register

#finally, restore x register
VIM::evaluate("setreg('x',\"#{old_x.vim_escape}\")")

And of course, somewhere earlier in the script I have defined

class String
   #escapes a string so it can be concatenated into
   #a call to VIM.evaluate
   def vim_escape
      result=dup
      result.gsub!("\\",'\\\\\\\\')
      result.gsub!('"','\"')
      result
   end
end

-- 
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to