blob: c865cf6905503270b50cc942809867b110faecc1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
Bram Moolenaar7d76c802014-10-15 22:51:52 +02002" Language: reStructuredText documentation format
3" Maintainer: Marshall Ward <marshall.ward@gmail.com>
Bram Moolenaar7b61a542014-08-23 15:31:19 +02004" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaar7571d552016-08-18 22:54:46 +02005" Website: https://github.com/marshallward/vim-restructuredtext
Bram Moolenaar4c05fa02019-01-01 15:32:17 +01006" Latest Revision: 2018-12-29
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +00009 finish
10endif
11
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012let s:cpo_save = &cpo
13set cpo&vim
14
Bram Moolenaar071d4272004-06-13 20:20:40 +000015syn case ignore
16
Bram Moolenaar5c736222010-01-06 20:54:52 +010017syn match rstTransition /^[=`:.'"~^_*+#-]\{4,}\s*$/
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaar779b74b2006-04-10 14:55:34 +000019syn cluster rstCruft contains=rstEmphasis,rstStrongEmphasis,
20 \ rstInterpretedText,rstInlineLiteral,rstSubstitutionReference,
21 \ rstInlineInternalTargets,rstFootnoteReference,rstHyperlinkReference
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaar779b74b2006-04-10 14:55:34 +000023syn region rstLiteralBlock matchgroup=rstDelimiter
24 \ start='::\_s*\n\ze\z(\s\+\)' skip='^$' end='^\z1\@!'
25 \ contains=@NoSpell
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar779b74b2006-04-10 14:55:34 +000027syn region rstQuotedLiteralBlock matchgroup=rstDelimiter
28 \ start="::\_s*\n\ze\z([!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]\)"
29 \ end='^\z1\@!' contains=@NoSpell
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar779b74b2006-04-10 14:55:34 +000031syn region rstDoctestBlock oneline display matchgroup=rstDelimiter
32 \ start='^>>>\s' end='^$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
Bram Moolenaar779b74b2006-04-10 14:55:34 +000034syn region rstTable transparent start='^\n\s*+[-=+]\+' end='^$'
35 \ contains=rstTableLines,@rstCruft
36syn match rstTableLines contained display '|\|+\%(=\+\|-\+\)\='
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar779b74b2006-04-10 14:55:34 +000038syn 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*\)\@>\)\+\)\@>$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar779b74b2006-04-10 14:55:34 +000047syn cluster rstDirectives contains=rstFootnote,rstCitation,
48 \ rstHyperlinkTarget,rstExDirective
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
Bram Moolenaar7b61a542014-08-23 15:31:19 +020050syn match rstExplicitMarkup '^\s*\.\.\_s'
Bram Moolenaar779b74b2006-04-10 14:55:34 +000051 \ nextgroup=@rstDirectives,rstComment,rstSubstitutionDefinition
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar91f84f62018-07-29 15:07:52 +020053" "Simple reference names are single words consisting of alphanumerics plus
54" isolated (no two adjacent) internal hyphens, underscores, periods, colons
55" and plus signs."
56let s:ReferenceName = '[[:alnum:]]\%([-_.:+]\?[[:alnum:]]\+\)*'
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaar779b74b2006-04-10 14:55:34 +000058syn keyword rstTodo contained FIXME TODO XXX NOTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
Bram Moolenaar779b74b2006-04-10 14:55:34 +000060execute 'syn region rstComment contained' .
61 \ ' start=/.*/'
Bram Moolenaar4c05fa02019-01-01 15:32:17 +010062 \ ' skip=+^$+' .
Bram Moolenaar779b74b2006-04-10 14:55:34 +000063 \ ' end=/^\s\@!/ contains=rstTodo'
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
Bram Moolenaar779b74b2006-04-10 14:55:34 +000065execute 'syn region rstFootnote contained matchgroup=rstDirective' .
66 \ ' start=+\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]\_s+' .
67 \ ' skip=+^$+' .
68 \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
69
70execute 'syn region rstCitation contained matchgroup=rstDirective' .
71 \ ' start=+\[' . s:ReferenceName . '\]\_s+' .
72 \ ' skip=+^$+' .
73 \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
74
75syn region rstHyperlinkTarget contained matchgroup=rstDirective
76 \ start='_\%(_\|[^:\\]*\%(\\.[^:\\]*\)*\):\_s' skip=+^$+ end=+^\s\@!+
77
78syn region rstHyperlinkTarget contained matchgroup=rstDirective
79 \ start='_`[^`\\]*\%(\\.[^`\\]*\)*`:\_s' skip=+^$+ end=+^\s\@!+
80
81syn region rstHyperlinkTarget matchgroup=rstDirective
82 \ start=+^__\_s+ skip=+^$+ end=+^\s\@!+
83
84execute 'syn region rstExDirective contained matchgroup=rstDirective' .
85 \ ' start=+' . s:ReferenceName . '::\_s+' .
86 \ ' skip=+^$+' .
Bram Moolenaar12969c02015-09-08 23:36:10 +020087 \ ' end=+^\s\@!+ contains=@rstCruft,rstLiteralBlock'
Bram Moolenaar779b74b2006-04-10 14:55:34 +000088
89execute 'syn match rstSubstitutionDefinition contained' .
Bram Moolenaar91f84f62018-07-29 15:07:52 +020090 \ ' /|.*|\_s\+/ nextgroup=@rstDirectives'
Bram Moolenaar779b74b2006-04-10 14:55:34 +000091
92function! s:DefineOneInlineMarkup(name, start, middle, end, char_left, char_right)
93 execute 'syn region rst' . a:name .
94 \ ' start=+' . a:char_left . '\zs' . a:start .
Bram Moolenaar25394022007-05-10 19:06:20 +000095 \ '\ze[^[:space:]' . a:char_right . a:start[strlen(a:start) - 1] . ']+' .
Bram Moolenaar779b74b2006-04-10 14:55:34 +000096 \ a:middle .
Bram Moolenaar7571d552016-08-18 22:54:46 +020097 \ ' end=+\S' . a:end . '\ze\%($\|\s\|[''"’)\]}>/:.,;!?\\-]\)+'
Bram Moolenaar779b74b2006-04-10 14:55:34 +000098endfunction
99
100function! s:DefineInlineMarkup(name, start, middle, end)
101 let middle = a:middle != "" ?
102 \ (' skip=+\\\\\|\\' . a:middle . '+') :
103 \ ""
104
105 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, "'", "'")
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200106 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '"', '"')
107 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '(', ')')
108 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\[', '\]')
109 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '{', '}')
110 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '<', '>')
Bram Moolenaar7571d552016-08-18 22:54:46 +0200111 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '’', '’')
112 " TODO: Additional Unicode Pd, Po, Pi, Pf, Ps characters
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000113
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200114 call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\%(^\|\s\|\%ua0\|[/:]\)', '')
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000115
116 execute 'syn match rst' . a:name .
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200117 \ ' +\%(^\|\s\|\%ua0\|[''"([{</:]\)\zs' . a:start .
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000118 \ '[^[:space:]' . a:start[strlen(a:start) - 1] . ']'
119 \ a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
120
121 execute 'hi def link rst' . a:name . 'Delimiter' . ' rst' . a:name
122endfunction
123
124call s:DefineInlineMarkup('Emphasis', '\*', '\*', '\*')
125call s:DefineInlineMarkup('StrongEmphasis', '\*\*', '\*', '\*\*')
126call s:DefineInlineMarkup('InterpretedTextOrHyperlinkReference', '`', '`', '`_\{0,2}')
127call s:DefineInlineMarkup('InlineLiteral', '``', "", '``')
128call s:DefineInlineMarkup('SubstitutionReference', '|', '|', '|_\{0,2}')
129call s:DefineInlineMarkup('InlineInternalTargets', '_`', '`', '`')
130
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200131" Sections are identified through their titles, which are marked up with
132" adornment: "underlines" below the title text, or underlines and matching
133" "overlines" above the title. An underline/overline is a single repeated
134" punctuation character that begins in column 1 and forms a line extending at
135" least as far as the right edge of the title text.
136"
137" It is difficult to count characters in a regex, but we at least special-case
138" the case where the title has at least three characters to require the
139" adornment to have at least three characters as well, in order to handle
140" properly the case of a literal block:
141"
142" this is the end of a paragraph
143" ::
144" this is a literal block
145syn match rstSections "\v^%(([=`:.'"~^_*+#-])\1+\n)?.{1,2}\n([=`:.'"~^_*+#-])\2+$"
146 \ contains=@Spell
147syn match rstSections "\v^%(([=`:.'"~^_*+#-])\1{2,}\n)?.{3,}\n([=`:.'"~^_*+#-])\2{2,}$"
148 \ contains=@Spell
Bram Moolenaar345efa02016-01-15 20:57:49 +0100149
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000150" TODO: Cant remember why these two cant be defined like the ones above.
151execute 'syn match rstFootnoteReference contains=@NoSpell' .
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200152 \ ' +\%(\s\|^\)\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]_+'
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000153
154execute 'syn match rstCitationReference contains=@NoSpell' .
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200155 \ ' +\%(\s\|^\)\[' . s:ReferenceName . '\]_\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000156
157execute 'syn match rstHyperlinkReference' .
Bram Moolenaar25394022007-05-10 19:06:20 +0000158 \ ' /\<' . s:ReferenceName . '__\=\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)/'
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000159
160syn match rstStandaloneHyperlink contains=@NoSpell
161 \ "\<\%(\%(\%(https\=\|file\|ftp\|gopher\)://\|\%(mailto\|news\):\)[^[:space:]'\"<>]\+\|www[[:alnum:]_-]*\.[[:alnum:]_-]\+\.[^[:space:]'\"<>]\+\)[[:alnum:]/]"
162
Bram Moolenaar97d62492012-11-15 21:28:22 +0100163syn region rstCodeBlock contained matchgroup=rstDirective
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200164 \ start=+\%(sourcecode\|code\%(-block\)\=\)::\s\+.*\_s*\n\ze\z(\s\+\)+
Bram Moolenaar97d62492012-11-15 21:28:22 +0100165 \ skip=+^$+
Bram Moolenaar7b61a542014-08-23 15:31:19 +0200166 \ end=+^\z1\@!+
Bram Moolenaar97d62492012-11-15 21:28:22 +0100167 \ contains=@NoSpell
168syn cluster rstDirectives add=rstCodeBlock
169
170if !exists('g:rst_syntax_code_list')
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200171 " A mapping from a Vim filetype to a list of alias patterns (pattern
172 " branches to be specific, see ':help /pattern'). E.g. given:
173 "
174 " let g:rst_syntax_code_list = {
175 " \ 'cpp': ['cpp', 'c++'],
176 " \ }
177 "
178 " then the respective contents of the following two rST directives:
179 "
180 " .. code:: cpp
181 "
182 " auto i = 42;
183 "
184 " .. code:: C++
185 "
186 " auto i = 42;
187 "
188 " will both be highlighted as C++ code. As shown by the latter block
189 " pattern matching will be case-insensitive.
190 let g:rst_syntax_code_list = {
191 \ 'vim': ['vim'],
192 \ 'java': ['java'],
193 \ 'cpp': ['cpp', 'c++'],
194 \ 'lisp': ['lisp'],
195 \ 'php': ['php'],
196 \ 'python': ['python'],
197 \ 'perl': ['perl'],
198 \ 'sh': ['sh'],
199 \ }
200elseif type(g:rst_syntax_code_list) == type([])
201 " backward compatibility with former list format
202 let s:old_spec = g:rst_syntax_code_list
203 let g:rst_syntax_code_list = {}
204 for s:elem in s:old_spec
205 let g:rst_syntax_code_list[s:elem] = [s:elem]
206 endfor
Bram Moolenaar97d62492012-11-15 21:28:22 +0100207endif
208
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200209for s:filetype in keys(g:rst_syntax_code_list)
Bram Moolenaar97d62492012-11-15 21:28:22 +0100210 unlet! b:current_syntax
Bram Moolenaar7d76c802014-10-15 22:51:52 +0200211 " guard against setting 'isk' option which might cause problems (issue #108)
212 let prior_isk = &l:iskeyword
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200213 let s:alias_pattern = ''
214 \.'\%('
215 \.join(g:rst_syntax_code_list[s:filetype], '\|')
216 \.'\)'
217
218 exe 'syn include @rst'.s:filetype.' syntax/'.s:filetype.'.vim'
219 exe 'syn region rstDirective'.s:filetype
220 \.' matchgroup=rstDirective fold'
221 \.' start="\c\%(sourcecode\|code\%(-block\)\=\)::\s\+'.s:alias_pattern.'\_s*\n\ze\z(\s\+\)"'
Bram Moolenaara02a5512016-06-17 12:48:11 +0200222 \.' skip=#^$#'
223 \.' end=#^\z1\@!#'
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200224 \.' contains=@NoSpell,@rst'.s:filetype
225 exe 'syn cluster rstDirectives add=rstDirective'.s:filetype
226
Bram Moolenaar7d76c802014-10-15 22:51:52 +0200227 " reset 'isk' setting, if it has been changed
228 if &l:iskeyword !=# prior_isk
229 let &l:iskeyword = prior_isk
230 endif
231 unlet! prior_isk
Bram Moolenaar97d62492012-11-15 21:28:22 +0100232endfor
233
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200234" Enable top level spell checking
235syntax spell toplevel
236
Bram Moolenaar203d04d2013-06-06 21:36:40 +0200237" TODO: Use better syncing.
238syn sync minlines=50 linebreaks=2
239
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000240hi def link rstTodo Todo
241hi def link rstComment Comment
Bram Moolenaar9b451252012-08-15 17:43:31 +0200242hi def link rstSections Title
243hi def link rstTransition rstSections
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000244hi def link rstLiteralBlock String
245hi def link rstQuotedLiteralBlock String
246hi def link rstDoctestBlock PreProc
247hi def link rstTableLines rstDelimiter
248hi def link rstSimpleTableLines rstTableLines
249hi def link rstExplicitMarkup rstDirective
250hi def link rstDirective Keyword
251hi def link rstFootnote String
252hi def link rstCitation String
253hi def link rstHyperlinkTarget String
254hi def link rstExDirective String
255hi def link rstSubstitutionDefinition rstDirective
256hi def link rstDelimiter Delimiter
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000257hi def link rstInterpretedTextOrHyperlinkReference Identifier
258hi def link rstInlineLiteral String
259hi def link rstSubstitutionReference PreProc
260hi def link rstInlineInternalTargets Identifier
261hi def link rstFootnoteReference Identifier
262hi def link rstCitationReference Identifier
263hi def link rstHyperLinkReference Identifier
264hi def link rstStandaloneHyperlink Identifier
Bram Moolenaar97d62492012-11-15 21:28:22 +0100265hi def link rstCodeBlock String
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200266if exists('g:rst_use_emphasis_colors')
267 " TODO: Less arbitrary color selection
268 hi def rstEmphasis ctermfg=13 term=italic cterm=italic gui=italic
269 hi def rstStrongEmphasis ctermfg=1 term=bold cterm=bold gui=bold
270else
271 hi def rstEmphasis term=italic cterm=italic gui=italic
272 hi def rstStrongEmphasis term=bold cterm=bold gui=bold
273endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275let b:current_syntax = "rst"
276
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000277let &cpo = s:cpo_save
278unlet s:cpo_save