This file is indexed.

/usr/lib/python2.7/dist-packages/sagenb/data/sage/js/notebook_lib.js is in python-sagenb 1.0.1+ds1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
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
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
/*global $, alert, async_request, clearTimeout, confirm, document, escape, location, navigator, open, prompt, setTimeout, window, worksheet_filenames */
/*jslint maxerr: 10000, white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
//"use strict";

// Code and docstring conventions.  Please use 4-space indentation
// throughout.  JSLint (http://www.jslint.com/) on the "The Good
// Parts" setting can uncover potential problems.

//function foo(arg1, arg2) {
    /*
    Description.

    INPUT:
        arg1 -- description of arg1
        arg2 -- description of arg2
    GLOBAL INPUT / OUTPUT:
        glob1 -- how we use and/or change this
        glob2 -- how we use and/or change this
    OUTPUT:
        Returned variable(s).
    */
    /*
    // Declare variables at the top, since JS vars don't have block scope.
    var a, b, X;
    for (a = 0; a < arg1.length; a++) {
        // This is a comment.
        arg1[a] *= glob1;
    }
    // Here's another comment.
    return [arg2, arg1];
    */
//}


///////////////////////////////////////////////////////////////////
//
// GLOBAL VARIABLES
//
// PLEASE define all global variables up here, and if you want to set
// them with anything but simple assignment, 'var' them first, and set
// them later.  Your code might work in your browser, but it might
// break initial setup for other critical pieces in other browsers.
// Thanks. (and for the record, I'm guilty of this more than anybody
// else here -- I figure a big block comment might help keep me in
// check)
//
// Exception: keyboard globals are defined at the end.
//
///////////////////////////////////////////////////////////////////

// Cell lists, maps, and cache.
var cell_id_list = [];
var queue_id_list = [];
var onload_id_list = [];
var cell_element_cache = {};

// Worksheet information from worksheet.py
var worksheet_locked;
var original_title = document.title;
var state_number = -1;
// Current worksheet info, set in notebook.py.
var worksheet_filename = '';
var worksheet_name = '';
var user_name = '';

// Ping the server periodically for worksheet updates.
var server_ping_time = 10000;

// Interact constants.  See interact.py and related files.
// Present in wrapped output, forces re-evaluation of ambient cell.
var INTERACT_RESTART = '__SAGE_INTERACT_RESTART__';
// Delimit updated markup.
var INTERACT_START = '<?__SAGE__START>';
var INTERACT_END = '<?__SAGE__END>';

// Browser & OS identification.
var browser_op, browser_saf, browser_konq, browser_moz, browser_ie, browser_iphone;
var os_mac, os_lin, os_win;

// Functions assigned during keyboard setup.
var input_keypress;
var input_keydown;
// Bug workaround.
var skip_keyup = false;

// Interrupts.
var interrupt_state = {count: 0};

// Focus / blur.
var current_cell = -1;
var cell_has_changed = false;

// Resizing too often significantly affects performance.
var keypress_resize_delay = 250;
var last_keypress_resize = 0;
var will_resize_soon = false;
var previous = {};

// Are we're splitting a cell and evaluating it?
var doing_split_eval = false;
// Whether the the next call to jump_to_cell is ignored.  Used to
// avoid changing focus.
var ignore_next_jump = false;
// Set to true for pages with public interacts.
var ignore_all_jumps = false;
var control_key_pressed = 0;
var evaluating_all = false;

// Cell update check variables.  Times are in milliseconds.
var update_timeout = -1;
var updating = false;
var update_time = -1;
var update_count = 0;
var update_falloff_threshold = 20;
var update_falloff_level = 0;
var update_falloff_deltas = [250, 500, 1000, 5000];
var update_error_count = 0;
var update_error_threshold = 30;
var update_error_delta = 1024;
var update_normal_delta = update_falloff_deltas[0];
var cell_output_delta = update_normal_delta;

// Introspection data.
var introspect = {};

// Regular expressions to parse cell input for introspection.
// Characters that don't belong in a variable name.
var non_word = "[^a-zA-Z0-9_]";
// The command at the end of a string.
var command_pat = "([a-zA-Z_][a-zA-Z._0-9]*)$";
var function_pat = "([a-zA-Z_][a-zA-Z._0-9]*)\\([^()]*$";
var one_word_pat = "([a-zA-Z_][a-zA-Z._0-9]*)";
var unindent_pat = "^\\s{0,4}(.*)$";
// The # doesn't need a slash for now, but let's give it one anyway...
var uncomment_pat = "^([^\\#]*)\\#{0,1}(.*)$";
var whitespace_pat = "(\\s*)";

try {
    non_word = new RegExp(non_word);
    command_pat = new RegExp(command_pat);
    function_pat = new RegExp(function_pat);
    one_word_pat = new RegExp(one_word_pat);
    whitespace_pat = new RegExp(whitespace_pat);
    unindent_pat = new RegExp(unindent_pat);
    uncomment_pat = new RegExp(uncomment_pat);
} catch (e) {}

// The global cell_writer target.
var cell_writer = document;

// Slideshow mode?
var in_slide_mode = false;
// Does the current slide have the hidden input class?
var slide_hidden = false;

var title_spinner_i = 0;
var title_spinner = ['/ ', '\\ '];
//var title_spinner = ['    ', '.   ', '..  ', '... '];
//var title_spinner = ['[ ] ', '[.] ', '[:] ', '[.] '];
//var title_spinner = ['S ', 'SA ', 'SAG ', 'SAGE '];
//var title_spinner = ['[   ] ', '[.  ] ', '[.. ] ', '[...] '];
//var title_spinner = ['[-] ','[/] ','[|] ','[\\] '];

var modal_prompt_element =
    '<div class="modal-prompt" style="display: none;">' +
    '    <form>' +
    '        <div class="message"></div>' +
    '        <div class="field">' +
    '            <input type="text" />' +
    '        </div>' +
    '        <div class="button-div">' +
    '            <button type="submit">OK</submit>' +
    '        </div>' +
    '    </form>' +
    '</div>';


///////////////////////////////////////////////////////////////////
// Cross-Browser Stuff
///////////////////////////////////////////////////////////////////
function toint(x) {
    /*
    Convert a object to an integer, if it's possible.  We use this to
    convert a cell id to an integer if it's just a string
    representation of that integer.  Otherwise, we return the original
    id.  This allows us to use alphanumeric ids for special cells.

    INPUT:
        x -- any object, e.g., a string, integer, float, etc.
    OUTPUT:
        an integer or the object
     */
    if (x === '0') {
        return 0;
    } else {
        return parseInt(x, 10) || x;
    }
}


function decode_response(text) {
    /*
    Reconstructs a JSON-encoded object from a string.  We use this to
    parse server responses into cell IDs, data, etc.  In particular,
    any key in the reconstructed object that ends in 'id' is filtered
    through toint.

    INPUT:
        text -- string
    OUTPUT:
        object
    */
    return JSON.parse(text, function (key, value) {
        if (typeof(key) === 'string' && key.slice(-2) === 'id') {
            return toint(value);
        }
        return value;
    });
}


function encode_response(obj) {
    /*
    JSON-encodes a object to a string.

    INPUT:
        obj -- object
    OUTPUT:
        string
    */
    return JSON.stringify(obj);
}



function initialize_the_notebook() {
    /*
    Do the following:
        1. Determine the browser OS, type e.g., opera, safari, etc.;
           we set global variables for each type.
        2. Figure out which keyboard the user has.
    */
    var i, n, nav, nap, nua;

    // TODO: Use js-hotkeys (http://code.google.com/p/js-hotkeys/)?
    // Determine the browser, OS and set global variables.
    try {
        n = navigator;
        nav = n.appVersion;
        nap = n.appName;
        nua = n.userAgent;
        browser_op = (nua.indexOf('Opera') !== -1);
        browser_saf = (nua.indexOf('Safari') !== -1);
        browser_iphone = (nua.indexOf('iPhone') !== -1);
        browser_konq = (!browser_saf &&
                        (nua.indexOf('Konqueror') !== -1)) ? true : false;
        browser_moz = ((!browser_saf && !browser_konq) &&
                       (nua.indexOf('Gecko') !== -1)) ? true : false;
        browser_ie = ((nap.indexOf('Internet Explorer') !== -1) && !browser_op);
        os_mac = (nav.indexOf('Mac') !== -1);
        os_win = (((nav.indexOf('Win') !== -1) ||
                   (nav.indexOf('NT') !== -1)) && !os_mac) ? true : false;
        os_lin = (nua.indexOf('Linux') !== -1);
    } catch (e) {
        alert(e);
    }

    // Get the keyboard codes for our browser/os combination.
    get_keyboard();

    // Parse the cell IDs.
    cell_id_list = $.map(cell_id_list, function (id) {
        // Reset each cell's introspection variables.
        if (is_compute_cell(id)) {
            halt_introspection(id);
        }
        return toint(id);
    });
    //TODO: why do we do the following twice?
    // Parse active cell IDs and mark these cells as running.  We
    // don't use $.map here, to avoid the possibility of overwriting a
    // debug version of the list.  See debug.js for details.
    for (i = 0; i < queue_id_list.length; i += 1) {
       queue_id_list[i] = toint(queue_id_list[i]);
       cell_set_running(queue_id_list[i]);
    }
    if (queue_id_list.length) {
        start_update_check();
    }

    // Parse active cell IDs and mark these cells as running.  We
    // don't use $.map here, to avoid the possibility of overwriting a
    // debug version of the list.  See debug.js for details.
    for (i = 0; i < queue_id_list.length; i += 1) {
       queue_id_list[i] = toint(queue_id_list[i]);
       cell_set_running(queue_id_list[i]);
    }
    if (queue_id_list.length) {
        start_update_check();
    }

    // Parse "onload" cell IDs and evaluate these cells.  Note: The
    // server fires "%auto" cells, whereas the client fires "onload"
    // cells.
    onload_id_list = $.map(onload_id_list, function (id) {
        id = toint(id);
        evaluate_cell(id, 0);
        return id;
    });

    // Resize all cells on window resize.
    previous.height = $(document.documentElement).height();
    previous.width = $(document.documentElement).width();
    $(window).resize(function () {
        var h, w;
        h = $(document.documentElement).height();
        w = $(document.documentElement).width();

        // IE fires global resize *far* too often (e.g., on every cell
        // focus/blur).
        if ((h !== previous.height) || (w !== previous.width)) {
            resize_all_cells();
            previous.height = h;
            previous.width = w;
        }
    });

    // Resize and save on paste.
    $(document).on('paste','textarea', function () {
        var id = $(this).attr('id').slice(11);
        setTimeout(function () {
            send_cell_input(id);
            cell_input_resize(id);
        }, keypress_resize_delay);
    });

    // Quit the sage process on close for doc/pub-browser worksheets.
    i = worksheet_filename.indexOf('/');
    if (i !== -1 && worksheet_filename.slice(0, i) === '_sage_') {
	    $(window).on('unload', function () {
	        quit_sage();
	    });
    }

    //bind events to our DOM elements
    bind_events();
}


function bind_events() {
    /*
     * Attaches events to DOM elements.
     */
    $('body').on('focus', 'textarea.cell_input', function () {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        cell_focused(this, cell_id);
        return true;
    });
    $('body').on('focus', 'textarea.cell_input_hide', function () {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        cell_focused(this, cell_id);
        return true;
    });
    $('body').on('blur', 'textarea.cell_input_active', function () {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        cell_blur(cell_id);
        return true;
    });
    $('body').on('keyup', 'textarea.cell_input_active', function (event) {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        return input_keyup(cell_id, event);
    });
    $('body').on('keydown', 'textarea.cell_input_active', function (event) {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        return input_keydown(cell_id, event);
    });
    $('body').on('keypress', 'textarea.cell_input_active', function (event) {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        return input_keypress(cell_id, event);
    });
    $('body').on('click', 'input.eval_button_active', function () {
        var id = $(this).attr("id");
        var cell_id = get_cell_id_from_id(id);
        evaluate_cell(cell_id, 0);
    });
}


function get_cell_id_from_id(id) {
    /*
    * A function to get the cell_id from the button's id attribute
    */
    var num_re = /[0-9]+/;
    var match_result = num_re.exec(id);
    var cell_id = toint(match_result[0]);
    return cell_id;
}


function true_function() {
    /*
    A function that always returns true.
    */
    return true;
}
input_keypress = true_function;


function get_keyboard() {
    /*
    Determine which keycodes we want, then make a request back to the
    server for those keycodes.  When the server returns the javascript
    with exactly those keycodes, we eval that javascript.

    OUTPUT:
        set some global variables that record platform specific key
        codes
    */
    var b, o, warn = false;

    input_keypress = cell_input_key_event;
    input_keydown = true_function;

    if (browser_op) {
        b = "o";
    } else if (browser_ie) {
        b = "i";
        input_keypress = true_function;
        input_keydown = cell_input_key_event;
    } else if (browser_saf) {
        b = "s";
        input_keypress = true_function;
        input_keydown = cell_input_key_event;
    } else if (browser_konq) {
        b = "k";
        warn = true;
    } else {
        b = "m";
    }

    if (os_mac) {
        o = "m";
    } else if (os_lin) {
        o = "l";
    } else {
        o = "w";
    }

    if (!b || !o || warn) {
        alert(translations['Your browser / OS combination is not supported.\\nPlease use Firefox or Opera under Linux, Windows, or Mac OS X, or Safari.']);
    }

    $.getScript('/javascript/dynamic/keyboard/' + b + o);
}


function get_element(id) {
    /*
    Return the DOM element with the given id.  If no element has the
    id, return null.

    INPUT:
        id -- an integer or string
    OUTPUT:
        a DOM element or null.
    */
    var elem = $('#' + id);
    if (elem.length) {
        return elem[0];
    } else {
        return null;
    }
}


function set_class(id, cname) {
    /*
    Set the class of the DOM element with given id to cname.

    INPUT:
        id -- an integer or string
        cname -- a string
    OUTPUT:
        Sets the class of the DOM element with the
        given id to be class.
    */
    $('#' + id).attr('class', cname);
}


function key_event(e) {
    /*
    Normalizes the different possible keyboard event structures for
    different browsers.  NOTE: We use key_event as an object.

    INPUT:
        e -- a DOM event
    OUTPUT:
        Sets properties of the DOM object in a uniform way.

    TODO: Use jQuery's Event, instead.
   */
    // IE uses the global variable event.
    e = e || window.event;

    // Record whether alt, control, and shift were pressed.
    this.v = 0;
    if (e.altKey) {
        this.v += 1;
    }
    if (e.ctrlKey) {
        this.v += 2;
    }
    if (e.shiftKey) {
        this.v += 4;
    }

    // We set the specific key that was pressed (no modifier), which
    // is string as a string pair n,m.  See keyboards.py for details.
    this.m = e.keyCode + "," + e.which;
    return this;
}


function time_now() {
    /*
    Return the time right now as an integer since Unix epoch in
    milliseconds.

    OUTPUT:
        an integer
    */
    return (new Date()).getTime();
}


