ansi-color.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. ;;; ansi-color.el --- translate ANSI escape sequences into faces
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Alex Schroeder <alex@gnu.org>
  4. ;; Maintainer: Alex Schroeder <alex@gnu.org>
  5. ;; Version: 3.4.2
  6. ;; Keywords: comm processes terminals services
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file provides a function that takes a string or a region
  20. ;; containing Select Graphic Rendition (SGR) control sequences (formerly
  21. ;; known as ANSI escape sequences) and tries to translate these into
  22. ;; faces.
  23. ;;
  24. ;; This allows you to run ls --color=yes in shell-mode. It is now
  25. ;; enabled by default; to disable it, set ansi-color-for-comint-mode
  26. ;; to nil.
  27. ;;
  28. ;; Note that starting your shell from within Emacs might set the TERM
  29. ;; environment variable. The new setting might disable the output of
  30. ;; SGR control sequences. Using ls --color=yes forces ls to produce
  31. ;; these.
  32. ;;
  33. ;; SGR control sequences are defined in section 3.8.117 of the ECMA-48
  34. ;; standard (identical to ISO/IEC 6429), which is freely available as a
  35. ;; PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>. The
  36. ;; "Graphic Rendition Combination Mode (GRCM)" implemented is
  37. ;; "cumulative mode" as defined in section 7.2.8. Cumulative mode means
  38. ;; that whenever possible, SGR control sequences are combined (ie. blue
  39. ;; and bold).
  40. ;; The basic functions are:
  41. ;;
  42. ;; `ansi-color-apply' to colorize a string containing SGR control
  43. ;; sequences.
  44. ;;
  45. ;; `ansi-color-filter-apply' to filter SGR control sequences from a
  46. ;; string.
  47. ;;
  48. ;; `ansi-color-apply-on-region' to colorize a region containing SGR
  49. ;; control sequences.
  50. ;;
  51. ;; `ansi-color-filter-region' to filter SGR control sequences from a
  52. ;; region.
  53. ;;; Thanks
  54. ;; Georges Brun-Cottan <gbruncot@emc.com> for improving ansi-color.el
  55. ;; substantially by adding the code needed to cope with arbitrary chunks
  56. ;; of output and the filter functions.
  57. ;;
  58. ;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48.
  59. ;;
  60. ;; Stefan Monnier <foo@acm.com> for explaining obscure font-lock stuff and for
  61. ;; code suggestions.
  62. ;;; Code:
  63. (defvar comint-last-output-start)
  64. ;; Customization
  65. (defgroup ansi-colors nil
  66. "Translating SGR control sequences to faces.
  67. This translation effectively colorizes strings and regions based upon
  68. SGR control sequences embedded in the text. SGR (Select Graphic
  69. Rendition) control sequences are defined in section 3.8.117 of the
  70. ECMA-48 standard \(identical to ISO/IEC 6429), which is freely available
  71. as a PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>."
  72. :version "21.1"
  73. :group 'processes)
  74. (defcustom ansi-color-faces-vector
  75. [default bold default italic underline bold bold-italic modeline]
  76. "Faces used for SGR control sequences determining a face.
  77. This vector holds the faces used for SGR control sequence parameters 0
  78. to 7.
  79. Parameter Description Face used by default
  80. 0 default default
  81. 1 bold bold
  82. 2 faint default
  83. 3 italic italic
  84. 4 underlined underline
  85. 5 slowly blinking bold
  86. 6 rapidly blinking bold-italic
  87. 7 negative image modeline
  88. Note that the symbol `default' is special: It will not be combined
  89. with the current face.
  90. This vector is used by `ansi-color-make-color-map' to create a color
  91. map. This color map is stored in the variable `ansi-color-map'."
  92. :type '(vector face face face face face face face face)
  93. :set 'ansi-color-map-update
  94. :initialize 'custom-initialize-default
  95. :group 'ansi-colors)
  96. (defcustom ansi-color-names-vector
  97. ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
  98. "Colors used for SGR control sequences determining a color.
  99. This vector holds the colors used for SGR control sequences parameters
  100. 30 to 37 \(foreground colors) and 40 to 47 (background colors).
  101. Parameter Color
  102. 30 40 black
  103. 31 41 red
  104. 32 42 green
  105. 33 43 yellow
  106. 34 44 blue
  107. 35 45 magenta
  108. 36 46 cyan
  109. 37 47 white
  110. This vector is used by `ansi-color-make-color-map' to create a color
  111. map. This color map is stored in the variable `ansi-color-map'.
  112. Each element may also be a cons cell where the car and cdr specify the
  113. foreground and background colors, respectively."
  114. :type '(vector (choice color (cons color color))
  115. (choice color (cons color color))
  116. (choice color (cons color color))
  117. (choice color (cons color color))
  118. (choice color (cons color color))
  119. (choice color (cons color color))
  120. (choice color (cons color color))
  121. (choice color (cons color color)))
  122. :set 'ansi-color-map-update
  123. :initialize 'custom-initialize-default
  124. :group 'ansi-colors)
  125. (defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)"
  126. "Regexp that matches SGR control sequences.")
  127. (defconst ansi-color-drop-regexp
  128. "\033\\[\\([ABCDsuK]\\|2J\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\)"
  129. "Regexp that matches ANSI control sequences to silently drop.")
  130. (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
  131. "Regexp that matches SGR control sequence parameters.")
  132. ;; Convenience functions for comint modes (eg. shell-mode)
  133. (defcustom ansi-color-for-comint-mode t
  134. "Determines what to do with comint output.
  135. If nil, do nothing.
  136. If the symbol `filter', then filter all SGR control sequences.
  137. If anything else (such as t), then translate SGR control sequences
  138. into text properties.
  139. In order for this to have any effect, `ansi-color-process-output' must
  140. be in `comint-output-filter-functions'.
  141. This can be used to enable colorized ls --color=yes output
  142. in shell buffers. You set this variable by calling one of:
  143. \\[ansi-color-for-comint-mode-on]
  144. \\[ansi-color-for-comint-mode-off]
  145. \\[ansi-color-for-comint-mode-filter]"
  146. :type '(choice (const :tag "Do nothing" nil)
  147. (const :tag "Filter" filter)
  148. (const :tag "Translate" t))
  149. :group 'ansi-colors
  150. :version "23.2")
  151. (defvar ansi-color-apply-face-function 'ansi-color-apply-overlay-face
  152. "Function for applying an Ansi Color face to text in a buffer.
  153. This function should accept three arguments: BEG, END, and FACE,
  154. and it should apply face FACE to the text between BEG and END.")
  155. ;;;###autoload
  156. (defun ansi-color-for-comint-mode-on ()
  157. "Set `ansi-color-for-comint-mode' to t."
  158. (interactive)
  159. (setq ansi-color-for-comint-mode t))
  160. (defun ansi-color-for-comint-mode-off ()
  161. "Set `ansi-color-for-comint-mode' to nil."
  162. (interactive)
  163. (setq ansi-color-for-comint-mode nil))
  164. (defun ansi-color-for-comint-mode-filter ()
  165. "Set `ansi-color-for-comint-mode' to symbol `filter'."
  166. (interactive)
  167. (setq ansi-color-for-comint-mode 'filter))
  168. ;;;###autoload
  169. (defun ansi-color-process-output (ignored)
  170. "Maybe translate SGR control sequences of comint output into text properties.
  171. Depending on variable `ansi-color-for-comint-mode' the comint output is
  172. either not processed, SGR control sequences are filtered using
  173. `ansi-color-filter-region', or SGR control sequences are translated into
  174. text properties using `ansi-color-apply-on-region'.
  175. The comint output is assumed to lie between the marker
  176. `comint-last-output-start' and the process-mark.
  177. This is a good function to put in `comint-output-filter-functions'."
  178. (let ((start-marker (if (and (markerp comint-last-output-start)
  179. (eq (marker-buffer comint-last-output-start)
  180. (current-buffer))
  181. (marker-position comint-last-output-start))
  182. comint-last-output-start
  183. (point-min-marker)))
  184. (end-marker (process-mark (get-buffer-process (current-buffer)))))
  185. (cond ((eq ansi-color-for-comint-mode nil))
  186. ((eq ansi-color-for-comint-mode 'filter)
  187. (ansi-color-filter-region start-marker end-marker))
  188. (t
  189. (ansi-color-apply-on-region start-marker end-marker)))))
  190. (defalias 'ansi-color-unfontify-region 'font-lock-default-unfontify-region)
  191. (make-obsolete 'ansi-color-unfontify-region "not needed any more" "24.1")
  192. ;; Working with strings
  193. (defvar ansi-color-context nil
  194. "Context saved between two calls to `ansi-color-apply'.
  195. This is a list of the form (FACES FRAGMENT) or nil. FACES is a list of
  196. faces the last call to `ansi-color-apply' ended with, and FRAGMENT is a
  197. string starting with an escape sequence, possibly the start of a new
  198. escape sequence.")
  199. (make-variable-buffer-local 'ansi-color-context)
  200. (defun ansi-color-filter-apply (string)
  201. "Filter out all ANSI control sequences from STRING.
  202. Every call to this function will set and use the buffer-local variable
  203. `ansi-color-context' to save partial escape sequences. This information
  204. will be used for the next call to `ansi-color-apply'. Set
  205. `ansi-color-context' to nil if you don't want this.
  206. This function can be added to `comint-preoutput-filter-functions'."
  207. (let ((start 0) end result)
  208. ;; if context was saved and is a string, prepend it
  209. (if (cadr ansi-color-context)
  210. (setq string (concat (cadr ansi-color-context) string)
  211. ansi-color-context nil))
  212. ;; find the next escape sequence
  213. (while (setq end (string-match ansi-color-regexp string start))
  214. (setq result (concat result (substring string start end))
  215. start (match-end 0)))
  216. ;; save context, add the remainder of the string to the result
  217. (let (fragment)
  218. (if (string-match "\033" string start)
  219. (let ((pos (match-beginning 0)))
  220. (setq fragment (substring string pos)
  221. result (concat result (substring string start pos))))
  222. (setq result (concat result (substring string start))))
  223. (setq ansi-color-context (if fragment (list nil fragment))))
  224. result))
  225. (defun ansi-color-apply (string)
  226. "Translates SGR control sequences into text properties.
  227. Delete all other control sequences without processing them.
  228. Applies SGR control sequences setting foreground and background colors
  229. to STRING using text properties and returns the result. The colors used
  230. are given in `ansi-color-faces-vector' and `ansi-color-names-vector'.
  231. See function `ansi-color-apply-sequence' for details.
  232. Every call to this function will set and use the buffer-local variable
  233. `ansi-color-context' to save partial escape sequences and current face.
  234. This information will be used for the next call to `ansi-color-apply'.
  235. Set `ansi-color-context' to nil if you don't want this.
  236. This function can be added to `comint-preoutput-filter-functions'."
  237. (let ((face (car ansi-color-context))
  238. (start 0) end escape-sequence result
  239. colorized-substring)
  240. ;; If context was saved and is a string, prepend it.
  241. (if (cadr ansi-color-context)
  242. (setq string (concat (cadr ansi-color-context) string)
  243. ansi-color-context nil))
  244. ;; Find the next escape sequence.
  245. (while (setq end (string-match ansi-color-regexp string start))
  246. (setq escape-sequence (match-string 1 string))
  247. ;; Colorize the old block from start to end using old face.
  248. (when face
  249. (put-text-property start end 'font-lock-face face string))
  250. (setq colorized-substring (substring string start end)
  251. start (match-end 0))
  252. ;; Eliminate unrecognized ANSI sequences.
  253. (while (string-match ansi-color-drop-regexp colorized-substring)
  254. (setq colorized-substring
  255. (replace-match "" nil nil colorized-substring)))
  256. (push colorized-substring result)
  257. ;; Create new face, by applying escape sequence parameters.
  258. (setq face (ansi-color-apply-sequence escape-sequence face)))
  259. ;; if the rest of the string should have a face, put it there
  260. (when face
  261. (put-text-property start (length string) 'font-lock-face face string))
  262. ;; save context, add the remainder of the string to the result
  263. (let (fragment)
  264. (if (string-match "\033" string start)
  265. (let ((pos (match-beginning 0)))
  266. (setq fragment (substring string pos))
  267. (push (substring string start pos) result))
  268. (push (substring string start) result))
  269. (setq ansi-color-context (if (or face fragment) (list face fragment))))
  270. (apply 'concat (nreverse result))))
  271. ;; Working with regions
  272. (defvar ansi-color-context-region nil
  273. "Context saved between two calls to `ansi-color-apply-on-region'.
  274. This is a list of the form (FACES MARKER) or nil. FACES is a list of
  275. faces the last call to `ansi-color-apply-on-region' ended with, and
  276. MARKER is a buffer position within an escape sequence or the last
  277. position processed.")
  278. (make-variable-buffer-local 'ansi-color-context-region)
  279. (defun ansi-color-filter-region (begin end)
  280. "Filter out all ANSI control sequences from region BEGIN to END.
  281. Every call to this function will set and use the buffer-local variable
  282. `ansi-color-context-region' to save position. This information will be
  283. used for the next call to `ansi-color-apply-on-region'. Specifically,
  284. it will override BEGIN, the start of the region. Set
  285. `ansi-color-context-region' to nil if you don't want this."
  286. (let ((end-marker (copy-marker end))
  287. (start (or (cadr ansi-color-context-region) begin)))
  288. (save-excursion
  289. (goto-char start)
  290. ;; Delete unrecognized escape sequences.
  291. (while (re-search-forward ansi-color-drop-regexp end-marker t)
  292. (replace-match ""))
  293. (goto-char start)
  294. ;; Delete SGR escape sequences.
  295. (while (re-search-forward ansi-color-regexp end-marker t)
  296. (replace-match ""))
  297. ;; save context, add the remainder of the string to the result
  298. (if (re-search-forward "\033" end-marker t)
  299. (setq ansi-color-context-region (list nil (match-beginning 0)))
  300. (setq ansi-color-context-region nil)))))
  301. (defun ansi-color-apply-on-region (begin end)
  302. "Translates SGR control sequences into overlays or extents.
  303. Delete all other control sequences without processing them.
  304. SGR control sequences are applied by setting foreground and
  305. background colors to the text between BEGIN and END using
  306. overlays. The colors used are given in `ansi-color-faces-vector'
  307. and `ansi-color-names-vector'. See `ansi-color-apply-sequence'
  308. for details.
  309. Every call to this function will set and use the buffer-local variable
  310. `ansi-color-context-region' to save position and current face. This
  311. information will be used for the next call to
  312. `ansi-color-apply-on-region'. Specifically, it will override BEGIN, the
  313. start of the region and set the face with which to start. Set
  314. `ansi-color-context-region' to nil if you don't want this."
  315. (let ((face (car ansi-color-context-region))
  316. (start-marker (or (cadr ansi-color-context-region)
  317. (copy-marker begin)))
  318. (end-marker (copy-marker end))
  319. escape-sequence)
  320. ;; First, eliminate unrecognized ANSI control sequences.
  321. (save-excursion
  322. (goto-char start-marker)
  323. (while (re-search-forward ansi-color-drop-regexp end-marker t)
  324. (replace-match "")))
  325. (save-excursion
  326. (goto-char start-marker)
  327. ;; Find the next SGR sequence.
  328. (while (re-search-forward ansi-color-regexp end-marker t)
  329. ;; Colorize the old block from start to end using old face.
  330. (funcall ansi-color-apply-face-function
  331. start-marker (match-beginning 0)
  332. face)
  333. ;; store escape sequence and new start position
  334. (setq escape-sequence (match-string 1)
  335. start-marker (copy-marker (match-end 0)))
  336. ;; delete the escape sequence
  337. (replace-match "")
  338. ;; create new face by applying all the parameters in the escape
  339. ;; sequence
  340. (setq face (ansi-color-apply-sequence escape-sequence face)))
  341. ;; search for the possible start of a new escape sequence
  342. (if (re-search-forward "\033" end-marker t)
  343. (progn
  344. ;; if the rest of the region should have a face, put it there
  345. (funcall ansi-color-apply-face-function
  346. start-marker (point) face)
  347. ;; save face and point
  348. (setq ansi-color-context-region
  349. (list face (copy-marker (match-beginning 0)))))
  350. ;; if the rest of the region should have a face, put it there
  351. (funcall ansi-color-apply-face-function
  352. start-marker end-marker face)
  353. (setq ansi-color-context-region (if face (list face)))))))
  354. (defun ansi-color-apply-overlay-face (beg end face)
  355. "Make an overlay from BEG to END, and apply face FACE.
  356. If FACE is nil, do nothing."
  357. (when face
  358. (ansi-color-set-extent-face
  359. (ansi-color-make-extent beg end)
  360. face)))
  361. ;; This function helps you look for overlapping overlays. This is
  362. ;; useful in comint-buffers. Overlapping overlays should not happen!
  363. ;; A possible cause for bugs are the markers. If you create an overlay
  364. ;; up to the end of the region, then that end might coincide with the
  365. ;; process-mark. As text is added BEFORE the process-mark, the overlay
  366. ;; will keep growing. Therefore, as more overlays are created later on,
  367. ;; there will be TWO OR MORE overlays covering the buffer at that point.
  368. ;; This function helps you check your buffer for these situations.
  369. ; (defun ansi-color-debug-overlays ()
  370. ; (interactive)
  371. ; (let ((pos (point-min)))
  372. ; (while (< pos (point-max))
  373. ; (if (<= 2 (length (overlays-at pos)))
  374. ; (progn
  375. ; (goto-char pos)
  376. ; (error "%d overlays at %d" (length (overlays-at pos)) pos))
  377. ; (let (message-log-max)
  378. ; (message "Reached %d." pos)))
  379. ; (setq pos (next-overlay-change pos)))))
  380. ;; Emacs/XEmacs compatibility layer
  381. (defun ansi-color-make-face (property color)
  382. "Return a face with PROPERTY set to COLOR.
  383. PROPERTY can be either symbol `foreground' or symbol `background'.
  384. For Emacs, we just return the cons cell \(PROPERTY . COLOR).
  385. For XEmacs, we create a temporary face and return it."
  386. (if (featurep 'xemacs)
  387. (let ((face (make-face (intern (concat color "-" (symbol-name property)))
  388. "Temporary face created by ansi-color."
  389. t)))
  390. (set-face-property face property color)
  391. face)
  392. (cond ((eq property 'foreground)
  393. (cons 'foreground-color color))
  394. ((eq property 'background)
  395. (cons 'background-color color))
  396. (t
  397. (cons property color)))))
  398. (defun ansi-color-make-extent (from to &optional object)
  399. "Make an extent for the range [FROM, TO) in OBJECT.
  400. OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs
  401. uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT,
  402. Emacs requires OBJECT to be a buffer."
  403. (if (fboundp 'make-extent)
  404. (make-extent from to object)
  405. ;; In Emacs, the overlay might end at the process-mark in comint
  406. ;; buffers. In that case, new text will be inserted before the
  407. ;; process-mark, ie. inside the overlay (using insert-before-marks).
  408. ;; In order to avoid this, we use the `insert-behind-hooks' overlay
  409. ;; property to make sure it works.
  410. (let ((overlay (make-overlay from to object)))
  411. (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay))
  412. overlay)))
  413. (defun ansi-color-freeze-overlay (overlay is-after begin end &optional len)
  414. "Prevent OVERLAY from being extended.
  415. This function can be used for the `modification-hooks' overlay
  416. property."
  417. ;; if stuff was inserted at the end of the overlay
  418. (when (and is-after
  419. (= 0 len)
  420. (= end (overlay-end overlay)))
  421. ;; reset the end of the overlay
  422. (move-overlay overlay (overlay-start overlay) begin)))
  423. (defun ansi-color-set-extent-face (extent face)
  424. "Set the `face' property of EXTENT to FACE.
  425. XEmacs uses `set-extent-face', Emacs uses `overlay-put'."
  426. (if (featurep 'xemacs)
  427. (set-extent-face extent face)
  428. (overlay-put extent 'face face)))
  429. ;; Helper functions
  430. (defun ansi-color-apply-sequence (escape-sequence faces)
  431. "Apply ESCAPE-SEQ to FACES and return the new list of faces.
  432. ESCAPE-SEQ is an escape sequences parsed by `ansi-color-get-face'.
  433. If the new faces start with the symbol `default', then the new
  434. faces are returned. If the faces start with something else,
  435. they are appended to the front of the FACES list, and the new
  436. list of faces is returned.
  437. If `ansi-color-get-face' returns nil, then we either got a
  438. null-sequence, or we stumbled upon some garbage. In either
  439. case we return nil."
  440. (let ((new-faces (ansi-color-get-face escape-sequence)))
  441. (cond ((null new-faces)
  442. nil)
  443. ((eq (car new-faces) 'default)
  444. (cdr new-faces))
  445. (t
  446. ;; Like (append NEW-FACES FACES)
  447. ;; but delete duplicates in FACES.
  448. (let ((modified-faces (copy-sequence faces)))
  449. (dolist (face (nreverse new-faces))
  450. (setq modified-faces (delete face modified-faces))
  451. (push face modified-faces))
  452. modified-faces)))))
  453. (defun ansi-color-make-color-map ()
  454. "Creates a vector of face definitions and returns it.
  455. The index into the vector is an ANSI code. See the documentation of
  456. `ansi-color-map' for an example.
  457. The face definitions are based upon the variables
  458. `ansi-color-faces-vector' and `ansi-color-names-vector'."
  459. (let ((ansi-color-map (make-vector 50 nil))
  460. (index 0))
  461. ;; miscellaneous attributes
  462. (mapc
  463. (function (lambda (e)
  464. (aset ansi-color-map index e)
  465. (setq index (1+ index)) ))
  466. ansi-color-faces-vector)
  467. ;; foreground attributes
  468. (setq index 30)
  469. (mapc
  470. (function (lambda (e)
  471. (aset ansi-color-map index
  472. (ansi-color-make-face 'foreground
  473. (if (consp e) (car e) e)))
  474. (setq index (1+ index)) ))
  475. ansi-color-names-vector)
  476. ;; background attributes
  477. (setq index 40)
  478. (mapc
  479. (function (lambda (e)
  480. (aset ansi-color-map index
  481. (ansi-color-make-face 'background
  482. (if (consp e) (cdr e) e)))
  483. (setq index (1+ index)) ))
  484. ansi-color-names-vector)
  485. ansi-color-map))
  486. (defvar ansi-color-map (ansi-color-make-color-map)
  487. "A brand new color map suitable for `ansi-color-get-face'.
  488. The value of this variable is usually constructed by
  489. `ansi-color-make-color-map'. The values in the array are such that the
  490. numbers included in an SGR control sequences point to the correct
  491. foreground or background colors.
  492. Example: The sequence \033[34m specifies a blue foreground. Therefore:
  493. (aref ansi-color-map 34)
  494. => \(foreground-color . \"blue\")")
  495. (defun ansi-color-map-update (symbol value)
  496. "Update `ansi-color-map'.
  497. Whenever the vectors used to construct `ansi-color-map' are changed,
  498. this function is called. Therefore this function is listed as the :set
  499. property of `ansi-color-faces-vector' and `ansi-color-names-vector'."
  500. (set-default symbol value)
  501. (setq ansi-color-map (ansi-color-make-color-map)))
  502. (defun ansi-color-get-face-1 (ansi-code)
  503. "Get face definition from `ansi-color-map'.
  504. ANSI-CODE is used as an index into the vector."
  505. (condition-case nil
  506. (aref ansi-color-map ansi-code)
  507. (args-out-of-range nil)))
  508. (defun ansi-color-get-face (escape-seq)
  509. "Create a new face by applying all the parameters in ESCAPE-SEQ.
  510. Should any of the parameters result in the default face (usually this is
  511. the parameter 0), then the effect of all previous parameters is canceled.
  512. ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter
  513. 34 is used by `ansi-color-get-face-1' to return a face definition."
  514. (let ((i 0)
  515. f val)
  516. (while (string-match ansi-color-parameter-regexp escape-seq i)
  517. (setq i (match-end 0)
  518. val (ansi-color-get-face-1
  519. (string-to-number (match-string 1 escape-seq) 10)))
  520. (cond ((not val))
  521. ((eq val 'default)
  522. (setq f (list val)))
  523. (t
  524. (unless (member val f)
  525. (push val f)))))
  526. f))
  527. (provide 'ansi-color)
  528. ;;; ansi-color.el ends here