如何在emacs中为任何模式显示ANSI颜色代码?

时间:2014-04-30 01:44:52

标签: emacs ansi

我有一个日志文件,它使用ANSI转义颜色代码来格式化文本。模式为fundamental。还有其他已回答的问题可以解决此问题,但我不确定如何将其应用于此模式或任何其他模式。我知道解决方案与以某种方式配置ansi-color有关。

4 个答案:

答案 0 :(得分:42)

您可以使用以下代码

(require 'ansi-color)
(defun display-ansi-colors ()
  (interactive)
  (ansi-color-apply-on-region (point-min) (point-max)))

然后你可以通过M-x执行display-ansi-colors,通过你选择的键绑定,或者通过一些程序条件(也许你的日志文件有一个与一些正则表达式匹配的扩展名或名称)

如果要使用只读缓冲区(日志文件,grep结果)执行此操作,可以使用inhibit-read-only,因此函数将为:

(defun display-ansi-colors ()
  (interactive)
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region (point-min) (point-max))))

答案 1 :(得分:6)

Gavenkoa和Juanleon的解决方案对我有用,但不满意,因为他们正在修改我正在阅读的文件的内容。

要着色而不修改文件内容,请下载tty-format.el并将以下内容添加到.emacs中:

(add-to-list 'load-path "path/to/your/tty-format.el/")

(require 'tty-format)

;; M-x display-ansi-colors to explicitly decode ANSI color escape sequences                                                                                                                                        
(defun display-ansi-colors ()
  (interactive)
  (format-decode-buffer 'ansi-colors))

;; decode ANSI color escape sequences for *.txt or README files                                                                                                                                                    
(add-hook 'find-file-hooks 'tty-format-guess)

;; decode ANSI color escape sequences for .log files                                                                                                                                                               
(add-to-list 'auto-mode-alist '("\\.log\\'" . display-ansi-colors))

tty-format基于ansi-color.el,它仅与最新版本的emacs一起本地发送。

答案 2 :(得分:3)

用户定义的功能:

(defun my-ansi-color (&optional beg end)
  "Interpret ANSI color esacape sequence by colorifying cotent.
Operate on selected region on whole buffer."
  (interactive
   (if (use-region-p)
       (list (region-beginning) (region-end))
     (list (point-min) (point-max))))
  (ansi-color-apply-on-region beg end))

对于使用comint / compilation的缓冲区,请使用filter:

(ignore-errors
  (require 'ansi-color)
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))

答案 3 :(得分:1)

在大文件上,ansi-color-apply-on-region的性能很慢。这是为当前区域着色并与只读缓冲区配合使用的解决方案。

(require 'ansi-color)
(defun ansi-color-region ()
  "Color the ANSI escape sequences in the acitve region.
Sequences start with an escape \033 (typically shown as \"^[\")
and end with \"m\", e.g. this is two sequences
  ^[[46;1mTEXT^[[0m
where the first sequence says to diplay TEXT as bold with
a cyan background and the second sequence turns it off.

This strips the ANSI escape sequences and if the buffer is saved,
the sequences will be lost."
  (interactive)
  (if (not (region-active-p))
      (message "ansi-color-region: region is not active"))
  (if buffer-read-only
      ;; read-only buffers may be pointing a read-only file system, so don't mark the buffer as
      ;; modified. If the buffer where to become modified, a warning will be generated when emacs
      ;; tries to autosave.
      (let ((inhibit-read-only t)
            (modified (buffer-modified-p)))
        (ansi-color-apply-on-region (region-beginning) (region-end))
        (set-buffer-modified-p modified))
    (ansi-color-apply-on-region (region-beginning) (region-end))))

一个缺点是ansi-color-apply-on-region从缓冲区中删除了ANSI转义序列字符,因此在保存时,它们会丢失。我想知道是否有一种隐藏字符而不是剥离字符的方法?