comparison runtime/syntax/2html.vim @ 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 7bc41231fbc7
children 1902913f2049
comparison
equal deleted inserted replaced
2303:6ebb886efe3c 2304:a59e6ac5ed28
12 " this file uses line continuations 12 " this file uses line continuations
13 let s:cpo_sav = &cpo 13 let s:cpo_sav = &cpo
14 set cpo-=C 14 set cpo-=C
15 15
16 " Number lines when explicitely requested or when `number' is set 16 " Number lines when explicitely requested or when `number' is set
17 if exists("html_number_lines") 17 if exists("g:html_number_lines")
18 let s:numblines = html_number_lines 18 let s:numblines = html_number_lines
19 else 19 else
20 let s:numblines = &number 20 let s:numblines = &number
21 endif 21 endif
22 22
23 " Font 23 " Font
24 if exists("html_font") 24 if exists("g:html_font")
25 let s:htmlfont = html_font . ", monospace" 25 let s:htmlfont = html_font . ", monospace"
26 else 26 else
27 let s:htmlfont = "monospace" 27 let s:htmlfont = "monospace"
28 endif 28 endif
29 29
30 " make copies of the user-defined settings that we may overrule 30 " make copies of the user-defined settings that we may overrule
31 if exists("html_dynamic_folds") 31 if exists("g:html_dynamic_folds")
32 let s:html_dynamic_folds = 1 32 let s:html_dynamic_folds = 1
33 endif 33 endif
34 if exists("html_hover_unfold") 34 if exists("g:html_hover_unfold")
35 let s:html_hover_unfold = 1 35 let s:html_hover_unfold = 1
36 endif 36 endif
37 if exists("html_use_css") 37 if exists("g:html_use_css")
38 let s:html_use_css = 1 38 let s:html_use_css = 1
39 endif 39 endif
40 40
41 " hover opening implies dynamic folding 41 " hover opening implies dynamic folding
42 if exists("s:html_hover_unfold") 42 if exists("s:html_hover_unfold")
43 let s:html_dynamic_folds = 1 43 let s:html_dynamic_folds = 1
44 endif 44 endif
45 45
46 " dynamic folding with no foldcolumn implies hover opens 46 " dynamic folding with no foldcolumn implies hover opens
47 if exists("s:html_dynamic_folds") && exists("html_no_foldcolumn") 47 if exists("s:html_dynamic_folds") && exists("g:html_no_foldcolumn")
48 let s:html_hover_unfold = 1 48 let s:html_hover_unfold = 1
49 endif 49 endif
50 50
51 " ignore folding overrides dynamic folding 51 " ignore folding overrides dynamic folding
52 if exists("html_ignore_folding") && exists("s:html_dynamic_folds") 52 if exists("g:html_ignore_folding") && exists("s:html_dynamic_folds")
53 unlet s:html_dynamic_folds 53 unlet s:html_dynamic_folds
54 endif 54 endif
55 55
56 " dynamic folding implies css 56 " dynamic folding implies css
57 if exists("s:html_dynamic_folds") 57 if exists("s:html_dynamic_folds")
205 endfunction 205 endfunction
206 206
207 endif 207 endif
208 208
209 " Figure out proper MIME charset from the 'encoding' option. 209 " Figure out proper MIME charset from the 'encoding' option.
210 if exists("html_use_encoding") 210 if exists("g:html_use_encoding")
211 let s:html_encoding = html_use_encoding 211 let s:html_encoding = html_use_encoding
212 else 212 else
213 let s:vim_encoding = &encoding 213 let s:vim_encoding = &encoding
214 if s:vim_encoding =~ '^8bit\|^2byte' 214 if s:vim_encoding =~ '^8bit\|^2byte'
215 let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '') 215 let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '')
266 let s:old_paste = &paste 266 let s:old_paste = &paste
267 set paste 267 set paste
268 let s:old_magic = &magic 268 let s:old_magic = &magic
269 set magic 269 set magic
270 270
271 if exists("use_xhtml") 271 if exists("g:use_xhtml")
272 if s:html_encoding != "" 272 if s:html_encoding != ""
273 exe "normal! a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e" 273 exe "normal! a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e"
274 else 274 else
275 exe "normal! a<?xml version=\"1.0\"?>\n\e" 275 exe "normal! a<?xml version=\"1.0\"?>\n\e"
276 endif 276 endif
278 else 278 else
279 let s:tag_close = '>' 279 let s:tag_close = '>'
280 endif 280 endif
281 281
282 " Cache html_no_pre in case we have to turn it on for non-css mode 282 " Cache html_no_pre in case we have to turn it on for non-css mode
283 if exists("html_no_pre") 283 if exists("g:html_no_pre")
284 let s:old_html_no_pre = html_no_pre 284 let s:old_html_no_pre = html_no_pre
285 endif 285 endif
286 286
287 if !exists("s:html_use_css") 287 if !exists("s:html_use_css")
288 " Can't put font tags in <pre> 288 " Can't put font tags in <pre>
290 endif 290 endif
291 291
292 let s:HtmlSpace = ' ' 292 let s:HtmlSpace = ' '
293 let s:LeadingSpace = ' ' 293 let s:LeadingSpace = ' '
294 let s:HtmlEndline = '' 294 let s:HtmlEndline = ''
295 if exists("html_no_pre") 295 if exists("g:html_no_pre")
296 let s:HtmlEndline = '<br' . s:tag_close 296 let s:HtmlEndline = '<br' . s:tag_close
297 let s:LeadingSpace = '&nbsp;' 297 let s:LeadingSpace = '&nbsp;'
298 let s:HtmlSpace = '\' . s:LeadingSpace 298 let s:HtmlSpace = '\' . s:LeadingSpace
299 endif 299 endif
300 300
385 \ "}\n". 385 \ "}\n".
386 \ "-->\n". 386 \ "-->\n".
387 \ "</script>\n\e" 387 \ "</script>\n\e"
388 endif 388 endif
389 389
390 if exists("html_no_pre") 390 if exists("g:html_no_pre")
391 exe "normal! a</head>\n<body>\n\e" 391 exe "normal! a</head>\n<body>\n\e"
392 else 392 else
393 exe "normal! a</head>\n<body>\n<pre>\n\e" 393 exe "normal! a</head>\n<body>\n<pre>\n\e"
394 endif 394 endif
395 395
472 silent! %foldclose! 472 silent! %foldclose!
473 endif 473 endif
474 474
475 " Now loop over all lines in the original text to convert to html. 475 " Now loop over all lines in the original text to convert to html.
476 " Use html_start_line and html_end_line if they are set. 476 " Use html_start_line and html_end_line if they are set.
477 if exists("html_start_line") 477 if exists("g:html_start_line")
478 let s:lnum = html_start_line 478 let s:lnum = html_start_line
479 if s:lnum < 1 || s:lnum > line("$") 479 if s:lnum < 1 || s:lnum > line("$")
480 let s:lnum = 1 480 let s:lnum = 1
481 endif 481 endif
482 else 482 else
483 let s:lnum = 1 483 let s:lnum = 1
484 endif 484 endif
485 if exists("html_end_line") 485 if exists("g:html_end_line")
486 let s:end = html_end_line 486 let s:end = html_end_line
487 if s:end < s:lnum || s:end > line("$") 487 if s:end < s:lnum || s:end > line("$")
488 let s:end = line("$") 488 let s:end = line("$")
489 endif 489 endif
490 else 490 else
498 let s:margin = strlen(s:end) + 1 498 let s:margin = strlen(s:end) + 1
499 else 499 else
500 let s:margin = 0 500 let s:margin = 0
501 endif 501 endif
502 502
503 if has('folding') && !exists('html_ignore_folding') 503 if has('folding') && !exists('g:html_ignore_folding')
504 let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')] 504 let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
505 if s:foldfillchar == '' 505 if s:foldfillchar == ''
506 let s:foldfillchar = '-' 506 let s:foldfillchar = '-'
507 endif 507 endif
508 endif 508 endif
520 if s:filler > 0 520 if s:filler > 0
521 let s:n = s:filler 521 let s:n = s:filler
522 while s:n > 0 522 while s:n > 0
523 let s:new = repeat(s:difffillchar, 3) 523 let s:new = repeat(s:difffillchar, 3)
524 524
525 if s:n > 2 && s:n < s:filler && !exists("html_whole_filler") 525 if s:n > 2 && s:n < s:filler && !exists("g:html_whole_filler")
526 let s:new = s:new . " " . s:filler . " inserted lines " 526 let s:new = s:new . " " . s:filler . " inserted lines "
527 let s:n = 2 527 let s:n = 2
528 endif 528 endif
529 529
530 if !exists("html_no_pre") 530 if !exists("g:html_no_pre")
531 " HTML line wrapping is off--go ahead and fill to the margin 531 " HTML line wrapping is off--go ahead and fill to the margin
532 let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin) 532 let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin)
533 else 533 else
534 let s:new = s:new . repeat(s:difffillchar, 3) 534 let s:new = s:new . repeat(s:difffillchar, 3)
535 endif 535 endif
556 let s:numcol = "" 556 let s:numcol = ""
557 endif 557 endif
558 558
559 let s:new = "" 559 let s:new = ""
560 560
561 if has('folding') && !exists('html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('s:html_dynamic_folds') 561 if has('folding') && !exists('g:html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('s:html_dynamic_folds')
562 " 562 "
563 " This is the beginning of a folded block (with no dynamic folding) 563 " This is the beginning of a folded block (with no dynamic folding)
564 " 564 "
565 let s:new = s:numcol . foldtextresult(s:lnum) 565 let s:new = s:numcol . foldtextresult(s:lnum)
566 if !exists("html_no_pre") 566 if !exists("g:html_no_pre")
567 " HTML line wrapping is off--go ahead and fill to the margin 567 " HTML line wrapping is off--go ahead and fill to the margin
568 let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new)) 568 let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
569 endif 569 endif
570 570
571 let s:new = s:HtmlFormat(s:new, "Folded") 571 let s:new = s:HtmlFormat(s:new, "Folded")
596 " Unless disabled, add a fold column for the opening line of a fold. 596 " Unless disabled, add a fold column for the opening line of a fold.
597 " 597 "
598 " Note that dynamic folds require using css so we just use css to take 598 " Note that dynamic folds require using css so we just use css to take
599 " care of the leading spaces rather than using &nbsp; in the case of 599 " care of the leading spaces rather than using &nbsp; in the case of
600 " html_no_pre to make it easier 600 " html_no_pre to make it easier
601 if !exists("html_no_foldcolumn") 601 if !exists("g:html_no_foldcolumn")
602 " add fold column that can open the new fold 602 " add fold column that can open the new fold
603 if s:allfolds[0].level > 1 && s:firstfold 603 if s:allfolds[0].level > 1 && s:firstfold
604 let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>" 604 let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>"
605 let s:new = s:new . repeat('|', s:allfolds[0].level - 1) . "</a>" 605 let s:new = s:new . repeat('|', s:allfolds[0].level - 1) . "</a>"
606 endif 606 endif
645 " Unless disabled, add a fold column for other lines. 645 " Unless disabled, add a fold column for other lines.
646 " 646 "
647 " Note that dynamic folds require using css so we just use css to take 647 " Note that dynamic folds require using css so we just use css to take
648 " care of the leading spaces rather than using &nbsp; in the case of 648 " care of the leading spaces rather than using &nbsp; in the case of
649 " html_no_pre to make it easier 649 " html_no_pre to make it easier
650 if !exists("html_no_foldcolumn") 650 if !exists("g:html_no_foldcolumn")
651 if empty(s:foldstack) 651 if empty(s:foldstack)
652 " add the empty foldcolumn for unfolded lines 652 " add the empty foldcolumn for unfolded lines
653 let s:new = s:new . s:HtmlFormat(repeat(' ', s:foldcolumn), "FoldColumn") 653 let s:new = s:new . s:HtmlFormat(repeat(' ', s:foldcolumn), "FoldColumn")
654 else 654 else
655 " add the fold column for folds not on the opening line 655 " add the fold column for folds not on the opening line
678 let s:id = diff_hlID(s:lnum, s:col) 678 let s:id = diff_hlID(s:lnum, s:col)
679 let s:col = s:col + 1 679 let s:col = s:col + 1
680 " Speed loop (it's small - that's the trick) 680 " Speed loop (it's small - that's the trick)
681 " Go along till we find a change in hlID 681 " Go along till we find a change in hlID
682 while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile 682 while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
683 if s:len < &columns && !exists("html_no_pre") 683 if s:len < &columns && !exists("g:html_no_pre")
684 " Add spaces at the end to mark the changed line. 684 " Add spaces at the end to mark the changed line.
685 let s:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin) 685 let s:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin)
686 let s:len = &columns 686 let s:len = &columns
687 endif 687 endif
688 else 688 else
750 " Close off the font tag that encapsulates the whole <body> 750 " Close off the font tag that encapsulates the whole <body>
751 if !exists("s:html_use_css") 751 if !exists("s:html_use_css")
752 exe "normal! a</font>\e" 752 exe "normal! a</font>\e"
753 endif 753 endif
754 754
755 if exists("html_no_pre") 755 if exists("g:html_no_pre")
756 exe "normal! a</body>\n</html>\e" 756 exe "normal! a</body>\n</html>\e"
757 else 757 else
758 exe "normal! a</pre>\n</body>\n</html>\e" 758 exe "normal! a</pre>\n</body>\n</html>\e"
759 endif 759 endif
760 760
776 776
777 " Normal/global attributes 777 " Normal/global attributes
778 " For Netscape 4, set <body> attributes too, though, strictly speaking, it's 778 " For Netscape 4, set <body> attributes too, though, strictly speaking, it's
779 " incorrect. 779 " incorrect.
780 if exists("s:html_use_css") 780 if exists("s:html_use_css")
781 if exists("html_no_pre") 781 if exists("g:html_no_pre")
782 execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e" 782 execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e"
783 else 783 else
784 execute "normal! A\npre { font-family: ". s:htmlfont ."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e" 784 execute "normal! A\npre { font-family: ". s:htmlfont ."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
785 yank 785 yank
786 put 786 put
826 826
827 " Add hyperlinks 827 " Add hyperlinks
828 %s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge 828 %s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge
829 829
830 " The DTD 830 " The DTD
831 if exists("use_xhtml") 831 if exists("g:use_xhtml")
832 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" 832 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"
833 else 833 else
834 exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e" 834 exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e"
835 endif 835 endif
836 836
837 if exists("use_xhtml") 837 if exists("g:use_xhtml")
838 exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e" 838 exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e"
839 endif 839 endif
840 840
841 " Cleanup 841 " Cleanup
842 %s:\s\+$::e 842 %s:\s\+$::e
854 854
855 " Reset old <pre> settings 855 " Reset old <pre> settings
856 if exists("s:old_html_no_pre") 856 if exists("s:old_html_no_pre")
857 let html_no_pre = s:old_html_no_pre 857 let html_no_pre = s:old_html_no_pre
858 unlet s:old_html_no_pre 858 unlet s:old_html_no_pre
859 elseif exists("html_no_pre") 859 elseif exists("g:html_no_pre")
860 unlet html_no_pre 860 unlet html_no_pre
861 endif 861 endif
862 862
863 " Save a little bit of memory (worth doing?) 863 " Save a little bit of memory (worth doing?)
864 unlet s:htmlfont 864 unlet s:htmlfont