]> arthur.barton.de Git - ax-zsh.git/blobdiff - plugins/iterm2/iterm2.zshrc
iterm2: Don't setup command aliases, we always update the search path
[ax-zsh.git] / plugins / iterm2 / iterm2.zshrc
index 6ee03e3826afeaa8b546f4f7b9d0951cbaee4e9c..648bd8319c20c0d2c5889d56dc17d1f5ac85f550 100644 (file)
@@ -7,23 +7,41 @@
 axzsh_is_modern_terminal || return 91
 [[ -o interactive ]] || return 91
 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 91
-[[ "$TERM" != "screen" && "$TERM" != "screen-256color" ]] || return 91
+[[ "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}$TERM" =~ "^screen" ]] && return 91
+
+# Add iTerm2 commands to PATH, when available.
+[[ -d ~/.iterm2 ]] && path+=(~/.iterm2)
 
 # Try to source user-local shell integration installed by iTerm2 itself,
 # and only fall back to the implementation here when not found.
 [[ -e "$HOME/.iterm2_shell_integration.zsh" ]] && source "$HOME/.iterm2_shell_integration.zsh"
+
+# ax-zsh specific iTerm2 functions
+iterm2_clear_captured_output() {
+       printf "\e]1337;ClearCapturedOutput\e\\"
+}
+
 [[ -z "$ITERM_SHELL_INTEGRATION_INSTALLED" ]] || return 0
 
 ITERM_SHELL_INTEGRATION_INSTALLED="Yes"
 ITERM2_SHOULD_DECORATE_PROMPT="1"
 
+if [[ -n "$TMUX" ]]; then
+       # Pass escape sequences through in tmux(1), see
+       # <https://gist.github.com/antifuchs/c8eca4bcb9d09a7bbbcd>.
+       TMUX_PREFIX='\ePtmux;\e'
+       TMUX_POSTFIX='\e\\'
+else
+       unset TMUX_PREFIX TMUX_POSTFIX
+fi
+
 # Indicates start of command output. Runs just before command executes.
 iterm2_before_cmd_executes() {
-       printf "\033]133;C;\007"
+       printf "${TMUX_PREFIX}\033]133;C;\007${TMUX_POSTFIX}"
 }
 
 iterm2_set_user_var() {
-       printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
+       printf "${TMUX_PREFIX}\033]1337;SetUserVar=%s=%s\007${TMUX_POSTFIX}" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
 }
 
 # Users can write their own version of this method. It should call
@@ -31,33 +49,32 @@ iterm2_set_user_var() {
 # e.g., iterm2_set_user_var currentDirectory $PWD
 # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
 # \(user.currentDirectory).
-whence -v iterm2_print_user_vars > /dev/null 2>&1
-if [[ $? -ne 0 ]]; then
+if ! whence iterm2_print_user_vars >/dev/null; then
        iterm2_print_user_vars() {
                :
        }
 fi
 
 iterm2_print_state_data() {
-       printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "$iterm2_hostname"
-       printf "\033]1337;CurrentDir=%s\007" "$PWD"
+       printf "${TMUX_PREFIX}\033]1337;RemoteHost=%s@%s\007${TMUX_POSTFIX}" "$USER" "$HOST"
+       printf "${TMUX_PREFIX}\033]1337;CurrentDir=%s\007${TMUX_POSTFIX}" "$PWD"
        iterm2_print_user_vars
 }
 
 # Report return code of command; runs after command finishes but before prompt
 iterm2_after_cmd_executes() {
-       printf "\033]133;D;%s\007" "$STATUS"
+       printf "${TMUX_PREFIX}\033]133;D;%s\007${TMUX_POSTFIX}" "$STATUS"
        iterm2_print_state_data
 }
 
 # Mark start of prompt
 iterm2_prompt_mark() {
-       printf "\033]133;A\007"
+       printf "${TMUX_PREFIX}\033]133;A\007${TMUX_POSTFIX}"
 }
 
 # Mark end of prompt
 iterm2_prompt_end() {
-       printf "\033]133;B\007"
+       printf "${TMUX_PREFIX}\033]133;B\007${TMUX_POSTFIX}"
 }
 
 # There are three possible paths in life.
@@ -105,24 +122,29 @@ iterm2_decorate_prompt() {
        ITERM2_SHOULD_DECORATE_PROMPT=""
 
        # Add our escape sequences just before the prompt is shown.
-       if [[ $PS1 == *'$(iterm2_prompt_mark)'* ]]
-       then
-               PS1="$PS1%{$(iterm2_prompt_end)%}"
+       # Use ITERM2_SQUELCH_MARK for people who can't modify PS1 directly, like powerlevel9k users.
+       # This is gross but I had a heck of a time writing a correct if statement for zsh 5.0.2.
+       local PREFIX=""
+       if [[ $PS1 == *"$(iterm2_prompt_mark)"* ]]; then
+               PREFIX=""
+       elif [[ "${ITERM2_SQUELCH_MARK-}" != "" ]]; then
+               PREFIX=""
        else
-               PS1="%{$(iterm2_prompt_mark)%}$PS1%{$(iterm2_prompt_end)%}"
+               PREFIX="%{$(iterm2_prompt_mark)%}"
        fi
+       PS1="$PREFIX$PS1%{$(iterm2_prompt_end)%}"
 }
 
 iterm2_precmd() {
        local STATUS="$?"
-       if [ -z "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
+       if [[ -z "${ITERM2_SHOULD_DECORATE_PROMPT-}" ]]; then
                # You pressed ^C while entering a command (iterm2_preexec did not run)
                iterm2_before_cmd_executes
        fi
 
        iterm2_after_cmd_executes "$STATUS"
 
-       if [ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
+       if [[ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]]; then
                iterm2_decorate_prompt
        fi
 }
@@ -135,17 +157,8 @@ iterm2_preexec() {
        iterm2_before_cmd_executes
 }
 
-# If hostname -f is slow on your system, set iterm2_hostname prior to sourcing this script.
-[[ -z "$iterm2_hostname" ]] && iterm2_hostname=`hostname -f`
-
 precmd_functions+=(iterm2_precmd)
 preexec_functions+=(iterm2_preexec)
 
 iterm2_print_state_data
-printf "\033]1337;ShellIntegrationVersion=5;shell=zsh\007"
-
-# Setup iTerm2 command aliases
-for cmd (~/.iterm2/*(N)); do
-       [[ -x "$cmd" ]] && alias "$(basename "$cmd")=$cmd"
-done
-unset cmd
+printf "${TMUX_PREFIX}\033]1337;ShellIntegrationVersion=12;shell=zsh\007${TMUX_POSTFIX}"