This file is indexed.

/usr/share/doc/pymacs/html/pymacs.html is in pymacs 0.23-1.1.

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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
<title>Pymacs version 0.23</title>
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
:Revision: $Revision: 4224 $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  margin-bottom: 0.5em }

/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
  font-weight: bold }
*/

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left {
  clear: left }

img.align-right {
  clear: right }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

tt.docutils {
  background-color: #eeeeee }

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="pymacs-version-0-23">
<h1 class="title">Pymacs version 0.23</h1>
<h2 class="subtitle" id="extending-emacs-with-python">Extending Emacs with Python</h2>
<blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">François Pinard</td>
</tr>
<tr class="field"><th class="field-name">Email:</th><td class="field-body"><a class="reference" href="mailto:pinard&#64;iro.umontreal.ca">pinard&#64;iro.umontreal.ca</a></td>
</tr>
<tr class="field"><th class="field-name">Copyright:</th><td class="field-body">© Progiciels Bourbeau-Pinard inc., Montréal 2003, 2008</td>
</tr>
</tbody>
</table>
</blockquote>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#introduction" id="id3" name="id3">1&nbsp;&nbsp;&nbsp;Introduction</a><ul class="auto-toc">
<li><a class="reference" href="#what-is-pymacs" id="id4" name="id4">1.1&nbsp;&nbsp;&nbsp;What is Pymacs?</a></li>
<li><a class="reference" href="#documentation-and-examples" id="id5" name="id5">1.2&nbsp;&nbsp;&nbsp;Documentation and examples</a></li>
<li><a class="reference" href="#other-resources" id="id6" name="id6">1.3&nbsp;&nbsp;&nbsp;Other resources</a></li>
</ul>
</li>
<li><a class="reference" href="#installation" id="id7" name="id7">2&nbsp;&nbsp;&nbsp;Installation</a><ul class="auto-toc">
<li><a class="reference" href="#select-emacs-and-python" id="id8" name="id8">2.1&nbsp;&nbsp;&nbsp;Select Emacs and Python</a></li>
<li><a class="reference" href="#check-if-pymacs-would-work" id="id9" name="id9">2.2&nbsp;&nbsp;&nbsp;Check if Pymacs would work</a></li>
<li><a class="reference" href="#install-the-pymacs-proper" id="id10" name="id10">2.3&nbsp;&nbsp;&nbsp;Install the Pymacs proper</a></li>
<li><a class="reference" href="#prepare-your-emacs-file" id="id11" name="id11">2.4&nbsp;&nbsp;&nbsp;Prepare your <tt class="file docutils literal"><span class="pre">.emacs</span></tt> file</a></li>
<li><a class="reference" href="#porting-and-caveats" id="id12" name="id12">2.5&nbsp;&nbsp;&nbsp;Porting and caveats</a></li>
</ul>
</li>
<li><a class="reference" href="#emacs-lisp-structures-and-python-objects" id="id13" name="id13">3&nbsp;&nbsp;&nbsp;Emacs Lisp structures and Python objects</a><ul class="auto-toc">
<li><a class="reference" href="#conversions" id="id14" name="id14">3.1&nbsp;&nbsp;&nbsp;Conversions</a></li>
<li><a class="reference" href="#simple-objects" id="id15" name="id15">3.2&nbsp;&nbsp;&nbsp;Simple objects</a></li>
<li><a class="reference" href="#sequences" id="id16" name="id16">3.3&nbsp;&nbsp;&nbsp;Sequences</a></li>
<li><a class="reference" href="#opaque-objects" id="id17" name="id17">3.4&nbsp;&nbsp;&nbsp;Opaque objects</a><ul class="auto-toc">
<li><a class="reference" href="#emacs-lisp-handles" id="id18" name="id18">3.4.1&nbsp;&nbsp;&nbsp;Emacs Lisp handles</a></li>
<li><a class="reference" href="#python-handles" id="id19" name="id19">3.4.2&nbsp;&nbsp;&nbsp;Python handles</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference" href="#usage-on-the-emacs-lisp-side" id="id20" name="id20">4&nbsp;&nbsp;&nbsp;Usage on the Emacs Lisp side</a><ul class="auto-toc">
<li><a class="reference" href="#pymacs-exec" id="id21" name="id21">4.1&nbsp;&nbsp;&nbsp;<strong>pymacs-exec</strong></a></li>
<li><a class="reference" href="#pymacs-eval" id="id22" name="id22">4.2&nbsp;&nbsp;&nbsp;<strong>pymacs-eval</strong></a></li>
<li><a class="reference" href="#pymacs-call" id="id23" name="id23">4.3&nbsp;&nbsp;&nbsp;<strong>pymacs-call</strong></a></li>
<li><a class="reference" href="#pymacs-apply" id="id24" name="id24">4.4&nbsp;&nbsp;&nbsp;<strong>pymacs-apply</strong></a></li>
<li><a class="reference" href="#pymacs-load" id="id25" name="id25">4.5&nbsp;&nbsp;&nbsp;<strong>pymacs-load</strong></a></li>
<li><a class="reference" href="#expected-usage" id="id26" name="id26">4.6&nbsp;&nbsp;&nbsp;Expected usage</a></li>
<li><a class="reference" href="#special-emacs-lisp-variables" id="id27" name="id27">4.7&nbsp;&nbsp;&nbsp;Special Emacs Lisp variables</a><ul class="auto-toc">
<li><a class="reference" href="#pymacs-load-path" id="id28" name="id28">4.7.1&nbsp;&nbsp;&nbsp;pymacs-load-path</a></li>
<li><a class="reference" href="#pymacs-trace-transit" id="id29" name="id29">4.7.2&nbsp;&nbsp;&nbsp;pymacs-trace-transit</a></li>
<li><a class="reference" href="#pymacs-forget-mutability" id="id30" name="id30">4.7.3&nbsp;&nbsp;&nbsp;pymacs-forget-mutability</a></li>
<li><a class="reference" href="#pymacs-mutable-strings" id="id31" name="id31">4.7.4&nbsp;&nbsp;&nbsp;pymacs-mutable-strings</a></li>
<li><a class="reference" href="#timeout-variables" id="id32" name="id32">4.7.5&nbsp;&nbsp;&nbsp;Timeout variables</a></li>
<li><a class="reference" href="#pymacs-dreadful-zombies" id="id33" name="id33">4.7.6&nbsp;&nbsp;&nbsp;pymacs-dreadful-zombies</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference" href="#usage-on-the-python-side" id="id34" name="id34">5&nbsp;&nbsp;&nbsp;Usage on the Python side</a><ul class="auto-toc">
<li><a class="reference" href="#python-setup" id="id35" name="id35">5.1&nbsp;&nbsp;&nbsp;Python setup</a></li>
<li><a class="reference" href="#emacs-lisp-symbols" id="id36" name="id36">5.2&nbsp;&nbsp;&nbsp;Emacs Lisp symbols</a></li>
<li><a class="reference" href="#dynamic-bindings" id="id37" name="id37">5.3&nbsp;&nbsp;&nbsp;Dynamic bindings</a></li>
<li><a class="reference" href="#raw-emacs-lisp-expressions" id="id38" name="id38">5.4&nbsp;&nbsp;&nbsp;Raw Emacs Lisp expressions</a></li>
<li><a class="reference" href="#user-interaction" id="id39" name="id39">5.5&nbsp;&nbsp;&nbsp;User interaction</a></li>
<li><a class="reference" href="#key-bindings" id="id40" name="id40">5.6&nbsp;&nbsp;&nbsp;Key bindings</a></li>
</ul>
</li>
<li><a class="reference" href="#debugging" id="id41" name="id41">6&nbsp;&nbsp;&nbsp;Debugging</a><ul class="auto-toc">
<li><a class="reference" href="#on-the-communication-protocol" id="id42" name="id42">6.1&nbsp;&nbsp;&nbsp;On the communication protocol</a></li>
<li><a class="reference" href="#the-pymacs-buffer" id="id43" name="id43">6.2&nbsp;&nbsp;&nbsp;The <strong>*Pymacs*</strong> buffer</a></li>
<li><a class="reference" href="#emacs-usual-debugging" id="id44" name="id44">6.3&nbsp;&nbsp;&nbsp;Emacs usual debugging</a></li>
<li><a class="reference" href="#auto-reloading-on-save" id="id45" name="id45">6.4&nbsp;&nbsp;&nbsp;Auto-reloading on save</a></li>
<li><a class="reference" href="#debugging-the-pymacs-helper" id="id46" name="id46">6.5&nbsp;&nbsp;&nbsp;Debugging the Pymacs helper</a></li>
</ul>
</li>
<li><a class="reference" href="#about-and-around-pymacs" id="id47" name="id47">7&nbsp;&nbsp;&nbsp;About and around Pymacs</a><ul class="auto-toc">
<li><a class="reference" href="#known-limitations" id="id48" name="id48">7.1&nbsp;&nbsp;&nbsp;Known limitations</a></li>
<li><a class="reference" href="#history" id="id49" name="id49">7.2&nbsp;&nbsp;&nbsp;History</a></li>
<li><a class="reference" href="#pymacs-and-me" id="id50" name="id50">7.3&nbsp;&nbsp;&nbsp;Pymacs and me!</a></li>
<li><a class="reference" href="#vim-considerations" id="id51" name="id51">7.4&nbsp;&nbsp;&nbsp;Vim considerations</a></li>
<li><a class="reference" href="#inclusion-within-emacs" id="id52" name="id52">7.5&nbsp;&nbsp;&nbsp;Inclusion within Emacs</a></li>
<li><a class="reference" href="#speed-issues" id="id53" name="id53">7.6&nbsp;&nbsp;&nbsp;Speed issues</a></li>
<li><a class="reference" href="#the-future-of-pymacs" id="id54" name="id54">7.7&nbsp;&nbsp;&nbsp;The future of Pymacs</a></li>
</ul>
</li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id3" id="introduction" name="introduction">1&nbsp;&nbsp;&nbsp;Introduction</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id4" id="what-is-pymacs" name="what-is-pymacs">1.1&nbsp;&nbsp;&nbsp;What is Pymacs?</a></h2>
<p>Pymacs is a powerful tool which, once started from Emacs, allows two-way
communication between Emacs Lisp and Python.  Pymacs aims to employ
Python as an extension language for Emacs rather than the other way
around, and this asymmetry is reflected in some design choices.  Within
Emacs Lisp code, one may load and use Python modules.  Python functions
may themselves use Emacs services, and handle Emacs Lisp objects kept in
Emacs Lisp space.</p>
<p>The goals are to write <em>naturally</em> in both languages, debug with ease,
fall back gracefully on errors, and allow full cross-recursion.</p>
<p>It is very easy to install Pymacs, as neither Emacs nor Python need to
be compiled nor relinked.  Emacs merely starts Python as a subprocess,
and Pymacs implements a communication protocol between both processes.</p>
<p>Report problems, documentation flaws, or suggestions to François Pinard:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="mailto:pinard&#64;iro.umontreal.ca">mailto:pinard&#64;iro.umontreal.ca</a></li>
</ul>
</blockquote>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id5" id="documentation-and-examples" name="documentation-and-examples">1.2&nbsp;&nbsp;&nbsp;Documentation and examples</a></h2>
<p>The main Pymacs site conveys the Pymacs documentation (you are reading
its Pymacs manual right now) and distributions:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca">http://pymacs.progiciels-bpi.ca</a></li>
</ul>
</blockquote>
<p>I expect average Pymacs users to have a deeper knowledge of Python
than Emacs Lisp.  People have widely varying approaches in writing
<tt class="file docutils literal"><span class="pre">.emacs</span></tt> files, as far as Pymacs is concerned:</p>
<blockquote>
<ul class="simple">
<li>Some can go and write almost no Emacs Lisp, yet a bit is still
necessary for establishing a few loading hooks.  For many simple
needs, one can do a lot without having to learn much.</li>
<li>On the other hand, for more sophisticated usages, people cannot
really escape knowing the Emacs Lisp API to some extent, because they
should be programming-wise familiarity with what is a buffer, a point,
a mark, etc. and what are the allowed operations on those.</li>
</ul>
</blockquote>
<p>While Pymacs examples are no substitute for a careful reading of the
Pymacs manual, the contemplation and study of others' nice works may
well enligthen and deepen your understanding.  A few examples are
included within the Pymacs distribution, each as a subdirectory of the
<tt class="file docutils literal"><span class="pre">contrib/</span></tt> directory, and each haviing its own <tt class="file docutils literal"><span class="pre">README</span></tt>
file.  These are listed below, easiest examples first:</p>
<blockquote>
<ul class="simple">
<li>Paul Winkler's example<ul>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/Winkler.html">http://pymacs.progiciels-bpi.ca/Winkler.html</a></li>
</ul>
</li>
<li>Fernando Pérez' examples<ul>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/Perez.html">http://pymacs.progiciels-bpi.ca/Perez.html</a></li>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/contrib/Perez/">http://pymacs.progiciels-bpi.ca/contrib/Perez/</a></li>
</ul>
</li>
<li>Giovanni Giorgi's files<ul>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/Giorgi.html">http://pymacs.progiciels-bpi.ca/Giorgi.html</a></li>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/contrib/Giorgi/">http://pymacs.progiciels-bpi.ca/contrib/Giorgi/</a></li>
</ul>
</li>
<li>A reformatter for boxed comments<ul>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/rebox.html">http://pymacs.progiciels-bpi.ca/rebox.html</a></li>
<li><a class="reference" href="http://pymacs.progiciels-bpi.ca/contrib/rebox/">http://pymacs.progiciels-bpi.ca/contrib/rebox/</a></li>
</ul>
</li>
</ul>
</blockquote>
<p>A few more substantial examples of Pymacs usage have been brought to my
attention, and are available externally (listed here in no particular
order):</p>
<blockquote>
<ul class="simple">
<li>pymdev -- A Python Emacs Development Module:<ul>
<li><a class="reference" href="http://www.toolness.com/pymdev/">http://www.toolness.com/pymdev/</a></li>
</ul>
</li>
<li>Ropemacs -- Features like refactoring and code-assists:<ul>
<li><a class="reference" href="http://rope.sf.net/">http://rope.sf.net/</a></li>
<li><a class="reference" href="http://rope.sf.net/hg/rpymacs">http://rope.sf.net/hg/rpymacs</a></li>
</ul>
</li>
<li>Bicycle Repair Man -- A Refactoring Tool for Python:<ul>
<li><a class="reference" href="http://bicyclerepair.sourceforge.net/">http://bicyclerepair.sourceforge.net/</a></li>
</ul>
</li>
<li>Emacs Freex -- A personal wiki on steroids:<ul>
<li><a class="reference" href="http://www.princeton.edu/%7Egdetre/software/freex/docs/index.html">http://www.princeton.edu/%7Egdetre/software/freex/docs/index.html</a></li>
</ul>
</li>
</ul>
</blockquote>
<p>The QaTeX project was influenced by Pymacs, according to its author:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="http://qatex.sourceforge.net/">http://qatex.sourceforge.net/</a></li>
<li><a class="reference" href="http://www.pytex.org/doc/index.html#eurotex2005">http://www.pytex.org/doc/index.html#eurotex2005</a></li>
</ul>
</blockquote>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id6" id="other-resources" name="other-resources">1.3&nbsp;&nbsp;&nbsp;Other resources</a></h2>
<p>You are welcome writing to or joining the following mailing list, where
there are a few people around likely to give you feedback:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="mailto:pymacs-devel&#64;googlegroups.com">mailto:pymacs-devel&#64;googlegroups.com</a></li>
</ul>
</blockquote>
<p>If you have no fear of wider crowds :-), there still is:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="mailto:python-list&#64;python.org">mailto:python-list&#64;python.org</a></li>
</ul>
</blockquote>
<p>There are other Web sites specifically about Pymacs. <a class="reference" href="http://blog.objectsroot.com/">Giovanni Giorgi</a>
has one of them:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="http://blog.objectsroot.com/projects/pymacs/">http://blog.objectsroot.com/projects/pymacs/</a></li>
</ul>
</blockquote>
<p>There is also <cite>revised pymacs</cite>, part of the Ropemacs project:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="http://rope.sourceforge.net/ropemacs.html">http://rope.sourceforge.net/ropemacs.html</a></li>
</ul>
</blockquote>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id7" id="installation" name="installation">2&nbsp;&nbsp;&nbsp;Installation</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id8" id="select-emacs-and-python" name="select-emacs-and-python">2.1&nbsp;&nbsp;&nbsp;Select Emacs and Python</a></h2>
<p>The environment variable <tt class="docutils literal"><span class="pre">PYMACS_PYTHON</span></tt> is usually left unset or
empty, in which case <tt class="docutils literal"><span class="pre">python</span></tt> is implied.  It has the purpose of
naming the Python interpreter program to be called for starting the
Pymacs helper.  It may be set to give the full path of the executable if
the Python program exists at some location outside the program search
path.  It may also be given when the interpreter name is different,
for exemple when the Python version is part of the program name.</p>
<p>The similar environment variable <tt class="docutils literal"><span class="pre">PYMACS_EMACS</span></tt> is usually left unset
or empty, in which case <tt class="docutils literal"><span class="pre">emacs</span></tt> is implied.  It has the purpose of
naming the Emacs editor, yet this is only meaningful for the validation
(see next section).  For normal Pymacs usage, Emacs is launched by the
user long before Pymacs is itself started, and consequently, there
is absolutely no need to tell Pymacs which Emacs is needed.  For the
validation suite however, it may be set to give the full path of the
executable if the Emacs program exists at some location outside the
program search path.  It may also be given when the editor name is
different, for example when the Emacs version is part of the program
name, or when this is a different editor (like the value <tt class="docutils literal"><span class="pre">xemacs</span></tt> to
call XEmacs).</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id9" id="check-if-pymacs-would-work" name="check-if-pymacs-would-work">2.2&nbsp;&nbsp;&nbsp;Check if Pymacs would work</a></h2>
<p>To know, before installing Pymacs, if it would work on your system,
try the validation suite by running <tt class="docutils literal"><span class="pre">make</span> <span class="pre">check</span></tt>.  The suite is
fairly elementary, but nevertheless, it is able to detect some common
show stoppers.  As a convenience for those who want to quickly try
various Emacs and Python combinations, <tt class="docutils literal"><span class="pre">make</span> <span class="pre">check</span> <span class="pre">emacs=SOME_EMACS</span>
<span class="pre">python=SOME_PYTHON</span></tt> temporarily overrides the environment variables
<tt class="docutils literal"><span class="pre">PYMACS_EMACS</span></tt> and <tt class="docutils literal"><span class="pre">PYMACS_PYTHON</span></tt>.  For example, <tt class="docutils literal"><span class="pre">make</span> <span class="pre">check</span>
<span class="pre">emacs=xemacs</span></tt> runs the validation suite using <tt class="docutils literal"><span class="pre">xemacs</span></tt> for an editor.</p>
<p>The remaining of this section may be safely be skipped, for mere Pymacs
installation.</p>
<p>I did not base the validation suite on Junit (the Python unit testing
framework is a re-implementation of it), but on Codespeak's pylib
<tt class="file docutils literal"><span class="pre">py.test</span></tt>, which is much simpler, and still very powerful.  The
<strong>pylib</strong> project is driven by Holge Kregel, but attracted some
Python brains, like Armin Rigo (known for Psyco, among other things --
I think his <strong>lsprof</strong> has also been added to Python 2.5 under the
name <strong>cProfile</strong>).  This gang addresses overdone/heavy methods in
Python, and do them better.  Even <tt class="file docutils literal"><span class="pre">py.test</span></tt> is a bit more complex
that I would want, and has (or at least had) flaws on the Unicode side,
so I rewrote my own, as a simple single file.  I merely translated it
from French to English, to make it more distributable within Pymacs.</p>
<p>It has not been fruitful, trying to use Emacs stdin and stdout for
communicating expressions to evaluate and getting back results from
within the validation suite.  After some fight, I reluctantly put this
avenue aside.  Currently, the suite writes problems in files, for Emacs
to read, and Emacs writes replies in files, for the suite to check.
Busy waiting (with small sleep added in the loops) is used on both
sides.  This is all too heavy, and it slows down the suite.  Hopefully,
the suite is not run often, this is not a real problem.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id10" id="install-the-pymacs-proper" name="install-the-pymacs-proper">2.3&nbsp;&nbsp;&nbsp;Install the Pymacs proper</a></h2>
<p>Pymacs is a small package.  Putting the documentation and administrative
files aside, there is one Python file and one Emacs Lisp file to it, to
be installed in turn.  Always start with the Python file.</p>
<ul>
<li><p class="first">For the Python part</p>
<p>At the top-level of the Pymacs distribution, then execute <tt class="docutils literal"><span class="pre">python</span>
<span class="pre">setup.py</span> <span class="pre">install</span></tt>.  First, the script copies a few source files while
presetting the version strings in them.  Second, it installs the
Python package through the Python standard Distutils tool.  To get
an option reminder, do <tt class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span> <span class="pre">--help</span></tt>.  Consult
the Distutils documentation if you need more information about this.</p>
<p>That's all to it.  To check that <tt class="file docutils literal"><span class="pre">pymacs.py</span></tt> is properly
installed, start an interactive Python session and type <tt class="docutils literal"><span class="pre">from</span> <span class="pre">Pymacs</span>
<span class="pre">import</span> <span class="pre">lisp</span></tt>: you should not receive any error.</p>
</li>
<li><p class="first">For the Emacs part</p>
<p>This is usually done by hand now.  First select some directory along
the list kept in your Emacs <strong>load-path</strong>, for which you have
write access, and copy file <tt class="file docutils literal"><span class="pre">pymacs.el</span></tt> in that directory.</p>
<p>If you want speed, you should ideally byte-compile this file.  To do
so, go to that directory, launch Emacs, then give the command <tt class="docutils literal"><span class="pre">M-x</span>
<span class="pre">byte-compile-file</span> <span class="pre">RET</span> <span class="pre">pymacs.el</span> <span class="pre">RET</span></tt>.  If for some reason you intend
to such commands often, you could create a little script to do so.
Here is an example of such a script, assuming here that you use Emacs
and want to install in directory <tt class="file docutils literal"><span class="pre">~/share/emacs/lisp/</span></tt>:</p>
<pre class="literal-block">
#!/bin/bash
cp pymacs.el ~/share/emacs/lisp/
emacs -batch -eval '(byte-compile-file &quot;~/share/emacs/lisp/pymacs.el&quot;)'
</pre>
<p>You should be done now.  To check that <tt class="file docutils literal"><span class="pre">pymacs.el</span></tt> is properly
installed, return to your usual directories, start Emacs and give
it the command <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">load-library</span> <span class="pre">RET</span> <span class="pre">pymacs</span> <span class="pre">RET</span></tt>: you should not
receive any error.</p>
</li>
</ul>
<p>Some features from previous Pymacs releases have been dropped:</p>
<ul class="simple">
<li>There used to be a script for installing the Emacs Lisp file.  As it
was difficult to get it right in all circumstances; the script grew
an interactive mode and lot of options.  This is just not worth the
complexity, so this script is now gone.</li>
<li>Examples were all installed automatically, but at least for some of
them, this was more pollution than help.  You may browse the contents of
the <tt class="file docutils literal"><span class="pre">contrib/</span></tt> directory to learn about available examples.</li>
</ul>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id11" id="prepare-your-emacs-file" name="prepare-your-emacs-file">2.4&nbsp;&nbsp;&nbsp;Prepare your <tt class="file docutils literal"><span class="pre">.emacs</span></tt> file</a></h2>
<p>The <tt class="file docutils literal"><span class="pre">.emacs</span></tt> file is not given in the distribution, you likely
have one already in your home directory.  You need to add these lines:</p>
<pre class="literal-block">
(autoload 'pymacs-apply &quot;pymacs&quot;)
(autoload 'pymacs-call &quot;pymacs&quot;)
(autoload 'pymacs-eval &quot;pymacs&quot; nil t)
(autoload 'pymacs-exec &quot;pymacs&quot; nil t)
(autoload 'pymacs-load &quot;pymacs&quot; nil t)
;;(eval-after-load &quot;pymacs&quot;
;;  '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY&quot;))
</pre>
<p>If you plan to use a special directory to hold your own Pymacs code in
Python, which should be searched prior to the usual Python import search
path, then uncomment the last two lines (by removing the semi-colons)
and replace <em>YOUR-PYMACS-DIRECTORY</em> by the name of your special
directory.  If the file <tt class="file docutils literal"><span class="pre">~/.emacs</span></tt> does not exist, merely create
it with the above lines.  You are now all set to use Pymacs.</p>
<p>To check this, start a fresh Emacs session, and type <tt class="docutils literal"><span class="pre">M-x</span>
<span class="pre">pymacs-eval</span> <span class="pre">RET</span></tt>.  Emacs should prompt you for a Python expression.
Try <tt class="docutils literal"><span class="pre">repr(2L**111)</span> <span class="pre">RET</span></tt>.  The mini buffer should display
<cite>&quot;2596148429267413814265248164610048L&quot;</cite>. <tt class="docutils literal"><span class="pre">M-x</span> <span class="pre">pymacs-load</span> <span class="pre">RET</span></tt> should
prompt you for a Python module name.  Reply <tt class="docutils literal"><span class="pre">os</span> <span class="pre">RET</span> <span class="pre">RET</span></tt> (the second
<tt class="docutils literal"><span class="pre">RET</span></tt> is for accepting the default prefix.  This should have the
effect of importing the Python <strong>os</strong> module within Emacs.  Typing
<tt class="docutils literal"><span class="pre">M-:</span> <span class="pre">(os-getcwd)</span> <span class="pre">RET</span></tt> should echo the current directory in the message
buffer, as returned by the <strong>os.getcwd</strong> Python function.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id12" id="porting-and-caveats" name="porting-and-caveats">2.5&nbsp;&nbsp;&nbsp;Porting and caveats</a></h2>
<p>Pymacs has been initially developed on Linux, Python 1.5.2, and Emacs
20, and currently on Python 2.5, Emacs 22.1 and XEmacs 21.5.  It is
expected to work out of the box on many flavours of Unix, MS Windows and
Mac OSX, and also on many version of Python, Emacs and XEmacs.</p>
<p>From Pymacs 0.23 and upwards, Python 2.2 or better is likely needed,
and for the Pymacs proper, I rely on testers or users for portability
issues.  However, the validation suite itself requires Python 2.4 or
better, someone might choose to contribute the back porting.</p>
<p>Pymacs uses Emacs weak hash tables.  It can run without them, but then,
complex Python objects transmitted to Emacs will tie Python memory
forever.  It should not be a practical problem in most simple cases.
Some later versions of Emacs 20 silently create ordinary tables when
asked for weak hash tables.  Older Emacses do not have hash tables.</p>
<p>The <tt class="file docutils literal"><span class="pre">Pymacs</span></tt> Python package holds a single <tt class="file docutils literal"><span class="pre">pymacs.py</span></tt> file
(and the mandatory <tt class="file docutils literal"><span class="pre">__init__.py</span></tt>).  Programmers might elect, but
are not required, to install their own Pymacs applications either as
sub-modules or sub-packages on <tt class="file docutils literal"><span class="pre">Pymacs</span></tt>.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id13" id="emacs-lisp-structures-and-python-objects" name="emacs-lisp-structures-and-python-objects">3&nbsp;&nbsp;&nbsp;Emacs Lisp structures and Python objects</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id14" id="conversions" name="conversions">3.1&nbsp;&nbsp;&nbsp;Conversions</a></h2>
<p>Whenever Emacs Lisp calls Python functions giving them arguments, these
arguments are Emacs Lisp structures that should be converted into Python
objects in some way.  Conversely, whenever Python calls Emacs Lisp
functions, the arguments are Python objects that should be received
as Emacs Lisp structures.  We need some conventions for doing such
conversions.</p>
<p>Conversions generally transmit mutable Emacs Lisp structures as mutable
objects on the Python side, in such a way that transforming the object
in Python will effectively transform the structure on the Emacs Lisp
side (strings are handled a bit specially however, see below).  The
other way around, Python objects transmitted to Emacs Lisp often loose
their mutability, so transforming the Emacs Lisp structure is not
reflected on the Python side.</p>
<p>Pymacs sticks to standard Emacs Lisp, it explicitly avoids various Emacs
Lisp extensions.  One goal for many Pymacs users is taking some distance
from Emacs Lisp, so Pymacs is not overly pushing users deeper into it.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id15" id="simple-objects" name="simple-objects">3.2&nbsp;&nbsp;&nbsp;Simple objects</a></h2>
<p>Emacs Lisp <strong>nil</strong> and the equivalent Emacs Lisp <tt class="docutils literal"><span class="pre">()</span></tt> yield Python
<strong>None</strong>.  Python <strong>None</strong>, Python <strong>False</strong> and the Python
empty list <tt class="docutils literal"><span class="pre">[]</span></tt> are returned as <strong>nil</strong> in Emacs Lisp.  Notice
the assymetry, in that three different Python objects are mapped into
a single Emacs Lisp object.  So, neither <strong>False</strong> nor <tt class="docutils literal"><span class="pre">[]</span></tt> are
likely produced by automatic conversions from Emacs Lisp to Python.</p>
<p>Emacs Lisp <strong>t</strong> yields Python <strong>True</strong>.  Python <strong>True</strong> is
returned as <strong>t</strong> in Emacs Lisp.</p>
<p>Emacs Lisp numbers, either integer or floating, are converted in
equivalent Python numbers.  Emacs Lisp characters are really numbers
and yield Python numbers.  In the other direction, Python numbers are
converted into Emacs Lisp numbers, with the exception of long Python
integers and complex numbers.</p>
<p>Emacs Lisp strings are usually converted into equivalent Python strings.
As Python strings do not have text properties, these are not reflected.
This may be changed by setting the <strong>pymacs-mutable-strings</strong>
option: if this variable is not <strong>nil</strong>, Emacs Lisp strings are
then transmitted opaquely.  Python strings are always converted into
Emacs Lisp strings.  Unicode strings are produced on the Python side for
Emacs Lisp multi-byte strings, but only when they do not fit in ASCII,
otherwise Python narrow strings are produced.  Conversely, Emacs Lisp
multi-byte strings are produced for Python Unicode strings, but only
when they do not fit ASCII, otherwise Emacs Lisp uni-byte strings are
produced.  Currently, Pymacs behaviour is undefined for users wandering
outside the limits of Emacs' <strong>utf-8</strong> coding system.</p>
<p>Emacs Lisp symbols yield <tt class="docutils literal"><span class="pre">lisp[STRING]</span></tt> notations on the Python
side, where <em>STRING</em> names the symbol.  In the other direction,
Python <tt class="docutils literal"><span class="pre">lisp[STRING]</span></tt> corresponds to an Emacs Lisp symbol printed
with that <em>STRING</em> which, of course, should then be a valid Emacs
Lisp symbol name.  As a convenience, <tt class="docutils literal"><span class="pre">lisp.SYMBOL</span></tt> on the Python side
yields an Emacs Lisp symbol with underscores replaced with hyphens;
this convention is welcome, as Emacs Lisp programmers commonly prefer
using dashes, where Python programmers use underlines.  Of course, this
<tt class="docutils literal"><span class="pre">lisp.SYMBOL</span></tt> notation is only usable when the <em>SYMBOL</em> is a
valid Python identifier, while not being a Python keyword.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id16" id="sequences" name="sequences">3.3&nbsp;&nbsp;&nbsp;Sequences</a></h2>
<p>The case of strings has been discussed in the previous section.</p>
<p>Proper Emacs Lisp lists, those for which the <strong>cdr</strong> of last cell
is <strong>nil</strong>, are normally transmitted opaquely to Python.  If
<strong>pymacs-forget-mutability</strong> is set, or if Python later asks for
these to be expanded, proper Emacs Lisp lists get converted into Python
lists, if we except the empty list, which is always converted as Python
<strong>None</strong>.  In the other direction, Python lists are always converted
into proper Emacs Lisp lists.</p>
<p>Emacs Lisp vectors are normally transmitted opaquely to Python.
However, if <strong>pymacs-forget-mutability</strong> is set, or if Python
later asks for these to be expanded, Emacs Lisp vectors get converted
into Python tuples.  In the other direction, Python tuples are always
converted into Emacs Lisp vectors.</p>
<p>Remember the rule: <cite>Round parentheses correspond to square brackets!</cite>.
It works for lists, vectors, tuples, seen from either Emacs Lisp or
Python.</p>
<p>The above choices were debatable.  Since Emacs Lisp proper lists
and Python lists are the bread-and-butter of algorithms modifying
structures, at least in my experience, I guess they are more naturally
mapped into one another, this spares many casts in practice.  While in
Python, the most usual idiom for growing lists is appending to their
end, the most usual idiom in Emacs Lisp to grow a list is by cons'ing
new items at its beginning:</p>
<pre class="literal-block">
(setq accumulator (cons 'new-item accumulator))
</pre>
<p>or more simply:</p>
<pre class="literal-block">
(push 'new-item accumulator)
</pre>
<p>So, in case speed is especially important and many modifications
happen in a row on the same side, while order of elements ought to
be preserved, some <tt class="docutils literal"><span class="pre">(nreverse</span> <span class="pre">...)</span></tt> on the Emacs Lisp side or
<tt class="docutils literal"><span class="pre">.reverse()</span></tt> on the Python side side might be needed.  Surely, proper
lists in Emacs Lisp and lists in Python are the normal structure for
which length is easily modified.</p>
<p>We cannot so easily change the size of a vector, the same as it is a bit
more of a stunt to <em>modify</em> a tuple.  The shape of these objects is
fixed.  Mapping vectors to tuples, which is admittedly strange, will
only be done if the Python side requests an expanded copy, otherwise an
opaque Emacs Lisp object is seen in Python.  In the other direction,
whenever an Emacs Lisp vector is needed, one has to write
<tt class="docutils literal"><span class="pre">tuple(python_list)</span></tt> while transmitting the object.  Such
transmissions are most probably to be unusual, as people are not going
to blindly transmit whole big structures back and forth between Emacs
and Python, they would rather do it once in a while only, and do only
local modifications afterwards.  The infrequent casting to <strong>tuple</strong>
for getting an Emacs Lisp vector seems to suggest that we did a
reasonable compromise.</p>
<p>In Python, both tuples and lists have O(1) access, so there is no real
speed consideration there.  Emacs Lisp is different: vectors have
O(1) access while lists have O(N) access.  The rigidity of Emacs Lisp
vectors is such that people do not resort to vectors unless there
is a speed issue, so in real Emacs Lisp practice, vectors are used
rather parsimoniously.  So much, in fact, that Emacs Lisp vectors are
overloaded for what they are not meant: for example, very small vectors
are used to represent X events in key-maps, programmers only want to
test vectors for their type, or users just like bracketed syntax.  The
speed of access is hardly an issue then.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id17" id="opaque-objects" name="opaque-objects">3.4&nbsp;&nbsp;&nbsp;Opaque objects</a></h2>
<div class="section">
<h3><a class="toc-backref" href="#id18" id="emacs-lisp-handles" name="emacs-lisp-handles">3.4.1&nbsp;&nbsp;&nbsp;Emacs Lisp handles</a></h3>
<p>When a Python function is called from Emacs Lisp, the function arguments
have already been converted to Python types from Emacs Lisp types and
the function result is going to be converted back to Emacs Lisp.</p>
<p>Several Emacs Lisp objects do not have Python equivalents, like for
Emacs windows, buffers, markers, overlays, etc.  It is nevertheless
useful to pass them to Python functions, hoping that these Python
functions will <em>operate</em> on these Emacs Lisp objects.  Of course, the
Python side may not itself modify such objects, it has to call for
Emacs services to do so.  Emacs Lisp handles are a mean to ease this
communication.</p>
<p>Whenever an Emacs Lisp object may not be converted to a Python object,
an Emacs Lisp handle is created and used instead.  Whenever that Emacs
Lisp handle is returned into Emacs Lisp from a Python function, or
is used as an argument to an Emacs Lisp function from Python, the
original Emacs Lisp object behind the Emacs Lisp handle is automatically
retrieved.</p>
<p>Emacs Lisp handles are either instances of the internal <strong>Lisp</strong>
class, or of one of its subclasses.  If <em>OBJECT</em> is an Emacs
Lisp handle, and if the underlying Emacs Lisp object is an Emacs
Lisp sequence, then whenever <tt class="docutils literal"><span class="pre">OBJECT[INDEX]</span></tt>, <tt class="docutils literal"><span class="pre">OBJECT[INDEX]</span> <span class="pre">=</span>
<span class="pre">VALUE</span></tt> and <tt class="docutils literal"><span class="pre">len(OBJECT)</span></tt> are meaningful, these may be used to
fetch or alter an element of the sequence directly in Emacs Lisp
space.  Also, if <em>OBJECT</em> corresponds to an Emacs Lisp function,
<tt class="docutils literal"><span class="pre">OBJECT(ARGUMENTS)</span></tt> may be used to apply the Emacs Lisp function over
the given arguments.  Since arguments have been evaluated the Python
way on the Python side, it would be conceptual overkill evaluating them
again the Emacs Lisp way on the Emacs Lisp side, so Pymacs manage to
quote arguments for defeating Emacs Lisp evaluation.  The same logic
applies the other way around.</p>
<p>Emacs Lisp handles have a <tt class="docutils literal"><span class="pre">value()</span></tt> method, which merely returns
self.  They also have a <tt class="docutils literal"><span class="pre">copy()</span></tt> method, which tries to <em>open
the box</em> if possible.  Emacs Lisp proper lists are turned into Python
lists, Emacs Lisp vectors are turned into Python tuples.  Then,
modifying the structure of the copy on the Python side has no effect on
the Emacs Lisp side.</p>
<p>For Emacs Lisp handles, <tt class="docutils literal"><span class="pre">str()</span></tt> returns an Emacs Lisp representation
of the handle which should be <strong>eq</strong> to the original object if
read back and evaluated in Emacs Lisp. <tt class="docutils literal"><span class="pre">repr()</span></tt> returns a Python
representation of the expanded Emacs Lisp object.  If that Emacs Lisp
object has an Emacs Lisp representation which Emacs Lisp could read
back, then <tt class="docutils literal"><span class="pre">repr()</span></tt> value is such that it could be read back and
evaluated in Python as well, this would result in another object which
is <strong>equal</strong> to the original, but not necessarily <strong>eq</strong>.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id19" id="python-handles" name="python-handles">3.4.2&nbsp;&nbsp;&nbsp;Python handles</a></h3>
<p>The same as Emacs Lisp handles are useful for handling Emacs Lisp
objects on the Python side, Python handles are useful for handling
Python objects on the Emacs Lisp side.</p>
<p>Many Python objects do not have direct Emacs Lisp equivalents, including
long integers, complex numbers, modules, classes, instances and surely a
lot of others.  When such are being transmitted to the Emacs Lisp side,
Pymacs use Python handles.  These are automatically recovered into the
original Python objects whenever transmitted back to Python, either as
arguments to a Python function, as the Python function itself, or as the
return value of an Emacs Lisp function called from Python.</p>
<p>The objects represented by these Python handles may be inspected or
modified using the basic library of Python functions.  For example, in:</p>
<pre class="literal-block">
(pymacs-exec &quot;import re&quot;)
(setq matcher (pymacs-eval &quot;re.compile('PATTERN').match&quot;))
(pymacs-call matcher ARGUMENT)
</pre>
<p>the <strong>setq</strong> line above could be decomposed into:</p>
<pre class="literal-block">
(setq compiled (pymacs-eval &quot;re.compile('PATTERN')&quot;)
      matcher (pymacs-call &quot;getattr&quot; compiled &quot;match&quot;))
</pre>
<p>This example shows that one may use <strong>pymacs-call</strong> with
<strong>getattr</strong> as the function, to get a wanted attribute for a Python
object.</p>
</div>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id20" id="usage-on-the-emacs-lisp-side" name="usage-on-the-emacs-lisp-side">4&nbsp;&nbsp;&nbsp;Usage on the Emacs Lisp side</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id21" id="pymacs-exec" name="pymacs-exec">4.1&nbsp;&nbsp;&nbsp;<strong>pymacs-exec</strong></a></h2>
<p>Function <tt class="docutils literal"><span class="pre">(pymacs-exec</span> <span class="pre">TEXT)</span></tt> gets <em>TEXT</em> executed as a Python
statement, and its value is always <strong>nil</strong>.  So, this function may
only be useful because of its possible side effects on the Python side.</p>
<p>This function may also be called interactively:</p>
<pre class="literal-block">
M-x pymacs-exec RET TEXT RET
</pre>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id22" id="pymacs-eval" name="pymacs-eval">4.2&nbsp;&nbsp;&nbsp;<strong>pymacs-eval</strong></a></h2>
<p>Function <tt class="docutils literal"><span class="pre">(pymacs-eval</span> <span class="pre">TEXT)</span></tt> gets <em>TEXT</em> evaluated as a Python
expression, and returns the value of that expression converted back to
Emacs Lisp.</p>
<p>This function may also be called interactively:</p>
<pre class="literal-block">
M-x pymacs-eval RET TEXT RET
</pre>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id23" id="pymacs-call" name="pymacs-call">4.3&nbsp;&nbsp;&nbsp;<strong>pymacs-call</strong></a></h2>
<p>Function <tt class="docutils literal"><span class="pre">(pymacs-call</span> <span class="pre">FUNCTION</span> <span class="pre">ARGUMENT...)</span></tt> will get Python to
apply the given <em>FUNCTION</em> over zero or more <em>ARGUMENT</em>.
<em>FUNCTION</em> is either a string holding Python source code for a
function (like a mere name, or even an expression), or else, a Python
handle previously received from Python, and hopefully holding a callable
Python object.  Each <em>ARGUMENT</em> gets separately converted to Python
before the function is called. <strong>pymacs-call</strong> returns the resulting
value of the function call, converted back to Emacs Lisp.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id24" id="pymacs-apply" name="pymacs-apply">4.4&nbsp;&nbsp;&nbsp;<strong>pymacs-apply</strong></a></h2>
<p>Function <tt class="docutils literal"><span class="pre">(pymacs-apply</span> <span class="pre">FUNCTION</span> <span class="pre">ARGUMENTS)</span></tt> will get Python to
apply the given <em>FUNCTION</em> over the given <em>ARGUMENTS</em>.
<em>ARGUMENTS</em> is a list containing all arguments, or <strong>nil</strong>
if there is none.  Besides arguments being bundled together
instead of given separately, the function acts pretty much like
<strong>pymacs-call</strong>.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id25" id="pymacs-load" name="pymacs-load">4.5&nbsp;&nbsp;&nbsp;<strong>pymacs-load</strong></a></h2>
<p>Function <tt class="docutils literal"><span class="pre">(pymacs-load</span> <span class="pre">MODULE</span> <span class="pre">PREFIX)</span></tt> imports the Python
<em>module</em> into Emacs Lisp space. <em>MODULE</em> is the name of the
file containing the module, without any <tt class="file docutils literal"><span class="pre">.py</span></tt> or <tt class="file docutils literal"><span class="pre">.pyc</span></tt>
extension.  If the directory part is omitted in <em>MODULE</em>, the
module will be looked into the current Python search path.  Dot notation
may be used when the module is part of a package.  Each top-level
function in the module produces a trampoline function in Emacs Lisp
having the same name, except that underlines in Python names are
turned into dashes in Emacs Lisp, and that <em>PREFIX</em> is uniformly
added before the Emacs Lisp name (as a way to avoid name clashes).
<em>PREFIX</em> may be omitted, in which case it defaults to base name
of <em>MODULE</em> with underlines turned into dashes, and followed by a
dash.</p>
<p>Note that <strong>pymacs-load</strong> has the effect of declaring the module
variables and methods the Emacs Lisp side, but it does <em>not</em> declare
anything on the Python side.  Of course, Python imports the module
before making it available for Emacs, but there is no Pymacs ready
variable on the Python side holding that module.  If you need to import
<em>MODULE</em> in a variable on the Python side, the proper incantation
is <tt class="docutils literal"><span class="pre">(pymacs-exec</span> <span class="pre">&quot;import</span> <span class="pre">MODULE&quot;)</span></tt>.  And of course, that this latter
statement does not declare anything on the Emacs Lisp side.</p>
<p>Whenever <strong>pymacs_load_hook</strong> is defined in the loaded
Python module, <strong>pymacs-load</strong> calls it without arguments,
but before creating the Emacs view for that module.  So, the
<strong>pymacs_load_hook</strong> function may create new definitions or even add
<strong>interaction</strong> attributes to functions.</p>
<p>The return value of a successful <strong>pymacs-load</strong> is the module
object.  An optional third argument, <em>noerror</em>, when given and not
<strong>nil</strong>, will have <strong>pymacs-load</strong> to return <strong>nil</strong> instead
of raising an error, if the Python module could not be found.</p>
<p>When later calling one of these trampoline functions, all provided
arguments are converted to Python and transmitted, and the function
return value is later converted back to Emacs Lisp.  It is left to
the Python side to check for argument consistency.  However, for an
interactive function, the interaction specification drives some checking
on the Emacs Lisp side.  Currently, there is no provision for collecting
keyword arguments in Emacs Lisp.</p>
<p>This function may also be called interactively:</p>
<pre class="literal-block">
M-x pymacs-load RET MODULE RET PREFIX RET
</pre>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id26" id="expected-usage" name="expected-usage">4.6&nbsp;&nbsp;&nbsp;Expected usage</a></h2>
<p>We do not expect that <strong>pymacs-exec</strong>, <strong>pymacs-eval</strong>,
<strong>pymacs-call</strong> or <strong>pymacs-apply</strong> will be much used, if
ever, in most Pymacs applications.  In practice, the Emacs Lisp side
of a Pymacs application might call <strong>pymacs-load</strong> a few times for
linking into the Python modules, with the indirect effect of defining
trampoline functions for these modules on the Emacs Lisp side, which can
later be called like usual Emacs Lisp functions.</p>
<p>These imported functions are usually those which are of interest for the
user, and the preferred way to call Python services with Pymacs.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id27" id="special-emacs-lisp-variables" name="special-emacs-lisp-variables">4.7&nbsp;&nbsp;&nbsp;Special Emacs Lisp variables</a></h2>
<p>Users could alter the inner working of Pymacs through a few variables,
these are all documented here.  Except for <strong>pymacs-load-path</strong>,
which should be set before calling any Pymacs function, the value of
these variables can be changed at any time.</p>
<div class="section">
<h3><a class="toc-backref" href="#id28" id="pymacs-load-path" name="pymacs-load-path">4.7.1&nbsp;&nbsp;&nbsp;pymacs-load-path</a></h3>
<p>Users might want to use special directories for holding their Python
modules, when these modules are meant to be used from Emacs.  Best is to
preset <strong>pymacs-load-path</strong>, <strong>nil</strong> by default, to a list of
these directory names.  (Tilde expansions and such occur automatically.)</p>
<p>Here is how it works.  The first time Pymacs is needed from Emacs,
a Pymacs helper is automatically started as an Emacs subprocess, and
given as arguments all strings in the <strong>pymacs-load-path</strong> list.
These arguments are added at the beginning of <strong>sys.path</strong>,
or moved at the beginning if they were already on <strong>sys.path</strong>.
So in practice, nothing is removed from <strong>sys.path</strong>.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id29" id="pymacs-trace-transit" name="pymacs-trace-transit">4.7.2&nbsp;&nbsp;&nbsp;pymacs-trace-transit</a></h3>
<p>The <strong>*Pymacs*</strong> buffer, within Emacs, holds a trace of transactions
between Emacs and Python.  When <strong>pymacs-trace-transit</strong> is
<strong>nil</strong>, the buffer only holds the last bi-directional transaction
(a request and a reply).  In this case, it gets erased before each and
every transaction.  If that variable is <strong>t</strong>, all transactions are
kept.  This could be useful for debugging, but the drawback is that
this buffer could grow big over time, to the point of diminishing Emacs
performance.  As a compromise, that variable may also be a cons cell
of integers <tt class="docutils literal"><span class="pre">(KEEP</span> <span class="pre">.</span> <span class="pre">LIMIT)</span></tt>, in which case the buffer is reduced to
approximately <em>KEEP</em> bytes whenever its size exceeds <em>LIMIT</em>
bytes, by deleting an integral number of lines from its beginning.  The
default setting for <strong>pymacs-trace-transit</strong> is <tt class="docutils literal"><span class="pre">(5000</span> <span class="pre">.</span> <span class="pre">30000)</span></tt>.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id30" id="pymacs-forget-mutability" name="pymacs-forget-mutability">4.7.3&nbsp;&nbsp;&nbsp;pymacs-forget-mutability</a></h3>
<p>The default behaviour of Pymacs is to transmit Emacs Lisp objects to
Python in such a way that they are fully modifiable from the Python
side, would it mean triggering Emacs Lisp functions to act on them.
When <strong>pymacs-forget-mutability</strong> is not <strong>nil</strong>, the behaviour
is changed, and the flexibility is lost.  Pymacs then tries to expand
proper lists and vectors as full copies when transmitting them on the
Python side.  This variable, seen as a user setting, is best left to
<strong>nil</strong>.  It may be temporarily overridden within some functions,
when deemed useful.</p>
<p>There is no corresponding variable from objects transmitted to Emacs
from Python.  Pymacs automatically expands what gets transmitted.
Mutability is preserved only as a side-effect of not having a natural
Emacs Lisp representation for the Python object.  This asymmetry is on
purpose, yet debatable.  Maybe Pymacs could have a variable telling that
mutability is important for Python objects?  That would give Pymacs
users the capability of restoring the symmetry somewhat, yet so far, in
our experience, this has never been needed.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id31" id="pymacs-mutable-strings" name="pymacs-mutable-strings">4.7.4&nbsp;&nbsp;&nbsp;pymacs-mutable-strings</a></h3>
<p>Strictly speaking, Emacs Lisp strings are mutable. Yet, it does not
come naturally to a Python programmer to modify a string <em>in-place</em>, as
Python strings are never mutable.  When <strong>pymacs-mutable-strings</strong>
is <strong>nil</strong>, which is the default setting, Emacs Lisp strings are
transmitted to Python as Python strings, and so, loose their mutability.
Moreover, text properties are not reflected on the Python side.  But
if that variable is not <strong>nil</strong>, Emacs Lisp strings are rather
passed as Emacs Lisp handles.  This variable is ignored whenever
<strong>pymacs-forget-mutability</strong> is set.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id32" id="timeout-variables" name="timeout-variables">4.7.5&nbsp;&nbsp;&nbsp;Timeout variables</a></h3>
<p>Emacs needs to protect itself a bit, in case the Pymacs service program,
which handles the Python side of requests, would not start correctly, or
maybe later die unexpectedly.  So, whenever Emacs reads data coming from
that program, it sets a time limit, and take some action whenever that
time limit expires.  All times are expressed in seconds.</p>
<p>The <strong>pymacs-timeout-at-start</strong> variable defaults to 30 seconds,
this time should only be increased if a given machine is so heavily
loaded that the Pymacs service program has not enough of 30 seconds to
start, in which case Pymacs refuses to work, with an appropriate message
in the mini buffer.</p>
<p>The two remaining timeout variables almost never need to be changed
in practice.  When Emacs is expecting a reply from Python, it might
repeatedly check the status of the Pymacs service program when that
reply is not received fast enough, just to make sure that this program
did not die.  The <strong>pymacs-timeout-at-reply</strong> variable, which
defaults to 5, says how many seconds to wait without checking, while
expecting the first line of a reply.  The <strong>pymacs-timeout-at-line</strong>
variable, which defaults to 2, says how many seconds to wait without
checking, while expecting a line of the reply after the first.</p>
</div>
<div class="section">
<h3><a class="toc-backref" href="#id33" id="pymacs-dreadful-zombies" name="pymacs-dreadful-zombies">4.7.6&nbsp;&nbsp;&nbsp;pymacs-dreadful-zombies</a></h3>
<p>When the Pymacs helper dies, all useful Python objects it might contain
also die with it.  However, if the death occurs unexpectedly, instead of
normally at the end of the Emacs session, there might now exist dangling
references in Emacs Lisp space towards those vanished Python objects.</p>
<p>Pymacs could not do much without a Pymacs helper, and likely, a new
one will soon be created within the same Emacs session, and brand new
Python objects may be created within that new helper.  Now, and this is
a bit technical, all references are transmitted in form of object slot
numbers.  As a consequence, the new Pymacs helper should be careful at
never allocating a new Python object using a slot number of a useful
vanished object, as this might possibly create fatal confusion.</p>
<p>There is not enough information for the new Pymacs helper to recreate
the useful objects which disappeared.  However, there is enough
machinery to recover all their slot numbers, and then, all these slots
are initialized with so-called <em>zombies</em>.  If Emacs later calls a
vanished Python object, this merely awakes its zombie, which will then
make some noise, then fall asleep again.  The noise has the form of a
diagnostic within the <tt class="docutils literal"><span class="pre">*Messages*</span></tt> buffer, sometimes visible in the
mini-buffer as well when the mini-buffer is not simultaneously used for
some other purpose.</p>
<p>Zombies get more dreadful if <strong>pymacs-dreadful-zombies</strong> is set to a
non-<strong>nil</strong> value.  In this case, calling a vanished Python object
raises an error that will eventually interrupt the current computation.
Such a behaviour might be useful for debugging purposes, or for making
sure that no call to a vanished Python object goes unnoticed.</p>
<p>In previous Pymacs releases, zombies were always dreadful, under the
assumption that calling a vanished object is a real error.  However, it
could cause irritation in some circumstances, like when associated with
frequently triggered Emacs Lisp hook functions.  That's why that, by
default, zombies have been finally turned into more innocuous beings!</p>
</div>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id34" id="usage-on-the-python-side" name="usage-on-the-python-side">5&nbsp;&nbsp;&nbsp;Usage on the Python side</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id35" id="python-setup" name="python-setup">5.1&nbsp;&nbsp;&nbsp;Python setup</a></h2>
<p>For Python modules meant to be used from Emacs and which receive nothing
but Emacs <strong>nil</strong>, numbers or strings, or return nothing but Python
<strong>None</strong>, numbers or strings, then Pymacs requires little or no
setup.  Otherwise, use <tt class="docutils literal"><span class="pre">from</span> <span class="pre">Pymacs</span> <span class="pre">import</span> <span class="pre">lisp</span></tt> at the start of your
module.  If you need more Pymacs features, like the <strong>Let</strong> class,
then write <tt class="docutils literal"><span class="pre">from</span> <span class="pre">Pymacs</span> <span class="pre">import</span> <span class="pre">lisp,</span> <span class="pre">Let</span></tt>.</p>
<p>The Pymacs helper runs Python code to serve the Emacs side, and it is
blocked waiting until Emacs sends a request.  Until the Pymacs helper
returns a reply, Emacs is blocked in turn, yet fully listening to serve
eventual Python sub-requests, etc.  So, either Emacs or the Pymacs
helper is active at a given instant, but never both at once.</p>
<p>Unless Emacs has sent a request to the Pymacs helper and is expecting
a reply, it is just not listening to receive Python requests.  So, any
other Python thread may not asynchronously use Pymacs to get Emacs
services.  The design of the Python application should be such that the
communication is always be channelled from the main Python thread.</p>
<p>When Pymacs starts, all process signals are inhibited on the Python
side.  Yet, <strong>SIGINT</strong> gets re-enabled while running user functions.
If the user elects to reactivate some other signal in her Python code,
she should do so as to not damage or severe the communication protocol.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id36" id="emacs-lisp-symbols" name="emacs-lisp-symbols">5.2&nbsp;&nbsp;&nbsp;Emacs Lisp symbols</a></h2>
<p><strong>lisp</strong> is a special object which has useful built-in magic.  Its
attributes do nothing but represent Emacs Lisp symbols, created on the
fly as needed (symbols also have their built-in magic).</p>
<p>As special cases, <tt class="docutils literal"><span class="pre">lisp.nil</span></tt> or <tt class="docutils literal"><span class="pre">lisp[&quot;nil&quot;]</span></tt> are the same
as <strong>None</strong>, and <tt class="docutils literal"><span class="pre">lisp.t</span></tt> or <tt class="docutils literal"><span class="pre">lisp[&quot;t&quot;]</span></tt> are the same as
<strong>True</strong>.  Otherwise, both <tt class="docutils literal"><span class="pre">lisp.SYMBOL</span></tt> and <tt class="docutils literal"><span class="pre">lisp[STRING]</span></tt>
yield objects of the internal <strong>Symbol</strong> type.  These are genuine
Python objects, that could be referred to by simple Python variables.
One may write <tt class="docutils literal"><span class="pre">quote</span> <span class="pre">=</span> <span class="pre">lisp.quote</span></tt>, for example, and use <tt class="docutils literal"><span class="pre">quote</span></tt>
afterwards to mean that Emacs Lisp symbol.  If a Python function
received an Emacs Lisp symbol as an argument, it can check with <tt class="docutils literal"><span class="pre">==</span></tt>
if that argument is <tt class="docutils literal"><span class="pre">lisp.never</span></tt> or <tt class="docutils literal"><span class="pre">lisp.ask</span></tt>, say.  A Python
function may well choose to return some symbol, like <tt class="docutils literal"><span class="pre">lisp.always</span></tt>.</p>
<p>In Python, writing <tt class="docutils literal"><span class="pre">lisp.SYMBOL</span> <span class="pre">=</span> <span class="pre">VALUE</span></tt> or <tt class="docutils literal"><span class="pre">lisp[STRING]</span> <span class="pre">=</span> <span class="pre">VALUE</span></tt>
does assign <em>VALUE</em> to the corresponding symbol in Emacs Lisp
space.  Beware that in such cases, the <tt class="docutils literal"><span class="pre">lisp.</span></tt> prefix may not be
spared.  After <tt class="docutils literal"><span class="pre">result</span> <span class="pre">=</span> <span class="pre">lisp.result</span></tt>, one cannot hope that a later
<tt class="docutils literal"><span class="pre">result</span> <span class="pre">=</span> <span class="pre">3</span></tt> will have any effect in the Emacs Lisp space: this would
merely change the Python variable <tt class="docutils literal"><span class="pre">result</span></tt>, which was a reference to a
<strong>Symbol</strong> instance, so it is now a reference to the number 3.</p>
<p>The <strong>Symbol</strong> class has <tt class="docutils literal"><span class="pre">value()</span></tt> and <tt class="docutils literal"><span class="pre">copy()</span></tt> methods.  One
can use either <tt class="docutils literal"><span class="pre">lisp.SYMBOL.value()</span></tt> or <tt class="docutils literal"><span class="pre">lisp.SYMBOL.copy()</span></tt>
to access the Emacs Lisp value of a symbol, after conversion to
some Python object, of course.  However, if <tt class="docutils literal"><span class="pre">value()</span></tt> would have
given an Emacs Lisp handle, <tt class="docutils literal"><span class="pre">lisp.SYMBOL.copy()</span></tt> has the effect of
<tt class="docutils literal"><span class="pre">lisp.SYMBOL.value().copy()</span></tt>, that is, it returns the value of the
symbol as opened as possible.</p>
<p>A symbol may also be used as if it was a Python function, in which case
it really names an Emacs Lisp function that should be applied over the
following function arguments.  The result of the Emacs Lisp function
becomes the value of the call, with all due conversions of course.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id37" id="dynamic-bindings" name="dynamic-bindings">5.3&nbsp;&nbsp;&nbsp;Dynamic bindings</a></h2>
<p>As Emacs Lisp uses dynamic bindings, it is common that Emacs Lisp
programs use <strong>let</strong> for temporarily setting new values for some
Emacs Lisp variables having global scope.  These variables recover their
previous value automatically when the <strong>let</strong> gets completed, even
if an error occurs which interrupts the normal flow of execution.</p>
<p>Pymacs has a <strong>Let</strong> class to represent such temporary
settings.  Suppose for example that you want to recover the value of
<tt class="docutils literal"><span class="pre">lisp.mark()</span></tt> when the transient mark mode is active on the Emacs Lisp
side.  One could surely use <tt class="docutils literal"><span class="pre">lisp.mark(True)</span></tt> to <em>force</em> reading the
mark in such cases, but for the sake of illustration, let's ignore that,
and temporarily deactivate transient mark mode instead.  This could be
done this way:</p>
<pre class="literal-block">
try:
    let = Let()
    let.push(transient_mark_mode=None)
    ... USER CODE ...
finally:
    let.pop()
</pre>
<p><tt class="docutils literal"><span class="pre">let.push()</span></tt> accepts any number of keywords arguments.  Each keyword
name is interpreted as an Emacs Lisp symbol written the Pymacs way, with
underlines.  The value of that Emacs Lisp symbol is saved on the Python
side, and the value of the keyword becomes the new temporary value for
this Emacs Lisp symbol.  A later <tt class="docutils literal"><span class="pre">let.pop()</span></tt> restores the previous
value for all symbols which were saved together at the time of the
corresponding <tt class="docutils literal"><span class="pre">let.push()</span></tt>.  There may be more than one <tt class="docutils literal"><span class="pre">let.push()</span></tt>
call for a single <strong>Let</strong> instance, they stack within that instance.
Each <tt class="docutils literal"><span class="pre">let.pop()</span></tt> will undo one and only one <tt class="docutils literal"><span class="pre">let.push()</span></tt> from the
stack, in the reverse order or the pushes.</p>
<p>When the <strong>Let</strong> instance disappears, either because the programmer
does <tt class="docutils literal"><span class="pre">del</span> <span class="pre">let</span></tt> or <tt class="docutils literal"><span class="pre">let</span> <span class="pre">=</span> <span class="pre">None</span></tt>, or just because the Python
<strong>let</strong> variable goes out of scope, all remaining <tt class="docutils literal"><span class="pre">let.pop()</span></tt> get
automatically executed, so the <strong>try</strong>/<strong>finally</strong> statement
may be omitted in practice.  For this omission to work flawlessly, the
programmer should be careful at not keeping extra references to the
<strong>Let</strong> instance.</p>
<p>The constructor call <tt class="docutils literal"><span class="pre">let</span> <span class="pre">=</span> <span class="pre">Let()</span></tt> also has an implied initial
<tt class="docutils literal"><span class="pre">.push()</span></tt> over all given arguments, so the explicit <tt class="docutils literal"><span class="pre">let.push()</span></tt> may
be omitted as well.  In practice, this sums up and the above code could
be reduced to a mere:</p>
<pre class="literal-block">
let = Let(transient_mark_mode=None)
... USER CODE ...
</pre>
<p>Be careful at assigning the result of the constructor to some Python
variable.  Otherwise, the instance would disappear immediately after
having been created, restoring the Emacs Lisp variable much too soon.</p>
<p>Any variable to be bound with <strong>Let</strong> should have been bound in
advance on the Emacs Lisp side.  This restriction usually does no kind
of harm.  Yet, it will likely be lifted in some later version of Pymacs.</p>
<p>The <strong>Let</strong> class has other methods meant for some macros which are
common in Emacs Lisp programming, in the spirit of <strong>let</strong> bindings.
These method names look like <tt class="docutils literal"><span class="pre">push_*</span></tt> or <tt class="docutils literal"><span class="pre">pop_*</span></tt>, where Emacs Lisp
macros are <tt class="docutils literal"><span class="pre">save-*</span></tt>.  One has to use the matching <tt class="docutils literal"><span class="pre">pop_*</span></tt> for
undoing the effect of a given <tt class="docutils literal"><span class="pre">push_*</span></tt> rather than a mere <tt class="docutils literal"><span class="pre">.pop()</span></tt>:
the Python code is clearer, this also ensures that things are undone in
the proper order.  The same <strong>Let</strong> instance may use many <tt class="docutils literal"><span class="pre">push_*</span></tt>
methods, their effects nest.</p>
<p><tt class="docutils literal"><span class="pre">push_excursion()</span></tt> and <tt class="docutils literal"><span class="pre">pop_excursion()</span></tt> save and restore
the current buffer, point and mark. <tt class="docutils literal"><span class="pre">push_match_data()</span></tt> and
<tt class="docutils literal"><span class="pre">pop_match_data()</span></tt> save and restore the state of the last regular
expression match. <tt class="docutils literal"><span class="pre">push_restriction()</span></tt> and <tt class="docutils literal"><span class="pre">pop_restriction()</span></tt> save
and restore the current narrowing limits. <tt class="docutils literal"><span class="pre">push_selected_window()</span></tt> and
<tt class="docutils literal"><span class="pre">pop_selected_window()</span></tt> save and restore the fact that a window holds
the cursor. <tt class="docutils literal"><span class="pre">push_window_excursion()</span></tt> and <tt class="docutils literal"><span class="pre">pop_window_excursion()</span></tt>
save and restore the current window configuration in the Emacs display.</p>
<p>As a convenience, <tt class="docutils literal"><span class="pre">let.push()</span></tt> and all other <tt class="docutils literal"><span class="pre">push_*</span></tt> methods return
the <strong>Let</strong> instance.  This helps chaining various <tt class="docutils literal"><span class="pre">push_*</span></tt> right
after the instance generation.  For example, one may write:</p>
<pre class="literal-block">
let = Let().push_excursion()
if True:
    ... USER CODE ...
del let
</pre>
<p>The <tt class="docutils literal"><span class="pre">if</span> <span class="pre">True:</span></tt> (use <tt class="docutils literal"><span class="pre">if</span> <span class="pre">1:</span></tt> with older Python releases, some people
might prefer writing <tt class="docutils literal"><span class="pre">if</span> <span class="pre">let:</span></tt> anyway), has the only goal of indenting
<em>USER CODE</em>, so the scope of the <strong>let</strong> variable is made very
explicit.  This is purely stylistic, and not at all necessary.  The last
<tt class="docutils literal"><span class="pre">del</span> <span class="pre">let</span></tt> might be omitted in a few circumstances, for example if the
excursion lasts until the end of the Python function.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id38" id="raw-emacs-lisp-expressions" name="raw-emacs-lisp-expressions">5.4&nbsp;&nbsp;&nbsp;Raw Emacs Lisp expressions</a></h2>
<p>Pymacs offers a device for evaluating a raw Emacs Lisp expression, or a
sequence of such, expressed as a string.  One merely uses <strong>lisp</strong>
as a function, like this:</p>
<pre class="literal-block">
lisp(&quot;&quot;&quot;
...
POSSIBLY-LONG-SEQUENCE-OF-LISP-EXPRESSIONS
...
&quot;&quot;&quot;)
</pre>
<p>The Emacs Lisp value of the last or only expression in the sequence
becomes the value of the <strong>lisp</strong> call, after conversion back to
Python.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id39" id="user-interaction" name="user-interaction">5.5&nbsp;&nbsp;&nbsp;User interaction</a></h2>
<p>Emacs functions have the concept of user interaction for completing the
specification of their arguments while being called.  This happens only
when a function is interactively called by the user, it does not happen
when a function is directly called by another.  As Python does not have
a corresponding facility, a bit of trickery was needed to retrofit that
facility on the Python side.</p>
<p>After loading a Python module but prior to creating an Emacs view
for this module, Pymacs decides whether loaded functions will be
interactively callable from Emacs, or not.  Whenever a function has
an <strong>interaction</strong> attribute, this attribute holds the Emacs
interaction specification for this function.  The specification is
either another Python function or a string.  In the former case, that
other function is called without arguments and should, maybe after
having consulted the user, return a list of the actual arguments to be
used for the original function.  In the latter case, the specification
string is used verbatim as the argument to the <tt class="docutils literal"><span class="pre">(interactive</span> <span class="pre">...)</span></tt>
function on the Emacs side.  To get a short reminder about how this
string is interpreted on the Emacs side, try <tt class="docutils literal"><span class="pre">C-h</span> <span class="pre">f</span> <span class="pre">interactive</span></tt>
within Emacs.  Here is an example where an empty string is used to
specify that an interactive has no arguments:</p>
<pre class="literal-block">
from Pymacs import lisp

def hello_world():
    &quot;`Hello world' from Python.&quot;
    lisp.insert(&quot;Hello from Python!&quot;)
hello_world.interaction = ''
</pre>
<!-- ` -->
<p>Versions of Python released before the integration of PEP 232 do not
allow users to add attributes to functions, so there is a fall-back
mechanism.  Let's presume that a given function does not have an
<strong>interaction</strong> attribute as explained above.  If the Python module
contains an <strong>interactions</strong> global variable which is a dictionary,
if that dictionary has an entry for the given function with a value
other than <strong>None</strong>, that function is going to be interactive on the
Emacs side.  Here is how the preceding example should be written for an
older version of Python, or when portability is at premium:</p>
<pre class="literal-block">
from Pymacs import lisp
interactions = &#64;{&#64;}

def hello_world():
    &quot;`Hello world' from Python.&quot;
    lisp.insert(&quot;Hello from Python!&quot;)
interactions[hello_world] = ''
</pre>
<p>One might wonder why we do not merely use <tt class="docutils literal"><span class="pre">lisp.interactive(...)</span></tt>
from within Python.  There is some magic in the Emacs Lisp interpreter
itself, looking for that call <em>before</em> the function is actually entered,
this explains why <tt class="docutils literal"><span class="pre">(interactive</span> <span class="pre">...)</span></tt> has to appear first in an Emacs
Lisp <strong>defun</strong>.  Pymacs could try to scan the already compiled
form of the Python code, seeking for <tt class="docutils literal"><span class="pre">lisp.interactive</span></tt>, but as the
evaluation of <strong>lisp.interactive</strong> arguments could get arbitrarily
complex, it would a real challenge un-compiling that evaluation into
Emacs Lisp.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id40" id="key-bindings" name="key-bindings">5.6&nbsp;&nbsp;&nbsp;Key bindings</a></h2>
<p>An interactive function may be bound to a key sequence.</p>
<p>To translate bindings like <tt class="docutils literal"><span class="pre">C-x</span> <span class="pre">w</span></tt>, say, one might have to know a
bit more how Emacs Lisp processes string escapes like <tt class="docutils literal"><span class="pre">\C-x</span></tt> or
<tt class="docutils literal"><span class="pre">\M-\C-x</span></tt> in Emacs Lisp, and emulate it within Python strings, since
Python does not have such escapes.  <tt class="docutils literal"><span class="pre">\C-L</span></tt>, where L is an upper case
letter, produces a character which ordinal is the result of subtracting
0x40 from ordinal of <tt class="docutils literal"><span class="pre">L</span></tt>.  <tt class="docutils literal"><span class="pre">\M-</span></tt> has the ordinal one gets by adding
0x80 to the ordinal of following described character.  So people can
use self-inserting non-ASCII characters, <tt class="docutils literal"><span class="pre">\M-</span></tt> is given another
representation, which is to replace the addition of 0x80 by prefixing
with `ESC', that is 0x1b.</p>
<!-- ` -->
<p>So <tt class="docutils literal"><span class="pre">\C-x</span></tt> in Emacs is 'x18' in Python.  This is easily found, using
an interactive Python session, by giving it: chr(ord('X') - ord('A') +
1).  An easier way would be using the <strong>kbd</strong> function on the Emacs
Lisp side, like with lisp.kbd('C-x w') or lisp.kbd('M-&lt;f2&gt;').</p>
<p>To bind the F1 key to the <strong>helper</strong> function in some
<strong>module</strong>:</p>
<pre class="literal-block">
lisp.global_set_key((lisp.f1,), lisp.module_helper)
</pre>
<p><tt class="docutils literal"><span class="pre">(item,)</span></tt> is a Python tuple yielding an Emacs Lisp vector.
<tt class="docutils literal"><span class="pre">lisp.f1</span></tt> translates to the Emacs Lisp symbol <strong>f1</strong>.  So, Python
<tt class="docutils literal"><span class="pre">(lisp.f1,)</span></tt> is Emacs Lisp <tt class="docutils literal"><span class="pre">[f1]</span></tt>.  Keys like <tt class="docutils literal"><span class="pre">[M-f2]</span></tt> might
require some more ingenuity, one may write either <tt class="docutils literal"><span class="pre">(lisp['M-f2'],)</span></tt> or
<tt class="docutils literal"><span class="pre">(lisp.M_f2,)</span></tt> on the Python side.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id41" id="debugging" name="debugging">6&nbsp;&nbsp;&nbsp;Debugging</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id42" id="on-the-communication-protocol" name="on-the-communication-protocol">6.1&nbsp;&nbsp;&nbsp;On the communication protocol</a></h2>
<p>Initially, the Pymacs communication protocol was rather simple deep
down, merely using evaluation on arrival on both sides.  All the rest
was recursion trickery over that simple idea.  But the magic was fragile
to interruption requests, so the protocol has been revisited a bit so
each message action could be recognized before evaluation is attempted.
The idea (not fully implemented yet) is to make the protocol part immune
to interruptions, but to allow evaluations themselves to be interrupted.</p>
<blockquote>
<ul class="simple">
<li>As it is more easy to generate than to parse, and also because Emacs
has a Lisp parser and Python has a Python parser, Emacs generates
Python code when preparing a message to the Pymacs helper, and Python
generates Emacs Lisp expressions when preparing a message for Emacs.</li>
<li>Messages are exchanged in strictly alternating directions (from
Python to Emacs, from Emacs to Python, etc.), the first message being
sent by the Pymacs helper just after it started, identifying the
current Pymacs version.</li>
<li>Messages in both directions have a similar envelope.  Each physical
message has a prefix, the message contents, and a newline.  The
prefix starts with either <tt class="docutils literal"><span class="pre">&lt;</span></tt> or <tt class="docutils literal"><span class="pre">&gt;</span></tt> to mark the directionality,
immediately followed by the decimal expression of the contents length
counted in characters, immediately followed by a single horizontal
tab.  The count excludes the prefix, but includes the newline.</li>
<li>In each direction, messages are made up of two elements: an action
keyword and a single argument (yet the argument may sometimes be
complex).  As a special case, memory cleanup messages, from Python
to Emacs, use four elements: the atom <strong>free</strong>, a list of slot
numbers to free, and then the real action and argument.  This is
because the cleanup is delayed and piggy-backed over some other
message from Python to Emacs.</li>
<li>For Emacs originated messages, the action and the argument are
separated by a space.  For Python originated messages, the action and
the argument are made into a Lisp list.</li>
<li>Most actions in the following table are available in both
directions, unless noted.  The first three actions <em>start</em> a new level
of Pymacs evaluation, the remaining actions end the current level.<ul>
<li><strong>eval</strong> requests the evaluation of its expression argument.</li>
<li><strong>exec</strong> requests the execution of its statement argument (this may
only be received on the Python side).</li>
<li><strong>expand</strong> requests the opening of an Emacs Lisp structure (this may
only be received on the Emacs side).</li>
<li><strong>return</strong> represents the normal reply to a request, the argument
holds the value to be returned (<strong>nil</strong> in case of <strong>exec</strong>).</li>
<li><strong>raise</strong> represents the error reply to a request, the argument
then holds a diagnostic string.</li>
</ul>
</li>
</ul>
</blockquote>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id43" id="the-pymacs-buffer" name="the-pymacs-buffer">6.2&nbsp;&nbsp;&nbsp;The <strong>*Pymacs*</strong> buffer</a></h2>
<p>Emacs and Python are two separate processes (well, each may use more
than one process).  Pymacs implements a simple communication protocol
between both, and does whatever needed so the programmers do not have
to worry about details.  The main debugging tool is the communication
buffer between Emacs and Python, which is named <strong>*Pymacs*</strong>.  By
default, this buffer gets erased before each transaction.  To make good
debugging use of it, first set <strong>pymacs-trace-transit</strong> to either
<strong>t</strong> or to some <tt class="docutils literal"><span class="pre">(KEEP</span> <span class="pre">.</span> <span class="pre">LIMIT)</span></tt>.  As it is sometimes helpful to
understand the communication protocol, it is briefly explained here,
using an artificially complex example to do so.  Consider:</p>
<pre class="literal-block">
(pymacs-eval &quot;lisp('(pymacs-eval \&quot;repr(2L**111)\&quot;)')&quot;)
&quot;2596148429267413814265248164610048L&quot;
</pre>
<p>Here, Emacs asks Python to ask Emacs to ask Python for a simple bignum
computation.  Note that Emacs does not natively know how to handle big
integers, nor has an internal representation for them.  This is why I
use the <strong>repr</strong> function, so Python returns a string representation
of the result, instead of the result itself.  Here is a trace for this
example.  Imagine that Emacs stands on the left and that Python stands
on the right.  The <tt class="docutils literal"><span class="pre">&lt;</span></tt> character flags a message going from Python to
Emacs, while the <tt class="docutils literal"><span class="pre">&gt;</span></tt> character flags a message going from Emacs to
Python.  The number gives the length of the message, including the end
of line.  (Acute readers may notice that the first number is incorrect,
as the version number gets replaced in the example while this manual is
being produced.)</p>
<pre class="literal-block">
&lt;22     (version &quot;0.23&quot;)
&gt;43     eval lisp('(pymacs-eval &quot;repr(2L**111)&quot;)')
&lt;45     (eval (progn (pymacs-eval &quot;repr(2L**111)&quot;)))
&gt;19     eval repr(2L**111)
&lt;47     (return &quot;2596148429267413814265248164610048L&quot;)
&gt;45     return &quot;2596148429267413814265248164610048L&quot;
&lt;47     (return &quot;2596148429267413814265248164610048L&quot;)
</pre>
<p>Python evaluation is done in the context of the <strong>Pymacs.pymacs</strong>
module, so for example a mere <strong>reply</strong> really means
<tt class="docutils literal"><span class="pre">Pymacs.pymacs.reply</span></tt>.  On the Emacs Lisp side, there is no concept of
module name spaces, so we use the <tt class="docutils literal"><span class="pre">pymacs-</span></tt> prefix as an attempt to
stay clean.  Users should ideally refrain from naming their Emacs Lisp
objects with a <tt class="docutils literal"><span class="pre">pymacs-</span></tt> prefix.</p>
<p><strong>reply</strong> and <strong>pymacs-reply</strong> are special functions meant to
indicate that an expected result is finally transmitted. <strong>error</strong>
and <strong>pymacs-error</strong> are special functions that introduce
a string which explains an exception which recently occurred.
<strong>pymacs-expand</strong> is a special function implementing the <tt class="docutils literal"><span class="pre">copy()</span></tt>
methods of Emacs Lisp handles or symbols.  In all other cases, the
expression is a request for the other side, that request stacks until a
corresponding reply is received.</p>
<p>Part of the protocol manages memory, and this management generates some
extra-noise in the <strong>*Pymacs*</strong> buffer.  Whenever Emacs passes a
structure to Python, an extra pointer is generated on the Emacs side to
inhibit garbage collection by Emacs.  Python garbage collector detects
when the received structure is no longer needed on the Python side, at
which time the next communication will tell Emacs to remove the extra
pointer.  It works symmetrically as well, that is, whenever Python
passes a structure to Emacs, an extra Python reference is generated to
inhibit garbage collection on the Python side.  Emacs garbage collector
detects when the received structure is no longer needed on the Emacs
side, after which Python will be told to remove the extra reference.
For efficiency, those allocation-related messages are delayed, merged
and batched together within the next communication having another
purpose.</p>
<p>Variable <strong>pymacs-trace-transit</strong> may be modified for controlling
how and when the <strong>*Pymacs*</strong> buffer, or parts thereof, get erased.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id44" id="emacs-usual-debugging" name="emacs-usual-debugging">6.3&nbsp;&nbsp;&nbsp;Emacs usual debugging</a></h2>
<p>If cross-calls between Emacs Lisp and Python nest deeply, an error will
raise successive exceptions alternatively on both sides as requests
unstack, and the diagnostic gets transmitted back and forth, slightly
growing as we go.  So, errors will eventually be reported by Emacs.  I
made no kind of effort to transmit the Emacs Lisp back trace on the
Python side, as I do not see a purpose for it: all debugging is done
within Emacs windows anyway.</p>
<p>On recent Emacses, the Python back trace gets displayed in the
mini-buffer, and the Emacs Lisp back trace is simultaneously shown
in the <strong>*Backtrace*</strong> window.  One useful thing is to allow to
mini-buffer to grow big, so it has more chance to fully contain the
Python back trace, the last lines of which are often especially useful.
Here, I use:</p>
<pre class="literal-block">
(setq resize-mini-windows t
      max-mini-window-height .85)
</pre>
<p>in my <tt class="file docutils literal"><span class="pre">.emacs</span></tt> file, so the mini-buffer may use 85% of the screen,
and quickly shrinks when fewer lines are needed.  The mini-buffer
contents disappear at the next keystroke, but you can recover the Python
back trace by looking at the end of the <strong>*Messages*</strong> buffer.  In
which case the <strong>ffap</strong> package in Emacs may be yet another friend!
From the <strong>*Messages*</strong> buffer, once <strong>ffap</strong> activated, merely
put the cursor on the file name of a Python module from the back trace,
and <tt class="docutils literal"><span class="pre">C-x</span> <span class="pre">C-f</span> <span class="pre">RET</span></tt> will quickly open that source for you.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id45" id="auto-reloading-on-save" name="auto-reloading-on-save">6.4&nbsp;&nbsp;&nbsp;Auto-reloading on save</a></h2>
<p>I found useful to automatically <strong>pymacs-load</strong> some Python files
whenever they get saved from Emacs.  This can be decided on a per-file
or per-directory basis.  To get a particular Python file to be reloaded
automatically on save, add the following lines at the end:</p>
<pre class="literal-block">
# Local Variables:
# pymacs-auto-reload: t
# End:
</pre>
<p>Here is an example of automatic reloading on a per-directory basis.
The code below assumes that Python files meant for Pymacs are kept in
<tt class="file docutils literal"><span class="pre">~/share/emacs/python</span></tt>:</p>
<pre class="literal-block">
(defun fp-maybe-pymacs-reload ()
  (let ((pymacsdir (expand-file-name &quot;~/share/emacs/python/&quot;)))
    (when (and (string-equal (file-name-directory buffer-file-name)
                             pymacsdir)
               (string-match &quot;\\.py\\'&quot; buffer-file-name))
      (pymacs-load (substring buffer-file-name 0 -3)))))
(add-hook 'after-save-hook 'fp-maybe-pymacs-reload)
</pre>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id46" id="debugging-the-pymacs-helper" name="debugging-the-pymacs-helper">6.5&nbsp;&nbsp;&nbsp;Debugging the Pymacs helper</a></h2>
<p>The Pymacs helper is a Python program which accepts options and arguments.
The available options, which are only meant for debugging, are:</p>
<blockquote>
<table class="docutils option-list" frame="void" rules="none">
<col class="option" />
<col class="description" />
<tbody valign="top">
<tr><td class="option-group">
<kbd><span class="option">-d <var>FILE</var></span></kbd></td>
<td>Debug the protocol to FILE</td></tr>
<tr><td class="option-group">
<kbd><span class="option">-s <var>FILE</var></span></kbd></td>
<td>Trace received signals to FILE</td></tr>
</tbody>
</table>
</blockquote>
<ul class="simple">
<li>The <tt class="docutils literal"><span class="pre">-d</span></tt> option saves a copy of the communication protocol in the
given file, as seen from the Pymacs helper.  The file should be fairly
identical to the contents of the <strong>*Pymacs*</strong> buffer within Emacs.</li>
<li>The <tt class="docutils literal"><span class="pre">-s</span></tt> option monitors most signals received by the Pymacs helper
and logs them in the given file.  Each log line merely contains a signal
number, possibly followed by a star if the interruption was allowed in.
Besides logging, signals are usually ignored.</li>
</ul>
<p>The arguments list directories to be added at the beginning of the
Python module search path, and whenever Emacs launches the Pymacs
helper, the contents of the Emacs Lisp <strong>pymacs-load-path</strong> variable
is turned into this argument list.</p>
<p>The Pymacs helper options may be set through the <strong>PYMACS_OPTIONS</strong>
environment variable.  For example, one could execute something like:</p>
<pre class="literal-block">
export PYMACS_OPTIONS='-d /tmp/pymacs-debug -s /tmp/pymacs-signals'
</pre>
<p>in a shell (presuming <strong>bash</strong> here) and start Emacs from that shell.
Then, when Emacs will launch the Pymacs helper, the above options will
be obeyed.</p>
</div>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id47" id="about-and-around-pymacs" name="about-and-around-pymacs">7&nbsp;&nbsp;&nbsp;About and around Pymacs</a></h1>
<div class="section">
<h2><a class="toc-backref" href="#id48" id="known-limitations" name="known-limitations">7.1&nbsp;&nbsp;&nbsp;Known limitations</a></h2>
<p>Memory may leak in some theoretical circumstances (I say theoretical,
because no one ever reported this as being an actual problem).  As
Richard Stallman once put it (2002-08):</p>
<blockquote>
<cite>I wonder, though, can this</cite> [memory management] <cite>technique fully handle
cycles that run between Lisp and Python?  Suppose Lisp object A refers
to Python object B, which refers to Lisp object A, and suppose nothing
else refers to either one of them.  Will you succeed in recognizing
these two objects as garbage?</cite></blockquote>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id49" id="history" name="history">7.2&nbsp;&nbsp;&nbsp;History</a></h2>
<p>I once hungered for a Python-extensible editor, so much so that I
pondered the idea of dropping Emacs for other avenues, but found nothing
much convincing.  Moreover, looking at all Lisp extensions I'd made
for myself, and considering all those superb tools written by others,
all of which are now part of my computer life, it would have been a
huge undertaking for me to reprogram these all in Python.  So, when I
began to see that something like Pymacs was possible, I felt strongly
motivated! :-)</p>
<p>Pymacs draws on previous work of Cedric Adjih that enabled
the running of Python as a process separate from Emacs.
See <a class="reference" href="http://www.crepuscule.com/pyemacs/">http://www.crepuscule.com/pyemacs/</a>, or write Cedric at
<a class="reference" href="mailto:adjih-pam&#64;crepuscule.com">mailto:adjih-pam&#64;crepuscule.com</a>.  Cedric presented <strong>pyemacs</strong> to me
as a proof of concept.  As I simplified that concept a bit, I dropped
the <tt class="docutils literal"><span class="pre">e</span></tt> in <tt class="docutils literal"><span class="pre">pyemacs</span></tt> :-). Cedric also previously wrote patches for
linking Python right into XEmacs, but abandoned the idea, as he found
out that his patches were unmaintainable over the evolution of both
Python and XEmacs.</p>
<p>As Brian McErlean independently and simultaneously wrote a tool
similar to this one, we decided to merge our projects.  In an amusing
coincidence, he even chose <strong>pymacs</strong> as a name.  Brian paid
good attention to complex details that escaped my courage, so his
help and collaboration have been beneficial.  You may reach Brian at
<a class="reference" href="mailto:brianmce&#64;crosswinds.net">mailto:brianmce&#64;crosswinds.net</a>.</p>
<p>The initial throw at Pymacs has been written on 2001-09-05, and releases
in the 0.x series followed in a rapid pace for a few months, and Pymacs
became stable.  Since then, it did not need to move much, as bugs are
not found often.  Yet, in my opinion, some missing features should be
addressed before we dare some 1.0 release.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id50" id="pymacs-and-me" name="pymacs-and-me">7.3&nbsp;&nbsp;&nbsp;Pymacs and me!</a></h2>
<p>Pymacs has been fairly stable since the early versions.  I surely used
it a great deal, constantly, magically, in my daily works, to the point
of forgetting that was it there all the time.  It was fairly complete,
at least for my own needs, and did not move much anymore.</p>
<p>Some time later, someone begged me to consider Vim, and not only Emacs,
for some tools I was then writing.  Looking at Vim more closely, I
discovered that it is a worth editor, with Python nicely integrated,
enough for me to switch.  In a <a class="reference" href="http://pinard.progiciels-bpi.ca/opinions/editors.html">Web article</a> (which many enjoyed, as
they told me), I detailed my feelings on these matters.</p>
<p>My viewpoint is that Pymacs, maybe after an initial flurry of a bit more
than a dozen releases, soon became stable in its history.  Reported
bugs or suggestions were minor, there was not enough new material to
warrant other releases.  Nevertheless, when I switched from Emacs to
Vim in my day-to-day habits, I felt that Pymacs needed a more credible
maintainer than me.  Syver Enstad, who was an enthusiastic user and
competent contributor, was kind enough to accept the duty (2003-10).
Some more bugs and suggestions flowed in since then, but Syver did not
elect to make any new release, and this did not bother me.  Syver then
became unavailable, to the point I could not contact him in years.  I
would loathe to see myself interfering with an official maintainer,
but when I decided to return to some moderate Emacs usage, and because
of the long silence, I considered resuming Pymacs maintenance as well
(2007-11).  Then, I dived into it for real (2008-01).</p>
<p>Giovanni Giorgi once (2007-03) wanted to expand on Pymacs and publish
it on his own, and later felt like maintaining it whole (late 2007-12).
I rather suggested an attempt at collaborative maintenance, and this
experiment is still going on...</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id51" id="vim-considerations" name="vim-considerations">7.4&nbsp;&nbsp;&nbsp;Vim considerations</a></h2>
<p>Emacs Lisp is deeply soldered into Emacs internals.  Vim has its own
language, which people sometimes call Vimscript, similarly tightened
into Vim.  My feeling is that Emacs Lisp allows for a more intimate
handling of edit buffers and external processes than Vimscript does, yet
this intimacy has a price in complexity, so all totalled, they may be
perceived as comparable for most practical purposes.</p>
<p>Pymacs allows customising Emacs with Python instead of Emacs Lisp, and
then runs Python as a process external to Emacs, with a communication
protocol between both processes.  Python may be built into Vim, and then
both Python and Vim use a single process.  The same as Pymacs gives
access to almost all of Emacs Lisp, Python within Vim gives access to
almost all of Vimscript, but with a much smaller overhead than Pymacs.</p>
<p>Pymacs is not Emacs Lisp, and Python in Vim is not Vimscript either,
tweaks are needed in both cases for accessing some of the underlying
scripting facilities.  Pymacs is rather elegant, Python in Vim is rather
clean.  Python itself is both elegant and clean, but one strong point of
Python for me is the legibility, which builds deeper roots on the clean
side than on the elegant side.  All in all, despite I know how debatable
it can be, I guess I now have a prejudice towards Python in Vim.</p>
<p>I figured out a simple way to have the same Python source usable both
within Pymacs or Vim.  However, Emacs is byte oriented, while Vim is
line oriented.  In a few Pymacs applications of mine, I internally
toggle between line orientation and byte orientation, keeping both for
speed most probably, while I see things would be a bit simpler (and
maybe slower) if I was pushing myself on the line-oriented side.  Each
of Emacs and Vim have their own logic and elegance, and it is probable
that we loose overall if we try to emulate one with the other.</p>
<p>The idea traversed me to convert all the few Pymacs examples so they
work both for Pymacs and Vim, and through the documentation, publicise
how people writing Python extensions could write them for both editors
at once.  Yet, while doing so, one has to stretch either towards Emacs
or Vim, and I guess I would favour Vim over Emacs when the time comes to
evaluate efficiency-related choices.</p>
<p>I also thought about writing a Pymacs module for running Python scripts
already written for Vim, by offering a compatibility layer.  The
complexity of this might be unbounded, I should study actual Python
scripts for Vim before knowing better if this is thinkable or not.</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id52" id="inclusion-within-emacs" name="inclusion-within-emacs">7.5&nbsp;&nbsp;&nbsp;Inclusion within Emacs</a></h2>
<p>Gerd Möllman, who was maintaining Emacs at the time of Pymacs birth and
development, retrofitted (2001-09) the idea of a <strong>post-gc-hook</strong>
from XEmacs, as a way to facilitate memory management within Pymacs.</p>
<p>Richard Stallman once suggested (2001-10) that Pymacs be distributed
within Emacs, and while discussing the details of this, I underlined
small technical difficulties about Emacs installing the Python parts,
and the need of a convention about where to install Python files meant
for Pymacs.  As Richard felt, at the time, very overwhelmed with other
duties, no decision was taken and the integration went nowhere.</p>
<p>After Gerd resigned as an Emacs maintainer, someone from the Emacs
development team wrote again (2002-01) asking information about how
to integrate Pymacs.  It was easy for me to write a good and thorough
summary, after all these discussions with Richard.  And that's the end
of the story: I never heard of it again. :-)</p>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id53" id="speed-issues" name="speed-issues">7.6&nbsp;&nbsp;&nbsp;Speed issues</a></h2>
<p>Doug Bagley's shoot out project compares the relative speed of many
popular languages, and this might interest Pymacs users.  The first URL
points to the original, the second points to a newer version oriented
towards Win32 systems, the third is more recent but Debian-oriented:</p>
<blockquote>
<ul class="simple">
<li><a class="reference" href="http://www.bagley.org/~doug/shootout/">http://www.bagley.org/~doug/shootout/</a></li>
<li><a class="reference" href="http://dada.perl.it/shootout/index.html">http://dada.perl.it/shootout/index.html</a></li>
<li><a class="reference" href="http://shootout.alioth.debian.org/">http://shootout.alioth.debian.org/</a></li>
</ul>
</blockquote>
<p>I've not heard of any Python to Lisp compiler.  Lisp may be slow or fast
depending on how one uses it, and how much one uses declarations.  Some
Lisp systems have really excellent compilers, that give very fast code
when properly hinted.</p>
<p>Python itself may be slow or fast, once again depending on how one
uses it.  With the proper bend, one can develop the habit of writing
Python which shows honest speed.  And there is always Pyrex, which is
Python complemented with explicit declarations (a bit like some Lisp
implementations), and which can buy a lot of speed.</p>
<p>This is quite likely that one can have fast programs while using Python,
or a mix of Python and Pyrex (or even Psyco sometimes), that is, within
Python paradigms, without feeling any need of resorting to Lisp.</p>
<p>If Python looks like being slow while being used with Emacs, the problem
probably lies in Emacs-Python communication which Pymacs implements.
One has to learn how to do the proper compromises for having less
communications.  (In that regard, Vim and Python are really linked
together, so Python in Vim is likely faster than Pymacs for someone who
does not pay special attention to such matters.)</p>
<p>Ali Gholami Rudi also writes (2008-02):</p>
<blockquote>
<p><cite>Well, there seems to be lots of overhead when transferring large
strings.  Transferring them requires:</cite></p>
<blockquote>
<ol class="arabic simple">
<li><cite>escaping characters in the strings</cite></li>
<li><cite>putting them in</cite> <strong>*Pymacs*</strong> <cite>buffer</cite></li>
<li><cite>sending the region to Python process</cite></li>
<li><cite>evaluating the Python string in Python-side (involves compiling)</cite></li>
</ol>
</blockquote>
<p><cite>In my experiments, transferring a ~5k-line file takes more than a
second on a relatively new computer (data from</cite> <strong>rope-dev</strong><cite>).
Improving that probably requires a new protocol that does not
use Python eval and has an optional debug buffer.  Probably few
applications need to transfer large strings to Python but if they do,
it is quite slow.</cite></p>
</blockquote>
<p>All in all, speed may sometimes become a real issue for Pymacs.  I once
wrote within <a class="reference" href="http://pinard.progiciels-bpi.ca/opinions/editors.html">http://pinard.progiciels-bpi.ca/opinions/editors.html</a> :</p>
<blockquote>
<cite>While Pymacs is elegant in my opinion, one cannot effectively use
Pymacs (the Python part) without knowing at least the specification
of many Lisp functions, and I found that it requires some doing for a
Pymacs developer to decouple the Emacs interaction part from the purer
algorithmic part in applications.  Moreover, if you do not consider
speed issues, they bite you.</cite></blockquote>
</div>
<div class="section">
<h2><a class="toc-backref" href="#id54" id="the-future-of-pymacs" name="the-future-of-pymacs">7.7&nbsp;&nbsp;&nbsp;The future of Pymacs</a></h2>
<p>Some people suggested important internal Pymacs changes.  In my opinion,
new bigger features are better implemented in a careful way, first as
examples or contributions, and moved closer to internal integration
depending on how users use or appreciate them.  For now, Pymacs should
concentrate at doing its own humble job well, and resist bloat.</p>
<p>Before Pymacs closes to some version 1.0, some specifications should be
revisited, user suggestions pondered, porting matters documented.  The
test suite should grow up, we should collect more examples.  Pymacs
should aim seamless integration with <tt class="file docutils literal"><span class="pre">.el</span></tt> files and with
transparent <strong>autoload</strong> (my little tries were not so successful).
On the Python side, Pymacs <em>might</em> fake primitives like <strong>getindex</strong>
and <strong>putindex</strong>, better support iterators and some newer Python
features, and at least consider Python 3.0.</p>
<p>Pymacs is not much geared towards Python threads.  It is not clear yet if
it would be reasonably tractable to better support them.</p>
</div>
</div>
</div>
</body>
</html>