function current_selection(input) {
    /*
    Return the text that is currently selected in a given text area.

    INPUT:
        input -- a DOM object (a textarea)
    OUTPUT:
        a string
    */
    var range;
    if (browser_ie) {
        range = document.selection.createRange();
        return range.text;
    } else {
        return input.value.substring(input.selectionStart, input.selectionEnd);
    }
}


function get_selection_range(input) {
    /*
    Return the start and end positions of the currently selected text
    in the input text area (a DOM object).

    INPUT:
        input -- a DOM object (a textarea)
    OUTPUT:
        an array of two nonnegative integers
    */
    var end, range, start, tmprange;
    if (browser_ie) {
        range = document.selection.createRange();

        tmprange = range.duplicate();
        tmprange.moveToElementText(input);
        tmprange.setEndPoint("endToStart", range);
        start = tmprange.text.length;

        tmprange = range.duplicate();
        tmprange.moveToElementText(input);
        tmprange.setEndPoint("endToEnd", range);
        end = tmprange.text.length;

        return [start, end];
    } else {
        return [input.selectionStart, input.selectionEnd];
    }
}


function set_selection_range(input, start, end) {
    /*
    Select a range of text in a given textarea.

    INPUT:
        input -- a DOM input text area
        start -- an integer
        end -- an integer
    OUTPUT:
        changes the state of the input textarea.
    */
    var range;
    if (browser_ie) {
        input.value = input.value.replaceAll("\r\n", "\n");
        range = document.selection.createRange();
        range.moveToElementText(input);
        range.moveStart('character', start);
        range.setEndPoint("endToStart", range);
        range.moveEnd('character', end - start);
        range.select();
    } else {
        input.selectionStart = start;
        input.selectionEnd = end;
    }
}


function get_cursor_position(cell) {
    /*
    Return an integer that gives the position of the text cursor in
    the cells input field.

    INPUT:
        cell -- an input cell (not the id but the actual DOM element)
    OUTPUT:
        a single integer
    */
    return get_selection_range(cell)[1];
}


function set_cursor_position(cell, n) {
    /*
    Move the cursor position in the cell to position n.

    WARNING: Does nothing when n is 0 on Opera at present.

    INPUT:
        cell -- an actual cell in the DOM, returned by get_cell
        n -- a non-negative integer
    OUTPUT:
        changes the position of the cursor.
    */
//    if (browser_op && !n) {
        // program around a "bug" in opera where using this hack to
        // position the cursor selects the entire text area (which is
        // very painful, since then the user accidentally deletes all
        // their code).
//        return;
//    }
    // TODO: note for explorer:  may need to focus cell first.
    set_selection_range(cell, n, n);
}


///////////////////////////////////////////////////////////////////
// Misc page functions -- for making the page work nicely
///////////////////////////////////////////////////////////////////
function hide_java_applets() {
    /*
    Hides all Jmol applets by moving them off the screen and putting a
    box of the same size in the same place.
     */
    $('.jmol_applet').each(function () {
        var me = $(this),
            width = me.width(),
            height = me.height();
        me.css({
            marginLeft: '-' + (width + 1000) + 'px'
        })
        me.after(
            $('<table><tbody><tr><td align="center" valign="middle">' + 
              translations["Java Applet Hidden"] + '</td></tr></tbody></table>').css({
                marginTop: '-' + height.toString() + 'px',
                width: width.toString() + 'px',
                height: height.toString() + 'px',
                border: '1px solid black',
                backgroundColor: '#ccc',
                color: 'black'
            })
        );
    });
}


function show_java_applets() {
    /*
    Shows all the java applets hid with applet_hide().
    */
    $('.jmol_applet').each(function () {
        $(this).css({
            marginLeft: '0px'
        }).next().remove();
    });
}


function modal_prompt(form_options, options, modal_options) {
    /*
    Displays a prompt with a modal dialog. Use this instead of
    prompt().

    INPUT:
        form_options -- options passed to jQuery.Form. All options
        have the same behavior as jQuery.Form's except success, which
        is passed the form and prompt as arguments. Please refer to
        the jQuery.Form documentation
        (http://jquery.malsup.com/form/#options-object) for more
        information.

        success -- function to be called when the form is
        submitted. It is passed the generated form and prompt as
        arguments.

     OR, for convenience:

        form_options -- function to be called when the form is
        submitted. It is passed the generated form and prompt as
        arguments.

        options -- an object with any of the following attributes:
           - title -- the title of the modal prompt.
           - message -- the message to be displayed in the prompt.
           - default -- the default value of the prompt.
           - submit -- the value of the submit button. Defaults to "OK".
           - overlay_close -- whether to close the dialog if the
             overlay is clicked. Defaults to true.
           - id -- id for the modal prompt.
           - form_id -- id for the form.
           - css -- CSS to be applied to the prompt. Consider editing
             the stylesheet files instead of using this option.
           - post_submit_behavior -- any of "close", "destroy", which
             also removes the dialog code or a custom function to be
             called after submitting the form. Defaults to "destroy"

        modal_options -- options passed to jQuery UI Dialog. Refer to
        the jQuery UI Dialog documentation
        (http://jqueryui.com/demos/dialog/#default>) for more
        options. Default options are:
           - autoOpen: true -- automatically opens the dialog. If set
             to false, open the dialog with <prompt>.dialog('open').
           - modal: true -- makes the dialog modal, i.e., UI blocking.
           - width: "20em" -- width of the dialog box.

    OUTPUT:
        returns the generated prompt
    */
    var title, message, default_value, submit_value, css, new_prompt, new_form, overlay_close, close_behavior, close_dialog, input, old_success_function;

    // Options setup.
    options = options || {};
    title = options.title || '';
    message = options.message || '';
    default_value = options['default'] || '';
    submit_value = options.submit || 'OK';
    css = options.css || {};

    overlay_close = options.overlay_close;
    if (typeof(options.overlay_close) === 'undefined') { 
        overlay_close = true;
    }

    close_behavior = options.close_behavior || 'destroy';
    close_dialog = function () {
        if (close_behavior === 'destroy') {
            new_prompt.dialog('destroy');
            new_prompt.remove();
        } else if (close_behavior === 'close') {
            new_prompt.dialog('close');
        } else if (typeof(close_behavior) === 'function') {
            close_behavior();
        }
        show_java_applets();
    };
    
    modal_options = $.extend({
        autoOpen: true,
        modal: true,
        width: '20em',
        close: close_dialog
    },
    modal_options);

    new_prompt = $($.parseHTML(modal_prompt_element));
    $('body').append(new_prompt);
    new_prompt.css(css);

    new_form = new_prompt.find('form');
    if (options.id) {
        new_prompt.attr('id', options.id);
    }
    if (options.form_id) {
        new_form.attr('id', options.form_id);
    }

    // Prompt setup.
    new_prompt.find('div.message').html(message);
    input = new_prompt.find('input', 'div.field').attr('value', default_value).css('width', '100%');
    new_prompt.find('button[type="submit"]').html(submit_value);

    modal_options.title = modal_options.title || title;

    if (overlay_close) {
        //$('div.ui-widget-overlay').live('click', close_dialog);
        $(document).on('click','div.ui-widget-overlay',close_dialog);
    }

    // Form setup.
    old_success_function = form_options.success;
    form_options.success = function () {
        old_success_function(new_form, new_prompt);
        close_dialog();
    };
    new_form.ajaxForm(form_options);
    hide_java_applets();
    new_prompt.dialog(modal_options);
    input.select();
}


function refresh() {
    /*
    This function simply refreshes the current HTML page, thus completely
    reloading the DOM from the server.
    */
    window.location.replace(location.href);
}


function refresh_cell_list() {
    /*
    This function refreshes all the cells in the worksheet using an
    async call back to the server.
    */
    async_request(worksheet_command('cell_list'), refresh_cell_list_callback);
}


function refresh_cell_list_callback(status, response) {
    /*
    In conjunction with refresh_cell_list, this function does the
    actual update of the HTML of the list of cells.  Here
    response is a pair consisting of the updated state_number and
    the new HTML for the worksheet_cell_list div DOM element.
    */

    var s, X, z;
    if (status !== 'success') {
        return;
    }
    X = decode_response(response);
    state_number = parseInt(X.state_number, 10);

    // Now we replace the HTML for every cell *except* the current
    // cell.
    z = get_element(current_cell);
    if (z) {
        s = z.innerHTML;
    }

    refresh();

    if (z) {
        z = get_element(current_cell);
        z.innerHTML = s;
        cell_input_resize(current_cell);
        jump_to_cell(current_cell, 0);
    }
}


String.prototype.replaceAll = function (old_sub, new_sub) {
    /*
    Replace all instances of the given substring by another string.

    INPUT:
        this -- the initial string
        old_sub -- the substring to replace
        new_sub -- the replacement
    OUTPUT:
        the updated string
    */
    // The regular expression engine should avoid infinite loops, e.g.:
    // 'a'.replaceAll('a', 'a');
    return this.replace(new RegExp(old_sub, 'g'), new_sub);
};


function is_whitespace(s) {
    /*
    Return true precisely if the input string s consists only of
    whitespace, e.g., spaces, tabs, etc.

    INPUT:
        s -- a string
    OUTPUT:
        true or false
    */

    // We check using the whitespace_pat regular expression defined at
    // the top of this file.
    var m = whitespace_pat.exec(s);
    return (m[1] === s);
}


function first_variable_name_in_string(s) {
    /*
    This function returns the first valid variable name in a string.

    INPUT:
        s -- a string
    OUTPUT:
        a string
    */
    var m = one_word_pat.exec(s);
    if (m === null) {
        return s;
    }
    return m[1];
}


function lstrip(s) {
    /*
    Given a string s, strip leading whitespace from s and return the
    resulting string.

    INPUT:
        s -- a string
    OUTPUT:
        a string
    */
    var i = 0, n = s.length;
    while (i < n && (s[i] === ' ' || s[i] === '\n' || s[i] === '\t')) {
        i = i + 1;
    }
    return s.slice(i);
}


function resize_all_cells() {
    /*
    Resizes all cells that do not start with %hide; called whenever
    the window gets resized.

    GLOBAL INPUT:
        cell_id_list -- a list of integers
    */
    var i, id, len = cell_id_list.length;
    for (i = 0; i < len; i += 1) {
        // Get the id of the cell to resize
        id = cell_id_list[i];
        if (!is_compute_cell(id)) {
            continue;
        }
        // Make sure it is not hidden, and if not resize it.
        if (get_cell(id).className !== "cell_input_hide") {
            cell_input_resize(id);
        }
    }
}


function input_keyup(id, event) {
    /*
    Resizes and auto-idents cells on key up.

    INPUT:
        id -- integer or string; cell id
        event -- a keyup event
    GLOBAL INPUT:
        keypress_resize_delay -- amount of time to wait between resizes
        last_keypress_resize -- last time we resized
        will_resize_soon -- if a keyup event is ignored for the
        purpose of resizing, then we queue one up.  Avoid a
        timeout-flood with this lock.
    */
    var cell, e, indent, m, position, t, text;
    id = toint(id);

    if (skip_keyup) {
        skip_keyup = false;
        return false;
    }

    t = time_now();
    if ((t - last_keypress_resize) > keypress_resize_delay) {
        last_keypress_resize = t;
        cell_input_resize(id);
    } else if (!will_resize_soon) {
        will_resize_soon = true;
        setTimeout(function () {
            cell_input_resize(id);
            will_resize_soon = false;
        }, keypress_resize_delay);
    }

    // Automatic indentation.
    if (browser_iphone) {
        return;
    }

    e = new key_event(event);
    if (!e) {
        return;
    }

    if (key_enter(e)) {
        cell = get_cell(id);
        /* Warning!  Very subtle regular expression (for non-JAPHs):

         (?:\n|^)        -- starting from the last line ending (or beginning
                            of the cell) (don't capture contents)

         ( *)            -- capture as many spaces as we can find

         (?:.*?)         -- everything else in the string, but save room for
                            the following terms (don't capture contents)

         (:?)            -- capture an optional colon before the following
                            term

         [ \t\r\v\f]*\n$ -- ignore whitespace at the end of the line
        */
        // TODO: Really fix auto-indentation in IE.
        position = get_cursor_position(cell);
        text = text_cursor_split(cell);

        // We use exec instead of test, since the latter does not
        // populate RegExp.$1, etc., with captured groups in IE.
        m = /(?:\n|^)( *)(?:.*?)(:?)[ \t\r\v\f]*\n$/.exec(text[0]);
        if (m) {
            indent = m[1];
            if (m[2] === ':') {
                indent = indent + "    ";
            }

            cell.value = text[0] + indent + text[1];
            set_cursor_position(cell, position + indent.length);
        }
    }
}


///////////////////////////////////////////////////////////////////
// Completions interface stuff
///////////////////////////////////////////////////////////////////
function handle_introspection(id, cell_input, event) {
    /*
    Handles introspection key events.

    INPUT:
        id -- integer or string; cell id
        cell_input -- input cell
        event -- keypress event
    OUTPUT:
        a boolean
    */
    var before, col, intr, row, select_replacement = true;

    intr = introspect[id];
    if (intr.replacing && !intr.docstring) {
        // We're in the completions menu.
        col = intr.replacement_col;
        row = intr.replacement_row;

        // TODO: Embed the total number of rows and columns in a
        // completions menu.
        if (key_menu_up(event)) {
            // Up arrow pressed.
            row -= 1;
            // Wrap around vertically.
            if (!replacement_element_exists(id, row, col)) {
                row = 1;
                while (replacement_element_exists(id, row, col)) {
                    row += 1;
                }
                row -= 1;
            }
        } else if (key_menu_down(event)) {
            // Down arrow pressed.
            row += 1;
            if (!replacement_element_exists(id, row, col)) {
                row = 0;
            }
        } else if (key_menu_right(event)) {
            // Right arrow pressed.
            col += 1;
            if (!replacement_element_exists(id, row, col)) {
                col = 0;
            }
        } else if (key_menu_left(event)) {
            // Left arrow pressed.
            col -= 1;
            // Wrap around horizontally.
            if (!replacement_element_exists(id, row, col)) {
                col = 1;
                while (replacement_element_exists(id, row, col)) {
                    col += 1;
                }
                col -= 1;
            }
        } else {
            select_replacement = false;
        }
        if (select_replacement) {
            select_replacement_element(id, row, col);
            return false;
        }
        if (key_menu_pick(event)) {
            // Enter pressed.  Do the replacement.
            do_replacement(id, intr.replacement_word, true);
            skip_keyup = true;
            return false;
        }
    }

    if (key_request_introspections(event)) {
        // We start over if the input has changed.
        if (intr.changed) {
            intr.changed = false;
            evaluate_cell_introspection(id, null, null);
            return false;
        }

        // We began with a docstring or source code.  Toggle between
        // the two.
        if (!intr.replacing && intr.docstring) {
            before = intr.before_replacing_word;

            if (before.slice(-2) === '??') {
                before = before.slice(0, -1);
            } else {
                before += '?';
            }

            intr.before_replacing_word = before;
            evaluate_cell_introspection(id, before, intr.after_cursor);
            return false;
        }

        // We began with a list of completions.  Toggle between the
        // list and the docstring of the latest replacement candidate.
        if (intr.replacing && !intr.docstring) {
            intr.docstring = true;
            evaluate_cell_introspection(id, intr.before_replacing_word +
                                        intr.replacement_word + '?',
                                        intr.after_cursor);
            return false;
        }
        if (intr.replacing && intr.docstring) {
            intr.docstring = false;
            evaluate_cell_introspection(id, intr.before_replacing_word +
                                        intr.replacing_word, intr.after_cursor);
            return false;
        }

    } else if (key_close_helper(event)) {
        // ESC pressed.  Stop introspecting.
        halt_introspection(id);
        return false;
    }

    // Any other key.  Return true to continue handling this event.
    if (intr.replacing && !intr.docstring) {
        halt_introspection(id);
    }
    return true;
}


function do_replacement(id, word, do_trim) {
    /*
    Replaces an object's name with an item from a cell's completions
    menu.

    INPUT:
        id -- integer or string; cell id
        word -- a string; the replacement
        do_trim -- true or false; whether to trim the replacement
    */
    var cell_input, pos;
    id = toint(id);

    // Get the input cell and focus on it.
    cell_input = get_cell(id);
    cell_focus(id, false);

    // If necessary get only the first word out of the input word
    // string.
    if (do_trim) {
        word = first_variable_name_in_string(word);
    }

    // Do the actual completion.
    cell_input.value = introspect[id].before_replacing_word + word +
        introspect[id].after_cursor;

    // Put the cursor right after the word we just put in.
    pos = introspect[id].before_replacing_word.length + word.length;
    set_cursor_position(cell_input, pos);

    // Done completing, so get rid of the completions menu.
    halt_introspection(id);
}


function get_replacement_element(id, row, col) {
    /*
    Returns the highlighted DOM element in a cell's completions menu.

    INPUT:
        id -- integer or string; cell id
        row -- integer; vertical position of element
        col -- integer; horizontal position of element
    OUTPUT:
        a DOM element
    */
    return get_element("completion" + id + "_" + row + "_" + col);
}


function replacement_element_exists(id, row, col) {
    /*
    Returns whether a non-empty replacement exists in the given
    position in a cell's completions menu.

    INPUT:
        id -- integer or string; cell id
        row -- integer; vertical position of element
        col -- integer; horizontal position of element
    OUTPUT:
        a boolean
    */
    var elem = get_replacement_element(id, row, col);
    return elem !== null && $.trim($(elem).text()) !== '';
}

function select_replacement_element(id, row, col) {
    /*
    (Un-)highlights prospective replacement elements in a cell's
    completions menu.

    INPUT:
        id -- integer or string; cell id
        row -- integer; vertical position of element
        col -- integer; horizontal position of element
    */
    var e, intr;

    id = toint(id);
    intr = introspect[id];

    e = get_replacement_element(id, intr.replacement_row, intr.replacement_col);
    if (!e) {
        return;
    }
    e.className = 'completion_menu_two';

    intr.replacement_row = toint(row);
    intr.replacement_col = toint(col);

    e = get_replacement_element(id, intr.replacement_row, intr.replacement_col);
    if (!e) {
        return;
    }
    e.className = 'completion_menu_two completion_menu_selected';
    intr.replacement_word = $.trim($(e).text()) || intr.replacement_word;
}


function update_introspection_text(id, text) {
    /*
    Updates a cell's introspection text.

    INPUT:
        id -- integer or string; cell id
        text -- string; the new text
    */
    var d, intr = introspect[id];

    var introspect_div = $("#introspect_div_" + id);
    if (introspect_div.length == 0) {
        return;
    }
    
    if (intr.loaded) {
        introspect_div.html(text);
        try {
            MathJax.Hub.Queue(["Typeset",MathJax.Hub,introspect_div.get(0)]);
        } catch (e) {
            introspect_div.html('Error typesetting mathematics' + introspect_div.html());
        }

        introspect_div.find('.docstring').prepend('<div class="click-message" style="cursor: pointer">' + translations["Click here to pop out"] + '</div><div class="unprinted-note">' + translations["unprinted"] + '</div>');

        if (intr.replacing && !intr.docstring) {
            select_replacement_element(id, intr.replacement_row,
                                       intr.replacement_col);
        }
    } else {
        introspect_div.html(text);
    }
}


function halt_introspection(id) {
    /*
    Closes a cell's introspection "window" (completions menu or
    docstring).

    INPUT:
        id -- integer or string; cell id
    */
    var intr;
    id = toint(id);
    if (!introspect[id]) {
        introspect[id] = {};
    }
    intr = introspect[id];

    intr.loaded = false;
    update_introspection_text(id, '');

    intr.changed = false;
    intr.docstring = false;
    intr.replacing = false;
    intr.replacing_word = '';
    intr.replacement_word = '';
    intr.replacement_col = 0;
    intr.replacement_row = 0;
}


///////////////////////////////////////////////////////////////////
// Paren Matching
///////////////////////////////////////////////////////////////////
function paren_jump(cell, i, c, eat) {
    /*
    Replaces / inserts the desired paren, and moves the cursor to
    immediately after the paren.

    INPUT:
        cell -- a textarea object
        i -- integer; the index of where to insert/replace a paren
        c -- string; the character to insert (may be empty)
        eat -- boolean; whether or not to eat the character at i
    */
    var j = i;
    if (eat) {
        j += 1;
    }
    cell.value = cell.value.substring(0, i) + c + cell.value.substring(j);
    set_cursor_position(cell, i + c.length);
}


function paren_match(cell) {
    /*
    Fix parentheses / braces / brackets.  If mis-matched parentheses
    are found, fix them.  If an unmatched paren is found, insert it at
    the cursor.  This is implemented via a character-by-character
    lexer, so quotes and comments are handled appropriately.  Perhaps
    in the future this lexer will be generalized so it can be used for
    other stuff.

    EXAMPLES:  (the pipe character indicates cursor position)
        IN:
            this = some(sample.code(
                        "foo))",
            #           bar)),
                        baz|
        OUT:
            this = some.code(
                        "foo)",
            #           bar),
                        baz)|

        IN:
            foo = bar(baz(],a,b,c)|
        OUT:
            foo = bar(baz()|,a,b,c)

        IN:
            foo = bar()|
        OUT:
            foo = bar()|

        IN:
            foo = bar]bar()|
        OUT:
            foo = bar|bar()

        IN:
            foo = barbar).baz|
        OUT:
            foo = barbar|.baz

    INPUT:
        cell -- cell textarea
    OUTPUT:
        the text in the cell is possibly changed, and the cursor may
        move.
    */
    var c, comment = false, deparen = [], escape = false, i, n, p,
        pstack = [], quote = '', txt, squo = "'", dquo = '"', hash = "#",
        cr = '\n', esc = '\\', empty = '', lpar = '(', rpar = ')',
        lbrk = '[', rbrk = ']', lbrc = '{', rbrc = '}';

    deparen[lpar] = rpar;
    deparen[lbrc] = rbrc;
    deparen[lbrk] = rbrk;
    txt = cell.value.substring(0, get_cursor_position(cell));
    n = txt.length;

    for (i = 0; i < n; i += 1) {
        c = txt[i];
        if (quote !== empty) {
            if (escape) {
                escape = false;
            } else if (c === quote) {
                quote = empty;
            } else if (c === esc) {
                escape = true;
            }
        } else if (comment) {
            if (c === cr) {
                comment = false;
            }
        } else switch (c) {
        case lpar:
        case lbrc:
        case lbrk:
            pstack.push(c);
            break;
        case rpar:
        case rbrc:
        case rbrk:
            if (pstack.length <= 0) {
                paren_jump(cell, i, '', true);
                return;
            }
            p = pstack.pop();
            if (deparen[p] !== c) {
                paren_jump(cell, i, deparen[p], true);
                return;
            }
            break;
        case squo:
            quote = squo;
            break;
        case dquo:
            quote = dquo;
            break;
        case hash:
            comment = true;
            break;
        }
    }
    p = pstack.pop();
    i = txt.length;
    if (quote === empty && !comment && typeof(p) !== 'undefined') {
        paren_jump(cell, i, deparen[p], false);
    }
}


///////////////////////////////////////////////////////////////////
// WORKSHEET functions -- for switching between and managing 
// worksheets
///////////////////////////////////////////////////////////////////
function new_worksheet() {
    /*
    Ask the server to create a new worksheet, which is then opened
    replacing the current worksheet.
    */
    open("/new_worksheet");
}


function set_worksheet_list_checks() {
    /*
    Go through and set all check boxes the same as they are in the
    control box.  This is called when the user clicks the checkbox in
    the top left of the list of worksheets, which toggles all the
    checkboxes below it to be either on or off (select all or none).

    GLOBAL INPUT:
        worksheet_filenames -- list of strings
    */
    var C, i, id, X;
    C = get_element("controlbox");
    for (i = 0; i < worksheet_filenames.length; i += 1) {
        id = worksheet_filenames[i].replace(/[^\-A-Za-z_0-9]/g, '-');
        X = get_element(id);
        X.checked = C.checked;
    }
}


function checked_worksheet_filenames() {
    /*
    For each filename listed in worksheet_filenames, look up the
    corresponding input check box, see if it is checked, and if so,
    add it to the list.

    GLOBAL INPUT:
        worksheet_filenames -- list of strings
    OUTPUT:
        string of worksheet filenames that are checked, encoded as JSON
    */
    var i, id, X, checked_filenames = [];

    // Concatenate the list of all worksheet filenames that are
    // checked together separated by the separator string.
    for (i = 0; i < worksheet_filenames.length; i += 1) {
        id = worksheet_filenames[i].replace(/[^\-A-Za-z_0-9]/g, '-');
        X = get_element(id);
        if (X.checked) {
            checked_filenames.push(worksheet_filenames[i]);
            X.checked = 0;
        }
    }
    return encode_response(checked_filenames);
}


function worksheet_list_button(action) {
    /*
    For each filename listed in worksheet_filenames, look up the
    corresponding input check box, see if it is checked, and if so, do
    the corresponding action.

    INPUT:
        action -- URL that defines a message to send to the server
    GLOBAL INPUT:
        worksheet_filenames -- list of strings
    OUTPUT:
        calls the server and requests an action be performed on all
        the listed worksheets
    */
    // Send the list of worksheet names and requested action back to
    // the server.
    async_request(action, worksheet_list_button_callback, {
        filenames: checked_worksheet_filenames()
    });
}


function worksheet_list_button_callback(status, response) {
    /*
    Handle result of performing some action on a list of worksheets.

    INPUT:
        status, response -- standard AJAX return values
    OUTPUT:
        display an alert if something goes wrong; refresh this browser
        window no matter what.
    */
    if (status === 'success') {
        if (response !== '') {
            alert(response);
        }
    } else {
        alert(translations['Error applying function to worksheet(s).'] + response);
    }
    window.location.reload(true);
}


function delete_button() {
    /*
    This javascript function is called when the worksheet list delete
    button is pressed.  Each worksheet whose box is checked gets sent
    to the trash.
    */
    worksheet_list_button("/send_to_trash");
}


function make_active_button() {
    /*
    Sends each checked worksheet to the active worksheets folder.
    */
    worksheet_list_button("/send_to_active");
}


function archive_button() {
    /*
    Sends each checked worksheet to the archived worksheets folder.
    */
    worksheet_list_button("/send_to_archive");
}


function stop_worksheets_button() {
    /*
    Saves and then quits sage process for each checked worksheet.
    */
    worksheet_list_button("/send_to_stop");
}


function download_worksheets_button() {
    /*
    Downloads the set of checked worksheets as a zip file.
    */
    window.location.replace("/download_worksheets.zip?filenames=" +
                            checked_worksheet_filenames());
}


function history_window() {
    /*
    Display the history popup window, which displays the last few hundred
    commands typed into any worksheet.
    */
    window.open("/history", "", "menubar=1,scrollbars=1,width=800," +
                "height=600,toolbar=1,resizable=1");
}


function upload_worksheet_button() {
    /*
    Replace the current display window with the upload entry box.
    */
    window.location.replace("/upload");
}


function copy_worksheet() {
    /*
    Make a copy of the current worksheet then load the copy into the
    current frame.
    */
    window.location.replace(worksheet_command("copy"));
}


function download_worksheet() {
    /*
    Download the current worksheet to the file with name select by the
    user.  The title of the worksheet is also changed to match the
    filename.

    INPUT:
        base_filename
    */
    var title = prompt(translations['Title of saved worksheet'], worksheet_name), winref;
    if (title) {
        winref = open(worksheet_command("download/" + title + '.sws'));
    }
}


function worksheet_settings() {
    /*
    Bring up the worksheet settings menu.
    */
    window.location.replace(worksheet_command("settings"));
}


function share_worksheet() {
    /*
    Display the worksheet sharing window.
    */
    window.location.replace(worksheet_command("share"));
}


function publish_worksheet() {
    /*
    Public the current worksheet.
    */
    window.open(worksheet_command("publish"), "",
                "menubar=1,location=1,scrollbars=1,width=800," +
                "height=600,toolbar=1,  resizable=1");
}


function save_as(typ) {
    /*
    Save the current worksheet to a file.
    */
    open(worksheet_command('save_as') + '?typ=' + typ);
}

function save_worksheet() {
    /*
    Save a snapshot of the current worksheet.
    */
    async_request(worksheet_command('save_snapshot'), save_worksheet_callback);
}


function save_worksheet_callback(status, response) {
    /*
    Verify that saving the current worksheet worked.
    */
    if (status !== 'success') {
        alert(translations['Failed to save worksheet.']);
        return;
    }
}


function close_callback(status, response) {
    /*
    Called when we successfully close the current worksheet and want
    to display the user home screen (i.e., worksheet list).
    */
    if (status !== 'success') {
        alert(response);
        return;
    }
    window.location.replace('/home/' + user_name);
}


function save_worksheet_and_close() {
    /*
    Send message back to the server saving the current worksheet and
    quitting the Sage process, then close the current window returning
    to the home screen.
    */
    async_request(worksheet_command('save_and_quit'), close_callback);
}


function worksheet_discard() {
    /*
    Discard the current worksheet and quit the currently running Sage
    process, then close the current window and replace it by the home
    screen.
    */
    async_request(worksheet_command('discard_and_quit'), close_callback);
}


function rename_worksheet() {
    /*
    Rename the current worksheet.  This pops up a dialog that asks for
    the new worksheet name, then sets it in the browser, and finally
    sends a message back to the server stating that the worksheet has
    been renamed.
    */
    var callback = function (new_worksheet_name) {
        var title = $('#worksheet_title'), set_name;
        if (new_worksheet_name.length >= 30) {
            set_name = new_worksheet_name.slice(0, 30) + ' ...';
        } else {
            set_name = new_worksheet_name;
        }
        title.html(set_name);
        worksheet_name = new_worksheet_name;
        original_title = worksheet_name + ' (Sage)';
        document.title = original_title;
        async_request(worksheet_command('rename'), null, {
            name: new_worksheet_name
        });
    };
    modal_prompt({
        success: function (form, prompt) {
            callback($(':text',form).prop('value'));
        }
    }, {
        id: "rename_prompt",
        title: translations["Rename worksheet"],
        message: translations['Please enter a name for this worksheet.'],
        'default': worksheet_name,
        submit: translations["Rename"]
    });
}


