File: ftcllib.tcl

package info (click to toggle)
cbrowser 0.8-1
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 324 kB
  • ctags: 66
  • sloc: tcl: 4,572; makefile: 34
file content (1597 lines) | stat: -rw-r--r-- 50,161 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
#!/usr/bin/wish

# $Id: ftcllib.tcl,v 1.1 2000/06/26 19:23:59 cfelaco Exp $

# Ftcllib is a collection of useful procedures for Tcl/Tk programs.
# Copyright (C) 1999  B. Christopher Felaco

# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# For more information about Ftcllib and its author see:
#    URL:http://cbrowser.sourceforge.net/
#
# Feel free to contact me at URL:mailto:cfelaco@users.sourceforge.net
# with enhancements or suggestions.

# $Log: ftcllib.tcl,v $
# Revision 1.1  2000/06/26 19:23:59  cfelaco
# Initial source file revisions for sourceforge.net.
# Existing revision history is based on RCS revisions made by original author
# in private repository.
#
# Revision 1.1  1999/03/15 01:06:19  chris
# Initial revision
#

# This file contains assorted Tcl/Tk utility functions.  There are various
# short convenience functions for string/list manipulation, and many Tk
# utilities:
#   - Paned windows
#   - file viewer based on edit widget (does not allow editing)
#   - general purpose edit menu
#   - text finder for file viewer
#   - goto line dialog for file viewer
#   - "magic" scrollbars, appear only when needed
#   - scrollable history in entry fields
#   - C source code syntax highlighting
#   - blinking activity light
#----------------------------------------------------------------------------

# Setup some aliases for those pesky string commands
interp alias {} strcmp {} string compare
interp alias {} strlen {} string length
interp alias {} substring {} string range

#----------------------------------------------------------------------------
# The following is from "Practical Programming in Tcl and Tk" Second Edition
# by Brent B. Welch Copyright 1997 Prentice Hall.
#
# Enhancements:
#    - When the cursor leaves the window, reversing direction will not take
#      effect until the cursor reenters the window.
#    - A new option -background has been added to control the color of the
#      separator.
#    - Uses appropriate cursor for resizing instead of crosshair.
#----------------------------------------------------------------------------

proc Pane_Create {f1 f2 args} {

  # Map optional arguments into array values
  set t(-orient) vertical
  set t(-percent) 0.5
  set t(-in) [winfo parent $f1]
  set t(-background) black
  set t(-minpercent) 0.0
  set t(-maxpercent) 1.0
  array set t $args

  # Keep state in an array associated with the master frame
  set master $t(-in)
  upvar \#0 Pane$master pane
  array set pane [array get t]

  # Create the grip and set placement attributes that
  # will not change. A thin divider line is achieved by
  # making the two frames one pixel smaller in the
  # adjustable dimension and making the main frame black.

  set pane(1) $f1
  set pane(2) $f2
  set cursor sb_[string range $pane(-orient) 0 0]_double_arrow
  set pane(grip) [frame $master.grip -background gray50 \
                      -width 10 -height 10 -bd 2 -relief raised \
                      -cursor $cursor]
  if {[string match vert* $pane(-orient)]} {
    set pane(D) Y		;# Adjust boundary in Y direction
    place $pane(1) -in $master -x 0 -rely 0.0 -anchor nw \
        -relwidth 1.0 -height -1
    place $pane(2) -in $master -x 0 -rely 1.0 -anchor sw \
        -relwidth 1.0 -height -1
    place $pane(grip) -in $master -anchor c -relx 0.9
  } else {
    set pane(D) X 		;# Adjust boundary in X direction
    place $pane(1) -in $master -relx 0.0 -y 0 -anchor nw \
        -relheight 1.0 -width -1
    place $pane(2) -in $master -relx 1.0 -y 0 -anchor ne \
        -relheight 1.0 -width -1
    place $pane(grip) -in $master -anchor c -rely 0.8
  }
  $master configure -background $pane(-background)

  set pane(toplevel) [winfo toplevel $master]

  # Set up bindings for resize, <Configure>, and
  # for dragging the grip.

  bind $master <Configure> [list PaneGeometry $master %w %h]
  bind $pane(grip) <ButtonPress-1> \
      [list PaneDrag $master %$pane(D)]
  bind $pane(grip) <B1-Motion> \
      [list PaneDrag $master %$pane(D)]
  bind $pane(grip) <ButtonRelease-1> \
      [list PaneStop $master]

  # Do the initial layout
  PaneGeometry $master
}

proc PaneDrag {master D} {
  upvar \#0 Pane$master pane

  set d [string tolower $pane(D)]
  if {! [info exists pane(base)]} {
    # Get the screen coordinate (either x or y) of the top of the pane.
    # The vroot$d is used to correct for virtual screen managers.  The
    # root$d command on the gets the screen coordinate as opposed to the
    # coordinate relative to the parent.
    set pane(base) [expr [winfo vroot$d $pane(toplevel)] + \
        [winfo root$d $master]]
  }

  set pane(-percent) [expr double($D - $pane(base)) / $pane(size)]
  if {$pane(-percent) < $pane(-minpercent)} {
    set pane(-percent) $pane(-minpercent)
  } elseif {$pane(-percent) > $pane(-maxpercent)} {
    set pane(-percent) $pane(-maxpercent)
  }

  PaneGeometry $master
}

proc PaneStop {master} {
  upvar \#0 Pane$master pane
  catch {unset pane(base)}
}

proc PaneGeometry {master {width -1} {height -1}} {

  upvar \#0 Pane$master pane
  if {[strcmp $pane(D) "X"] == 0} {
    place $pane(1) -relwidth $pane(-percent)
    place $pane(2) -relwidth [expr 1.0 - $pane(-percent)]
    place $pane(grip) -relx $pane(-percent)

    if {$width < 0} {
      set pane(size) [winfo width $master]
    } else {
      set pane(size) $width
    }
  } else {
    place $pane(1) -relheight $pane(-percent)
    place $pane(2) -relheight [expr 1.0 - $pane(-percent)]
    place $pane(grip) -rely $pane(-percent)

    if {$height < 0} {
      set pane(size) [winfo height $master]
    } else {
      set pane(size) $height
    }
  }
}

##############################################################################
#
#  Purpose    : Setup a file viewer window with scrollbars
#
#  Parameters : root - the root widget path
#               frame - the frame to pack the file_viewer in
#
#  Result     : NONE
#
##############################################################################

proc setup_file_viewer {root frame} {

  set_root_base $root

  if {[strcmp [bind Viewer] ""] == 0} {
    setup_viewer_bindings
  }

#   # Determine the width of the first tab by calculating the width of the
#   # text_mark
#   global text_mark
#   set tabwidth [expr ([image width $text_mark] + 4) / (72 * [tk scaling])]
#   puts "tabwidth = $tabwidth"
  set tabwidth 0.5

  # Create the text widget for the viewer.
  # The automagic scrollbar system is installed for this widget.
  text $base.file_viewer \
      -height 25 -width 1 -state disabled \
      -wrap none \
      -background black -foreground grey \
      -selectbackground grey -selectforeground black \
      -insertbackground green 
  #-tabs "${tabwidth}i left 1i left 1i left 1i left" 

  magic_scroll $base.file_viewer $frame

  # Replace the Text group with the Viewer group
  set tags [bindtags $base.file_viewer]
  set index [lsearch $tags Text]
  set tags [lreplace $tags $index $index Viewer]
  bindtags $base.file_viewer $tags

  global highlight_tags
  # Preconfigure the code highlight tags (avoids redraws)
  $base.file_viewer tag configure quote -foreground green3
  $base.file_viewer tag configure keyword -foreground red
  $base.file_viewer tag configure cpp -foreground khaki4
  $base.file_viewer tag configure typename -foreground orange
  $base.file_viewer tag configure comment -foreground tan
  # Make sure comment tags override any others
  $base.file_viewer tag raise comment

  # Create a tag to underline matches
  $base.file_viewer tag configure matched_text -underline yes

  # Setup the search tag
  $base.file_viewer tag configure find -foreground black -background grey

  # Setup the binding for the finder and goto
  bind $base.file_viewer <Control-s> "browser_find_dialog $base.file_viewer"
  bind $base.file_viewer <Control-r> "browser_find_dialog $base.file_viewer"
  bind $base.file_viewer <Meta-g> "browser_goto $base.file_viewer"

  # Setup the popup edit menu
  setup_edit_menu $base.file_viewer.menu $base.file_viewer
  bind $base.file_viewer <Button-3> "tk_popup $base.file_viewer.menu %X %Y"
}

##############################################################################
#
#  Purpose    : Setup the Viewer binding tag.  It is identical to the Text
#               binding tag except without insertion.
#
#  Parameters : NONE
#
#  Result     : NONE
#
##############################################################################

proc setup_viewer_bindings {} {
  # Copy the event bindings from Text to Viewer
  foreach event [bind Text] {
    bind Viewer $event [bind Text $event]
  }

  # Shut any events that can modify the text.  Hopefully this is all of them.
  # It would have been nice if all of these were already grouped into an
  # "Editor" tags group!
  foreach event { <Key> 
    <Key-Delete> <Key-BackSpace> <Control-Key-h> 
    <Meta-Key-Delete> <Meta-Key-BackSpace> <Meta-Key-d>
    <Control-Key-t> <Control-Key-o> <Control-Key-k> <Control-Key-d>
    <Key-Insert> <Key-Return> <Control-Key-i>
    <<Clear>> <<Paste>> <<Cut>> \
    <Button-2> <ButtonPress-2> <ButtonRelease-2>} {
    bind Viewer $event {}
  }


  # Make Tab act like normal widgets
  bind Viewer <Key-Tab> [bind all <Tab>]
  bind Viewer <Shift-Key-Tab> [bind all <Shift-Key-Tab>]

  # Allow use of scroll mouse in viewer
  setup_scroll_bindings Viewer
}

##############################################################################
#
#  Purpose    : Setup an Edit menu
#
#  Parameters : menu - the menu widget to create
#               viewer - the viewer widget it operates on
#
#  Result     : NONE
#
##############################################################################

proc setup_edit_menu {menu viewer} {

  # Set up the Edit menu
  menu $menu -postcommand "edit_menu_filter $menu $viewer"
  set accel [lindex [event info <<Cut>>] 0]; regsub "Key-" $accel "" accel
  $menu add command -label "Cut" -underline 1 -accel $accel \
      -command {clipboard_cut}
  set accel [lindex [event info <<Copy>>] 0]; regsub "Key-" $accel "" accel
  $menu add command -label "Copy" -underline 0 -accel $accel \
      -command {clipboard_copy}
  set accel [lindex [event info <<Paste>>] 0]; regsub "Key-" $accel "" accel
  $menu add command -label "Paste" -underline 0 -accel $accel \
      -command "set_query_selection $menu"

  $menu add separator
  $menu add command -label "Find..." -underline 0 -accel <Control-s> \
      -command "browser_find_dialog $viewer"
  $menu add command -label "Goto line..." -underline 0 \
      -accel <Meta-g> -command "browser_goto $viewer"
}

##############################################################################
#
#  Purpose    : Filter the entries in the edit menu before posting.
#
#  Parameters : menu - the menu it was invoked on
#               viewer - the viewer widget it checks
#
#  Result     : NONE
#
##############################################################################

proc edit_menu_filter {menu viewer} {
  global current_file
  if {[info exists current_file($viewer)] &&
      [strlen $current_file($viewer)] > 0} {
    $menu entryconfigure "Find..." -state normal
    $menu entryconfigure "Goto line..." -state normal
  } else {
    $menu entryconfigure "Find..." -state disabled
    $menu entryconfigure "Goto line..." -state disabled
  }

  set widget [selection own]
  if {[strlen $widget] > 0} {
    $menu entryconfigure "Copy" -state normal
    if {[strcmp [winfo class $widget] "Entry"] == 0} {
      if {[strcmp [$widget cget -state] "normal"] == 0} {
        $menu entryconfigure "Cut"  -state normal
      } else {
        $menu entryconfigure "Cut"  -state disabled
      }
    } else {
      $menu entryconfigure "Cut"  -state disabled
    }
  } else {
    $menu entryconfigure "Cut"  -state disabled
    $menu entryconfigure "Copy" -state disabled
  }
}

#----------------------------------------------------------------------------
# The following functions for automagic scrollbars deserve some comments.
# The basic idea is to leave the scrollbars unpacked until they are needed.
# Once they are needed, leave them up, even if the window is scrolled to the
# point where no lines extend out of the window.  This avoids the possibility
# of an infinite loop.  If the size of the window is increased, it is safe to
# unpack the scrollbars, because they will be repacked by the resulting
# scrollcommand if they are needed.
#----------------------------------------------------------------------------

##############################################################################
#
#  Purpose    : Install magic scrollbars around the given widget.
#
#  Parameters : widget - the widget to put scrollbars on
#               frame - the frame to pack it in
#
#  Result     : NONE
#
##############################################################################

set magic_scroll [expr [info tclversion] >= 8.0]

proc magic_scroll {widget frame} {

  global magic_scroll

  # Create the scrollbars
  scrollbar ${widget}_xscroll -orient h -takefocus 0 \
      -command [list ${widget} xview]
  scrollbar ${widget}_yscroll -orient v -takefocus 0 \
      -command [list ${widget} yview]

  # Use these commands to pack the scrollbars
  set pack_xscroll [list grid ${widget}_xscroll \
                        -in $frame -row 1 -column 0 -sticky we]
  set pack_yscroll [list grid ${widget}_yscroll \
                        -in $frame -row 0 -column 1 -sticky ns]

  # Pack the scrollable widget
  grid $widget -row 0 -column 0 -sticky nesw -in $frame
  
  if {$magic_scroll} {
    ${widget} configure \
        -xscrollcommand [list scroll_set ${widget}_xscroll $pack_xscroll] \
        -yscrollcommand [list scroll_set ${widget}_yscroll $pack_yscroll]

    # Handle automagic scrollbars removal when the widget resizes
    bind $widget <Configure> \
      "scroll_reconfigure %W %w %h ${widget}_xscroll ${widget}_yscroll"
  } else {
    ${widget} configure \
      -xscrollcommand [list ${widget}_xscroll set] \
      -yscrollcommand [list ${widget}_yscroll set]

    eval $pack_xscroll
    eval $pack_yscroll
  }

  # Configure the rows and columns
  grid rowconfigure    $frame 0 -weight 1
  grid columnconfigure $frame 0 -weight 1
  grid columnconfigure $frame 1 -weight 0
}

##############################################################################
#
#  Purpose    : Set the scrollbar to match the window.  Pack it if it is not
#               already packed.  For use as [xy]scrollcommand.
#
#  Parameters : scrollbar - the scrollbar to modify
#               geoCmd - the command to pack the scrollbar
#               offset - the offset within the window
#               size - the overall size of the window
#
#  Result     : NONE
#
##############################################################################

proc scroll_set {scrollbar geoCmd offset size} {

  set ispacked [string length [winfo manager $scrollbar]]

  if {$offset != 0.0 || $size != 1.0} {
    if {!$ispacked} {
      eval $geoCmd
    }
  }
  $scrollbar set $offset $size
}

##############################################################################
#
#  Purpose    : Remove scrollbars when widget size increases.
#
#  Parameters : widget - the widget being reconfigured
#               width - the new width
#               height - the new height
#               xscroll - the corresponding x scrollbar
#               yscroll - the corresponding y scrollbar
#
#  Result     : NONE
#
##############################################################################

proc scroll_reconfigure {widget width height xscroll yscroll} {
  global widget_width widget_height

  if {[info exists widget_width($widget)] && 
      $width > $widget_width($widget)} {

    set xview [$widget xview]
    if { [lindex $xview 0] == 0.0 && [lindex $xview 1] == 1.0} {
      unmap $xscroll
    }
  } 

  if {[info exists widget_height($widget)] && 
      $height > $widget_height($widget)} {  

    set yview [$widget yview]
    if { [lindex $yview 0] == 0.0 && [lindex $yview 1] == 1.0} {
      unmap $yscroll
    }
  }

  # Record the new width and height
  set widget_width($widget) $width
  set widget_height($widget) $height
}

###############################################################################
#
#  Purpose    : Unmap a widget using whatever geometry manager was used to
#               map it.
#
#  Parameters : widget - the widget to unmap
#
#  Result     : NONE
#
##############################################################################

proc unmap {widget} {
  set manager [winfo manager $widget]
  if {[string length $manager] > 0} {
    $manager forget $widget
  }
}


##############################################################################
#
#  Purpose    : Create a dialog box for finding text in the textwidget.
#
#  Parameters : textw - the text widget to act upon
#
#  Result     : NONE
#
##############################################################################

proc browser_find_dialog {textw} {
  global history_button find_hlist find_dialog current_file

  set toplevel $textw.finder

  if {[strcmp [$textw cget -state] "disabled"] == 0} {
    return
  }

  if { ![winfo exists $toplevel] } {
    toplevel $toplevel

    frame $toplevel.toprow
    pack $toplevel.toprow -side top -expand yes -fill x  -pady 5

    label $toplevel.l -text "Find:"
    entry $toplevel.entry -textvariable find_text($textw)
    menubutton $toplevel.recall -image $history_button \
        -menu $toplevel.recall.menu
    menu $toplevel.recall.menu
    pack $toplevel.l -side left -in $toplevel.toprow -padx 5
    pack $toplevel.entry -side left -in $toplevel.toprow -expand yes -fill x
    pack $toplevel.recall -side left -in $toplevel.toprow -padx 3

    frame $toplevel.checkrow
    pack $toplevel.checkrow -side top -pady 5

    checkbutton $toplevel.regexp -variable find_regexp($textw) -text "Regexp"
    checkbutton $toplevel.case -variable find_case($textw) -text "Case Sensitive"
    pack $toplevel.regexp $toplevel.case -side left -in $toplevel.checkrow

    frame $toplevel.buttonbar
    pack $toplevel.buttonbar -side top -expand yes -pady 3

    button $toplevel.forward  -text "Forward" -underline 0 \
        -command "browser_find $textw -forward; focus $toplevel.forward"
    button $toplevel.backward -text "Backward" -underline 0 \
        -command "browser_find $textw -backward; focus $toplevel.backward"
    button $toplevel.close    -text "Close" -underline 0 \
        -command "$textw tag remove find 1.0 end
                  wm withdraw $toplevel"

    bind $toplevel <Control-f> "$toplevel.forward invoke"
    bind $toplevel <Meta-f>    "$toplevel.forward invoke"
    bind $toplevel <Control-b> "$toplevel.backward invoke"
    bind $toplevel <Meta-b>    "$toplevel.backward invoke"
    bind $toplevel <Control-s> "$toplevel.forward invoke"
    bind $toplevel <Control-r> "$toplevel.backward invoke"
    bind $toplevel <<Cancel>>  "$toplevel.close invoke"
    bind $toplevel <Meta-c>    "$toplevel.close invoke"

    bind $toplevel.entry    <Return> "focus $toplevel.forward"
    bind $toplevel.entry    <Key-Up> \
        "field_history %W %K find_hindex find_hlist"
    bind $toplevel.entry    <Key-Down> \
        "field_history %W %K find_hindex find_hlist"
    bind $toplevel.forward  <Return> "%W invoke"
    bind $toplevel.backward <Return> "%W invoke"
    bind $toplevel.close    <Return> "%W invoke"

    # Ensure that the array elements are unset when the dialog is destroyed
    bind $toplevel          <Destroy> "browser_find_close"

    pack $toplevel.forward $toplevel.backward $toplevel.close \
        -in $toplevel.buttonbar -side left -padx 10

    # Initialize the start and end marks to the first position
    $textw mark set find_start 1.0
    $textw mark set find_end   1.0

    # Initialize the recall history if unset
    if ![info exists find_hlist] {
      set find_hlist ""
      $toplevel.recall configure -state disabled
    }

    # Track the dialog for each widget
    set find_dialog($textw) $toplevel

    # Populate the recall menu
    foreach item $find_hlist {
      $toplevel.recall.menu add command -label $item \
        -command [list set find_text($textw) $item]
    }

    wm title $toplevel "Find: $current_file($textw)"
    wm minsize $toplevel 300 110

    tkwait visibility $toplevel

    bind $toplevel <Destroy> "browser_find_close $textw"
  } else {
    wm deiconify $toplevel
  }
  catch {raise $toplevel}
  catch {focus -force $toplevel.entry}
}

##############################################################################
#
#  Purpose    : Cleanup when browser_find dialog is closed.
#
#  Parameters : toplevel - the text widget associated with the dialog being
#               destroyed
#
#  Result     : NONE
#
##############################################################################

proc browser_find_close {textw} {
  foreach array { \
    find_text find_regexp find_case find_dialog} {

    global $array
    # NOTE: the backslash is needed to suppress the array substitution.
    if [info exists $array\($textw\)] {
      unset $array\($textw\)
    }
  }
}

##############################################################################
#
#  Purpose    : Find the selected text in the given text widget.
#
#  Parameters : widget - the text widget to search in
#               direction - direction to search in
#
#  Result     : NONE
#
##############################################################################

proc browser_find {widget {direction -forward}} {
  global find_regexp find_case find_text find_hlist find_hindex find_dialog

  $widget tag remove find 1.0 end

  if {[strlen $find_text($widget)] == 0} {
    return
  }

  # Add text to history.
  if {[lsearch $find_hlist $find_text($widget)] < 0} {
    set find_hlist [concat [list $find_text($widget)] $find_hlist]
    if [info exists find_hindex] {unset find_hindex}

    foreach widget [array names find_text] {
      set finder "$widget.finder"
      if [winfo exists $finder] {
        $finder.recall.menu add command -label $find_text($widget) \
            -command [list set find_text($widget) $find_text($widget)]
        $widget.finder.recall configure -state normal
      }
    }
  }

  set args ""

  if {$find_regexp($widget)} {
    lappend args "-regexp"
  }
  if {!$find_case($widget)} {
    lappend args "-nocase"
  }

  # By default, use the current insertion point as the start of the search
  set index insert

  # If switching directions, make sure not to just find the same point
  if {[string match {-b*} $direction] && 
      [$widget compare insert == find_end]} {
     set index find_start
  } elseif {[string match {-f*} $direction] &&
            [$widget compare insert == find_start]} {
    set index find_end
  }

  set match [eval [concat $widget search $direction $args -count count -- \
                       [list $find_text($widget)] $index]]

  if {[strlen $match] > 0} {
    $widget see $match

    set match_end "$match +$count char"
    $widget tag add find $match $match_end

    # Set the new start and end marks
    $widget mark set find_start $match
    $widget mark set find_end   $match_end

    if {[string match {-b*} $direction]} {
      $widget mark set insert $match
    } elseif {[string match {-f*} $direction]} {
      $widget mark set insert $match_end
    } else {
      error "bad direction, expected -backward or -forward"
    }
  } else {
    tk_messageBox -type ok -parent $widget \
        -title "Not Found" -message "$find_text($widget) not found"
    # Reraise and focus the find dialog.
    if ![catch {raise $find_dialog($widget)}] {
      focus $find_dialog($widget).entry
    }
  }
}

