Skip to content

Implementing user mode

Alex Leferry 2 edited this page Mar 29, 2017 · 17 revisions

The common pattern to do that is to use the on-key command-line command, plus the switch statement of your shell.

def -hidden command-mode %{
  info -title 'Command commands' %{
    <space>: command
    e: edit
    b: buffer
    w: write
    q: quit
  }
  on-key %{ %sh{
    case $kak_key in
      '<space>') echo exec : ;;
      e) echo exec ':edit<space>' ;;
      b) echo buffer-mode ;;
      w) echo write ;;
      q) echo quit ;;
    esac
  }
}}
def -hidden buffer-mode %{
  info -title 'Buffer commands' %{
    <space>: buffer
    n: next
    p: previous
  }
  on-key %{ %sh{
    case $kak_key in
      '<space>') echo exec ':buffer<space>' ;;
      n) echo buffer-next ;;
      p) echo buffer-previous ;;
    esac
  }
}}

Then expose the mode on a key.

map global normal : :command-mode<ret>

Spacemacs like info when pressing user key :

by jhentula

map global normal <space> :space-fn<ret>

def -allow-override space-fn %{
    %sh{
    	categories=()
    	keys=()
        cmds=()
        infos=()
        IFS=,
        while IFS=, read key cmd info; do
        	keys+=($key)
                cmds+=($cmd)
                infos+=($info)
        done < ~/.config/.spacefn

	infopopup=""
        for ((i=0;i<${#keys[@]};++i)); do
            printf -v line "%s:    %s\n" "${keys[i]}" "${infos[i]}"
            infopopup+=$line
        done
        echo "info \"$infopopup\""

	onkey='on-key %{%sh{ case $kak_key in'
        for ((i=0;i<${#keys[@]};++i)); do
            printf -v line "(%s) %s ;;" "${keys[i]}" "${cmds[i]}"
            onkey+=$line
        done
        onkey+='esac}}'
        echo "$onkey"
    }
}

-----
Content of .spacefn file: 

'<space>',echo "exec <space>",Clear selection 
'h',tmux select-pane -l,Select left pane 
'j',tmux select-pane -D,Select below pane 
'k',tmux select-pane -U,Select above pane 
'l',tmux select-pane -R,Select right pane 
'f',echo "find-file",Find file 
's',echo "find-symbol",Fine symbol 
'w',echo "write",Write buffer 
'e',echo "source ${kak_buffile}",Source buffer 
'k',echo "e ~/.config/kak/kakrc",Open .kakrc 
[0-9],tmux select-window -t ${kak_key},Select window 
Clone this wiki locally