#zdep zshrc.d/options,zshrc.d/modules,zshrc.d/misc
### vim:ft=zsh:foldmethod=marker
###
### styles - zstyle invocation for everything except compsys
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Sun Feb 21 22:36:27 2010
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###

##
#   vcs_info setup
#
zstyle ':vcs_info:*' max-exports 2
zstyle ':vcs_info:*' actionformats " $Pfg[ma]($PNC%s$Pfg[ma])$Pfg[ye]-$Pfg[ma][$Pfg[grn]%b$Pfg[ye]|$Pfg[red]%a$Pfg[ma]]$PNC-" \
                                   "zsh: %r %u"
zstyle ':vcs_info:*' formats       " $Pfg[ma]($PNC%s$Pfg[ma])$Pfg[ye]-$Pfg[ma][$Pfg[grn]%b$Pfg[ma]]$PNC%}-" \
                                   "zsh: %r %u"

zstyle ':vcs_info:git:*' actionformats " $Pfg[ma][$Pfg[grn]%b$Pfg[red]%c%u$Pfg[ye]|$Pfg[red]%a$Pfg[ma]]$PNC-" \
                                   "zsh: %r"
zstyle ':vcs_info:git:*' formats       " $Pfg[ma][$Pfg[grn]%b$Pfg[red]%c%u$Pfg[ma]]$PNC-" \
                                   "zsh: %r"
zstyle ':vcs_info:cvs:*' formats       " $Pfg[ma]($PNC%s$Pfg[ma])$Pfg[ye]-$Pfg[ma][$Pfg[grn]%r$Pfg[ma]]$PNC-" \
                                   "zsh: %r"

zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat "%b$Pfg[red]:$Pfg[ye]%r"

zstyle ':vcs_info:bzr:*' use-simple true
zstyle ':vcs_info:*'     enable git hg cvs svn

function estyle-cfc() {
    local d
    local -a cfc_dirs
    cfc_dirs=( ${HOME}/src/code/*(/) )

    for d in ${cfc_dirs}; do
        d=${d%/##}
        [[ $PWD == $d(|/*) ]] && return 0
    done
    return 1
}

zstyle -e ':vcs_info:git:*' check-for-changes 'estyle-cfc && reply=( true ) || reply=( false )'

##
#   ft-complete
#
zstyle ':zle:ft-complete:tilde' widget ft-complete-tilde
zstyle ':zle:ft-complete:empty' action dot-slash-complete

##
#   acceptline setup
#
zstyle ':acceptline:empty' call_default false
zstyle ':acceptline:*' nocompwarn   true

if zis_431 "atleast" && check_com lookup; then
    ##
    #   lookup setup
    #
    zstyle ':lookup:*'         use-pager   true
    zstyle ':lookup:*'         pager-auto  true
    zstyle ':lookup:*'         pager       'less -M'
    zstyle ':lookup:*'         txt-browser 'w3m'
    zstyle ':lookup:*'         txt-formats '%s'
    zstyle ':lookup:*'         gui-browser '/opt/firefox/firefox'
    zstyle ':lookup:*'         gui-formats '-remote' 'openurl(%s)'
    zstyle ':lookup:*:rfc:*'   search-path ~/share/rfc /usr/share/doc/RFC/links
    zstyle ':lookup:*:rfc:*'   save-path   ~/share/rfc

    # Here's a nice one: I got this script cmus-rstat.sh, which tells me which
    # track cmus (my audio player) is currently playing in this format:
    #       np: Artist - Album - Track
    #
    # Well, that's easily parsable. :)
    # Now I can make automated searches via letssingit.com with lookup's
    # query-handler feature like this:
    #
    # If I want to search for the artist:
    #   % lookup -Q letssingit -m artist
    # or the album:
    #   % lookup -Q letssingit -m album
    # or the track name (for instant sing-along :->):
    #   % lookup -Q letssingit -m song
    #
    zstyle ':lookup:*:letssingit:*' query-handlers letssingit
    function LOOKUP_qh_letssingit() {
        local mode=${lookup_communicate[mode]}
        local np

        case ${mode} in
        (artist|album|song)
            np="$(cmus-rstat.sh)"
            np=${np/np: /}
            if [[ ${np} == '[cmus]: -stopped-' ]] ; then
                printf '\n  Cmus is currently playing nothing!\n\n'
                return 3
            fi
            ;;
        esac

        case ${mode} in
        (artist)
            QUERY=${np%% - *}
            ;;
        (album)
            np=${np% - *}
            if [[ ${np} == *' - '* ]] ; then
                QUERY=${np#* - }
            else
                printf '\n Could not get album (%s)!\n\n' "$(cmus-rstat.sh)"
                return 3
            fi
            ;;
        (song)
            QUERY=${np##* - }
            ;;
        esac

        return 0
    }

    # aliases for lookup backends
    lookup -a s=google
    lookup -a d=leo
    lookup -a we="wikipedia -l en"
    lookup -a wd="wikipedia -l de"
fi

##
#   exec_on_xclip setup
#
#zstyle ':exec_on_xclip:x11:*' operate false
zstyle ':exec_on_xclip:*' contexts "0:uri_http"  'http://*'  \
                                   "1:file_gz"   '(/|~)*.gz' \
                                   "2:file"      '(/|~)*'
zstyle ':exec_on_xclip:*' stages   '0:nox' '1:x11'
zstyle ':exec_on_xclip:*' xclip    "xclip"
zstyle ':exec_on_xclip:*' options  "-o"
zstyle ':exec_on_xclip:*' pager    "less -Mr"
zstyle ':exec_on_xclip:nox:*'         clearterm true
if   [[ -x $(which opera) ]] ; then
    zstyle ':exec_on_xclip:x11:uri_http'  app 'opera -remote openURL(_eoxc_)'
elif [[ -x $(which firefox) ]] ; then
    zstyle ':exec_on_xclip:x11:uri_http'  app 'firefox -new-tab _eoxc_'
else
    zstyle ':exec_on_xclip:x11:uri_http'  app false
fi
zstyle ':exec_on_xclip:*:uri_http'    app "${BROWSER:-w3m} _eoxc_"
zstyle ':exec_on_xclip:*:file_gz'     app "zless _eoxc_"
zstyle ':exec_on_xclip:*:file'        app "less _eoxc_"

##
#   setup for directory based profiles
#

# styles
zstyle ':chpwd:profiles:/usr/src/sys/zsh.git(|/|/*)'    profile zshsrc
zstyle ':chpwd:profiles:/usr/src/sys/git(|/|/*)'        profile gitsrc
zstyle ':chpwd:profiles:'${HOME}'/src/code/cmus(|/|/*)' profile cmussrc

# functions
function chpwd_switch_profile_ () {
    [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
    print "chpwd(): Switching to profile: $profile"
    return 0
}

function chpwd_profile_zshsrc() {
    chpwd_switch_profile_ || return 1
    export VIM_PLEASE_USE_INDENT='3'
}

function chpwd_profile_cmussrc() {
    chpwd_switch_profile_ || return 1
    export VIM_PLEASE_USE_INDENT='4'
}

function chpwd_profile_gitsrc() {
    chpwd_profile_cmussrc
}

function chpwd_profile_default() {
    if [[ $1 != 'init' ]] ; then
        chpwd_switch_profile_ || return 1
    fi
    export VIM_PLEASE_USE_INDENT='2'
}

# get the right profile running
chpwd_profiles > /dev/null

##
#   setup for gitfp()
#
function +gitfp_zsh_ps_fix() {
    # zsh-workers prefers PATCH: as its prefix.
    #   fixes [PATCH] to PATCH:
    #     and [PATCH m/n] to PATCH: (m/n)

    [ -z "$1" ] && return 1

    (
        printf '/^Subject: \[\n'
        printf 's,\[PATCH \([0-9]\+/[0-9]\+\)\],PATCH: (\\1),\n'
        printf 'w\nq\n'
    ) | ed "$1" > /dev/null 2>&1

    [ "$?" -eq 0 ] && return 0

    (
        printf '/^Subject: \[\n'
        printf 's,\[PATCH\],PATCH:,\n'
        printf 'w\nq\n'
    ) | ed "$1" > /dev/null 2>&1

    return 0
}

# The 3rd part of the context is the currently active profile
# from the directory based contexts, which are setup above.
zstyle ':functions:gitfp:zshsrc' hooks +gitfp_zsh_ps_fix
zstyle ':functions:gitfp:zshsrc' options --src-prefix= --dst-prefix=