d5eddc653fc00f2b41e8bda43017564f35a6ee6a

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #1376 from javageek/master

Updated code

Diff

 1diff --git a/plugins/terminalapp/terminalapp.plugin.zsh b/plugins/terminalapp/terminalapp.plugin.zsh
 2index 4695ad0556d62806bf335408a7ed79171860f1f4..2249b1e2f1d4485f6c88349636b9e6adda065d2a 100644
 3--- a/plugins/terminalapp/terminalapp.plugin.zsh
 4+++ b/plugins/terminalapp/terminalapp.plugin.zsh
 5@@ -1,11 +1,39 @@
 6 # Set Apple Terminal.app resume directory
 7 # based on this answer: http://superuser.com/a/315029
 8+# 2012-10-26: (javageek) Changed code using the updated answer
 9 
10-function chpwd {
11-  local SEARCH=' '
12-  local REPLACE='%20'
13-  local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
14-  printf '\e]7;%s\a' "$PWD_URL"
15-}
16+# Tell the terminal about the working directory whenever it changes.
17+if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
18+  update_terminal_cwd() {
19+        # Identify the directory using a "file:" scheme URL, including
20+        # the host name to disambiguate local vs. remote paths.
21 
22-chpwd
23+        # Percent-encode the pathname.
24+        local URL_PATH=''
25+        {
26+            # Use LANG=C to process text byte-by-byte.
27+            local i ch hexch LANG=C
28+            for ((i = 1; i <= ${#PWD}; ++i)); do
29+                ch="$PWD[i]"
30+                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
31+                    URL_PATH+="$ch"
32+                else
33+                    hexch=$(printf "%02X" "'$ch")
34+                    URL_PATH+="%$hexch"
35+                fi
36+            done
37+        }
38+
39+        local PWD_URL="file://$HOST$URL_PATH"
40+        #echo "$PWD_URL"        # testing
41+        printf '\e]7;%s\a' "$PWD_URL"
42+    }
43+
44+    # Register the function so it is called whenever the working
45+    # directory changes.
46+    autoload add-zsh-hook
47+    add-zsh-hook chpwd update_terminal_cwd
48+
49+    # Tell the terminal about the initial directory.
50+    update_terminal_cwd
51+fi