diff --git a/runtime/autoload/tohtml.vim b/runtime/autoload/tohtml.vim
--- a/runtime/autoload/tohtml.vim
+++ b/runtime/autoload/tohtml.vim
@@ -1,4 +1,4 @@
 " Vim autoload file for the tohtml plugin.
-" Maintainer: Bram Moolenaar <Bram@vim.org>
-" Last Change: 2010 Jul 11
+" Maintainer: Ben Fritz <fritzophrenic@gmail.com>
+" Last Change: 2010 Jul 13
 "
@@ -4,4 +4,13 @@
 "
-" Diff2HTML() added by Christian Brabandt <cb@256bit.org>
+" Additional contributors:
+"
+"              Original by Bram Moolenar <Bram@vim.org>
+"              Diff2HTML() added by Christian Brabandt <cb@256bit.org>
+"
+"              See Mercurial change logs for more!
+ 
+" this file uses line continuations
+let s:cpo_sav = &cpo
+set cpo-=C
 
 func! tohtml#Convert2HTML(line1, line2)
@@ -6,5 +15,30 @@
 
 func! tohtml#Convert2HTML(line1, line2)
+  " make copies of the user-defined settings that we may overrule
+  let s:html_dynamic_folds = exists("g:html_dynamic_folds")
+  let s:html_hover_unfold = exists("g:html_hover_unfold")
+  let s:html_use_css = exists("g:html_use_css")
+
+  " hover opening implies dynamic folding
+  if exists("g:html_hover_unfold")
+    let g:html_dynamic_folds = 1
+  endif
+
+  " dynamic folding with no foldcolumn implies hover opens
+  if exists("g:html_dynamic_folds") && exists("g:html_no_foldcolumn")
+    let g:html_hover_unfold = 1
+  endif
+
+  " ignore folding overrides dynamic folding
+  if exists("g:html_ignore_folding") && exists("g:html_dynamic_folds")
+    unlet g:html_dynamic_folds
+  endif
+
+  " dynamic folding implies css
+  if exists("g:html_dynamic_folds")
+    let g:html_use_css = 1
+  endif
+
   if !&diff || exists("g:diff_one_file")
     if a:line2 >= a:line1
       let g:html_start_line = a:line1
@@ -17,6 +51,6 @@
   else
     let win_list = []
     let buf_list = []
-    windo | if (&diff) | call add(win_list, winbufnr(0)) | endif
+    windo | if &diff | call add(win_list, winbufnr(0)) | endif
     let save_hwf = exists("g:html_whole_filler")
     let g:html_whole_filler = 1
@@ -21,6 +55,7 @@
     let save_hwf = exists("g:html_whole_filler")
     let g:html_whole_filler = 1
+    let g:html_diff_win_num = 0
     for window in win_list
       exe ":" . bufwinnr(window) . "wincmd w"
       let g:html_start_line = 1
       let g:html_end_line = line('$')
@@ -23,6 +58,7 @@
     for window in win_list
       exe ":" . bufwinnr(window) . "wincmd w"
       let g:html_start_line = 1
       let g:html_end_line = line('$')
+      let g:html_diff_win_num += 1
       runtime syntax/2html.vim
       call add(buf_list, bufnr('%'))
@@ -27,3 +63,4 @@
       runtime syntax/2html.vim
       call add(buf_list, bufnr('%'))
+      "exec '%s#<span id=''\zsfold\d\+\ze''#win'.win_num.'\0#ge'
     endfor
@@ -29,7 +66,8 @@
     endfor
+    unlet g:html_diff_win_num
     if !save_hwf
       unlet g:html_whole_filler
     endif
     call tohtml#Diff2HTML(win_list, buf_list)
   endif
 
@@ -30,9 +68,26 @@
     if !save_hwf
       unlet g:html_whole_filler
     endif
     call tohtml#Diff2HTML(win_list, buf_list)
   endif
 
+  " restore any overridden user options
+  if s:html_dynamic_folds
+    let g:html_dynamic_folds = 1
+  else
+    unlet! g:html_dynamic_folds
+  endif
+  if s:html_hover_unfold
+    let g:html_hover_unfold = 1
+  else
+    unlet! g:html_hover_unfold
+  endif
+  if s:html_use_css
+    let g:html_use_css = 1
+  else
+    unlet! g:html_use_css
+  endif
+
   unlet g:html_start_line
   unlet g:html_end_line
 endfunc
@@ -46,7 +101,9 @@
   call add(html, '<head>')
   call add(html, '<title>diff</title>')
   call add(html, '<meta name="Generator" content="Vim/7.3">')
-  "call add(html, '<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">')
+  " TODO: copy or move encoding logic from 2html.vim so generated markup can
+  " validate without warnings about encoding
+
   call add(html, '</head>')
   call add(html, '<body>')
   call add(html, '<table border="1" width="100%">')
@@ -50,7 +107,7 @@
   call add(html, '</head>')
   call add(html, '<body>')
   call add(html, '<table border="1" width="100%">')
-  "call add(html, '<font face="monospace">')
+
   call add(html, '<tr>')
   for buf in a:win_list
     call add(html, '<th>'.bufname(buf).'</th>')
@@ -78,4 +135,5 @@
     let temp = getline(1,'$')
     " undo deletion of start and end part
     " so we can later save the file as valid html
+    " TODO: restore using grabbed lines if undolevel is 1?
     normal 2u
@@ -81,3 +139,3 @@
     normal 2u
-    call add(html, '<td nowrap valign="top">')
+    call add(html, '<td nowrap valign="top"><div>')
     let html += temp
@@ -83,4 +141,4 @@
     let html += temp
-    call add(html, '</td>')
+    call add(html, '</div></td>')
 
     " Close this buffer
@@ -85,5 +143,7 @@
 
     " Close this buffer
+    " TODO: the comment above says we're going to allow saving the file
+    " later...but here we discard it?
     quit!
   endfor
 
@@ -94,6 +154,7 @@
 
   let i = 1
   let name = "Diff" . ".html"
+  " Find an unused file name if current file name is already in use
   while filereadable(name)
     let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
     let i += 1
@@ -97,7 +158,8 @@
   while filereadable(name)
     let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
     let i += 1
-  endw
-  exe "new " . name
+  endwhile
+  exe "topleft new " . name
+  " TODO: Use setlocal? Why is this even needed?
   set modifiable
   call append(0, html)
@@ -102,4 +164,5 @@
   set modifiable
   call append(0, html)
+
   if len(style) > 0
     1
@@ -104,8 +167,51 @@
   if len(style) > 0
     1
-    let style_start = search('^</head>')
-    call append(style_start, '</style>')
-    call append(style_start, style)
-    call append(style_start, '<style type="text/css">')
+    let style_start = search('^</head>')-1
+
+    " Insert javascript to toggle matching folds open and closed in all windows,
+    " if dynamic folding is active.
+    if exists("g:html_dynamic_folds")
+      call append(style_start, [
+            \  "<script type='text/javascript'>",
+            \  "  <!--",
+            \  "  function toggleFold(objID)",
+            \  "  {",
+            \  "    for (win_num = 1; win_num <= 2; win_num++)",
+            \  "    {",
+            \  "      var fold;",
+            \  '      fold = document.getElementById("win"+win_num+objID);',
+            \  "      if(fold.className == 'closed-fold')",
+            \  "      {",
+            \  "        fold.className = 'open-fold';",
+            \  "      }",
+            \  "      else if (fold.className == 'open-fold')",
+            \  "      {",
+            \  "        fold.className = 'closed-fold';",
+            \  "      }",
+            \  "    }",
+            \  "  }",
+            \  "  -->",
+            \  "</script>"
+            \ ])
+    endif
+
+    " Insert styles from all the generated html documents and additional styles
+    " for the table-based layout of the side-by-side diff. The diff should take
+    " up the full browser window (but not more), and be static in size,
+    " horizontally scrollable when the lines are too long. Otherwise, the diff
+    " is pretty useless for really long lines.
+    if exists("g:html_use_css")
+      call append(style_start, [
+            \ '<style type="text/css">']+
+            \  style+[
+            \ '<!--',
+            \ 'table { table-layout: fixed; }',
+            \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
+            \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
+            \ 'td div { overflow: auto; }',
+            \ '-->',
+            \  '</style>'
+            \ ])
+    endif
   endif
 endfunc
@@ -110,2 +216,8 @@
   endif
 endfunc
