Fix yet another bug in the shipped version of vcs_info()
[grml-etc-core.git] / etc / zsh / zshrc
index 51141a1..cb911c1 100644 (file)
@@ -1128,8 +1128,8 @@ fi
 # from <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>,
 # which distributed under the same terms as zsh itself.
 
-# we will only be using one variable, so let the code know now.
-zstyle ':vcs_info:*' max-exports 1
+# we will be using two variables, so let the code know now.
+zstyle ':vcs_info:*' max-exports 2
 
 # vcs_info() documentation:
 #{{{
@@ -1417,17 +1417,21 @@ VCS_INFO_formats () { # {{{
         (( ${#msgs} < 1 )) && msgs[1]=' (%s)-[%b]-'
     fi
 
-    (( ${#msgs} > maxexports )) && msgs[${maxexports},-1]=()
+    (( ${#msgs} > maxexports )) && msgs[$(( maxexports + 1 )),-1]=()
     for i in {1..${#msgs}} ; do
-        zformat -f msg ${msgs[$i]} a:${action} b:${branch} s:${vcs} r:${base:t} R:${base} S:"$(VCS_INFO_reposub ${base})"
+        zformat -f msg ${msgs[$i]}                      \
+                        a:${action}                     \
+                        b:${branch}                     \
+                        r:${base:t}                     \
+                        s:${vcs}                        \
+                        R:${base}                       \
+                        S:"$(VCS_INFO_reposub ${base})"
         msgs[$i]=${msg}
     done
     return 0
 }
 # }}}
 VCS_INFO_maxexports () { #{{{
-    local -ix maxexports
-
     zstyle -s ":vcs_info:${vcs}:${usercontext}" "max-exports" maxexports || maxexports=2
     if [[ ${maxexports} != <-> ]] || (( maxexports < 1 )); then
         printf 'vcs_info(): expecting numeric arg >= 1 for max-exports (got %s).\n' ${maxexports}
@@ -1859,6 +1863,7 @@ vcs_info () { # {{{
     local -i found
     local -a VCSs disabled dps
     local -x vcs usercontext
+    local -ix maxexports
     local -ax msgs
     local -Ax vcs_comm
 
@@ -1925,14 +1930,20 @@ vcs_info () { # {{{
 VCS_INFO_set --nvcs preinit
 # }}}
 
-# change vcs_info formats for the grml prompt
+# Change vcs_info formats for the grml prompt. The 2nd format sets up
+# $vcs_info_msg_1_ to contain "zsh: repo-name" used to set our screen title.
+# TODO: The included vcs_info() version still uses $VCS_INFO_message_N_.
+#       That needs to be the use of $VCS_INFO_message_N_ needs to be changed
+#       to $vcs_info_msg_N_ as soon as we use the included version.
 if [[ "$TERM" == dumb ]] ; then
-    zstyle ':vcs_info:*' actionformats "(%s%)-[%b|%a] "
-    zstyle ':vcs_info:*' formats       "(%s%)-[%b] "
+    zstyle ':vcs_info:*' actionformats "(%s%)-[%b|%a] " "zsh: %r"
+    zstyle ':vcs_info:*' formats       "(%s%)-[%b] "    "zsh: %r"
 else
     # these are the same, just with a lot of colours:
-    zstyle ':vcs_info:*' actionformats "${MAGENTA}(${NO_COLOUR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${YELLOW}|${RED}%a${MAGENTA}]${NO_COLOUR} "
-    zstyle ':vcs_info:*' formats       "${MAGENTA}(${NO_COLOUR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${MAGENTA}]${NO_COLOUR}%} "
+    zstyle ':vcs_info:*' actionformats "${MAGENTA}(${NO_COLOUR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${YELLOW}|${RED}%a${MAGENTA}]${NO_COLOUR} " \
+                                       "zsh: %r"
+    zstyle ':vcs_info:*' formats       "${MAGENTA}(${NO_COLOUR}%s${MAGENTA})${YELLOW}-${MAGENTA}[${GREEN}%b${MAGENTA}]${NO_COLOUR}%} " \
+                                       "zsh: %r"
     zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat "%b${RED}:${YELLOW}%r"
 fi
 
@@ -1955,27 +1966,27 @@ is4 && [[ $NOPRECMD -eq 0 ]] && precmd () {
     # update VCS information
     vcs_info
 
-    # allow manual overwriting of RPROMPT
-    if [[ -n $RPROMPT ]] ; then
-        [[ $TERM == screen* ]] && print -nP "\ekzsh\e\\"
-        # return 0
+    if [[ $TERM == screen* ]] ; then
+        if [[ -n ${VCS_INFO_message_1_} ]] ; then
+            print -nP "\ek${VCS_INFO_message_1_}\e\\"
+        else
+            print -nP "\ekzsh\e\\"
+        fi
     fi
     # just use DONTSETRPROMPT=1 to be able to overwrite RPROMPT
     if [[ $DONTSETRPROMPT -eq 0 ]] ; then
         if [[ $BATTERY -gt 0 ]] ; then
-            # update BATTERY information
+            # update battery (dropped into $PERCENT) information
             battery
-            RPROMPT="%(?..:()% ${PERCENT}${SCREENTITLE}"
-            # RPROMPT="${PERCENT}${SCREENTITLE}"
+            RPROMPT="%(?..:() ${PERCENT}"
         else
-            RPROMPT="%(?..:()% ${SCREENTITLE}"
-            # RPROMPT="${SCREENTITLE}"
+            RPROMPT="%(?..:() "
         fi
     fi
     # adjust title of xterm
     # see http://www.faqs.org/docs/Linux-mini/Xterm-Title.html
     case $TERM in
-        (xterm*|rxvt)
+        (xterm*|rxvt*)
             print -Pn "\e]0;%n@%m: %~\a"
             ;;
     esac
@@ -1996,12 +2007,6 @@ preexec () {
         local CMD="${1[(wr)^(*=*|sudo|ssh|-*)]}$NAME" # use hostname
         echo -ne "\ek$CMD\e\\"
     fi
-# set the screen title to "zsh" when sitting at the command prompt:
-    if [[ "$TERM" == screen* ]] ; then
-        SCREENTITLE=$'%{\ekzsh\e\\%}'
-    else
-        SCREENTITLE=''
-    fi
 # adjust title of xterm
     case $TERM in
         (xterm*|rxvt)
@@ -3922,17 +3927,39 @@ if check_com -c highlight ; then
     compdef _hl_complete hl
 fi
 
-# create small urls via tinyurl.com using wget, grep and sed
-zurl() {
-    [[ -z ${1} ]] && print "please give an url to shrink." && return 1
-    local url=${1}
-    local tiny="http://tinyurl.com/create.php?url="
-    #print "${tiny}${url}" ; return
-    wget  -O-             \
-          -o/dev/null     \
-          "${tiny}${url}" \
-        | grep -Eio 'value="(http://tinyurl.com/.*)"' \
-        | sed 's/value=//;s/"//g'
+# Create small urls via http://tinyurl.com using wget(1).
+function zurl() {
+    [[ -z ${1} ]] && { print "USAGE: zurl <URL>" ; return 1 }
+
+    local PN url tiny grabber search result preview
+    PN=${0}
+    url=${1}
+#   Check existence of given URL with the help of ping(1).
+#   N.B. ping(1) only works without an eventual given protocol.
+    ping -c 1 ${${url#(ftp|http)://}%%/*} >& /dev/null || \
+        read -q "?Given host ${${url#http://*/}%/*} is not reachable by pinging. Proceed anyway? [y|n] "
+
+    if (( $? == 0 )) ; then
+#           Prepend 'http://' to given URL where necessary for later output.
+            [[ ${url} != http(s|)://* ]] && url='http://'${url}
+            tiny='http://tinyurl.com/create.php?url='
+            if check_com -c wget ; then
+                grabber='wget -O- -o/dev/null'
+            else
+                print "wget is not available, but mandatory for ${PN}. Aborting."
+            fi
+#           Looking for i.e.`copy('http://tinyurl.com/7efkze')' in TinyURL's HTML code.
+            search='copy\(?http://tinyurl.com/[[:alnum:]]##*'
+            result=${(M)${${${(f)"$(${=grabber} ${tiny}${url})"}[(fr)${search}*]}//[()\';]/}%%http:*}
+#           TinyURL provides the rather new feature preview for more confidence. <http://tinyurl.com/preview.php>
+            preview='http://preview.'${result#http://}
+
+            printf '%s\n\n' "${PN} - Shrinking long URLs via webservice TinyURL <http://tinyurl.com>."
+            printf '%s\t%s\n\n' 'Given URL:' ${url}
+            printf '%s\t%s\n\t\t%s\n' 'TinyURL:' ${result} ${preview}
+    else
+        return 1
+    fi
 }
 
 #f2# Print a specific line of file(s).