" Copyright (c) 2013 Junegunn Choi " " MIT License " " Permission is hereby granted, free of charge, to any person obtaining " a copy of this software and associated documentation files (the " "Software"), to deal in the Software without restriction, including " without limitation the rights to use, copy, modify, merge, publish, " distribute, sublicense, and/or sell copies of the Software, and to " permit persons to whom the Software is furnished to do so, subject to " the following conditions: " " The above copyright notice and this permission notice shall be " included in all copies or substantial portions of the Software. " " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, " EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND " NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE " LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION " OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. if exists("g:loaded_easy_align") finish endif let g:loaded_easy_align = 1 let s:easy_align_delimiters_default = { \ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }, \ '=': { 'pattern': '===\|<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=[#?]\?', \ 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }, \ ':': { 'pattern': ':', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, \ ',': { 'pattern': ',', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, \ '|': { 'pattern': '|', 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }, \ '.': { 'pattern': '\.', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }, \ '{': { 'pattern': '(\@ 0 " Positive field number if len(tokens) < a:nth continue endif let nth = a:nth - 1 " make it 0-based else " Negative field number let nth = len(tokens) + a:nth if match(tokens[len(tokens) - 1], pattern.'$') == -1 let nth -= 1 endif if nth < 0 || nth == len(tokens) continue endif endif let last = tokens[nth] let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '') let token = substitute(last, pattern.'$', '', '') let delim = get(matchlist(last, pattern.'$'), 1, '') if empty(delim) && a:just == 0 && a:ignore_unmatched continue endif let max_just_len = max([s:strwidth(prefix.token), max_just_len]) let max_delim_len = max([s:strwidth(delim), max_delim_len]) let lines[line] = [nth, prefix, token, delim] endfor " Phase 2 for [line, elems] in items(lines) let tokens = a:all_tokens[line] let [nth, prefix, token, delim] = elems " Remove the leading whitespaces of the next token if len(tokens) > nth + 1 let tokens[nth + 1] = s:ltrim(tokens[nth + 1]) endif " Pad the token with spaces let pad = repeat(' ', max_just_len - s:strwidth(prefix) - s:strwidth(token)) let rpad = '' if a:just == 0 if a:stick_to_left let rpad = pad else let token = token . pad endif elseif a:just == 1 let token = pad . token endif let tokens[nth] = token " Pad the delimiter let dpad = repeat(' ', max_delim_len - s:strwidth(delim)) if a:stick_to_left let rpad = rpad . dpad else let delim = dpad . delim endif " Before and after the range (for blockwise visual mode) let cline = getline(line) let before = strpart(cline, 0, a:fc - 1) let after = a:lc ? strpart(cline, a:lc) : '' " Determine the left and right margin around the delimiter let rest = join(tokens[nth + 1 : -1], '') let ml = empty(prefix . token) ? '' : a:ml let mr = (empty(rest) || \ (empty(rest) && stridx(after, a:mr) == 0)) ? '' : a:mr " Align the token let aligned = join([token, ml, delim, mr, rpad], '') let tokens[nth] = aligned " Update the line let newline = s:rtrim(before.join(tokens, '').after) call setline(line, newline) endfor if a:recursive && a:nth < max_tokens let just = a:recursive == 2 ? !a:just : a:just call s:do_align(just, a:all_tokens, a:fl, a:ll, a:fc, a:lc, a:pattern, \ a:nth + 1, a:ml, a:mr, a:stick_to_left, a:ignore_unmatched, \ a:ignores, a:recursive) endif endfunction function! s:interactive(just) let just = a:just let n = '' let ch = '' while 1 call s:echon(just, n, '') let c = getchar() let ch = nr2char(c) if c == 3 || c == 27 " CTRL-C / ESC throw 'exit' elseif c == '€kb' " Backspace if len(n) > 0 let n = strpart(n, 0, len(n) - 1) endif elseif c == 13 " Enter key let just = (just + 1) % len(s:just) elseif ch == '-' if empty(n) | let n = '-' elseif n == '-' | let n = '' else | break endif elseif ch == '*' if empty(n) | let n = '*' elseif n == '*' | let n = '**' elseif n == '**' | let n = '' else | break endif elseif c >= 48 && c <= 57 " Numbers if n[0] == '*' | break else | let n = n . ch end else break endif endwhile return [just, n, ch] endfunction function! s:parse_args(args) let n = '' let ch = '' let args = a:args let cand = '' let option = {} " Poor man's option parser let idx = 0 while 1 let midx = match(args, '{.*}\s*$', idx) if midx == -1 | break | endif let cand = strpart(args, midx) try let o = eval(cand) if type(o) == 4 let option = o let args = strpart(args, 0, midx) break endif catch " Ignore endtry let idx = midx + 1 endwhile " Invalid option dictionary if len(substitute(cand, '\s', '', 'g')) > 2 && empty(option) call s:exit("Invalid option: ". cand) endif " Has /Regexp/? let matches = matchlist(args, '^\(.\{-}\)\s*/\(.*\)/\s*$') " Found regexp if !empty(matches) let regexp = matches[2] " Test regexp try | call matchlist('', regexp) catch | call s:exit("Invalid regular expression: ". regexp) endtry " Unsupported regular expression if match(regexp, '\\zs') != -1 call s:exit("Using \\zs is not allowed. Use stick_to_left option instead.") endif return [matches[1], regexp, option, 1] else let tokens = matchlist(args, '^\([1-9][0-9]*\|-[0-9]*\|\*\*\?\)\?\s*\(.\{-}\)\?$') return [tokens[1], tokens[2], option, 0] endif endfunction function! easy_align#align(just, expr) range let just = a:just let recur = 0 let n = '' let ch = '' let option = {} let regexp = 0 try if empty(a:expr) let [just, n, ch] = s:interactive(just) else let [n, ch, option, regexp] = s:parse_args(a:expr) if empty(ch) " Try swapping n and ch let [n, ch] = ['', n] endif endif catch 'exit' return endtry if n == '*' | let [nth, recur] = [1, 1] elseif n == '**' | let [nth, recur] = [1, 2] elseif n == '-' | let nth = -1 elseif empty(n) | let nth = 1 elseif n == '0' || ( n != '-0' && n != string(str2nr(n)) ) echon "\rInvalid field number: ". n return else let nth = n endif let delimiters = s:easy_align_delimiters_default if exists('g:easy_align_delimiters') let delimiters = extend(copy(delimiters), g:easy_align_delimiters) endif if regexp let dict = { 'pattern': ch } else if ch =~ '^\\\s\+$' let ch = ' ' elseif ch =~ '^\\\\\s\+$' let ch = '\' endif if !has_key(delimiters, ch) echon "\rUnknown delimiter key: ". ch return endif let dict = delimiters[ch] endif try if !empty(option) let dict = extend(copy(dict), s:normalize_options(option)) endif catch 'exit' return endtry call s:do_align(just, {}, a:firstline, a:lastline, \ visualmode() == '' ? min([col("'<"), col("'>")]) : 1, \ visualmode() == '' ? max([col("'<"), col("'>")]) : 0, \ get(dict, 'pattern', ch), \ nth, \ get(dict, 'margin_left', ' '), \ get(dict, 'margin_right', ' '), \ get(dict, 'stick_to_left', 0), \ get(dict, 'ignore_unmatched', get(g:, 'easy_align_ignore_unmatched', 1)), \ get(dict, 'ignores', s:ignored_syntax()), \ recur) call s:echon(just, n, regexp ? '/'.ch.'/' : ch) endfunction