allows :LesserAlign command to take arguments

This commit is contained in:
Junegunn Choi
2013-04-11 20:54:05 +09:00
parent 39a520ed7b
commit 37734710c4
3 changed files with 65 additions and 47 deletions

View File

@@ -4,7 +4,7 @@ vim-lesser-align
Yet another Vim alignment plugin without too much ambition. 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, 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 Usage
----- -----
@@ -20,8 +20,8 @@ vnoremap <silent> <Enter> :LesserAlign<cr>
Then a key sequence becomes a combination of 3 parts. Then a key sequence becomes a combination of 3 parts.
1. `<Enter>` 1. `<Enter>`
- Shortcut for `:LesserAssign<Enter>` - Shortcut for `:LesserAssign<cr>`
1. Integer (optional, default: 1) 1. Integer (*optional*, default: 1)
- `1`: Alignment around 1st delimiter - `1`: Alignment around 1st delimiter
- `2`: Alignment around 2nd delimiter - `2`: Alignment around 2nd delimiter
- `...` - `...`
@@ -36,16 +36,16 @@ Then a key sequence becomes a combination of 3 parts.
Examples Examples
-------- --------
| Keystroke | Description | | With visual map | Description | Equivalent command |
| ------------------- | ----------------------------------------------------- | | ----------------- | -------------------------------------------------- | ----------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) | | `<Enter>=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | | `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | | `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` |
| `<Enter>*=` | Alignment around all equals signs (and the likes) | | `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` |
| `<Enter><space>` | Alignment around 1st whitespace | | `<Enter><space>` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` |
| `<Enter>2<space>` | Alignment around 2nd whitespace | | `<Enter>2<space>` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` |
| `<Enter>:` | Alignment around 1st colon | | `<Enter>:` | Alignment around 1st colon | `:'<,'>LesserAlign :` |
| ... | ... | | ... | ... | |
Defining custom alignment rules Defining custom alignment rules
------------------------------- -------------------------------

View File

@@ -78,38 +78,61 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive)
endif endif
endfunction endfunction
function! lesser_align#align() range function! lesser_align#align(...) range
echon "\rlesser-align ()"
let n = ''
let recursive = 0 let recursive = 0
let n = ''
let ch = ''
if a:0 == 0
echon "\rlesser-align ()"
while 1 while 1
let c = getchar() let c = getchar()
let ch = nr2char(c) let ch = nr2char(c)
if c == 3 || c == 27 if c == 3 || c == 27
return return
elseif c >= 48 && c <= 57 elseif c >= 48 && c <= 57
if recursive if n == '*'
echo "Number(*) already specified" echon "\rField number(*) already specified"
return return
endif endif
let n = n . nr2char(c) let n = n . nr2char(c)
echon "\rlesser-align (". n .")" echon "\rlesser-align (". n .")"
elseif ch == '*' elseif ch == '*'
if !empty(n) if !empty(n)
echo "Number already specified" echon "\rField number(". n .") already specified"
return return
endif endif
let recursive = 1 let n = '*'
echon "\rlesser-align (*)" echon "\rlesser-align (*)"
else else
break break
endif endif
endwhile endwhile
elseif a:0 == 1
let tokens = matchlist(a:1, '^\([1-9][0-9]*\|\*\)\?\(.\)$')
if empty(tokens)
echo "Invalid arguments: ". a:1
return
endif
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) if has_key(g:lesser_align_delimiters_merged, ch)
let dict = g:lesser_align_delimiters_merged[ch] let dict = g:lesser_align_delimiters_merged[ch]
call s:do_align(a:firstline, a:lastline, call s:do_align(a:firstline, a:lastline,
@@ -118,14 +141,9 @@ function! lesser_align#align() range
\ get(dict, 'margin_left', ' '), \ get(dict, 'margin_left', ' '),
\ get(dict, 'margin_right', ' '), \ get(dict, 'margin_right', ' '),
\ get(dict, 'stick_to_left', 0), recursive) \ 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 .")" echon "\rlesser-align (". (recursive ? '*' : n) . ch .")"
else
echon "\rUnknown delimiter: ". ch
endif endif
endfunction endfunction

View File

@@ -3,4 +3,4 @@ if exists("g:lesser_align_plugin_loaded")
endif endif
let g:lesser_align_plugin_loaded = 1 let g:lesser_align_plugin_loaded = 1
command! -nargs=0 -range LesserAlign <line1>,<line2>call lesser_align#align() command! -nargs=* -range LesserAlign <line1>,<line2>call lesser_align#align(<f-args>)