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