;; ZSA Voyager configuration file.
;;

;; Adapted from https://github.com/linkarzu/dotfiles-latest/blob/main/kanata/configs/macbook-subl.kbd
;; Video related to this file
;; https://youtu.be/jvJ3f4HHiMY

;; Kanata docs
;; https://github.com/jtroo/kanata/blob/main/docs/config.adoc

;; OS key code mappings
;; https://github.com/jtroo/kanata/blob/main/parser/src/keys/mod.rs

(defcfg
  process-unmapped-keys yes
  log-layer-changes no
  danger-enable-cmd yes
  concurrent-tap-hold yes
  rapid-event-delay 3
  allow-hardware-repeat false

  ;; When any non-chord activation happens, this timeout begins. Until this timeout expires
  ;; all inputs will immediately skip chords processing and be processed by the active layer.
  ;; chords-v2-min-idle 250
)

;; Variables
(defvar
 tap-higher 400
 tap-time 200
 tap-time-plus 300
 hold-time-plus 300
 hold-time 200
 chord-time 60
 subl-time 200
 tmux-delay 40
)

;; Only one defsrc is allowed.
;;
;; defsrc defines the keys that will be intercepted by kanata. The order of the
;; keys matches with deflayer declarations and all deflayer declarations must
;; have the same number of keys as defsrc. Any keys not listed in defsrc will
;; be passed straight to the operating system.
(defsrc
;;----------------LEFT-------------------;------------------RIGHT-------------------;;
  =      1    2    3    4    5                6    7    8    9    0    -
  grv    q    w    e    r    t                y    u    i    o    p    \
  esc    a    s    d    f    g                h    j    k    l    ;    '
  lsft   z    x    c    v    b                n    m    ,    .    /    rsft
                            ret tab     bspc spc
)

(defalias
  ;; Modifier keys - Left
  mlctl (tap-hold $tap-time $hold-time a lctl)
  mlalt (tap-hold $tap-time $hold-time s lalt)
  mlmet (tap-hold $tap-time $hold-time d lmet)
  mlsft (tap-hold $tap-time $hold-time f lsft)

  ;; Modifier keys - Right
  mrsft (tap-hold $tap-time $hold-time j rsft)
  mrmet (tap-hold $tap-time $hold-time k rmet)
  mralt (tap-hold $tap-time $hold-time l ralt)
  mrctl (tap-hold $tap-time $hold-time ; rctl)

  mretu (tap-hold $tap-time $hold-time tab ret)

  ;; number and symbols layer trigger keys.
  mlnums (tap-hold-press $tap-time-plus $hold-time-plus ret (layer-while-held nums_and_symbols))
  mrnums (tap-hold-press $tap-time-plus $hold-time-plus spc (layer-while-held nums_and_symbols))

  ;; mouse layer trigger keys.
  mlms (tap-hold-press $tap-time $hold-time esc (layer-while-held mouse))
  mrms (tap-hold-press $tap-time $hold-time ' (layer-while-held mouse))

  ;; Multi
  mhypr (tap-hold $tap-time $hold-time bspc (multi lsft lctl lalt lmet)) ;; Hyper key

  ;; Sublayers
  ;; I implemented sublayers thanks to the help in this discussion
  ;; https://github.com/jtroo/kanata/discussions/1765
  ;; DO NOT USE THE SUBLAYERS IN A FREQUENTLY PRESSED KEY LIKE SPACEBAR
  ;; It made typing spaces a pain in the ass, enter is not pressed to often, so it works way better
  ;; msubl (tap-hold-press $subl-time $subl-time ret (layer-while-held sublayers))

  ;; Execute script
  ;; maltt (cmd open "btt://execute_assigned_actions_for_trigger/?uuid=F35EF770-FAA5-448A-957D-70BB449DEB0F")

  ;; key and script at the same time
  ;; maltt (tap-hold $tap-time $hold-time lsft (cmd open "btt://execute_assigned_actions_for_trigger/?uuid=F35EF770-FAA5-448A-957D-70BB449DEB0F"))
)

;; BLANK LAYER
;; (deflayer {{NAME}}
;;;;----------------LEFT-------------------;------------------RIGHT-------------------;;
;;   =      1    2    3    4    5                  6    7    8    9    0    -
;;   grv    q    w    e    r    t                  y    u    i    o    p    \
;;   esc    a    s    d    f    g                  h    j    k    l    ;    '
;;   lsft   z    x    c    v    b                  n    m    ,    .    /    rsft
;;                             ret  tab      bspc spc 
;; )

;; Main layer
(deflayer main
;;----------------LEFT----------------------------;-------------------------RIGHT---------------------------;;
  =      1       2      3      4       5                      6      7       8       9       0       -
  grv    q       w      e      r       t                      y      u       i       o       p       \
  @mlms  @mlctl  @mlalt @mlmet @mlsft  g                      h      @mrsft  @mrmet  @mralt  @mrctl  @mrms
  lsft   z       x      c      v       b                      n      m       ,       .       /        rsft
                                    @mlnums tab     @mhypr @mrnums
)

(defalias
  ;; Number layer modifier keys - Left
  nlctl (tap-hold $tap-time $hold-time [ lctl)
  nlalt (tap-hold $tap-time $hold-time ] lalt)
  nlmet (tap-hold $tap-time $hold-time S-[ lmet)
  nlsft (tap-hold $tap-time $hold-time S-] lsft)
)

;; Number and Symbols layer
(deflayer nums_and_symbols
;;----------------LEFT---------------------------------;-------------------RIGHT-------------------;;
  =      XX      XX     XX     XX        XX                    XX     XX   XX   XX   XX   -
  S-grv  S-1     S-9    S-0    S-2       XX                    -      1    2    3    \    \
  XX     @nlctl  @nlalt @nlmet @nlsft    XX                    S-=    4    5    6    .    '
  lsft   S-7     S-8    S-3    S-4       XX                    S-8    7    8    9    /    rsft
                                         @mlnums tab       @mhypr 0
)

(defalias
  mwu (mwheel-up 50 120)
  mwd (mwheel-down 50 120)
  mwl (mwheel-left 50 120)
  mwr (mwheel-right 50 120)

  ma↑ (movemouse-accel-up 5 1000 1 3)
  ma← (movemouse-accel-left 5 1000 1 3)
  ma↓ (movemouse-accel-down 5 1000 1 3)
  ma→ (movemouse-accel-right 5 1000 1 3)

  sm (setmouse 32228 32228)

  fst (movemouse-speed 200)
)

;; Mouse and arrow layer
(deflayer mouse
;;----------------LEFT-------------------;------------------RIGHT-------------------;;
  XX     brdn bru  XX   XX   XX                  XX    XX    mute  vold   volu    XX
  XX     XX   XX   ▲    XX   XX                  @mwl  @mwd  @mwu  @mwr   XX      XX
  XX     XX   ◀    ▼    ▶    XX                  @ma←  @ma↓  @ma↑  @ma→   XX      XX
  XX     XX   XX   XX   XX   XX                  XX    XX    XX    XX     XX      XX
                             ret  tab      mltp mrtp 
)
