File: hv3_bookmarks.tcl

package info (click to toggle)
tk-html3 3.0~fossil20110109-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,416 kB
  • sloc: ansic: 48,980; tcl: 26,031; sh: 1,186; yacc: 161; makefile: 24
file content (2035 lines) | stat: -rw-r--r-- 60,646 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
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
namespace eval hv3 { set {version($Id: hv3_bookmarks.tcl,v 1.22 2008/02/10 07:50:01 danielk1977 Exp $)} 1 }

namespace eval ::hv3::bookmarks {

  set INSTRUCTIONS {
BASICS:

This set of frames is Hv3's bookmarks-system-slash-homepage. Initially,
there are 5 bookmarks in the system. You can see them in the left-hand frame,
under the words "Start here" and above "Trash".

Changes made to the bookmarks system are saved in the state-file
selected by the -statefile command-line option. If no -statefile option
is supplied, then changes are discarded when the browser window is closed.

Initially, there are no folders in the database. Folders can be 
added to the bookmarks list by pressing the "New Folder" button. You can then
organize existing bookmarks into folders by dragging and dropping them.
Clicking the folder icon beside a folder expands or hides it's contents.
Clicking on the name of a folder loads it's contents into this window.

Similarly, new bookmarks can be added to the system and initialized manually by
clicking "New Bookmark". Or, to bookmark the current page when browsing an HTML
document, press Ctrl-B or select the "File->Bookmark Page" menu option.

To edit an existing bookmark, click on it's containing folder (or "Start
here" if the bookmark is not in any folder). Then click the corresponding
"Edit Bookmark..." button in the right-hand frame (this window).

SNAPSHOTS:

Hv3's bookmarks system has one unique feature - it allows you to save a
copy of a bookmarked document in the database along with the bookmark
itself. This copy is called a snapshot. The snapshot contains all 
document content and style information, but any scripts are discarded
before it is saved. Images are not saved. Snapshots are useful for two
reasons - they can be viewed when the original resource is not available 
(i.e. because you are not online) and they make it easier to search the
bookmarks database (see below).

You can tell if a bookmark has an accompanying snapshot by looking at
it's icon. This document (INSTRUCTIONS) has a document icon, indicating
that it has a snapshot and if the accompanying hyperlink is clicked the
default action is to load the snapshot into the right-hand frame (this 
window). The four Hv3 related links have a different icon, one that
indicates no snapshot is saved in the database for this link.

The third kind of icon a bookmark indicates that a snapshot is saved,
but clicking on the hyperlink still navigates to the original resource,
not the saved snapshot. You can toggle the status of any bookmark that
has a saved snapshot by clicking on the icon itself. Try it now with
the INSTRUCTIONS bookmark, so that you know what the third type of icon
looks like.

When bookmarking a webpage using Ctrl-B or the "File->New Bookmark" menu,
you can save a snapshot along with the bookmark by checking the "Save website
text in database" checkbox on the create new bookmark dialog.

Documents can also be imported from the local file system by clicking 
"Import Data...", then "Import Document Tree". This allows you to select a 
file-system directory to import. All *.html or *.htm documents within the
selected directory and any sub-directories are saved with snapshots to 
the database. This is useful for creating a searchable full-text database
of downloaded documentation distributed in html format.

SEARCHING:

You can search the bookmarks database using SQLite's FTS3 extension by 
entering a string in the search box at the top of this frame and 
pressing enter.
  }

  set fts3_warning ""

  proc noop {args} {}

  proc have_fts3 {} {
    if {[info exists ::hv3::have_fts3]} {return $::hv3::have_fts3}
    # Test if fts3 is present.
    #
    set ::hv3::have_fts3 [expr {![catch {::hv3::sqlitedb eval {
      SELECT * FROM bm_fulltext2 WHERE 0
    }} msg]}]
    if {$::hv3::have_fts3 == 0} {
      puts "WARNING: fts3 not loaded ($msg)"
      set ::hv3::bookmarks::save_snapshot_variable 0
    }
  }

  proc ensure_initialized {} {
    set db ::hv3::sqlitedb
    $db transaction {
	if {![$db exists {select * from sqlite_master
	    where name = 'bm_bookmark2'}]} {
	    puts stderr "initializing bookmark database.."
	    initialise_database
	}
    }
    have_fts3
  }

  proc initialise_database {} {
    set rc [catch {
      ::hv3::sqlitedb eval {
        CREATE VIRTUAL TABLE bm_fulltext2 USING fts3(
          caption, description, snapshot
        );
      }
    }]

    have_fts3

    set rc [catch {
      ::hv3::sqlitedb eval {

        -- has_snapshot interpretation:
        --
        --     0 -> There is no snapshot (snapshot column is NULL)
        --     1 -> There is a snapshot, but it is not used by default
        --     2 -> There is a snapshot, used by default
        --
        CREATE TABLE bm_bookmark2(
          bookmarkid INTEGER PRIMARY KEY,
          caption TEXT,
          uri TEXT,
          description TEXT,
          has_snapshot INTEGER NOT NULL DEFAULT 0,
          image BLOB,
          snapshot TEXT
        );

        CREATE TABLE bm_folder2(
          folderid INTEGER PRIMARY KEY,
          name TEXT
        );

        CREATE TABLE bm_tree2(
          linkid     INTEGER PRIMARY KEY,
          folderid   INTEGER,               -- Index into bm_folder2
          objecttype TEXT,                  -- Either "f" or "b"
          objectid   INTEGER,               -- Index into object table

          UNIQUE(objecttype, objectid)
        );

        CREATE INDEX bm_tree2_i1 ON bm_tree2(folderid, objecttype);
      }
    } msg]

    if {$rc == 0} {
      set folderid 0

      ::hv3::sqlitedb transaction {
        foreach {A B C} {
  "INSTRUCTIONS"                http://tkhtml.tcl.tk 1
  "Hv3/Tkhtml3 Home page"       http://tkhtml.tcl.tk 0
  "Hv3/Tkhtml3 Mailing List"    http://groups.google.com/group/tkhtml3 0
  "Hv3/Tkhtml3 Repository"      http://tkhtml.tcl.tk/fossil 0
  "Hv3 @ Freecode"              http://freecode.com/projects/hv3 0
        } {
          if {$C} {
            set zSnapshot "
              <DIV style=font-family:courier>
              [string map {\n\n <P>} $::hv3::bookmarks::INSTRUCTIONS]
              </DIV>
            "
            set zSnapshotText $::hv3::bookmarks::INSTRUCTIONS
            set iSnapshot 2
            set zDesc {
              Instruction manual for Hv3.
            }
          } else {
            set zSnapshot ""
            set zDesc ""
            set zSnapshotText ""
            set iSnapshot 0
          }
          db_store_new_bookmark 0 $A $B $zDesc $iSnapshot $zSnapshot $zSnapshotText
        }
      }
    } else {
	puts stderr "Can't create bookmark tables! $msg"
    }

    if {$::hv3::have_fts3} {
      set rc [catch {::hv3::sqlitedb eval {SELECT * FROM bm_rebuild_fulltext2}}]
      if {$rc == 0} {
        set ::hv3::bookmarks::fts3_warning {
          <DIV style="font-size:small;color:darkred">
          <APPLET 
            style="float:right"
            class=bookmarks_button
            text="Rebuild Fts3 Database"
            command="::hv3::bookmarks::rebuild_fts_database"
          ></APPLET>
	    WARNING: Fts3 database requires rebuild. Fts3 is available now,
	    but the database was edited while it was not.  Until the database
            is rebuilt, search results will be unreliable.
          </DIV>
        }
      }
    } else {
      set ::hv3::bookmarks::fts3_warning {
        <DIV style="font-size:small;color:darkred">
              WARNING: Fts3 extension not available.
              Search functionality is severely compromised.
        </DIV>
      }
    }
  }

