Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Vim script |
| 3 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 4 | " Last Change: 2012 Aug 02 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | |
| 6 | " Only load this indent file when no other was loaded. |
| 7 | if exists("b:did_indent") |
| 8 | finish |
| 9 | endif |
| 10 | let b:did_indent = 1 |
| 11 | |
| 12 | setlocal indentexpr=GetVimIndent() |
| 13 | setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\ |
| 14 | |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 15 | let b:undo_indent = "setl indentkeys< indentexpr<" |
| 16 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | " Only define the function once. |
| 18 | if exists("*GetVimIndent") |
| 19 | finish |
| 20 | endif |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 21 | let s:keepcpo= &cpo |
| 22 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
| 24 | function GetVimIndent() |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 25 | let ignorecase_save = &ignorecase |
| 26 | try |
| 27 | let &ignorecase = 0 |
| 28 | return GetVimIndentIntern() |
| 29 | finally |
| 30 | let &ignorecase = ignorecase_save |
| 31 | endtry |
| 32 | endfunc |
| 33 | |
| 34 | function GetVimIndentIntern() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | " Find a non-blank line above the current line. |
| 36 | let lnum = prevnonblank(v:lnum - 1) |
| 37 | |
| 38 | " If the current line doesn't start with '\' and below a line that starts |
| 39 | " with '\', use the indent of the line above it. |
| 40 | if getline(v:lnum) !~ '^\s*\\' |
| 41 | while lnum > 0 && getline(lnum) =~ '^\s*\\' |
| 42 | let lnum = lnum - 1 |
| 43 | endwhile |
| 44 | endif |
| 45 | |
| 46 | " At the start of the file use zero indent. |
| 47 | if lnum == 0 |
| 48 | return 0 |
| 49 | endif |
| 50 | |
| 51 | " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function |
| 52 | " and :else. Add it three times for a line that starts with '\' after |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 53 | " a line that doesn't (or g:vim_indent_cont if it exists). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | let ind = indent(lnum) |
| 55 | if getline(v:lnum) =~ '^\s*\\' && v:lnum > 1 && getline(lnum) !~ '^\s*\\' |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 56 | if exists("g:vim_indent_cont") |
| 57 | let ind = ind + g:vim_indent_cont |
| 58 | else |
| 59 | let ind = ind + &sw * 3 |
| 60 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | elseif getline(lnum) =~ '^\s*aug\%[roup]' && getline(lnum) !~ '^\s*aug\%[roup]\s*!\=\s\+END' |
| 62 | let ind = ind + &sw |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 63 | else |
| 64 | let line = getline(lnum) |
| 65 | let i = match(line, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>') |
| 66 | if i >= 0 |
| 67 | let ind += &sw |
| 68 | if strpart(line, i, 1) == '|' && has('syntax_items') |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 69 | \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$' |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 70 | let ind -= &sw |
| 71 | endif |
| 72 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | endif |
| 74 | |
| 75 | " If the previous line contains an "end" after a pipe, but not in an ":au" |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 76 | " command. And not when there is a backslash before the pipe. |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 77 | " And when syntax HL is enabled avoid a match inside a string. |
| 78 | let line = getline(lnum) |
| 79 | let i = match(line, '[^\\]|\s*\(ene\@!\)') |
| 80 | if i > 0 && line !~ '^\s*au\%[tocmd]' |
| 81 | if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$' |
| 82 | let ind = ind - &sw |
| 83 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | endif |
| 85 | |
| 86 | |
| 87 | " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry, |
| 88 | " :endfun, :else and :augroup END. |
| 89 | if getline(v:lnum) =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s*!\=\s\+END\)' |
| 90 | let ind = ind - &sw |
| 91 | endif |
| 92 | |
| 93 | return ind |
| 94 | endfunction |
| 95 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 96 | let &cpo = s:keepcpo |
| 97 | unlet s:keepcpo |
| 98 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | " vim:sw=2 |