Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only colorize visible area #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions autoload/colorizer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ endfunction
function! s:RgbaColor(str, lineno) "{{{2
if has("gui_running")
let bg = synIDattr(synIDtrans(hlID("Normal")), "bg")
let bg_r = str2nr(bg[1].bg[2], 16)
let bg_g = str2nr(bg[3].bg[4], 16)
let bg_b = str2nr(bg[5].bg[6], 16)
let bg_r = str2nr(bg[1:2], 16)
let bg_g = str2nr(bg[3:4], 16)
let bg_b = str2nr(bg[5:6], 16)
else
" translucent colors would display incorrectly, so ignore the alpha value
return s:RgbaColorForTerm(a:str, a:lineno)
Expand Down Expand Up @@ -195,15 +195,9 @@ function! s:RgbaColor(str, lineno) "{{{2
let r = float2nr(ceil(ar * alpha) + ceil(bg_r * (1 - alpha)))
let g = float2nr(ceil(ag * alpha) + ceil(bg_g * (1 - alpha)))
let b = float2nr(ceil(ab * alpha) + ceil(bg_b * (1 - alpha)))
if r > 255
let r = 255
endif
if g > 255
let g = 255
endif
if b > 255
let b = 255
endif
let r = min([r, 255])
let g = min([g, 255])
let b = min([b, 255])
let l:color = printf('#%02x%02x%02x', r, g, b)
call add(ret, [l:color, pat])
endwhile
Expand Down Expand Up @@ -286,9 +280,18 @@ function! colorizer#ColorHighlight(update, ...) "{{{1
if g:colorizer_fgcontrast != s:saved_fgcontrast || (exists("a:1") && a:1 == '!')
let s:force_group_update = 1
endif
for i in range(1, line("$"))
for i in range(line("w0"), line("w$"))
" skip folded lines
if foldclosed(i) > 0
continue
endif
call s:PreviewColorInLine(i)
endfor

" A hack for colorizing after opening a folding
if ( winheight('.') - line('w$') + line('w0') ) > 1
autocmd CursorHold * silent call colorizer#ColorHighlight(1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个自动命令不在组里,可能会被反复添加。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明白了,难怪加了这个之后会卡死没反应。可以移到组里。
或者,去掉call s:ClearMatches(),这样就不需要这段代码。

endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比较关键的改动是这以上的十几行。

let s:force_group_update = 0
let s:saved_fgcontrast = g:colorizer_fgcontrast
augroup Colorizer
Expand All @@ -305,7 +308,8 @@ function! colorizer#ColorHighlight(update, ...) "{{{1
autocmd CursorMoved,CursorMovedI * silent call s:CursorMoved()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不需要了吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我认为不需要。因为后面还有BufEnter

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:gui 命令并不会触发 BufEnter 的呀。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:gui 命令并不会触发 BufEnter 的呀。

我并没有查到相关都文档说明这个。而且:gui还是会高亮的。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但是不会高亮带 alpha 值的颜色,比如 rgba(255,0,255,0.5) 这种。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,alpha值会被漏掉。

endif
" rgba handles differently, so need updating
autocmd GUIEnter * silent call colorizer#ColorHighlight(1)
autocmd CursorMoved * silent call colorizer#ColorHighlight(1)
autocmd CursorMovedI * silent call colorizer#ColorHighlight(1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这会导致移动光标时异常缓慢。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般可视范围内的代码小于100行(我的设置字号9,14寸笔记本上最多不到50行。含有“折叠”时,也会跳过"折叠“的代码),处理应该会很快。我自己使用并不觉得卡。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你按住 l 或者 k 试试呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按住jk不会卡,hl会比较卡。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我个人一般是用f,t,w之类的或者用easyMotion,能够避免这个。但考虑到更多的用户,也许这个解决方案并不适合。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不知道是不是我 CPU 比较拙还是可视区域颜色比较多,j k 也会卡。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的CPU是4核2.6GHz。

autocmd BufEnter * silent call colorizer#ColorHighlight(1)
autocmd WinEnter * silent call colorizer#ColorHighlight(1)
autocmd ColorScheme * let s:force_group_update=1 | silent call colorizer#ColorHighlight(1)
Expand Down