File: util.tcl

package info (click to toggle)
dotfile 2.2-1
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 4,596 kB
  • ctags: 456
  • sloc: tcl: 11,732; sh: 965; makefile: 304; csh: 13; ansic: 7
file content (476 lines) | stat: -rw-r--r-- 12,655 bytes parent folder | download
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
### Copyright (C) 1995-1997 Jesper K. Pedersen
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


#
# function to chose between two element. just like "? : " operator
#
proc pick {truth true false} {
  if {[uplevel \#0 expr \{$truth\}]} {
    return $true
  } else {
    return $false
  }
}
  

proc _tospace {str} {
  return [join [split $str "_"] " "]
}

proc spaceto_ {str} {
  return [join [split $str " "] "_"]
}

proc checkInt {widget} {
  if {![regexp {^-?[0-9]*$} [$widget get]]} {
    $widget delete [expr [$widget index insert] -1] [$widget index insert]
    error "This entry is only for integers"
  }
}

proc checkFloat {widget} {
  if {![regexp {^-?[0-9]*\.?[0-9]*$} [$widget get]]} {
    $widget delete [expr [$widget index insert] -1] [$widget index insert]
    error "This entry is only for floating points"
  }
}

#
# append to a list, if it the element doesn't exist.
#
proc uniqAppend {list value} {
  upvar $list l
  if {![info exists l]} {
    uplevel set \"$list\" [list \"$value\"]
  } elseif {[lsearch -exact $l $value] == -1} {
    uplevel lappend \"$list\" \"$value\"
  }
}

proc lremove {list value} {
  set index [lsearch -exact $list $value]
  if {$index == -1} {
    return "__error__"
  }
  return [lreplace $list $index $index]
}

proc min {x y} {
  if {$x < $y} {return $x}
  return $y
}
proc max {x y} {
  if {$x > $y} {return $x}
  return $y
}

############################################################
# This function checks wheter element can be a index in
# a widget with noOfElm number of elements.
############################################################
proc isIndex {element noOfElm} {
  if {![regexp {^[0-9]+$} $element]} {
    return 0
  }
  if {[expr $element+1] > $noOfElm} {
    return 0
  }
  return 1
}

proc tkTrue {value} {
  if {$value == "1" || $value == "true" || $value == "yes" || $value == "on"} {
    return 1
  } else {
    return 0
  }
}



######################################################################
# This function takes as argument a number in the range 0-15,
# and return the number as a hexadecimal digit (0..F)
######################################################################
proc hexDigit {i} {
  switch $i {
    10 {return A}
    11 {return B}
    12 {return C}
    13 {return D}
    14 {return E}
    15 {return F}
    default {return $i}
  }
}
######################################################################
# This function takes as argument a number in the range 0-255,
# and return the number as a hexadecimal digit.
######################################################################
proc itoh {i} {
  if {$i>255} {
    error "number to large"
  }
  set j [expr $i/16]
  set k [expr $i%16]
  return "[hexDigit $j][hexDigit $k]"
}
######################################################################
# This function converts a hexadecimal number to an integer.
######################################################################
proc htoi {number} {
  set total 0
  for {set i 0} {$i < [string length $number]} {incr i} {
    set total [expr $total*16 + [htoiDigit [string index $number $i]]]
  }
  return $total
}
######################################################################
# This function converts a hexadecimal diget to a decimal diget
######################################################################
proc htoiDigit {i} {
  switch $i {
    A {return 10}
    B {return 11}
    C {return 12}
    D {return 13}
    E {return 14}
    F {return 15}
    default {return $i}
  }
}
######################################################################
### This function upcase each first letter in a string
######################################################################
proc capitalize {str} {
  set list [split $str]
  set res {}
  foreach elm $list {
    lappend res [string toupper [string index $elm 0]][string tolower [string range $elm 1 end]]
  }
  return [join $res]
}
proc pushGrab {type w} {
  global __grabs
  if {![info exists __grabs]} {
    set __grabs ""
  }
  if {[grab current] != {}} {
    lappend __grabs "[grab current] [grab status [grab current]]"
  }
  if {$type == "global"} {
    grabSet -global $w
  } else {
    grabSet $w
  }
  ### Due to a bug in Tk I need to take the focus, to avoid another window
  ### still to be active.
  focus $w
}

proc popGrab {} {
  global __grabs
  grab release [grab current]
  if {[llength $__grabs] > 0} {
    set grabelm [lindex $__grabs end]
    set __grabs [lrange $__grabs 0 [expr [llength $__grabs] -2]]
    if {[lindex $grabelm 1] == "global"} {
      grabSet -global [lindex $grabelm 0]
    } else {
      grabSet [lindex $grabelm 0]
    }
    ### Due to a bug in Tk I need to take the focus, to avoid another window
    ### still to be active.
    focus [lindex $grabelm 0]
  }
}

proc saveDefinition {func} {
  global __definition
  if {![info exists __definition(body_$func)]} {
    set __definition(body_$func) {}
    set __definition(args_$func) {}
  }
  set __definition(body_$func) \
      [concat [list [info body $func]] $__definition(body_$func)]
  set __definition(args_$func) \
      [concat  [list [info args $func]] $__definition(args_$func)]
}
proc loadDefinition {func} {
  global __definition
  proc $func [lindex $__definition(args_$func) end] \
      [lindex $__definition(body_$func) 0]
  set __definition(args_$func) [lrange $__definition(args_$func) 1 end]
  set __definition(body_$func) [lrange $__definition(body_$func) 1 end]
}

proc space {number} {
  return [string range "                                                                               " 1 $number]
}

proc upCase {text} {
  return [string toupper $text]
}

proc timeWindow {width title text} {
  global __time __timeWindowPath tk_version
  # show only the time window if we are running TK
  # we run tclsh, when bytecompiling!
  if {[info exists tk_version]}  {
    set __time 0
    set w [makeTempWindow $title]
    pack [label $w.text -justify left -text $text]
    for {set i 0} {$i < $width} {incr i} {
      pack [label $w.$i -relief groove -bd 1] -side left -fill x -expand 1
    }
    update
    pushGrab local $w
    wm resizable $w 0 0
    set __timeWindowPath $w
  } else {
    puts $text
  }
}

proc incrTimeWindow {} {
  global __timeWindowPath __time tk_version
  if {[info exists tk_version]} {
    $__timeWindowPath.$__time configure -background black
    incr __time
    update
  } else {
    puts -nonewline "."
    flush stdout
  }
}

proc destroyTimeWindow {} {
  global __timeWindowPath tk_version
  if {[info exists tk_version]} {
    popGrab
    destroy $__timeWindowPath
  } else {
    puts ""
  }
}
rename error error_handler

proc error {args} {
  error_handler [join $args]
}
proc warning {args} {
  puts "Warning:\n[join $args]\n"
}


######################################################################
### This function return a list of widget, which is direct decent
### to the given widget (seen as variables) Ie. a child, which is
### located inside a frame or a window, is also included.
######################################################################
proc variableChildren {func parent} {
  global __children __widgetArgs

  set children {}
  
  foreach child $__children(${func}__$parent) {
    set type $__widgetArgs(${func}__${child}__type)
    if {$type == "window" || $type == "frame"} {
      set children [concat $children [variableChildren $func $child]]
    } else {
      lappend children $child
    }
  }
  return $children
}

proc noOfVariableChildren {func name} {
  global __children __widgetArgs
  set type $__widgetArgs(${func}__${name}__type)
  if {$type == "frame" || $type == "window"} {
    return [llength [variableChildren $func $name]]
  } else {
    return 1
  }
}

######################################################################
### This function is used in for the Command widgets
### it is used to give a user freindly interface.
### The arguments to the function is two list. The first is the option
### with no arguments, and the next is the options with arguments.
### The function set the variable "options" in the scope of the caller
######################################################################
proc parseOpt {noargs withargs list} {
  upvar options options

  set switch ""
  set nextHaveToBeValue 0
  set value ""

  foreach elm $noargs {
    set options($elm) 0
  }
  foreach elm $withargs {
    set options($elm) ""
  }
  foreach elm $list {
    if {[string index $elm 0] == "-" && [string index $elm 1] != "-"} {
      ### The next element is a switch
      if {$nextHaveToBeValue} {
        error "No Value given for option \"$switch\""
      }
      ### save the last switch
      if {$value != ""} {
        if {$value == "{}" || $value == {""}} {
          set value ""
        }
        set options($switch) $value
        set value ""
      }
      set switch [string range $elm 1 end]
      if {[lsearch -exact $noargs $switch] != -1} {
        set options($switch) 1
      } elseif {[lsearch -exact $withargs $switch] != -1} {
        set nextHaveToBeValue 1
      } else {
        error "unknown option: \"$switch\""
      }
    } else {
      set nextHaveToBeValue 0
      if {$switch == ""} {
        error "no switch for value \"$elm\""
      }
      if {[string index $elm 0] == "-"} {
        set elm [string range $elm 1 end]
      }
      append value $elm
    }
  }
  if {$value != ""} {
    set options($switch) $value
  }
}

######################################################################
### These functions sets the busy text in the main window.
######################################################################
proc startBusy {text} {
  global __busy
  .busy configure -text $text
  update
}

proc endBusy {} {
  global __busy
  set length [llength $__busy]
  if {$length  <= 1} {
    set text ""
  } else {
    set text [lindex $__busy [expr $length -2]]
  }
  set __busy [lrange $__busy 0 [expr $length -2]]
  .busy configure -text $text
}

######################################################################
### This function format the text, which is given as argument to
### the Help, Desc, ShortDesc etc. functions
######################################################################
proc makeText {arg} {
  regsub -all "\n " [join $arg] "\n" arg
  return $arg
}

proc createDir {dir} {
  global __system env
  set list [split $dir /]
  if {[lindex $list 0] == ""} {
    set list [lrange $list 1 end]
    set dir /
  } else {
    set dir ""
  }

  ### convert ~
  if {[lindex $list 0] == "~"} {
    set list [concat [list $env(HOME)] [lrange $list 1 end]]
  }
  foreach elm $list {
    set dir $dir$elm
    if {![file exists $dir]} {
      eval "exec $__system(mkdir) $dir"
    }
    set dir $dir/
  }
}  

######################################################################
### This function is used to take grab, so that TDG doesn't crash
### when it cannot take a crash
######################################################################
proc grabSet args {
  if {[llength $args] == 2} {
    set flag [lindex $args 0]
    set win [lindex $args 1]
  } else {
    set win [lindex $args 0]
    set flag ""
  }

  set count 0
  while {$count < 20} {
    set res [catch "grab $flag $win"]
    if {$res} {
      if {$count == 0} {
        puts -nonewline "WAITING FOR GRAB."
        flush stdout
      } else {
        puts -nonewline "."
        flush stdout
      }
      after 500
    } else {
      break
    }
    incr count
  }
  if {$res} {
    puts "Continuing without grab!"
  } elseif {$count > 1} {
    puts "Got it!"
  }
}

proc myGlob args {
  if {[lindex $args 0] == "-nocomplain"} {
    set path [lindex $args 1]
    set res [glob -nocomplain $path]
    if {$res == ""} {
      return ""
    }
  } else {
    set path [lindex $args 0]
    set res [glob $path]
  }

  if {[string index $path [expr [string length $path] -1]] == "/" &&
    [string index $res [expr [string length $res] -1]] != "/"} {
    append res /
  }
  return $res
}