xref: /vim-8.2.3635/runtime/syntax/rst.vim (revision 8ee2d36e)
1" Vim syntax file
2" Language: reStructuredText documentation format
3" Maintainer: Marshall Ward <[email protected]>
4" Previous Maintainer: Nikolai Weibull <[email protected]>
5" Website: https://github.com/marshallward/vim-restructuredtext
6" Latest Revision: 2016-08-18
7
8if exists("b:current_syntax")
9  finish
10endif
11
12let s:cpo_save = &cpo
13set cpo&vim
14
15syn case ignore
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*\.\.\_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,rstLiteralBlock'
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  call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '’', '’')
108  " TODO: Additional Unicode Pd, Po, Pi, Pf, Ps characters
109
110  call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\%(^\|\s\|[/:]\)', '')
111
112  execute 'syn match rst' . a:name .
113        \ ' +\%(^\|\s\|[''"([{</:]\)\zs' . a:start .
114        \ '[^[:space:]' . a:start[strlen(a:start) - 1] . ']'
115        \ a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
116
117  execute 'hi def link rst' . a:name . 'Delimiter' . ' rst' . a:name
118endfunction
119
120call s:DefineInlineMarkup('Emphasis', '\*', '\*', '\*')
121call s:DefineInlineMarkup('StrongEmphasis', '\*\*', '\*', '\*\*')
122call s:DefineInlineMarkup('InterpretedTextOrHyperlinkReference', '`', '`', '`_\{0,2}')
123call s:DefineInlineMarkup('InlineLiteral', '``', "", '``')
124call s:DefineInlineMarkup('SubstitutionReference', '|', '|', '|_\{0,2}')
125call s:DefineInlineMarkup('InlineInternalTargets', '_`', '`', '`')
126
127syn match   rstSections "^\%(\([=`:.'"~^_*+#-]\)\1\+\n\)\=.\+\n\([=`:.'"~^_*+#-]\)\2\+$"
128
129" TODO: Can’t remember why these two can’t be defined like the ones above.
130execute 'syn match rstFootnoteReference contains=@NoSpell' .
131      \ ' +\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]_+'
132
133execute 'syn match rstCitationReference contains=@NoSpell' .
134      \ ' +\[' . s:ReferenceName . '\]_\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
135
136execute 'syn match rstHyperlinkReference' .
137      \ ' /\<' . s:ReferenceName . '__\=\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)/'
138
139syn match   rstStandaloneHyperlink  contains=@NoSpell
140      \ "\<\%(\%(\%(https\=\|file\|ftp\|gopher\)://\|\%(mailto\|news\):\)[^[:space:]'\"<>]\+\|www[[:alnum:]_-]*\.[[:alnum:]_-]\+\.[^[:space:]'\"<>]\+\)[[:alnum:]/]"
141
142syn region rstCodeBlock contained matchgroup=rstDirective
143      \ start=+\%(sourcecode\|code\%(-block\)\=\)::\s\+\w*\_s*\n\ze\z(\s\+\)+
144      \ skip=+^$+
145      \ end=+^\z1\@!+
146      \ contains=@NoSpell
147syn cluster rstDirectives add=rstCodeBlock
148
149if !exists('g:rst_syntax_code_list')
150    let g:rst_syntax_code_list = ['vim', 'java', 'cpp', 'lisp', 'php',
151                                \ 'python', 'perl', 'sh']
152endif
153
154for code in g:rst_syntax_code_list
155    unlet! b:current_syntax
156    " guard against setting 'isk' option which might cause problems (issue #108)
157    let prior_isk = &l:iskeyword
158    exe 'syn include @rst'.code.' syntax/'.code.'.vim'
159    exe 'syn region rstDirective'.code.' matchgroup=rstDirective fold'
160                \.' start=#\%(sourcecode\|code\%(-block\)\=\)::\s\+'.code.'\_s*\n\ze\z(\s\+\)#'
161                \.' skip=#^$#'
162                \.' end=#^\z1\@!#'
163                \.' contains=@NoSpell,@rst'.code
164    exe 'syn cluster rstDirectives add=rstDirective'.code
165    " reset 'isk' setting, if it has been changed
166    if &l:iskeyword !=# prior_isk
167        let &l:iskeyword = prior_isk
168    endif
169    unlet! prior_isk
170endfor
171
172" TODO: Use better syncing.
173syn sync minlines=50 linebreaks=2
174
175hi def link rstTodo                         Todo
176hi def link rstComment                      Comment
177hi def link rstSections                     Title
178hi def link rstTransition                   rstSections
179hi def link rstLiteralBlock                 String
180hi def link rstQuotedLiteralBlock           String
181hi def link rstDoctestBlock                 PreProc
182hi def link rstTableLines                   rstDelimiter
183hi def link rstSimpleTableLines             rstTableLines
184hi def link rstExplicitMarkup               rstDirective
185hi def link rstDirective                    Keyword
186hi def link rstFootnote                     String
187hi def link rstCitation                     String
188hi def link rstHyperlinkTarget              String
189hi def link rstExDirective                  String
190hi def link rstSubstitutionDefinition       rstDirective
191hi def link rstDelimiter                    Delimiter
192hi def rstEmphasis ctermfg=13 term=italic cterm=italic gui=italic
193hi def rstStrongEmphasis ctermfg=1 term=bold cterm=bold gui=bold
194hi def link rstInterpretedTextOrHyperlinkReference  Identifier
195hi def link rstInlineLiteral                String
196hi def link rstSubstitutionReference        PreProc
197hi def link rstInlineInternalTargets        Identifier
198hi def link rstFootnoteReference            Identifier
199hi def link rstCitationReference            Identifier
200hi def link rstHyperLinkReference           Identifier
201hi def link rstStandaloneHyperlink          Identifier
202hi def link rstCodeBlock                    String
203
204let b:current_syntax = "rst"
205
206let &cpo = s:cpo_save
207unlet s:cpo_save
208