tmalsburg / guess-language.el

Emacs minor mode that detects the language you're typing in. Automatically switches spell checker. Supports multiple languages per document.
115 stars 14 forks source link

Use idle-timer to trigger flyspell #32

Closed smoeding closed 3 years ago

smoeding commented 3 years ago

I experience freezing buffers after switching to guess-language. Unfortunately I can't present a simple reproducible example. But I think it might be related to what was discussed in #26.

Sometimes loading a file using a mode that enables guess-language and flyspell just hangs. I have to "C-g" and then the buffer appears as it should be. I believe it is caused by some other code (e.g. font-lock) or packages (e.g. yasnippet) in combination with the guess-language code. Obviously this makes it difficult for you to find the issue.

One of the things I tried was replacing the run-at-time call with run-with-idle-timer. Indeed it seemed to have solved my problem!

I tried to find out the difference and someone on the Emacs Help mailing list helped to create an example explaining the difference of these two functions. Run the following code and look at the Messages buffer:

(progn
  (message "start")
  (run-at-time 0 nil 'message "running at time")
  (run-with-idle-timer 0 nil 'message "running at idle time")
  (redisplay)                           ; (sit-for 0)
  (message "after redisplay")
  (message ""))

The "running at time" message will appear before the "after redisplay" message while "running at idle time" will appear later. redisplay will cause the run-at-time timer to run!

My explanation for my problem: your code currently uses the run-at-time function to trigger flycheck. At least one of the packages I use has a call to redisplay or a similar function which causes the timer to execute the lambda. Unfortunately the buffer or the hunspell process is not ready at this point and that somehow causes the freeze for me.

Switching to run-with-idle-timer would only run the timer if Emacs becomes idle (meaning: waiting for user input). As I understand this is what you are trying to do here.

tmalsburg commented 3 years ago

Thanks for the PR and the explanation. Sounds plausible. Once question before I merge. Do you experience the issue with the GitHub version of guess-language or the version from ELPA? The latter is outdated (need to fix that).

smoeding commented 3 years ago

I'm using MELPA. The package is in a directory guess-language-20210131.1100 so I guess it should be a pretty recent version.

tmalsburg commented 3 years ago

Thanks. Will test later and then merge.

tmalsburg commented 3 years ago

Merged, thank you!

smoeding commented 3 years ago

Thx for pushing to MELPA!