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