neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

config.vim (2222B)


      1 " Vim indent file
      2 " Language:		Autoconf configure.{ac,in} file
      3 " Maintainer:		Doug Kearns <dougkearns@gmail.com>
      4 " Previous Maintainer:	Nikolai Weibull <now@bitwi.se>
      5 " Last Change:		24 Sep 2021
      6 
      7 " TODO: how about nested [()]'s in one line what's wrong with '\\\@!'?
      8 
      9 " Only load this indent file when no other was loaded.
     10 if exists("b:did_indent")
     11  finish
     12 endif
     13 
     14 runtime! indent/sh.vim          " will set b:did_indent
     15 
     16 setlocal indentexpr=GetConfigIndent()
     17 setlocal indentkeys=!^F,o,O,=then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
     18 setlocal nosmartindent
     19 
     20 let b:undo_indent = "setl inde< indk< si<"
     21 
     22 " Only define the function once.
     23 if exists("*GetConfigIndent")
     24  finish
     25 endif
     26 
     27 " get the offset (indent) of the end of the match of 'regexp' in 'line'
     28 function s:GetOffsetOf(line, regexp)
     29  let end = matchend(a:line, a:regexp)
     30  let width = 0
     31  let i = 0
     32  while i < end
     33    if a:line[i] != "\t"
     34      let width = width + 1
     35    else
     36      let width = width + &ts - (width % &ts)
     37    endif
     38    let i = i + 1
     39  endwhile
     40  return width
     41 endfunction
     42 
     43 function GetConfigIndent()
     44  " Find a non-blank line above the current line.
     45  let lnum = prevnonblank(v:lnum - 1)
     46 
     47  " Hit the start of the file, use zero indent.
     48  if lnum == 0
     49    return 0
     50  endif
     51 
     52  " where to put this
     53  let ind = GetShIndent()
     54  let line = getline(lnum)
     55 
     56  " if previous line has unmatched, unescaped opening parentheses,
     57  " indent to its position. TODO: not failsafe if multiple ('s
     58  if line =~ '\\\@<!([^)]*$'
     59    let ind = s:GetOffsetOf(line, '\\\@!(')
     60  endif
     61 
     62  " if previous line has unmatched opening bracket,
     63  " indent to its position. TODO: same as above
     64  if line =~ '\[[^]]*$'
     65    let ind = s:GetOffsetOf(line, '\[')
     66  endif
     67 
     68  " if previous line had an unmatched closing parentheses,
     69  " indent to the matching opening parentheses
     70  if line =~ '[^(]\+\\\@<!)$'
     71    call search(')', 'bW')
     72    let lnum = searchpair('\\\@<!(', '', ')', 'bWn')
     73    let ind = indent(lnum)
     74  endif
     75 
     76  " if previous line had an unmatched closing bracket,
     77  " indent to the matching opening bracket
     78  if line =~ '[^[]\+]$'
     79    call search(']', 'bW')
     80    let lnum = searchpair('\[', '', ']', 'bWn')
     81    let ind = indent(lnum)
     82  endif
     83 
     84  return ind
     85 endfunction