blob: b840b1e391083d2b3f6838d1e628f0285a80acba [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar6e649222021-10-04 21:32:54 +01002" 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 '\\\@!'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
9" Only load this indent file when no other was loaded.
10if exists("b:did_indent")
11 finish
12endif
13
Bram Moolenaar25394022007-05-10 19:06:20 +000014runtime! indent/sh.vim " will set b:did_indent
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16setlocal indentexpr=GetConfigIndent()
17setlocal indentkeys=!^F,o,O,=then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
Bram Moolenaar25394022007-05-10 19:06:20 +000018setlocal nosmartindent
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaar6e649222021-10-04 21:32:54 +010020let b:undo_indent = "setl inde< indk< si<"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022" Only define the function once.
23if exists("*GetConfigIndent")
24 finish
25endif
26
27" get the offset (indent) of the end of the match of 'regexp' in 'line'
28function 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
41endfunction
42
43function 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
Bram Moolenaar6c391a72021-09-09 21:55:11 +020068 " if previous line had an unmatched closing parentheses,
69 " indent to the matching opening parentheses
Bram Moolenaar899dddf2006-03-26 21:06:50 +000070 if line =~ '[^(]\+\\\@<!)$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 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
Bram Moolenaar899dddf2006-03-26 21:06:50 +000078 if line =~ '[^[]\+]$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 call search(']', 'bW')
80 let lnum = searchpair('\[', '', ']', 'bWn')
81 let ind = indent(lnum)
82 endif
83
84 return ind
85endfunction