kakoune

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

tmux.kak (3204B)


      1 # http://tmux.github.io/
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 provide-module tmux %{
      5 
      6 # ensure we're running under tmux
      7 evaluate-commands %sh{
      8     [ -z "${kak_opt_windowing_modules}" ] || [ -n "$TMUX" ] || echo 'fail tmux not detected'
      9 }
     10 
     11 define-command -hidden -params 2.. tmux-terminal-impl %{
     12     evaluate-commands %sh{
     13         tmux=${kak_client_env_TMUX:-$TMUX}
     14         if [ -z "$tmux" ]; then
     15             echo "fail 'This command is only available in a tmux session'"
     16             exit
     17         fi
     18         tmux_args="$1"
     19         if [ "${1%%-*}" = split ]; then
     20             tmux_args="$tmux_args -t ${kak_client_env_TMUX_PANE}"
     21         elif [ "${1%% *}" = new-window ]; then
     22             session_id=$(tmux display-message -p -t ${kak_client_env_TMUX_PANE} '#{session_id}')
     23             tmux_args="$tmux_args -t $session_id"
     24         fi
     25         shift
     26         # ideally we should escape single ';' to stop tmux from interpreting it as a new command
     27         # but that's probably too rare to care
     28         if [ -n "$TMPDIR" ]; then
     29             TMUX=$tmux tmux $tmux_args env TMPDIR="$TMPDIR" "$@"
     30         else
     31             TMUX=$tmux tmux $tmux_args "$@"
     32         fi
     33     }
     34 }
     35 
     36 define-command tmux-terminal-vertical -params 1.. -docstring '
     37 tmux-terminal-vertical <program> [<arguments>]: create a new terminal as a tmux pane
     38 The current pane is split into two, top and bottom
     39 The program passed as argument will be executed in the new terminal' \
     40 %{
     41     tmux-terminal-impl 'split-window -v' %arg{@}
     42 }
     43 complete-command tmux-terminal-vertical shell
     44 
     45 define-command tmux-terminal-horizontal -params 1.. -docstring '
     46 tmux-terminal-horizontal <program> [<arguments>]: create a new terminal as a tmux pane
     47 The current pane is split into two, left and right
     48 The program passed as argument will be executed in the new terminal' \
     49 %{
     50     tmux-terminal-impl 'split-window -h' %arg{@}
     51 }
     52 complete-command tmux-terminal-horizontal shell
     53 
     54 define-command tmux-terminal-window -params 1.. -docstring '
     55 tmux-terminal-window <program> [<arguments>]: create a new terminal as a tmux window
     56 The program passed as argument will be executed in the new terminal' \
     57 %{
     58     tmux-terminal-impl 'new-window' %arg{@}
     59 }
     60 complete-command tmux-terminal-window shell
     61 
     62 define-command tmux-focus -params ..1 -docstring '
     63 tmux-focus [<client>]: focus the given client
     64 If no client is passed then the current one is used' \
     65 %{
     66     evaluate-commands %sh{
     67         if [ $# -eq 1 ]; then
     68             printf "evaluate-commands -client '%s' focus" "$1"
     69         elif [ -n "${kak_client_env_TMUX}" ]; then
     70             # select-pane makes the pane active in the window, but does not select the window. Both select-pane
     71             # and select-window should be invoked in order to select a pane on a currently not focused window.
     72             TMUX="${kak_client_env_TMUX}" tmux select-window -t "${kak_client_env_TMUX_PANE}" \; \
     73                                                select-pane   -t "${kak_client_env_TMUX_PANE}" > /dev/null
     74         fi
     75     }
     76 }
     77 complete-command -menu tmux-focus client
     78 
     79 ## The default behaviour for the `new` command is to open an horizontal pane in a tmux session
     80 alias global focus tmux-focus
     81 
     82 }