1" Vim syntax file 2" Language: reStructuredText documentation format 3" Maintainer: Marshall Ward <[email protected]> 4" Previous Maintainer: Nikolai Weibull <[email protected]> 5" Latest Revision: 2015-09-07 6 7if exists("b:current_syntax") 8 finish 9endif 10 11let s:cpo_save = &cpo 12set cpo&vim 13 14syn case ignore 15 16syn match rstSections "^\%(\([=`:.'"~^_*+#-]\)\1\+\n\)\=.\+\n\([=`:.'"~^_*+#-]\)\2\+$" 17 18syn match rstTransition /^[=`:.'"~^_*+#-]\{4,}\s*$/ 19 20syn cluster rstCruft contains=rstEmphasis,rstStrongEmphasis, 21 \ rstInterpretedText,rstInlineLiteral,rstSubstitutionReference, 22 \ rstInlineInternalTargets,rstFootnoteReference,rstHyperlinkReference 23 24syn region rstLiteralBlock matchgroup=rstDelimiter 25 \ start='::\_s*\n\ze\z(\s\+\)' skip='^$' end='^\z1\@!' 26 \ contains=@NoSpell 27 28syn region rstQuotedLiteralBlock matchgroup=rstDelimiter 29 \ start="::\_s*\n\ze\z([!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]\)" 30 \ end='^\z1\@!' contains=@NoSpell 31 32syn region rstDoctestBlock oneline display matchgroup=rstDelimiter 33 \ start='^>>>\s' end='^$' 34 35syn region rstTable transparent start='^\n\s*+[-=+]\+' end='^$' 36 \ contains=rstTableLines,@rstCruft 37syn match rstTableLines contained display '|\|+\%(=\+\|-\+\)\=' 38 39syn region rstSimpleTable transparent 40 \ start='^\n\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$' 41 \ end='^$' 42 \ contains=rstSimpleTableLines,@rstCruft 43syn match rstSimpleTableLines contained display 44 \ '^\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$' 45syn match rstSimpleTableLines contained display 46 \ '^\%(\s*\)\@>\%(\%(-\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(-\+\)\@>\%(\s*\)\@>\)\+\)\@>$' 47 48syn cluster rstDirectives contains=rstFootnote,rstCitation, 49 \ rstHyperlinkTarget,rstExDirective 50 51syn match rstExplicitMarkup '^\s*\.\.\_s' 52 \ nextgroup=@rstDirectives,rstComment,rstSubstitutionDefinition 53 54let s:ReferenceName = '[[:alnum:]]\+\%([_.-][[:alnum:]]\+\)*' 55 56syn keyword rstTodo contained FIXME TODO XXX NOTE 57 58execute 'syn region rstComment contained' . 59 \ ' start=/.*/' 60 \ ' end=/^\s\@!/ contains=rstTodo' 61 62execute 'syn region rstFootnote contained matchgroup=rstDirective' . 63 \ ' start=+\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]\_s+' . 64 \ ' skip=+^$+' . 65 \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell' 66 67execute 'syn region rstCitation contained matchgroup=rstDirective' . 68 \ ' start=+\[' . s:ReferenceName . '\]\_s+' . 69 \ ' skip=+^$+' . 70 \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell' 71 72syn region rstHyperlinkTarget contained matchgroup=rstDirective 73 \ start='_\%(_\|[^:\\]*\%(\\.[^:\\]*\)*\):\_s' skip=+^$+ end=+^\s\@!+ 74 75syn region rstHyperlinkTarget contained matchgroup=rstDirective 76 \ start='_`[^`\\]*\%(\\.[^`\\]*\)*`:\_s' skip=+^$+ end=+^\s\@!+ 77 78syn region rstHyperlinkTarget matchgroup=rstDirective 79 \ start=+^__\_s+ skip=+^$+ end=+^\s\@!+ 80 81execute 'syn region rstExDirective contained matchgroup=rstDirective' . 82 \ ' start=+' . s:ReferenceName . '::\_s+' . 83 \ ' skip=+^$+' . 84 \ ' end=+^\s\@!+ contains=@rstCruft,rstLiteralBlock' 85 86execute 'syn match rstSubstitutionDefinition contained' . 87 \ ' /|' . s:ReferenceName . '|\_s\+/ nextgroup=@rstDirectives' 88 89function! s:DefineOneInlineMarkup(name, start, middle, end, char_left, char_right) 90 execute 'syn region rst' . a:name . 91 \ ' start=+' . a:char_left . '\zs' . a:start . 92 \ '\ze[^[:space:]' . a:char_right . a:start[strlen(a:start) - 1] . ']+' . 93 \ a:middle . 94 \ ' end=+\S' . a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+' 95endfunction 96 97function! s:DefineInlineMarkup(name, start, middle, end) 98 let middle = a:middle != "" ? 99 \ (' skip=+\\\\\|\\' . a:middle . '+') : 100 \ "" 101 102 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, "'", "'") 103 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '"', '"') 104 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '(', ')') 105 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\[', '\]') 106 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '{', '}') 107 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '<', '>') 108 109 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\%(^\|\s\|[/:]\)', '') 110 111 execute 'syn match rst' . a:name . 112 \ ' +\%(^\|\s\|[''"([{</:]\)\zs' . a:start . 113 \ '[^[:space:]' . a:start[strlen(a:start) - 1] . ']' 114 \ a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+' 115 116 execute 'hi def link rst' . a:name . 'Delimiter' . ' rst' . a:name 117endfunction 118 119call s:DefineInlineMarkup('Emphasis', '\*', '\*', '\*') 120call s:DefineInlineMarkup('StrongEmphasis', '\*\*', '\*', '\*\*') 121call s:DefineInlineMarkup('InterpretedTextOrHyperlinkReference', '`', '`', '`_\{0,2}') 122call s:DefineInlineMarkup('InlineLiteral', '``', "", '``') 123call s:DefineInlineMarkup('SubstitutionReference', '|', '|', '|_\{0,2}') 124call s:DefineInlineMarkup('InlineInternalTargets', '_`', '`', '`') 125 126" TODO: Can’t remember why these two can’t be defined like the ones above. 127execute 'syn match rstFootnoteReference contains=@NoSpell' . 128 \ ' +\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]_+' 129 130execute 'syn match rstCitationReference contains=@NoSpell' . 131 \ ' +\[' . s:ReferenceName . '\]_\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+' 132 133execute 'syn match rstHyperlinkReference' . 134 \ ' /\<' . s:ReferenceName . '__\=\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)/' 135 136syn match rstStandaloneHyperlink contains=@NoSpell 137 \ "\<\%(\%(\%(https\=\|file\|ftp\|gopher\)://\|\%(mailto\|news\):\)[^[:space:]'\"<>]\+\|www[[:alnum:]_-]*\.[[:alnum:]_-]\+\.[^[:space:]'\"<>]\+\)[[:alnum:]/]" 138 139syn region rstCodeBlock contained matchgroup=rstDirective 140 \ start=+\%(sourcecode\|code\%(-block\)\=\)::\_s*\n\ze\z(\s\+\)+ 141 \ skip=+^$+ 142 \ end=+^\z1\@!+ 143 \ contains=@NoSpell 144syn cluster rstDirectives add=rstCodeBlock 145 146if !exists('g:rst_syntax_code_list') 147 let g:rst_syntax_code_list = ['vim', 'java', 'cpp', 'lisp', 'php', 148 \ 'python', 'perl', 'sh'] 149endif 150 151for code in g:rst_syntax_code_list 152 unlet! b:current_syntax 153 " guard against setting 'isk' option which might cause problems (issue #108) 154 let prior_isk = &l:iskeyword 155 exe 'syn include @rst'.code.' syntax/'.code.'.vim' 156 exe 'syn region rstDirective'.code.' matchgroup=rstDirective fold ' 157 \.'start=#\%(sourcecode\|code\%(-block\)\=\)::\s\+'.code.'\_s*\n\ze\z(\s\+\)# ' 158 \.'skip=#^$# ' 159 \.'end=#^\z1\@!# contains=@NoSpell,@rst'.code 160 exe 'syn cluster rstDirectives add=rstDirective'.code 161 " reset 'isk' setting, if it has been changed 162 if &l:iskeyword !=# prior_isk 163 let &l:iskeyword = prior_isk 164 endif 165 unlet! prior_isk 166endfor 167 168" TODO: Use better syncing. 169syn sync minlines=50 linebreaks=2 170 171hi def link rstTodo Todo 172hi def link rstComment Comment 173hi def link rstSections Title 174hi def link rstTransition rstSections 175hi def link rstLiteralBlock String 176hi def link rstQuotedLiteralBlock String 177hi def link rstDoctestBlock PreProc 178hi def link rstTableLines rstDelimiter 179hi def link rstSimpleTableLines rstTableLines 180hi def link rstExplicitMarkup rstDirective 181hi def link rstDirective Keyword 182hi def link rstFootnote String 183hi def link rstCitation String 184hi def link rstHyperlinkTarget String 185hi def link rstExDirective String 186hi def link rstSubstitutionDefinition rstDirective 187hi def link rstDelimiter Delimiter 188" TODO: I dunno... 189hi def rstEmphasis term=italic cterm=italic gui=italic 190hi def link rstStrongEmphasis Special 191"term=bold cterm=bold gui=bold 192hi def link rstInterpretedTextOrHyperlinkReference Identifier 193hi def link rstInlineLiteral String 194hi def link rstSubstitutionReference PreProc 195hi def link rstInlineInternalTargets Identifier 196hi def link rstFootnoteReference Identifier 197hi def link rstCitationReference Identifier 198hi def link rstHyperLinkReference Identifier 199hi def link rstStandaloneHyperlink Identifier 200hi def link rstCodeBlock String 201 202let b:current_syntax = "rst" 203 204let &cpo = s:cpo_save 205unlet s:cpo_save 206