jtroo / kanata

Improve keyboard comfort and usability with advanced customization
GNU Lesser General Public License v3.0
3.23k stars 131 forks source link

Feature request: Compile-time conditional mappings #1109

Open oblitzitate opened 5 months ago

oblitzitate commented 5 months ago

Is your feature request related to a problem? Please describe.

I issued a feature request to allow to check the active layer inside a switch, which got merged. Now while I liked that you can use it to reduce the amount of aliases you need, I disliked that it added a bit of run-time logic via layer checks. So I figured why not have a feature that can reduce both: compile-time conditional mappings

Describe the solution you'd like.

With such a feature, you'd be able to define what a key does at compile-time:

(defalias
    lft (comptime (switch
        ((layer alt-layer)) (unmod left) break
        ((layer one-shot-navigation-layer)) (multi left @virtual-one-shot-navigation) break
        () left break
    ))
)

Under the comptime context, switch only allows compile-time conditions (e.g. layer, window manager, OS, hardware keyboard model, etc.). If you want, you can use another keyword like cond instead of switch to differentiate it from the run-time version.

Now given the following...

(deflayermap (alt-layer)
    h @lft
)

(deflayermap (one-shot-navigation-layer)
    h @lft
)

(deflayermap (adl-navigation-layer)
    h @lft
)

...when kanata starts, it would check the layers in which @lft is mapped. Instead of keeping the switch logic at run-time, @lft will be converted to the following:

(unmod left) ;; for the alt-layer

(multi left @virtual-one-shot-navigation) ;; for the one-shot-navigation-layer

left ;; for any other layers

That way, you reduce the amount of aliases you need without the cost of unnecessary run-time logic.

Describe alternatives you've considered.

Using switch with layer checks.

Additional context

No response

jtroo commented 5 months ago

To help justify this feature, it would be good to measure the CPU time difference between the runtime evaluation of

(defalias
    lft (switch
        ((layer alt-layer)) (unmod left) break
        ((layer one-shot-navigation-layer)) (multi left @virtual-one-shot-navigation) break
        () left break
    )
)

vs its the worst-case time difference:

(defalias
    lft left
)