diff --git a/README.md b/README.md index 2069af6..6bfc679 100644 --- a/README.md +++ b/README.md @@ -640,6 +640,42 @@ let g:easy_align_delimiters = { \ } ``` +Other options +------------- + +### Disabling &foldmethod during alignment + +[It is reported](https://github.com/junegunn/vim-easy-align/issues/14) that +&foldmethod of `expr` or `syntax` can significantly slow down the alignment when +editing a large, complex file with many folds. To alleviate this issue, +EasyAlign provides an option to temporarily set &foldmethod to manual during the +alignment task. In order to enable this feature, set `g:easy_align_bypass_fold` +switch to 1. + +```vim +let g:easy_align_bypass_fold = 1 +``` + +### Left/right/center mode switch in interactive mode + +In interactive mode, you can choose the alignment mode you want by pressing +enter keys. The non-bang command, `:EasyAlign` starts in left-alignment mode +and changes to right and center mode as you press enter keys, while the bang +version first starts in right-alignment mode. + +- `:EasyAlign` + - Left, Right, Center +- `:EasyAlign!` + - Right, Left, Center + +If you do not prefer this default mode transition, you can define your own +settings as follows. + +```vim +let g:easy_align_interactive_modes = ['l', 'r'] +let g:easy_align_bang_interactive_modes = ['c', 'r'] +``` + Advanced examples and use cases ------------------------------- diff --git a/autoload/easy_align.vim b/autoload/easy_align.vim index e1a90b3..fde5e95 100644 --- a/autoload/easy_align.vim +++ b/autoload/easy_align.vim @@ -254,6 +254,7 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left, " Phase 1: split let ignorable = 0 let token = '' + let phantom = 0 while 1 let matches = matchlist(string, pattern, idx) " No match @@ -284,8 +285,7 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left, " If the string is non-empty and ends with the delimiter, " append an empty token to the list if idx == len(string) - call add(tokens, '') - call add(delims, '') + let phantom = 1 break endif endwhile @@ -295,6 +295,9 @@ function! s:split_line(line, nth, modes, cycle, fc, lc, pattern, stick_to_left, let ignorable = s:highlighted_as(a:line, len(string) + a:fc - 1, a:ignore_groups) call add(tokens, leftover) call add(delims, '') + elseif phantom + call add(tokens, '') + call add(delims, '') endif let [pmode, mode] = [mode, s:shift(a:modes, a:cycle)] @@ -926,9 +929,17 @@ function! s:align(bang, first_line, last_line, expr) while len(args) > 1 let args = call('s:do_align', args) endwhile - for [line, content] in items(todo) - call setline(line, s:rtrim(content)) - endfor + + let bypass_fold = get(g:, 'easy_align_bypass_fold', 0) + let ofm = &l:foldmethod + try + if bypass_fold | let &l:foldmethod = 'manual' | endif + for [line, content] in items(todo) + call setline(line, s:rtrim(content)) + endfor + finally + if bypass_fold | let &l:foldmethod = ofm | endif + endtry let copts = s:compact_options(opts) let nbmode = s:modes(0)[0] diff --git a/doc/easy_align.txt b/doc/easy_align.txt index 38b3f34..d36d8dc 100644 --- a/doc/easy_align.txt +++ b/doc/easy_align.txt @@ -524,3 +524,14 @@ you can extend the rules by setting a dictionary named \ } +Disabling &foldmethod during alignment *g:easy_align_bypass_fold* +------------------------------------------------------------------------- + +It is reported that &foldmethod of expr or syntax can significantly slow +down the alignment when editing a large, complex file with many folds. To +alleviate this issue, EasyAlign provides an option to temporarily set +&foldmethod to manual during the alignment task. In order to enable this +feature, set g:easy_align_bypass_fold to 1. + + let g:easy_align_bypass_fold = 1 + diff --git a/test/commandline.vader b/test/commandline.vader index ac1db2f..cae4783 100644 --- a/test/commandline.vader +++ b/test/commandline.vader @@ -1,4 +1,9 @@ Execute (Clean up test environment): + Save g:easy_align_ignore_groups, g:easy_align_ignore_unmatched + Save g:easy_align_indentation, g:easy_align_delimiter_align + Save g:easy_align_interactive_modes, g:easy_align_bang_interactive_modes + Save g:easy_align_delimiters + let g:easy_align_delimiters = {} silent! unlet g:easy_align_ignore_groups silent! unlet g:easy_align_ignore_unmatched @@ -155,3 +160,5 @@ Expect javascript: "user: pass": "r00t: pa55" }; +Execute: + Restore diff --git a/test/fixme.vader b/test/fixme.vader index 00a736c..54f580f 100644 --- a/test/fixme.vader +++ b/test/fixme.vader @@ -10,3 +10,11 @@ Do (FIXME invalid judgement - block-wise visual mode): Expect: a | b | c +Do (TODO Workaround: reset visualmode() on error): + \\ + :%EasyAlign|\ + :%EasyAlign|\ + +Expect: + a | b | c + diff --git a/test/interactive.vader b/test/interactive.vader index 3733485..887b990 100644 --- a/test/interactive.vader +++ b/test/interactive.vader @@ -1,4 +1,9 @@ Execute (Clean up test environment): + Save g:easy_align_ignore_groups, g:easy_align_ignore_unmatched + Save g:easy_align_indentation, g:easy_align_delimiter_align + Save g:easy_align_interactive_modes, g:easy_align_bang_interactive_modes + Save g:easy_align_delimiters + " TODO: revert after test silent! unlet g:easy_align_ignore_groups silent! unlet g:easy_align_ignore_unmatched @@ -1283,3 +1288,5 @@ Expect: a = b = c aabba = bbbbb +Execute: + Restore diff --git a/test/tex.vader b/test/tex.vader index 1a32935..25f6303 100644 --- a/test/tex.vader +++ b/test/tex.vader @@ -1,6 +1,8 @@ # http://en.wikibooks.org/wiki/LaTeX/Tables Execute: + Save g:easy_align_delimiters, g:easy_align_bypass_fold let g:easy_align_delimiters = {} + silent! unlet g:easy_align_bypass_fold Given tex (table with escaped &): \begin{tabular}{ l c r } @@ -9,8 +11,9 @@ Given tex (table with escaped &): 777&8\&8&999\\ \end{tabular} +# FIXME vip doesn't work if folded Do (Align around all &s and \\s): - vip\*& + VG\*& Expect tex: \begin{tabular}{ l c r } @@ -20,7 +23,7 @@ Expect tex: \end{tabular} Do (right-align with explicit ignore_unmatched): - vip\\\\*& + VG\\\\*& Expect tex: \begin{tabular}{ l c r } @@ -30,7 +33,7 @@ Expect tex: \end{tabular} Do (center-align with explicit ignore_unmatched): - vip\\\\\*& + VG\\\\\*& Expect tex: \begin{tabular}{ l c r } @@ -47,7 +50,7 @@ Given tex (simple table with \hline): \end{tabular} Do: - vip\*& + VG\*& Expect tex: \begin{tabular}{ l c r } @@ -68,7 +71,7 @@ Given tex (table with lines w/o &s): \end{tabular} Do (left-align*): - vip\*& + VG\*& Expect tex: \begin{tabular}{|r|l|} @@ -82,7 +85,7 @@ Expect tex: \end{tabular} Do(left-align* and right-align around 2nd): - vip\*& + VG\*& gv\\2& Expect tex: @@ -96,3 +99,63 @@ Expect tex: \hline \end{tabular} +Given tex: + \begin{tabular}{} + 32&1.14\e1&&5.65\e2&&&&1.16\e1&&1.28\e1&\\ + 64&1.03\e1&0.1&4.98\e2&0.2&&&9.21\e2&0.3&1.02\e1&0.3\\ + 128&9.86\e2&0.1&4.69\e2&0.1&&&8.46\e2&0.1&9.45\e2&0.1\\ + 256&9.65\e2&0.0&4.59\e2&0.0&&&8.15\e2&0.1&9.11\e2&0.1\\ + % 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\ + 1024&9.49\e2&0.0&4.53\e2&0.0&&&7.94\e2&0.0&8.89\e2&0.0\\ + 2048&9.47\e2&0.0&4.52\e2&0.0&&&7.91\e2&0.0&8.85\e2&0.0\\ + 4096&9.46\e2&0.0&4.51\e2&0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\ + 8192&9.45\e2&0.0&4.51\e2&0.0&&&&&&\\ + \end{tabular} + +Execute (Alignment around &s, foldmethod should not change): + setlocal foldmethod=syntax + %EasyAlign*& + AssertEqual 'syntax', &l:foldmethod + + setlocal foldmethod=manual + %EasyAlign*& + AssertEqual 'manual', &l:foldmethod + +Expect tex: + \begin{tabular}{} + 32 & 1.14\e1 & & 5.65\e2 & & & & 1.16\e1 & & 1.28\e1 & \\ + 64 & 1.03\e1 & 0.1 & 4.98\e2 & 0.2 & & & 9.21\e2 & 0.3 & 1.02\e1 & 0.3 \\ + 128 & 9.86\e2 & 0.1 & 4.69\e2 & 0.1 & & & 8.46\e2 & 0.1 & 9.45\e2 & 0.1 \\ + 256 & 9.65\e2 & 0.0 & 4.59\e2 & 0.0 & & & 8.15\e2 & 0.1 & 9.11\e2 & 0.1 \\ + % 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\ + 1024 & 9.49\e2 & 0.0 & 4.53\e2 & 0.0 & & & 7.94\e2 & 0.0 & 8.89\e2 & 0.0 \\ + 2048 & 9.47\e2 & 0.0 & 4.52\e2 & 0.0 & & & 7.91\e2 & 0.0 & 8.85\e2 & 0.0 \\ + 4096 & 9.46\e2 & 0.0 & 4.51\e2 & 0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\ + 8192 & 9.45\e2 & 0.0 & 4.51\e2 & 0.0 & & & & & & \\ + \end{tabular} + +Execute (g:easy_align_bypass_fold set, foldmethod should not change): + let g:easy_align_bypass_fold = 1 + setlocal foldmethod=syntax + %EasyAlign*& + AssertEqual 'syntax', &l:foldmethod + + setlocal foldmethod=manual + %EasyAlign*& + AssertEqual 'manual', &l:foldmethod + +Expect tex: + \begin{tabular}{} + 32 & 1.14\e1 & & 5.65\e2 & & & & 1.16\e1 & & 1.28\e1 & \\ + 64 & 1.03\e1 & 0.1 & 4.98\e2 & 0.2 & & & 9.21\e2 & 0.3 & 1.02\e1 & 0.3 \\ + 128 & 9.86\e2 & 0.1 & 4.69\e2 & 0.1 & & & 8.46\e2 & 0.1 & 9.45\e2 & 0.1 \\ + 256 & 9.65\e2 & 0.0 & 4.59\e2 & 0.0 & & & 8.15\e2 & 0.1 & 9.11\e2 & 0.1 \\ + % 512&9.55\e2&0.0&4.56\e2&0.0&&&8.01\e2&0.0&8.96\e2&0.0\\ + 1024 & 9.49\e2 & 0.0 & 4.53\e2 & 0.0 & & & 7.94\e2 & 0.0 & 8.89\e2 & 0.0 \\ + 2048 & 9.47\e2 & 0.0 & 4.52\e2 & 0.0 & & & 7.91\e2 & 0.0 & 8.85\e2 & 0.0 \\ + 4096 & 9.46\e2 & 0.0 & 4.51\e2 & 0.0%&&&7.90\e2&0.0&8.83\e2&0.0\\ + 8192 & 9.45\e2 & 0.0 & 4.51\e2 & 0.0 & & & & & & \\ + \end{tabular} + +Execute: + Restore