Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Emacs to use a particular encoding if and only if that causes no trouble

Tags:

emacs

encoding

In my .emacs file, I use the line

'(setq coding-system-for-write 'iso-8859-1-unix)

to have Emacs save files in the iso-8859-1-unix encoding. When I enter characters that cannot be encoded that way ("Łódź" for example), I get prompted to select a different encoding, but upon entering `iso-8859-1-unix' into the minibuffer, the file is saved and the offending characters are lost.

If I just hit enter at the prompt, the file is saved in Unicode, and when I close and reopen Emacs it is interpreted as a Unicode file again. If I then remove the offending characters, save the file and close and reopen Emacs another time, it is still interpreted as a Unicode file -- from which I deduce that it has still been saved in Unicode, even though saving in iso-8859-1-unix would have been possible.

So is there a way to force Emacs to write a file in iso-8859-1 whenever possible, and never save it in that encoding if doing so would gobble characters?

Many thanks in advance,

Thure Dührsen

like image 698
Thure Dührsen Avatar asked May 16 '11 18:05

Thure Dührsen


1 Answers

As per the doc string for coding-system-for-write, you should not be setting it globally.

Perhaps what you are looking for is (prefer-coding-system 'iso-8859-1-unix)?

like image 116
tripleee Avatar answered Sep 21 '22 23:09

tripleee