forked from yqrashawn/GokuRakuJoudo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_symbols.clj
252 lines (245 loc) Β· 10.8 KB
/
keys_symbols.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
; Add various key symbols to be used instead of capitalized letters
(ns karabiner-configurator.keys-symbols
(:require
[clojure.string :as string]
[clojure.core.match :refer [match]]))
(import [java.util.regex Pattern])
; S T O C β left_shift left_control left_option left_command
; R W E Q β right_shift right_control right_option right_command
; SS β shift ...
; labels must = same position
(def modi-sym ["β§" "β" "β" "β" "β₯" "β" "β" "β" ])
(def modi-βΉl ["S" "T" "T" "O" "O" "C" "C" "C" ])
(def modi-lβΊ ["R" "W" "W" "E" "E" "Q" "Q" "Q" ])
(def modi-lβ ["SS" "TT" "TT" "OO" "OO" "CC" "CC" "CC" ])
(def modi-lβ-as-key ["shift" "control" "control" "option" "option" "command" "command" "command" ])
(def βΉkey ["βΉ" "'"])
(def keyβΊ ["βΊ" "'"])
(def keyοΉ ["οΉ" "?"])
(def modi-βΉl-as-key (mapv #(str "left_" %) modi-lβ-as-key))
(def modi-lβΊ-as-key (mapv #(str "right_" %) modi-lβ-as-key))
(def keys-symbols-other {
"π" "!F","Ζ""!F","β""!F","β»""!F","π΅""!F","π
""!F","π
΅""!F"
"βͺ" "P" ; capslock
"β" "!A" ; any regardless of side
"β±" "!!" ; hyper
"βοΉ" "##","β?""##","οΉοΉ""##","??""##"; optional any
"οΈ" "semicolon"
"β" "quote","β""quote","οΌ""quote","Β«""quote","Β»""quote"
"⧡" "backslash","\""backslash"
"οΉ¨" "non_us_backslash"
"γ" "open_bracket" ,"γ""open_bracket" ,"γ""open_bracket" ,"β‘""open_bracket"
"γ" "close_bracket","γ""close_bracket","γ""close_bracket","β£""close_bracket"
"βΈ΄" "comma"
"οΌ" "period"
"β" "slash"
"β" "escape"
"βΎ" "tab","βΉ""tab"
"β" "hyphen"
"β" "equal_sign"
"Β£" "non_us_pound"
"β" "page_up","β""page_down"
"β€" "home","β₯""end","β€""home","β€""end","β""home","β""end",
"β" "insert",
"Λ" "grave_accent_and_tilde","Λ""grave_accent_and_tilde"
"β " "spacebar","β£""spacebar"
"β" "delete_or_backspace","β«""delete_or_backspace"
"β‘" "delete_forward","β¦""delete_forward"
"β" "return_or_enter","β©""return_or_enter","β€""return_or_enter","β€""return_or_enter",
"β²" "up_arrow","βΌ""down_arrow","β""left_arrow","βΆ""right_arrow"
"Β " "" ; no-break space removed, used only for rudimentary alignment
"π’β""keypad_1","π’β""keypad_2","π’β""keypad_3","π’β""keypad_4","π’β
""keypad_5"
"π’β""keypad_6","π’β""keypad_7","π’β""keypad_8","π’β""keypad_9","π’β""keypad_0"
"π’β""keypad_equal_sign","π’β""keypad_hyphen","π’β""keypad_plus"
"π’β""keypad_slash","π’οΌ""keypad_period","π’β""keypad_asterisk","π’β""keypad_enter"
"π""volume_up" ,"π+""volume_up" ,"πβ""volume_up" ,"πβ""volume_up" ,"πβ""volume_up"
"π""volume_down","πβ""volume_down","πβ""volume_down","πβ""volume_down","πβ""volume_down"
"π""mute","πβͺ""mute","πβΏ""mute","πβ""mute"
"π""vk_consumer_brightness_up"
"π
""vk_consumer_brightness_down"
"β¨π‘+""vk_consumer_illumination_up" ,"β¨π‘β""vk_consumer_illumination_up" ,"β¨π‘β""vk_consumer_illumination_up" ,"β¨π‘β""vk_consumer_illumination_up"
"β¨π‘β""vk_consumer_illumination_down","β¨π‘β""vk_consumer_illumination_down","β¨π‘β""vk_consumer_illumination_down","β¨π‘β""vk_consumer_illumination_down"
"β¦""vk_launchpad"
"π""vk_dashboard"
"ββ―""vk_mission_control"
"ββ""vk_consumer_previous"
"βΆβΈ""vk_consumer_play"
"βΆβΆ""vk_consumer_next"
"β€""application","β°""application","π""application"
"π°1" "button1" ,"π°2" "button2" ,"π°3" "button3" ,"π°4" "button4" ,"π°5" "button5"
"π°6" "button6" ,"π°7" "button7" ,"π°8" "button8" ,"π°9" "button9" ,"π°10""button10"
"π°11""button11","π°12""button12","π°13""button13","π°14""button14","π°15""button15"
"π°16""button16","π°17""button17","π°18""button18","π°19""button19"
"π°21""button21","π°22""button22","π°23""button23","π°24""button24","π°25""button25"
"π°26""button26","π°27""button27","π°28""button28","π°29""button29","π°30""button30"
"π°31""button31","π°32""button32"
})
(def keys-symbols-generated (into {} (
mapcat (fn [mod]
(mapcat (fn [βΉ]
(mapcat (fn [βΊ]
(map (fn [οΉ]
(def mI (.indexOf modi-sym mod)) ; modifier index to pick labels (which have the same position)
(def βΉl (nth modi-βΉl mI))
(def lβΊ (nth modi-lβΊ mI))
(def lβ (nth modi-lβ mI))
(match [ ; !mandatory ; #optional
(some? βΉ) (some? βΊ) (some? οΉ)]
[true false false] {(str βΉ mod ) (str "!" βΉl)}
[false true false] {(str mod βΊ ) (str "!" lβΊ)}
[false false false] {(str mod ) (str "!" lβ)}
[true false true] {(str βΉ mod οΉ ) (str "#" βΉl)}
[false true true] {(str mod βΊ οΉ ) (str "#" lβΊ)}
[false false true] {(str mod οΉ ) (str "#" lβ)}
:else nil
)) (concat keyοΉ '(nil))
)) (concat keyβΊ '(nil))
)) (concat βΉkey '(nil))
)) modi-sym))
)
(def keys-symbols-generated-modi-as-key (into {} (
mapcat (fn [mod]
(mapcat (fn [βΉ]
(map (fn [βΊ]
(def mI (.indexOf modi-sym mod)) ; modifier index to pick labels (which have the same position)
(def βΉl (nth modi-βΉl-as-key mI))
(def lβΊ (nth modi-lβΊ-as-key mI))
(def lβ (nth modi-lβ-as-key mI))
(match [ ; !mandatory ; #optional
(some? βΉ) (some? βΊ)]
[true false ] {(str βΉ mod ) βΉl}
[false true ] {(str mod βΊ ) lβΊ}
[false false ] {(str mod ) lβ}
:else nil
)) (concat keyβΊ '(nil))
)) (concat βΉkey '(nil))
)) modi-sym))
)
(def keys-symbols-other-modi-as-key {
"βͺ" "caps_lock"
})
(def keys-symbols-unordered (merge keys-symbols-generated keys-symbols-other ))
(def keys-symbols-unordered-modi-as-key (merge keys-symbols-generated-modi-as-key keys-symbols-other-modi-as-key))
; Sort by key length (BB > A) to match β§βΊ before β§
(defn sort-map-key-len
([m ] (sort-map-key-len m "asc"))
([m ord] (into
(sorted-map-by (fn [key1 key2] (
compare
(if (or (= ord "asc") (= ord "β")) [(count (str key1)) key1] [(count (str key2)) key2])
(if (or (= ord "asc") (= ord "β")) [(count (str key2)) key2] [(count (str key1)) key1])
))) m)))
(def keys-symbols (sort-map-key-len keys-symbols-unordered "β"))
(def keys-symbols-modi-as-key (sort-map-key-len keys-symbols-unordered-modi-as-key "β"))
(defn replace-map-h "input string + hash-map β string with all map-keys β map-values in input"
[s_in m_in & {:keys [modi-as-key] :or {modi-as-key nil}}]
(def keys_in (keys m_in)) ;{"β""A","β""C"} β "β""β"
(if modi-as-key ;
(def keys_in_q (map #(str #"(?<!_)" (Pattern/quote %) "$") keys_in)) ;"\\Qβ\\E" "\\Qβ\\E" + "$" if passed
(def keys_in_q (map #(str #"(?<!_)" (Pattern/quote %) ) keys_in)) )
(def keys_in_q_or (interpose "|" keys_in_q)) ;"\\Qβ\\E""|""\\Qβ\\E"
(def keys_in_q_s (apply str keys_in_q_or)) ;"\\Qβ\\E|\\Qβ\\E"
(def keys_in_re (re-pattern keys_in_q_s)) ;#"\Qβ\E|\Qβ\E"
(string/replace s_in keys_in_re m_in)
)
(defn contains-in?
[m ks]
(not= ::absent (get-in m ks ::absent)))
(defn update-in-if-has
[m_ ks f & args]
(def m m_)
(mapv #(if (contains-in? m [%])
(def m (apply (partial update-in m [%] f) args))
)
ks)
m)
(defn despace "replace all whitespace" [str_in]
(string/replace str_in #"[\s]" "")
)
(defn key-name-sub-or-self [k]
(if (keyword? k)
(do
(def k1 (replace-map-h k keys-symbols-modi-as-key :modi-as-key true)) ; first replace modi-as-keys
(def k2 (replace-map-h k1 keys-symbols)) ; then replace other key symbols
(keyword (string/replace k2 #"^:" ""))
)
(if (map? k)
(update-in-if-has k [:key :sim] key-name-sub-or-self)
(if (vector? k)
(mapv #(key-name-sub-or-self %) k)
(if (and (string? k) (string/starts-with? k "β"))
(key-name-sub-or-self (keyword (string/replace (despace k) #"^β" "")))
k
)
)
)
)
)
(def modi-re #"[ACSTOQWERFP]+")
(defn find-modi
[s prefix matches-found]
(def modi-prefix-re (re-pattern (str prefix modi-re)))
(def modi-match (re-find modi-prefix-re s))
(if (nil? modi-match)
[s matches-found]
(do
(if (vector? modi-match) ; [full-match G1...]
(def modi-match-str (first modi-match))
(def modi-match-str modi-match )
)
(def matches-found-cc (conj matches-found modi-match-str))
(recur (string/replace-first s modi-match-str "") prefix matches-found-cc)
))
)
(defn move-modi-front
"Replace individual modifiers `!CC!TT` (prefix=!) into a single group with one `!CCTT`"
[k prefix]
(def s (if (keyword? k)
(name k)
(if (string? k)
k
nil
)))
(if (string? s) ; skip non-string keys like ["var" 1]
(do
(def modi-must [])
(let [[s-remain modi-must] (find-modi s prefix modi-must)]
(def modi-must-str
(if (empty? modi-must)
""
(str prefix (string/replace (string/join "" modi-must) prefix "")) ; :!CC!AA β :!CCAA
))
(keyword (str modi-must-str s-remain))
))
k
))
(defn move-modi-mandatory-front
[k]
(def prefix "!")
(move-modi-front k prefix)
)
(defn move-modi-optional-front
[k]
(def prefix "#")
(move-modi-front k prefix)
)
(defn move-modi-prefix-front
[k]
(if (vector? k)
(mapv #(move-modi-mandatory-front %) (mapv #(move-modi-optional-front %) k))
( move-modi-mandatory-front (move-modi-optional-front k))
))
(defn key-sym-to-key
"Takes key with symbols as input and returns keys without; optional :dbg debug print value"
[k & {:keys [dbg] :or {dbg nil}}]
(def sub1 (key-name-sub-or-self k))
(if (not= k sub1)
(if (map? sub1)
(def sub (update-in-if-has sub1 [:key :sim] move-modi-prefix-front))
(def sub (move-modi-prefix-front sub1))
)
(def sub sub1 )
)
; (if (and (some? dbg) (not= k sub)) (println (str " " dbg "Β¦" k " βΆβΆβΆ " sub)))
sub
)