*** syntax\2html.vim	Sat Jan 17 10:35:14 2009
--- \Documents and Settings\Ben\vimfiles\syntax\2html.vim	Sat Jan 17 11:16:04 2009
***************
*** 1,13 ****
  " Vim syntax support file
  " Maintainer: Bram Moolenaar <Bram@vim.org>
! " Last Change: 2009 Jan 13
! "	       (modified by David Ne\v{c}as (Yeti) <yeti@physics.muni.cz>)
! "	       (XHTML support by Panagiotis Issaris <takis@lumumba.luc.ac.be>)
! "	       (made w3 compliant by Edd Barrett <vext01@gmail.com>)
! "	       (added html_font. Edd Barrett <vext01@gmail.com>)
  
  " Transform a file into HTML, using the current syntax highlighting.
  
  " Number lines when explicitely requested or when `number' is set
  if exists("html_number_lines")
    let s:numblines = html_number_lines
--- 1,18 ----
  " Vim syntax support file
  " Maintainer: Bram Moolenaar <Bram@vim.org>
! " Last Change: 2009 Jan 17
! "              (modified by David Ne\v{c}as (Yeti) <yeti@physics.muni.cz>)
! "              (XHTML support by Panagiotis Issaris <takis@lumumba.luc.ac.be>)
! "              (made w3 compliant by Edd Barrett <vext01@gmail.com>)
! "              (added html_font. Edd Barrett <vext01@gmail.com>)
! "              (dynamic folding by Ben Fritz <fritzophrenic@gmail.com>)
  
  " Transform a file into HTML, using the current syntax highlighting.
  
+ " this file uses line continuations
+ let s:cpo_sav = &cpo
+ set cpo-=C
+ 
  " Number lines when explicitely requested or when `number' is set
  if exists("html_number_lines")
    let s:numblines = html_number_lines
***************
*** 22,27 ****
--- 27,52 ----
    let s:htmlfont = "monospace"
  endif
  
+ " hover opening implies dynamic folding
+ if exists("html_hover_unfold")
+   let html_dynamic_folds = 1
+ endif
+ 
+ " dynamic folding with no foldcolumn implies hover opens
+ if exists("html_dynamic_folds") && exists("html_no_foldcolumn")
+   let html_hover_unfold = 1
+ endif
+ 
+ " ignore folding overrides dynamic folding
+ if exists("html_ignore_folding") && exists("html_dynamic_folds")
+   unlet html_dynamic_folds
+ endif
+ 
+ " dynamic folding implies css
+ if exists("html_dynamic_folds")
+   let html_use_css = 1
+ endif
+ 
  " When not in gui we can only guess the colors.
  if has("gui_running")
    let s:whatterm = "gui"
***************
*** 150,155 ****
--- 175,200 ----
    return a
  endfun
  
+ if exists("html_dynamic_folds")
+ 
+   " compares two folds as stored in our list of folds
+   " A fold is "less" than another if it starts at an earlier line number,
+   " or ends at a later line number, ties broken by fold level
+   function! s:FoldCompare(f1, f2)
+     if a:f1.firstline != a:f2.firstline
+       " put it before if it starts earlier
+       return a:f1.firstline - a:f2.firstline
+     elseif a:f1.lastline != a:f2.lastline
+       " put it before if it ends later
+       return a:f2.lastline - a:f1.lastline
+     else
+       " if folds begin and end on the same lines, put lowest fold level first
+       return a:f1.level - a:f2.level
+     endif
+   endfunction
+ 
+ endif
+ 
  " Figure out proper MIME charset from the 'encoding' option.
  if exists("html_use_encoding")
    let s:html_encoding = html_use_encoding
***************
*** 183,189 ****
    endif
  endif
  
- 
  " Set some options to make it work faster.
  " Don't report changes for :substitute, there will be many of them.
  let s:old_title = &title
--- 228,233 ----
***************
*** 252,259 ****
  endif
  
  if exists("html_use_css")
!   exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e"
  endif
  if exists("html_no_pre")
    exe "normal! a</head>\n<body>\n\e"
  else
--- 296,378 ----
  endif
  
  if exists("html_use_css")
!   if exists("html_dynamic_folds")
!     if exists("html_hover_unfold")
!       " if we are doing hover_unfold, use css 2 with css 1 fallback for IE6
!       exe "normal! a".
!           \ "<style type=\"text/css\">\n<!--\n".
!           \ ".FoldColumn { text-decoration: none; }\n\n".
!           \ "body * { margin: 0; padding: 0; }\n".
!           \ "\n".
!           \ ".open-fold   > .Folded { display: none;  }\n".
!           \ ".open-fold   > .fulltext { display: inline; }\n".
!           \ ".closed-fold > .fulltext { display: none;  }\n".
!           \ ".closed-fold > .Folded { display: inline; }\n".
!           \ "\n".
!           \ ".open-fold   > .toggle-open   { display: none;   }\n".
!           \ ".open-fold   > .toggle-closed { display: inline; }\n".
!           \ ".closed-fold > .toggle-open   { display: inline; }\n".
!           \ ".closed-fold > .toggle-closed { display: none;   }\n"
!       exe "normal! a\n/* opening a fold while hovering won't be supported by IE6 and other\n".
!           \ "similar browsers, but it should fail gracefully. */\n".
!           \ ".closed-fold:hover > .fulltext { display: inline; }\n".
!           \ ".closed-fold:hover > .Folded { display: none; }\n"
!       exe "normal! a-->\n</style>\n"
!       exe "normal! a<!--[if lt IE 7]>".
!             \ "<style type=\"text/css\">\n".
!             \ ".open-fold   .Folded      { display: none; }\n".
!             \ ".open-fold   .fulltext      { display: inline; }\n".
!             \ ".open-fold   .toggle-open   { display: none; }\n".
!             \ ".closed-fold .toggle-closed { display: inline; }\n".
!             \ "\n".
!             \ ".closed-fold .fulltext      { display: none; }\n".
!             \ ".closed-fold .Folded      { display: inline; }\n".
!             \ ".closed-fold .toggle-open   { display: inline; }\n".
!             \ ".closed-fold .toggle-closed { display: none; }\n".
!             \ "</style>\n".
!             \ "<![endif]-->\n"
!     else
!       " if we aren't doing hover_unfold, use CSS 1 only
!       exe "normal! a<style type=\"text/css\">\n<!--\n".
!             \ ".FoldColumn { text-decoration: none; }\n\n".
!             \ ".open-fold   .Folded      { display: none; }\n".
!             \ ".open-fold   .fulltext      { display: inline; }\n".
!             \ ".open-fold   .toggle-open   { display: none; }\n".
!             \ ".closed-fold .toggle-closed { display: inline; }\n".
!             \ "\n".
!             \ ".closed-fold .fulltext      { display: none; }\n".
!             \ ".closed-fold .Folded      { display: inline; }\n".
!             \ ".closed-fold .toggle-open   { display: inline; }\n".
!             \ ".closed-fold .toggle-closed { display: none; }\n".
!             \ "-->\n</style>\n"
!     endif
!   else
!     " if we aren't doing any dynamic folding, no need for any special rules
!     exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e"
!   endif
  endif
+ 
+ if exists("html_dynamic_folds")
+   exe "normal! a\n".
+         \ "<script type='text/javascript'>\n".
+         \ "<!--\n".
+         \ "function toggleFold(objID)\n".
+         \ "{\n".
+         \ "  var fold;\n".
+         \ "  fold = document.getElementById(objID);\n".
+         \ "  if(fold.className == 'closed-fold')\n".
+         \ "  {\n".
+         \ "    fold.className = 'open-fold';\n".
+         \ "  }\n".
+         \ "  else if (fold.className == 'open-fold')\n".
+         \ "  {\n".
+         \ "    fold.className = 'closed-fold';\n".
+         \ "  }\n".
+         \ "}\n".
+         \ "-->\n".
+         \ "</script>\n\e"
+ endif
+ 
  if exists("html_no_pre")
    exe "normal! a</head>\n<body>\n\e"
  else
***************
*** 265,271 ****
  " List of all id's
  let s:idlist = ","
  
! " Loop over all lines in the original text.
  " Use html_start_line and html_end_line if they are set.
  if exists("html_start_line")
    let s:lnum = html_start_line
--- 384,463 ----
  " List of all id's
  let s:idlist = ","
  
! " First do some preprocessing for dynamic folding. Do this for the entire file
! " so we don't accidentally start within a closed fold or something.
! let s:allfolds = []
! 
! if exists("html_dynamic_folds")
!   let s:lnum = 1
!   let s:end = line('$')
!   " save the fold text and set it to standard so we can determine fold levels
!   let s:foldtext_save = &foldtext
!   set foldtext&
! 
!   " we will set the foldcolumn in the html to the greater of the maximum fold
!   " level and the current foldcolumn setting
!   let s:foldcolumn = &foldcolumn
! 
!   " get the fold text and line numbers for all closed folds (except for
!   " duplicates, of which only one will be added)
!   while s:lnum < s:end
!     if foldclosed(s:lnum) == s:lnum
!       " default fold text has '+-' and then a number of dashes equal to fold
!       " level, so subtract 2 from index of first non-dash after the dashes
!       " in order to get the fold level of the current fold
!       let s:level = match(foldtextresult(s:lnum), '+-*\zs[^-]') - 2
!       if s:level+1 > s:foldcolumn
!         let s:foldcolumn = s:level+1
!       endif
!       let s:newfold = {'firstline': s:lnum, 'lastline': foldclosedend(s:lnum), 'level': s:level,'type': "closed-fold"}
!       " only add the fold if it isn't a duplicate (which would be the last
!       " one added)
!       call add(s:allfolds, s:newfold)
!       execute s:lnum."foldopen"
!     else
!       let s:lnum = s:lnum + 1
!     endif
!   endwhile
! 
!   " close all folds to get info for originally open folds
!   silent! %foldclose!
!   let s:lnum = 1
! 
!   " the originally open folds will be all lines folded currently that aren't
!   " already in the list (except for two folds that start and end on the same
!   " line, but there is no real reason to duplicate that in the html)
!   while s:lnum < s:end
!     if foldclosed(s:lnum) == s:lnum
!       " default fold text has '+-' and then a number of dashes equal to fold
!       " level, so subtract 2 from index of first non-dash after the dashes
!       " in order to get the fold level of the current fold
!       let s:level = match(foldtextresult(s:lnum), '+-*\zs[^-]') - 2
!       if s:level+1 > s:foldcolumn
!         let s:foldcolumn = s:level+1
!       endif
!       let s:newfold = {'firstline': s:lnum, 'lastline': foldclosedend(s:lnum), 'level': s:level,'type': "closed-fold"}
!       " only add the fold if we don't already have it
!       if empty(s:allfolds) || index(s:allfolds, s:newfold) == -1
!         let s:newfold.type = "open-fold"
!         call add(s:allfolds, s:newfold)
!       endif
!       execute s:lnum."foldopen"
!     else
!       let s:lnum = s:lnum + 1
!     endif
!   endwhile
! 
!   call sort(s:allfolds, "s:FoldCompare")
! 
!   let &foldtext = s:foldtext_save
!   unlet s:foldtext_save
! 
!   " close all folds again so we can get the fold text as we go
!   silent! %foldclose! 
! endif
! 
! " Now loop over all lines in the original text to convert to html.
  " Use html_start_line and html_end_line if they are set.
  if exists("html_start_line")
    let s:lnum = html_start_line
***************
*** 283,288 ****
--- 475,484 ----
  else
    let s:end = line("$")
  endif
+ 
+ " stack to keep track of all the folds the current line is in
+ let s:foldstack = []
+ 
  if s:numblines
    let s:margin = strlen(s:end) + 1
  else
***************
*** 300,305 ****
--- 496,502 ----
    let s:difffillchar = '-'
  endif
  
+ let s:foldId = 0
  
  while s:lnum <= s:end
  
***************
*** 311,331 ****
        let s:new = repeat(s:difffillchar, 3)
  
        if s:n > 2 && s:n < s:filler && !exists("html_whole_filler")
! 	let s:new = s:new . " " . s:filler . " inserted lines "
! 	let s:n = 2
        endif
  
        if !exists("html_no_pre")
! 	" HTML line wrapping is off--go ahead and fill to the margin
! 	let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin)
        else
! 	let s:new = s:new . repeat(s:difffillchar, 3)
        endif
  
        let s:new = s:HtmlFormat(s:new, "DiffDelete")
        if s:numblines
! 	" Indent if line numbering is on; must be after escaping.
! 	let s:new = repeat(s:LeadingSpace, s:margin) . s:new
        endif
        exe s:newwin . "wincmd w"
        exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
--- 508,528 ----
        let s:new = repeat(s:difffillchar, 3)
  
        if s:n > 2 && s:n < s:filler && !exists("html_whole_filler")
!         let s:new = s:new . " " . s:filler . " inserted lines "
!         let s:n = 2
        endif
  
        if !exists("html_no_pre")
!         " HTML line wrapping is off--go ahead and fill to the margin
!         let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin)
        else
!         let s:new = s:new . repeat(s:difffillchar, 3)
        endif
  
        let s:new = s:HtmlFormat(s:new, "DiffDelete")
        if s:numblines
!         " Indent if line numbering is on; must be after escaping.
!         let s:new = repeat(s:LeadingSpace, s:margin) . s:new
        endif
        exe s:newwin . "wincmd w"
        exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
***************
*** 339,354 ****
  
    " Start the line with the line number.
    if s:numblines
!     let s:new = repeat(' ', s:margin - 1 - strlen(s:lnum)) . s:lnum . ' '
    else
!     let s:new = ""
    endif
  
!   if has('folding') && !exists('html_ignore_folding') && foldclosed(s:lnum) > -1
      "
!     " This is the beginning of a folded block
      "
!     let s:new = s:new . foldtextresult(s:lnum)
      if !exists("html_no_pre")
        " HTML line wrapping is off--go ahead and fill to the margin
        let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
--- 536,553 ----
  
    " Start the line with the line number.
    if s:numblines
!     let s:numcol = repeat(' ', s:margin - 1 - strlen(s:lnum)) . s:lnum . ' '
    else
!     let s:numcol = ""
    endif
  
!   let s:new = ""
! 
!   if has('folding') && !exists('html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('html_dynamic_folds')
      "
!     " This is the beginning of a folded block (with no dynamic folding)
      "
!     let s:new = s:numcol . foldtextresult(s:lnum)
      if !exists("html_no_pre")
        " HTML line wrapping is off--go ahead and fill to the margin
        let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
***************
*** 361,373 ****
  
    else
      "
!     " A line that is not folded.
      "
      let s:line = getline(s:lnum)
      let s:len = strlen(s:line)
  
      if s:numblines
!       let s:new = s:HtmlFormat(s:new, "lnr")
      endif
  
      " Get the diff attribute, if any.
--- 560,637 ----
  
    else
      "
!     " A line that is not folded, or doing dynamic folding.
      "
      let s:line = getline(s:lnum)
+ 
      let s:len = strlen(s:line)
  
+     if exists("html_dynamic_folds")
+       " First close off any open folds that end on this line
+       while !empty(s:foldstack) && get(s:foldstack,0).lastline == s:lnum-1
+         let s:new = s:new."</span></span>"
+         call remove(s:foldstack, 0)
+       endwhile
+ 
+       " Now open any new folds that start on this line
+       let s:firstfold = 1
+       while !empty(s:allfolds) && get(s:allfolds,0).firstline == s:lnum
+         let s:foldId = s:foldId + 1
+         let s:new = s:new . "<span id='fold".s:foldId."' class='".s:allfolds[0].type."'>"
+ 
+         if !exists("html_no_foldcolumn")
+           " add fold column to open the new fold
+           if s:allfolds[0].level > 1 && s:firstfold
+             let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>"
+             let s:new = s:new . repeat('|', s:allfolds[0].level - 1) . "</a>"
+           endif
+           let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>+"
+           let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level) . "</a>"
+ 
+           " add fold column to close the new fold
+           let s:new = s:new . "<a class='toggle-closed FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>"
+           if s:firstfold
+             let s:new = s:new . repeat('|', s:allfolds[0].level - 1)
+           endif
+           let s:new = s:new . "-"
+           " only add a space if we aren't opening another fold on the same line
+           if get(s:allfolds, 1, {'firstline': 0}).firstline != s:lnum
+             let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level)
+           endif
+           let s:new = s:new . "</a>"
+           let s:firstfold = 0
+         endif
+ 
+         " add fold text, moving the span ending to the next line so collapsing
+         " works correctly
+         let s:new = s:new . substitute(s:HtmlFormat(s:numcol . foldtextresult(s:lnum), "Folded"), '</span>', '\r\0', '')
+         let s:new = s:new . "<span class='fulltext'>"
+ 
+         " open the fold now that we have the fold text to allow retrieval of
+         " fold text for subsequent folds
+         execute s:lnum."foldopen"
+         call insert(s:foldstack, remove(s:allfolds,0))
+         let s:foldstack[0].id = s:foldId
+       endwhile
+ 
+       if !exists("html_no_foldcolumn")
+         if empty(s:foldstack)
+           " add the empty foldcolumn for unfolded lines
+             let s:new = s:new . s:HtmlFormat(repeat(' ', s:foldcolumn), "FoldColumn")
+         else
+           " add the fold column for folds not on the opening line
+           if get(s:foldstack, 0).firstline < s:lnum
+             let s:new = s:new . "<a class='FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>"
+             let s:new = s:new . repeat('|', s:foldstack[0].level)
+             let s:new = s:new . repeat(' ', s:foldcolumn - s:foldstack[0].level) . "</a>"
+           endif
+         endif
+       endif
+     endif
+ 
+     " Now continue with the unfolded line text
      if s:numblines
