diff --git a/runtime/autoload/tohtml.vim b/runtime/autoload/tohtml.vim
--- 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: 2010 Aug 12
+" Last Change: 2010 Sep 09
 "
 " Additional contributors:
 "
@@ -13,6 +13,276 @@
 let s:cpo_sav = &cpo
 set cpo-=C
 
-func! tohtml#Convert2HTML(line1, line2)
+" Automatically find charsets from all encodings supported natively by Vim. With
+" the 8bit- and 2byte- prefixes, Vim can actually support more encodings than
+" this. Let the user specify these however since they won't be supported on
+" every system. TODO: how? g:html_charsets and g:html_encodings?
+"
+" Note, not all of Vim's supported encodings have a charset to use.
+"
+" Names in this list are from:
+"   http://www.iana.org/assignments/character-sets
+" g:tohtml#encoding_to_charset: {{{
+let g:tohtml#encoding_to_charset = {
+      \ 'latin1' : 'ISO-8859-1',
+      \ 'iso-8859-2' : 'ISO-8859-2',
+      \ 'iso-8859-3' : 'ISO-8859-3',
+      \ 'iso-8859-4' : 'ISO-8859-4',
+      \ 'iso-8859-5' : 'ISO-8859-5',
+      \ 'iso-8859-6' : 'ISO-8859-6',
+      \ 'iso-8859-7' : 'ISO-8859-7',
+      \ 'iso-8859-8' : 'ISO-8859-8',
+      \ 'iso-8859-9' : 'ISO-8859-9',
+      \ 'iso-8859-10' : 'ISO-8859-10',
+      \ 'iso-8859-13' : 'ISO-8859-13',
+      \ 'iso-8859-14' : 'ISO-8859-14',
+      \ 'iso-8859-15' : 'ISO-8859-15',
+      \ 'koi8-r' : 'KOI8-R',
+      \ 'koi8-u' : 'KOI8-U',
+      \ 'macroman' : 'macintosh',
+      \ 'cp437' : 'IBM437',
+      \ 'cp775' : 'IBM775',
+      \ 'cp850' : 'IBM850',
+      \ 'cp852' : 'IBM852',
+      \ 'cp855' : 'IBM855',
+      \ 'cp857' : 'IBM857',
+      \ 'cp860' : 'IBM860',
+      \ 'cp861' : 'IBM861',
+      \ 'cp862' : 'IBM862',
+      \ 'cp863' : 'IBM863',
+      \ 'cp865' : 'IBM865',
+      \ 'cp866' : 'IBM866',
+      \ 'cp869' : 'IBM869',
+      \ 'cp874' : 'IBM-Thai',
+      \ 'cp1250' : 'windows-1250',
+      \ 'cp1251' : 'windows-1251',
+      \ 'cp1253' : 'windows-1253',
+      \ 'cp1254' : 'windows-1254',
+      \ 'cp1255' : 'windows-1255',
+      \ 'cp1256' : 'windows-1256',
+      \ 'cp1257' : 'windows-1257',
+      \ 'cp1258' : 'windows-1258',
+      \ 'euc-jp' : 'EUC-JP',
+      \ 'sjis' : 'Shift_JIS',
+      \ 'cp932' : 'Shift_JIS',
+      \ 'cp949' : 'KS_C_5601-1987',
+      \ 'euc-kr' : 'EUC-KR',
+      \ 'cp936' : 'GBK',
+      \ 'euc-cn' : 'GB_2312-80',
+      \ 'big5' : 'Big5',
+      \ 'cp950' : 'Big5',
+      \ 'utf-8' : 'UTF-8',
+      \ 'ucs-2' : 'ISO-10646-UCS-2',
+      \ 'ucs-2le' : 'ISO-10646-UCS-2',
+      \ 'utf-16' : 'UTF-16',
+      \ 'utf-16le' : 'UTF-16LE',
+      \ 'ucs-4' : 'UTF-32',
+      \ 'ucs-4le' : 'UTF-32LE',
+      \ }
+lockvar g:tohtml#encoding_to_charset
+" }}}
+
+" Only automatically find encodings supported natively by Vim, let the user
+" specify the encoding if it's not natively supported. This function is only
+" used when the user specifies the charset, they better know what they are
+" doing!
+"
+" Names in this list are from:
+"   http://www.iana.org/assignments/character-sets
+" g:tohtml#charset_to_encoding: {{{
+let g:tohtml#charset_to_encoding = {
+      \ 'ISO_8859-1:1987' : 'latin1',
+      \ 'iso-ir-100' : 'latin1',
+      \ 'ISO_8859-1' : 'latin1',
+      \ 'ISO-8859-1' : 'latin1',
+      \ 'latin1' : 'latin1',
+      \ 'l1' : 'latin1',
+      \ 'IBM819' : 'latin1',
+      \ 'CP819' : 'latin1',
+      \ 'csISOLatin1' : 'latin1',
+      \ 'ISO_8859-2:1987' : 'iso-8859-2',
+      \ 'iso-ir-101' : 'iso-8859-2',
+      \ 'ISO_8859-2' : 'iso-8859-2',
+      \ 'ISO-8859-2' : 'iso-8859-2',
+      \ 'latin2' : 'iso-8859-2',
+      \ 'l2' : 'iso-8859-2',
+      \ 'csISOLatin2' : 'iso-8859-2',
+      \ 'ISO_8859-3:1988' : 'iso-8859-3',
+      \ 'iso-ir-109' : 'iso-8859-3',
+      \ 'ISO_8859-3' : 'iso-8859-3',
+      \ 'ISO-8859-3' : 'iso-8859-3',
+      \ 'latin3' : 'iso-8859-3',
+      \ 'l3' : 'iso-8859-3',
+      \ 'csISOLatin3' : 'iso-8859-3',
+      \ 'ISO_8859-4:1988' : 'iso-8859-4',
+      \ 'iso-ir-110' : 'iso-8859-4',
+      \ 'ISO_8859-4' : 'iso-8859-4',
+      \ 'ISO-8859-4' : 'iso-8859-4',
+      \ 'latin4' : 'iso-8859-4',
+      \ 'l4' : 'iso-8859-4',
+      \ 'csISOLatin4' : 'iso-8859-4',
+      \ 'ISO_8859-5:1988' : 'iso-8859-5',
+      \ 'iso-ir-144' : 'iso-8859-5',
+      \ 'ISO_8859-5' : 'iso-8859-5',
+      \ 'ISO-8859-5' : 'iso-8859-5',
+      \ 'cyrillic' : 'iso-8859-5',
+      \ 'csISOLatinCyrillic' : 'iso-8859-5',
+      \ 'ISO_8859-6:1987' : 'iso-8859-6',
+      \ 'iso-ir-127' : 'iso-8859-6',
+      \ 'ISO_8859-6' : 'iso-8859-6',
+      \ 'ISO-8859-6' : 'iso-8859-6',
+      \ 'ECMA-114' : 'iso-8859-6',
+      \ 'ASMO-708' : 'iso-8859-6',
+      \ 'arabic' : 'iso-8859-6',
+      \ 'csISOLatinArabic' : 'iso-8859-6',
+      \ 'ISO_8859-7:1987' : 'iso-8859-7',
+      \ 'iso-ir-126' : 'iso-8859-7',
+      \ 'ISO_8859-7' : 'iso-8859-7',
+      \ 'ISO-8859-7' : 'iso-8859-7',
+      \ 'ELOT_928' : 'iso-8859-7',
+      \ 'ECMA-118' : 'iso-8859-7',
+      \ 'greek' : 'iso-8859-7',
+      \ 'greek8' : 'iso-8859-7',
+      \ 'csISOLatinGreek' : 'iso-8859-7',
+      \ 'ISO_8859-8:1988' : 'iso-8859-8',
+      \ 'iso-ir-138' : 'iso-8859-8',
+      \ 'ISO_8859-8' : 'iso-8859-8',
+      \ 'ISO-8859-8' : 'iso-8859-8',
+      \ 'hebrew' : 'iso-8859-8',
+      \ 'csISOLatinHebrew' : 'iso-8859-8',
+      \ 'ISO_8859-9:1989' : 'iso-8859-9',
+      \ 'iso-ir-148' : 'iso-8859-9',
+      \ 'ISO_8859-9' : 'iso-8859-9',
+      \ 'ISO-8859-9' : 'iso-8859-9',
+      \ 'latin5' : 'iso-8859-9',
+      \ 'l5' : 'iso-8859-9',
+      \ 'csISOLatin5' : 'iso-8859-9',
+      \ 'ISO-8859-10' : 'iso-8859-10',
+      \ 'iso-ir-157' : 'iso-8859-10',
+      \ 'l6' : 'iso-8859-10',
+      \ 'ISO_8859-10:1992' : 'iso-8859-10',
+      \ 'csISOLatin6' : 'iso-8859-10',
+      \ 'latin6' : 'iso-8859-10',
+      \ 'ISO-8859-13' : 'iso-8859-13',
+      \ 'ISO-8859-14' : 'iso-8859-14',
+      \ 'iso-ir-199' : 'iso-8859-14',
+      \ 'ISO_8859-14:1998' : 'iso-8859-14',
+      \ 'ISO_8859-14' : 'iso-8859-14',
+      \ 'latin8' : 'iso-8859-14',
+      \ 'iso-celtic' : 'iso-8859-14',
+      \ 'l8' : 'iso-8859-14',
+      \ 'ISO-8859-15' : 'iso-8859-15',
+      \ 'ISO_8859-15' : 'iso-8859-15',
+      \ 'Latin-9' : 'iso-8859-15',
+      \ 'KOI8-R' : 'koi8-r',
+      \ 'csKOI8R' : 'koi8-r',
+      \ 'KOI8-U' : 'koi8-u',
+      \ 'macintosh' : 'macroman',
+      \ 'mac' : 'macroman',
+      \ 'csMacintosh' : 'macroman',
+      \ 'IBM437' : 'cp437',
+      \ 'cp437' : 'cp437',
+      \ '437' : 'cp437',
+      \ 'csPC8CodePage437' : 'cp437',
+      \ 'IBM775' : 'cp775',
+      \ 'cp775' : 'cp775',
+      \ 'csPC775Baltic' : 'cp775',
+      \ 'IBM850' : 'cp850',
+      \ 'cp850' : 'cp850',
+      \ '850' : 'cp850',
+      \ 'csPC850Multilingual' : 'cp850',
+      \ 'IBM852' : 'cp852',
+      \ 'cp852' : 'cp852',
+      \ '852' : 'cp852',
+      \ 'csPCp852' : 'cp852',
+      \ 'IBM855' : 'cp855',
+      \ 'cp855' : 'cp855',
+      \ '855' : 'cp855',
+      \ 'csIBM855' : 'cp855',
+      \ 'IBM857' : 'cp857',
+      \ 'cp857' : 'cp857',
+      \ '857' : 'cp857',
+      \ 'csIBM857' : 'cp857',
+      \ 'IBM860' : 'cp860',
+      \ 'cp860' : 'cp860',
+      \ '860' : 'cp860',
+      \ 'csIBM860' : 'cp860',
+      \ 'IBM861' : 'cp861',
+      \ 'cp861' : 'cp861',
+      \ '861' : 'cp861',
+      \ 'cp-is' : 'cp861',
+      \ 'csIBM861' : 'cp861',
+      \ 'IBM862' : 'cp862',
+      \ 'cp862' : 'cp862',
+      \ '862' : 'cp862',
+      \ 'csPC862LatinHebrew' : 'cp862',
+      \ 'IBM863' : 'cp863',
+      \ 'cp863' : 'cp863',
+      \ '863' : 'cp863',
+      \ 'csIBM863' : 'cp863',
+      \ 'IBM865' : 'cp865',
+      \ 'cp865' : 'cp865',
+      \ '865' : 'cp865',
+      \ 'csIBM865' : 'cp865',
+      \ 'IBM866' : 'cp866',
+      \ 'cp866' : 'cp866',
+      \ '866' : 'cp866',
+      \ 'csIBM866' : 'cp866',
+      \ 'IBM869' : 'cp869',
+      \ 'cp869' : 'cp869',
+      \ '869' : 'cp869',
+      \ 'cp-gr' : 'cp869',
+      \ 'csIBM869' : 'cp869',
+      \ 'windows-1250' : 'cp1250',
+      \ 'windows-1251' : 'cp1251',
+      \ 'windows-1253' : 'cp1253',
+      \ 'windows-1254' : 'cp1254',
+      \ 'windows-1255' : 'cp1255',
+      \ 'windows-1256' : 'cp1256',
+      \ 'windows-1257' : 'cp1257',
+      \ 'windows-1258' : 'cp1258',
+      \ 'Extended_UNIX_Code_Packed_Format_for_Japanese' : 'euc-jp',
+      \ 'csEUCPkdFmtJapanese' : 'euc-jp',
+      \ 'EUC-JP' : 'euc-jp',
+      \ 'Shift_JIS' : 'sjis',
+      \ 'MS_Kanji' : 'sjis',
+      \ 'sjis' : 'sjis',
+      \ 'csShiftJIS' : 'sjis',
+      \ 'IBM-Thai' : 'cp874',
+      \ 'csIBMThai' : 'cp874',
+      \ 'KS_C_5601-1987' : 'cp949',
+      \ 'iso-ir-149' : 'cp949',
+      \ 'KS_C_5601-1989' : 'cp949',
+      \ 'KSC_5601' : 'cp949',
+      \ 'korean' : 'cp949',
+      \ 'csKSC56011987' : 'cp949',
+      \ 'EUC-KR' : 'euc-kr',
+      \ 'csEUCKR' : 'euc-kr',
+      \ 'GBK' : 'cp936',
+      \ 'CP936' : 'cp936',
+      \ 'MS936' : 'cp936',
+      \ 'windows-936' : 'cp936',
+      \ 'GB_2312-80' : 'euc-cn',
+      \ 'iso-ir-58' : 'euc-cn',
+      \ 'chinese' : 'euc-cn',
+      \ 'csISO58GB231280' : 'euc-cn',
+      \ 'Big5' : 'big5',
+      \ 'csBig5' : 'big5',
+      \ 'UTF-8' : 'utf-8',
+      \ 'ISO-10646-UCS-2' : 'ucs-2',
+      \ 'csUnicode' : 'ucs-2',
+      \ 'UTF-16' : 'utf-16',
+      \ 'UTF-16BE' : 'utf-16',
+      \ 'UTF-16LE' : 'utf-16le',
+      \ 'UTF-32' : 'ucs-4',
+      \ 'UTF-32BE' : 'ucs-4',
+      \ 'UTF-32LE' : 'ucs-4le',
+      \ 'ISO-10646-UCS-4' : 'ucs-4',
+      \ 'csUCS4' : 'ucs-4'
+      \ }
+lockvar g:tohtml#charset_to_encoding
+"}}}
+
+func! tohtml#Convert2HTML(line1, line2) "{{{
   let s:settings = tohtml#GetUserSettings()
 
@@ -17,6 +287,6 @@
   let s:settings = tohtml#GetUserSettings()
 
-  if !&diff || s:settings.diff_one_file
+  if !&diff || s:settings.diff_one_file "{{{
     if a:line2 >= a:line1
       let g:html_start_line = a:line1
       let g:html_end_line = a:line2
@@ -24,11 +294,11 @@
       let g:html_start_line = a:line2
       let g:html_end_line = a:line1
     endif
-    runtime syntax/2html.vim
-  else
+    runtime syntax/2html.vim "}}}
+  else "{{{
     let win_list = []
     let buf_list = []
     windo | if &diff | call add(win_list, winbufnr(0)) | endif
     let s:settings.whole_filler = 1
     let g:html_diff_win_num = 0
     for window in win_list
@@ -29,7 +299,8 @@
     let win_list = []
     let buf_list = []
     windo | if &diff | call add(win_list, winbufnr(0)) | endif
     let s:settings.whole_filler = 1
     let g:html_diff_win_num = 0
     for window in win_list
+      " switch to the next buffer to convert
       exe ":" . bufwinnr(window) . "wincmd w"
@@ -35,4 +306,18 @@
       exe ":" . bufwinnr(window) . "wincmd w"
+
+      " figure out whether current charset and encoding will work, if not
+      " default to UTF-8
+      if !exists('g:html_use_encoding') &&
+	    \ (&l:fileencoding!='' && &l:fileencoding!=s:settings.vim_encoding ||
+	    \  &l:fileencoding=='' &&       &encoding!=s:settings.vim_encoding)
+	echohl WarningMsg
+	echomsg "TOhtml: mismatched file encodings in Diff buffers, using UTF-8"
+	echohl None
+	let s:settings.vim_encoding = 'utf-8'
+	let s:settings.encoding = 'UTF-8'
+      endif
+
+      " set up for diff-mode conversion
       let g:html_start_line = 1
       let g:html_end_line = line('$')
       let g:html_diff_win_num += 1
@@ -36,4 +321,6 @@
       let g:html_start_line = 1
       let g:html_end_line = line('$')
       let g:html_diff_win_num += 1
+
+      " convert this file
       runtime syntax/2html.vim
@@ -39,5 +326,7 @@
       runtime syntax/2html.vim
+
+      " remember the HTML buffer for later combination
       call add(buf_list, bufnr('%'))
     endfor
     unlet g:html_diff_win_num
     call tohtml#Diff2HTML(win_list, buf_list)
@@ -40,9 +329,9 @@
       call add(buf_list, bufnr('%'))
     endfor
     unlet g:html_diff_win_num
     call tohtml#Diff2HTML(win_list, buf_list)
-  endif
+  endif "}}}
 
   unlet g:html_start_line
   unlet g:html_end_line
   unlet s:settings
@@ -45,6 +334,6 @@
 
   unlet g:html_start_line
   unlet g:html_end_line
   unlet s:settings
-endfunc
+endfunc "}}}
 
@@ -50,5 +339,5 @@
 
-func! tohtml#Diff2HTML(win_list, buf_list)
+func! tohtml#Diff2HTML(win_list, buf_list) "{{{
   let xml_line = ""
   let tag_close = '>'
 
@@ -87,7 +376,7 @@
   call add(html, '<head>')
 
   " include encoding as close to the top as possible, but only if not already
