diff --git a/README.md b/README.md index a42efc9..2cf84c7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ vim-lesser-align Yet another Vim alignment plugin without too much ambition. This plugin clearly has less features than the other pre-existing ones with the similar goals, -but it is simpler, easier to use, and good enough for most of the cases. +but it is simpler, easier to use, and good enough for the most of the cases. Usage ----- @@ -20,8 +20,8 @@ vnoremap :LesserAlign Then a key sequence becomes a combination of 3 parts. 1. `` - - Shortcut for `:LesserAssign` -1. Integer (optional, default: 1) + - Shortcut for `:LesserAssign` +1. Integer (*optional*, default: 1) - `1`: Alignment around 1st delimiter - `2`: Alignment around 2nd delimiter - `...` @@ -36,16 +36,16 @@ Then a key sequence becomes a combination of 3 parts. Examples -------- -| Keystroke | Description | -| ------------------- | ----------------------------------------------------- | -| `=` | Alignment around 1st equals sign (and the likes) | -| `2=` | Alignment around 2nd equals sign (and the likes) | -| `3=` | Alignment around 3rd equals sign (and the likes) | -| `*=` | Alignment around all equals signs (and the likes) | -| `` | Alignment around 1st whitespace | -| `2` | Alignment around 2nd whitespace | -| `:` | Alignment around 1st colon | -| ... | ... | +| With visual map | Description | Equivalent command | +| ----------------- | -------------------------------------------------- | ----------------------- | +| `=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` | +| `2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` | +| `3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` | +| `*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` | +| `` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` | +| `2` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` | +| `:` | Alignment around 1st colon | `:'<,'>LesserAlign :` | +| ... | ... | | Defining custom alignment rules ------------------------------- diff --git a/autoload/lesser_align.vim b/autoload/lesser_align.vim index 38b07ca..09cf5a8 100644 --- a/autoload/lesser_align.vim +++ b/autoload/lesser_align.vim @@ -78,38 +78,61 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive) endif endfunction -function! lesser_align#align() range - echon "\rlesser-align ()" - let n = '' +function! lesser_align#align(...) range let recursive = 0 + let n = '' + let ch = '' - while 1 - let c = getchar() - let ch = nr2char(c) - if c == 3 || c == 27 + if a:0 == 0 + echon "\rlesser-align ()" + while 1 + let c = getchar() + let ch = nr2char(c) + if c == 3 || c == 27 + return + elseif c >= 48 && c <= 57 + if n == '*' + echon "\rField number(*) already specified" + return + endif + let n = n . nr2char(c) + echon "\rlesser-align (". n .")" + elseif ch == '*' + if !empty(n) + echon "\rField number(". n .") already specified" + return + endif + let n = '*' + echon "\rlesser-align (*)" + else + break + endif + endwhile + elseif a:0 == 1 + let tokens = matchlist(a:1, '^\([1-9][0-9]*\|\*\)\?\(.\)$') + if empty(tokens) + echo "Invalid arguments: ". a:1 return - elseif c >= 48 && c <= 57 - if recursive - echo "Number(*) already specified" - return - endif - let n = n . nr2char(c) - echon "\rlesser-align (". n .")" - elseif ch == '*' - if !empty(n) - echo "Number already specified" - return - endif - let recursive = 1 - echon "\rlesser-align (*)" - else - break endif - endwhile + let [n, ch] = tokens[1:2] + elseif a:0 == 2 + let n = a:1 + let ch = a:2 + else + echo "Invalid number of arguments: ". a:0 ." (expected 0, 1, or 2)" + return + endif - let n = empty(n) ? 1 : n + if n == '*' + let n = 1 + let recursive = 1 + elseif empty(n) + let n = 1 + elseif n != string(str2nr(n)) + echon "\rInvalid field number: ". n + return + endif - let error = 0 if has_key(g:lesser_align_delimiters_merged, ch) let dict = g:lesser_align_delimiters_merged[ch] call s:do_align(a:firstline, a:lastline, @@ -118,14 +141,9 @@ function! lesser_align#align() range \ get(dict, 'margin_left', ' '), \ get(dict, 'margin_right', ' '), \ get(dict, 'stick_to_left', 0), recursive) - else - let error = 1 - endif - - if error - echon "\rUnknown delimiter: ". ch - else echon "\rlesser-align (". (recursive ? '*' : n) . ch .")" + else + echon "\rUnknown delimiter: ". ch endif endfunction diff --git a/plugin/lesser_align.vim b/plugin/lesser_align.vim index 592352e..66b034e 100644 --- a/plugin/lesser_align.vim +++ b/plugin/lesser_align.vim @@ -3,4 +3,4 @@ if exists("g:lesser_align_plugin_loaded") endif let g:lesser_align_plugin_loaded = 1 -command! -nargs=0 -range LesserAlign ,call lesser_align#align() +command! -nargs=* -range LesserAlign ,call lesser_align#align()