Sunday, April 21, 2013

Sohi Vila

Clearly, I use Blogger. That I use asciidoc to write my blog posts might be less obvious. For the most part, it works fine. There is, however, one little thing that has been annoying me all this time…

The blogger backend for asciidoc uses the html4 stylesheets which don’t seem to be compatible with pygments (my preferred syntax highlighting solution). I guess I could have tried to bash the blogger backend into using html5 or xhtml11 to get pygments, but there were too many unknowns there to make that worthwhile investigating. The other alternative was to get asciidoc’s default highlighter to love Vim.

Asciidoc’s default syntax highlighter is GNU source-highlight. For the most part, it does a reasonable job. Why it doesn’t support Vim files out of the box is beyond me. SohiVila fixes that problem.

Here’s some VimL lovingly rendered with it:

" PrintWithHighlighting()
" Original Code by Jürgen Krämer on vim-dev
" Modified by Barry Arthur, 2013-03-21
"
" Usage:
" * As a subcommand to the :g// command
" :g/something/P
"
" * As a standalone command
" :1,10 P
" :%P
" :P

function! PrintWithHighlighting() range
  for line in getline(a:firstline, a:lastline)
    let ms = match(line, @/)
    let me = matchend(line, @/)

    while ms != -1
      echohl none
      echon strpart(line, 0, ms)
      echohl Search
      echon strpart(line, ms, me - ms)
      echohl none
      let line = strpart(line, me)
      let ms = match(line, @/)
      let me = matchend(line, @/)
    endwhile
    echon line . "\n"
  endfor
endfunction

command! -range P <line1>,<line2>call PrintWithHighlighting()