'
endif
return substitute(a:attr, '.', '<&>', 'g') . bgs . fgs . substitute(substitute(substitute(substitute(substitute(a:txt, '&', '\&', 'g'), '<', '\<', 'g'), '>', '\>', 'g'), '"', '\"', 'g'), "\x0c", '
', 'g') . fge . bge . substitute(a:attr[2] . a:attr[1] . a:attr[0], '.', '', 'g')
endfun
" Set some options to make it work faster.
" Expand tabs in original buffer to get 'tabstop' correctly used.
let old_title = &title
let old_icon = &icon
let old_paste = &paste
let old_et = &et
set notitle noicon paste et
" Split window to create a buffer with the HTML file.
if expand("%") == ""
new Untitled.html
else
new %.html
endif
1,$d
set noet
" Find out the background and foreground color.
let bg = synIDattr(highlightID("Normal"), "bg#", "gui")
let fg = synIDattr(highlightID("Normal"), "fg#", "gui")
if bg == ""
if &background == "dark"
let bg = "#000000"
if fg == ""
let fg = "#FFFFFF"
endif
else
let bg = "#FFFFFF"
if fg == ""
let fg = "#000000"
endif
endif
endif
" Insert HTML header, with the background color. Add the foreground color
" only when it is defined.
exe "normal a\n\n".expand("%:t")."\n\n\n\n\e"
exe "normal \p"
" Some 'constants' for ease of addressing with []
let uline="U"
let bld="B"
let itl="I"
" Loop over all lines in the original text
let end = line("$")
let lnum = 1
while lnum <= end
" Get the current line, with tabs expanded to spaces when needed
let line = getline(lnum)
if match(line, "\t") >= 0
exe lnum . "retab!"
let did_retab = 1
let line = getline(lnum)
else
let did_retab = 0
endif
let len = strlen(line)
let new = ""
" Loop over each character in the line
let col = 1
while col <= len
let startcol = col " The start column for processing text
let id = synID(lnum, col, 1)
let col = col + 1
" Speed loop (it's small - that's the trick)
" Go along till we find a change in synID
while col <= len && id == synID(lnum, col, 1) | let col = col + 1 | endwhile
" output the text with the same synID, with all its attributes
" The first part turns attributes into [U][I][B]
let id = synIDtrans(id)
let new = new . HTMLPutText(uline[synIDattr(id, "underline", "gui") - 1] . itl[synIDattr(id, "italic", "gui") - 1] . bld[synIDattr(id, "bold", "gui") - 1], synIDattr(id, "bg#", "gui"), synIDattr(id, "fg#", "gui"), strpart(line, startcol - 1, col - startcol))
if col > len
break
endif
endwhile
if did_retab
undo
endif
exe "normal \pa" . strtrans(new) . "\n\e\p"
let lnum = lnum + 1
+
endwhile
" Finish with the last line
exe "normal \pa
\n\n\e"
let &title = old_title
let &icon = old_icon
let &paste = old_paste
exe "normal \p"
let &et = old_et
exe "normal \p"
" In case they didn't get used
let startcol = 0
let id = 0
unlet uline bld itl lnum end col startcol line len new id
unlet old_title old_icon old_paste old_et did_retab