3 " Maintainer: Todd Zullinger <tmz@pobox.com>
4 " Last Change: 2009 Aug 19
7 if exists("b:did_ftplugin")
10 let b:did_ftplugin = 1
12 if !exists("no_plugin_maps") && !exists("no_puppet_maps")
13 if !hasmapto("<Plug>AlignRange")
14 map <buffer> <LocalLeader>= <Plug>AlignRange
18 noremap <buffer> <unique> <script> <Plug>AlignArrows :call <SID>AlignArrows()<CR>
19 noremap <buffer> <unique> <script> <Plug>AlignRange :call <SID>AlignRange()<CR>
21 iabbrev => =><C-R>=<SID>AlignArrows('=>')<CR>
22 iabbrev +> +><C-R>=<SID>AlignArrows('+>')<CR>
24 if exists('*s:AlignArrows')
28 let s:arrow_re = '[=+]>'
29 let s:selector_re = '[=+]>\s*\$.*\s*?\s*{\s*$'
31 function! s:AlignArrows(op)
32 let cursor_pos = getpos('.')
34 let line = getline(lnum)
38 let pos = stridx(line, a:op)
43 let pline = getline(pnum)
44 if pline !~ s:arrow_re || pline =~ s:selector_re
52 let cline = getline(cnum)
53 if cline !~ s:arrow_re ||
54 \ (indent(cnum) != indent(cnum+1) && getline(cnum+1) !~ '\s*}')
60 call s:AlignSection(start, end)
61 let cursor_pos[2] = stridx(getline('.'), a:op) + strlen(a:op) + 1
62 call setpos('.', cursor_pos)
66 function! s:AlignRange() range
67 call s:AlignSection(a:firstline, a:lastline)
70 " AlignSection and AlignLine are from the vim wiki:
71 " http://vim.wikia.com/wiki/Regex-based_text_alignment
72 function! s:AlignSection(start, end)
76 let section = getline(a:start, a:end)
78 let pos = match(line, ' *'.sep)
83 call map(section, 's:AlignLine(v:val, sep, maxpos, extra)')
84 call setline(a:start, section)
87 function! s:AlignLine(line, sep, maxpos, extra)
88 let m = matchlist(a:line, '\(.\{-}\) \{-}\('.a:sep.'.*\)')
92 let spaces = repeat(' ', a:maxpos - strlen(m[1]) + a:extra)
93 return m[1] . spaces . m[2]