##############################################################################
#
#  Purpose    : Popup a dialog box to allow the user to jump to a particular
#               line.
#
#  Parameters : textw - the text widget to act upon
#
#  Result     : NONE
#
##############################################################################

proc browser_goto {textw} {
  global goto_line

  set toplevel $textw.goto

  if {[strcmp [$textw cget -state] "disabled"] == 0} {
    return
  }

  if {![winfo exists $textw.goto]} {
    toplevel $toplevel

    frame $toplevel.top_row
    label $toplevel.label -text "Goto line:"
    entry $toplevel.entry -textvariable goto_line($textw) -width 5
    pack $toplevel.label -side left -in $toplevel.top_row
    pack $toplevel.entry -side left -fill x -in $toplevel.top_row
    pack $toplevel.top_row -side top -fill x

    scale $toplevel.slider -from 1 -variable goto_line($textw) \
        -orient horizontal -showvalue true -bigincrement 50

    pack $toplevel.slider -side top -fill x -expand yes -pady 3 -padx 3

    frame $toplevel.buttonbar
    button $toplevel.ok -text OK -width 8 \
        -command "$textw mark set insert \$goto_line($textw).1
                  $textw see insert
                  focus $textw
                  wm withdraw $toplevel"
    button $toplevel.cancel -text Cancel -width 8 \
        -command "wm withdraw $toplevel"
    pack $toplevel.ok $toplevel.cancel -side left -in $toplevel.buttonbar \
        -padx 30

    pack $toplevel.buttonbar -side top

    bind $toplevel <Return> "$toplevel.ok invoke"
    bind $toplevel <<Cancel>> "$toplevel.cancel invoke"

    wm geometry $toplevel 500x130
    wm minsize $toplevel 300 135
    wm title $toplevel "Goto Line"
  } else {
    wm deiconify $toplevel
  }

  set numlines [expr int([$textw index end])]
  set tickinterval [expr $numlines / 7]
  $toplevel.slider configure -to $numlines -tickinterval $tickinterval

  set goto_line($textw) [expr int([$textw index insert])]

  catch {raise $toplevel}
  catch {focus -force $toplevel.entry}
  # Select the line number so immediate typing will override the value.
  $toplevel.entry select range 0 end
}


##############################################################################
#
#  Purpose    : Trim a history list to a certain number of entries.
#
#  Parameters : varname - the name of the list to modify
#               maxlength - the maximum length of the list
#
#  Result     : the new value of the list
#
##############################################################################

proc history_trim {varname {maxlength 20}} {
  upvar $varname history

  set len [llength $history]
  if {$len > $maxlength} {
    set history [lrange $history [expr $len - $maxlength] end]
  }
  return $history
}

##############################################################################
#
#  Purpose    : Utility function to determine the "base" window path from the
#               root window path.  Basically, base is the prefix for other
#               widgets, while root can stand alone.
#
#  Parameters : root - the root window parameter
#
#  Result     : NONE
#
##############################################################################

proc set_root_base {root} {
  # This treats "." as a special case
  if {[strcmp $root "."] == 0} {
    uplevel [list set base {}]
  } else {
    uplevel [list set base $root]
  }
}

##############################################################################
#
#  Purpose    : Unconditionally execute a block of code after another even if
#               the first returns an error.
#
#  Parameters : bodyform - the main text to evaluate
#               unwindform - the form to evaluate when the body returns
#               resultvar - variable to store the result in
#
#  Result     : return value of bodyform
#
##############################################################################

proc unwind_protect {bodyform unwindform {resultvar ""}} {

  set failed 0
  if [catch {uplevel $bodyform} results] {
    global errorInfo errorCode
    set failed 1
    set info $errorInfo
    set code $errorCode
    set message $results
  }

  uplevel $unwindform

  if {$failed} {
    error $message $info $code
  } else {
    if {[strlen $resultvar] > 0} {
      upvar $resultvar var
      set var $results
    }
    return $results
  }
}


##############################################################################
#
#  Purpose    : Set the status message.
#
#  Parameters : msg - the message to set
#
#  Result     : NONE
#
##############################################################################

proc set_message {msg} {
  global status_msg
  set status_msg $msg
}

##############################################################################
#
#  Purpose    : Clear the file viewer in the given window.
#
#  Parameters : root - the root of the window to operate on
#
#  Result     : NONE
#
##############################################################################

proc display_clear {root} {
  global current_file current_line

  set_root_base $root

  set textw $base.file_viewer

  set current_file($textw) ""
  set current_line($textw) ""

  # Clear the display viewer and disable it
  $textw delete 1.0 end
  $textw configure -state disabled

  # If we're doing magic scrollbars, unmap them
  global magic_scroll
  if {$magic_scroll} {
    # Unmap the scrollbars if they are mapped
    unmap $base.file_viewer_xscroll
    unmap $base.file_viewer_yscroll
  }

  set current_results ""
}

##############################################################################
#
#  Purpose    : Create a dialog box for finding text in the textwidget.
#
#  Parameters : textw - the text widget to act upon
#
#  Result     : NONE
#
##############################################################################

