ANSI & XTERM 256 color support
Emacs Lisp Perl
Switch branches/tags
Nothing to show
Pull request Compare This branch is even with atomontage:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
img
tests
README.org
xterm-color.el

README.org

https://img.shields.io/badge/license-BSD-blue.svg https://melpa.org/packages/xterm-color-badge.svg https://stable.melpa.org/packages/xterm-color-badge.svg

xterm-color.el is an ANSI control sequence to text-property translator

Features

  • Regular ANSI color support
  • XTERM 256 color support
  • Works with compilation-mode (experimental)
  • Works with eshell
  • Bonus: Faster than ansi-color.el

Usage

Call xterm-color-filter to propertize strings that you can then insert into a buffer. All state is kept in buffer-local variables which means that control sequences can span xterm-color-filter call boundaries.

Example:

(xterm-color-filter "�[0;1;3;4")
(xterm-color-filter ";35")
(xterm-color-filter ";mThis is only a test")
(xterm-color-filter "�[0m")

You may also use xterm-color-colorize-buffer, interactively or from elisp, to colorize an entire buffer.

If you are inserting into a buffer that has activated font locking, you NO LONGER need to set font-lock-unfontify-region-function to xterm-color-unfontify-region as the latter has been removed.

You can replace ansi-color.el with xterm-color for all comint buffers:

;; comint install
(progn (add-hook 'comint-preoutput-filter-functions 'xterm-color-filter)
       (setq comint-output-filter-functions (remove 'ansi-color-process-output comint-output-filter-functions)))

;; comint uninstall
(progn (remove-hook 'comint-preoutput-filter-functions 'xterm-color-filter)
       (add-to-list 'comint-output-filter-functions 'ansi-color-process-output))

;; For M-x shell, also set TERM accordingly (xterm-256color)

;; You can also use it with eshell (and thus get color output from system ls):

(require 'eshell)

(add-hook 'eshell-mode-hook
          (lambda ()
            (setq xterm-color-preserve-properties t)))

(add-to-list 'eshell-preoutput-filter-functions 'xterm-color-filter)
(setq eshell-output-filter-functions (remove 'eshell-handle-ansi-color eshell-output-filter-functions))

;;  Don't forget to setenv TERM xterm-256color

You may use `compilation-shell-minor-mode’ with comint-mode buffers (e.g. M-x shell) and the comint configuration previously described.

For standalone compilation buffers use the following configuration:

(setq compilation-environment '("TERM=xterm-256color"))

(add-hook 'compilation-start-hook
          (lambda (proc)
            ;; We need to differentiate between compilation-mode buffers
            ;; and running as part of comint (which at this point we assume
            ;; has been configured separately for xterm-color)
            (when (eq (process-filter proc) 'compilation-filter)
              ;; This is a process associated with a compilation-mode buffer.
              ;; We may call `xterm-color-filter' before its own filter function.
              (set-process-filter
               proc
               (lambda (proc string)
                 (funcall 'compilation-filter proc
                          (xterm-color-filter string)))))))

Standalone compilation-mode buffer support is experimental, report any issues that may arise.

Test

M-x xterm-color-test

For comint or eshell:

M-x shell || M-x eshell

perl tests/xterm-colortest && perl tests/256colors2.pl

img/xterm-thumb.png img/godwars2-thumb.png

License

The code comes with a BSD-style license so you can basically do with it whatever you want.

Author

xristos (AT) sdf (DOT) lonestar (DOT) org