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 | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 4 | " Last Change: 2021 Jan 06 |
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() |
Bram Moolenaar | d58a3bf | 2020-09-28 21:48:16 +0200 | [diff] [blame] | 13 | setlocal indentkeys+==end,=},=else,=cat,=finall,=END,0\\,0=\"\\\ |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 14 | setlocal indentkeys-=0# |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
Bram Moolenaar | c873442 | 2012-06-01 22:38:45 +0200 | [diff] [blame] | 16 | let b:undo_indent = "setl indentkeys< indentexpr<" |
| 17 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | " Only define the function once. |
| 19 | if exists("*GetVimIndent") |
| 20 | finish |
| 21 | endif |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 22 | let s:keepcpo= &cpo |
| 23 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | |
| 25 | function GetVimIndent() |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 26 | let ignorecase_save = &ignorecase |
| 27 | try |
| 28 | let &ignorecase = 0 |
| 29 | return GetVimIndentIntern() |
| 30 | finally |
| 31 | let &ignorecase = ignorecase_save |
| 32 | endtry |
| 33 | endfunc |
| 34 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 35 | let s:lineContPat = '^\s*\(\\\|"\\ \)' |
| 36 | |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 37 | function GetVimIndentIntern() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | " Find a non-blank line above the current line. |
| 39 | let lnum = prevnonblank(v:lnum - 1) |
| 40 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 41 | " If the current line doesn't start with '\' or '"\ ' and below a line that |
| 42 | " starts with '\' or '"\ ', use the indent of the line above it. |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 43 | let cur_text = getline(v:lnum) |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 44 | if cur_text !~ s:lineContPat |
| 45 | while lnum > 0 && getline(lnum) =~ s:lineContPat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | let lnum = lnum - 1 |
| 47 | endwhile |
| 48 | endif |
| 49 | |
| 50 | " At the start of the file use zero indent. |
| 51 | if lnum == 0 |
| 52 | return 0 |
| 53 | endif |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 54 | let prev_text = getline(lnum) |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 55 | let found_cont = 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | |
| 57 | " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 58 | " and :else. Add it three times for a line that starts with '\' or '"\ ' |
| 59 | " after 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] | 60 | let ind = indent(lnum) |
Bram Moolenaar | 1ff14ba | 2019-11-02 14:09:23 +0100 | [diff] [blame] | 61 | |
| 62 | " In heredoc indenting works completely differently. |
| 63 | if has('syntax_items') |
| 64 | let syn_here = synIDattr(synID(v:lnum, 1, 1), "name") |
| 65 | if syn_here =~ 'vimLetHereDocStop' |
| 66 | " End of heredoc: use indent of matching start line |
| 67 | let lnum = v:lnum - 1 |
| 68 | while lnum > 0 |
| 69 | if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' |
| 70 | return indent(lnum) |
| 71 | endif |
| 72 | let lnum -= 1 |
| 73 | endwhile |
| 74 | return 0 |
| 75 | endif |
| 76 | if syn_here =~ 'vimLetHereDoc' |
| 77 | if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' |
| 78 | " First line in heredoc: increase indent |
| 79 | return ind + shiftwidth() |
| 80 | endif |
| 81 | " Heredoc continues: no change in indent |
| 82 | return ind |
| 83 | endif |
| 84 | endif |
| 85 | |
Bram Moolenaar | 67f8ab8 | 2018-09-11 22:37:29 +0200 | [diff] [blame] | 86 | if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 87 | let found_cont = 1 |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 88 | if exists("g:vim_indent_cont") |
| 89 | let ind = ind + g:vim_indent_cont |
| 90 | else |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 91 | let ind = ind + shiftwidth() * 3 |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 92 | endif |
Bram Moolenaar | e18dbe8 | 2016-07-02 21:42:23 +0200 | [diff] [blame] | 93 | elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 94 | let ind = ind + shiftwidth() |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 95 | else |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 96 | " A line starting with :au does not increment/decrement indent. |
| 97 | if prev_text !~ '^\s*au\%[tocmd]' |
Bram Moolenaar | d58a3bf | 2020-09-28 21:48:16 +0200 | [diff] [blame] | 98 | let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|fu\%[nction]\|def\|el\%[seif]\)\>\)') |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 99 | if i >= 0 |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 100 | let ind += shiftwidth() |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 101 | if strpart(prev_text, i, 1) == '|' && has('syntax_items') |
| 102 | \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 103 | let ind -= shiftwidth() |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 104 | endif |
Bram Moolenaar | cab49df | 2011-03-22 17:40:10 +0100 | [diff] [blame] | 105 | endif |
| 106 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | endif |
| 108 | |
| 109 | " 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] | 110 | " command. And not when there is a backslash before the pipe. |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 111 | " And when syntax HL is enabled avoid a match inside a string. |
Bram Moolenaar | 91e15e1 | 2014-09-19 22:38:48 +0200 | [diff] [blame] | 112 | let i = match(prev_text, '[^\\]|\s*\(ene\@!\)') |
| 113 | if i > 0 && prev_text !~ '^\s*au\%[tocmd]' |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 114 | if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 115 | let ind = ind - shiftwidth() |
Bram Moolenaar | dc27ac1 | 2005-07-06 22:35:45 +0000 | [diff] [blame] | 116 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | endif |
| 118 | |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 119 | " For a line starting with "}" find the matching "{". If it is at the start |
| 120 | " of the line align with it, probably end of a block. |
| 121 | " Use the mapped "%" from matchit to find the match, otherwise we may match |
| 122 | " a { inside a comment or string. |
| 123 | if cur_text =~ '^\s*}' |
| 124 | if maparg('%') != '' |
| 125 | exe v:lnum |
| 126 | silent! normal % |
| 127 | if line('.') < v:lnum && getline('.') =~ '^\s*{' |
| 128 | let ind = indent('.') |
| 129 | endif |
| 130 | else |
| 131 | " todo: use searchpair() to find a match |
| 132 | endif |
| 133 | endif |
| 134 | |
| 135 | " Below a line starting with "}" find the matching "{". If it is at the |
| 136 | " end of the line we must be below the end of a dictionary. |
| 137 | if prev_text =~ '^\s*}' |
| 138 | if maparg('%') != '' |
| 139 | exe lnum |
| 140 | silent! normal % |
| 141 | if line('.') == lnum || getline('.') !~ '^\s*{' |
| 142 | let ind = ind - shiftwidth() |
| 143 | endif |
| 144 | else |
| 145 | " todo: use searchpair() to find a match |
| 146 | endif |
| 147 | endif |
| 148 | |
| 149 | " Below a line starting with "]" we must be below the end of a list. |
| 150 | if prev_text =~ '^\s*]' |
| 151 | let ind = ind - shiftwidth() |
| 152 | endif |
| 153 | |
| 154 | " A line ending in "{"/"[} is most likely the start of a dict/list literal, |
| 155 | " indent the next line more. Not for a continuation line. |
| 156 | if prev_text =~ '[{[]\s*$' && !found_cont |
| 157 | let ind = ind + shiftwidth() |
| 158 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | |
| 160 | " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 161 | " :endfun, :enddef, :else and :augroup END. |
Bram Moolenaar | 82be484 | 2021-01-11 19:40:15 +0100 | [diff] [blame] | 162 | if cur_text =~ '^\s*\(ene\@!\|cat\|finall\|el\|aug\%[roup]\s\+[eE][nN][dD]\)' |
Bram Moolenaar | 705ada1 | 2016-01-24 17:56:50 +0100 | [diff] [blame] | 163 | let ind = ind - shiftwidth() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | endif |
| 165 | |
| 166 | return ind |
| 167 | endfunction |
| 168 | |
Bram Moolenaar | 8e52a59 | 2012-05-18 21:49:28 +0200 | [diff] [blame] | 169 | let &cpo = s:keepcpo |
| 170 | unlet s:keepcpo |
| 171 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | " vim:sw=2 |