-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_editor.py
executable file
·112 lines (84 loc) · 2.95 KB
/
generic_editor.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
# https://github.com/JonathanNickerson/talon_voice_user_scripts
# jsc added indent/outdent and simplified jolt
from talon.voice import Key, press, Str, Context
ctx = Context('generic_editor') # , bundle='com.microsoft.VSCode')
numeral_map = dict((str(n), n) for n in range(0, 20))
for n in [20, 30, 40, 50, 60, 70, 80, 90]:
numeral_map[str(n)] = n
numeral_map["oh"] = 0 # synonym for zero
numerals = ' (' + ' | '.join(sorted(numeral_map.keys())) + ')+'
optional_numerals = ' (' + ' | '.join(sorted(numeral_map.keys())) + ')*'
def text_to_number(words):
tmp = [str(s).lower() for s in words]
words = [parse_word(word) for word in tmp]
result = 0
factor = 1
for word in reversed(words):
if word not in numerals:
raise Exception('not a number')
result = result + factor * int(numeral_map[word])
factor = 10 * factor
return result
def parse_word(word):
word = word.lstrip('\\').split('\\', 1)[0]
return word
def jump_to_bol(m):
line = text_to_number(m)
press('cmd-l')
Str(str(line))(None)
press('enter')
def jump_to_end_of_line():
press('cmd-right')
def jump_to_beginning_of_text():
press('cmd-left')
def jump_to_nearly_end_of_line():
press('left')
def jump_to_bol_and(then):
def fn(m):
if len(m._words) > 1:
jump_to_bol(m._words[1:])
else:
press('ctrl-a')
press('cmd-left')
then()
return fn
def jump_to_eol_and(then):
def fn(m):
if len(m._words) > 1:
jump_to_bol(m._words[1:])
press('cmd-right')
then()
return fn
def toggle_comments():
# works in VSCode with Finnish keyboard layout
# press('cmd-shift-7')
# does not work in VSCode, see https://github.com/talonvoice/talon/issues/3
press('cmd-/')
def snipline():
press('shift-cmd-right')
press('delete')
press('delete')
press('ctrl-a')
press('cmd-left')
keymap = {
'sprinkle' + optional_numerals: jump_to_bol,
'spring' + optional_numerals: jump_to_eol_and(jump_to_beginning_of_text),
'dear' + optional_numerals: jump_to_eol_and(lambda: None),
'smear' + optional_numerals: jump_to_eol_and(jump_to_nearly_end_of_line),
'trundle' + optional_numerals: jump_to_bol_and(toggle_comments),
'jolt': Key('ctrl-a cmd-left shift-down cmd-c down cmd-v' ), # jsc simplified
'snipline' + optional_numerals: jump_to_bol_and(snipline),
# NB these do not work properly if there is a selection
'snipple': Key('shift-cmd-left delete'),
'snipper': Key('shift-cmd-right delete'),
'shackle': Key('cmd-right shift-cmd-left'),
'bracken': [Key('cmd-shift-ctrl-right')],
'shockey': Key('ctrl-a cmd-left enter up'),
'shockoon': Key('cmd-right enter'),
'sprinkoon' + numerals: jump_to_eol_and(lambda: press('enter')),
'olly': Key('cmd-a'),
# jsc added
'(indent | shabble)': Key('cmd-['),
'(outdent | shabber)': Key('cmd-]'),
}
ctx.keymap(keymap)