【发布时间】:2011-09-08 12:08:39
【问题描述】:
我在 Emacs (GNU Emacs 22.2.1 / Debian GNU Linux) 下使用 hexl-mode 时遇到了一些奇怪的问题。
我有一个 UTF8 文本文件,我想向其附加 BOM(字节顺序掩码:即使不建议将无意义的 BOM 附加到 UTF8 文件,规范明确指定 UTF8 文件中的 BOM 是合法的)。
file 命令查看文件的方式如下:
...$ file /tmp/test.txt
/tmp/test.txt: UTF-8 Unicode English text
以下作品:
open the UTF8 file (without BOM) in text mode
add three ASCII characters at the beginning of the file
close the file (<-- see, very important, I need to close the file)
M-x hexl-mode
M-x hexl-find-file (re-opening the file but this time in hexl-mode)
M-x hexl-insert-hex-string
EFBBBF
C-x C-s (saving the file)
M-x hexl-mode-exit
然后我得到一个带有 BOM 的 UTF-8 文件,如 file 命令所示:
...$ file /tmp/test.txt
/tmp/test.txt: UTF-8 Unicode (with BOM) English text
(请注意,文件命令以启发式方式将其检测为带有 BOM“英文文本”的 UTF-8,但该文件确实包含很多欧元符号:我的意思是,在添加 BOM 之前,它不是一个 ASCII 文件但已经是一个 UTF-8 文件,如上所示)
但是我根本无法在 Emacs 下打开文件 first 然后调用 hexl-mode 然后尝试用 0xEB 0xFF 0xBF (BOM)替换前三个字符,然后保存。
显然,从 (Text) 切换到 (Hexl) 模式时会出现疯狂的转换问题。
我是否遗漏了一些明显的东西,或者与 Text / Hexl 之间的转换有点损坏,我最好先切换到 hexl-mode,进行十六进制编辑,然后保存并关闭文件并以文本模式重新打开?
【问题讨论】: