kakoune

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

sway.kak (2710B)


      1 provide-module sway %{
      2 
      3 # Ensure we're actually in Sway
      4 evaluate-commands %sh{
      5     [ -z "${kak_opt_windowing_modules}" ] || 
      6     [ -n "$SWAYSOCK" ] ||
      7     echo 'fail SWAYSOCK is not set'
      8 }
      9 
     10 require-module 'wayland'
     11 
     12 alias global sway-terminal-window wayland-terminal-window
     13 
     14 define-command sway-terminal-vertical -params 1.. -docstring '
     15     sway-terminal-vertical <program> [<arguments>]: create a new terminal as a Sway window
     16     The current pane is split into two, top and bottom
     17     The program passed as argument will be executed in the new terminal' \
     18 %{
     19     nop %sh{swaymsg split vertical}
     20     wayland-terminal-window %arg{@}
     21 }
     22 complete-command sway-terminal-vertical shell
     23 
     24 define-command sway-terminal-horizontal -params 1.. -docstring '
     25     sway-terminal-horizontal <program> [<arguments>]: create a new terminal as a Sway window
     26     The current pane is split into two, left and right
     27     The program passed as argument will be executed in the new terminal' \
     28 %{
     29     nop %sh{swaymsg split horizontal}
     30     wayland-terminal-window %arg{@}
     31 }
     32 complete-command sway-terminal-horizontal shell
     33 
     34 define-command sway-terminal-tab -params 1.. -docstring '
     35     sway-terminal-tab <program> [<arguments>]: create a new terminal as a Sway window
     36     The program passed as argument will be executed in the new terminal' \
     37 %{
     38     nop %sh{swaymsg 'split horizontal; layout tabbed'}
     39     wayland-terminal-window %arg{@}
     40 }
     41 complete-command sway-terminal-tab shell
     42 
     43 define-command sway-focus-pid -hidden %{
     44     evaluate-commands %sh{
     45         pid=$kak_client_pid
     46 
     47         # Try to focus a window with the current PID, walking up the tree of 
     48         # parent processes until the focus eventually succeeds
     49         while ! swaymsg [pid=$pid] focus > /dev/null 2> /dev/null ; do
     50             # Replace the current PID with its parent PID
     51             pid=$(ps -p $pid -o ppid=)
     52 
     53             # If we couldn't get a PPID for some reason, or it's 1 or less, we 
     54             # should just fail. 
     55             if [ -z $pid ] || [ $pid -le 1 ]; then
     56                 echo "fail Can't find PID for Sway window to focus"
     57                 break
     58             fi
     59         done
     60     }
     61 }
     62 
     63 define-command sway-focus -params ..1 -docstring '
     64 sway-focus [<kakoune_client>]: focus a given client''s window.
     65 If no client is passed, then the current client is used' \
     66 %{
     67     # Quick branch to make sure we're calling sway-focus-pid from the client 
     68     # the user wants to focus on.
     69     evaluate-commands %sh{
     70         if [ $# -eq 1 ]; then
     71             printf "evaluate-commands -client '%s' sway-focus-pid" "$1"
     72         else
     73             echo sway-focus-pid
     74         fi
     75     }
     76 }
     77 complete-command -menu sway-focus client
     78 
     79 alias global focus sway-focus
     80 
     81 }