This file is indexed.

/usr/share/emacs/site-lisp/maxima/emaxima.el is in maxima-emacs 5.24.0-1build1.

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
;; emaxima.el  Mode for interaction with Maxima from TeX buffer
;; Written 2/12/1991 by Dan Dill dan@chem.bu.edu
;; Modified for Maxima by Jay Belanger

;; Copyright (C) 1991, 1993 Dan Dill (dan@chem.bu.edu) 
;;               1999-2007 Jay Belanger (belanger@truman.edu)

;; Authors: Dan Dill
;;          Jay Belanger
;;
;; Keywords: maxima, emaxima

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;          
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU General Public License for more details.
;;          
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;;
;; You will need, in addition to this file,
;; maxima.el, maxima-font-lock.el and emaxima.sty

;;; Commentary:

;;  See the file EMintro.ps for a quick introduction.

(require 'maxima)
(provide 'emaxima)

;;;; The variables that the user may wish to change

(defgroup emaxima nil
  "Maxima mode"
  :prefix "emaxima-"
  :tag    "EMaxima")

(defcustom emaxima-use-tex 'auctex
  "Possible modes to use within EMaxima.
Possible choices are 'auctex, 'tex or nil"
  :group 'emaxima
  :type '(choice :menu-tag "TeX style"
                 :tag      "TeX style"
                 (const auctex)
                 (const tex) 
                 (const nil)))

(defcustom emaxima-use-emaxima-indent nil
  "Use Maxima indent when in cells.
Only works with AUCTeX."
  :group 'emaxima
  :type 'boolean)

(defcustom emaxima-tex-lisp-file (locate-library "emaxima.lisp" t)
  "The file to be loaded that allows TeX output."
  :group 'emaxima
  :type '(file))

(defcustom emaxima-output-marker "---"
  "The string to separate the input from the output."
  :group 'emaxima
  :type 'string)

(defcustom emaxima-abbreviations-allowed t
  "If non-nil, then `...' abbreviations are allowed in cell labels 
and references. Note that enabling this options will slow cell and 
package assembly."
  :group 'emaxima
  :type '(boolean))

(defcustom emaxima-preview-after-update-all t
  "If non-nil and preview-latex is available, preview after update-all"
  :group 'emaxima
  :type 'boolean)

(defcustom emaxima-max-references 5
  "Number of references in a cell below which cell references are fetched
as needed, scanning the entire document for each reference.  At or above this
number, all cells in a document for the given filename are precollated in a
single scan of the document."
  :group 'emaxima
  :type '(integer))

(defcustom emaxima-temp-dir "/tmp/"
  "Directory for temporary files.
Specify \"\" to use the directory of the EMaxima document buffer."
  :group 'emaxima
  :type '(directory))

;;; Other variables and constants

(defvar emaxima-zap-file nil
  "Temporary file name used for text being sent as input to Maxima.")

(defvar emaxima-dereference-path nil
  "List of buffers referenced in cell assembly.
Used by `emaxima-dereference-buffer' to detect self-reference.")

(defvar emaxima-zap-file-prefix nil
  "Global variable used as prefix to make unique buffer names for cell 
and package assembly.")

(defvar emaxima-error-point nil
  "Buffer position where error detected.")

(defvar emaxima-buffer-alist nil
  "Alist of temporary buffers associate with cells `file:part'.
The buffers are used in package and cell assembly.")

(defvar emaxima-source-buffer nil
  "Buffer from which emaxima-collate-cells works.")

(defconst emaxima-standard-cell-begin-regexp
  "\\\\begin{maxima\\(?:\\(?:\\*\\|nu\\*?\\)?}\\)"
  "A regexp matching the beginning of a standard cell.")

(defconst emaxima-standard-cell-end-regexp
"\\\\end{maxima\\(?:\\(?:\\*\\|nu\\*?\\)?}\\)"
  "A regexp matching the end of a standard cell.")

(defconst emaxima-session-cell-begin-regexp
  "\\\\begin{maximasession\\(?:\\(?:\\*\\|nu\\*?\\)?}\\)"
  "A regexp matching the beginning of a session cell.")

(defconst emaxima-session-cell-end-regexp
"\\\\end{maximasession\\(?:\\(?:\\*\\|nu\\*?\\)?}\\)"
  "A regexp matching the end of a session cell.")

(defconst emaxima-any-cell-begin-regexp
"\\\\begin{maxima\\(?:\\(?:\\*\\|nu\\*?\\|session\\(?:\\*\\|nu\\*?\\)?\\)?}\\)"
  "A regexp matching the beginning of any cell.")

(defconst emaxima-any-non-nu-cell-begin-regexp
  "\\\\begin{maxima\\(?:\\(?:\\*\\|session\\*?\\)?}\\)"
  "A regexp matching the beginning of any cell.")

(defconst emaxima-any-cell-end-regexp
  "\\\\end{maxima\\(?:\\(?:\\*\\|nu\\*?\\|session\\(?:\\*\\|nu\\*?\\)?\\)?}\\)"
  "A regexp matching the end of any cell.")

(defconst emaxima-package-cell-regexp
  (concat emaxima-standard-cell-begin-regexp "\\[.*:.*\\]")
  "A regexp matching the beginning of a standard cell.")
  
;;; Some utility functions

(defun emaxima-insert-quote (arg)
  "Insert a quote as appropriate"
  (interactive "*P")
  (cond
   ((emaxima-cell-p)
      (self-insert-command (prefix-numeric-value arg)))
   ((eq emaxima-use-tex 'auctex)
    (TeX-insert-quote arg))
   ((eq emaxima-use-tex 'tex)
    (tex-insert-quote arg))
   (t (self-insert-command (prefix-numeric-value arg)))))

(defun emaxima-insert-dollar (arg)
  "Insert a dollar sign as appropriate"
  (interactive "*P")
  (cond
   ((emaxima-cell-p)
    (self-insert-command (prefix-numeric-value arg)))
   ((eq emaxima-use-tex 'auctex)
    (TeX-insert-dollar arg))
   ((eq emaxima-use-tex 'tex)
    (skeleton-pair-insert-maybe arg))
   (t (self-insert-command (prefix-numeric-value arg)))))

(defun emaxima-mark-file-as-emaxima ()
  "Mark the file as an EMaxima buffer.
The next time the file is loaded, it will then be in EMaxima mode"
  (interactive)
  (save-excursion
    (goto-line 1)
    (beginning-of-line)
    (if (looking-at ".*-\\*-EMaxima-\\*-")
	()
	(open-line 1)
      (insert "%-*-EMaxima-*-"))))

(defun emaxima-load-tex-library ()
  (when emaxima-tex-lisp-file
    (maxima-single-string-wait
                           (concat "block(load(\"" 
                                   emaxima-tex-lisp-file
                                   "\"), linenum:linenum-1)$"))))
(defun emaxima-tex-on ()
  (maxima-start)
  (when emaxima-tex-lisp-file
    (maxima-single-string-wait 
       "block(origdisplay:display2d, display2d:emaxima, linenum:linenum-1)$")))

(defun emaxima-tex-off ()
  (maxima-start)
  (when emaxima-tex-lisp-file
    (maxima-single-string-wait "block(display2d:origdisplay, linenum:linenum-1)$")))

(defun emaxima-indent-line ()
  (interactive)
  (if (emaxima-cell-p)
      (maxima-indent-line)
    (LaTeX-indent-line)))

;;; Which type of cell, if any, is the point in.

(defun emaxima-cell-p ()
  "Non-nil if point is in a Emaxima cell."
  (let ((found nil))
    (save-excursion
      (if (re-search-backward emaxima-any-cell-begin-regexp
                              (point-min) t) ; \beginmaxima
            (setq found (point))))
    (save-excursion
      (if (and found
               (re-search-backward emaxima-any-cell-end-regexp 
                                   found t)) ; Intervening \endmaxima
          (setq found nil)))
    (save-excursion
      (if (and found
               (re-search-forward emaxima-any-cell-end-regexp 
                                  (point-max) t)) ;\endmaxima
          (setq found (point))))
    (save-excursion
        (if (and found 
                (re-search-forward emaxima-any-cell-begin-regexp 
                                   found t)) ; Intervening \beginmaxima
            (setq found nil)))
    (if found t nil)))

(defun emaxima-standard-cell-p ()
  "Non-nil if point is in a standard cell."
  (and
   (emaxima-cell-p)
   (save-excursion
     (re-search-backward emaxima-any-cell-begin-regexp)
     (looking-at emaxima-standard-cell-begin-regexp))))

(defun emaxima-session-cell-p ()
  "Non-nil if point is in a session cell."
  (and
   (emaxima-cell-p)
   (save-excursion
     (re-search-backward emaxima-any-cell-begin-regexp)
     (looking-at emaxima-session-cell-begin-regexp))))

(defun emaxima-package-cell-p ()
  (and 
   (emaxima-standard-cell-p)
   (save-excursion
     (re-search-backward emaxima-standard-cell-begin-regexp)
     (goto-char (match-end 0))
     (and
      (looking-at "\\[")
      (not (looking-at "\\[\\]"))))))

;;; Create the cells.
(defun emaxima-new-standard-cell ()
  "Insert cell in buffer."
  (if (not (bolp))
      (progn
        (open-line 1)
        (forward-line 1)))
  (insert "\\begin{maxima}[]\n\n\\end{maxima}")
  (unless (looking-at " *$")
    (insert "\n")
    (forward-line -1))
  (beginning-of-line)
  (previous-line 1))

(defun emaxima-new-session-cell ()
  "Insert cell in buffer."
  (if (not (bolp))
      (progn
        (open-line 1)
        (forward-line 1)))
  (insert "\\begin{maximasession}\n\n\\end{maximasession}")
  (unless (looking-at " *$")
    (insert "\n")
    (forward-line -1))
  (beginning-of-line)
  (previous-line 1))

(defun emaxima-create-standard-cell ()
  "Insert standard cell in buffer."
  (interactive)
  (if (not (emaxima-cell-p))
      (emaxima-new-standard-cell)
    (if (emaxima-standard-cell-p)
        (error "Currently in cell.")
      (save-excursion
        (re-search-backward emaxima-session-cell-begin-regexp)
        (re-search-forward "maximasession")
        (replace-match "maxima")
        (search-forward "}")
        (insert "[]")
        (re-search-forward emaxima-session-cell-end-regexp)
        (search-backward "maximasession")
        (replace-match "maxima")))))

(defun emaxima-create-session-cell ()
  "Insert session cell in buffer."
  (interactive)
  (if (not (emaxima-cell-p))
      (emaxima-new-session-cell)
    (if (emaxima-package-cell-p)
        (error "Currently in package cell.")
      (if (emaxima-session-cell-p)
          (error "Currently in session cell.")
        (save-excursion
          (re-search-backward emaxima-standard-cell-begin-regexp)
          (re-search-forward "maxima")
          (replace-match "maximasession")
          (search-forward "}")
          (if (looking-at "\\[\\]")
              (delete-char 2))
          (re-search-forward emaxima-standard-cell-end-regexp)
          (search-backward "maxima")
          (replace-match "maximasession"))))))

(defun emaxima-toggle-starred-cell ()
  (interactive)
  "Toggle whether or not the current cell is starred."
  (if (not (emaxima-cell-p))
      (error "Not in cell.")
    (save-excursion
      (re-search-backward emaxima-any-cell-begin-regexp)
      (re-search-forward "maxima\\(?:session\\)?")
      (if (looking-at "\\*")
          (delete-char 1)
        (insert "*"))
      (re-search-forward emaxima-any-cell-end-regexp)
      (beginning-of-line)
      (re-search-forward "maxima\\(?:session\\)?")
      (if (looking-at "\\*")
          (delete-char 1)
        (insert "*")))))
    
(defun emaxima-starred-cell-p ()
  "Non-nil if point is in starred cell."
  (if (not (emaxima-cell-p))
      nil
    (save-excursion
      (re-search-backward emaxima-any-cell-begin-regexp)
      (re-search-forward "maxima\\(?:session\\)?")
      (looking-at "\\*"))))

(defun emaxima-package-part ()
  "Insert package marker for cell."
  (interactive)
  (cond
   ((emaxima-standard-cell-p)
    (save-excursion
      (let ((initstring)
            (package))
        (re-search-backward emaxima-standard-cell-begin-regexp)
        (goto-char (match-end 0))
        (if (looking-at "\\[\\(.*\\)\\]")
            (progn
              (setq initstring (match-string 1))
              (delete-region (match-beginning 0) (match-end 0)))
          (setq initstring ""))
        (setq package (read-string "Package: " initstring))
        (unless (string= package "")
          (insert (concat "[" package "]"))))))
   (t
    (message "Not in (standard) Maxima cell"))))
  
;;; Cell positions

(defun emaxima-cell-start ()
  "Return position of start of cell containing point."
  (save-excursion
    (if (not (looking-at emaxima-any-cell-begin-regexp))
        (re-search-backward emaxima-any-cell-begin-regexp))
    (forward-line 1)
    (point)))

(defun emaxima-cell-end ()
  "Return position of end of cell containing point."
    (save-excursion
      (re-search-forward emaxima-any-cell-end-regexp)
      (forward-line -1)
      (end-of-line)
      (point)))

(defun emaxima-previous-cell-start ()
  "Get start of preceding cell.  If none, return current position."
  (let ((cur-pos (point))
        (start nil))
    (save-excursion
      (if (not (re-search-backward emaxima-any-cell-end-regexp (point-min) t))
          cur-pos
        (if (emaxima-cell-p)
            (progn
              (re-search-backward emaxima-any-cell-begin-regexp)
              (forward-line 1)
              (point))
          cur-pos)))))
              
(defun emaxima-next-cell-start (&optional arg)
  "Get start of next cell.  If none, return current position."
  (let ((cur-pos (point))
        (start nil)
        (re (if arg
                emaxima-any-cell-begin-regexp
              emaxima-any-non-nu-cell-begin-regexp)))
    (save-excursion
      (if (re-search-forward re (point-max) t)
          (progn
            (if (not (emaxima-cell-p))
                cur-pos)
            (forward-line 1)
            (point))
        cur-pos))))

;;; Cell motion

(defun emaxima-forward-cell (&optional arg)
  "Move to next cell."
  (interactive)
    (let ((cur-pos (point))
          (cell-pos (point-max))
          new-pos)
        (setq new-pos (emaxima-next-cell-start arg))
        (if (not (equal new-pos cur-pos))
            (if (> new-pos cell-pos)
                nil
              (setq cell-pos new-pos)))
      (if (equal cell-pos (point-max))
          nil; No more cells
        (goto-char cell-pos))))

(defun emaxima-backward-cell ()
  "Move to previous cell."
  (interactive)
    (let ((cur-pos (point))
          (cell-pos (point-min))
          new-pos)
        (setq new-pos (emaxima-previous-cell-start))
        (if (not (equal new-pos cur-pos))
            (if (< new-pos cell-pos)
                nil
              (setq cell-pos new-pos)))
      (if (equal cell-pos (point-min))
          nil ; No more cells
        (goto-char cell-pos))))

;;; Output related functions
        
(defun emaxima-output-p ()
  "Return start of output text if present, else return nil.  Assumes
point in cell.  Output assumed to follow input, separated by a
\maximaoutput or \maximaoutput*."
  (save-excursion
    (goto-char (emaxima-cell-start))
    (if (re-search-forward "^\\\\maximaoutput"
         (emaxima-cell-end) t)
        (progn
          (forward-line -1)
          (end-of-line)
          (point))
      nil)))

(defun emaxima-empty-output-p ()
  "Check if there is not output or only empty lines in output."
  (save-excursion
    (goto-char (emaxima-cell-start))
    (if (re-search-forward "^\\\\maximaoutput"
                           (emaxima-cell-end) t)
        (progn
          (forward-line 1)
          (while (looking-at "[ \t]*$")
            (forward-line 1))
          (looking-at "\\\\end{maxima"))
      nil)))
     
(defun emaxima-delete-output ()
  "Delete current output (if any).  Assumes point in cell.
Output assumed to follow input, separated by a emaxima-output-marker line.
Input *may* contain blank lines."
  (interactive)
  (let ((out-start (emaxima-output-p)))
    (if out-start
        (delete-region out-start (emaxima-cell-end))
      t)))


;;; @@ EMaxima functions for package assembly

(defun emaxima-replace-assoc (alist key val)
  "Replace ALIST KEY VALUE, if KEY present, else add KEY VALUE.
Return modified alist."
  (if (assoc key alist)
      (setcdr (assoc key alist) val)
    (setcdr alist (cons (cons key val) (cdr alist))))
  alist)

(defun emaxima-assemble (arg)
 "Assemble package (see emaxima-assemble-package), or, with C-u prefix,
assemble references within a cell (see emaxima-assemble-cell)."
  (interactive "P")
  (if arg
      (emaxima-assemble-package)
    (emaxima-assemble-cell)))

(defun emaxima-assemble-cell (&optional delete)
  "Assemble references in cell to file with unique name.  The buffer used to
write the file is not deleted, unless optional DELETE is non-nil.
Return the filename."

  ;; Here is how this function works:

  ;; The text of the cell is written to a buffer with key `file:part'.  Then
  ;; the number of references in the cell is counted.  If the number of
  ;; references in the cell is less than emaxima-max-references, then the cell
  ;; references are resolved by successive calls to emaxima-dereference-buffer
  ;; which collates the text for cell references as needed, using
  ;; emaxima-collate-cells.  If the number of references is equal to or
  ;; greater than emaxima-max-references, then all cells in the document
  ;; correpsonding to the current cell type and filename are collated into
  ;; buffers, using emaxima-collate-cells, and then the all cell references
  ;; are are resolved by successive calls to emaxima-dereference-buffer.

  ;; The global `emaxima-buffer-alist' associates buffer names with keys.
  ;; Buffer names are unique.  The names of all buffers are constructed with
  ;; `maxima-make-temp-name' and are unique.  All buffers except possibly the
  ;; cell-buffer are deleted on exit.

  (interactive)
  (let ((home-buffer (current-buffer))
        files parts file part 
        ref-count
        cell-key cell-buffer tmp-alist tmp-buffer)
    (if (not (emaxima-cell-p)) (error "Not in a cell"))
    (if (not (emaxima-reference-p)) (error "Cell contains no references"))
    (save-excursion
      (goto-char (emaxima-cell-start))
      (forward-line -1)
      (if (not (looking-at emaxima-package-cell-regexp))
          (error "Cell is not a package"))
      (setq emaxima-error-point (point))
      (if emaxima-abbreviations-allowed
          (unwind-protect ; In case filename errors
              ;; This can take some seconds
              (progn
                (message "Getting filenames...")
                (setq files (emaxima-get-filenames))
                (message ""))
            (goto-char emaxima-error-point)))
      (setq file (emaxima-get-filename files))
      (if (not file) (error "Ambiguous filename"))
      (if emaxima-abbreviations-allowed
          ;; This can take several seconds for a document with many cells
          (progn
            (message "Getting partnames")
            (setq parts (emaxima-get-partnames file files))
            (message "")))
      (setq part (emaxima-get-partname parts))
      (if  (not part) (error "Ambiguous partname"))) ; save-excursion
    (setq cell-key (concat file ":"))
    (if (not (equal part "")) (setq cell-key (concat cell-key part)))
    (message "Assembling `%s' ..." cell-key) ; (sleep-for 1)
    (setq cell-buffer (maxima-make-temp-name))
    (setq emaxima-buffer-alist (list (cons cell-key cell-buffer)))
    (unwind-protect
        (save-excursion
          (emaxima-append-cell-to-buffer cell-buffer)
          (setq emaxima-source-buffer (current-buffer)) ; Collate from here
          (if (< (emaxima-reference-count cell-buffer) emaxima-max-references)
              ;; Build reference buffers as needed
                (while (emaxima-dereference-buffer cell-key files parts nil))
            ;; Prebuild all reference buffers
            (emaxima-collate-cells file part files parts nil)
            (while (emaxima-dereference-buffer cell-key files parts nil)))
          (set-buffer cell-buffer)
          (write-file (concat emaxima-temp-dir cell-buffer))
          (set-buffer home-buffer))
      ;; unwind-protect forms: deleted cell buffers
      (setq tmp-alist emaxima-buffer-alist)
      (while (setq tmp-buffer (cdr (car tmp-alist)))
        (setq tmp-alist (cdr tmp-alist))
        (condition-case nil ; In case buffer not actually created
            (if (and (not delete) (equal tmp-buffer cell-buffer))
                nil ; Don't delete the assembly buffer
              (kill-buffer tmp-buffer))
          (error nil)))) ; unwind-protect
    (message "`%s' assembled in file `%s%s'" 
	     cell-key emaxima-temp-dir cell-buffer)
    (concat emaxima-temp-dir cell-buffer)))

(defun emaxima-assemble-package (&optional file overwrite)
  "Assemble text into a package buffer and write that buffer to a file.
The buffer is *not* deleted.  Return the filename.
Optional arguments (useful for batch processing):
FILE package filename;
OVERWRITE, if not nil package filename buffer will be overwritten 
without asking."
  ;; Here is how this function works:

  ;; The entire buffer is scanned for marked cells matching TYPE and FILE and
  ;; these are collated by `file' and `part' into buffers with keys
  ;; `file:part' and, for `part' = "" (a package cell), into a buffer with key
  ;; `FILE'.

  ;; Once the cell buffers have been created, then all cell references in the
  ;; package buffer, with key `FILE', are replaced by the contents of the
  ;; corresponding buffers with keys `file:part', by successive calls to
  ;; emaxima-dereference-buffer.

  ;; The global `emaxima-buffer-alist' associates buffer names with keys.
  ;; Buffer names are unique.  The names of all buffers are constructed with
  ;; `maxima-make-temp-name' and are unique.    All buffers
  ;; except the package buffer `FILE' are deleted on exit.
  (interactive)
  (let ((home-buffer (current-buffer))
        files parts prompt
        tmp-buffer tmp-alist file-buffer)
    (if (not file)
        ;; If file has not been specifed, prompt
        (progn
              ;; Get default file from cell label, if any
              (save-excursion
                (goto-char (emaxima-cell-start))
                (forward-line -1)
                (if (looking-at emaxima-package-cell-regexp)
                    (progn
                      (setq emaxima-error-point (point))
                      (unwind-protect ; In case filename errors
                          (if emaxima-abbreviations-allowed
                              ;; This can take some seconds
                              (progn
                                (message "Getting filenames...")
                                (if (not (setq files (emaxima-get-filenames)))
                                    (error 
                                       "No complete package filenames found"))
                                (message "")))
                        (goto-char emaxima-error-point))
                      (setq file (emaxima-get-filename files)))))
              (setq file (read-from-minibuffer "Package file: " file))
              (if (or (not file) (equal file "")) 
                  (error "No file specified"))))
    (if (not overwrite)
        (if (file-exists-p file)
            (progn
              (setq prompt (concat
                            "Package file `"
                            file
                            "' exists. Overwrite it ? "))
              (if (not (y-or-n-p prompt))
                  (error "Package assembly cancelled")))))
    (if (get-buffer file) 
        (kill-buffer file))
    (if emaxima-abbreviations-allowed
        ;; This can take several seconds for a document with many cells
        (progn
          (message "Getting partnames...")
          (setq parts (emaxima-get-partnames file files))
          (message "")))
    (message "Assembling package `%s' ..." file) ;(sleep-for 1)
    ;; Set where assembly will occur
    (setq file-buffer (maxima-make-temp-name))
    (setq emaxima-buffer-alist (list (cons file file-buffer)))
    (unwind-protect ; So buffer can be deleted even if errors or abort
        (progn
          (setq emaxima-source-buffer (current-buffer)) ; Collate from here
          (emaxima-collate-cells file nil files parts nil)
          (or (get-buffer (cdr (assoc file emaxima-buffer-alist)))
              (error "No `%s' cell `%s:' found" file file))
          ;; OK, here we go:  Recursively dereference the cell buffer:
          (while (emaxima-dereference-buffer file files parts))
          (set-buffer file-buffer)
          (write-file file)
          (set-buffer home-buffer))
      ;; unwind-protect tail:  Delete part files
      (setq tmp-alist emaxima-buffer-alist)
      (while (setq tmp-buffer (cdr (car tmp-alist)))
        (setq tmp-alist (cdr tmp-alist))
        (condition-case nil ; In case buffer not actually created
            (if (equal tmp-buffer file-buffer)
                nil ; Don't delete the package buffer
              (kill-buffer tmp-buffer))
          (error nil)))) ; unwind-protect
    (message "Package `%s' assembled" file)
;    file
    (switch-to-buffer-other-window file)))

(defun emaxima-reference-count (buffer)
  "Return the number of references in BUFFER."
  (let ((count 0)
        (home-buffer (current-buffer)))
    (save-excursion
          (set-buffer buffer)
          (goto-char (point-min))
          (while (re-search-forward "^ *\t*<[^:].*:[^>].*>$" (point-max) t)
            (setq count (+ count 1)))
          (set-buffer home-buffer))
    count))

(defun emaxima-append-cell-to-buffer (buffer)
  "Append text of cell containing point to BUFFER.
Create BUFFER if it does not exist."
  (if (not (emaxima-cell-p))
      (error "Not in a cell.")
    (let ((home-buffer (current-buffer))
          (start (emaxima-cell-start))
          end)
      (save-excursion
	(goto-char start)
	(beginning-of-line)
        (while (looking-at "^ *$") (forward-line 1))
	(setq start (point))
	(if (not (setq end (emaxima-output-p)))
	    (progn
	      (goto-char (emaxima-cell-end))
	      (while (looking-at "^ *$") (forward-line -1))
	      (end-of-line)
	      (setq end (point)))
	  (progn
	    (goto-char end)
	    (while (looking-at "^ *$") (forward-line -1))
	    (end-of-line)
	    (setq end (point))))
        (set-buffer (get-buffer-create buffer))
        (goto-char (point-max))
        (insert-buffer-substring home-buffer start end)
        (insert "\n")))))

(defun emaxima-collate-cells (file part files parts &optional single)

  "Assemble cells marked with filename FILE in buffers with keys
`file:part' or, for part = null string (package cells), with key `file'.  The
names of all buffers are constructed with `maxima-make-temp-name' and are
unique.  If PART is non-nil then do not collate cells with keys `FILE:PART'
and `FILE' (package cells).  Use FILES and PARTS for name completion \(see
`emaxima-get-filename' and `emaxima-get-partname'\).  If optional SINGLE is
non-nil, then collate just cells `FILE:PART' (PART must be non-nil).

The global `emaxima-buffer-alist' associates buffer names with keys.  It must
be initialized, typically with the buffer for key `FILE' or `FILE:PART',
according to whether PART is nil or not."

  (let ((home-buffer (current-buffer))
        this-part this-file key)
    (unwind-protect ; For error location
        (setq emaxima-error-point (point)) ; Go here if no error
        (progn
          ;; Scan buffer to construct buffers for all `file:part'
          (save-excursion
            (set-buffer emaxima-source-buffer) ; Collate from here
            (goto-char (point-min))
            (while (emaxima-forward-cell)
                   ;; We have a cell of the right type
                (forward-line -1) ; Move to \begin{...
                (if (not (looking-at emaxima-package-cell-regexp))
                    (forward-line 1) ; So we go to next cell next time through
                  ;; We have a marked cell
                  (setq this-file (emaxima-get-filename files))
                  (cond
                   ((not this-file)
                    (setq emaxima-error-point (point))
                    (error "Ambiguous filename"))
                   ((not (equal file this-file))
                    (forward-line 1)) ; So we go to next cell next time through
                   (t
                    ;; We have a cell of the right package filename
                    (setq this-part (emaxima-get-partname parts))
                    (cond
                     ((not this-part)
                      (setq emaxima-error-point (point))
                      (error "Ambiguous partname"))
                     ((and single (not (equal this-part part)))
                      (forward-line 1));Do only `file:part' for SINGLE non-nil
                     ((and part (equal this-part ""))
                      (forward-line 1));Cell assembly, 
                                       ;ignore package cell `FILE:'
                     ((and (not single) (equal this-part part))
                      (forward-line 1));Cell assembly, ignore cell `FILE:PART'
                     (t
                      ;; We have a cell with a valid partname
                      (forward-line 1) ; Move into cell
                      (if (equal this-part "")
                          (setq key file)
                        (setq key (concat file ":" this-part)))
                      (or
                       (assoc key emaxima-buffer-alist) ; buffer already created
                       (emaxima-replace-assoc
                        emaxima-buffer-alist
                        key (maxima-make-temp-name)))
                      ;; Append cell contents to its buffer
                      (emaxima-append-cell-to-buffer
                       (cdr (assoc key emaxima-buffer-alist)))
                      ) ; t on valid partname
                     ) ; cond on partname
                    ) ; t on right filename (package)
                   ) ; cond on filename
                  ) ; if a marked cell
              ) ; while still cells to process
            (set-buffer home-buffer)
            ) ; save excursion
          ) ; progn of unwind-protect body
      ;; unwind-protect tail:  Delete part files
      (goto-char emaxima-error-point))))

(defun emaxima-dereference-buffer (key files parts &optional noinit)
  "Resolve all references in buffer corresponding to KEY in alist
emaxima-buffer-alist, using FILES and PARTS for name completion.  If optional
NOINIT is nil, initialize global variable `emaxima-dereference-path' with KEY.
If NOINIT is non-nil, add KEY to `emaxima-dereference-path'. 
then references are collated in buffers and added to emaxima-buffer-alist if
necessary.  Use `emaxima-dereference-path' to check for self-reference and
report error if detected,"
  (let ((ref-found nil)
        (home-buffer (current-buffer))
        path-to-here
        ref-indent ref-key ref-buffer
        (key-buffer (cdr (assoc key emaxima-buffer-alist)))
        file part
        re-found)
    (or key-buffer (error "No cell `%s'" key))
    (set-buffer key-buffer)
    (goto-char (point-min))
    (if noinit
        t
      (setq noinit t)
      (setq emaxima-dereference-path (list key)))
    (setq path-to-here emaxima-dereference-path)
    (while (re-search-forward "^ *\t*<[^:].*:[^>].*>$" (point-max) t)
      (setq re-found 1)
      (beginning-of-line)
      (setq ref-indent (emaxima-get-reference-indentation))
      (setq file (emaxima-get-filename files))
      (setq part (emaxima-get-partname parts))
      (setq ref-key (concat file ":" part))
      (if (emaxima-string-mem ref-key path-to-here)
            (emaxima-dereference-error (cons ref-key path-to-here)))
      (setq emaxima-dereference-path (cons ref-key path-to-here))
      (if (not (assoc ref-key emaxima-buffer-alist))
          ;; Construct buffer on the fly
          (progn
            (setq ref-buffer (maxima-make-temp-name))
            (emaxima-replace-assoc emaxima-buffer-alist ref-key ref-buffer)
            (emaxima-collate-cells file part files parts t))
        (setq ref-buffer (cdr (assoc ref-key emaxima-buffer-alist))))
      (while (emaxima-dereference-buffer ref-key files parts noinit))
      (kill-line 1) ; Remove reference line
      (insert-buffer ref-buffer)
      (let ((indent-start (point))
            indent-end)
        (exchange-point-and-mark)
        (setq indent-end (point))
        (exchange-point-and-mark)
        (if ref-indent (indent-rigidly indent-start indent-end ref-indent))))
    (setq emaxima-dereference-path path-to-here)
    (set-buffer home-buffer)
    ref-found))

(defun emaxima-dereference-error (path)
  "Report package self-reference error, in PATH"
  (let ((cell (car path))
        (home-buffer (current-buffer))
        to-cell from-cell)
    (setq to-cell cell)
    (with-output-to-temp-buffer "*Help*" (message ""))
    (pop-to-buffer "*Help*")
    (insert "Self-reference detected assembling EMaxima cell\n\n")
    (insert (concat "\t\t" to-cell "\n\n"))
    (insert "Here is how the self-reference happened:\n\n")
    (setq path (reverse path))
    (setq from-cell (car path))
    (insert (concat "\t" from-cell "\n"))
    (while (setq path (cdr path))
      (setq to-cell (car path))
      (if (equal cell to-cell)
          (insert (concat " !!! ->\t   -->\t" to-cell "\n"))
        (insert (concat "\t   -->\t" to-cell "\n")))
      (setq from-cell to-cell))
    (pop-to-buffer home-buffer)
    (error "Self-reference detected")))

(defun emaxima-get-reference-indentation ()
  "Return indentation of reference on current line.
Line assumed tabified."
  (let (start end)
    (save-excursion
      (beginning-of-line)
      (setq start (point))
      (search-forward "<")
      (untabify start (point))
      (setq end (point))
      (beginning-of-line)
      (tabify (point) end)
      (- end start 1))))

(defun emaxima-insert-complete-name ()
  "Insert complete name in buffer for cell.
Return t if successful, else nil."
  (interactive)
  (let ((here (point))
        start end name text files parts)
    (save-excursion
      (beginning-of-line)
      (cond
       ((and ; partname
         (or
          (re-search-forward 
           (concat emaxima-standard-cell-begin-regexp "\\[.*:[^\t]*") here t)
          (re-search-forward "^[ \t]*<.*:[^\t]*" here t))
         (equal here (point)))
        ;; This can take a second or two
        (message "Getting filenames...")
        (if (not (setq files (emaxima-get-filenames)))
            (error "No package filenames in document"))
        (message "")
        (search-backward "[") ;; Look here
        (forward-char 1)
        (setq start (point))
        (search-forward ":")
        (forward-char -1)
        (setq text (buffer-substring start (point)))
        (if (not (setq name (emaxima-complete-name text files)))
            (error "No matching package filename found"))
        ;; This can take several seconds for a document with many cells
        (message "Getting partnames")
        (setq parts (emaxima-get-partnames name files))
        (message "")
        (forward-char 1)
        (setq start (point)) ; New start, for partname deletion
        (setq text (buffer-substring (point) here))
        (if (not (setq name (emaxima-complete-name
                             (concat text "...")
                             parts)))
            (error "No matching package partname found"))
        (cond
         ((equal t name) ; Text is complete
          (setq name text))
         ((equal t (try-completion name parts)))
         (t ; Else, get completion
          (setq name
                (completing-read
                 "Partname (<space> to see partnames): "
                 parts nil t name)))) ; cond: what kind of partname completion was done
        (delete-region start here)
        (insert (concat name "]"))) ; End of partname completion
       ((and ; filename
         (or (re-search-forward 
              (concat emaxima-standard-cell-begin-regexp "\\[[^ \t]*" here t))
             (re-search-forward "^[ \t]*<[^ \t]*" here t))
         (equal here (point)))
        ;; This can take a second or two
        (message "Getting filenames...")
        (if (not (setq files (emaxima-get-filenames)))
            (error "No package filenames in document"))
        (message "")
        (re-search-backward "\\[\\|<") ;;xxx
        (forward-char 1)
        (setq start (point))
        (setq text (buffer-substring start here))
        (if (not (setq name (emaxima-complete-name
                             (concat text "...") ; completion form
                             files)))
            (error "No matching package filename found"))
        (cond
         ((equal t name) ; Text is complete
          (setq name text))
         ((equal t (try-completion name files)))
         (t ; Else, get completion
          (setq name
                (completing-read
                 "Filename (<space> to see filenames): "
                 files nil t name))
          (if (equal "" name) (error ""))))
        (delete-region start here)
        (insert (concat name ":")))
       (t
        ;;(error "Nothing to complete")
        nil))) ; save-excursion
    (if (not name)
        nil
      (goto-char (+ (point) (length name) 1))
      t)))

(defun emaxima-get-filenames ()
  "Return alist of package filenames for cells."
  (let (file files)
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell)
          (forward-line -1)
          (if (not (looking-at emaxima-package-cell-regexp))
              (forward-line 1) ; Cell not marked.  Get set for next one
            (if (setq file (emaxima-get-filename)) ; Only unabbreviated names
                (if files
                    (if (assoc file files)
                        nil ; already only
                      (setq files (cons (list file) files))) ; Add to alist
                  (setq files (list (list file))))) ; Start alist
            (forward-line 1)) ; if a marked cell
        ) ; while cell to look at
      ) ; save-excursion
    files))

(defun emaxima-complete-name (text alist &optional exact)
  "Get full name corresponding to TEXT.
If text is a string ending in `...',
then the substring preceding the `...' is used with try-completion on ALIST.
An exact match is required if optional EXACT is t.
If text is just `...' and alist is length 1, then the car of its single element
is returned.
Oherwise nil is returned."
  (let (name try-name)
    (if (not (string-match "\\(\\.\\.\\.$\\)" text))
        (setq name text) ; don't do completion on full names
      (if (and
           (eq 0 (match-beginning 1)) ; just "..."
           (eq 1 (length alist))) ; a single package filename
          (setq name (car (car alist)))
        (setq try-name (substring text 0 (match-beginning 1)))
        (setq name (try-completion try-name alist)))
      (cond
       ((equal t name)
        (setq name try-name))
       ((and
         exact
         (not (equal t (try-completion name alist))))
        (setq name nil)))) ; Not an exact match, so error
    name))

(defun emaxima-get-partnames (file files)
  "Return alist of partnames for package FILE, using FILES for
filename completion."
  (let (cell-end cell-file part parts)
    (setq emaxima-error-point (point))
    (unwind-protect
        (save-excursion
          (goto-char (point-min))
          (while (emaxima-forward-cell)
              (setq cell-end (emaxima-cell-end))
              (forward-line -1)
              (if (not (looking-at
                       (concat emaxima-standard-cell-begin-regexp "\\[[^:].*:.*\\]")))
                  (forward-line 1) ; Not a marked cell
                (setq cell-file (emaxima-get-filename files))
                (if (not (equal file cell-file))
                    (forward-line 1) ; Wrong file
                  (while (and
                          (<= (point) cell-end)
                          (or
                           (re-search-forward
                            (concat emaxima-standard-cell-begin-regexp 
                                    "\\[[^:].*:.*\\]") cell-end t)
                           (re-search-forward
                            "^ *\t*<[^:].*:.*>" cell-end t)))
                    (beginning-of-line) ; We have a filename-partname reference
                    (if (not (setq file (emaxima-get-filename files)))
                        (progn
                          (setq emaxima-error-point (point))
                          (error "Ambiguous filename")))
                    (if (not (equal cell-file file))
                        (progn
                          (setq emaxima-error-point (point))
                          (error "Reference must match cell filename: `%s'"
                                 cell-file)))
                    (setq part (emaxima-get-partname))
                    (if (not part)
                        nil ; Need full (unabbreviated) parts only, for alist
                      (if parts ; Update alist
                          (if (or
                               (equal part "")
                               (emaxima-string-mem part parts))
                              nil; already on list
                            (setq parts (append (list part) parts))) 
					; Add to alist
                        (if (not (equal part ""))
                            (setq parts (list part)))) ; Create alist
                      ) ; if an unabbreviated part                    
                    (forward-line 1)
                    ) ; while references to process in this cell
                  ) ; if a marked cell of this FILE
                ) ; if a marked cell
            ) ; while cells to process
          ); save-excursion
      (goto-char emaxima-error-point) ; unwind-protect form
      ) ; unwind-protect
    (setq parts (mapcar 'list parts)) ; Make list into an alist
    parts))

