0
0
mirror of https://github.com/vim/vim.git synced 2025-07-04 23:07:33 -04:00
vim/runtime/indent/vim.vim

103 lines
2.9 KiB
VimL
Raw Normal View History

2004-06-13 20:20:40 +00:00
" Vim indent file
" Language: Vim script
" Maintainer: Bram Moolenaar <Bram@vim.org>
2016-01-24 17:56:50 +01:00
" Last Change: 2016 Jan 24
2004-06-13 20:20:40 +00:00
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal indentexpr=GetVimIndent()
setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\
2012-06-01 22:38:45 +02:00
let b:undo_indent = "setl indentkeys< indentexpr<"
2004-06-13 20:20:40 +00:00
" Only define the function once.
if exists("*GetVimIndent")
finish
endif
let s:keepcpo= &cpo
set cpo&vim
2004-06-13 20:20:40 +00:00
function GetVimIndent()
2012-08-15 17:43:31 +02:00
let ignorecase_save = &ignorecase
try
let &ignorecase = 0
return GetVimIndentIntern()
finally
let &ignorecase = ignorecase_save
endtry
endfunc
function GetVimIndentIntern()
2004-06-13 20:20:40 +00:00
" Find a non-blank line above the current line.
let lnum = prevnonblank(v:lnum - 1)
" If the current line doesn't start with '\' and below a line that starts
" with '\', use the indent of the line above it.
2014-09-19 22:38:48 +02:00
let cur_text = getline(v:lnum)
if cur_text !~ '^\s*\\'
2004-06-13 20:20:40 +00:00
while lnum > 0 && getline(lnum) =~ '^\s*\\'
let lnum = lnum - 1
endwhile
endif
" At the start of the file use zero indent.
if lnum == 0
return 0
endif
2014-09-19 22:38:48 +02:00
let prev_text = getline(lnum)
2004-06-13 20:20:40 +00:00
" Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
" and :else. Add it three times for a line that starts with '\' after
2004-09-02 19:12:26 +00:00
" a line that doesn't (or g:vim_indent_cont if it exists).
2004-06-13 20:20:40 +00:00
let ind = indent(lnum)
2014-09-19 22:38:48 +02:00
if cur_text =~ '^\s*\\' && v:lnum > 1 && prev_text !~ '^\s*\\'
2004-09-02 19:12:26 +00:00
if exists("g:vim_indent_cont")
let ind = ind + g:vim_indent_cont
else
2016-01-24 17:56:50 +01:00
let ind = ind + shiftwidth() * 3
2004-09-02 19:12:26 +00:00
endif
2014-09-19 22:38:48 +02:00
elseif prev_text =~ '^\s*aug\%[roup]' && prev_text !~ '^\s*aug\%[roup]\s*!\=\s\+END'
2016-01-24 17:56:50 +01:00
let ind = ind + shiftwidth()
else
2014-09-19 22:38:48 +02:00
" A line starting with :au does not increment/decrement indent.
if prev_text !~ '^\s*au\%[tocmd]'
let i = match(prev_text, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
if i >= 0
2016-01-24 17:56:50 +01:00
let ind += shiftwidth()
2014-09-19 22:38:48 +02:00
if strpart(prev_text, i, 1) == '|' && has('syntax_items')
\ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
2016-01-24 17:56:50 +01:00
let ind -= shiftwidth()
2014-09-19 22:38:48 +02:00
endif
endif
endif
2004-06-13 20:20:40 +00:00
endif
" If the previous line contains an "end" after a pipe, but not in an ":au"
2005-06-16 21:59:56 +00:00
" command. And not when there is a backslash before the pipe.
2005-07-06 22:35:45 +00:00
" And when syntax HL is enabled avoid a match inside a string.
2014-09-19 22:38:48 +02:00
let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
2005-07-06 22:35:45 +00:00
if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
2016-01-24 17:56:50 +01:00
let ind = ind - shiftwidth()
2005-07-06 22:35:45 +00:00
endif
2004-06-13 20:20:40 +00:00
endif
" Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
" :endfun, :else and :augroup END.
2014-12-14 01:27:49 +01:00
if cur_text =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s*!\=\s\+[eE][nN][dD]\)'
2016-01-24 17:56:50 +01:00
let ind = ind - shiftwidth()
2004-06-13 20:20:40 +00:00
endif
return ind
endfunction
let &cpo = s:keepcpo
unlet s:keepcpo
2004-06-13 20:20:40 +00:00
" vim:sw=2