<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>George V. Reilly's Technical Blog : reStructuredText</title><link>http://weblogs.asp.net/george_v_reilly/archive/tags/reStructuredText/default.aspx</link><description>Tags: reStructuredText</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>reStructuredText syntax highlighting</title><link>http://weblogs.asp.net/george_v_reilly/archive/2009/03/24/restructuredtext-syntax-highlighting.aspx</link><pubDate>Wed, 25 Mar 2009 03:54:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6998099</guid><dc:creator>george_v_reilly</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/george_v_reilly/rsscomments.aspx?PostID=6998099</wfw:commentRss><comments>http://weblogs.asp.net/george_v_reilly/archive/2009/03/24/restructuredtext-syntax-highlighting.aspx#comments</comments><description>&lt;img alt="reStructuredText syntax highlighting" src="http://www.georgevreilly.com/blog/content/binary/rst-syntax-dash.png" /&gt;
&lt;p&gt;Vim has had &lt;a class="reference external" href="http://vimdoc.sourceforge.net/htmldoc/syntax.html"&gt;syntax highlighting&lt;/a&gt; since version 5.0 in 1998.
It quickly became indispensable.
It's hard to go back to looking at monochromatic source code
after you've become accustomed to syntax highlighting.&lt;/p&gt;
&lt;p&gt;The syntax highlighting is tied into Vim's support for colorschemes,
which define colors for the fundamental syntax groups
like &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Number&lt;/span&gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Comment&lt;/span&gt;&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;String&lt;/span&gt;&lt;/tt&gt;.
The syntax highlighting for a particular language
defines custom syntax groups for specific language features,
such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cppExceptions&lt;/span&gt;&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;htmlEndTag&lt;/span&gt;&lt;/tt&gt;,&lt;/p&gt;
&lt;p&gt;The custom syntax groups are &lt;em&gt;linked&lt;/em&gt; to the underlying fundamental syntax groups.
Hence, if you change your colorscheme, your syntax highlighting is updated automatically.&lt;/p&gt;
&lt;p&gt;The reStructuredText syntax highlighting in Vim 7.2
has some shortcomings, in my opinion.
For example, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;*italic*&lt;/span&gt;&lt;/tt&gt; shows up as &lt;em&gt;italic&lt;/em&gt; in gVim,
but in the same color as regular text,
while &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;**bold**&lt;/span&gt;&lt;/tt&gt; shows up in a different color, but not bolded.&lt;/p&gt;
&lt;p&gt;When you declare a syntax group,
you can either link it to another gropu and pick up that's one color and fontstyle,
or you can give it a concrete fontstyle and color.
If you do that, then the syntax group
won't change color when you change the colorscheme.&lt;/p&gt;
&lt;p&gt;After much poking around, I found a way to set a syntax group's
fontstyle &lt;em&gt;and&lt;/em&gt; link it to another group's color:
see &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;hi&lt;/span&gt; &lt;span class="pre"&gt;def&lt;/span&gt; &lt;span class="pre"&gt;rstItalic&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;hi&lt;/span&gt; &lt;span class="pre"&gt;def&lt;/span&gt; &lt;span class="pre"&gt;rstBold&lt;/span&gt;&lt;/tt&gt; below.&lt;/p&gt;
&lt;p&gt;I also make use of certain Unicode characters in my reStructuredText source,
such as endash and emdash,
which are very hard to tell apart in a fixed-width font—even though an emdash (—) is twice as wide as an endash (–) in a proportional font.
Worse, a non-breaking space is invisible and
can't easily be distinguished from a normal space.&lt;/p&gt;
&lt;p&gt;I provided custom highlighting for these Unicode characters
and the various ‘curly’ “quotes”.&lt;/p&gt;
&lt;p&gt;All of this goes into &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;~/.vim/syntax/rst.vim&lt;/span&gt;&lt;/tt&gt;,
which treats &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$VIMRUNTIME/syntax/rst.vim&lt;/span&gt;&lt;/tt&gt; as a subroutine.
I tried putting it into &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;~/.vim/after/syntax/rst.vim&lt;/span&gt;&lt;/tt&gt;,
which gets executed after &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;$VIMRUNTIME/syntax/rst.vim&lt;/span&gt;&lt;/tt&gt; completes,
but then I can't provide non-overrideable definitions for
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;rstEmphasis&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;rstStrongEmphasis&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;function&lt;/span&gt;! s:SynFgColor(hlgrp)
    &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; synIDattr(synIDtrans(hlID(a:hlgrp)), &lt;span style="color: #BA2121"&gt;&amp;#39;fg&amp;#39;&lt;/span&gt;)
endfun

&lt;span style="color: #008000; font-weight: bold"&gt;function&lt;/span&gt;! s:SynBgColor(hlgrp)
    &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; synIDattr(synIDtrans(hlID(a:hlgrp)), &lt;span style="color: #BA2121"&gt;&amp;#39;bg&amp;#39;&lt;/span&gt;)
endfun

&lt;span style="color: #008000"&gt;syn&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;match&lt;/span&gt; rstEnumeratedList &lt;span style="color: #BB6688"&gt;/^\s*[0-9#]\{1,3}\.\s/&lt;/span&gt;
&lt;span style="color: #008000"&gt;syn&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;match&lt;/span&gt; rstBulletedList &lt;span style="color: #BB6688"&gt;/^\s*[+*-]\s/&lt;/span&gt;
&lt;span style="color: #008000"&gt;syn&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;match&lt;/span&gt; rstNbsp &lt;span style="color: #BB6688"&gt;/[\xA0]/&lt;/span&gt;
&lt;span style="color: #008000"&gt;syn&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;match&lt;/span&gt; rstEmDash &lt;span style="color: #BB6688"&gt;/[\u2014]/&lt;/span&gt;
&lt;span style="color: #008000"&gt;syn&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;match&lt;/span&gt; rstUnicode &lt;span style="color: #BB6688"&gt;/[\u2013\u2018\u2019\u201C\u201D]/&lt;/span&gt; &lt;span style="color: #408080; font-style: italic"&gt;&amp;quot; – ‘ ’ “ ”&lt;/span&gt;

exec &lt;span style="color: #BA2121"&gt;&amp;#39;hi def rstBold    term=bold cterm=bold gui=bold guifg=&amp;#39;&lt;/span&gt; . s:SynFgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;PreProc&amp;#39;&lt;/span&gt;)
exec &lt;span style="color: #BA2121"&gt;&amp;#39;hi def rstItalic  term=italic cterm=italic gui=italic guifg=&amp;#39;&lt;/span&gt; . s:SynFgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;Statement&amp;#39;&lt;/span&gt;)
exec &lt;span style="color: #BA2121"&gt;&amp;#39;hi def rstNbsp    gui=underline guibg=&amp;#39;&lt;/span&gt; . s:SynBgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;ErrorMsg&amp;#39;&lt;/span&gt;)
exec &lt;span style="color: #BA2121"&gt;&amp;#39;hi def rstEmDash  gui=bold guifg=&amp;#39;&lt;/span&gt; . s:SynFgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;Title&amp;#39;&lt;/span&gt;) . &lt;span style="color: #BA2121"&gt;&amp;#39; guibg=&amp;#39;&lt;/span&gt;. s:SynBgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;Folded&amp;#39;&lt;/span&gt;)
exec &lt;span style="color: #BA2121"&gt;&amp;#39;hi def rstUnicode guifg=&amp;#39;&lt;/span&gt; . s:SynFgColor(&lt;span style="color: #BA2121"&gt;&amp;#39;Number&amp;#39;&lt;/span&gt;)

&lt;span style="color: #008000"&gt;hi&lt;/span&gt; link rstEmphasis       rstItalic
&lt;span style="color: #008000"&gt;hi&lt;/span&gt; link rstStrongEmphasis rstBold
&lt;span style="color: #008000"&gt;hi&lt;/span&gt; link rstEnumeratedList Operator
&lt;span style="color: #008000"&gt;hi&lt;/span&gt; link rstBulletedList   Operator

source $VIMRUNTIME&lt;span style="color: #BB6688"&gt;/syntax/&lt;/span&gt;rst.&lt;span style="color: #008000; font-weight: bold"&gt;vim&lt;/span&gt;

&lt;span style="color: #008000"&gt;syn&lt;/span&gt; cluster rstCruft                contains=rstEmphasis,rstStrongEmphasis,
      \ rstInterpretedText,rstInlineLiteral,rstSubstitutionReference,
      \ rstInlineInternalTargets,rstFootnoteReference,rstHyperlinkReference,
      \ rstNbsp,rstEmDash,rstUnicode
&lt;/pre&gt;&lt;/div&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6998099" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/george_v_reilly/archive/tags/Vim/default.aspx">Vim</category><category domain="http://weblogs.asp.net/george_v_reilly/archive/tags/reStructuredText/default.aspx">reStructuredText</category></item></channel></rss>