all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* configuring coding systems for loading/saving
@ 2010-04-20 14:16 David Madore
  2010-05-06 23:23 ` Kevin Rodgers
  0 siblings, 1 reply; 3+ messages in thread
From: David Madore @ 2010-04-20 14:16 UTC (permalink / raw
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: configuring coding systems for loading/saving
  2010-04-20 14:16 configuring coding systems for loading/saving David Madore
@ 2010-05-06 23:23 ` Kevin Rodgers
  2010-05-07  8:49   ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Rodgers @ 2010-05-06 23:23 UTC (permalink / raw
  To: help-gnu-emacs

David Madore wrote:
> 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.

How about:

(equal (coding-system-get coding 'mime-charset)
        (coding-system-get buffer-file-coding-system 'mime-charset)

or

(equal (coding-system-change-eol-conversion coding 'unix)
        (coding-system-change-eol-conversion buffer-file-coding-system 'unix))

Can someone explain why

(coding-system-change-eol-conversion 'iso-8859-1 nil)
⇒ iso-8859-1

(coding-system-change-eol-conversion 'iso-8859-1-unix nil)
⇒ iso-latin-1

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

-- 
Kevin Rodgers
Denver, Colorado, USA





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: configuring coding systems for loading/saving
  2010-05-06 23:23 ` Kevin Rodgers
@ 2010-05-07  8:49   ` Eli Zaretskii
  0 siblings, 0 replies; 3+ messages in thread
From: Eli Zaretskii @ 2010-05-07  8:49 UTC (permalink / raw
  To: help-gnu-emacs

> From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
> Date: Thu, 06 May 2010 17:23:21 -0600
> 
> David Madore wrote:
> > 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.
> 
> How about:
> 
> (equal (coding-system-get coding 'mime-charset)
>         (coding-system-get buffer-file-coding-system 'mime-charset)
> 
> or
> 
> (equal (coding-system-change-eol-conversion coding 'unix)
>         (coding-system-change-eol-conversion buffer-file-coding-system 'unix))

I think this is better:

  (coding-system-equal coding
  		       (coding-system-base buffer-file-coding-system))

This is better, because it will also handle aliases to coding systems,
such as `latin-1'.

> Can someone explain why
> 
> (coding-system-change-eol-conversion 'iso-8859-1 nil)
> ⇒ iso-8859-1
> 
> (coding-system-change-eol-conversion 'iso-8859-1-unix nil)
> ⇒ iso-latin-1

What did you expect it to do?  The doc string says

    If EOL-TYPE is nil, the returned coding system detects
    how end-of-line is formatted automatically while decoding.

and that's what you are getting.

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

To the OP: I suggest to try using the overriden
select-safe-coding-system-accept-default-p and see if you have any
trouble.  If you do, please report them, as they are most probably
bugs.





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-05-07  8:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-20 14:16 configuring coding systems for loading/saving David Madore
2010-05-06 23:23 ` Kevin Rodgers
2010-05-07  8:49   ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.