-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_docNav_edit.py
103 lines (91 loc) · 3.86 KB
/
_docNav_edit.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
print "importing " + __file__
from dragonfly import *
import BaseGrammars
from BaseRules import *
import chajLib.ui.docnav as docnav
import chajLib.ui.keyboard as kb
grammar = BaseGrammars.ContinuousGrammar("document navigation - edit grammar")
#decorator
def GrammarRule(rule):
if inspect.isclass(rule):
if issubclass(rule, BaseQuickRules):
rule(grammar)
else:
grammar.add_rule(rule())
else:
grammar.add_rule(rule)
@GrammarRule
class DocNavEditRules(QuickContinuousRules):
extrasDict = {
"n": IntegerRef("n", 1, 1000),
}
defaultsDict = {
"n": 1,
}
mapping = {
"(clap|capitalize hovered)": Mouse("left:2") + Function(docnav.capitalize_first_word_of_selection),
"(clop|lowercase hovered)": Mouse("left:2") + Function(docnav.lowercase_first_word_of_selection),
"crater": Mouse("left:1") + Key("end, enter"),
"cut line": Key("end, s-home") + Mimic("cut"),
"delete last word [<n> [times]]": Key("end") + Key("c-left") * Repeat(extra="n") + Key("s-end, delete"),
"delete line": Key("end, s-home, s-home") + Mimic("delete"),
"inner wedge": Key("enter:2, up"),
"lineless [<n> [times]]": Key("home, end, s-home:2, delete, backspace") * Repeat(extra="n"),
"lineless down [<n> [times]]": Key("down, home, end, s-home:2, delete, backspace") * Repeat(extra="n"),
"plaster": Key("c-v, enter"),
"printer [<n> [times]]": Key("end, enter") * Repeat(extra="n") + Key("c-v"),
"quote selection": Key("c-x, dquote, c-v, dquote"),
"renter [<n> [times]]": Key("end, enter") * Repeat(extra="n"),
"renter down [<n> [times]]": (Key("down") * Repeat(extra="n")) + Key("end, enter"),
"renter up [<n> [times]]": (Key("up") * Repeat(extra="n")) + Key("end, enter"),
"trim left": Key("s-home, delete"),
"trim right": Key("s-end, delete"),
}
@GrammarRule
class ReplaceSurroundingCharacters(ContinuousRule_OptionalRunOn):
spec = "replace <direction> [<n> [times]] (character|characters)"
intro_spec = "replace (left|right)"
extras = (IntegerRef("n", 1, 200), Choice("direction", {"left":"left", "right":"right"}))
defaults = { "n": 1}
def _process_recognition(self, node, extras):
action = Key("s-" + extras["direction"]) * Repeat(count=extras["n"])
if "RunOn" in extras:
replacement = " ".join(extras["RunOn"].words)
action += Text(replacement)
else:
action += Key("delete")
action.execute()
@GrammarRule
class ReplaceTrim(ContinuousRule_OptionalRunOn):
spec = "replace <direction>"
intro_spec = "replace (left|right)"
extras = (Choice("direction", {"left":"home", "right":"end"}),)
def _process_recognition(self, node, extras):
action = Key("s-" + extras["direction"])
if "RunOn" in extras:
replacement = extras["RunOn"].format()
action += Text(replacement)
else:
action += Key("delete")
action.execute()
@GrammarRule
class DocNavEditCalls(QuickContinuousCalls):
mapping = [
["paste left", docnav.replace_left_from_clipboard],
["paste right", docnav.replace_right_from_clipboard],
["replace left <RunOn>", docnav.replace_left, "replacement"],
["replace right <RunOn>", docnav.replace_right, "replacement"],
["[make] space count <count>", docnav.respace_around_caret],
["replace selection <to_replace> with <RunOn>",
docnav.replace_all_in_selection, "replacement"],
["replace line <to_replace> with <RunOn>",
docnav.replace_all_in_line, "replacement"],
]
extras = (IntegerRef("count", 0, 1000),
Dictation("to_replace"),
)
grammar.load()
def unload():
global grammar
if grammar: grammar.unload()
grammar = None