-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_eval-1.tm
More file actions
230 lines (218 loc) · 7.71 KB
/
app_eval-1.tm
File metadata and controls
230 lines (218 loc) · 7.71 KB
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
# Copyright © 2025 Mark Summerfield. All rights reserved.
oo::define App method on_eval {} {
set eval_txt [string trim [$EvalCombo get]]
if {$eval_txt eq ""} { return }
if {$eval_txt eq "cls" | $eval_txt eq "clear"} {
$AnsText delete 1.0 end
} elseif {$::WORDFILE ne "" && ($eval_txt eq "rand(word)" ||
$eval_txt eq "random(word)")} {
my do_random_word
} elseif {[regexp {\d{2,4}-\d\d?-\d\d?|\mtoday\M} $eval_txt]} {
my do_date $eval_txt
} elseif {[regexp -expanded {(\m|\d)(meter|km|kilo|kg|second|
rad(?:ian)?|deg(?:gree)?|foot|ft|hectare|in(?:ch)?|
mi(?:le)?pound|lb|yd|yards?|litres?|stones?|mm|pt|
points?)\M} $eval_txt]} {
my do_conversion $eval_txt
} elseif {[regexp {[]^$\{:?\\|]} $eval_txt]} {
my do_regexp $eval_txt
} elseif {[string first = $eval_txt] > -1} {
my do_assignment $eval_txt
} elseif {$::ASPELL ne {} && [regexp {^[A-Za-z]+$} $eval_txt]} {
my do_spellcheck $eval_txt
} else {
my do_expression $eval_txt
}
$AnsText mark set insert end
$AnsText see end
}
oo::define App method do_random_word {} {
my update_combo $EvalCombo rand(word)
if {![llength $Words]} {
set Words [get_random_words $::WORDFILE]
}
set say "$AnsText insert end"
set word [lrandom $Words]
{*}$say "$word\n" blue
if {$::HAS_TLS} { my show_word_info $word }
}
oo::define App method do_regexp pattern {
set say "$AnsText insert end"
set re_text [string trim [$RegexTextCombo get]]
if {$re_text eq ""} {
{*}$say "Enter text for regexp to match…\n\n" red
focus $RegexTextCombo
$AnsText see end
return
} else {
my update_combo $RegexTextCombo $re_text
try {
{*}$say "re: " {green indent}
{*}$say $pattern\n {blue indent}
{*}$say "txt: " {green indent}
{*}$say “$re_text”\n {blue indent}
set matches [regexp -inline -- $pattern $re_text]
if {[llength $matches]} {
foreach match $matches i [lseq [llength $matches]] {
{*}$say "#$i: " {green indent}
{*}$say “$match”\n {blue indent}
}
} else {
{*}$say "no match\n" magenta
}
my update_combo $EvalCombo $pattern
} on error err {
{*}$say $err\n red
}
}
}
oo::define App method do_conversion txt {
set txt [regsub -nocase {\mto\M} $txt ""]
set say "$AnsText insert end"
try {
{*}$say $txt {blue indent}
{*}$say " → " {green indent}
{*}$say [units::convert {*}$txt]\n {blue indent}
my update_combo $EvalCombo $txt
} on error err {
{*}$say $err\n red
}
}
oo::define App method do_date txt {
set say "$AnsText insert end"
set txt [regsub -all today $txt [clock format [clock scan now] \
-format %Y-%m-%d]]
if {[regexp -expanded {
((\d{2,4})-\d\d?-\d\d?) # from
\s*([-+])\s* # op
(
(\d+\s+(:?days?|months?))| # add or subtract
(\d{2,4})-\d\d?-\d\d? # or to
)} $txt _ start year1 op by_or_end year2]} {
try {
set fmt [expr {$year1 < 100 ? "%y-%m-%d" : "%Y-%m-%d"}]
set from [clock scan $start -format $fmt]
if {[string first {-} $by_or_end] != -1} {
set fmt [expr {$year2 < 100 ? "%y-%m-%d" : "%Y-%m-%d"}]
set to [clock scan $by_or_end -format $fmt]
set days [expr {abs($from - $to) / 86400}]
{*}$say $start {blue indent}
{*}$say " - " {green indent}
{*}$say $by_or_end {blue indent}
{*}$say " → " {green indent}
{*}$say $days {blue indent}
{*}$say " days\n" {green indent}
} else {
set value [clock add $from {*}"$op$by_or_end"]
set to [clock format $value -format %Y-%m-%d]
{*}$say "$start $op $by_or_end" {blue indent}
{*}$say " → " {green indent}
{*}$say $to\n {blue indent}
}
my update_combo $EvalCombo $txt
} on error err {
{*}$say $err\n red
}
} else {
{*}$say "invalid date expression\n" red
}
}
oo::define App method do_assignment txt {
set i [string first = $txt]
set name [string trim [string range $txt 0 $i-1]]
set expression [string trim [string range $txt $i+1 end]]
if {$expression eq ""} {
dict unset Vars $name
my refresh_vars
} else {
my evaluate $name $expression
my update_combo $EvalCombo $txt
}
}
oo::define App method do_spellcheck txt {
set say "$AnsText insert end"
set cmd [list $::ASPELL -a]
set reply [exec {*}$cmd << $txt 1>]
if {[string index $reply end-1] eq "*"} {
{*}$say "$txt ✔\n" {green indent}
my update_combo $EvalCombo $txt
if {$::HAS_TLS} { my show_word_info $txt }
} else {
set suggestions [list]
set i [string first : $reply]
if {$i > -1} {
set reply [string trim [string range $reply \
[expr {$i + 1}] end]]
foreach suggestion [split $reply ,] {
lappend suggestions [string trim $suggestion]
}
}
{*}$say "$txt ✘\n" {red indent}
{*}$say "suggestions:\n" {green indent}
{*}$say " [join $suggestions " "]\n" {blue indent}
}
}
oo::define App method show_word_info word {
set token [http::geturl $::DICT_URL/$word]
try {
if {[http::status $token] eq "ok"} {
set data [http::data $token]]
set definitions [word_data_get_definitions $data]
if {$definitions eq {}} {
$AnsText insert end "no definition found\n" {italic orange}
return
}
set synonyms [word_data_get_synonyms $data]
my write_list Definition $definitions
if {[llength $synonyms]} { my write_list Synonym $synonyms }
}
} finally {
http::cleanup $token
}
}
oo::define App method write_list {name lst} {
set say "$AnsText insert end"
if {[llength $lst] == 1} {
{*}$say "$name: " {italic lavender}
{*}$say [lindex $lst 0]\n {brown indent}
} else {
{*}$say "${name}s:\n" {italic lavender}
foreach i [lseq 1 [llength $lst]] x $lst {
{*}$say "$i " {olive indent}
{*}$say $x\n {brown indent}
}
}
}
oo::define App method do_expression txt {
my evaluate [my next_name] [string trim $txt]
}
oo::define App method evaluate {name expression} {
set say "$AnsText insert end"
set expression [regsub -all -command {\m[[:alpha:]]\w*\M} \
$expression [lambda {vars match} \
{ dict getdef $vars $match $match } $Vars]]
try {
set fixed [regsub -all -command {rand[(](\d+)[)]} $expression \
[lambda {_ n} { return "int(rand()*$n)" }]]
set value [expr $fixed]
dict set Vars $name $value
{*}$say $name {blue indent}
{*}$say " = " {green indent}
set fmt [expr {[string is integer $value] ? "%Ld" : "%Lg"}]
{*}$say [format $fmt\n $value] {blue indent}
my update_vars_list $name
my refresh_vars
my update_combo $EvalCombo $expression
} on error err {
{*}$say $err\n red
}
}
oo::define App method update_vars_list name {
if {[set i [lsearch -exact $VarsList $name]] > -1} {
set VarsList [lremove $VarsList $i]
}
lappend VarsList $name
if {[llength $VarsList] > 50} {
set VarsList [lrange $VarsList end-50 end]
}
}