emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob c9cca4ad1fa97d90146599d8c6c1bbab7c656359 69476 bytes (raw)
name: EXPERIMENTAL/org-e-ascii.el 	 # note: path name is non-authoritative(*)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
 
;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine

;; Copyright (C) 2012  Free Software Foundation, Inc.

;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
;; Keywords: outlines, hypermedia, calendar, wp

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This library implements an ASCII back-end for Org generic exporter.

;; To test it, run
;;
;;   M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
;;
;; in an Org mode buffer then switch to that buffer to see the ASCII
;; export.  See contrib/lisp/org-export.el for more details on how
;; this exporter works.

;;; Code:

(eval-when-compile (require 'cl))

(declare-function org-element-contents "org-element" (element))
(declare-function org-element-property "org-element" (property element))
(declare-function org-element-normalize-string "org-element" (s))
(declare-function org-element-map "org-element"
		  (data types fun &optional info first-match))
(declare-function org-element-time-stamp-interpreter
		  "org-element" (time-stamp contents))

(declare-function org-export-clean-table "org-export" (table specialp))
(declare-function org-export-collect-footnote-definitions
		  "org-export" (data info))
(declare-function org-export-collect-headlines "org-export" (info &optional n))
(declare-function org-export-collect-listings "org-export" (info))
(declare-function org-export-collect-tables "org-export" (info))
(declare-function org-export-data "org-export" (data backend info))
(declare-function org-export-expand-macro "org-export" (macro info))
(declare-function org-export-get-coderef-format "org-export" (path desc))
(declare-function org-export-get-footnote-number "org-export" (footnote info))
(declare-function org-export-get-headline-number "org-export" (headline info))
(declare-function org-export-get-ordinal "org-export"
		  (element info &optional types within-section predicate))
(declare-function org-export-get-parent-headline "org-export" (blob info))
(declare-function org-export-get-relative-level "org-export" (headline info))
(declare-function org-export-handle-code
		  "org-export" (element info &optional num-fmt ref-fmt delayed))
(declare-function org-export-included-file "org-export" (keyword backend info))
(declare-function org-export-low-level-p "org-export" (headline info))
(declare-function org-export-output-file-name "org-export"
		  (extension &optional subtreep pub-dir))
(declare-function org-export-resolve-coderef "org-export" (ref info))
(declare-function org-export-resolve-fuzzy-link "org-export" (link info))
(declare-function org-export-resolve-id-link "org-export" (link info))
(declare-function org-export-resolve-ref-link "org-export" (link info))
(declare-function org-export-secondary-string
		  "org-export" (secondary backend info))
(declare-function org-export-table-format-info "org-export" (table))
(declare-function
 org-export-to-file "org-export"
 (backend file &optional subtreep visible-only body-only ext-plist))


\f
;;; Internal Variables

;; The following setting won't allow to modify preferred charset
;; through a buffer keyword or an option item, but, since the property
;; will appear in communication channel nonetheless, it allows to
;; override `org-e-ascii-charset' variable on the fly by the ext-plist
;; mechanism.

;; We also install a filter for headlines and sections, in order to
;; control blank lines separating them in output string.

(defconst org-e-ascii-option-alist
  '((:ascii-charset nil nil org-e-ascii-charset)
    )
  "Alist between ASCII export properties and ways to set them.
See `org-export-option-alist' for more information on the
structure or the values.")

(defconst org-e-ascii-filters-alist
  '((:filter-headline . org-e-ascii-filter-headline-blank-lines)
    (:filter-section . org-e-ascii-filter-headline-blank-lines))
  "Alist between filters keywords and back-end specific filters.
See `org-export-filters-alist' for more information.")

(defconst org-e-ascii-dictionary
  '(("Footnotes\n"
     ("en"
      :ascii "Footnotes\n"
      :latin1 "Footnotes\n"
      :utf-8 "Footnotes\n")
     ("fr"
      :ascii "Notes de bas de page\n"
      :latin1 "Notes de bas de page\n"
      :utf-8 "Notes de bas de page\n"))
    ("Listing %d: %s"
     ("en"
      :ascii "Listing %d: %s"
      :latin1 "Listing %d: %s"
      :utf-8 "Listing %d: %s")
     ("fr"
      :ascii "Programme %d : %s"
      :latin1 "Programme %d : %s"
      :utf-8 "Programme nº %d : %s"))
    ("List Of Listings\n"
     ("en"
      :ascii "List Of Listings\n"
      :latin1 "List Of Listings\n"
      :utf-8 "List Of Listings\n")
     ("fr"
      :ascii "Liste des programmes\n"
      :latin1 "Liste des programmes\n"
      :utf-8 "Liste des programmes\n"))
    ("List Of Tables\n"
     ("en"
      :ascii "List Of Tables\n"
      :latin1 "List Of Tables\n"
      :utf-8 "List Of Tables\n")
     ("fr"
      :ascii "Liste des tableaux\n"
      :latin1 "Liste des tableaux\n"
      :utf-8 "Liste des tableaux\n"))
    ("Listing %d: "
     ("en"
      :ascii "Listing %d: "
      :latin1 "Listing %d: "
      :utf-8 "Listing %d: ")
     ("fr"
      :ascii "Programme %d : "
      :latin1 "Programme %d : "
      :utf-8 "Programme nº %d : "))
    ("Table Of Contents\n"
     ("en"
      :ascii "Table Of Contents\n"
      :latin1 "Table Of Contents\n"
      :utf-8 "Table Of Contents\n")
     ("fr"
      :ascii "Sommaire\n"
      :latin1 "Table des matières\n"
      :utf-8 "Table des matières\n"))
    ("Table %d: %s"
     ("en"
      :ascii "Table %d: %s"
      :latin1 "Table %d: %s"
      :utf-8 "Table %d: %s")
     ("fr"
      :ascii "Tableau %d : %s"
      :latin1 "Tableau %d : %s"
      :utf-8 "Tableau nº %d : %s"))
    ("See section %s"
     ("en"
      :ascii "See section %s"
      :latin1 "See section %s"
      :utf-8 "See section %s")
     ("fr"
      :ascii "cf. section %s"
      :latin1 "cf. section %s"
      :utf-8 "cf. section %s"))
    ("Table %d: "
     ("en"
      :ascii "Table %d: "
      :latin1 "Table %d: "
      :utf-8 "Table %d: ")
     ("fr"
      :ascii "Tableau %d : "
      :latin1 "Tableau %d : "
      :utf-8 "Tableau nº %d : "))
    ("Unknown reference"
     ("en"
      :ascii "Unknown reference"
      :latin1 "Unknown reference"
      :utf-8 "Unknown reference")
     ("fr"
      :ascii "Destination inconnue"
      :latin1 "Référence inconnue"
      :utf-8 "Référence inconnue")))
  "Dictionary for ASCII back-end.

Alist whose car is the string to translate and cdr is an alist
whose car is the language string and cdr is a plist whose
properties are possible charsets and value the translated term.

It is used as a database for `org-e-ascii--translate'.")


\f
;;; User Configurable Variables

(defgroup org-export-e-ascii nil
  "Options for exporting Org mode files to ASCII."
  :tag "Org Export ASCII"
  :group 'org-export)

(defcustom org-e-ascii-text-width 72
  "Maximum width of exported text.
This number includes margin size, as set in
`org-e-ascii-global-margin'."
  :group 'org-export-e-ascii
  :type 'integer)

(defcustom org-e-ascii-global-margin 0
  "Width of the left margin, in number of characters."
  :group 'org-export-e-ascii
  :type 'integer)

(defcustom org-e-ascii-inner-margin 2
  "Width of the inner margin, in number of characters.
Inner margin is applied between each headline."
  :group 'org-export-e-ascii
  :type 'integer)

(defcustom org-e-ascii-quote-margin 6
  "Width of margin used for quoting text, in characters.
This margin is applied on both sides of the text."
  :group 'org-export-e-ascii
  :type 'integer)

(defcustom org-e-ascii-inlinetask-width 30
  "Width of inline tasks, in number of characters.
This number ignores any margin."
  :group 'org-export-e-ascii
  :type 'integer)

(defcustom org-e-ascii-headline-spacing '(1 . 2)
  "Number of blank lines inserted around headlines.

This variable can be set to a cons cell.  In that case, its car
represents the number of blank lines present before headline
contents whereas its cdr reflects the number of blank lines after
contents.

A nil value replicates the number of blank lines found in the
original Org buffer at the same place."
  :group 'org-export-e-ascii
  :type '(choice
	  (const :tag "Replicate original spacing" nil)
	  (cons :tag "Set an uniform spacing"
		(integer :tag "Number of blank lines before contents")
		(integer :tag "Number of blank lines after contents"))))

(defcustom org-e-ascii-charset 'ascii
  "The charset allowed to represent various elements and objects.
Possible values are:
`ascii'    Only use plain ASCII characters
`latin1'   Include Latin-1 characters
`utf-8'    Use all UTF-8 characters"
  :group 'org-export-e-ascii
  :type '(choice
	  (const :tag "ASCII" ascii)
	  (const :tag "Latin-1" latin1)
	  (const :tag "UTF-8" utf-8)))

(defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
				   (latin1 ?= ?~ ?-)
				   (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  "Characters for underlining headings in ASCII export.

Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
and whose value is a list of characters.

For each supported charset, this variable associates a sequence
of underline characters.  In a sequence, the characters will be
used in order for headlines level 1, 2, ...  If no character is
available for a given level, the headline won't be underlined."
  :group 'org-export-e-ascii
  :type '(list
	  (cons :tag "Underline characters sequence"
		(const :tag "ASCII charset" ascii)
		(repeat character))
	  (cons :tag "Underline characters sequence"
		(const :tag "Latin-1 charset" latin1)
		(repeat character))
	  (cons :tag "Underline characters sequence"
		(const :tag "UTF-8 charset" utf-8)
		(repeat character))))

(defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
				 (latin1 ?§ ?¶)
				 (utf-8 ?◊))
  "Bullet characters for headlines converted to lists in ASCII export.

Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
and whose value is a list of characters.

The first character is used for the first level considered as low
level, and so on.  If there are more levels than characters given
here, the list will be repeated.

Note that this variable doesn't affect plain lists
representation."
  :group 'org-export-e-ascii
  :type '(list
	  (cons :tag "Bullet characters for low level headlines"
		(const :tag "ASCII charset" ascii)
		(repeat character))
	  (cons :tag "Bullet characters for low level headlines"
		(const :tag "Latin-1 charset" latin1)
		(repeat character))
	  (cons :tag "Bullet characters for low level headlines"
		(const :tag "UTF-8 charset" utf-8)
		(repeat character))))