proc browser_find_dialog {textw} {
  global history_button find_hlist find_dialog current_file

  set toplevel $textw.finder

  if {[strcmp [$textw cget -state] "disabled"] == 0} {
    return
  }

  if { ![winfo exists $toplevel] } {
    toplevel $toplevel

    frame $toplevel.toprow
    pack $toplevel.toprow -side top -expand yes -fill x  -pady 5

    label $toplevel.l -text "Find:"
    entry $toplevel.entry -textvariable find_text($textw)
    menubutton $toplevel.recall -image $history_button \
        -menu $toplevel.recall.menu
    menu $toplevel.recall.menu
    pack $toplevel.l -side left -in $toplevel.toprow -padx 5
    pack $toplevel.entry -side left -in $toplevel.toprow -expand yes -fill x
    pack $toplevel.recall -side left -in $toplevel.toprow -padx 3

    frame $toplevel.checkrow
    pack $toplevel.checkrow -side top -pady 5

    checkbutton $toplevel.regexp -variable find_regexp($textw) -text "Regexp"
    checkbutton $toplevel.case -variable find_case($textw) -text "Case Sensitive"
    pack $toplevel.regexp $toplevel.case -side left -in $toplevel.checkrow

    frame $toplevel.buttonbar
    pack $toplevel.buttonbar -side top -expand yes -pady 3

    button $toplevel.forward  -text "Forward" -underline 0 \
        -command "browser_find $textw -forward; focus $toplevel.forward"
    button $toplevel.backward -text "Backward" -underline 0 \
        -command "browser_find $textw -backward; focus $toplevel.backward"
    button $toplevel.close    -text "Close" -underline 0 \
        -command "$textw tag remove find 1.0 end
                  wm withdraw $toplevel"

    bind $toplevel <Control-f> "$toplevel.forward invoke"
    bind $toplevel <Meta-f>    "$toplevel.forward invoke"
    bind $toplevel <Control-b> "$toplevel.backward invoke"
    bind $toplevel <Meta-b>    "$toplevel.backward invoke"
    bind $toplevel <Control-s> "$toplevel.forward invoke"
    bind $toplevel <Control-r> "$toplevel.backward invoke"
    bind $toplevel <<Cancel>>  "$toplevel.close invoke"
    bind $toplevel <Meta-c>    "$toplevel.close invoke"

    bind $toplevel.entry    <Return> "focus $toplevel.forward"
    bind $toplevel.entry    <Key-Up> \
        "field_history %W %K find_hindex find_hlist"
    bind $toplevel.entry    <Key-Down> \
        "field_history %W %K find_hindex find_hlist"
    bind $toplevel.forward  <Return> "%W invoke"
    bind $toplevel.backward <Return> "%W invoke"
    bind $toplevel.close    <Return> "%W invoke"

    # Ensure that the array elements are unset when the dialog is destroyed
    bind $toplevel          <Destroy> "browser_find_close"

    pack $toplevel.forward $toplevel.backward $toplevel.close \
        -in $toplevel.buttonbar -side left -padx 10

    # Initialize the start and end marks to the first position
    $textw mark set find_start 1.0
    $textw mark set find_end   1.0

    # Initialize the recall history if unset
    if ![info exists find_hlist] {
      set find_hlist ""
      $toplevel.recall configure -state disabled
    }

    # Track the dialog for each widget
    set find_dialog($textw) $toplevel

    # Populate the recall menu
    foreach item $find_hlist {
      $toplevel.recall.menu add command -label $item \
        -command [list set find_text($textw) $item]
    }

    wm title $toplevel "Find: $current_file($textw)"
    wm minsize $toplevel 300 110

    tkwait visibility $toplevel

    bind $toplevel <Destroy> "browser_find_close $textw"
  } else {
    wm deiconify $toplevel
  }
  catch {raise $toplevel}
  catch {focus -force $toplevel.entry}
}

##############################################################################
#
#  Purpose    : Cleanup when browser_find dialog is closed.
#
#  Parameters : toplevel - the text widget associated with the dialog being
#               destroyed
#
#  Result     : NONE
#
##############################################################################

proc browser_find_close {textw} {
  foreach array { \
    find_text find_regexp find_case find_dialog} {

    global $array
    # NOTE: the backslash is needed to suppress the array substitution.
    if [info exists $array\($textw\)] {
      unset $array\($textw\)
    }
  }
}

##############################################################################
#
#  Purpose    : Find the selected text in the given text widget.
#
#  Parameters : widget - the text widget to search in
#               direction - direction to search in
#
#  Result     : NONE
#
##############################################################################

proc browser_find {widget {direction -forward}} {
  global find_regexp find_case find_text find_hlist find_hindex find_dialog

  $widget tag remove find 1.0 end

  if {[strlen $find_text($widget)] == 0} {
    return
  }

  # Add text to history.
  if {[lsearch $find_hlist $find_text($widget)] < 0} {
    set find_hlist [concat [list $find_text($widget)] $find_hlist]
    if [info exists find_hindex] {unset find_hindex}

    foreach widget [array names find_text] {
      set finder "$widget.finder"
      if [winfo exists $finder] {
        $finder.recall.menu add command -label $find_text($widget) \
            -command [list set find_text($widget) $find_text($widget)]
        $widget.finder.recall configure -state normal
      }
    }
  }

  set args ""

  if {$find_regexp($widget)} {
    lappend args "-regexp"
  }
  if {!$find_case($widget)} {
    lappend args "-nocase"
  }

  # By default, use the current insertion point as the start of the search
  set index insert

  # If switching directions, make sure not to just find the same point
  if {[string match {-b*} $direction] && 
      [$widget compare insert == find_end]} {
     set index find_start
  } elseif {[string match {-f*} $direction] &&
            [$widget compare insert == find_start]} {
    set index find_end
  }

  set match [eval [concat $widget search $direction $args -count count -- \
                       [list $find_text($widget)] $index]]

  if {[strlen $match] > 0} {
    $widget see $match

    set match_end "$match +$count char"
    $widget tag add find $match $match_end

    # Set the new start and end marks
    $widget mark set find_start $match
    $widget mark set find_end   $match_end

    if {[string match {-b*} $direction]} {
      $widget mark set insert $match
    } elseif {[string match {-f*} $direction]} {
      $widget mark set insert $match_end
    } else {
      error "bad direction, expected -backward or -forward"
    }
  } else {
    tk_messageBox -type ok -parent $widget \
        -title "Not Found" -message "$find_text($widget) not found"
    # Reraise and focus the find dialog.
    if ![catch {raise $find_dialog($widget)}] {
      focus $find_dialog($widget).entry
    }
  }
}

