| 111 | | |
| 112 | | function! HighlightConsoleCodes() |
| 113 | | 0 |
| 114 | | let register_save = @" |
| 115 | | while search('\[[0-9;]*m', 'c') |
| 116 | | normal! dfm |
| 117 | | |
| 118 | | let [lnum, col] = getpos('.')[1:2] |
| 119 | | if len(getline('.')) == col |
| 120 | | let col += 1 |
| 121 | | endif |
| 122 | | let syntax_name = 'ConsoleCodeAt_' . bufnr('%') . '_' . lnum . '_' . col |
| 123 | | execute 'syntax region' syntax_name 'start=+\%' . lnum . 'l\%' . col . 'c+ end=+\%$+' 'contains=ALL' |
| 124 | | |
| 125 | | let highlight = '' |
| 126 | | for color_code in split(matchstr(@", '[0-9;]\+'), ';') |
| 127 | | if color_code == 0 |
| 128 | | let highlight .= ' ctermfg=NONE ctermbg=NONE' |
| 129 | | elseif color_code == 1 |
| 130 | | let highlight .= ' cterm=bold' |
| 131 | | elseif 30 <= color_code && color_code <= 37 |
| 132 | | let highlight .= ' ctermfg=' . (color_code - 30) |
| 133 | | elseif color_code == 38 |
| 134 | | " TODO |
| 135 | | elseif color_code == 39 |
| 136 | | " TODO |
| 137 | | elseif 40 <= color_code && color_code <= 47 |
| 138 | | let highlight .= ' ctermbg=' . (color_code - 40) |
| 139 | | elseif color_code == 49 |
| 140 | | " TODO |
| 141 | | endif |
| 142 | | endfor |
| 143 | | if len(highlight) |
| 144 | | execute 'highlight' syntax_name highlight |
| 145 | | endif |
| 146 | | endwhile |
| 147 | | let @" = register_save |
| 148 | | 0 |
| 149 | | endfunction |
| 150 | | |
| 151 | | autocmd BufRead,StdinReadPost * if search('[\d*m', 'n') | call HighlightConsoleCodes() | set buftype=nofile nomodifiable | endif |
| 152 | | " `:set modifiable | undo | syntax clear' to revert |
| 153 | | |