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!
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!
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.
*inferior-lisp*
buffer, which is in comint-mode. Were slime-repl-mode derived from comint-mode (as it should be) this would just work without duplication, reinvention and feature lag, e.g., the slime-repl-ansi-color
feature described below. –
Chiba 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))
^[[32m
, instead of colors in the slime repl. I tried M-x slime-repl-ansi-color-mode RET
, and I got [No match]
, and no colors. M-x list-packages
shows that slime-repl-ansi-colors is installed. :( –
Teledu slime-repl-ansi-color.el
file that was downloaded from melpa, and was in my .emac.d
directory to the suggested directory: .emacs.d/elpa/slime-repl-ansi-color-20230214.1453% cp slime-repl-ansi-color.el ../slime-20240206.1339/contrib
. Then I added the suggested code to my .emacs file, quit and restarted emacs, then the colors worked. –
Teledu (defun color-text (string color); ANSI escape code
(let((color
(cond
((string= color "red") "31")
((string= color "green") "32")
((string= color "yellow") "33")
((string= color "white") "37")
((string= color "bright blue") "94")
((string= color "bright yellow") "93")
((string= color "bright cyan") "96")
((string= color "bright magneta") "95")
(t "90")
)))
(format t (concatenate 'string "~c[" color "m" ) #\ESC )
(eval(read-from-string string))
(format t (concatenate 'string "~c[" color "m~c[0m" ) #\ESC #\ESC))
); (color-text "(format t \"~a\" \"ADASDASDASDA dsfsdf\")" "red")
© 2022 - 2024 — McMap. All rights reserved.