vim-anyfold icon indicating copy to clipboard operation
vim-anyfold copied to clipboard

[feature] add augroup

Open Freed-Wu opened this issue 4 years ago • 4 comments

" deprecated initialization using anyfold_activate variable
" still works but echoes a warning
au BufNewFile,BufRead * call anyfold#init(0)

First, this autocmd don't be included in an augroup, which make user cannot use autocmd! augroup_name to disable it.

Second, this autocmd make vim call this function when read or create a buffer, which means vim will source autoload/anyfold.vim. If we change it to (just move some lines from autoload function to plugin/anyfold.vim)

au BufNewFile,BufRead * call s:init()
function! s:init() abort
    if exists("g:anyfold_activate")
        let b:anyfold_activate = g:anyfold_activate
    endif

    if !exists("b:anyfold_activate")
        return
    elseif !b:anyfold_activate
        return
    endif
    call anyfold#init(1)
endfunction

Now, if g: or b:anyfold_activate don't exist, the autoload function anyfold#init will not be called, so autoload/anyfold.vim will not be sourced, it can save time of sourcing a script.

I think it will be better.

Thanks!

Freed-Wu avatar Aug 16 '21 13:08 Freed-Wu

This is a test file, which emulate user use vim to edit a file then press :q.

path=~/.local/share/nvim/.cache/init.vim/.dein
file=init.vim
vi --cmd 'profile start profile.txt' \
  --cmd "profile file $path/plugin/anyfold.vim" \
  --cmd "profile file $path/autoload/anyfold.vim" \
  -cq $file
rg 'Total time' profile.txt

The output in the screen is:

3:Total time:   0.000049
20:Total time:   0.000579

0.000579 second of sourcing autoload/anyfold.vim can be avoided if vim don't call anyfold#init() in event BufRead.

Freed-Wu avatar Aug 16 '21 13:08 Freed-Wu

PS: You can use

if !get(b:, 'anyfold_activate')
    return
endif

to replace

if !exists("b:anyfold_activate")
    return
elseif !b:anyfold_activate
    return
endif

See more by :h get().

Freed-Wu avatar Aug 16 '21 13:08 Freed-Wu

These are just some thoughts. No offense.

Thanks!

Freed-Wu avatar Aug 16 '21 13:08 Freed-Wu

Sorry! I found b:anyfold_activate has been deprecated. Maybe remove this autocmd is better.

Freed-Wu avatar Aug 16 '21 13:08 Freed-Wu