kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

tmux.kak (3321B)


      1 # http://tmux.github.io/
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 # Tmux version >= 2 is required to use this module
      4 
      5 hook global ModuleLoaded tmux %{
      6     require-module tmux-repl
      7 }
      8 
      9 provide-module tmux-repl %{
     10 
     11 declare-option -docstring "tmux pane id in which the REPL is running" str tmux_repl_id
     12 
     13 define-command -hidden -params 1.. tmux-repl-impl %{
     14     evaluate-commands %sh{
     15         if [ -z "$TMUX" ]; then
     16             echo 'fail This command is only available in a tmux session'
     17             exit
     18         fi
     19         tmux_args="$1"
     20         if [ "${1%%-*}" = split ]; then
     21             tmux_args="$tmux_args -t ${kak_client_env_TMUX_PANE}"
     22         elif [ "${1%% *}" = new-window ]; then
     23             session_id=$(tmux display-message -p -t ${kak_client_env_TMUX_PANE} '#{session_id}')
     24             tmux_args="$tmux_args -t $session_id"
     25         fi
     26         shift
     27         repl_pane_id=$(tmux $tmux_args -P -F '#{pane_id}' "$@")
     28         printf "set-option current tmux_repl_id '%s'" "$repl_pane_id"
     29     }
     30 }
     31 
     32 define-command tmux-repl-vertical -params 0.. -docstring "Create a new vertical pane for repl interaction" %{
     33     tmux-repl-impl 'split-window -v' %arg{@}
     34 }
     35 complete-command tmux-repl-vertical shell
     36 
     37 define-command tmux-repl-horizontal -params 0.. -docstring "Create a new horizontal pane for repl interaction" %{
     38     tmux-repl-impl 'split-window -h' %arg{@}
     39 }
     40 complete-command tmux-repl-horizontal shell
     41 
     42 define-command tmux-repl-window -params 0.. -docstring "Create a new window for repl interaction" %{
     43     tmux-repl-impl 'new-window' %arg{@}
     44 }
     45 complete-command tmux-repl-window shell
     46 
     47 define-command -params 0..1 tmux-repl-set-pane -docstring %{
     48         tmux-repl-set-pane [pane number]: Set an existing tmux pane for repl interaction
     49         If the address of new pane is not given, next pane is used
     50         (To get the pane number in tmux,
     51         use 'tmux display-message -p '#{pane_id}'" in that pane)
     52     } %{
     53     evaluate-commands %sh{
     54         if [ -z "$TMUX" ]; then
     55             echo 'fail This command is only available in a tmux session'
     56             exit
     57         fi
     58         if [ $# -eq 0 ]; then
     59             curr_pane_no="${kak_client_env_TMUX_PANE#%}"
     60             tgt_pane=$((curr_pane_no+1))
     61         else
     62             tgt_pane="$1"
     63         fi
     64         curr_win="$(tmux display-message -t ${kak_client_env_TMUX_PANE} -p '#{window_id}')" 
     65         if tmux list-panes -t "$curr_win" -F \#D | grep -Fxq "%"$tgt_pane; then
     66             printf "set-option current tmux_repl_id '%s'" %$tgt_pane
     67         else
     68             echo 'fail The correct pane is not there. Activate using tmux-terminal-* or some other way'
     69         fi
     70     }
     71 }
     72 
     73 define-command -hidden tmux-send-text -params 0..1 -docstring %{
     74         tmux-send-text [text]: Send text to the REPL pane.
     75         If no text is passed, then the selection is used
     76     } %{
     77     evaluate-commands %sh{
     78         if [ $# -eq 0 ]; then
     79             tmux set-buffer -b kak_selection -- "${kak_selection}"
     80         else
     81             tmux set-buffer -b kak_selection -- "$1"
     82         fi
     83         tmux paste-buffer -b kak_selection -t "$kak_opt_tmux_repl_id" ||
     84         echo 'fail tmux-send-text: failed to send text, see *debug* buffer for details'
     85     }
     86 }
     87 
     88 alias global repl-new tmux-repl-horizontal
     89 alias global repl-send-text tmux-send-text
     90 
     91 }