vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

tohtml.vim (15160B)


      1 vim9script
      2 # Vim plugin for converting a syntax highlighted file to HTML.
      3 # Maintainer: Ben Fritz <fritzophrenic@gmail.com>
      4 # Last Change: 2026 Apr 9
      5 #
      6 # The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
      7 # $VIMRUNTIME/syntax/2html.vim
      8 #
      9 if exists('g:loaded_2html_plugin')
     10   finish
     11 endif
     12 g:loaded_2html_plugin = 'vim9.2_v1'
     13 
     14 #
     15 # Changelog: {{{
     16 #   9.2_v1  (this version): - mao-yining: Convert main script to vim9script
     17 #                             (SourceForge issue #28/Vim Github PR #19915).
     18 #
     19 #   Unspecified version:    - Inherit links colors from colorscheme styles for
     20 #                             auto-generated links in HTML output (Vim Github
     21 #                             issue #10191).
     22 #
     23 #   9.0 updates: {{{
     24 #   9.0_v2  (Vim 9.0.1885): - Warn if using deprecated g:use_xhtml option
     25 #                           - Change default g:html_use_input_for_pc to "none"
     26 #                             instead of "fallback". All modern browsers support
     27 #                             the "user-select: none" and "content:" CSS
     28 #                             properties so the older method relying on extra
     29 #                             markup and unspecified browser/app clipboard
     30 #                             handling is only needed in rare special cases.
     31 #                           - Fix SourceForge issue #33: generate diff filler
     32 #                             correctly when new lines have been added to or
     33 #                             removed from end of buffer.
     34 #                           - Fix SourceForge issue #32/Vim Github issue #8547:
     35 #                             use translated highlight ID for styling the
     36 #                             special-use group names (e.g. LineNr) used
     37 #                             directly by name in the 2html processing.
     38 #                           - Fix SourceForge issue #26, refactoring to use
     39 #                             :let-heredoc style string assignment and
     40 #                             additional fixes for ".." vs. "." style string
     41 #                             concatenation. Requires Vim v8.1.1354 or higher.
     42 #   9.0_v1  (Vim 9.0.1275): - Implement g:html_no_doc and g:html_no_modeline
     43 #                             for diff mode. Add tests.
     44 #           (Vim 9.0.1122): NOTE: no version string update for this version!
     45 #                           - Bugfix for variable name in g:html_no_doc
     46 #           (Vim 9.0.0819): NOTE: no version string update for this version!
     47 #                           - Add options g:html_no_doc, g:html_no_lines,
     48 #                             and g:html_no_modeline (partially included in Vim
     49 #                             runtime prior to version string update).
     50 #                           - Updates for new Vim9 string append style (i.e. use
     51 #                             ".." instead of "."). Requires Vim version
     52 #                             8.1.1114 or higher.
     53 #}}}
     54 #
     55 #   8.1 updates: {{{
     56 #   8.1_v2  (Vim 8.1.2312): - Fix SourceForge issue #19: fix calculation of tab
     57 #                             stop position to use in expanding a tab, when that
     58 #                             tab occurs after a syntax match which in turn
     59 #                             comes after previously expanded tabs.
     60 #                           - Set eventignore while splitting a window for the
     61 #                             destination file to ignore FileType events;
     62 #                             speeds up processing when the destination file
     63 #                             already exists and HTML highlight takes too long.
     64 #                           - Fix SourceForge issue #20: progress bar could not be
     65 #                             seen when DiffDelete background color matched
     66 #                             StatusLine background color. Added TOhtmlProgress
     67 #                             highlight group for manual user override, but
     68 #                             calculate it to be visible compared to StatusLine
     69 #                             by default.
     70 #                           - Fix SourceForge issue #1: Remove workaround for old
     71 #                             browsers which don't support 'ch' CSS unit, since
     72 #                             all modern browsers, including IE>=9, support it.
     73 #                           - Fix SourceForge issue #10: support termguicolors
     74 #                           - Fix SourceForge issue #21: default to using
     75 #                             generated content instead of <input> tags for
     76 #                             uncopyable text, so that text is correctly
     77 #                             prevented from being copied in chrome. Use
     78 #                             g:html_use_input_for_pc option to control the
     79 #                             method used.
     80 #                           - Switch to HTML5 to allow using vnu as a validator
     81 #                             in unit test.
     82 #                           - Fix fallback sizing of <input> tags for browsers
     83 #                             without "ch" support.
     84 #                           - Fix cursor on unselectable diff filler text.
     85 #   8.1_v1  (Vim 8.1.0528): - Fix SourceForge issue #6: Don't generate empty
     86 #                             script tag.
     87 #                           - Fix SourceForge issue #5: javascript should
     88 #                             declare variables with "var".
     89 #                           - Fix SourceForge issue #13: errors thrown sourcing
     90 #                             2html.vim directly when plugins not loaded.
     91 #                           - Fix SourceForge issue #16: support 'vartabstop'.
     92 #}}}
     93 #
     94 #   7.4 updates: {{{
     95 #   7.4_v2  (Vim 7.4.0899): Fix error raised when converting a diff containing
     96 #                           an empty buffer. Jan Stocker: allow g:html_font to
     97 #                           take a list so it is easier to specfiy fallback
     98 #                           fonts in the generated CSS.
     99 #   7.4_v1  (Vim 7.4.0000): Fix modeline mangling for new "Vim:" format, and
    100 #			    also for version-specific modelines like "vim>703:".
    101 #}}}
    102 #
    103 #   7.3 updates: {{{
    104 #   7.3_v14 (Vim 7.3.1246): Allow suppressing line number anchors using
    105 #			    g:html_line_ids=0. Allow customizing
    106 #			    important IDs (like line IDs and fold IDs) using
    107 #			    g:html_id_expr evaluated when the buffer conversion
    108 #			    is started.
    109 #   7.3_v13 (Vim 7.3.1088): Keep foldmethod at manual in the generated file and
    110 #			    insert modeline to set it to manual.
    111 #			    Fix bug: diff mode with 2 unsaved buffers creates a
    112 #			    duplicate of one buffer instead of including both.
    113 #			    Add anchors to each line so you can put '#L123'
    114 #			    or '#123' at the end of the URL to jump to line 123
    115 #			    (idea by Andy Spencer). Add javascript to open folds
    116 #			    to show the anchor being jumped to if it is hidden.
    117 #			    Fix XML validation error: &nsbp; not part of XML.
    118 #			    Allow TOhtml to chain together with other commands
    119 #			    using |.
    120 #   7.3_v12 (Vim 7.3.0616): Fix modeline mangling to also work for when multiple
    121 #			    highlight groups make up the start-of-modeline text.
    122 #			    Improve render time of page with uncopyable regions
    123 #			    by not using one-input-per-char. Change name of
    124 #			    uncopyable option from html_unselectable to
    125 #			    html_prevent_copy. Added html_no_invalid option and
    126 #			    default to inserting invalid markup for uncopyable
    127 #			    regions to prevent MS Word from pasting undeletable
    128 #			    <input> elements. Fix 'cpo' handling (Thilo Six).
    129 #		 7.3_v12b1: Add html_unselectable option. Rework logic to
    130 #			    eliminate post-processing substitute commands in
    131 #			    favor of doing the work up front. Remove unnecessary
    132 #			    special treatment of 'LineNr' highlight group. Minor
    133 #			    speed improvements. Fix modeline mangling in
    134 #			    generated output so it works for text in the first
    135 #			    column. Fix missing line number and fold column in
    136 #			    diff filler lines. Fix that some fonts have a 1px
    137 #			    gap (using a dirty hack, improvements welcome). Add
    138 #			    "colorscheme" meta tag. Does NOT include support for
    139 #			    the new default foldtext added in v11, as the patch
    140 #			    adding it has not yet been included in Vim.
    141 #   7.3_v11 ( unreleased ): Support new default foldtext from patch by Christian
    142 #			    Brabandt in
    143 #			    http://groups.google.com/d/topic/vim_dev/B6FSGfq9VoI/discussion.
    144 #			    This patch has not yet been included in Vim, thus
    145 #			    these changes are removed in the next version.
    146 #   7.3_v10 (Vim 7.3.0227): Fix error E684 when converting a range wholly inside
    147 #			    multiple nested folds with dynamic folding on.
    148 #			    Also fix problem with foldtext in this situation.
    149 #   7.3_v9  (Vim 7.3.0170): Add html_pre_wrap option active with html_use_css
    150 #			    and without html_no_pre, default value same as
    151 #			    'wrap' option, (Andy Spencer). Don't use
    152 #			    'fileencoding' for converted document encoding if
    153 #			    'buftype' indicates a special buffer which isn't
    154 #			    written.
    155 #   7.3_v8  (Vim 7.3.0100): Add html_expand_tabs option to allow leaving tab
    156 #			    characters in generated output (Andy Spencer).
    157 #			    Escape text that looks like a modeline so Vim
    158 #			    doesn't use anything in the converted HTML as a
    159 #			    modeline. Bugfixes: Fix folding when a fold starts
    160 #			    before the conversion range. Remove fold column when
    161 #			    there are no folds.
    162 #   7.3_v7  (Vim 7-3-0063): see betas released on vim_dev below:
    163 #		  7.3_v7b3: Fixed bug, convert Unicode to UTF-8 all the way.
    164 #		  7.3_v7b2: Remove automatic detection of encodings that are not
    165 #			    supported by all major browsers according to
    166 #			    http://wiki.whatwg.org/wiki/Web_Encodings and
    167 #			    convert to UTF-8 for all Unicode encodings. Make
    168 #			    HTML encoding to Vim encoding detection be
    169 #			    case-insensitive for built-in pairs.
    170 #		  7.3_v7b1: Remove use of setwinvar() function which cannot be
    171 #			    called in restricted mode (Andy Spencer). Use
    172 #			    'fencoding' instead of 'encoding' to determine by
    173 #			    charset, and make sure the 'fenc' of the generated
    174 #			    file matches its indicated charset. Add charsets for
    175 #			    all of Vim's natively supported encodings.
    176 #   7.3_v6  (Vim 7.3.0000): Really fix bug with 'nowrapscan', 'magic' and other
    177 #			    user settings interfering with diff mode generation,
    178 #			    trailing whitespace (e.g. line number column) when
    179 #			    using html_no_pre, and bugs when using
    180 #			    html_hover_unfold.
    181 #   7.3_v5  ( unreleased ): Fix bug with 'nowrapscan' and also with out-of-sync
    182 #			    folds in diff mode when first line was folded.
    183 #   7.3_v4  (Vim 7.3.0000): Bugfixes, especially for xhtml markup, and diff mode
    184 #   7.3_v3  (Vim 7.3.0000): Refactor option handling and make html_use_css
    185 #			    default to true when not set to anything. Use strict
    186 #			    doctypes where possible. Rename use_xhtml option to
    187 #			    html_use_xhtml for consistency. Use .xhtml extension
    188 #			    when using this option. Add meta tag for settings.
    189 #   7.3_v2  (Vim 7.3.0000): Fix syntax highlighting in diff mode to use both the
    190 #			    diff colors and the normal syntax colors
    191 #   7.3_v1  (Vim 7.3.0000): Add conceal support and meta tags in output
    192 #}}}
    193 #}}}
    194 #
    195 # TODO: {{{
    196 #   * Check the issue tracker:
    197 #     https://sourceforge.net/p/vim-tohtml/issues/search/?q=%21status%3Aclosed
    198 #   * Options for generating the CSS in external style sheets. New :TOcss
    199 #     command to convert the current color scheme into a (mostly) generic CSS
    200 #     stylesheet which can be re-used. Alternate stylesheet support? Good start
    201 #     by Erik Falor
    202 #     ( https://groups.google.com/d/topic/vim_use/7XTmC4D22dU/discussion ).
    203 #   * Add optional argument to :TOhtml command to specify mode (gui, cterm,
    204 #     term) to use for the styling. Suggestion by "nacitar".
    205 #   * Add way to override or specify which RGB colors map to the color numbers
    206 #     in cterm. Get better defaults than just guessing? Suggestion by "nacitar".
    207 #   * Disable filetype detection until after all processing is done.
    208 #   * Add option for not generating the hyperlink on stuff that looks like a
    209 #     URL? Or just color the link to fit with the colorscheme (and only special
    210 #     when hovering)?
    211 #   * Bug: Opera does not allow printing more than one page if uncopyable
    212 #     regions is turned on. Possible solution: Add normal text line numbers with
    213 #     display:none, set to display:inline for print style sheets, and hide
    214 #     <input> elements for print, to allow Opera printing multiple pages (and
    215 #     other uncopyable areas?). May need to make the new text invisible to IE
    216 #     with conditional comments to prevent copying it, IE for some reason likes
    217 #     to copy hidden text. Other browsers too?
    218 #   * Bug: still a 1px gap throughout the fold column when html_prevent_copy is
    219 #     "fn" in some browsers. Specifically, in Chromium on Ubuntu (but not Chrome
    220 #     on Windows). Perhaps it is font related?
    221 #   * Bug: still some gaps in the fold column when html_prevent_copy contains
    222 #     'd' and showing the whole diff (observed in multiple browsers). Only gaps
    223 #     on diff lines though.
    224 #   * Undercurl support via CSS3, with fallback to dotted or something:
    225 #	https://groups.google.com/d/topic/vim_use/BzXA6He1pHg/discussion
    226 #   * Redo updates for modified default foldtext (v11) when/if the patch is
    227 #     accepted to modify it.
    228 #   * Test case +diff_one_file-dynamic_folds+expand_tabs-hover_unfold
    229 #		+ignore_conceal-ignore_folding+no_foldcolumn+no_pre+no_progress
    230 #		+number_lines-pre_wrap-use_css+use_xhtml+whole_filler.xhtml
    231 #     does not show the whole diff filler as it is supposed to?
    232 #   * Bug: when 'isprint' is wrong for the current encoding, will generate
    233 #     invalid content. Can/should anything be done about this? Maybe a separate
    234 #     plugin to correct 'isprint' based on encoding?
    235 #   * Check to see if the windows-125\d encodings actually work in Unix without
    236 #     the 8bit- prefix. Add prefix to autoload dictionaries for Unix if not.
    237 #   * Font auto-detection similar to
    238 #     http://www.vim.org/scripts/script.php?script_id=2384 but for a variety of
    239 #     platforms.
    240 #   * Pull in code from http://www.vim.org/scripts/script.php?script_id=3113 :
    241 #	- listchars support
    242 #	- full-line background highlight
    243 #	- other?
    244 #   * Make it so deleted lines in a diff don't create side-scrolling (get it
    245 #     free with full-line background highlight above).
    246 #   * Restore open/closed folds and cursor position after processing each file
    247 #     with option not to restore for speed increase.
    248 #   * Add extra meta info (generation time, etc.)?
    249 #   * Tidy up so we can use strict doctype in even more situations
    250 #   * Implementation detail: add threshold for writing the lines to the html
    251 #     buffer before we're done (5000 or so lines should do it)
    252 #   * TODO comments for code cleanup scattered throughout
    253 #}}}
    254 
    255 # Define the :TOhtml command when:
    256 # - 'compatible' is not set
    257 # - this plugin or user override was not already loaded
    258 # - user commands are available. {{{
    259 if !&cp && !exists(":TOhtml") && has("user_commands")
    260   command -range=% -bar TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
    261 endif "}}}
    262 
    263 # Make sure any patches will probably use consistent indent
    264 #   vim: ts=8 sw=2 sts=2 noet fdm=marker