aboutsummaryrefslogtreecommitdiff
path: root/vim/indent/perl.vim
blob: edb91028cc32da45dc3a5c1cf88f08b948eedec3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
" Custom Vim indent file for Perl5; the stock one didn't suit me.

" Only load this indent file when no other was loaded.
if exists('b:did_indent') || &compatible
  finish
endif
let b:did_indent = 1

" Define indent function
function! GetPerlIndent()

  " Just return 0 if we have no previous line to work from
  let l:pn = prevnonblank(v:lnum - 1)
  if !l:pn
    return 0
  endif

  " Get line properties
  let l:cl = getline(v:lnum)
  let l:ci = indent(l:cn)
  let l:pl = getline(l:pn)
  let l:pi = indent(l:pn)

  " Get value of 'shiftwidth'
  let l:sw = exists('*shiftwidth')
        \ ? shiftwidth()
        \ : &shiftwidth

  " Don't touch comments
  if l:pl =~# '^\s*#'
    return l:pi

  " After opening brace
  elseif l:pl =~# '[{([]\s*$'

    " Closing brace
    if l:cl =~# '^\s*[])}]'
      return l:pi

    " Block content
    else
      return l:pi + l:sw
    endif

  " Closing brace
  elseif l:cl =~# '^\s*[])}]'

    " Reduce indent if possible
    if l:pi >= l:sw
      return l:pi - l:sw
    else
      return 0
    endif

  " After a semicolon, comma, or closing brace
  elseif l:pl =~# '[;,}]\s*$'
    return l:pi - (l:pi % l:sw)

  " Continued line; add half shiftwidth
  elseif l:sw >= 2
    return l:pi + l:sw / 2
  endif

endfunction

" Indent settings
setlocal indentexpr=GetPerlIndent()
setlocal indentkeys+=0},0),0]

" Undo all of the above crap
let b:undo_indent = '|setlocal indentexpr<'
      \ . '|setlocal indentkeys<'
      \ . '|delfunction GetPerlIndent'