[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: single function to open utf-16 file?
From: |
Kevin Rodgers |
Subject: |
Re: single function to open utf-16 file? |
Date: |
Mon, 10 Jul 2006 16:16:34 -0600 |
User-agent: |
Thunderbird 1.5.0.4 (Windows/20060516) |
tom.tulinsky@gmail.com wrote:
can tell me how to easily open a utf-16 file, or a function for me that
will open a utf-16 file in PC-win32 Emacs 21.3.1? Currently I have to
do
'C-x C-m c utf-16-le RET' followed by C-x C-f
I have tried to write a function or create a keyboard macro to do this,
but they did not work because
C-x C-m c (universal-coding-system-argument)
is some kind of special function and demands the next command be
entered from the keyboard.
I want a single elisp function that I can bind to a key.
Do the files begin with a BOM? If so, something like this should work:
(push '("\\`\xEF\xBB\xBF" . utf-8) auto-coding-regexp-alist)
(push '("\\`\xFE\xFF" . utf-16-be) auto-coding-regexp-alist)
(push '("\\`\xFF\xFE" . utf-16-le) auto-coding-regexp-alist)
Are all your files UTF-16? If so, this should work:
(prefer-coding-system 'utf-16-le)
If only some of your files are UTF-16 but they can be recognized by
their directory or file name, you can frob the file-coding-system-alist
variable (see the modify-coding-system-alist function).
If you only know for sure that the file is UTF-16 after you visit it
and see it displayed, you can immediately correct that with
`C-x <RET> r utf-16-le <RET>', which is suitable for a keyboard macro.
As a last resort, something like this (cribbed from
universal-coding-system-argument) might work:
(defun find-file-utf-16-le (&rest find-file-args)
"Temporarily bind coding systems to `utf-16-le' before calling
`find-file'."
(interactive)
(if (interactive-p)
(let ((prefix-arg current-prefix-arg)
(coding-system-for-read coding-system)
(coding-system-for-write coding-system)
(coding-system-require-warning t))
(call-interactively 'find-file))
(apply 'find-file find-file-args)))
--
Kevin