kakoune

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

iterm.kak (5488B)


      1 # https://www.iterm2.com
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 provide-module iterm %{
      5 
      6 # ensure that we're running on iTerm
      7 evaluate-commands %sh{
      8     [ -z "${kak_opt_windowing_modules}" ] || [ "$TERM_PROGRAM" = "iTerm.app" ] || echo 'fail iTerm not detected'
      9 }
     10 
     11 define-command -hidden -params 2.. iterm-terminal-impl %{
     12     nop %sh{
     13         direction="$1"
     14         shift
     15         # join the arguments as one string for the shell execution (see x11.kak)
     16         args=$(
     17             for i in "$@"; do
     18                 printf "'%s' " "$(printf %s "$i" | sed "s|'|'\\\\''|g")"
     19             done
     20         )
     21 
     22         # go through another round of escaping for osascript
     23         # \ -> \\
     24         # " -> \"
     25         do_esc() {
     26             printf %s "$*" | sed -e 's|\\|\\\\|g; s|"|\\"|g'
     27         }
     28 
     29         escaped=$(do_esc "$args")
     30         esc_path=$(do_esc "$PATH")
     31         esc_tmp=$(do_esc "$TMPDIR")
     32         cmd="env PATH='${esc_path}' TMPDIR='${esc_tmp}' $escaped"
     33         if [ "$direction" = 'tab' ]; then
     34             osascript                                                       \
     35             -e "tell application \"iTerm\""                                 \
     36             -e "    tell current window"                                    \
     37             -e "        create tab with default profile command \"${cmd}\"" \
     38             -e "    end tell"                                               \
     39             -e "end tell" >/dev/null
     40         elif [ "$direction" = 'window' ]; then
     41             osascript                                                      \
     42             -e "tell application \"iTerm\""                                \
     43             -e "    create window with default profile command \"${cmd}\"" \
     44             -e "end tell" >/dev/null
     45         else
     46             osascript                                                                             \
     47             -e "tell application \"iTerm\""                                                       \
     48             -e "    tell current session of current window"                                       \
     49             -e "        tell (split ${direction} with same profile command \"${cmd}\") to select" \
     50             -e "    end tell"                                                                     \
     51             -e "end tell" >/dev/null
     52         fi
     53     }
     54 }
     55 
     56 define-command iterm-terminal-vertical -params 1.. -docstring '
     57 iterm-terminal-vertical <program> [<arguments>]: create a new terminal as an iterm pane
     58 The current pane is split into two, left and right
     59 The program passed as argument will be executed in the new terminal'\
     60 %{
     61     iterm-terminal-impl 'vertically' %arg{@}
     62 }
     63 complete-command iterm-terminal-vertical shell
     64 
     65 define-command iterm-terminal-horizontal -params 1.. -docstring '
     66 iterm-terminal-horizontal <program> [<arguments>]: create a new terminal as an iterm pane
     67 The current pane is split into two, top and bottom
     68 The program passed as argument will be executed in the new terminal'\
     69 %{
     70     iterm-terminal-impl 'horizontally' %arg{@}
     71 }
     72 complete-command iterm-terminal-horizontal shell
     73 
     74 define-command iterm-terminal-tab -params 1.. -docstring '
     75 iterm-terminal-tab <program> [<arguments>]: create a new terminal as an iterm tab
     76 The program passed as argument will be executed in the new terminal'\
     77 %{
     78     iterm-terminal-impl 'tab' %arg{@}
     79 }
     80 complete-command iterm-terminal-tab shell
     81 
     82 define-command iterm-terminal-window -params 1.. -docstring '
     83 iterm-terminal-window <program> [<arguments>]: create a new terminal as an iterm window
     84 The program passed as argument will be executed in the new terminal'\
     85 %{
     86     iterm-terminal-impl 'window' %arg{@}
     87 }
     88 complete-command iterm-terminal-window shell
     89 
     90 define-command iterm-focus -params ..1 -docstring '
     91 iterm-focus [<client>]: focus the given client
     92 If no client is passed then the current one is used' \
     93 %{
     94     evaluate-commands %sh{
     95         if [ $# -eq 1 ]; then
     96             printf %s\\n "evaluate-commands -client '$1' focus"
     97         else
     98             session="${kak_client_env_ITERM_SESSION_ID#*:}"
     99             osascript                                                      \
    100             -e "tell application \"iTerm\" to repeat with aWin in windows" \
    101             -e "    tell aWin to repeat with aTab in tabs"                 \
    102             -e "        tell aTab to repeat with aSession in sessions"     \
    103             -e "            tell aSession"                                 \
    104             -e "                if (unique id = \"${session}\") then"      \
    105             -e "                    tell aWin"                             \
    106             -e "                        select"                            \
    107             -e "                    end tell"                              \
    108             -e "                    tell aTab"                             \
    109             -e "                        select"                            \
    110             -e "                    end tell"                              \
    111             -e "                    select"                                \
    112             -e "                end if"                                    \
    113             -e "            end tell"                                      \
    114             -e "        end repeat"                                        \
    115             -e "    end repeat"                                            \
    116             -e "end repeat"
    117         fi
    118     }
    119 }
    120 complete-command -menu iterm-focus client
    121 
    122 alias global focus iterm-focus
    123 
    124 }