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