summaryrefslogtreecommitdiff
path: root/support/dinbrief-gui/dinbrief.vfs/util.tcl
blob: 195d25ac606e190c3891426d06cd227e52fd87db (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
proc comment args {}

switch $tcl_platform(platform) {
    unix {
	event add <<Cut>> <Shift-Delete>
	event add <<Copy>> <Control-Insert>
	event add <<Paste>> <Shift-Insert>

	event delete <<Paste>> <Control-Key-y>
	event add <<Redo>> <Control-Key-y>

	# correct some strange failures in key bindings

	bind Text <Control-v> {event generate %W <<Paste>>}
	bind Text <Control-y> {event generate %W <<Redo>>}
    }
}

option add *Menu.tearOffCommand		fixTornOffMenu
option add *Canvas.highlightThickness	0
option add *Label.borderWidth		1
option add *Entry.borderWidth		1
option add *Spinbox.borderWidth		1
option add *Button.borderWidth		1
option add *Menubutton.borderWidth	1
option add *Scrollbar.borderWidth	1

option add *Entry.font			{Helvetica 9}
option add *Label.font			{Helvetica 9}
option add *Button.font			{Helvetica 9}
option add *Menubutton.font		{Helvetica 9}
option add *Checkbutton.font		{Helvetica 9}
option add *Labelframe.font		{Helvetica 9}
option add *Text.font			{Helvetica 9} 

option add *Menu.background		\#c0c0c0
option add *Menu.borderWidth		1
option add *Menu.activeBorderWidth		1

option add *Radiobutton.font		{Helvetica 9}
option add *Radiobutton.justify		left
option add *Radiobutton.wrapLength	300

option add *Checkbutton.justify		left
option add *Checkbutton.wrapLength	300


option add *Entry.background		white
option add *Entry.highlightThickness	0

option add *Text.background		white
option add *Text.relief			sunken 
option add *Text.borderWidth		1
option add *Text.insertOffTime		0
option add *Text.highlightThickness	0

option add *Text.undo			1

option add *Menu.background		\#c0c0c0
option add *Menu.borderwidth		0

proc fixTornOffMenu {menu toplevel} {
    # make tear-off transient to parent window.
    set parent [winfo parent $toplevel]
    wm transient $toplevel $parent
    if {$::tcl_platform(platform) ne "unix"} {
	set px [winfo pointerx $parent]
	set py [winfo pointery $parent]
	set x [expr {$px - 30}]
	set y [expr {$py - 10}]
	wm geometry $toplevel +$x+$y
    }
    wm resizable $toplevel no no
}

proc setVscrollbarIn {toplevel a b} {
    # set vertical scrollbar in $toplevel (managed by grid)
    if {$toplevel eq "."} {
	set scrollbar .vscroll
    } else {
	set scrollbar $toplevel.vscroll
    }
    if {($a == 0.0) && ($b == 1.0)} {
	set code "catch {grid forget $scrollbar}"
	after cancel $code
	after 100 $code
    } else {
	grid configure $scrollbar -row 0 -column 1 -sticky news
	set code [list $scrollbar set $a $b]
	after cancel $code
	after idle $code
    }
}

proc setHscrollbarIn {toplevel a b} {
    # set horizontal scrollbar in $toplevel (managed by grid)
    if {$toplevel eq "."} {
	set scrollbar .hscroll
    } else {
	set scrollbar $toplevel.hscroll
    }
    if {($a == 0.0) && ($b == 1.0)} {
	set code "catch {grid forget $scrollbar}"
	after cancel $code
	after 100 $code
    } else {
	grid configure $scrollbar -row 1 -column 0 -sticky news
	set code [list $scrollbar set $a $b]
	after cancel $code
	after idle $code
    }
}


namespace eval dummy {}

proc scrolledtext {f args} {
    frame $f -class Scrolledtext
    text $f.text
    grid $f.text\
	[scrollbar $f.vscroll\
	     -command [list $f.text yview]]\
	-sticky news
    grid [scrollbar $f.hscroll\
	      -orient horizontal\
	      -command [list $f.text xview]]\
	-sticky news
    grid rowconfigure $f 0 -weight 1
    grid columnconfigure $f 0 -weight 1
    $f.text configure\
	-xscrollcommand [list setHscrollbarIn $f]\
	-yscrollcommand [list setVscrollbarIn $f]\
	-wrap word
    rename $f ::dummy::$f
    proc $f {args} [subst {
	eval $f.text \$args
    }]
    eval $f.text configure $args
    set f
}

proc scrolledrichtext {f args} {
    frame $f -class Scrolledrichtext
    richtext $f.text
    grid $f.text\
	[scrollbar $f.vscroll\
	     -command [list $f.text yview]]\
	-sticky news
    grid [scrollbar $f.hscroll\
	      -orient horizontal\
	      -command [list $f.text xview]]\
	-sticky news
    grid rowconfigure $f 0 -weight 1
    grid columnconfigure $f 0 -weight 1
    $f.text configure\
	-xscrollcommand [list setHscrollbarIn $f]\
	-yscrollcommand [list setVscrollbarIn $f]\
	-wrap word
    rename $f ::dummy::$f
    proc $f {args} [subst {
	eval $f.text \$args
    }]
    eval $f.text configure $args
    set f
}

namespace eval Lambda {
    variable counter 0
    array set function {}
    namespace eval Function {}
    namespace export lambda
}

proc ::Lambda::nextName {} {
    variable counter
    return [namespace current]::Function::[incr counter]
}

proc ::Lambda::lambda {arglist body} {
    # return quasi-anonymous lambda term
    # (i. e. build a procedure bound to some name, and return that name).
    variable function
    set formals [list $arglist $body]
    if {![info exists function($formals)]} {
	set name [nextName]
	uplevel 1 [list proc $name $arglist $body]
	set function($formals) $name
    }
    return $function($formals)
}

namespace import -force ::Lambda::lambda

proc unicode2texNormal {unicode} {
    variable char2texTable
    set string [string trim $unicode]
    set texChars [string map $char2texTable $string]
    set lineFeedMap [list \n \\\\\n]
    set texLines [string map $lineFeedMap $texChars]
    set parPattern {(\s*\\\\\n){2,}}
    set texLines [regsub -all $parPattern $texLines "\n\n"]
    string map [list\
		    ... [globalSetting ...]\
		    {\pagebreak{}\\} {\pagebreak{}}
		   ] $texLines
}

#
proc subSubjectIndices {src} {
    set indices\
 	[regexp -all -inline -indices {(?:^|\n+)([*/_])[^\n]*?\1} $src]
    set result {}
    foreach {match sign} $indices {
	let {from to} $match
	lappend result $from
	set foundRange [string range $src $from $to]
	set char\
	    [string index [string trim $foundRange] 0]
	set pat \\$char.*?\\$char
	if {[regexp -indices $pat $foundRange match1]} {
	    let {from1 to1} $match1
	    set len [expr {$to1-$from1}]
	    lappend result\
		[expr {$from + $len + [string first $char $foundRange]}]
	} else {
	    lappend result $to
	}
    }
    set result
}

proc textSubSubjectFragments {src} {
    set indices -1
    set subSubjects {}
    foreach {from to} [subSubjectIndices $src] {
	lappend subSubjects [string range $src $from $to]
	lappend indices [expr {$from - 1}] [expr {$to + 1}]
    }
    lappend indices [string length $src]
    set textList {}
    foreach {from to} $indices {
	lappend textList [string range $src $from $to]
    }
    # list subSubjectes $subSubjects textList $textList
    set result [lrange $textList -1 0]
    foreach subSubject $subSubjects text [lrange $textList 1 end] {
	lappend result $subSubject $text
    }
    set result
}

proc unicode2tex {src} {
    set frags [textSubSubjectFragments $src]
    set result [unicode2texNormal [lindex $frags 0]]
    array set meaning {
	* \\bf
	/ \\it
	_ \\underline
    }
    foreach {sub txt} [lrange $frags 1 end] {
	regexp -indices {[*/_]} $sub indices
	let {from to} $indices
	set subChar [string range $sub $from $to]
	set subText [string range $sub [expr {$to+1}] end-1]
	set splitRange [regexp -inline ^\\s* $txt]
	append result \n\n\{\
	    $meaning($subChar) \
	    \{ [unicode2texNormal $subText] \}\
	    \}\
	    $splitRange\
	    [unicode2texNormal $txt]
    }
    string trim $result
}

#

proc charVal {char} {
    scan $char %c
}

proc hex {number} {
    format %x $number
}

proc echo {args} {
    uplevel [list puts $args]
}

#
# return string with non-ascii chars encoded as in ö = \u00f6 etc.
#
proc unicode2asciiEncoded {rawString} {
    set string [string map {\\ \\\\} $rawString]
    while {[regexp {[^[:ascii:]]} $string nonAsciiChar]} {
	set nonAsciiVal [charVal $nonAsciiChar]
	set hexVal [hex $nonAsciiVal]
	while {[string length $hexVal] < 4} {
	    set hexVal 0$hexVal
	}
	set hexVal \\u$hexVal
	set string [string map [list $nonAsciiChar $hexVal] $string]
    }
    set string
}

proc saveString {string file} {
    set port [open $file w]
    puts -nonewline $port $string
    close $port	
}

proc cat file {
    set port [open $file]
    set contents [read $port]
    close $port
    return $contents
}

proc let {args} {
    uplevel 1 foreach $args {break}
}

proc range {num args} {
    switch [llength $args] {
	0 {
	    set from 0
	    set to $num
	    set step 1
	}
	1 {
	    set from $num
	    set to $args
	    set step 1
	}
	2 {
	    set from $num
	    let {to step} $args
	}
    }
    set result [list ]
    for {set i $from} {$i < $to} {set i [expr {$i + $step}]} {
	lappend result $i
    }
    return $result
}

proc map {func list} {
    set result [list ]
    foreach el $list {
	lappend result [uplevel [list $func $el]]
    }
    return $result
}

proc llindex {l args} {
    # return nested list index, e.g. llindex $nestedList 4 2
    if {[llength $args] == 0} {
	return $l
    } else {
	eval llindex [list [lindex $l [lindex $args 0]]] [lrange $args 1 end]
    }
}

namespace eval Stack {
    variable stack {}
    namespace export push pop tos
}

proc Stack::pop {{s ::Stack::stack}} {
    upvar $s stack
    set result [lindex $stack end]
    set stack [lrange $stack 0 end-1]
    return $result
}

proc Stack::push {val {s ::Stack::stack}} {
    upvar $s stack
    lappend stack $val
}

proc Stack::tos {{s ::Stack::stack}} {
    upvar $s stack
    lindex $stack end
}

namespace import -force ::Stack::push
namespace import -force ::Stack::pop
namespace import -force ::Stack::tos

proc map {func list} {
    set result [list ]
    foreach el $list {
	lappend result [uplevel [list $func $el]]
    }
    return $result
}

proc shift {listVar} {
    upvar $listVar list
    set result [lindex $list 0]
    set list [lrange $list 1 end]
    set result
}

proc unshift {el listVar} {
    upvar $listVar list
    set list [concat $el $list]
}

namespace eval Sleep {
    namespace export sleep
    variable count 0
}

proc ::Sleep::newVar {} {
    variable count
    return var[incr count]
}

proc ::Sleep::sleep {num} {
    set var [newVar]
    uplevel 1 set ::Sleep::$var 1
    after $num [list set ::Sleep::$var 0]
    vwait ::Sleep::$var
    unset ::Sleep::$var
}

namespace import -force ::Sleep::sleep

proc lreverse {l} {
    set i [llength $l]
    set result {}
    while {$i} {
	lappend result [lindex $l [incr i -1]]
    }
    # return this value
    set result
}

proc relPathFromTo {fromDir toDir} {
    # return path string relative from $fromDir to $toDir.
    # $fromDir is assumed to be a directory (not a file).
    set from [file normalize $fromDir]
    set to [file normalize $toDir]
    if {$::tcl_platform(platform) eq "windows"} {
	set driveMap {
	    a: A: b: B: c: C: d: D: e: E: f: F: g: 
	    G: h: H: i: I: j: J: k: K: l: L: m: M:
	    n: N: o: O: p: P: q: Q: r: R: s: S: t:
	    T: u: U: v: V: w: W: x: X: y: Y: z: Z:
	}
	regexp {^[a-zA-Z]:} [pwd] drive
	if {![regexp {^[a-zA-Z]:} $from]} {
	    set from $drive$from
	}
	set from [string map $driveMap $from]
	if {![regexp {^[a-zA-Z]:} $to]} {
	    set to $drive$to
	}
	set to [string map $driveMap $to]
    }
    set fromList [file split $from]
    set fromLength [llength $fromList]
    set toList [file split $to]
    set toLength [llength $toList]
    set commonList {}
    foreach path1 $fromList path2 $toList {
	if {$path1 ne $path2} {
	    break
	} else {
	    lappend commonList $path1
	}
    }
    set commonLength [llength $commonList]
    set fromList1 [lrange $fromList $commonLength end]
    set toList1 [lrange $toList $commonLength end]
    set resultList {}
    foreach i $fromList1 {
	lappend resultList ..
    }
    eval lappend resultList $toList1
    if {$resultList ne {}} {
	eval file join $resultList
    }
}