Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of the text in the Common Lisp REPL

I'd like to control the color of the text displayed in Common Lisp. Something like this pseudo-code:

(print-color (:red "hello") (:blue "world"))

Is there any way this can be done? I use SBCL and my repl is inside emacs. Thanks!

like image 297
S4M Avatar asked Oct 06 '13 12:10

S4M


2 Answers

You can use ANSI escape code to print colorful texts:

(format t "~c[31mabc~c[0m~%" #\ESC #\ESC) ; this prints a red "abc" for most modern terminals

I'm not sure whether this works in slime, although.

like image 124
SaltyEgg Avatar answered Sep 25 '22 10:09

SaltyEgg


To enable ANSI color escape sequences, load the http://melpa.org/#/slime-repl-ansi-color package — but due to a bug, you may have to M-x slime-repl-ansi-color-mode RET in the REPL buffer. Distilled from various abandoned buggy versions, find the best and latest version at https://gitlab.com/augfab/slime-repl-ansi-color

slime-repl-ansi-color.el

(require 'ansi-color)
(require 'slime)

(define-minor-mode slime-repl-ansi-color-mode
  "Process ANSI colors in Lisp output."
  nil
  :lighter " SlimeANSI")

(define-slime-contrib slime-repl-ansi-color
  "Turn on ANSI colors in REPL output"
  (:authors "Max Mikhanosha")
  (:license "GPL")
  (:slime-dependencies slime-repl)
  (:on-load
   (add-hook 'slime-repl-mode-hook 'slime-repl-ansi-color-mode)))

(defadvice slime-repl-emit (around slime-repl-ansi-colorize activate compile)
  "Process ANSI colors in the Lisp output."
  (with-current-buffer (slime-output-buffer)
    (let ((start slime-output-start))
      (setq ad-return-value ad-do-it)
      (when slime-repl-ansi-color-mode
        (ansi-color-apply-on-region start slime-output-end)))))

(provide 'slime-repl-ansi-color)

In your .emacs init file, something like

(add-to-list 'slime-contribs 'slime-repl-ansi-color)

should enable the slime repl expression

(format t "~c[31mRed~:*~c[32mGreen~:*~c[34mBlue~:*~c[mPlain~%" (code-char 27))

to produce varicolored output. Try

(ql:quickload :cl-ansi-text)

(cl-ansi-text:with-color (:green :style :background)
  (cl-ansi-text:with-color (:yellow :effect :bright)
    (princ " Yellow on Green ")))

(princ (cl-ansi-text:green
        (cl-ansi-text:yellow " Yellow on Green " :effect :bright)
        :style :background))
like image 43
Devon Avatar answered Sep 22 '22 10:09

Devon