Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Colored Characters in Lisp / Emacs

Tags:

emacs

lisp

I'm writing a simple connect-4 program in Lisp and ideally each player (red, black) would have their own color when the game state is displayed. Does anyone know how to print colored ASCII characters? How is this done in general? I'm using emacs 23, so the solution might be specific to emacs itself.

Anyways, I've checked the hyperspec to see if FORMAT can do it but no luck so far. Thanks in advance.

like image 703
Limpoch Avatar asked Mar 10 '10 17:03

Limpoch


2 Answers

The appearance of text in Emacs is controlled by faces. Face can be changed through either overlay or text properties. Here is an example using the latter:

;; Emacs-Lisp
(insert (propertize "foo" 'font-lock-face '(:foreground "red")))

However, if the game is implemented in SBCL, you'll need a way to communicate with Emacs from your SBCL program. As it seems that you're using Slime, using Swank, which is a part of Slime, might be the most convenient:

;; Common-Lisp
(swank::eval-in-emacs
 '(with-current-buffer (slime-repl-buffer)
    (insert (propertize "foo" 'font-lock-face '(:foreground "red")))))
like image 51
huaiyuan Avatar answered Oct 11 '22 18:10

huaiyuan


Shameless self plug: you might want to try this, which is a graphical terminal for Common Lisp running in a web browser. It uses html for printing stuff, so you could do something like:

(gtfl-out (:p :style "color:red;" "some characters"))
like image 25
Martin Loetzsch Avatar answered Oct 11 '22 19:10

Martin Loetzsch