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