Change the color of the text in the Common Lisp REPL
Asked Answered
D

3

7

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!

Dense answered 6/10, 2013 at 12:37 Comment(8)
I think that SLIME scratch-buffer already has colouring, wouldn't that be an alternative? Some faces in the REPL have meaning unrelated to the language semantics (used to invoke some SLIME functions for introspection / reflection), so I'd not rush into changing them.Thee
@wvxvw What's the SLIME scratch buffer? Is this something different from the scratch buffer in emacs? If so, how do I invoke it? The reason I want to control the color in my repl is to help my test my program - I am doing a board game.Dense
M-x slime-scratch common-lisp.net/project/slime/doc/html/Scratch-Buffer.htmlNevarez
@Nevarez I must be missing something obvious, but your link doesn't tell me how to install that package slime-scratch, and googling it is not helping.Dense
Well I don’t know how slime scratch will help with colours but if you have slime installed then you already have slime scratch. If you don’t use emacs and slime then it isn’t something that can be installed separately. If you want to look into how to install slime and emacs then have a look here: youtube.com/… do note though that this is about lisp devlopment, I have no idea how slime scratch relates to coloured outputNevarez
What I was trying to say is that when you write code in the scratch buffer it has the same syntax highlighting it would have in a regular buffer (unlike the REPL, where syntax highlighting conveys different meaning - it marks objects SLIME can introspect, or interact in some other way).Thee
@Nevarez I managed to install SLIME according to your video and I finally got slime-scratch. SLIME is really awesome, thanks for making that video. However for this particular question I'll go with SaltyEgg's answer (but I'll keep using SLIME).Dense
@wvxvw: Cool, cheers for the info!Nevarez
B
10

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.

Beaumont answered 6/10, 2013 at 16:6 Comment(1)
Does NOT work in slime but works in the *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
C
2

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))
Chiba answered 12/7, 2020 at 17:51 Comment(2)
Doesn't work for me. I used emacs 29.1 to install from melpa (M-x list-packages, C-s to search, then i and x on the package name. I restarted emacs, but I just get the ansi codes, e.g. ^[[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
I got the colors working following the instructions and comments here: emacs.stackexchange.com/questions/32518/…. I just copied the 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
A
0
(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")
Apothecium answered 2/3, 2020 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.