0
0
mirror of https://github.com/vim/vim.git synced 2025-10-05 05:34:07 -04:00

When the buffer is in diff mode, have :TOhtml create HTML to show the diff

side-by-side.  (Christian Brabandt)
This commit is contained in:
Bram Moolenaar
2010-07-11 22:38:52 +02:00
parent 69f787af84
commit b02cbe3468
5 changed files with 155 additions and 49 deletions

111
runtime/autoload/tohtml.vim Normal file
View File

@@ -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

View File

@@ -387,6 +387,12 @@ Or use the ":TOhtml" user command. It is defined in a standard plugin.
:10,40TOhtml :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 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. colors should be exactly the same as you see them in Vim.

View File

@@ -1090,6 +1090,7 @@ Patch to support horizontal scroll wheel in GTK. Untested. (Bjorn Winckler,
Before (beta) release 7.3: Before (beta) release 7.3:
- Add fixes for 7.2 to version7.txt - 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) - Rename vim73 branch to default (hints: Xavier de Gaye, 2010 May 23)
Vim 7.3: Vim 7.3:
@@ -1109,6 +1110,8 @@ Patches to possibly include:
Update 2009 May 2, 'margincolumn' Update 2009 May 2, 'margincolumn'
Alternative patch. (2010 Feb 2, Gregor Uhlenheuer, update Apr 18 2010) Alternative patch. (2010 Feb 2, Gregor Uhlenheuer, update Apr 18 2010)
Fix by Lech Lorens, Apr 19 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. - Add different highlighting for a fold line depending on the fold level.
Patch. (Noel Henson, 2009 Sep 13) Patch. (Noel Henson, 2009 Sep 13)
- Patch to make synIDattr() work for GUI attributes in Vim without GUI - Patch to make synIDattr() work for GUI attributes in Vim without GUI

View File

@@ -1,27 +1,13 @@
" Vim plugin for converting a syntax highlighted file to HTML. " Vim plugin for converting a syntax highlighted file to HTML.
" Maintainer: Bram Moolenaar <Bram@vim.org> " Maintainer: Bram Moolenaar <Bram@vim.org>
" Last Change: 2003 Apr 06 " Last Change: 2010 Jul 11
"
" The core of the code is in $VIMRUNTIME/autoload/tohtml.vim
" Don't do this when: " Define the :TOhtml command when:
" - when 'compatible' is set " - 'compatible' is not set
" - this plugin was already loaded " - this plugin was not already loaded
" - user commands are not available. " - user commands are available.
if !&cp && !exists(":TOhtml") && has("user_commands") if !&cp && !exists(":TOhtml") && has("user_commands")
command -range=% TOhtml :call Convert2HTML(<line1>, <line2>) command -range=% TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
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
endif endif

View File

