Printing color characters in Lisp / Emacs

I am writing a simple connect-4 program in Lisp, and ideally each player (red, black) will have its own color when displaying the state of the game. Does anyone know how to print colored ASCII characters? How is this done in general? I am using emacs 23, so the solution may be specific to emacs.

Anyway, I checked hyperspec to see if FORMAT can do this, but so far no luck. Thanks in advance.

+5
source share
4 answers

The appearance of text in Emacs is controlled by individuals. A face can be changed using overlay properties or text. Here is an example of using the latter:

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

, SBCL, Emacs SBCL. , Slime, Swank, Slime, :

;; Common-Lisp
(swank::eval-in-emacs
 '(with-current-buffer (slime-repl-buffer)
    (insert (propertize "foo" 'font-lock-face '(:foreground "red")))))
+6

: this, Common Lisp, -. html , - :

(gtfl-out (:p :style "color:red;" "some characters"))
+2

Elisp defun, . , , Linux VT. , ( "", "" ..) , - , , . , .

;;;; insert colored and/or bright text
(defun insert-colored-text (str clr bright)
  "Inserts str at point, in color clr, bright or not."
  (interactive (list (read-string " String: ")
                     (read-string " Color: ")
                     (y-or-n-p    " Bright? ") ))
  (insert (propertize str 'font-lock-face
          `(:weight ,(if bright 'bold 'normal) :foreground ,clr) )))
(defalias 'ict 'insert-colored-text)

(defun test-all-faces ()
  "Prints a test string in al colors, both normal and bright."
  (interactive)
  (let ((str "This is what it looks like"))
    (dolist (bold '(nil t) nil)
      (dolist (color
               '("black" "red" "green" "yellow" "blue"
                 "magenta" "cyan" "white") nil)
        (insert-colored-text
         (format "%s in %s (that is %sbold)\n" str color
                 (if bold "" "not ")) color bold) ))))
(defalias 'taf 'test-all-faces)

http://user.it.uu.se/~embe8573/cols.png

+1

, [ Common Lisp REPL.

, , , :

Blockquote

escape- ANSI :

(format t "~ c [31mabc ~ c [0m ~%" # \ ES # \ ESC); it prints a red "abc" for most modern terminals. I'm not sure if this works in mucus, though.

Share this answer answered October 6, 13 at 16:06

SaltyEgg 626412

Blockquote

+1
source

All Articles