blob: 620c9c35022753689e38f0b7e899a158e9efb1cc [file] [log] [blame]
Bram Moolenaarb02cbe32010-07-11 22:38:52 +02001" Vim autoload file for the tohtml plugin.
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02002" Maintainer: Ben Fritz <fritzophrenic@gmail.com>
Bram Moolenaar7510fe72010-07-25 12:46:44 +02003" Last Change: 2010 July 24
Bram Moolenaarb02cbe32010-07-11 22:38:52 +02004"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02005" Additional contributors:
6"
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +02007" Original by Bram Moolenaar <Bram@vim.org>
8" Diff2HTML() added by Christian Brabandt <cb@256bit.org>
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02009"
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +020010" See Mercurial change logs for more!
11
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020012" this file uses line continuations
13let s:cpo_sav = &cpo
14set cpo-=C
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020015
16func! tohtml#Convert2HTML(line1, line2)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020017 let old_vals = tohtml#OverrideUserSettings()
18
19 if !&diff || exists("g:html_diff_one_file")
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020020 if a:line2 >= a:line1
21 let g:html_start_line = a:line1
22 let g:html_end_line = a:line2
23 else
24 let g:html_start_line = a:line2
25 let g:html_end_line = a:line1
26 endif
27 runtime syntax/2html.vim
28 else
29 let win_list = []
30 let buf_list = []
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020031 windo | if &diff | call add(win_list, winbufnr(0)) | endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020032 let save_hwf = exists("g:html_whole_filler")
33 let g:html_whole_filler = 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020034 let g:html_diff_win_num = 0
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020035 for window in win_list
36 exe ":" . bufwinnr(window) . "wincmd w"
37 let g:html_start_line = 1
38 let g:html_end_line = line('$')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020039 let g:html_diff_win_num += 1
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020040 runtime syntax/2html.vim
41 call add(buf_list, bufnr('%'))
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020042 "exec '%s#<span id=''\zsfold\d\+\ze''#win'.win_num.'\0#ge'
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020043 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020044 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020045 if !save_hwf
46 unlet g:html_whole_filler
47 endif
48 call tohtml#Diff2HTML(win_list, buf_list)
49 endif
50
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020051 call tohtml#RestoreUserSettings(old_vals)
52
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020053 unlet g:html_start_line
54 unlet g:html_end_line
55endfunc
56
57func! tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020058 " TODO: add logic for xhtml
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020059 let style = []
60 let html = []
61 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
62 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
63 call add(html, '<html>')
64 call add(html, '<head>')
65 call add(html, '<title>diff</title>')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020066 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'">')
Bram Moolenaar7510fe72010-07-25 12:46:44 +020067 call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020068 " TODO: copy or move encoding logic from 2html.vim so generated markup can
69 " validate without warnings about encoding
70
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020071 call add(html, '</head>')
72 call add(html, '<body>')
73 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020074
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020075 call add(html, '<tr>')
76 for buf in a:win_list
77 call add(html, '<th>'.bufname(buf).'</th>')
78 endfor
79 call add(html, '</tr><tr>')
80
81 for buf in a:buf_list
82 let temp = []
83 exe bufwinnr(buf) . 'wincmd w'
84
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020085 " If text is folded because of user foldmethod settings, etc. we don't want
86 " to act on everything in a fold by mistake.
87 setlocal nofoldenable
88
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020089 " Grab the style information. Some of this will be duplicated...
90 1
91 let style_start = search('^<style type="text/css">')
92 1
93 let style_end = search('^</style>')
94 if style_start > 0 && style_end > 0
95 let style += getline(style_start + 1, style_end - 1)
96 endif
97
98 " Delete those parts that are not needed so
99 " we can include the rest into the resulting table
100 1,/^<body/d_
101 $
102 ?</body>?,$d_
103 let temp = getline(1,'$')
104 " undo deletion of start and end part
105 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200106 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200107 normal 2u
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200108 call add(html, '<td nowrap valign="top"><div>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200109 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200110 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200111
112 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200113 " TODO: the comment above says we're going to allow saving the file
114 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200115 quit!
116 endfor
117
118 call add(html, '</tr>')
119 call add(html, '</table>')
120 call add(html, '</body>')
121 call add(html, '</html>')
122
123 let i = 1
124 let name = "Diff" . ".html"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200125 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200126 while filereadable(name)
127 let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
128 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200129 endwhile
130 exe "topleft new " . name
131 setlocal modifiable
132
133 " just in case some user autocmd creates content in the new buffer, make sure
134 " it is empty before proceeding
135 %d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200136 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200137
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200138 if len(style) > 0
139 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200140 let style_start = search('^</head>')-1
141
142 " Insert javascript to toggle matching folds open and closed in all windows,
143 " if dynamic folding is active.
144 if exists("g:html_dynamic_folds")
145 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200146 \ "<script type='text/javascript'>",
147 \ " <!--",
148 \ " function toggleFold(objID)",
149 \ " {",
150 \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
151 \ " {",
152 \ " var fold;",
153 \ ' fold = document.getElementById("win"+win_num+objID);',
154 \ " if(fold.className == 'closed-fold')",
155 \ " {",
156 \ " fold.className = 'open-fold';",
157 \ " }",
158 \ " else if (fold.className == 'open-fold')",
159 \ " {",
160 \ " fold.className = 'closed-fold';",
161 \ " }",
162 \ " }",
163 \ " }",
164 \ " -->",
165 \ "</script>"
166 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200167 endif
168
169 " Insert styles from all the generated html documents and additional styles
170 " for the table-based layout of the side-by-side diff. The diff should take
171 " up the full browser window (but not more), and be static in size,
172 " horizontally scrollable when the lines are too long. Otherwise, the diff
173 " is pretty useless for really long lines.
174 if exists("g:html_use_css")
175 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200176 \ '<style type="text/css">']+
177 \ style+[
178 \ '<!--',
179 \ 'table { table-layout: fixed; }',
180 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
181 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
182 \ 'td div { overflow: auto; }',
183 \ '-->',
184 \ '</style>'
185 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200186 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200187 endif
188endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200189
190func! tohtml#OverrideUserSettings()
191 let old_settings = {}
192 " make copies of the user-defined settings that we may overrule
193 let old_settings.html_dynamic_folds = exists("g:html_dynamic_folds")
194 let old_settings.html_hover_unfold = exists("g:html_hover_unfold")
195 let old_settings.html_use_css = exists("g:html_use_css")
196
197 " hover opening implies dynamic folding
198 if exists("g:html_hover_unfold")
199 let g:html_dynamic_folds = 1
200 endif
201
202 " dynamic folding with no foldcolumn implies hover opens
203 if exists("g:html_dynamic_folds") && exists("g:html_no_foldcolumn")
204 let g:html_hover_unfold = 1
205 endif
206
207 " ignore folding overrides dynamic folding
208 if exists("g:html_ignore_folding") && exists("g:html_dynamic_folds")
209 unlet g:html_dynamic_folds
210 endif
211
212 " dynamic folding implies css
213 if exists("g:html_dynamic_folds")
214 let g:html_use_css = 1
215 endif
216
217 return old_settings
218endfunc
219
220func! tohtml#RestoreUserSettings(old_settings)
221 " restore any overridden user options
222 if a:old_settings.html_dynamic_folds
223 let g:html_dynamic_folds = 1
224 else
225 unlet! g:html_dynamic_folds
226 endif
227 if a:old_settings.html_hover_unfold
228 let g:html_hover_unfold = 1
229 else
230 unlet! g:html_hover_unfold
231 endif
232 if a:old_settings.html_use_css
233 let g:html_use_css = 1
234 else
235 unlet! g:html_use_css
236 endif
237endfunc
238
239let &cpo = s:cpo_sav
240unlet s:cpo_sav
241
242" Make sure any patches will probably use consistent indent
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200243" vim: ts=8 sw=2 sts=2 noet