  proc init {hv3} {
    set frames [[$hv3 win] child_frames]
    set browser  [[winfo parent [$hv3 win]] browser]

    set tree_hv3 [[lindex $frames 0] hv3]
    set html_hv3 [[lindex $frames 1] hv3]

    #set controller [winfo parent [$html_hv3 win]].controller
    set controller [$html_hv3 win].controller
    set treewidget [$tree_hv3 html].treewidget

    controller $controller $browser $html_hv3 $treewidget 
    treewidget $treewidget $browser $controller

    #pack $controller -before [$html_hv3 win] -side top -fill x
    grid configure $controller -row 0 -column 0 -columnspan 2 -sticky ew
    place $treewidget -x 0.0 -y 0.0 -relwidth 1.0 -relheight 1.0

    $treewidget populate_tree
    focus ${controller}.filter

    foreach node [$html_hv3 html search applet] {
      $controller applet $node
    }
  }

  proc dropobject {target drag insertAfter inFolder} {
    foreach {drag_type drag_id}     $drag {}
    foreach {target_type target_id} $target {}

    set drag_type [string range $drag_type 0 0]
    set target_type [string range $target_type 0 0]

    set targetfolder ""
    set new_items [list]

    set N [::hv3::sqlitedb one {SELECT max(linkid) FROM bm_tree2}]
    incr N
    
    if {($target_type eq "f") && (
         $inFolder || $drag_type eq "b" || $target_id <= 0
        )
    } {
      set targetfolder $target_id
      lappend new_items $N $target_id $drag_type $drag_id
    } else {
      set targetfolder [::hv3::sqlitedb one {
        SELECT folderid 
        FROM bm_tree2 
        WHERE objecttype = $target_type AND objectid = $target_id
      }]
    }

    ::hv3::sqlitedb eval {
      SELECT objecttype, objectid 
      FROM bm_tree2
      WHERE folderid = $targetfolder
      ORDER BY linkid ASC
    } {
      set isTarget [expr {
          $objecttype eq $target_type && 
          $objectid == $target_id
      }]
      if {!$insertAfter && $isTarget} {
        incr N
        lappend new_items $N $targetfolder $drag_type $drag_id
      }

      if {$objecttype ne $drag_type || $objectid != $drag_id} {
        incr N
        lappend new_items $N $targetfolder $objecttype $objectid
      }

      if {$insertAfter && $isTarget} {
        incr N
        lappend new_items $N $targetfolder $drag_type $drag_id
      }
    }

    foreach {a b c d} $new_items {
      ::hv3::sqlitedb eval {
        REPLACE INTO bm_tree2(linkid, folderid, objecttype, objectid)
        VALUES($a, $b, $c, $d)
      }
    }
  }

  # The argument is a tkhtml3 widget. This proc generates an HTML 
  # document equivalent to the one currently loaded into the widget.
  # All style information is inlined in a <STYLE> block in the 
  # document head.
  #
  proc create_snapshot {hv3} {
    set zTitle ""
    set zStyle "\n"
    set zBody ""
    set zBase ""

    set html [$hv3 html]
    set zBase [$hv3 resolve_uri ""]

    foreach rule [$html _styleconfig] {
      foreach {selector properties origin} $rule {}
      if {$origin eq "agent"} continue
      append zStyle "$selector { $properties }\n"
    }

    set titlenode [$html search title]
    if {$titlenode ne ""} {
      set child [lindex [$titlenode children] 0]
      if {$child ne ""} { set zTitle [$child text] }
    }

    set bodynode [$html search body]
    set zBody [serialize_node_content $bodynode]

    return [subst {
      <HTML>
        <HEAD>
          <TITLE>$zTitle</TITLE>
          <STYLE>$zStyle</STYLE>
          <BASE href="$zBase"></BASE>
        </HEAD>
        <BODY>$zBody</BODY>
      </HTML>
    }]
  }

  proc serialize_node {node} {
    set tag [$node tag]
    if {$tag eq ""} {
      return [string map {< &lt; > &gt;} [$node text -pre]]
    } else {
      set attr ""
      foreach {k v} [$node attribute] {
        set val [string map [list "\x22" "\x5C\x22"] $v]
        append attr " $k=\"$val\""
      }
      set content [serialize_node_content $node]
      return "<${tag}${attr}>$content</${tag}>"
    }
  }

  proc serialize_node_content {node} {
    set zRes ""
    foreach child [$node children] {
      append zRes [serialize_node $child]
    }
    return $zRes
  }


  ::snit::widget treewidget {

    # The browser (::hv3::browser) containing this frameset
    #
    variable myBrowser

    # The controller that controls the left-hand frame of the frameset.
    #
    variable myController

    # Id of the current canvas text item that the mouse is hovering over.
    # A negative value means the mouse is not currently over any item.
    #
    variable myCurrentHover -1

    variable myTextId -array ""

    variable myIconId -array ""

    variable myTreeStart 0

    variable myOpenFolder -array ""

    variable myDragX ""
    variable myDragY ""
    variable myDragObject ""
    variable myPressedX ""
    variable myPressedY ""
    variable myPressedItem ""
    variable myPressedItemColor ""

    method click_new_folder {} {
      ${win}.newfolder configure -state normal
      ::hv3::bookmarks::new_folder $self
    }
    method click_new_bookmark {} {
      ${win}.newbookmark configure -state normal
      ::hv3::bookmarks::new_bookmark ""
    }

    method click_importexport {} {
      ${win}.importexport configure -state normal

      set initial /home/dan/.mozilla/firefox/jghthwxj.default/bookmarks.html
      set initialdir [file dirname $initial]
      set f [tk_getOpenFile -initialfile $initial -initialdir $initialdir \
          -filetypes [list \
              {{Html Files} {.html}} \
              {{All Files} *}
          ]
      ]
      if {$f eq ""} return

      set fd [open $f]
      set zBookmarks [read $fd]
      close $fd

      set H [html .boomarksparser]
      $H parse -final $zBookmarks

      unset -nocomplain zTitle
      foreach N [$H search dl,h3] {

        if {[$N tag] eq "dl"} {
          if {![info exists zTitle]} {
            set zTitle "Imported Bookmarks"
          }

          # Create the bm_folder2 record.
          ::hv3::sqlitedb eval {
            INSERT INTO bm_folder2(name) VALUES($zTitle);
          }
          set iFolderId [::hv3::sqlitedb last_insert_rowid]
          set imported_folders($N) $iFolderId

          set iParentId 0
          for {set P [$N parent]} {$P ne ""} {set P [$P parent]} {
            if {[$P tag] eq "dl"} {
              set iParentId $imported_folders($P)
              break
            }
          }

          # Create the bm_tree2 record.
          ::hv3::sqlitedb eval {
            INSERT INTO bm_tree2(folderid, objecttype, objectid) 
            VALUES($iParentId, 'f', $iFolderId)
          }

        } else {
          set zTitle [[lindex [$N children] 0] text]
        }
      }

      foreach N [$H search {dl a[href]}] {
        set zCaption [[lindex [$N children] 0] text]
        set zUri [$N attribute href]

        for {set P [$N parent]} {$P ne ""} {set P [$P parent]} {
          if {[$P tag] eq "dl"} {
            set iParentId $imported_folders($P)
            break
          }
        }

        ::hv3::bookmarks::db_store_new_bookmark \
            $iParentId $zCaption $zUri "" 0 "" ""
      }

      destroy $H

      $self populate_tree
    }

    constructor {browser controller} {
      set myBrowser $browser
      set myController $controller

      # On certain systems (linux using xft), it is very expensive
      # to grab a font for the first time. So create a label here to
      # make sure there is always at least one reference to the font
      # Hv3DefaultBold. There are always lot's of references to the
      # other font used by this widget - Hv3DefaultFont.
      #
      label ${win}.fontrefholder -font Hv3DefaultBold

      # Set up the button widgets used by this gui.
      #
      menubutton ${win}.importexport \
          -text "Import Data..."     \
          -font Hv3DefaultFont       \
          -borderwidth 1             \
          -relief raised             \
          -pady 1                    

      set menu [::hv3::menu ${win}.importexport.menu]
      ${win}.importexport configure -menu $menu
      $menu add command \
          -label "Auto-import Mozilla bookmarks"            \
          -command ::hv3::bookmarks::import_bookmarks
      $menu add command \
          -label "Import Mozilla bookmarks.html file"       \
          -command ::hv3::bookmarks::import_bookmarks_manual
      $menu add command \
          -label "Import Document Tree" \
          -command [list ::hv3::bookmarks::import_tree]
      $menu add command \
          -label "Index Hv3 DOM Reference" \
          -command [list ::hv3::bookmarks::import_dom]
      ::hv3::button ${win}.newfolder                        \
          -text "New Folder"                                \
          -command [list $self click_new_folder] 
      ::hv3::button ${win}.newbookmark                      \
          -text "New Bookmark"                              \
          -command [list $self click_new_bookmark] 
      ::hv3::button ${win}.expand                           \
          -text "Expand All"                                \
          -command [list $self expand_all]
      ::hv3::button ${win}.collapse                         \
          -text "Collapse All"                              \
          -command [list $self collapse_all]


      # Create the canvas on which the tree is drawn
      #
      ::hv3::scrolled canvas ${win}.canvas -background white -propagate 1
      ${win}.canvas configure -yscrollincrement 1

      # Set up the geometry manager
      #
      grid ${win}.importexport              -columnspan 2 -sticky ew     ;# 0
      grid ${win}.newfolder                 -columnspan 2 -sticky ew     ;# 1
      grid ${win}.newbookmark               -columnspan 2 -sticky ew     ;# 2
      grid ${win}.canvas                    -columnspan 2 -sticky nsew   ;# 3
      grid ${win}.expand ${win}.collapse    -sticky ew                   ;# 4
      grid rowconfigure    ${win} 3 -weight 1
      grid columnconfigure ${win} 0 -weight 1
      grid columnconfigure ${win} 1 -weight 1

      bind ${win}.canvas <Motion>          [list $self motion_event %x %y]
      bind ${win}.canvas <Leave>           [list $self leave_event]
      bind ${win}.canvas <ButtonPress-1>   [list $self press_event %x %y]
      bind ${win}.canvas <ButtonRelease-1> [list $self release_event %x %y]
    }

    method open_folders {objecttype objectid} {
      set parent [::hv3::sqlitedb one {
        SELECT folderid 
        FROM bm_tree2 
        WHERE objecttype = $objecttype AND objectid = $objectid
      }]
      if {$parent eq "" || $parent == 0} return
      set myOpenFolder($parent) 1
      if {$parent == -1} return

      $self open_folders f $parent
    }

    method seek_tree {} {
      #set C ${win}.canvas.widget
      set C ${win}.canvas
      foreach {page pageid} [$myController current] {}
      if {$page ne "folder" && $page ne "snapshot"} return

      if {$page eq "snapshot"} {set page bookmark}
      set tag "${page}$pageid"

      set bbox [$C bbox $tag]
      if {$bbox eq ""} return

      foreach {dummy1 top dummy2 bottom} $bbox {}
      if {$top < [$C canvasy 0]} {
        set pixels [expr {int($top - [$C canvasy 0] - 20)}]
        $C yview scroll $pixels units
      } elseif {$bottom > [$C canvasy [winfo height $C]]} {
        set pixels [expr {int($bottom + 20 - [$C canvasy [winfo height $C]])}]
        $C yview scroll $pixels units
      }
    }

    method populate_tree {{isAutoOpen 0}} {
      #set C ${win}.canvas.widget
      set C ${win}.canvas

      set y 20
      set x 10
      set yincr [expr [font metrics Hv3DefaultFont -linespace] + 5]

      set myPressedItem ""
      $C delete all
      array unset myTextId
      array unset myIconId

      set myTreeStart $y
      incr y $yincr
      ::hv3::sqlitedb transaction { 

        if {$isAutoOpen} {
          foreach {page pageid} [$myController current] {}
          if {$page eq "folder" || $page eq "snapshot"} {
            if {$page eq "folder"} {
              set page f
            } else {
              set page b
            }
            $self open_folders $page $pageid
          }
        }
  
        # Color for special links.
        #
        set c darkblue

        # Create the special "bookmarks" folder.
        #
        set f Hv3DefaultFont
        if {[$myController current] eq "folder 0"} {
          set f Hv3DefaultBold
        }
        set tid [
            $C create text $x $y -text "Start here" -anchor sw -font $f -fill $c
        ]
        set myTextId($tid) {folder 0 "Click to view root folder"}
        incr y $yincr
        incr y $yincr

        # Create the tree of user bookmarks.
        #
        set y [$self drawSubTree $x $y 0]
        incr y $yincr
  
        # Create the special "trash" folder.
        #
        set f Hv3DefaultFont
        set x2 [expr {$x + [image width itrash] + 5}]
        if {[$myController current] eq "folder -1"} {
          set f Hv3DefaultBold
        }
        set tid [
            $C create text $x2 $y -text "Trash" -anchor sw -font $f -fill $c
        ]
        set myTextId($tid)  "folder -1"
        set myTextId($tid) "folder -1 {Click to view trashcan contents}"
        if {[info exists myOpenFolder(-1)]} {
          set tid [$C create image $x $y -image iopentrash -anchor sw]
        } else {
          set tid [$C create image $x $y -image itrash -anchor sw]
        }
        set myIconId($tid) "folder -1"
        incr y $yincr
        if {[info exists myOpenFolder(-1)]} {
          set y [$self drawSubTree [expr $x + $yincr] $y -1]
        }

        # Create the special "history" folder.
        #
        set f Hv3DefaultFont
        if {[$myController current] eq "recent -1"} {
          set f Hv3DefaultBold
        }
        set tid [
            $C create text $x2 $y -text "History" -anchor sw -font $f -fill $c
        ]
        set myTextId($tid) {recent -1 "Click to view all recently viewed URIs"}
  
        $C configure -scrollregion [concat 0 0 [lrange [$C bbox all] 2 3]]

        if {$isAutoOpen} {
          $self seek_tree
        }
      }
    }

    method set_drag_tag {item} {
      set C ${win}.canvas.widget
      set tag1 [lindex [$C itemcget $item -tags] 0]
      if {[string range $tag1 0 7] eq "bookmark" ||
          [string range $tag1 0 5] eq "folder"
      } {
        $C itemconfigure $tag1 -tags draggable
        $C raise draggable

        set info ""
        if {[info exists myTextId($item)]} {
          set info [lrange $myTextId($item) 0 1]
        } elseif {[info exists myIconId($item)]} {
          set info $myIconId($item)
        }

        set myDragObject $info
      }
      return
    }

    method motion_event {x y} {
      set C ${win}.canvas.widget
      set x [$C canvasx $x]
      set y [$C canvasy $y]
      set hover [$C find overlapping $x $y $x $y]
      if {$myCurrentHover>=0 && $hover != $myCurrentHover} {
        $C delete underline
      }

      if {
          $myDragObject eq "" &&
          $myPressedItem ne "" && 
          (abs($myPressedX-$x) > 6 || abs($myPressedY-$y)>6)
      } {
        $self set_drag_tag $myPressedItem
        if {$myPressedItemColor ne ""} {
          $C itemconfigure $myPressedItem -fill $myPressedItemColor
          set myPressedItemColor ""
          $C delete underline
        }
      }

      if {$myDragObject ne ""} {
        $C move draggable [expr {$x-$myDragX}] [expr {$y-$myDragY}]
        set myDragX $x
        set myDragY $y
        return
      }

      if {[$C type $hover] eq "text"} {
        set bbox [$C bbox $hover]
        set y [expr [lindex $bbox 3] - 2]
        $C create line [lindex $bbox 0] $y [lindex $bbox 2] $y -tags underline
        $C configure -cursor hand2
        $myBrowser set_frame_status [lindex $myTextId($hover) 2]
      } else {
        set hover -1
        $C configure -cursor ""
        $myBrowser set_frame_status ""
      }

      set myCurrentHover $hover
    }

    method leave_event {} {
      set C ${win}.canvas
      $C delete underline
      set myCurrentHover -1
      $myBrowser set_frame_status ""
      $C configure -cursor ""
    }

    method press_event {x y} {
      set C ${win}.canvas
      set x [$C canvasx $x]
      set y [$C canvasy $y]
      set myPressedX $x
      set myPressedY $y
      set myDragX $x
      set myDragY $y

      set myPressedItem [$C find overlapping $x $y $x $y]
      if {[$C type $myPressedItem] eq "text"} {
        set myPressedItemColor [$C itemcget $myPressedItem -fill]
        $C itemconfigure $myPressedItem -fill red
      } else {
        set myPressedItemColor ""
      }
    }

    method drop {target drag insertAfter} {
      ::hv3::bookmarks::dropobject $target $drag $insertAfter \
          [info exists myOpenFolder([lindex $target 1])]
      ::hv3::bookmarks::refresh_gui
    }

    method release_event {x y} { 
      if {$myPressedItem eq ""} return

      set C ${win}.canvas
      if {[$C type $myPressedItem] eq "text"} {
        $C itemconfigure $myPressedItem -fill $myPressedItemColor
      }
      
      set x [$C canvasx $x]
      set y [$C canvasy $y]

      if {$myDragObject ne ""} {
        set item [lindex [
          $C find overlapping               \
              [expr {$x-300}] [expr {$y-3}] \
              [expr {$x+300}] [expr {$y+3}]
        ] 0]

        # We were dragging either a bookmark or a folder. Let's see what
        # we dropped it over:
        set dropid ""
        if {[info exists myTextId($item)]} {
          if {[lindex $myTextId($item) 0] ne "recent"} {
            set dropid [lrange $myTextId($item) 0 1]
          }
        } elseif {[info exists myIconId($item)]} {
          set dropid [lrange $myIconId($item) 0 1]
        }

        if {$dropid eq $myDragObject} {
          set dropid ""
        }
        if {$dropid eq "" && $y <= $myTreeStart} {
          set dropid [list folder 0]
        }

        ::hv3::sqlitedb transaction {
          if {$dropid ne "" && $myDragObject ne ""} {
            $self drop $dropid $myDragObject [expr $myDragY > $myPressedY]
          }
        
          $self populate_tree
        }
      } else {
        set item [lindex [$C find overlapping $x $y $x $y] 0]
        if {$item eq $myPressedItem} {
          $self click_event $item
        }
      }
   
      # Clear the DragObject and PressedItem variables. This has to
      # be in a [catch] block as the call to [click_event] above may
      # have destroyed the whole widget.
      catch {
        set myDragObject ""
        set myPressedItem ""
      }
    }

    method click_event {click} {
      set C ${win}.canvas

      if {$click eq ""} return
      set clicktype [$C type $click]
 
      if {$clicktype eq "text"} {
        foreach {type id msg} $myTextId($click) {}
        switch -exact -- $type {
          folder {
            if {![info exists myOpenFolder($id)]} {
              set myOpenFolder($id) 1
            }
            #$myController time populate_folder $id
            $myController goto "home://bookmarks/folder/$id"
          }
          bookmark {
            # Find the URI for this bookmark and send the browser there :)
            ::hv3::sqlitedb eval {
              SELECT uri, has_snapshot FROM bm_bookmark2 WHERE bookmarkid = $id
            } {
              if {$has_snapshot == 2} {
                $myController goto "home://bookmarks/snapshot/$id"
              } elseif {$uri ne ""} {
                $myBrowser goto $uri
              }
            }
          }
          recent {
            $myController goto "home://bookmarks/recent/$id"
          }
        }
      } elseif {$clicktype eq "image"} {
        foreach {type id} $myIconId($click) {}
        switch -exact -- $type {
          folder {
            if {[info exists myOpenFolder($id)]} {
              unset myOpenFolder($id)
            } else {
              set myOpenFolder($id) 1
            }
            $self populate_tree
          }
          bookmark {
            ::hv3::sqlitedb eval {
              UPDATE bm_bookmark2 SET has_snapshot = 
                CASE WHEN has_snapshot = 0 THEN 0 
                     WHEN has_snapshot = 1 THEN 2
                     ELSE 1 END
              WHERE bookmarkid = $id
            }
            $self populate_tree
          }
        }
      }
    }

    method expand_all {} {
      ::hv3::sqlitedb transaction {
        ::hv3::sqlitedb eval { SELECT folderid FROM bm_folder2 } {
          set myOpenFolder($folderid) 1
        }
        $self populate_tree
      }
    }
    method collapse_all {} {
      array unset myOpenFolder
      $self populate_tree
    }

    method drawSubTree {x y folderid {tags ""}} {
      set C ${win}.canvas

      set yincr [expr [font metrics Hv3DefaultFont -linespace] + 5]
      set x2 [expr {$x + [image width idir] + 5}]
      foreach {page pageid} [$myController current] {}

      ::hv3::sqlitedb eval {
        SELECT bookmarkid, caption, uri, has_snapshot
        FROM bm_tree2, bm_bookmark2
        WHERE 
          bm_tree2.folderid = $folderid AND
          bm_tree2.objecttype = 'b' AND
          bm_tree2.objectid = bm_bookmark2.bookmarkid
        ORDER BY bm_tree2.linkid
      } {
        set font Hv3DefaultFont
        if {$page eq "snapshot" && $pageid eq $bookmarkid} {
          set font Hv3DefaultBold
        }
        set t [concat "bookmark$bookmarkid" $tags]
        if {$caption eq ""} {set caption $uri}

        switch -exact -- $has_snapshot {
          0 { set image iworld }
          1 { set image iworldfile }
          2 { set image ifile }
        }
        set tid [$C create image $x $y -image $image -anchor sw -tags $t]
        set myIconId($tid) [list bookmark $bookmarkid]

        set tid [
          $C create text $x2 $y -text $caption -anchor sw -font $font -tags $t
        ]
        set myTextId($tid) [list bookmark $bookmarkid "hyper-link: $uri"]
        incr y $yincr
      }

      ::hv3::sqlitedb eval {
        SELECT 
          bm_folder2.folderid AS thisfolderid, 
          name
        FROM bm_tree2, bm_folder2
        WHERE 
          bm_tree2.folderid = $folderid AND
          bm_tree2.objecttype = 'f' AND
          bm_tree2.objectid = bm_folder2.folderid
        ORDER BY bm_tree2.linkid 
      } {
        set font Hv3DefaultFont
        if {$page eq "folder" && $pageid eq $thisfolderid} {
          set font Hv3DefaultBold
        }
        set t [concat "folder$thisfolderid" $tags]

        set image idir
        if {[info exists myOpenFolder($thisfolderid)]} {
          set image iopendir
        }

        set tid [$C create image $x $y -image $image -anchor sw -tags $t]
        set myIconId($tid) [list folder $thisfolderid]
        set tid [
          $C create text $x2 $y -text $name -anchor sw -font $font -tags $t
        ]
        set myTextId($tid) \
            [list folder $thisfolderid "Click to view folder \"$name\""]
        incr y $yincr
        if {[info exists myOpenFolder($thisfolderid)]} {
          set y [$self drawSubTree [expr $x + $yincr] $y $thisfolderid $t]
        }
      }

      return $y
    }
  }