@@ -14,27 +14,27 @@ let s:cpo_sav = &cpo
set cpo-=C set cpo-=C
" Number lines when explicitely requested or when `number' is set " 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 let s:numblines = html_number_lines
else else
let s:numblines = &number let s:numblines = &number
endif endif
" Font " Font
if exists("html_font") if exists("g:html_font")
let s:htmlfont = html_font . ", monospace" let s:htmlfont = html_font . ", monospace"
else else
let s:htmlfont = "monospace" let s:htmlfont = "monospace"
endif endif
" make copies of the user-defined settings that we may overrule " 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 let s:html_dynamic_folds = 1
endif endif
if exists("html_hover_unfold") if exists("g:html_hover_unfold")
let s:html_hover_unfold = 1 let s:html_hover_unfold = 1
endif endif
if exists("html_use_css") if exists("g:html_use_css")
let s:html_use_css = 1 let s:html_use_css = 1
endif endif
@@ -44,12 +44,12 @@ if exists("s:html_hover_unfold")
endif endif
" dynamic folding with no foldcolumn implies hover opens " 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 let s:html_hover_unfold = 1
endif endif
" ignore folding overrides dynamic folding " 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 unlet s:html_dynamic_folds
endif endif
@@ -207,7 +207,7 @@ if exists("s:html_dynamic_folds")
endif endif
" Figure out proper MIME charset from the 'encoding' option. " 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 let s:html_encoding = html_use_encoding
else else
let s:vim_encoding = &encoding let s:vim_encoding = &encoding
@@ -268,7 +268,7 @@ set paste
let s:old_magic = &magic let s:old_magic = &magic
set magic set magic
if exists("use_xhtml") if exists("g:use_xhtml")
if s:html_encoding != "" if s:html_encoding != ""
exe "normal! a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e" exe "normal! a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e"
else else
@@ -280,7 +280,7 @@ else
endif endif
" Cache html_no_pre in case we have to turn it on for non-css mode " 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 let s:old_html_no_pre = html_no_pre
endif endif
@@ -292,7 +292,7 @@ endif
let s:HtmlSpace = ' ' let s:HtmlSpace = ' '
let s:LeadingSpace = ' ' let s:LeadingSpace = ' '
let s:HtmlEndline = '' let s:HtmlEndline = ''
if exists("html_no_pre") if exists("g:html_no_pre")
let s:HtmlEndline = '<br' . s:tag_close let s:HtmlEndline = '<br' . s:tag_close
let s:LeadingSpace = '&nbsp;' let s:LeadingSpace = '&nbsp;'
let s:HtmlSpace = '\' . s:LeadingSpace let s:HtmlSpace = '\' . s:LeadingSpace
@@ -387,7 +387,7 @@ if exists("s:html_dynamic_folds")
\ "</script>\n\e" \ "</script>\n\e"
endif endif
if exists("html_no_pre") if exists("g:html_no_pre")
exe "normal! a</head>\n<body>\n\e" exe "normal! a</head>\n<body>\n\e"
else else
exe "normal! a</head>\n<body>\n<pre>\n\e" 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. " 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. " 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 let s:lnum = html_start_line
if s:lnum < 1 || s:lnum > line("$") if s:lnum < 1 || s:lnum > line("$")
let s:lnum = 1 let s:lnum = 1
@@ -482,7 +482,7 @@ if exists("html_start_line")
else else
let s:lnum = 1 let s:lnum = 1
endif endif
if exists("html_end_line") if exists("g:html_end_line")
let s:end = html_end_line let s:end = html_end_line
if s:end < s:lnum || s:end > line("$") if s:end < s:lnum || s:end > line("$")
let s:end = line("$") let s:end = line("$")
@@ -500,7 +500,7 @@ else
let s:margin = 0 let s:margin = 0
endif endif
if has('folding') && !exists('html_ignore_folding') if has('folding') && !exists('g:html_ignore_folding')
let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')] let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
if s:foldfillchar == '' if s:foldfillchar == ''
let s:foldfillchar = '-' let s:foldfillchar = '-'
@@ -522,12 +522,12 @@ while s:lnum <= s:end
while s:n > 0 while s:n > 0
let s:new = repeat(s:difffillchar, 3) 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:new = s:new . " " . s:filler . " inserted lines "
let s:n = 2 let s:n = 2
endif endif
if !exists("html_no_pre") if !exists("g:html_no_pre")
" HTML line wrapping is off--go ahead and fill to the 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) let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin)
else else
@@ -558,12 +558,12 @@ while s:lnum <= s:end
let s:new = "" 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) " This is the beginning of a folded block (with no dynamic folding)
" "
let s:new = s:numcol . foldtextresult(s:lnum) 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 " HTML line wrapping is off--go ahead and fill to the margin
let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new)) let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
endif endif
@@ -598,7 +598,7 @@ while s:lnum <= s:end
" Note that dynamic folds require using css so we just use css to take " 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 " care of the leading spaces rather than using &nbsp; in the case of
" html_no_pre to make it easier " 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 " add fold column that can open the new fold
if s:allfolds[0].level > 1 && s:firstfold 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 . "<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 " 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 " care of the leading spaces rather than using &nbsp; in the case of
" html_no_pre to make it easier " html_no_pre to make it easier
if !exists("html_no_foldcolumn") if !exists("g:html_no_foldcolumn")
if empty(s:foldstack) if empty(s:foldstack)
" add the empty foldcolumn for unfolded lines " add the empty foldcolumn for unfolded lines
let s:new = s:new . s:HtmlFormat(repeat(' ', s:foldcolumn), "FoldColumn") 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) " Speed loop (it's small - that's the trick)
" Go along till we find a change in hlID " 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 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. " 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:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin)
let s:len = &columns let s:len = &columns
@@ -752,7 +752,7 @@ if !exists("s:html_use_css")
exe "normal! a</font>\e" exe "normal! a</font>\e"
endif endif
if exists("html_no_pre") if exists("g:html_no_pre")
exe "normal! a</body>\n</html>\e" exe "normal! a</body>\n</html>\e"
else else
exe "normal! a</pre>\n</body>\n</html>\e" 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 " For Netscape 4, set <body> attributes too, though, strictly speaking, it's
" incorrect. " incorrect.
if exists("s:html_use_css") 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" execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e"
else else
execute "normal! A\npre { font-family: ". s:htmlfont ."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e" 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 %s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge
" The DTD " 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" 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 else
exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e" exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e"
endif endif
if exists("use_xhtml") if exists("g:use_xhtml")
exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e" exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e"
endif endif
@@ -856,7 +856,7 @@ exe s:newwin . "wincmd w"
if exists("s:old_html_no_pre") if exists("s:old_html_no_pre")
let html_no_pre = s:old_html_no_pre let html_no_pre = s:old_html_no_pre
unlet 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 unlet html_no_pre
endif endif