galilette |
2004-04-26 22:56 |
发信人: hhuu (归来,从某个地方), 信区: Linux 标 题: Emacs的日常生活 9 发信站: BBS 水木清华站 (Mon May 26 15:44:12 2003), 转信
*8 支持中文GBK
何曾几时,中文支持是Emacs的强项。在Emacs里面开一个shell上水 木几乎是我唯一的选择。 时代变了,Emacs的中文支持就渐渐的落后了。
Emacs内部有一套表示多国的方法,就是所谓的emacs-mule。我们能够 在同一个emacs buffer里面能够同时看中日韩文字,能够同时看到阿拉伯 文,能够同时看到德文,丹麦文;这都拜emacs-mule所赐。不幸的是 emacs-mule并没有成为事实上的编码标准。emacs-mule除了emacs自己能够 认识以外,其他的编辑器都不支持。所以Emacs必须在和其他文本处理器交 互的时候重新编码内部的emacs-mule。这里没有必要谈太多的细节, Emacs是一个self-document的编辑器,上面这些细节都可以在 coding.[ch]和charset.[ch]中找到。 为了能和键盘交互(可以认为键盘是一个文本处理器,Emacs从键盘中 读入文本),Emacs将从键盘中读入的文本“解码”为emacs-mule(我们这 里说到编码解码,都是从Emacs的角度来看),为了文件能被其他的文本编 辑器打开,比如vi,Emacs在存盘的时候将emacs-mule编码为 chinese-iso-8bit。这就是我们平常用到的各种coding system起到的作用。
为了让Emacs支持gbk,我们需要做的,就是让所有的gbk编码字符,都 能够在emacs-mule中找到自己的座位。虽然实际上emacs-mule里面所有的 座位都已经被人坐满了,我们还是可以假设那些很少有人出现的座位依然 是空的。前面给出的chinese-gbk就强占了cns11643-5, 6, 7的座位。这些 座位的汉字几乎不可能出现在我们这些人的屏幕上,所以这种做法基本上 是可行的。所以如果有一天,你在使用chinese-gbk的时候,又试图用 cns11643的编码来保存,还请你回到这里来想想可能会发生的事情。
因为Emacs已经开始支持unicode了,所以让utf-8或者utf-16编码的 gbk汉字的文件在Emacs中显示并不是麻烦。而且Emacs已经在这里预留了 hook,只需要给一个translate-table,那就一切ok了。
让Emacs支持从X拷贝过来的gbk汉字,很难直接在lisp代码中实现。因 为X和emacs一样是个历史悠久的软件,所以它同样也有一套自己的多字节 编码格式。在Emacs中,缺省是采用compound-text-with-extension来处理 这种编码格式的。从作者当初开发的思路来看,我们让它支持gbk编码,只 需要添加一项("GBK-0" . chinese-gbk)到 non-standard-icccm-encodings-alist中去,就可以简单的扩展支持gbk编 码了。然而,因为实现上bug,和X本身的问题,这终究只是一个美好的愿 望。如果你坚持的话,可以尝试一下这个补丁。注意这个时候就不能再使 用compound-text-with-extensions作为selection的coding system了,而 应该用chinese-gbk。
--- /home/huxw/src/Resp/emacs/src/xselect.c 2003-04-07 04:35:06.000000000 +0800 +++ xselect.c 2003-05-26 11:17:42.966829744 +0800 @@ -1496,6 +1496,11 @@ Lisp_Object target_type; /* for error messages only */ Atom selection_atom; /* for error messages only */ { + // by huxw start here + XTextProperty text_prop; + char** local_list; + int local_number = 0; + // by huxw end here Atom actual_type; int actual_format; unsigned long actual_size; @@ -1554,12 +1559,70 @@ /* It's been read. Now convert it to a lisp object in some semi-rational manner. */ + //by huxw start here + if (XSupportsLocale()) { + int local_status; + + text_prop.value = (char*)data; + text_prop.encoding = actual_type; + text_prop.format = actual_format; + text_prop.nitems = actual_size; + + local_status = XmbTextPropertyToTextList(display, &text_prop, &local_list, &local_number); + if (local_status < Success || !local_number || !*local_list ) { + } else { + xfree((char*)data); + data = strdup(*local_list); + XFreeStringList(local_list); + } + } else { + } + //by huxw end here + +#if 0 val = selection_data_to_lisp_data (display, data, bytes, actual_type, actual_format); +#else + val = selection_data_to_lisp_data (display, data, strlen(data), actual_type, actual_format); +#endif /* Use xfree, not XFree, because x_get_window_property calls xmalloc itself. */ - xfree ((char *) data); + + // by huxw start here +// xfree ((char *) data); + if (local_number == 0) { // Xmb is not used to not successed + xfree((char*)data); + } else { + free(data); + } + // by huxw end here return val; }
--
C programmers think memory management is too important to be left to the computer. Lisp programmers think memory management is too important to be left to the user.
※ 来源:·BBS 水木清华站 smth.org·[FROM: 210.74.191.34]
|
|