Re: vim ruby extension and visual selection

2009-03-29 Thread Ken Bloom

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



Re: vim ruby extension and visual selection

2009-03-25 Thread benjamin.thoma...@gmail.com

Hi,
I'm a noob myself but I believe "VIM::Buffer.current.line" + a "vmap"
is all you need.
Here's what I've coded recently, maybe that'll help you out:

###
start of vimscript#
###

autocmd bufenter * :call Get_filetype()

nmap  :call PrependComment("up")
nmap  :call PrependComment("down")
imap  :call PrependComment("up")
imap  :call PrependComment("down")

""  is  (for me)
nmap  :call PrependComment("stationary")
imap  :call PrependComment("stationary")
vmap  :call PrependComment("stationary_vmap")

fun! Get_filetype()
ruby << EOF_Get_filetype

  filetype = VIM::evaluate("&filetype")
  $comment_token = case filetype
  when "sh", "perl", "python", "ruby": "#"
  when "c", "cpp", "java": "//"
  when "vim": '"'
  else ""
  end
EOF_Get_filetype
endfun

fun! PrependComment(direction)
ruby << EOF_PrependComment

  direction = VIM::evaluate("a:direction")
  if direction == 'up'
VIM.command("normal -")
  end

  line = VIM::Buffer.current.line
  if line =~ /^\s*#{$comment_token}[ [:alnum:]].*/   #
if comment at beginning of line
line.gsub!(/(^\s*)(#{$comment_token}[ ]?)(.*)/, '\1\3')  #
delete comment
  else
if direction == "stationary_vmap"
  line.gsub!(/.+/, "#{$comment_token} \\0")  #
comment token on the far left
else
  line.gsub!(/[^ ].*/, "#{$comment_token} \\0")  #
otherwise add comment
end
  end
  VIM::Buffer.current.line = line

  if direction == 'down'
VIM.command("normal +")
  end
EOF_PrependComment
endfun

#
end of vimscript#
#

If that's of any interest, I can send you the rest of the file if you
wish.

All the best and good luck,
Ben


On Mar 25, 10:24 am, anton.hornqu...@gmail.com 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
--~--~-~--~~~---~--~~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~--~~~~--~~--~--~---