[Python-Dev] PEP 263 -- Python Source Code Encoding

Jeff Epler jepler@unpythonic.dhs.org
Wed, 27 Feb 2002 11:10:59 -0600

On Wed, Feb 27, 2002 at 10:20:57AM +0100, M.-A. Lemburg wrote:
> So if we use the RE "coding[=:]\s*([\w-]+)" on the first line,
> we should be able to reach out for the encoding, right ?
> This RE would then cover both vim and emacs.

I've been informed on a #vim irc channel that "vim:fillencoding=blah:"
does not work.  Unfortunate.  I overlooked the part of the documentation
which states
    To read a file in a certain encoding it won't work by setting
    'fileencoding', use the |++enc| argument.

However, there's a "charset plugin" for vim:
which could be adapted to follow whatever convention is chosen for
Python.  However, this plugin is not standard in any version of
vim.  It's not clear what license it's under, but referencing it from
the PEP and documenting that something like
    au BufReadPost *.py ReloadWhenCharset(1, "coding[:=]\s([\w-]+)")
    au BufReadPost *.py ReloadWhenCharset(2, "coding[:=]\s([\w-]+)")
(search the first two lines for the emacs coding special marker) would
cause it to detect the charset of a Python file would certainly be
possible.  The plugin functions by executing a reload of the file with
++enc when ReloadWhenCharset matches its pattern.