function go_system_select(form, original_system) {
    /*
    Switch the current input system from one system to another (e.g.,
    form Sage to Pari or Python).  A confirmation box is displayed.

    INPUT:
        form -- DOM element; the drop down with the list of systems
        original_system -- string; the system we're switching *from*
     */
    system_select(form.options[form.selectedIndex].value);
}


function system_select(s) {
    /*
    Send a message back to the server stating that we're switching to
    evaluating all cells using the new system s.

    INPUT:
        s -- string
    */
    async_request(worksheet_command('system/' + s));
}


function pretty_print_check(s) {
    /*
    Send a message back to the server either turn pretty typeset
    printing on or off.

    INPUT:
        s -- boolean; whether the pretty print box is checked.
    */
    async_request(worksheet_command('pretty_print/' + s));
}

function live_3D_check(s) {
    /*
    Send a message back to the server either turn live 3D on or off.

    INPUT:
        s -- boolean; whether the pretty Live 3D box is now checked.
    */
    async_request(worksheet_command('live_3D/' + s));
}

function handle_data_menu(form) {
    /*
    Handle what happens when the user clicks on the worksheet data
    menu and selects an option.

    INPUT:
    form -- DOM element; the form in the worksheet with the data
      drop down menu.
    */
    var value = form.options[form.selectedIndex].value;

    if (value === "__upload_data_file__") {
        window.location.replace(worksheet_command("upload_data"));
    } else if (value !== '') {
        window.location.replace("/home/" + worksheet_filename + "/" +
                                value);
    }
    form.options[0].selected = 1;
}


function delete_worksheet(name) {
    /*
    Send the worksheet with the given name to the trash.

    INPUT:
      name -- string
    */
    async_request('/send_to_trash', delete_worksheet_callback, {
        filename: name
    });
}


function delete_worksheet_callback(status, response) {
    /*
    Replace the current page by a page that shows the worksheet in the
    trash, or if the delete worksheet function failed display an
    error.
    */
    if (status === "success") {
        window.location.replace("/?typ=trash");
    } else {
        alert(translations['Possible failure deleting worksheet.']);
    }
}


///////////////////////////////////////////////////////////////////
// WORKSHEET list functions -- i.e., functions on a specific
// worksheet in the list of worksheets display.
///////////////////////////////////////////////////////////////////
function go_option(form) {
    /*
    This is called when the user selects a menu item.

    INPUT:
      form -- DOM element; the drop-down form element
    */
    var action = form.options[form.selectedIndex].value;
    // not safe, but more straigth forward than parsing
    // what is basically an eval string and running the 
    // corresponding function and arguments
    eval(action);
    form.options[0].selected = 1;
}


function link_datafile(target_worksheet_filename, filename) {
    /*
    Tell the server to create a symbolic link from the given data file
    to the target worksheet.  This is used to share data between
    multiple worksheets.

    INPUT:
       target_worksheet_filename -- string; the name of the worksheet
       to link this file to
       filename -- string; the name of this file
     */
    // TODO: What is process?
    open(worksheet_command("link_datafile?filename=" + escape0(filename) +
                           "&target=" + escape0(target_worksheet_filename)),
         process = false);
}


function list_rename_worksheet(filename, curname) {
    /*
    Prompt for a new worksheet name, then send the requested new name
    back to the server, thus changing the worksheet name.  Prompt for
    a new worksheet name, then send the requested new name back to the
    server, thus changing the worksheet name.

    INPUT:
        filename -- string; the filename of this worksheet to rename
        curname -- string; the current name of this worksheet
    */
    var callback = function (new_name) {
        async_request('/home/' + filename + '/' + 'rename', refresh, {
            name: new_name
        });
    };
    modal_prompt(function (form, prompt) {
        callback($(':text', form).attr('value'));
    }, {
        title: translations["Rename worksheet"],
        message: translations["Please enter a name for this worksheet."],
        'default': curname,
        submit: translations["Rename"]
    });
}


function list_edit_worksheet(filename) {
    /*
    In the list of all worksheets, when the user selects "Edit" from
    the menu to edit a worksheet, this function is called, which
    simply loads that worksheet.

    INPUT:
        filename -- string
    */
    window.location.replace('/home/' + filename);
}


function list_copy_worksheet(filename) {
    /*
    When the user selects "Copy" from the list of worksheets, this
    function is called.  It simply sends a message back to the server
    asking that a copy of the worksheet is made.  The worksheet list
    is then refreshed.

    INPUT:
        filename -- string; filename of the worksheet to share
    */
    async_request('/home/' + filename + '/copy?no_load', refresh);
}


function list_share_worksheet(filename) {
    /*
    Bring up menu that allows one to share the selected worksheet from
    the worksheet list with other users.

    INPUT:
        filename -- string; filename of the worksheet to share
    */
    window.location.replace('/home/' + filename + '/share');
}


function list_publish_worksheet(filename) {
    /*
    Publish the given worksheet, when this is selected from the
    worksheet list, and popup the published worksheet.

    INPUT:
        filename -- string; filename of the worksheet to share
    */
    window.open('/home/' + filename + '/publish', "",
                "menubar=1,scrollbars=1,width=800,height=600,toolbar=1,  resizable=1");
}


function list_revisions_of_worksheet(filename) {
    /*
    Display all revisions of the selected worksheet.  This brings up
    the revisions browser.

    INPUT:
        filename -- string; filename of the worksheet to share
    */
    window.location.replace('/home/' + filename + '/revisions');
}


///////////////////////////////////////////////////////////////////
// Server pinging support, so server knows page is being viewed.
///////////////////////////////////////////////////////////////////
function server_ping_while_alive() {
    /*
    Ping the server every server_ping_time milliseconds to announce
    that we are still viewing this page.
    */
    async_request(worksheet_command('alive'), server_ping_while_alive_callback);
    setTimeout(function () {
        server_ping_while_alive();
    }, server_ping_time);
}


function server_ping_while_alive_callback(status, response) {
    /*
    Whenever the server ping callback occurs, this function runs, and
    setsif the server didn't respond it calls server_down(); otherwise it
    calls server_up().

    Also, each time the server is up and responds, the server includes
    the worksheet state_number is the response.  If this number is out
    of sync with our view of the worksheet state, then we force a
    refresh of the list of cells.  This is very useful in case the
    user uses the back button and the browser cache displays an
    invalid worksheet list (which can cause massive confusion), or the
    user open the same worksheet in multiple browsers, or multiple
    users open the same shared worksheet.
    */
    if (status === "failure") {
        set_class('ping', 'pingdown');
    } else {
        set_class('ping', 'ping');
        if (state_number >= 0 && parseInt(response, 10) > state_number) {
            // Force a refresh of just the cells in the body.
            refresh_cell_list();
        }
    }
}


///////////////////////////////////////////////////////////////////
// CELL functions -- for the individual cells
///////////////////////////////////////////////////////////////////
function get_cell(id) {
    /*
    Returns a cell's input textarea.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
        a DOM element or null
    GLOBAL INPUT:
        cell_element_cache -- an associative array that maps ids to
        elements
    */
    var v;
    id = toint(id);

    // TODO: Just use jQuery with context?
    v = cell_element_cache[id];
    if (!v) {
        v = get_element('cell_input_' + id);
        cell_element_cache[id] = v;
    }
    return v;
}


function cell_blur(id) {
    /*
    This function is called when the cell with the given id is
    blurred.  It removes whitespace around the input, and if the cell
    has changed sends the changed input back to the server.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
       true -- to avoid infinite recursion.
    */
    var cell, v;

    id = toint(id);
    cell = get_cell(id);
    if (!cell) {
        return true;
    }

    // Set the style back to the non-active input cell
    cell.className = 'cell_input';

    // If cell starts with %hide, hide the input.
    v = lstrip(cell.value).slice(0, 5);
    if (v === '%hide') {
        cell.className = 'cell_input_hide';
        cell.style.height = '1em';
    }

    if (cell_has_changed) {
        send_cell_input(id);
    }

    // It is very important to return true here, or one gets an
    // infinite javascript recursion.
    return true;
}


function send_cell_input(id) {
    /*
    Send the input text of the current cell back to the server.  This
    is typically called when the cursor leaves the current cell.

    INPUT:
       id -- an integer or string; cell id
    OUTPUT:
       makes an async call back to the server sending the input text.
    */
    var cell = get_cell(id);
    if (!cell) {
        return;
    }

    // When the input changes we set the CSS to indicate that the cell
    // with this new text has not been evaluated.
    cell_set_not_evaluated(id);

    async_request(worksheet_command('eval'), null, {
        save_only: 1,
        id: id,
        input: cell.value
    });
}


function evaluate_text_cell_input(id, value, settings) {
    /*
    Send the input text of the current cell back to the server.

    INPUT:
       id -- integer or string; cell id
       value -- string; new cell contents
       settings -- object
    OUTPUT:
       makes an async call back to the server sending the input text.
    */
    async_request(worksheet_command('eval'), evaluate_text_cell_callback, {
        text_only: 1,
        id: id,
        input: value
    });
    //update jmol applet list
    sage_jmol.delete_callback();
}


function evaluate_text_cell_callback(status, response) {
    /*
    Display the new content of a text cell, parsing for math if
    needed.

    INPUT:
        status -- string
        response -- string; encoded JSON object with parsed keys

             id -- string or integer; evaluated cell's id
             cell_html -- string; the cell's updated contents
    */
    var new_html, text_cell, X;
    if (status === "failure") {
        // Failure evaluating a cell.
        return;
    }
    X = decode_response(response);

    if (X.id === -1) {
        // Something went wrong, e.g., the requested cell doesn't
        // exist.
        alert(translations['You requested to evaluate a cell that, for some reason, the server is unaware of.']);
        return;
    }

    text_cell = get_element('cell_outer_' + X.id);
    new_html = separate_script_tags(X.cell_html);
    $(text_cell).replaceWith(new_html[0]);
    // Get the new text cell.
    text_cell = get_element('cell_outer_' + X.id);
    setTimeout(new_html[1], 50);

    try { MathJax.Hub.Queue(["Typeset",MathJax.Hub,text_cell]); 
	} catch (e) { text_cell.innerHTML = 'Error typesetting mathematics' + text_cell.innerHTML; }
}


function cell_focus(id, leave_cursor) {
    /*
    Set the focus on the cell with the given id.

    INPUT:
        id -- integer or string; cell id
        leave_cursor -- boolean; whether to move the cursor to the top
        left of the input cell
    OUTPUT:
        focuses on a given cell, possibly moves the cursor, and sets
        the global variable cell_has_changed to false, since we have
        just entered this cell and it hasn't been changed yet.
    */
    var cell;

    id = toint(id);
    cell = get_cell(id);

    if (!cell) {
        return true;
    }

    // Focus on the cell with the given id and resize it.
    cell_input_resize(id);
    cell.focus();

    // Possibly also move the cursor to the top left in this cell.
    if (!leave_cursor) {
        set_cursor_position(cell, 0);
    }
    // Set since we're now in a new cell, whose state hasn't changed
    // yet.
    cell_has_changed = false;
    return true;
}


function cell_focused(cell, id) {
    /*
    This function is called when the cell gets focus.  It sets the CSS
    so that the input part of the cell is highlighted.

    INPUT:
        cell -- DOM element
        id -- integer or string; cell id
    OUTPUT:
        sets the global variable current_cell and update display of
        the evaluate link.
    */
    id = toint(id);

    cell.className = "cell_input_active";

    // This makes sure the input textarea is resized right when it is
    // clicked on.
    cell_input_resize(id);

    if (current_cell === id) {
        return;
    }
    if (current_cell !== -1) {
        set_class("eval_button" + current_cell, "eval_button");
    }
    current_cell = id;
    set_class("eval_button" + id, "eval_button_active");
}


function cell_input_resize(id) {
    /*
    Resize the given input cell so that it has the right number of
    rows for the number of rows currently typed into the input cell.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
        changes the height of the corresponding DOM object to fit the
        input

    ALGORITHM:
        Create a hidden div with the same style as the textarea, then
        copy all the text into it, set the height of the textarea in
        pixels based on the height of the div, then delete the div.
    */
    var cell_input = get_cell(id), resizer = get_element('cell_resizer');

    if (!cell_input) {
        return;
    }

    // TODO: This isn't quite accurate for the most common sorts of
    // input cells.  In particular, backslashes appear to cause
    // miscalculation.  Perhaps scrollTop is a viabl alternative?
    resizer.style.width = cell_input.offsetWidth + 'px';
    resizer.innerHTML = cell_input.value
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/\r?\n/g, '<br>')
        .replace(/\s\s/g, ' &nbsp;') + '&nbsp;';

    cell_input.style.height = resizer.offsetHeight + 'px';

    if (slide_hidden) {
        cell_input.className = "cell_input_active";
        slide_hidden = false;
    }
    return;
}


function cell_delete(id) {
    /*
    Send a request back to the server that we would like to delete the
    cell with given id.

    INPUT:
        id -- integer or string; cell id
    */
    if ($.inArray(id, queue_id_list) !== -1) {
        // Deleting a running cell causes evaluation to be
        // interrupted.  In most cases this avoids potentially tons of
        // confusion.
        async_request(worksheet_command('interrupt'));
    }
    async_request(worksheet_command('delete_cell'), cell_delete_callback, {
        id: id
    });
    //update jmol applet list
    sage_jmol.delete_callback();
}


function cell_delete_callback(status, response) {
    /*
    Deletes a cell (removes it from the DOM and cell_id_list),
    depending on the server response.

    INPUT:

        status -- string
        response -- string; encoded JSON object with parsed keys

            id -- string or integer; deleted cell's id
            command -- string; 'delete' (delete the cell) or 'ignore'
            (do nothing)
            prev_id -- string or integer; id of preceding cell
            cell_id_list -- list; updated cell id list

    */
    var cell, X;

    if (status === "failure") {
        return;
    }

    X = decode_response(response);

    if (X.command === 'ignore') {
        // Don't delete, e.g., if there's only one compute cell left.
        return;
    }


    cell = get_element('cell_outer_' + X.id);
    if (!cell) {
        return;
    }
    get_element('worksheet_cell_list').removeChild(cell);
    cell_id_list.splice($.inArray(X.id, cell_id_list), 1);
    delete introspect[X.id];
    delete cell_element_cache[X.id];

    // If we are in slide mode, we call slide_mode() again to
    // recalculate the slides.
    if (in_slide_mode) {
        current_cell = -1;
        slide_mode();
        //update jmol applet list
        sage_jmol.delete_callback();
    }
}


function cell_delete_output(id) {
    /*
    Ask the server to delete the output of a cell.

    INPUT:
        id -- integer or string; cell id
    */
    id = toint(id);

    if ($.inArray(id, queue_id_list) !== -1) {
        // Deleting a running cell causes evaluation to be interrupted.
        // In most cases this avoids potentially tons of confusion.
        async_request(worksheet_command('interrupt'));
    }
    async_request(worksheet_command('delete_cell_output'),
                  cell_delete_output_callback, {
                      id: id
                  });
    //update jmol applet list
    sage_jmol.delete_callback();

}


