How do I display ANSI color codes in emacs for any mode?

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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.

回答1:

You could use code below

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

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)

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)))) 


回答2:

Gavenkoa's and Juanleon's solutions worked for me, but were not satisfying as they were modifying the contents of the file I was reading.

To colorize without modifying the contents of the file, download tty-format.el and add the following to your .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 is based on ansi-color.el which is only shipped natively with recent versions of emacs.



回答3:

User defined function:

(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)) 

For buffers that uses comint/compilation use 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)) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!