Friday, November 11, 2011

Re: Setting encoding for a type of file

On 11/11/11 04:52, Ben Fritz wrote:
>
>
> On Nov 10, 3:26 pm, Marcio Gil<marciom...@bol.com.br> wrote:
>> On Nov 10, 6:09 pm, Christian Brabandt<cbli...@256bit.org> wrote:
>>
>>> autocmd FileType dosbatch :e! ++enc=cp850
>>
>> works, but put the syntax highlight off
>>
>> On Nov 10, 6:16 pm, Tony Mechelynck<antoine.mechely...@gmail.com>
>> wrote:
>>
>>> au BufReadPre,BufNewFile *.bat,*.btm,*.sys setlocal fenc=cp850
>>
>> don't works.
>>
>> On Nov 10, 6:29 pm, Ben Fritz<fritzophre...@gmail.com> wrote:
>>
>>> autocmd FileType dosbatch e ++enc=cp850
>>
>> Same as Christian Brabandt's: works, but put the syntax highlight off
>>
>>
>
> Oops, forgot the "nested" keyword. Try:
> autocmd FileType dosbatch nested e ++enc=cp850
>
> See :help autocmd-nested
>
>>
>>
>>
>>> I actually have something a bit more complex (I've removed some
>>> irrelevant stuff for your immediate problem, if some of this is
>>> confusing as-is). I use a different method, by changing
>>> 'fileencodings' prior to loading the file, so that Vim automatically
>>> detects my desired fileencoding:
>>
>>> " Don't detect utf-8 without a BOM by default, I don't use UTF-8
>>> normally
>>> " and any files in latin1 will detect as UTF. Detect cp1252 rather
>>> than
>>> " latin1 so files are read in correctly. Fall back to latin1 if
>>> system does
>>> " not support cp1252 for some reason.
>>> exec 'set fileencodings=ucs-bom,'.s:windows_enc.',latin1'
>>> if has('autocmd')
>>> augroup fenc_detect
>>> au!
>>
>>> " batch files need to use the encoding of the cmd.exe prompt in
>>> Windows
>>> if has('win32') || has('win64')
>>> " get the cmd.exe encoding by asking for it
>>> let g:batcp = substitute(system('chcp'), '^\c\s*Active code
>>> page: \(\d\+\)\s*[^[:print:]]*$', 'cp\1', '')
>>> if g:batcp =~? '^cp\d\+$'
>>> autocmd BufReadPre *.bat exec 'set fileencodings='.g:batcp
>>> autocmd BufNewFile *.bat exec 'setlocal
>>> fileencoding='.g:batcp
>>> endif
>>> endif
>>> " restore default fileencodings after loading the files that use
>>> a special
>>> " value to force specific encodings
>>> exec 'autocmd BufReadPost *.bat set fileencodings=ucs-
>>> bom,'.s:windows_enc.',latin1'
>>> augroup END
>>> endif
>>
>> This works only for DOS batch files, other files are also opened in
>> cp850.
>>
>> But in the Cygwin vim don't recognizes the s:windows_enc variable, I
>> will substitute this for 'cp850'
>>
>
> Oops, that's an artifact of my using the same .vimrc on Unix and
> Windows. I do something like this, before the code snippet:
>
> if has('unix')
> let s:windows_enc = '8bit-cp1252'
> else " windows
> let s:windows_enc = 'cp1252'
> endif

You can also use the value 'Windows-1252' (the official name) which is
known by iconv and so should work both on Unix Vim statically linked
with +iconv and on Windows Vim dynamically linked with +iconv/dyn if
iconv.dll or libiconv.dll can be found.

>
> s:windows_enc doesn't exist by default. Setting it to cp850 means ALL
> files will be detected with this encoding, if they don't have a BOM.
>
>> This works for me:
>>
>> exec 'autocmd BufReadPre *.bat set fileencodings=ucs-bom,cp850,latin1'
>>
>
> As Tony says, this will set ALL files to cp850, unless they have a
> BOM.
>
> The point of my script snippet was:
>
> 1. For most files, use ucs-bom to use a Unicode encoding if the file
> has a BOM, then try windows-1252, but if the system doesn't recognize
> windows-1252, try latin1 (I actually have more autocmds to check for
> characters specific to windows-1252 and use latin1 if not present).
> 2. For *.bat files only, override this to ONLY try the cmd.exe
> encoding
> 3. Restore option (1) after loading dos files, since the option is
> global and the fenc is already set appropriately
>

Best regards,
Tony.
--
Mencken and Nathan's Fifteenth Law of The Average American:
The worst actress in the company is always the manager's wife.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: