xref: /vim-8.2.3635/runtime/syntax/rst.vim (revision cf2d8dee)
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: 2016-01-05
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   rstTransition  /^[=`:.'"~^_*+#-]\{4,}\s*$/
17
18syn cluster rstCruft                contains=rstEmphasis,rstStrongEmphasis,
19      \ rstInterpretedText,rstInlineLiteral,rstSubstitutionReference,
20      \ rstInlineInternalTargets,rstFootnoteReference,rstHyperlinkReference
21
22syn region  rstLiteralBlock         matchgroup=rstDelimiter
23      \ start='::\_s*\n\ze\z(\s\+\)' skip='^$' end='^\z1\@!'
24      \ contains=@NoSpell
25
26syn region  rstQuotedLiteralBlock   matchgroup=rstDelimiter
27      \ start="::\_s*\n\ze\z([!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]\)"
28      \ end='^\z1\@!' contains=@NoSpell
29
30syn region  rstDoctestBlock         oneline display matchgroup=rstDelimiter
31      \ start='^>>>\s' end='^$'
32
33syn region  rstTable                transparent start='^\n\s*+[-=+]\+' end='^$'
34      \ contains=rstTableLines,@rstCruft
35syn match   rstTableLines           contained display '|\|+\%(=\+\|-\+\)\='
36
37syn region  rstSimpleTable          transparent
38      \ start='^\n\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
39      \ end='^$'
40      \ contains=rstSimpleTableLines,@rstCruft
41syn match   rstSimpleTableLines     contained display
42      \ '^\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
43syn match   rstSimpleTableLines     contained display
44      \ '^\%(\s*\)\@>\%(\%(-\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(-\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
45
46syn cluster rstDirectives           contains=rstFootnote,rstCitation,
47      \ rstHyperlinkTarget,rstExDirective
48
49syn match   rstExplicitMarkup       '^\s*\.\.\_s'
50      \ nextgroup=@rstDirectives,rstComment,rstSubstitutionDefinition
51
52let s:ReferenceName = '[[:alnum:]]\+\%([_.-][[:alnum:]]\+\)*'
53
54syn keyword     rstTodo             contained FIXME TODO XXX NOTE
55
56execute 'syn region rstComment contained' .
57      \ ' start=/.*/'
58      \ ' end=/^\s\@!/ contains=rstTodo'
59
60execute 'syn region rstFootnote contained matchgroup=rstDirective' .
61      \ ' start=+\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]\_s+' .
62      \ ' skip=+^$+' .
63      \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
64
65execute 'syn region rstCitation contained matchgroup=rstDirective' .
66      \ ' start=+\[' . s:ReferenceName . '\]\_s+' .
67      \ ' skip=+^$+' .
68      \ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
69
70syn region rstHyperlinkTarget contained matchgroup=rstDirective
71      \ start='_\%(_\|[^:\\]*\%(\\.[^:\\]*\)*\):\_s' skip=+^$+ end=+^\s\@!+
72
73syn region rstHyperlinkTarget contained matchgroup=rstDirective
74      \ start='_`[^`\\]*\%(\\.[^`\\]*\)*`:\_s' skip=+^$+ end=+^\s\@!+
75
76syn region rstHyperlinkTarget matchgroup=rstDirective
77      \ start=+^__\_s+ skip=+^$+ end=+^\s\@!+
78
79execute 'syn region rstExDirective contained matchgroup=rstDirective' .
80      \ ' start=+' . s:ReferenceName . '::\_s+' .
81      \ ' skip=+^$+' .
82      \ ' end=+^\s\@!+ contains=@rstCruft,rstLiteralBlock'
83
84execute 'syn match rstSubstitutionDefinition contained' .
85      \ ' /|' . s:ReferenceName . '|\_s\+/ nextgroup=@rstDirectives'
86
87function! s:DefineOneInlineMarkup(name, start, middle, end, char_left, char_right)
88  execute 'syn region rst' . a:name .
89        \ ' start=+' . a:char_left . '\zs' . a:start .
90        \ '\ze[^[:space:]' . a:char_right . a:start[strlen(a:start) - 1] . ']+' .
91        \ a:middle .
92        \ ' end=+\S' . a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
93endfunction
94
95function! s:DefineInlineMarkup(name, start, middle, end)
96  let middle = a:middle != "" ?
97        \ (' skip=+\\\\\|\\' . a:middle . '+') :
98        \ ""
99
100  call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, "'", "'")
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
107  call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\%(^\|\s\|[/:]\)', '')
108
109  execute 'syn match rst' . a:name .
110        \ ' +\%(^\|\s\|[''"([{</:]\)\zs' . a:start .
111        \ '[^[:space:]' . a:start[strlen(a:start) - 1] . ']'
112        \ a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
113
114  execute 'hi def link rst' . a:name . 'Delimiter' . ' rst' . a:name
115endfunction
116
117call s:DefineInlineMarkup('Emphasis', '\*', '\*', '\*')
118call s:DefineInlineMarkup('StrongEmphasis', '\*\*', '\*', '\*\*')
119call s:DefineInlineMarkup('InterpretedTextOrHyperlinkReference', '`', '`', '`_\{0,2}')
120call s:DefineInlineMarkup('InlineLiteral', '``', "", '``')
121call s:DefineInlineMarkup('SubstitutionReference', '|', '|', '|_\{0,2}')
122call s:DefineInlineMarkup('InlineInternalTargets', '_`', '`', '`')
123
124syn match   rstSections "^\%(\([=`:.'"~^_*+#-]\)\1\+\n\)\=.\+\n\([=`:.'"~^_*+#-]\)\2\+$"
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