PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vim73/autoload/tohtml.vim

https://gitlab.com/shinvdu/vim_win_config
Vim Script | 378 lines | 274 code | 44 blank | 60 comment | 57 complexity | ff299e9242b3580868574f7033f31e84 MD5 | raw file
  1. " Vim autoload file for the tohtml plugin.
  2. " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
  3. " Last Change: 2010 Aug 12
  4. "
  5. " Additional contributors:
  6. "
  7. " Original by Bram Moolenaar <Bram@vim.org>
  8. " Diff2HTML() added by Christian Brabandt <cb@256bit.org>
  9. "
  10. " See Mercurial change logs for more!
  11. " this file uses line continuations
  12. let s:cpo_sav = &cpo
  13. set cpo-=C
  14. func! tohtml#Convert2HTML(line1, line2)
  15. let s:settings = tohtml#GetUserSettings()
  16. if !&diff || s:settings.diff_one_file
  17. if a:line2 >= a:line1
  18. let g:html_start_line = a:line1
  19. let g:html_end_line = a:line2
  20. else
  21. let g:html_start_line = a:line2
  22. let g:html_end_line = a:line1
  23. endif
  24. runtime syntax/2html.vim
  25. else
  26. let win_list = []
  27. let buf_list = []
  28. windo | if &diff | call add(win_list, winbufnr(0)) | endif
  29. let s:settings.whole_filler = 1
  30. let g:html_diff_win_num = 0
  31. for window in win_list
  32. exe ":" . bufwinnr(window) . "wincmd w"
  33. let g:html_start_line = 1
  34. let g:html_end_line = line('$')
  35. let g:html_diff_win_num += 1
  36. runtime syntax/2html.vim
  37. call add(buf_list, bufnr('%'))
  38. endfor
  39. unlet g:html_diff_win_num
  40. call tohtml#Diff2HTML(win_list, buf_list)
  41. endif
  42. unlet g:html_start_line
  43. unlet g:html_end_line
  44. unlet s:settings
  45. endfunc
  46. func! tohtml#Diff2HTML(win_list, buf_list)
  47. let xml_line = ""
  48. let tag_close = '>'
  49. let s:old_paste = &paste
  50. set paste
  51. let s:old_magic = &magic
  52. set magic
  53. if s:settings.use_xhtml
  54. if s:settings.encoding != ""
  55. let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>"
  56. else
  57. let xml_line = "<?xml version=\"1.0\"?>"
  58. endif
  59. let tag_close = ' />'
  60. endif
  61. let style = [s:settings.use_xhtml ? "" : '-->']
  62. let body_line = ''
  63. let html = []
  64. if s:settings.use_xhtml
  65. call add(html, xml_line)
  66. endif
  67. if s:settings.use_xhtml
  68. call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
  69. call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">')
  70. elseif s:settings.use_css && !s:settings.no_pre
  71. call add(html, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">")
  72. call add(html, '<html>')
  73. else
  74. call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
  75. call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
  76. call add(html, '<html>')
  77. endif
  78. call add(html, '<head>')
  79. " include encoding as close to the top as possible, but only if not already
  80. " contained in XML information (to avoid haggling over content type)
  81. if s:settings.encoding != "" && !s:settings.use_xhtml
  82. call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close)
  83. endif
  84. call add(html, '<title>diff</title>')
  85. call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close)
  86. call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close)
  87. call add(html, '<meta name="settings" content="'.
  88. \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
  89. \ '"'.tag_close)
  90. call add(html, '</head>')
  91. let body_line_num = len(html)
  92. call add(html, '<body>')
  93. call add(html, '<table border="1" width="100%">')
  94. call add(html, '<tr>')
  95. for buf in a:win_list
  96. call add(html, '<th>'.bufname(buf).'</th>')
  97. endfor
  98. call add(html, '</tr><tr>')
  99. let diff_style_start = 0
  100. let insert_index = 0
  101. for buf in a:buf_list
  102. let temp = []
  103. exe bufwinnr(buf) . 'wincmd w'
  104. " If text is folded because of user foldmethod settings, etc. we don't want
  105. " to act on everything in a fold by mistake.
  106. setlocal nofoldenable
  107. " When not using CSS or when using xhtml, the <body> line can be important.
  108. " Assume it will be the same for all buffers and grab it from the first
  109. " buffer. Similarly, need to grab the body end line as well.
  110. if body_line == ''
  111. 1
  112. call search('<body')
  113. let body_line = getline('.')
  114. $
  115. call search('</body>', 'b')
  116. let s:body_end_line = getline('.')
  117. endif
  118. " Grab the style information. Some of this will be duplicated...
  119. 1
  120. let style_start = search('^<style type="text/css">')
  121. 1
  122. let style_end = search('^</style>')
  123. if style_start > 0 && style_end > 0
  124. let buf_styles = getline(style_start + 1, style_end - 1)
  125. for a_style in buf_styles
  126. if index(style, a_style) == -1
  127. if diff_style_start == 0
  128. if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
  129. let diff_style_start = len(style)-1
  130. endif
  131. endif
  132. call insert(style, a_style, insert_index)
  133. let insert_index += 1
  134. endif
  135. endfor
  136. endif
  137. if diff_style_start != 0
  138. let insert_index = diff_style_start
  139. endif
  140. " Delete those parts that are not needed so
  141. " we can include the rest into the resulting table
  142. 1,/^<body/d_
  143. $
  144. ?</body>?,$d_
  145. let temp = getline(1,'$')
  146. " undo deletion of start and end part
  147. " so we can later save the file as valid html
  148. " TODO: restore using grabbed lines if undolevel is 1?
  149. normal 2u
  150. if s:settings.use_css
  151. call add(html, '<td valign="top"><div>')
  152. elseif s:settings.use_xhtml
  153. call add(html, '<td nowrap="nowrap" valign="top"><div>')
  154. else
  155. call add(html, '<td nowrap valign="top"><div>')
  156. endif
  157. let html += temp
  158. call add(html, '</div></td>')
  159. " Close this buffer
  160. " TODO: the comment above says we're going to allow saving the file
  161. " later...but here we discard it?
  162. quit!
  163. endfor
  164. let html[body_line_num] = body_line
  165. call add(html, '</tr>')
  166. call add(html, '</table>')
  167. call add(html, s:body_end_line)
  168. call add(html, '</html>')
  169. let i = 1
  170. let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html")
  171. " Find an unused file name if current file name is already in use
  172. while filereadable(name)
  173. let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e")
  174. let i += 1
  175. endwhile
  176. exe "topleft new " . name
  177. setlocal modifiable
  178. " just in case some user autocmd creates content in the new buffer, make sure
  179. " it is empty before proceeding
  180. %d
  181. call append(0, html)
  182. if len(style) > 0
  183. 1
  184. let style_start = search('^</head>')-1
  185. " Insert javascript to toggle matching folds open and closed in all windows,
  186. " if dynamic folding is active.
  187. if s:settings.dynamic_folds
  188. call append(style_start, [
  189. \ "<script type='text/javascript'>",
  190. \ s:settings.use_xhtml ? '//<![CDATA[' : " <!--",
  191. \ " function toggleFold(objID)",
  192. \ " {",
  193. \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
  194. \ " {",
  195. \ " var fold;",
  196. \ ' fold = document.getElementById("win"+win_num+objID);',
  197. \ " if(fold.className == 'closed-fold')",
  198. \ " {",
  199. \ " fold.className = 'open-fold';",
  200. \ " }",
  201. \ " else if (fold.className == 'open-fold')",
  202. \ " {",
  203. \ " fold.className = 'closed-fold';",
  204. \ " }",
  205. \ " }",
  206. \ " }",
  207. \ s:settings.use_xhtml ? '//]]>' : " -->",
  208. \ "</script>"
  209. \ ])
  210. endif
  211. " Insert styles from all the generated html documents and additional styles
  212. " for the table-based layout of the side-by-side diff. The diff should take
  213. " up the full browser window (but not more), and be static in size,
  214. " horizontally scrollable when the lines are too long. Otherwise, the diff
  215. " is pretty useless for really long lines.
  216. if s:settings.use_css
  217. call append(style_start,
  218. \ ['<style type="text/css">']+
  219. \ style+
  220. \ [ s:settings.use_xhtml ? '' : '<!--',
  221. \ 'table { table-layout: fixed; }',
  222. \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
  223. \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
  224. \ 'td div { overflow: auto; }',
  225. \ s:settings.use_xhtml ? '' : '-->',
  226. \ '</style>'
  227. \ ])
  228. endif
  229. endif
  230. let &paste = s:old_paste
  231. let &magic = s:old_magic
  232. endfunc
  233. " Gets a single user option and sets it in the passed-in Dict, or gives it the
  234. " default value if the option doesn't actually exist.
  235. func! tohtml#GetOption(settings, option, default)
  236. if exists('g:html_'.a:option)
  237. let a:settings[a:option] = g:html_{a:option}
  238. else
  239. let a:settings[a:option] = a:default
  240. endif
  241. endfunc
  242. " returns a Dict containing the values of all user options for 2html, including
  243. " default values for those not given an explicit value by the user. Discards the
  244. " html_ prefix of the option for nicer looking code.
  245. func! tohtml#GetUserSettings()
  246. if exists('s:settings')
  247. " just restore the known options if we've already retrieved them
  248. return s:settings
  249. else
  250. " otherwise figure out which options are set
  251. let user_settings = {}
  252. " Define the correct option if the old option name exists and we haven't
  253. " already defined the correct one. Maybe I'll put out a warnig message about
  254. " this sometime and remove the old option entirely at some even later time,
  255. " but for now just silently accept the old option.
  256. if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
  257. let g:html_use_xhtml = g:use_xhtml
  258. endif
  259. " get current option settings with appropriate defaults
  260. call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
  261. call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
  262. call tohtml#GetOption(user_settings, 'number_lines', &number )
  263. call tohtml#GetOption(user_settings, 'use_css', 1 )
  264. call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
  265. call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
  266. call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
  267. call tohtml#GetOption(user_settings, 'no_foldcolumn', 0 )
  268. call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
  269. call tohtml#GetOption(user_settings, 'no_pre', 0 )
  270. call tohtml#GetOption(user_settings, 'whole_filler', 0 )
  271. call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
  272. " override those settings that need it
  273. " hover opening implies dynamic folding
  274. if user_settings.hover_unfold
  275. let user_settings.dynamic_folds = 1
  276. endif
  277. " ignore folding overrides dynamic folding
  278. if user_settings.ignore_folding && user_settings.dynamic_folds
  279. let user_settings.dynamic_folds = 0
  280. let user_settings.hover_unfold = 0
  281. endif
  282. " dynamic folding with no foldcolumn implies hover opens
  283. if user_settings.dynamic_folds && user_settings.no_foldcolumn
  284. let user_settings.hover_unfold = 1
  285. endif
  286. " dynamic folding implies css
  287. if user_settings.dynamic_folds
  288. let user_settings.use_css = 1
  289. endif
  290. " if we're not using CSS we cannot use a pre section because <font> tags
  291. " aren't allowed inside a <pre> block
  292. if !user_settings.use_css
  293. let user_settings.no_pre = 1
  294. endif
  295. " Figure out proper MIME charset from the 'encoding' option.
  296. if exists("g:html_use_encoding")
  297. let user_settings.encoding = g:html_use_encoding
  298. else
  299. let vim_encoding = &encoding
  300. if vim_encoding =~ '^8bit\|^2byte'
  301. let vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '')
  302. endif
  303. if vim_encoding == 'latin1'
  304. let user_settings.encoding = 'iso-8859-1'
  305. elseif vim_encoding =~ "^cp12"
  306. let user_settings.encoding = substitute(vim_encoding, 'cp', 'windows-', '')
  307. elseif vim_encoding == 'sjis' || vim_encoding == 'cp932'
  308. let user_settings.encoding = 'Shift_JIS'
  309. elseif vim_encoding == 'big5' || vim_encoding == 'cp950'
  310. let user_settings.encoding = "Big5"
  311. elseif vim_encoding == 'euc-cn'
  312. let user_settings.encoding = 'GB_2312-80'
  313. elseif vim_encoding == 'euc-tw'
  314. let user_settings.encoding = ""
  315. elseif vim_encoding =~ '^euc\|^iso\|^koi'
  316. let user_settings.encoding = substitute(vim_encoding, '.*', '\U\0', '')
  317. elseif vim_encoding == 'cp949'
  318. let user_settings.encoding = 'KS_C_5601-1987'
  319. elseif vim_encoding == 'cp936'
  320. let user_settings.encoding = 'GBK'
  321. elseif vim_encoding =~ '^ucs\|^utf'
  322. let user_settings.encoding = 'UTF-8'
  323. else
  324. let user_settings.encoding = ""
  325. endif
  326. endif
  327. " TODO: font
  328. return user_settings
  329. endif
  330. endfunc
  331. let &cpo = s:cpo_sav
  332. unlet s:cpo_sav
  333. " Make sure any patches will probably use consistent indent
  334. " vim: ts=8 sw=2 sts=2 noet