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

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

Re: changing encoding of buffer


From: Eli Zaretskii
Subject: Re: changing encoding of buffer
Date: Mon, 28 May 2007 23:21:34 +0300

> From: M G Berberich <berberic@forwiss.uni-passau.de>
> Date: Mon, 28 May 2007 12:34:47 +0200
> 
> When I reply, my newsreader appends my UTF-8 signature and starts
> emacs.

If you append UTF-8 text unconditionally, I think you are guilty no
less than MS: no one said that arbitrary encoded text can be freely
mixed with UTF-8.  Suppose those unnamed "MS-products" did announce
they produce text in windows-1252, how would that help you avoid the
problem?

> I wrote this function to solve the problem:
> 
> (defun fix-ms-posting ()
>   "Fixes newsposting that are garbled up by Microsoft-Software"
>   (interactive)
>   (let ((coding-system-for-write 'raw-text)
>       (coding-system-for-read 'utf-8)
>       (end (progn (end-of-buffer) (search-backward "\n-- \n"))))
>     (revert-buffer-with-coding-system 'utf-8)
>     (set-buffer-file-coding-system 'utf-8)
>     (shell-command-on-region (point-min) end 
>                            "recode windows-1252..utf-8" nil t)))

I see no need to call `recode': Emacs can do that itself.

> - Is there realy no other way to change the encoding of the buffer
>   than doing a revert? Can't this be done in-place?

No, it cannot be done in-place, because by the time you look at the
text in the buffer, it was already converted (a.k.a. "decoded") from
the external encoding on the disk file to the internal representation
Emacs uses in buffers and strings.  The original byte stream is gone,
vanished without a trace.

> - revert-buffer-with-coding-system always ask if it should do so, can
>   this be switched off?

Currently, the only practical way is to define a revert-buffer-function
that simply invokes revert-buffer with its NOCONFIRM arg non-nil.
(Don't forget to unbind revert-buffer-function before calling
revert-buffer, to avoid infinite recursion!)

> - I moved the search-backward to the variables list of let to make it
>   fail before harm is done if there is no signature. Is this the way
>   to do it?

Sorry, I don't understand what you want to do, and why is that a
problem.




reply via email to

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