+
+let &cpo = s:cpo_sav
+unlet s:cpo_sav
+
+" Make sure any patches will probably use consistent indent
+"   vim: ts=2 sw=2 sts=2 et
diff --git a/runtime/plugin/tohtml.vim b/runtime/plugin/tohtml.vim
--- a/runtime/plugin/tohtml.vim
+++ b/runtime/plugin/tohtml.vim
@@ -1,4 +1,4 @@
 " Vim plugin for converting a syntax highlighted file to HTML.
-" Maintainer: Bram Moolenaar <Bram@vim.org>
-" Last Change: 2010 Jul 11
+" Maintainer: Ben Fritz <fritzophrenic@gmail.com>
+" Last Change: 2010 Jul 13
 "
@@ -4,5 +4,6 @@
 "
-" The core of the code is in $VIMRUNTIME/autoload/tohtml.vim
+" The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
+" $VIMRUNTIME/syntax/2html.vim
 
 " Define the :TOhtml command when:
 " - 'compatible' is not set
@@ -11,3 +12,6 @@
 if !&cp && !exists(":TOhtml") && has("user_commands")
   command -range=% TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
 endif
+
+" Make sure any patches will probably use consistent indent
+"   vim: ts=2 sw=2 sts=2 et
diff --git a/runtime/syntax/2html.vim b/runtime/syntax/2html.vim
--- a/runtime/syntax/2html.vim
+++ b/runtime/syntax/2html.vim
@@ -1,11 +1,16 @@
 " Vim syntax support file
-" Maintainer: Bram Moolenaar <Bram@vim.org>
-" Last Change: 2009 Jul 14
-"	       (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>)
+" Maintainer: Ben Fritz <fritzophrenic@gmail.com>
+" Last Change: 2010 July 13
+"
+" Additional contributors:
+"
+"              Original by Bram Moolenar <Bram@vim.org>
+"              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>
+"
+"              See Mercurial change logs for more!
 
 " Transform a file into HTML, using the current syntax highlighting.
 
@@ -22,8 +27,8 @@
 
 " Font
 if exists("g:html_font")
-  let s:htmlfont = html_font . ", monospace"
+  let s:htmlfont = "'". html_font . "', monospace"
 else
   let s:htmlfont = "monospace"
 endif
 
@@ -26,38 +31,7 @@
 else
   let s:htmlfont = "monospace"
 endif
 
-" make copies of the user-defined settings that we may overrule
-if exists("g:html_dynamic_folds")
-  let s:html_dynamic_folds = 1
-endif
-if exists("g:html_hover_unfold")
-  let s:html_hover_unfold = 1
-endif
-if exists("g:html_use_css")
-  let s:html_use_css = 1
-endif
-
-" hover opening implies dynamic folding
-if exists("s:html_hover_unfold")
-  let s:html_dynamic_folds = 1
-endif
-
-" dynamic folding with no foldcolumn implies hover opens
-if exists("s:html_dynamic_folds") && exists("g:html_no_foldcolumn")
-  let s:html_hover_unfold = 1
-endif
-
-" ignore folding overrides dynamic folding
-if exists("g:html_ignore_folding") && exists("s:html_dynamic_folds")
-  unlet s:html_dynamic_folds
-endif
-
-" dynamic folding implies css
-if exists("s:html_dynamic_folds")
-  let s:html_use_css = 1
-endif
-
 " When not in gui we can only guess the colors.
 if has("gui_running")
   let s:whatterm = "gui"
@@ -98,7 +72,7 @@
   endfun
 endif
 
-if !exists("s:html_use_css")
+if !exists("g:html_use_css")
   " Return opening HTML tag for given highlight id
   function! s:HtmlOpening(id)
     let a = ""
@@ -186,7 +160,7 @@
   return a
 endfun
 
