]> arthur.barton.de Git - ax-zsh.git/blob - plugins/iterm2/iterm2.zshrc
iterm2: Enable use inside of tmux(1) sessions
[ax-zsh.git] / plugins / iterm2 / iterm2.zshrc
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # iterm2.zshrc: iTerm2 Shell Integration
3
4 [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 92
5
6 # Check prerequisites ...
7 axzsh_is_modern_terminal || return 91
8 [[ -o interactive ]] || return 91
9 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 91
10 [[ "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}$TERM" =~ "^screen" ]] && return 91
11
12 # Add iTerm2 commands to PATH, when available.
13 [[ -d ~/.iterm2 ]] && path+=(~/.iterm2)
14
15 # Try to source user-local shell integration installed by iTerm2 itself,
16 # and only fall back to the implementation here when not found.
17 [[ -e "$HOME/.iterm2_shell_integration.zsh" ]] && source "$HOME/.iterm2_shell_integration.zsh"
18 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 0
19
20 ITERM_SHELL_INTEGRATION_INSTALLED="Yes"
21 ITERM2_SHOULD_DECORATE_PROMPT="1"
22
23 if [[ -n "$TMUX" ]]; then
24         # Pass escape sequenzes through in tmux(1), see
25         # <https://gist.github.com/antifuchs/c8eca4bcb9d09a7bbbcd>.
26         TMUX_PREFIX='\ePtmux;\e'
27         TMUX_POSTFIX='\e\\'
28 else
29         unset TMUX_PREFIX TMUX_POSTFIX
30 fi
31
32 # Indicates start of command output. Runs just before command executes.
33 iterm2_before_cmd_executes() {
34         printf "${TMUX_PREFIX}\033]133;C;\007${TMUX_POSTFIX}"
35 }
36
37 iterm2_set_user_var() {
38         printf "${TMUX_PREFIX}\033]1337;SetUserVar=%s=%s\007${TMUX_POSTFIX}" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
39 }
40
41 # Users can write their own version of this method. It should call
42 # iterm2_set_user_var but not produce any other output.
43 # e.g., iterm2_set_user_var currentDirectory $PWD
44 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
45 # \(user.currentDirectory).
46 whence -v iterm2_print_user_vars > /dev/null 2>&1
47 if [ $? -ne 0 ]; then
48         iterm2_print_user_vars() {
49                 :
50         }
51 fi
52
53 iterm2_print_state_data() {
54         printf "${TMUX_PREFIX}\033]1337;RemoteHost=%s@%s\007${TMUX_POSTFIX}" "$USER" "$iterm2_hostname"
55         printf "${TMUX_PREFIX}\033]1337;CurrentDir=%s\007${TMUX_POSTFIX}" "$PWD"
56         iterm2_print_user_vars
57 }
58
59 # Report return code of command; runs after command finishes but before prompt
60 iterm2_after_cmd_executes() {
61         printf "${TMUX_PREFIX}\033]133;D;%s\007${TMUX_POSTFIX}" "$STATUS"
62         iterm2_print_state_data
63 }
64
65 # Mark start of prompt
66 iterm2_prompt_mark() {
67         printf "${TMUX_PREFIX}\033]133;A\007${TMUX_POSTFIX}"
68 }
69
70 # Mark end of prompt
71 iterm2_prompt_end() {
72         printf "${TMUX_PREFIX}\033]133;B\007${TMUX_POSTFIX}"
73 }
74
75 # There are three possible paths in life.
76 #
77 # 1) A command is entered at the prompt and you press return.
78 #    The following steps happen:
79 #    * iterm2_preexec is invoked
80 #      * PS1 is set to ITERM2_PRECMD_PS1
81 #      * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
82 #    * The command executes (possibly reading or modifying PS1)
83 #    * iterm2_precmd is invoked
84 #      * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
85 #      * PS1 gets our escape sequences added to it
86 #    * zsh displays your prompt
87 #    * You start entering a command
88 #
89 # 2) You press ^C while entering a command at the prompt.
90 #    The following steps happen:
91 #    * (iterm2_preexec is NOT invoked)
92 #    * iterm2_precmd is invoked
93 #      * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
94 #      * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
95 #        sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
96 #    * zsh displays your prompt
97 #    * You start entering a command
98 #
99 # 3) A new shell is born.
100 #    * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
101 #    * iterm2_precmd is invoked
102 #      * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
103 #      * ITERM2_PRECMD_PS1 is set to the initial value of PS1
104 #      * PS1 gets our escape sequences added to it
105 #    * Your prompt is shown and you may begin entering a command.
106 #
107 # Invariants:
108 # * ITERM2_SHOULD_DECORATE_PROMPT is 1 during and just after command execution, and "" while the prompt is
109 #   shown and until you enter a command and press return.
110 # * PS1 does not have our escape sequences during command execution
111 # * After the command executes but before a new one begins, PS1 has escape sequences and
112 #   ITERM2_PRECMD_PS1 has PS1's original value.
113 iterm2_decorate_prompt() {
114         # This should be a raw PS1 without iTerm2's stuff. It could be changed during command
115         # execution.
116         ITERM2_PRECMD_PS1="$PS1"
117         ITERM2_SHOULD_DECORATE_PROMPT=""
118
119         # Add our escape sequences just before the prompt is shown.
120         # Use ITERM2_SQUELCH_MARK for people who can't mdoify PS1 directly, like powerlevel9k users.
121         # This is gross but I had a heck of a time writing a correct if statetment for zsh 5.0.2.
122         local PREFIX=""
123         if [[ $PS1 == *"$(iterm2_prompt_mark)"* ]]; then
124                 PREFIX=""
125         elif [[ "${ITERM2_SQUELCH_MARK-}" != "" ]]; then
126                 PREFIX=""
127         else
128                 PREFIX="%{$(iterm2_prompt_mark)%}"
129         fi
130         PS1="$PREFIX$PS1%{$(iterm2_prompt_end)%}"
131 }
132
133 iterm2_precmd() {
134         local STATUS="$?"
135         if [ -z "${ITERM2_SHOULD_DECORATE_PROMPT-}" ]; then
136                 # You pressed ^C while entering a command (iterm2_preexec did not run)
137                 iterm2_before_cmd_executes
138         fi
139
140         iterm2_after_cmd_executes "$STATUS"
141
142         if [ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
143                 iterm2_decorate_prompt
144         fi
145 }
146
147 # This is not run if you press ^C while entering a command.
148 iterm2_preexec() {
149         # Set PS1 back to its raw value prior to executing the command.
150         PS1="$ITERM2_PRECMD_PS1"
151         ITERM2_SHOULD_DECORATE_PROMPT="1"
152         iterm2_before_cmd_executes
153 }
154
155 # If hostname -f is slow on your system, set iterm2_hostname prior to sourcing this script.
156 [[ -z "$iterm2_hostname" ]] && iterm2_hostname=`hostname -f`
157
158 precmd_functions+=(iterm2_precmd)
159 preexec_functions+=(iterm2_preexec)
160
161 iterm2_print_state_data
162 printf "${TMUX_PREFIX}\033]1337;ShellIntegrationVersion=11;shell=zsh\007${TMUX_POSTFIX}"
163
164 # Setup iTerm2 command aliases
165 for cmd (~/.iterm2/*(N)); do
166         [[ -x "$cmd" ]] && alias "$(basename "$cmd")=$cmd"
167 done
168 unset cmd