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