vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

rst.vim (2137B)


      1 " Vim indent file
      2 " 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
      7 " 2023 Aug 28 by Vim Project (undo_indent)
      8 " 2025 Oct 13 by Vim project: preserve indentation #18566
      9 
     10 if exists("b:did_indent")
     11   finish
     12 endif
     13 let b:did_indent = 1
     14 
     15 " Save and modify cpoptions
     16 let s:save_cpo = &cpo
     17 set cpo&vim
     18 
     19 setlocal indentexpr=GetRSTIndent()
     20 setlocal indentkeys=!^F,o,O
     21 setlocal nosmartindent
     22 
     23 let b:undo_indent = "setlocal indentexpr< indentkeys< smartindent<"
     24 
     25 if exists("*GetRSTIndent")
     26   finish
     27 endif
     28 
     29 let s:itemization_pattern = '^\s*[-*+]\s'
     30 let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+'
     31 let s:note_pattern = '^\.\. '
     32 
     33 function! s:get_paragraph_start()
     34     let paragraph_mark_start = getpos("'{")[1]
     35     return getline(paragraph_mark_start) =~
     36         \ '\S' ? paragraph_mark_start : paragraph_mark_start + 1
     37 endfunction
     38 
     39 function GetRSTIndent()
     40   let lnum = prevnonblank(v:lnum - 1)
     41   if lnum == 0
     42     return 0
     43   endif
     44 
     45   let ind = indent(lnum)
     46   let line = getline(lnum)
     47 
     48   let psnum = s:get_paragraph_start()
     49   if psnum != 0
     50       if getline(psnum) =~ s:note_pattern
     51           let ind = max([3, ind])
     52       endif
     53   endif
     54 
     55   if line =~ s:itemization_pattern
     56     let ind += 2
     57   elseif line =~ s:enumeration_pattern
     58     let ind += matchend(line, s:enumeration_pattern)
     59   endif
     60 
     61   let line = getline(v:lnum - 1)
     62 
     63   " Indent :FIELD: lines.  Don’t match if there is no text after the field or
     64   " if the text ends with a sent-ender.
     65    if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$'
     66      return matchend(line, '^:.\{-1,}:\s\+')
     67    endif
     68 
     69   if line =~ '^\s*$'
     70     execute lnum
     71     call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW')
     72     let line = getline('.')
     73     if line =~ s:itemization_pattern
     74       let ind -= 2
     75     elseif line =~ s:enumeration_pattern
     76       let ind -= matchend(line, s:enumeration_pattern)
     77     elseif line =~ '^\s*\.\.'
     78       let ind -= 3
     79     endif
     80   endif
     81 
     82   return ind
     83 endfunction
     84 
     85 " Restore 'cpoptions'
     86 let &cpo = s:save_cpo
     87 unlet s:save_cpo