"\e\\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]"
"Regexp matching an ANSI control sequence.")
+(defconst ansi-color--control-seq-fragment-regexp
+ "\e\\[[\x30-\x3F]*[\x20-\x2F]*\\|\e"
+ "Regexp matching a partial ANSI control sequence.")
+
(defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
"Regexp that matches SGR control sequence parameters.")
;; save context, add the remainder of the string to the result
(let ((fragment ""))
(push (substring string start
- (if (string-match "\033" string start)
+ (if (string-match
+ (concat "\\(?:"
+ ansi-color--control-seq-fragment-regexp
+ "\\)\\'")
+ string start)
(let ((pos (match-beginning 0)))
(setq fragment (substring string pos))
pos)
(put-text-property start (length string)
'font-lock-face face string))
;; save context, add the remainder of the string to the result
- (if (string-match "\033" string start)
+ (if (string-match
+ (concat "\\(?:" ansi-color--control-seq-fragment-regexp "\\)\\'")
+ string start)
(let ((pos (match-beginning 0)))
(setcar (cdr context) (substring string pos))
(push (substring string start pos) result))
(while (re-search-forward ansi-color-control-seq-regexp end-marker t)
(delete-region (match-beginning 0) (match-end 0)))
;; save context, add the remainder of the string to the result
- (if (re-search-forward "\033" end-marker t)
+ (set-marker start (point))
+ (while (re-search-forward ansi-color--control-seq-fragment-regexp
+ end-marker t))
+ (if (and (/= (point) start)
+ (= (point) end-marker))
(set-marker start (match-beginning 0))
(set-marker start nil)))))
;; Otherwise, strip.
(delete-region esc-beg esc-end))))
;; search for the possible start of a new escape sequence
- (if (re-search-forward "\033" end-marker t)
+ (while (re-search-forward ansi-color--control-seq-fragment-regexp
+ end-marker t))
+ (if (and (/= (point) start-marker)
+ (= (point) end-marker))
(progn
- (while (re-search-forward "\033" end-marker t))
- (backward-char)
+ (goto-char (match-beginning 0))
(funcall ansi-color-apply-face-function
start-marker (point)
(ansi-color--face-vec-face face-vec))
(insert str)
(ansi-color-apply-on-region opoint (point))))
(should (ansi-color-tests-equal-props
- propertized-str (buffer-string))))))
+ propertized-str (buffer-string))))
+
+ ;; \e not followed by '[' and invalid ANSI escape seqences
+ (dolist (fun (list ansi-filt ansi-app))
+ (with-temp-buffer
+ (should (equal (funcall fun "\e") ""))
+ (should (equal (funcall fun "\e[33m test \e[0m")
+ (with-temp-buffer
+ (concat "\e" (funcall fun "\e[33m test \e[0m"))))))
+ (with-temp-buffer
+ (should (equal (funcall fun "\e[") ""))
+ (should (equal (funcall fun "\e[33m Z \e[0m")
+ (with-temp-buffer
+ (concat "\e[" (funcall fun "\e[33m Z \e[0m"))))))
+ (with-temp-buffer
+ (should (equal (funcall fun "\e a \e\e[\e[") "\e a \e\e["))
+ (should (equal (funcall fun "\e[33m Z \e[0m")
+ (with-temp-buffer
+ (concat "\e[" (funcall fun "\e[33m Z \e[0m")))))))))
(provide 'ansi-color-tests)