Wednesday, April 1, 2020

Re: How to avoid autocmd CursorMoved to lag ?

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.

- 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.
However, as mentioned, the vim syntax script already has support for
embedding of a couple of languages, so first check `:h g:vimsyn_embed`
and see if setting this variable can do what you like to achieve.


Best,
Christian
--
Rus, Ute:
leistete fruchtbare Beiträge zur Entwicklung der Gebärdensprache

--
--
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/20200402065706.GB16659%40256bit.org.

No comments: