Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 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 '\\\@!'? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8 | |
| 9 | " Only load this indent file when no other was loaded. |
| 10 | if exists("b:did_indent") |
| 11 | finish |
| 12 | endif |
| 13 | |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 14 | runtime! indent/sh.vim " will set b:did_indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
| 16 | setlocal indentexpr=GetConfigIndent() |
| 17 | setlocal indentkeys=!^F,o,O,=then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 18 | setlocal nosmartindent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 20 | let b:undo_indent = "setl inde< indk< si<" |
| 21 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 6c391a7 | 2021-09-09 21:55:11 +0200 | [diff] [blame] | 68 | " if previous line had an unmatched closing parentheses, |
| 69 | " indent to the matching opening parentheses |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 70 | if line =~ '[^(]\+\\\@<!)$' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 899dddf | 2006-03-26 21:06:50 +0000 | [diff] [blame] | 78 | if line =~ '[^[]\+]$' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | call search(']', 'bW') |
| 80 | let lnum = searchpair('\[', '', ']', 'bWn') |
| 81 | let ind = indent(lnum) |
| 82 | endif |
| 83 | |
| 84 | return ind |
| 85 | endfunction |