  proc requestpage {zPath} {
    set components [lrange [split $zPath /] 1 end]

    set page   [lindex $components 0]
    set pageid [lindex $components 1]

    ::hv3::sqlitedb transaction {
      switch -exact -- $page {
        images   { return [idir data -format ppm] }
        recent   { request_recent $pageid }
        folder   { request_folder $pageid }
        search   { request_search $pageid }
        snapshot { request_snapshot $pageid }
        default {
          error [join "
            {Bad page: \"$page\"}
            {- should be recent, folder, bookmark or search}
          "]
        }
      }
    }
  }

  proc start_page {title} {
    set Template {
      <STYLE>
        :visited { color: darkblue; }
        .lastvisited, .uri, .description { 
          display: block ; padding-left: 10ex 
        }
        .time,.info {font-size: small; text-align: center; font-style: italic}
        .info { margin: 0 10px }

        a[bookmarks_page] { color: purple }

        .uri a[href] { font-size: small ; color: green }

        .snippet {
            margin: 0 10ex;
            font-size: small;
        }

        .subfolder {
            float:left;
            padding: 0;
            padding-left:50px;
            min-width:20ex;
            white-space:nowrap;
        }
        .subfolder a {
            display: list-item;
            list-style-image: url(home://bookmarks/images/idir);
        }

        .viewbookmark { float:right }
        .viewfolder   { width:90% ; margin: 5px auto }
        .delete,.rename { float:right }

        .edit { width: 90% }

        a[href]:hover  { text-decoration: none; }
        a[href]:active { color: red; }

        h1 { font-size: 1.4em; }
      </STYLE>
    }
    return "$Template<H1>$title</H1>"
  }

  proc pp_bookmark_record {N id caption uri description has_snapshot snippet} {
    if {$caption eq ""} {set caption $uri}
    if {$has_snapshot > 0} {
      set A_attr "
        href=\"snapshot of $uri\" bookmarks_page=\"snapshot $id\"
      "
    } else {
      set A_attr "href=\"$uri\""
    }
    set applet ""
    if {$id != 0} {
      set applet "
        <APPLET 
          style=\"float:right\"
          class=bookmarks_button
          text=\"Edit Bookmark...\"
          command=\"::hv3::bookmarks::edit_bookmark $id\"
        ></APPLET>
      "
    }
    subst -nocommands {
      <DIV style="margin:1em">
        <DIV>
          <SPAN style="white-space:nowrap">
            ${N}. <A $A_attr>$caption</A>
          </SPAN>
          $applet
        </DIV>
        <DIV class="snippet">
          $snippet
        </DIV>
        <DIV class="description">$description</DIV>
        <SPAN class="uri"><A href="$uri">$uri</A></SPAN>
      </DIV>
    }
  }

  proc request_recent {nLimit} {
    set zRes "<APPLET bookmarks_page=\"recent $nLimit\"></APPLET>"

    append zRes [start_page "Recently Visited URIs"]

    set sql { 
      SELECT uri, title, lastvisited 
      FROM visiteddb 
      WHERE title IS NOT NULL
      ORDER BY oid DESC 
      LIMIT 200
    }
    set N 0
    ::hv3::sqlitedb eval { 
      SELECT uri, title, lastvisited 
      FROM visiteddb 
      WHERE title IS NOT NULL
      ORDER BY oid DESC 
      LIMIT 200
    } {
      incr N
      set description "Last visited: [clock format $lastvisited]"
      append zRes [pp_bookmark_record $N 0 $title $uri $description 0 ""]
    }
    return $zRes
  }

  proc html_to_text {zHtml} {
    .tmphv3 reset
    .tmphv3 parse -final $zHtml
    return [.tmphv3 html text text]
  }

  proc rebuild_fts_database {} {
    html .tmphv3
    ::hv3::sqlitedb transaction {
      ::hv3::sqlitedb eval {
        DELETE FROM bm_fulltext2;
        INSERT INTO bm_fulltext2(docid, caption, snapshot) 
          SELECT bookmarkid, caption, html_to_text(snapshot)
          FROM bm_bookmark2;
        DROP TABLE IF EXISTS bm_rebuild_fulltext2;
      }
      set ::hv3::bookmarks::fts3_warning {}
    }
    destroy .tmphv3
    refresh_gui
  }

  proc request_search {search} {
    set zRes "<APPLET bookmarks_page=\"search {$search}\"></APPLET>"
    append zRes [start_page "Search for $search"]

    append zRes $::hv3::bookmarks::fts3_warning

    if {$::hv3::have_fts3} {
      set sql {
        SELECT 
          bookmarkid, 
          bm_bookmark2.caption AS caption,
          bm_bookmark2.uri AS uri,
          bm_bookmark2.description AS description,
          bm_bookmark2.has_snapshot AS has_snapshot,
          snippet(bm_fulltext2) AS snippet
        FROM bm_fulltext2, bm_bookmark2
        WHERE 
          bm_fulltext2 MATCH $search AND 
          bm_fulltext2.docid = bookmarkid
        ORDER BY result_transform(offsets(bm_fulltext2)) DESC
        LIMIT 500
      }
    } else {
      set like "%${search}%"
      set sql {
        SELECT 
          bookmarkid, 
          bm_bookmark2.caption AS caption,
          bm_bookmark2.uri AS uri,
          bm_bookmark2.description AS description,
          bm_bookmark2.has_snapshot AS has_snapshot,
          '' AS snippet
        FROM bm_bookmark2
        WHERE 
          caption     LIKE $like OR
          description LIKE $like OR
          uri         LIKE $like
        ORDER BY ((caption LIKE $like) * 100 + (description LIKE $like)) DESC
        LIMIT 500
      }
    }

    set N 0
    ::hv3::sqlitedb eval $sql {
      incr N
      append zRes [pp_bookmark_record \
          $N $bookmarkid $caption $uri $description $has_snapshot $snippet
      ]
    }
    return $zRes
  }

  proc request_snapshot {bookmarkid} {
    set zRes "<APPLET bookmarks_page=\"snapshot $bookmarkid\"></APPLET>"
    append zRes [::hv3::sqlitedb one {
      SELECT snapshot FROM bm_bookmark2 WHERE bookmarkid = $bookmarkid
    }]
    return $zRes
  }

  proc request_folder {folderid} {

    set zRes "<APPLET bookmarks_page=\"folder $folderid\"></APPLET>"

    if {$folderid > 0} {
      set name [::hv3::sqlitedb one {
        SELECT name FROM bm_folder2 WHERE folderid = $folderid
      }]
      append zRes [start_page "$name
        <APPLET 
          style=\"float:right\"
          class=bookmarks_button
          text=\"Rename Folder...\"
          command=\"::hv3::bookmarks::rename_folder $folderid\"
        ></APPLET>
      "]
    } else {
      if {$folderid == 0} {
        append zRes [start_page "Bookmarks"]
      }
      if {$folderid == -1} {
        append zRes [start_page "Trash
          <APPLET 
            style=\"float:right\"
            class=bookmarks_button
            text=\"Permanently delete trashcan contents\"
            command=\"::hv3::bookmarks::delete_folder_contents $folderid\"
          ></APPLET>
        "]
      }
    }
 
    append zRes $::hv3::bookmarks::fts3_warning

    set N 0
    ::hv3::sqlitedb eval {
      SELECT bookmarkid, caption, uri, description, image, has_snapshot
      FROM bm_tree2, bm_bookmark2
      WHERE 
        bm_tree2.folderid = $folderid AND
        bm_tree2.objecttype = 'b' AND
        bm_tree2.objectid = bm_bookmark2.bookmarkid
      ORDER BY(bm_tree2.linkid)
    } {
      incr N
      set snippet ""
      append zRes [pp_bookmark_record \
          $N $bookmarkid $caption $uri $description $has_snapshot $snippet
      ]
    }
    append zRes <HR>
    ::hv3::sqlitedb eval {
      SELECT linkid, folderid AS thisfolderid, '.. (parent folder)' AS name
      FROM bm_tree2 
      WHERE objecttype = 'f' AND objectid = $folderid

      UNION ALL

      SELECT linkid, bm_folder2.folderid AS thisfolderid, name
      FROM bm_tree2, bm_folder2
      WHERE 
        bm_tree2.folderid = $folderid AND
        bm_tree2.objecttype = 'f' AND
        bm_tree2.objectid = bm_folder2.folderid
      ORDER BY 1
    } {
      append zRes [subst -nocommands {
          <DIV class=subfolder>
            <A href="." bookmarks_page="folder $thisfolderid">$name</A>
          </DIV>
      }]
      set foldername($thisfolderid) $name
    }
    return $zRes
  }


  ::snit::widget controller {
    variable myHv3
    variable myBrowser
    variable myTree

    variable myPage folder
    variable myPageId 0

    delegate option -width to hull
    delegate option -height to hull

    method goto {args} {eval $myHv3 goto $args}
    method refresh {} {
      $myHv3 seek_to_yview [lindex [$myHv3 yview] 0]
      $myHv3 goto "" -nosave -cachecontrol "no-cache"
    }

    method applet {node} {
      set bookmarks_page [$node attr -default "" bookmarks_page]
      if {$bookmarks_page ne ""} {
        eval $self set_page_id $bookmarks_page
      }

      set class [$node attr -default "" class]
      if {$class eq "bookmarks_button"} {
        set w [$myHv3 html].document.button_[string map {: _} $node]
        ::hv3::button $w -text [$node attr text] -command [$node attr command]
        $node replace $w -deletecmd [list destroy $w]
      }
    }

    constructor {browser hv3 tree} {
      set myHv3 $hv3
      set myTree $tree
      set myBrowser $browser

      $myHv3 html handler node applet [mymethod applet]

      set searchcmd [subst -nocommands {
          $myHv3 goto "home://bookmarks/search/[${win}.filter get]"
      }]

      ::hv3::label ${win}.filter_label -text "Search: " -background white
      ::hv3::entry  ${win}.filter
      ::hv3::button ${win}.go -text Go -command $searchcmd
  
      pack ${win}.filter_label -side left -padx 5 -pady 10
      pack ${win}.filter -side left
      pack ${win}.go -side left

      bind ${win}.filter <KeyPress-Return> $searchcmd

      $hv3 configure -targetcmd [list $self TargetCmd]

      $hull configure -background white
    }

    method TargetCmd {node} {
      set bookmarks_page [$node attr -default "" bookmarks_page]
 
      if {$myPage eq "snapshot" && [$node tag] eq "a"} {
        set obj [::tkhtml::uri [$myHv3 resolve_uri [$node attr href]]]
        set uri [$obj get_no_fragment]

        set bookmarkid [::hv3::sqlitedb one {
          SELECT bookmarkid FROM bm_bookmark2 
          WHERE uri = $uri AND has_snapshot
          ORDER BY bookmarkid DESC
        }]
        set fragment [$obj fragment]
        $obj destroy
        if {$bookmarkid ne ""} {
          set internal_uri "home://bookmarks/snapshot/$bookmarkid"
          if {$fragment ne ""} {
            append internal_uri #$fragment
          }
          $myHv3 goto $internal_uri
          return ::hv3::bookmarks::noop
        }
      }
      
      if {$bookmarks_page ne ""} {
        $myHv3 goto "home://bookmarks/[join $bookmarks_page /]"
        return ::hv3::bookmarks::noop
      }
      return [$myBrowser hv3]
    }

    method time {args} {
      set t [time {eval $self $args}]
      $myHv3 parse "
        <p class=time>
          Page generated in [lrange $t 0 1]
        </p>
      "
    }

    method start_page {title} {
      $myHv3 reset 0
      $myHv3 parse {
        <STYLE>
          :visited { color: darkblue; }
          .lastvisited, .uri, .description { 
            display: block ; padding-left: 10ex 
          }
          .time,.info {font-size: small; text-align: center; font-style: italic}
          .info { margin: 0 10px }

          a[bookmarks_page] { color: purple }

          .uri a[href] { font-size: small ; color: green }

          .snippet {
              margin: 0 10ex;
              font-size: small;
          }

          .viewbookmark { float:right }
          .viewfolder   { width:90% ; margin: 5px auto }
          .delete,.rename { float:right }

          .edit { width: 90% }

          a[href]:hover  { text-decoration: none; }
          a[href]:active { color: red; }

          h1 { font-size: 1.4em; }
        </STYLE>
      }
      $myHv3 parse "<H1>$title</H1>"
    }

    proc configurecmd {win args} {
      set descent [font metrics [$win cget -font] -descent]
      set ascent  [font metrics [$win cget -font] -ascent]
      expr {([winfo reqheight $win] + $descent - $ascent) / 2}
    }

    method set_page_id {page pageid} { 
      set myPage $page
      set myPageId $pageid
      $myTree populate_tree 1
    }
    method sub_page_id {pageid} {
      set myPageId $pageid
    }
    method current {} { list $myPage $myPageId }
  }

  proc db_delete_folder_contents {folderid} {
    ::hv3::sqlitedb eval {
      SELECT objectid AS thisfolderid 
      FROM bm_tree2 
      WHERE folderid = $folderid AND objecttype = 'f'
    } {
      db_delete_folder_contents $thisfolderid
    }

    if {$::hv3::have_fts3} {
      ::hv3::sqlitedb eval {
        DELETE FROM bm_fulltext2 WHERE rowid IN (
          SELECT objectid FROM bm_tree2 
          WHERE folderid = $folderid AND objecttype = 'b'
        );
      }
    } else {
      catch { ::hv3::sqlitedb eval {CREATE TABLE bm_rebuild_fulltext2(a)} }
    }

    ::hv3::sqlitedb eval {
      DELETE FROM bm_folder2 WHERE folderid IN (
        SELECT objectid FROM bm_tree2 
        WHERE folderid = $folderid AND objecttype = 'f'
      );
      DELETE FROM bm_bookmark2 WHERE bookmarkid IN (
        SELECT objectid FROM bm_tree2 
        WHERE folderid = $folderid AND objecttype = 'b'
      );
      DELETE FROM bm_tree2 WHERE folderid = $folderid
    }
  }
  proc delete_folder_contents {folderid} {
    ::hv3::sqlitedb transaction {
      db_delete_folder_contents $folderid
    }
    refresh_gui
  }

  #
  # Each match is worth a number of points based on whether it was 
  # found in the caption (0), description (1) or snapshot (2) column.
  # Matches with large scores are displayed before those with smaller
  # scores.
  #
  #    Caption:     1,000,000 points per match
  #    Description: 1,000 points per match
  #    Snapshot:    1 point per match
  #
  proc result_transform {offsets} {
    set iScore 0
    set INCR(0) 1000000
    set INCR(1) 1000
    set INCR(2) 1
    foreach {a b c d} $offsets {
      incr iScore $INCR($a)
    }
    return $iScore
  }

  proc store_new_folder {treewidget} {
    set name [.new.entry get]

    destroy .new

    ::hv3::sqlitedb transaction {
      ::hv3::sqlitedb eval { INSERT INTO bm_folder2(name) VALUES($name) }
      set object [list folder [::hv3::sqlitedb last_insert_rowid]]
      ::hv3::bookmarks::dropobject [list folder 0] $object 0 1
    }
    refresh_gui
  }

  proc store_rename_folder {folderid} {
    set name [.new.entry get]
    destroy .new
    ::hv3::sqlitedb eval {
      UPDATE bm_folder2 SET name = $name WHERE folderid = $folderid
    }
    refresh_gui
  }

  proc refresh_gui {} {
    foreach obj [::hv3::bookmarks::treewidget info instances] {
      $obj populate_tree
    }
    foreach obj [::hv3::bookmarks::controller info instances] {
      $obj refresh
    }
  }

  proc db_store_new_folder {iFolder zName} {
    ::hv3::sqlitedb eval {
      INSERT INTO bm_folder2(name) VALUES($zName);
    }
    set folderid [::hv3::sqlitedb last_insert_rowid]
    ::hv3::sqlitedb eval {
      INSERT INTO bm_tree2(folderid, objecttype, objectid)
      VALUES($iFolder, 'f', $folderid)
    }
    return $folderid
  }

  proc db_store_new_bookmark {
      iFolder zCaption zUri zDescription iSnapshot zSnapshot zSnapshotText
  } {
    # To store a new bookmark, we need to store a row in each of the
    # following tables:
    #
    #     + bm_bookmark2               (primary bookmark record)
    #     + bm_tree2                   (link the bookmark into the tree)
    #     + bm_fulltext2               (if available - for searching)
    #

    ::hv3::sqlitedb eval {
      INSERT INTO bm_bookmark2
      (caption, uri, description, has_snapshot, snapshot) 
      VALUES($zCaption, $zUri, $zDescription, $iSnapshot, $zSnapshot)
    }

    set bookmarkid [::hv3::sqlitedb last_insert_rowid]
    ::hv3::sqlitedb eval {
      INSERT INTO bm_tree2(folderid, objecttype, objectid)
      VALUES($iFolder, 'b', $bookmarkid)
    }

    if {$::hv3::have_fts3} {
      ::hv3::sqlitedb eval {
        INSERT INTO bm_fulltext2(rowid, caption, description, snapshot)
        VALUES($bookmarkid, $zCaption, $zDescription, $zSnapshotText)
      }
    } else {
      catch { ::hv3::sqlitedb eval {CREATE TABLE bm_rebuild_fulltext2(a)} }
    }

    return $bookmarkid
  }

  set save_snapshot_variable 1
  proc store_new_bookmark {hv3} {
    set caption [.new.caption get]
    set uri [.new.uri get]
    set desc [.new.desc get 0.0 end]
    destroy .new

    ::hv3::sqlitedb transaction {
      set has_snapshot 0
      set snapshot ""
      set snapshot_text ""
      if {$::hv3::bookmarks::save_snapshot_variable && $hv3 ne ""} {
        set has_snapshot 1
        set snapshot [create_snapshot $hv3]
        set snapshot_text [$hv3 html text text]
      }
      set bookmarkid [
        db_store_new_bookmark 0 $caption $uri $desc $has_snapshot $snapshot $snapshot_text
      ]
      set object [list bookmark $bookmarkid]
      ::hv3::bookmarks::dropobject [list folder 0] $object 0 1
    }

    refresh_gui
  }

  proc store_edit_bookmark {bookmarkid} {
    set caption     [.new.caption get]
    set uri         [.new.uri get]
    set description [.new.desc get 0.0 end]

    ::hv3::sqlitedb transaction {
      ::hv3::sqlitedb eval {
        UPDATE bm_bookmark2 
        SET caption = $caption, uri = $uri, description = $description
        WHERE bookmarkid = $bookmarkid
      }
      if {$::hv3::have_fts3} {
        ::hv3::sqlitedb eval {
          UPDATE bm_fulltext2 
          SET caption = $caption, description = $description
          WHERE rowid = $bookmarkid
        } 
      } else {
        catch { ::hv3::sqlitedb eval {CREATE TABLE bm_rebuild_fulltext2(a)} }
      }
    }
    destroy .new

    refresh_gui
  }

  proc new_folder {treewidget} {
    toplevel .new

    ::hv3::label .new.label -text "Create New Bookmarks Folder: " -anchor s
    ::hv3::entry .new.entry -width 60
    ::hv3::button .new.save                            \
        -text "Save"                                   \
        -command [list ::hv3::bookmarks::store_new_folder $treewidget]
    ::hv3::button .new.cancel -text "Cancel" -command {destroy .new}

    grid .new.label -columnspan 2 -sticky ew
    grid .new.entry -columnspan 2 -sticky ew -padx 5
    grid .new.cancel .new.save -pady 5 -padx 5 -sticky e

    grid columnconfigure .new 0 -weight 1

    bind .new.entry <KeyPress-Return> {.new.save invoke}
    .new.entry insert 0 "New Folder"
    .new.entry selection range 0 end
    focus .new.entry

    launch_dialog .new
    tkwait window .new
  }

  proc rename_folder {folderid} {
    toplevel .new

    set name [::hv3::sqlitedb one {
      SELECT name FROM bm_folder2 WHERE folderid = $folderid
    }]

    ::hv3::label .new.label -text "Rename Folder \"$name\" To: " -anchor s
    ::hv3::entry .new.entry -width 60
    set cmd [list ::hv3::bookmarks::store_rename_folder $folderid]
    ::hv3::button .new.save -text "Save" -command $cmd
    ::hv3::button .new.cancel -text "Cancel" -command {destroy .new}

    grid .new.label -columnspan 2 -sticky ew
    grid .new.entry -columnspan 2 -sticky ew -padx 5
    grid .new.cancel .new.save -pady 5 -padx 5 -sticky e

    grid columnconfigure .new 0 -weight 1

    bind .new.entry <KeyPress-Return> {.new.save invoke}
    .new.entry insert 0 $name
    .new.entry selection range 0 end
    focus .new.entry

    launch_dialog .new
    tkwait window .new
  }

  proc create_bookmark_dialog {isCreate} {
    toplevel .new

    if {$isCreate} {
      ::hv3::label .new.label -text "Create New Bookmark: " -anchor s
    } else {
      ::hv3::label .new.label -text "Edit Bookmark: " -anchor s
    }
    ::hv3::entry .new.caption -width 40
    ::hv3::entry .new.uri -width 40
    ::hv3::text .new.desc -height 10 -background white -borderwidth 1 -width 40

    ::hv3::label .new.l_caption -text "Caption: "
    ::hv3::label .new.l_uri -text "Uri: "
    ::hv3::label .new.l_desc -text "Description: "

    ::hv3::button .new.save -text "Save"
    ::hv3::button .new.cancel -text "Cancel" -command {destroy .new}

    grid .new.label -columnspan 4

    grid .new.l_caption .new.caption -pady 5 -padx 5
    grid .new.l_uri     .new.uri -pady 5 -padx 5
    grid .new.l_desc    .new.desc -pady 5 -padx 5

    grid configure .new.caption -columnspan 3 -sticky ew
    grid configure .new.uri -columnspan 3 -sticky ew 
    grid configure .new.desc -columnspan 3 -sticky ewns

    grid configure .new.l_caption -sticky e
    grid configure .new.l_uri -sticky e
    grid configure .new.l_desc -sticky e

    if {$isCreate<2} {
      checkbutton .new.snapshot \
          -text "Save website text in database"                   \
          -variable ::hv3::bookmarks::save_snapshot_variable
      grid .new.snapshot -column 1 -padx 5 -pady 5 -sticky w
    }

    grid .new.save .new.cancel -pady 5 -padx 5 -sticky e
    grid configure .new.save -column 3
    grid configure .new.cancel -column 2

    grid columnconfigure .new 1 -weight 1
    grid rowconfigure .new 3 -weight 1

    bind .new.caption <KeyPress-Return> {.new.save invoke}
    bind .new.uri     <KeyPress-Return> {.new.save invoke}
  }

  proc new_bookmark {hv3} {
    ::hv3::bookmarks::ensure_initialized
    set dialog_version [expr {($hv3 eq "") ? 2 : 1}]

    create_bookmark_dialog $dialog_version
    .new.save configure \
        -command [list ::hv3::bookmarks::store_new_bookmark $hv3]

    focus .new.caption
    if {$hv3 ne ""} {
      .new.caption insert 0 [$hv3 title]
      .new.uri insert 0     [$hv3 uri get]
      .new.caption selection range 0 end
    }

    launch_dialog .new
    tkwait window .new
  }

  proc edit_bookmark {bookmarkid} {
    create_bookmark_dialog 0
    set cmd [list ::hv3::bookmarks::store_edit_bookmark $bookmarkid]
    .new.save configure -command $cmd

    focus .new.caption
    ::hv3::sqlitedb eval {
      SELECT caption, uri, description 
      FROM bm_bookmark2 
      WHERE bookmarkid = $bookmarkid
    } {}
    .new.caption insert 0 $caption
    .new.uri insert 0     $uri
    .new.desc insert 0.0     $description
    .new.caption selection range 0 end

    launch_dialog .new
    tkwait window .new
  }

  proc launch_dialog {dialog} {
    wm withdraw $dialog
    update idletasks

    set w [winfo reqwidth $dialog]
    set h [winfo reqheight $dialog]
    scan [wm geometry [winfo parent $dialog]] "%dx%d+%d+%d" pw ph px py
    set geom "+[expr $px + $pw/2 - $w/2]+[expr $py + $ph/2 - $h/2]"
    wm geometry $dialog $geom

    wm deiconify $dialog

    grab $dialog
    wm transient $dialog .
    # wm protocol .new WM_DELETE_WINDOW { grab release .new ; destroy .new }
    raise $dialog
  }

  # Notes on the mozilla bookmarks.html format:
  #
  #     * Each folder is a <DL> element.
  #     * Each bookmark is an <A> element.
  #     * Folder names are stored in <H3> elements.
  #
  proc import_bookmarks_file {zFile} {

    set fd [open $zFile]
    set zBookmarks [read $fd]
    close $fd

    set H [html .boomarksparser]
    $H parse -final $zBookmarks

    unset -nocomplain zTitle
    foreach N [$H search dl,h3] {

      if {[$N tag] eq "dl"} {
        if {![info exists zTitle]} {
          set zTitle "Imported Bookmarks"
        }

        # Create the bm_folder2 record.
        ::hv3::sqlitedb eval {
          INSERT INTO bm_folder2(name) VALUES($zTitle);
        }
        set iFolderId [::hv3::sqlitedb last_insert_rowid]
        set imported_folders($N) $iFolderId

        set iParentId 0
        for {set P [$N parent]} {$P ne ""} {set P [$P parent]} {
          if {[$P tag] eq "dl"} {
            set iParentId $imported_folders($P)
            break
          }
        }

        # Create the bm_tree2 record.
        ::hv3::sqlitedb eval {
          INSERT INTO bm_tree2(folderid, objecttype, objectid) 
          VALUES($iParentId, 'f', $iFolderId)
        }

      } else {
        set zTitle [[lindex [$N children] 0] text]
      }
    }

    foreach N [$H search {dl a[href]}] {
      set zCaption [[lindex [$N children] 0] text]
      set zUri [$N attribute href]

      for {set P [$N parent]} {$P ne ""} {set P [$P parent]} {
        if {[$P tag] eq "dl"} {
          set iParentId $imported_folders($P)
          break
        }
      }

      ::hv3::bookmarks::db_store_new_bookmark \
          $iParentId $zCaption $zUri "" 0 "" ""
    }

    destroy $H
  }

  proc import_bookmarks_dir {zDir} {
    if {[file exists [file join $zDir bookmarks.html]]} {
      import_bookmarks_file [file join $zDir bookmarks.html]
    }
    foreach dir [glob -nocomplain -directory ${zDir} -type d *] {
      import_bookmarks_dir $dir
    }
  }

  proc import_bookmarks {} {
    import_bookmarks_dir [file join $::env(HOME) .mozilla]

    foreach obj [::hv3::bookmarks::treewidget info instances] {
      $obj populate_tree
    }
  }

  proc import_bookmarks_manual {} {
    set zFile [tk_getOpenFile]
    if {$zFile ne ""} {
      import_bookmarks_file $zFile
      foreach obj [::hv3::bookmarks::treewidget info instances] {
        $obj populate_tree
      }
    }
  }

  proc import_tree {{zDirname ""}} {
    if {$zDirname eq ""} {
      set zDirname [tk_chooseDirectory]
    }
    if {$zDirname eq ""} return

    setup_tmphv3

    toplevel .import
    ::hv3::label .import.label -width 60 -anchor w
    ::hv3::button .import.cancel \
        -command [list destroy .import] -text "Cancel Import"
    pack .import.label -fill both -expand 1 -side top
    pack .import.cancel -fill x -expand 1

    launch_dialog .import
    set ::hv3::bookmarks::files_to_import [count_html_files $zDirname]
    set ::hv3::bookmarks::files_imported 0

    set rc [catch {::hv3::sqlitedb transaction {
      set iFolder [db_store_new_folder 0 "Imported from $zDirname"]
      import_dir $iFolder $zDirname
    }} msg]

    destroy .tmphv3
    tmpprotocol destroy
    destroy .import

    if {$rc == 0} {
      foreach obj [::hv3::bookmarks::treewidget info instances] {
        $obj populate_tree
      }
    } else {
      error $msg
    }
  }

  set files_to_import 0
  set files_imported 0

  proc count_html_files {dir} {
    if {![winfo exists .import.label]} {
      error "Operation cancelled by user"
    }
    .import.label configure -text "Searching for html files in $dir"
    update
    set iRes [llength [
      glob -nocomplain -directory $dir *.html *.htm *.HTML *.HTM
    ]]
    foreach d [glob -nocomplain -type d -directory $dir *] {
      incr iRes [count_html_files $d]
    }
    set iRes
  }

  proc import_local_uri {iFolder zUri} {
    set zCaption $zUri
    set zDesc ""
    set zSnapshot ""

    set hv3 .tmphv3
    $hv3 reset 0
    $hv3 goto $zUri
    set zSnapshot [create_snapshot $hv3]
   
    set titlenode [$hv3 html search title]
    if {$titlenode ne "" && [llength [$titlenode children]]>0} {
      set zCaption [[lindex [$titlenode children] 0] text]
    }

    set text [$hv3 html text text]
    db_store_new_bookmark $iFolder $zCaption $zUri $zDesc 2 $zSnapshot $text
  }

  proc import_dir {iFolder dir} {
    set obj [::tkhtml::uri file://]
    set hv3 .tmphv3

    set flist [glob -nocomplain -directory $dir *.html *.htm *.HTML *.HTM]
    foreach f $flist {
      if {![winfo exists .import.label]} {
        error "Operation cancelled by user"
      }
      incr ::hv3::bookmarks::files_imported
      set    T "Importing file ${::hv3::bookmarks::files_imported}/"
      append T "${::hv3::bookmarks::files_to_import} ($f)"

      .import.label configure -text $T
      update

      set zUri [$obj resolve $f]
      import_local_uri $iFolder $zUri
    }
    $obj destroy

    foreach f [glob -nocomplain -directory $dir *] {
      if {[file isdirectory $f]} {
        set iSubFolder [db_store_new_folder $iFolder [file tail $f]]
        import_dir $iSubFolder $f
      }
    }
  }

  proc import_dom {} {
    setup_tmphv3
    ::hv3::sqlitedb transaction {
      set iFolder      [db_store_new_folder 0 "Hv3 DOM Reference"]
      set iTreeFolder  [db_store_new_folder $iFolder "DOM Tree Objects"]
      set iEventFolder [db_store_new_folder $iFolder "DOM Event Objects"]
      set iContFolder  [db_store_new_folder $iFolder "DOM Container Objects"]
      set iStyleFolder [db_store_new_folder $iFolder "DOM Style Objects"]
      set iXMLFolder   [db_store_new_folder $iFolder "XMLHttpRequest Objects"]
      set iNSFolder    [db_store_new_folder $iFolder "Compatibility Objects"]
      set iElemFolder  [
        db_store_new_folder $iTreeFolder "HTMLElement Sub-classes"
      ]
      
      #set fname [file normalize [file join $::hv3::scriptdir dom_events.html]]
      #import_local_uri $iFolder file://$fname

      if {[llength [info commands ::hv3::DOM::docs::HTMLElement]] == 0} {
        ::hv3::dom_init 1
      }

      foreach cmd [lsort [info commands ::hv3::DOM::docs::*]] {
        set localcmd [string range $cmd [expr {[string last : $cmd]+1}] end]
        set folder $iFolder

        if {[string match HTML?*Element $localcmd]} {
          set folder $iElemFolder
        } elseif { [string match *XML* $localcmd] } {
          set folder $iXMLFolder
        } elseif { [string match *Event* $localcmd] } {
          set folder $iEventFolder
        } elseif { [lsearch \
          {History Location Screen Window Navigator FramesList} $localcmd]>=0 
        } {
          set folder $iNSFolder
        } elseif { [lsearch \
          {HTMLDocument HTMLElement NodePrototype Text} $localcmd]>=0 
        } {
          set folder $iTreeFolder
        } elseif { [lsearch \
          {NodeListC NodeListS HTMLCollectionC HTMLCollectionS} $localcmd]>=0 
        } {
          set folder $iContFolder
        } elseif { [lsearch {CSSStyleDeclaration} $localcmd]>=0 } {
          set folder $iStyleFolder
        }

        import_local_uri $folder home://dom/$localcmd
      }
    }
    destroy .tmphv3
    tmpprotocol destroy
    refresh_gui
  }

  proc setup_tmphv3 {} {
    set hv3      [::hv3::hv3 .tmphv3]
    set protocol [::hv3::protocol ::tmpprotocol]
    ::hv3::home_scheme_init $hv3 $protocol
    $hv3 html handler node object ""
    $hv3 html handler node frameset ""
    $hv3 html handler node embed ""
    $hv3 configure -requestcmd [list $protocol requestcmd]
  }

}