tommcdo / vim-exchange

Easy text exchange operator for Vim
MIT License
756 stars 23 forks source link

Idea: Extend exchange.vim by bubbling commands #35

Closed kiryph closed 9 years ago

kiryph commented 9 years ago

I like the bubbling concept of a single line or serval lines see vimcast #26. Plugins offering such feature are unimpaired.vim (line/lines), matze/vim-move (line/lines), frace/vim-bubbles and sideways.vim (function arguments & other). I could imagine that exchange.vim could also have a bubbling feature. I think of following workflow:

Step 1: Invoke exchange.vim normally

cx{MOTION}

Step 2: Invoke bubbling feature, e.g. with

[e or ]e

to bubble/exchange the marked object with the equivalent previous or next object. The equivalent object must be defined for the used motion. This does not have to exist for all motions/objects. For instance, for the visual mode mapping X it can't be known what an equivalent next/prevous object would be.

In a first step I would like to see it working for cxiw, cxiW, cxis, cxip (i.e. words/WORDS, sentences, and paragraphs).

Step 3: After that it would also be useful to proceed with the dot command.

Mappings are stolen from unimpaired.vim:

[e - bubble over previous line ]e - bubble over next line

The mappings have a good mnemonic: e for exchange.

This clash of mappings could be resolved: If cx is not active, invoke previous mapped commands.

An implementation could look like this:

  1. Create a dictionary assigning to objects a sensible motion to the previous and next equivalent object, e.g. dict['ip'] = {'{', '}'}. This can be extended if needed and is possible/makes sense.
  2. query existing mappings of [e, and ]e and store them
  3. re-map [e, ]e to call funcion bubble_exchange() and make them repeatable
  4. The definition of the function bubble_exchange could look somehow like this:
     function bubble_exchange()
      if "motion in dictionary and cx active":
          if "[e"
            "do motion in backward direction and invoke dot command"
           if "]e"
            "do motion in forward direction and invoke dot command"
      else 
               "invoke saved mappings accordingly (]e or [e)"
     endfunction
tommcdo commented 9 years ago

This is a neat idea, but it's impossible to support all motions and text objects (think user-defined text objects). I think it falls outside the scope of exchange.vim to try to support the concept of "the next text object", when projects like targets.vim are doing a fine job of it. It makes more sense to combine the two and possibly create custom mappings for things you do frequently.