function cell_delete_output_callback(status, response) {
    /*
    Callback for after the server deletes a cell's output.  This
    function removes the cell's output from the DOM.

    INPUT:
        status -- string ('success' or 'failure')
        response -- [command]SEP[id]
               command -- string ('delete_output')
               id -- id of cell whose output is deleted.
    */
    var X;
    if (status !== 'success') {
        // Do not delete output, for some reason.
        return;
    }
    X = decode_response(response);

    // Delete the output.
    get_element('cell_output_' + X.id).innerHTML = "";
    get_element('cell_output_nowrap_' + X.id).innerHTML = "";
    get_element('cell_output_html_' + X.id).innerHTML = "";

    // Set the cell to not evaluated.
    cell_set_not_evaluated(X.id);
    //update list of jmol applets
    sage_jmol.delete_callback();
}


function cell_input_key_event(id, e) {
    /*
    This function is called each time a key is pressed when the cursor
    is inside an input cell.

    INPUT:
        id -- integer or string; cell id
        e -- key event
    GLOBAL_INPUT:
        control_key_pressed -- used to detect if the control key was
        pressed; this is really only relevant to handling Opera's
        quirky even model.
    OUTPUT:
        All kinds of interesting things can happen:
            - introspection
            - cell join
            - cell split
            - cell delete
            - a cell may be evaluated
    */
    // TODO: Use js-hotkeys?
    var after, before, cell_input, i, selection_is_empty, selection_range;

    if (browser_iphone) {
        return;
    }

    e = new key_event(e);
    if (!e) {
        return;
    }

    /*********** SPLIT AND JOIN HANDLING ********/

    // Record that just the control key was pressed.  We do this since
    // on Opera it is the only way to catch control + key.
    if (key_control(e)) {
        control_key_pressed = 1;
        return;
    }

    id = toint(id);
    // Check for the split and join keystrokes.  The extra
    // control_key_pressed cases are needed for Safari.
    if (key_split_cell(e) ||
        (key_split_cell_noctrl(e) && control_key_pressed)) {
        doing_split_eval = false;
        split_cell(id);
        return false;
    } else if (key_spliteval_cell(e) ||
               (key_enter(e) && control_key_pressed)) {
        doing_split_eval = true;
        jump_to_cell(id, 1);
        control_key_pressed = 0;
        split_cell(id);
        return false;
    } else if (key_join_cell(e) ||
               (key_delete_cell(e) && control_key_pressed) ||
               (key_delete_cell(e) && is_whitespace(get_cell(id).value))) {
        control_key_pressed = 0;
        join_cell(id);
        return false;
    }

    // Turn off recording that the control key may have pressed last,
    // since we *only* would use that in the above if statement.
    // NOTE: This is only needed on Opera.
    control_key_pressed = 0;

    /*********** END of SPLIT AND JOIN HANDLING ********/

    cell_input = get_cell(id);

    if (introspect[id].loaded &&
        !handle_introspection(id, cell_input, e)) {
        return false;
    }

    selection_range = get_selection_range(cell_input);
    selection_is_empty = (selection_range[0] === selection_range[1]);

    // Will need IE version... if possible.
    if (!in_slide_mode && key_up_arrow(e) && selection_is_empty) {
        before = cell_input.value.substring(0, selection_range[0]);
        i = before.indexOf('\n');
        if (i === -1 || before === '') {
            jump_to_cell(id, -1, true);
            return false;
        } else {
            return true;
        }
    } else if (!in_slide_mode && key_down_arrow(e) && selection_is_empty) {
        after = cell_input.value.substring(selection_range[0]);
        i = after.indexOf('\n');
        if ((i === -1 || after === '') && id !== extreme_compute_cell(-1)) {
            jump_to_cell(id, 1);
            return false;
        } else {
            return true;
        }
    } else if (key_send_input(e)) {
        // User pressed shift-enter (or whatever the submit key is).
        doing_split_eval = false;
        evaluate_cell(id, false);
        return false;
    } else if (key_send_input_newcell(e)) {
        doing_split_eval = false;
        evaluate_cell(id, true);
        return false;
    } else if (key_comment(e) && !selection_is_empty) {
        return comment_cell(cell_input);
    } else if (key_uncomment(e) && !selection_is_empty) {
        return uncomment_cell(cell_input);
    } else if (key_unindent(e)) {
        // Unfortunately, shift-tab needs to get caught before
        // not-shift tab.
        unindent_cell(cell_input);
        return false;
    } else if (key_request_introspections(e) && selection_is_empty) {
        // Introspection: tab completion, ?, ??.
        evaluate_cell_introspection(id, null, null);
        return false;
    } else if (key_indent(e) && !selection_is_empty) {
        indent_cell(cell_input);
        return false;
    } else if (key_interrupt(e)) {
        interrupt();
        return false;
    } else if (key_page_down(e)) {
        if (in_slide_mode) {
            slide_next();
        } else {
            jump_to_cell(id, 5);
        }
        return false;
    } else if (key_page_up(e)) {
        if (in_slide_mode) {
            slide_prev();
        } else {
            jump_to_cell(id, -5);
        }
        return false;
    } else if (key_request_history(e)) {
        history_window();
    } else if (key_request_log(e)) {
        // TODO: Write a function text_log_window or do ...?
        text_log_window(worksheet_filename);
    } else if (key_fix_paren(e)) {
        paren_match(cell_input);
        return false;
    }

    // An actual non-controlling character was sent, which means this
    // cell has changed.  When the cursor leaves the cell, we'll use
    // this to know to send the changed version back to the server.
    // We do still have to account for the arrow keys which don't
    // change the text.
    if (! (key_up_arrow(e) || key_down_arrow(e) ||
           key_menu_right(e) || key_menu_left(e))) {
        cell_has_changed = true;
        introspect[id].changed = true;
    }

    return true;
}


function is_compute_cell(id) {
    /*
    Return true precisely if the input id is the id of a compute cell.

    INPUT:
        id -- integer or string; cell id
    */
    return (get_cell(id) !== null);
}


function extreme_compute_cell(dir) {
    /*
    Return the id of the first or last compute cell.

    INPUT:
        dir -- integer; the direction: if 1 (not 1) return the first
        (last) compute cell's id
    OUTPUT:
        id -- integer or string; the first or last compute cell's id
    */
    var i, id, len = cell_id_list.length;
    if (dir === 1) {
        for (i = 0; i < len; i += 1) {
            id = cell_id_list[i];
            if (is_compute_cell(id)) {
                break;
            }
        }
    } else {
        for (i = len - 1; i > -1; i -= 1) {
            id = cell_id_list[i];
            if (is_compute_cell(id)) {
                break;
            }
        }
    }
    return id;
}


function id_of_cell_delta(id, delta, all_cells) {
    /*
    Return the id of the cell that is delta positions from the cell
    with given id, where delta is an integer, either positive,
    negative, or 0.

    INPUT:
        id -- integer or string; cell id
        delta -- integer; offset "index"
        all_cells -- boolean; whether to ignore non-compute cells
    OUTPUT:
        integer or string
    */
    var i, j, len = cell_id_list.length, s;

    if (len === 0) {
        return;
    }
    if (!delta || delta === 0) {
        return id;
    }

    i = $.inArray(id, cell_id_list);
    if (i === -1) {
        return id;
        // Better not to move.
    } else {
        if (delta < 0) {
            delta = -delta;
            s = -1;
        } else {
            s = 1;
        }
        for (j = 0; j < delta; j += 1) {
            i = i + s;
            while (!all_cells && i >= 0 && i < len &&
                   !is_compute_cell(cell_id_list[i])) {
                i = i + s;
            }
        }
        if (i < 0) {
            i = 0;
        } else if (i >= len) {
            i = len - 1;
        }
        if (!all_cells && !is_compute_cell(cell_id_list[i])) {
            return id;
        }
        return cell_id_list[i];
    }
}

function jump_to_cell(id, delta, bottom) {
    /*
    Put the focus and cursor in the cell that is positioned delta
    spots above or below the cell with given id.  If bottom is true
    the cursor is positioned at the bottom of the cell that is put in
    focus.

     INPUT:
         id -- integer or string; cell id
         delta -- integer; offset "index"
         bottom -- boolean; whether to put cursor at end of cell
     GLOBAL INPUT:
         ignore_next_jump -- if this variable is set globally to true,
         then this function immediately returns after setting it to
         false.  This is used because several functions create new
         cells with unknown id's then jump to them (example, when
         inserting a new cell after the current one).  In some cases,
         e.g., when splitting or joining cells, it is necessary to
         temporarily disable this behavior, even though we do call
         those functions.  This is done by simply setting
         ignore_next_jump to true.
     OUTPUT:
         Changes the focused cell.  Does not send any information back
         to the server.
     */
    if (ignore_all_jumps) {
        return;
    }

    if (ignore_next_jump) {
        ignore_next_jump = false;
        return;
    }

    if (delta && delta !== 0) {
        id = id_of_cell_delta(id, delta);
    }

    if (in_slide_mode) {
        jump_to_slide(id);
    } else {
        cell_focus(id, bottom);
    }
}


function escape0(input) {
    /*
    Escape the string for sending via a URL; also replace all +'s by
    %2B.

    INPUT:
        input -- string
    OUTPUT:
        a string
    */
    //TODO: Use the built-in encodeURIComponent function.
    input = escape(input);
    input = input.replace(/\+/g, "%2B");
    return input;
}


function text_cursor_split(cell) {
    /*
    Returns a pair of substrings, the first from the start of the cell
    to the cursor position, and the second from the cursor position to
    the end of the cell.

    INPUT:
        cell -- DOM textarea; an input cell
    OUTPUT:
        array of two strings
    */
    var a, b, R = get_selection_range(cell);
    b = cell.value.substr(0, R[1]);
    a = cell.value.substr(b.length);
    return [b, a];
}


function indent_cell(cell) {
    /*
    Indent all the highlighted text in the given input cell by 4
    spaces.

    INPUT:
        cell -- DOM textarea; an input cell
    */
    var a, b, c, i, lines, R = get_selection_range(cell), start;

    start = 1 + cell.value.lastIndexOf("\n", R[0]);
    a = cell.value.substring(0, start);
    b = cell.value.substring(start, R[1]);
    c = cell.value.substring(R[1]);

    lines = b.split("\n");

    for (i = 0; i < lines.length; i += 1) {
        lines[i] = "    " + lines[i];
    }

    b = lines.join("\n");

    cell.value = a + b + c;
    set_selection_range(cell, a.length, a.length + b.length);
}


function unindent_cell(cell) {
    /*
    Unindent the current line or highlighted text in the given input cell
    by 4 spaces.

    INPUT:
        cell -- DOM textarea; an input cell
    */
    var a, b, c, i, lines, R = get_selection_range(cell), start;

    start = 1 + cell.value.lastIndexOf("\n", R[0]);
    a = cell.value.substring(0, start);
    b = cell.value.substring(start, R[1]);
    c = cell.value.substring(R[1]);

    lines = b.split("\n");

    for (i = 0; i < lines.length; i += 1) {
        // Square brackets pull the captured pattern.
        lines[i] = unindent_pat.exec(lines[i])[1];
    }

    b = lines.join("\n");

    cell.value = a + b + c;
    if (R[0] === R[1]) { // nothing is selected
        set_cursor_position(cell, a.length + b.length);
    } else {
        set_selection_range(cell, a.length, a.length + b.length);
    }
}


function comment_cell(cell) {
    /*
    Comment out all the highlighted (selected) text in the given input
    cell.

    INPUT:
        cell -- DOM textarea; an input cell
    */
    var a, b, c, i, lines, R = get_selection_range(cell), start;

    if (R[0] === R[1]) {
        return true;
    }

    start = 1 + cell.value.lastIndexOf("\n", R[0]);
    a = cell.value.substring(0, start);
    b = cell.value.substring(start, R[1]);
    c = cell.value.substring(R[1]);

    lines = b.split("\n");

    for (i = 0; i < lines.length; i += 1) {
        lines[i] = "#" + lines[i];
    }

    b = lines.join("\n");

    cell.value = a + b + c;
    set_selection_range(cell, a.length, a.length + b.length);
}


function uncomment_cell(cell) {
    /*
    Uncomment the highlighted (selected) text in the given input cell.

    INPUT:
        cell -- DOM textarea; an input cell
    */
    var a, b, c, i, lines, m, R = get_selection_range(cell), start;

    if (R[0] === R[1]) {
        return true;
    }

    start = 1 + cell.value.lastIndexOf("\n", R[0]);
    a = cell.value.substring(0, start);
    b = cell.value.substring(start, R[1]);
    c = cell.value.substring(R[1]);

    lines = b.split("\n");

    for (i = 0; i < lines.length; i += 1) {
        m = uncomment_pat.exec(lines[i]);
        lines[i] = m[1] + m[2];
    }

    b = lines.join("\n");

    cell.value = a + b + c;
    set_selection_range(cell, a.length, a.length + b.length);
}


function join_cell(id) {
    /*
    Join the cell with given id to the cell before it.

    The output of the resulting joined cells is the output of the
    second cell, *unless* the input of the second cell is only
    whitespace, in which case the output is the output of the first
    cell.  We do this since a common way to delete a cell is to empty
    its input, then hit backspace.  It would be very confusing if the
    output of the second cell were retained.  WARNING: Backspace on
    the first cell if empty deletes it.

    INPUT:
        id -- integer or string; cell id.
    OUTPUT:
        change the state of the worksheet in the DOM, global
        variables, etc., and updates the server on this change.
    */
    var cell, cell_next, cell_prev, id_prev, n, val_prev;

    id_prev = id_of_cell_delta(id, -1);
    cell = get_cell(id);

    // The top cell is a special case.  Here we delete the top cell if
    // it is empty.  Otherwise, we simply return doing nothing.
    if (id_prev === id) {
        // Yes, top cell.
        if (is_whitespace(cell.value)) {
            // Special case -- deleting the first cell in a worksheet
            // and its whitespace get next cell
            cell_next = get_cell(id_of_cell_delta(id, 1));
            // Put cursor on next one.
            cell_next.focus();
            // Delete this cell.
            cell_delete(id);
            return;
        } else {
            return;
        }
    }

    cell_prev = get_cell(id_prev);

    // We delete the cell above the cell with given id except in the
    // one case when the cell with id has empty input, in which case
    // we just delete that cell.
    if (is_whitespace(cell.value)) {
        cell_prev.focus();
        cell_delete(id);
        return;
    }

    // The lower cell in the join is now not empty.  So we delete the
    // previous cell and put its contents in the bottom cell.
    val_prev = cell_prev.value;

    cell.focus();
    cell_delete(id_prev);

    // The following is so that joining two cells keeps a newline
    // between the input contents.
    n = val_prev.length;
    if (val_prev[n - 1] !== '\n') {
        val_prev += '\n';
        n += 1;
    }
    cell.value = val_prev + cell.value;

    // Send a message back to the server reporting that the cell has
    // changed (as a result of joining).
    send_cell_input(id);

    // Set the cursor position in the joined cell to about where it
    // was before the join.
    set_cursor_position(cell, n);

    // Finally resize the joined cell to account for its new text.
    cell_input_resize(id);
}


function split_cell(id) {
    /*
    Split the cell with the given id into two cells, inserting a new
    cell after the current one and placing the cursor at the beginning
    of the new cell.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
        changes the state of the worksheet, DOM, and sends a message
        back to the server.
    */
    var cell = get_cell(id), txt = text_cursor_split(cell);

    if (txt[1].length > 0 && txt[1][0] === '\n') {
        txt[1] = txt[1].slice(1);
    }

    cell.value = txt[1];
    cell_input_resize(id);
    send_cell_input(id);
    // Tell the server about how the input just got split in half.

    set_cursor_position(cell, 0);

    // Make sure that the cursor doesn't move to the new cell.
    ignore_next_jump = true;
    insert_new_cell_before(id, txt[0]);
}


function worksheet_command(cmd) {
    /*
    Create a string formatted as a URL to send back to the server and
    execute the given cmd on the current worksheet.

    INPUT:
        cmd -- string
    OUTPUT:
        a string
    */
    if (cmd === 'eval' 
	|| cmd === 'new_cell_before' 
	|| cmd === 'new_cell_after'
	|| cmd === 'new_text_cell_before'
	|| cmd === 'new_text_cell_after') {
        state_number = parseInt(state_number, 10) + 1;
    }
    // worksheet_filename differs from actual url for public interacts
    // users see /home/pub but worksheet_filename is /home/_sage_
    return ('/home/' + worksheet_filename + '/' + cmd);
}


function evaluate_cell(id, newcell) {
    /*
    Evaluates a cell.

    INPUT:
        id -- integer or string; cell id
        newcell -- boolean; whether to request insertion of a new cell
                   after the current one
    */
    var cell_input;

    if (worksheet_locked) {
        alert(translations['This worksheet is read only. Please make a copy or contact the owner to change it.']);
        return;
    }

    // Does the input cell exist?
    id = toint(id);
    cell_input = get_cell(id);
    if (!cell_input) {
        return;
    }

    // Request a new cell to insert after this one?
    newcell = (newcell || (id === extreme_compute_cell(-1))) ? 1 : 0;
    if (evaluating_all) {
        newcell = 0;
    }

    // Don't resend the input to the server upon leaving focus (see
    // send_cell_input).
    cell_has_changed = false;

    // Ask the server to start computing.
    async_request(worksheet_command('eval'), evaluate_cell_callback, {
        newcell: newcell,
        id: id,
        input: cell_input.value
    });
    //update jmol applet list
    sage_jmol.delete_callback();
}


function evaluate_cell_introspection(id, before, after) {
    /*
    Runs introspection in a cell.

    INPUT:
        id -- integer or string; the id a cell
        before -- null or string; all text before the cursor
        after -- null or string; all text after the cursor
    OUTPUT:
        sends a message back to the server to do an introspection on
        this cell; also set the cell running.
    */
    var cell_input, f, in_text, intr, m;
    id = toint(id);
    intr = introspect[id];
    cell_input = get_cell(id);

    if (before === null) {
        // We're starting from scratch.
        halt_introspection(id);

        in_text = text_cursor_split(cell_input);
        before = in_text[0];
        after = in_text[1];
        intr.before_replacing_word = before;
        intr.after_cursor = after;

        m = command_pat.exec(before);
        f = function_pat.exec(before);

        if (before.slice(-1) === "?") {
            // We're starting with a docstring or source code.
            intr.docstring = true;
        } else if (m) {
            // We're starting with a list of completions.
            intr.replacing = true;
            intr.replacing_word = m[1];
            intr.before_replacing_word = before.substring(0, before.length -
                                                          m[1].length);
        } else if (f !== null) {
            // We're in an open function paren -- give info on the
            // function.
            before = f[1] + "?";
            // We're starting with a docstring or source code.
            intr.docstring = true;
        } else {
            // Just a tab.
            cell_has_changed = true;
            do_replacement(id, '    ', false);
            return;
        }
    }

    async_request(worksheet_command('introspect'), evaluate_cell_callback, {
        id: id,
        before_cursor: before,
        after_cursor: after
    });
}


function evaluate_cell_callback(status, response) {
    /*
    Updates the queued cell list, marks a cell as running, changes the
    focus, inserts a new cell, and/or evaluates a subsequent cell,
    depending on the server response.  Also starts the cell update
    check.

    INPUT:
        status -- string
        response -- string; encoded JSON object with parsed keys

            id -- string or integer; evaluated cell's id
            command -- string; 'insert_cell', 'no_new_cell', or
            'introspect'
            next_id -- optional string or integer; next cell's id
            interact -- optional boolean; whether we're updating an
            interact
            new_cell_html -- optional string; new cell's contents
            new_cell_id -- optional string or integer; id of new cell
            to create

    */
    var X;
    if (status === "failure") {
        // alert("Unable to evaluate cell.");
        return;
    }

    X = decode_response(response);
    X.interact = X.interact ? true : false;

    if (X.id === -1) {
        // Something went wrong, e.g., the evaluated cell doesn't
        // exist.  TODO: Can we remove this?
        return;
    }

    if (X.command && (X.command.slice(0, 5) === 'error')) {
	// TODO: (Re)use an unobtrusive jQuery UI dialog.
    // console.log(X, X.id, X.command, X.message);
        return;
    }

    // Given a "successful" server response, we update the queued cell
    // list and mark the cell as running.
    queue_id_list.push(X.id);
    cell_set_running(X.id, X.interact);

    function go_next(evaluate, jump) {
        // Helper function that evaluates and/or jumps to a suggested
        // or the next compute cell, unless it's the current cell or
        // we're just updating an interact.
        var i, id, candidates = [X.next_id, id_of_cell_delta(X.id, 1)];

        if (X.interact) {
            return true;
        }
        for (i = 0; i < candidates.length; i += 1) {
            id = candidates[i];
            if (id !== X.id && is_compute_cell(id)) {
                if (evaluate) {
                    evaluate_cell(id, false);
                }
                if (jump) {
                    jump_to_cell(id, 0);
                }
                return true;
            }
        }
        return false;
    }

    if (evaluating_all) {
        // Evaluate the suggested next cell, another cell, or stop.
        if (!go_next(true, false)) {
            evaluating_all = false;
        }
    }

    if (X.command === 'insert_cell') {
        // Insert a new cell after the evaluated cell.
        do_insert_new_cell_after(X.id, X.new_cell_id, X.new_cell_html);
        jump_to_cell(X.new_cell_id, 0);
    } else if (X.command === 'introspect') {
	    introspect[X.id].loaded = false;
	    update_introspection_text(X.id, 'loading...');
    } else if (in_slide_mode || doing_split_eval ||
	           is_interacting_cell(X.id)) {
        // Don't jump.
    } else {
        // "Plain" evaluation.  Jump to a later cell.
        go_next(false, true);
    }

    start_update_check();
    //update jmol applet list
    sage_jmol.delete_callback();
}


function is_interacting_cell(id) {
    /*
    Return true if the cell with given id is currently an @interact
    cell.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
        boolean
    */
    return (get_element("cell-interact-" + id) !== null);
}


function cell_output_set_type(id, typ, do_async) {
    /*
    Set the output type of the cell with given id.

    INPUT:
        id -- integer or string; cell id
        typ -- string; 'wrap', 'nowrap', 'hidden'
        do_async -- boolean; whether to inform the server
    */

    // We do the following specifically because interact cells do not
    // work at all when displayed in nowrap mode, which is VERY BAD.
    // So instead for interacts one gets a toggle to and from hidden.
    if (typ === "nowrap" && is_interacting_cell(id)) {
        // If the type is nowrap and the cell-interact-[id] div exists
        // (i.e., we are interacting) then just make the thing hidden.
        typ = "hidden";
    }

    /* OK, now set the cell output type. */
    set_class('cell_div_output_' + id, 'cell_div_output_' + typ);
    set_class('cell_output_' + id, 'cell_output_' + typ);
    set_class('cell_output_nowrap_' + id, 'cell_output_nowrap_' + typ);
    set_class('cell_output_html_' + id, 'cell_output_html_' + typ);

    // Do async request back to the server.
    if (do_async) {
        async_request(worksheet_command('set_cell_output_type'), null, {
            id: id,
            type: typ
        });
    }
}


function cycle_cell_output_type(id) {
    /*
    When called the cell with given id has its output cycled from one
    type to the next.  There are three types: word wrap, no word wrap,
    hidden.

    INPUT:
        id -- integer or string; cell id
    */
    var cell_div;
    id = toint(id);
    cell_div = get_element('cell_div_output_' + id);

    if (cell_div.className === 'cell_div_output_hidden' ||
        cell_div.className === 'cell_div_output_running') {
        cell_output_set_type(id, 'wrap');
        return;
    }

    if (cell_div.className === 'cell_div_output_wrap') {
        cell_output_set_type(id, 'nowrap');
    } else {
        cell_output_set_type(id, 'hidden');
    }
}


function cell_set_evaluated(id) {
    /*
    Set the cell with given id to be evaluated.  This is purely a CSS
    style setting.

    INPUT:
        id -- integer or string; cell id
    */
    var D = get_element('cell_' + id);
    D.className = "cell_evaluated";
}


function cell_set_not_evaluated(id) {
    /*
    Set the cell with given id to be not evaluated.  This is purely a
    CSS style setting.

    INPUT:
        id -- integer or string; cell id
    */
    var D = get_element('cell_' + id);
    D.className = "cell_not_evaluated";
    cell_set_done(id);
}


function cell_set_running(id, interact) {
    /*
    Clears a cell's output and marks it as running.  This does not
    contact the server.

    INPUT:
        id -- integer or string; cell id
        interact -- boolean; whether we're just updating an interact
    */
    var cell_div, cell_number, out;
    id = toint(id);

    if (interact) {
        // Delete links to files output by earlier computations.
        out = get_element('cell_output_html_' + id);
        if (out) {
            out.innerHTML = '';
        }
    } else {
        // Delete all output. The true means not @interact.
        set_output_text(id, '', '', '', '', '', true);

        // If the output type is hidden, make it visible.
        cell_div = get_element('cell_div_output_' + id);
        if (cell_div.className ===  'cell_div_output_hidden') {
            cycle_cell_output_type(id);
        }
        // Now set it running.
        cell_div.className = 'cell_output_running';
    }

    cell_number = get_element('cell_number_' + id);
    cell_number.className = 'cell_number_running';
}


function cell_set_done(id) {
    /*
    Change the CSS for the cell with given id to indicate that it is
    no longer computing.

    INPUT:
        id -- integer or string; cell id
    */
    var cell_div, cell_number;

    cell_div = get_element('cell_div_output_' + id);
    cell_div.className = 'cell_div_output_wrap';
    cell_number = get_element('cell_number_' + id);
    cell_number.className = 'cell_number';
}


function check_for_cell_update() {
    /*
    Ask the server if there is any new output that should be placed in
    an output cell.

    OUTPUT:
        * if the queued cell list is empty, cancel update checking.
        * makes an async request
        * causes the title bar compute spinner to spin
    */
    var cell_id, busy_text, num_queued;

    // Cancel update checks if no cells are doing computations.
    if (queue_id_list.length === 0) {
        cancel_update_check();
        return;
    }

    // Record in a global variable when the last update occurred.
    update_time = time_now();

    // Check on the "lead" cell currently computing to see what's up.
    cell_id = queue_id_list[0];

    async_request(worksheet_command('cell_update'),
                  check_for_cell_update_callback, { 
                      id: cell_id 
                  });

    // Spin the little title spinner in the title bar.
    try {
        title_spinner_i = (title_spinner_i + 1) % title_spinner.length;
        busy_text = title_spinner[title_spinner_i] + original_title;
        num_queued = queue_id_list.length;
        if (num_queued > 1) {
            busy_text = num_queued + ' ' + busy_text;
        }
        document.title = busy_text;
    } catch (e) {}
}


function check_for_cell_update_callback(status, response) {
    /*
    Updates cell data from the server

    INPUT:
        status -- string
        response -- string; encoded JSON object with parsed keys

            id -- string or integer; queried cell's id
            status -- string; 'e' (empty queue), 'd' (done with
                      queried cell), or 'w' (still working)
            output -- string; cell's latest output text
            output_wrapped -- string; word-wrapped output
            output_html -- string; HTML output
            new_input -- string; updated input (e.g., from tab
                         completion)
            interrupted -- string; 'restart', 'false', or 'true',
                           whether/how the cell's computation was 
                           interrupted
            introspect_html -- string; updated introspection text
    */
    var elapsed_time, eval_hook, msg, X;

    // Make sure the update happens again in a few hundred
    // milliseconds, unless a problem occurs below.
    if (status !== "success") {
        // A problem occured -- stop trying to evaluate.
        if (update_error_count > update_error_threshold) {
            cancel_update_check();
            halt_queued_cells();
            elapsed_time = update_error_count * update_error_delta / 1000;
            msg = translations['Error updating cell output after '] + " " + elapsed_time + translations['s (canceling further update checks).'];
            
            /* alert(msg); */
            return;
        }
        cell_output_delta = update_error_delta;
        update_error_count += 1;
        continue_update_check();
        return;
    } else {
        if (update_error_count > 0) {
            update_error_count = 0;
            update_count = 0;
            update_falloff_level = 1;
            cell_output_delta = update_falloff_deltas[1];
        }
    }

    if (response === '') {
        // If the server returns nothing, we just ignore that response
        // and try again later.
        continue_update_check();
        return;
    }

    X = decode_response(response);

    if (X.status === 'e') {
        cancel_update_check();
        halt_queued_cells();
        return;
    }

    // Evaluate and update the cell's output.
    eval_hook = set_output_text(X.id, X.status, X.output, X.output_wrapped,
                                X.output_html, X.introspect_html, false);

    if (X.status === 'd') {
        cell_set_done(X.id);
        queue_id_list.splice($.inArray(X.id, queue_id_list), 1);

        if (X.interrupted === 'restart') {
            restart_sage();
        } else if (X.interrupted === 'false') {
            cell_set_evaluated(X.id);
        } else {
            cancel_update_check();
            halt_queued_cells();
        }

        if (queue_id_list.length === 0) {
            cancel_update_check();
        }

        if (X.new_input !== '') {
            set_input_text(X.id, X.new_input);
        }

        update_count = 0;
        update_falloff_level = 0;
        cell_output_delta = update_falloff_deltas[0];
    } else {
        if (update_count > update_falloff_threshold &&
            update_falloff_level + 1 < update_falloff_deltas.length) {
            update_falloff_level += 1;
            update_count = 0;
            cell_output_delta = update_falloff_deltas[update_falloff_level];
        } else {
            update_count += 1;
        }
    }

    if (eval_hook === 'trigger_interact') {
         interact(X.id, {}, 1);
    } else if (eval_hook === 'restart_interact') {
        evaluate_cell(X.id, 0);
    }

    continue_update_check();
}


function continue_update_check() {
    /*
    If enough time has elapsed, check for more output from the server.
    If not, wait longer and try again later.

    GLOBAL INPUT:
        update_time -- global variable that records when last update
        check occurred.
    */
    var time_elapsed = time_now() - update_time;
    if (time_elapsed < cell_output_delta) {
        update_timeout = setTimeout(function () {
            check_for_cell_update();
        }, cell_output_delta - time_elapsed);
    } else {
        check_for_cell_update();
    }
}


function start_update_check() {
    /*
    Start the updating check system.  This system checks for update
    from the server with an exponential backup strategy.
    */
    if (updating) {
        return;
    }

    // Set several global variables that cells are computing so we
    // have to check for output.
    updating = true;
    update_count = 0;
    update_falloff_level = 0;

    // The starting value for how long we wait between checks for new
    // updates.
    cell_output_delta = update_falloff_deltas[0];

    // Do one initial check without waiting, since some calculations
    // are very fast and doing this feels snappy.
    check_for_cell_update();
}


function cancel_update_check() {
    /*
    Turn off the loop that checks for now output updates in the
    worksheet.

    This just cancels the updating timer and gets rid of the spinning
    "active" indicator in the title bar.
    */
    updating = false;
    clearTimeout(update_timeout);
    document.title = original_title;
    reset_interrupts();
}


function set_output_text(id, status, output_text, output_text_wrapped,
                         output_html, introspect_html, no_interact) {
    /*
    Evaluate and update a cell's output.

    INPUT:
        id -- integer or string; cell id
        status -- string; 'd' (done) or anything else (still working)
        output_text -- string
        output_text_wrapped -- string; word wrapped version of text
        output_html -- string; html formatted output
        introspect_html -- string; when user is introspecting this
        html will go in the introspection dialog box
        no_interact -- boolean; whether to consider this @interact
        output
    OUTPUT:
        a boolean or a string; whether to trigger or restart an
        interact cell
    */
    var cell_interact, cell_output, cell_output_html, cell_output_nowrap, i, j, new_interact_output;

    if (typeof(id) === 'number' && id < 0) {
        // Negative id's come up for special internal usage, and
        // should be ignored.
        return false;
    }

    // Evaluate javascript, but *only* after the entire cell output
    // has been loaded (hence the status === 'd') below.
    if (status === 'd' && !is_interacting_cell(id)) {
        output_text_wrapped = eval_script_tags(output_text_wrapped);
        output_html = eval_script_tags(output_html);
    }

    // Handle an interact update.
    if (!no_interact && is_interacting_cell(id)) {
        // Comment this out to show output only at the end.
        if (status !== 'd') {
            return false;
        }

        i = output_text_wrapped.indexOf(INTERACT_START);
        j = output_text_wrapped.indexOf(INTERACT_END);
        // An error occurred accessing the data for this cell.  Just
        // force reload of the cell, which will certainly define that
        // data.
        if (output_text_wrapped.indexOf(INTERACT_RESTART) !== -1) {
            return 'restart_interact';
        }
        if (i === -1 || j === -1) {
            return false;
        }

        new_interact_output = output_text_wrapped.slice(i + 16, j);
        new_interact_output = eval_script_tags(new_interact_output);

        cell_interact = get_element('cell-interact-' + id);
        cell_interact.innerHTML = new_interact_output;
        MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_interact]);

        return false;
    }
    // Fill in output text got so far.
    cell_output = get_element('cell_output_' + id);
    if (!cell_output) {
        // This can happen, e.g., if a cell is deleted from the DOM,
        // but the server has some output it still wants to put in the
        // cell.  This happens because once a cell is running there is
        // no stopping it beyond an explicit interrupt (since
        // interrupt may or may not succeed -- this is the real world
        // with hard to kill C code, etc.).
        return false;
    }
    cell_output_nowrap = get_element('cell_output_nowrap_' + id);
    cell_output_html = get_element('cell_output_html_' + id);
    cell_output.innerHTML = output_text_wrapped;
    cell_output_nowrap.innerHTML = output_text;
    cell_output_html.innerHTML = output_html;

    // Call MathJax on the final output.
    if (status === 'd' ) {
        try {
            MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_output]);
        } catch (e) {
            cell_output.innerHTML = 'Error typesetting mathematics' + cell_output.innerHTML;
            cell_output_nowrap.innerHTML = 'Error typesetting mathematics' +
                cell_output_nowrap.innerHTML;
        }
    }

    // Update introspection.
    if (status === 'd') {
        if (introspect_html !== '') {
            introspect[id].loaded = true;
            update_introspection_text(id, introspect_html);
        } else {
            halt_introspection(id);
        }
    }

    // Trigger a new interact cell?
    if (status === 'd' && introspect_html === '' && is_interacting_cell(id)) {
        // This is the first time that the underlying Python interact
        // function (i.e., interact.recompute) is actually called!
        return 'trigger_interact';
    }

    return false;
}


function set_input_text(id, text) {
    /*
    Fill in input text for the cell with given id.  This is used by
    the tab completion system, so it also sets the cell with given id
    to be in focus and positions the cursor in exactly the right spot.

    INPUT:
        id -- integer or string; cell id
        text -- a string
    */
    var cell_input = get_cell(id), pos;
    cell_input.value = text;

    jump_to_cell(id, 0);
    pos = text.length - introspect[id].after_cursor.length;
    set_cursor_position(cell_input, pos);

    return false;
}


///////////////////////////////////////////////////////////////////
// Dynamic evaluation of javascript related in cell output.
///////////////////////////////////////////////////////////////////
function CellWriter() {
    /*TODO: Jmol does not use anymore. Remove if nothing else depends on it.
    When a new cell is loaded, this class is used to let javascript
    write directly to the document. After that, make sure javascript
    writes to a CellWriter object.  This is used in order to get jmol
    to work.
    */
    this.buffer = "";
    this.write = function (s) {
        this.buffer += s;
    };
}


function eval_script_tags(text) {
    /*
    Find all the tags in the given script and eval them, where tags
    are javascript code in <script>...</script> tags.  This allows us
    put javascript in the output of computations and have it
    evaluated.  Moreover, if the javascript writes to the global
    cell_writer object (with cell_write.write(string)), then that
    output is textually substituted in place of the
    <script>...</script>.

    INPUT:
        text -- string
    OUTPUT
        string with all script tags removed
    */

    var i, j, k, left_tag, right_tag, s, script, new_text, script_end, left_match;

    left_tag = new RegExp(/<(\s)*script([^>]*)?>/i);
    right_tag = new RegExp(/<(\s*)\/(\s*)script(\s)*>/i);

    script = '';
    new_text='';
    s = text;
    i = s.search(left_tag);
    while (i !== -1) {
        left_match = s.match(left_tag);
        j = s.search(right_tag);
        k = i + (left_match[0] + '').length;
	script_end=j + (s.match(right_tag)[0] + '').length;
        if (j === -1 || j < k) {
            break;
        }
        // MathJax uses the script tag with a type='math/tex(display|inline)'
        // to encode characters (as a sort of CDATA thing).  We *don't* want
        // to execute these script tags since they need to appear inline.
        if (!left_match[2] || left_match[2].indexOf('math/tex')===-1) {
            code = s.slice(k, j);
            try {
		cell_writer = new CellWriter();
		eval(code);
            } catch (e) {
		alert(e);
            }
            new_text += s.slice(0, i) + cell_writer.buffer;
	} else {
	    new_text += s.slice(0, script_end);
	}
        s = s.slice(script_end);
        i = s.search(left_tag);
    }
    new_text+=s;
    return new_text;
}


function separate_script_tags(text) {
    /*
    Find all the tags in the given script and return a list of two
    strings.  The first string is the html in text, the second is the
    contents of any <script>...</script> tags.

    INPUT:
        text -- string
    OUTPUT
        list of two strings:
            [text w/o script tags (but includes math/tex script tags), content of script tags]


    This is similar to what jQuery does when inserting html.
    See http://stackoverflow.com/questions/610995/jquery-cant-append-script-element
    */
    var i, j, k, left_tag, right_tag, s, script, new_text, script_end, left_match;
    
    left_tag = new RegExp(/<(\s)*script([^>]*)?>/i);
    right_tag = new RegExp(/<(\s*)\/(\s*)script(\s)*>/i);

    script = '';
    new_text='';
    s = text;
    i = s.search(left_tag);
    while (i !== -1) {
        left_match = s.match(left_tag);
        j = s.search(right_tag);
        k = i + (left_match[0] + '').length;
	script_end=j + (s.match(right_tag)[0] + '').length;
        if (j === -1 || j < k) {
            break;
        }
        // MathJax uses the script tag with a type='math/tex(display|inline)'
        // to encode characters (as a sort of CDATA thing).  We *don't* want
        // to extract these script tags since they need to appear inline.
        if (!left_match[2] ||  left_match[2].indexOf('math/tex') === -1) {
            script += s.slice(k, j);
	    new_text += s.slice(0, i);
	} else {
	    new_text += s.slice(0, script_end);
	}
        s = s.slice(script_end);
        i = s.search(left_tag);
    }
    new_text+=s;
    return [new_text, script];
}


///////////////////////////////////////////////////////////////////
// Single Cell Functions
///////////////////////////////////////////////////////////////////
function slide_mode() {
    /*
    Switch into single cell mode.  This involves changing a bunch of
    CSS and some global variables.
    */
    var i, id, len = cell_id_list.length;

    in_slide_mode = true;
    set_class('left_pane', 'hidden');
    set_class('cell_controls', 'hidden');
    set_class('slide_controls', 'slide_control_commands');
    set_class('left_pane_bar', 'hidden');

    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (is_compute_cell(id)) {
            set_class('cell_outer_' + id, 'hidden');
        }
    }
    slide_show();
}


function cell_mode() {
    /*
    Switch from single cell mode back to normal worksheet mode.  This
    involves changing CSS and global variables.
    */
    var i, id, len = cell_id_list.length;

    in_slide_mode = false;
    set_class('left_pane', 'pane');
    set_class('cell_controls', 'control_commands');
    set_class('slide_controls', 'hidden');
    set_class('worksheet', 'worksheet');
    set_class('left_pane_bar', 'left_pane_bar');

    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (is_compute_cell(id)) {
            set_class('cell_outer_' + id, 'cell_visible');
        }
    }
}


function slide_hide() {
    /*
    Hide the currently displayed slide.

    GLOBAL INPUT:
        current_cell -- integer
    */
    set_class('cell_outer_' + current_cell, 'hidden');
}


function slide_show() {
    /*
    Switch into slide show mode.  This involves changing a lot of CSS
    in the DOM.
    */
    var input, s;

    if (current_cell !== -1) {
        set_class('cell_outer_' + current_cell, 'cell_visible');
    } else {
        if (cell_id_list.length > 0) {
            current_cell = extreme_compute_cell(1);
        }
        set_class('cell_outer_' + current_cell, 'cell_visible');
    }

    if (current_cell !== -1) {
        input = get_cell(current_cell);
        if (!input) {
            s = lstrip(input.value).slice(0, 5);
            cell_focus(current_cell, false);
            if (s === '%hide') {
                slide_hidden = true;
                input.className = 'cell_input_hide';
                input.style.height = '1.5em';
            }
        }
    }
    update_slideshow_progress();
}


function slide_first() {
    /*
    Move to the first input cell in single cell mode.

    GLOBAL INPUT:
        the first cell is the first entry in the cell_id_list.
    */
    jump_to_slide(extreme_compute_cell(1));
}


function slide_last() {
    /*
    Move to the last input cell in single cell mode.

    GLOBAL INPUT:
        the last cell is the last entry in the cell_id_list.
    */
    jump_to_slide(extreme_compute_cell(-1));
}


function slide_next() {
    /*
    Move to the next cell in single cell mode.
    */
    jump_to_slide(id_of_cell_delta(current_cell, 1));
}


function slide_prev() {
    /*
    Move to the previous cell in single cell mode.
    */
    jump_to_slide(id_of_cell_delta(current_cell, -1));
}


function jump_to_slide(id) {
    /*
    Move to the display only the cell with the given id.

    INPUT:
        id -- integer or string; cell id
    OUTPUT:
        sets the global variable current_cell to the id.
    */
    slide_hide();
    current_cell = toint(id);
    slide_show();
}


function update_slideshow_progress() {
    /*
    There is a bar at the top of the screen that shows how far through
    the worksheet we currently are in the list of cells (in single
    cell mode).  This function updates the CSS of that "progress
    meter" to have the right percentage filled in.
    */
    var bar, i, n, text;

    i = $.inArray(current_cell, cell_id_list) + 1;
    n = cell_id_list.length;
    bar = get_element("slideshow_progress_bar");
    if (bar) {
        bar.style.width = "" + 100 * i / n + "%";
    }
    text = get_element("slideshow_progress_text");
    if (text) {
        text.innerHTML = i + " / " + n;
    }
}


///////////////////////////////////////////////////////////////////
// Insert and move cells
///////////////////////////////////////////////////////////////////
function make_new_cell(id, html) {
    /*
    Create a new cell in the DOM with given id and html defining it.
    This does not send a message back to the server.

    INPUT:
        id -- integer or string; cell id
        html -- string
    */
    var in_cell, new_cell, new_html;

    new_html = separate_script_tags(html);

    new_cell = document.createElement("div");
    in_cell = document.createElement("div");

    new_cell.appendChild(in_cell);

    new_cell.id = 'cell_outer_' + id;
    in_cell.id = 'cell_' + id;

    in_cell.innerHTML = new_html[0];
    setTimeout(new_html[1], 50);

    halt_introspection(id);
    return new_cell;
}


function make_new_text_cell(id, html) {
    /*
    Create a new cell in the DOM with given id and html defining it.
    This does not send a message back to the server.

    INPUT:
        id -- integer or string; cell id
        html -- string
    */
    var new_cell, new_html;
    new_html = separate_script_tags(html);
    new_cell = $(new_html[0]);
    setTimeout(new_html[1], 50);
    return new_cell;
}


function do_insert_new_cell_before(id, new_id, new_html) {
    /*
    Insert a new cell with the given new_id and new_html before the
    cell with given id.

    INPUT:
        id -- integer or string; cell id
        new_id -- integer or string; new cell's id
        new_html -- string; new cell's HTML
    GLOBAL INPUT:
        doing_split_eval -- boolean; whether both cells id and new_id
    */
    var i, new_cell = make_new_cell(new_id, new_html);
    $('#cell_outer_' + id).before(new_cell);

    i = $.inArray(id, cell_id_list);
    cell_id_list.splice(i, 0, new_id);

    // Deal with one special case when this function is called, where
    // we evaluate both of the cells that result from a split.  It is
    // annoying to put this code here, but it is much simpler than
    // coding it in any other way, because we don't know the new_id
    // until this point.
    if (doing_split_eval) {
        evaluate_cell(id, false);
        evaluate_cell(new_id, false);
    }
}


