changeset 2304:a59e6ac5ed28 vim73

When the buffer is in diff mode, have :TOhtml create HTML to show the diff side-by-side. (Christian Brabandt)
author Bram Moolenaar <bram@vim.org>
date Sun, 11 Jul 2010 22:38:52 +0200
parents 6ebb886efe3c
children 4b00cb7347fc
files runtime/autoload/tohtml.vim runtime/doc/syntax.txt runtime/doc/todo.txt runtime/plugin/tohtml.vim runtime/syntax/2html.vim
diffstat 5 files changed, 156 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/runtime/autoload/tohtml.vim
@@ -0,0 +1,111 @@
+" Vim autoload file for the tohtml plugin.
+" Maintainer: Bram Moolenaar <Bram@vim.org>
+" Last Change: 2010 Jul 11
+"
+" Diff2HTML() added by Christian Brabandt <cb@256bit.org>
+
+func! tohtml#Convert2HTML(line1, line2)
+  if !&diff || exists("g:diff_one_file")
+    if a:line2 >= a:line1
+      let g:html_start_line = a:line1
+      let g:html_end_line = a:line2
+    else
+      let g:html_start_line = a:line2
+      let g:html_end_line = a:line1
+    endif
+    runtime syntax/2html.vim
+  else
+    let win_list = []
+    let buf_list = []
+    windo | if (&diff) | call add(win_list, winbufnr(0)) | endif
+    let save_hwf = exists("g:html_whole_filler")
+    let g:html_whole_filler = 1
+    for window in win_list
+      exe ":" . bufwinnr(window) . "wincmd w"
+      let g:html_start_line = 1
+      let g:html_end_line = line('$')
+      runtime syntax/2html.vim
+      call add(buf_list, bufnr('%'))
+    endfor
+    if !save_hwf
+      unlet g:html_whole_filler
+    endif
+    call tohtml#Diff2HTML(win_list, buf_list)
+  endif
+
+  unlet g:html_start_line
+  unlet g:html_end_line
+endfunc
+
+func! tohtml#Diff2HTML(win_list, buf_list)
+  let style = []
+  let html = []
+  call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
+  call add(html, '  "http://www.w3.org/TR/html4/loose.dtd">')
+  call add(html, '<html>')
+  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">')
+  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>')
+  endfor
+  call add(html, '</tr><tr>')
+
+  for buf in a:buf_list
+    let temp = []
+    exe bufwinnr(buf) . 'wincmd w'
+
+    " Grab the style information.  Some of this will be duplicated...
+    1
+    let style_start = search('^<style type="text/css">')
+    1
+    let style_end = search('^</style>')
+    if style_start > 0 && style_end > 0
+      let style += getline(style_start + 1, style_end - 1)
+    endif
+
+    " Delete those parts that are not needed so
+    " we can include the rest into the resulting table
+    1,/^<body/d_
+    $
+    ?</body>?,$d_
+    let temp = getline(1,'$')
+    " undo deletion of start and end part
+    " so we can later save the file as valid html
+    normal 2u
+    call add(html, '<td nowrap valign="top">')
+    let html += temp
+    call add(html, '</td>')
+
+    " Close this buffer
+    quit!
+  endfor
+
+  call add(html, '</tr>')
+  call add(html, '</table>')
+  call add(html, '</body>')
+  call add(html, '</html>')
+
+  let i = 1
+  let name = "Diff" . ".html"
+  while filereadable(name)
+    let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
+    let i += 1
+  endw
+  exe "new " . name
+  set modifiable
+  call append(0, html)
+  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">')
+  endif
+endfunc
--- a/runtime/doc/syntax.txt
+++ b/runtime/doc/syntax.txt
@@ -387,6 +387,12 @@ Or use the ":TOhtml" user command.  It i
 
 	:10,40TOhtml
 
+":TOhtml" has another special feature: if the window is in diff mode, it will
+generate HTML that shows all the related windows.  This can be disabled by
+setting the g:diff_one_file variable: >
+
+	let g:diff_one_file = 1
+
 After you save the resulting file, you can view it with any browser.  The
 colors should be exactly the same as you see them in Vim.
 
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -1090,6 +1090,7 @@ 2010 Jun 30)
 
 Before (beta) release 7.3:
 - Add fixes for 7.2 to version7.txt
