diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 21:52:58 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 21:52:58 +0000 |
commit | 931bffbda3668ddc609fc1da8f9eb576b170aa52 (patch) | |
tree | d8c1843a95da5ea0bb4acc09f7e37843d9995c86 /runtime/autoload/tohtml.vim | |
parent | 142d9041391780ac15b89886a54015fdc5c73995 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-userreg.tar.gz rneovim-userreg.tar.bz2 rneovim-userreg.zip |
Merge remote-tracking branch 'upstream/master' into userreguserreg
Diffstat (limited to 'runtime/autoload/tohtml.vim')
-rw-r--r-- | runtime/autoload/tohtml.vim | 463 |
1 files changed, 242 insertions, 221 deletions
diff --git a/runtime/autoload/tohtml.vim b/runtime/autoload/tohtml.vim index 4ae17815ba..b1693efc5d 100644 --- a/runtime/autoload/tohtml.vim +++ b/runtime/autoload/tohtml.vim @@ -1,6 +1,6 @@ " Vim autoload file for the tohtml plugin. " Maintainer: Ben Fritz <fritzophrenic@gmail.com> -" Last Change: 2019 Aug 16 +" Last Change: 2023 Sep 03 " " Additional contributors: " @@ -307,7 +307,7 @@ func! tohtml#Convert2HTML(line1, line2) "{{{ let g:html_diff_win_num = 0 for window in win_list " switch to the next buffer to convert - exe ":" . bufwinnr(window) . "wincmd w" + exe ":" .. bufwinnr(window) .. "wincmd w" " figure out whether current charset and encoding will work, if not " default to UTF-8 @@ -351,68 +351,70 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ let s:old_magic = &magic set magic - if s:settings.use_xhtml - if s:settings.encoding != "" - let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>" - else - let xml_line = "<?xml version=\"1.0\"?>" + let html = [] + if !s:settings.no_doc + if s:settings.use_xhtml + if s:settings.encoding != "" + let xml_line = "<?xml version=\"1.0\" encoding=\"" .. s:settings.encoding .. "\"?>" + else + let xml_line = "<?xml version=\"1.0\"?>" + endif + let tag_close = ' />' endif - let tag_close = ' />' - endif - let style = [s:settings.use_xhtml ? "" : '-->'] - let body_line = '' + let style = [s:settings.use_xhtml ? "" : '-->'] + let body_line = '' - let html = [] - let s:html5 = 0 - if s:settings.use_xhtml - call add(html, xml_line) - endif - if s:settings.use_xhtml - call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">") - call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">') - elseif s:settings.use_css && !s:settings.no_pre - call add(html, "<!DOCTYPE html>") - call add(html, '<html>') - let s:html5 = 1 - else - call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"') - call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">') - call add(html, '<html>') - endif - call add(html, '<head>') - - " include encoding as close to the top as possible, but only if not already - " contained in XML information - if s:settings.encoding != "" && !s:settings.use_xhtml - if s:html5 - call add(html, '<meta charset="' . s:settings.encoding . '"' . tag_close) + let s:html5 = 0 + if s:settings.use_xhtml + call add(html, xml_line) + endif + if s:settings.use_xhtml + call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">") + call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">') + elseif s:settings.use_css && !s:settings.no_pre + call add(html, "<!DOCTYPE html>") + call add(html, '<html>') + let s:html5 = 1 else - call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close) + call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"') + call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">') + call add(html, '<html>') + endif + call add(html, '<head>') + + " include encoding as close to the top as possible, but only if not already + " contained in XML information + if s:settings.encoding != "" && !s:settings.use_xhtml + if s:html5 + call add(html, '<meta charset="' .. s:settings.encoding .. '"' .. tag_close) + else + call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" .. s:settings.encoding .. '"' .. tag_close) + endif endif - endif - call add(html, '<title>diff</title>') - call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close) - call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close) - call add(html, '<meta name="settings" content="'. - \ join(filter(keys(s:settings),'s:settings[v:val]'),','). - \ ',prevent_copy='.s:settings.prevent_copy. - \ ',use_input_for_pc='.s:settings.use_input_for_pc. - \ '"'.tag_close) - call add(html, '<meta name="colorscheme" content="'. - \ (exists('g:colors_name') - \ ? g:colors_name - \ : 'none'). '"'.tag_close) - - call add(html, '</head>') - let body_line_num = len(html) - call add(html, '<body'.(s:settings.line_ids ? ' onload="JumpToLine();"' : '').'>') - call add(html, "<table ".(s:settings.use_css? "" : "border='1' width='100%' ")."id='vimCodeElement".s:settings.id_suffix."'>") + call add(html, '<title>diff</title>') + call add(html, '<meta name="Generator" content="Vim/'..v:version/100..'.'..v:version%100..'"'..tag_close) + call add(html, '<meta name="plugin-version" content="'..g:loaded_2html_plugin..'"'..tag_close) + call add(html, '<meta name="settings" content="'. + \ join(filter(keys(s:settings),'s:settings[v:val]'),','). + \ ',prevent_copy='..s:settings.prevent_copy. + \ ',use_input_for_pc='..s:settings.use_input_for_pc. + \ '"'..tag_close) + call add(html, '<meta name="colorscheme" content="'. + \ (exists('g:colors_name') + \ ? g:colors_name + \ : 'none').. '"'..tag_close) + + call add(html, '</head>') + let body_line_num = len(html) + call add(html, '<body'..(s:settings.line_ids ? ' onload="JumpToLine();"' : '')..'>') + endif + call add(html, "<table "..(s:settings.use_css? "" : "border='1' width='100%' ").."id='vimCodeElement"..s:settings.id_suffix.."'>") call add(html, '<tr>') for buf in a:win_list - call add(html, '<th>'.bufname(buf).'</th>') + call add(html, '<th>'..bufname(buf)..'</th>') endfor call add(html, '</tr><tr>') @@ -421,7 +423,7 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ for buf in a:buf_list let temp = [] - exe bufwinnr(buf) . 'wincmd w' + exe bufwinnr(buf) .. 'wincmd w' " If text is folded because of user foldmethod settings, etc. we don't want " to act on everything in a fold by mistake. @@ -430,47 +432,53 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ " When not using CSS or when using xhtml, the <body> line can be important. " Assume it will be the same for all buffers and grab it from the first " buffer. Similarly, need to grab the body end line as well. - if body_line == '' - 1 - call search('<body') - let body_line = getline('.') - $ - call search('</body>', 'b') - let s:body_end_line = getline('.') - endif + if !s:settings.no_doc + if body_line == '' + 1 + call search('<body') + let body_line = getline('.') + $ + call search('</body>', 'b') + let s:body_end_line = getline('.') + endif - " Grab the style information. Some of this will be duplicated so only insert - " it if it's not already there. {{{ - 1 - let style_start = search('^<style\( type="text/css"\)\?>') - 1 - let style_end = search('^</style>') - if style_start > 0 && style_end > 0 - let buf_styles = getline(style_start + 1, style_end - 1) - for a_style in buf_styles - if index(style, a_style) == -1 - if diff_style_start == 0 - if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)' - let diff_style_start = len(style)-1 + " Grab the style information. Some of this will be duplicated so only insert + " it if it's not already there. {{{ + 1 + let style_start = search('^<style\( type="text/css"\)\?>') + 1 + let style_end = search('^</style>') + if style_start > 0 && style_end > 0 + let buf_styles = getline(style_start + 1, style_end - 1) + for a_style in buf_styles + if index(style, a_style) == -1 + if diff_style_start == 0 + if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)' + let diff_style_start = len(style)-1 + endif endif + call insert(style, a_style, insert_index) + let insert_index += 1 endif - call insert(style, a_style, insert_index) - let insert_index += 1 - endif - endfor - endif " }}} + endfor + endif " }}} - " everything new will get added before the diff styles so diff highlight - " properly overrides normal highlight - if diff_style_start != 0 - let insert_index = diff_style_start - endif + " everything new will get added before the diff styles so diff highlight + " properly overrides normal highlight + if diff_style_start != 0 + let insert_index = diff_style_start + endif - " Delete those parts that are not needed so we can include the rest into the - " resulting table. - 1,/^<body.*\%(\n<!--.*-->\_s\+.*id='oneCharWidth'.*\_s\+.*id='oneInputWidth'.*\_s\+.*id='oneEmWidth'\)\?\zs/d_ - $ - ?</body>?,$d_ + " Delete those parts that are not needed so we can include the rest into the + " resulting table. + 1,/^<body.*\%(\n<!--.*-->\_s\+.*id='oneCharWidth'.*\_s\+.*id='oneInputWidth'.*\_s\+.*id='oneEmWidth'\)\?\zs/d_ + $ + ?</body>?,$d_ + elseif !s:settings.no_modeline + " remove modeline from source files if it is included and we haven't deleted + " due to removing html footer already + $d + endif let temp = getline(1,'$') " clean out id on the main content container because we already set it on " the table @@ -478,7 +486,11 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ " undo deletion of start and end part " so we can later save the file as valid html " TODO: restore using grabbed lines if undolevel is 1? - normal! 2u + if !s:settings.no_doc + normal! 2u + elseif !s:settings.no_modeline + normal! u + endif if s:settings.use_css call add(html, '<td><div>') elseif s:settings.use_xhtml @@ -495,29 +507,35 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ quit! endfor - let html[body_line_num] = body_line + if !s:settings.no_doc + let html[body_line_num] = body_line + endif call add(html, '</tr>') call add(html, '</table>') - call add(html, s:body_end_line) - call add(html, '</html>') + if !s:settings.no_doc + call add(html, s:body_end_line) + call add(html, '</html>') + endif " The generated HTML is admittedly ugly and takes a LONG time to fold. " Make sure the user doesn't do syntax folding when loading a generated file, " using a modeline. - call add(html, '<!-- vim: set foldmethod=manual : -->') + if !s:settings.no_modeline + call add(html, '<!-- vim: set foldmethod=manual : -->') + endif let i = 1 - let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html") + let name = "Diff" .. (s:settings.use_xhtml ? ".xhtml" : ".html") " Find an unused file name if current file name is already in use while filereadable(name) - let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e") + let name = substitute(name, '\d*\.x\?html$', '', '') .. i .. '.' .. fnamemodify(copy(name), ":t:e") let i += 1 endwhile let s:ei_sav = &eventignore set eventignore+=FileType - exe "topleft new " . name + exe "topleft new " .. name let &eventignore=s:ei_sav unlet s:ei_sav @@ -542,129 +560,131 @@ func! tohtml#Diff2HTML(win_list, buf_list) "{{{ call append(0, html) - if len(style) > 0 - 1 - let style_start = search('^</head>')-1 + if !s:settings.no_doc + if len(style) > 0 + 1 + let style_start = search('^</head>')-1 - " add required javascript in reverse order so we can just call append again - " and again without adjusting {{{ + " add required javascript in reverse order so we can just call append again + " and again without adjusting {{{ - let s:uses_script = s:settings.dynamic_folds || s:settings.line_ids + let s:uses_script = s:settings.dynamic_folds || s:settings.line_ids - " insert script closing tag if needed - if s:uses_script - call append(style_start, [ - \ '', - \ s:settings.use_xhtml ? '//]]>' : '-->', - \ "</script>" - \ ]) - endif + " insert script closing tag if needed + if s:uses_script + call append(style_start, [ + \ '', + \ s:settings.use_xhtml ? '//]]>' : '-->', + \ "</script>" + \ ]) + endif - " insert javascript to get IDs from line numbers, and to open a fold before - " jumping to any lines contained therein - if s:settings.line_ids - call append(style_start, [ - \ " /* Always jump to new location even if the line was hidden inside a fold, or", - \ " * we corrected the raw number to a line ID.", - \ " */", - \ " if (lineElem) {", - \ " lineElem.scrollIntoView(true);", - \ " }", - \ " return true;", - \ "}", - \ "if ('onhashchange' in window) {", - \ " window.onhashchange = JumpToLine;", - \ "}" - \ ]) + " insert javascript to get IDs from line numbers, and to open a fold before + " jumping to any lines contained therein + if s:settings.line_ids + call append(style_start, [ + \ " /* Always jump to new location even if the line was hidden inside a fold, or", + \ " * we corrected the raw number to a line ID.", + \ " */", + \ " if (lineElem) {", + \ " lineElem.scrollIntoView(true);", + \ " }", + \ " return true;", + \ "}", + \ "if ('onhashchange' in window) {", + \ " window.onhashchange = JumpToLine;", + \ "}" + \ ]) - if s:settings.dynamic_folds + if s:settings.dynamic_folds + call append(style_start, [ + \ "", + \ " /* navigate upwards in the DOM tree to open all folds containing the line */", + \ " var node = lineElem;", + \ " while (node && node.id != 'vimCodeElement"..s:settings.id_suffix.."')", + \ " {", + \ " if (node.className == 'closed-fold')", + \ " {", + \ " /* toggle open the fold ID (remove window ID) */", + \ " toggleFold(node.id.substr(4));", + \ " }", + \ " node = node.parentNode;", + \ " }", + \ ]) + endif + endif + + if s:settings.line_ids call append(style_start, [ \ "", - \ " /* navigate upwards in the DOM tree to open all folds containing the line */", - \ " var node = lineElem;", - \ " while (node && node.id != 'vimCodeElement".s:settings.id_suffix."')", - \ " {", - \ " if (node.className == 'closed-fold')", - \ " {", - \ " /* toggle open the fold ID (remove window ID) */", - \ " toggleFold(node.id.substr(4));", - \ " }", - \ " node = node.parentNode;", + \ "/* function to open any folds containing a jumped-to line before jumping to it */", + \ "function JumpToLine()", + \ "{", + \ " var lineNum;", + \ " lineNum = window.location.hash;", + \ " lineNum = lineNum.substr(1); /* strip off '#' */", + \ "", + \ " if (lineNum.indexOf('L') == -1) {", + \ " lineNum = 'L'+lineNum;", \ " }", + \ " if (lineNum.indexOf('W') == -1) {", + \ " lineNum = 'W1'+lineNum;", + \ " }", + \ " var lineElem = document.getElementById(lineNum);" \ ]) endif - endif - if s:settings.line_ids - call append(style_start, [ - \ "", - \ "/* function to open any folds containing a jumped-to line before jumping to it */", - \ "function JumpToLine()", - \ "{", - \ " var lineNum;", - \ " lineNum = window.location.hash;", - \ " lineNum = lineNum.substr(1); /* strip off '#' */", - \ "", - \ " if (lineNum.indexOf('L') == -1) {", - \ " lineNum = 'L'+lineNum;", - \ " }", - \ " if (lineNum.indexOf('W') == -1) {", - \ " lineNum = 'W1'+lineNum;", - \ " }", - \ " var lineElem = document.getElementById(lineNum);" - \ ]) - endif + " Insert javascript to toggle matching folds open and closed in all windows, + " if dynamic folding is active. + if s:settings.dynamic_folds + call append(style_start, [ + \ " function toggleFold(objID)", + \ " {", + \ " for (win_num = 1; win_num <= "..len(a:buf_list).."; win_num++)", + \ " {", + \ " var fold;", + \ ' fold = document.getElementById("win"+win_num+objID);', + \ " if(fold.className == 'closed-fold')", + \ " {", + \ " fold.className = 'open-fold';", + \ " }", + \ " else if (fold.className == 'open-fold')", + \ " {", + \ " fold.className = 'closed-fold';", + \ " }", + \ " }", + \ " }", + \ ]) + endif - " Insert javascript to toggle matching folds open and closed in all windows, - " if dynamic folding is active. - if s:settings.dynamic_folds - call append(style_start, [ - \ " function toggleFold(objID)", - \ " {", - \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)", - \ " {", - \ " var fold;", - \ ' fold = document.getElementById("win"+win_num+objID);', - \ " if(fold.className == 'closed-fold')", - \ " {", - \ " fold.className = 'open-fold';", - \ " }", - \ " else if (fold.className == 'open-fold')", - \ " {", - \ " fold.className = 'closed-fold';", - \ " }", - \ " }", - \ " }", - \ ]) - endif + if s:uses_script + " insert script tag if needed + call append(style_start, [ + \ "<script" .. (s:html5 ? "" : " type='text/javascript'") .. ">", + \ s:settings.use_xhtml ? '//<![CDATA[' : "<!--"]) + endif - if s:uses_script - " insert script tag if needed - call append(style_start, [ - \ "<script" . (s:html5 ? "" : " type='text/javascript'") . ">", - \ s:settings.use_xhtml ? '//<![CDATA[' : "<!--"]) + " Insert styles from all the generated html documents and additional styles + " for the table-based layout of the side-by-side diff. The diff should take + " up the full browser window (but not more), and be static in size, + " horizontally scrollable when the lines are too long. Otherwise, the diff + " is pretty useless for really long lines. {{{ + if s:settings.use_css + call append(style_start, + \ ['<style' .. (s:html5 ? '' : 'type="text/css"') .. '>']+ + \ style+ + \ [ s:settings.use_xhtml ? '' : '<!--', + \ 'table { table-layout: fixed; }', + \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }', + \ 'table, td, th { border: 1px solid; }', + \ 'td { vertical-align: top; }', + \ 'th, td { width: '..printf("%.1f",100.0/len(a:win_list))..'%; }', + \ 'td div { overflow: auto; }', + \ s:settings.use_xhtml ? '' : '-->', + \ '</style>' + \]) + endif "}}} endif - - " Insert styles from all the generated html documents and additional styles - " for the table-based layout of the side-by-side diff. The diff should take - " up the full browser window (but not more), and be static in size, - " horizontally scrollable when the lines are too long. Otherwise, the diff - " is pretty useless for really long lines. {{{ - if s:settings.use_css - call append(style_start, - \ ['<style' . (s:html5 ? '' : 'type="text/css"') . '>']+ - \ style+ - \ [ s:settings.use_xhtml ? '' : '<!--', - \ 'table { table-layout: fixed; }', - \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }', - \ 'table, td, th { border: 1px solid; }', - \ 'td { vertical-align: top; }', - \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }', - \ 'td div { overflow: auto; }', - \ s:settings.use_xhtml ? '' : '-->', - \ '</style>' - \]) - endif "}}} endif let &paste = s:old_paste @@ -674,7 +694,7 @@ endfunc "}}} " Gets a single user option and sets it in the passed-in Dict, or gives it the " default value if the option doesn't actually exist. func! tohtml#GetOption(settings, option, default) "{{{ - if exists('g:html_'.a:option) + if exists('g:html_'..a:option) let a:settings[a:option] = g:html_{a:option} else let a:settings[a:option] = a:default @@ -693,10 +713,11 @@ func! tohtml#GetUserSettings() "{{{ let user_settings = {} " Define the correct option if the old option name exists and we haven't - " already defined the correct one. Maybe I'll put out a warning message about - " this sometime and remove the old option entirely at some even later time, - " but for now just silently accept the old option. + " already defined the correct one. if exists('g:use_xhtml') && !exists("g:html_use_xhtml") + echohl WarningMsg + echomsg "Warning: g:use_xhtml is deprecated, use g:html_use_xhtml" + echohl None let g:html_use_xhtml = g:use_xhtml endif @@ -719,7 +740,7 @@ func! tohtml#GetUserSettings() "{{{ call tohtml#GetOption(user_settings, 'whole_filler', 0 ) call tohtml#GetOption(user_settings, 'use_xhtml', 0 ) call tohtml#GetOption(user_settings, 'line_ids', user_settings.number_lines ) - call tohtml#GetOption(user_settings, 'use_input_for_pc', 'fallback') + call tohtml#GetOption(user_settings, 'use_input_for_pc', 'none') " }}} " override those settings that need it {{{ @@ -834,16 +855,16 @@ func! tohtml#GetUserSettings() "{{{ if user_settings.use_css if exists("g:html_prevent_copy") if user_settings.dynamic_folds && !user_settings.no_foldcolumn && g:html_prevent_copy =~# 'f' - let user_settings.prevent_copy .= 'f' + let user_settings.prevent_copy ..= 'f' endif if user_settings.number_lines && g:html_prevent_copy =~# 'n' - let user_settings.prevent_copy .= 'n' + let user_settings.prevent_copy ..= 'n' endif if &diff && g:html_prevent_copy =~# 'd' - let user_settings.prevent_copy .= 'd' + let user_settings.prevent_copy ..= 'd' endif if !user_settings.ignore_folding && g:html_prevent_copy =~# 't' - let user_settings.prevent_copy .= 't' + let user_settings.prevent_copy ..= 't' endif else let user_settings.prevent_copy = "" @@ -855,10 +876,10 @@ func! tohtml#GetUserSettings() "{{{ " enforce valid values for use_input_for_pc if user_settings.use_input_for_pc !~# 'fallback\|none\|all' - let user_settings.use_input_for_pc = 'fallback' + let user_settings.use_input_for_pc = 'none' echohl WarningMsg - echomsg '2html: "' . g:html_use_input_for_pc . '" is not valid for g:html_use_input_for_pc' - echomsg '2html: defaulting to "' . user_settings.use_input_for_pc . '"' + echomsg '2html: "' .. g:html_use_input_for_pc .. '" is not valid for g:html_use_input_for_pc' + echomsg '2html: defaulting to "' .. user_settings.use_input_for_pc .. '"' echohl None sleep 3 endif |