coding
elisp
emacs
encoding
gbk
utf8
]
在 簡體中文的操作系統 下,系統的編碼一般是GBK編碼,所以目錄名和文件名等都是用GBK編碼的,命令行窗口(cmd.exe或者cygwin)裡應該也是GBK編碼(Windows裡的別名是code page 936,如果不是936并且有亂碼,可以用chcp 936改回來並設為默認值)。所以emacs裡應該配置:
(setq file-name-coding-system ‘chinese-gbk-unix) (setq default-process-coding-system ’(chinese-gbk-unix . chinese-gbk-unix)) ;(DECODING . ENCODING) (modify-coding-system-alist ‘process “[sS][Hh][Ee][Ll][Ll]” ‘chinese-gbk-unix) (modify-coding-system-alist ‘process “[cC][mM][dD][pP][rR][oO][xX][yY]” ‘chinese-gbk-unix) (setq locale-coding-system ‘chinese-gbk-unix)
關於modify-coding-system-alist
,這裡摘錄部分文檔:
> > (modify-coding-system-alist TARGET-TYPE REGEXP CODING-SYSTEM) > > > > Modify one of look up tables for finding a coding system on I/O operation. There are three of such tables, `file-coding-system-alist`, `process-coding-system-alist`, and `network-coding-system-alist`. > > > > TARGET-TYPE specifies which of them to modify. If it is `file`, it affects `file-coding-system-alist` (which see). If it is `process`, it affects `process-coding-system-alist` (which see). If it is `network`, it affects `network-coding-system-alist` (which see). > > > > REGEXP is a regular expression matching a target of I/O operation. The target is a file name if TARGET-TYPE is `file`, a program name if TARGET-TYPE is `process`, or a network service name or a port number to connect to if TARGET-TYPE is `network`. > > > > CODING-SYSTEM is a coding system to perform code conversion on the I/O > > > > operation, or a cons cell (DECODING . ENCODING) specifying the coding systems for decoding and encoding respectively, or a function symbol which, when called, returns such a cons cell. > >
不過我又要 默認用UTF-8新建跟編輯文件,所以再配置下面兩句:
<span class="p">(</span><span class="nv">prefer-coding-system</span> <span class="ss">'utf-8-unix</span><span class="p">)</span>
<span class="p">(</span><span class="k">setq</span> <span class="nv">buffer-file-coding-system</span> <span class="ss">'utf-8-unix</span><span class="p">)</span>
關於prefer-coding-system
,這裡摘錄部分文檔:
(prefer-coding-system CODING-SYSTEM) > > > > Add CODING-SYSTEM at the front of the priority list for automatic detection. This also sets the following coding systems: * coding system of a newly created buffer * default coding system for subprocess I/O This also sets the following values: * default value used as `file-name-coding-system` for converting file names * default value for the command `set-terminal-coding-system` * default value for the command `set-keyboard-coding-system` > >
上面的設置,由於是GBK的系統環境,而 buffer-file-coding-system
卻是 utf-8-unix
, 所以在emacs裡面 M-x shell
的時候,如果顯示的內容裡有中文就會亂碼,這種情況用cygwin來代替emacs默認的cmdproxy.exe就不會亂碼,不過沒必要在任何情況下都用cygwin代替,比如執行交互命令(interactive)等等,所以這樣設置:
;;; cygwin bash (defun config-cygwin-bash () (require ‘cygwin-mount) (cygwin-mount-activate) (setq shell-file-name “C:/cygwin/bin/bash.exe”) (setenv “SHELL” shell-file-name) ;; -i tells bash to be interactive, which makes it load the ~/.bashrc file. (setq explicit-shell-args ’(”–login” “-i”)) (setq w32-quote-process-args ?”) (setq w32-quote-process-args t) (modify-coding-system-alist ‘process “[bB][aA][sS][hH]” ‘chinese-gbk-unix))
(defun reset-shell () “Set shell to `cmdproxy’.” (interactive) (setq shell-file-name “C:/Program Files/emacs/emacs-23.3/bin/cmdproxy.exe”) (setenv “SHELL” shell-file-name) (setq explicit-shell-args nil))
(defun cygwin () “Use cygwin bash shell.” (interactive) (config-cygwin-bash) (shell) (reset-shell))
所以在需要 M-x shell
的時候用 M-x cygwin
來代替就可以了,其它時候還是emacs默認的方式,這樣就不會有編碼的問題了。
NOTE1: 如果出現“stty: standard input: Inappropriate ioctl for device”,原因是:
> > Your shell startup file (.cshrc, .bashrc, .kshrc, whichever is appropriate for your shell) contains stty commands, which should only be done in interactive shells (in fact, probably only in the initial login shell, so they should probably be in .login or .profile). > >
NOTE2: 如果出現^[]~^G之類的,原因可能是PS1的設置造成的,可以在~/.bashrc裡重新export PS1為想要的格式,比如 export PS1="[[e[32m]u@h [e[33m]w[e[0m]]$ "
,看PS1的值可以在 cygwin 裡 echo $PS1
,關於PS1
在GBK編碼的系統下,在UTF-8編碼的.el文件裡,調用的shell命令如果含有非ascii字符(比如有中文),可以這樣,當然在emacs的編碼環境配置正確的情況下(比如上面像上面的配置)並不需要:
(let ((coding-system-for-read ‘chinese-gbk) (coding-system-for-write ‘chinese-gbk)) (shell-command …))