Check-in [ea4e9a080f]
Not logged in
Overview
Comment: tclreadline.c tclreadlineCompleter.tcl
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ea4e9a080fed5d5a1bdb87cacefeb7c931680b5b
User & Date: johannes@zellner.org on 1999-09-13 00:21:47
Other Links: manifest | tags
Context
1999-09-13
16:33
.tclshrc .vimrc Modified Files: Makefile.in configure.in sample.tclshrc tclreadline.c tclreadline.h.in Added Files: config.h.in check-in: 0d1401e9c1 user: johannes@zellner.org tags: trunk
00:21
tclreadline.c tclreadlineCompleter.tcl check-in: ea4e9a080f user: johannes@zellner.org tags: trunk
1999-09-10
19:01
Modified Files: Tag: 2.0 GPL README TODO configure.in pkgIndex.tcl.in sample.tclshrc sources tclreadline.h.in tclreadline.n.in tclreadlineCompleter.tcl tclreadlineSetup.tcl.in aux/config.guess aux/config.sub aux/install-sh aux/mkinstalldirs aux/tcltags aux/vimtags Added Files: Tag: 2.0 Makefile.in tclreadline.c tclreadlineConfig.sh.in tclreadlineInit.tcl.in check-in: e0dfd309af user: johannes@zellner.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified tclreadline.c from [19dbcdadd7] to [3d6f99eaa5].

1
2
3
4
5
6
7
8
9
10
11
12
...
472
473
474
475
476
477
478
479




480
481
482
483
484
485
486
...
522
523
524
525
526
527
528

529
530
531
532
533
534
535
...
624
625
626
627
628
629
630














631
632
633
634
635
636
637

 /* ==================================================================

    FILE: "/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Fri Sep 10 02:57:55 1999 (joze)"
    (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
    $Id$
    ---

    tclreadline -- gnu readline for tcl
    Copyright (C) 1999  Johannes Zellner

................................................................................
     * default is " \t\n\"\\'`@$><=;|&{("
     * removed "(" <-- arrays
     * removed "{" <-- `${' variables 
     * removed "<" <-- completion lists with < ... >
     * added "[]"
     * added "}"
     */
    rl_basic_word_break_characters = " \t\n\"\\@$}>=;|&[]";




    // rl_basic_quote_characters = "\"{"; // XXX ??? XXX
    // rl_completer_quote_characters = "\"";
    /*
    rl_filename_quote_characters
    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    rl_filename_quoting_function
................................................................................

char**
TclReadlineCompletion(char* text, int start, int end)
{
    char** matches = (char**) NULL;
    int status;
    // rl_attempted_completion_over = 0;


#if 0
    fprintf(stderr, "DEBUG> TclReadlineCompletion: text=|%s|\n", text);
    fprintf(stderr, "DEBUG> TclReadlineCompletion: start=|%d|\n", start);
    fprintf(stderr, "DEBUG> TclReadlineCompletion: end=|%d|\n", end);
#endif

................................................................................
                    return (char**) NULL;
                }
                /*
                fprintf (stderr, "(TclReadlineCompletion) len[%s]=%d\n",
                    matches[i], strlen(matches[i]));
                */
            }














            matches[i] = (char*) NULL; /* terminate */
        }
        Tcl_ResetResult(tclrl_interp); /* clear result space */
    }

    if (!matches && tclrl_use_builtin_completer) {
        matches = completion_matches(text, TclReadline0generator);




|







 







|
>
>
>
>







 







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
...
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
...
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

 /* ==================================================================

    FILE: "/home/joze/src/tclreadline/tclreadline.c"
    LAST MODIFICATION: "Mon Sep 13 02:21:35 1999 (joze)"
    (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
    $Id$
    ---

    tclreadline -- gnu readline for tcl
    Copyright (C) 1999  Johannes Zellner

................................................................................
     * default is " \t\n\"\\'`@$><=;|&{("
     * removed "(" <-- arrays
     * removed "{" <-- `${' variables 
     * removed "<" <-- completion lists with < ... >
     * added "[]"
     * added "}"
     */
    /* 11.Sep rl_basic_word_break_characters = " \t\n\"\\@$}=;|&[]"; */
    /* besser (11. Sept) 2. (removed \") */
    /* rl_basic_word_break_characters = " \t\n\\@$}=;|&[]"; */
    /* besser (11. Sept) 3. (removed }) */
    rl_basic_word_break_characters = " \t\n\\@$=;|&[]";
    // rl_basic_quote_characters = "\"{"; // XXX ??? XXX
    // rl_completer_quote_characters = "\"";
    /*
    rl_filename_quote_characters
    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    rl_filename_quoting_function
................................................................................

char**
TclReadlineCompletion(char* text, int start, int end)
{
    char** matches = (char**) NULL;
    int status;
    // rl_attempted_completion_over = 0;
    rl_completion_append_character = ' '; /* reset, just in case ... */

#if 0
    fprintf(stderr, "DEBUG> TclReadlineCompletion: text=|%s|\n", text);
    fprintf(stderr, "DEBUG> TclReadlineCompletion: start=|%d|\n", start);
    fprintf(stderr, "DEBUG> TclReadlineCompletion: end=|%d|\n", end);
#endif

................................................................................
                    return (char**) NULL;
                }
                /*
                fprintf (stderr, "(TclReadlineCompletion) len[%s]=%d\n",
                    matches[i], strlen(matches[i]));
                */
            }

            /**
             * this is a special one:
             * if the script returns exactly two arguments
             * and the second argument is the empty string,
             * the rl_completion_append_character is set
             * temporaryly to NULL.
             */
            if (2 == objc && !strlen(matches[1])) {
                i--;
                FREE(matches[1]);
                rl_completion_append_character = '\0';
            }

            matches[i] = (char*) NULL; /* terminate */
        }
        Tcl_ResetResult(tclrl_interp); /* clear result space */
    }

    if (!matches && tclrl_use_builtin_completer) {
        matches = completion_matches(text, TclReadline0generator);

Modified tclreadlineCompleter.tcl from [d03579b986] to [6becbaf62d].

1
2
3
4
5
6
7
8
9
10
..
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
...
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
...
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
...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
...
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
...
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
...
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
...
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
...
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
...
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
...
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
...
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
....
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
1598
1599
1600
1601
1602
1603




1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622

1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638



1639
1640
1641
1642
1643
1644
1645
1646

1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663




1664
1665
1666
1667
1668
1669
1670
1671

1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689


1690

1691
1692
1693
1694




1695
1696
1697
1698
1699
1700
1701

1702
1703
1704
1705
1706
1707

1708
































1709
1710
1711
1712
1713















































1714
1715
1716
1717
1718

1719
1720

1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732

1733
1734
1735


1736
1737
1738


1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751

1752





1753
1754


1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782




1783

1784
1785
1786
1787
1788
1789






1790
1791
1792
1793

1794
1795


1796
1797
1798
1799




1800
1801





1802
1803

1804
1805


1806









1807
1808
1809


1810
1811
1812
1813
1814
1815
1816
1817

1818
1819


1820
1821








1822
1823

1824
1825







1826




1827
1828
1829
1830
1831












1832
1833
1834




1835



































1836
1837

1838
1839
1840

1841
1842























1843
1844
1845
1846

1847
1848
1849
1850
1851
1852
1853




1854
1855
1856

















1857
1858
1859
1860
1861

1862
1863
1864
1865
1866








1867
1868
1869


1870
1871
1872
1873
1874
1875



















1876
1877
1878














1879
1880

1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891

1892
1893

1894
1895
1896
1897


1898
1899
1900
1901
1902
1903
1904
1905






1906
1907
1908






1909
1910

1911
1912











1913
1914
1915
1916
1917
1918

1919
1920

1921
1922
1923
1924
1925

1926
1927
1928
1929


1930
1931
1932
1933
1934
1935
1936
1937
1938

1939
1940
1941






1942
1943
1944
1945
1946
1947
1948
1949
#!/usr/locanl/bin/tclsh
# FILE: "/home/joze/src/tclreadline/tclreadlineCompleter.tcl"
# LAST MODIFICATION: "Fri Sep 10 03:06:31 1999 (joze)"
# (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
# $Id$
# ---
#
# tclreadline -- gnu readline for tcl
# Copyright (C) 1999  Johannes Zellner
#
................................................................................
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# johannes@zellner.org
# http://www.zellner.org/tclreadline/
#
# ================================================================== 

# done:
#
# - after
# - append
# - array
# - bgerror
# - binary
# - break
# - catch
# - cd
# - clock
# - close
# - concat
# - continue
# - (ddd is only on M$)
# - encoding
# - eof
# - error
# - eval
# - exec
# - exit
# - expr
# - fblocked
# - fconfigure
# - fcopy
# - file
# - fileevent
# - flush
# - for # TODO
# - foreach # TODO
# - format # TODO
# - gets
# - glob
# - global
# - if # TODO
# - incr
# - index
# - info
# - interp
# - join
# - lappend
# - llength
# - linsert
# - list
# - load
# - lrange
# - lreplace
# - lsearch
# - lsort
# - history
# - load
# - namespace
# - open
# - package
# - pkg_mkIndex
# - proc
# - puts
# - pwd
# - pid
# - read
# - regexp
# - (registry is only on M$)
# - regsub
# - rename
# - (resource is on mac only)
# - return
# - scan # TODO
# - seek
# - socket
# - source
# - split # TODO
# - string
# - subst
# - switch
# - tell
# - time # TODO ??
# - trace
# - set
# - unknown
# - unset
# - update
# - uplevel
# - upvar
# - variable
# - vwait
# - while # TODO
# 







namespace eval tclreadline {








# TryFromList will return an empty string, if
# the text typed so far does not match any of the
# elements in list. This might be used to allow
# subsequent filename completion by the builtin
# completer.
#
proc TryFromList {text lst} {

    # puts stderr "(CompleteFromList) \ntext=|$text|"
    # puts stderr "(CompleteFromList) lst=|$lst|"
    set pre [GetQuotedPrefix ${text}]
    set matches [MatchesFromList $text $lst]

    # puts stderr "(CompleteFromList) matches=|$matches|"
    if {1 == [llength $matches]} { ; # unique match
        # puts stderr \nunique=$matches\n
        # puts stderr "\n|${pre}${matches}[Right ${pre}]|\n"




        return [string trim ${pre}${matches}[Right ${pre}]]

    } elseif {"" != ${matches}} {
        # puts stderr \nmore=$matches\n
        set longest [CompleteLongest ${matches}]
        # puts stderr longest=|$longest|
        if {"" == $longest} {
            return [string trim "[list $text] ${matches}"]
        } else {
................................................................................
            return [string trim "${pre}${longest} ${matches}"]
        }
    } else {
        return ""; # nothing to complete
    }
}


# CompleteFromList will never return an empty string.
# completes, if a completion can be done, or ring
# the bell if not.
#
proc CompleteFromList {text lst} {
    set result [TryFromList ${text} ${lst}]
    if {![llength ${result}]} {
        Alert
        # return [string trim [list ${text}] ${lst}"]

        return [string trim "${text} ${lst}"]



    } else {
        return ${result}
    }
}

proc MenuFromList {text lst} {





    return [CompleteFromList $text $lst]
}
# ???????
# 
# proc MenuFromList {text lst} {
#     if {![llength ${text}]} {
#         return [string trim "{} ${lst}"]
#     } else {
#         return [TryFromList ${text} ${lst}]
#     }
# }




# 


















































#**
# never return an empty string, never complete.
# This is useful for showing options lists for example.
#
proc DisplayHints {lst} {
    return [string trim "{} ${lst}"]
}

#**
# find (partial) matches for `text' in `lst'.
# Ring the bell and return the whole list, if
# the user tries to complete ?..? options or
# <..> hints.
#





proc MatchesFromList {text lst} {
    set result ""
    set text [StripPrefix $text]
    set null [string index $text 0]
    if {"<" == $null || "?" == $null} {

        Alert
        return $lst

    }
    # puts stderr "(MatchesFromList) text=$text"
    # puts stderr "(MatchesFromList) lst=$lst"
    foreach word $lst {
        if {[string match ${text}* ${word}]} {
            lappend result ${word}
        }
................................................................................
    return ${expr_pos}
}

proc RemoveUsedOptions {line opts {terminate {}}} {
    if {[llength ${terminate}]} {
        if {[regexp -- ${terminate} ${line}]} {
            return ""

        }
    }
    set new ""
    foreach word ${opts} {
        if {![regexp -- ${word} ${line}]} {
            lappend new ${word}
        }
    }
    return [string trim ${new}]
}

proc Alert {} {
    puts -nonewline \a
    flush stdout
}


# get the longest common completion
# e.g. str == {tcl_version tclreadline_version tclreadline_library}
# --> [CompleteLongest ${str}] == "tcl"
#
proc CompleteLongest {str} {
    # puts stderr str=$str
    set match0 [lindex ${str} 0]
................................................................................
    if {1 < [llength $value] && "" == $right} {
        return [list \"${value}\"]
    } else {
        return [list ${left}${value}${right}]
    }
}






proc InChannelId {text} {
    # return [ChannelId ${text} inChannel {stdin}]









    return [ChannelId ${text} inChannel]
}

proc OutChannelId {text} {

    # return [ChannelId ${text} outChannel {stdout stderr}]







    return [ChannelId ${text} outChannel]
}

proc ChannelId {text {descript channelId} {chs ""}} {
    if {"" == ${chs}} {
        # the `file channels' command is present
        # only in pretty new versions.
        #
        if [catch {set chs [file channels]}] {
            set chs {stdin stdout stderr}
        }
    }
    if {[llength [set channel [MatchesFromList ${text} ${chs}]]]} {
        return ${channel}
    } else {
        return [DisplayHints ${descript}]
    }
}

proc QuoteQuotes {line} {
    regsub -all -- \" $line {\"} line
    regsub -all -- \{ $line {\{} line; # \}\} (keep the editor happy)
    return $line
................................................................................
#     return [llength $pre_text]
# 
}

proc Right {left} {
    # puts left=$left
    if {"\"" == $left} {
        return {\"}
    } elseif {"\\\"" == $left} {
        return "\\\""
    } elseif {"\{" == $left} {
        return "\}"
    } elseif {"\\\{" == $left} {
        return "\\\}"
    }
................................................................................
    set pos 0
    while {-1 != [set pos [string first $char $line $pos]]} {
        incr pos
        incr found
    }
    return $found
}



































#**
# save `lindex'. works also for non-complete lines
# with opening parentheses or quotes.
# usage as `lindex'.


#
proc Lindex {line pos} {
    if {[catch [list set sub [lindex $line $pos]]]} {
        set diff [expr [CountChar $line \{] - [CountChar $line \}]]
        # puts stderr diff=$diff
        for {set i 0} {$i < $diff} {incr i} { ; # \{ keep the editor happy
            append line \}
        }

        # puts stderr line=$line
        if {!$diff || [catch [list set sub [lindex $line $pos]]]} {
            if {[expr [CountChar $line \"] % 2]} { append line \" }
        }
        if {[catch [list set sub [lindex $line $pos]]]} { return {} }
    }
    return $sub
}

#**
# save `llength' (see above).
#
proc Llength {line} {
    set diff 0
    if {[catch [list set len [llength $line]]]} {
        set diff [expr [CountChar $line \{] - [CountChar $line \}]]
        # puts stderr diff=$diff
        for {set i 0} {$i < $diff} {incr i} { ; # \{ keep the editor happy
            append line \}
        }
        if {$diff < 0} {
            set diff 0
        }
        # puts stderr line=$line
        if {!$diff || [catch [list set len [llength $line]]]} {
            incr diff
            if {[expr [CountChar $line \"] % 2]} { append line \" }
        }

        if {[catch [list set len [llength $line]]]} { return {} }
    }

    return [expr $len - $diff]
}

proc StripPrefix {text} {
    # puts "(StripPrefix) text=|$text|"
    set null [string index $text 0]
    if {"\"" == $null || "\{" == $null} {
        return [string range $text 1 end]
    } else {
        return $text
    }
}

proc ListCompletion {text {level -1}} {
    # TODO
    return ""
    # return [VarCompletion ${text} ${level}]
}

proc VarCompletion {text {level -1}} {
    if {-1 == ${level}} {
        set level [info level]
    } else {
        incr level
    }
    set pre [GetQuotedPrefix ${text}]
................................................................................
        }
        set namespaces ${new}
        unset new
    }
    set matches \
    [string trim "[uplevel ${level} info vars ${var}*] ${namespaces}"]
    if {1 == [llength $matches]} { ; # unique match

        # check if this unique match is an
        # array name, (whith no "(" yet).
        #
        if {[uplevel ${level} array exists $matches]} {
            return [VarCompletion ${matches}( ${level}]; # recursion
        } else {
            return ${pre}${matches}[Right ${pre}]
        }
    } elseif {"" != $matches} { ; # more than one match
        #puts stderr "(VarComletion) matches=|$matches|"
        #puts stderr "(VarComletion) text=|$text|"



# 
#         set common [CompleteLongest ${matches}]
#         if {"" == ${common}} {
#             return [Format ${matches} ${text}]







#         } else {
#             return [string trim "${pre}${common} ${matches}"]
#         }






























# 
          return [CompleteFromList ${text} ${matches}]



    } else {
        return ""; # nothing to complete



    }


}


proc FullQualifiedMatches {qualifier matchlist} {
    set new ""



    foreach entry ${matchlist} {
        set full ${qualifier}::${entry}
        if {"" != [namespace which ${full}]} {
            lappend new ${full}
        }
    }
    return ${new}
}

................................................................................
    return [CommandCompletion ${cmd} procs]
}

proc CommandsOnlyCompletion {cmd} {
    return [CommandCompletion ${cmd} commands]
}

proc CommandCompletion {cmd {action both} {spc ::}} {
    # puts stderr "(CommandCompletion) cmd=|$cmd|"
    # puts stderr "(CommandCompletion) action=|$action|"
    # puts stderr "(CommandCompletion) spc=|$spc|"








    set quali [namespace qualifiers ${cmd}]
    if {[llength ${quali}]} {


        set rec [CommandCompletion [namespace tail ${cmd}] ${action} ${quali}]
        return [FullQualifiedMatches ${quali} ${rec}]


    }
    # puts stderr \ncmd=|$cmd|\n
    set cmd [string trim ${cmd}]*

    if {"procs" != ${action}} {
        set commands [namespace eval $spc "info commands [QuoteQuotes ${cmd}]"]

        # puts stderr commands=|$commands|






    } else {
        set commands ""
    }
    if {"commands" != ${action}} {
        set procs [namespace eval $spc "info procs [QuoteQuotes ${cmd}]"]
        # puts stderr procs=|$procs|







    } else {
        set procs ""
    }
    set matches [namespace eval $spc concat ${commands} ${procs}]
# 
#     foreach match ${matches} {
#         set full ${spc}::${match}
#         if {"" != [namespace which ${full}]} {
#             lappend new bla::${full}
#         }
#     }
# 
    set namespaces [namespace children $spc ${cmd}]

    if {![llength ${matches}] && 1 == [llength ${namespaces}]} {
        set namespaces [string trim ${namespaces}]
        regsub {^([^:])} $namespaces {::\1} namespaces
    #    set matches [namespace eval ${namespaces} \
    #    {concat [info commands] [info procs]}]
        if {"procs" != ${action}} {
            set n_commands [namespace eval ${namespaces} "info commands"]
        } else {
            set n_commands ""



        }
        if {"commands" != ${action}} {
            set n_procs [namespace eval ${namespaces} "info procs"]
        } else {
            set n_procs ""
        }
        set matches [string trim "${n_commands} ${n_procs}"]
        if {[llength ${matches}]} {


# 
#             foreach match ${matches} {
#                 set full ${namespaces}::${match}
#                 if {"" != [namespace which ${full}]} {
#                     lappend new ${namespaces}::${match}
#                 }
#             }

# 
#             set matches ${new}
#             unset new
# 
            set matches [FullQualifiedMatches ${namespaces} ${matches}]
            set namespaces ""
        }

        return [string trim "${matches} ${namespaces}"]
    } else {
        return [string trim "${matches} ${namespaces}"]
    }
}

#**
# check, if the first argument starts with a '['
# and must be evaluated before continuing.

# NOTE: eventually modifies all arguments.
# DATE: Sep-06-1999
#
proc EventuallyEvaluateFirst {partT startT endT lineT} {
    # return; # disabled
    upvar $partT part $startT start $endT end $lineT line


    set line [string trim ${line}]




    set char [string index ${line} 0]
    if {{[} != ${char} && {$} != ${char}} {return}

    set pos 0
    while {-1 != [set idx [string first {]} ${line} ${pos}]]} {
        set cmd [string range ${line} 0 ${idx}]
................................................................................
# % puts $b<TAB>
# part  == $b
# start == 5
# end   == 7
# line  == "$puts $b"
#
proc ScriptCompleter {part start end line} {

    # puts stderr "(ScriptCompleter) |$part| $start $end |$line|"
    variable known_cmds











    if {{$} == [string index $part 0]} {

        # check for a !$ history event
        #
        if {$start > 0} {
            if {{!} == [string index $line [expr $start - 1]]} {
                return ""
            }
        }
        # variable completion. Check first, if the
        # variable starts with a plain `$' or should
        # be enclosed in braces.
        #
        set var [string range $part 1 end]
# 
#         if {"\{" == [string index $part 1]} {
#             set var [string range $part 2 end]
#             set left "\{"
#         } else {
#             set left ""
#             set var [string range $part 1 end]
#         }
# 
        # check if $var is an array name, which
        # already has already a "(" somewhere inside.
        #
        if {"" != [set vc [VarCompletion $var]]} {
            if {"" == [lindex $vc 0]} {
                return "\$ [lrange ${vc} 1 end]"
            } else {
                return \$${vc}
            }
            # puts stderr vc=|$vc|
        } else {
            return ""
        }

    # SCENARIO:
    #
    # % puts bla; put<TAB> $b
    # part  == put
    # start == 10
    # end   == 13
    # line  == "puts bla; put $b"
................................................................................
        set new_start [lindex $sub 0]
        set new_end [expr $end - ($start - $new_start)]
        set new_line [lindex $sub 1]
        # puts stderr "(SplitLine) $new_start $new_end $new_line"
        return [ScriptCompleter $part $new_start $new_end $new_line]
    } elseif {0 == [set pos [PartPosition part start end line]]} {
        # puts stderr "(PartPosition) $part $start $end $line"
        # set matches [array names known_cmds "[string trim ${part}]*"]
        set all [CommandCompletion ${part}]
        # puts stderr "(ScriptCompleter) all=$all"
        #puts \nmatches=$matches\n
        # return [Format $all $part]
        return [TryFromList $part $all]
    } else {

        # try to use $pos further ...
        # puts stderr |$line|

        if {"." == [string index [string trim ${line}] 0]} {
            set alias WIDGET
        } else {






            set alias [lindex [QuoteQuotes ${line}] 0]
        }















        foreach cmd [list ${alias} tclreadline_complete_unknown] {

            if {"" != [namespace eval ::tclreadline \
                [list info procs complete(${cmd})]]
            } {

                # to be more error-proof, we check here,
                # if complete($cmd) takes exactly 5 arguments.
                #
                if {6 != [set arguments [llength \
                    [namespace eval ::tclreadline \
                    [list info args complete(${cmd})]]]]
                } {
                    error [list complete(${cmd}) takes ${arguments} \
                    arguments, but should take exactly 6.]
                }

                # remove leading quotes
                #
                set mod [StripPrefix $part]
                # puts stderr mod=$mod

                if {[catch [list set script_result \

                    [complete(${cmd}) $part \
                    $start $end $line $pos $mod]] ::tclreadline::errorMsg]
                } {
                    error "error during evaluation of `complete(${cmd})'"
                }
                # puts stderr \nscript_result=|${script_result}|
                return ${script_result}
            }

        }
        # no specific command completer found.
        if {"" != [array names known_cmds $cmd]} {
            set current [lindex $known_cmds($cmd) $pos]
            if {"" != $current && "" == [string trim $part]} {
                return $current
            } else {
                return ""
            }
        } else {
            return ""
        }
    }
    error "{NOTREACHED (this is probably an error)}"
}


# explicit command completers
#
................................................................................
    switch -- $pos {
        1 {
            return [CompleteFromList ${text} {<ms> cancel idle info}]
        }
        2 {
            switch -- $sub {
                cancel {
                    set after_info [after info]
                    if {![llength $after_info]} {
                        return [DisplayHints <script>]
                    } else {
                        return [CompleteFromList $mod "<script> [after info]"]
                    }
                }
                idle {
                    return [DisplayHints <script>]
                }
                info {
                    return [CompleteFromList $mod [after info]]
                }
                default { return [DisplayHints ?script?] }
            }
        }
        default {
            switch -- $sub {
                info { return [DisplayHints {}] }
................................................................................
                anymore donesearch exists get names
                nextelement set size startsearch
            }
            return [CompleteFromList $text $cmds]
        }
        2 {
            set matches ""
            set vars [uplevel [info level] info vars ${mod}*]






            foreach var ${vars} {
                if {[uplevel [info level] array exists ${var}]} {
                    lappend matches ${var}
                }
            }
            return [CompleteFromList ${text} ${matches}]
        }
        default {
            set cmd [Lindex $line 1]
            set array_name [Lindex $line 2]
            switch -- $cmd {
                get -
                names {
                    set pattern [Lindex $line 3]
                    set matches [uplevel [info level] \
                    array names ${array_name} ${pattern}*]
                    if {![llength $matches]} {
                        return [Menu ?pattern?]
                    } else {
                        return [CompleteFromList ${text} ${matches}]
                    }
                }
                anymore -
                donesearch -
                nextid { return [Menu <searchId>] }
            }
        }
    }
    return ""
}

# proc complete(bgerror) {text start end line pos mod} {
................................................................................
    }
    return ""
}

# proc complete(cd) {text start end line pos mod} {
# }

proc complete(if) {text start end line pos mod} {
    # TODO: this is not good yet.
    switch -- $pos {
        1 { return [CompleteFromList $mod {\{}] }
        2 { return [TryFromList $mod {then}] }
        default {
            set prev [PreviousWord ${start} ${line}]
            switch $prev {
                then -
                else -
                elseif { return [CompleteFromList $mod {\{}] }
                default { return [TryFromList $text {then else elseif}] }
            }
        }
    }
    return ""
}

proc complete(incr) {text start end line pos mod} {
    if {1 == $pos} {
        set matches [uplevel 2 info vars "${mod}*"]
        set final ""
        # check for integers
        #
        foreach match $matches {
            if {[uplevel 2 array exists $match]} {
                continue
            }
            if {[regexp {^[0-9]+$} [uplevel 2 set $match]]} {
                lappend final $match
            }
        }
        return [Format ${final} $text]
    }
}

proc complete(clock) {text start end line pos mod} {
    set cmd [lindex $line 1]
    switch -- $pos {
        1 {
            return [TryFromList $text {clicks format scan seconds}]
        }
        2 {
            switch -- $cmd {
                format  { return [DisplayHints clockValue] }
                scan    { return [DisplayHints dateString] }















                clicks  -
                seconds {}
            }
        }

        3 {

            switch -- $cmd {
                format {
                    set sub [lindex $line 3]
                    set subcmds {-fmt -gmt}
                    return [TryFromList $text $subcmds]


                }

                scan {
                    set sub [lindex $line 3]
                    set subcmds {-base -gmt}
                    return [TryFromList $text $subcmds]




                }
                clicks  -
                seconds {}
            }
        }
    }
    return ""
}



















proc complete(encoding) {text start end line pos mod} {
    set cmd [lindex $line 1]
    switch -- $pos {
        1 {
            return [TryFromList $text {convertfrom convertto names system}]
        }
        2 {
            switch -- $cmd {
                names {}
                convertfrom -
                convertto -
                system {
                    return [TryFromList ${text} [encoding names]]
                }
            }
        }




    }


    return ""
}





































































proc complete(expr) {text start end line pos mod} {




    set cmds {

        acos    cos     hypot   sinh 
        asin    cosh    log     sqrt 
        atan    exp     log10   tan 
        atan2   floor   pow     tanh 
        ceil    fmod    sin     abs 
        double  int     rand    round 
        srand 
    }




































    return [TryFromList $text $cmds]




}
















proc complete(fconfigure) {text start end line pos mod} {
    set cmd [lindex $line 1]
    switch -- $pos {
        1 {
            return [ChannelId ${mod}]
        }
        default {
            set option [PreviousWord ${start} ${line}]
            switch -- $option {
                -blocking {
                    return [TryFromList ${text} {yes no}]
                }
                -buffering {
                    return [TryFromList ${text} {full line none}]
                }
                -buffersize {
                    if {![llength ${text}]} {
                        return [DisplayHints newSize]
                    }
                }
                -encoding {
                    return [TryFromList ${text} [encoding names]]
                }
                -eofchar {
                    return [DisplayHints {\{inChar\ outChar\}}]
                }
                -translation {
                    return [TryFromList ${text} {auto binary cr crlf lf}]
                }
                default {return [TryFromList $text {

                    -blocking -buffering -buffersize
                    -encoding -eofchar -translation}]
                }
            }
        }
    }
    return ""
}

proc complete(fcopy) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [InChannelId ${mod}]
        }
        2 {
            return [OutChannelId ${mod}]
        }
        default {
            set option [PreviousWord ${start} ${line}]
            switch -- $option {
                -size    { return [DisplayHints size] }
                -command { return [DisplayHints callback] }
                default  { return [TryFromList $text {-size -command}] }


            }
        }
    }
    return ""
}

proc complete(file) {text start end line pos mod} {
    switch -- $pos {
        1 {
            set cmds {
                atime attributes copy delete dirname executable exists
                extension isdirectory isfile join lstat mtime mkdir
                nativename owned pathtype readable readlink rename
                rootname size split stat tail type volumes writable
            }
            return [TryFromList $text $cmds]
        }
        2 {
            set cmd [lindex $line 1]
            switch -- $cmd {
                atime -
                attributes -
                dirname -
                executable -
                exists -
                extension -
................................................................................
                writable {
                    return ""
                }

                copy -
                delete -
                rename {
                #    set match [MenuFromList ${mod} {-force}]


                    return ""
                }
            }
        }
    }
    return ""
}

proc complete(fileevent) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [ChannelId ${mod}]
        }
        2 {
            return [MenuFromList ${mod} {readable writable}]



        }
    }
    return ""
}

proc complete(flush) {text start end line pos mod} {
    switch -- $pos {
        1 { return [ChannelId ${mod}] }



































    }
    return ""
}

proc complete(gets) {text start end line pos mod} {
    switch -- $pos {
        1 { return [InChannelId ${mod}] }

    }
    return ""
}

proc complete(glob) {text start end line pos mod} {
    switch -- $pos {
        1 {


            set matches [TryFromList ${mod} {-nocomplain --}]
            if {[llength [string trim ${mod}]] && [llength ${matches}]} {
                return ${matches}
            }
        }
    }
    return ""
}

proc complete(global) {text start end line pos mod} {
    return [VarCompletion ${text}]
}

proc complete(index) {text start end line pos mod} {
    if {1 == $pos} {






















        return [VarCompletion ${text}]
    } elseif {2 == $pos && ![llength ${mod}]} {




































        return <index>
    }



























    return ""
}





































































































proc complete(info) {text start end line pos mod} {
    if {1 == $pos} {



        set cmds {
            args body cmdcount commands complete default exists
            globals hostname level library loaded locals nameofexecutable
            patchlevel procs script sharedlibextension tclversion vars}
        return [TryFromList $text $cmds]
    } elseif {2 == $pos} {
        set cmd [lindex $line 1]


        switch -- $cmd {
            args -
            body -
            default -
            procs { return [complete(proc) ${text} 0 0 ${line} 1 ${mod}] }
            complete { ; # TODO
            }
            level { ; # TODO
            }
            loaded { ;# TODO
            }



            commands -
            exists -
            globals -
            locals -
            vars {
                if {"exists" == $cmd} {
                    set do vars
                } else {
                    set do $cmd
                }
                # puts stderr [list complete(info) level = [info level]]
                return \
                [Format [uplevel 2 info ${do} "${mod}*"] $text]

            }
        }
    }



















    return ""
}

proc complete(interp) {text start end line pos mod} {
    set cmd [lindex $line 1]
    if {1 == $pos} {


        set cmds {
            alias aliases create delete eval exists expose hide hidden
            issafe invokehidden marktrusted slaves share target transfer}

        return [TryFromList $text $cmds]
    } elseif {2 == $pos} {


        switch -- $cmd {

            create {
                return [TryFromList $text {-safe -- ?path?}]





            }


            eval -
            exists -
            expose -
            hide -
            hidden -
            invokehidden -
            marktrusted -
            target {if {![llength ${mod}]} { return <path> }}


            aliases -
            delete -
            issafe -
            slaves {if {![llength ${mod}]} { return ?path? }}


            alias -
            share -
            transfer {if {![llength ${mod}]} { return <srcPath> }}

        }
    } elseif {3 == $pos} {


        switch -- $cmd {

            alias {if {![llength ${mod}]} { return <srcCmd> }}


            create {
                return [TryFromList $text {-safe -- ?path?}]





            }

            eval {if {![llength ${mod}]} { return <arg> }}
            delete {if {![llength ${mod}]} { return ?path? }}

            expose {if {![llength ${mod}]} { return <hiddenName> }}
            hide {if {![llength ${mod}]} { return <exposedCmdName> }}






            invokehidden {
                return \
                [TryFromList $text {?-global? <hiddenCmdName}]
            }

            target {if {![llength ${mod}]} { return <alias> }}


            exists {}
            hidden {}
            marktrusted {}
            aliases {}
            issafe {}
            slaves {}

            share -
            tranfer {return [ChannelId ${mod}]}
        }
    } elseif {4 == $pos} {


        switch -- $cmd {

            alias {if {![llength ${mod}]} { return <targetPath> }}







            create {
                return [TryFromList $text {-safe -- path}]





            }

            expose {if {![llength ${mod}]} { return ?exposedCmdName? }}
            hide {if {![llength ${mod}]} { return ?hiddenCmdName? }}




            share -
            tranfer {if {![llength ${mod}]} { return ?destPath? }}

        }
    }















    return ""
}

proc complete(join) {text start end line pos mod} {
    if {1 == $pos} {
        return [VarCompletion ${text}]



    }
    return ""
}

proc complete(lappend) {text start end line pos mod} {
    if {1 == $pos} {

        return [ListCompletion ${text}]

    }
    return ""
}



























































































proc complete(linsert) {text start end line pos mod} {
    if {1 == $pos} {
        return [ListCompletion ${text}]





    }
    return ""
}





proc complete(llength) {text start end line pos mod} {
    if {1 == $pos} {
        return [ListCompletion ${text}]




    }
    return ""
}

proc complete(load) {text start end line pos mod} {
    if {1 == $pos} {

        return ""; # filename


    } elseif {2 == $pos && ![llength ${mod}]} {
        return "<packageName>"




    } elseif {3 == $pos && ![llength ${mod}]} {
        return "<interp>"



    }
    return ""
}

proc complete(lrange) {text start end line pos mod} {
    if {1 == $pos} {
        return [ListCompletion ${text}]
    } elseif {2 == $pos && ![llength ${mod}]} {
        return "<first>"
    } elseif {3 == $pos && ![llength ${mod}]} {
        return "<last>"
    }
    return ""
}

proc complete(lreplace) {text start end line pos mod} {
    if {1 == $pos} {
        return [ListCompletion ${text}]
    } elseif {2 == $pos && ![llength ${mod}]} {
        return "<first>"
    } elseif {3 == $pos && ![llength ${mod}]} {
        return "<last>"
    } elseif {![llength ${mod}]} {
        return "?element?"
    }
    return ""
}

proc complete(lsearch) {text start end line pos mod} {
    if {1 == $pos} {
        set options [MenuFromList ${mod} {
            -exact -glob -regexp <list>}]


        set matches [ListCompletion ${text}]
        return [string trim "${matches} ${options}"]

    } else {
        if {![llength ${mod}]} {
            set opt [lindex ${line} 1]
            if {[llength [MenuFromList ${opt} {
                -exact -glob -regexp }]]} {
                incr pos -1
            }
            if {1 == $pos} {
                return <list>
            } elseif {2 == $pos} {
                return <pattern>
            }
        }
    }
    return ""
}

proc complete(lsort) {text start end line pos mod} {
    set options [DisplayHints ${mod} {
        -ascii -dictionary -integer -real -command
        -increasing -decreasing -index
    }]
    set matches [ListCompletion ${text}]
    return [string trim "${matches} ${options}"]




}

















proc complete(history) {text start end line pos mod} {
    if {1 == $pos} {
        set cmds {add change clear event info keep nextid redo}
        return [TryFromList $text $cmds]
    } elseif {2 == ${pos}} {
        set cmd [lindex $line 1]
        switch -- $cmd {
            add { if {![llength ${mod}]} { return <newValue> } }
            change { if {![llength ${mod}]} { return <newValue> } }


            info -
            keep { if {![llength ${mod}]} { return ?count? } }


            event -
            redo { if {![llength ${mod}]} { return ?event? } }

            clear -




            nextid { return "" }
        }
    }







    return ""
}





















proc complete(namespace) {text start end line pos mod} {
    regsub {^([^:])} ${mod} {::\1} mod
    # TODO dosn't work ???
    set space_matches [namespace children :: [string trim ${mod}*]]
    # puts \nspace_matches=|${space_matches}|
    set cmd [lindex $line 1]
    if {1 == $pos} {


        set cmds {
            children code current delete eval export forget
            import inscope origin parent qualifiers tail which}
        return [TryFromList $text $cmds]
    } elseif {2 == $pos} {


        switch -- $cmd {
            children -
            delete -
            eval -
            inscope -
            forget -




            parent { return [TryFromList ${mod} $space_matches] }

            code { return "" }
            current {}
            export { return [MenuFromList ${mod} {-clear ?pattern?}] }
            import {



                return [MenuFromList ${mod} {-force $space_matches}]
            }
            origin { if {![llength ${mod}]} { return <command> } }

            qualifiers -
            tail { if {![llength ${mod}]} { return <string> } }

            which { return [MenuFromList ${mod} {
                -command -variable <name>}] }
        }
     #      forget { if {![llength ${mod}]} { return ?pattern? } }
    } elseif {3 == $pos && "inscope" == $cmd} {
            if {![llength ${mod}]} { return arg }
    } else {


        switch -- $cmd {
            children { if {![llength ${mod}]} { return ?pattern? } }




            delete { return [TryFromList $text $space_matches] }
            eval { if {![llength ${mod}]} { return ?arg? } }
            inscope { if {![llength ${mod}]} { return ?arg? } }
            parent {}





            code {}
            current {}

            export -
            forget -
            import { return [DisplayHints ?pattern?] }
            origin {}
            qualifiers {}
            tail {}



            which { return [MenuFromList $text {
                -command -variable <name>}] }
        }
    }

    return ""
}

proc complete(open) {text start end line pos mod} {
    if {![llength ${mod}]} {
        if {2 == $pos} {
            return ?access?
        } elseif {3 == $pos} {
            return ?permissions?






        }

    }
    return ""
}

proc complete(package) {text start end line pos mod} {
    set cmd [lindex $line 1]
    if {1 == $pos} {


        set cmds {
            forget ifneeded names present provide require
            unknown vcompare versions vsatisfies}
        return [TryFromList $text $cmds]
    } elseif {2 == $pos} {


        switch -- $cmd {
            forget -
            ifneeded -
            provide -
            versions { return [MenuFromList ${mod} [package names]] }
            present -
            require {
                return [MenuFromList ${mod} "-exact [package names]"] }
            names {}
            unknown { if {![llength ${mod}]} { return ?command? } }

            vcompare -
            vsatisfies { if {![llength ${mod}]} { return <version1> } }

        }
    } elseif {3 == $pos} {


        switch -- $cmd {
            forget {}
            ifneeded { if {![llength ${mod}]} { return <version> } }
            provide { if {![llength ${mod}]} { return ?version? } }


            versions {}
            present -
            require {
                set prev [PreviousWord ${start} ${line}]
                if {[llength [MenuFromList ${prev} -exact]]} {
                    return [MenuFromList ${mod} [package names]]
                } elseif {![llength ${mod}]} {

                    return ?version?
                }
            }
            names {}
            unknown {}
            vcompare -
            vsatisfies { if {![llength ${mod}]} { return <version2> } }

        }
    } 

    return ""
}







proc complete(pkg_mkIndex) {text start end line pos mod} {
    set cmds [RemoveUsedOptions ${line} {-direct -load -verbose -- <dir>} {--}]
    set res [string trim [MenuFromList $text $cmds]]
    if {[regexp -- [PreviousWord ${start} ${line}] -load] \
        && ![llength ${mod}]} {

            return <pkgPat>


    }
    if {![llength [join ${res}]]} {
        return ""
    } else {
        return ${res}
    }
    return ""
}

proc complete(proc) {text start end line pos mod} {
    # puts known_procs=|${known_procs}|
    if {1 == $pos} {
        set known_procs [ProcsOnlyCompletion ${mod}]
        set common [CompleteLongest ${known_procs}]
        if {"" == ${common}} {
            return [Format ${known_procs} ${text}]
        } else {
            return [string trim "${common} ${known_procs}"]
        }
    } elseif {2 == $pos} {
        set proc [lindex $line 1]
        if {[catch {set args [uplevel 2 info args ${proc}]}]} {
            return ""
        } else {
            return [list "\{${args}\} \{"]




        }
    }
    return ""
}

proc complete(puts) {text start end line pos mod} {
    if {1 == $pos} {
        return [TryFromList ${mod} "-nonewline [OutChannelId ${mod}]"]
    } elseif {2 <= $pos} {
        if {![llength ${mod}]} {
            set opt [lindex ${line} 1]
            if {[llength [MenuFromList ${opt} {-nonewline}]]} {
                incr pos -1
            }
            if {1 == $pos} {
                return [OutChannelId ${mod}]
            } elseif {2 == $pos} {
                return [DisplayHints <string>]
                return <string>

            }
        }
    }
    return ""
}

proc complete(read) {text start end line pos mod} {
    if {1 == $pos} {
        return [MenuFromList ${mod} {-nonewline [InChannelId ${mod}]}]
    } elseif {2 == $pos} {
        if {![llength ${mod}]} {
            set opt [lindex ${line} 1]
            if {[llength [MenuFromList ${opt} {-nonewline}]]} {
                return [InChannelId ${mod}]
            } elseif {![llength ${mod}]} {
                return [DisplayHints <numBytes>]



            }
        }
    }
    return ""
}

proc complete(regexp) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]

    if {[llength ${prev}] && ("-" == [string index ${prev} 0] || 1 == $pos)} {
        set cmds [RemoveUsedOptions ${line} {
            -nocase -indices -expanded -line 
            -linestop -lineanchor -about <expression> --} {--}]
        if {[llength ${cmds}]} {
            return [string trim [MenuFromList $text $cmds]]
        }
    } else {
        set virtual_pos [expr ${pos} - [FirstNonOption ${line}]]
        switch -- ${virtual_pos} {
            1 { if {![llength ${mod}]} { return <string> } }
            2 { if {![llength ${mod}]} { return ?matchVar? } }
            default { if {![llength ${mod}]} { return ?subMatchVar? } }
        }
    }
    return ""
}





proc complete(regsub) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]
    if {[llength ${prev}] && ("-" == [string index ${prev} 0] || 1 == $pos)} {
        set cmds [RemoveUsedOptions ${line} {-all -nocase -- <expression>} {--}]
        set res [string trim [MenuFromList ${mod} ${cmds}]]
        if {[llength ${res}]} {
            return ${res}

        }
    } else {
        set virtual_pos [expr ${pos} - [FirstNonOption ${line}]]
        switch -- ${virtual_pos} {
            1 { if {![llength ${mod}]} { return <expression> } }
            2 { if {![llength ${mod}]} { return <string> } }
            3 { if {![llength ${mod}]} { return <subSpec> } }
            4 { if {![llength ${mod}]} { return <varName> } }
        }
    }
    return ""
}

proc complete(rename) {text start end line pos mod} {
    if {1 == $pos} {
        # TODO set all [CommandCompletion ${mod}]
        return [Format $all ${mod}]
    } elseif {2 == $pos && ![llength ${mod}]} {


        return <newName>

    }
    return ""
}





proc complete(return) {text start end line pos mod} {
    # TODO this is not perfect yet
    set cmds {-code -errorinfo -errorcode <string>}
    set res [MenuFromList [PreviousWord ${start} ${line}] ${cmds}]
    if {1 == [llength ${res}]} {
        switch -- ${res} {
            -errorinfo { if {![llength ${mod}]} { return <info> } }

            -code -
            -errorcode {
                set codes {ok error return break continue}
                return [TryFromList ${mod} ${codes}]
            }
        }

    }
































    set cmds [RemoveUsedOptions ${line} ${cmds}]
    set res [string trim [MenuFromList ${mod} ${cmds}]]
    if {[llength ${res}]} {
        return ${res}
    }















































    return ""
}

proc complete(seek) {text start end line pos mod} {
    if {1 == $pos} {

        return [ChannelId ${mod}]
    } elseif {2 == $pos} {

        return [TryFromList ${mod} {start current end}]
    }
    return ""
}

proc complete(set) {text start end line pos mod} {
    # puts stderr "\ntext=|$text| $start $end\n"
    # puts stderr \nline=|$line|\n
    # puts stderr \npos=|$pos|\n
    # puts stderr \nmod=|$mod|\n
    if {1 == $pos} {
        return [VarCompletion ${text}]

    } elseif {2 == $pos && ($text == "" || $text == "\"" || $text == "\{")} {
        set line [QuoteQuotes $line]
        if {[catch "set value [list [uplevel [info level] set [lindex $line 1]]]" msg]} {


            return ""
        } else {
            return [Quote $value ${text}]


        }
    }
    return ""
}

proc complete(socket) {text start end line pos mod} {
    set cmd [lindex ${line} 1]
    set prev [PreviousWord ${start} ${line}]
    if {"-server" == ${cmd}} {
        # server sockets
        #
        if {2 == $pos && ![llength ${mod}]} { return <command> }
        switch -- ${prev} {

            -myaddr { if {![llength ${mod}]} { return <addr> } }





        }
        return [TryFromList ${mod} [concat {-error -sockname -peername}]]


    } else {
        # client sockets
        #
        switch -- ${prev} {
            -myaddr { if {![llength ${mod}]} { return <addr> } }
            -myport { if {![llength ${mod}]} { return <port> } }
        }

        # read the host table only once.
        #
        variable hosts
        if {![info exists hosts] && "-server" != ${cmd}} {
            set id [open /etc/hosts r]
            set hosts ""
            if {0 != ${id}} {
                while {-1 != [gets ${id} line]} {
                    regsub {#.*} ${line} {} line
                    if {[llength ${line}] >= 2} {
                        lappend hosts [lindex ${line} 1]
                    }
                }
                close $id
            } 
        }
        set cmds {-myaddr -myport -async -myaddr -error -sockname -peername}
        if {$pos <= 1} {
            lappend cmds -server
        }




        return [TryFromList ${mod} [concat ${cmds} ${hosts}]]

    }
    return ""
}

proc complete(source) {text start end line pos mod} {
    return ""; # force file name completion






}

proc complete(string) {text start end line pos mod} {
    set cmd [lindex ${line} 1]

    set cmds {
        compare first index last length match range tolower


        totitle toupper trim trimleft trimright wordend wordstart}
    if {1 == $pos} {
        return [TryFromList ${mod} ${cmds}]
    } elseif {2 == $pos} {




        switch -- $cmd {
            compare -





            first -
            last { if {![llength ${mod}]} { return <string1> } }


            match { if {![llength ${mod}]} { return <pattern> } }












            index -
            length -
            range -


            tolower -
            totitle -
            toupper -
            trim -
            trimleft -
            trimright -
            wordend -
            wordstart { if {![llength ${mod}]} { return <string> } }

        }
    } elseif {3 == $pos} {


        switch -- $cmd {
            compare -








            first -
            last { if {![llength ${mod}]} { return <string2> } }


            index { if {![llength ${mod}]} { return <charIndex> } }







            length {}





            match { if {![llength ${mod}]} { return <string> } }

            range { if {![llength ${mod}]} { return <first> } }













            tolower -
            totitle -
            toupper {}








































            trim -
            trimleft { if {![llength ${mod}]} { return ?chars? } }

            trimright -
            wordend -
            wordstart { if {![llength ${mod}]} { return <index> } }

        }
    }























    return ""
}

proc complete(subst) {text start end line pos mod} {

    set opts {-nobackslashes -nocommands -novariables}
    set opts [RemoveUsedOptions ${line} ${opts}]
    return [TryFromList ${mod} [concat ${opts} <string>]]
    return ""
}

proc complete(switch) {text start end line pos mod} {




    set opts {-exact -glob -regexp --}
    set opts [RemoveUsedOptions ${line} ${opts} {--}]
    return [TryFromList ${mod} [concat ${opts} <string>]]

















    return ""
}

proc complete(tell) {text start end line pos mod} {
    if {1 == $pos} {

        return [ChannelId ${mod}]
    }
    return ""
}









proc complete(trace) {text start end line pos mod} {
    set cmd [lindex ${line} 1]
    if {1 == $pos} {


        return [TryFromList ${mod} {variable vdelete vinfo}]
    } elseif {2 == $pos} {
        return [Format [uplevel 2 info vars "${mod}*"] ${mod}]
    } elseif {3 == $pos && "variable" == ${cmd}} {
        return [TryFromList ${mod} {r w u}]
    }



















    return ""
}















proc complete(update) {text start end line pos mod} {
    if {1 == $pos && ![llength ${mod}]} {

        return idletasks
    }
    return ""
}

proc complete(uplevel) {text start end line pos mod} {
    if {![llength ${mod}]} {
        if {1 == $pos} {
            return ?level?
        } elseif {2 == $pos} {
            return <command>

        } elseif {3 == $pos} {
            return ?arg?

        } elseif {4 == $pos} {
            return ?...?
        }
    }


    return ""
}

proc complete(upvar) {text start end line pos mod} {
    if {![llength ${mod}]} {
        if {1 == $pos} {
            return ?level?
        } elseif {2 == $pos} {






            return <otherVar>
        } elseif {3 == $pos} {
            return <myVar>






        } elseif {4 == $pos} {
            return ?...?

        }
    }











    return ""
}

proc complete(variable) {text start end line pos mod} {
    set modulo [expr $pos % 2]
    if {1 == ${modulo}} {

        return [VarCompletion ${mod}]
    } elseif {0 == ${modulo} && \

        ($text == "" || $text == "\"" || $text == "\{")} {
        set line [QuoteQuotes $line]
        incr pos -1
        if {[catch \
            "set value [list [uplevel [info level] set [lindex $line ${pos}]]]"\

            msg]} {
            return ""
        } else {
            return [Quote $value ${mod}]


        }
    }
    return ""
}

proc complete(vwait) {text start end line pos mod} {
    if {1 == ${pos}} {
        return [VarCompletion ${mod}]
    }

}

proc complete(unset) {text start end line pos mod} {






    return [VarCompletion ${text}]
}

# -------------------------------------
#                  TK
# -------------------------------------

# generic widget configuration


|







 







<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
|
>
|
>
>
>


>
>
>
>
>
>
>






|




|





>
>
>
>
|
>







 







>









>
|
>
>
>





<
>
>
>
>
>
|

<
|
<
<
<
|
|
|
|
>
>
>
>

>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










|
|
|
<

>
>
>
>
>
|



|
>
|
|
>







 







>




|











|







 







>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
|


|
>
|
>
>
>
>
>
>
>
|


|








|


|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
>



|
|
<
<

>
|
<
<
<









<

<
<
<
<
<
<
<
<
<
<
<
<
<
>


>
|












<
<
<
<
<
<







 







>









|
|
>
>
>
|
<
<
<
>
>
>
>
>
>
>
|
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>

<
>
>
>
|
>
>
|
|
>


>
>
>

|







 







|



>
>
>
>
>
>
>
>

|
>
>
|
<
>
>

<

>

|
>
|
>
>
>
>
>
>




|

>
>
>
>
>
>
>




<
<
<
<
<
<
<
<

>

<
<
<
<
<
<
<
<
>
>
>
|
<
<
<
<
|
<
<
>
>
|
<
<
<
<
<
<
>
|
<
<
<
|
<
<
>
|
<
<
|
|
<



>
|





>
>

>
>
>







 







>

<
>
>
>
>
>
>
>
>
>
>
>

>












|
<
<
<
<
<
<
<
<













>







 







<






>


>



>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
|


>




|
|











>
|
|

|




>


<
<
<
<
<
|
<
<
<
<







 







<
<
<
<
|
<





|







 







|
>
>
>
>
>
>







|









|






|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

|


|



|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
|
>


<
<
|
>
>
|
>

<
<
<
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|


|



<



|



>
>
>
>
|
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>

>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>

|


|





|


|



|



|


|


|

|
>

|










|


|




|
|
|
>
>











|






|







 







|
>
>











|


|
>
>
>







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|
>







>
>
|
|











|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
>
>
>
|
|
|
|
|
<
<
>
>
|
|
|
|
|
<
<
<
<
<
<
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
<
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|
<
>
>
|
|
<
>
|
<
>
>
|
>
|
<
>
>
>
>
>
|
|
>
|
|
|
|
|
|
|
<
>

|
|
|
<
>

|
|
<
>
|
<
>
>
|

<
>

|
<
>
>
>
>
>
|
|
<
<

<
<
>
>

>
>
>
|
|
|
|

<
>

|
|
|
|
|
|

|
|
|
<
>
>
|

<
>
>

>
>
>
>
|
<
>
>
>
>
>
|
|
<
<

>
>
>
|
<
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




<
<
>
>
>





<
>
|
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
<
>
>
>
>
>




>
>
>
>

<
<
>
>
>
>





|
>
|
>
>
|
<
>
>
>
>
|
<
>
>
>





|
|
|
|
<
<





|
|
|
|
|
<
<
<





<
<
|
>
>
|
<
>
|
|
|
|
|


|
|
|
<







|

|

|
|
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
|
<
<
>
|
<
<
>
|
<
<

<
>
>
>
>
|
|
|
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<



|
<
>
>
|
|
|
|
<
>
>
|
|
|
|
|
|
>
>
>
>
|
>
|
|
|
|
>
>
>
|
|
<
>
|
<
>
|
|
|
<
<
<
<
>
>
|
<
>
>
>
>
|
<
<
<
>
>
>
>
>
|
<
>
|
|
|
<
<
<
>
>
>
|
<
|
|
>




<
<
<
<
<
>
>
>
>
>
>

>





|
<
>
>
|
|
|
|
<
>
>
|
|
|
|
|
|
|
|
|
<
>
|
<
>
|
<
>
>
|
|
<
<
>
>
|
|
|
|
<
|
<
>
|
|
|
|
|
|
<
>
|
|
>



>
>
>
>
>
>


|
|
<
>
|
>
>

<
<
<
|
<
<



|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>






|
|
|
|
|
|
|
|
|
|
|
|
|
>





|
|
|
|
|
|
|
|
|
|
|
>
>
>








>
|




|




|
|
|




>
>
>
>



|
|
|
|
|
>




|
|
|
|






|
|
|
<
>
>
|
>




>
>
>
>


|
|
<
|
<
>
|
|
|
|
|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




<
>
|
<
>
|





|
<
<
<
<
|
>
|
|
|
>
>
|
|
|
>
>






|




<
|
>
|
>
>
>
>
>
|
<
>
>




|
|


<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<




>
>
>
>
|
>




|
|
>
>
>
>
>
>



|
>

<
>
>
|
<
<
|
>
>
>
>
|
|
>
>
>
>
>
|
<
>

<
>
>

>
>
>
>
>
>
>
>
>
|
|
|
>
>
|
|
|
|
|
|
|
<
>
|
<
>
>
|
|
>
>
>
>
>
>
>
>
|
<
>

<
>
>
>
>
>
>
>
|
>
>
>
>
|
<
|
<

>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
|
|
<
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
|
<
<
<



>
>
>
>
|
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




<
>
|




>
>
>
>
>
>
>
>

|
<
>
>
|
<
<
<
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
>
|





|
|
|
|
<
>
|
|
>
|
|
|
|
>
>




<
<
<
|
>
>
>
>
>
>
|
|
<
>
>
>
>
>
>
|
<
>
|
|
>
>
>
>
>
>
>
>
>
>
>





<
>
|
<
>
|
|
<
<
|
>
|
|
|
|
>
>






|
|

>


|
>
>
>
>
>
>
|







1
2
3
4
5
6
7
8
9
10
..
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
...
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
...
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
...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
...
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
...
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
...
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
...
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
...
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
...
929
930
931
932
933
934
935




936

937
938
939
940
941
942
943
944
945
946
947
948
949
...
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
....
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
....
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
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689

1690
1691
1692
1693
1694
1695
1696
1697


1698
1699
1700
1701
1702
1703
1704






1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719

1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747

1748
1749
1750
1751

1752
1753

1754
1755
1756
1757
1758

1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773

1774
1775
1776
1777
1778

1779
1780
1781
1782

1783
1784

1785
1786
1787
1788

1789
1790
1791

1792
1793
1794
1795
1796
1797
1798


1799


1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810

1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822

1823
1824
1825
1826

1827
1828
1829
1830
1831
1832
1833
1834

1835
1836
1837
1838
1839
1840
1841


1842
1843
1844
1845
1846

1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868


1869
1870
1871
1872
1873
1874
1875
1876

1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974


1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988


1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003

2004
2005
2006
2007
2008

2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020


2021
2022
2023
2024
2025
2026
2027
2028
2029
2030



2031
2032
2033
2034
2035


2036
2037
2038
2039

2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050

2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086





2087


2088
2089


2090
2091


2092

2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130

2131
2132
2133
2134

2135
2136
2137
2138
2139
2140

2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163

2164
2165

2166
2167
2168
2169




2170
2171
2172

2173
2174
2175
2176
2177



2178
2179
2180
2181
2182
2183

2184
2185
2186
2187



2188
2189
2190
2191

2192
2193
2194
2195
2196
2197
2198





2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212

2213
2214
2215
2216
2217
2218

2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229

2230
2231

2232
2233

2234
2235
2236
2237


2238
2239
2240
2241
2242
2243

2244

2245
2246
2247
2248
2249
2250
2251

2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268

2269
2270
2271
2272
2273



2274


2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391

2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407

2408

2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450



2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502

2503
2504

2505
2506
2507
2508
2509
2510
2511
2512




2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535

2536
2537
2538
2539
2540
2541
2542
2543
2544

2545
2546
2547
2548
2549
2550
2551
2552
2553
2554





2555










2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583

2584
2585
2586


2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599

2600
2601

2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625

2626
2627

2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640

2641
2642

2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655

2656

2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671

2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712

2713
2714
2715

2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747



2748
2749
2750
2751
2752
2753
2754
2755


2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776

2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792

2793
2794
2795




2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833

2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844

2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858



2859
2860
2861
2862
2863
2864
2865
2866
2867

2868
2869
2870
2871
2872
2873
2874

2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893

2894
2895

2896
2897
2898


2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
#!/usr/locanl/bin/tclsh
# FILE: "/home/joze/src/tclreadline/tclreadlineCompleter.tcl"
# LAST MODIFICATION: "Mon Sep 13 02:21:21 1999 (joze)"
# (C) 1998, 1999 by Johannes Zellner, <johannes@zellner.org>
# $Id$
# ---
#
# tclreadline -- gnu readline for tcl
# Copyright (C) 1999  Johannes Zellner
#
................................................................................
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# johannes@zellner.org
# http://www.zellner.org/tclreadline/
#
# ================================================================== 





























# TODO:

























































#
#     - tcltest is missing
#
#     - last try: as for widgets
#



namespace eval tclreadline {

namespace export \
TryFromList CompleteFromList DisplayHints Rehash \
PreviousWord CommandCompletion RemoveUsedOptions \
HostList ChannelId InChannelId OutChannelId \
Lindex Llength CompleteBoolean

#**
# TryFromList will return an empty string, if
# the text typed so far does not match any of the
# elements in list. This might be used to allow
# subsequent filename completion by the builtin
# completer.
#
proc TryFromList {text lst {allow ""}} {

    # puts stderr "(CompleteFromList) \ntext=|$text|"
    # puts stderr "(CompleteFromList) lst=|$lst|"
    set pre [GetQuotedPrefix ${text}]
    set matches [MatchesFromList $text $lst $allow]

    # puts stderr "(CompleteFromList) matches=|$matches|"
    if {1 == [llength $matches]} { ; # unique match
        # puts stderr \nunique=$matches\n
        # puts stderr "\n|${pre}${matches}[Right ${pre}]|\n"
        set null [string index $matches 0]
        if {"<" == $null || "?" == $null} {
            return [string trim "[list $text] $lst"]
        } else {
            return [string trim ${pre}${matches}[Right ${pre}]]
        }
    } elseif {"" != ${matches}} {
        # puts stderr \nmore=$matches\n
        set longest [CompleteLongest ${matches}]
        # puts stderr longest=|$longest|
        if {"" == $longest} {
            return [string trim "[list $text] ${matches}"]
        } else {
................................................................................
            return [string trim "${pre}${longest} ${matches}"]
        }
    } else {
        return ""; # nothing to complete
    }
}

#**
# CompleteFromList will never return an empty string.
# completes, if a completion can be done, or ring
# the bell if not.
#
proc CompleteFromList {text lst} {
    set result [TryFromList ${text} ${lst}]
    if {![llength ${result}]} {
        Alert
        # return [string trim [list ${text}] ${lst}"]
        if {[llength ${lst}]} {
            return [string trim "${text} ${lst}"]
        } else {
            return [string trim [list ${text} {}]]
        }
    } else {
        return ${result}
    }
}


#**
# CompleteBoolean does a CompleteFromList
# with a list of all valid boolean values.
#
proc CompleteBoolean {text} {
    return [CompleteFromList $text {yes no true false 1 0}]
}





#**
# build a list of all executables which can be
# found in $env(PATH). This is (naturally) a bit
# slow, and should not called frequently. Instead
# it is a good idea to check if the variable
# `executables' exists and then just use it's
# content instead of calling Rehash.
# (see complete(exec)).
# 
proc Rehash {} {

    global env
    variable executables

    if {![info exists env] || ![array exists env]} {
        return
    }
    if {![info exists env(PATH)]} {
        return
    }

    set executables 0
    foreach dir [split $env(PATH) :] {
        if {[catch [list set files [glob -nocomplain ${dir}/*]]]} { continue }
        foreach file $files {
            if {[file executable $file]} {
                lappend executables [file tail $file]
            }
        }
    }
}

#**
# build a list hosts from the /etc/hosts file.
# this is only done once. This is sort of a
# dirty hack, /etc/hosts is hardcoded ...
# 
proc HostList {} {
    # read the host table only once.
    #
    variable hosts
    if {![info exists hosts]} {
        catch {
            set id [open /etc/hosts r]
            set hosts ""
            if {0 != ${id}} {
                while {-1 != [gets ${id} line]} {
                    regsub {#.*} ${line} {} line
                    if {[llength ${line}] >= 2} {
                        lappend hosts [lindex ${line} 1]
                    }
                }
                close $id
            } 
        }
    }
    return $hosts
}

#**
# never return an empty string, never complete.
# This is useful for showing options lists for example.
#
proc DisplayHints {lst} {
    return [string trim "{} ${lst}"]
}

#**
# find (partial) matches for `text' in `lst'. Ring
# the bell and return the whole list, if the user
# tries to complete ?..? options or <..> hints.

#
# MatchesFromList returns a list which is not suitable
# for passing to the readline completer. Thus,
# MatchesFromList should not be called directly but
# from formatting routines as TryFromList.
#
proc MatchesFromList {text lst {allow ""}} {
    set result ""
    set text [StripPrefix $text]
    set null [string index $text 0]
    foreach char {< ?} {
        if {$char == $null && -1 == [string first $char $allow]} {
            Alert
            return $lst
        }
    }
    # puts stderr "(MatchesFromList) text=$text"
    # puts stderr "(MatchesFromList) lst=$lst"
    foreach word $lst {
        if {[string match ${text}* ${word}]} {
            lappend result ${word}
        }
................................................................................
    return ${expr_pos}
}

proc RemoveUsedOptions {line opts {terminate {}}} {
    if {[llength ${terminate}]} {
        if {[regexp -- ${terminate} ${line}]} {
            return ""
            # return ${terminate}
        }
    }
    set new ""
    foreach word ${opts} {
        if {-1 == [string first ${word} ${line}]} {
            lappend new ${word}
        }
    }
    return [string trim ${new}]
}

proc Alert {} {
    puts -nonewline \a
    flush stdout
}

#**
# get the longest common completion
# e.g. str == {tcl_version tclreadline_version tclreadline_library}
# --> [CompleteLongest ${str}] == "tcl"
#
proc CompleteLongest {str} {
    # puts stderr str=$str
    set match0 [lindex ${str} 0]
................................................................................
    if {1 < [llength $value] && "" == $right} {
        return [list \"${value}\"]
    } else {
        return [list ${left}${value}${right}]
    }
}

# the following two channel proc's make use of
# the brandnew (Sep 99) `file channels' command
# but have some fallback behaviour for older
# tcl version.
#
proc InChannelId {text {switches ""}} {

    if [catch {set chs [file channels]}] {
        set chs {stdin}
    }
    set result ""
    foreach ch $chs {
        if {![catch {fileevent $ch readable}]} {
            lappend result $ch
        }
    }
    return [ChannelId ${text} <inChannel> $result $switches]
}

proc OutChannelId {text {switches ""}} {
    if [catch {set chs [file channels]}] {
        set chs {stdout stderr}
    }
    set result ""
    foreach ch $chs {
        if {![catch {fileevent $ch writable}]} {
            lappend result $ch
        }
    }
    return [ChannelId ${text} <outChannel> $result $switches]
}

proc ChannelId {text {descript <channelId>} {chs ""} {switches ""}} {
    if {"" == ${chs}} {
        # the `file channels' command is present
        # only in pretty new versions.
        #
        if [catch {set chs [file channels]}] {
            set chs {stdin stdout stderr}
        }
    }
    if {[llength [set channel [TryFromList ${text} "${chs} ${switches}"]]]} {
        return ${channel}
    } else {
        return [DisplayHints [string trim "${descript} ${switches}"]]
    }
}

proc QuoteQuotes {line} {
    regsub -all -- \" $line {\"} line
    regsub -all -- \{ $line {\{} line; # \}\} (keep the editor happy)
    return $line
................................................................................
#     return [llength $pre_text]
# 
}

proc Right {left} {
    # puts left=$left
    if {"\"" == $left} {
        return "\""
    } elseif {"\\\"" == $left} {
        return "\\\""
    } elseif {"\{" == $left} {
        return "\}"
    } elseif {"\\\{" == $left} {
        return "\\\}"
    }
................................................................................
    set pos 0
    while {-1 != [set pos [string first $char $line $pos]]} {
        incr pos
        incr found
    }
    return $found
}

#**
# make a proper tcl list from an icomplete
# string, that is: remove the junk. This is
# complementary to `IncompleteListRemainder'.
# e.g.:
#       for {set i 1} "
#  -->  for {set i 1}
#
proc ProperList {line} {
    set last [expr [string length $line] - 1]
    for {set i $last} {$i >= 0} {incr i -1} {
        if {![catch {llength [string range $line 0 $i]}]} {
            break
        }
    }
    return [string range $line 0 $i]
}

#**
# return the last part of a line which
# prevents the line from beeing a list.
# This is complementary to `ProperList'.
#
proc IncompleteListRemainder {line} {
    set last [expr [string length $line] - 1]
    for {set i $last} {$i >= 0} {incr i -1} {
        if {![catch {llength [string range $line 0 $i]}]} {
            break
        }
    }
    incr i
    return [string range $line $i end]
}

#**
# save `lindex'. works also for non-complete lines
# with opening parentheses or quotes.
# usage as `lindex'.
# Eventually returns the Rest of an incomplete line,
# if the index is `end' or == [Llength $line].
#
proc Lindex {line pos} {
    if {[catch [list set sub [lindex $line $pos]]]} {
        if {"end" == $pos || [Llength $line] == $pos} {
            return [IncompleteListRemainder $line]


        }
        set line [ProperList $line]
        # puts stderr \nproper_line=|$proper_line|



        if {[catch [list set sub [lindex $line $pos]]]} { return {} }
    }
    return $sub
}

#**
# save `llength' (see above).
#
proc Llength {line} {

    if {[catch [list set len [llength $line]]]} {













        set line [ProperList $line]
        if {[catch [list set len [llength $line]]]} { return {} }
    }
    # puts stderr \nline=$line
    return $len
}

proc StripPrefix {text} {
    # puts "(StripPrefix) text=|$text|"
    set null [string index $text 0]
    if {"\"" == $null || "\{" == $null} {
        return [string range $text 1 end]
    } else {
        return $text
    }
}







proc VarCompletion {text {level -1}} {
    if {-1 == ${level}} {
        set level [info level]
    } else {
        incr level
    }
    set pre [GetQuotedPrefix ${text}]
................................................................................
        }
        set namespaces ${new}
        unset new
    }
    set matches \
    [string trim "[uplevel ${level} info vars ${var}*] ${namespaces}"]
    if {1 == [llength $matches]} { ; # unique match

        # check if this unique match is an
        # array name, (whith no "(" yet).
        #
        if {[uplevel ${level} array exists $matches]} {
            return [VarCompletion ${matches}( ${level}]; # recursion
        } else {
            return ${pre}${matches}[Right ${pre}]
        }
    } elseif {"" != $matches} { ; # more than one match
          return [CompleteFromList ${text} ${matches}]
    } else {
        return ""; # nothing to complete
    }
}




proc CompleteControlStatement {text start end line pos mod pre new_line} {
    set pre [GetQuotedPrefix ${pre}]
    set cmd [Lindex $new_line 0]
    set diff [expr \
    [string length $line] - [string length $new_line]]
    if {$diff == [expr $start + 1]} {
        set mod1 $mod
    } else {


        set mod1 $text
        set pre ""
    }
    set new_end [expr $end - $diff]
    set new_start [expr $new_end - [string length $mod1]]
    # puts ""
    # puts new_start=$new_start
    # puts new_end=$new_end
    # puts new_line=$new_line
    # puts mod1=$mod1
    if {$new_start < 0} {
        return ""; # when does this occur?
    }
    # puts stderr ""
    # puts stderr start=|$start|
    # puts stderr end=|$end|
    # puts stderr mod=|$mod|
    # puts stderr new_start=|$new_start|
    # puts stderr new_end=|$new_end|
    # puts stderr new_line=|$new_line|
    # puts stderr ""
    set res [ScriptCompleter $mod1 $new_start $new_end $new_line]
    # puts stderr \n\${pre}\${res}=|${pre}${res}|
    if {[string length [Lindex ${res} 0]]} {
        return ${pre}${res}
    } else {
        return ${res}
    }
    return ""
}


proc BraceOrControlStatement {text start end line pos mod} {
    if {![string length [Lindex $line $pos]]} {
        return [list \{ {}]; # \}
    } else {

        set new_line [string trim [IncompleteListRemainder $line]]
        if {![regexp {^([\{\"])(.*)$} $new_line all pre new_line]} {
            set pre ""
        }
        return [CompleteControlStatement $text \
        $start $end $line $pos $mod $pre $new_line]
    }
}

proc FullQualifiedMatches {qualifier matchlist} {
    set new ""
    if {"" != $qualifier && ![regexp ::$ $qualifier]} {
        append qualifier ::
    }
    foreach entry ${matchlist} {
        set full ${qualifier}${entry}
        if {"" != [namespace which ${full}]} {
            lappend new ${full}
        }
    }
    return ${new}
}

................................................................................
    return [CommandCompletion ${cmd} procs]
}

proc CommandsOnlyCompletion {cmd} {
    return [CommandCompletion ${cmd} commands]
}

proc CommandCompletion {cmd {action both} {spc ::} {pre UNDEFINED}} {
    # puts stderr "(CommandCompletion) cmd=|$cmd|"
    # puts stderr "(CommandCompletion) action=|$action|"
    # puts stderr "(CommandCompletion) spc=|$spc|"

    # get the leading colons in `cmd'.
    if {"UNDEFINED" == $pre} {
        regexp {^:*} ${cmd} pre
    }
    # puts stderr \npre=|$pre|

    set cmd [StripPrefix ${cmd}]
    set quali [namespace qualifiers ${cmd}]
    if {[string length ${quali}]} {
        # puts stderr \nquali=|$quali|
        set matches [CommandCompletion \
        [namespace tail ${cmd}] ${action} ${spc}${quali} ${pre}]

        # puts stderr \nmatches1=|$matches|
        return $matches
    }

    set cmd [string trim ${cmd}]*
    # puts stderr \ncmd=|$cmd|\n
    if {"procs" != ${action}} {
        set all_commands [namespace eval $spc [list info commands ${cmd}]]
        # puts stderr all_commands=|$all_commands|
        set commands ""
        foreach command $all_commands {
            if {[namespace eval $spc [list namespace origin $command]] == \
                [namespace eval $spc [list namespace which $command]]} {
                lappend commands $command
            }
        }
    } else {
        set commands ""
    }
    if {"commands" != ${action}} {
        set all_procs [namespace eval $spc [list info procs ${cmd}]]
        # puts stderr procs=|$procs|
        set procs ""
        foreach proc $all_procs {
            if {[namespace eval $spc [list namespace origin $command]] == \
                [namespace eval $spc [list namespace which $command]]} {
                lappend procs $command
            }
        }
    } else {
        set procs ""
    }
    set matches [namespace eval $spc concat ${commands} ${procs}]








    set namespaces [namespace children $spc ${cmd}]

    if {![llength ${matches}] && 1 == [llength ${namespaces}]} {








        set matches [CommandCompletion {} ${action} ${namespaces} ${pre}]
        # puts stderr \nmatches=|$matches|
        return $matches
    }







    # make `namespaces' having exactly
    # the same number of colons as `cmd'.
    #






    regsub -all {^:*} $spc $pre spc




    set matches [FullQualifiedMatches ${spc} ${matches}]


    # puts stderr \nmatches3=|$matches|
    return [string trim "${matches} ${namespaces}"]


}


#**
# check, if the first argument starts with a '['
# and must be evaluated before continuing.
# NOTE: trims the `line'.
#       eventually modifies all arguments.
# DATE: Sep-06-1999
#
proc EventuallyEvaluateFirst {partT startT endT lineT} {
    # return; # disabled
    upvar $partT part $startT start $endT end $lineT line

    set oldlen [string length ${line}]
    set line [string trim ${line}]
    set diff [expr [string length $line] - $oldlen]
    incr start $diff
    incr end $diff

    set char [string index ${line} 0]
    if {{[} != ${char} && {$} != ${char}} {return}

    set pos 0
    while {-1 != [set idx [string first {]} ${line} ${pos}]]} {
        set cmd [string range ${line} 0 ${idx}]
................................................................................
# % puts $b<TAB>
# part  == $b
# start == 5
# end   == 7
# line  == "$puts $b"
#
proc ScriptCompleter {part start end line} {

    # puts stderr "(ScriptCompleter) |$part| $start $end |$line|"


    # if the character before the cursor is a terminating
    # quote and the user wants completion, we insert a white
    # space here.
    #
    set char [string index $line [expr $end - 1]]
    if {"\}" == $char} {
        append $part " "
        return [list $part]
    }

    if {{$} == [string index $part 0]} {

        # check for a !$ history event
        #
        if {$start > 0} {
            if {{!} == [string index $line [expr $start - 1]]} {
                return ""
            }
        }
        # variable completion. Check first, if the
        # variable starts with a plain `$' or should
        # be enclosed in braces.
        #
        set var [string range $part 1 end]









        # check if $var is an array name, which
        # already has already a "(" somewhere inside.
        #
        if {"" != [set vc [VarCompletion $var]]} {
            if {"" == [lindex $vc 0]} {
                return "\$ [lrange ${vc} 1 end]"
            } else {
                return \$${vc}
            }
            # puts stderr vc=|$vc|
        } else {
            return ""
        }

    # SCENARIO:
    #
    # % puts bla; put<TAB> $b
    # part  == put
    # start == 10
    # end   == 13
    # line  == "puts bla; put $b"
................................................................................
        set new_start [lindex $sub 0]
        set new_end [expr $end - ($start - $new_start)]
        set new_line [lindex $sub 1]
        # puts stderr "(SplitLine) $new_start $new_end $new_line"
        return [ScriptCompleter $part $new_start $new_end $new_line]
    } elseif {0 == [set pos [PartPosition part start end line]]} {
        # puts stderr "(PartPosition) $part $start $end $line"

        set all [CommandCompletion ${part}]
        # puts stderr "(ScriptCompleter) all=$all"
        #puts \nmatches=$matches\n
        # return [Format $all $part]
        return [TryFromList $part $all]
    } else {

        # try to use $pos further ...
        # puts stderr |$line|
        #
        if {"." == [string index [string trim ${line}] 0]} {
            set alias WIDGET
        } else {

            # the double `lindex' strips {} or quotes.
            # the subst enables variables containing
            # command names.
            #
            set alias [uplevel [info level] \
            subst [lindex [lindex [QuoteQuotes ${line}] 0] 0]]

            # make `alias' a fully qualified name.
            # this can raise an error, if alias is
            # no valid command.
            #
            if {[catch [list set alias [namespace origin $alias]]]} {
                return ""
            }

            # strip leading ::'s.
            #
            regsub -all {^::} $alias {} alias
            set namespc [namespace qualifiers $alias]
            set alias [namespace tail $alias]
        }

        foreach cmd [list ${alias} tclreadline_complete_unknown] {
            # puts stderr ${namespc}complete(${cmd})
            if {"" != [namespace eval ::tclreadline::${namespc} \
                [list info procs complete(${cmd})]]
            } {
                # puts found=|complete($cmd)|
                # to be more error-proof, we check here,
                # if complete($cmd) takes exactly 5 arguments.
                #
                if {6 != [set arguments [llength \
                    [namespace eval ::tclreadline::${namespc} \
                    [list info args complete($cmd)]]]]
                } {
                    error [list complete(${cmd}) takes ${arguments} \
                    arguments, but should take exactly 6.]
                }

                # remove leading quotes
                #
                set mod [StripPrefix $part]
                # puts stderr mod=$mod

                if {[catch [list set script_result \
                    [namespace eval ::tclreadline::${namespc} \
                    [list complete(${cmd}) $part $start $end $line $pos $mod]]]\
                    ::tclreadline::errorMsg]
                } {
                    error [list error during evaluation of `complete(${cmd})']
                }
                # puts stderr \nscript_result=|${script_result}|
                return ${script_result}
            }
            # set namespc ""; # no qualifiers for tclreadline_complete_unknown
        }
        # no specific command completer found.





        return ""




    }
    error "{NOTREACHED (this is probably an error)}"
}


# explicit command completers
#
................................................................................
    switch -- $pos {
        1 {
            return [CompleteFromList ${text} {<ms> cancel idle info}]
        }
        2 {
            switch -- $sub {
                cancel {




                    return [CompleteFromList $text "<script> [after info]"]

                }
                idle {
                    return [DisplayHints <script>]
                }
                info {
                    return [CompleteFromList $text [after info]]
                }
                default { return [DisplayHints ?script?] }
            }
        }
        default {
            switch -- $sub {
                info { return [DisplayHints {}] }
................................................................................
                anymore donesearch exists get names
                nextelement set size startsearch
            }
            return [CompleteFromList $text $cmds]
        }
        2 {
            set matches ""
            # set vars [uplevel [info level] info vars ${mod}*]
            #
            # better: this displays a list of array names if the
            # user inters with something which cannot be matched.
            # The matching against `text' is done by CompleteFromList.
            #
            set vars [uplevel [info level] info vars]
            foreach var ${vars} {
                if {[uplevel [info level] array exists ${var}]} {
                    lappend matches ${var}
                }
            }
            return [CompleteFromList ${text} ${matches}]
        }
        3 {
            set cmd [Lindex $line 1]
            set array_name [Lindex $line 2]
            switch -- $cmd {
                get -
                names {
                    set pattern [Lindex $line 3]
                    set matches [uplevel [info level] \
                    array names ${array_name} ${pattern}*]
                    if {![llength $matches]} {
                        return [DisplayHints ?pattern?]
                    } else {
                        return [CompleteFromList ${text} ${matches}]
                    }
                }
                anymore -
                donesearch -
                nextelement { return [DisplayHints <searchId>] }
            }
        }
    }
    return ""
}

# proc complete(bgerror) {text start end line pos mod} {
................................................................................
    }
    return ""
}

# proc complete(cd) {text start end line pos mod} {
# }





































proc complete(clock) {text start end line pos mod} {
    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            return [CompleteFromList $text {clicks format scan seconds}]
        }
        2 {
            switch -- $cmd {
                format  { return [DisplayHints <clockValue>] }
                scan    { return [DisplayHints <dateString>] }
                clicks  -
                seconds {}
            }
        }
        3 -
        5 {
            switch -- $cmd {
                format {
                    set subcmds [RemoveUsedOptions $line {-format -gmt}]
                    return [TryFromList $text $subcmds]
                }
                scan {
                    set subcmds [RemoveUsedOptions $line {-base -gmt}]
                    return [TryFromList $text $subcmds]
                }
                clicks  -
                seconds {}
            }
        }
        4 -
        6 {
            set sub [Lindex $line [expr $pos - 1]]
            switch -- $cmd {
                format {


                    switch -- $sub {
                        -format { return [DisplayHints <string>] }
                        -gmt    { return [DisplayHints <boolean>] }
                    }
                }
                scan {



                    switch -- $sub {
                        -base { return [DisplayHints <clockVal>] }
                        -gmt  { return [DisplayHints <boolean>] }
                    }
                }
                clicks  -
                seconds {}
            }
        }
    }
    return ""
}

proc complete(close) {text start end line pos mod} {
    switch -- $pos {
        1 { return [ChannelId $text] }
    }
    return ""
}

proc complete(concat) {text start end line pos mod} {
    return [DisplayHints ?arg?]
}

# proc complete(continue) {text start end line pos mod} {
# }

# proc complete(dde) {text start end line pos mod} {
#     We're not on windoze here ...
# }

proc complete(encoding) {text start end line pos mod} {
    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            return [CompleteFromList $text {convertfrom convertto names system}]
        }
        2 {
            switch -- $cmd {

                convertfrom -
                convertto -
                system {
                    return [CompleteFromList ${text} [encoding names]]
                }
            }
        }
        3 {
            switch -- $cmd {
                convertfrom { return [DisplayHints <data>] }
                convertto { return [DisplayHints <string>] }
            }
        }
    }
    return ""
}

proc complete(eof) {text start end line pos mod} {
    switch -- $pos {
        1 { return [InChannelId $text] }
    }
    return ""
}

proc complete(error) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <message>] }
        2 { return [DisplayHints ?info?] }
        3 { return [DisplayHints ?code?] }
    }
    return ""
}

proc complete(eval) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <arg>] }
        default { return [DisplayHints ?arg?] }
    }
    return ""
}

proc complete(exec) {text start end line pos mod} {
    set redir [list | |& < <@ << > 2> >& >> 2>> >>& >@ 2>@ >&@]
    variable executables
    if {![info exists executables]} {
        Rehash
    }
    switch -- $pos {
        1 {
            return [TryFromList $text "-keepnewline -- $executables"]
        }
        default {
            set prev [PreviousWord ${start} ${line}]
            if {"-keepnewline" == $prev && 2 == $pos} {
                return [TryFromList $text "-- $executables"]
            }
            switch -exact -- $prev {
                | -
                |& { return [TryFromList $text $executables] }
                < -
                > -
                2> -
                >& -
                >> -
                2>> -
                >>& { return "" }
                <@ -
                >@ -
                2>@ -
                >&@ { return [ChannelId $text] }
                << { return [DisplayHints <value>] }
                default { return [TryFromList $text $redir "<>"] }
            }
        }
    }
    return ""
}

proc complete(exit) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints ?returnCode?] }
    }
    return ""
}

proc complete(expr) {text start end line pos mod} {
    set left $text
    set right ""
    set substitution [regexp -- {(.*)(\(.*)} $text all left right]; #-)

    set cmds {
        - + ~ !  * / % + - << >> < > <= >= == != & ^ | && || <x?y:z>
        acos    cos     hypot   sinh 
        asin    cosh    log     sqrt 
        atan    exp     log10   tan 
        atan2   floor   pow     tanh 
        ceil    fmod    sin     abs 
        double  int     rand    round 
        srand 
    }

    if {")" == [string index $text end] && -1 != [lsearch $cmds $left]} {
        return "$text "; # append a space after a closing ')'
    }

    switch -- $left {
        rand { return "rand() " }

        abs  -
        acos -
        asin -
        atan -
        ceil  -
        cos -
        cosh -
        double -
        exp -
        floor -
        int -
        log -
        log10 -
        round  -
        sin  -
        sinh  -
        sqrt  -
        srand  -
        tan  -
        tanh { return [DisplayHints <value>] }


        atan2 -
        fmod -
        hypot -
        pow { return [DisplayHints <value>,<value>] }
    }

    set completions [TryFromList $left $cmds <>]
    if {1 == [llength $completions]} {
        if {!$substitution} {
            if {"rand" == $completions} {
                return "rand() "; # rand() takes no arguments
            }
            append completions (; #-)
            return [list $completions {}]
        }
    } else {
        return $completions
    }
    return ""
}

proc complete(fblocked) {text start end line pos mod} {
    switch -- $pos {
        1 { return [InChannelId $text] }
    }
    return ""
}

proc complete(fconfigure) {text start end line pos mod} {
    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            return [ChannelId ${text}]
        }
        default {
            set option [PreviousWord ${start} ${line}]
            switch -- $option {
                -blocking {
                    return [CompleteBoolean ${text}]
                }
                -buffering {
                    return [CompleteFromList ${text} {full line none}]
                }
                -buffersize {
                    if {![llength ${text}]} {
                        return [DisplayHints <newSize>]
                    }
                }
                -encoding {
                    return [CompleteFromList ${text} [encoding names]]
                }
                -eofchar {
                    return [DisplayHints {\{<inChar>\ <outChar>\}}]
                }
                -translation {
                    return [CompleteFromList ${text} {auto binary cr crlf lf}]
                }
                default {return [CompleteFromList $text \
                    [RemoveUsedOptions $line {
                    -blocking -buffering -buffersize
                    -encoding -eofchar -translation}]]
                }
            }
        }
    }
    return ""
}

proc complete(fcopy) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [InChannelId ${text}]
        }
        2 {
            return [OutChannelId ${text}]
        }
        default {
            set option [PreviousWord ${start} ${line}]
            switch -- $option {
                -size    { return [DisplayHints <size>] }
                -command { return [DisplayHints <callback>] }
                default  { return [CompleteFromList $text \
                    [RemoveUsedOptions $line {-size -command}]]
                }
            }
        }
    }
    return ""
}

proc complete(file) {text start end line pos mod} {
    switch -- $pos {
        1 {
            set cmds {
                atime attributes copy delete dirname executable exists
                extension isdirectory isfile join lstat mkdir mtime
                nativename owned pathtype readable readlink rename
                rootname size split stat tail type volumes writable
            }
            return [TryFromList $text $cmds]
        }
        2 {
            set cmd [Lindex $line 1]
            switch -- $cmd {
                atime -
                attributes -
                dirname -
                executable -
                exists -
                extension -
................................................................................
                writable {
                    return ""
                }

                copy -
                delete -
                rename {
                    # return [TryFromList $text "-force [glob *]"]
                    # this is not perfect. The  `-force' and `--'
                    # options will not be displayed.
                    return ""
                }
            }
        }
    }
    return ""
}

proc complete(fileevent) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [ChannelId ${text}]
        }
        2 {
            return [CompleteFromList ${text} {readable writable}]
        }
        3 {
            return [DisplayHints ?script?]
        }
    }
    return ""
}

proc complete(flush) {text start end line pos mod} {
    switch -- $pos {
        1 { return [OutChannelId ${text}] }
    }
    return ""
}

proc complete(for) {text start end line pos mod} {
    switch -- $pos {
        1 -
        2 -
        3 -
        4 {
            return [BraceOrControlStatement $text $start $end $line $pos $mod]
        }
    }
    return ""
}

proc complete(foreach) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <varname>] }
        2 { return [DisplayHints <list>] }
        default {
            if {[expr $pos % 2]} {
                return [DisplayHints [list ?varname? <body>]]
            } else {
                return [DisplayHints ?list?]
            }
        }
    }
    return ""
}

proc complete(format) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <formatString>] }
        default { return [DisplayHints ?arg?] }
    }
    return ""
}

proc complete(gets) {text start end line pos mod} {
    switch -- $pos {
        1 { return [InChannelId ${text}] }
        2 { return [VarCompletion ${text}]}
    }
    return ""
}

proc complete(glob) {text start end line pos mod} {
    switch -- $pos {
        1 {
            # This also is not perfect.
            # This will not display the options as hints!
            set matches [TryFromList ${text} {-nocomplain --}]
            if {[llength [string trim ${text}]] && [llength ${matches}]} {
                return ${matches}
            }
        }
    }
    return ""
}

proc complete(global) {text start end line pos mod} {
    return [VarCompletion ${text}]
}

proc complete(history) {text start end line pos mod} {

    switch -- $pos {
        1 {
            set cmds {add change clear event info keep nextid redo}
            return [TryFromList $text $cmds]
        }
        2 {
            set cmd [Lindex $line 1]
            switch -- $cmd {
                add { return [DisplayHints <command>] }
                change { return [DisplayHints <newValue>] }

                info -
                keep { return [DisplayHints ?count?] }

                event -
                redo { return [DisplayHints ?event?] }

                clear -
                nextid { return "" }
            }
        }
    }
    return ""

}

# --- HTTP PACKAGE ---

# create a http namespace inside
# tclreadline and import some commands.
#
namespace eval http {
    catch {
        namespace import \
        ::tclreadline::DisplayHints ::tclreadline::PreviousWord \
        ::tclreadline::CompleteFromList ::tclreadline::CommandCompletion \
        ::tclreadline::RemoveUsedOptions ::tclreadline::HostList \
        ::tclreadline::ChannelId ::tclreadline::Lindex \
        ::tclreadline::CompleteBoolean
    }
}

proc http::complete(config) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]
    switch -- $prev {
        -accept { return [DisplayHints <mimetypes>] }
        -proxyhost {
            return [CompleteFromList $text [HostList]]
        }
        -proxyport { return [DisplayHints <number>] }
        -proxyfilter {
            return [CompleteFromList $text [CommandCompletion $text]]
        }
        -useragent { return [DisplayHints <string>] }
        default {
            return [CompleteFromList $text [RemoveUsedOptions $line {
                -accept -proxyhost -proxyport -proxyfilter -useragent
            }]]
        }
    }
    return ""
}

proc http::complete(geturl) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <url>] }
        default {
            set prev [PreviousWord ${start} ${line}]
            switch -- $prev {
                -blocksize { return [DisplayHints <size>] }
                -channel { return [ChannelId ${text}] }
                -command -
                -handler -
                -progress {
                    return [CompleteFromList $text [CommandCompletion $text]]
                }
                -headers { return [DisplayHints <keyvaluelist>] }
                -query { return [DisplayHints <query>] }
                -timeout { return [DisplayHints <milliseconds>] }
                -validate { return [CompleteBoolean $text] }
                default {
                    return [CompleteFromList $text [RemoveUsedOptions $line {
                        -blocksize -channel -command -handler -headers
                        -progress -query -timeout -validate
                    }]]
                }
            }
        }
    }
    return ""
}

proc http::complete(formatQuery) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <key>] }
        2 { return [DisplayHints <value>] }
        default {
            switch [expr $pos % 2] {
                0 { return [DisplayHints ?value?] }
                1 { return [DisplayHints ?key?] }
            }
        }
    }
    return ""
}

proc http::complete(reset) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <token>] }
        2 { return [DisplayHints ?why?] }
    }
    return ""
}

# the unknown proc handles the rest
#
proc \
http::complete(tclreadline_complete_unknown) {text start end line pos mod} {
    set cmd [Lindex $line 0]
    regsub -all {^.*::} $cmd "" cmd
    switch -- $pos {
        1 {
            switch -- $cmd {
                reset -
                wait -
                data -
                status -
                code -
                size -
                cleanup {
                    return [DisplayHints <token>]
                }
            }
        }
    }
    return ""
}

# --- END OF HTTP PACKAGE ---

proc complete(if) {text start end line pos mod} {
    # we don't offer the completion `then':
    # it's optional, more difficult to parse
    # and who uses it anyway?
    #
    switch -- $pos {
        1 -
        2 {
            return [BraceOrControlStatement $text $start $end $line $pos $mod]
        }
        default {
            set prev [PreviousWord ${start} ${line}]
            switch -- $prev {
                then -
                else -
                elseif {
                    return [BraceOrControlStatement \
                    $text $start $end $line $pos $mod]
                }
                default {
                    if {-1 == [lsearch [ProperList $line] else]} {
                        return [CompleteFromList $text {else elseif}]
                    }
                }
            }
        }
    }
    return ""
}

proc complete(incr) {text start end line pos mod} {
    switch -- $pos {
        1 {
            set matches [uplevel [info level] info vars ${mod}*]
            set integers ""
            # check for integers
            #
            foreach match $matches {
                if {[uplevel [info level] array exists $match]} {
                    continue
                }
                if {[regexp {^[0-9]+$} [uplevel [info level] set $match]]} {
                    lappend integers $match
                }
            }
            return [CompleteFromList ${text} ${integers}]
        }
        2 { return [DisplayHints ?increment?] }
    }
    return ""
}

proc complete(info) {text start end line pos mod} {

    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            set cmds {
                args body cmdcount commands complete default exists
                globals hostname level library loaded locals nameofexecutable
                patchlevel procs script sharedlibextension tclversion vars}
            return [CompleteFromList $text $cmds]


        }
        2 {
            switch -- $cmd {
                args -
                body -
                default -
                procs { return [complete(proc) ${text} 0 0 ${line} 1 ${mod}] }






                complete { return [DisplayHints <command>] }
                level { return [DisplayHints ?number?] }
                loaded { return [DisplayHints ?interp?] }
                commands -
                exists -
                globals -
                locals -
                vars {
                    if {"exists" == $cmd} {
                        set do vars
                    } else {
                        set do $cmd
                    }
                    # puts stderr [list complete(info) level = [info level]]
                    return \

                    [CompleteFromList ${text} [uplevel [info level] info ${do}]]
                }
            }
        }
        3 {
            switch -- $cmd {
                default {
                    set proc [Lindex $line 2]
                    return [CompleteFromList ${text} \
                    [uplevel [info level] info args $proc]]
                }
                default {}
            }
        }
        4 {
            switch -- $cmd {
                default {
                    return [VarCompletion ${text}]
                }
                default {}
            }
        }
    }
    return ""
}

proc complete(interp) {text start end line pos mod} {
    set cmd [Lindex $line 1]

    switch -- $pos {
        1 {
            set cmds {
                alias aliases create delete eval exists expose hide hidden

                invokehidden issafe marktrusted share slaves target transfer}
            return [TryFromList $text $cmds]

        }
        2 {
            switch -- $cmd {

                create {

                    set cmds [RemoveUsedOptions ${line} {-save --} {--}]
                    if {[llength $cmds]} {
                        return [CompleteFromList $text "$cmds ?path?"]
                    } else {
                        return [DisplayHints ?path?]
                    }
                }

                eval -
                exists -
                expose -
                hide -
                hidden -
                invokehidden -
                marktrusted -

                target { return [DisplayHints [interp slaves]] }

                aliases -
                delete -
                issafe -

                slaves { return [DisplayHints [interp slaves]] }

                alias -
                share -

                transfer { return [DisplayHints <srcPath>] }
            }

        }
        3 {
            switch -- $cmd {


                alias { return [DisplayHints <srcCmd>] }

                create {

                    set cmds [RemoveUsedOptions ${line} {-save --} {--}]
                    if {[llength $cmds]} {
                        return [CompleteFromList $text "$cmds ?path?"]
                    } else {
                        return [DisplayHints ?path?]
                    }
                }





                eval { return [DisplayHints <arg>] }
                delete { return [DisplayHints [interp slaves]] }

                expose { return [DisplayHints <hiddenName>] }
                hide { return [DisplayHints <exposedCmdName>] }

                invokehidden {
                    return \
                    [CompleteFromList $text {?-global? <hiddenCmdName>}]
                }


                target { return [DisplayHints <alias>] }

                exists {}
                hidden {}
                marktrusted {}
                aliases {}
                issafe {}
                slaves {}

                share -
                transfer {return [ChannelId ${text}]}
            }

        }
        4 {
            switch -- $cmd {


                alias { return [DisplayHints <targetPath>] }
                eval { return [DisplayHints ?arg?] }

                invokehidden {
                    return [CompleteFromList $text {<hiddenCmdName> ?arg?}]
                }

                create {

                    set cmds [RemoveUsedOptions ${line} {-save --} {--}]
                    if {[llength $cmds]} {
                        return [CompleteFromList $text "$cmds ?path?"]
                    } else {
                        return [DisplayHints ?path?]
                    }
                }



                expose { return [DisplayHints ?exposedCmdName?] }
                hide { return [DisplayHints ?hiddenCmdName?] }

                share -

                transfer { return [DisplayHints [interp slaves]] }
            }
        }
        5 {
            switch -- $cmd {

                alias { return [DisplayHints <targetCmd>] }
                invokehidden -
                eval { return [DisplayHints ?arg?] }

                expose { return [DisplayHints ?exposedCmdName?] }
                hide { return [DisplayHints ?hiddenCmdName?] }

                share -
                transfer { return [DisplayHints [interp slaves]] }
            }
        }
    }
    return ""
}

proc complete(join) {text start end line pos mod} {


    switch -- $pos {
        1 { return [DisplayHints <list>] }
        2 { return [DisplayHints ?joinString?] }
    }
    return ""
}

proc complete(lappend) {text start end line pos mod} {

    switch -- $pos {
        1 { return [VarCompletion ${text}] }
        default { return [DisplayHints ?value?] }
    }
    return ""
}

# the following routines are described in the
# `library' man page.
# --- LIBRARY ---

proc complete(auto_execok) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <cmd>] }
    }
    return ""
}

proc complete(auto_load) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <cmd>] }
    }
    return ""
}

proc complete(auto_mkindex) {text start end line pos mod} {
    switch -- $pos {
        1 { return "" }
        default { return [DisplayHints ?pattern?] }
    }
    return ""
}

# proc complete(auto_reset) {text start end line pos mod} {
# }

proc complete(tcl_findLibrary) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <basename>] }
        2 { return [DisplayHints <version>] }
        3 { return [DisplayHints <patch>] }
        4 { return [DisplayHints <initScript>] }
        5 { return [DisplayHints <enVarName>] }
        6 { return [DisplayHints <varName>] }
    }
    return ""
}

proc complete(parray) {text start end line pos mod} {
    switch -- $pos {
        1 {
            set vars [uplevel [info level] info vars]
            foreach var ${vars} {
                if {[uplevel [info level] array exists ${var}]} {
                    lappend matches ${var}
                }
            }
            return [CompleteFromList $text $matches]
        }
    }
    return ""
}

proc complete(tcl_endOfWord) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <str>] }
        2 { return [DisplayHints <start>] }
    }
    return ""
}

proc complete(tcl_startOfNextWord) {text start end line pos mod} {
    return [complete(tcl_endOfWord) $text $start $end $line $pos $mod]
}

proc complete(tcl_startOfPreviousWord) {text start end line pos mod} {
    return [complete(tcl_endOfWord) $text $start $end $line $pos $mod]
}

proc complete(tcl_wordBreakAfter) {text start end line pos mod} {
    return [complete(tcl_endOfWord) $text $start $end $line $pos $mod]
}

proc complete(tcl_wordBreakBefore) {text start end line pos mod} {
    return [complete(tcl_endOfWord) $text $start $end $line $pos $mod]
}

# --- END OF `LIBRARY' ---

proc complete(lindex) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <list>] }
        2 { return [DisplayHints <index>] }
    }
    return ""
}

proc complete(linsert) {text start end line pos mod} {


    switch -- $pos {
        1 { return [DisplayHints <list>] }
        2 { return [DisplayHints <index>] }
        3 { return [DisplayHints <element>] }
        default { return [DisplayHints ?element?] }
    }
    return ""
}

proc complete(list) {text start end line pos mod} {
    return [DisplayHints ?arg?]
}

proc complete(llength) {text start end line pos mod} {


    switch -- $pos {
        1 {
            return [DisplayHints <list>]
        }
    }
    return ""
}

proc complete(load) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return ""; # filename
        }
        2 {
            if {![llength ${mod}]} {

                return [DisplayHints ?packageName?]
            }
        }
        3 {
            if {![llength ${mod}]} {

                return [DisplayHints ?interp?]
            }
        }
    }
    return ""
}

proc complete(lrange) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <list>] }
        2 { return [DisplayHints <first>] }
        3 { return [DisplayHints <last>] }


    }
    return ""
}

proc complete(lreplace) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <list>] }
        2 { return [DisplayHints <first>] }
        3 { return [DisplayHints <last>] }
        default { return [DisplayHints ?element?] }



    }
    return ""
}

proc complete(lsearch) {text start end line pos mod} {


    set options {-exact -glob -regexp}
    switch -- $pos {
        1 {
            return [CompleteFromList ${text} "$options <list>"]

        }
        2 -
        3 -
        4 {
            set sub [Lindex $line 1]
            if {-1 != [lsearch $options $sub]} {
                incr pos -1
            }
            switch -- $pos {
                1 { return [DisplayHints <list>] }
                2 { return [DisplayHints <pattern>] }

            }
        }
    }
    return ""
}

proc complete(lsort) {text start end line pos mod} {
    set options [RemoveUsedOptions ${line} {
        -ascii -dictionary -integer -real -command
        -increasing -decreasing -index <list>
    }]
    switch -- $pos {
        1 { return [CompleteFromList ${text} ${options}] }
        default {
            switch -- [PreviousWord ${start} ${line}] {
                -command {
                    return [CompleteFromList $text [CommandCompletion $text]]
                }
                -index { return [DisplayHints <index>] }
                default { return [CompleteFromList ${text} ${options}] }
            }
        }
    }
    return ""
}

# --- MSGCAT PACKAGE ---

# create a msgcat namespace inside
# tclreadline and import some commands.
#
namespace eval msgcat {
    catch {namespace import ::tclreadline::DisplayHints}
}

proc msgcat::complete(mc) {text start end line pos mod} {





    switch -- $pos {


        1 { return [DisplayHints <src-string>] }
    }


    return ""
}




proc msgcat::complete(mclocale) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints ?newLocale?] }
    }
    return ""
}

# proc msgcat::complete(mcpreferences) {text start end line pos mod} {
# }

proc msgcat::complete(mcload) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <dirname>] }
    }
    return ""
}

proc msgcat::complete(mcset) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <locale>] }
        2 { return [DisplayHints <src-string>] }
        3 { return [DisplayHints ?translate-string?] }
    }
    return ""
}

proc msgcat::complete(mcunknown) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <locale>] }
        2 { return [DisplayHints <src-string>] }
    }
    return ""
}

# --- END OF MSGCAT PACKAGE ---

# TODO import ! -force
proc complete(namespace) {text start end line pos mod} {

    # TODO dosn't work ???
    set space_matches [namespace children :: [string trim ${mod}*]]
    # puts \nspace_matches=|${space_matches}|
    set cmd [Lindex $line 1]

    switch -- $pos {
        1 {
            set cmds {
                children code current delete eval export forget
                import inscope origin parent qualifiers tail which}
            return [TryFromList $text $cmds]

        }
        2 {
            switch -- $cmd {
                children -
                delete -
                eval -
                inscope -
                forget -
                parent -
                qualifiers -
                tail {
                    regsub {^([^:])} ${mod} {::\1} mod; # full qual. name
                    return [TryFromList ${mod} $space_matches]
                }
                code { return [DisplayHints <script> ] }
                current {}
                export { return [CompleteFromList ${text} {-clear ?pattern?}] }
                import {
                    if {"-" != [string index ${mod} 0]} {
                        regsub {^([^:])} ${mod} {::\1} mod; # full qual. name
                    }
                    return [CompleteFromList ${mod} "-force $space_matches"]
                }

                origin { return [DisplayHints <command>] }
                # qualifiers -

                # tail { return [DisplayHints <string>] }
                which { return [CompleteFromList ${mod} {
                    -command -variable <name>}] }
            }




        }
        3 {
            switch -- $cmd {

                children -
                export -
                forget -
                import { return [DisplayHints ?pattern?] }
                delete { return [TryFromList ${mod} $space_matches] }



                eval -
                inscope { return [DisplayHints <arg>] }
                which { return [CompleteFromList ${mod} {-variable <name>}] }
            }
        }
        4 {

            switch -- $cmd {
                export -
                forget -
                import { return [DisplayHints ?pattern?] }



                delete { return [TryFromList ${mod} $space_matches] }
                eval -
                inscope { return [DisplayHints ?arg?] }
                which { return [CompleteFromList ${mod} {<name>}] }

            }
        }
    }
    return ""
}

proc complete(open) {text start end line pos mod} {





        # 2 { return [DisplayHints ?access?] }
    switch -- $pos {
        2 {
            set access {r r+ w w+ a a+ 
                RDONLY WRONLY RDWR APPEND CREAT EXCL NOCTTY NONBLOCK TRUNC}
            return [CompleteFromList ${text} $access]
        }
        3 { return [DisplayHints ?permissions?] }
    }
    return ""
}

proc complete(package) {text start end line pos mod} {
    set cmd [Lindex $line 1]

    switch -- $pos {
        1 {
            set cmds {
                forget ifneeded names present provide require
                unknown vcompare versions vsatisfies}
            return [TryFromList $text $cmds]

        }
        2 {
            switch -- $cmd {
                forget -
                ifneeded -
                provide -
                versions { return [CompleteFromList ${mod} [package names]] }
                present -
                require {
                    return [CompleteFromList ${mod} "-exact [package names]"] }
                names {}

                unknown { return [DisplayHints ?command?] }
                vcompare -

                vsatisfies { return [DisplayHints <version1>] }
            }

        }
        3 {
            switch -- $cmd {
                forget {}


                ifneeded { return [DisplayHints <version>] }
                provide { return [DisplayHints ?version?] }
                versions {}
                present -
                require {
                    if {"-exact" == [PreviousWord ${start} ${line}]} {

                        return [CompleteFromList ${mod} [package names]]

                    } else {
                        return [DisplayHints ?version?]
                    }
                }
                names {}
                unknown {}
                vcompare -

                vsatisfies { return [DisplayHints <version2>] }
            }
        }
    }
    return ""
}

proc complete(pid) {text start end line pos mod} {
    switch -- $pos {
        1 { return [ChannelId ${text}] }
    }
}

proc complete(pkg_mkIndex) {text start end line pos mod} {
    set cmds [RemoveUsedOptions ${line} {-direct -load -verbose -- <dir>} {--}]
    set res [string trim [TryFromList $text $cmds]]
    set prev [PreviousWord ${start} ${line}]

    if {"-load" == $prev} {
        return [DisplayHints <pkgPat>]
    } elseif {"--" == $prev} {
        return [TryFromList ${text} <dir>]
    }



    return ${res}


}

proc complete(proc) {text start end line pos mod} {
    switch -- $pos {
        1 {
            set known_procs [ProcsOnlyCompletion ${text}]
            return [CompleteFromList ${text} ${known_procs}]
        }
        2 {
            set proc [Lindex $line 1]
            if {[catch {set args [uplevel [info level] info args ${proc}]}]} {
                return [DisplayHints <args>]
            } else {
                return [list "\{${args}\}"]
            }
        }
        3 {
            if {![string length [Lindex $line $pos]]} {
                return [list \{ {}]; # \}
            } else {
                return [DisplayHints <body>]
            }
        }
    }
    return ""
}

proc complete(puts) {text start end line pos mod} {
    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            return [OutChannelId ${text} "-nonewline"]
        }
        2 {
            switch -- $cmd {
                -nonewline { return [OutChannelId ${text}] }
                default { return [DisplayHints <string>] }
            }
        }
        3 {
            switch -- $cmd {
                -nonewline { return [DisplayHints <string>] }
            }
        }
    }
    return ""
}

# proc complete(pwd) {text start end line pos mod} {
# }

proc complete(read) {text start end line pos mod} {
    set cmd [Lindex $line 1]
    switch -- $pos {
        1 {
            return [InChannelId ${text} "-nonewline"]
        }
        2 {
            switch -- $cmd {
                -nonewline { return [InChannelId ${text}] }
                default { return [DisplayHints <numChars>] }
            }
        }
    }
    return ""
}

proc complete(regexp) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]
    if {[llength ${prev}] && "--" != $prev && \
        ("-" == [string index ${prev} 0] || 1 == $pos)} {
        set cmds [RemoveUsedOptions ${line} {
            -nocase -indices -expanded -line 
            -linestop -lineanchor -about <expression> --} {--}]
        if {[llength ${cmds}]} {
            return [string trim [CompleteFromList $text $cmds]]
        }
    } else {
        set virtual_pos [expr ${pos} - [FirstNonOption ${line}]]
        switch -- ${virtual_pos} {
            0 { return [DisplayHints <string>] }
            1 { return [DisplayHints ?matchVar?] }
            default { return [DisplayHints ?subMatchVar?] }
        }
    }
    return ""
}

# proc complete(regexp) {text start end line pos mod} {
#     We're not on windoze here ...
# }

proc complete(regsub) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]
    if {[llength ${prev}] && "--" != $prev && \
        ("-" == [string index ${prev} 0] || 1 == $pos)} {
        set cmds [RemoveUsedOptions ${line} {
            -all -nocase --} {--}]
        if {[llength ${cmds}]} {
            return [string trim [CompleteFromList $text $cmds]]
        }
    } else {
        set virtual_pos [expr ${pos} - [FirstNonOption ${line}]]
        switch -- ${virtual_pos} {
            0 { return [DisplayHints <expression>] }
            1 { return [DisplayHints <string>] }
            2 { return [DisplayHints <subSpec>] }
            3 { return [DisplayHints <varName>] }
        }
    }
    return ""
}

proc complete(rename) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [CompleteFromList $text [CommandCompletion $text]]

        }
        2 {
            return [DisplayHints <newName>]
        }
    }
    return ""
}

# proc complete(resource) {text start end line pos mod} {
#     This is not a mac ...
# }

proc complete(return) {text start end line pos mod} {
    # TODO this is not perfect yet
    set cmds {-code -errorinfo -errorcode ?string?}
    set res [PreviousWord ${start} ${line}]

    switch -- ${res} {

        -errorinfo { return [DisplayHints <info>] }
        -code -
        -errorcode {
            set codes {ok error return break continue}
            return [TryFromList ${mod} ${codes}]
        }
    }
    return [CompleteFromList ${text} [RemoveUsedOptions ${line} ${cmds}]]
}

# --- SAFE PACKAGE ---

# create a safe namespace inside
# tclreadline and import some commands.
#
namespace eval safe {
    catch {
        namespace import \
        ::tclreadline::DisplayHints ::tclreadline::PreviousWord \
        ::tclreadline::CompleteFromList ::tclreadline::CommandCompletion \
        ::tclreadline::RemoveUsedOptions ::tclreadline::HostList \
        ::tclreadline::ChannelId ::tclreadline::Lindex \
        ::tclreadline::CompleteBoolean
    }
    variable opts
    set opts {
        -accessPath -statics -noStatics -nested -nestedLoadOk -deleteHook
    }
    proc SlaveOrOpts {text start line pos slave} {
        set prev [PreviousWord ${start} ${line}]
        variable opts
        if {$pos > 1} {
            set slave ""
        }
        switch -- $prev {
            -accessPath { return [DisplayHints <directoryList>] }
            -statics { return [CompleteBoolean $text] }
            -nested { return [CompleteBoolean $text] }
            -deleteHook { return [DisplayHints <script>] }
            default {
                return [CompleteFromList ${text} \
                [RemoveUsedOptions ${line} "${opts} $slave"]]



            }
        }
    }
}

proc safe::complete(interpCreate) {text start end line pos mod} {
    return [SlaveOrOpts $text $start $line $pos ?slave?]
}

proc safe::complete(interpInit) {text start end line pos mod} {
    return [SlaveOrOpts $text $start $line $pos [interp slaves]]
}

proc safe::complete(interpConfigure) {text start end line pos mod} {
    return [SlaveOrOpts $text $start $line $pos [interp slaves]]
}

proc safe::complete(interpDelete) {text start end line pos mod} {
    return [CompleteFromList $text [interp slaves]]
}

proc safe::complete(interpAddToAccessPath) {text start end line pos mod} {
    switch -- $pos {
        1 { return [CompleteFromList $text [interp slaves]] }
    }
}

proc safe::complete(interpFindInAccessPath) {text start end line pos mod} {
    switch -- $pos {
        1 { return [CompleteFromList $text [interp slaves]] }
    }
}

proc safe::complete(setLogCmd) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints ?cmd?] }
        default { return [DisplayHints ?arg?] }
    }
}

# --- END OF SAFE PACKAGE ---

proc complete(scan) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <string>] }
        2 { return [DisplayHints <format>] }
        default { return [VarCompletion ${text}] }
    }
    return ""
}

proc complete(seek) {text start end line pos mod} {

    switch -- $pos {
        1 { return [ChannelId ${text}] }

        2 { return [DisplayHints <offset>] }
        3 { return [TryFromList ${text} {start current end}] }
    }
    return ""
}

proc complete(set) {text start end line pos mod} {
    switch -- $pos {




        1 { return [VarCompletion ${text}] }
        2 {
            if {$text == "" || $text == "\"" || $text == "\{"} {
                set line [QuoteQuotes $line]
                if {[catch [list set value [list [uplevel [info level] \
                    set [Lindex $line 1]]]] msg]
                } {
                    return ""
                } else {
                    return [Quote $value ${text}]
                }
            }
        }
    }
    return ""
}

proc complete(socket) {text start end line pos mod} {
    set cmd [Lindex ${line} 1]
    set prev [PreviousWord ${start} ${line}]
    if {"-server" == ${cmd}} {
        # server sockets
        #

        switch -- $pos {
            2 { return [DisplayHints <command>] }
            default {
                if {"-myaddr" == $prev} {
                    return [DisplayHints <addr>]
                } else {
                    return [CompleteFromList ${mod} \
                    [RemoveUsedOptions $line {-myaddr -error -sockname <port>}]]
                }

            }
        }
    } else {
        # client sockets
        #
        switch -- ${prev} {
            -myaddr { return [DisplayHints <addr>] }
            -myport { return [DisplayHints <port>] }
        }






        set hosts [HostList]










        set cmds {-myaddr -myport -async -myaddr -error -sockname -peername}
        if {$pos <= 1} {
            lappend cmds -server
        }
        set cmds [RemoveUsedOptions $line $cmds]
        if {-1 != [lsearch $hosts $prev]} {
            return [DisplayHints <port>]
        } else {
            return [CompleteFromList ${mod} [concat ${cmds} ${hosts}]]
        }
    }
    return ""
}

# proc complete(source) {text start end line pos mod} {
# }

proc complete(split) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <string>] }
        2 { return [DisplayHints ?splitChars?] }
    }
}

proc complete(string) {text start end line pos mod} {
    set cmd [Lindex ${line} 1]
    set prev [PreviousWord ${start} ${line}]
    set cmds {

        bytelength compare equal first index is last length map match
        range repeat replace tolower toupper totitle trim trimleft
        trimright wordend wordstart}


    switch -- $pos {
        1 {
            return [CompleteFromList ${text} ${cmds}]
        }
        2 {
            switch -- $cmd {
                compare -
                equal {
                    return [CompleteFromList ${text} {
                        -nocase -length <string> }]
                }

                first -

                last { return [DisplayHints <string1>] }


                map { return [CompleteFromList ${text} {-nocase <charMap>]} }
                match { return [CompleteFromList ${text} {-nocase <pattern>]} }

                is {
                    return [CompleteFromList ${text} {
                        alnum alpha ascii boolean control digit double 
                        false graph integer lower print punct space 
                        true upper wordchar xdigit 
                    }]
                }

                bytelength -
                index -
                length -
                range -
                repeat -
                replace -
                tolower -
                totitle -
                toupper -
                trim -
                trimleft -
                trimright -
                wordend -

                wordstart { return [DisplayHints <string>] }
            }

        }
        3 {
            switch -- $cmd {
                compare -
                equal {
                    if {"-length" == $prev} {
                        return [DisplayHints <int>]
                    }
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-nocase -length <string>}]]
                }

                first -

                last { return [DisplayHints <string2>] }


                map {
                    if {"-nocase" == $prev} {
                        return [DisplayHints <charMap>]
                    } else {
                        return [DisplayHints <string>]
                    }
                }
                match {
                    if {"-nocase" == $prev} {
                        return [DisplayHints <pattern>]
                    } else {
                        return [DisplayHints <string>]
                    }

                }


                is {
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-strict -failindex <string>}]]
                }

                bytelength {}
                index -
                wordend -
                wordstart { return [DisplayHints <charIndex>] }
                range -
                replace { return [DisplayHints <first>] }
                repeat { return [DisplayHints <count>] }
                tolower -
                totitle -

                toupper { return [DisplayHints ?first?] }
                trim -
                trimleft -
                trimright { return [DisplayHints ?chars?] }
            }
        }
        4 {
            switch -- $cmd {
                compare -
                equal {
                    if {"-length" == $prev} {
                        return [DisplayHints <int>]
                    }
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-nocase -length <string>}]]
                }

                first -
                last { return [DisplayHints ?startIndex?] }

                map -
                match { return [DisplayHints <string>] }

                is {
                    if {"-failindex" == $prev} {
                        return [VarCompletion ${text}]
                    }
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-strict -failindex <string>}]]
                }

                bytelength {}
                index {}
                length {}
                range -
                replace { return [DisplayHints <last>] }
                repeat {}
                tolower -
                totitle -
                toupper { return [DisplayHints ?last?] }
                trim -

                trimleft -
                trimright {}
                wordend -

                wordstart {}
            }
        }
        default {
            switch -- $cmd {
                compare -
                equal {
                    if {"-length" == $prev} {
                        return [DisplayHints <int>]
                    }
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-nocase -length <string>}]]
                }

                is {
                    if {"-failindex" == $prev} {
                        return [VarCompletion ${text}]
                    }
                    return [CompleteFromList ${text} \
                    [RemoveUsedOptions $line {-strict -failindex <string>}]]
                }

                replace { return [DisplayHints ?newString?] }
            }
        }
    }
    return ""
}

proc complete(subst) {text start end line pos mod} {
    return [CompleteFromList ${text} [RemoveUsedOptions $line {
        -nobackslashes -nocommands -novariables <string>}]]



}

proc complete(switch) {text start end line pos mod} {
    set prev [PreviousWord ${start} ${line}]
    if {[llength ${prev}] && "--" != $prev && \
        ("-" == [string index ${prev} 0] || 1 == $pos)} {
        set cmds [RemoveUsedOptions ${line} {
            -exact -glob -regexp --} {--}]


        if {[llength ${cmds}]} {
            return [string trim [CompleteFromList $text $cmds]]
        }
    } else {
        set virtual_pos [expr ${pos} - [FirstNonOption ${line}]]
        switch -- ${virtual_pos} {
            0 { return [DisplayHints <string>] }
            1 { return [DisplayHints <pattern>] }
            2 { return [DisplayHints <body>] }
            default { 
                switch [expr $virtual_pos % 2] {
                    0 { return [DisplayHints ?body?] }
                    1 { return [DisplayHints ?pattern?] }
                }
            }
        }
    }
    return ""
}

proc complete(tell) {text start end line pos mod} {

    switch -- $pos {
        1 { return [ChannelId ${mod}] }
    }
    return ""
}

proc complete(time) {text start end line pos mod} {
    switch -- $pos {
        1 { return [DisplayHints <script>] }
        2 { return [DisplayHints ?count?] }
    }
    return ""
}

proc complete(trace) {text start end line pos mod} {
    set cmd [Lindex ${line} 1]

    switch -- $pos {
        1 {
            return [CompleteFromList ${mod} {variable vdelete vinfo}]




        }
        2 {
            return [CompleteFromList ${text} \
            [uplevel [info level] info vars "${mod}*"]]
        }
        3 {
            switch -- $cmd {
                variable -
                vdelete { return [CompleteFromList ${text} {r w u}] }
            }
        }
        4 {
            switch -- $cmd {
                variable -
                vdelete {
                    return [CompleteFromList $text [CommandCompletion $text]]
                }
            }
        }
    }
    return ""
}

proc complete(unknown) {text start end line pos mod} {
    switch -- $pos {
        1 {
            return [CompleteFromList $text [CommandCompletion $text]]
        }
        default { return [DisplayHints ?arg?] }
    }
    return ""
}

proc complete(unset) {text start end line pos mod} {
    return [VarCompletion ${text}]
}

proc complete(update) {text start end line pos mod} {

    switch -- $pos {
        1 { return idletasks }
    }
    return ""
}

proc complete(uplevel) {text start end line pos mod} {
    set one [Lindex ${line} 1]
    switch -- $pos {
        1 {
            return [CompleteFromList $text "?level? [CommandCompletion $text]"]

        }
        2 {
            if {"#" == [string index $one 0] || [regexp {^[0-9]*$} $one]} {
                return [CompleteFromList $text [CommandCompletion $text]]
            } else {
                return [DisplayHints ?arg?]
            }
        }
        default { return [DisplayHints ?arg?] }
    }
    return ""
}

proc complete(upvar) {text start end line pos mod} {



    set one [Lindex ${line} 1]
    switch -- $pos {
        1 {
            return [DisplayHints {?level? <otherVar>}]
        }
        2 {
            if {"#" == [string index $one 0] || [regexp {^[0-9]*$} $one]} {
                return [DisplayHints <otherVar>]
            } else {

                return [DisplayHints <myVar>]
            }
        }
        3 {
            if {"#" == [string index $one 0] || [regexp {^[0-9]*$} $one]} {
                return [DisplayHints <myVar>]
            } else {

                return [DisplayHints ?otherVar?]
            }
        }
        default {
            set virtual_pos $pos
            if {"#" == [string index $one 0] || [regexp {^[0-9]*$} $one]} {
                incr virtual_pos
            }
            switch [expr $virtual_pos % 2] {
                0 { return [DisplayHints ?myVar?] }
                1 { return [DisplayHints ?otherVar?] }
            }
        }
    }
    return ""
}

proc complete(variable) {text start end line pos mod} {
    set modulo [expr $pos % 2]

    switch -- $modulo {
        1 { return [VarCompletion ${text}] }

        0 {
            if {$text == "" || $text == "\"" || $text == "\{"} {
                set line [QuoteQuotes $line]


                if {[catch [list set value [list [uplevel [info level] \
                    set [PreviousWord $start $line]]]] msg]
                } {
                    return ""
                } else {
                    return [Quote $value ${text}]
                }
            }
        }
    }
    return ""
}

proc complete(vwait) {text start end line pos mod} {
    switch -- $pos {
        1 { return [VarCompletion ${mod}] }
    }
    return ""
}

proc complete(while) {text start end line pos mod} {
    switch -- $pos {
        1 -
        2 {
            return [BraceOrControlStatement $text $start $end $line $pos $mod]
        }
    }
    return ""
}

# -------------------------------------
#                  TK
# -------------------------------------

# generic widget configuration