702ff1ca9167455d2df385790e9023f758d5fc33

Author
Andrew Janke <janke@pobox.com>
Committer
Andrew Janke <janke@pobox.com>
Date

Message

Add support for non-UTF-8 encodings in caller's locale.

Diff

 1diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
 2index 9ff8a811cb0706fc76f61c386b6662dbcd0f24f4..70b54301d29b30c862b3d7c0293754e59d1deabd 100644
 3--- a/lib/termsupport.zsh
 4+++ b/lib/termsupport.zsh
 5@@ -55,35 +55,58 @@ preexec_functions+=(omz_termsupport_preexec)
 6 
 7 # Keep Apple Terminal.app's current working directory updated
 8 # Based on this answer: http://superuser.com/a/315029
 9+# With extra fixes to handle multibyte chars and non-UTF-8 locales
10 
11 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
12-  # Emits the control sequence to notify Terminal.app of the cwd
13-  function update_terminalapp_cwd() {
14-        # Identify the directory using a "file:" scheme URL, including
15-        # the host name to disambiguate local vs. remote paths.
16-
17-        # Percent-encode the pathname.
18-        local URL_PATH=''
19-        {
20-            # Use LC_CTYPE=C to process text byte-by-byte.
21-            local i ch hexch LC_CTYPE=C
22-            for ((i = 1; i <= ${#PWD}; ++i)); do
23-                ch="$PWD[i]"
24-                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
25-                    URL_PATH+="$ch"
26-                else
27-                    hexch=$(printf "%02X" "'$ch")
28-                    URL_PATH+="%$hexch"
29-                fi
30-            done
31-        }
32-
33-        local PWD_URL="file://$HOST$URL_PATH"
34-        printf '\e]7;%s\a' "$PWD_URL"
35+
36+  # URL-encodes a string
37+  # Outputs the encoded string on stdout
38+  # Returns nonzero if encoding failed
39+  function _omz_urlencode() {
40+    local url_str=''
41+    {
42+      local str=$1
43+
44+      # URLs must use UTF-8 encoding; convert if required
45+      local encoding=${LC_CTYPE/*./}
46+      if [[ $encoding != UTF-8 ]]; then
47+        str=$(iconv -f $encoding -t UTF-8)
48+        if [[ $? != 0 ]]; then
49+          echo "Error converting string from $encoding to UTF-8" >&2
50+          return 1
51+        fi
52+      fi
53+
54+      # Use LC_CTYPE=C to process text byte-by-byte
55+      local i ch hexch LC_CTYPE=C
56+      for ((i = 1; i <= ${#str}; ++i)); do
57+        ch="$str[i]"
58+        if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
59+          url_str+="$ch"
60+        else
61+          hexch=$(printf "%02X" "'$ch")
62+          url_str+="%$hexch"
63+        fi
64+      done
65+      echo $url_str
66     }
67+  }
68 
69-    # Use a precmd hook instead of a chpwd hook to avoid contaminating output
70-    precmd_functions+=(update_terminalapp_cwd)
71-    # Run once to get initial cwd set
72-    update_terminalapp_cwd
73+  # Emits the control sequence to notify Terminal.app of the cwd
74+  function update_terminalapp_cwd() {
75+    # Identify the directory using a "file:" scheme URL, including
76+    # the host name to disambiguate local vs. remote paths.
77+
78+    # Percent-encode the pathname.
79+    local URL_PATH=$(_omz_urlencode $PWD)
80+    [[ $? != 0 ]] && return 1
81+    local PWD_URL="file://$HOST$URL_PATH"
82+    # Undocumented Terminal.app-specific control sequence
83+    printf '\e]7;%s\a' $PWD_URL
84+  }
85+
86+  # Use a precmd hook instead of a chpwd hook to avoid contaminating output
87+  precmd_functions+=(update_terminalapp_cwd)
88+  # Run once to get initial cwd set
89+  update_terminalapp_cwd
90 fi