grml-etc-core: Rewrite of function zurl().
[grml-etc-core.git] / etc / zsh / zshrc
index af0e3a6..fde07a3 100644 (file)
@@ -233,7 +233,12 @@ NOPRECMD=${NOPRECMD:-0}
 BATTERY=${BATTERY:-0}
 GRMLSMALL_SPECIFIC=${GRMLSMALL_SPECIFIC:-1}
 GRML_ALWAYS_LOAD_ALL=${GRML_ALWAYS_LOAD_ALL:-0}
-GRML_WARN_SKEL=${GRML_WARN_SKEL:-1}
+
+if isgrmlcd ; then
+    GRML_WARN_SKEL=${GRML_WARN_SKEL:-0}
+else
+    GRML_WARN_SKEL=${GRML_WARN_SKEL:-1}
+fi
 
 if (( GRML_WARN_SKEL != 0 )) ; then
 
@@ -521,10 +526,12 @@ if [[ -n "$BROKEN_COMPLETION_DIR" ]] ; then
 else
     [[ -d /etc/zsh/completion.d ]] && fpath=( $fpath /etc/zsh/completion.d )
     if [[ -d /etc/zsh/functions.d ]] ; then
-        fpath+=( /etc/zsh/functions.d )
-        for func in /etc/zsh/functions.d/[^_]*[^~](N.) ; do
-            zrcautoload -U ${func:t}
+        fdir='/etc/zsh/functions.d'
+        fpath=( ${fdir} ${fdir}/**/*(/N) ${fpath} )
+        for func in ${fdir}/**/[^_]*[^~](N.) ; do
+            zrcautoload ${func:t}
         done
+        unset fdir
     fi
 fi
 
@@ -1968,7 +1975,7 @@ is4 && [[ $NOPRECMD -eq 0 ]] && precmd () {
     # 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
@@ -2342,7 +2349,7 @@ limit -s
 # {{{ completion system
 
 # called later (via is4 && grmlcomp)
-# notice: use 'zstyle' for getting current settings
+# note: use 'zstyle' for getting current settings
 #         press ^Xh (control-x h) for getting tags in context; ^X? (control-x ?) to run complete_debug with trace output
 grmlcomp() {
     # TODO: This could use some additional information
@@ -3298,7 +3305,7 @@ if [[ -r ~/.terminfo/m/mostlike ]] ; then
 fi
 
 # d():Copyright 2005 Nikolai Weibull <nikolai@bitwi.se>
-# notice: option AUTO_PUSHD has to be set
+# note: option AUTO_PUSHD has to be set
 #f5# Jump between directories
 d() {
     emulate -L zsh
@@ -3915,17 +3922,30 @@ 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}
+#   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}
 }
 
 #f2# Print a specific line of file(s).