-
Notifications
You must be signed in to change notification settings - Fork 2
/
keg-ansi.el
302 lines (257 loc) · 10.2 KB
/
keg-ansi.el
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
;;; keg-ansi.el --- Utility for ANSI terminal escape codes -*- lexical-binding: t; -*-
;; Copyright (C) 2010-2013 Johan Andersson
;; Copyright (C) 2020 Naoya Yamashita
;; Author: Naoya Yamashita <conao3@gmail.com>
;; URL: https://github.com/conao3/keg.el
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Utility for ANSI terminal escape codes.
;; Core concept and some codes comes from ansi.el (GPLv3) by Johan Andersson.
;; https://github.com/rejeep/ansi.el
;;; Code:
(require 'macroexp)
(defgroup keg-ansi nil
"Utility for ANSI terminal escape codes."
:group 'convenience
:link '(url-link :tag "Github" "https://github.com/conao3/keg.el"))
(defconst keg-ansi-codes
'((reset . 0)
(bold . 1)
(faint . 2)
(italic . 3)
(underline . 4)
(blink . 5)
(r-blink . 6) (rapid-blink . 6)
(invert . 7)
(conceal . 8)
(strike . 9)
(font-0 . 10) (default-font . 10)
(font-1 . 11)
(font-2 . 12)
(font-3 . 13)
(font-4 . 14)
(font-5 . 15)
(font-6 . 16)
(font-7 . 17)
(font-8 . 18)
(font-9 . 19)
(font-10 . 20)
(bold-off . 21) (d-underline . 21) (double-underline . 21)
(faint-off . 22)
(italic-off . 23)
(underline-off . 24)
(blink-off . 25)
(r-blink-off . 26) (rapid-blink-off . 26)
(invert-off . 27)
(conceal-off . 28)
(strike-off . 29)
(black . 30)
(red . 31)
(green . 32)
(yellow . 33)
(blue . 34)
(magenta . 35)
(cyan . 36)
(white . 37)
;; ( . 38) ; 256 color / 24bit color
(default . 39)
(black-bg . 40)
(red-bg . 41)
(green-bg . 42)
(yellow-bg . 43)
(blue-bg . 44)
(magenta-bg . 45)
(cyan-bg . 46)
(white-bg . 47)
;; ( . 48) ; 256 color / 24bit color
(default-bg . 49)
;; ( . 50)
(frame . 51)
(circle . 52)
(overline . 53)
(frame-off . 54) (circle-off . 54)
(overline-off . 55)
;; ( . 56)
;; ( . 57)
;; ( . 58)
;; ( . 59)
(ideogram-underline . 60) (right-side-line . 60)
(ideogram-double-underline . 61) (double-line-right-side . 61)
(ideogram-overline . 62) (left-side-line . 62)
(ideogram-double-overline . 63) (double-line-left-side . 63)
(ideogram-stress-marking . 64)
(ideogram-off . 65)
;; ( . 66) - ( . 89)
(b-black . 90) (bright-black . 90)
(b-red . 91) (bright-red . 91)
(b-green . 92) (bright-green . 92)
(b-yellow . 93) (bright-yellow . 93)
(b-blue . 94) (bright-blue . 94)
(b-magenta . 95) (bright-magenta . 95)
(b-cyan . 96) (bright-cyan . 96)
(b-white . 97) (bright-white . 97)
;; ( . 98)
(b-default . 99) (bright-default . 97)
(b-black-bg . 100) (bright-black-bg . 100)
(b-red-bg . 101) (bright-red-bg . 101)
(b-green-bg . 102) (bright-green-bg . 102)
(b-yellow-bg . 103) (bright-yellow-bg . 103)
(b-blue-bg . 104) (bright-blue-bg . 104)
(b-magenta-bg . 105) (bright-magenta-bg . 105)
(b-cyan-bg . 106) (bright-cyan-bg . 106)
(b-white-bg . 107) (bright-white-bg . 107)
;; ( . 108)
(b-default-bg . 109) (bright-default-bg . 109))
"List of SGR (Select graphic rendition) codes.
See https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters")
(defconst keg-ansi-csis
'((up . "A")
(down . "B")
(forward . "C")
(backward . "D")
(ahead-down . "E") (beginning-of-line-down . "E")
(ahead-up . "F") (beginning-of-line-up . "F")
(column . "G") (move-at-column . "G")
(point . "H") (move-at-point . "H") ; require 2 arguments (x,y)
(clear . "J")
;; 0 (default): clear forward all
;; 1: clear behind all
;; 2: clear all
(clear-line . "K")
;; 0 (default): clear forward
;; 1: clear behind
;; 2: clear line
(scroll-next . "S")
(scroll-back . "T"))
"List of CSI (Control sequence introducer) codes.
See https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences")
(defun keg-ansi--macroexp-progn (exps)
"Return EXPS (a list of expressions) with `progn' prepended.
If EXPS is a list with a single expression, `progn' is not
prepended, but that expression is returned instead.
Copied from `macroexp-progn'."
(if (cdr exps) `(progn ,@exps) (car exps)))
(defmacro keg-ansi--cl-macrolet (bindings &rest body)
"Make temporary macro definitions.
This is like `cl-flet', but for macros instead of functions.
see `cl-macrolet' for BINDINGS, BODY info.
\(fn ((NAME ARGLIST BODY...) ...) FORM...)"
(declare (indent 1))
(if (null bindings)
(keg-ansi--macroexp-progn body)
(macroexpand-all (keg-ansi--macroexp-progn body)
(append
(mapcar
(lambda (binding)
(let* ((name (car binding))
(args (car (cdr binding)))
(macrobody (cdr (cdr binding))))
(cons name
(eval `(lambda ,args ,@macrobody) t))))
bindings)
macroexpand-all-environment))))
(defun keg-ansi--alist-get (key alist &optional default)
"Find the first element of ALIST whose `car' equals KEY and return its `cdr'.
If KEY is not found in ALIST, return DEFAULT.
For backward compatibility, TESTFN is always `eq'.
This function is `alist-get' polifill for Emacs < 25.1."
(declare (indent 1))
(let ((x (assq key alist)))
(if x (cdr x) default)))
(defun keg-ansi (effect-or-code format-string &rest objects)
"Apply EFFECT-OR-CODE to text.
FORMAT-STRING and OBJECTS are processed same as `apply'."
(declare (indent 1))
(let* ((str (if (stringp format-string)
format-string
(prin1-to-string format-string)))
(code (if (numberp effect-or-code)
effect-or-code
(keg-ansi--alist-get effect-or-code keg-ansi-codes)))
(text (apply 'format str objects)))
(format "\e[%dm%s\e[0m" code text)))
(defun keg-ansi-csi (effect-or-char &rest args)
"Apply EFFECT-OR-CHAR ARGS (1 default) number of times."
(declare (indent 1))
(let ((code (if (stringp effect-or-char)
effect-or-char
(keg-ansi--alist-get effect-or-char keg-ansi-csis))))
(concat "\e[" (when args (mapconcat #'prin1-to-string args ";")) code)))
(defun keg-ansi-256 (code format-string &rest objects)
"Apply 256-color CODE to text.
FORMAT-STRING and OBJECTS are processed same as `apply'."
(declare (indent 1))
(let* ((str (if (stringp format-string)
format-string
(prin1-to-string format-string)))
(text (apply 'format str objects)))
(format "\e[38;5;%sm%s\e[0m" code text)))
(defun keg-ansi-256-bg (code format-string &rest objects)
"Apply 256-color CODE to text background.
FORMAT-STRING and OBJECTS are processed same as `apply'."
(declare (indent 1))
(let* ((str (if (stringp format-string)
format-string
(prin1-to-string format-string)))
(text (apply 'format str objects)))
(format "\e[48;5;%sm%s\e[0m" code text)))
(defun keg-ansi-rgb (r g b format-string &rest objects)
"Apply R G B color to text.
FORMAT-STRING and OBJECTS are processed same as `apply'."
(declare (indent 3))
(let* ((str (if (stringp format-string)
format-string
(prin1-to-string format-string)))
(code (mapconcat #'prin1-to-string (list r g b) ";"))
(text (apply 'format str objects)))
(format "\e[38;2;%sm%s\e[0m" code text)))
(defun keg-ansi-rgb-bg (r g b format-string &rest objects)
"Apply R G B color to text background.
FORMAT-STRING and OBJECTS are processed same as `apply'."
(declare (indent 3))
(let* ((str (if (stringp format-string)
format-string
(prin1-to-string format-string)))
(code (mapconcat #'prin1-to-string (list r g b) ";"))
(text (apply 'format str objects)))
(format "\e[48;2;%sm%s\e[0m" code text)))
(defmacro with-keg-ansi (&rest body)
"Exec BODY with `keg-ansi' DSL."
(let* ((exps (macroexpand-all
`(keg-ansi--cl-macrolet
(,@(mapcar
(lambda (name)
`(,name (format-string &rest args)
`(keg-ansi ',',name ,format-string ,@args)))
(mapcar #'car keg-ansi-codes))
,@(mapcar
(lambda (name)
`(,name (&rest args)
`(keg-ansi-csi ',',name ,@args)))
(mapcar #'car keg-ansi-csis))
(256-color (code format-string &rest objects)
`(keg-ansi-256 ,code ,format-string ,@objects))
(256-bg (code format-string &rest objects)
`(keg-ansi-256-bg ,code ,format-string ,@objects))
(rgb-color (r g b format-string &rest objects)
`(keg-ansi-rgb ,r ,g ,b ,format-string ,@objects))
(rgb-bg (r g b format-string &rest objects)
`(keg-ansi-rgb-bg ,r ,g ,b ,format-string ,@objects)))
magic-spacer ; must be wrap progn
,@body)
macroexpand-all-environment)))
`(concat ,@(cddr exps)))) ; drop 'progn and spacer
(provide 'keg-ansi)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; keg-ansi.el ends here