Repository: jtroo/kanata (Rust)

Issue:
defoverrides is ignored in unmod and unshift
### Requirements

- [X] I've searched [platform-specific issues](https://github.com/jtroo/kanata/blob/main/docs/platform-known-issues.adoc), [issues](https://github.com/jtroo/kanata/issues) and [discussions](https://github.com/jtroo/kanata/discussions) to see if this has been reported before.

### Describe the bug

When using `unshift` or `unmod` the `defoverrides` keys are ignored, and the original key is used instead.

### Relevant kanata config

```
(defoverrides
    (a)   (b)
    (b)   (a)
)

(defalias
    testb (unshift b)) ;; --> a if pressed while shift is held
    testa (unshift a)) ;; --> b if pressed while shift is held
)
```
### To Reproduce

1.  Try above configuration
2. Check the output is how described in comment (a and b are reversed)


### Expected behavior

The key taken into account by `unshift` or `unmod` should be the ones in defoverrides.

### Kanata version

kanata 1.6.1-prerelease-1

### Operating system

MacOs



Discussion:
Oh in fact I just found a workaround, this works
```
(defalias
    testb (multi (unshift f24) b) ;; --> b if pressed while shift is held 🎉 
)
```


Hm I wonder if this would be fixed simply by moving:

https://github.com/jtroo/kanata/blob/e04aef6a0f85fc19d7bc31d9d11be360951e6fad/src/kanata/mod.rs#L874-L880

Down to line 935 instead.



Patch:
diff --git a/src/kanata/mod.rs b/src/kanata/mod.rs
index 7b30a6d8d..365d93303 100755
--- a/src/kanata/mod.rs
+++ b/src/kanata/mod.rs
@@ -872,13 +872,6 @@ impl Kanata {
         let mut live_reload_requested = false;
         let cur_keys = &mut self.cur_keys;
         cur_keys.extend(layout.keycodes());
-        self.overrides
-            .override_keys(cur_keys, &mut self.override_states);
-        if let Some(caps_word) = &mut self.caps_word {
-            if caps_word.maybe_add_lsft(cur_keys) == CapsWordNextState::End {
-                self.caps_word = None;
-            }
-        }
 
         // Deal with unmodded. Unlike other custom actions, this should come before key presses and
         // releases. I don't quite remember why custom actions come after the key processing, but I
@@ -934,6 +927,14 @@ impl Kanata {
             cur_keys.extend(self.unshifted_keys.iter());
         }
 
+        self.overrides
+            .override_keys(cur_keys, &mut self.override_states);
+        if let Some(caps_word) = &mut self.caps_word {
+            if caps_word.maybe_add_lsft(cur_keys) == CapsWordNextState::End {
+                self.caps_word = None;
+            }
+        }
+
         // Release keys that do not exist in the current state but exist in the previous state.
         // This used to use a HashSet but it was changed to a Vec because the order of operations
         // matters.


Test patch:
diff --git a/src/tests/sim_tests/mod.rs b/src/tests/sim_tests/mod.rs
index 950e53c3e..ff719b7ad 100644
--- a/src/tests/sim_tests/mod.rs
+++ b/src/tests/sim_tests/mod.rs
@@ -14,6 +14,7 @@ mod block_keys_tests;
 mod capsword_sim_tests;
 mod chord_sim_tests;
 mod layer_sim_tests;
+mod override_tests;
 mod repeat_sim_tests;
 mod seq_sim_tests;
 mod switch_sim_tests;
diff --git a/src/tests/sim_tests/override_tests.rs b/src/tests/sim_tests/override_tests.rs
new file mode 100644
index 000000000..28524217c
--- /dev/null
+++ b/src/tests/sim_tests/override_tests.rs
@@ -0,0 +1,27 @@
+use super::*;
+
+#[test]
+fn override_with_unmod() {
+    let result = simulate(
+        "
+(defoverrides
+ (a) (b)
+ (b) (a)
+)
+
+(defalias
+ b (unshift b)
+ a (unshift a)
+)
+(defsrc a b)
+(deflayer base @a @b)
+        ",
+        "d:lsft d:a t:50 u:a t:50 d:b t:50 u:b t:50 u:lsft",
+    )
+    .to_ascii()
+    .no_time();
+    assert_eq!(
+        "dn:LShift up:LShift dn:B up:B dn:LShift up:LShift dn:A up:A dn:LShift",
+        result
+    );
+}