blob: d2722a4021334e6589915394243441b946ea88cd [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>
fritzophrenic86cfb392023-09-08 12:20:01 -05003" Last Change: 2023 Sep 03
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
Bram Moolenaar6c35bea2012-07-25 17:49:10 +020014set cpo&vim
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020015
Bram Moolenaar166af9b2010-11-16 20:34:40 +010016" Automatically find charsets from all encodings supported natively by Vim. With
17" the 8bit- and 2byte- prefixes, Vim can actually support more encodings than
18" this. Let the user specify these however since they won't be supported on
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +020019" every system.
Bram Moolenaar166af9b2010-11-16 20:34:40 +010020"
21" Note, not all of Vim's supported encodings have a charset to use.
22"
23" Names in this list are from:
24" http://www.iana.org/assignments/character-sets
25" g:tohtml#encoding_to_charset: {{{
26let g:tohtml#encoding_to_charset = {
27 \ 'latin1' : 'ISO-8859-1',
28 \ 'iso-8859-2' : 'ISO-8859-2',
29 \ 'iso-8859-3' : 'ISO-8859-3',
30 \ 'iso-8859-4' : 'ISO-8859-4',
31 \ 'iso-8859-5' : 'ISO-8859-5',
32 \ 'iso-8859-6' : 'ISO-8859-6',
33 \ 'iso-8859-7' : 'ISO-8859-7',
34 \ 'iso-8859-8' : 'ISO-8859-8',
35 \ 'iso-8859-9' : 'ISO-8859-9',
36 \ 'iso-8859-10' : '',
37 \ 'iso-8859-13' : 'ISO-8859-13',
38 \ 'iso-8859-14' : '',
39 \ 'iso-8859-15' : 'ISO-8859-15',
40 \ 'koi8-r' : 'KOI8-R',
41 \ 'koi8-u' : 'KOI8-U',
42 \ 'macroman' : 'macintosh',
43 \ 'cp437' : '',
44 \ 'cp775' : '',
45 \ 'cp850' : '',
46 \ 'cp852' : '',
47 \ 'cp855' : '',
48 \ 'cp857' : '',
49 \ 'cp860' : '',
50 \ 'cp861' : '',
51 \ 'cp862' : '',
52 \ 'cp863' : '',
53 \ 'cp865' : '',
54 \ 'cp866' : 'IBM866',
55 \ 'cp869' : '',
56 \ 'cp874' : '',
57 \ 'cp1250' : 'windows-1250',
58 \ 'cp1251' : 'windows-1251',
59 \ 'cp1253' : 'windows-1253',
60 \ 'cp1254' : 'windows-1254',
61 \ 'cp1255' : 'windows-1255',
62 \ 'cp1256' : 'windows-1256',
63 \ 'cp1257' : 'windows-1257',
64 \ 'cp1258' : 'windows-1258',
65 \ 'euc-jp' : 'EUC-JP',
66 \ 'sjis' : 'Shift_JIS',
67 \ 'cp932' : 'Shift_JIS',
68 \ 'cp949' : '',
69 \ 'euc-kr' : 'EUC-KR',
70 \ 'cp936' : 'GBK',
71 \ 'euc-cn' : 'GB2312',
72 \ 'big5' : 'Big5',
73 \ 'cp950' : 'Big5',
74 \ 'utf-8' : 'UTF-8',
75 \ 'ucs-2' : 'UTF-8',
76 \ 'ucs-2le' : 'UTF-8',
77 \ 'utf-16' : 'UTF-8',
78 \ 'utf-16le' : 'UTF-8',
79 \ 'ucs-4' : 'UTF-8',
80 \ 'ucs-4le' : 'UTF-8',
81 \ }
82lockvar g:tohtml#encoding_to_charset
83" Notes:
84" 1. All UCS/UTF are converted to UTF-8 because it is much better supported
85" 2. Any blank spaces are there because Vim supports it but at least one major
86" web browser does not according to http://wiki.whatwg.org/wiki/Web_Encodings.
87" }}}
88
89" Only automatically find encodings supported natively by Vim, let the user
90" specify the encoding if it's not natively supported. This function is only
91" used when the user specifies the charset, they better know what they are
92" doing!
93"
94" Names in this list are from:
95" http://www.iana.org/assignments/character-sets
96" g:tohtml#charset_to_encoding: {{{
97let g:tohtml#charset_to_encoding = {
98 \ 'iso_8859-1:1987' : 'latin1',
99 \ 'iso-ir-100' : 'latin1',
100 \ 'iso_8859-1' : 'latin1',
101 \ 'iso-8859-1' : 'latin1',
102 \ 'latin1' : 'latin1',
103 \ 'l1' : 'latin1',
104 \ 'ibm819' : 'latin1',
105 \ 'cp819' : 'latin1',
106 \ 'csisolatin1' : 'latin1',
107 \ 'iso_8859-2:1987' : 'iso-8859-2',
108 \ 'iso-ir-101' : 'iso-8859-2',
109 \ 'iso_8859-2' : 'iso-8859-2',
110 \ 'iso-8859-2' : 'iso-8859-2',
111 \ 'latin2' : 'iso-8859-2',
112 \ 'l2' : 'iso-8859-2',
113 \ 'csisolatin2' : 'iso-8859-2',
114 \ 'iso_8859-3:1988' : 'iso-8859-3',
115 \ 'iso-ir-109' : 'iso-8859-3',
116 \ 'iso_8859-3' : 'iso-8859-3',
117 \ 'iso-8859-3' : 'iso-8859-3',
118 \ 'latin3' : 'iso-8859-3',
119 \ 'l3' : 'iso-8859-3',
120 \ 'csisolatin3' : 'iso-8859-3',
121 \ 'iso_8859-4:1988' : 'iso-8859-4',
122 \ 'iso-ir-110' : 'iso-8859-4',
123 \ 'iso_8859-4' : 'iso-8859-4',
124 \ 'iso-8859-4' : 'iso-8859-4',
125 \ 'latin4' : 'iso-8859-4',
126 \ 'l4' : 'iso-8859-4',
127 \ 'csisolatin4' : 'iso-8859-4',
128 \ 'iso_8859-5:1988' : 'iso-8859-5',
129 \ 'iso-ir-144' : 'iso-8859-5',
130 \ 'iso_8859-5' : 'iso-8859-5',
131 \ 'iso-8859-5' : 'iso-8859-5',
132 \ 'cyrillic' : 'iso-8859-5',
133 \ 'csisolatincyrillic' : 'iso-8859-5',
134 \ 'iso_8859-6:1987' : 'iso-8859-6',
135 \ 'iso-ir-127' : 'iso-8859-6',
136 \ 'iso_8859-6' : 'iso-8859-6',
137 \ 'iso-8859-6' : 'iso-8859-6',
138 \ 'ecma-114' : 'iso-8859-6',
139 \ 'asmo-708' : 'iso-8859-6',
140 \ 'arabic' : 'iso-8859-6',
141 \ 'csisolatinarabic' : 'iso-8859-6',
142 \ 'iso_8859-7:1987' : 'iso-8859-7',
143 \ 'iso-ir-126' : 'iso-8859-7',
144 \ 'iso_8859-7' : 'iso-8859-7',
145 \ 'iso-8859-7' : 'iso-8859-7',
146 \ 'elot_928' : 'iso-8859-7',
147 \ 'ecma-118' : 'iso-8859-7',
148 \ 'greek' : 'iso-8859-7',
149 \ 'greek8' : 'iso-8859-7',
150 \ 'csisolatingreek' : 'iso-8859-7',
151 \ 'iso_8859-8:1988' : 'iso-8859-8',
152 \ 'iso-ir-138' : 'iso-8859-8',
153 \ 'iso_8859-8' : 'iso-8859-8',
154 \ 'iso-8859-8' : 'iso-8859-8',
155 \ 'hebrew' : 'iso-8859-8',
156 \ 'csisolatinhebrew' : 'iso-8859-8',
157 \ 'iso_8859-9:1989' : 'iso-8859-9',
158 \ 'iso-ir-148' : 'iso-8859-9',
159 \ 'iso_8859-9' : 'iso-8859-9',
160 \ 'iso-8859-9' : 'iso-8859-9',
161 \ 'latin5' : 'iso-8859-9',
162 \ 'l5' : 'iso-8859-9',
163 \ 'csisolatin5' : 'iso-8859-9',
164 \ 'iso-8859-10' : 'iso-8859-10',
165 \ 'iso-ir-157' : 'iso-8859-10',
166 \ 'l6' : 'iso-8859-10',
167 \ 'iso_8859-10:1992' : 'iso-8859-10',
168 \ 'csisolatin6' : 'iso-8859-10',
169 \ 'latin6' : 'iso-8859-10',
170 \ 'iso-8859-13' : 'iso-8859-13',
171 \ 'iso-8859-14' : 'iso-8859-14',
172 \ 'iso-ir-199' : 'iso-8859-14',
173 \ 'iso_8859-14:1998' : 'iso-8859-14',
174 \ 'iso_8859-14' : 'iso-8859-14',
175 \ 'latin8' : 'iso-8859-14',
176 \ 'iso-celtic' : 'iso-8859-14',
177 \ 'l8' : 'iso-8859-14',
178 \ 'iso-8859-15' : 'iso-8859-15',
179 \ 'iso_8859-15' : 'iso-8859-15',
180 \ 'latin-9' : 'iso-8859-15',
181 \ 'koi8-r' : 'koi8-r',
182 \ 'cskoi8r' : 'koi8-r',
183 \ 'koi8-u' : 'koi8-u',
184 \ 'macintosh' : 'macroman',
185 \ 'mac' : 'macroman',
186 \ 'csmacintosh' : 'macroman',
187 \ 'ibm437' : 'cp437',
188 \ 'cp437' : 'cp437',
189 \ '437' : 'cp437',
190 \ 'cspc8codepage437' : 'cp437',
191 \ 'ibm775' : 'cp775',
192 \ 'cp775' : 'cp775',
193 \ 'cspc775baltic' : 'cp775',
194 \ 'ibm850' : 'cp850',
195 \ 'cp850' : 'cp850',
196 \ '850' : 'cp850',
197 \ 'cspc850multilingual' : 'cp850',
198 \ 'ibm852' : 'cp852',
199 \ 'cp852' : 'cp852',
200 \ '852' : 'cp852',
201 \ 'cspcp852' : 'cp852',
202 \ 'ibm855' : 'cp855',
203 \ 'cp855' : 'cp855',
204 \ '855' : 'cp855',
205 \ 'csibm855' : 'cp855',
206 \ 'ibm857' : 'cp857',
207 \ 'cp857' : 'cp857',
208 \ '857' : 'cp857',
209 \ 'csibm857' : 'cp857',
210 \ 'ibm860' : 'cp860',
211 \ 'cp860' : 'cp860',
212 \ '860' : 'cp860',
213 \ 'csibm860' : 'cp860',
214 \ 'ibm861' : 'cp861',
215 \ 'cp861' : 'cp861',
216 \ '861' : 'cp861',
217 \ 'cp-is' : 'cp861',
218 \ 'csibm861' : 'cp861',
219 \ 'ibm862' : 'cp862',
220 \ 'cp862' : 'cp862',
221 \ '862' : 'cp862',
222 \ 'cspc862latinhebrew' : 'cp862',
223 \ 'ibm863' : 'cp863',
224 \ 'cp863' : 'cp863',
225 \ '863' : 'cp863',
226 \ 'csibm863' : 'cp863',
227 \ 'ibm865' : 'cp865',
228 \ 'cp865' : 'cp865',
229 \ '865' : 'cp865',
230 \ 'csibm865' : 'cp865',
231 \ 'ibm866' : 'cp866',
232 \ 'cp866' : 'cp866',
233 \ '866' : 'cp866',
234 \ 'csibm866' : 'cp866',
235 \ 'ibm869' : 'cp869',
236 \ 'cp869' : 'cp869',
237 \ '869' : 'cp869',
238 \ 'cp-gr' : 'cp869',
239 \ 'csibm869' : 'cp869',
240 \ 'windows-1250' : 'cp1250',
241 \ 'windows-1251' : 'cp1251',
242 \ 'windows-1253' : 'cp1253',
243 \ 'windows-1254' : 'cp1254',
244 \ 'windows-1255' : 'cp1255',
245 \ 'windows-1256' : 'cp1256',
246 \ 'windows-1257' : 'cp1257',
247 \ 'windows-1258' : 'cp1258',
248 \ 'extended_unix_code_packed_format_for_japanese' : 'euc-jp',
249 \ 'cseucpkdfmtjapanese' : 'euc-jp',
250 \ 'euc-jp' : 'euc-jp',
251 \ 'shift_jis' : 'sjis',
252 \ 'ms_kanji' : 'sjis',
253 \ 'sjis' : 'sjis',
254 \ 'csshiftjis' : 'sjis',
255 \ 'ibm-thai' : 'cp874',
256 \ 'csibmthai' : 'cp874',
257 \ 'ks_c_5601-1987' : 'cp949',
258 \ 'iso-ir-149' : 'cp949',
259 \ 'ks_c_5601-1989' : 'cp949',
260 \ 'ksc_5601' : 'cp949',
261 \ 'korean' : 'cp949',
262 \ 'csksc56011987' : 'cp949',
263 \ 'euc-kr' : 'euc-kr',
264 \ 'cseuckr' : 'euc-kr',
265 \ 'gbk' : 'cp936',
266 \ 'cp936' : 'cp936',
267 \ 'ms936' : 'cp936',
268 \ 'windows-936' : 'cp936',
269 \ 'gb_2312-80' : 'euc-cn',
270 \ 'iso-ir-58' : 'euc-cn',
271 \ 'chinese' : 'euc-cn',
272 \ 'csiso58gb231280' : 'euc-cn',
273 \ 'big5' : 'big5',
274 \ 'csbig5' : 'big5',
275 \ 'utf-8' : 'utf-8',
276 \ 'iso-10646-ucs-2' : 'ucs-2',
277 \ 'csunicode' : 'ucs-2',
278 \ 'utf-16' : 'utf-16',
279 \ 'utf-16be' : 'utf-16',
280 \ 'utf-16le' : 'utf-16le',
281 \ 'utf-32' : 'ucs-4',
282 \ 'utf-32be' : 'ucs-4',
283 \ 'utf-32le' : 'ucs-4le',
284 \ 'iso-10646-ucs-4' : 'ucs-4',
285 \ 'csucs4' : 'ucs-4'
286 \ }
287lockvar g:tohtml#charset_to_encoding
288"}}}
289
290func! tohtml#Convert2HTML(line1, line2) "{{{
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200291 let s:settings = tohtml#GetUserSettings()
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200292
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100293 if !&diff || s:settings.diff_one_file "{{{
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200294 if a:line2 >= a:line1
295 let g:html_start_line = a:line1
296 let g:html_end_line = a:line2
297 else
298 let g:html_start_line = a:line2
299 let g:html_end_line = a:line1
300 endif
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100301 runtime syntax/2html.vim "}}}
302 else "{{{
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200303 let win_list = []
304 let buf_list = []
Bram Moolenaar60cce2f2015-10-13 23:21:27 +0200305 windo if &diff | call add(win_list, winbufnr(0)) | endif
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200306 let s:settings.whole_filler = 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200307 let g:html_diff_win_num = 0
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200308 for window in win_list
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100309 " switch to the next buffer to convert
fritzophrenic86cfb392023-09-08 12:20:01 -0500310 exe ":" .. bufwinnr(window) .. "wincmd w"
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100311
312 " figure out whether current charset and encoding will work, if not
313 " default to UTF-8
314 if !exists('g:html_use_encoding') &&
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200315 \ (((&l:fileencoding=='' || (&l:buftype!='' && &l:buftype!=?'help'))
316 \ && &encoding!=?s:settings.vim_encoding)
317 \ || &l:fileencoding!='' && &l:fileencoding!=?s:settings.vim_encoding)
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100318 echohl WarningMsg
319 echomsg "TOhtml: mismatched file encodings in Diff buffers, using UTF-8"
320 echohl None
321 let s:settings.vim_encoding = 'utf-8'
322 let s:settings.encoding = 'UTF-8'
323 endif
324
325 " set up for diff-mode conversion
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200326 let g:html_start_line = 1
327 let g:html_end_line = line('$')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200328 let g:html_diff_win_num += 1
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100329
330 " convert this file
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200331 runtime syntax/2html.vim
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100332
333 " remember the HTML buffer for later combination
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200334 call add(buf_list, bufnr('%'))
335 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200336 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200337 call tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100338 endif "}}}
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200339
340 unlet g:html_start_line
341 unlet g:html_end_line
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200342 unlet s:settings
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100343endfunc "}}}
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200344
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100345func! tohtml#Diff2HTML(win_list, buf_list) "{{{
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200346 let xml_line = ""
347 let tag_close = '>'
348
Bram Moolenaar8df7f882010-08-13 11:30:02 +0200349 let s:old_paste = &paste
350 set paste
351 let s:old_magic = &magic
352 set magic
353
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200354 let html = []
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000355 if !s:settings.no_doc
356 if s:settings.use_xhtml
357 if s:settings.encoding != ""
fritzophrenic86cfb392023-09-08 12:20:01 -0500358 let xml_line = "<?xml version=\"1.0\" encoding=\"" .. s:settings.encoding .. "\"?>"
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000359 else
360 let xml_line = "<?xml version=\"1.0\"?>"
361 endif
362 let tag_close = ' />'
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100363 endif
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000364
365 let style = [s:settings.use_xhtml ? "" : '-->']
366 let body_line = ''
367
368 let s:html5 = 0
369 if s:settings.use_xhtml
370 call add(html, xml_line)
371 endif
372 if s:settings.use_xhtml
373 call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
374 call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">')
375 elseif s:settings.use_css && !s:settings.no_pre
376 call add(html, "<!DOCTYPE html>")
377 call add(html, '<html>')
378 let s:html5 = 1
379 else
380 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
381 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
382 call add(html, '<html>')
383 endif
384 call add(html, '<head>')
385
386 " include encoding as close to the top as possible, but only if not already
387 " contained in XML information
388 if s:settings.encoding != "" && !s:settings.use_xhtml
389 if s:html5
fritzophrenic86cfb392023-09-08 12:20:01 -0500390 call add(html, '<meta charset="' .. s:settings.encoding .. '"' .. tag_close)
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000391 else
fritzophrenic86cfb392023-09-08 12:20:01 -0500392 call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" .. s:settings.encoding .. '"' .. tag_close)
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000393 endif
394 endif
395
396 call add(html, '<title>diff</title>')
fritzophrenic86cfb392023-09-08 12:20:01 -0500397 call add(html, '<meta name="Generator" content="Vim/'..v:version/100..'.'..v:version%100..'"'..tag_close)
398 call add(html, '<meta name="plugin-version" content="'..g:loaded_2html_plugin..'"'..tag_close)
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000399 call add(html, '<meta name="settings" content="'.
400 \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
fritzophrenic86cfb392023-09-08 12:20:01 -0500401 \ ',prevent_copy='..s:settings.prevent_copy.
402 \ ',use_input_for_pc='..s:settings.use_input_for_pc.
403 \ '"'..tag_close)
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000404 call add(html, '<meta name="colorscheme" content="'.
405 \ (exists('g:colors_name')
406 \ ? g:colors_name
fritzophrenic86cfb392023-09-08 12:20:01 -0500407 \ : 'none').. '"'..tag_close)
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000408
409 call add(html, '</head>')
410 let body_line_num = len(html)
fritzophrenic86cfb392023-09-08 12:20:01 -0500411 call add(html, '<body'..(s:settings.line_ids ? ' onload="JumpToLine();"' : '')..'>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200412 endif
fritzophrenic86cfb392023-09-08 12:20:01 -0500413 call add(html, "<table "..(s:settings.use_css? "" : "border='1' width='100%' ").."id='vimCodeElement"..s:settings.id_suffix.."'>")
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200414
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200415 call add(html, '<tr>')
416 for buf in a:win_list
fritzophrenic86cfb392023-09-08 12:20:01 -0500417 call add(html, '<th>'..bufname(buf)..'</th>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200418 endfor
419 call add(html, '</tr><tr>')
420
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200421 let diff_style_start = 0
422 let insert_index = 0
423
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200424 for buf in a:buf_list
425 let temp = []
fritzophrenic86cfb392023-09-08 12:20:01 -0500426 exe bufwinnr(buf) .. 'wincmd w'
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200427
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200428 " If text is folded because of user foldmethod settings, etc. we don't want
429 " to act on everything in a fold by mistake.
430 setlocal nofoldenable
431
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200432 " When not using CSS or when using xhtml, the <body> line can be important.
433 " Assume it will be the same for all buffers and grab it from the first
434 " buffer. Similarly, need to grab the body end line as well.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000435 if !s:settings.no_doc
436 if body_line == ''
437 1
438 call search('<body')
439 let body_line = getline('.')
440 $
441 call search('</body>', 'b')
442 let s:body_end_line = getline('.')
443 endif
444
445 " Grab the style information. Some of this will be duplicated so only insert
446 " it if it's not already there. {{{
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200447 1
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000448 let style_start = search('^<style\( type="text/css"\)\?>')
449 1
450 let style_end = search('^</style>')
451 if style_start > 0 && style_end > 0
452 let buf_styles = getline(style_start + 1, style_end - 1)
453 for a_style in buf_styles
454 if index(style, a_style) == -1
455 if diff_style_start == 0
456 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
457 let diff_style_start = len(style)-1
458 endif
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200459 endif
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000460 call insert(style, a_style, insert_index)
461 let insert_index += 1
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200462 endif
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000463 endfor
464 endif " }}}
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200465
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000466 " everything new will get added before the diff styles so diff highlight
467 " properly overrides normal highlight
468 if diff_style_start != 0
469 let insert_index = diff_style_start
470 endif
471
472 " Delete those parts that are not needed so we can include the rest into the
473 " resulting table.
474 1,/^<body.*\%(\n<!--.*-->\_s\+.*id='oneCharWidth'.*\_s\+.*id='oneInputWidth'.*\_s\+.*id='oneEmWidth'\)\?\zs/d_
475 $
476 ?</body>?,$d_
477 elseif !s:settings.no_modeline
478 " remove modeline from source files if it is included and we haven't deleted
479 " due to removing html footer already
480 $d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200481 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200482 let temp = getline(1,'$')
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200483 " clean out id on the main content container because we already set it on
484 " the table
Bram Moolenaar31c31672013-06-26 13:28:14 +0200485 let temp[0] = substitute(temp[0], " id='vimCodeElement[^']*'", "", "")
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200486 " undo deletion of start and end part
487 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200488 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000489 if !s:settings.no_doc
490 normal! 2u
491 elseif !s:settings.no_modeline
492 normal! u
493 endif
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200494 if s:settings.use_css
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100495 call add(html, '<td><div>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200496 elseif s:settings.use_xhtml
497 call add(html, '<td nowrap="nowrap" valign="top"><div>')
498 else
499 call add(html, '<td nowrap valign="top"><div>')
500 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200501 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200502 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200503
504 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200505 " TODO: the comment above says we're going to allow saving the file
506 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200507 quit!
508 endfor
509
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000510 if !s:settings.no_doc
511 let html[body_line_num] = body_line
512 endif
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200513
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200514 call add(html, '</tr>')
515 call add(html, '</table>')
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000516 if !s:settings.no_doc
517 call add(html, s:body_end_line)
518 call add(html, '</html>')
519 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200520
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200521 " The generated HTML is admittedly ugly and takes a LONG time to fold.
522 " Make sure the user doesn't do syntax folding when loading a generated file,
523 " using a modeline.
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000524 if !s:settings.no_modeline
525 call add(html, '<!-- vim: set foldmethod=manual : -->')
526 endif
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200527
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200528 let i = 1
fritzophrenic86cfb392023-09-08 12:20:01 -0500529 let name = "Diff" .. (s:settings.use_xhtml ? ".xhtml" : ".html")
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200530 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200531 while filereadable(name)
fritzophrenic86cfb392023-09-08 12:20:01 -0500532 let name = substitute(name, '\d*\.x\?html$', '', '') .. i .. '.' .. fnamemodify(copy(name), ":t:e")
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200533 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200534 endwhile
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100535
536 let s:ei_sav = &eventignore
537 set eventignore+=FileType
fritzophrenic86cfb392023-09-08 12:20:01 -0500538 exe "topleft new " .. name
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100539 let &eventignore=s:ei_sav
540 unlet s:ei_sav
541
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200542 setlocal modifiable
543
544 " just in case some user autocmd creates content in the new buffer, make sure
545 " it is empty before proceeding
546 %d
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100547
548 " set the fileencoding to match the charset we'll be using
549 let &l:fileencoding=s:settings.vim_encoding
550
551 " According to http://www.w3.org/TR/html4/charset.html#doc-char-set, the byte
552 " order mark is highly recommend on the web when using multibyte encodings. But,
553 " it is not a good idea to include it on UTF-8 files. Otherwise, let Vim
554 " determine when it is actually inserted.
555 if s:settings.vim_encoding == 'utf-8'
556 setlocal nobomb
557 else
558 setlocal bomb
559 endif
560
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200561 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200562
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000563 if !s:settings.no_doc
564 if len(style) > 0
565 1
566 let style_start = search('^</head>')-1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200567
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000568 " add required javascript in reverse order so we can just call append again
569 " and again without adjusting {{{
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200570
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000571 let s:uses_script = s:settings.dynamic_folds || s:settings.line_ids
Bram Moolenaarf0d58ef2018-11-16 16:13:44 +0100572
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000573 " insert script closing tag if needed
574 if s:uses_script
Bram Moolenaarf0d58ef2018-11-16 16:13:44 +0100575 call append(style_start, [
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000576 \ '',
577 \ s:settings.use_xhtml ? '//]]>' : '-->',
578 \ "</script>"
Bram Moolenaarf0d58ef2018-11-16 16:13:44 +0100579 \ ])
580 endif
Bram Moolenaarf0d58ef2018-11-16 16:13:44 +0100581
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000582 " insert javascript to get IDs from line numbers, and to open a fold before
583 " jumping to any lines contained therein
584 if s:settings.line_ids
585 call append(style_start, [
586 \ " /* Always jump to new location even if the line was hidden inside a fold, or",
587 \ " * we corrected the raw number to a line ID.",
588 \ " */",
589 \ " if (lineElem) {",
590 \ " lineElem.scrollIntoView(true);",
591 \ " }",
592 \ " return true;",
593 \ "}",
594 \ "if ('onhashchange' in window) {",
595 \ " window.onhashchange = JumpToLine;",
596 \ "}"
597 \ ])
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200598
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000599 if s:settings.dynamic_folds
600 call append(style_start, [
601 \ "",
602 \ " /* navigate upwards in the DOM tree to open all folds containing the line */",
603 \ " var node = lineElem;",
fritzophrenic86cfb392023-09-08 12:20:01 -0500604 \ " while (node && node.id != 'vimCodeElement"..s:settings.id_suffix.."')",
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000605 \ " {",
606 \ " if (node.className == 'closed-fold')",
607 \ " {",
608 \ " /* toggle open the fold ID (remove window ID) */",
609 \ " toggleFold(node.id.substr(4));",
610 \ " }",
611 \ " node = node.parentNode;",
612 \ " }",
613 \ ])
614 endif
615 endif
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200616
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000617 if s:settings.line_ids
618 call append(style_start, [
619 \ "",
620 \ "/* function to open any folds containing a jumped-to line before jumping to it */",
621 \ "function JumpToLine()",
622 \ "{",
623 \ " var lineNum;",
624 \ " lineNum = window.location.hash;",
625 \ " lineNum = lineNum.substr(1); /* strip off '#' */",
626 \ "",
627 \ " if (lineNum.indexOf('L') == -1) {",
628 \ " lineNum = 'L'+lineNum;",
629 \ " }",
630 \ " if (lineNum.indexOf('W') == -1) {",
631 \ " lineNum = 'W1'+lineNum;",
632 \ " }",
633 \ " var lineElem = document.getElementById(lineNum);"
634 \ ])
635 endif
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200636
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000637 " Insert javascript to toggle matching folds open and closed in all windows,
638 " if dynamic folding is active.
639 if s:settings.dynamic_folds
640 call append(style_start, [
641 \ " function toggleFold(objID)",
642 \ " {",
fritzophrenic86cfb392023-09-08 12:20:01 -0500643 \ " for (win_num = 1; win_num <= "..len(a:buf_list).."; win_num++)",
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000644 \ " {",
645 \ " var fold;",
646 \ ' fold = document.getElementById("win"+win_num+objID);',
647 \ " if(fold.className == 'closed-fold')",
648 \ " {",
649 \ " fold.className = 'open-fold';",
650 \ " }",
651 \ " else if (fold.className == 'open-fold')",
652 \ " {",
653 \ " fold.className = 'closed-fold';",
654 \ " }",
655 \ " }",
656 \ " }",
657 \ ])
658 endif
659
660 if s:uses_script
661 " insert script tag if needed
662 call append(style_start, [
fritzophrenic86cfb392023-09-08 12:20:01 -0500663 \ "<script" .. (s:html5 ? "" : " type='text/javascript'") .. ">",
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000664 \ s:settings.use_xhtml ? '//<![CDATA[' : "<!--"])
665 endif
666
667 " Insert styles from all the generated html documents and additional styles
668 " for the table-based layout of the side-by-side diff. The diff should take
669 " up the full browser window (but not more), and be static in size,
670 " horizontally scrollable when the lines are too long. Otherwise, the diff
671 " is pretty useless for really long lines. {{{
672 if s:settings.use_css
673 call append(style_start,
fritzophrenic86cfb392023-09-08 12:20:01 -0500674 \ ['<style' .. (s:html5 ? '' : 'type="text/css"') .. '>']+
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000675 \ style+
676 \ [ s:settings.use_xhtml ? '' : '<!--',
677 \ 'table { table-layout: fixed; }',
678 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
679 \ 'table, td, th { border: 1px solid; }',
680 \ 'td { vertical-align: top; }',
fritzophrenic86cfb392023-09-08 12:20:01 -0500681 \ 'th, td { width: '..printf("%.1f",100.0/len(a:win_list))..'%; }',
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000682 \ 'td div { overflow: auto; }',
683 \ s:settings.use_xhtml ? '' : '-->',
684 \ '</style>'
685 \])
686 endif "}}}
687 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200688 endif
Bram Moolenaar8df7f882010-08-13 11:30:02 +0200689
690 let &paste = s:old_paste
691 let &magic = s:old_magic
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100692endfunc "}}}
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200693
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200694" Gets a single user option and sets it in the passed-in Dict, or gives it the
695" default value if the option doesn't actually exist.
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100696func! tohtml#GetOption(settings, option, default) "{{{
fritzophrenic86cfb392023-09-08 12:20:01 -0500697 if exists('g:html_'..a:option)
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200698 let a:settings[a:option] = g:html_{a:option}
699 else
700 let a:settings[a:option] = a:default
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200701 endif
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100702endfunc "}}}
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200703
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200704" returns a Dict containing the values of all user options for 2html, including
705" default values for those not given an explicit value by the user. Discards the
706" html_ prefix of the option for nicer looking code.
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100707func! tohtml#GetUserSettings() "{{{
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200708 if exists('s:settings')
709 " just restore the known options if we've already retrieved them
710 return s:settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200711 else
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200712 " otherwise figure out which options are set
713 let user_settings = {}
714
715 " Define the correct option if the old option name exists and we haven't
fritzophrenic86cfb392023-09-08 12:20:01 -0500716 " already defined the correct one.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200717 if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
fritzophrenic86cfb392023-09-08 12:20:01 -0500718 echohl WarningMsg
719 echomsg "Warning: g:use_xhtml is deprecated, use g:html_use_xhtml"
720 echohl None
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200721 let g:html_use_xhtml = g:use_xhtml
722 endif
723
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100724 " get current option settings with appropriate defaults {{{
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100725 call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
726 call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
727 call tohtml#GetOption(user_settings, 'number_lines', &number )
728 call tohtml#GetOption(user_settings, 'pre_wrap', &wrap )
729 call tohtml#GetOption(user_settings, 'use_css', 1 )
730 call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
731 call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
732 call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
733 call tohtml#GetOption(user_settings, 'no_foldcolumn', user_settings.ignore_folding)
734 call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
735 call tohtml#GetOption(user_settings, 'no_pre', 0 )
Bram Moolenaar6ebe4f92022-10-28 20:47:54 +0100736 call tohtml#GetOption(user_settings, 'no_doc', 0 )
737 call tohtml#GetOption(user_settings, 'no_links', 0 )
738 call tohtml#GetOption(user_settings, 'no_modeline', 0 )
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100739 call tohtml#GetOption(user_settings, 'no_invalid', 0 )
740 call tohtml#GetOption(user_settings, 'whole_filler', 0 )
741 call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
742 call tohtml#GetOption(user_settings, 'line_ids', user_settings.number_lines )
fritzophrenic86cfb392023-09-08 12:20:01 -0500743 call tohtml#GetOption(user_settings, 'use_input_for_pc', 'none')
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100744 " }}}
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200745
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100746 " override those settings that need it {{{
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200747
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200748 " hover opening implies dynamic folding
749 if user_settings.hover_unfold
750 let user_settings.dynamic_folds = 1
751 endif
752
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200753 " ignore folding overrides dynamic folding
754 if user_settings.ignore_folding && user_settings.dynamic_folds
755 let user_settings.dynamic_folds = 0
756 let user_settings.hover_unfold = 0
757 endif
758
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200759 " dynamic folding with no foldcolumn implies hover opens
760 if user_settings.dynamic_folds && user_settings.no_foldcolumn
761 let user_settings.hover_unfold = 1
762 endif
763
764 " dynamic folding implies css
765 if user_settings.dynamic_folds
766 let user_settings.use_css = 1
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200767 else
768 let user_settings.no_foldcolumn = 1 " won't do anything but for consistency and for the test suite
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200769 endif
770
771 " if we're not using CSS we cannot use a pre section because <font> tags
772 " aren't allowed inside a <pre> block
773 if !user_settings.use_css
774 let user_settings.no_pre = 1
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200775 endif
776
777 " pre_wrap doesn't do anything if not using pre or not using CSS
778 if user_settings.no_pre || !user_settings.use_css
Bram Moolenaar6ebe4f92022-10-28 20:47:54 +0100779 let user_settings.pre_wrap = 0
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200780 endif
781 "}}}
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200782
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100783 " set up expand_tabs option after all the overrides so we know the
784 " appropriate defaults {{{
785 if user_settings.no_pre == 0
786 call tohtml#GetOption(user_settings,
787 \ 'expand_tabs',
Bram Moolenaarf0d58ef2018-11-16 16:13:44 +0100788 \ &expandtab || &ts != 8 || &vts != '' || user_settings.number_lines ||
Bram Moolenaar2a8a3ec2011-01-08 16:06:37 +0100789 \ (user_settings.dynamic_folds && !user_settings.no_foldcolumn))
790 else
791 let user_settings.expand_tabs = 1
792 endif
793 " }}}
794
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200795 " textual options
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100796 if exists("g:html_use_encoding") "{{{
797 " user specified the desired MIME charset, figure out proper
798 " 'fileencoding' from it or warn the user if we cannot
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200799 let user_settings.encoding = g:html_use_encoding
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100800 let user_settings.vim_encoding = tohtml#EncodingFromCharset(g:html_use_encoding)
801 if user_settings.vim_encoding == ''
802 echohl WarningMsg
803 echomsg "TOhtml: file encoding for"
804 \ g:html_use_encoding
805 \ "unknown, please set 'fileencoding'"
806 echohl None
807 endif
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200808 else
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100809 " Figure out proper MIME charset from 'fileencoding' if possible
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200810 if &l:fileencoding != ''
811 " If the buffer is not a "normal" type, the 'fileencoding' value may not
812 " be trusted; since the buffer should not be written the fileencoding is
813 " not intended to be used.
814 if &l:buftype=='' || &l:buftype==?'help'
815 let user_settings.vim_encoding = &l:fileencoding
816 call tohtml#CharsetFromEncoding(user_settings)
817 else
818 let user_settings.encoding = '' " trigger detection using &encoding
819 endif
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200820 endif
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100821
822 " else from 'encoding' if possible
823 if &l:fileencoding == '' || user_settings.encoding == ''
824 let user_settings.vim_encoding = &encoding
825 call tohtml#CharsetFromEncoding(user_settings)
826 endif
827
828 " else default to UTF-8 and warn user
829 if user_settings.encoding == ''
830 let user_settings.vim_encoding = 'utf-8'
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200831 let user_settings.encoding = 'UTF-8'
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100832 echohl WarningMsg
833 echomsg "TOhtml: couldn't determine MIME charset, using UTF-8"
834 echohl None
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200835 endif
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100836 endif "}}}
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200837
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200838 " Default to making nothing uncopyable, because we default to
839 " not-standards way of doing things, and also because Microsoft Word and
840 " others paste the <input> elements anyway.
841 "
842 " html_prevent_copy only has an effect when using CSS.
843 "
844 " All options:
845 " f - fold column
846 " n - line numbers (also within fold text)
847 " t - fold text
848 " d - diff filler
849 " c - concealed text (reserved future)
850 " l - listchars (reserved possible future)
851 " s - signs (reserved possible future)
852 "
853 " Normal text is always selectable.
854 let user_settings.prevent_copy = ""
855 if user_settings.use_css
856 if exists("g:html_prevent_copy")
857 if user_settings.dynamic_folds && !user_settings.no_foldcolumn && g:html_prevent_copy =~# 'f'
fritzophrenic86cfb392023-09-08 12:20:01 -0500858 let user_settings.prevent_copy ..= 'f'
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200859 endif
860 if user_settings.number_lines && g:html_prevent_copy =~# 'n'
fritzophrenic86cfb392023-09-08 12:20:01 -0500861 let user_settings.prevent_copy ..= 'n'
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200862 endif
863 if &diff && g:html_prevent_copy =~# 'd'
fritzophrenic86cfb392023-09-08 12:20:01 -0500864 let user_settings.prevent_copy ..= 'd'
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200865 endif
866 if !user_settings.ignore_folding && g:html_prevent_copy =~# 't'
fritzophrenic86cfb392023-09-08 12:20:01 -0500867 let user_settings.prevent_copy ..= 't'
Bram Moolenaar6c35bea2012-07-25 17:49:10 +0200868 endif
869 else
870 let user_settings.prevent_copy = ""
871 endif
872 endif
873 if empty(user_settings.prevent_copy)
874 let user_settings.no_invalid = 0
875 endif
876
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100877 " enforce valid values for use_input_for_pc
878 if user_settings.use_input_for_pc !~# 'fallback\|none\|all'
fritzophrenic86cfb392023-09-08 12:20:01 -0500879 let user_settings.use_input_for_pc = 'none'
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100880 echohl WarningMsg
fritzophrenic86cfb392023-09-08 12:20:01 -0500881 echomsg '2html: "' .. g:html_use_input_for_pc .. '" is not valid for g:html_use_input_for_pc'
882 echomsg '2html: defaulting to "' .. user_settings.use_input_for_pc .. '"'
Bram Moolenaar09c6f262019-11-17 15:55:14 +0100883 echohl None
884 sleep 3
885 endif
886
Bram Moolenaar31c31672013-06-26 13:28:14 +0200887 if exists('g:html_id_expr')
888 let user_settings.id_suffix = eval(g:html_id_expr)
889 if user_settings.id_suffix !~ '^[-_:.A-Za-z0-9]*$'
890 echohl WarningMsg
891 echomsg '2html: g:html_id_expr evaluated to invalid string for HTML id attributes'
892 echomsg '2html: Omitting user-specified suffix'
893 echohl None
894 sleep 3
895 let user_settings.id_suffix=""
896 endif
897 else
898 let user_settings.id_suffix=""
899 endif
900
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200901 " TODO: font
902
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200903 return user_settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200904 endif
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100905endfunc "}}}
906
907" get the proper HTML charset name from a Vim encoding option.
908function! tohtml#CharsetFromEncoding(settings) "{{{
909 let l:vim_encoding = a:settings.vim_encoding
910 if exists('g:html_charset_override') && has_key(g:html_charset_override, l:vim_encoding)
911 let a:settings.encoding = g:html_charset_override[l:vim_encoding]
912 else
913 if l:vim_encoding =~ '^8bit\|^2byte'
914 " 8bit- and 2byte- prefixes are to indicate encodings available on the
915 " system that Vim will convert with iconv(), look up just the encoding name,
916 " not Vim's prefix.
917 let l:vim_encoding = substitute(l:vim_encoding, '^8bit-\|^2byte-', '', '')
918 endif
919 if has_key(g:tohtml#encoding_to_charset, l:vim_encoding)
920 let a:settings.encoding = g:tohtml#encoding_to_charset[l:vim_encoding]
921 else
922 let a:settings.encoding = ""
923 endif
924 endif
925 if a:settings.encoding != ""
926 let l:vim_encoding = tohtml#EncodingFromCharset(a:settings.encoding)
927 if l:vim_encoding != ""
928 " if the Vim encoding to HTML encoding conversion is set up (by default or
929 " by the user) to convert to a different encoding, we need to also change
930 " the Vim encoding of the new buffer
931 let a:settings.vim_encoding = l:vim_encoding
932 endif
933 endif
934endfun "}}}
935
936" Get the proper Vim encoding option setting from an HTML charset name.
937function! tohtml#EncodingFromCharset(encoding) "{{{
938 if exists('g:html_encoding_override') && has_key(g:html_encoding_override, a:encoding)
939 return g:html_encoding_override[a:encoding]
940 elseif has_key(g:tohtml#charset_to_encoding, tolower(a:encoding))
941 return g:tohtml#charset_to_encoding[tolower(a:encoding)]
942 else
943 return ""
944 endif
945endfun "}}}
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200946
947let &cpo = s:cpo_sav
948unlet s:cpo_sav
949
950" Make sure any patches will probably use consistent indent
Bram Moolenaar166af9b2010-11-16 20:34:40 +0100951" vim: ts=8 sw=2 sts=2 noet fdm=marker