;; Copyright (C) 1999 Free Software Foundation, Inc.
-;; Author: Alex Schroeder <a.schroeder@bsiag.ch>
-;; Maintainer: Alex Schroeder <a.schroeder@bsiag.ch>
-;; Version: 1.1.1
+;; Author: Alex Schroeder <alex@gnu.ch>
+;; Maintainer: Alex Schroeder <alex@gnu.ch>
+;; Version: 1.2.0
;; Keywords: comm processes
;; This file is part of GNU Emacs.
;; (setq line "\e[1mbold\e[0m and \e[34mblue\e[0m, \e[1m\e[34mbold and blue\e[0m!!")
;; (ansi-color-to-text-properties line)
;; (insert line))
+;;
+;; Other test strings: (m-eating-bug) "\e[1mmold\e[0m should be mold"
;;; Bugs:
-;; Only supports the ANSI sequences that the MUSH I'm on uses (the MUSH
-;; is Elendor, see http://www.elendor.net). To see the list of codes
-;; supported I did a `help ansi()'. Based on this information, I used
-;; TinyTalk.el (without ANSI color support), gave myself the ANSI color
-;; flags using `@set me=ANSI' and `@set me=COLOR', and noted the ANSI
-;; escape sequences produced by the MUSH using `think ansi(r,red)' for
-;; example.
+;; 1. Only supports the ANSI sequences that the MUSH I'm on uses (the
+;; MUSH is Elendor, see http://www.elendor.net). To see the list of
+;; codes supported I did a `help ansi()'. Based on this information,
+;; I used TinyTalk.el (without ANSI color support), gave myself the
+;; ANSI color flags using `@set me=ANSI' and `@set me=COLOR', and
+;; noted the ANSI escape sequences produced by the MUSH using `think
+;; ansi(r,red)' for example.
+;;
+;; 2. The code is spaghetti-code, I hate it.
;;
-;; The code is spaghetti-code, I hate it.
+;; 3. If a squence of chars looks like the start of an ANSI sequence,
+;; the chars will be set invisible. If the squence of chars turns
+;; out not to be an ANSI sequence, this is not undone. Here is a
+;; teststring: "Is '\e[3' visible as ^[[3?" This could be solved by
+;; using `state': it shows most of the time how many characters have
+;; been set invisible.
\f
(while (< i str-length)
(setq char (aref str i))
(cond
- ;; Eeither just finished an ANSI control squence (state 4) or
- ;; wrote normal chars (state 0).
- ((and (or (= state 0) (= state 4)) (= char ?\e))
+ ;; When writing normal chars (state 0) and happening upon an ANSI sequence.
+ ((and (= state 0) (= char ?\e))
(setq state 1)); saw escape
((and (= state 1) (= char ?\[)); seen escape
(setq state 2
(setq param2 (string-to-number (substring str i (1+ i)))
state 4))); read second digit, prepare to quit
((and (or (= state 3) (= state 4)) (= char ?m)); reading last char: m
- (setq state 4); state 4: m will be invisible. Now reset face
- ;; according to param1 and param2.
+ (setq state 5); state 5: m will be last invisible char. Now
+ ;; reset face according to param1 and param2.
(if (null param2); only param1 set: no color changes!
;; \e[0m: default face
(if (= param1 0)
;; if reading normal chars, state is 0, put them in the
;; current face.
(put-text-property i (1+ i) 'face face str))
- ;; next char
+
+ ;; Debug: (message "%c: %d" char state)
+
+ ;; If we just finished reading an ANSI sequence (state 5), reset
+ ;; state (state 0).
+ (if (> state 4) (setq state 0))
+ ;; Next char
(setq i (1+ i)))))
(provide 'ansi-color)