如何在任何模式下在 emacs 中显示 ANSI 颜色代码? [英] How do I display ANSI color codes in emacs for any mode?
问题描述
我有一个使用 ANSI 转义颜色代码来格式化文本的日志文件.模式是fundamental
.还有其他已回答的问题可以解决此问题,但我不确定如何将其应用于此模式或任何其他模式.我知道该解决方案与以某种方式配置 ansi-color
有关.
I have a log file that uses ANSI escape color codes to format the text. The mode is fundamental
. There are other answered questions that address this issue but I'm not sure how to apply it to this mode or any other mode. I know the solution has something to do with configuring ansi-color
in some way.
推荐答案
你可以使用下面的代码
(require 'ansi-color)
(defun display-ansi-colors ()
(interactive)
(ansi-color-apply-on-region (point-min) (point-max)))
然后您可以通过 Mx、通过您选择的键绑定或通过某些编程条件(也许您的日志文件具有与某些正则表达式匹配的扩展名或名称)执行 display-ansi-colors
)
Then you can execute display-ansi-colors
via M-x, via a key-binding of your choosing, or via some programmatic condition (maybe your log files have a extension or name that matches some regexp)
如果你想用只读缓冲区(日志文件、grep 结果)来做这件事,你可以使用inhibit-read-only
,所以函数将是:
If you want to do this with read-only buffers (log files, grep results), you may use inhibit-read-only
, so the function will be:
(defun display-ansi-colors ()
(interactive)
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max))))
这篇关于如何在任何模式下在 emacs 中显示 ANSI 颜色代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!