diff --git a/README.md b/README.md index 33ed24a..d25b986 100644 --- a/README.md +++ b/README.md @@ -170,20 +170,20 @@ You can override these default rules or define your own rules with #### Examples -| With visual map | Description | Equivalent command | -| ------------------- | ---------------------------------- | ---------------------- | -| `` | Around 1st whitespaces | `:'<,'>EasyAlign\ ` | -| `2` | Around 2nd whitespaces | `:'<,'>EasyAlign2\ ` | -| `-` | Around the last whitespaces | `:'<,'>EasyAlign-\ ` | -| `-2` | Around the 2nd to last whitespaces | `:'<,'>EasyAlign-2\ ` | -| `:` | Around 1st colon (`key: value`) | `:'<,'>EasyAlign:` | -| `:` | Around 1st colon (`key : value`) | `:'<,'>EasyAlign:s0l1` | -| `=` | Around 1st operators with = | `:'<,'>EasyAlign=` | -| `3=` | Around 3rd operators with = | `:'<,'>EasyAlign3=` | -| `*=` | Around all operators with = | `:'<,'>EasyAlign*=` | -| `**=` | Left-right alternating around = | `:'<,'>EasyAlign**=` | -| `=` | Right alignment around 1st = | `:'<,'>EasyAlign!=` | -| `**=` | Right-left alternating around = | `:'<,'>EasyAlign!**=` | +| With visual map | Description | Equivalent command | +| ------------------- | ---------------------------------- | --------------------- | +| `` | Around 1st whitespaces | `:'<,'>EasyAlign\ ` | +| `2` | Around 2nd whitespaces | `:'<,'>EasyAlign2\ ` | +| `-` | Around the last whitespaces | `:'<,'>EasyAlign-\ ` | +| `-2` | Around the 2nd to last whitespaces | `:'<,'>EasyAlign-2\ ` | +| `:` | Around 1st colon (`key: value`) | `:'<,'>EasyAlign:` | +| `:` | Around 1st colon (`key : value`) | `:'<,'>EasyAlign:=` | Around 1st operators with = | `:'<,'>EasyAlign=` | +| `3=` | Around 3rd operators with = | `:'<,'>EasyAlign3=` | +| `*=` | Around all operators with = | `:'<,'>EasyAlign*=` | +| `**=` | Left-right alternating around = | `:'<,'>EasyAlign**=` | +| `=` | Right alignment around 1st = | `:'<,'>EasyAlign!=` | +| `**=` | Right-left alternating around = | `:'<,'>EasyAlign!**=` | #### Using regular expressions @@ -211,7 +211,7 @@ keys listed below. The meaning of each option will be described in | `CTRL-D` | `delimiter_align` | left, center, right | | `CTRL-U` | `ignore_unmatched` | 0, 1 | | `CTRL-G` | `ignore_groups` | [], ['String'], ['Comment'], ['String', 'Comment'] | -| `CTRL-O` | `mode_sequence` | Input string (`/[lrc]+\*{0,2}/`) | +| `CTRL-A` | `align` | Input string (`/[lrc]+\*{0,2}/`) | | `` | `stick_to_left` | `{ 'stick_to_left': 1, 'left_margin': 0 }` | | `` | `stick_to_left` | `{ 'stick_to_left': 0, 'left_margin': 1 }` | | `` | `*_margin` | `{ 'left_margin': 0, 'right_margin': 0 }` | @@ -275,21 +275,21 @@ You can even omit spaces between the arguments, so concisely (or cryptically): Nice. But let's make it even shorter. Option values can be written in shorthand notation. -- `:EasyAlign*/[:;]\+/s1l0` +- `:EasyAlign*/[:;]\+/" - let modes = tolower(s:input("Mode sequence: ", get(opts, 'm', mode), a:vis)) + elseif ch == "\" || ch == "\" + let modes = tolower(s:input("Alignment ([lrc...][[*]*]): ", get(opts, 'a', mode), a:vis)) if match(modes, '^[lrc]\+\*\{0,2}$') != -1 - let opts['m'] = modes + let opts['a'] = modes let mode = modes[0] while mode != s:shift(a:modes, 1) endwhile else - silent! call remove(opts, 'm') + silent! call remove(opts, 'a') endif elseif ch == "\" || ch == "\" if a:live && regx && !empty(d) @@ -811,8 +814,8 @@ endfunction let s:shorthand_regex = \ '\s*\%(' - \ .'\(lm\?[0-9]\+\)\|\(rm\?[0-9]\+\)\|\(iu[01]\)\|\(s\%(tl\)\?[01]\)\|' - \ .'\(da\?[clr]\)\|\(ms\?[lrc*]\+\)\|\(i\%(dt\)\?[kdsn]\)\|\([gv]/.*/\)\|\(ig\[.*\]\)' + \ .'\(lm\?[0-9]\+\)\|\(rm\?[0-9]\+\)\|\(iu[01]\)\|\(\%(s\%(tl\)\?[01]\)\|<\)\|' + \ .'\(da\?[clr]\)\|\(\%(ms\?\|a\)[lrc*]\+\)\|\(i\%(dt\)\?[kdsn]\)\|\([gv]/.*/\)\|\(ig\[.*\]\)' \ .'\)\+\s*$' function! s:parse_shorthand_opts(expr) @@ -828,7 +831,7 @@ function! s:parse_shorthand_opts(expr) let match = matchlist(expr, regex) if empty(match) | break | endif for m in filter(match[ 1 : -1 ], '!empty(v:val)') - for key in ['lm', 'rm', 'l', 'r', 'stl', 's', 'iu', 'da', 'd', 'ms', 'm', 'ig', 'i', 'g', 'v'] + for key in ['lm', 'rm', 'l', 'r', 'stl', 's', '<', 'iu', 'da', 'd', 'ms', 'm', 'ig', 'i', 'g', 'v', 'a'] if stridx(tolower(m), key) == 0 let rest = strpart(m, len(key)) if key == 'i' | let key = 'idt' | endif @@ -837,7 +840,7 @@ function! s:parse_shorthand_opts(expr) let key = 'f' endif - if key == 'idt' || index(['d', 'f', 'm'], key[0]) >= 0 + if key == 'idt' || index(['d', 'f', 'm', 'a'], key[0]) >= 0 let opts[key] = rest elseif key == 'ig' try @@ -850,6 +853,8 @@ function! s:parse_shorthand_opts(expr) catch call s:exit("Invalid ignore_groups: ". a:expr) endtry + elseif key == '<' + let opts['stl'] = 1 else let opts[key] = str2nr(rest) endif @@ -1025,7 +1030,7 @@ function! s:process(range, mode, n, ch, opts, regexp, rules, bvis) let [nth, recur] = s:parse_nth(a:n) let dict = s:build_dict(a:rules, a:ch, a:regexp, a:opts) let [mode_sequence, recur] = s:build_mode_sequence( - \ get(dict, 'mode_sequence', recur == 2 ? s:alternating_modes(a:mode) : a:mode), + \ get(dict, 'align', recur == 2 ? s:alternating_modes(a:mode) : a:mode), \ recur) if recur && a:bvis @@ -1050,11 +1055,11 @@ endfunction function s:summarize(opts, recur, mode_sequence) let copts = s:compact_options(a:opts) let nbmode = s:interactive_modes(0)[0] - if !has_key(copts, 'm') && ( + if !has_key(copts, 'a') && ( \ (a:recur == 2 && s:alternating_modes(nbmode) != a:mode_sequence) || \ (a:recur != 2 && (a:mode_sequence[0] != nbmode || len(a:mode_sequence) > 1)) \ ) - call extend(copts, { 'm': a:mode_sequence }) + call extend(copts, { 'a': a:mode_sequence }) endif return copts endfunction diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 69cc48d..a9fdbee 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -1,4 +1,4 @@ -easy-align.txt vim-easy-align Last change: May 31 2014 +easy-align.txt vim-easy-align Last change: July 23 2014 VIM-EASY-ALIGN - TABLE OF CONTENTS *easyalign* *easy-align* *easy-align-toc* ============================================================================== @@ -158,7 +158,7 @@ shortcuts for the most common use cases with the concept of "alignment rule". An alignment rule is a predefined set of options for common alignment tasks, which is identified by a single character, DELIMITER KEY, such as , -`=`, `:`, `.`, `|`, `&`, and `,`. +`=`, `:`, `.`, `|`, `&`, `#`, and `,`. Think of it as a shortcut. Instead of writing regular expression and setting several options, you can just type in a single character. @@ -231,7 +231,7 @@ With these mappings, you can align text with only a few keystrokes. - `-` Around the last occurrences of delimiters (`-1`) - `-2` Around the second to last occurrences of delimiters - ... - 4. Delimiter key (a single keystroke; , `=`, `:`, `.`, `|`, `&`, `,`) + 4. Delimiter key (a single keystroke; , `=`, `:`, `.`, `|`, `&`, `#`, `,`) Predefined alignment rules~ @@ -246,6 +246,7 @@ Predefined alignment rules~ `.` | Multi-line method chaining `,` | Multi-line method arguments `&` | LaTeX tables (matches `&` and `\\` ) + `#` | Ruby/Python comments | Table markdown *g:easy_align_delimiters* @@ -260,13 +261,13 @@ Examples~ *easy-align-5-3-2* With visual map | Description | Equivalent command~ - ------------------- | ---------------------------------- | ---------------------- + ------------------- | ---------------------------------- | --------------------- | Around 1st whitespaces | :'<,'>EasyAlign\ 2 | Around 2nd whitespaces | :'<,'>EasyAlign2\ - | Around the last whitespaces | :'<,'>EasyAlign-\ -2 | Around the 2nd to last whitespaces | :'<,'>EasyAlign-2\ : | Around 1st colon (`key: value`) | :'<,'>EasyAlign: - : | Around 1st colon (`key : value`) | :'<,'>EasyAlign:s0l1 + : | Around 1st colon (`key : value`) | :'<,'>EasyAlign:= | Around 1st operators with = | :'<,'>EasyAlign= 3= | Around 3rd operators with = | :'<,'>EasyAlign3= *= | Around all operators with = | :'<,'>EasyAlign*= @@ -306,7 +307,7 @@ following sections (|easy-align-alignment-options|). CTRL-D | `delimiter_align` | left, center, right CTRL-U | `ignore_unmatched` | 0, 1 CTRL-G | `ignore_groups` | [], ['String'], ['Comment'], ['String', 'Comment'] - CTRL-O | `mode_sequence` | Input string (`/[lrc]+\*{0,2}/` ) + CTRL-A | `align` | Input string (`/[lrc]+\*{0,2}/` ) | `stick_to_left` | `{ 'stick_to_left': 1, 'left_margin': 0 }` | `stick_to_left` | `{ 'stick_to_left': 0, 'left_margin': 1 }` | `*_margin` | `{ 'left_margin': 0, 'right_margin': 0 }` @@ -375,20 +376,20 @@ You can even omit spaces between the arguments, so concisely (or cryptically): Nice. But let's make it even shorter. Option values can be written in shorthand notation. - - `:EasyAlign*/[:;]\+/s1l0` + - `:EasyAlign*/[:;]\+/___________________________________________________________~ @@ -556,7 +557,10 @@ For example if you set `ignore_groups` option to be an empty list, you get 'grape: fruits': 3 } < -Satisfied? +If a pattern in `ignore_groups` is prepended by a `!`, it will have the +opposite meaning. For instance, if `ignore_groups` is given as `['!Comment']`, +delimiters that are not highlighted as Comment will be ignored during the +alignment. < Ignoring unmatched lines >__________________________________________________~ @@ -724,36 +728,36 @@ To recap: :EasyAlign! **= < In addition to these, you can fine-tune alignments over multiple occurrences of -the delimiters with 'mode_sequence' option. (The option can also be set in -interactive mode with the special key CTRL-O) +the delimiters with 'align' option. (The option can also be set in interactive +mode with the special key CTRL-A) > " Left alignment over the first two occurrences of delimiters - :EasyAlign = { 'mode_sequence': 'll' } + :EasyAlign = { 'align': 'll' } " Right, left, center alignment over the 1st to 3rd occurrences of delimiters - :EasyAlign = { 'm': 'rlc' } + :EasyAlign = { 'a': 'rlc' } " Using shorthand notation - :EasyAlign = mrlc + :EasyAlign = arlc " Right, left, center alignment over the 2nd to 4th occurrences of delimiters - :EasyAlign 2=mrlc + :EasyAlign 2=arlc " (*) Repeating alignments (default: l, r, or c) " Right, left, center, center, center, center, ... - :EasyAlign *=mrlc + :EasyAlign *=arlc " (**) Alternating alignments (default: lr or rl) " Right, left, center, right, left, center, ... - :EasyAlign **=mrlc + :EasyAlign **=arlc " Right, left, center, center, center, ... repeating alignment " over the 3rd to the last occurrences of delimiters - :EasyAlign 3=mrlc* + :EasyAlign 3=arlc* " Right, left, center, right, left, center, ... alternating alignment " over the 3rd to the last occurrences of delimiters - :EasyAlign 3=mrlc** + :EasyAlign 3=arlc** < < Extending alignment rules >_________________________________________________~ @@ -774,8 +778,10 @@ Examples~ > let g:easy_align_delimiters = { \ '>': { 'pattern': '>>\|=>\|>' }, - \ '/': { 'pattern': '//\+\|/\*\|\*/', 'ignore_groups': ['String'] }, - \ '#': { 'pattern': '#\+', 'ignore_groups': ['String'], 'delimiter_align': 'l' }, + \ '/': { + \ 'pattern': '//\+\|/\*\|\*/', + \ 'delimiter_align': 'l', + \ 'ignore_groups': ['!Comment'] }, \ ']': { \ 'pattern': '[[\]]', \ 'left_margin': 0, @@ -789,8 +795,8 @@ Examples~ \ 'stick_to_left': 0 \ }, \ 'd': { - \ 'pattern': ' \(\S\+\s*[;=]\)\@=', - \ 'left_margin': 0, + \ 'pattern': ' \(\S\+\s*[;=]\)\@=', + \ 'left_margin': 0, \ 'right_margin': 0 \ } \ } diff --git a/test/commandline.vader b/test/commandline.vader index aae733e..c23740d 100644 --- a/test/commandline.vader +++ b/test/commandline.vader @@ -40,14 +40,21 @@ Expect: apple;:;;banana :: cake data ;; exchange:;::format -Execute (shorthand notation of mode_sequence and margin): +Execute (DEPRECATED: shorthand notation of mode_sequence and margin): %EasyAlign/[:;]\+/mrc*l2r2 Expect: apple ;:;; banana :: cake data ;; exchange :;:: format -Execute (deep indentation): +Execute (shorthand notation of align and margin): + %EasyAlign/[:;]\+/arc*l2r2 + +Expect: + apple ;:;; banana :: cake + data ;; exchange :;:: format + +Execute (DEPRECATED: deep indentation): %EasyAlign/[:;]\+/mrc*l2r2 %EasyAlign*/[:;]\+/idmrl* @@ -55,6 +62,14 @@ Expect: apple ;:;; banana :: cake data ;; exchange :;:: format +Execute (deep indentation): + %EasyAlign/[:;]\+/arc*l2r2 + %EasyAlign*/[:;]\+/idarl* + +Expect: + apple ;:;; banana :: cake + data ;; exchange :;:: format + Execute (stick_to_left): %EasyAlign*/[:;]\+/stl1l0dlrm3 @@ -62,6 +77,13 @@ Expect: apple;:;; banana:: cake data;; exchange:;:: format +Execute (<): + %EasyAlign*/[:;]\+/'} diff --git a/test/interactive.vader b/test/interactive.vader index 9c1f4db..8db0b0b 100644 --- a/test/interactive.vader +++ b/test/interactive.vader @@ -758,7 +758,7 @@ Expect ruby: dddddd /= 123 gg <=> ee -Do (Alignment using mode_sequence, delimiter_align): +Do (DEPRECATED: Alignment using mode_sequence, delimiter_align): vip\\\cr*\\= Expect ruby: @@ -787,7 +787,37 @@ Expect ruby: dddddd /= 123 gg <=> ee -Do (mode_sequence starting from 2nd, delimiter_align = center): +Do (Alignment using align, delimiter_align): + vip\\\cr*\\= + +Expect ruby: + a = + a = 1 + bbbb .= 2 + ccccccc = 3 + ccccccccccccccc + ddd = # + eeee === eee = eee = eee = f + fff = ggg += gg &&= gg + g != hhhhhhhh == # 8 + i := 5 + i %= 5 + i *= 5 + j =~ 5 + j >= 5 + aa => 123 + aa <<= 123 + aa >>= 123 + bbb => 123 + c => 1233123 + d => 123 + dddddd &&= 123 + dddddd ||= 123 + dddddd /= 123 + gg <=> ee + + +Do (DEPRECATED: mode_sequence starting from 2nd, delimiter_align = center): vip\\\rc**\\\2= Expect ruby: @@ -816,6 +846,35 @@ Expect ruby: dddddd /= 123 gg <=> ee +Do (align starting from 2nd, delimiter_align = center): + vip\\\rc**\\\2= + +Expect ruby: + a = + a = 1 + bbbb .= 2 + ccccccc = 3 + ccccccccccccccc + ddd = # + eeee === eee = eee = eee = f + fff = ggg += gg &&= gg + g != hhhhhhhh == # 8 + i := 5 + i %= 5 + i *= 5 + j =~ 5 + j >= 5 + aa => 123 + aa <<= 123 + aa >>= 123 + bbb => 123 + c => 1233123 + d => 123 + dddddd &&= 123 + dddddd ||= 123 + dddddd /= 123 + gg <=> ee + Do (around all =s, do not ignore unmatched): vip\\ \0\