+- Add hg history to version7.txt
 - Rename vim73 branch to default (hints: Xavier de Gaye, 2010 May 23)
 
 Vim 7.3:
@@ -1109,6 +1110,8 @@ Patches to possibly include:
   Update 2009 May 2, 'margincolumn'
   Alternative patch. (2010 Feb 2, Gregor Uhlenheuer, update Apr 18 2010)
   Fix by Lech Lorens, Apr 19
+- Another patch for Javascript indenting. (Hari Kumar, 2010 Jul 11)
+  Needs a few tests.
 - Add different highlighting for a fold line depending on the fold level.
   Patch. (Noel Henson, 2009 Sep 13)
 - Patch to make synIDattr() work for GUI attributes in Vim without GUI
--- a/runtime/plugin/tohtml.vim
+++ b/runtime/plugin/tohtml.vim
@@ -1,27 +1,13 @@
 " Vim plugin for converting a syntax highlighted file to HTML.
 " Maintainer: Bram Moolenaar <Bram@vim.org>
-" Last Change: 2003 Apr 06
-
-" Don't do this when:
-" - when 'compatible' is set
-" - this plugin was already loaded
-" - user commands are not available.
-if !&cp && !exists(":TOhtml") && has("user_commands")
-  command -range=% TOhtml :call Convert2HTML(<line1>, <line2>)
+" Last Change: 2010 Jul 11
+"
+" The core of the code is in $VIMRUNTIME/autoload/tohtml.vim
 
-  func Convert2HTML(line1, line2)
-    if a:line2 >= a:line1
-      let g:html_start_line = a:line1
-      let g:html_end_line = a:line2
-    else
-      let g:html_start_line = a:line2
-      let g:html_end_line = a:line1
-    endif
-
-    runtime syntax/2html.vim
-
-    unlet g:html_start_line
-    unlet g:html_end_line
-  endfunc
-
+" Define the :TOhtml command when:
+" - 'compatible' is not set
+" - this plugin was not already loaded
+" - user commands are available.
+if !&cp && !exists(":TOhtml") && has("user_commands")
+  command -range=% TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
 endif
--- a/runtime/syntax/2html.vim
+++ b/runtime/syntax/2html.vim
@@ -14,27 +14,27 @@ let s:cpo_sav = &cpo
 set cpo-=C
 
 " Number lines when explicitely requested or when `number' is set
-if exists("html_number_lines")
+if exists("g:html_number_lines")
   let s:numblines = html_number_lines
 else
   let s:numblines = &number
 endif
 
 " Font
-if exists("html_font")
+if exists("g:html_font")
   let s:htmlfont = html_font . ", monospace"
 else
   let s:htmlfont = "monospace"
 endif
 
 " make copies of the user-defined settings that we may overrule
-if exists("html_dynamic_folds")
+if exists("g:html_dynamic_folds")
   let s:html_dynamic_folds = 1
 endif
-if exists("html_hover_unfold")
+if exists("g:html_hover_unfold")
   let s:html_hover_unfold = 1
 endif
-if exists("html_use_css")
+if exists("g:html_use_css")
   let s:html_use_css = 1
 endif
 
@@ -44,12 +44,12 @@ if exists("s:html_hover_unfold")
 endif
 
 " dynamic folding with no foldcolumn implies hover opens
-if exists("s:html_dynamic_folds") && exists("html_no_foldcolumn")
+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("html_ignore_folding") && exists("s:html_dynamic_folds")
+if exists("g:html_ignore_folding") && exists("s:html_dynamic_folds")
   unlet s:html_dynamic_folds
 endif
 
@@ -207,7 +207,7 @@ if exists("s:html_dynamic_folds")
 endif
 
 " Figure out proper MIME charset from the 'encoding' option.
-if exists("html_use_encoding")
+if exists("g:html_use_encoding")
   let s:html_encoding = html_use_encoding
 else
   let s:vim_encoding = &encoding
@@ -268,7 +268,7 @@ set paste
 let s:old_magic = &magic
 set magic
 
