views:

503

answers:

2

I'll try to explain by example..

If we have a piece of code like this in vim:

if ($feck == true && $drink == false)
{
    echo 'They lie in wait like wolves..';
}

And I go to visual mode and select "$drink" for example, is there a way to:

  • detect whether the current selection is one of vim's text-objects (word, WORD, inner {, etc.)
  • do a lookup on both sides of the selection to check for the next available vim text-object (again, word, WORD, a block, inner ", etc..)

Please note that I'm thinking of vim scripting not just plain editing, so both of these "actions" would be in a function, sourced in my .vimrc

Also, I would need to do this without breaking the visual selection or moving the cursor (or, if it really can't be avoided, restore both the selection and cursor position).

A: 

After a visual selection is made, '< and '> are available for use in anything that accepts a "range."

dwc
+1  A: 

This question only makes sense in the context of this other question by dr Hannibal Lecter.

I think what you want to achieve is possible, but it requires some work. The information given in the other answer and the comments to your post, already point in the right direction.

Your vim function that is called from your "extend selection" key mapping must do the following:

  1. Check what the current selection probably represents (markers '< and '> as dwc describes and vim functions should be helpful for this). Questions like this must be answered: is '< and '> on the same line? Are all letters between '< and '> iskeyword characters? Are the first and last characters of the selection special delimiters like '{','(', etc. ? And so on - there is no builtin functionality that checks this automatically. See also :help visualmode(

  2. Once this is determined, you have to think, how you then would like to extend your selection further - however the logic for this works in ReSharper.

  3. You will need to take care of putting the cursor back where it was, depending on the commands you used to extend the selection.

In general, the commands presented in :help eval.txt will be helpful in implementing this.

Excellent, exactly the tips I need to get started. Thanks.
dr Hannibal Lecter