vim-utils / vim-troll-stopper

Stop Unicode trolls from messing with your code.
MIT License
177 stars 4 forks source link

Try to fix lag issue when switching between a lot of buffers #1

Closed lacygoill closed 8 years ago

lacygoill commented 8 years ago

Hello,

Thank you for this very useful vim plugin. I'm new to github and I don't know a lot about vimscript, however it seems to me that the plugin causes some lag when I switch between a lot of buffers.

I think it comes from the fact that the more I switch between buffers the more the HighlighTrolling() function piles highlight patterns.

At least, that's what I've been told on vi.stackexchange.com.

I fixed the issue by changing the event that triggers the autocommand that calls the function from BufEnter to BufNewFile,BufRead. It is not relevant to the issue, but I also wrapped the autocmd inside an augroup and cleared it with autocmd!.

I don't know if it's correct, but it seems to work on my machine, so I thought I could propose you this pull request.

Thank you for having read my message, have a good day.

bruno- commented 8 years ago

Hey, first of all: welcome to github and congrats on a pull request!

You are right, the approach proposed in your PR is correct, I just merged it.

bruno- commented 8 years ago

Hi, after using the plugin for a couple more days, I noticed a couple more downsides:

  1. when empty vim is started (eg just $ vim in the command line), and then some text with troll characters is pasted in the buffer - those troll chars aren't highlighted
  2. when a file with troll chars is opened, then a vim command :split is invoked, new split buffer does NOT highlight troll characters.

I spent quite some time working on this, but I think I got it right with 8a53b5b5bf26a070419ccaf9cbfccc6cf6cb7cfc.

@chenoweth if you have some time, please try it and let me know how things work. This new solution shouldn't be slow either.

lacygoill commented 8 years ago

You're right, I've just checked and can confirm that the old version of the plugin, in the scenarios you're describing, did not detect troll characters. Now with the new version, they're always detected without reintroducing the lag that I experienced before.

Sato's answer helped me realise that there were no duplicate autocommands, but duplicate calls to the matchadd() function. Your answer explains how to make the plugin detect troll characters in all scenarios and how to limit calls to the matchadd() function by using a variable local to the window (w:guard_var) whose purpose is to remember whether the function has already been called for the current window.

Both your answers are helping me better understand how vim and vimscript work.

Since only one answer can be marked as the solution to a topic, I've marked your answer as the most helpful on vi.stackexchange.com so that other people interested in the subject see your post first.

I tried answering on the site, but it won't let me (it says my message is too long), so I do it here.

When my skills in vimscript will be better, if I can contribute to the plugin, I will.

Thank you for having accepted my first PR, that was nice.