automated, scripts, shell

Moving between encodings with emacs

I live in Spain. I work at Academia. This is a problem.

My School’s computers are plagued with Windows (XP!), my pupils most certainly use Windows (I bet it is 7) and I use (obviously) an MBPro with Snow Leopard. Even more, my School’s servers are Linux. This gives rise to latin-1, UTF-8 collisions.

While writing some text-only documents for use with MATLAB (another problem, by the way) I realized I was going to need to convert them from my Mac’s UTF-8 to Windows latin-1 every time I edited them. Time for a makefile. I am not commenting this though, only the rather short elisp code I found here. Say you have a file named convert.el containing:

;; Read the file with no character code conversion.
;; Assume crlf represents end-of-line.
(let ((coding-system-for-read \'utf-8))
    (insert-file-contents "name_of_input_file.m"))
(let ((coding-system-for-write \'latin-1))
    (write-file "name_of_output_file.m"))

Running emacs in batch mode allows you to perform the conversion between UTF-8 and latin-1 automatically:

~$ emacs -batch -l convert.el

Will convert name_of_input_file.m from UTF-8 into name_of_output_file.m which will be in latin-1.

If you mix this with some Perl (or even with some more elisp, but I am not comfortable with the latter), you can achieve conversion enlightenment with a simple makefile. I did. (Which automatically implies I did not, or does it?).

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields