Thursday, December 10, 2020

Re: Setting file encoding in a modeline

Albert Wik wrote:

> > > It seems to me that a modeline would be a convenient place to set the
> > > encoding used for a file. However, while it does set 'fenc'
> > > accordingly, the file is not loaded and displayed according to this
> > > setting.
> > >
> > > Bram said reading is tried with each encoding in fencs until one
> > > succeeds. Why not reload the file with the correct encoding once a
> > > modeline with a fenc setting has been read?
> >
> > The problem with this is, it is a catch 22. To be able to handle the
> > modeline, Vim must have already read correctly the buffer with the
> > correct encoding.
> >
> > So if it was wrong, the modeline could not be read correctly and
> > therefore this doesn't help.
>
> I see, but let's assume there is a "latin1" in 'fencs', and Vim reads
> the file as if it is in this encoding, and successfully decodes a
> modeline that says "fenc=cp437", and sets the 'fenc' accordingly, then
> why not also do a reload or just a "re-display" based on this setting?

These days using utf-8 is the standard way. If you still have a file
laying around in another encoding and you are OK editing it, it's easier
to convert it to utf-8 then to add a modeline.

It's also easy to get wrong: If 'fenc' is set in the modeline, one can
write the file in another encoding and mess it up.

If you really want to, you can make a BufReadPost autocommand that does
it for you. You don't even need a modeline then, anything you can
recognize the file by would do (e.g. path prefix or some text found in
the file).

--
A law to reduce crime states: "It is mandatory for a motorist with criminal
intentions to stop at the city limits and telephone the chief of police as he
is entering the town.
[real standing law in Washington, United States of America]

/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/202012101603.0BAG3uRg2228736%40masaka.moolenaar.net.

No comments: