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