function do_insert_new_text_cell_before(id, new_id, new_html) {
    /*
    Insert a new cell with the given new_id and new_html before the
    cell with given id.

   INPUT:
        id -- integer or string; cell id
        new_id -- integer or string; new cell's id
        new_html -- string; new cell's HTMl
     */
    var i, new_cell = make_new_text_cell(new_id, new_html);
    $('#cell_outer_' + id).before(new_cell);

    i = $.inArray(id, cell_id_list);
    cell_id_list.splice(i, 0, new_id);
}


function insert_new_cell_after(id, input) {
    /*
    Send a message back to the server requesting that a new cell with
    the given input be inserted before the cell with given id.

    INPUT:
        id -- integer or string; cell id
        input -- string (default: ''); cell input text
    */
    input = input || '';
    async_request(worksheet_command('new_cell_after'),
                  insert_new_cell_after_callback,
                  { id: id, input: input });
}


function insert_new_cell_after_callback(status, response) {
    /*
    Callback that is called when the server inserts a new cell after a
    given cell.

    INPUT:
        status -- string
        response -- string; 'locked' (user not allowed to insert new
        cells) or encoded JSON object with parsed keys

            id -- string or integer; preceding cell's id
            new_id -- string or integer; new cell's id
            new_html -- string; new cell's HTML

    */
    var X;

    if (status === "failure") {
        alert(translations['Problem inserting new input cell after current input cell.\\n'] + response);
        return;
    }
    if (response === "locked") {
        alert(translations['Worksheet is locked. Cannot insert cells.']);
        return;
    }

    X = decode_response(response);
    do_insert_new_cell_after(X.id, X.new_id, X.new_html);
    jump_to_cell(X.new_id, 0);
}


function insert_new_text_cell_after(id, input) {
    /*
    Insert a new text cell after the cell with given id.  This sends a
    message to the server requesting that a new cell be inserted, then
    via a callback modifies the DOM.

    INPUT:
        id -- integer or string; cell id
        input -- string (default: ''); cell input text
    */
    input = input || '';
    async_request(worksheet_command('new_text_cell_after'),
                  insert_new_text_cell_after_callback,
                  { id: id, input: input });
}


function insert_new_text_cell_after_callback(status, response) {
    /*
    Callback that is called when the server inserts a new cell after a
    given cell.

    INPUT:
        status -- string
        response -- string; 'locked' (user not allowed to insert new
        cells) or encoded JSON object with parsed keys

            id -- string or integer; preceding cell's id
            new_id -- string or integer; new cell's id
            new_html -- string; new cell's HTML

    */
    var X;
    if (status === "failure") {
        alert(translations['Problem inserting new text cell before current input cell.'] );
        return;
    }
    if (response === "locked") {
        alert(translations['Worksheet is locked. Cannot insert cells.']);
        return;
    }

    X = decode_response(response);
    do_insert_new_text_cell_after(X.id, X.new_id, X.new_html);
}


function do_insert_new_cell_after(id, new_id, new_html) {
    /*
    Insert a new cell with the given new_id and new_html after the
    cell with given id.

    INPUT:
        id -- integer or string; cell id
        new_id -- integer or string; new cell's id
        new_html -- string; new cell's HTML
    */
    // Find the cell id of the cell right after the cell with id.
    var i = id_of_cell_delta(id, 1);

    if (i === id) {
        // i is the last cell.
        append_new_cell(new_id, new_html);
    } else {
        do_insert_new_cell_before(i, new_id, new_html);
    }
}


function do_insert_new_text_cell_after(id, new_id, new_html) {
    /*
    Insert a new text cell with the given new_id and new_html after
    the cell with given id.

    INPUT:
        id -- integer or string; cell id
        new_id -- integer or string; new cell's id
        new_html -- string; new cell's HTML
    */
    // Find the cell id of the cell right after the cell with id.
    var i = id_of_cell_delta(id, 1);

    if (i === id) {
        // i is the last cell.
        append_new_text_cell(new_id, new_html);
    } else {
        do_insert_new_text_cell_before(i, new_id, new_html);
    }
}


function insert_new_cell_before(id, input) {
    /*
    Insert a new cell before the cell with given id.  This sends a
    message to the server requesting that a new cell be inserted, then
    via a callback modifies the DOM.

    INPUT:
        id -- integer or string; cell id
        input -- string (default: ''); cell input text
    */
    input = input || '';
    async_request(worksheet_command('new_cell_before'),
                  insert_new_cell_before_callback, {
                      id: id,
                      input: input
                  });
}


function insert_new_cell_before_callback(status, response) {
    /*
    See the documentation for insert_new_cell_after_callback, since
    response is encoded in exactly the same way there.
    */
    var X;
    if (status === "failure") {
        alert(translations['Problem inserting new input cell before current input cell.\\n'] + response);
        return;
    }
    if (response === "locked") {
        alert(translations['Worksheet is locked. Cannot insert cells.']);
        return;
    }

    X = decode_response(response);
    do_insert_new_cell_before(X.id, X.new_id, X.new_html);
    jump_to_cell(X.new_id, 0);

}


function insert_new_text_cell_before(id, input) {
    /*
    Insert a new text cell before the cell with given id.  This sends
    a message to the server requesting that a new cell be inserted,
    then via a callback modifies the DOM.

    INPUT:
        id -- integer or string; cell id
        input -- string (default: ''); cell input text
    */
    input = input || '';
    async_request(worksheet_command('new_text_cell_before'),
                  insert_new_text_cell_before_callback, {
                      id: id,
                      input: input
                  });
}


function insert_new_text_cell_before_callback(status, response) {
    /*
    See the documentation for insert_new_text_cell_after_callback,
    since response is encoded in exactly the same way there.
    */
    var X;
    if (status === "failure") {
        alert(translations['Problem inserting new text cell before current input cell.\\n'] + response);
        return;
    }
    if (response === "locked") {
        alert(translations['Worksheet is locked. Cannot insert cells.']);
        return;
    }

    X = decode_response(response);
    do_insert_new_text_cell_before(X.id, X.new_id, X.new_html);
}


function append_new_cell(id, html) {
    /*
    Append a new cell with given id to the end of the list of cells,
    then position the cursor in that cell.  This modifies the DOM and
    nothing else.

    INPUT:
        id -- integer or string; cell id
        html -- string; cell HTML
    */
    var new_cell, worksheet;
    new_cell = make_new_cell(id, html);

    worksheet = get_element('worksheet_cell_list');
    worksheet.appendChild(new_cell);

    cell_id_list.push(id);

    if (in_slide_mode) {
        set_class('cell_outer_' + id, 'hidden');
        update_slideshow_progress();
    } else {
        jump_to_cell(id, 0);
    }
}


function append_new_text_cell(id, html) {
    /*
    Append a new text cell with given id to the end of the list of
    cells, then position the cursor in that cell.  This modifies the
    DOM and nothing else.

    INPUT:
        id -- integer or string; cell id
        html -- string; cell HTML
    */
    var new_cell = make_new_text_cell(id, html);
    $('#worksheet_cell_list').append(new_cell);
    cell_id_list.push(id);
}


///////////////////////////////////////////////////////////////////
// CONTROL functions
///////////////////////////////////////////////////////////////////
function interrupt() {
    /*
    Send a message to the server that we would like to interrupt all
    running calculations in the worksheet.
    */
    if (!updating) {
        return;
    }
    async_request(worksheet_command('interrupt'), interrupt_callback);
}


function reset_interrupts() {
    /*
    Stops sending periodic interrupt commands, closes any running
    alerts, and resets state variables.
    */
    var is = interrupt_state;

    if (is.alert) {
        is.alert.achtung('close');
    }
    is.alert = null;
    is.count = 0;
}




function evaluate_all() {
    /*
    Iterate through every input cell in the document, in order, and
    evaluate it.  Previously, we just called evaluate on everything
    all at once.  This is undesirable, since packets often arrive
    out-of-order, so the cells get evaluated out-of-order.

    Set the global variable evaluating_all = true.  Then, we kick off
    evaluations by evaluating the first cell.  In
    cell_evaluate_callback, we check to see if evaluating_all is set,
    and proceed from there.  This way, each cell is evaluated
    immediately after the server acknowledges that it has received the
    previous request.
    */
    evaluating_all = true;
    evaluate_cell(extreme_compute_cell(1), false);
}


function hide_all() {
    /*
    Hide every output cell in the worksheet (via CSS) then send a
    message back to the server recording that we hid all cells, so if
    we refresh the browser or visit the page with another browser,
    etc., the cells are still hidden.
    */
    var i, id, len = cell_id_list.length;
    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (is_compute_cell(id)) {
            cell_output_set_type(id, 'hidden', false);
        }
    }
    async_request(worksheet_command('hide_all'));
}


function show_all() {
    /*
    Show ever output cell in the worksheet, and send a message to the
    server reporting that we showed every cell.
    */
    var i, id, len = cell_id_list.length;
    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (is_compute_cell(id)) {
            cell_output_set_type(id, 'wrap', false);
        }
    }
    async_request(worksheet_command('show_all'));
}


function delete_all_output() {
    /*
    Delete the contents of every output cell in the worksheet (in the
    DOM) then send a message back to the server recording that we
    deleted all cells, so if we refresh the browser or visit the page
    with another browser, etc., the cells are still deleted.

    Things that could go wrong:
         1. Message to server to actually do the delete is not
            received or fails.  Not so bad, since no data is lost; a
            mild inconvenience.
         2. User accidentally clicks on delete all.  There is no
            confirm dialog.  Not so bad, since we save a revision
            right before the delete all, so they can easily go back to
            the previous version.
    */
    var i, id, len = cell_id_list.length;

    // Iterate over each cell in the worksheet.
    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (!is_compute_cell(id)) {
            continue;
        }

        // First delete the actual test from the output of each cell.
        get_element('cell_output_' + id).innerHTML = "";
        get_element('cell_output_nowrap_' + id).innerHTML = "";
        get_element('cell_output_html_' + id).innerHTML = "";

        // Then record that the cell hasn't been evaluated and
        // produced that output.
        cell_set_not_evaluated(id);
    }
    // Finally tell the server to do the actual delete.  We first
    // delete from DOM then contact the server for maximum snappiness
    // of the user interface.
    async_request(worksheet_command('delete_all_output'));
    //update jmol applet info
    sage_jmol.delete_all_callback();
}


function halt_queued_cells() {
    /*
    Set all cells so they do not look like they are being evaluates or
    queued up for evaluation, and empty the list of active cells from
    the global queue_id_list variable.
    */
    var i;
    for (i = 0; i < queue_id_list.length; i += 1) {
        cell_set_not_evaluated(queue_id_list[i]);
    }

    // We use splice here, so that we can "overload" the list for
    // debugging.  See debug.js.
    queue_id_list.splice(0, queue_id_list.length);

    reset_interrupts();
}


function set_all_cells_to_be_not_evaluated() {
    /*
    Change the CSS so that all cells are displayed as having not been
    evaluated.
    */
    var i, id, len = cell_id_list.length;
    for (i = 0; i < len; i += 1) {
        id = cell_id_list[i];
        if (is_compute_cell(id)) {
            cell_set_not_evaluated(id);
        }
    }
}


function restart_sage() {
    /*
    Restart the running Sage process that supports calculations in
    this worksheet.

    This function immediately changes the DOM so it looks like no
    cells are running and none have been evaluated, then it sends a
    message back to the server requesting that the worksheet Sage
    process actually be stopped.
    */
    halt_queued_cells();
    set_all_cells_to_be_not_evaluated();
    async_request(worksheet_command('restart_sage'));
}


function quit_sage() {
    /*
    Called when the worksheet process is terminated.  All actively
    computing cells are stopped, and a request is sent to the server
    to quit the worksheet process.
    */
    halt_queued_cells();
    set_all_cells_to_be_not_evaluated();
    async_request(worksheet_command('quit_sage'));
}


function login(username, password) {
    /*
    Set the username and password for this user in a cookie.  This is
    called when the user logs in from the login screen to set the
    user's credentials.

    INPUT:
        username -- string
        password -- string
    */
    document.cookie = "username=" + username;
    document.cookie = "password=" + password;
    window.location = "/";
}

///////////////////////////////////////////////////////////////////
// Various POPUP WINDOWS
///////////////////////////////////////////////////////////////////
function history_window() {
    /*
    Popup the history window.
    */
    window.open("/history", "",
                "menubar=1,scrollbars=1,width=800,height=600, toolbar=1,resizable=1");
}


function print_worksheet() {
    /*
    Display a version of this worksheet that is suitable for printing.
    */
    window.open(worksheet_command("print"), "",
                "menubar=1,scrollbars=1,width=800,height=600,toolbar=1,  resizable=1");
}


function help() {
    /*
    Popup the help window.
    */
    window.open("/help", "",
                "menubar=1,location=1,scrollbars=1,width=800,height=650,toolbar=1,  resizable=1");
}


function bugreport() {
    /*
    Popup the bug report window.
    */
    window.open("http://sagemath.org/report-issue", "", "menubar=1,location=1,scrollbars=1,width=800,height=650,toolbar=1,  resizable=1");
}


///////////////////////////////////////////////////////////////////
// Interact
///////////////////////////////////////////////////////////////////
function interact(id, update, recompute) {
    /*
    Sends an interact request back to the server.  This is called by
    individual interact controls.

    INPUT:
        id -- integer or string; cell id
        update -- dictionary; data to update
            variable -- string; name of variable to update
            adapt_number -- integer; number of control to update
            value -- string; updated value, base-64 encoded
        recompute -- integer; whether to recompute the interact
    */
    id = toint(id);

    cell_has_changed = false;
    current_cell = id;

    update = update || {};

    async_request(worksheet_command('eval'), evaluate_cell_callback, {
        id: id,
        interact: 1,
        variable: update.variable || '',
        adapt_number: update.adapt_number || -1,
        value: update.value || '',
        recompute: recompute,
        newcell: 0
    });
}


///////////////////////////////////////////////////////////////////
// Base 64 encoding and decoding (mainly used for @interact).
///////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// The following header applies to the encode64 and decode64 functions
// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
//////////////////////////////////////////////////////////////////
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";


function encode64(input) {
    /*
    Base 64 encode the given input.

    INPUT:
        input -- string
    OUTPUT:
        string
    */
    // I had to add this, since otherwise when input is numeric there
    // are errors below.
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, output = "";

    try {
        input = input.toString();
    } catch (e) {
        return input;
    }

    while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = 64;
            enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
            keyStr.charAt(enc3) + keyStr.charAt(enc4);
    }

    return output;
}


function decode64(input) {
    /*
    Base 64 decode the given input.

    INPUT:
        input -- string
    OUTPUT:
        string
    */
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, output = "";

    // remove all characters that are not A-Z, a-z, 0-9, +, slash, or =
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);

        if (enc3 !== 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 !== 64) {
            output = output + String.fromCharCode(chr3);
        }
    }

    return output;
}

///////////////////////////////////////////////////////////////////
// Trash
///////////////////////////////////////////////////////////////////

function empty_trash() {
    /*

      This asks for confirmation from the user before submitting the
      empty trash form, which sends a POST request. GET requests are not
      allowed by the server.

    */
    if(confirm(translations["Emptying the trash will permanently delete all items in the trash. Continue?"])) {
        $('#empty-trash-form').submit();
    }
}