Brian Tiffin - 2016-10-22

I was chasing down a problem with Vim highlighting in code blocks in the UP docs, asterisk characters caused the rst syntax highlighter to go into emphasis mode inside code blocks. Not optimal, as it requires a second asterisk to turn off the mode, and there are times when that isn't possible without adding extraneous markup.

Found a cool thing while hunting down a possible fix.

If you are using rst files and use code-block (or the alias sourcecode) directives, the Vim syntax files will allow a fallback to the mentioned language.

In syntax/rst.vim these lines

if !exists('g:rst_syntax_code_list')
    let g:rst_syntax_code_list = ['vim', 'java', 'cobol', 'cobolfree',
                                \ 'unicon', 'icon', 'cpp', 'lisp', 'php',
                                \ 'python', 'perl', 'sh']
endif

can be customized. I already added cobol, icon and unicon above.

That will allow vim to highlight the codeblocks as if that was the current syntax= setting.

Some ReStructuredText
---------------------

With a code block
.................

.. sourcecode:: unicon

    procedure main()
        write("neat")
    end

Very nice on the eyes. And no more getting confused with Unicon operator characters and other highlighting modes. Of course it means having a unicon.vim file, and a proper FileType handler installed to set syntax=unicon when a *.icn file is detected (currently defaults to set syntax=icon), that change occurs in filetype.vim just above the syntax tree.

As can be seen above, the rst pygments lexer here on the forge has the same capability, highlighting code blocks using the given language. I still haven't posted code to Team Pocoo to get the unicon/icon pygments lexers into the upstream build of pygments, so it'll still be a while before those colours look right in these discussion groups, without the pygments support, that code will highlight as a simple string, but that will change someday soon.

Vim and ReStructuredText with Unicon for the win.

Have good, make well,
Brian

 

Last edit: Brian Tiffin 2016-10-22