From 12324856c6645c2e71c4dfcf5ef12313c989b387 Mon Sep 17 00:00:00 2001
From: Andrea De Pasquale <andrea@de-pasquale.name>
Date: Tue, 24 Sep 2013 11:11:35 +0200
Subject: Added ssh-like mosh window title

Display the hostname when running Mosh (http://mosh.mit.edu/).
Same thing as displaying "hostname" when running "ssh hostname".
---
 lib/termsupport.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 9c0a430fb..30410c1fd 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -28,7 +28,7 @@ function omz_termsupport_preexec {
   setopt extended_glob
 
   # cmd name only, or if this is sudo or ssh, the next cmd
-  local CMD=${1[(wr)^(*=*|sudo|ssh|rake|-*)]:gs/%/%%}
+  local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
   local LINE="${2:gs/%/%%}"
 
   title '$CMD' '%100>...>$LINE%<<'
-- 
cgit v1.2.3-70-g09d2


From a9e97e5d2da90cf3e2b4575888fe585972ebcc74 Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Sat, 14 Feb 2015 19:05:27 -0500
Subject: Remove terminalapp plugin and fold its implementation in to
 lib/termsupport.zsh. Replaces the redundant Terminal.app support that was
 recently added to termsupport.

---
 lib/termsupport.zsh                        | 43 +++++++++++++++++++++++-------
 plugins/terminalapp/terminalapp.plugin.zsh | 39 ---------------------------
 2 files changed, 33 insertions(+), 49 deletions(-)
 delete mode 100644 plugins/terminalapp/terminalapp.plugin.zsh

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index e1c2e2f93..da74dfdba 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -53,14 +53,37 @@ precmd_functions+=(omz_termsupport_precmd)
 preexec_functions+=(omz_termsupport_preexec)
 
 
-# Runs before showing the prompt, to update the current directory in Terminal.app
-function omz_termsupport_cwd {
-  # Notify Terminal.app of current directory using undocumented OSC sequence
-  # found in OS X 10.9 and 10.10's /etc/bashrc
-  if [[ $TERM_PROGRAM == Apple_Terminal ]] && [[ -z $INSIDE_EMACS ]]; then
-    local PWD_URL="file://$HOSTNAME${PWD// /%20}"
-    printf '\e]7;%s\a' "$PWD_URL"
-  fi
-}
+# Keep Apple Terminal.app's current working directory updated
+# Based on this answer: http://superuser.com/a/315029
+
+if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
+  # Emits the control sequence to notify Terminal.app of the cwd
+  function update_terminalapp_cwd() {
+        # Identify the directory using a "file:" scheme URL, including
+        # the host name to disambiguate local vs. remote paths.
+
+        # Percent-encode the pathname.
+        local URL_PATH=''
+        {
+            # Use LANG=C to process text byte-by-byte.
+            local i ch hexch LANG=C
+            for ((i = 1; i <= ${#PWD}; ++i)); do
+                ch="$PWD[i]"
+                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
+                    URL_PATH+="$ch"
+                else
+                    hexch=$(printf "%02X" "'$ch")
+                    URL_PATH+="%$hexch"
+                fi
+            done
+        }
+
+        local PWD_URL="file://$HOST$URL_PATH"
+        printf '\e]7;%s\a' "$PWD_URL"
+    }
 
-precmd_functions+=(omz_termsupport_cwd)
+    # Use a precmd hook instead of a chpwd hook to avoid contaminating output
+    precmd_functions+=(update_terminalapp_cwd)
+    # Run once to get initial cwd set
+    update_terminalapp_cwd
+fi
diff --git a/plugins/terminalapp/terminalapp.plugin.zsh b/plugins/terminalapp/terminalapp.plugin.zsh
deleted file mode 100644
index 6e47ee188..000000000
--- a/plugins/terminalapp/terminalapp.plugin.zsh
+++ /dev/null
@@ -1,39 +0,0 @@
-# Set Apple Terminal.app resume directory
-# based on this answer: http://superuser.com/a/315029
-# 2012-10-26: (javageek) Changed code using the updated answer
-
-# Tell the terminal about the working directory whenever it changes.
-if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
-  update_terminal_cwd() {
-        # Identify the directory using a "file:" scheme URL, including
-        # the host name to disambiguate local vs. remote paths.
-
-        # Percent-encode the pathname.
-        local URL_PATH=''
-        {
-            # Use LANG=C to process text byte-by-byte.
-            local i ch hexch LANG=C
-            for ((i = 1; i <= ${#PWD}; ++i)); do
-                ch="$PWD[i]"
-                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
-                    URL_PATH+="$ch"
-                else
-                    hexch=$(printf "%02X" "'$ch")
-                    URL_PATH+="%$hexch"
-                fi
-            done
-        }
-
-        local PWD_URL="file://$HOST$URL_PATH"
-        #echo "$PWD_URL"        # testing
-        printf '\e]7;%s\a' "$PWD_URL"
-    }
-
-    # Register the function so it is called whenever the working
-    # directory changes.
-    autoload add-zsh-hook
-    add-zsh-hook precmd update_terminal_cwd
-
-    # Tell the terminal about the initial directory.
-    update_terminal_cwd
-fi
-- 
cgit v1.2.3-70-g09d2


From 3a18c33f17fb73ead87f3c532b3f5c21ea192e10 Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Sat, 14 Feb 2015 20:30:03 -0500
Subject: In termsupport, use LC_CTYPE instead of LANG to enable byte-by-byte
 text processing. LANG doesn't seem to actually work.

---
 lib/termsupport.zsh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index da74dfdba..9ff8a811c 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -65,8 +65,8 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
         # Percent-encode the pathname.
         local URL_PATH=''
         {
-            # Use LANG=C to process text byte-by-byte.
-            local i ch hexch LANG=C
+            # Use LC_CTYPE=C to process text byte-by-byte.
+            local i ch hexch LC_CTYPE=C
             for ((i = 1; i <= ${#PWD}; ++i)); do
                 ch="$PWD[i]"
                 if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
-- 
cgit v1.2.3-70-g09d2


From 702ff1ca9167455d2df385790e9023f758d5fc33 Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Tue, 17 Feb 2015 00:49:53 -0500
Subject: Add support for non-UTF-8 encodings in caller's locale.

---
 lib/termsupport.zsh | 77 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 50 insertions(+), 27 deletions(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 9ff8a811c..70b54301d 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -55,35 +55,58 @@ preexec_functions+=(omz_termsupport_preexec)
 
 # Keep Apple Terminal.app's current working directory updated
 # Based on this answer: http://superuser.com/a/315029
+# With extra fixes to handle multibyte chars and non-UTF-8 locales
 
 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
-  # Emits the control sequence to notify Terminal.app of the cwd
-  function update_terminalapp_cwd() {
-        # Identify the directory using a "file:" scheme URL, including
-        # the host name to disambiguate local vs. remote paths.
-
-        # Percent-encode the pathname.
-        local URL_PATH=''
-        {
-            # Use LC_CTYPE=C to process text byte-by-byte.
-            local i ch hexch LC_CTYPE=C
-            for ((i = 1; i <= ${#PWD}; ++i)); do
-                ch="$PWD[i]"
-                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
-                    URL_PATH+="$ch"
-                else
-                    hexch=$(printf "%02X" "'$ch")
-                    URL_PATH+="%$hexch"
-                fi
-            done
-        }
-
-        local PWD_URL="file://$HOST$URL_PATH"
-        printf '\e]7;%s\a' "$PWD_URL"
+
+  # URL-encodes a string
+  # Outputs the encoded string on stdout
+  # Returns nonzero if encoding failed
+  function _omz_urlencode() {
+    local url_str=''
+    {
+      local str=$1
+
+      # URLs must use UTF-8 encoding; convert if required
+      local encoding=${LC_CTYPE/*./}
+      if [[ $encoding != UTF-8 ]]; then
+        str=$(iconv -f $encoding -t UTF-8)
+        if [[ $? != 0 ]]; then
+          echo "Error converting string from $encoding to UTF-8" >&2
+          return 1
+        fi
+      fi
+
+      # Use LC_CTYPE=C to process text byte-by-byte
+      local i ch hexch LC_CTYPE=C
+      for ((i = 1; i <= ${#str}; ++i)); do
+        ch="$str[i]"
+        if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
+          url_str+="$ch"
+        else
+          hexch=$(printf "%02X" "'$ch")
+          url_str+="%$hexch"
+        fi
+      done
+      echo $url_str
     }
+  }
 
-    # Use a precmd hook instead of a chpwd hook to avoid contaminating output
-    precmd_functions+=(update_terminalapp_cwd)
-    # Run once to get initial cwd set
-    update_terminalapp_cwd
+  # Emits the control sequence to notify Terminal.app of the cwd
+  function update_terminalapp_cwd() {
+    # Identify the directory using a "file:" scheme URL, including
+    # the host name to disambiguate local vs. remote paths.
+
+    # Percent-encode the pathname.
+    local URL_PATH=$(_omz_urlencode $PWD)
+    [[ $? != 0 ]] && return 1
+    local PWD_URL="file://$HOST$URL_PATH"
+    # Undocumented Terminal.app-specific control sequence
+    printf '\e]7;%s\a' $PWD_URL
+  }
+
+  # Use a precmd hook instead of a chpwd hook to avoid contaminating output
+  precmd_functions+=(update_terminalapp_cwd)
+  # Run once to get initial cwd set
+  update_terminalapp_cwd
 fi
-- 
cgit v1.2.3-70-g09d2


From 187cf07c9148d5d6dda9b8ac1b453bd6e39ea550 Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Tue, 17 Feb 2015 00:54:54 -0500
Subject: For unspecified encodings, assume it's UTF-8 or compatible (e.g.
 ASCII) and muddle through without character encoding conversion.

---
 lib/termsupport.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 70b54301d..ef9f0e5f1 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -69,7 +69,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
 
       # URLs must use UTF-8 encoding; convert if required
       local encoding=${LC_CTYPE/*./}
-      if [[ $encoding != UTF-8 ]]; then
+      if [[ -n $encoding && $encoding != UTF-8 ]]; then
         str=$(iconv -f $encoding -t UTF-8)
         if [[ $? != 0 ]]; then
           echo "Error converting string from $encoding to UTF-8" >&2
-- 
cgit v1.2.3-70-g09d2


From cb37c934c0e1083897b501e5b8a85340e3b7e470 Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Sat, 21 Feb 2015 16:00:40 -0500
Subject: Remove unnecessary braces.

---
 lib/termsupport.zsh | 48 +++++++++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 25 deletions(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index ef9f0e5f1..7cb3791e6 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -63,33 +63,31 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
   # Outputs the encoded string on stdout
   # Returns nonzero if encoding failed
   function _omz_urlencode() {
+    local str=$1
     local url_str=''
-    {
-      local str=$1
-
-      # URLs must use UTF-8 encoding; convert if required
-      local encoding=${LC_CTYPE/*./}
-      if [[ -n $encoding && $encoding != UTF-8 ]]; then
-        str=$(iconv -f $encoding -t UTF-8)
-        if [[ $? != 0 ]]; then
-          echo "Error converting string from $encoding to UTF-8" >&2
-          return 1
-        fi
-      fi
 
-      # Use LC_CTYPE=C to process text byte-by-byte
-      local i ch hexch LC_CTYPE=C
-      for ((i = 1; i <= ${#str}; ++i)); do
-        ch="$str[i]"
-        if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
-          url_str+="$ch"
-        else
-          hexch=$(printf "%02X" "'$ch")
-          url_str+="%$hexch"
-        fi
-      done
-      echo $url_str
-    }
+    # URLs must use UTF-8 encoding; convert if required
+    local encoding=${LC_CTYPE/*./}
+    if [[ -n $encoding && $encoding != UTF-8 ]]; then
+      str=$(iconv -f $encoding -t UTF-8)
+      if [[ $? != 0 ]]; then
+        echo "Error converting string from $encoding to UTF-8" >&2
+        return 1
+      fi
+    fi
+
+    # Use LC_CTYPE=C to process text byte-by-byte
+    local i ch hexch LC_CTYPE=C
+    for ((i = 1; i <= ${#str}; ++i)); do
+      ch="$str[i]"
+      if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
+        url_str+="$ch"
+      else
+        hexch=$(printf "%02X" "'$ch")
+        url_str+="%$hexch"
+      fi
+    done
+    echo $url_str
   }
 
   # Emits the control sequence to notify Terminal.app of the cwd
-- 
cgit v1.2.3-70-g09d2


From 88f42b6132adedff7557170168416d575569b56e Mon Sep 17 00:00:00 2001
From: Andrew Janke <janke@pobox.com>
Date: Sat, 21 Feb 2015 16:25:28 -0500
Subject: Add support for variant "utf8" locale suffix spelling. Fix dumb bug
 in iconv call that would cause it to hang.

---
 lib/termsupport.zsh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 7cb3791e6..52622f5ab 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -64,12 +64,12 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
   # Returns nonzero if encoding failed
   function _omz_urlencode() {
     local str=$1
-    local url_str=''
+    local url_str=""
 
     # URLs must use UTF-8 encoding; convert if required
     local encoding=${LC_CTYPE/*./}
-    if [[ -n $encoding && $encoding != UTF-8 ]]; then
-      str=$(iconv -f $encoding -t UTF-8)
+    if [[ -n $encoding && $encoding != UTF-8 && $encoding != utf8 ]]; then
+      str=$(echo $str | iconv -f $encoding -t UTF-8)
       if [[ $? != 0 ]]; then
         echo "Error converting string from $encoding to UTF-8" >&2
         return 1
-- 
cgit v1.2.3-70-g09d2


From 90ea67edf0982923f82905d87bda5b685089f219 Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Fri, 31 Jul 2015 22:23:12 -0400
Subject: termsupport: protect title() with `emulate -L zsh`

This prevents it from malfunctioning when `setopt prompt_subst` is off.
---
 lib/termsupport.zsh | 3 +++
 1 file changed, 3 insertions(+)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index e1c2e2f93..973c4dcf6 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -7,6 +7,9 @@
 # (In screen, only short_tab_title is used)
 # Limited support for Apple Terminal (Terminal can't set window and tab separately)
 function title {
+  emulate -L zsh
+  setopt prompt_subst
+  
   [[ "$EMACS" == *term* ]] && return
 
   # if $2 is unset use $1 as default
-- 
cgit v1.2.3-70-g09d2


From 17da983442f8872e147381db215266735751abc8 Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Sun, 9 Aug 2015 15:15:12 -0400
Subject: termsupport: avoid repeated dir in window title in Terminal.app

---
 lib/termsupport.zsh | 4 ++++
 1 file changed, 4 insertions(+)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index e1c2e2f93..ecca516f2 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -23,6 +23,10 @@ function title {
 
 ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
 ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
+# Avoid duplication of directory in terminals with independent dir display
+if [[ $TERM_PROGRAM == Apple_Terminal ]]; then
+  ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
+fi
 
 # Runs before showing the prompt
 function omz_termsupport_precmd {
-- 
cgit v1.2.3-70-g09d2


From 14b4ba83c35c34f4a1f3a69c9967f502ee2d6528 Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Sun, 9 Aug 2015 16:28:47 -0400
Subject: Move urlencode/urldecode functions to core lib

---
 lib/functions.zsh   | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/termsupport.zsh |  33 +------------
 2 files changed, 135 insertions(+), 32 deletions(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/functions.zsh b/lib/functions.zsh
index 17f5f9cbf..5c1a5a283 100644
--- a/lib/functions.zsh
+++ b/lib/functions.zsh
@@ -73,3 +73,137 @@ function env_default() {
     env | grep -q "^$1=" && return 0 
     export "$1=$2"       && return 3
 }
+
+
+# Required for $langinfo
+zmodload zsh/langinfo
+
+# URL-encode a string
+#
+# Encodes a string using RFC 2396 URL-encoding (%-escaped).
+# See: https://www.ietf.org/rfc/rfc2396.txt
+#
+# By default, reserved characters and unreserved "mark" characters are
+# not escaped by this function. This allows the common usage of passing
+# an entire URL in, and encoding just special characters in it, with 
+# the expectation that reserved and mark characters are used appropriately.
+# The -r and -m options turn on escaping of the reserved and mark characters,
+# respectively, which allows arbitrary strings to be fully escaped for
+# embedding inside URLs, where reserved characters might be misinterpreted.
+#
+# Prints the encoded string on stdout.
+# Returns nonzero if encoding failed.
+#
+# Usage:
+#  omz_urlencode [-r] [-m] <string>
+#  
+#    -r causes reserved characters (;/?:@&=+$,) to be escaped
+#
+#    -m causes "mark" characters (_.!~*''()-) to be escaped
+#
+#    -P causes spaces to be encoded as '%20' instead of '+'
+function omz_urlencode() {
+  emulate -L zsh
+  zparseopts -D -E -a opts r m P
+
+  local in_str=$1
+  local url_str=""
+  local spaces_as_plus
+  if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
+  local str="$in_str"
+
+  # URLs must use UTF-8 encoding; convert str to UTF-8 if required
+  local encoding=$langinfo[CODESET]
+  local safe_encodings
+  safe_encodings=(UTF-8 utf8 US-ASCII)
+  if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
+    str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
+    if [[ $? != 0 ]]; then
+      echo "Error converting string from $encoding to UTF-8" >&2
+      return 1
+    fi
+  fi
+
+  # Use LC_CTYPE=C to process text byte-by-byte
+  local i byte ord LC_ALL=C
+  export LC_ALL
+  local reserved=';/?:@&=+$,'
+  local mark='_.!~*''()-'
+  local dont_escape="[A-Za-z0-9"
+  if [[ -z $opts[(r)-r] ]]; then
+    dont_escape+=$reserved
+  fi
+  # $mark must be last because of the "-"
+  if [[ -z $opts[(r)-m] ]]; then
+    dont_escape+=$mark
+  fi
+  dont_escape+="]"
+
+  # Implemented to use a single printf call and avoid subshells in the loop,
+  # for performance (primarily on Windows).
+  local url_str=""
+  for (( i = 1; i <= ${#str}; ++i )); do
+    byte="$str[i]"
+    if [[ "$byte" =~ "$dont_escape" ]]; then
+      url_str+="$byte"
+    else
+      if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
+        url_str+="+"
+      else
+        ord=$(( [##16] #byte ))
+        url_str+="%$ord"
+      fi
+    fi
+  done
+  echo -E "$url_str"
+}
+
+# URL-decode a string
+#
+# Decodes a RFC 2396 URL-encoded (%-escaped) string.
+# This decodes the '+' and '%' escapes in the input string, and leaves 
+# other characters unchanged. Does not enforce that the input is a 
+# valid URL-encoded string. This is a convenience to allow callers to
+# pass in a full URL or similar strings and decode them for human
+# presentation.
+#
+# Outputs the encoded string on stdout.
+# Returns nonzero if encoding failed.
+#
+# Usage:
+#   omz_urldecode <urlstring>  - prints decoded string followed by a newline
+function omz_urldecode {
+  emulate -L zsh
+  local encoded_url=$1
+
+  echo -e input $1
+  # Work bytewise, since URLs escape UTF-8 octets
+  local caller_encoding=$langinfo[CODESET]
+  local LC_ALL=C
+  export LC_ALL
+  
+  # Change + back to ' '
+  local tmp=${encoded_url:gs/+/ /}
+  # Protect other escapes to pass through the printf unchanged
+  tmp=${tmp:gs/\\/\\\\/}
+  # Handle %-escapes by turning them into `\xXX` printf escapes
+  tmp=${tmp:gs/%/\\x/}
+  echo -E "before decode $tmp"
+  local decoded
+  eval "decoded=\$'$tmp'"
+
+  # Now we have a UTF-8 encoded string in the variable. We need to re-encode
+  # it if caller is in a non-UTF-8 locale.
+  local safe_encodings
+  safe_encodings=(UTF-8 utf8 US-ASCII)
+  if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
+    decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
+    if [[ $? != 0 ]]; then
+      echo "Error converting string from UTF-8 to $caller_encoding" >&2
+      return 1
+    fi
+  fi
+
+  echo -E "$decoded"
+}
+
diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 52622f5ab..726cdce41 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -59,44 +59,13 @@ preexec_functions+=(omz_termsupport_preexec)
 
 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
 
-  # URL-encodes a string
-  # Outputs the encoded string on stdout
-  # Returns nonzero if encoding failed
-  function _omz_urlencode() {
-    local str=$1
-    local url_str=""
-
-    # URLs must use UTF-8 encoding; convert if required
-    local encoding=${LC_CTYPE/*./}
-    if [[ -n $encoding && $encoding != UTF-8 && $encoding != utf8 ]]; then
-      str=$(echo $str | iconv -f $encoding -t UTF-8)
-      if [[ $? != 0 ]]; then
-        echo "Error converting string from $encoding to UTF-8" >&2
-        return 1
-      fi
-    fi
-
-    # Use LC_CTYPE=C to process text byte-by-byte
-    local i ch hexch LC_CTYPE=C
-    for ((i = 1; i <= ${#str}; ++i)); do
-      ch="$str[i]"
-      if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
-        url_str+="$ch"
-      else
-        hexch=$(printf "%02X" "'$ch")
-        url_str+="%$hexch"
-      fi
-    done
-    echo $url_str
-  }
-
   # Emits the control sequence to notify Terminal.app of the cwd
   function update_terminalapp_cwd() {
     # Identify the directory using a "file:" scheme URL, including
     # the host name to disambiguate local vs. remote paths.
 
     # Percent-encode the pathname.
-    local URL_PATH=$(_omz_urlencode $PWD)
+    local URL_PATH=$(omz_urlencode -P $PWD)
     [[ $? != 0 ]] && return 1
     local PWD_URL="file://$HOST$URL_PATH"
     # Undocumented Terminal.app-specific control sequence
-- 
cgit v1.2.3-70-g09d2


From 47d19cc56425aa7c550d845726111ee8bd9520d1 Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Mon, 17 Aug 2015 20:55:41 -0400
Subject: Add defensive `emulate -L zsh` to termsupport functions

---
 lib/termsupport.zsh | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

(limited to 'lib/termsupport.zsh')

diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 726cdce41..ff61d5329 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -7,6 +7,7 @@
 # (In screen, only short_tab_title is used)
 # Limited support for Apple Terminal (Terminal can't set window and tab separately)
 function title {
+  emulate -L zsh
   [[ "$EMACS" == *term* ]] && return
 
   # if $2 is unset use $1 as default
@@ -26,6 +27,7 @@ ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
 
 # Runs before showing the prompt
 function omz_termsupport_precmd {
+  emulate -L zsh
   if [[ $DISABLE_AUTO_TITLE == true ]]; then
     return
   fi
@@ -35,11 +37,11 @@ function omz_termsupport_precmd {
 
 # Runs before executing the command
 function omz_termsupport_preexec {
+  emulate -L zsh
   if [[ $DISABLE_AUTO_TITLE == true ]]; then
     return
   fi
 
-  emulate -L zsh
   setopt extended_glob
 
   # cmd name only, or if this is sudo or ssh, the next cmd
@@ -61,6 +63,7 @@ if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
 
   # Emits the control sequence to notify Terminal.app of the cwd
   function update_terminalapp_cwd() {
+    emulate -L zsh
     # Identify the directory using a "file:" scheme URL, including
     # the host name to disambiguate local vs. remote paths.
 
-- 
cgit v1.2.3-70-g09d2