-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.py
143 lines (126 loc) · 3.51 KB
/
add.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
import sublime, sublime_plugin
from traceback import print_exc
class AddSumNumbersCommand(sublime_plugin.TextCommand):
def run(self, edit):
# get non-empty selections
selections = self.view.sel()
tokens = [self.view.substr(x) for x in selections if not x.empty()]
# if there's no non-empty selection, end
if len(tokens) == 0:
print "no selected numbers"
return
result = 0
for number_in_text_form in tokens:
try:
num_str = str(number_in_text_form).strip()
if not num_str: continue
num = float(num_str)
print "+", num
result += num
except:
print " - ERR: failed to convers to num:", number_in_text_form
print_exc()
print "storing", result, "in clipboard"
sublime.set_clipboard(str(result))
class IncrementNumbersCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel(
"Increment selections by what number?",
"",
self.on_done,
None,
None,
)
def on_done(self, increment_string):
try:
increment_value = int(increment_string)
except ValueError, e:
# try float then
increment_value = float(increment_string)
edit = self.view.begin_edit()
selections = self.view.sel()
for selection in selections:
try:
try:
x = int(self.view.substr(selection))
except ValueError, e:
# try float then
x = float(self.view.substr(selection))
x += increment_value
self.view.replace(edit, selection, str(x))
except:
print_exc()
self.view.end_edit(edit)
class MultiplyNumbersCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel(
"Multiply selections by what number?",
"",
self.on_done,
None,
None,
)
def on_done(self, increment_string):
try:
mul_value = int(increment_string)
except ValueError, e:
# try float then
mul_value = float(increment_string)
edit = self.view.begin_edit()
selections = self.view.sel()
for selection in selections:
try:
try:
x = int(self.view.substr(selection))
except ValueError, e:
# try float then
x = float(self.view.substr(selection))
x *= mul_value
self.view.replace(edit, selection, str(x))
except:
print_exc()
self.view.end_edit(edit)
class IncrementSequentiallyCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.window().show_input_panel(
"Increment selections incrementially, what is starting offset?",
"0",
self.on_done,
None,
None,
)
def on_done(self, increment_string):
try:
increment_value = int(increment_string)
except ValueError, e:
# try float then
increment_value = float(increment_string)
edit = self.view.begin_edit()
selections = self.view.sel()
for selection in selections:
try:
try:
x = int(self.view.substr(selection))
except ValueError, e:
# try float then
x = float(self.view.substr(selection))
x += increment_value
self.view.replace(edit, selection, str(x))
increment_value += 1
except:
print_exc()
self.view.end_edit(edit)
class EvalExpressionCommand(sublime_plugin.TextCommand):
def run(self, edit):
edit = self.view.begin_edit()
selections = self.view.sel()
for selection in selections:
try:
try:
x = eval(self.view.substr(selection))
self.view.replace(edit, selection, str(x))
except:
print_exc()
except:
print_exc()
self.view.end_edit(edit)