Skip to content

Instantly share code, notes, and snippets.

View yyamasak's full-sized avatar

Yusuke Yamasaki yyamasak

View GitHub Profile
@yyamasak
yyamasak / tk_motif_bindings.tcl
Last active May 14, 2019 06:26
I prefer UNIX key binding on Tk 8.6 console.
set tk_motif_binding_script {
event delete <<SelectAll>>
event add <<SelectAll>> <Control-Key-slash>
event add <<PrevChar>> <Control-Key-b> <Control-Lock-Key-B>
event add <<NextChar>> <Control-Key-f> <Control-Lock-Key-F>
event add <<PrevLine>> <Control-Key-p> <Control-Lock-Key-P>
event add <<NextLine>> <Control-Key-n> <Control-Lock-Key-N>
event add <<LineStart>> <Control-Key-a> <Control-Lock-Key-A>
event add <<LineEnd>> <Control-Key-e> <Control-Lock-Key-E>
event add <<SelectPrevChar>> <Control-Key-B> <Control-Lock-Key-b>
@yyamasak
yyamasak / mymath.tcl
Last active May 17, 2018 07:20
Making Tcl expr handle Inf or NaN transparently
proc is_computable_double {val} {
expr {[string is double -strict $val] && ![is_nan $val]}
}
proc is_nan {val} {
string match -nocase [string trimleft $val +-] "nan"
}
proc is_infinite {val} {
expr {$val == Inf || $val == -Inf}
@yyamasak
yyamasak / mymath.tcl
Last active May 15, 2018 09:08
A challenge to make Tcl expr handle Inf or NaN transparently (but failed because of [expr {NaN}])
proc is_computable_double {val} {
expr {[string is double -strict $val] && ![is_nan $val]}
}
proc is_nan {val} {
string match -nocase [string trimleft $val +-] "nan"
}
proc is_infinite {val} {
expr {$val == Inf || $val == -Inf}
@yyamasak
yyamasak / ESC-IME.ahk
Created April 10, 2018 09:12
Map short ESC to IME ON/OFF and long ESC to Escape
$Esc::
KeyWait, Esc, T0.1
if ErrorLevel
send,{Esc}
else
send,!{vkC0}
keywait, Esc
return
@yyamasak
yyamasak / screenshot_in_tcl.tcl
Created June 12, 2017 10:38
A Tcl/Tk script that saves a Windows desktop screenshot into a PNG file
package require Tk
package require Img
package require twapi
# http://wiki.tcl.tk/15647
# Copy the contents of the Windows clipboard into a photo image.
# Return the photo image identifier.
proc Clipboard2Img {} {
twapi::open_clipboard
@yyamasak
yyamasak / iso8601_to_unixepoch.tcl
Last active December 14, 2016 02:45
Tcl + sqlite3 script to parse an ISO8601 datetime string
package require sqlite3
sqlite3 mdb :memory:
set dt "2016-12-13T18:54:21.182556"
set unixepoch [mdb eval {SELECT strftime('%s',$dt,'UTC')}]
puts $unixepoch
#=> 1481622861
@yyamasak
yyamasak / myround.tcl
Last active November 1, 2016 08:11
Rounding in decimal arithmetic
package require math::decimal
math::decimal::setVariable extended 0
proc tcl::mathfunc::roundto {value sigfig dp} {
math::decimal::setVariable precision $sigfig
set d_val [math::decimal::fromstr $value]
set d_rou [math::decimal::round_half_up $d_val 100]
set d_rou [math::decimal::round_floor $d_rou $dp]
math::decimal::tostr $d_rou
package require Garuda
set script {
object load -import System
object load -import UIAutomationClient
object load -import UIAutomationTypes
object load -import UIAutomationProvider
object load -import UIAutomationClientsideProviders
set process [object invoke Process Start "calc"]
@yyamasak
yyamasak / twapi_timer.tcl
Created June 21, 2016 05:35
Replaces Tcl's after command by twapi::wait_on_handle to use monotonic timer on Windows
package require twapi
if {![namespace exists twapi_timer]} {
namespace eval twapi_timer {
variable ids
array set ids {}
}
}
proc twapi_timer::_timer_handler {script hevent sig} {
@yyamasak
yyamasak / resize-shadowstorage.bat
Created April 25, 2016 02:58
Reset disk space usage to 10% if the max usage is automatically extended to 100%
chcp 437
vssadmin list ShadowStorage /On=C:|findstr /c:"Maximum Shadow Copy Storage space: UNBOUNDED (100%)"
IF ERRORLEVEL 0 (vssadmin Resize ShadowStorage /For=C: /On=C: /MaxSize=10%)