如何设置 shell-command-on-region 输出的编码?
我有一个小的 elisp 脚本,它将 Perl::Tidy 应用于区域或整个文件。作为参考,这里是脚本(借自 EmacsWiki):
(defun perltidy-command(start end)
"The perltidy command we pass markers to."
(shell-command-on-region start
end
"perltidy"
t
t
(get-buffer-create "*Perltidy Output*")))
(defun perltidy-dwim (arg)
"Perltidy a region of the entire buffer"
(interactive "P")
(let ((point (point)) (start) (end))
(if (and mark-active transient-mark-mode)
(setq start (region-beginning)
end (region-end))
(setq start (point-min)
end (point-max)))
(perltidy-command start end)
(goto-char point)))
(global-set-key "\C-ct" 'perltidy-dwim)
我正在使用当前的 Windows 版 Emacs 23.1 (EmacsW32)。我遇到的问题是,如果我将该脚本应用于 UTF-8 编码文件(状态栏中的“U(Unix)”),输出将返回 Latin-1 编码,即每个非字符有两个或更多字符ASCII 源字符。
我有什么办法可以解决这个问题吗?
编辑:问题似乎可以通过在我的 init.el
中使用 (set-terminal-coding-system 'utf-8-unix)
来解决。如果有人有其他解决方案,请继续写下去!
I have a small elisp script which applies Perl::Tidy on region or whole file. For reference, here's the script (borrowed from EmacsWiki):
(defun perltidy-command(start end)
"The perltidy command we pass markers to."
(shell-command-on-region start
end
"perltidy"
t
t
(get-buffer-create "*Perltidy Output*")))
(defun perltidy-dwim (arg)
"Perltidy a region of the entire buffer"
(interactive "P")
(let ((point (point)) (start) (end))
(if (and mark-active transient-mark-mode)
(setq start (region-beginning)
end (region-end))
(setq start (point-min)
end (point-max)))
(perltidy-command start end)
(goto-char point)))
(global-set-key "\C-ct" 'perltidy-dwim)
I'm using current Emacs 23.1 for Windows (EmacsW32). The problem I'm having is that if I apply that script on a UTF-8 coded file ("U(Unix)" in the status bar) the output comes back Latin-1 coded, i.e. two or more characters for each non-ASCII source character.
Is there any way I can fix that?
EDIT: Problem seems to be solved by using (set-terminal-coding-system 'utf-8-unix)
in my init.el
. In anyone has other solutions, go ahead and write them!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下来自
shell-command-on-region
文档执行过程中,首先从
process-coding-system-alist
中查找编码系统,如果为nil,则查找来自默认进程编码系统
。如果您想更改编码,您可以将转换选项添加到
process-coding-system-alist
,以下是其内容。或者,如果您没有设置
process-coding-system-alist
,则它为零,您可以将编码选项分配给default-process-coding-system
,例如:(
如果输入编码为
utf-8
,则输出编码为utf-8
)或者
我还写了一个帖子 如果您想了解详细信息。
Below are from
shell-command-on-region
documentDuring executing, it looks for coding system from
process-coding-system-alist
at first, if it's nil, then looks fromdefault-process-coding-system
.If your want to change the encoding, you can add your converting option to
process-coding-system-alist
, below are the content of it.Or, if you didn't set
process-coding-system-alist
, it's nil, you could assign your encoding option todefault-process-coding-system
,for example:
(If input is encoded as
utf-8
, then output encoded asutf-8
)Or
I also wrote a post about this if you want details.
引用
shell-command-on-region
的文档 (Ch f shell-command-on-region RET
):换句话说,你会做这样的事情
This is untested,不确定
coding-system-for-read
的值是什么(或者可能是-write
?或者作为好吧?)应该适合你的情况。我想您还可以利用 OUTPUT-BUFFER 参数并将输出定向到一个缓冲区,该缓冲区的编码系统设置为您需要的值。另一种选择可能是在 perltidy 调用中调整区域设置,但同样,由于没有有关您现在使用的内容的更多信息,并且无法在与您类似的系统上进行实验,我只能暗示。
Quoting the documentation for
shell-command-on-region
(C-h f shell-command-on-region RET
):In other words, you'd do something like
This is untested, not sure what the value of
coding-system-for-read
(or perhaps-write
instead? or as well?) should be in your case. I guess you could also utilize the OUTPUT-BUFFER argument and direct the output to a buffer whose coding system is set to what you need it to be.Another option might be to wiggle the locale in the perltidy invocation, but again, without more information about what you are using now, and no means to experiment on a system similar to yours, I can only hint.