removes center-justification mode

This commit is contained in:
Junegunn Choi
2013-05-05 01:01:42 +09:00
parent 0020db37ae
commit 78d1a23dce
3 changed files with 28 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
vim-easy-align vim-easy-align
============== ==============
A simple, easy-to-use Vim alignment plugin. A simple, easy-to-use Vim alignment plugin without too much ambition.
Demo Demo
---- ----
@@ -21,7 +21,7 @@ vnoremap <silent> <Enter> :EasyAlign<cr>
With the mapping, you can align selected lines with a few keystrokes. With the mapping, you can align selected lines with a few keystrokes.
1. `<Enter>` key to start interactive EasyAlign command 1. `<Enter>` key to start interactive EasyAlign command
1. Optional Enter keys to switch justficiation mode (default: left) 1. Optional Enter keys to toggle right justification mode
1. Optional field number (default: 1) 1. Optional field number (default: 1)
- `1` Alignment around 1st delimiter - `1` Alignment around 1st delimiter
- `2` Alignment around 2nd delimiter - `2` Alignment around 2nd delimiter

View File

@@ -13,15 +13,14 @@ let s:easy_align_delimiters_default = {
\ '.': { 'pattern': '\.', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 } \ '.': { 'pattern': '\.', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }
\ } \ }
let s:just = ['L', 'R', 'C'] let s:just = ['', '[R]']
function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, recursive) function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left, recursive)
let lines = {} let lines = {}
let max_token_len = 0 let max_just_len = 0
let max_delim_len = 0 let max_delim_len = 0
let max_prefix_len = 0 let max_tokens = 0
let max_tokens = 0 let pattern = '\s*\(' .a:pattern. '\)\s*'
let pattern = '\s*\(' .a:pattern. '\)\s*'
for line in range(a:fl, a:ll) for line in range(a:fl, a:ll)
let tokens = split(a:lc ? let tokens = split(a:lc ?
\ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) : \ strpart(getline(line), a:fc - 1, a:lc - a:fc + 1) :
@@ -31,17 +30,21 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left,
continue continue
endif endif
" Preserve indentation
if match(tokens[0], '^\s*$') != -1
let tokens = extend([join(tokens[0:1], '')], tokens[2:-1])
endif
let max_tokens = max([len(tokens), max_tokens]) let max_tokens = max([len(tokens), max_tokens])
let nth = match(tokens[0], '^\s*$') != -1 ? a:nth + 1 : a:nth
if len(tokens) < nth if len(tokens) < a:nth
continue continue
endif endif
let nth = a:nth - 1 " 0-based
let last = tokens[nth - 1] let last = tokens[nth]
let prefix = (nth > 1 ? join(tokens[0 : nth - 2], '') : '') let prefix = (nth > 0 ? join(tokens[0 : nth - 1], '') : '')
let token = substitute(last, pattern.'$', '', '') let token = substitute(last, pattern.'$', '', '')
let suffix = join(tokens[nth : -1], '') let suffix = join(tokens[nth + 1: -1], '')
if match(last, pattern.'$') == -1 if match(last, pattern.'$') == -1
if !exists("g:easy_align_ignore_unmatched") || g:easy_align_ignore_unmatched if !exists("g:easy_align_ignore_unmatched") || g:easy_align_ignore_unmatched
@@ -53,16 +56,15 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left,
let delim = matchlist(last, pattern)[1] let delim = matchlist(last, pattern)[1]
endif endif
let max_token_len = max([len(token), max_token_len]) let max_just_len = max([len(token.prefix), max_just_len])
let max_prefix_len = max([len(prefix), max_prefix_len]) let max_delim_len = max([len(delim), max_delim_len])
let max_delim_len = max([len(delim), max_delim_len]) let lines[line] = [prefix, token, delim, suffix]
let lines[line] = [prefix, token, delim, suffix]
endfor endfor
for [line, tokens] in items(lines) for [line, tokens] in items(lines)
let [prefix, token, delim, suffix] = tokens let [prefix, token, delim, suffix] = tokens
let pad = repeat(' ', max_token_len - len(token) + max_prefix_len - len(prefix)) let pad = repeat(' ', max_just_len - len(prefix) - len(token))
if a:just == 0 if a:just == 0
if a:stick_to_left if a:stick_to_left
let suffix = pad . suffix let suffix = pad . suffix
@@ -71,15 +73,6 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left,
endif endif
elseif a:just == 1 elseif a:just == 1
let token = pad . token let token = pad . token
else
let p1 = strpart(pad, 0, len(pad) / 2)
let p2 = strpart(pad, len(pad) / 2)
if a:stick_to_left
let token = p1 . token
let suffix = p2 . suffix
else
let token = p1. token .p2
endif
endif endif
let delim = repeat(' ', max_delim_len - len(delim)). delim let delim = repeat(' ', max_delim_len - len(delim)). delim
@@ -101,7 +94,8 @@ function! s:do_align(just, fl, ll, fc, lc, pattern, nth, ml, mr, stick_to_left,
endfunction endfunction
function! s:echon(l, n, d) function! s:echon(l, n, d)
echon "\rEasyAlign[". s:just[a:l] ."] (" .a:n.a:d. ")" echon "\r"
echon "\rEasyAlign". s:just[a:l] ." (" .a:n.a:d. ")"
endfunction endfunction
function! easy_align#align(just, ...) range function! easy_align#align(just, ...) range

View File

@@ -1,7 +1,7 @@
vim-easy-align *vim-easy-align* *easy-align* vim-easy-align *vim-easy-align* *easy-align*
========================================================================= =========================================================================
A simple, easy-to-use Vim alignment plugin. A simple, easy-to-use Vim alignment plugin without too much ambition.
Author: Junegunn Choi Author: Junegunn Choi
Source: https://github.com/junegunn/vim-easy-align Source: https://github.com/junegunn/vim-easy-align
@@ -34,8 +34,7 @@ With this mapping, you can align selected lines with a few keystrokes.
, Multi-line method arguments. CSV. , Multi-line method arguments. CSV.
| Table markdown | Table markdown
During the key sequence, <Enter> key will switch justification mode. During the key sequence, <Enter> key will toggle right-justification mode.
(left -> right -> center)
Examples: Examples:
@@ -49,11 +48,10 @@ Examples:
<Enter>: Alignment around 1st colon <Enter>: Alignment around 1st colon
EasyAlignRight, EasyAlignCenter *EasyAlignRight* *EasyAlignCenter* EasyAlignRight *EasyAlignRight*
------------------------------------------------------------------------- -------------------------------------------------------------------------
*EasyAlignRight* and *EasyAlignCenter* are the right and center justified *EasyAlignRight* is the right justified version of EasyAlign command.
versions of EasyAlign command.
Partial alignment in blockwise-visual mode Partial alignment in blockwise-visual mode
@@ -120,3 +118,4 @@ Then we get,
# Quantity of grapefruits # Quantity of grapefruits
grapefruits: 3 grapefruits: 3
} }