mle icon indicating copy to clipboard operation
mle copied to clipboard

Block edit and overtype

Open smartmic opened this issue 3 years ago • 5 comments

By using drop cursor column and anchor it is possible to select a rectangluar area. It would be great if I can filter only this specific column selection through a shell command but currently this did not work for me. While cat worked as expected, sort just inserts (multi) newlines and other commands fail completely. Also, copying such blocks and pasting them somewhere else yields to strange results (no linebreaks, mixed order). I must admit, multi-cursor editing is new to me, so maybe I just did it wrong?

My typical use case here is doing quick calculations/aggregations with e.g. awk for certain columns holding numbers in text tables.

Is there a way to do this or will be possible in the future?

smartmic avatar Jan 23 '21 23:01 smartmic

Hi @smartmic I have a block select implementation on a branch somewhere, but never merged it because it did not feel very elegant. I would like to take it in a different direction, specifically to combine it with a replace (overtype) mode, which is also currently lacking from mle:

  • Add cmd_toggle_block which toggles a new field cursor_t/mark_t.is_block.
  • When is_block==1:
    • Change cursor behavior to replace (aka overtype) instead of insert. The general idea is that we should optimize for preserving the shape of text instead of the text itself in block mode. Insert, append, and delete operations can already be done with multiple cursors. So in the new mode, we should optimize for replace and box operations.
    • Fix cmd_replace, cmd_insert_data (newlines, auto-indent), and any other stuff that does not respect the block illusion.
  • When is_block==1 && is_anchored==1 (block select):
    • Modify various mark_* and cursor_* functions for block select. At the very least this will be all mark_*between* and mark_insert|delete|replace* functions. Any intent to delete should instead replace with whitespace. (For example, cmd_cut should replace with whitespace instead of deleting text.) Inserting content with newlines, for example with cmd_uncut, should block paste instead of pasting as normal.
  • Add cmd_swap_anchor which swaps the cursor's mark with its anchor. (This is the equivalent of o in vim's visual block mode.)
  • Maybe add cmds to convert back and forth between multiple is_block==0 cursors and 1 big is_block==1 cursor.
  • Add a visual indicator in the status bar for is_block.
  • Note: If we do it right, multiple is_block==1 cursors should be possible, which will be fun to show off at least.

Curious to hear your thoughts on this. It is a very broad change so I'm sure there will be bugs. I would want to flag it as experimental for at least a version or two.

By the way, to clarify what is going on with your example, using cmd_drop_cursor_column then cmd_toggle_anchor results in N selections, one per line. If you then use cmd_shell in this state, the command will run multiple times applied to each selection individually. For the type of thing you want to do, this approach does not work. To get back to one cursor from there, use cmd_remove_extra_cursors.

adsr avatar Jan 24 '21 04:01 adsr

Thanks, @adsr for sharing your ideas. Since I am not used to multi cursor editing, my first thought was: deleting a selected block should really delete the text and not fill up the selection with white space. But after some reflection, I agree, this can be done with multiple cursors. And your idea to preserve the text shape while in block mode makes sense. So yes, the whole mode description sounds very compelling to me.

Maybe it makes sense to add a function so that one can easily switch to multiple cursor if in block mode (and back), e.g. if one decides to insert, delete etc. instead of replace? I think a challenge will be the distinction between multiple (column) cursors and block mode, both should not overlap in functionality and confuse the user. With your suggestion, I think this is solved really elegantly. And I have a reason to look forward to sending the block selection to shell ;)

smartmic avatar Jan 24 '21 22:01 smartmic

I recorded a short demo here https://webm.red/view/vAdy.webm. I still don't like the implementation as it required a bunch of if (is_block) ... special cases. I'll explore writing individual cmd_block_(cut|copy|paste|shell) commands next. While not perfectly correct or consistent, it is way simpler.

adsr avatar Feb 04 '21 00:02 adsr

Looks great in the demo! Can we try it out already?

smartmic avatar Feb 06 '21 21:02 smartmic

I haven't come up with a satisfying implemenation for this. I do think it'd be useful however and remains on the backlog.

adsr avatar Aug 27 '22 19:08 adsr

Added in bffa40f. insert (by default) toggles block mode.

adsr avatar Apr 21 '23 05:04 adsr