yqrashawn / GokuRakuJoudo

config karabiner with ease
GNU General Public License v3.0
1.14k stars 121 forks source link

Can't combine multiple application/anti-application rules #189

Open januz opened 1 year ago

januz commented 1 year ago

I was very happy to find goku. Makes using Karabiner Elements much easier! I have the following issue though: I would like to remap a key combination only if several conditions are met

Based on the tutorial, I thought this would be possible with

{
 :applications
 {
  :kitty    ["^net\\.kovidgoyal\\.kitty$"]
  :terminal ["^$com\\.apple\\.Terminal"]
  :vimr     ["^$com.\\qvacua.\\VimR"]
 }
 :main
 [
  {
   :des "simultaneous jk/kj press to enter kindaVim normal mode"
   :rules [[[:j :k] :!!equal_sign [:!kVNormal :!kitty :!terminal :!vimr]]]
  }
 ]
}

or

...
  {
   :des "simultaneous jk/kj press to enter kindaVim normal mode"
   :rules [[[:j :k] :!!equal_sign ["kVNormal" 0 :!kitty :!terminal :!vimr]]]
  }
...

but neither seems to work. If I only use the variable condition based on kVNormal, the mapping works (but I need it not to be active in certain applications).

Thanks for your help!

bangedorrunt commented 1 year ago

@januz, you could try

  {
   :des "simultaneous jk/kj press to enter kindaVim normal mode"
   :rules [:!kVNormal
           [[:j :k] :!!equal_sign [:!kitty :!terminal :!vimr]]]
  }

@yqrashawn, I have similar issue with combining several conditions, for example below is not working

:main [{:des "loremipsum"
        :rules [:g-mode
                [:slash :!Cslash [:Element :Discord]]]}]

:applications {:Discord   ["^com\\.hnc\\.Discord$"]
               :Element   ["^im\\.riot\\.app$"]}

}
yqrashawn commented 1 year ago

Seems goku don't support combine multiple application conditions like this. You can combine multiple regex as one app for this purpose for now. like this one

yqchilde commented 1 year ago

I have also encountered this problem, so let's regroup.

januz commented 1 year ago

@yqrashawn

You can combine multiple regex as one app for this purpose for now

I tried this but it also did not work for me. Currently, I don't know whether there is a way to get the behavior I would like to have.

Thanks for looking into it!

bangedorrunt commented 1 year ago

@januz this should work, mind sharing your config so we can troubleshoot it.

januz commented 1 year ago

@bangedorrunt I have this in my karabiner.edn:

{
 :applications
 {
  :kitty    ["^net\\.kovidgoyal\\.kitty$"]
  :terminal ["^$com\\.apple\\.Terminal"]
  :vimr     ["^$com.\\qvacua.\\VimR"]
  :safari   ["^com\\.apple\\.Safari$"]
  :needjk   [
             "^net\\.kovidgoyal\\.kitty$"
             "^$com\\.apple\\.Terminal"
             "^$com.\\qvacua.\\VimR"
            ]

 }
 :main
 [
  {
   :des "simultaneous jk/kj press to enter kindaVim normal mode"
   :rules [
           [
            [:j :k] :!!equal_sign
            [:!kVNormal :!needjk]
           ]
          ]
  }
  {
   :des "simultaneous jk/kj to exit kindaVim normal mode"
   :rules [
           [
            [:j :k] :i
            [:kVNormal :!needjk]
           ]
          ]
  }
 ]
}

What I want to do is activate KindaVim (by remapping jk/kj to the shortcut I have set up to activate kindaVim, Cmd-Ctrl-Shift-=) if KindaVim is not in normal mode (the kVNormal variable is set by Hammerspoon) AND if the application is not one that has their own internal jk/kj remapping, e.g., kitty, VimR, etc.). The same for the second mapping for deactivating KindaVim.

Please let me know if I'm understanding something incorrectly and thanks for your help!

bangedorrunt commented 1 year ago

@januz I don't think kvNormal will communicate with Karabiner as you expected. You can open Karabiner Event Viewer to see list of available variables for your current setup. I'm not with computer and not using KindaVim atm but I believe we could have a workaround to resolve your prob https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/conditions/variable/#confirm-the-current-variable-values

januz commented 1 year ago

@bangedorrunt It works if I just use [:kvNormal] so the variable can definitely be seen by Karabiner Elements. The problem only occurs if I combine this condition with an application condition (because I don't want the mapping to be effective in certain applications). Thanks for your help!

bangedorrunt commented 1 year ago

Oh, it's interesting, first time I learnt that we can use shell variables as condition. So you can try the following to see if it works. Sorry I'm on mobile

{
 :applications
 {
  :kitty    ["^net\\.kovidgoyal\\.kitty$"]
  :terminal ["^$com\\.apple\\.Terminal"]
  :vimr     ["^$com.\\qvacua.\\VimR"]
  :safari   ["^com\\.apple\\.Safari$"]
  :needjk   [
             "^net\\.kovidgoyal\\.kitty$"
             "^$com\\.apple\\.Terminal"
             "^$com.\\qvacua.\\VimR"
            ]

 }
 :main
 [
  {
   :des "simultaneous jk/kj press to enter kindaVim normal mode"
   :rules [
           :needjk
           [
            [:j :k] :!!equal_sign
            :!kVNormal
           ]
          ]
  }
  {
   :des "simultaneous jk/kj to exit kindaVim normal mode"
   :rules [
           :!needjk
           [
            [:j :k] :i
            :kVNormal
           ]
          ]
  }
 ]
}
januz commented 1 year ago

Oh, it's interesting, first time I learnt that we can use shell variables as condition

Well, it's not a shell variable. It's a Karabiner variable that is set using Hammerspoon as described here

can try the following to see if it works

Unfortunately, no. It has the same effect, i.e., the mapping not being active at all, independent of current application.

yqrashawn commented 1 year ago

Hi @bangedorrunt, the application identifier "^$com\\.apple\\.Terminal" is a regex expression. "^$com\\.apple\\.Terminal" should be changed to "^com\\.apple\\.Terminal$" ^ is indicating to match the start $ is to match the end

yqrashawn commented 1 year ago

@bangedorrunt You can find these (some of) identifiers in my config https://github.com/yqrashawn/yqdotfiles/blob/1b52767ec4f539926ac997cdffd8b3115700d83e/modules/yqrashawn/home-manager/dotfiles/karabiner.edn#L805

bangedorrunt commented 1 year ago

It's my bad that overlooked the incorrect patterns. Yes, I'm well aware of regex syntax. I believe this could fix @januz issue. Many thanks @yqrashawn for clarification. Goku is simply the best 😘

januz commented 1 year ago

@yqrashawn Thanks for noticing the typos in the regexes. However fixing them does not resolve the problem:

Thanks for your help!

januz commented 1 year ago

@yqrashawn Sorry to bug you again with this issue but is there any way I can use Goku to specify conditions that combine variable and application conditions?