-if exists("s:html_dynamic_folds")
+if exists("g: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,
@@ -284,7 +258,7 @@
   let s:old_html_no_pre = html_no_pre
 endif
 
-if !exists("s:html_use_css")
+if !exists("g:html_use_css")
   " Can't put font tags in <pre>
   let html_no_pre=1
 endif
@@ -307,8 +281,8 @@
   exe "normal! a<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . s:tag_close . "\n\e"
 endif
 
-if exists("s:html_use_css")
-  if exists("s:html_dynamic_folds")
-    if exists("s:html_hover_unfold")
+if exists("g:html_use_css")
+  if exists("g:html_dynamic_folds")
+    if exists("g:html_hover_unfold")
       " if we are doing hover_unfold, use css 2 with css 1 fallback for IE6
       exe "normal! a".
@@ -313,16 +287,16 @@
       " 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; white-space: pre; }\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"
+            \ "<style type=\"text/css\">\n<!--\n".
+            \ ".FoldColumn { text-decoration: none; white-space: pre; }\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".
@@ -328,7 +302,7 @@
       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 > .toggle-filler { display: none; }\n".
-	  \ ".closed-fold:hover > .Folded { display: none; }\n"
+            \ "similar browsers, but it should fail gracefully. */\n".
+            \ ".closed-fold:hover > .fulltext { display: inline; }\n".
+            \ ".closed-fold:hover > .toggle-filler { display: none; }\n".
+            \ ".closed-fold:hover > .Folded { display: none; }\n"
       exe "normal! a-->\n</style>\n"
       exe "normal! a<!--[if lt IE 7]>".
@@ -333,17 +307,17 @@
       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"
+            \ "<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".
@@ -347,17 +321,17 @@
     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; white-space: pre; }\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"
+            \ ".FoldColumn { text-decoration: none; white-space: pre; }\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
@@ -366,5 +340,5 @@
 endif
 
 " insert javascript to toggle folds open and closed
-if exists("s:html_dynamic_folds")
+if exists("g:html_dynamic_folds")
   exe "normal! a\n".
@@ -370,21 +344,21 @@
   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"
+        \ "<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("g:html_no_pre")
@@ -402,7 +376,7 @@
 " so we don't accidentally start within a closed fold or something.
 let s:allfolds = []
 
-if exists("s:html_dynamic_folds")
+if exists("g:html_dynamic_folds")
   let s:lnum = 1
   let s:end = line('$')
   " save the fold text and set it to the default so we can find fold levels
@@ -421,7 +395,7 @@
       " 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
+        let s:foldcolumn = s:level+1
       endif
       " store fold info for later use
       let s:newfold = {'firstline': s:lnum, 'lastline': foldclosedend(s:lnum), 'level': s:level,'type': "closed-fold"}
@@ -446,8 +420,8 @@
       " 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
+        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
@@ -450,9 +424,9 @@
       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)
+        let s:newfold.type = "open-fold"
+        call add(s:allfolds, s:newfold)
       endif
       " open the fold so we can find any contained folds
       execute s:lnum."foldopen"
@@ -523,8 +497,8 @@
       let s:new = repeat(s:difffillchar, 3)
 
       if s:n > 2 && s:n < s:filler && !exists("g:html_whole_filler")
-	let s:new = s:new . " " . s:filler . " inserted lines "
-	let s:n = 2
+        let s:new = s:new . " " . s:filler . " inserted lines "
+        let s:n = 2
       endif
 
       if !exists("g:html_no_pre")
