comparison runtime/autoload/tohtml.vim @ 2496:a29075150aee vim73

Improve handling of user settings in :TOhtml. Default to generating CSS. (Benjamin Fritz)
author Bram Moolenaar <bram@vim.org>
date Thu, 05 Aug 2010 21:54:00 +0200
parents 80229a724a11
children 7e008c174cc3
comparison
equal deleted inserted replaced
2495:a847363bf06e 2496:a29075150aee
1 " Vim autoload file for the tohtml plugin. 1 " Vim autoload file for the tohtml plugin.
2 " Maintainer: Ben Fritz <fritzophrenic@gmail.com> 2 " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
3 " Last Change: 2010 Jul 29 3 " Last Change: 2010 Aug 02
4 " 4 "
5 " Additional contributors: 5 " Additional contributors:
6 " 6 "
7 " Original by Bram Moolenaar <Bram@vim.org> 7 " Original by Bram Moolenaar <Bram@vim.org>
8 " Diff2HTML() added by Christian Brabandt <cb@256bit.org> 8 " Diff2HTML() added by Christian Brabandt <cb@256bit.org>
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 func! tohtml#Convert2HTML(line1, line2) 16 func! tohtml#Convert2HTML(line1, line2)
17 let old_vals = tohtml#OverrideUserSettings() 17 let s:settings = tohtml#GetUserSettings()
18 18
19 if !&diff || exists("g:html_diff_one_file") 19 if !&diff || s:settings.diff_one_file
20 if a:line2 >= a:line1 20 if a:line2 >= a:line1
21 let g:html_start_line = a:line1 21 let g:html_start_line = a:line1
22 let g:html_end_line = a:line2 22 let g:html_end_line = a:line2
23 else 23 else
24 let g:html_start_line = a:line2 24 let g:html_start_line = a:line2
27 runtime syntax/2html.vim 27 runtime syntax/2html.vim
28 else 28 else
29 let win_list = [] 29 let win_list = []
30 let buf_list = [] 30 let buf_list = []
31 windo | if &diff | call add(win_list, winbufnr(0)) | endif 31 windo | if &diff | call add(win_list, winbufnr(0)) | endif
32 let save_hwf = exists("g:html_whole_filler") 32 let s:settings.whole_filler = 1
33 let g:html_whole_filler = 1
34 let g:html_diff_win_num = 0 33 let g:html_diff_win_num = 0
35 for window in win_list 34 for window in win_list
36 exe ":" . bufwinnr(window) . "wincmd w" 35 exe ":" . bufwinnr(window) . "wincmd w"
37 let g:html_start_line = 1 36 let g:html_start_line = 1
38 let g:html_end_line = line('$') 37 let g:html_end_line = line('$')
39 let g:html_diff_win_num += 1 38 let g:html_diff_win_num += 1
40 runtime syntax/2html.vim 39 runtime syntax/2html.vim
41 call add(buf_list, bufnr('%')) 40 call add(buf_list, bufnr('%'))
42 endfor 41 endfor
43 unlet g:html_diff_win_num 42 unlet g:html_diff_win_num
44 if !save_hwf
45 unlet g:html_whole_filler
46 endif
47 call tohtml#Diff2HTML(win_list, buf_list) 43 call tohtml#Diff2HTML(win_list, buf_list)
48 endif 44 endif
49 45
50 call tohtml#RestoreUserSettings(old_vals)
51
52 unlet g:html_start_line 46 unlet g:html_start_line
53 unlet g:html_end_line 47 unlet g:html_end_line
48 unlet s:settings
54 endfunc 49 endfunc
55 50
56 func! tohtml#Diff2HTML(win_list, buf_list) 51 func! tohtml#Diff2HTML(win_list, buf_list)
57 " TODO: add logic for xhtml 52 " TODO: add logic for xhtml
58 let style = ['-->'] 53 let style = ['-->']
173 1 168 1
174 let style_start = search('^</head>')-1 169 let style_start = search('^</head>')-1
175 170
176 " Insert javascript to toggle matching folds open and closed in all windows, 171 " Insert javascript to toggle matching folds open and closed in all windows,
177 " if dynamic folding is active. 172 " if dynamic folding is active.
178 if exists("g:html_dynamic_folds") 173 if s:settings.dynamic_folds
179 call append(style_start, [ 174 call append(style_start, [
180 \ "<script type='text/javascript'>", 175 \ "<script type='text/javascript'>",
181 \ " <!--", 176 \ " <!--",
182 \ " function toggleFold(objID)", 177 \ " function toggleFold(objID)",
183 \ " {", 178 \ " {",
203 " Insert styles from all the generated html documents and additional styles 198 " 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 199 " 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, 200 " 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 201 " horizontally scrollable when the lines are too long. Otherwise, the diff
207 " is pretty useless for really long lines. 202 " is pretty useless for really long lines.
208 if exists("g:html_use_css") 203 if s:settings.use_css
209 call append(style_start, [ 204 call append(style_start, [
210 \ '<style type="text/css">']+ 205 \ '<style type="text/css">']+
211 \ style+[ 206 \ style+[
212 \ '<!--', 207 \ '<!--',
213 \ 'table { table-layout: fixed; }', 208 \ 'table { table-layout: fixed; }',
219 \ ]) 214 \ ])
220 endif 215 endif
221 endif 216 endif
222 endfunc 217 endfunc
223 218
224 func! tohtml#OverrideUserSettings() 219 " Gets a single user option and sets it in the passed-in Dict, or gives it the
225 let old_settings = {} 220 " default value if the option doesn't actually exist.
226 " make copies of the user-defined settings that we may overrule 221 func! tohtml#GetOption(settings, option, default)
227 let old_settings.html_dynamic_folds = exists("g:html_dynamic_folds") 222 if exists('g:html_'.a:option)
228 let old_settings.html_hover_unfold = exists("g:html_hover_unfold") 223 let a:settings[a:option] = g:html_{a:option}
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
252 endfunc
253
254 func! 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 224 else
259 unlet! g:html_dynamic_folds 225 let a:settings[a:option] = a:default
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 226 endif
271 endfunc 227 endfunc
272 228
229 " returns a Dict containing the values of all user options for 2html, including
230 " default values for those not given an explicit value by the user. Discards the
231 " html_ prefix of the option for nicer looking code.
232 func! tohtml#GetUserSettings()
233 if exists('s:settings')
234 " just restore the known options if we've already retrieved them
235 return s:settings
236 else
237 " otherwise figure out which options are set
238 let user_settings = {}
239
240 " Define the correct option if the old option name exists and we haven't
241 " already defined the correct one. Maybe I'll put out a warnig message about
242 " this sometime and remove the old option entirely at some even later time,
243 " but for now just silently accept the old option.
244 if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
245 let g:html_use_xhtml = g:use_xhtml
246 endif
247
248 " get current option settings with appropriate defaults
249 call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
250 call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
251 call tohtml#GetOption(user_settings, 'number_lines', &number )
252 call tohtml#GetOption(user_settings, 'use_css', 1 )
253 call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
254 call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
255 call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
256 call tohtml#GetOption(user_settings, 'no_foldcolumn', 0 )
257 call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
258 call tohtml#GetOption(user_settings, 'no_pre', 0 )
259 call tohtml#GetOption(user_settings, 'whole_filler', 0 )
260 call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
261
262 " TODO: encoding? font? These are string options that require more parsing.
263
264 " override those settings that need it
265
266 " ignore folding overrides dynamic folding
267 if user_settings.ignore_folding && user_settings.dynamic_folds
268 let user_settings.dynamic_folds = 0
269 let user_settings.hover_unfold = 0
270 endif
271
272 " hover opening implies dynamic folding
273 if user_settings.hover_unfold
274 let user_settings.dynamic_folds = 1
275 endif
276
277 " dynamic folding with no foldcolumn implies hover opens
278 if user_settings.dynamic_folds && user_settings.no_foldcolumn
279 let user_settings.hover_unfold = 1
280 endif
281
282 " dynamic folding implies css
283 if user_settings.dynamic_folds
284 let user_settings.use_css = 1
285 endif
286
287 " if we're not using CSS we cannot use a pre section because <font> tags
288 " aren't allowed inside a <pre> block
289 if !user_settings.use_css
290 let user_settings.no_pre = 1
291 endif
292
293 return user_settings
294 endif
295 endfunc
296
273 let &cpo = s:cpo_sav 297 let &cpo = s:cpo_sav
274 unlet s:cpo_sav 298 unlet s:cpo_sav
275 299
276 " Make sure any patches will probably use consistent indent 300 " Make sure any patches will probably use consistent indent
277 " vim: ts=8 sw=2 sts=2 noet 301 " vim: ts=8 sw=2 sts=2 noet