Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created May 29, 2012 14:10
Show Gist options
  • Save ynkdir/2828657 to your computer and use it in GitHub Desktop.
Save ynkdir/2828657 to your computer and use it in GitHub Desktop.
syntax highlighter
" /usr/bin/vim -e -s -u colorcat.vim file-to-be-colored.c
if has('gui_running')
finish
endif
set nocompatible
syntax on
filetype on
edit
function! s:main()
" FIXME: Output color is different from vim's output.
let color16workaround = [0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15]
let outbuf = []
for lnum in range(1, line('$'))
let buf = ''
let prevattr = []
let line = getline(lnum)
for col in range(1, len(line))
let id = synIDtrans(synID(lnum, col, 1))
let fg = synIDattr(id, 'fg', 'cterm')
let bg = synIDattr(id, 'bg', 'cterm')
let bold = synIDattr(id, 'bold', 'cterm')
let italic = synIDattr(id, 'italic', 'cterm')
let reverse = synIDattr(id, 'reverse', 'cterm')
let standout = synIDattr(id, 'standout', 'cterm')
let underline = synIDattr(id, 'underline', 'cterm')
let attr = []
if fg == '' || fg == -1
call add(attr, 39) " set fg default
else
call extend(attr, [38, 5, get(color16workaround, fg, fg)])
endif
if bg == '' || bg == -1
call add(attr, 49) " set bg default
else
call extend(attr, [48, 5, get(color16workaround, bg, bg)])
endif
if bold
call add(attr, 1)
endif
if italic
endif
if reverse
call add(attr, 7)
endif
if standout
endif
if underline
call add(attr, 4)
endif
if attr != prevattr
let buf .= printf("\033[%sm", join(attr, ';'))
endif
let prevattr = attr
let buf .= line[col - 1]
endfor
let buf .= "\033[0;39;49m" " clear
call add(outbuf, buf)
endfor
call writefile(outbuf, '/dev/stdout')
endfunction
call s:main()
qall!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment