Thursday, April 2, 2020

Re: How to avoid autocmd CursorMoved to lag ?

On Do, 02 Apr 2020, Ni Va wrote:

> Thank you Christian. I test your advises and added answers below.
>
>
> Le jeudi 2 avril 2020 08:57:15 UTC+2, Christian Brabandt a écrit :
>
>
> On Di, 31 Mär 2020, Ni Va wrote:
>
> > Hi,
> >
> > I use this func to switch filetype when cursor is moving outside/inside embed
> > section:
> >
> >
> >     fun! helper#SwitchFileType() "{{{
> >       if !exists('b:busy')
> >       let b:busy=1
> >     let start = str2nr(search( '^\w\+\s\+<<\s\+EOF', 'n' ))
> >     if start > 0
> >       let end    = str2nr(search( '^EOF', 'n' ))
> >       let curpos = getcurpos()[1]
> >       let lang   = split(getline(start), '<<')[0]->substitute('\s', '', "g") 
> >       if (curpos > start) && (curpos < end)
> >     exe 'set ft='.lang
> >       else
> >     exe 'set ft=vim'
> >       end
> >     "echo 'Filetype switched to ' . &ft
> >     end
> >       unlet b:busy
> >       end
> >     endfun "}}}
> >
> >
> >   autocmd CursorMoved    *.vim   call helper#SwitchFileType()
> >   autocmd CursorMovedI   *.vim   call helper#SwitchFileType()
> >  
> >
> >
> > The func is called on event cursormoved and lag cursor effectively moving
> > action.
> >
> > how to avoid this lag ?
>
> I am not exactly sure, but a couple of things to check:
>
> - Loading filetypes every time you move (even for single letters):
>
>   :set ft=<filetype>
>
>   this will cause vim to load several runtime files (ftplugin, syntax and
>   indent scripts) and although vim usually checks whether they have been
>   already loaded using some buffer local variables, the files have to be
>   read (and loaded from your harddisk). This might make vim slow,
>   especially, if Vim is installed on a slow hard disk (or even worse: a
>   network share).
>
>   Better here is to cache the current filetype and only call `:set ft=` if
>   you detect that you are already in a different filetype.  it's better, less slow
>
> - Second, the searching for the regions of different filetypes happens
>   every time you move your cursor (even when moving horizontal). This
>   might slow down vim, although the regular expression does not look
>   very expansive).
>   I believe the builtin vim syntax file, already has support for
>   different syntax regions so you could simply check the name of the
>   current syntax region using synid()/synidattr() and only load your
>   filetype scripts then. 
>
>  
>
> >> echo synID(line('.'), col('.'), 1)->synIDtrans()->synIDattr("name") just give me "Comment" or other info not related to lang ruby, vim etc... so how to switch good highlight with that ??
>
>
>  
>
>   However, as mentioned, the vim syntax script already has support for
>   embedding of a couple of languages, so first check `:h g:vimsyn_embed` 
>
>  
>
> >> g:vimsyn_embed is set to 'r' in my case even if cursor is out of EOF section.. ??

Try setting it to 'lmpPrt' explicitly. This basically tells the vim
syntax script which other filetype syntax script to embed into the vim
syntax language.

> but how do you explain that without my script, syntax highlight is not well as it is done with my script that set ft=ruby

I am not sure, the vim syntax script does include all embedable syntax
files for lua, python and so on, if g:vimsyn_embed has been set
correctly.


Best,
Christian
--
Frauen sind erstaunt, was Männer alles vergessen.
Männer sind erstaunt, woran Frauen sich erinnern.
-- Peter Bamm

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200402100902.GH16659%40256bit.org.

No comments:

Post a Comment