blob: 55399a788e4aec48c865d4e1c7009715cdd83ada [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 Moolenaar8ada2cc2010-07-29 20:43:36 +02003" Last Change: 2010 Jul 29
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('%'))
42 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020043 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020044 if !save_hwf
45 unlet g:html_whole_filler
46 endif
47 call tohtml#Diff2HTML(win_list, buf_list)
48 endif
49
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020050 call tohtml#RestoreUserSettings(old_vals)
51
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020052 unlet g:html_start_line
53 unlet g:html_end_line
54endfunc
55
56func! tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020057 " TODO: add logic for xhtml
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020058 let style = ['-->']
59 let body_line = ''
60
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020061 let html = []
62 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
63 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
64 call add(html, '<html>')
65 call add(html, '<head>')
66 call add(html, '<title>diff</title>')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020067 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'">')
Bram Moolenaar7510fe72010-07-25 12:46:44 +020068 call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020069 " TODO: copy or move encoding logic from 2html.vim so generated markup can
70 " validate without warnings about encoding
71
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020072 call add(html, '</head>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020073 let body_line_num = len(html)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020074 call add(html, '<body>')
75 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020076
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020077 call add(html, '<tr>')
78 for buf in a:win_list
79 call add(html, '<th>'.bufname(buf).'</th>')
80 endfor
81 call add(html, '</tr><tr>')
82
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020083 let diff_style_start = 0
84 let insert_index = 0
85
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020086 for buf in a:buf_list
87 let temp = []
88 exe bufwinnr(buf) . 'wincmd w'
89
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020090 " If text is folded because of user foldmethod settings, etc. we don't want
91 " to act on everything in a fold by mistake.
92 setlocal nofoldenable
93
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020094 " When not using CSS or when using xhtml, the <body> line can be important.
95 " Assume it will be the same for all buffers and grab it from the first
96 " buffer. Similarly, need to grab the body end line as well.
97 if body_line == ''
98 1
99 call search('<body')
100 let body_line = getline('.')
101 $
102 call search('</body>', 'b')
103 let s:body_end_line = getline('.')
104 endif
105
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200106 " Grab the style information. Some of this will be duplicated...
107 1
108 let style_start = search('^<style type="text/css">')
109 1
110 let style_end = search('^</style>')
111 if style_start > 0 && style_end > 0
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200112 let buf_styles = getline(style_start + 1, style_end - 1)
113 for a_style in buf_styles
114 if index(style, a_style) == -1
115 if diff_style_start == 0
116 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
117 let diff_style_start = len(style)-1
118 endif
119 endif
120 call insert(style, a_style, insert_index)
121 let insert_index += 1
122 endif
123 endfor
124 endif
125
126 if diff_style_start != 0
127 let insert_index = diff_style_start
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200128 endif
129
130 " Delete those parts that are not needed so
131 " we can include the rest into the resulting table
132 1,/^<body/d_
133 $
134 ?</body>?,$d_
135 let temp = getline(1,'$')
136 " undo deletion of start and end part
137 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200138 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200139 normal 2u
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200140 call add(html, '<td nowrap valign="top"><div>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200141 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200142 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200143
144 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200145 " TODO: the comment above says we're going to allow saving the file
146 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200147 quit!
148 endfor
149
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200150 let html[body_line_num] = body_line
151
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200152 call add(html, '</tr>')
153 call add(html, '</table>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200154 call add(html, s:body_end_line)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200155 call add(html, '</html>')
156
157 let i = 1
158 let name = "Diff" . ".html"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200159 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200160 while filereadable(name)
161 let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
162 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200163 endwhile
164 exe "topleft new " . name
165 setlocal modifiable
166
167 " just in case some user autocmd creates content in the new buffer, make sure
168 " it is empty before proceeding
169 %d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200170 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200171
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200172 if len(style) > 0
173 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200174 let style_start = search('^</head>')-1
175
176 " Insert javascript to toggle matching folds open and closed in all windows,
177 " if dynamic folding is active.
178 if exists("g:html_dynamic_folds")
179 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200180 \ "<script type='text/javascript'>",
181 \ " <!--",
182 \ " function toggleFold(objID)",
183 \ " {",
184 \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
185 \ " {",
186 \ " var fold;",
187 \ ' fold = document.getElementById("win"+win_num+objID);',
188 \ " if(fold.className == 'closed-fold')",
189 \ " {",
190 \ " fold.className = 'open-fold';",
191 \ " }",
192 \ " else if (fold.className == 'open-fold')",
193 \ " {",
194 \ " fold.className = 'closed-fold';",
195 \ " }",
196 \ " }",
197 \ " }",
198 \ " -->",
199 \ "</script>"
200 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200201 endif
202
203 " Insert styles from all the generated html documents and additional styles
204 " for the table-based layout of the side-by-side diff. The diff should take
205 " up the full browser window (but not more), and be static in size,
206 " horizontally scrollable when the lines are too long. Otherwise, the diff
207 " is pretty useless for really long lines.
208 if exists("g:html_use_css")
209 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200210 \ '<style type="text/css">']+
211 \ style+[
212 \ '<!--',
213 \ 'table { table-layout: fixed; }',
214 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
215 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
216 \ 'td div { overflow: auto; }',
217 \ '-->',
218 \ '</style>'
219 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200220 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200221 endif
222endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200223
224func! tohtml#OverrideUserSettings()
225 let old_settings = {}
226 " make copies of the user-defined settings that we may overrule
227 let old_settings.html_dynamic_folds = exists("g:html_dynamic_folds")
228 let old_settings.html_hover_unfold = exists("g:html_hover_unfold")
229 let old_settings.html_use_css = exists("g:html_use_css")
230
231 " hover opening implies dynamic folding
232 if exists("g:html_hover_unfold")
233 let g:html_dynamic_folds = 1
234 endif
235
236 " dynamic folding with no foldcolumn implies hover opens
237 if exists("g:html_dynamic_folds") && exists("g:html_no_foldcolumn")
238 let g:html_hover_unfold = 1
239 endif
240
241 " ignore folding overrides dynamic folding
242 if exists("g:html_ignore_folding") && exists("g:html_dynamic_folds")
243 unlet g:html_dynamic_folds
244 endif
245
246 " dynamic folding implies css
247 if exists("g:html_dynamic_folds")
248 let g:html_use_css = 1
249 endif
250
251 return old_settings
252endfunc
253
254func! tohtml#RestoreUserSettings(old_settings)
255 " restore any overridden user options
256 if a:old_settings.html_dynamic_folds
257 let g:html_dynamic_folds = 1
258 else
259 unlet! g:html_dynamic_folds
260 endif
261 if a:old_settings.html_hover_unfold
262 let g:html_hover_unfold = 1
263 else
264 unlet! g:html_hover_unfold
265 endif
266 if a:old_settings.html_use_css
267 let g:html_use_css = 1
268 else
269 unlet! g:html_use_css
270 endif
271endfunc
272
273let &cpo = s:cpo_sav
274unlet s:cpo_sav
275
276" Make sure any patches will probably use consistent indent
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200277" vim: ts=8 sw=2 sts=2 noet