Limelight for a visual range (#15)

This commit is contained in:
Junegunn Choi
2015-06-07 21:26:18 +09:00
parent 64d1b28b93
commit f282610943
3 changed files with 36 additions and 22 deletions

View File

@@ -18,6 +18,8 @@ Usage
- `Limelight!! [0.0 ~ 1.0]` - `Limelight!! [0.0 ~ 1.0]`
- Toggle Limelight - Toggle Limelight
You can also invoke `:Limelight` for a visual range.
### Options ### Options
For some color schemes, Limelight may not be able to calculate the color for For some color schemes, Limelight may not be able to calculate the color for

View File

@@ -57,12 +57,12 @@ function! s:getpos()
endfunction endfunction
function! s:limelight() function! s:limelight()
if !empty(get(w:, 'limelight_range', []))
return
endif
if !exists('w:limelight_prev') if !exists('w:limelight_prev')
let w:limelight_prev = [0, 0, 0, 0] let w:limelight_prev = [0, 0, 0, 0]
endif endif
if !exists('w:limelight_match_ids')
let w:limelight_match_ids = []
endif
let curr = [line('.'), line('$')] let curr = [line('.'), line('$')]
if curr ==# w:limelight_prev[0 : 1] if curr ==# w:limelight_prev[0 : 1]
@@ -75,15 +75,20 @@ function! s:limelight()
endif endif
call s:clear_hl() call s:clear_hl()
call add(w:limelight_match_ids, matchadd('LimelightDim', '\%<'.paragraph[0].'l')) call call('s:hl', paragraph)
if paragraph[1] > 0
call add(w:limelight_match_ids, matchadd('LimelightDim', '\%>'.paragraph[1].'l'))
endif
let w:limelight_prev = extend(curr, paragraph) let w:limelight_prev = extend(curr, paragraph)
endfunction endfunction
function! s:hl(startline, endline)
let w:limelight_match_ids = get(w:, 'limelight_match_ids', [])
call add(w:limelight_match_ids, matchadd('LimelightDim', '\%<'.a:startline.'l'))
if a:endline > 0
call add(w:limelight_match_ids, matchadd('LimelightDim', '\%>'.a:endline.'l'))
endif
endfunction
function! s:clear_hl() function! s:clear_hl()
while !empty(w:limelight_match_ids) while exists('w:limelight_match_ids') && !empty(w:limelight_match_ids)
silent! call matchdelete(remove(w:limelight_match_ids, -1)) silent! call matchdelete(remove(w:limelight_match_ids, -1))
endwhile endwhile
endfunction endfunction
@@ -186,7 +191,7 @@ function! s:parse_coeff(coeff)
return c return c
endfunction endfunction
function! s:on(...) function! s:on(range, ...)
try try
let s:limelight_coeff = a:0 > 0 ? s:parse_coeff(a:1) : -1 let s:limelight_coeff = a:0 > 0 ? s:parse_coeff(a:1) : -1
call s:dim(s:limelight_coeff) call s:dim(s:limelight_coeff)
@@ -194,12 +199,20 @@ function! s:on(...)
return s:error(v:exception) return s:error(v:exception)
endtry endtry
let w:limelight_range = a:range
if !empty(a:range)
call s:clear_hl()
call call('s:hl', a:range)
endif
augroup limelight augroup limelight
let was_on = exists('#limelight#CursorMoved')
autocmd! autocmd!
autocmd CursorMoved,CursorMovedI * call s:limelight() if empty(a:range) || was_on
autocmd CursorMoved,CursorMovedI * call s:limelight()
endif
autocmd ColorScheme * try autocmd ColorScheme * try
\| call s:dim(s:limelight_coeff) \| call s:dim(s:limelight_coeff)
\| call s:limelight()
\| catch \| catch
\| call s:off() \| call s:off()
\| throw v:exception \| throw v:exception
@@ -216,14 +229,12 @@ function! s:on(...)
endfunction endfunction
function! s:off() function! s:off()
if exists('w:limelight_match_ids') call s:clear_hl()
call s:clear_hl()
endif
augroup limelight augroup limelight
autocmd! autocmd!
augroup END augroup END
augroup! limelight augroup! limelight
unlet! w:limelight_prev w:limelight_match_ids unlet! w:limelight_prev w:limelight_match_ids w:limelight_range
endfunction endfunction
function! s:is_on() function! s:is_on()
@@ -231,26 +242,27 @@ function! s:is_on()
endfunction endfunction
function! s:cleanup() function! s:cleanup()
if !s:is_on() && exists('w:limelight_match_ids') if !s:is_on()
call s:clear_hl() call s:clear_hl()
end end
endfunction endfunction
function! limelight#execute(bang, ...) function! limelight#execute(bang, visual, ...) range
let range = a:visual ? [a:firstline, a:lastline] : []
if a:bang if a:bang
if a:0 > 0 && a:1 =~ '^!' && !s:is_on() if a:0 > 0 && a:1 =~ '^!' && !s:is_on()
if len(a:1) > 1 if len(a:1) > 1
call s:on(a:1[1:-1]) call s:on(range, a:1[1:-1])
else else
call s:on() call s:on(range)
endif endif
else else
call s:off() call s:off()
endif endif
elseif a:0 > 0 elseif a:0 > 0
call s:on(a:1) call s:on(range, a:1)
else else
call s:on() call s:on(range)
endif endif
endfunction endfunction

View File

@@ -21,5 +21,5 @@
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION " OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
command! -nargs=? -bar -bang Limelight call limelight#execute(<bang>0, <f-args>) command! -nargs=? -bar -bang -range Limelight <line1>,<line2>call limelight#execute(<bang>0, <count> > 0, <f-args>)