" Enable UTF-8 support, but use locale encoding for new files. {{{
" Save locale encoding for future use
let s:locale_encoding = &encoding
" This will silenty fail for GTK2 GUI, which is the correct thing to do.
let &termencoding = s:locale_encoding
let &encoding = 'utf-8'
" How does this work when opening an existing file:
" - First Vim tries the 'ucs-bom' pseudo-encoding - it checks if the file
" starts with an Unicode Byte Order Mark (BOM); if a BOM is found, Vim
" selects 'fileencoding' according to its size and byte order.
" - If a BOM was not found, Vim tries to use the 'ascii' encoding - this will
" succeed if the file contains only ASCII characters. The BufReadPost
" handler below will catch this case and replace 'ascii' with the locale
" encoding (to make sure that the file can be saved successfully even if
" some non-ASCII characters have been inserted during editing).
" - If the file contains non-ASCII characters, Vim tries to decode it as
" UTF-8; if this succeeds, Vim assumes that the file is really in UTF-8.
" - If the file does not contain valid UTF-8, assume that it is in the locale
" encoding.
let &fileencodings = 'ucs-bom,ascii,utf-8,' . s:locale_encoding
" Use locale encoding for new files.
au BufNewFile *
\ let &l:fileencoding = s:locale_encoding
" Use locale encoding for pure ASCII files.
au BufReadPost *
\ if &l:fileencoding == 'ascii' |
\ let &l:fileencoding = s:locale_encoding |
\ endif
" }}}