vamolessa / pepper

simple and opinionated modal code editor for your terminal
https://vamolessa.github.io/pepper/
372 stars 17 forks source link

Support `map select` (create mapping for selection mode) #61

Closed NNBnh closed 1 year ago

NNBnh commented 2 years ago

We currently have:

map

Creates a keyboard mapping for an editor mode. <mode> is one of normal, insert, command, readline and picker. <from> and <to> are a string of keys.

  • usage: map <mode> <from> <to>

Unfortunate there is no support for mapping on selection mode, but if map select is support this will open up many possibility for user to make the editor way more convenient for examples:

map normal "<backspace>" "cVcvhd" # Delete one character to the left
map normal "<delete>" "cVcvld" # Delete one character to the right

# Delete selection
map select "<backspace>" "d"
map select "<delete>" "d"
# By default, these keys do nothing in normal mode anyway.
# Let's make them more useful by select the whole line/buffer before doing the action:
map normal "d" "Vd" # Delete the whole line in normal mode
map normal "y" "Vy" # Copy the whole line in normal mode
map normal "<c-y>" "V<c-y>" # Copy the whole line to register in normal mode
map normal "cs" "gkghvgjglcs" # Search whole buffer and select all match
map normal "cS" "gkghvgjglcS" # Search whole buffer and select everything that not match
map normal "CS" "gkghvgjglCS" # Search whole buffer and select everything that not match

# Make those keys use selection instead of the whole line/buffer in selection mode:
# (not sure if this is necessary, perhaps `map normal` shouldn't effect selection mode)
map select "d" "d"
map select "y" "y"
map select "<c-y>" "<c-y>"
map select "cs" "cs"
map select "cS" "cS"
map select "CS" "CS"
vamolessa commented 2 years ago

I'm afraid this would not be possible as there's actually no 'select' mode. The select mode is more of a 'submode' of normal mode if you will. You see, there's nothing preventing you from having a selection (a cursor with a position different than it's anchor) outside the selection submode.

vamolessa commented 2 years ago

It could be possible to create custom commands that do what you want through a code plugin and then map them to keys in normal mode.

However, it would be some really verbose solution.

On another thought, we could make an if command that conditionally executes other commands and an some @has-selection() expansion that would evaluate to true when on selection submode. Then you'd be able to do something like this:

command on-backspace-normal-mode @{
    if @has-selection() @{
        enqueue-keys cVcvhd # delete one character to the left
    } else @{
        enqueue-keys <esc> # cancel selection
    }
}
map normal <backspace> :<space>on-backspace-normal-mode<enter>

Not really ideal, but could work.

vamolessa commented 2 years ago

This is actually possible in the upcoming v0.29.0 (currently on branch parse-multi-cursors). In that version, you can do:

command on-backspace-normal-mode @{
    if @cursor-anchor() != @cursor-position() @{
        enqueue-keys cVcvhd # delete one character to the left
    }
    if @cursor-anchor() == @cursor-position() @{
        enqueue-keys <esc> # cancel selection
    }
}
map normal <backspace> :<space>on-backspace-normal-mode<enter>
vamolessa commented 1 year ago

Will close this since it's possible to implement with custom commands.