3c782f78f1d93fac94b3637fffe69f20c987bd0d

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

Message

Merge pull request #3582 from apjanke/fold-terminalapp-plugin-into-termsupport

Fold terminalapp plugin in to lib/termsupport

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/functions.zsh b/lib/functions.zsh
  2index 0d632a2686693f5d624ebd17694d9c4e18bc3d3e..efb73a1bd91170f1c2622635875ceb900942ee43 100644
  3--- a/lib/functions.zsh
  4+++ b/lib/functions.zsh
  5@@ -89,3 +89,135 @@ function env_default() {
  6     env | grep -q "^$1=" && return 0 
  7     export "$1=$2"       && return 3
  8 }
  9+
 10+
 11+# Required for $langinfo
 12+zmodload zsh/langinfo
 13+
 14+# URL-encode a string
 15+#
 16+# Encodes a string using RFC 2396 URL-encoding (%-escaped).
 17+# See: https://www.ietf.org/rfc/rfc2396.txt
 18+#
 19+# By default, reserved characters and unreserved "mark" characters are
 20+# not escaped by this function. This allows the common usage of passing
 21+# an entire URL in, and encoding just special characters in it, with 
 22+# the expectation that reserved and mark characters are used appropriately.
 23+# The -r and -m options turn on escaping of the reserved and mark characters,
 24+# respectively, which allows arbitrary strings to be fully escaped for
 25+# embedding inside URLs, where reserved characters might be misinterpreted.
 26+#
 27+# Prints the encoded string on stdout.
 28+# Returns nonzero if encoding failed.
 29+#
 30+# Usage:
 31+#  omz_urlencode [-r] [-m] <string>
 32+#  
 33+#    -r causes reserved characters (;/?:@&=+$,) to be escaped
 34+#
 35+#    -m causes "mark" characters (_.!~*''()-) to be escaped
 36+#
 37+#    -P causes spaces to be encoded as '%20' instead of '+'
 38+function omz_urlencode() {
 39+  emulate -L zsh
 40+  zparseopts -D -E -a opts r m P
 41+
 42+  local in_str=$1
 43+  local url_str=""
 44+  local spaces_as_plus
 45+  if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
 46+  local str="$in_str"
 47+
 48+  # URLs must use UTF-8 encoding; convert str to UTF-8 if required
 49+  local encoding=$langinfo[CODESET]
 50+  local safe_encodings
 51+  safe_encodings=(UTF-8 utf8 US-ASCII)
 52+  if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
 53+    str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
 54+    if [[ $? != 0 ]]; then
 55+      echo "Error converting string from $encoding to UTF-8" >&2
 56+      return 1
 57+    fi
 58+  fi
 59+
 60+  # Use LC_CTYPE=C to process text byte-by-byte
 61+  local i byte ord LC_ALL=C
 62+  export LC_ALL
 63+  local reserved=';/?:@&=+$,'
 64+  local mark='_.!~*''()-'
 65+  local dont_escape="[A-Za-z0-9"
 66+  if [[ -z $opts[(r)-r] ]]; then
 67+    dont_escape+=$reserved
 68+  fi
 69+  # $mark must be last because of the "-"
 70+  if [[ -z $opts[(r)-m] ]]; then
 71+    dont_escape+=$mark
 72+  fi
 73+  dont_escape+="]"
 74+
 75+  # Implemented to use a single printf call and avoid subshells in the loop,
 76+  # for performance (primarily on Windows).
 77+  local url_str=""
 78+  for (( i = 1; i <= ${#str}; ++i )); do
 79+    byte="$str[i]"
 80+    if [[ "$byte" =~ "$dont_escape" ]]; then
 81+      url_str+="$byte"
 82+    else
 83+      if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
 84+        url_str+="+"
 85+      else
 86+        ord=$(( [##16] #byte ))
 87+        url_str+="%$ord"
 88+      fi
 89+    fi
 90+  done
 91+  echo -E "$url_str"
 92+}
 93+
 94+# URL-decode a string
 95+#
 96+# Decodes a RFC 2396 URL-encoded (%-escaped) string.
 97+# This decodes the '+' and '%' escapes in the input string, and leaves 
 98+# other characters unchanged. Does not enforce that the input is a 
 99+# valid URL-encoded string. This is a convenience to allow callers to
100+# pass in a full URL or similar strings and decode them for human
101+# presentation.
102+#
103+# Outputs the encoded string on stdout.
104+# Returns nonzero if encoding failed.
105diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
106index babbaa95789afb3233e8b4eb74216ceed44ce716..5f61fe8ef2d4e3cd34fe441dde04779f709f9fa7 100644
107--- a/lib/termsupport.zsh
108+++ b/lib/termsupport.zsh
109@@ -33,6 +33,7 @@ fi
110 
111 # Runs before showing the prompt
112 function omz_termsupport_precmd {
113+  emulate -L zsh
114   if [[ $DISABLE_AUTO_TITLE == true ]]; then
115     return
116   fi
117@@ -42,11 +43,11 @@ function omz_termsupport_precmd {
118 
119 # Runs before executing the command
120 function omz_termsupport_preexec {
121+  emulate -L zsh
122   if [[ $DISABLE_AUTO_TITLE == true ]]; then
123     return
124   fi
125 
126-  emulate -L zsh
127   setopt extended_glob
128 
129   # cmd name only, or if this is sudo or ssh, the next cmd
130@@ -60,14 +61,28 @@ precmd_functions+=(omz_termsupport_precmd)
131 preexec_functions+=(omz_termsupport_preexec)
132 
133 
134-# Runs before showing the prompt, to update the current directory in Terminal.app
135-function omz_termsupport_cwd {
136-  # Notify Terminal.app of current directory using undocumented OSC sequence
137-  # found in OS X 10.9 and 10.10's /etc/bashrc
138-  if [[ $TERM_PROGRAM == Apple_Terminal ]] && [[ -z $INSIDE_EMACS ]]; then
139-    local PWD_URL="file://$HOSTNAME${PWD// /%20}"
140-    printf '\e]7;%s\a' "$PWD_URL"
141-  fi
142-}
143+# Keep Apple Terminal.app's current working directory updated
144+# Based on this answer: http://superuser.com/a/315029
145+# With extra fixes to handle multibyte chars and non-UTF-8 locales
146 
147-precmd_functions+=(omz_termsupport_cwd)
148+if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
149+
150+  # Emits the control sequence to notify Terminal.app of the cwd
151+  function update_terminalapp_cwd() {
152+    emulate -L zsh
153+    # Identify the directory using a "file:" scheme URL, including
154+    # the host name to disambiguate local vs. remote paths.
155+
156+    # Percent-encode the pathname.
157+    local URL_PATH=$(omz_urlencode -P $PWD)
158+    [[ $? != 0 ]] && return 1
159+    local PWD_URL="file://$HOST$URL_PATH"
160+    # Undocumented Terminal.app-specific control sequence
161+    printf '\e]7;%s\a' $PWD_URL
162+  }
163+
164+  # Use a precmd hook instead of a chpwd hook to avoid contaminating output
165+  precmd_functions+=(update_terminalapp_cwd)
166+  # Run once to get initial cwd set
167+  update_terminalapp_cwd
168+fi
169diff --git a/plugins/svn/svn.plugin.zsh b/plugins/svn/svn.plugin.zsh
170index 9f7a4c6eb4721c57b5f6c1b710a052b494248dc9..816055afe956311ee22ca2b564bb7eb521b3a526 100644
171--- a/plugins/svn/svn.plugin.zsh
172+++ b/plugins/svn/svn.plugin.zsh
173@@ -1,16 +1,17 @@
174 # vim:ft=zsh ts=2 sw=2 sts=2
175 #
176 function svn_prompt_info() {
177+  local _DISPLAY
178   if in_svn; then
179     if [ "x$SVN_SHOW_BRANCH" = "xtrue" ]; then
180       unset SVN_SHOW_BRANCH
181       _DISPLAY=$(svn_get_branch_name)
182     else
183       _DISPLAY=$(svn_get_repo_name)
184+      _DISPLAY=$(omz_urldecode "${_DISPLAY}")
185     fi
186     echo "$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_SVN_PROMPT_PREFIX\
187 $ZSH_THEME_REPO_NAME_COLOR$_DISPLAY$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_SVN_PROMPT_SUFFIX$ZSH_PROMPT_BASE_COLOR$(svn_dirty)$(svn_dirty_pwd)$ZSH_PROMPT_BASE_COLOR"
188-    unset _DISPLAY
189   fi
190 }
191 
192@@ -30,7 +31,7 @@ function svn_get_repo_name() {
193 }
194 
195 function svn_get_branch_name() {
196-  _DISPLAY=$(
197+  local _DISPLAY=$(
198     svn info 2> /dev/null | \
199       awk -F/ \
200       '/^URL:/ { \
201@@ -49,7 +50,6 @@ function svn_get_branch_name() {
202   else
203     echo $_DISPLAY
204   fi
205-  unset _DISPLAY
206 }
207 
208 function svn_get_rev_nr() {
209@@ -60,7 +60,7 @@ function svn_get_rev_nr() {
210 
211 function svn_dirty_choose() {
212   if in_svn; then
213-    root=`svn info 2> /dev/null | sed -n 's/^Working Copy Root Path: //p'`
214+    local root=`svn info 2> /dev/null | sed -n 's/^Working Copy Root Path: //p'`
215     if $(svn status $root 2> /dev/null | command grep -Eq '^\s*[ACDIM!?L]'); then
216       # Grep exits with 0 when "One or more lines were selected", return "dirty".
217       echo $1
218@@ -77,7 +77,7 @@ function svn_dirty() {
219 
220 function svn_dirty_choose_pwd () {
221   if in_svn; then
222-    root=$PWD
223+    local root=$PWD
224     if $(svn status $root 2> /dev/null | command grep -Eq '^\s*[ACDIM!?L]'); then
225       # Grep exits with 0 when "One or more lines were selected", return "dirty".
226       echo $1
227diff --git a/plugins/terminalapp/terminalapp.plugin.zsh b/plugins/terminalapp/terminalapp.plugin.zsh
228index 6e47ee188db466f713b1203ae4918a8ddd6d8982..7c0c278b98387bc3f611884c28fb33d1b856c9d2 100644
229--- a/plugins/terminalapp/terminalapp.plugin.zsh
230+++ b/plugins/terminalapp/terminalapp.plugin.zsh
231@@ -1,39 +1,6 @@
232-# Set Apple Terminal.app resume directory
233-# based on this answer: http://superuser.com/a/315029
234-# 2012-10-26: (javageek) Changed code using the updated answer
235-
236-# Tell the terminal about the working directory whenever it changes.
237-if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
238-  update_terminal_cwd() {
239-        # Identify the directory using a "file:" scheme URL, including
240-        # the host name to disambiguate local vs. remote paths.
241-
242-        # Percent-encode the pathname.
243-        local URL_PATH=''
244-        {
245-            # Use LANG=C to process text byte-by-byte.
246-            local i ch hexch LANG=C
247-            for ((i = 1; i <= ${#PWD}; ++i)); do
248-                ch="$PWD[i]"
249-                if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
250-                    URL_PATH+="$ch"
251-                else
252-                    hexch=$(printf "%02X" "'$ch")
253-                    URL_PATH+="%$hexch"
254-                fi
255-            done
256-        }
257-
258-        local PWD_URL="file://$HOST$URL_PATH"
259-        #echo "$PWD_URL"        # testing
260-        printf '\e]7;%s\a' "$PWD_URL"
261-    }
262-
263-    # Register the function so it is called whenever the working
264-    # directory changes.
265-    autoload add-zsh-hook
266-    add-zsh-hook precmd update_terminal_cwd
267-
268-    # Tell the terminal about the initial directory.
269-    update_terminal_cwd
270-fi
271+# This file is intentionally empty.
272+#
273+# The terminalapp plugin is deprecated and may be removed in a future release.
274+# Its functionality has been folded in to the core lib/termsupport.zsh, which
275+# is loaded for all users. You can remove terminalapp from your $plugins list
276+# once all your systems are updated to the current version of Oh My Zsh.