@@ -528,6 +502,6 @@
       endif
 
       if !exists("g: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)
+        " 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
@@ -533,6 +507,6 @@
       else
-	let s:new = s:new . repeat(s:difffillchar, 3)
+        let s:new = s:new . repeat(s:difffillchar, 3)
       endif
 
       let s:new = s:HtmlFormat(s:new, "DiffDelete")
       if s:numblines
@@ -535,9 +509,9 @@
       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
+        " 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"
@@ -558,7 +532,7 @@
 
   let s:new = ""
 
-  if has('folding') && !exists('g:html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('s:html_dynamic_folds')
+  if has('folding') && !exists('g:html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('g:html_dynamic_folds')
     "
     " This is the beginning of a folded block (with no dynamic folding)
     "
@@ -580,6 +554,6 @@
     let s:line = getline(s:lnum)
     let s:len = strlen(s:line)
 
-    if exists("s:html_dynamic_folds")
+    if exists("g:html_dynamic_folds")
       " First insert a closing for any open folds that end on this line
       while !empty(s:foldstack) && get(s:foldstack,0).lastline == s:lnum-1
@@ -584,9 +558,9 @@
       " First insert a closing for 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)
+        let s:new = s:new."</span></span>"
+        call remove(s:foldstack, 0)
       endwhile
 
       " Now insert an opening any new folds that start on this line
       let s:firstfold = 1
       while !empty(s:allfolds) && get(s:allfolds,0).firstline == s:lnum
@@ -588,8 +562,10 @@
       endwhile
 
       " Now insert an opening 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."'>"
+        let s:foldId = s:foldId + 1
+        let s:new .= "<span id='"
+        let s:new .= (exists('g:html_diff_win_num') ? "win".g:html_diff_win_num : "")
+        let s:new .= "fold".s:foldId."' class='".s:allfolds[0].type."'>"
 
@@ -595,24 +571,24 @@
 
-	" Unless disabled, add a fold column for the opening line of a fold.
-	"
-	" Note that dynamic folds require using css so we just use css to take
-	" care of the leading spaces rather than using &nbsp; in the case of
-	" html_no_pre to make it easier
-	if !exists("g:html_no_foldcolumn")
-	  " add fold column that can 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."\")'>+</a>"
-	  let s:new = s:new . "<a class='toggle-open "
-	  " If this is not the last fold we're opening on this line, we need
-	  " to keep the filler spaces hidden if the fold is opened by mouse
-	  " hover. If it is the last fold to open in the line, we shouldn't hide
-	  " them, so don't apply the toggle-filler class.
-	  if get(s:allfolds, 1, {'firstline': 0}).firstline == s:lnum
-	    let s:new = s:new . "toggle-filler "
-	  endif
-	  let s:new = s:new . "FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>"
-	  let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level) . "</a>"
+        " Unless disabled, add a fold column for the opening line of a fold.
+        "
+        " Note that dynamic folds require using css so we just use css to take
+        " care of the leading spaces rather than using &nbsp; in the case of
+        " html_no_pre to make it easier
+        if !exists("g:html_no_foldcolumn")
+          " add fold column that can 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."\")'>+</a>"
+          let s:new = s:new . "<a class='toggle-open "
+          " If this is not the last fold we're opening on this line, we need
+          " to keep the filler spaces hidden if the fold is opened by mouse
+          " hover. If it is the last fold to open in the line, we shouldn't hide
+          " them, so don't apply the toggle-filler class.
+          if get(s:allfolds, 1, {'firstline': 0}).firstline == s:lnum
+            let s:new = s:new . "toggle-filler "
+          endif
+          let s:new = s:new . "FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>"
+          let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level) . "</a>"
 
@@ -618,15 +594,15 @@
 
-	  " add fold column that can 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 spaces 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 column that can 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 spaces 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
 
@@ -632,6 +608,6 @@
 
-	" add fold text, moving the span ending to the next line so collapsing
-	" of folds works correctly
-	let s:new = s:new . substitute(s:HtmlFormat(s:numcol . foldtextresult(s:lnum), "Folded"), '</span>', s:HtmlEndline.'\r\0', '')
-	let s:new = s:new . "<span class='fulltext'>"
+        " add fold text, moving the span ending to the next line so collapsing
+        " of folds works correctly
+        let s:new = s:new . substitute(s:HtmlFormat(s:numcol . foldtextresult(s:lnum), "Folded"), '</span>', s:HtmlEndline.'\r\0', '')
+        let s:new = s:new . "<span class='fulltext'>"
 
@@ -637,9 +613,9 @@
 
-	" 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
+        " 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
 
       " Unless disabled, add a fold column for other lines.
@@ -648,17 +624,17 @@
       " care of the leading spaces rather than using &nbsp; in the case of
       " html_no_pre to make it easier
       if !exists("g: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
+        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
 
@@ -675,14 +651,14 @@
     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("g: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
+        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("g: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
@@ -688,9 +664,9 @@
       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
+        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
@@ -698,24 +674,24 @@
       let s:offset = 0
       let s:idx = stridx(s:expandedtab, "\t")
       while s:idx >= 0
-	if has("multi_byte_encoding")
-	  if s:startcol + s:idx == 1
-	    let s:i = &ts
-	  else
-	    if s:idx == 0
-	      let s:prevc = matchstr(s:line, '.\%' . (s:startcol + s:idx + s:offset) . 'c')
-	    else
-	      let s:prevc = matchstr(s:expandedtab, '.\%' . (s:idx + 1) . 'c')
-	    endif
-	    let s:vcol = virtcol([s:lnum, s:startcol + s:idx + s:offset - len(s:prevc)])
-	    let s:i = &ts - (s:vcol % &ts)
-	  endif
-	  let s:offset -= s:i - 1
-	else
-	  let s:i = &ts - ((s:idx + s:startcol - 1) % &ts)
-	endif
-	let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', s:i), '')
-	let s:idx = stridx(s:expandedtab, "\t")
+        if has("multi_byte_encoding")
+          if s:startcol + s:idx == 1
+            let s:i = &ts
+          else
+            if s:idx == 0
+              let s:prevc = matchstr(s:line, '.\%' . (s:startcol + s:idx + s:offset) . 'c')
+            else
+              let s:prevc = matchstr(s:expandedtab, '.\%' . (s:idx + 1) . 'c')
+            endif
+            let s:vcol = virtcol([s:lnum, s:startcol + s:idx + s:offset - len(s:prevc)])
+            let s:i = &ts - (s:vcol % &ts)
+          endif
+          let s:offset -= s:i - 1
+        else
+          let s:i = &ts - ((s:idx + s:startcol - 1) % &ts)
+        endif
+        let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', s:i), '')
+        let s:idx = stridx(s:expandedtab, "\t")
       endwhile
 
       " Output the text with the same synID, with class set to {s:id_name}
@@ -733,7 +709,7 @@
 " Finish with the last line
 exe s:newwin . "wincmd w"
 
-if exists("s:html_dynamic_folds")
+if exists("g:html_dynamic_folds")
   " finish off any open folds
   while !empty(s:foldstack)
     exe "normal! a</span></span>"
@@ -748,7 +724,7 @@
 endif
 
 " Close off the font tag that encapsulates the whole <body>
-if !exists("s:html_use_css")
+if !exists("g:html_use_css")
   exe "normal! a</font>\e"
 endif
 
@@ -760,7 +736,7 @@
 
 
 " Now, when we finally know which, we define the colors and styles
-if exists("s:html_use_css")
+if exists("g:html_use_css")
   1;/<style type="text/+1
 endif
 
@@ -777,7 +753,7 @@
 " Normal/global attributes
 " For Netscape 4, set <body> attributes too, though, strictly speaking, it's
 " incorrect.
-if exists("s:html_use_css")
+if exists("g:html_use_css")
   if exists("g:html_no_pre")
     execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e"
   else
@@ -792,7 +768,7 @@
 
 " Line numbering attributes
 if s:numblines
-  if exists("s:html_use_css")
+  if exists("g:html_use_css")
     execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
   else
     execute '%s+^<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g'
@@ -811,10 +787,10 @@
   " If the class has some attributes, export the style, otherwise DELETE all
   " its occurences to make the HTML shorter
   if s:attr != ""
-    if exists("s:html_use_css")
+    if exists("g:html_use_css")
       execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
     else
       execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
     endif
   else
     execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+ge'
@@ -815,10 +791,10 @@
       execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
     else
       execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
     endif
   else
     execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+ge'
-    if exists("s:html_use_css")
+    if exists("g:html_use_css")
       1;/<style type="text/+1
     endif
   endif
@@ -870,7 +846,7 @@
   delfunc s:HtmlColor
   delfunc s:HtmlFormat
   delfunc s:CSS1
-  if !exists("s:html_use_css")
+  if !exists("g:html_use_css")
     delfunc s:HtmlOpening
     delfunc s:HtmlClosing
   endif
@@ -878,7 +854,7 @@
 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("s:html_dynamic_folds")
+if exists("g:html_dynamic_folds")
   delfunc s:FoldCompare
 endif
 
@@ -882,8 +858,6 @@
   delfunc s:FoldCompare
 endif
 
-silent! unlet s:html_dynamic_folds s:html_hover_unfold s:html_use_css
-
 let &cpo = s:cpo_sav
 unlet s:cpo_sav
 
@@ -887,4 +861,5 @@
 let &cpo = s:cpo_sav
 unlet s:cpo_sav
 
-" vim: noet sw=2 sts=2
+" Make sure any patches will probably use consistent indent
+"   vim: ts=2 sw=2 sts=2 et
