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.
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 <silent> <Enter> :LesserAlign<cr>
Then a key sequence becomes a combination of 3 parts.
1. `<Enter>`
- Shortcut for `:LesserAssign<Enter>`
1. Integer (optional, default: 1)
- Shortcut for `:LesserAssign<cr>`
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 |
| ------------------- | ----------------------------------------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) |
| `<Enter>*=` | Alignment around all equals signs (and the likes) |
| `<Enter><space>` | Alignment around 1st whitespace |
| `<Enter>2<space>` | Alignment around 2nd whitespace |
| `<Enter>:` | Alignment around 1st colon |
| ... | ... |
| With visual map | Description | Equivalent command |
| ----------------- | -------------------------------------------------- | ----------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` |
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` |
| `<Enter><space>` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` |
| `<Enter>2<space>` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` |
| `<Enter>:` | Alignment around 1st colon | `:'<,'>LesserAlign :` |
| ... | ... | |
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
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

View File

@@ -3,4 +3,4 @@ if exists("g:lesser_align_plugin_loaded")
endif
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>)