-if exists("use_xhtml")
+if exists("g:use_xhtml")
   if s:html_encoding != ""
     exe "normal!  a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e"
   else
@@ -280,7 +280,7 @@ else
 endif
 
 " Cache html_no_pre in case we have to turn it on for non-css mode
-if exists("html_no_pre")
+if exists("g:html_no_pre")
   let s:old_html_no_pre = html_no_pre
 endif
 
@@ -292,7 +292,7 @@ endif
 let s:HtmlSpace = ' '
 let s:LeadingSpace = ' '
 let s:HtmlEndline = ''
-if exists("html_no_pre")
+if exists("g:html_no_pre")
   let s:HtmlEndline = '<br' . s:tag_close
   let s:LeadingSpace = '&nbsp;'
   let s:HtmlSpace = '\' . s:LeadingSpace
@@ -387,7 +387,7 @@ if exists("s:html_dynamic_folds")
 	\ "</script>\n\e"
 endif
 
-if exists("html_no_pre")
+if exists("g:html_no_pre")
   exe "normal! a</head>\n<body>\n\e"
 else
   exe "normal! a</head>\n<body>\n<pre>\n\e"
@@ -474,7 +474,7 @@ 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")
+if exists("g:html_start_line")
   let s:lnum = html_start_line
   if s:lnum < 1 || s:lnum > line("$")
     let s:lnum = 1
@@ -482,7 +482,7 @@ if exists("html_start_line")
 else
   let s:lnum = 1
 endif
-if exists("html_end_line")
+if exists("g:html_end_line")
   let s:end = html_end_line
   if s:end < s:lnum || s:end > line("$")
     let s:end = line("$")
@@ -500,7 +500,7 @@ else
   let s:margin = 0
 endif
 
-if has('folding') && !exists('html_ignore_folding')
+if has('folding') && !exists('g:html_ignore_folding')
   let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
   if s:foldfillchar == ''
     let s:foldfillchar = '-'
@@ -522,12 +522,12 @@ while s:lnum <= s:end
     while s:n > 0
       let s:new = repeat(s:difffillchar, 3)
 
-      if s:n > 2 && s:n < s:filler && !exists("html_whole_filler")
+      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
       endif
 
-      if !exists("html_no_pre")
+      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)
       else
@@ -558,12 +558,12 @@ while s:lnum <= s:end
 
   let s:new = ""
 
-  if has('folding') && !exists('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('s: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")
+    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:foldfillchar, &columns - strlen(s:new))
     endif
@@ -598,7 +598,7 @@ while s:lnum <= s:end
 	" 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("html_no_foldcolumn")
+	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."\")'>"
@@ -647,7 +647,7 @@ while s:lnum <= s:end
       " 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("html_no_foldcolumn")
+      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")
@@ -680,7 +680,7 @@ while s:lnum <= s:end
 	" 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")
+	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
@@ -752,7 +752,7 @@ if !exists("s:html_use_css")
   exe "normal! a</font>\e"
 endif
 
-if exists("html_no_pre")
+if exists("g:html_no_pre")
   exe "normal! a</body>\n</html>\e"
 else
   exe "normal! a</pre>\n</body>\n</html>\e"
@@ -778,7 +778,7 @@ endif
 " For Netscape 4, set <body> attributes too, though, strictly speaking, it's
 " incorrect.
 if exists("s:html_use_css")
-  if exists("html_no_pre")
+  if exists("g:html_no_pre")
     execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e"
   else
     execute "normal! A\npre { font-family: ". s:htmlfont ."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
@@ -828,13 +828,13 @@ endwhile
 %s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge
 
 " The DTD
-if exists("use_xhtml")
+if exists("g:use_xhtml")
   exe "normal! gg$a\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\e"
 else
   exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e"
 endif
 
-if exists("use_xhtml")
+if exists("g:use_xhtml")
   exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e"
 endif
 
@@ -856,7 +856,7 @@ exe s:newwin . "wincmd w"
 if exists("s:old_html_no_pre")
   let html_no_pre = s:old_html_no_pre
   unlet s:old_html_no_pre
-elseif exists("html_no_pre")
+elseif exists("g:html_no_pre")
   unlet html_no_pre
 endif