blob: c24d6ff4274264196b48731bc5742a70980aa2f3 [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 Moolenaar8df7f882010-08-13 11:30:02 +02003" Last Change: 2010 Aug 12
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 Moolenaar076e8b22010-08-05 21:54:00 +020017 let s:settings = tohtml#GetUserSettings()
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020018
Bram Moolenaar076e8b22010-08-05 21:54:00 +020019 if !&diff || s:settings.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 Moolenaar076e8b22010-08-05 21:54:00 +020032 let s:settings.whole_filler = 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020033 let g:html_diff_win_num = 0
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020034 for window in win_list
35 exe ":" . bufwinnr(window) . "wincmd w"
36 let g:html_start_line = 1
37 let g:html_end_line = line('$')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020038 let g:html_diff_win_num += 1
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020039 runtime syntax/2html.vim
40 call add(buf_list, bufnr('%'))
41 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020042 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020043 call tohtml#Diff2HTML(win_list, buf_list)
44 endif
45
46 unlet g:html_start_line
47 unlet g:html_end_line
Bram Moolenaar076e8b22010-08-05 21:54:00 +020048 unlet s:settings
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020049endfunc
50
51func! tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020052 let xml_line = ""
53 let tag_close = '>'
54
Bram Moolenaar8df7f882010-08-13 11:30:02 +020055 let s:old_paste = &paste
56 set paste
57 let s:old_magic = &magic
58 set magic
59
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020060 if s:settings.use_xhtml
61 if s:settings.encoding != ""
62 let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>"
63 else
64 let xml_line = "<?xml version=\"1.0\"?>"
65 endif
66 let tag_close = ' />'
67 endif
68
69 let style = [s:settings.use_xhtml ? "" : '-->']
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020070 let body_line = ''
71
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020072 let html = []
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020073 if s:settings.use_xhtml
74 call add(html, xml_line)
75 endif
76 if s:settings.use_xhtml
77 call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
78 call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">')
79 elseif s:settings.use_css && !s:settings.no_pre
80 call add(html, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">")
81 call add(html, '<html>')
82 else
83 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
84 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
85 call add(html, '<html>')
86 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020087 call add(html, '<head>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020088
89 " include encoding as close to the top as possible, but only if not already
90 " contained in XML information (to avoid haggling over content type)
91 if s:settings.encoding != "" && !s:settings.use_xhtml
92 call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close)
93 endif
94
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020095 call add(html, '<title>diff</title>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020096 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close)
97 call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close)
98 call add(html, '<meta name="settings" content="'.
99 \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
100 \ '"'.tag_close)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200101
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200102 call add(html, '</head>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200103 let body_line_num = len(html)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200104 call add(html, '<body>')
105 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200106
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200107 call add(html, '<tr>')
108 for buf in a:win_list
109 call add(html, '<th>'.bufname(buf).'</th>')
110 endfor
111 call add(html, '</tr><tr>')
112
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200113 let diff_style_start = 0
114 let insert_index = 0
115
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200116 for buf in a:buf_list
117 let temp = []
118 exe bufwinnr(buf) . 'wincmd w'
119
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200120 " If text is folded because of user foldmethod settings, etc. we don't want
121 " to act on everything in a fold by mistake.
122 setlocal nofoldenable
123
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200124 " When not using CSS or when using xhtml, the <body> line can be important.
125 " Assume it will be the same for all buffers and grab it from the first
126 " buffer. Similarly, need to grab the body end line as well.
127 if body_line == ''
128 1
129 call search('<body')
130 let body_line = getline('.')
131 $
132 call search('</body>', 'b')
133 let s:body_end_line = getline('.')
134 endif
135
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200136 " Grab the style information. Some of this will be duplicated...
137 1
138 let style_start = search('^<style type="text/css">')
139 1
140 let style_end = search('^</style>')
141 if style_start > 0 && style_end > 0
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200142 let buf_styles = getline(style_start + 1, style_end - 1)
143 for a_style in buf_styles
144 if index(style, a_style) == -1
145 if diff_style_start == 0
146 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
147 let diff_style_start = len(style)-1
148 endif
149 endif
150 call insert(style, a_style, insert_index)
151 let insert_index += 1
152 endif
153 endfor
154 endif
155
156 if diff_style_start != 0
157 let insert_index = diff_style_start
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200158 endif
159
160 " Delete those parts that are not needed so
161 " we can include the rest into the resulting table
162 1,/^<body/d_
163 $
164 ?</body>?,$d_
165 let temp = getline(1,'$')
166 " undo deletion of start and end part
167 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200168 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200169 normal 2u
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200170 if s:settings.use_css
171 call add(html, '<td valign="top"><div>')
172 elseif s:settings.use_xhtml
173 call add(html, '<td nowrap="nowrap" valign="top"><div>')
174 else
175 call add(html, '<td nowrap valign="top"><div>')
176 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200177 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200178 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200179
180 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200181 " TODO: the comment above says we're going to allow saving the file
182 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200183 quit!
184 endfor
185
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200186 let html[body_line_num] = body_line
187
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200188 call add(html, '</tr>')
189 call add(html, '</table>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200190 call add(html, s:body_end_line)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200191 call add(html, '</html>')
192
193 let i = 1
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200194 let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html")
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200195 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200196 while filereadable(name)
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200197 let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e")
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200198 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200199 endwhile
200 exe "topleft new " . name
201 setlocal modifiable
202
203 " just in case some user autocmd creates content in the new buffer, make sure
204 " it is empty before proceeding
205 %d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200206 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200207
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200208 if len(style) > 0
209 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200210 let style_start = search('^</head>')-1
211
212 " Insert javascript to toggle matching folds open and closed in all windows,
213 " if dynamic folding is active.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200214 if s:settings.dynamic_folds
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200215 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200216 \ "<script type='text/javascript'>",
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200217 \ s:settings.use_xhtml ? '//<![CDATA[' : " <!--",
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200218 \ " function toggleFold(objID)",
219 \ " {",
220 \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
221 \ " {",
222 \ " var fold;",
223 \ ' fold = document.getElementById("win"+win_num+objID);',
224 \ " if(fold.className == 'closed-fold')",
225 \ " {",
226 \ " fold.className = 'open-fold';",
227 \ " }",
228 \ " else if (fold.className == 'open-fold')",
229 \ " {",
230 \ " fold.className = 'closed-fold';",
231 \ " }",
232 \ " }",
233 \ " }",
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200234 \ s:settings.use_xhtml ? '//]]>' : " -->",
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200235 \ "</script>"
236 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200237 endif
238
239 " Insert styles from all the generated html documents and additional styles
240 " for the table-based layout of the side-by-side diff. The diff should take
241 " up the full browser window (but not more), and be static in size,
242 " horizontally scrollable when the lines are too long. Otherwise, the diff
243 " is pretty useless for really long lines.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200244 if s:settings.use_css
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200245 call append(style_start,
246 \ ['<style type="text/css">']+
247 \ style+
248 \ [ s:settings.use_xhtml ? '' : '<!--',
249 \ 'table { table-layout: fixed; }',
250 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
251 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
252 \ 'td div { overflow: auto; }',
253 \ s:settings.use_xhtml ? '' : '-->',
254 \ '</style>'
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200255 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200256 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200257 endif
Bram Moolenaar8df7f882010-08-13 11:30:02 +0200258
259 let &paste = s:old_paste
260 let &magic = s:old_magic
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200261endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200262
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200263" Gets a single user option and sets it in the passed-in Dict, or gives it the
264" default value if the option doesn't actually exist.
265func! tohtml#GetOption(settings, option, default)
266 if exists('g:html_'.a:option)
267 let a:settings[a:option] = g:html_{a:option}
268 else
269 let a:settings[a:option] = a:default
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200270 endif
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200271endfunc
272
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200273" returns a Dict containing the values of all user options for 2html, including
274" default values for those not given an explicit value by the user. Discards the
275" html_ prefix of the option for nicer looking code.
276func! tohtml#GetUserSettings()
277 if exists('s:settings')
278 " just restore the known options if we've already retrieved them
279 return s:settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200280 else
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200281 " otherwise figure out which options are set
282 let user_settings = {}
283
284 " Define the correct option if the old option name exists and we haven't
285 " already defined the correct one. Maybe I'll put out a warnig message about
286 " this sometime and remove the old option entirely at some even later time,
287 " but for now just silently accept the old option.
288 if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
289 let g:html_use_xhtml = g:use_xhtml
290 endif
291
292 " get current option settings with appropriate defaults
293 call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
294 call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
295 call tohtml#GetOption(user_settings, 'number_lines', &number )
296 call tohtml#GetOption(user_settings, 'use_css', 1 )
297 call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
298 call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
299 call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
300 call tohtml#GetOption(user_settings, 'no_foldcolumn', 0 )
301 call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
302 call tohtml#GetOption(user_settings, 'no_pre', 0 )
303 call tohtml#GetOption(user_settings, 'whole_filler', 0 )
304 call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
305
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200306 " override those settings that need it
307
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200308 " hover opening implies dynamic folding
309 if user_settings.hover_unfold
310 let user_settings.dynamic_folds = 1
311 endif
312
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200313 " ignore folding overrides dynamic folding
314 if user_settings.ignore_folding && user_settings.dynamic_folds
315 let user_settings.dynamic_folds = 0
316 let user_settings.hover_unfold = 0
317 endif
318
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200319 " dynamic folding with no foldcolumn implies hover opens
320 if user_settings.dynamic_folds && user_settings.no_foldcolumn
321 let user_settings.hover_unfold = 1
322 endif
323
324 " dynamic folding implies css
325 if user_settings.dynamic_folds
326 let user_settings.use_css = 1
327 endif
328
329 " if we're not using CSS we cannot use a pre section because <font> tags
330 " aren't allowed inside a <pre> block
331 if !user_settings.use_css
332 let user_settings.no_pre = 1
333 endif
334
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200335 " Figure out proper MIME charset from the 'encoding' option.
336 if exists("g:html_use_encoding")
337 let user_settings.encoding = g:html_use_encoding
338 else
339 let vim_encoding = &encoding
340 if vim_encoding =~ '^8bit\|^2byte'
341 let vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '')
342 endif
343 if vim_encoding == 'latin1'
344 let user_settings.encoding = 'iso-8859-1'
345 elseif vim_encoding =~ "^cp12"
346 let user_settings.encoding = substitute(vim_encoding, 'cp', 'windows-', '')
347 elseif vim_encoding == 'sjis' || vim_encoding == 'cp932'
348 let user_settings.encoding = 'Shift_JIS'
349 elseif vim_encoding == 'big5' || vim_encoding == 'cp950'
350 let user_settings.encoding = "Big5"
351 elseif vim_encoding == 'euc-cn'
352 let user_settings.encoding = 'GB_2312-80'
353 elseif vim_encoding == 'euc-tw'
354 let user_settings.encoding = ""
355 elseif vim_encoding =~ '^euc\|^iso\|^koi'
356 let user_settings.encoding = substitute(vim_encoding, '.*', '\U\0', '')
357 elseif vim_encoding == 'cp949'
358 let user_settings.encoding = 'KS_C_5601-1987'
359 elseif vim_encoding == 'cp936'
360 let user_settings.encoding = 'GBK'
361 elseif vim_encoding =~ '^ucs\|^utf'
362 let user_settings.encoding = 'UTF-8'
363 else
364 let user_settings.encoding = ""
365 endif
366 endif
367
368 " TODO: font
369
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200370 return user_settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200371 endif
372endfunc
373
374let &cpo = s:cpo_sav
375unlet s:cpo_sav
376
377" Make sure any patches will probably use consistent indent
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200378" vim: ts=8 sw=2 sts=2 noet