about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/xterm-color-20180202.1518/xterm-color.el
blob: a5e9befda0cee7b8e7039ecec3a0eb223837fc0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
;;; xterm-color.el --- ANSI & XTERM 256 color support -*- lexical-binding: t -*-
;;
;; Copyright (C) 2010-2018 xristos@sdf.lonestar.org
;; All rights reserved
;;
;; Version: 1.7 - 2018-2-2
;; Package-Version: 20180202.1518
;; Author: xristos@sdf.lonestar.org
;; URL: https://github.com/atomontage/xterm-color
;; Package-Requires: ((cl-lib "0.5"))
;; Keywords: faces
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;;   * Redistributions of source code must retain the above copyright
;;     notice, this list of conditions and the following disclaimer.
;;
;;   * Redistributions in binary form must reproduce the above
;;     copyright notice, this list of conditions and the following
;;     disclaimer in the documentation and/or other materials
;;     provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; Commentary:
;;
;; Translates ANSI control sequences into text properties.
;;
;; * Regular ANSI colors
;;
;; * XTERM 256 colors
;;
;; * Works with compilation-mode
;;
;; * Works with eshell
;;
;; * More accurate than ansi-color.el
;;
;; * Should perform much better than ansi-color.el
;;
;;; Usage:
;;
;; Call xterm-color-filter to propertize strings that you can then insert into
;; a buffer. All state is kept in buffer-local variables which means that
;; control sequences can span xterm-color-filter call boundaries.
;;
;; Example:
;;
;; (xterm-color-filter "[0;1;3;4")
;; (xterm-color-filter ";35")
;; (xterm-color-filter ";mThis is only a test")
;; (xterm-color-filter "")
;;
;;
;; You may also use xterm-color-colorize-buffer, interactively or from elisp,
;; to colorize an entire buffer.
;;
;;
;; * You can replace ansi-color.el with xterm-color for all comint buffers,
;;   but this may create problems with modes that propertize strings
;;   and feed them through comint-preoutput-filter-functions since xterm-color-filter
;;   will strip all text properties.
;;
;;   The recommended configuration is to remove ansi-color-process-output from
;;   comint-output-filter-functions and add xterm-color-filter as the *first*
;;   hook in the buffer-local comint-preoutput-filter-functions for any comint-based
;;   mode that you would like it to affect (e.g. shell-mode).
;;
;;   An example configuration for shell-mode (M-x shell) is shown below:
;;
;; (setq comint-output-filter-functions
;;       (remove 'ansi-color-process-output comint-output-filter-functions))
;;
;; (add-hook 'shell-mode-hook
;;           (lambda () (add-hook 'comint-preoutput-filter-functions 'xterm-color-filter nil t)))
;;
;; Also set TERM accordingly (xterm-256color)
;;
;; * You can also use it with eshell (and thus get color output from system ls):
;;
;; (require 'eshell)
;;
;; (add-hook 'eshell-before-prompt-hook
;;           (lambda ()
;;             (setq xterm-color-preserve-properties t)))
;;
;;  (add-to-list 'eshell-preoutput-filter-functions 'xterm-color-filter)
;;  (setq eshell-output-filter-functions (remove 'eshell-handle-ansi-color eshell-output-filter-functions))
;;
;;  Don't forget to setenv TERM xterm-256color
;;
;;
;; * Compilation buffers
;;
;;
;; You may use `compilation-shell-minor-mode' with shell-mode buffers
;; and the configuration previously described.
;;
;; For standalone compilation-mode buffers, you may use the following
;; configuration:
;;
;; (setq compilation-environment '("TERM=xterm-256color"))
;;
;; (add-hook 'compilation-start-hook
;;           (lambda (proc)
;;             ;; We need to differentiate between compilation-mode buffers
;;             ;; and running as part of comint (which at this point we assume
;;             ;; has been configured separately for xterm-color)
;;             (when (eq (process-filter proc) 'compilation-filter)
;;               ;; This is a process associated with a compilation-mode buffer.
;;               ;; We may call `xterm-color-filter' before its own filter function.
;;               (set-process-filter
;;                proc
;;                (lambda (proc string)
;;                  (funcall 'compilation-filter proc (xterm-color-filter string)))))))
;;
;;
;;; Test:
;;
;; M-x xterm-color-test
;;
;; For shell or eshell:
;;
;; M-x shell || M-x eshell
;;
;; perl tests/xterm-colortest && perl tests/256colors2.pl
;;
;;
;;; Code:

(require 'cl-lib)

(defgroup xterm-color nil
  "Translates ANSI control sequences to text properties."
  :prefix "xterm-color-"
  :group 'processes)

;;
;; CUSTOM
;;

(defcustom xterm-color-debug nil
  "Print ANSI state machine debug information in *Messages* if T."
  :type 'boolean
  :group 'xterm-color)

(defcustom xterm-color-names
  ["#192033"    ; black
   "#A93F43"    ; red
   "#59963A"    ; green
   "#BE8A2D"    ; yellow
   "#4068A3"    ; blue
   "#7F60A7"    ; magenta
   "#4E9B9B"    ; cyan
   "#7E8A90"]   ; white
  "The default colors to use as regular ANSI colors."
  :type '(vector string string string string string string string string)
  :group 'xterm-color)

(defcustom xterm-color-names-bright
  ["#666666"    ; black
   "#EC6261"    ; red
   "#ADCF44"    ; green
   "#F0C649"    ; yellow
   "#63B4F6"    ; blue
   "#CB77F9"    ; magenta
   "#86D7DB"    ; cyan
   "#D3D2D1"]   ; white
  "The default colors to use as bright ANSI colors."
  :type '(vector string string string string string string string string)
  :group 'xterm-color)

;;
;; Buffer locals, used by state machine
;;

(defvar xterm-color--current nil
  "Hash table with current ANSI color.")

(make-variable-buffer-local 'xterm-color--current)

(defvar xterm-color-preserve-properties nil
  "If T, preserve existing text properties on input about to be filtered.
This should be NIL most of the time as it can mess up the internal state
machine if it encounters ANSI data with text properties applied.  It is
really meant for and works fine with eshell.")

(make-variable-buffer-local 'xterm-color-preserve-properties)

(defvar xterm-color-ignore-movement nil
  "If T, ignore CSI sequences that move the cursor.")

(make-variable-buffer-local 'xterm-color-ignore-movement)

(defvar xterm-color--char-buffer ""
  "Buffer with characters that the current ANSI color applies to.
In order to avoid having per-character text properties, we grow this
buffer dynamically until we encounter an ANSI reset sequence.

Once that happens, we generate a single text property for the entire string.")

(make-variable-buffer-local 'xterm-color--char-buffer)

(defvar xterm-color--CSI-buffer ""
  "Buffer with current ANSI CSI sequence bytes.")

(make-variable-buffer-local 'xterm-color--CSI-buffer)

(defvar xterm-color--osc-buffer ""
  "Buffer with current ANSI OSC sequence bytes.")

(make-variable-buffer-local 'xterm-color--osc-buffer)

(defvar xterm-color--state :char
  "The current state of the ANSI sequence state machine.")

(make-variable-buffer-local 'xterm-color--state)

(defvar xterm-color--attributes 0)

(make-variable-buffer-local 'xterm-color--attributes)


(defconst +xterm-color--bright+    1)
(defconst +xterm-color--italic+    2)
(defconst +xterm-color--underline+ 4)
(defconst +xterm-color--strike+    8)
(defconst +xterm-color--negative+  16)
(defconst +xterm-color--frame+     32)
(defconst +xterm-color--overline+  64)

;;
;; Functions
;;

(cl-defun xterm-color--string-properties (string)
  (cl-loop
   with res = '()
   with pos = 0
   do
   (let ((next-pos (next-property-change pos string)))
     (if next-pos
         (progn
           (push (list pos (text-properties-at pos string) (substring string pos next-pos)) res)
           (setq pos next-pos))
       (push (list pos (text-properties-at pos string) (substring string pos)) res)
       (cl-return-from xterm-color--string-properties (nreverse res))))))

(defun xterm-color--message (format-string &rest args)
  "Call `message' with FORMAT-STRING and ARGS if `xterm-color-debug' is T."
  (when xterm-color-debug
    (let ((message-truncate-lines t))
      (apply 'message format-string args)
      (message nil))))

(defun xterm-color--dispatch-SGR (elems)
  (cl-loop
   for elem = (cl-first elems)
   while elem do
   (cond
    ;; Reset
    ((= 0 elem)
     (clrhash xterm-color--current)
     (setq xterm-color--attributes 0)
     (setq elems (cdr elems)))
    ;; ANSI FG color
    ((and (>= elem 30)
          (<= elem 37))
     (setf (gethash 'foreground-color xterm-color--current)
           (- elem 30))
     (setq elems (cdr elems)))
    ;; ANSI BG color
    ((and (>= elem 40)
          (<= elem 47))
     (setf (gethash 'background-color xterm-color--current)
           (- elem 40))
     (setq elems (cdr elems)))
    ;; XTERM 256 FG color
    ((= 38 elem)
     (setf (gethash 'foreground-color xterm-color--current)
           (xterm-color-256 (cl-third elems)))
     (setq elems (cl-cdddr elems)))
    ;; XTERM 256 BG color
    ((= 48 elem)
     (setf (gethash 'background-color xterm-color--current)
           (xterm-color-256 (cl-third elems)))
     (setq elems (cl-cdddr elems)))
    ;; Reset to default FG color
    ((= 39 elem)
     (remhash 'foreground-color xterm-color--current)
     (setq elems (cdr elems)))
    ;; Reset to default BG color
    ((= 49 elem)
     (remhash 'background-color xterm-color--current)
     (setq elems (cdr elems)))
    ;; Bright color
    ((= 1 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--bright+))
     (setq elems (cdr elems)))
    ;; Faint color, emulated as normal intensity
    ((= 2 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--bright+)))
     (setq elems (cdr elems)))
    ;; Italic
    ((= 3 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--italic+))
     (setq elems (cdr elems)))
    ;; Underline
    ((= 4 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--underline+))
     (setq elems (cdr elems)))
    ;; Negative
    ((= 7 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--negative+))
     (setq elems (cdr elems)))
    ;; Strike through
    ((= 9 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--strike+))
     (setq elems (cdr elems)))
    ;; Normal intensity
    ((= 22 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--bright+)))
     (setq elems (cdr elems)))
    ;; No italic
    ((= 23 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--italic+)))
     (setq elems (cdr elems)))
    ;; No underline
    ((= 24 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--underline+)))
     (setq elems (cdr elems)))
    ;; No negative
    ((= 27 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--negative+)))
     (setq elems (cdr elems)))
    ;; No strike through
    ((= 29 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--strike+)))
     (setq elems (cdr elems)))
    ;; Frame
    ((= 51 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--frame+))
     (setq elems (cdr elems)))
    ;; Overline
    ((= 53 elem)
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--overline+))
     (setq elems (cdr elems)))
    ;; No frame
    ((= 54 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--frame+)))
     (setq elems (cdr elems)))
    ;; No overline
    ((= 55 elem)
     (setq xterm-color--attributes
           (logand xterm-color--attributes
                   (lognot +xterm-color--overline+)))
     (setq elems (cdr elems)))
    ;; AIXTERM hi-intensity FG color
    ((and (>= elem 90)
          (<= elem 97))
     (setf (gethash 'foreground-color xterm-color--current)
           (- elem 90))
     (setq xterm-color--attributes
           (logior xterm-color--attributes
                   +xterm-color--bright+))
     (setq elems (cdr elems)))
    ;; Fallback
    (t (xterm-color--message "xterm-color: not implemented SGR attribute %s" elem)
       (setq elems (cdr elems))))))


(defun xterm-color--dispatch-CSI (csi)
  (let* ((len (1- (length csi)))
         (term (aref csi len)))
    (cl-macrolet ((maybe-ignore-CSI
                   (&rest body)
                   `(if xterm-color-ignore-movement
                        (xterm-color--message "xterm-color: ignoring %s CSI since xterm-color-ignore-movement is set" csi)
                      ,@body)))
      (cond ((= ?m term)
             ;; SGR
             (if (= len 0)
                 (xterm-color--dispatch-SGR '(0))
               (let ((len (1- len)))              ; Don't need the ?m character
                 (subst-char-in-string 59 ? csi t)
                 (xterm-color--dispatch-SGR
                  ;; The following is somewhat faster than
                  ;; (mapcar 'string-to-number (split-string csi))
                  (cl-loop with idx = 0
                           while (<= idx len)
                           for (num . end) = (read-from-string csi idx (1+ len))
                           do (progn (cl-assert (integerp num))
                                     (setf idx end))
                           collect num)))))
            ((= ?J term)
             ;; Clear screen
             (xterm-color--message "xterm-color: %s CSI not implemented (clear screen)" csi))
            ((= ?C term)
             (maybe-ignore-CSI
              (let ((num (string-to-number (substring csi 0 len))))
                (setq xterm-color--char-buffer
                      (concat xterm-color--char-buffer
                              (make-string num 32))))))
            (t
             (xterm-color--message "xterm-color: %s CSI not implemented" csi))))))


(defun xterm-color-256 (color)
  (cond ((and (>= color 232)
              (<= color 255))
         ;; Grayscale
         (let ((val (+ 8 (* (- color 232) 10))))
           (format "#%02x%02x%02x"
                   val val val)))
        ((<= color 7)
         ;; Normal ANSI color
         (aref xterm-color-names color))
        ((and (>= color 8)
              (<= color 15))
         ;; Bright ANSI color
         (aref xterm-color-names-bright (- color 8)))
        (t (let* ((color-table [0 #x5f #x87 #xaf #xd7 #xff])
                  (color (- color 16))
                  (red (/ color 36))
                  (color (mod color 36))
                  (green (/ color 6))
                  (color (mod color 6))
                  (blue color))
             ;; XTERM 256 color
             (format "#%02x%02x%02x"
                     (aref color-table red)
                     (aref color-table green)
                     (aref color-table blue))))))

(defun xterm-color--make-property ()
  (let ((ret nil)
        (fg (gethash 'foreground-color xterm-color--current))
        (bg (gethash 'background-color xterm-color--current)))
    (cl-macrolet ((is-set? (attrib) `(> (logand ,attrib xterm-color--attributes) 0)))
      (when (is-set? +xterm-color--italic+)
        (push `(:slant italic) ret))
      (when (is-set? +xterm-color--underline+)
        (push `(:underline t) ret))
      (when (is-set? +xterm-color--strike+)
        (push `(:strike-through t) ret))
      (when (is-set? +xterm-color--negative+)
        (push `(:inverse-video t) ret))
      (when (is-set? +xterm-color--overline+)
        (push `(:overline t) ret))
      (when (is-set? +xterm-color--frame+)
        (push `(:box t) ret))
      (when fg
        (push `(:foreground ,(if (stringp fg)
                                 fg
                               (if (is-set? +xterm-color--bright+)
                                   (aref xterm-color-names-bright fg)
                                 (aref xterm-color-names fg))))
              ret))
      (when bg
        (push `(:background ,(if (stringp bg) bg (aref xterm-color-names bg)))
              ret)))
    ret))

(defun xterm-color-filter-real (string)
  "Translate ANSI color sequences in STRING into text properties.
Returns new STRING with text properties applied.

This function strips text properties that may be present in STRING."
  ;; It is *a lot* faster to keep track of propertized strings in a list
  ;; and mapconcat at the end, than using a temporary buffer to insert them.
  (let ((result nil))
    (cl-macrolet ((output (x) `(push ,x result))
                  (update (x place) `(setq ,place (concat ,place (string ,x))))
                  (new-state (state) `(setq xterm-color--state ,state))
                  (has-color? () `(or (> (hash-table-count xterm-color--current) 0)
                                      (not (= xterm-color--attributes 0))))
                  (maybe-fontify ()
                                 `(when (> (length xterm-color--char-buffer) 0)
                                    (if (has-color?)
                                        (output (propertize xterm-color--char-buffer 'xterm-color t
                                                            (if font-lock-mode 'font-lock-face 'face) (xterm-color--make-property)))
                                      (output xterm-color--char-buffer))
                                    (setq xterm-color--char-buffer ""))))
      (cl-loop for char across string do
	       (cl-case xterm-color--state
		 (:char
		  (cond
		   ((= char 27)		; ESC
		    (maybe-fontify)
		    (new-state :ansi-esc))
		   (t
		    (if (has-color?)
			(update char xterm-color--char-buffer)
		      (output (string char))))))
		 (:ansi-esc
		  (cond ((= char ?\[)
			 (new-state :ansi-csi))
			((= char ?\])
			 (new-state :ansi-osc))
			(t
			 (update char xterm-color--char-buffer)
			 (new-state :char))))
		 (:ansi-csi
		  (update char xterm-color--CSI-buffer)
		  (when (and (>= char #x40)
			     (<= char #x7e))
		    ;; Dispatch
		    (xterm-color--dispatch-CSI xterm-color--CSI-buffer)
		    (setq xterm-color--CSI-buffer "")
		    (new-state :char)))
		 (:ansi-osc
		  ;; Read entire sequence
		  (update char xterm-color--osc-buffer)
		  (cond ((= char 7)
			 ;; BEL
					;(xterm-color--dispatch-osc xterm-color--osc-buffer)
			 (setq xterm-color--osc-buffer "")
			 (new-state :char))
			((= char 27)
			 ;; ESC
			 (new-state :ansi-osc-esc))))
		 (:ansi-osc-esc
		  (update char xterm-color--osc-buffer)
		  (cond ((= char ?\\)
					;(xterm-color--dispatch-osc xterm-color--osc-buffer)
			 (setq xterm-color--osc-buffer "")
			 (new-state :char))
			(t (new-state :ansi-osc))))))
      (when (eq xterm-color--state :char) (maybe-fontify)))
    (mapconcat 'identity (nreverse result) "")))


;;;###autoload
(defun xterm-color-filter (string)
  "Translate ANSI color sequences in STRING into text properties.
Returns new STRING with text properties applied.

This function will check if `xterm-color-preserve-properties' is
set to T and only call `xterm-color-filter-real' on substrings
that do not have text properties applied (passing through the rest
unmodified).  Preserving properties in this fashion is really a hack
and not very robust as there may be situations where text properties
are applied on ANSI data, which will mess up the state machine.
It works fine with and is really meant for eshell though.

This can be inserted into `comint-preoutput-filter-functions'."
  (when (null xterm-color--current)
    (setq xterm-color--current (make-hash-table)))
  (if (not xterm-color-preserve-properties)
      (xterm-color-filter-real string)
    (cl-loop with res = nil
	     for (_ props substring) in (xterm-color--string-properties string) do
	     (push (if props substring (xterm-color-filter-real substring))
		   res)
	     finally return (mapconcat 'identity (nreverse res) ""))))

;; This will be removed in 2.0, it's here so as not to break existing configs
;; for 1.0 -> 1.6 transition.
(defalias 'xterm-color-unfontify-region 'font-lock-default-unfontify-region)

;;
;; Interactive
;;

;;;###autoload
(cl-defun xterm-color-colorize-buffer ()
  "Apply `xterm-color-filter' to current buffer, and replace its contents."
  (interactive)
  (let ((read-only-p buffer-read-only))
    (when read-only-p
      (unless (y-or-n-p "Buffer is read only, continue colorizing? ")
        (cl-return-from xterm-color-colorize-buffer))
      (read-only-mode -1))
    (insert (xterm-color-filter (delete-and-extract-region (point-min) (point-max))))
    (goto-char (point-min))
    (when read-only-p (read-only-mode 1))))


;;
;; Tests
;;

(let ((test-attributes
       '((1  . "bright")
         (51 . "frame")
         (3  . "italic")
         (4  . "underline")
         (7  . "negative")
         (9  . "strike through")
         (53 . "overline"))))

  (defun xterm-color--test-ansi ()
    ;; System colors
    (insert "* ANSI system colors\n\n")
    (cl-loop for color from 40 to 47 do
	     (insert (xterm-color-filter (format "[0;%sm  " color)))
	     finally (insert (xterm-color-filter "\n\n")))

    ;; Attributes (no color)
    (insert "* ANSI attributes (default colors)\n\n")
    (cl-loop for (attrib . name) in test-attributes do
	     (insert (xterm-color-filter (format "[0;%smThis is only a test!\t --[ %s ]\n" attrib name)))
	     finally (insert "\n"))

    ;; Attributes (blue fg)
    (insert "* ANSI attributes (blue foreground)\n\n")
    (cl-loop for (attrib . name) in test-attributes do
	     (insert (xterm-color-filter (format "[0;34;%smThis is only a test!\t --[ %s ]\n" attrib name)))
	     finally (insert "\n"))

    ;; Attributes (blue bg)
    (insert "* ANSI attributes (blue background)\n\n")
    (cl-loop for (attrib . name) in test-attributes do
	     (insert (xterm-color-filter (format "[0;44;%smThis is only a test!\t --[ %s ]\n" attrib name)))
	     finally (insert "\n"))))

(defun xterm-color--test-xterm ()
  ;; Normal ANSI colors mapped to XTERM
  (insert "* ANSI colors mapped to XTERM\n\n")
  (cl-loop for color from 0 to 7 do
	   (insert (xterm-color-filter (format "[48;5;%sm  " color)))
	   finally (insert (xterm-color-filter "\n\n")))

  ;; Bright ANSI colors mapped to XTERM
  (insert "* ANSI bright colors mapped to XTERM\n\n")
  (cl-loop for color from 8 to 15 do
	   (insert (xterm-color-filter (format "[48;5;%sm  " color)))
	   finally (insert (xterm-color-filter "\n\n")))

  ;; XTERM 256 color cubes
  (insert "*  XTERM 256 color cubes\n\n")
  (cl-loop for green from 0 to 5 do
	   (cl-loop for red from 0 to 5 do
		    (cl-loop for blue from 0 to 5
			     for color = (+ 16 (* 36 red) (* green 6) blue) do
			     (insert (xterm-color-filter (format "[48;5;%sm  " color))))
		    (insert (xterm-color-filter " ")))
	   (insert "\n"))
  (insert "\n")

  (insert "*  XTERM color grayscale ramp\n\n")
  (cl-loop for color from 232 to 255 do
	   (insert (xterm-color-filter (format "[48;5;%sm  " color)))
	   finally (insert (xterm-color-filter "\n\n"))))

;;;###autoload
(defun xterm-color-test ()
  "Create and display a new buffer that contains ANSI control sequences."
  (interactive)
  (let* ((name (generate-new-buffer-name "xterm-color-test"))
         (buf (get-buffer-create name)))
    (switch-to-buffer buf))
  (xterm-color--test-ansi)
  (xterm-color--test-xterm)
  (setq buffer-read-only t))


(provide 'xterm-color)
;;; xterm-color.el ends here