Poor man's surround.kak

define-command -override my-surround -docstring %{
 Surround all selections with the typed character. 
} %{ on-key %{ evaluate-commands %sh{
  left=$kak_key
  right=$kak_key
  pair() {
   ( [ "$kak_key" = "$1" ] || [ "$kak_key" = "$2" ] )
   && left=$1 && right=$2
  }
  pair '(' ')' || pair '[' ']' ||
  pair '{' '}' || pair '<lt>' '<gt>'
  printf "execute-keys %%{i%s<esc>a%s<esc>}" "$left" "$right"
}}}

5 Likes

editorial error in the pair function (but I can’t edit anymore), a correct version is

  pair() {
   ( [ "$kak_key" = "$1" ] || [ "$kak_key" = "$2" ] ) \
   && left=$1 && right=$2
  }

How about using a user mode?

declare-user-mode surround

define-command declare-surrounding-pair -params 4 -docstring 'declare-surrounding-pair <description> <alias> <opening> <closing>: declare surrounding pair' %{
  map -docstring %arg{1} global surround %arg{2} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
  map -docstring %arg{1} global surround %arg{3} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
  map -docstring %arg{1} global surround %arg{4} "Z\i%arg{3}<esc>\a%arg{4}<esc>Hz"
}

declare-surrounding-pair 'parenthesis block' b ( )
declare-surrounding-pair 'brace block' B { }
declare-surrounding-pair 'bracket block' r [ ]
declare-surrounding-pair 'angle block' a <lt> <gt>

map -docstring 'enter surround mode' global normal q ': enter-user-mode surround<ret>'
3 Likes