From d62ce6e88624f5601067e0d5352f453e0888f401 Mon Sep 17 00:00:00 2001 From: Frank Terbeck Date: Mon, 18 Mar 2013 16:03:38 +0100 Subject: [PATCH] zshrc: Add `grml_theme_add_token()' function Signed-off-by: Frank Terbeck --- etc/zsh/zshrc | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 113 insertions(+), 3 deletions(-) diff --git a/etc/zsh/zshrc b/etc/zsh/zshrc index d975fbd..e94f25a 100644 --- a/etc/zsh/zshrc +++ b/etc/zsh/zshrc @@ -1440,7 +1440,8 @@ function prompt_grml-large_setup () { # matter by using zsh's `zstyle' mechanism. typeset -gA grml_prompt_pre_default \ grml_prompt_post_default \ - grml_prompt_token_default + grml_prompt_token_default \ + grml_prompt_token_function grml_prompt_pre_default=( at '' @@ -1505,6 +1506,110 @@ grml_prompt_token_default=( vcs '0' ) +function GRML_theme_add_token_usage () { + cat <<__EOF__ + Usage: grml_theme_add_token [-f|-i] [
 ]
+
+     is the name for the newly added token. If the \`-f' or \`-i' options
+    are used,  is the name of the function (see below for
+    details). Otherwise it is the literal token string to be used. 
 and
+     are optional.
+
+  Options:
+
+    -f    Use a function named \`' each time the token
+                    is to be expanded.
+
+    -i    Use a function named \`' to initialise the
+                    value of the token _once_ at runtime.
+
+    The functions are called with one argument: the tokens new name. The
+    return value is expected in the \$REPLY parameter. The use of these
+    options is mutually exclusive.
+
+  Example:
+
+    To add a new token \`day' that expands to the current weekday in the
+    current locale in green foreground colour, use this:
+
+      grml_theme_add_token day '%D{%A}' '%F{green}' '%f'
+
+    Another example would be support for \$VIRTUAL_ENV:
+
+      function virtual_env_prompt () {
+        REPLY=${VIRTUAL_ENV+${VIRTUAL_ENV:t} }
+      }
+      grml_theme_add_token virtual-env -f virtual_env_prompt
+
+    After that, you will be able to use a changed \`items' style to
+    assemble your prompt.
+__EOF__
+}
+
+function grml_theme_add_token () {
+    emulate -L zsh
+    local name token pre post
+    local -i init funcall
+
+    if (( ARGC == 0 )); then
+        GRML_theme_add_token_usage
+        return 0
+    fi
+
+    init=0
+    funcall=0
+    pre=''
+    post=''
+    name=$1
+    shift
+    if [[ $1 == '-f' ]]; then
+        funcall=1
+        shift
+    elif [[ $1 == '-i' ]]; then
+        init=1
+        shift
+    fi
+
+    if (( ARGC == 0 )); then
+        printf '
+grml_theme_add_token: No token-string/function-name provided!\n\n'
+        GRML_theme_add_token_usage
+        return 1
+    fi
+    token=$1
+    shift
+    if (( ARGC != 0 && ARGC != 2 )); then
+        printf '
+grml_theme_add_token: 
 and  need to by specified _both_!\n\n'
+        GRML_theme_add_token_usage
+        return 1
+    fi
+    if (( ARGC )); then
+        pre=$1
+        post=$2
+        shift 2
+    fi
+
+    if (( ${+grml_prompt_token_default[$name]} )); then
+        printf '
+grml_theme_add_token: Token `%s'\'' exists! Giving up!\n\n' $name
+        GRML_theme_add_token_usage
+        return 2
+    fi
+    if (( init )); then
+        $token $name
+        token=$REPLY
+    fi
+    grml_prompt_pre_default[$name]=$pre
+    grml_prompt_post_default[$name]=$post
+    if (( funcall )); then
+        grml_prompt_token_function[$name]=$token
+        grml_prompt_token_default[$name]=23
+    else
+        grml_prompt_token_default[$name]=$token
+    fi
+}
+
 function grml_typeset_and_wrap () {
     emulate -L zsh
     local target="$1"
@@ -1535,7 +1640,11 @@ function grml_prompt_addto () {
         zstyle -s ":prompt:${grmltheme}:${lr}:items:$it" token new \
             || new=${grml_prompt_token_default[$it]}
         typeset -g "${target}=${(P)target}${apre}"
-        case $it in
+        if (( ${+grml_prompt_token_function[$it]} )); then
+            ${grml_prompt_token_function[$it]} $it
+            typeset -g "${target}=${(P)target}${REPLY}"
+        else
+            case $it in
             battery)
                 grml_typeset_and_wrap $target $new '' ''
                 ;;
@@ -1558,7 +1667,8 @@ function grml_prompt_addto () {
                 fi
                 ;;
             *) typeset -g "${target}=${(P)target}${new}" ;;
-        esac
+            esac
+        fi
         typeset -g "${target}=${(P)target}${apost}"
     done
 }
-- 
2.1.4