(defcustom org-e-ascii-links-to-notes t
  "Non-nil means convert links to notes before the next headline.
When nil, the link will be exported in place.  If the line
becomes long in this way, it will be wrapped."
  :group 'org-export-e-ascii
  :type 'boolean)

(defcustom org-e-ascii-table-keep-all-vertical-lines nil
  "Non-nil means keep all vertical lines in ASCII tables.
When nil, vertical lines will be removed except for those needed
for column grouping."
  :group 'org-export-e-ascii
  :type 'boolean)

(defcustom org-e-ascii-table-widen-columns t
  "Non-nil means widen narrowed columns for export.
When nil, narrowed columns will look in ASCII export just like in
Org mode, i.e. with \"=>\" as ellipsis."
  :group 'org-export-e-ascii
  :type 'boolean)

(defcustom org-e-ascii-caption-above nil
  "When non-nil, place caption string before the element.
Otherwise, place it right after it."
  :group 'org-export-e-ascii
  :type 'boolean)

(defcustom org-e-ascii-verbatim-format "`%s'"
  "Format string used for verbatim text and inline code."
  :group 'org-export-e-ascii
  :type 'string)

(defcustom org-e-ascii-format-drawer-function nil
  "Function called to format a drawer in ASCII.

The function must accept two parameters:
  NAME      the drawer name, like \"LOGBOOK\"
  CONTENTS  the contents of the drawer.
  WIDTH     the text width within the drawer.

The function should return either the string to be exported or
nil to ignore the drawer.

For example, the variable could be set to the following function
in order to mimic default behaviour:

\(defun org-e-ascii-format-drawer-default \(name contents width\)
  \"Format a drawer element for ASCII export.\"
  contents\)"
  :group 'org-export-e-ascii
  :type 'function)

(defcustom org-e-ascii-format-inlinetask-function nil
  "Function called to format an inlinetask in ASCII.

The function must accept six parameters:
  TODO      the todo keyword, as a string
  TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  PRIORITY  the inlinetask priority, as a string
  NAME      the inlinetask name, as a string.
  TAGS      the inlinetask tags, as a string.
  CONTENTS  the contents of the inlinetask, as a string.

The function should return either the string to be exported or
nil to ignore the inline task.

For example, the variable could be set to the following function
in order to mimic default behaviour:

\(defun org-e-ascii-format-inlinetask-default
  \(todo type priority name tags contents\)
  \"Format an inline task element for ASCII export.\"
  \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
           \(width org-e-ascii-inlinetask-width\)
    \(org-e-ascii--indent-string
     \(concat
      ;; Top line, with an additional blank line if not in UTF-8.
      \(make-string width \(if utf8p ?━ ?_\)\)  \"\\n\"
      \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
      ;; Add title.  Fill it if wider than inlinetask.
      \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
	\(if \(<= \(length title\) width\) title
	  \(org-e-ascii--fill-string title width info\)\)\)
      \"\\n\"
      ;; If CONTENTS is not empty, insert it along with
      ;; a separator.
      \(when \(org-string-nw-p contents\)
        \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
      ;; Bottom line.
      \(make-string width \(if utf8p ?━ ?_\)\)\)
     ;; Flush the inlinetask to the right.
     \(- \(plist-get info :ascii-width\)
        \(plist-get info :ascii-margin\)
        \(plist-get info :ascii-inner-margin\)
        \(org-e-ascii--current-text-width inlinetask info\)\)"
  :group 'org-export-e-ascii
  :type 'function)


\f
;;; Internal Functions

;; Internal functions fall into three categories.

;; The first one is about text formatting.  The core function is
;; `org-e-ascii--current-text-width', which determines the current
;; text width allowed to a given element.  In other words, it helps
;; keeping each line width within maximum text width defined in
;; `org-e-ascii-text-width'.  Once this information is known,
;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
;; operate on a given output string.

;; The second category contains functions handling elements listings,
;; triggered by "#+TOC:" keyword.  As such, `org-e-ascii--build-toc'
;; returns a complete table of contents, `org-e-ascii--list-listings'
;; returns a list of referenceable src-block elements, and
;; `org-e-ascii--list-tables' does the same for table elements.

;; The third category includes general helper functions.
;; `org-e-ascii--build-title' creates the title for a given headline
;; or inlinetask element.  `org-e-ascii--build-caption' returns the
;; caption string associated to a table or a src-block.
;; `org-e-ascii--describe-links' creates notes about links for
;; insertion at the end of a section.  It uses
;; `org-e-ascii--unique-links' to get the list of links to describe.
;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
;; to internationalize output.


(defun org-e-ascii--fill-string (s text-width info &optional justify)
  "Fill a string with specified text-width and return it.

S is the string being filled.  TEXT-WIDTH is an integer
specifying maximum length of a line.  INFO is the plist used as
a communication channel.

Optional argument JUSTIFY can specify any type of justification
among `left', `center', `right' or `full'.  A nil value is
equivalent to `left'.  For a justification that doesn't also fill
string, see `org-e-ascii--justify-string'.

Return nil if S isn't a string."
  ;; Don't fill paragraph when break should be preserved.
  (cond ((not (stringp s)) nil)
	((plist-get info :preserve-breaks) s)
	(t (with-temp-buffer
	     (let ((fill-column text-width)
		   (use-hard-newlines t))
	       (insert s)
	       (fill-region (point-min) (point-max) justify))
	     (buffer-string)))))

(defun org-e-ascii--justify-string (s text-width how)
  "Justify string S.
TEXT-WIDTH is an integer specifying maximum length of a line.
HOW determines the type of justification: it can be `left',
`right', `full' or `center'."
  (with-temp-buffer
    (insert s)
    (goto-char (point-min))
    (let ((fill-column text-width))
      (while (< (point) (point-max))
	(justify-current-line how)
	(forward-line)))
    (buffer-string)))

(defun org-e-ascii--indent-string (s width)
  "Indent string S by WIDTH white spaces.
Empty lines are not indented."
  (when (stringp s)
    (replace-regexp-in-string
     "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))

(defun org-e-ascii--box-string (s info)
  "Return string S with a partial box to its left.
INFO is a plist used as a communicaton channel."
  (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
    (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
	    (replace-regexp-in-string
	     "^" (if utf8p "│ " "| ")
	     ;; Remove last newline character.
	     (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))

(defun org-e-ascii--current-text-width (element info)
  "Return maximum text width for ELEMENT's contents.
INFO is a plist used as a communication channel."
  (case (org-element-type element)
    ;; Elements with an absolute width: `headline' and `inlinetask'.
    (inlinetask org-e-ascii-inlinetask-width)
    ('headline
     (- org-e-ascii-text-width
	(let ((low-level-rank (org-export-low-level-p element info)))
	  (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
    ;; Elements with a relative width: store maximum text width in
    ;; TOTAL-WIDTH.
    (otherwise
     (let* ((genealogy (cons element (org-export-get-genealogy element info)))
	    ;; Total width is determined by the presence, or not, of an
	    ;; inline task among ELEMENT parents.
	    (total-width
	     (if (loop for parent in genealogy
		       thereis (eq (org-element-type parent) 'inlinetask))
		 org-e-ascii-inlinetask-width
	       ;; No inlinetask: Remove global margin from text width.
	       (- org-e-ascii-text-width
		  org-e-ascii-global-margin
		  (let ((parent (org-export-get-parent-headline element info)))
		    ;; Inner margin doesn't apply to text before first
		    ;; headline.
		    (if (not parent) 0
		      (let ((low-level-rank
			     (org-export-low-level-p parent info)))
			;; Inner margin doesn't apply to contents of
			;; low level headlines, since they've got their
			;; own indentation mechanism.
			(if low-level-rank (* low-level-rank 2)
			  org-e-ascii-inner-margin))))))))
       (- total-width
	  ;; Each `quote-block', `quote-section' and `verse-block' above
	  ;; narrows text width by twice the standard margin size.
	  (+ (* (loop for parent in genealogy
		      when (memq (org-element-type parent)
				 '(quote-block quote-section verse-block))
		      count parent)
		2 org-e-ascii-quote-margin)
	     ;; Text width within a plain-list is restricted by
	     ;; indentation of current item.  If that's the case,
	     ;; compute it with the help of `:structure' property from
	     ;; parent item, if any.
	     (let ((parent-item
		    (if (eq (org-element-type element) 'item) element
		      (loop for parent in genealogy
			    when (eq (org-element-type parent) 'item)
			    return parent))))
	       (if (not parent-item) 0
		 ;; Compute indentation offset of the current item,
		 ;; that is the sum of the difference between its
		 ;; indentation and the indentation of the top item in
		 ;; the list and current item bullet's length.  Also
		 ;; remove tag length (for description lists) or bullet
		 ;; length.
		 (let ((struct (org-element-property :structure parent-item))
		       (beg-item (org-element-property :begin parent-item)))
		   (+ (- (org-list-get-ind beg-item struct)
			 (org-list-get-ind
			  (org-list-get-top-point struct) struct))
		      (length
		       (or (org-list-get-tag beg-item struct)
			   (org-list-get-bullet beg-item struct)))))))))))))

(defun org-e-ascii--build-title
  (element info text-width &optional underline notags)
  "Format ELEMENT title and return it.

ELEMENT is either an `headline' or `inlinetask' element.  INFO is
a plist used as a communication channel.  TEXT-WIDTH is an
integer representing the maximum length of a line.

When optional argument UNDERLINE is non-nil, underline title,
without the tags, according to `org-e-ascii-underline'
specifications.

if optional argument NOTAGS is nil, no tags will be added to the
title."
  (let* ((headlinep (eq (org-element-type element) 'headline))
	 (numbers
	  ;; Numbering is specific to headlines.
	  (and headlinep
	       ;; Section numbering must be active, and headline's
	       ;; level should be above specified limit, if any.
	       (let ((sec-num (plist-get info :section-numbers)))
		 (if (not (wholenump sec-num)) sec-num
		   (<= (org-export-get-relative-level headline info) sec-num)))
	       ;; All tests passed: build numbering string.
	       (concat
		(mapconcat
		 #'number-to-string
		 (org-export-get-headline-number element info) ".")
		" ")))
	 (text (org-export-secondary-string
		(org-element-property :title element) 'e-ascii info))
	 (todo
	  (and (plist-get info :with-todo-keywords)
	       (let ((todo (org-element-property :todo-keyword element)))
		 (and todo
		      (concat (org-export-secondary-string todo 'e-ascii info)
			      " ")))))
	 (tags (and (not notags)
		    (plist-get info :with-tags)
		    (org-element-property :tags element)))
	 (priority
	  (and (plist-get info :with-priority)
	       (concat (org-element-property :priority element) " ")))
	 (first-part (concat numbers todo priority text)))
    (concat
     first-part
     ;; Align tags, if any.
     (when tags
       (format
	(format " %%%ds"
		(max (- text-width  (1+ (length first-part))) (length tags)))
	tags))
     ;; Maybe underline text, if ELEMENT type is `headline' and an
     ;; underline character has been defined.
     (when (and underline headlinep)
       (let ((under-char
	      (nth (1- (org-export-get-relative-level element info))
		   (cdr (assq (plist-get info :ascii-charset)
			      org-e-ascii-underline)))))
	 (and under-char
	      (concat "\n"
		      (make-string (length first-part) under-char))))))))

(defun org-e-ascii--build-caption (element info)
  "Return caption string for ELEMENT, if applicable.

INFO is a plist used as a communication channel.

The caption string contains the sequence number of ELEMENT if it
has a name affiliated keyword, along with the real caption, if
any.  Return nil when ELEMENT has no affiliated caption or name
keyword."
  (let ((caption (org-element-property :caption element))
	(name (org-element-property :name element)))
    (when (or caption name)
      ;; Get sequence number of current src-block among every
      ;; src-block with either a caption or a name.
      (let ((reference
	     (org-export-get-ordinal
	      element info nil nil
	      (lambda (el) (or (org-element-property :caption el)
			  (org-element-property :name el)))))
	    (title-fmt (org-e-ascii--translate
			(case (org-element-type element)
			  (table "Table %d: %s")
			  (src-block "Listing %d: %s")) info)))
	(org-e-ascii--fill-string
	 (format
	  title-fmt reference
	  (if (not caption) name
	    (org-export-secondary-string (car caption) 'e-ascii info)))
	 (org-e-ascii--current-text-width element info) info)))))

(defun org-e-ascii--build-toc (info &optional n keyword)
  "Return a table of contents.

INFO is a plist used as a communication channel.

Optional argument N, when non-nil, is an integer specifying the
depth of the table.

Optional argument KEYWORD specifies the TOC keyword, if any, from
which the table of contents generation has been initiated."
  (let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
    (concat
     title
     (make-string (1- (length title))
		  (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
     "\n\n"
     (let ((text-width
	    (if keyword (org-e-ascii--current-text-width keyword info)
	      (- org-e-ascii-text-width org-e-ascii-global-margin))))
       (mapconcat
	(lambda (headline)
	  (let* ((level (org-export-get-relative-level headline info))
		 (indent (* (1- level) 3)))
	    (concat
	     (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
	     (org-e-ascii--build-title
	      headline info (- text-width indent) nil
	      (eq (plist-get info :with-tags) 'not-in-toc)))))
	(org-export-collect-headlines info n) "\n")))))

(defun org-e-ascii--list-listings (keyword info)
  "Return a list of listings.

KEYWORD is the keyword that initiated the list of listings
generation.  INFO is a plist used as a communication channel."
  (let ((title (org-e-ascii--translate "List Of Listings\n" info)))
    (concat
     title
     (make-string (1- (length title))
		  (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
     "\n\n"
     (let ((text-width
	    (if keyword (org-e-ascii--current-text-width keyword info)
	      (- org-e-ascii-text-width org-e-ascii-global-margin)))
	   ;; Use a counter instead of retreiving ordinal of each
	   ;; src-block.
	   (count 0))
       (mapconcat
	(lambda (src-block)
	  ;; Store initial text so its length can be computed.  This is
	  ;; used to properly align caption right to it in case of
	  ;; filling (like contents of a description list item).
	  (let ((initial-text
		 (format (org-e-ascii--translate "Listing %d: " info)
			 (incf count))))
	    (concat
	     initial-text
	     (org-trim
	      (org-e-ascii--indent-string
	       (org-e-ascii--fill-string
		(let ((caption (org-element-property :caption src-block)))
		  (if (not caption) (org-element-property :name src-block)
		    (org-export-secondary-string
		     ;; Use short name in priority, if available.
		     (or (cdr caption) (car caption)) 'e-ascii info)))
		(- text-width (length initial-text)) info)
	       (length initial-text))))))
	(org-export-collect-listings info) "\n")))))

(defun org-e-ascii--list-tables (keyword info)
  "Return a list of listings.

KEYWORD is the keyword that initiated the list of listings
generation.  INFO is a plist used as a communication channel."
  (let ((title (org-e-ascii--translate "List Of Tables\n" info)))
    (concat
     title
     (make-string (1- (length title))
		  (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
     "\n\n"
     (let ((text-width
	    (if keyword (org-e-ascii--current-text-width keyword info)
	      (- org-e-ascii-text-width org-e-ascii-global-margin)))
	   ;; Use a counter instead of retreiving ordinal of each
	   ;; src-block.
	   (count 0))
       (mapconcat
	(lambda (table)
	  ;; Store initial text so its length can be computed.  This is
	  ;; used to properly align caption right to it in case of
	  ;; filling (like contents of a description list item).
	  (let ((initial-text
		 (format (org-e-ascii--translate "Table %d: " info)
			 (incf count))))
	    (concat
	     initial-text
	     (org-trim
	      (org-e-ascii--indent-string
	       (org-e-ascii--fill-string
		(let ((caption (org-element-property :caption table)))
		  (if (not caption) (org-element-property :name table)
		    ;; Use short name in priority, if available.
		    (org-export-secondary-string
		     (or (cdr caption) (car caption)) 'e-ascii info)))
		(- text-width (length initial-text)) info)
	       (length initial-text))))))
	(org-export-collect-tables info) "\n")))))

(defun org-e-ascii--unique-links (element info)
  "Return a list of unique link references in ELEMENT.

ELEMENT is either an headline element or a section element.  INFO
is a plist used as a communication channel.

It covers links that may be found current headline's title, in
the following section and in any inlinetask's title there."
  (let* (seen
	 (unique-link-p
	  (function
	   ;; Return LINK if it wasn't referenced so far, or nil.
	   ;; Update SEEN links along the way.
	   (lambda (link)
	     (let ((footprint
		    (cons (org-element-property :raw-link link)
			  (org-element-contents link))))
	       (unless (member footprint seen)
		 (push footprint seen) link)))))
	 (harvest-links-in-title
	  (function
	   ;; Return a list of all unique links in ELEMENT.  ELEMENT
	   ;; may be an headline or an inlinetask element.
	   (lambda (element)
	     (let (acc)
	       (dolist (obj (org-element-property :title element) acc)
		 (when (eq (org-element-type obj) 'link)
		   (let ((link (funcall unique-link-p obj)))
		     (and link (push link acc)))))))))
	 ;; Retrieve HEADLINE's section, if it exists.
	 (section (if (eq (org-element-type element) 'section) element
		    (let ((sec (car (org-element-contents element))))
		      (and (eq (org-element-type sec) 'section) sec))))
	 (headline (if (eq (org-element-type element) 'headline) element
		     (org-export-get-parent-headline element info))))
    (append
     ;; Links that may be in HEADLINE's title.
     (funcall harvest-links-in-title headline)
     ;; Get all links in SECTION.
     (org-element-map
      section 'link (lambda (link) (funcall unique-link-p link)) info))))

(defun org-e-ascii--describe-links (links width info)
  "Return a string describing a list of links.

LINKS is a list of link type objects, as returned by
`org-e-ascii--unique-links'.  WIDTH is the text width allowed for
the output string.  INFO is a plist used as a communication
channel."
  (mapconcat
   (lambda (link)
     (let ((type (org-element-property :type link))
	   (anchor (let ((desc (org-element-contents link)))
		     (if (not desc) (org-element-property :raw-link link)
		       (org-export-secondary-string desc 'e-ascii info)))))
       (cond
	;; Coderefs, radio links and fuzzy links are ignored.
	((member type '("coderef" "radio" "fuzzy")) nil)
	;; Id and custom-id links: Headlines refer to their numbering.
	((member type '("custom-id" "id"))
	 (let ((destination (org-export-resolve-id-link link info)))
	   (concat
	    (org-e-ascii--fill-string
	     (format
	      "[%s] %s"
	      anchor
	      (if (not destination)
		  (org-e-ascii--translate "Unknown reference" info)
		(format
		 (org-e-ascii--translate "See section %s" info)
		 (mapconcat 'number-to-string
			    (org-export-get-headline-number destination info)
			    "."))))
	     width info) "\n\n")))
	;; Do not add a link that cannot be resolved and doesn't have
	;; any description: destination is already visible in the
	;; paragraph.
	((not (org-element-contents link)) nil)
	(t
	 (concat
	  (org-e-ascii--fill-string
	   (format "[%s] %s" anchor (org-element-property :raw-link link))
	   width info)
	  "\n\n")))))
   links ""))


\f
;;; Template

(defun org-e-ascii-template--document-title (info)
  "Return document title, as a string.
INFO is a plist used as a communication channel."
  (let ((text-width org-e-ascii-text-width)
	(title (org-export-secondary-string
		(plist-get info :title) 'e-ascii info))
	(author
	 (and (plist-get info :with-author)
	      (let ((auth (plist-get info :author)))
		(and auth (org-export-secondary-string auth 'e-ascii info)))))
	(email
	 (and (plist-get info :with-email)
	      (org-export-secondary-string
	       (plist-get info :email) 'e-ascii info)))
	(date (plist-get info :date)))
    ;; There are two types of title blocks depending on the presence
    ;; of a title to display.
    (if (string= title "")
	;; Title block without a title.  DATE is positioned at the top
	;; right of the document, AUTHOR to the top left and EMAIL
	;; just below.
	(cond
	 ((and (org-string-nw-p date) (org-string-nw-p author))
	  (concat
	   author
	   (make-string (- text-width (length date) (length author)) ? )
	   date
	   (when (org-string-nw-p email) (concat "\n" email))
	   "\n\n\n"))
	 ((and (org-string-nw-p date) (org-string-nw-p email))
	  (concat
	   email
	   (make-string (- text-width (length date) (length email)) ? )
	   date "\n\n\n"))
	 ((org-string-nw-p date)
	  (concat
	   (org-e-ascii--justify-string date text-width 'right)
	   "\n\n\n"))
	 ((and (org-string-nw-p author) (org-string-nw-p email))
	  (concat author "\n" email "\n\n\n"))
	 ((org-string-nw-p author) (concat author "\n\n\n"))
	 ((org-string-nw-p email) (concat email "\n\n\n")))
      ;; Title block with a title.  Document's TITLE, along with the
      ;; AUTHOR and its EMAIL are both overlined and an underlined,
      ;; centered.  Date is just below, also centered.
      (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
	     ;; Format TITLE.  It may be filled if it is too wide,
	     ;; that is wider than the two thirds of the total width.
	     (title-len (min (length title) (/ (* 2 text-width) 3)))
	     (formatted-title (org-e-ascii--fill-string title title-len info))
	     (line
	      (make-string
	       (min (+ (max title-len (length author) (length email)) 2)
		    text-width) (if utf8p ?━ ?_))))
	(org-e-ascii--justify-string
	 (concat line "\n"
		 (unless utf8p "\n")
		 (upcase formatted-title)
		 (cond
		  ((and (org-string-nw-p author) (org-string-nw-p email))
		   (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
		  ((org-string-nw-p author)
		   (concat (if utf8p "\n\n\n" "\n\n") author))
		  ((org-string-nw-p email)
		   (concat (if utf8p "\n\n\n" "\n\n") email)))
		 "\n" line
		 (when (org-string-nw-p date) (concat "\n\n\n" date))
		 "\n\n\n") text-width 'center)))))

(defun org-e-ascii-template (contents info)
  "Return complete document string after ASCII conversion.
CONTENTS is the transcoded contents string.  INFO is a plist
holding export options."
  (org-element-normalize-string
   (org-e-ascii--indent-string
    (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
      ;; 1. Build title block.
      (concat
       (org-e-ascii-template--document-title info)
       ;; 2. Table of contents.
       (let ((depth (plist-get info :with-toc)))
	 (when depth
	   (concat
	    (org-e-ascii--build-toc info (and (wholenump depth) depth))
	    "\n\n\n")))
       ;; 3. Document's body.
       contents
       ;; 4. Footnote definitions.
       (let ((definitions (org-export-collect-footnote-definitions
			   (plist-get info :parse-tree) info))
	     ;; Insert full links right inside the footnote definition
	     ;; as they have no chance to be inserted later.
	     (org-e-ascii-links-to-notes nil))
	 (when definitions
	   (concat
	    "\n\n\n"
	    (let ((title (org-e-ascii--translate "Footnotes\n" info)))
	      (concat
	       title
	       (make-string
		(1- (length title))
		(if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
	    "\n\n"
	    (mapconcat
	     (lambda (ref)
	       (let ((id (format "[%s] " (car ref))))
		 ;; Distinguish between inline definitions and
		 ;; full-fledged definitions.
		 (org-trim
		  (let ((def (nth 2 ref)))
		    (if (eq (org-element-type def) 'org-data)
			;; Full-fledged definition: footnote ID is
			;; inserted inside the first parsed paragraph
			;; (FIRST), if any, to be sure filling will
			;; take it into consideration.
			(let ((first (car (org-element-contents def))))
			  (if (not (eq (org-element-type first) 'paragraph))
			      (concat id "\n" (org-export-data def 'e-ascii info))
			    (push id (nthcdr 2 first))
			    (org-export-data def 'e-ascii info)))
		      ;; Fill paragraph once footnote ID is inserted in
		      ;; order to have a correct length for first line.
		      (org-e-ascii--fill-string
		       (concat id (org-export-secondary-string def 'e-ascii info))
		       text-width info))))))
	     definitions "\n\n"))))
       ;; 5. Creator.  Ignore `comment' value as there are no comments in
       ;;    ASCII.  Justify it to the bottom right.
       (let ((creator-info (plist-get info :with-creator)))
	 (unless (or (not creator-info) (eq creator-info 'comment))
	   (concat
	    "\n\n\n"
	    (org-e-ascii--fill-string
	     (plist-get info :creator) text-width info 'right))))))
    org-e-ascii-global-margin)))

(defun org-e-ascii--translate (s info)
  "Translate string S.

INFO is a plist used as a communication channel.

Translation depends on `:language' property and allowed charset.
If no translation in found for a given language and a given
charset, fall-back to S."
  (let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
	 (lang (plist-get info :language))
	 (translations (cdr (assoc s org-e-ascii-dictionary))))
    (or (plist-get (cdr (assoc lang translations)) charset) s)))


\f
;;; Transcode Functions

;;;; Babel Call

;; Babel Calls are ignored.


;;;; Center Block

(defun org-e-ascii-center-block (center-block contents info)
  "Transcode a CENTER-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  (org-e-ascii--justify-string
   contents (org-e-ascii--current-text-width center-block info) 'center))


;;;; Comment

;; Comments are ignored.


;;;; Comment Block

;; Comment Blocks are ignored.


;;;; Drawer

(defun org-e-ascii-drawer (drawer contents info)
  "Transcode a DRAWER element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  (let ((name (org-element-property :drawer-name drawer))
	(width (org-e-ascii--current-text-width drawer info)))
    (if (functionp org-e-ascii-format-drawer-function)
	(funcall org-e-ascii-format-drawer-function name contents width)
      ;; If there's no user defined function: simply
      ;; display contents of the drawer.
      contents)))


;;;; Dynamic Block

(defun org-e-ascii-dynamic-block (dynamic-block contents info)
  "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information.  See
`org-export-data'."
  contents)


;;;; Emphasis

(defun org-e-ascii-emphasis (emphasis contents info)
  "Transcode EMPHASIS from Org to ASCII.
CONTENTS is the contents of the emphasized text.  INFO is a plist
holding contextual information.."
  (let ((marker (org-element-property :marker emphasis)))
    ;; Leave emphasis markers as-is.
    (concat marker contents marker)))


;;;; Entity

(defun org-e-ascii-entity (entity contents info)
  "Transcode an ENTITY object from Org to ASCII.
CONTENTS are the definition itself.  INFO is a plist holding
contextual information."
  (org-element-property
   (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
   entity))


;;;; Example Block

(defun org-e-ascii-example-block (example-block contents info)
  "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (org-e-ascii--box-string (org-export-handle-code example-block info) info))


;;;; Export Snippet

(defun org-e-ascii-export-snippet (export-snippet contents info)
  "Transcode a EXPORT-SNIPPET object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
    (org-element-property :value export-snippet)))


;;;; Export Block

(defun org-e-ascii-export-block (export-block contents info)
  "Transcode a EXPORT-BLOCK element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (when (string= (org-element-property :type export-block) "ascii")
    (org-remove-indentation (org-element-property :value export-block))))


;;;; Fixed Width

(defun org-e-ascii-fixed-width (fixed-width contents info)
  "Transcode a FIXED-WIDTH element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (org-e-ascii--box-string
   (replace-regexp-in-string
    "^[ \t]*: ?" "" (org-element-property :value fixed-width)) info))


;;;; Footnote Definition

;; Footnote Definitions are ignored.  They are compiled at the end of
;; the document, by `org-e-ascii-template'.


;;;; Footnote Reference

(defun org-e-ascii-footnote-reference (footnote-reference contents info)
  "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (format "[%s]" (org-export-get-footnote-number footnote-reference info)))


;;;; Headline

(defun org-e-ascii-headline (headline contents info)
  "Transcode an HEADLINE element from Org to ASCII.
CONTENTS holds the contents of the headline.  INFO is a plist
holding contextual information."
  ;; Don't export footnote section, which will be handled at the end
  ;; of the template.
  (unless (org-element-property :footnote-section-p headline)
    (let* ((low-level-rank (org-export-low-level-p headline info))
	   (width (org-e-ascii--current-text-width headline info))
	   ;; Blank lines between headline and its contents.
	   ;; `org-e-ascii-headline-spacing', when set, overwrites
	   ;; original buffer's spacing.
	   (pre-blanks
	    (make-string
	     (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
	       (org-element-property :pre-blank headline)) ?\n))
	   ;; Even if HEADLINE has no section, there might be some
	   ;; links in its title that we shouldn't forget to describe.
	   (links
	    (unless (eq (caar (org-element-contents headline)) 'section)
	      (org-e-ascii--describe-links
	       (org-e-ascii--unique-links headline info) width info))))
      ;; Deep subtree: export it as a list item.
      (if low-level-rank
	  (concat
	   ;; Bullet.
	   (let ((bullets (cdr (assq (plist-get info :ascii-charset)
				     org-e-ascii-bullets))))
	     (char-to-string
	      (nth (mod (1- low-level-rank) (length bullets)) bullets)))
	   " "
	   ;; Title.
	   (org-e-ascii--build-title headline info width) "\n"
	   ;; Contents, indented by length of bullet.
	   pre-blanks
	   (org-e-ascii--indent-string
	    (concat contents
		    (when (org-string-nw-p links) (concat "\n\n" links)))
	    2))
	;; Else: Standard headline.
	(concat
	 (org-e-ascii--build-title headline info width 'underline)
	 "\n" pre-blanks
	 (concat (when (org-string-nw-p links) links) contents))))))


;;;; Horizontal Rule

(defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  "Transcode an HORIZONTAL-RULE  object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (let ((attr
	 (read
	  (format
	   "(%s)"
	   (mapconcat
	    #'identity
	    (org-element-property :attr_ascii horizontal-rule)
	    " ")))))
    (make-string (or (and (wholenump (plist-get attr :width))
			  (plist-get attr :width))
		     (org-e-ascii--current-text-width horizontal-rule info))
		 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))


;;;; Inline Babel Call

;; Inline Babel Calls are ignored.


;;;; Inline Src Block

(defun org-e-ascii-inline-src-block (inline-src-block contents info)
  "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (format org-e-ascii-verbatim-format
	  (org-element-property :value inline-src-block)))


;;;; Inlinetask

(defun org-e-ascii-inlinetask (inlinetask contents info)
  "Transcode an INLINETASK element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  (let ((width (org-e-ascii--current-text-width inlinetask info))
	(title (org-export-secondary-string
		(org-element-property :title inlinetask) 'e-ascii info))
	(todo (and (plist-get info :with-todo-keywords)
		   (let ((todo (org-element-property
				:todo-keyword inlinetask)))
		     (and todo
			  (org-export-secondary-string todo 'e-ascii info)))))
	(todo-type (org-element-property :todo-type inlinetask))
	(tags (and (plist-get info :with-tags)
		   (org-element-property :tags inlinetask)))
	(priority (and (plist-get info :with-priority)
		       (org-element-property :priority inlinetask))))
    ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
    ;; with appropriate arguments.
    (if (functionp org-e-ascii-format-inlinetask-function)
	(funcall org-e-ascii-format-inlinetask-function
		 todo todo-type priority title tags contents width)
      ;; Otherwise, use a default template.
      (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
	(org-e-ascii--indent-string
	 (concat
	  ;; Top line, with an additional blank line if not in UTF-8.
	  (make-string width (if utf8p ?━ ?_))  "\n"
	  (unless utf8p (concat (make-string width ? ) "\n"))
	  ;; Add title.  Fill it if wider than inlinetask.
	  (let ((title (org-e-ascii--build-title inlinetask info width)))
	    (if (<= (length title) width) title
		(org-e-ascii--fill-string title width info)))
	  "\n"
	  ;; If CONTENTS is not empty, insert it along with
	  ;; a separator.
	  (when (org-string-nw-p contents)
	    (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
	  ;; Bottom line.
	  (make-string width (if utf8p ?━ ?_)))
	 ;; Flush the inlinetask to the right.
	 (- org-e-ascii-text-width org-e-ascii-global-margin
	    (if (not (org-export-get-parent-headline inlinetask info)) 0
	      org-e-ascii-inner-margin)
	    (org-e-ascii--current-text-width inlinetask info)))))))


;;;; Item

(defun org-e-ascii-item (item contents info)
  "Transcode an ITEM element from Org to ASCII.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (let ((bullet
	 ;; First parent of ITEM is always the plain-list.  Get
	 ;; `:type' property from it.
	 (org-list-bullet-string
	  (case (org-element-property :type (org-export-get-parent item info))
	    (descriptive
	     (concat
	      (org-export-secondary-string
	       (org-element-property :tag item) 'e-ascii info) ": "))
	    (ordered
	     ;; Return correct number for ITEM, paying attention to
	     ;; counters.
	     (let* ((struct (org-element-property :structure item))
		    (bul (org-element-property :bullet item))
		    (num
		     (number-to-string
		      (car (last (org-list-get-item-number
				  (org-element-property :begin item)
				  struct
				  (org-list-prevs-alist struct)
				  (org-list-parents-alist struct)))))))
	       (replace-regexp-in-string "[0-9]+" num bul)))
	    (t (let ((bul (org-element-property :bullet item)))
		 ;; Change bullets into more visible form if UTF-8 is active.
		 (if (not (eq (plist-get info :ascii-charset) 'utf-8)) bul
		   (replace-regexp-in-string
		    "-" "•"
		    (replace-regexp-in-string
		     "+" "⁃"
		     (replace-regexp-in-string "*" "‣" bul))))))))))
    (concat
     bullet
     ;; Contents: Pay attention to indentation.  Note: check-boxes are
     ;; already taken care of at the paragraph level so they don't
     ;; interfere with indentation.
     (let ((contents (org-e-ascii--indent-string contents (length bullet))))
       (if (eq (caar (org-element-contents item)) 'paragraph)
	   (org-trim contents)
	 (concat "\n" contents))))))


;;;; Keyword

(defun org-e-ascii-keyword (keyword contents info)
  "Transcode a KEYWORD element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (let ((key (downcase (org-element-property :key keyword)))
	(value (org-element-property :value keyword)))
    (cond
     ((string= key "ascii") value)
     ((string= key "toc")
      (let ((value (downcase value)))
	(cond
	 ((string-match "\\<headlines\\>" value)
	  (let ((depth (or (and (string-match "[0-9]+" value)
				(string-to-number (match-string 0 value)))
			   (plist-get info :with-toc))))
	    (org-e-ascii--build-toc
	     info (and (wholenump depth) depth) keyword)))
	 ((string= "tables" value)
	  (org-e-ascii--list-tables keyword info))
	 ((string= "listings" value)
	  (org-e-ascii--list-listings keyword info))))))))


;;;; Latex Environment

(defun org-e-ascii-latex-environment (latex-environment contents info)
  "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (org-remove-indentation (org-element-property :value latex-environment)))


;;;; Latex Fragment

(defun org-e-ascii-latex-fragment (latex-fragment contents info)
  "Transcode a LATEX-FRAGMENT object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (org-element-property :value latex-fragment))


;;;; Line Break

(defun org-e-ascii-line-break (line-break contents info)
  "Transcode a LINE-BREAK object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
  information."  hard-newline)


;;;; Link

(defun org-e-ascii-link (link desc info)
  "Transcode a LINK object from Org to ASCII.

DESC is the description part of the link, or the empty string.
INFO is a plist holding contextual information."
  (let ((raw-link (org-element-property :raw-link link))
	(type (org-element-property :type link)))
    (cond
     ((string= type "coderef")
      (let ((ref (org-element-property :path link)))
	(format (org-export-get-coderef-format ref desc)
		(org-export-resolve-coderef ref info))))
     ;; Do not apply a special syntax on radio links.  Though, parse
     ;; and transcode path to have a proper display of contents.
     ((string= type "radio")
      (org-export-secondary-string
       (org-element-parse-secondary-string
	(org-element-property :path link)
	(cdr (assq 'radio-target org-element-object-restrictions)))
       'e-ascii info))
     ;; Do not apply a special syntax on fuzzy links pointing to
     ;; targets.
     ((string= type "fuzzy")
      (let ((destination (org-export-resolve-fuzzy-link link info)))
	;; Ignore invisible "#+target: path".
	(unless (eq (org-element-type destination) 'keyword)
	  (if (org-string-nw-p desc) desc
	    (when destination
	      (let ((number (org-export-get-ordinal destination info)))
		(when number
		  (if (atom number) (number-to-string number)
		    (mapconcat 'number-to-string number ".")))))))))
     (t
      (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
	(concat
	 (format "[%s]" desc)
	 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))


;;;; Macro

(defun org-e-ascii-macro (macro contents info)
  "Transcode a MACRO element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual
information."
  (org-export-expand-macro macro info))


;;;; Paragraph

(defun org-e-ascii-paragraph (paragraph contents info)
  "Transcode a PARAGRAPH element from Org to ASCII.
CONTENTS is the contents of the paragraph, as a string.  INFO is
the plist used as a communication channel."
  (org-e-ascii--fill-string
   (let ((parent (org-export-get-parent paragraph info)))
     ;; If PARAGRAPH is the first one in a list element, be sure to
     ;; add the check-box in front of it, before any filling.  Later,
     ;; it would interfere with line width.
     (if (and (eq (org-element-type parent) 'item)
	      (equal (car (org-element-contents parent)) paragraph))
	 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
	   (concat (case (org-element-property :checkbox parent)
		     (on (if utf8p "☑ " "[X] "))
		     (off (if utf8p "☐ " "[ ] "))
		     (trans (if utf8p "☒ " "[-] ")))
		   contents))
       contents))
   (org-e-ascii--current-text-width paragraph info) info))


;;;; Plain List

(defun org-e-ascii-plain-list (plain-list contents info)
  "Transcode a PLAIN-LIST element from Org to ASCII.
CONTENTS is the contents of the list.  INFO is a plist holding
contextual information."
  contents)


;;;; Plain Text

(defun org-e-ascii-plain-text (text info)
  "Transcode a TEXT string from Org to ASCII.
INFO is a plist used as a communication channel."
  (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
		(plist-get info :with-special-strings)))
      text
    ;; Usual replacements in utf-8 with proper option set.
    (replace-regexp-in-string
     "\\.\\.\\." "…"
     (replace-regexp-in-string
      "--" "–"
      (replace-regexp-in-string "---" "—" text)))))


;;;; Property Drawer

(defun org-e-ascii-property-drawer (property-drawer contents info)
  "Transcode a PROPERTY-DRAWER element from Org to ASCII.
CONTENTS is nil.  INFO is a plist used as a communication
channel."
  ;; The property drawer isn't exported but we want separating blank
  ;; lines nonetheless.
  "")


;;;; Quote Block

(defun org-e-ascii-quote-block (quote-block contents info)
  "Transcode a QUOTE-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  (let ((width (org-e-ascii--current-text-width quote-block info)))
    (org-e-ascii--indent-string
     (org-remove-indentation
      (org-e-ascii--fill-string contents width info))
     org-e-ascii-quote-margin)))


;;;; Quote Section

(defun org-e-ascii-quote-section (quote-section contents info)
  "Transcode a QUOTE-SECTION element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (let ((width (org-e-ascii--current-text-width quote-section info))
	(value
	 (org-export-secondary-string
	  (org-remove-indentation
	   (org-element-property :value quote-section)) 'e-ascii info)))
    (org-e-ascii--indent-string
     value
     (+ org-e-ascii-quote-margin
	;; Don't apply inner margin if parent headline is low level.
	(let ((headline (org-export-get-parent-headline quote-section info)))
	  (if (org-export-low-level-p headline info) 0
	    org-e-ascii-inner-margin))))))


;;;; Radio Target

(defun org-e-ascii-radio-target (radio-target contents info)
  "Transcode a RADIO-TARGET object from Org to ASCII.
CONTENTS is the contents of the target.  INFO is a plist holding
contextual information."
  contents)

;;;; Section

(defun org-e-ascii-section (section contents info)
  "Transcode a SECTION element from Org to ASCII.
CONTENTS is the contents of the section.  INFO is a plist holding
contextual information."
  (org-e-ascii--indent-string
   (concat
    contents
    (when org-e-ascii-links-to-notes
      ;; Add list of links at the end of SECTION.
      (let ((links (org-e-ascii--describe-links
		    (org-e-ascii--unique-links section info)
		    (org-e-ascii--current-text-width section info) info)))
	;; Separate list of links and section contents.
	(when (org-string-nw-p links) (concat "\n\n" links)))))
   ;; Do not apply inner margin if parent headline is low level.
   (let ((headline (org-export-get-parent-headline section info)))
     (if (or (not headline) (org-export-low-level-p headline info)) 0
       org-e-ascii-inner-margin))))


;;;; Special Block

(defun org-e-ascii-special-block (special-block contents info)
  "Transcode a SPECIAL-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the block.  INFO is a plist
holding contextual information."
  contents)


;;;; Src Block

(defun org-e-ascii-src-block (src-block contents info)
  "Transcode a SRC-BLOCK element from Org to ASCII.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information."
  (let ((caption (org-e-ascii--build-caption src-block info)))
    (concat
     (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
     (org-e-ascii--box-string (org-export-handle-code src-block info) info)
     (when (and caption (not org-e-ascii-caption-above))
       (concat "\n" caption)))))

;;;; Statistics Cookie

(defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  "Transcode a STATISTICS-COOKIE object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (org-element-property :value statistics-cookie))


;;;; Subscript

(defun org-e-ascii-subscript (subscript contents info)
  "Transcode a SUBSCRIPT object from Org to ASCII.
CONTENTS is the contents of the object.  INFO is a plist holding
contextual information."
  (if (org-element-property :use-brackets-p subscript)
      (format "_{%s}" contents)
    (format "_%s" contents)))


;;;; Superscript

(defun org-e-ascii-superscript (superscript contents info)
  "Transcode a SUPERSCRIPT object from Org to ASCII.
CONTENTS is the contents of the object.  INFO is a plist holding
contextual information."
  (if (org-element-property :use-brackets-p superscript)
      (format "_{%s}" contents)
    (format "_%s" contents)))


;;;; Table

;; While `org-e-ascii-table' is the callback function expected by
;; org-export mechanism, it requires four subroutines to display
;; tables accordingly to chosen charset, alignment and width
;; specifications.

;; Thus, `org-e-ascii-table--column-width' computes the display width
;; for each column in the table,
;; `org-e-ascii-table--vertical-separators' returns a vector
;; containing separators (or lack thereof),
;; `org-e-ascii-table--build-hline' creates various hline strings,
;; depending on charset, separators and position within the tabl and
;; `org-e-ascii-table--format-cell' properly aligns contents within
;; a given cell and width.

(defun org-e-ascii-table (table contents info)
  "Transcode a TABLE element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (let ((raw-table (org-element-property :raw-table table))
	(caption (org-e-ascii--build-caption table info)))
    (concat
     ;; Possibly add a caption string above.
     (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
     ;; Insert table.  Note: "table.el" tables are left unmodified.
     (if (eq (org-element-property :type table) 'table.el) raw-table
       (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
	      ;; Extract information out of the raw table (TABLE-INFO)
	      ;; and clean it (CLEAN-TABLE).
	      (table-info (org-export-table-format-info raw-table))
	      (special-col-p (plist-get table-info :special-column-p))
	      (alignment (plist-get table-info :alignment))
	      (clean-table (org-export-clean-table raw-table special-col-p))
	      ;; Change table into lisp, much like
	      ;; `org-table-to-lisp', though cells are parsed and
	      ;; transcoded along the way.
	      (lisp-table
	       (mapcar
		(lambda (line)
		  (if (string-match org-table-hline-regexp line) 'hline
		    (mapcar
		     (lambda (cell)
		       (org-trim
			(org-export-secondary-string
			 (org-element-parse-secondary-string
			  cell
			  (cdr (assq 'item org-element-string-restrictions)))
			 'e-ascii info)))
		     (org-split-string (org-trim line) "\\s-?|\\s-?"))))
		(org-split-string clean-table "[ \t]*\n[ \t]*")))
	      ;; Compute real column widths.
	      (column-widths
	       (org-e-ascii-table--column-width lisp-table table-info))
	      ;; Construct separators according to column groups.
	      (separators (org-e-ascii-table--vertical-separators table-info))
	      ;; Build different `hline' strings, depending on
	      ;;  separators, column widths and position.
	      (hline-standard
	       (org-e-ascii-table--build-hline
		nil separators column-widths info))
	      (hline-top
	       (and utf8p (org-e-ascii-table--build-hline
			   'top separators column-widths info)))
	      (hline-bottom
	       (and utf8p (org-e-ascii-table--build-hline
			   'bottom separators column-widths info))))
	 ;; Now build table back, with correct alignment, considering
	 ;; columns widths and separators.
	 (mapconcat
	  (lambda (line)
	    (cond
	     ((eq line 'hline) hline-standard)
	     ((eq line 'hline-bottom) hline-bottom)
	     ((eq line 'hline-top) hline-top)
	     (t (loop for cell in line
		      for col from 0 to (length line)
		      concat
		      (concat
		       (let ((sep (aref separators col)))
			 (if (and utf8p (not (string= sep ""))) "│" sep))
		       (org-e-ascii-table--format-cell
			cell col column-widths alignment info)) into l
			finally return
			(concat l
				(let ((sep (aref separators col)))
				  (if (and utf8p (not (string= sep ""))) "│"
				    sep)))))))
	  ;; If charset is `utf-8', make sure lisp-table always starts
	  ;; with `hline-top' and ends with `hline-bottom'.
	  (if (not utf8p) lisp-table
	    (setq lisp-table
		  (cons 'hline-top
			(if (eq (car lisp-table) 'hline) (cdr lisp-table)
			  lisp-table)))
	    (setq lisp-table
		  (nconc
		   (if (eq (car (last lisp-table)) 'hline) (butlast lisp-table)
		     lisp-table)
		   '(hline-bottom)))) "\n")))
     ;; Possible add a caption string below.
     (when (and caption (not org-e-ascii-caption-above))
       (concat "\n" caption)))))

(defun org-e-ascii-table--column-width (table table-info)
  "Return vector of TABLE columns width.

TABLE is the Lisp representation of the Org table considered.
TABLE-INFO holds information about the table.  See
`org-export-table-format-info'.

Unlike to `:width' property from `org-export-table-format-info',
the return value is a vector containing width of every column,
not only those with an explicit width cookie.  Special column, if
any, is ignored."
  ;; All rows have the same length, but be sure to ignore hlines.
  (let ((width (make-vector
		(loop for row in table
		      unless (eq row 'hline)
		      return (length row))
		0)))
    ;; Set column width to the maximum width of the cells in that
    ;; column.
    (mapc
     (lambda (line)
       (let ((idx 0))
	 (unless (eq line 'hline)
	   (mapc (lambda (cell)
		   (let ((len (length cell)))
		     (when (> len (aref width idx)) (aset width idx len)))
		   (incf idx))
		 line))))
     table)
    (unless org-e-ascii-table-widen-columns
      ;; When colums are not widened, width cookies have precedence
      ;; over string lengths.  Thus, overwrite the latter with the
      ;; former.
      (let ((cookies (plist-get table-info :width))
	    (specialp (plist-get table-info :special-column-p)))
	;; Remove special column from COOKIES vector, if any.
	(loop for w across (if specialp (substring cookies 1) cookies)
	      for idx from 0 to width
	      when w do (aset width idx w))))
    ;; Return value.
    width))

(defun org-e-ascii-table--vertical-separators (table-info)
  "Return a vector of strings for vertical separators.

TABLE-INFO holds information about considered table.  See
`org-export-table-format-info'.

Return value is a vector whose length is one more than the number
of columns in the table.  Special column, if any, is ignored."
  (let* ((colgroups (plist-get table-info :column-groups))
	 (separators (make-vector (1+ (length colgroups)) "")))
    (if org-e-ascii-table-keep-all-vertical-lines
	(make-vector (length separators) "|")
      (let ((column 0))
	(mapc (lambda (group)
		(when (memq group '(start start-end))
		  (aset separators column "|"))
		(when (memq group '(end start-end))
		  (aset separators (1+ column) "|"))
		(incf column))
	      colgroups)
	;; Remove unneeded special column.
	(if (not (plist-get table-info :special-column-p)) separators
	  (substring separators 1))))))

(defun org-e-ascii-table--format-cell (cell col width alignment info)
  "Format CELL with column width and alignment constraints.

CELL is the contents of the cell, as a string.

COL is the column containing the cell considered.

WIDTH is a vector holding every column width, as returned by
`org-e-ascii-table--column-width'.

ALIGNMENT is a vector containing alignment strings for every
column.

INFO is a plist used as a communication channel."
  (let ((col-width (if org-e-ascii-table-widen-columns (aref width col)
		     (or (aref width col) (length cell)))))
    ;; When CELL is too large, it has to be truncated.
    (unless (or org-e-ascii-table-widen-columns (<= (length cell) col-width))
      (setq cell (concat (substring cell 0 (- col-width 2)) "=>")))
    (let* ((indent-tabs-mode nil)
	   (align (aref alignment col))
	   (aligned-cell
	    (org-e-ascii--justify-string
	     (org-trim cell) col-width
	     (cond ((string= align "c") 'center)
		   ((string= align "r") 'right)))))
      ;; Return aligned cell, with missing white spaces added and
      ;; space separators between columns.
      (format
       " %s "
       (concat aligned-cell
	       (make-string (- col-width (length aligned-cell)) ? ))))))

(defun org-e-ascii-table--build-hline (position separators column-widths info)
  "Return string used as an horizontal line in tables.

POSITION is a symbol among `top', `bottom' and nil, which
specifies position of the horizontal line within the table.

SEPARATORS is a vector strings specifying separators used in the
table, as returned by `org-e-ascii-table--vertical-separators'.

COLUMN-WIDTHS is a vector of numbers specifying widths of all
columns in the table, as returned by
`org-e-ascii-table--column-width'.

INFO is a plist used as a communication channel."
  (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
    (loop for idx from 0 to (length separators)
	  for width across column-widths
	  concat
	  (concat
	   (cond ((string= (aref separators idx) "") nil)
		 ((and utf8p (zerop idx))
		  (cond ((eq position 'top) "┍")
			((eq position 'bottom) "┕")
			(t "├")))
		 (utf8p
		  (cond ((eq position 'top) "┯")
			((eq position 'bottom) "┷")
			(t "┼")))
		 (t "+"))
	   ;; Hline has to cover all the cell and both white spaces
	   ;; between columns.
	   (make-string (+ width 2)
			(cond ((not utf8p) ?-)
			      ((not position) ?─)
			      (t ?━))))
	  into hline
	  finally return
	  ;; There is one separator more than columns, so handle it
	  ;; here.
	  (concat
	   hline
	   (cond
	    ((string= (aref separators idx) "") nil)
	    (utf8p (cond ((eq position 'top) "┑")
			 ((eq position 'bottom) "┙")
			 (t "┤")))
	    (t "+"))))))


;;;; Target

;; Targets are invisible.


;;;; Time-stamp

(defun org-e-ascii-time-stamp (time-stamp contents info)
  "Transcode a TIME-STAMP object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  ;; Return time-stamps as-is.
  (org-element-time-stamp-interpreter time-stamp contents))


;;;; Verbatim

(defun org-e-ascii-verbatim (verbatim contents info)
  "Return a VERBATIM object from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (format org-e-ascii-verbatim-format
	  (org-element-property :value verbatim)))


;;;; Verse Block

(defun org-e-ascii-verse-block (verse-block contents info)
  "Transcode a VERSE-BLOCK element from Org to ASCII.
CONTENTS is nil.  INFO is a plist holding contextual information."
  (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
    (org-e-ascii--indent-string
     (org-e-ascii--justify-string
      (org-export-secondary-string
       (org-element-property :value verse-block) 'e-ascii info)
      verse-width 'left)
     org-e-ascii-quote-margin)))

\f
;;; Filter

(defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  "Filter controlling number of blank lines after an headline.

HEADLINE is a string representing a transcoded headline.
BACK-END is symbol specifying back-end used for export.  INFO is
plist containing the communication channel.

This function only applies to `e-ascii' back-end.  See
`org-e-ascii-headline-spacing' for information.

For any other back-end, HEADLINE is returned as-is."
  (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
    (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
      (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))


\f
;;; Interactive function

(defun org-e-ascii-export-to-ascii
  (&optional subtreep visible-only body-only ext-plist pub-dir)
  "Export current buffer to a text file.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, strip title, table
of contents and footnote definitions from output.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

When optional argument PUB-DIR is set, use it as the publishing
directory.

Return output file's name."
  (interactive)
  (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
    (org-export-to-file
     'e-ascii outfile subtreep visible-only body-only ext-plist)))


(provide 'org-e-ascii)
;;; org-e-ascii.el ends here

debug log:

solving c9cca4a ...
found c9cca4a in https://list.orgmode.org/orgmode/87399wrrqx.fsf@gmail.com/
found 0eb547b in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 0eb547b9b831f69485ed7c2dfc218276a2f5adc5	EXPERIMENTAL/org-e-ascii.el

applying [1/1] https://list.orgmode.org/orgmode/87399wrrqx.fsf@gmail.com/
diff --git a/EXPERIMENTAL/org-e-ascii.el b/EXPERIMENTAL/org-e-ascii.el
index 0eb547b..c9cca4a 100644

Checking patch EXPERIMENTAL/org-e-ascii.el...
Applied patch EXPERIMENTAL/org-e-ascii.el cleanly.

index at:
100644 c9cca4ad1fa97d90146599d8c6c1bbab7c656359	EXPERIMENTAL/org-e-ascii.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).