-  " contained in XML information (to avoid haggling over content type)
+  " contained in XML information
   if s:settings.encoding != "" && !s:settings.use_xhtml
     call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close)
   endif
@@ -133,7 +422,8 @@
       let s:body_end_line = getline('.')
     endif
 
-    " Grab the style information.  Some of this will be duplicated...
+    " 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
@@ -151,5 +441,5 @@
 	  let insert_index += 1
 	endif
       endfor
-    endif
+    endif " }}}
 
@@ -155,4 +445,6 @@
 
+    " 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
@@ -203,6 +495,20 @@
   " just in case some user autocmd creates content in the new buffer, make sure
   " it is empty before proceeding
   %d
+
+  " set the fileencoding to match the charset we'll be using
+  let &l:fileencoding=s:settings.vim_encoding
+
+  " According to http://www.w3.org/TR/html4/charset.html#doc-char-set, the byte
+  " order mark is highly recommend on the web when using multibyte encodings. But,
+  " it is not a good idea to include it on UTF-8 files. Otherwise, let Vim
+  " determine when it is actually inserted.
+  if s:settings.vim_encoding == 'utf-8'
+    setlocal nobomb
+  else
+    setlocal bomb
+  endif
+
   call append(0, html)
 
   if len(style) > 0
@@ -210,7 +516,7 @@
     let style_start = search('^</head>')-1
 
     " Insert javascript to toggle matching folds open and closed in all windows,
-    " if dynamic folding is active.
+    " if dynamic folding is active. {{{
     if s:settings.dynamic_folds
       call append(style_start, [
 	    \  "<script type='text/javascript'>",
@@ -234,9 +540,9 @@
 	    \  s:settings.use_xhtml ? '//]]>' : "  -->",
 	    \  "</script>"
 	    \ ])
-    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
@@ -238,9 +544,9 @@
 
     " 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.
+    " is pretty useless for really long lines. {{{
     if s:settings.use_css
       call append(style_start,
 	    \ ['<style type="text/css">']+
@@ -252,9 +558,9 @@
 	    \   'td div { overflow: auto; }',
 	    \   s:settings.use_xhtml ? '' : '-->',
 	    \   '</style>'
-	    \ ])
-    endif
+	    \])
+    endif "}}}
   endif
 
   let &paste = s:old_paste
   let &magic = s:old_magic
@@ -257,8 +563,8 @@
   endif
 
   let &paste = s:old_paste
   let &magic = s:old_magic
-endfunc
+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.
@@ -262,9 +568,9 @@
 
 " 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)
+func! tohtml#GetOption(settings, option, default) "{{{
   if exists('g:html_'.a:option)
     let a:settings[a:option] = g:html_{a:option}
   else
     let a:settings[a:option] = a:default
   endif
@@ -266,10 +572,10 @@
   if exists('g:html_'.a:option)
     let a:settings[a:option] = g:html_{a:option}
   else
     let a:settings[a:option] = a:default
   endif
-endfunc
+endfunc "}}}
 
 " returns a Dict containing the values of all user options for 2html, including
 " default values for those not given an explicit value by the user. Discards the
 " html_ prefix of the option for nicer looking code.
@@ -272,8 +578,8 @@
 
 " returns a Dict containing the values of all user options for 2html, including
 " default values for those not given an explicit value by the user. Discards the
 " html_ prefix of the option for nicer looking code.
-func! tohtml#GetUserSettings()
+func! tohtml#GetUserSettings() "{{{
   if exists('s:settings')
     " just restore the known options if we've already retrieved them
     return s:settings
@@ -289,7 +595,7 @@
       let g:html_use_xhtml = g:use_xhtml
     endif
 
-    " get current option settings with appropriate defaults
+    " get current option settings with appropriate defaults {{{
     call tohtml#GetOption(user_settings,    'no_progress',  !has("statusline") )
     call tohtml#GetOption(user_settings,  'diff_one_file',  0 )
     call tohtml#GetOption(user_settings,   'number_lines',  &number )
@@ -302,4 +608,5 @@
     call tohtml#GetOption(user_settings,         'no_pre',  0 )
     call tohtml#GetOption(user_settings,   'whole_filler',  0 )
     call tohtml#GetOption(user_settings,      'use_xhtml',  0 )
+    " }}}
     
@@ -305,5 +612,5 @@
     
-    " override those settings that need it
+    " override those settings that need it {{{
 
     " hover opening implies dynamic folding
     if user_settings.hover_unfold
@@ -330,5 +637,5 @@
     " aren't allowed inside a <pre> block
     if !user_settings.use_css
       let user_settings.no_pre = 1
-    endif
+    endif "}}}
 
@@ -334,4 +641,5 @@
 
-    " Figure out proper MIME charset from the 'encoding' option.
-    if exists("g:html_use_encoding")
+    if exists("g:html_use_encoding") "{{{
+      " user specified the desired MIME charset, figure out proper
+      " 'fileencoding' from it or warn the user if we cannot
       let user_settings.encoding = g:html_use_encoding
@@ -337,2 +645,10 @@
       let user_settings.encoding = g:html_use_encoding
+      let user_settings.vim_encoding = tohtml#EncodingFromCharset(g:html_use_encoding)
+      if user_settings.vim_encoding == ''
+	echohl WarningMsg
+	echomsg "TOhtml: file encoding for"
+	      \ g:html_use_encoding
+	      \ "unknown, please set 'fileencoding'"
+	echohl None
+      endif
     else
@@ -338,5 +654,6 @@
     else
-      let vim_encoding = &encoding
-      if vim_encoding =~ '^8bit\|^2byte'
-	let vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '')
+      " Figure out proper MIME charset from 'fileencoding' if possible
+      if &l:fileencoding != ''
+	let user_settings.vim_encoding = &l:fileencoding
+	call tohtml#CharsetFromEncoding(user_settings)
       endif
@@ -342,21 +659,12 @@
       endif
-      if vim_encoding == 'latin1'
-	let user_settings.encoding = 'iso-8859-1'
-      elseif vim_encoding =~ "^cp12"
-	let user_settings.encoding = substitute(vim_encoding, 'cp', 'windows-', '')
-      elseif vim_encoding == 'sjis' || vim_encoding == 'cp932'
-	let user_settings.encoding = 'Shift_JIS'
-      elseif vim_encoding == 'big5' || vim_encoding == 'cp950'
-	let user_settings.encoding = "Big5"
-      elseif vim_encoding == 'euc-cn'
-	let user_settings.encoding = 'GB_2312-80'
-      elseif vim_encoding == 'euc-tw'
-	let user_settings.encoding = ""
-      elseif vim_encoding =~ '^euc\|^iso\|^koi'
-	let user_settings.encoding = substitute(vim_encoding, '.*', '\U\0', '')
-      elseif vim_encoding == 'cp949'
-	let user_settings.encoding = 'KS_C_5601-1987'
-      elseif vim_encoding == 'cp936'
-	let user_settings.encoding = 'GBK'
-      elseif vim_encoding =~ '^ucs\|^utf'
+
+      " else from 'encoding' if possible
+      if &l:fileencoding == '' || user_settings.encoding == ''
+	let user_settings.vim_encoding = &encoding
+	call tohtml#CharsetFromEncoding(user_settings)
+      endif
+
+      " else default to UTF-8 and warn user
+      if user_settings.encoding == ''
+	let user_settings.vim_encoding = 'utf-8'
 	let user_settings.encoding = 'UTF-8'
@@ -362,4 +670,5 @@
 	let user_settings.encoding = 'UTF-8'
-      else
-	let user_settings.encoding = ""
+	echohl WarningMsg
+	echomsg "TOhtml: couldn't determine MIME charset, using UTF-8"
+	echohl None
       endif
@@ -365,7 +674,7 @@
       endif
-    endif
+    endif "}}}
 
     " TODO: font
 
     return user_settings
   endif
@@ -367,11 +676,45 @@
 
     " TODO: font
 
     return user_settings
   endif