!       let s:new = s:new . s:HtmlFormat(s:numcol, "lnr")
      endif
  
      " Get the diff attribute, if any.
***************
*** 378,408 ****
      while s:col <= s:len || (s:col == 1 && s:diffattr)
        let s:startcol = s:col " The start column for processing text
        if s:diffattr
! 	let s:id = diff_hlID(s:lnum, s:col)
! 	let s:col = s:col + 1
! 	" Speed loop (it's small - that's the trick)
! 	" Go along till we find a change in hlID
! 	while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
! 	if s:len < &columns && !exists("html_no_pre")
! 	  " Add spaces at the end to mark the changed line.
! 	  let s:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin)
! 	  let s:len = &columns
! 	endif
        else
! 	let s:id = synID(s:lnum, s:col, 1)
! 	let s:col = s:col + 1
! 	" Speed loop (it's small - that's the trick)
! 	" Go along till we find a change in synID
! 	while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
        endif
  
        " Expand tabs
        let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol)
        let idx = stridx(s:expandedtab, "\t")
        while idx >= 0
! 	let i = &ts - ((idx + s:startcol - 1) % &ts)
! 	let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', i), '')
! 	let idx = stridx(s:expandedtab, "\t")
        endwhile
  
        " Output the text with the same synID, with class set to {s:id_name}
--- 642,672 ----
      while s:col <= s:len || (s:col == 1 && s:diffattr)
        let s:startcol = s:col " The start column for processing text
        if s:diffattr
!         let s:id = diff_hlID(s:lnum, s:col)
!         let s:col = s:col + 1
!         " Speed loop (it's small - that's the trick)
!         " Go along till we find a change in hlID
!         while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
!         if s:len < &columns && !exists("html_no_pre")
!           " Add spaces at the end to mark the changed line.
!           let s:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin)
!           let s:len = &columns
!         endif
        else
!         let s:id = synID(s:lnum, s:col, 1)
!         let s:col = s:col + 1
!         " Speed loop (it's small - that's the trick)
!         " Go along till we find a change in synID
!         while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
        endif
  
        " Expand tabs
        let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol)
        let idx = stridx(s:expandedtab, "\t")
        while idx >= 0
!         let i = &ts - ((idx + s:startcol - 1) % &ts)
!         let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', i), '')
!         let idx = stridx(s:expandedtab, "\t")
        endwhile
  
        " Output the text with the same synID, with class set to {s:id_name}
***************
*** 420,425 ****
--- 684,703 ----
  " Finish with the last line
  exe s:newwin . "wincmd w"
  
+ if exists("html_dynamic_folds")
+   " finish off any open folds
+   while !empty(s:foldstack)
+     exe "normal! a</span></span>"
+     call remove(s:foldstack, 0)
+   endwhile
+ 
+   " add fold column to the style list if not already there
+   let s:id = hlID('FoldColumn')
+   if stridx(s:idlist, "," . s:id . ",") == -1
+     let s:idlist = s:idlist . s:id . ","
+   endif
+ endif
+ 
  " Close off the font tag that encapsulates the whole <body>
  if !exists("html_use_css")
    exe "normal! a</font>\e"
***************
*** 460,466 ****
      execute "normal! ^cwbody\e"
    endif
  else
!     execute '%s:<body>:<body bgcolor="' . s:bgc . '" text="' . s:fgc . '"><font face="'. s:htmlfont .'">'
  endif
  
  " Line numbering attributes
--- 738,744 ----
      execute "normal! ^cwbody\e"
    endif
  else
!   execute '%s:<body>:<body bgcolor="' . s:bgc . '" text="' . s:fgc . '"><font face="'. s:htmlfont .'">'
  endif
  
  " Line numbering attributes
***************
*** 548,551 ****
      delfunc s:HtmlClosing
    endif
  endif
! silent! unlet s:diffattr s:difffillchar s:foldfillchar s:HtmlSpace s:LeadingSpace s:HtmlEndline
--- 826,839 ----
      delfunc s:HtmlClosing
    endif
  endif
! silent! unlet s:diffattr s:difffillchar s:foldfillchar s:HtmlSpace s:LeadingSpace s:HtmlEndline s:firstfold s:foldcolumn
! unlet s:foldstack s:allfolds s:foldId s:numcol
! 
! if exists("html_dynamic_folds")
!   delfunc s:FoldCompare
! endif
! 
! let &cpo = s:cpo_sav
! unlet s:cpo_sav
! 
! " vim: et sw=2 sts=2
