blob: a31ad8e0809797908d0f188d5e773b12aba9713e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaard1caa942020-04-10 22:10:56 +02002" Vim reST indent file
3" Language: reStructuredText Documentation Format
4" Maintainer: Marshall Ward <marshall.ward@gmail.com>
5" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
6" Latest Revision: 2020-03-31
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008if exists("b:did_indent")
9 finish
10endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011let b:did_indent = 1
12
13setlocal indentexpr=GetRSTIndent()
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014setlocal indentkeys=!^F,o,O
Bram Moolenaar335437b2007-05-10 18:32:52 +000015setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000016
Bram Moolenaar071d4272004-06-13 20:20:40 +000017if exists("*GetRSTIndent")
18 finish
19endif
20
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020021let s:itemization_pattern = '^\s*[-*+]\s'
22let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+'
Bram Moolenaard1caa942020-04-10 22:10:56 +020023let s:note_pattern = '^\.\. '
24
25function! s:get_paragraph_start()
26 let paragraph_mark_start = getpos("'{")[1]
27 return getline(paragraph_mark_start) =~ '\S' ? paragraph_mark_start : paragraph_mark_start + 1
28endfunction
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030function GetRSTIndent()
31 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 if lnum == 0
33 return 0
34 endif
35
36 let ind = indent(lnum)
37 let line = getline(lnum)
38
Bram Moolenaard1caa942020-04-10 22:10:56 +020039 let psnum = s:get_paragraph_start()
40 if psnum != 0
41 if getline(psnum) =~ s:note_pattern
42 let ind = 3
43 endif
44 endif
45
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020046 if line =~ s:itemization_pattern
47 let ind += 2
48 elseif line =~ s:enumeration_pattern
49 let ind += matchend(line, s:enumeration_pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 endif
51
52 let line = getline(v:lnum - 1)
53
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020054 " Indent :FIELD: lines. Don’t match if there is no text after the field or
55 " if the text ends with a sent-ender.
56 if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$'
57 return matchend(line, '^:.\{-1,}:\s\+')
58 endif
59
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 if line =~ '^\s*$'
61 execute lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020062 call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW')
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 let line = getline('.')
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020064 if line =~ s:itemization_pattern
65 let ind -= 2
66 elseif line =~ s:enumeration_pattern
67 let ind -= matchend(line, s:enumeration_pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 elseif line =~ '^\s*\.\.'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020069 let ind -= 3
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 endif
71 endif
72
73 return ind
74endfunction