-endfunc
+endfunc "}}}
+
+" get the proper HTML charset name from a Vim encoding option.
+function! tohtml#CharsetFromEncoding(settings) "{{{
+  let l:vim_encoding = a:settings.vim_encoding
+  if exists('g:html_charset_override') && has_key(g:html_charset_override, l:vim_encoding)
+    let a:settings.encoding = g:html_charset_override[l:vim_encoding]
+    " since the user has overridden the default charset for this encoding, we
+    " may need to convert to a different encoding to make the two match
+    let a:settings.vim_encoding = tohtml#EncodingFromCharset(a:settings.encoding)
+  else
+    if l:vim_encoding =~ '^8bit\|^2byte'
+      " 8bit- and 2byte- prefixes are to indicate encodings available on the
+      " system that Vim will convert with iconv(), look up just the encoding name,
+      " not Vim's prefix.
+      let l:vim_encoding = substitute(l:vim_encoding, '^8bit-\|^2byte-', '', '')
+    endif
+    if has_key(g:tohtml#encoding_to_charset, l:vim_encoding)
+      let a:settings.encoding = g:tohtml#encoding_to_charset[l:vim_encoding]
+    else
+      let a:settings.encoding = ""
+    endif
+  endif
+endfun "}}}
+
+" Get the proper Vim encoding option setting from an HTML charset name.
+function! tohtml#EncodingFromCharset(encoding) "{{{
+  if exists('g:html_encoding_override') && has_key(g:html_encoding_override, a:encoding)
+    return g:html_encoding_override[a:encoding]
+  elseif has_key(g:tohtml#charset_to_encoding, a:encoding)
+    return g:tohtml#charset_to_encoding[a:encoding]
+  else
+    return ""
+  endif
+endfun "}}}
 
 let &cpo = s:cpo_sav
 unlet s:cpo_sav
 
 " Make sure any patches will probably use consistent indent
@@ -373,6 +716,6 @@
 
 let &cpo = s:cpo_sav
 unlet s:cpo_sav
 
 " Make sure any patches will probably use consistent indent
-"   vim: ts=8 sw=2 sts=2 noet
+"   vim: ts=8 sw=2 sts=2 noet fdm=marker
diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt
--- a/runtime/doc/syntax.txt
+++ b/runtime/doc/syntax.txt
@@ -475,14 +475,32 @@
 This will use <br> at the end of each line and use "&nbsp;" for repeated
 spaces.
 
-The current value of 'encoding' is used to specify the charset of the HTML
-file.  This only works for those values of 'encoding' that have an equivalent
-HTML charset name.  To overrule this set g:html_use_encoding to the name of
-the charset to be used: >
-   :let g:html_use_encoding = "foobar"
-To omit the line that specifies the charset, set g:html_use_encoding to an
-empty string: >
+For diff mode on a single file (with g:html_diff_one_file) a sequence of more
+than 3 filler lines is displayed as three lines with the middle line
+mentioning the total number of inserted lines.  If you prefer to see all the
+inserted lines as with the side-by-side diff, use: >
+    :let g:html_whole_filler = 1
+And to go back to displaying up to three lines again: >
+    :unlet g:html_whole_filler
+<
+TOhtml uses the current value of 'fileencoding' if set, or 'encoding' if not,
+to determine the charset and 'fileencoding' of the HTML file.  By default,
+this only works for encodings supported natively by Vim (mentioned
+specifically by name in |encoding-names|) which also appear in the IANA
+registry: http://www.iana.org/assignments/character-sets. Note that not all
+names in the IANA registry are actually widely supported, but TOhtml will
+choose them anyway unless you override with a different charset.
+
+To overrule all automatic charset detection, set g:html_use_encoding to the
+name of the charset to be used. TOhtml will try to determine the appropriate
+'fileencoding' setting from the charset, but you may need to set it manually
+if TOhtml cannot determine the encoding. It is probably a very good idea to
+set this variable to something widely supported, like UTF-8, for anything you
+will be serving up on a webserver: >
+   :let g:html_use_encoding = "UTF-8"
+You can also use this option to omit the line that specifies the charset
+entirely, by setting g:html_use_encoding to an empty string: >
    :let g:html_use_encoding = ""
 To go back to the automatic mechanism, delete the g:html_use_encoding
 variable: >
    :unlet g:html_use_encoding
@@ -485,5 +503,14 @@
    :let g:html_use_encoding = ""
 To go back to the automatic mechanism, delete the g:html_use_encoding
 variable: >
    :unlet g:html_use_encoding
+
+If you specify a charset with g:html_use_encoding for which TOhtml cannot
+automatically detect the corresponding 'fileencoding' setting, you can use
+g:html_encoding_override to allow TOhtml to detect the correct encoding.
+This is a dictionary of charset-encoding pairs that will replace existing
+pairs automatically detected by TOhtml, or supplement with new pairs. For
+example, to allow TOhtml to detect the HTML charset "windows-1252" properly as
+the encoding "8bit-cp1252", use: >
+   :let g:html_encoding_override = {'windows-1252': '8bit-cp1252'}
 <
@@ -489,13 +516,13 @@
 <
-For diff mode a sequence of more than 3 filler lines is displayed as three
-lines with the middle line mentioning the total number of inserted lines.  If
-you prefer to see all the inserted lines use: >
-    :let g:html_whole_filler = 1
-And to go back to displaying up to three lines again: >
-    :unlet g:html_whole_filler
-<
+The g:html_charset_override is similar, it allows TOhtml to detect the charset
+for any 'fileencoding' or 'encoding' which is not detected automatically. You
+can also use it to override specific existing encoding-charset pairs. For
+example, many user agents do not handle UTF-32 very well, so you could use the
+following to convert such documents to UTF-8 instead: >
+   :let g:html_charset_override = {'ucs-4': 'UTF-8'}
+
 					    *convert-to-XML* *convert-to-XHTML*
 An alternative is to have the script generate XHTML (XML compliant HTML).  To
 do this set the "html_use_xhtml" variable: >
     :let g:html_use_xhtml = 1
 
@@ -497,10 +524,10 @@
 					    *convert-to-XML* *convert-to-XHTML*
 An alternative is to have the script generate XHTML (XML compliant HTML).  To
 do this set the "html_use_xhtml" variable: >
     :let g:html_use_xhtml = 1
 
-Any of these options can be enabled or disabled by setting them explicitly to
-the desired value, or restored to their default by removing the variable using
-|:unlet|.
+Any of the on/off options can be enabled or disabled by setting them
+explicitly to the desired value, or restored to their default by removing the
+variable using |:unlet|.
 
 Remarks:
@@ -505,7 +532,4 @@
 
 Remarks:
-- This only works in a version with GUI support.  If the GUI is not actually
-  running (possible for X11) it still works, but not very well (the colors
-  may be wrong).
 - Some truly ancient browsers may not show the background colors.
 - From most browsers you can also print the file (in color)!
@@ -510,5 +534,8 @@
 - Some truly ancient browsers may not show the background colors.
 - From most browsers you can also print the file (in color)!
+- This version of TOhtml may work with older versions of Vim, but some
+  features such as conceal support will not function, and the colors may be
+  incorrect for an old Vim without GUI support compiled in.
 
 Here is an example how to run the script over all .c and .h files from a
 Unix shell: >
diff --git a/runtime/plugin/tohtml.vim b/runtime/plugin/tohtml.vim
--- a/runtime/plugin/tohtml.vim
+++ b/runtime/plugin/tohtml.vim
@@ -1,8 +1,8 @@
 " Vim plugin for converting a syntax highlighted file to HTML.
 " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
-" Last Change: 2010 Aug 12
+" Last Change: 2010 Sep 10
 "
 " The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
 " $VIMRUNTIME/syntax/2html.vim
 "
 " TODO:
@@ -4,8 +4,11 @@
 "
 " The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
 " $VIMRUNTIME/syntax/2html.vim
 "
 " TODO:
+"   * Allow user to specify more encoding/charset pairs to auto-detect, or
+"     override some of the built-in ones.
+"   * Increase number of supported charset-vim encoding pairs if possible
 "   * Restore open/closed folds and cursor position after processing each file
 "     with option not to restore for speed increase
 "   * Add extra meta info (generation time, etc.)
@@ -13,7 +16,33 @@
 "   * Implementation detail: add threshold for writing the lines to the html
 "     buffer before we're done (5000 or so lines should do it)
 "   * TODO comments for code cleanup scattered throughout
+"
+"
+" Changelog:
+"   7.3_v7b1 (this beta ): Remove use of setwinvar() function which cannot be
+"                          called in restricted mode (Andy Spencer). Use
+"                          'fencoding' instead of 'encoding' to determine by
+"                          charset, and make sure the 'fenc' of the generated
+"                          file matches its indicated charset. Add charsets for
+"                          all of Vim's natively supported encodings.
+"   7.3_v6 (0d3f0e3d289b): Really fix bug with 'nowrapscan', 'magic' and other
+"                          user settings interfering with diff mode generation,
+"                          trailing whitespace (e.g. line number column) when
+"                          using html_no_pre, and bugs when using
+"                          html_hover_unfold.
+"   7.3_v5 ( unreleased ): Fix bug with 'nowrapscan' and also with out-of-sync
+"                          folds in diff mode when first line was folded.
+"   7.3_v4 (7e008c174cc3): Bugfixes, especially for xhtml markup, and diff mode.
+"   7.3_v3 (a29075150aee): Refactor option handling and make html_use_css
+"                          default to true when not set to anything. Use strict
+"                          doctypes where possible. Rename use_xhtml option to
+"                          html_use_xhtml for consistency. Use .xhtml extension
+"                          when using this option. Add meta tag for settings.
+"   7.3_v2 (80229a724a11): Fix syntax highlighting in diff mode to use both the
+"                          diff colors and the normal syntax colors
+"   7.3_v1 (e7751177126b): Add conceal support and meta tags in output
+"   Pre-v1 baseline: Mercurial changeset 3c9324c0800e
 
 if exists('g:loaded_2html_plugin')
   finish
 endif
@@ -16,8 +45,8 @@
 
 if exists('g:loaded_2html_plugin')
   finish
 endif
-let g:loaded_2html_plugin = 'vim7.3_v6'
+let g:loaded_2html_plugin = 'vim7.3_v7b1'
 
 " Define the :TOhtml command when:
 " - 'compatible' is not set
diff --git a/runtime/syntax/2html.vim b/runtime/syntax/2html.vim
--- a/runtime/syntax/2html.vim
+++ b/runtime/syntax/2html.vim
@@ -1,6 +1,6 @@
 " Vim syntax support file
 " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
-" Last Change: 2010 Aug 12
+" Last Change: 2010 Sep 04
 "
 " Additional contributors:
 "
@@ -265,6 +265,19 @@
 let s:old_magic = &magic
 set magic
 
+" set the fileencoding to match the charset we'll be using
+let &l:fileencoding=s:settings.vim_encoding
+
+" According to http://www.w3.org/TR/html4/charset.html#doc-char-set, the byte
+" order mark is highly recommend on the web when using multibyte encodings. But,
+" it is not a good idea to include it on UTF-8 files. Otherwise, let Vim
+" determine when it is actually inserted.
+if s:settings.vim_encoding == 'utf-8'
+  setlocal nobomb
+else
+  setlocal bomb
+endif
+
 let s:lines = []
 
 if s:settings.use_xhtml
@@ -1071,6 +1084,7 @@
 let @/ = s:old_search
 let &more = s:old_more
 exe s:orgwin . "wincmd w"
+let &l:stl = s:origwin_stl
 let &l:et = s:old_et
 let &l:scrollbind = s:old_bind
 exe s:newwin . "wincmd w"
@@ -1074,6 +1088,7 @@
 let &l:et = s:old_et
 let &l:scrollbind = s:old_bind
 exe s:newwin . "wincmd w"
+let &l:stl = s:newwin_stl
 exec 'resize' s:old_winheight
 let &l:winfixheight = s:old_winfixheight
 
@@ -1077,8 +1092,6 @@
 exec 'resize' s:old_winheight
 let &l:winfixheight = s:old_winfixheight
 
-call setwinvar(s:orgwin,'&stl', s:origwin_stl)
-call setwinvar(s:newwin,'&stl', s:newwin_stl)
 let &ls=s:ls
 
 " Save a little bit of memory (worth doing?)