(defun emaxima-get-filename (&optional alist)
  "Get filename in package reference on current line.
If optional ALIST is supplied, use it for name completion.
Return nil if no name or error in name."
  (let ((match-re "\\(\\[\\|<\\)[^:]*\\(:\\)")
        (abbrev-re "\\.\\.\\.")
        beg text)
    (save-excursion
      (beginning-of-line)
      (setq beg (point))
      (end-of-line)
      (setq text (buffer-substring beg (point)))
      (string-match match-re text)
      (setq text 
	    (substring text (+ 1 (match-beginning 1)) (+ -1 (match-end 2)))))
    (if alist
        (emaxima-complete-name text alist t)
      (if (string-match abbrev-re text)
          (if emaxima-abbreviations-allowed
              nil
            (setq emaxima-error-point (point))
	    (error 
  "Set emaxima-abbreviations-allowed (M-x set-variable) to use abbreviations"))
        text))))

(defun emaxima-get-partname (&optional alist)
  "Get partname in package reference on current line.
If optional ALIST is supplied, use it for name completion.
Return nil if no name or error in name."
  (let ((match-re "\\(:\\)\\([^]>]*\\)")
        (abbrev-re "\\.\\.\\.")
        beg text)
    (save-excursion
      (beginning-of-line)
      (setq beg (point))
      (end-of-line)
      (setq text (buffer-substring beg (point)))
      (string-match match-re text)
      (setq text (substring text (+ 1 (match-beginning 1)) (match-end 2))))
    (if alist
        (emaxima-complete-name text alist t)
      (if (string-match abbrev-re text)
          (if emaxima-abbreviations-allowed
              nil
            (setq emaxima-error-point (point))
	    (error 
   "Set emaxima-abbreviations-allowed (M-x set-variable) to use abbreviations"))
        text))))

(defun emaxima-string-mem (element list) ; memq doesn't work for strings
  "Returns t if string ELEMENT is in LIST of strings, else returns nil."
  (let (try
        (found nil))
    (while (and (setq try (car list)) (not found))
      (setq list (cdr list))
      (if (equal element try)
          (setq found t)))
    found))

(defun emaxima-reference-p ()
  "Return t if cell contains a cell reference, else retrun nil."
  (save-excursion
    (goto-char (emaxima-cell-start))
    (if (re-search-forward "^ *\t*<[^:].*:[^>].*>$" (emaxima-cell-end) t)
        t
      nil)))

;;; Cell evaluation
;;; Get information about the cell input and output

(defun emaxima-get-cell-contents ()
  "Return the cell contents as a string."
  (if (not (emaxima-cell-p))
      (message "Not in Maxima cell"))
  (let ((home-buffer (current-buffer))
        assembled-file start end)
    (if (emaxima-reference-p)
          (save-excursion
            (widen) ; So cell references will be found
            (set-buffer (find-file-noselect
                         (emaxima-assemble-cell t)))
            (buffer-substring-no-properties (point-min) (point-max)))
      (save-excursion
	(goto-char (emaxima-cell-start))
        ;; Now I want to skip over any blank lines at the beginning of the cell
	(beginning-of-line)
	(while (looking-at "^ *$") (forward-line 1))
	(setq start (point))
        ;; as well as at the end of the cell
	(if (not (setq end (emaxima-output-p)))
	    (progn
	      (goto-char (emaxima-cell-end))
	      (while (looking-at "^ *$") (forward-line -1))
	      (end-of-line)
	      (setq end (point)))
	  (progn
	    (goto-char end)
	    (while (looking-at "^ *$") (forward-line -1))
	    (end-of-line)
	    (setq end (point)))))
      (buffer-substring-no-properties start end))))

(defun emaxima-fix-tex-output (string)
  (maxima-replace-in-string ":=" "\\\\mathbin{:=}" string))

(defun emaxima-insert-preoutput (string &optional strip)
  (if (and (> (length string) 1)
           (string= "$$" (substring string 0 2)))
      (progn
        (insert "\\ps\n")
        (insert (emaxima-fix-tex-output string) "\n" ))
    (if strip 
        (setq string (maxima-strip-string-end string)))
    (insert "\\p\n")
    (insert string)
    (insert " \\\\\n")))

(defun emaxima-insert-last-output-tex ()
  (let ((mb)
        (me)
        (ie)
        (out (maxima-strip-string (maxima-last-output))))
    (while (string-match (concat "(" maxima-linechar "[0-9]+)") out)
      (setq mb (match-beginning 0))
      (setq me (match-end 0))
      (when (> mb 0)
        (emaxima-insert-preoutput (substring out 0 mb)))
      (insert "\\t")
      (insert (substring out (+ mb 3) (- me 1)))
      (insert ". ")
      (setq out (maxima-strip-string-beginning (substring out me)))
      (string-match 
       (concat "(\\(" maxima-outchar "\\|" maxima-linechar "\\)[0-9]+)") out)
      (setq ie (match-beginning 0))
      (insert (emaxima-fix-tex-output 
               (maxima-strip-string-end (substring out 0 ie))))
      (insert " \\\\\n")
      (setq out (maxima-strip-string-beginning (substring out ie))))
    (if (string-match (concat "(" maxima-outchar "[0-9]+)") out)
        (progn
          (setq mb (match-beginning 0))
          (setq me (match-end 0))
          (when (> mb 0)
            (emaxima-insert-preoutput (substring out 0 mb) t))
          (insert "\\o")
          (insert (substring out (+ mb 3 ) (- me 1)))
;          (insert (substring out (+ mb (1+ (length maxima-outchar))) (- me 1)))
          (insert ".  ")
          (insert (emaxima-fix-tex-output 
                   (maxima-strip-string (substring out me))))
          (insert " \\\\\n"))
      (when (not (string= out ""))
        (emaxima-insert-preoutput out)))))

(defun emaxima-insert-last-output-tex-noprompt ()
  (let ((out (maxima-strip-string (maxima-last-output)))
        (me)
        (mb)
        (ie))
    (while (string-match (concat "(" maxima-linechar "[0-9]+)") out)
      (setq mb (match-beginning 0))
      (setq me (match-end 0))
      (when (> mb 0)
        (emaxima-insert-preoutput (substring out 0 mb)))
      (insert "\\t")
      (insert (substring out (+ mb 3) (- me 1)))
      (insert ". ")
      (setq out (maxima-strip-string-beginning (substring out me)))
      (string-match 
       (concat "(\\(" maxima-outchar "\\|" maxima-linechar "\\)[0-9]+)") out)
      (setq ie (match-beginning 0))
      (insert (emaxima-fix-tex-output
               (maxima-strip-string-end (substring out 0 ie))))
      (insert " \\\\\n")
      (setq out (substring out ie)))
    (if (string-match (concat "(" maxima-outchar "[0-9]+)") out)
        (progn
          (setq mb (match-beginning 0))
          (setq me (match-end 0))
          (when (> mb 0)
            (emaxima-insert-preoutput (substring out 0 mb) t))
          (setq out (maxima-strip-string-beginning (substring out me)))
          (insert "\\m")
          (insert "  ")
          (insert (emaxima-fix-tex-output out));(substring out me))
          (insert " \\\\\n"))
      (when (not (string= out ""))
        (emaxima-insert-preoutput out)))))

(defun emaxima-last-input-prompt ()
  "Copy the last input-prompt from Maxima."
  (let ((old-buffer (current-buffer))
        (maxima-buffer (get-buffer "*maxima*"))
        (prompt))
    (if (null maxima-buffer)
        (message "No Maxima output buffer")
      (set-buffer maxima-buffer)
      (save-excursion
	(goto-char (point-max))
        (if (not (inferior-maxima-running))
            (re-search-backward inferior-maxima-prompt (point-min) nil 1)
          (re-search-backward inferior-maxima-prompt (point-min) nil 2))
        (setq prompt
              (buffer-substring-no-properties 
               (match-beginning 0) (- (match-end 0) 1))))
      (set-buffer old-buffer)
      prompt)))

;;; Update the different cell types

(defun emaxima-get-lisp-end (string)
  (let* ((tmpfile (maxima-make-temp-name))
         (tmpbuf (get-buffer-create tmpfile))
         (end))
    (save-excursion
      (set-buffer tmpbuf)
      (maxima-remove-kill-buffer-hooks)
      (insert string)
      (goto-char (point-min))
      (search-forward ":lisp")
      (forward-sexp)
      (setq end  (- (point) 1)))
    (kill-buffer tmpbuf)
    end))

(defun emaxima-get-form-end (string)
  (let* ((tmpfile (maxima-make-temp-name))
         (tmpbuf (get-buffer-create tmpfile))
         (end))
    (save-excursion
      (set-buffer tmpbuf)
      (maxima-remove-kill-buffer-hooks)
      (insert string)
      (goto-char (point-min))
      (maxima-goto-end-of-form)
      (setq end (- (point) 1)))
    (kill-buffer tmpbuf)
    end))

(defun emaxima-update-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (cond 
   ((emaxima-standard-cell-p)
    (emaxima-update-standard-cell))
   ((emaxima-session-cell-p)
    (emaxima-update-session-cell)))
  (if (emaxima-empty-output-p)
      (emaxima-delete-output)))

(defun emaxima-tex-update-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (cond 
   ((emaxima-standard-cell-p)
    (emaxima-tex-update-standard-cell))
   ((emaxima-session-cell-p)
    (emaxima-tex-update-session-cell)))
  (if (emaxima-empty-output-p)
      (emaxima-delete-output)))

(defun emaxima-update-standard-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (emaxima-delete-output)
  (let ((end)
        (cell (emaxima-get-cell-contents)))
    (goto-char (emaxima-cell-end))
    (forward-line 1)
    (insert "\\maximaoutput\n")
    (while (or
            (string-match "[$;]" cell)
            (eq (string-match "[ \n]*:lisp" cell) 0))
      (if (eq (string-match "[ \n]*:lisp" cell) 0)
          (setq end (emaxima-get-lisp-end cell))
        (setq end  (emaxima-get-form-end cell)))
      (maxima-single-string-wait (substring cell 0 end))
      (setq cell (substring cell end))
      (let ((mlon (maxima-last-output-noprompt)))
        (unless (= (length (maxima-strip-string mlon)) 0)
          (insert mlon))))))

(defun emaxima-tex-update-standard-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (emaxima-delete-output)
  (let ((end)
        (cell (emaxima-get-cell-contents)))
    (goto-char (emaxima-cell-end))
    (forward-line 1)
    (insert "\\maximaoutput*\n")
    (while (or
            (string-match "[$;]" cell)
            (eq (string-match "[ \n]*:lisp" cell) 0))
      (if (eq (string-match "[ \n]*:lisp" cell) 0)
          (setq end (emaxima-get-lisp-end cell))
        (setq end  (emaxima-get-form-end cell)))
      (if (and emaxima-tex-lisp-file (not (inferior-maxima-running)))
          (emaxima-tex-on))
      (maxima-single-string-wait (substring cell 0 end))
      (setq cell (substring cell end))
      (emaxima-insert-last-output-tex-noprompt))))

(defun emaxima-update-session-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (save-excursion
    (emaxima-delete-output)
    (let ((end)
          (cell (emaxima-get-cell-contents)))
      (goto-char (emaxima-cell-end))
      (forward-line 1)
      (insert "\\maximaoutput\n")
      (while (or
              (string-match "[$;]" cell)
              (eq (string-match "[ \n]*:lisp" cell) 0))
        (if (eq (string-match "[ \n]*:lisp" cell) 0)
            (setq end (emaxima-get-lisp-end cell))
          (setq end (emaxima-get-form-end cell)))
        (maxima-single-string-wait (substring cell 0 end))
        (insert (emaxima-last-input-prompt))
        (insert " ")
        (while (or (string= "\n" (substring cell 0 1))
                   (string= " " (substring cell 0 1)))
          (setq cell (substring cell 1))
          (setq end (- end 1)))
        (insert (substring cell 0 end))
        (unless (string= "\n" (substring cell (- end 1) end))
          (insert "\n"))
        (insert (maxima-last-output))
        (setq cell (substring cell end))))))

(defun emaxima-tex-update-session-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (if (not emaxima-tex-lisp-file)
      (emaxima-update-session-cell)
    (save-excursion
      (emaxima-delete-output)
      (let ((end)
            (out)
            (cell (emaxima-get-cell-contents)))
        (goto-char (emaxima-cell-end))
        (forward-line 1)
        (insert "\\maximaoutput*\n")
        (while (or
                (string-match "[$;]" cell)
                (eq (string-match "[ \n]*:lisp" cell) 0))
          (if (eq (string-match "[ \n]*:lisp" cell) 0)
              (setq end (emaxima-get-lisp-end cell))
            (setq end (emaxima-get-form-end cell)))
          (if (and emaxima-tex-lisp-file (not (inferior-maxima-running)))
              (emaxima-tex-on))
          (maxima-single-string-wait (substring cell 0 end))
          (while (or (string= "\n" (substring cell 0 1))
                     (string= " " (substring cell 0 1)))
            (setq cell (substring cell 1))
            (setq end (- end 1)))
          (insert "\\i")
          (insert (substring (emaxima-last-input-prompt) 
                             3 -1))
;                             (1+ (length maxima-inchar)) -1))
          (insert ".  ")
;          (insert (maxima-replace-in-string "\\$" "\\\\$" (substring cell 0 end)))
          (insert (substring cell 0 end))
          (insert " \\\\")
          (unless (string= "\n" (substring cell (- end 1) end))
            (insert "\n"))
;          (insert "\n")
          (emaxima-insert-last-output-tex)
          (setq cell (substring cell end)))))))

;;; Update the different groups

(defun emaxima-update-all-cells (arg)
  "Optionally update all cells.
With C-u prefix, update without confirmation at each cell."
  (interactive "P")
  (maxima-start)
  (let ((ask))
    (if arg
        (setq ask t)
      (setq ask nil))
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell t)
        (if (or ask (y-or-n-p "Update this cell? "))
            (emaxima-update-cell))))
    (if (and emaxima-preview-after-update-all
             (fboundp 'preview-buffer))
        (preview-buffer))))


(defun emaxima-tex-update-all-cells (arg)
  "Optionally update all cells.
With C-u prefix, update without confirmation at each cell."
  (interactive "P")
  (maxima-start)
  (if (not emaxima-tex-lisp-file)
      (error "File `emaxima.lisp' not found in Emacs load path.")
    (emaxima-tex-on))
  (let ((ask))
    (if arg
        (setq ask t)
      (setq ask nil))
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell)
        (if (or ask (y-or-n-p "Update this cell? "))
            (emaxima-tex-update-cell))))
    (emaxima-tex-off)
    (if (and emaxima-preview-after-update-all
             (fboundp 'preview-buffer))
        (preview-buffer))))

(defun emaxima-update-session-cells (arg)
  "Optionally update all session cells.
With C-u prefix, update without confirmation at each cell."
  (interactive "P")
  (maxima-start)
  (let ((ask))
    (if arg
        (setq ask t)
      (setq ask nil))
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell)
        (if (and
             (emaxima-session-cell-p)
             (or ask (y-or-n-p "Update this cell? ")))
            (emaxima-update-session-cell))))
    (if (and emaxima-preview-after-update-all
             (fboundp 'preview-buffer))
        (preview-buffer))))


(defun emaxima-tex-update-session-cells (arg)
  "Optionally update all cells.
With C-u prefix, update without confirmation at each cell."
  (interactive "P")
  (maxima-start)
  (if (not emaxima-tex-lisp-file)
      (error "File `emaxima.lisp' not found in Emacs load path.")
    (emaxima-tex-on))
  (let ((ask))
    (if arg
        (setq ask t)
      (setq ask nil))
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell)
        (if (and
             (emaxima-session-cell-p)
             (or ask (y-or-n-p "Update this cell? ")))
            (emaxima-tex-update-session-cell))))
    (emaxima-tex-off)
    (if (and emaxima-preview-after-update-all
             (fboundp 'preview-buffer))
        (preview-buffer))))

(defun emaxima-update-single-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (interactive)
  (maxima-start)
  (save-excursion
    (cond 
     ((emaxima-standard-cell-p)
      (emaxima-update-standard-cell)
      (if (emaxima-empty-output-p)
          (emaxima-delete-output)))
     ((emaxima-session-cell-p)
      (emaxima-update-session-cell)
      (if (emaxima-empty-output-p)
          (emaxima-delete-output)))
     (t
      (error "Not in a cell.")))))

(defun emaxima-tex-update-single-cell ()
  "Send the current cell's contents to Maxima, and return the results."
  (interactive)
  (maxima-start)
  (if (not emaxima-tex-lisp-file)
      (error "File `emaxima.lisp' not found in Emacs load path.")
    (save-excursion
      (cond 
       ((emaxima-standard-cell-p)
        (emaxima-tex-on)
        (emaxima-tex-update-standard-cell)
        (emaxima-tex-off)
        (if (emaxima-empty-output-p)
            (emaxima-delete-output)))
       ((emaxima-session-cell-p)
        (emaxima-tex-on)
        (emaxima-tex-update-session-cell)
        (emaxima-tex-off)
        (if (emaxima-empty-output-p)
            (emaxima-delete-output)))
       (t
        (error "Not in a cell."))))))
  
(defun emaxima-replace-line-with-tex ()
  "Sends the current line to Maxima, and then replaces it with the Maxima
output in TeX form."
  (interactive)
  (if (not emaxima-tex-lisp-file)
      (error "File `emaxima.lisp' not found in Emacs load path.")
    (maxima-start)
    (emaxima-tex-on)
    (maxima-single-string-wait 
     (buffer-substring-no-properties 
      (maxima-line-beginning-position) (maxima-line-end-position)))
    (beginning-of-line)
    (insert "% ")
    (end-of-line)
    (newline)
    (emaxima-insert-last-output-tex-noprompt)
    (emaxima-tex-off)))

(defun emaxima-replace-line ()
  "Sends the current line to Maxima, and then replaces it with the Maxima
output."
  (interactive)
  (maxima-start)
  (maxima-single-string-wait 
   (buffer-substring-no-properties 
    (maxima-line-beginning-position) (maxima-line-end-position)))
  (beginning-of-line)
  (insert "% ")
  (end-of-line)
  (newline)
  (insert (maxima-last-output-noprompt)))

;;; The following section adds a command which will comment out all the 
;;; cells, and replace them by a more-or-less LaTeX equivalent.
;;; Very preliminary

(defun emaxima-tex-up-standard-cell (string)
  (let* ((tmpfile (maxima-make-temp-name))
         (tmpbuf (get-buffer-create tmpfile))
         (end))
    (save-excursion
      (set-buffer tmpbuf)
      (maxima-remove-kill-buffer-hooks)
      (insert string)
      ;; Replace beginning \maxima with \begin{verbatim}
      (goto-char (point-min))
      (kill-line)
      (insert "\\begin{verbatim}")
      ;; Take care of the output
      (if (re-search-forward "^\\\\maximaoutput" nil t)
          (progn
            (beginning-of-line)
            (if (not (looking-at "\\\\maximaoutput\\*")) 
                (progn
                  (kill-line)
                  (insert "\\end{verbatim}\n")
                  (insert emaxima-output-marker "\n")
                  (insert "\\begin{verbatim}")
                  (re-search-forward emaxima-standard-cell-end-regexp)
                  (beginning-of-line)
                  (kill-line)
                  (insert "\\end{verbatim}\n")
                  (insert "%% End of cell\n"))
              ;; Else, in a TeX cell
              (kill-line)
              (insert "\\end{verbatim}\n")
              (insert emaxima-output-marker "\\\\")
              (let ((pt (point)))
                ;; First, for the standard cells
                ;; Get rid of the \os
                (while (re-search-forward "^\\\\o" nil t)
                  (delete-region (line-beginning-position) (point))
                  (insert "\\[")
                  (search-forward "\\\\")
                  (delete-char -2)
                  (insert "\\]"))
                ;; Next, get rid of the \Es
                (goto-char pt)
                (while (re-search-forward "^\\\\t[^N]" nil t)
                  (delete-region (line-beginning-position) (point))
                  (insert "\\[ \%t")
                  (search-forward ".")
                  (delete-char -1)
                  (insert "=")
                  (search-forward "\\\\")
                  (delete-char -2)
                  (insert "\\]"))
                ;; Next, get rid of the \ms
                (goto-char pt)
                (while (re-search-forward "^\\\\m" nil t)
                  (delete-region (line-beginning-position) (point))
                  (insert "\\[")
                  (search-forward "\\\\")
                  (delete-char -2)
                  (insert "\\]"))
                ;; Finally, get rid of the \ps
                (goto-char pt)
                (while (re-search-forward "^\\\\p" nil t)
                  (delete-region (line-beginning-position) (point))
                  (insert "\\begin{verbatim}")
                  (search-forward "\\\\")
                  (delete-char -2)
                  (insert "\n\\end{verbatim}"))
                (re-search-forward emaxima-standard-cell-end-regexp)
                (beginning-of-line)
                (kill-line)
                (insert "%% End of cell\n"))))
        ;; No output
        (re-search-forward emaxima-standard-cell-end-regexp)
        (beginning-of-line)
        (kill-line)
        (insert "\\end{verbatim}\n")
        (insert "%% End of cell\n"))
      (setq string (buffer-substring-no-properties (point-min) (point-max))))
    (kill-buffer tmpbuf)
    string))

(defun emaxima-tex-up-session-cell (string)
  (let* ((tmpfile (maxima-make-temp-name))
         (tmpbuf (get-buffer-create tmpfile))
         (end))
    (save-excursion
      (set-buffer tmpbuf)
      (maxima-remove-kill-buffer-hooks)
;      (make-local-hook 'kill-buffer-hook)
;      (setq kill-buffer-hook nil)
      (insert string)
      (goto-char (point-min))
      ;; Take care of the output
      (if (re-search-forward "^\\\\maximaoutput" nil t)
          (progn
            (beginning-of-line)
            (delete-region (point-min) (point))
            (if (not (looking-at "\\\\maximaoutput\\*"))
                (progn
                  (kill-line)
                  (insert "\\begin{verbatim}")
                  (re-search-forward emaxima-session-cell-end-regexp)
                  (beginning-of-line)
                  (kill-line)
                  (insert "\\end{verbatim}\n")
                  (insert "%% End of cell"))
              ;; Else, in a TeX cell
              (kill-line)
              (insert "\\noindent\n")
              ;; First, take care of the Cs
              (while (re-search-forward "^\\\\i" nil t)
                (delete-region (line-beginning-position) (point))
                (insert "\\begin{verbatim}\n")
                (insert "(%i")
                (search-forward ".")
                (delete-char -1)
                (insert ")")
                (search-forward "\\\\")
                (delete-char -2)
                (insert "\n\\end{verbatim}"))
              ;; Next, get rid of the \os
              (goto-char (point-min))
              (while (re-search-forward "^\\\\o" nil t)
                (delete-region (line-beginning-position) (point))
                (insert "\\verb+(%o")
                (search-forward ".")
                (delete-char -1)
                (insert ")+\n\\[")
                (search-forward "\\\\")
                (delete-char -2)
                (insert "\\]"))
              ;; Next, get rid of the \ts
              (goto-char (point-min))
              (while (re-search-forward "^\\\\t[^N]" nil t)
                (delete-region (line-beginning-position) (point))
                (insert "\verb+(%t")
                (search-forward ".")
                (delete-char -1)
                (insert ")+\n\\[")
                (search-forward "\\\\")
                (delete-char -2)
                (insert "\\]"))
              ;; Finally, get rid of the \ps
              (goto-char (point-min))
              (while (re-search-forward "^\\\\p" nil t)
                (delete-region (line-beginning-position) (point))
                (insert "\\begin{verbatim}")
                (search-forward "\\\\")
                (delete-char -2)
                (insert "\n\\end{verbatim}"))
              (re-search-forward emaxima-session-cell-end-regexp)
              (beginning-of-line)
              (kill-line)
              (insert "%% End of cell\n")))
        ;; No output
        (erase-buffer)
        (insert "%% End of cell\n"))
      (setq string (buffer-substring-no-properties (point-min) (point-max))))
    (kill-buffer tmpbuf)
    string))

(defun emaxima-tex-up-starred-cell (string)
  (let* ((tmpfile (maxima-make-temp-name))
         (tmpbuf (get-buffer-create tmpfile))
         (end))
    (save-excursion
      (set-buffer tmpbuf)
      (maxima-remove-kill-buffer-hooks)
      (insert string)
      (goto-char (point-min))
      (re-search-forward emaxima-any-cell-begin-regexp)
      (replace-match "\\begin{comment}")
      (re-search-forward emaxima-any-cell-end-regexp)
      (replace-match "\\end{comment}")
      (setq string (buffer-substring-no-properties (point-min) (point-max))))
    (kill-buffer tmpbuf)
    string))

(defun emaxima-replace-cells-by-latex ()
  (interactive)
  (let ((cell)
        (beg)
        (end))
    (save-excursion
      (goto-char (point-min))
      (while (emaxima-forward-cell)
        (if (get-char-property (point) 'preview-state)
            (preview-clearout-at-point))
        (forward-line -1)
        (setq beg (point))
        (cond
         ((emaxima-starred-cell-p)
          (re-search-forward emaxima-any-cell-end-regexp)
          (setq end (point))
          (setq cell (buffer-substring-no-properties beg end))
          (comment-region beg end)
          (forward-line 1)
          (insert (emaxima-tex-up-starred-cell cell)))
         ((emaxima-session-cell-p)
          (re-search-forward emaxima-any-cell-end-regexp)
          (setq end (point))
          (setq cell (buffer-substring-no-properties beg end))
          (comment-region beg end)
          (forward-line 1)
          (insert (emaxima-tex-up-session-cell cell)))
         (t
          (re-search-forward emaxima-any-cell-end-regexp)
          (setq end (point))
          (setq cell (buffer-substring-no-properties beg end))
          (comment-region beg end)
          (forward-line 1)
          (insert (emaxima-tex-up-standard-cell cell))))))))

;;; Some preview abilities
(defun emaxima-preview-cell ()
  "Previews the current cell in the emacs buffer."
  (interactive)
  (if (not (emaxima-cell-p))
      (error "Not in an Emaxima cell")
    (if (emaxima-starred-cell-p)
        (message "Starred cell")
      (let ((beg))
        (goto-char (emaxima-cell-start))
        (forward-line -1)
        (setq beg (point))
        (search-forward emaxima-any-cell-end-regexp)
        (forward-line 1)
        (preview-region beg (point))))))

;; The following doesn't work yet...

(defun emaxima-preview-cells ()
  "Previews all cells in the emacs buffer."
  (interactive)
  (while (emaxima-forward-cell)
    (emaxima-preview-cell)))

;; First, find out what kind of TeX mode is being used.

(cond
 ((eq emaxima-use-tex 'auctex)
  (require 'tex-site)
  ;; I don't think this is the best thing to do...
  (load "latex")
  (defun texmode () (latex-mode)))
 ((eq emaxima-use-tex 'tex)
  (require 'tex-mode)
  (defun texmode () (tex-mode)))
 (t
  (autoload 'text-mode "text-mode")
  (defun texmode () (text-mode))))

;; ;;; A function for font-locking
;; (defun emaxima-match-cells (limit)
;;   "Used to fontify whatever's between \\beginmaxima and \\endmaxima."
;;   (when (re-search-forward "\\\\beginmaxima" 
;;                            limit t)
;;     (let ((beg (match-end 0)) end)
;;       (if (search-forward "\\endmaxima"
;;                           limit 'move)
;;           (setq end (match-beginning 0))
;;         (setq end (point)))
;;       (store-match-data (list beg end))
;;       t)))

(define-derived-mode emaxima-mode texmode  "EMaxima"
  "This is a mode intended to allow the user to write documents that
include Maxima code.  The file can be LaTeXed to produce nice 
looking output (although that isn't necessary, of course), and so the
mode is an extension of texmode (AucTeX).
The units of Maxima code that are worked with are \"cells\", which are 
delimited by \"\\beginmaxima\" and \"\\endmaxima\". The cells can be evaluated 
individually, as a group, and the output can be returned as Maxima output 
or in TeX form.  Evaluating a cell and returning the output is called 
\"updating\" the cell.  This mode also supports some literate programming 
constructs.  (See the file \"EMaximaIntro.tex\" for more 
information.)
The commands for working with cells are:
 \\[emaxima-create-standard-cell]  create a cell         
 \\[emaxima-update-all-cells] update all the cells 
 \\[emaxima-tex-update-all-cells] update all the cells in TeX form 
 \\[emaxima-forward-cell] go to the next cell 
 \\[emaxima-backward-cell] go to the previous cell

\(With a prefix, C-u \\[emaxima-update-all-cells] and C-u \\[emaxima-tex-update-all-cells] will update the cells 
without prompting)
Since the Maxima output can be returned to the EMaxima buffer,
the buffer which runs the Maxima process is not shown.

Single lines can be evaluated:
 \\[emaxima-replace-line] replace the current line with Maxima output
 \\[emaxima-replace-line-with-tex] replace the current line with Maxima output in TeX form.

Within a cell, the following commands are available:\\<emaxima-maxima-map>
 \\[emaxima-delete-output]  delete the cell's output
 \\[emaxima-update-cell]  update a cell 
 \\[emaxima-tex-update-cell] update a cell in TeX form
 \\[emaxima-assemble]  assemble a cell which defines a package
 C-u \\[emaxima-assemble]  assemble a cell with references

Finally, the command \\[emaxima-mark-file-as-emaxima] will insert a 
%-*-EMaxima-*- at the beginning of the file (if there isn't one there 
already) so the file will begin in emaxima-mode next time it's opened.

\\{emaxima-mode-map}
"
  (when (or (eq emaxima-use-tex 'auctex) (eq emaxima-use-tex 'tex))
    (make-local-variable 'ispell-parser)
    (setq ispell-parser 'tex)
    (make-local-variable 'ispell-tex-p)
    (setq ispell-tex-p t))
  (when (eq emaxima-use-tex 'auctex)
    (make-local-variable 'TeX-auto-untabify)
    (setq TeX-auto-untabify t))
  (make-local-variable 'texmathp-tex-commands)
  (setq texmathp-tex-commands 
     '(("\\endmaxima" sw-off)))
  (when (eq emaxima-use-tex 'auctex)
    (require 'font-latex)
    (when emaxima-use-emaxima-indent
      (make-local-variable 'indent-line-function)
      (setq indent-line-function 'emaxima-indent-line)))
        
;;     (defvar emaxima-keywords
;;       (append font-latex-keywords-2
;;               '((emaxima-match-cells (0 font-lock-function-name-face t t))
;;                 ("\\(\\\\\\(beginmaxima\\(?:noshow\\|session\\)?\\|endmaxima\\(?:noshow\\|session\\)?\\|maxima\\(?:output\\|session\\|tex\\(?:output\\|session\\)\\)\\)\\)"
;;                  (0 font-lock-keyword-face t t))))
;;       "Keywords for EMaxima font-locking.")
;;     (make-local-variable 'font-lock-defaults)
;;     (setq font-lock-defaults 
;;           '(emaxima-keywords
;;             nil nil ((?\( . ".") (?\) . ".") (?$ . "\"")) nil
;;             (font-lock-comment-start-regexp . "%")
;;             (font-lock-mark-block-function . mark-paragraph)))
;;)
  (if (inferior-maxima-running)
     (emaxima-load-tex-library))
  (add-hook 'inferior-maxima-mode-hook 'emaxima-load-tex-library)
;  (if running-xemacs 
;      (add-hook 'inferior-maxima-mode-hook (lambda () (sit-for 1))))
  (run-hooks 'emaxima-mode-hook))


;;; Now, add to  the keymap
(define-key emaxima-mode-map "\"" 'emaxima-insert-quote)
(define-key emaxima-mode-map "$" 'emaxima-insert-dollar)
(define-key emaxima-mode-map "\C-c\C-u" nil)
(define-key emaxima-mode-map "\C-c+" 'emaxima-forward-cell)
(define-key emaxima-mode-map "\C-c-" 'emaxima-backward-cell)
(define-key emaxima-mode-map "\C-c\C-ua" 'emaxima-update-all-cells)
(define-key emaxima-mode-map "\C-c\C-uA" 'emaxima-tex-update-all-cells)
(define-key emaxima-mode-map "\C-c\C-us" 'emaxima-update-session)
(define-key emaxima-mode-map "\C-c\C-uS" 'emaxima-tex-update-session)
(define-key emaxima-mode-map "\C-c\C-o" 'emaxima-create-standard-cell)
(define-key emaxima-mode-map "\C-c\C-a" 'emaxima-create-session-cell)
(define-key emaxima-mode-map "\C-c\C-n" 'emaxima-toggle-starred-cell)
(define-key emaxima-mode-map "\C-c\C-ul" 'emaxima-replace-line)
(define-key emaxima-mode-map "\C-c\C-uL" 'emaxima-replace-line-with-tex)
(define-key emaxima-mode-map "\C-c\C-k"  'maxima-stop)
;; And some emaxima keys that make sense in cells
(define-key emaxima-mode-map "\C-c\C-v" 'emaxima-send-cell)
(define-key emaxima-mode-map "\C-c\C-uc" 'emaxima-update-single-cell)
(define-key emaxima-mode-map "\C-c\C-uC" 'emaxima-tex-update-single-cell)
(define-key emaxima-mode-map "\C-c\C-d" 'emaxima-delete-output)
(define-key emaxima-mode-map "\C-c\C-x" 'emaxima-package-part)
(define-key emaxima-mode-map "\C-c@" 'emaxima-assemble)
(define-key emaxima-mode-map "\C-c\C-h" 'maxima-help)
(define-key emaxima-mode-map "\C-c\C-i" 'maxima-info)
(define-key emaxima-mode-map [(control c) (control tab)] 'emaxima-insert-complete-name)

;;; Now, the menu.
(easy-menu-define emaxima-menu emaxima-mode-map
  "EMaxima mode menu"
  '("EMaxima"
    ("Cells"
     ["Create cell"  emaxima-create-standard-cell 
                (not (emaxima-standard-cell-p))]
     ["Create session cell" emaxima-create-session-cell 
                (not (emaxima-session-cell-p))]
     ["Toggle starred cell" emaxima-toggle-starred-cell
      (emaxima-cell-p)]
     ["Send cell"  emaxima-send-cell (emaxima-cell-p)]
     ["Update cell"   emaxima-update-single-cell (emaxima-cell-p)]
     ["TeX update cell"  emaxima-tex-update-single-cell (emaxima-cell-p)]
     ["Delete output"   emaxima-delete-output (emaxima-cell-p)]
     ["Mark as package part" emaxima-package-part (emaxima-standard-cell-p)]
     ["Insert complete name" emaxima-insert-complete-name 
                                                  (emaxima-standard-cell-p)]
     ["Forward cell"  emaxima-forward-cell]
     ["Backwards cell"  emaxima-backward-cell])
    ("Update"
     ["Update line"   emaxima-replace-line (not (emaxima-cell-p))]
     ["Update all cells"  emaxima-update-all-cells]
     ["Update session cells" emaxima-update-session]
     "---"
     ["TeX update line"   emaxima-replace-line-with-tex (not (emaxima-cell-p))]
     ["TeX update all cells"  emaxima-tex-update-all-cells])
    ("Process"
     ["Start a Maxima process"   maxima-start
      (not (processp inferior-maxima-process))]
     ["Run Maxima on region"  maxima-region]
     ["Kill Maxima process"  maxima-stop (processp inferior-maxima-process)])
    ("Misc"
     ["Indent region"   maxima-indent-region (emaxima-cell-p)]
     ["Short comment"  maxima-short-comment (emaxima-cell-p)]
     ["Long comment" maxima-long-comment (emaxima-cell-p)]
     ["Mark file as EMaxima"  emaxima-mark-file-as-emaxima])
    ("Web"
     ["Assemble cell"  emaxima-assemble-cell 
      (and (emaxima-cell-p) (emaxima-reference-p))]
     ["Assemble package"  emaxima-assemble-package 
      (and (emaxima-cell-p) (emaxima-reference-p))])
    ("Help"
     ["Manual"   maxima-info]
     ["Describe"  maxima-help])))

;;; The next line is necessary for XEmacs
(if (featurep 'xemacs)
    (easy-menu-add emaxima-menu emaxima-mode-map))
  
;;; emaxima.el ends here