summaryrefslogtreecommitdiff
path: root/.emacs.d/org-7.4/contrib/lisp/org-export-generic.el
blob: f8e8c4afe6b1b14ef35ce29bfe1d83e4e012078b (plain)
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
;; org-export-generic.el --- Export frameworg with custom backends

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

;; Author:   Wes Hardaker <hardaker at users dot sourceforge dot net>
;; Keywords: outlines, hypermedia, calendar, wp, export
;; Homepage: http://orgmode.org
;; Version:  6.25trans
;; Acks:     Much of this code was stolen form the ascii export from Carsten
;;
;; This file is not yet part of GNU Emacs.
;;
;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
;;
;; ----------------------------------------------------------------------
;;
;; OVERVIEW
;;
;; org-export-generic is basically a simple translation system that
;; knows how to parse at least most of a .org buffer and then add
;; various formatting prefixes before and after each section type.  It
;; does this by examining a property list stored in org-generic-alist.
;; You can dynamically add propety lists of your own using the
;; org-set-generic-type function:
;;
;;    (org-set-generic-type
;;     "really-basic-text" 
;;     '(:file-suffix  ".txt"
;;       :key-binding  ?R
;;
;;       :title-format "=== %s ===\n"
;;       :body-header-section-numbers t
;;       :body-header-section-number-format "%s) "
;;       :body-section-header-prefix  "\n"
;;       :body-section-header-suffix "\n"
;;       :body-line-format "  %s\n"
;;       :body-line-wrap   75
;;       ))
;;
;; Note: Upper case key-bindings are reserved for your use.  Lower
;; case key bindings may conflict with future export-generic
;; publications.
;;
;; Then run org-export (ctrl-c ctrl-e) and select generic or run
;; org-export-generic.  You'll then be prompted with a list of export
;; types to choose from which will include your new type assigned to
;; the key "r".
;;
;; ----------------------------------------------------------------------
;;
;; TODO (non-ordered)
;;   * handle function references
;;   * handle other types of multi-complex-listy-things to do
;;     ideas:  (t ?- "%s" ?-)
;;   * handle indent specifiers better
;;     ideas:  (4 ?\  "%s")
;;   * need flag to remove indents from body text
;;   * handle links
;;   * handle internationalization strings better
;;   * date/author/etc needs improvment (internationalization too)
;;   * allow specifying of section ordering
;;     ideas:  :ordering  ("header" "toc" "body" "footer")
;;                         ^ matches current hard coded ordering
;;   * err, actually *do* a footer
;;   * deal with usage of org globals
;;   *** should we even consider them, or let the per-section specifiers do it
;;   *** answer: remove; mostly removed now
;;   * deal with interactive support for picking a export specifier label
;;   * char specifiers that need extra length because of formatting
;;     idea: (?- 4)  for 4-longer
;;   * centering specifier
;;     idea: ('center " -- %s -- ")
;;   * remove more of the unneeded export-to-ascii copy code
;;   * tags
;;     *** supported now, but need separate format per tag
;;     *** allow different open/closing prefixes
;;   * properties
;;   * drawers
;;   * oh my
;;   * optmization (many plist extracts should be in let vars)
;;   * define defcustom spec for the specifier list
;;   * fonts:  at least monospace is not handled at all here.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:

(require 'org-exp)
(require 'assoc)

(defgroup org-export-generic nil
  "Options specific for ASCII export of Org-mode files."
  :tag "Org Export ASCII"
  :group 'org-export)

(defcustom org-export-generic-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-generic
  :type 'boolean)


(defvar org-generic-current-indentation nil) ; For communication

(defvar org-generic-alist
  '(
    ;;
    ;; generic DEMO exporter
    ;;
    ;; (this tries to use every specifier for demo purposes)
    ;;
    ("demo"
     :file-suffix        	    ".txt"
     :key-binding        	    ?d

     :header-prefix             "<header>\n"
     :header-suffix             "</header>\n"

     :author-export             t
     :tags-export               t

     :drawers-export            t


     :title-prefix              ?=
     :title-format              "<h1>%s</h1>\n"
     :title-suffix              ?=

     :date-export        	    t
     :date-prefix               "<date>"
     :date-format               "<br /><b>Date:</b> <i>%s</i><br />"
     :date-suffix               "</date>\n\n"

     :toc-export                t
     :toc-header-prefix         "<tocname>\n"
     :toc-header-format         "__%s__\n"
     :toc-header-suffix         "</tocname>\n"

     :toc-prefix                "<toc>\n"
     :toc-suffix                "</toc>\n"

     :toc-section-numbers       t
     :toc-section-number-format "\#(%s) "
     :toc-format                "--%s--"    
     :toc-format-with-todo      "!!%s!!\n"
     :toc-indent-char           ?\ 
     :toc-indent-depth          4         

     :toc-tags-export           t
     :toc-tags-prefix           "   <tags>"
     :toc-tags-format           "*%s*"
     :toc-tags-suffix           "</tags>\n"
     :toc-tags-none-string      "\n"

     :body-header-section-numbers 3	; t = all, nil = none

					; lists indicate different things per level
					; list contents or straight value can either be a
					;    ?x char reference for printing strings that match the header len
					;    "" string to print directly
     :body-section-header-prefix  ("<h1>" "<h2>" "<h3>"
				   "<h4>" "<h5>" "<h6>")
     :body-section-header-format  "%s"
     :body-section-header-suffix  ("</h1>\n" "</h2>\n" "</h3>\n"
				   "</h4>\n" "</h5>\n" "</h6>\n")

     :timestamps-export           t
     :priorities-export           t
     :todo-keywords-export        t

     :body-tags-export 		  t
     :body-tags-prefix 		  "  <tags>"
     :body-tags-suffix 		  "</tags>\n"

					; section prefixes/suffixes can be direct strings or lists as well
     :body-section-prefix         "<secprefix>\n"
     :body-section-suffix         "</secsuffix>\n"
;	 :body-section-prefix         ("<sec1>\n" "<sec2>\n" "<sec3>\n")
;	 :body-section-suffix         ("</sec1>\n" "</sec2>\n" "</sec3>\n")


					; if preformated text should be included (eg, : prefixed)
     :body-line-export-preformated t
     :body-line-fixed-prefix       "<pre>\n"
     :body-line-fixed-suffix       "\n</pre>\n"
     :body-line-fixed-format       "%s\n"


     :body-list-prefix             "<list>\n"
     :body-list-suffix             "</list>\n"
     :body-list-format             "<li>%s</li>\n"

     :body-number-list-prefix       "<ol>\n"
     :body-number-list-suffix       "</ol>\n"
     :body-number-list-format       "<li>%s</li>\n"
     :body-number-list-leave-number t

     :body-list-checkbox-todo      "<checkbox type=\"todo\">"
     :body-list-checkbox-todo-end  "</checkbox (todo)>"
     :body-list-checkbox-done      "<checkbox type=\"done\">"
     :body-list-checkbox-done-end  "</checkbox (done)>"
     :body-list-checkbox-half      "<checkbox type=\"half\">"
     :body-list-checkbox-half-end  "</checkbox (half)>"


     

					; other body lines
     :body-line-format             "%s"
     :body-line-wrap               60	; wrap at 60 chars

					; print above and below all body parts
     :body-text-prefix 	       "<p>\n"
     :body-text-suffix 	       "</p>\n"

     )

    ;;
    ;; ascii exporter
    ;;
    ;; (close to the original ascii specifier)
    ;;
    ("ascii"
     :file-suffix        	    ".txt"
     :key-binding        	    ?a

     :header-prefix             ""
     :header-suffix             ""

     :title-prefix ?=
     :title-format "%s\n"
     :title-suffix ?=

     :date-export        	    t
     :date-prefix               ""
     :date-format               "Date: %s\n"
     :date-suffix               ""

     :toc-header-prefix ""
     :toc-header-format "%s\n"
     :toc-header-suffix ?=

     :toc-export                t
     :toc-section-numbers       t
     :toc-section-number-format "%s "
     :toc-format                "%s\n"    
     :toc-format-with-todo      "%s (*)\n"
     :toc-indent-char           ?\ 
     :toc-indent-depth          4         

     :body-header-section-numbers 3
     :body-section-prefix         "\n"

;	 :body-section-header-prefix  "\n"
;	 :body-section-header-format  "%s\n"
;	 :body-section-header-suffix  (?\$ ?\# ?^ ?\~ ?\= ?\-)

     :body-section-header-prefix  ("" "" "" "* " "  + " "    - ")
     :body-section-header-format  "%s\n"
     :body-section-header-suffix  (?~ ?= ?- "\n" "\n" "\n")

;	 :body-section-marker-prefix  ""
;	 :body-section-marker-chars   (?\$ ?\# ?^ ?\~ ?\= ?\-)
;	 :body-section-marker-suffix  "\n"

     :body-line-export-preformated t
     :body-line-format             "%s\n"
     :body-line-wrap               75

;	 :body-text-prefix "<t>\n"
;	 :body-text-suffix "</t>\n"


     :body-bullet-list-prefix      (?* ?+ ?-)
;	 :body-bullet-list-suffix      (?* ?+ ?-)
     )

    ;;
    ;; wikipedia
    ;;
    ("wikipedia"
     :file-suffix        	    ".txt"
     :key-binding                   ?w

     :header-prefix            	    ""
     :header-suffix            	    ""

     :title-format             	    "= %s =\n"

     :date-export        	    nil

     :toc-export                    nil

     :body-header-section-numbers   nil
     :body-section-prefix           "\n"

     :body-section-header-prefix    ("= " "== " "=== "
				     "==== " "===== " "====== ")
     :body-section-header-suffix    (" =\n\n" " ==\n\n" " ===\n\n" 
				     " ====\n\n" " =====\n\n" " ======\n\n")

     :body-line-export-preformated  t          ;; yes/no/maybe???
     :body-line-format              "%s\n"
     :body-line-wrap                75

     :body-line-fixed-format       " %s\n"

     :body-list-format              "* %s\n"
     :body-number-list-format       "# %s\n"

     :body-bullet-list-prefix       ("* " "** " "*** " "**** " "***** ")
     )

    ;;
    ;; minimal html exporter
    ;;
    ("html"
     ;; simple html output
     :file-suffix        	    ".html"
     :key-binding                   ?h

     :header-prefix             "<body>"

     :title-format              "<h1>%s</h1>\n\n"

     :date-export        	    t
     :date-format               "<br /><b>Date:</b> <i>%s</i><br />\n\n"

     :toc-export                nil

     :body-header-section-numbers 3

     :body-section-header-prefix  ("<h1>" "<h2>" "<h3>"
				   "<h4>" "<h5>" "<h6>")
     :body-section-header-format  "%s"
     :body-section-header-suffix  ("</h1>\n" "</h2>\n" "</h3>\n"
				   "</h4>\n" "</h5>\n" "</h6>\n")

     :body-section-prefix         "<secprefix>\n"
     :body-section-suffix         "</secsuffix>\n"
;	 :body-section-prefix         ("<sec1>\n" "<sec2>\n" "<sec3>\n")
;	 :body-section-suffix         ("</sec1>\n" "</sec2>\n" "</sec3>\n")

     :body-line-export-preformated t
     :body-line-format             "%s\n"

     :body-text-prefix "<p>\n"
     :body-text-suffix "</p>\n"

     :body-bullet-list-prefix      (?* ?+ ?-)
;	 :body-bullet-list-suffix      (?* ?+ ?-)
     )

    ;;
    ;; internet-draft .xml for xml2rfc exporter
    ;;
    ("ietfid"
     ;; this tries to use every specifier for demo purposes
     :file-suffix        	    ".xml"
     :key-binding                   ?i

     :title-prefix              "<?xml version=\"1.0\"\?>
<!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
<!ENTITY rfcs PUBLIC '' 'blah'>
<?rfc strict=\"yes\" ?>
<?rfc toc=\"yes\" ?>
<?rfc tocdepth=\"4\" ?>
<?rfc symrefs=\"yes\" ?>
<?rfc compact=\"yes\" ?>
<?rfc subcompact=\"no\" ?>
<rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
    <front>
"
     :title-format              "<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
     :title-suffix              "<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
      <organization>Comany, Inc..</organization>
      <address>
        <postal>
          <street></street>
          <city></city>
          <region></region>
          <code></code>
          <country></country>
        </postal>
        <phone></phone>
        <email></email>
      </address>
    </author>
    <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
    <area>Operations and Management</area>
    <workgroup>FIXME</workgroup>
<abstract>\n"
     :date-export        	    nil

     :toc-export                nil

     :body-header-section-numbers nil

     :body-section-header-format  "<section title=\"%s\">\n"
     :body-section-suffix         "</section>\n"

					; if preformated text should be included (eg, : prefixed)
     :body-line-export-preformated t
     :body-line-fixed-prefix       "<figure>\n<artwork>\n"
     :body-line-fixed-suffix       "\n</artwork>\n</figure>\n"

					; other body lines
     :body-line-format             "%s"
     :body-line-wrap               75

					; print above and below all body parts
     :body-text-prefix 	       "<t>\n"
     :body-text-suffix 	       "</t>\n"

     :body-list-prefix 	       "<list style=\"symbols\">\n"
     :body-list-suffix 	       "</list>\n"
     :body-list-format 	       "<t>%s</t>\n"

     )
    )
  "A assoc list of property lists to specify export definitions"
)

(setq org-generic-export-type "demo")

(defvar org-export-generic-section-type "")
(defvar org-export-generic-section-suffix "")

;;;###autoload
(defun org-set-generic-type (type definition)
  "Adds a TYPE and DEFINITION to the existing list of defined generic
export definitions."
  (aput 'org-generic-alist type definition))

;;; helper functions for org-set-generic-type
(defvar org-export-generic-keywords nil)
(defmacro* def-org-export-generic-keyword (keyword
                                           &key documentation
                                                type)
  "Define KEYWORD as a legitimate element for inclusion in
the body of an org-set-generic-type definition."
  `(progn
     (pushnew ,keyword org-export-generic-keywords)
     ;; TODO: push the documentation and type information
     ;; somewhere where it will do us some good.
     ))

(def-org-export-generic-keyword :body-newline-paragraph
    :documentation "Bound either to NIL or to a pattern to be 
inserted in the output for every blank line in the input.
  The intention is to handle formats where text is flowed, and
newlines are interpreted as significant \(e.g., as indicating
preformatted text\).  A common non-nil value for this keyword
is \"\\n\".  Should typically be combined with a value for 
:body-line-format that does NOT end with a newline."
    :type string)

;;; fontification keywords
(def-org-export-generic-keyword :bold-format)
(def-org-export-generic-keyword :italic-format)
(def-org-export-generic-keyword :underline-format)
(def-org-export-generic-keyword :strikethrough-format)
(def-org-export-generic-keyword :code-format)
(def-org-export-generic-keyword :verbatim-format)

    
  

(defun org-export-generic-remember-section (type suffix &optional prefix)
  (setq org-export-generic-section-type type)
  (setq org-export-generic-section-suffix suffix)
  (if prefix
      (insert prefix))
)

(defun org-export-generic-check-section (type &optional prefix suffix)
  "checks to see if type is already in use, or we're switching parts
If we're switching, then insert a potentially previously remembered
suffix, and insert the current prefix immediately and then save the
suffix a later change time."

  (when (not (equal type org-export-generic-section-type))
    (if org-export-generic-section-suffix
      (insert org-export-generic-section-suffix))
    (setq org-export-generic-section-type type)
    (setq org-export-generic-section-suffix suffix)
    (if prefix
	(insert prefix))))

;;;###autoload
(defun org-export-generic (arg)
  "Export the outline as generic output.
If there is an active region, export only the region.
The prefix ARG specifies how many levels of the outline should become
underlined headlines.  The default is 3."
  (interactive "P")
  (setq-default org-todo-line-regexp org-todo-line-regexp)
  (let* ((opt-plist (org-combine-plists (org-default-export-plist)
					(org-infile-export-plist)))
	 (region-p (org-region-active-p))
	 (rbeg (and region-p (region-beginning)))
	 (rend (and region-p (region-end)))
	 (subtree-p
	  (when region-p
	    (save-excursion
	      (goto-char rbeg)
	      (and (org-at-heading-p)
		   (>= (org-end-of-subtree t t) rend)))))
	 (level-offset (if subtree-p
			   (save-excursion
			     (goto-char rbeg)
			     (+ (funcall outline-level)
				(if org-odd-levels-only 1 0)))
			 0))
	 (opt-plist (setq org-export-opt-plist
			  (if subtree-p
			      (org-export-add-subtree-options opt-plist rbeg)
			    opt-plist)))

	 helpstart 
	 (bogus (mapc (lambda (x)
			(setq helpstart
			      (concat helpstart "\["
				      (char-to-string
				       (plist-get (cdr x) :key-binding))
				      "]  " (car x) "\n")))
		      org-generic-alist))

	 (help (concat helpstart "

\[ ] the current setting of the org-generic-export-type variable
"))

	 (cmds

	  (append
	   (mapcar (lambda (x)
		     (list
		      (plist-get (cdr x) :key-binding)
		      (car x)))
		      org-generic-alist)
	   (list (list ?  "default"))))

	 r1 r2 ass

	 ;; read in the type to use
	 (export-plist
	  (progn
	    (save-excursion
	      (save-window-excursion
		(delete-other-windows)
		(with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
		  (princ help))
		(org-fit-window-to-buffer (get-buffer-window
					   "*Org Export/Generic Styles Help*"))
		(message "Select command: ")
		(setq r1 (read-char-exclusive))))
	    (setq r2 (if (< r1 27) (+ r1 96) r1))
	    (unless (setq ass (cadr (assq r2 cmds)))
	      (error "No command associated with key %c" r1))

	    (cdr (assoc 
		  (if (equal ass "default") org-generic-export-type ass)
		  org-generic-alist))))

	 (custom-times org-display-custom-times)
	 (org-generic-current-indentation '(0 . 0))
	 (level 0) (old-level 0) line txt lastwastext
	 (umax nil)
	 (umax-toc nil)
	 (case-fold-search nil)
	 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
	 (filesuffix (or (plist-get export-plist :file-suffix) ".foo"))
	 (filename (concat (file-name-as-directory
			    (org-export-directory :ascii opt-plist))
			   (file-name-sans-extension
			    (or (and subtree-p
				     (org-entry-get (region-beginning)
						    "EXPORT_FILE_NAME" t))
				(file-name-nondirectory bfname)))
			   filesuffix))
	 (filename (if (equal (file-truename filename)
			      (file-truename bfname))
		       (concat filename filesuffix)
		     filename))
	 (buffer (find-file-noselect filename))
	 (org-levels-open (make-vector org-level-max nil))
	 (odd org-odd-levels-only)
	 (date  (plist-get opt-plist :date))
	 (author      (plist-get opt-plist :author))
	 (title       (or (and subtree-p (org-export-get-title-from-subtree))
			  (plist-get opt-plist :title)
			  (and (not
				(plist-get opt-plist :skip-before-1st-heading))
			       (org-export-grab-title-from-buffer))
			  (file-name-sans-extension
			   (file-name-nondirectory bfname))))
	 (email       (plist-get opt-plist :email))
	 (language    (plist-get opt-plist :language))
	 (quote-re0   (concat "^[ \t]*" org-quote-string "\\>"))
;	 (quote-re    (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
	 (todo nil)
	 (lang-words nil)
	 (region
	  (buffer-substring
	   (if (org-region-active-p) (region-beginning) (point-min))
	   (if (org-region-active-p) (region-end) (point-max))))
	 (lines (org-split-string
		 (org-export-preprocess-string
		  region
		  :for-ascii t
		  :skip-before-1st-heading
		  (plist-get opt-plist :skip-before-1st-heading)
		  :drawers (plist-get export-plist :drawers-export)
		  :tags (plist-get export-plist :tags-export)
		  :priority (plist-get export-plist :priority-export)
		  :footnotes (plist-get export-plist :footnotes-export)
		  :timestamps (plist-get export-plist :timestamps-export)
		  :todo-keywords (plist-get export-plist :todo-keywords-export)
		  :verbatim-multiline t
		  :select-tags (plist-get export-plist :select-tags-export)
		  :exclude-tags (plist-get export-plist :exclude-tags-export)
                  :emph-multiline t
		  :archived-trees
		  (plist-get export-plist :archived-trees-export)
		  :add-text (plist-get opt-plist :text))
		 "\n"))
	 ;; export-generic plist variables
	 (withtags 	(plist-get export-plist :tags-export))
	 (tagsintoc 	(plist-get export-plist :toc-tags-export))
	 (tocnotagsstr 	(or (plist-get export-plist :toc-tags-none-string) ""))
	 (tocdepth 	(plist-get export-plist :toc-indent-depth))
	 (tocindentchar (plist-get export-plist :toc-indent-char))
	 (tocsecnums    (plist-get export-plist :toc-section-numbers))
	 (tocsecnumform (plist-get export-plist :toc-section-number-format))
	 (tocformat     (plist-get export-plist :toc-format))
	 (tocformtodo   (plist-get export-plist :toc-format-with-todo))
	 (tocprefix     (plist-get export-plist :toc-prefix))
	 (tocsuffix     (plist-get export-plist :toc-suffix))
	 (bodyfixedpre  (plist-get export-plist :body-line-fixed-prefix))
	 (bodyfixedsuf  (plist-get export-plist :body-line-fixed-suffix))
	 (bodyfixedform (or (plist-get export-plist :body-line-fixed-format)
			    "%s"))
	 (listprefix  	(plist-get export-plist :body-list-prefix))
	 (listsuffix  	(plist-get export-plist :body-list-suffix))
	 (listformat 	(or (plist-get export-plist :body-list-format) "%s\n"))
	 (numlistleavenum
	  (plist-get export-plist :body-number-list-leave-number))
	 (numlistprefix	(plist-get export-plist :body-number-list-prefix))
	 (numlistsuffix	(plist-get export-plist :body-number-list-suffix))
	 (numlistformat
	  (or (plist-get export-plist :body-number-list-format) "%s\n"))
	 (listchecktodo
	  (or (plist-get export-plist :body-list-checkbox-todo) "\\1"))
	 (listcheckdone
	  (or (plist-get export-plist :body-list-checkbox-done) "\\1"))
	 (listcheckhalf
	  (or (plist-get export-plist :body-list-checkbox-half) "\\1"))
	 (listchecktodoend
	  (or (plist-get export-plist :body-list-checkbox-todo-end) ""))
	 (listcheckdoneend
	  (or (plist-get export-plist :body-list-checkbox-done-end) ""))
	 (listcheckhalfend
	  (or (plist-get export-plist :body-list-checkbox-half-end) ""))
         (bodynewline-paragraph   (plist-get export-plist :body-newline-paragraph))
	 (bodytextpre   (plist-get export-plist :body-text-prefix))
	 (bodytextsuf   (plist-get export-plist :body-text-suffix))
	 (bodylinewrap  (plist-get export-plist :body-line-wrap))
	 (bodylineform  (or (plist-get export-plist :body-line-format) "%s"))
         (blockquotestart (or (plist-get export-plist :blockquote-start) "\n\n\t"))
         (blockquoteend (or (plist-get export-plist :blockquote-end) "\n\n"))

         ;; dynamic variables used heinously in fontification
         ;; not referenced locally...
         (format-boldify (plist-get export-plist :bold-format))
         (format-italicize (plist-get export-plist :italic-format))
         (format-underline (plist-get export-plist :underline-format))
         (format-strikethrough (plist-get export-plist :strikethrough-format))
         (format-code (plist-get export-plist :code-format))
         (format-verbatim (plist-get export-plist :verbatim-format))

         

	 thetoc toctags have-headings first-heading-pos
	 table-open table-buffer link-buffer link desc desc0 rpl wrap)
    
    (let ((inhibit-read-only t))
      (org-unmodified
       (remove-text-properties (point-min) (point-max)
			       '(:org-license-to-kill t))))

    (setq org-min-level (org-get-min-level lines level-offset))
    (setq org-last-level org-min-level)
    (org-init-section-numbers)

    (find-file-noselect filename)

    (setq lang-words (or (assoc language org-export-language-setup)
			 (assoc "en" org-export-language-setup)))
    (switch-to-buffer-other-window buffer)
    (erase-buffer)
    (fundamental-mode)
    ;; create local variables for all options, to make sure all called
    ;; functions get the correct information
    (mapc (lambda (x)
	    (set (make-local-variable (nth 2 x))
		 (plist-get opt-plist (car x))))
	  org-export-plist-vars)
    (org-set-local 'org-odd-levels-only odd)
    (setq umax (if arg (prefix-numeric-value arg)
		 org-export-headline-levels))
    (setq umax-toc umax)

    ;; File header
    (if title
	(insert
	 (org-export-generic-header title export-plist
				    :title-prefix
				    :title-format
				    :title-suffix)))

    (if (and (or author email)
	     (plist-get export-plist :author-export))
	(insert (concat (nth 1 lang-words) ": " (or author "")
			(if email (concat " <" email ">") "")
			"\n")))

    (cond
     ((and date (string-match "%" date))
      (setq date (format-time-string date)))
     (date)
     (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))

    (if (and date (plist-get export-plist :date-export))
	(insert
	 (org-export-generic-header date export-plist
				    :date-prefix
				    :date-format
				    :date-suffix)))

    ;; export the table of contents first
    (if (plist-get export-plist :toc-export)
	(progn
	  (push
	   (org-export-generic-header (nth 3 lang-words) export-plist
				      :toc-header-prefix
				      :toc-header-format
				      :toc-header-suffix)
	   thetoc)

	  (if tocprefix
	      (push tocprefix thetoc))

	  (mapc '(lambda (line)
		   (if (string-match org-todo-line-regexp line)
		       ;; This is a headline
		       (progn
			 (setq have-headings t)
			 (setq level (- (match-end 1) (match-beginning 1)
					level-offset)
			       level (org-tr-level level)
			       txt (match-string 3 line)
			       todo
			       (or (and org-export-mark-todo-in-toc
					(match-beginning 2)
					(not (member (match-string 2 line)
						     org-done-keywords)))
					; TODO, not DONE
				   (and org-export-mark-todo-in-toc
					(= level umax-toc)
					(org-search-todo-below
					 line lines level))))
			 (setq txt (org-html-expand-for-generic txt))

			 (while (string-match org-bracket-link-regexp txt)
			   (setq txt
				 (replace-match
				  (match-string (if (match-end 2) 3 1) txt)
				  t t txt)))

			 (if (and (not tagsintoc)
				  (string-match
				   (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
				   txt))
			     (setq txt (replace-match "" t t txt))
			   ; include tags but formated
			   (if (string-match
				(org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
				txt)
			       (progn
				 (setq 
				  toctags
				  (org-export-generic-header
				   (match-string 1 txt)
				   export-plist :toc-tags-prefix
				   :toc-tags-format :toc-tags-suffix))
				 (string-match
				  (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
				  txt)
				 (setq txt (replace-match "" t t txt)))
			     (setq toctags tocnotagsstr)))
			   
			 (if (string-match quote-re0 txt)
			     (setq txt (replace-match "" t t txt)))

			 (if (<= level umax-toc)
			     (progn
			       (push
				(concat

				 (make-string
				  (* (max 0 (- level org-min-level)) tocdepth)
				  tocindentchar)

				 (if tocsecnums
				     (format tocsecnumform
				      (org-section-number level))
				   "")

				 (format
				  (if todo tocformtodo tocformat) 
				  txt)

				 toctags)

				thetoc)
			       (setq org-last-level level))
			   ))))
		lines)
	  (if tocsuffix
	      (push tocsuffix thetoc))
	  (setq thetoc (if have-headings (nreverse thetoc) nil))))

    (org-init-section-numbers)
    (org-export-generic-check-section "top")
    (while (setq line (pop lines))
      (when (and link-buffer (string-match "^\\*+ " line))
	(org-export-generic-push-links (nreverse link-buffer))
	(setq link-buffer nil))
      (setq wrap nil)
      ;; Remove the quoted HTML tags.
      ;; XXX
      (setq line (org-html-expand-for-generic line))
      ;; Replace links with the description when possible
      ;; XXX
      (while (string-match org-bracket-link-regexp line)
	(setq link (match-string 1 line)
	      desc0 (match-string 3 line)
	      desc (or desc0 (match-string 1 line)))
	(if (and (> (length link) 8)
		 (equal (substring link 0 8) "coderef:"))
	    (setq line (replace-match
			(format (org-export-get-coderef-format (substring link 8) desc)
				(cdr (assoc
				      (substring link 8)
				      org-export-code-refs)))
			t t line))
	  (setq rpl (concat "[" 
			    (or (match-string 3 line) (match-string 1 line))
			    "]"))
	  (when (and desc0 (not (equal desc0 link)))
	    (if org-export-generic-links-to-notes
		(push (cons desc0 link) link-buffer)
	      (setq rpl (concat rpl " (" link ")")
		    wrap (+ (length line) (- (length (match-string 0 line)))
			    (length desc)))))
	  (setq line (replace-match rpl t t line))))
      (when custom-times
	(setq line (org-translate-time line)))
      (cond
       ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
	;;
	;; a Headline
	;;
	(org-export-generic-check-section "headline")

	(setq first-heading-pos (or first-heading-pos (point)))
	(setq level (org-tr-level (- (match-end 1) (match-beginning 1)
				     level-offset))
	      txt (match-string 2 line))
	(org-generic-level-start level old-level txt umax export-plist lines)
	(setq old-level level))

       ((and org-export-with-tables
	     (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
	;;
	;; a Table
	;;
	(org-export-generic-check-section "table")

	(if (not table-open)
	    ;; New table starts
	    (setq table-open t table-buffer nil))
	;; Accumulate table lines
	(setq table-buffer (cons line table-buffer))
	(when (or (not lines)
		  (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
				     (car lines))))
	  (setq table-open nil
		table-buffer (nreverse table-buffer))
	  (insert (mapconcat
		   (lambda (x)
		     (org-fix-indentation x org-generic-current-indentation))
		   (org-format-table-generic table-buffer)
		   "\n") "\n")))

       ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line)
	;;
	;; pre-formatted text
	;;
	(setq line (replace-match "\\1" nil nil line))

	(org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf)

	(insert (format bodyfixedform line)))

       ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line)
            ;; if the bullet list item is an asterisk, the leading space is /mandatory/
            ;; [2010/02/02:rpg]
            (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line))
	;;
	;; plain list item
	;; TODO: nested lists
	;;
        ;; first add a line break between any previous paragraph or line item and this
        ;; one
        (when bodynewline-paragraph
          (insert bodynewline-paragraph))

        ;; I believe this gets rid of leading whitespace.
	(setq line (replace-match "" nil nil line))

        ;; won't this insert the suffix /before/ the last line of the list?
        ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
        ;; unless 'org-empty-line-terminates-plain-lists' is true?
	(org-export-generic-check-section "liststart" listprefix listsuffix)

	;; deal with checkboxes
	(cond
	 ((string-match "^\\(\\[ \\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listchecktodo nil nil line)
			     listchecktodoend)))
	 ((string-match "^\\(\\[X\\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listcheckdone nil nil line)
			     listcheckdoneend)))
	 ((string-match "^\\(\\[/\\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listcheckhalf nil nil line)
			     listcheckhalfend)))
	 )

	(insert (format listformat (org-export-generic-fontify line))))
       ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line)
	;;
	;; numbered list item
	;;
	;; TODO: nested lists
	;;
	(setq line (replace-match (if numlistleavenum "\\2" "") nil nil line))

	(org-export-generic-check-section "numliststart"
					  numlistprefix numlistsuffix)

	;; deal with checkboxes
	;; TODO: whoops; leaving the numbers is a problem for ^ matching
	(cond
	 ((string-match "\\(\\[ \\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listchecktodo nil nil line)
			     listchecktodoend)))
	 ((string-match "\\(\\[X\\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listcheckdone nil nil line)
			     listcheckdoneend)))
	 ((string-match "\\(\\[/\\]\\)[ \t]*" line)
	  (setq line (concat (replace-match listcheckhalf nil nil line)
			     listcheckhalfend)))
	 )

	(insert (format numlistformat (org-export-generic-fontify line))))

       ((equal line "ORG-BLOCKQUOTE-START")
        (setq line blockquotestart))
       ((equal line "ORG-BLOCKQUOTE-END")
        (setq line blockquoteend))
       ((string-match "^\\s-*$" line)
        ;; blank line
        (if bodynewline-paragraph
            (insert bodynewline-paragraph)))
       (t
	;;
	;; body
	;;
	(org-export-generic-check-section "body" bodytextpre bodytextsuf)

        (setq line 
              (org-export-generic-fontify line))

	;; XXX: properties?  list?
	(if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
	    (setq line (replace-match "\\1\\3:" t nil line)))

	(setq line (org-fix-indentation line org-generic-current-indentation))

	;; Remove forced line breaks
	(if (string-match "\\\\\\\\[ \t]*$" line)
	    (setq line (replace-match "" t t line)))

	(if bodylinewrap
	    ;; XXX: was dependent on wrap var which was calculated by???
	    (if (> (length line) bodylinewrap)
		(setq line
		      (org-export-generic-wrap line bodylinewrap))
	      (setq line line)))
	(insert (format bodylineform line)))))

    ;; if we're at a level > 0; insert the closing body level stuff
    (let ((counter 0))
      (while (> (- level counter) 0)
	(insert
	 (org-export-generic-format export-plist :body-section-suffix 0
				    (- level counter)))
	(setq counter (1+ counter))))

    (org-export-generic-check-section "bottom")

    (org-export-generic-push-links (nreverse link-buffer))

    (normal-mode)

    ;; insert the table of contents
    (when thetoc
      (goto-char (point-min))
      (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
	  (progn
	    (goto-char (match-beginning 0))
	    (replace-match ""))
	(goto-char first-heading-pos))
      (mapc 'insert thetoc)
      (or (looking-at "[ \t]*\n[ \t]*\n")
	  (insert "\n\n")))

    ;; Convert whitespace place holders
    (goto-char (point-min))
    (let (beg end)
      (while (setq beg (next-single-property-change (point) 'org-whitespace))
	(setq end (next-single-property-change beg 'org-whitespace))
	(goto-char beg)
	(delete-region beg end)
	(insert (make-string (- end beg) ?\ ))))

    (save-buffer)

    ;; remove display and invisible chars
    (let (beg end)
      (goto-char (point-min))
      (while (setq beg (next-single-property-change (point) 'display))
	(setq end (next-single-property-change beg 'display))
	(delete-region beg end)
	(goto-char beg)
	(insert "=>"))
      (goto-char (point-min))
      (while (setq beg (next-single-property-change (point) 'org-cwidth))
	(setq end (next-single-property-change beg 'org-cwidth))
	(delete-region beg end)
	(goto-char beg)))
    (goto-char (point-min))))


(defun org-export-generic-format (export-plist prop &optional len n reverse)
  "converts a property specification to a string given types of properties

The EXPORT-PLIST should be defined as the lookup plist.
The PROP should be the property name to search for in it.
LEN is set to the length of multi-characters strings to generate (or 0)
N is the tree depth
REVERSE means to reverse the list if the plist match is a list
 "
  (let* ((prefixtype (plist-get export-plist prop))
	 subtype)
    (cond
     ((null prefixtype) "")
     ((and len (char-or-string-p prefixtype) (not (stringp prefixtype)))
      ;; sequence of chars
      (concat (make-string len prefixtype) "\n"))
     ((stringp prefixtype)
      prefixtype)
     ((and n (listp prefixtype))
      (if reverse
	  (setq prefixtype (reverse prefixtype)))
      (setq subtype (if (> n (length prefixtype))
			(car (last prefixtype))
		      (nth (1- n) prefixtype)))
      (if (stringp subtype)
	  subtype
	(concat (make-string len subtype) "\n")))
     (t ""))
    ))

(defun org-export-generic-header (header export-plist
				  prefixprop formatprop postfixprop
				  &optional n reverse)
  "convert a header to an output string given formatting property names"
  (let* ((formatspec (plist-get export-plist formatprop))
	 (len (length header)))
    (concat
     (org-export-generic-format export-plist prefixprop len n reverse)
     (format (or formatspec "%s") header)
     (org-export-generic-format export-plist postfixprop len n reverse))
    ))

(defun org-export-generic-preprocess (parameters)
  "Do extra work for ASCII export"
  ;; Put quotes around verbatim text
  (goto-char (point-min))
  (while (re-search-forward org-verbatim-re nil t)
    (goto-char (match-end 2))
    (backward-delete-char 1) (insert "'")
    (goto-char (match-beginning 2))
    (delete-char 1) (insert "`")
    (goto-char (match-end 2)))
  ;; Remove target markers
  (goto-char (point-min))
  (while (re-search-forward  "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
    (replace-match "\\1\\2")))

(defun org-html-expand-for-generic (line)
  "Handle quoted HTML for ASCII export."
  (if org-export-html-expand
      (while (string-match "@<[^<>\n]*>" line)
	;; We just remove the tags for now.
	(setq line (replace-match "" nil nil line))))
  line)

(defun org-export-generic-wrap (line where)
  "Wrap LINE at or before WHERE."
  (let* ((ind (org-get-indentation line))
	 (indstr (make-string ind ?\ ))
	 (len (length line))
	 (result "")
	 pos didfirst)
    (while (> len where)
      (catch 'found
	(loop for i from where downto (/ where 2) do
	  (and (equal (aref line i) ?\ )
	       (setq pos i)
	       (throw 'found t))))
      (if pos
	  (progn
	    (setq result
		  (concat result
			  (if didfirst indstr "")
			  (substring line 0 pos)
			  "\n"))
	    (setq didfirst t)
	    (setq line (substring line (1+ pos)))
	    (setq len (length line)))
	(setq result (concat result line))
	(setq len 0)))
    (concat result indstr line)))
			   
(defun org-export-generic-push-links (link-buffer)
  "Push out links in the buffer."
  (when link-buffer
    ;; We still have links to push out.
    (insert "\n")
    (let ((ind ""))
      (save-match-data
	(if (save-excursion
	      (re-search-backward
	       "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
	    (setq ind (or (match-string 2)
			  (make-string (length (match-string 3)) ?\ )))))
      (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
	    link-buffer))
    (insert "\n")))

(defun org-generic-level-start (level old-level title umax export-plist
				      &optional lines)
  "Insert a new level in a generic export."
  (let ((n (- level umax 1))
	(ind 0)
	(diff (- level old-level)) (counter 0)
	(secnums (plist-get export-plist :body-header-section-numbers))
	(secnumformat
	 (plist-get export-plist :body-header-section-number-format))
	char tagstring)
    (unless org-export-with-tags
      (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
	  (setq title (replace-match "" t t title))))

    (cond
     ;; going deeper
     ((> level old-level)
      (while (< (+ old-level counter) (1- level))
	(insert
	 (org-export-generic-format export-plist :body-section-prefix 0
				    (+ old-level counter)))
	(setq counter (1+ counter))
	))
     ;; going up
     ((< level old-level)
      (while (> (- old-level counter) (1- level))
	(insert
	 (org-export-generic-format export-plist :body-section-suffix 0
				    (- old-level counter)))
	(setq counter (1+ counter))
	))
     ;; same level
     ((= level old-level)
      (insert 
       (org-export-generic-format export-plist :body-section-suffix 0 level))
      )
     )
    (insert
     (org-export-generic-format export-plist :body-section-prefix 0 level))
    
    (if (and org-export-with-section-numbers
	     secnums
	     (or (not (numberp secnums))
		 (< level secnums)))
	(setq title
	      (concat (format (or secnumformat "%s ")
			      (org-section-number level)) title)))

    ;; handle tags and formatting
    (if (string-match
	 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title)
	(progn
	  (if (plist-get export-plist :body-tags-export)
	      (setq tagstring (org-export-generic-header (match-string 1 title)
							 export-plist
							 :body-tags-prefix
							 :body-tags-format
							 :body-tags-suffix)))
	  (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title)
	  (setq title (replace-match "" t t title)))
      (setq tagstring (plist-get export-plist :body-tags-none-string)))

    (insert
     (org-export-generic-header title export-plist
				:body-section-header-prefix
				:body-section-header-format
				:body-section-header-suffix
				level))
    (if tagstring
	(insert tagstring))

    (setq org-generic-current-indentation '(0 . 0))))

(defun org-insert-centered (s &optional underline)
  "Insert the string S centered and underline it with character UNDERLINE."
  (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
    (insert (make-string ind ?\ ) s "\n")
    (if underline
	(insert (make-string ind ?\ )
		(make-string (string-width s) underline)
		"\n"))))

(defvar org-table-colgroup-info nil)
(defun org-format-table-generic (lines)
  "Format a table for ascii export."
  (if (stringp lines)
      (setq lines (org-split-string lines "\n")))
  (if (not (string-match "^[ \t]*|" (car lines)))
      ;; Table made by table.el - test for spanning
      lines

    ;; A normal org table
    ;; Get rid of hlines at beginning and end
    (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
    (setq lines (nreverse lines))
    (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
    (setq lines (nreverse lines))
    (when org-export-table-remove-special-lines
      ;; Check if the table has a marking column.  If yes remove the
      ;; column and the special lines
      (setq lines (org-table-clean-before-export lines)))
    ;; Get rid of the vertical lines except for grouping
    (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
	  rtn line vl1 start)
      (while (setq line (pop lines))
	(if (string-match org-table-hline-regexp line)
	    (and (string-match "|\\(.*\\)|" line)
		 (setq line (replace-match " \\1" t nil line)))
	  (setq start 0 vl1 vl)
	  (while (string-match "|" line start)
	    (setq start (match-end 0))
	    (or (pop vl1) (setq line (replace-match " " t t line)))))
	(push line rtn))
      (nreverse rtn))))

(defun org-colgroup-info-to-vline-list (info)
  (let (vl new last)
    (while info
      (setq last new new (pop info))
      (if (or (memq last '(:end :startend))
	      (memq new  '(:start :startend)))
	  (push t vl)
	(push nil vl)))
    (setq vl (nreverse vl))
    (and vl (setcar vl nil))
    vl))


;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
(defvar org-export-generic-emphasis-alist
  '(("*" format-boldify nil)
    ("/" format-italicize nil)
    ("_" format-underline nil)
    ("+" format-strikethrough nil)
    ("=" format-code t)
    ("~" format-verbatim t))
  "Alist of org format -> formatting variables for fontification.
Each element of the list is a list of three elements.
The first element is the character used as a marker for fontification.
The second element is a variable name, set in org-export-generic.  That
variable will be dereferenced to obtain a formatting string to wrap 
fontified text with.
The third element decides whether to protect converted text from other
conversions.")

;;; Cargo-culted from the latex translation.  I couldn't figure out how
;;; to keep the structure since the generic export operates on lines, rather
;;; than on a buffer as in the latex export, meaning that none of the
;;; search forward code could be kept.  This led me to rewrite the
;;; whole thing recursively.  A huge lose for efficiency (potentially),
;;; but I couldn't figure out how to make the looping work.
;;; Worse, it's /doubly/ recursive, because this function calls
;;; org-export-generic-emph-format, which can call it recursively...
;;; [2010/05/20:rpg]
(defun org-export-generic-fontify (string)
  "Convert fontification according to generic rules."
  (if (string-match org-emph-re string)
        ;; The match goes one char after the *string*, except at the end of a line
        (let ((emph (assoc (match-string 3 string)
                           org-export-generic-emphasis-alist))
              (beg (match-beginning 0))
              (end (match-end 0)))
          (unless emph
            (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
                     (match-string 3 string)))
          ;; now we need to determine whether we have strikethrough or
          ;; a list, which is a bit nasty
          (if (and (equal (match-string 3 string) "+")
                   (save-match-data
                     (string-match "\\`-+\\'" (match-string 4 string))))
              ;; a list --- skip this match and recurse on the point after the
              ;; first emph char...
              (concat (substring string 0 (1+ (match-beginning 3)))
                      (org-export-generic-fontify (substring string (match-beginning 3))))
              (concat (substring string 0 beg) ;; part before the match
                      (match-string 1 string)
                      (org-export-generic-emph-format (second emph)
                                                      (match-string 4 string)
                                                      (third emph))
                      (or (match-string 5 string) "")
                      (org-export-generic-fontify (substring string end)))))
        string))

(defun org-export-generic-emph-format (format-varname string protect)
  "Return a string that results from applying the markup indicated by
FORMAT-VARNAME to STRING."
  (let ((format (symbol-value format-varname)))
    (let ((string-to-emphasize
           (if protect
               string
               (org-export-generic-fontify string))))
      (if format
          (format format string-to-emphasize)
          string-to-emphasize))))

(provide 'org-generic)
(provide 'org-export-generic)

;;; org-export-generic.el ends here