-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
341 lines (287 loc) · 11.4 KB
/
config.py
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import itertools
import os
import sys
from enum import Enum
from pathlib import WindowsPath
from typing import Any, Callable, List, Optional, Tuple
class Platform(Enum):
"""The platform for support of Keyhac."""
WINDOWS = "windows"
MAC = "mac"
def detect_platform_of_keyhac() -> Platform:
if sys.platform == "win32":
return Platform.WINDOWS
elif sys.platform == "darwin":
return Platform.MAC
raise ValueError(sys.platform)
keyhac_literal_special_source: List[Tuple[str, str]] = [
("[", "OpenBracket"),
("]", "CloseBracket"),
("\\", "BackSlash"),
("`", "BackQuote"),
("'", "Quote"),
(",", "Comma"),
(".", "Period"),
("/", "Slash"),
("-", "Minus"),
("+", "Plus"),
(";", "Semicolon"),
]
""""文字のリテラル表現と、Keyhacの特殊なキー表現の対応リスト。"""
def keyhac_literal_special(literal: str) -> Optional[str]:
"""リテラル表現をKeyhacの表現に変換する。"""
return next((t[1] for t in keyhac_literal_special_source if t[0] == literal), None)
def keyhac_special_literal(special: str) -> Optional[str]:
"""Keyhacの表現をリテラル表現に変換する。"""
return next((t[0] for t in keyhac_literal_special_source if t[1] == special), None)
dvorak = "[]\\`',.pyfgcrl/+aoeuidhtns-;qjkxbmwvz"
"""
USキーボードでDvorakとQwertyで差分が生じそうなリストのDvorak。
Dvorak的には=と+は=をプレーンとするが、Keyhac的には+がプレーン。
"""
qwerty = "-+\\`qwertyuiop[]asdfghjkl;'zxcvbnm,./"
"""USキーボードでDvorakとQwertyで差分が生じそうなリストのQWERTY。"""
def d2q(key: str) -> str:
"""
Dvorak to Qwerty.
"""
literal = keyhac_special_literal(key) or key
try:
fi = dvorak.index(literal)
to = qwerty[fi]
return keyhac_literal_special(to) or to
except ValueError:
return key
def q2d(key: str) -> str:
"""
Qwerty to Dvorak.
"""
literal = keyhac_special_literal(key) or key
try:
fi = qwerty.index(literal)
to = dvorak[fi]
return keyhac_literal_special(to) or to
except ValueError:
return key
process_name_of_linux: List[str] = [
"mstsc.exe", # WSLg
"msrdc.exe", # WSLg
"XWin.exe", # Cygwin/X
"XWin_MobaX.exe", # MobaXterm/X
"XWin_MobaX_1.16.3.exe", # MobaXterm/X
"XWin_Cygwin_1.14.5.exe", # MobaXterm/X
"XWin_Cygwin_1.16.3.exe", # MobaXterm/X
"Xming.exe", # Xming
"vcxsrv.exe", # VcXsrv
"GWSL_vcxsrv.exe", # GWSL
"GWSL_vcxsrv_lowdpi.exe", # GWSL
"X410.exe", # X410
"Xpra-Launcher.exe", # Xpra
]
def check_func_linux(window) -> bool:
"""WSLのプロセスらしいものを検出します。"""
return window.getProcessName() in process_name_of_linux
def check_func_emacs(window) -> bool:
"""
check_func of ActivateWindowCommand argument.
It is based on the Fakeymacs code `https://github.com/smzht/fakeymacs`
"""
return window.getClassName() == "Emacs" or (
window.getProcessName() in process_name_of_linux
and
# ウィンドウのタイトルを検索する正規表現を指定する
# Emacs を起動しているウィンドウを検索できるように、
# Emacs の frame-title-format 変数を設定するなどして、識別できるようにする
window.getText().startswith("emacs ")
)
def set_keymap_dvorak_for_linux(_, keymap_window) -> None:
"""WSLg向けに全てDvorakに変換する。"""
# "C-M-a"みたいなprefixを全て合成する。
modifiers = ["S", "C", "A"]
prefixs = [
"-".join(c)
for n in range(1, len(modifiers) + 1)
for c in itertools.combinations(modifiers, n)
]
for key in dvorak:
f = keyhac_literal_special(key) or key
t = q2d(key)
# 単発のキー入力を変換する。
keymap_window[f] = t
# prefix付きのキー入力を変換する。
for p in prefixs:
keymap_window[p + "-" + f] = p + "-" + t
def set_keymap_weblike(keymap, keymap_window, for_linux=False) -> None:
# Linux(WSLg)はDvorak設定を内部で行わないためキーを変換する必要がある。
# 内部Windowsではそうではないため変換関数を分岐する。
if for_linux:
t = q2d
else:
# Pythonにはビルドインの恒等関数は無いのかな?
t = lambda x: x
keymap_window["C-y"] = "C-" + t("v")
keymap_window["C-g"] = "Esc"
keymap_window["C-Slash"] = "C-" + t("z")
keymap_window["C-a"] = "Home"
keymap_window["C-o"] = "C-" + t("t")
keymap_window["A-o"] = "C-S-" + t("t")
keymap_window["C-e"] = "End"
keymap_window["C-u"] = ["Home", "S-End", "C-" + t("x"), "Delete"]
keymap_window["C-d"] = "Delete"
keymap_window["C-h"] = "Left"
keymap_window["A-h"] = "C-Left"
keymap_window["C-t"] = "Up"
keymap_window["C-n"] = "Down"
keymap_window["C-s"] = "Right"
keymap_window["A-s"] = "C-Right"
keymap_window["A-Minus"] = "C-S-" + t("t")
keymap_window["C-q"] = "C-" + t("w")
keymap_window["C-k"] = ["S-End", "C-" + t("x")]
keymap_window["C-x"] = keymap.defineMultiStrokeKeymap("C-" + t("x"))
keymap_window["C-x"]["C-g"] = "Esc"
keymap_window["C-x"]["h"] = "C-" + t("a")
keymap_window["C-b"] = "Back"
keymap_window["A-b"] = "C-Back"
keymap_window["C-m"] = "Enter"
keymap_window["C-w"] = "C-" + t("x")
keymap_window["A-w"] = "C-" + t("c")
def run_or_raise(
keymap: Any,
exe_name: Optional[str] = None,
class_name: Optional[str] = None,
window_text: Optional[str] = None,
check_func: Optional[Callable[[Any], bool]] = None,
force: bool = False,
command: Optional[str] = None,
param: Optional[str] = None,
) -> Callable[[], Any]:
"""
XMonadの
[runOrRaise](https://hackage.haskell.org/package/xmonad-contrib-0.17.1/docs/XMonad-Actions-WindowGo.html#v:runOrRaise)
をKeyhacに移植する。
既にプロセスやウィンドウが存在すればそれにフォーカスして、
存在しなければ起動する。
Keyhacの型情報を適切に参照するのが難しいため型定義に`Any`がしばしば入る。
"""
def inner() -> Any:
"""機能を提供する関数を返す必要があるので内部で関数を生成する。"""
isActive = keymap.ActivateWindowCommand(
exe_name, class_name, window_text, check_func, force
)()
if isActive != None:
# ウィンドウが見つかった場合一応その値を返す。
return isActive
else:
# ウインドウが見つからなかった場合、起動する。
com = command or exe_name
if com == None:
raise ValueError(f"command: {command}, exe_name: {exe_name}")
keymap.ShellExecuteCommand(None, com, param, "", swmode="maximized")()
return isActive
return inner
def program_files(*pathsegments: str) -> WindowsPath:
"""`C:/Program Files/`以下を単純に参照します。"""
# Keyhacが現状32bitで動いているため、64ビット版のディレクトリを指してくれるように指定します。
return WindowsPath(os.environ["ProgramW6432"], *pathsegments)
def start_menu_programs(*pathsegments: str) -> WindowsPath:
"""
典型的には、
`C:/Users/ユーザ名/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/`
以下を参照します。
"""
roaming = os.environ["APPDATA"]
return WindowsPath(
roaming, "Microsoft", "Windows", "Start Menu", "Programs", *pathsegments
)
def configure_windows(keymap) -> None:
keymap.clipboard_history.enableHook(False)
keymap_global = keymap.defineWindowKeymap()
# W-qで大抵のウィンドウを閉じれるようにします。
keymap_global["W-q"] = "A-F4"
# ウィンドウ最大化を簡単に行えるようにします。
keymap_global["W-Semicolon"] = "W-Up"
# アクションセンターではなく通知を開きます。
keymap_global["W-a"] = "W-n"
keymap_global["W-d"] = run_or_raise(
keymap,
exe_name="Discord.exe",
command=str(start_menu_programs("Discord Inc", "Discord.lnk")),
)
keymap_global["W-h"] = run_or_raise(
keymap,
exe_name="firefox.exe",
command=str(program_files("Mozilla Firefox", "firefox.exe")),
)
keymap_global["W-t"] = run_or_raise(
keymap, exe_name="WindowsTerminal.exe", command="wt.exe"
)
keymap_global["W-n"] = run_or_raise(
keymap,
check_func=check_func_emacs,
command=str(program_files("WSL", "wslg.exe")),
param="--cd ~ -d Ubuntu -- emacs",
)
keymap_global["W-s"] = run_or_raise(keymap, exe_name="slack.exe")
keymap_global["W-b"] = run_or_raise(
keymap,
exe_name="KeePassXC.exe",
command=str(program_files("KeePassXC", "KeePassXC.exe")),
)
keymap_global["W-m"] = run_or_raise(
keymap,
exe_name="thunderbird.exe",
command=str(program_files("Mozilla Thunderbird", "thunderbird.exe")),
)
keymap_global["W-z"] = run_or_raise(
keymap,
exe_name=str(
WindowsPath(
os.environ["LOCALAPPDATA"],
"Programs",
"youtube-music",
"YouTube Music.exe",
)
),
)
set_keymap_dvorak_for_linux(
keymap, keymap.defineWindowKeymap(check_func=check_func_linux)
)
keymap_discord = keymap.defineWindowKeymap(exe_name="Discord.exe")
set_keymap_weblike(keymap, keymap_discord)
keymap_discord["A-j"] = "A-S-Down"
keymap_discord["A-k"] = "A-S-Up"
keymap_discord["A-t"] = "A-Up"
keymap_discord["A-n"] = "A-Down"
keymap_discord["C-m"] = "S-Enter"
keymap_discord["C-Comma"] = "29" # 無変換
keymap_discord["C-Period"] = "28" # 変換
set_keymap_weblike(
keymap,
keymap.defineWindowKeymap(
exe_name="firefox.exe", class_name="MozillaWindowClass"
),
)
set_keymap_weblike(
keymap,
keymap.defineWindowKeymap(
exe_name="thunderbird.exe", class_name="MozillaWindowClass"
),
)
# WindowsネイティブのEmacsのみを探索する。
keymap_emacs = keymap.defineWindowKeymap(exe_name="emacs.exe")
keymap_emacs["C-m"] = "Enter"
set_keymap_weblike(keymap, keymap.defineWindowKeymap(exe_name="chrome.exe"))
keymap_slack = keymap.defineWindowKeymap(exe_name="slack.exe")
set_keymap_weblike(keymap, keymap_slack)
keymap_slack["A-j"] = "A-S-Down"
keymap_slack["A-k"] = "A-S-Up"
keymap_slack["A-t"] = "A-Up"
keymap_slack["A-n"] = "A-Down"
keymap_slack["Enter"] = "C-Enter"
keymap_slack["C-Comma"] = "29" # 無変換
keymap_slack["C-Period"] = "28" # 変換
set_keymap_weblike(keymap, keymap.defineWindowKeymap(exe_name="YouTube Music.exe"))
set_keymap_weblike(keymap, keymap.defineWindowKeymap(exe_name="Code.exe"))
def configure(keymap) -> None:
keymap.setTheme("black")
configure_windows(keymap)