# *** Thanks to Brian Meifert for the original basis of this highlighting code.
# *** It has been completely rewritten to use features of Tk7.6 and for improved
# *** support of C++.

##############################################################################
#
#  Purpose    : Highlight C/C++ code to clarify syntax.
#
#  Parameters : widget - the widget to be acted upon
#
#  Result     : NONE
#
##############################################################################

set highlight_tags {
  comment keyword typename cpp quote
}

set c_keywords "if while for return else typedef struct const static enum
switch case break default extern class inline protected private public virtual
operator using namespace template throw try catch sizeof"

set c_typenames "void char int float double long short unsigned signed wchar_t
bool"

proc c_highlights { widget } {

  # Clear all highlight tags if the code_highlight option is off.
  global code_highlight
  if { ! $code_highlight } {
    global highlight_tags
    foreach tag $highlight_tags {
      $widget tag remove $tag 1.0 end
    }
    return
  }

  comment_highlight $widget
  update idletasks

  quote_highlight $widget
  update idletasks

  global keyword_highlight c_keywords_regexp c_typenames_regexp
  if ![info exists c_keywords_regexp] {
    set c_keywords_regexp "namespace|using|operator|virtual|p(r(ivate|otected)|ublic)|default|break|c(onst|lass|a(tch|se))|s(t(atic|ruct)|witch|izeof)|t(ypedef|emplate|hrow|ry)|e(lse|num|xtern)|return|for|while|i(f|nline)"

    set c_typenames_regexp "void|char|int|float|double|long|short|(un)?signed|wchar_t|bool"
  }

  if {$keyword_highlight} {
    # Highlight C keywords
    highlight_word $c_keywords_regexp keyword $widget
    update idletasks

    highlight_word $c_typenames_regexp typename $widget
    update idletasks

    cpp_highlight $widget
    update idletasks
  } else {
    foreach tag {keyword typename cpp} {
      $widget tag remove $tag 1.0 end
    }
  }
}

##############################################################################
#
#  Purpose    : Trace modifications to the code_highlight and
#               keyword_highlight toggles to ensure the displays are
#               consistent with the settings
#
#  Parameters : variable - the variable to trace
#               index - the index if variable is an array
#               op - the operation performed
#
#  Result     : NONE
#
##############################################################################

proc highlight_trace {variable index op} {
  global current_file current_line
  foreach textw [array names current_file] {
    if {[strlen $current_file($textw)] > 0} {
      c_highlights $textw
    }
  }
}

##############################################################################
#
#  Purpose    : Highlight C/C++ style comments.
#
#  Parameters : widget - the widget to be acted upon
#
#  Result     : NONE
#
##############################################################################

proc comment_highlight {widget} {

  set temp [$widget search -regexp {/\*|//} 1.0]
  while { [strlen $temp] > 0 &&
          [$widget compare $temp < end] } {
    set match [$widget get $temp "$temp + 2chars"]
    if {[strcmp $match "//"] == 0} {
      set endcomment [$widget index "$temp lineend"]
    } else {
      set endcomment [$widget search -regexp {\*/} "$temp + 2chars"]
      if {[strlen $endcomment] == 0} {
        break
      } else {
        set endcomment "$endcomment + 2chars"
      }
    }
    if {[strlen $endcomment] != 0} {
      $widget tag add comment $temp "$endcomment"
      set temp [$widget search -regexp {/\*|//} "$endcomment" end]
    } else {
      break
    }
  }
}

##############################################################################
#
#  Purpose    : To highlight double quoted phrases
#
#  Parameters : widget - the widget to be acted upon
#
#  Result     : NONE
#
##############################################################################

proc quote_highlight { widget } {

  set pattern {[^\\']\"}

  # Look in between commented regions for quotes
  foreach {start end} [concat 1.0 [$widget tag ranges comment] end] {

    while {[set temp [$widget search -regexp -- $pattern $start $end]] != ""} {

      set endquote [$widget search -regexp -- {[^\\]\"} "$temp + 1chars" $end]

      if {[strlen $endquote] > 0} {
        set start [$widget index "$endquote + 2chars"]

        $widget tag add quote "$temp + 1chars" $start
      }
    }
  }
}

##############################################################################
#
#  Purpose    : To highlight the given words in the given text widget
#
#  Parameters : word - regexp pattern to search for
#               color - the text tag to mark the word with
#               widget - the text widget to act on
#
#  Result     : NONE
#
##############################################################################

proc highlight_word {word color widget {range_start 1.0} {range_end end}} {

  set start $range_start
  set end $range_end

  # Safety check, otherwise infinite loop could result...
  if {[strlen $word] <= 0} {
    error "Can't highlight blank pattern"
  }

  while {[$widget compare $start < $end] &&
         [set temp [$widget search -count count -regexp -- \
                        $word $start $end]] != ""} {

    # Check if in a comment
    set crange [$widget tag prevrange comment $temp]
    if {[llength $crange] == 2 &&
        [$widget compare $temp < [lindex $crange 1]]} {
      set start [lindex $crange 1]
      continue
    }

    # Check if in quotes
    set qrange [$widget tag prevrange quote $temp]
    if {[llength $qrange] == 2 &&
        [$widget compare $temp < [lindex $qrange 1]]} {
      set start [lindex $qrange 1]
      continue
    }

    # Make sure that a whole word was found.
    if {[$widget compare [$widget index "$temp wordstart"] == "$temp"] &&
        [$widget compare [$widget index "$temp wordend"] == \
             [$widget index "$temp + ${count}chars"]]} {

      $widget tag add "$color" $temp "$temp + ${count}chars"
    }

    set start [$widget index "$temp +  ${count}chars"]
  }

  return

  # Look in between commented regions for words to mark
  foreach {start end} [concat [list $range_start] \
                           [$widget tag ranges comment] \
                           [list $range_end]] {
    while {[set temp [$widget search -count count -regexp -- \
                          "$word" $start $end]] != ""} {

      # Make sure that a whole word was found.
      if {[$widget compare [$widget index "$temp wordstart"] == "$temp"] &&
          [$widget compare [$widget index "$temp wordend"] == \
               [$widget index "$temp + ${count}chars"]]} {

        $widget tag add "$color" $temp "$temp + ${count}chars"
      }
      set start [$widget index "$temp +  ${count}chars"]
    }
  }

  return
}

##############################################################################
#
#  Purpose    : To highlight preprocessor statements
#
#  Parameters : widget - the widget to be acted upon
#
#  Result     : NONE
#
##############################################################################

proc cpp_highlight {widget} {
  set pattern \
      [subst -nocommands -novariables \
           {^[ \t\n]*\#[ \t]*(ifdef|ifndef|if|define|undef|include|endif|else)}]

  # Look in between commented regions for words to mark
  foreach {start end} [concat 1.0 [$widget tag ranges comment] end] {

    while {[$widget compare $start < $end]} {

      set temp [$widget search -count count -regexp -- $pattern $start $end]
      if {[strlen $temp] > 0} {
        $widget tag add cpp $temp "$temp +${count} chars"
      }
      set start [$widget index "$start lineend +1 char"]
    }
  }
  return
}

#----------------------------------------------------------------------------

##############################################################################
#
#  Purpose    : Utility function to be used as a trace.
#               The simple boolean variable controls the state of
#               the given entry in the given menu,
#
#  Parameters : menu - the menu widget where the toggle lives
#               entry - the label of the toggle widget
#               varname - the name of the toggle controlling variable
#               index - required parameter when used as trace, ignored
#               op -  required parameter when used as trace, ignored
#
#  Result     : NONE
#
#############################################################################

proc toggle_trace {menu entry varname index op} {
  upvar $varname var
  if {$var} {
    $menu entryconfigure $entry -state normal
  } else {
    $menu entryconfigure $entry -state disabled
  }
}

##############################################################################
#
#  Purpose    : Allow only a single tearoff for a menu.
#               Use as -tearoffcommand
#
#  Parameters : menu - the menu being tornoff
#               torn - the new tearoff menu
#
#  Result     : NONE
#
##############################################################################

proc single_tearoff {menu torn} {
  global tearoff

  # Record the correspondence with the tearoff
  set tearoff($menu) $torn

  # Disable future tearoffs
  $menu configure -tearoff 0

  # Restore when the tearoff is deleted
  bind $torn <Destroy> \
      "$menu configure -tearoff 1;
       unset tearoff($menu)"
}

##############################################################################
#
#  Purpose    : Add an item to a field's scrollable history
#
#  Parameters : field - the entry field to scroll
#               direction - Up or Down
#               indexvar - the index of the current value
#               hlistvar - the history list variable
#
#  Result     : NONE
#
##############################################################################

proc field_history_add {hlistvar indexvar value} {
  upvar \#0 $indexvar hindex
  upvar \#0 $hlistvar hlist

  # Reset the history index mechanism, and add this to the list
  if [info exists hindex] {unset hindex}

  if [info exists hlist] {
    # If already in the list, remove it
    set index [lsearch $hlist $value]
    if {$index >= 0} {
      set hlist [lreplace $hlist $index $index]
    }
    set hlist [concat [list $value] $hlist]
  } else {
    set hlist $value
  }
}

##############################################################################
#
#  Purpose    : Scroll through field history with keyboard
#
#  Parameters : field - the entry field to scroll
#               direction - Up or Down
#               indexvar - the index of the current value
#               hlistvar - the history list variable
#
#  Result     : NONE
#
##############################################################################

proc field_history {field direction indexvar hlistvar} {
  upvar \#0 $indexvar hindex
  upvar \#0 $hlistvar hlist

  # If there's no history list, just return
  if ![info exists hlist] {
    return
  }

  set last [expr [llength $hlist] - 1]

  if [info exists hindex] {
    switch -- $direction {
      "Up" {incr hindex}
      "Down" {incr hindex -1}
    }
    if {$hindex > $last} {
      set hindex $last
    }
    if {$hindex < 0} {
      set hindex 0
    }
  } else {
    set hindex 0
  }

  # Set the fields value
  $field delete 0 end
  $field insert 0 [lindex $hlist $hindex]
}

##############################################################################
#
#  Purpose    : Blink an activity indicator every interval.
#
#  Parameters : time - the interval to blink at
#               light - the widget to use as the blinking light
#
#  Result     : NONE
#
##############################################################################

proc activity_start {time light} {
  if {[strcmp [$light cget -background] "grey80"] == 0} {
    $light configure -background green
  } else {
    $light configure -background grey80
  }
  update idletasks

  after $time "activity_start $time $light"
}

##############################################################################
#
#  Purpose    : Stop the blinking activity indicator when the 
#
#  Parameters : time - the interval to blink at
#               light - the widget to use as the blinking light
#
#  Result     : NONE
#
##############################################################################

proc activity_finish {time light} {
  after cancel "activity_start $time $light"
  $light configure -background grey80
}

##############################################################################
#
#  Purpose    : Setup the event bindings for a scroll mouse.
#
#  Parameters : bindtag - the bindtag to setup
#
#  Result     : NONE
#
##############################################################################

proc setup_scroll_bindings {bindtag} {
    bind $bindtag <Button-5> [list %W yview scroll 5 units]
    bind $bindtag <Button-4> [list %W yview scroll -5 units]
    bind $bindtag <Shift-Button-5> [list %W yview scroll 1 units]
    bind $bindtag <Shift-Button-4> [list %W yview scroll -1 units]
    bind $bindtag <Control-Button-5> [list %W yview scroll 1 pages]
    bind $bindtag <Control-Button-4> [list %W yview scroll -1 pages]
}