Idle Timer Hook?

I’m currently trying to write a plugin that highlights the current line after the user idles for a few seconds. NormalIdle and InsertIdle hooks seem to execute the moment I stop doing anything (not a critique of the hook design, I understand why they behave this way and don’t think this behavior should change). The docs refer to these hooks as executing after a ‘certain duration of time’. Is there any way to set that timeout? A different way to emulate the behavior I described?

As explained in this thread:

the duration of time you are referring to is the value in milliseconds of the idle_timeout option. The default is 50 ms, which you experience as instantaneous and is too short for your use case. Change it to something bigger, for example 2 seconds:

set-option global idle_timeout 2000

and the behavior of your pluging should improve. See :doc options for more detailed info.

2 Likes

Thank you so much, that’s exactly what I was looking for.

For what it’s worth, I’m unsure if I would have been able to find this organically in the docs.

I have sympathy… :wink: When you are stuck, :doc options is a last-resort maneuver, but it often pays.

idle_timeout affects the timeout everywhere though and idling is used by a number of plugins, e.g. for generating completions. You might run into issues with even set-option window down the line.

You are right, the same idle_timeout variable is used by NormalIdle and InsertIdle. It would be good if different options were used for different purposes. Perhaps this should be filed under Complaints and Improvements?

It would be useful. Could be something like the special ui_options option

declare-option str-to-int-map idle_timeout default=50

set-option -add global idle_timeout completions_timeout=100
set-option -add global idle_timeout highlight_line_timeout=2000

hook global InsertIdle 'completions_timeout' %{
    eval %sh{generate-completions ... }
}

hook global InsertIdle 'highlight_line_timeout' %{
    add-higlighter...
}

I think OP could do something like

declare-option int timeout_temp
hook global InsertIdle %{
    set window timeout_temp %opt{idle_timeout}
    set window idle_timeout 1950 # maybe replace with an %sh to do math
    # if the second idle hook goes off add highlighter and reset idle
    hook -once window InsertIdle %{
        set global idle_timeout %opt{timeout_temp}
        add-highlighter ...
    }
    # when the user does something reset timeout
    # could have already been done. rm highlighter if it exists.
    hook -once window RawKey * %{
        set global idle_timeout %opt{timeout_temp}
        try %{ rm-highlighter ... }
    }
}

but this solution does not scale if other plugins use the same system. I also don’t know if the idle would re-trigger with the changed timeout or if Kakoune considers it still Idle from the first.