blob: e3c10865a680f90e2d5cb4392d1792f84114be90 [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
dkearns0382f052023-08-29 05:32:59 +10007" 2023 Aug 28 by Vim Project (undo_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
Bram Moolenaar071d4272004-06-13 20:20:40 +00009if exists("b:did_indent")
10 finish
11endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012let b:did_indent = 1
13
14setlocal indentexpr=GetRSTIndent()
Bram Moolenaar42eeac32005-06-29 22:40:58 +000015setlocal indentkeys=!^F,o,O
Bram Moolenaar335437b2007-05-10 18:32:52 +000016setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
dkearns0382f052023-08-29 05:32:59 +100018let b:undo_indent = "setlocal indentexpr< indentkeys< smartindent<"
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020if exists("*GetRSTIndent")
21 finish
22endif
23
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020024let s:itemization_pattern = '^\s*[-*+]\s'
25let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+'
Bram Moolenaard1caa942020-04-10 22:10:56 +020026let s:note_pattern = '^\.\. '
27
28function! s:get_paragraph_start()
29 let paragraph_mark_start = getpos("'{")[1]
30 return getline(paragraph_mark_start) =~ '\S' ? paragraph_mark_start : paragraph_mark_start + 1
31endfunction
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033function GetRSTIndent()
34 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 if lnum == 0
36 return 0
37 endif
38
39 let ind = indent(lnum)
40 let line = getline(lnum)
41
Bram Moolenaard1caa942020-04-10 22:10:56 +020042 let psnum = s:get_paragraph_start()
43 if psnum != 0
44 if getline(psnum) =~ s:note_pattern
45 let ind = 3
46 endif
47 endif
48
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020049 if line =~ s:itemization_pattern
50 let ind += 2
51 elseif line =~ s:enumeration_pattern
52 let ind += matchend(line, s:enumeration_pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 endif
54
55 let line = getline(v:lnum - 1)
56
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020057 " Indent :FIELD: lines. Don’t match if there is no text after the field or
58 " if the text ends with a sent-ender.
59 if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$'
60 return matchend(line, '^:.\{-1,}:\s\+')
61 endif
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 if line =~ '^\s*$'
64 execute lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020065 call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW')
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 let line = getline('.')
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020067 if line =~ s:itemization_pattern
68 let ind -= 2
69 elseif line =~ s:enumeration_pattern
70 let ind -= matchend(line, s:enumeration_pattern)
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 elseif line =~ '^\s*\.\.'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020072 let ind -= 3
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 endif
74 endif
75
76 return ind
77endfunction