help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

configuring coding systems for loading/saving


From: David Madore
Subject: configuring coding systems for loading/saving
Date: Tue, 20 Apr 2010 14:16:22 +0000 (UTC)

Hi,

I'm trying to configure the way Emacs tries various encoding systems
when loading and saving a file, and I find myself unable to do what I
want.

Basically, the problem I have is that if I load a pure ASCII file,
insert a non-ASCII Unicode character in it, and then save it, Emacs
saves it in UTF-8 without asking for any kind of confirmation.
Generally speaking, that's not what I want: if the file was pure ASCII
to start with, I probably want it to stay that way, so I'd like Emacs
to ask for confirmation before trying any other encoding.

I tried doing (prefer-coding-system 'us-ascii), but this messes up
with the way encoding is detected on loading (which was fine with me
by default): e.g., if I load an ISO-8859-1 encoded file that encoding
is not correctly detected if I do (prefer-coding-system 'us-ascii)
whereas it is if I don't.

I also tried this:

(setq select-safe-coding-system-accept-default-p
      '(lambda (coding) (string= coding buffer-file-coding-system)))

which I think should do more or less what I want (accept an encoding
as safe only if it matches exactly the buffer-file-coding-system), but
it fails for a stupid reason: coding is typically something like
"iso-8859-1" whereas buffer-file-coding-system is typically something
like "iso-8859-1-unix" - and I don't know how to test more
intelligently than with string-equal.  Also, I'm afraid overriding
select-safe-coding-system-accept-default-p could have unpleasant
effects (e.g., if some Emacs application wishes to suggest that the
previously chosen file encoding is inadequate).

Any other ideas?

Ideally, I'd like Emacs to behave as follows:

* when loading a file in the absence of an explicit encoding argument,
  try the following encodings: us-ascii (always first), then whatever
  encodings are preferred by the locale (e.g., iso-8859-2 if I happen
  to have an iso-8859-2 locale), then utf-8, and lastly iso-8859-1 (in
  that order);

* when saving the file in the absence of an explicit encoding
  argument, always try to save it in the buffer's file coding system,
  and ask for user input if this fails.

(In that way, loading an ASCII file and adding a non-ASCII character
to it will cause for confirmation when saving.)

Is this achievable?  If not, what is the closest I can do?

Many thanks for any help,

-- 
     David A. Madore
   ( http://www.madore.org/~david/ )


reply via email to

[Prev in Thread] Current Thread [Next in Thread]