<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text" indent="no" encoding="utf-8" />

<xsl:strip-space elements="*" />

<xsl:template name="escape-quote">
    <xsl:param    name="str" />
    <xsl:variable name="quote">"</xsl:variable>
    <xsl:if test="contains($str, $quote)">
        <xsl:value-of select="substring-before($str, $quote)" />
        <xsl:text>\</xsl:text>
        <xsl:value-of select="$quote" />
        <xsl:call-template name="escape-quote">
            <xsl:with-param name="str" select="substring-after($str, $quote)" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template match="refpurpose">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="refpurpose/function">
  <xsl:value-of select="normalize-space()" />
</xsl:template>

<xsl:template name="prototype-entry">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="normalize-space(refnamediv/refname)" />
    <xsl:text>" : "</xsl:text>
    <xsl:choose>
      <xsl:when test="refsect1/methodsynopsis">
        <xsl:apply-templates select="refsect1/methodsynopsis" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="refnamediv/refname" />
      </xsl:otherwise>
    </xsl:choose>

    <xsl:variable name="purpose">
        <xsl:apply-templates select="refnamediv/refpurpose" />
    </xsl:variable>
    
    <xsl:if test="$purpose != ''">
        <xsl:text> - </xsl:text>
        <xsl:value-of select="$purpose" />
    </xsl:if>
    <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="refnamediv/refpurpose">
    <xsl:call-template name="escape-quote">
        <xsl:with-param name="str" select="normalize-space()" />
    </xsl:call-template>
    <!--<xsl:value-of select="normalize-space()" />-->
</xsl:template>

<!-- Copied from xsl/common.xsl -->
<xsl:template match="methodsynopsis">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="methodsynopsis/type">
  <xsl:value-of select="normalize-space()" />
  <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="methodsynopsis/void">
  <xsl:text>()</xsl:text>
</xsl:template>

<xsl:template match="methodsynopsis/methodname | refnamediv/refname">
  <xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="methodparam/type">
  <xsl:value-of select="normalize-space()" />
  <xsl:text> </xsl:text>
</xsl:template>

<xsl:template match="methodparam/parameter">
  <xsl:if test="@role='reference'">
    <xsl:text>&amp;</xsl:text>
  </xsl:if>
  <xsl:value-of select="normalize-space()" />
</xsl:template>

<xsl:template match="methodparam">
  <xsl:if test="preceding-sibling::methodparam=false()">
    <xsl:text>(</xsl:text>
    <xsl:if test="@choice='opt'">
      <xsl:text>[</xsl:text>
    </xsl:if>
  </xsl:if>
  <xsl:apply-templates select="type | parameter" />
  <xsl:choose>
    <xsl:when test="following-sibling::methodparam">
      <xsl:choose>
        <xsl:when test="following-sibling::methodparam[position()=1]/@choice='opt'">
          <xsl:text> [, </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>, </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="preceding-sibling::methodparam">
				<xsl:if test="@choice='opt'">
					<xsl:text>]</xsl:text>
				</xsl:if>
      </xsl:for-each>
      <xsl:if test="self::methodparam/@choice='opt'">
        <xsl:text>]</xsl:text>
      </xsl:if>
      <xsl:text>)</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="/">
<xsl:text><![CDATA[" PHP prototype display by Jörn Horstmann
" requires vim7

let g:php_prototypes = {
]]></xsl:text>
<xsl:for-each select="//refentry[refsect1][refnamediv]">
    <xsl:text>  \ </xsl:text>
    <xsl:call-template name="prototype-entry" />
    <xsl:if test="position() !=  last()">
        <xsl:text>,&#10;</xsl:text>
    </xsl:if>
</xsl:for-each>
<xsl:text><![CDATA[}

fun! JHShowProto()
    let char = getline(line("."))[col(".")-1]
    if char =~ '\k'
        let word = expand('<cword>')
        if has_key(g:php_prototypes, word)
             echo g:php_prototypes[word]
        else
            echo 'no documentation available'
        endif
    endif
endfunction

fun! JHShowProtoBalloon()
    if has_key(g:php_prototypes, v:beval_text)
        return g:php_prototypes[v:beval_text]
    else
        return 'no documentation available'
    endif
endfunction

set noshowmode
inoremap <buffer> ( <C-O>:call JHShowProto()<CR>()<LEFT>

set bexpr=JHShowProtoBalloon()
set ballooneval
]]></xsl:text>
       
</xsl:template>

</xsl:stylesheet>

