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