summaryrefslogtreecommitdiffstats
path: root/src/document/io/LilyPondExporter.cpp
blob: 0ffccffa510976d3f5d9538ab0a84c3cf94fbbc2 (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
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    This file is Copyright 2002
        Hans Kieserman      <hkieserman@mail.com>
    with heavy lifting from csoundio as it was on 13/5/2002.

    Numerous additions and bug fixes by
        Michael McIntyre    <dmmcintyr@users.sourceforge.net>

    Some restructuring by Chris Cannam.

    Massive brain surgery, fixes, improvements, and additions by
        Heikki Junes

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "LilyPondExporter.h"

#include <klocale.h>
#include "misc/Debug.h"
#include "misc/Strings.h"
#include "document/ConfigGroups.h"
#include "base/BaseProperties.h"
#include "base/Composition.h"
#include "base/Configuration.h"
#include "base/Event.h"
#include "base/Exception.h"
#include "base/Instrument.h"
#include "base/NotationTypes.h"
#include "base/PropertyName.h"
#include "base/Segment.h"
#include "base/SegmentNotationHelper.h"
#include "base/Sets.h"
#include "base/Staff.h"
#include "base/Studio.h"
#include "base/Track.h"
#include "base/NotationQuantizer.h"
#include "base/Marker.h"
#include "base/StaffExportTypes.h"
#include "document/RosegardenGUIDoc.h"
#include "gui/application/RosegardenApplication.h"
#include "gui/application/RosegardenGUIView.h"
#include "gui/editors/notation/NotationProperties.h"
#include "gui/editors/notation/NotationView.h"
#include "gui/editors/guitar/Chord.h"
#include "gui/general/ProgressReporter.h"
#include "gui/widgets/CurrentProgressDialog.h"
#include <kconfig.h>
#include <kmessagebox.h>
#include <tqfileinfo.h>
#include <tqobject.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqtextcodec.h>
#include <kapplication.h>
#include <sstream>
#include <algorithm>

namespace Rosegarden
{

using namespace BaseProperties;

const PropertyName LilyPondExporter::SKIP_PROPERTY
    = "LilyPondExportSkipThisEvent";

LilyPondExporter::LilyPondExporter(RosegardenGUIApp *parent,
                                   RosegardenGUIDoc *doc,
                                   std::string fileName) :
        ProgressReporter((TQObject *)parent, "lilypondExporter"),
        m_doc(doc),
        m_fileName(fileName),
        m_lastClefFound(Clef::Treble)
{
    m_composition = &m_doc->getComposition();
    m_studio = &m_doc->getStudio();
    m_view = ((RosegardenGUIApp *)parent)->getView();
    m_notationView = NULL;

    readConfigVariables();
}

LilyPondExporter::LilyPondExporter(NotationView *parent,
                                   RosegardenGUIDoc *doc,
                                   std::string fileName) :
        ProgressReporter((TQObject *)parent, "lilypondExporter"),
        m_doc(doc),
        m_fileName(fileName),
        m_lastClefFound(Clef::Treble)

{
    m_composition = &m_doc->getComposition();
    m_studio = &m_doc->getStudio();
    m_view = NULL;
    m_notationView = ((NotationView *)parent);

    readConfigVariables();
}

void
LilyPondExporter::readConfigVariables(void)
{
    // grab config info
    KConfig *cfg = kapp->config();
    cfg->setGroup(NotationViewConfigGroup);

    m_paperSize = cfg->readUnsignedNumEntry("lilypapersize", PAPER_A4);
    m_paperLandscape = cfg->readBoolEntry("lilypaperlandscape", false);
    m_fontSize = cfg->readUnsignedNumEntry("lilyfontsize", FONT_20);
    m_raggedBottom = cfg->readBoolEntry("lilyraggedbottom", false);
    m_exportSelection = cfg->readUnsignedNumEntry("lilyexportselection", EXPORT_NONMUTED_TRACKS);
    m_exportLyrics = cfg->readBoolEntry("lilyexportlyrics", true);
    m_exportMidi = cfg->readBoolEntry("lilyexportmidi", false);
    m_exportTempoMarks = cfg->readUnsignedNumEntry("lilyexporttempomarks", EXPORT_NONE_TEMPO_MARKS);
    m_exportPointAndClick = cfg->readBoolEntry("lilyexportpointandclick", false);
    m_exportBeams = cfg->readBoolEntry("lilyexportbeamings", false);
    m_exportStaffMerge = cfg->readBoolEntry("lilyexportstaffmerge", false);
    m_exportStaffGroup = cfg->readBoolEntry("lilyexportstaffbrackets", true);
    m_lyricsHAlignment = cfg->readBoolEntry("lilylyricshtqalignment", LEFT_ALIGN);

    m_languageLevel = cfg->readUnsignedNumEntry("lilylanguage", LILYPOND_VERSION_2_6);
    m_exportMarkerMode = cfg->readUnsignedNumEntry("lilyexportmarkermode", EXPORT_NO_MARKERS );
}

LilyPondExporter::~LilyPondExporter()
{
    // nothing
}

void
LilyPondExporter::handleStartingEvents(eventstartlist &eventsToStart,
                                       std::ofstream &str)
{
    eventstartlist::iterator m = eventsToStart.begin();

    while (m != eventsToStart.end()) {

        try {
            Indication i(**m);

            if (i.getIndicationType() == Indication::Slur) {
                if ((*m)->get
                        <Bool>(NotationProperties::SLUR_ABOVE))
                    str << "^( ";
                else
                    str << "_( ";
            } else if (i.getIndicationType() == Indication::PhrasingSlur) {
                if ((*m)->get
                        <Bool>(NotationProperties::SLUR_ABOVE))
                    str << "^\\( ";
                else
                    str << "_\\( ";
            } else if (i.getIndicationType() == Indication::Crescendo) {
                str << "\\< ";
            } else if (i.getIndicationType() == Indication::Decrescendo) {
                str << "\\> ";
            }

        } catch (Event::BadType) {
            // Not an indication
        } catch (Event::NoData e) {
            std::cerr << "Bad indication: " << e.getMessage() << std::endl;
        }

        eventstartlist::iterator n(m);
        ++n;
        eventsToStart.erase(m);
        m = n;
    }
}

void
LilyPondExporter::handleEndingEvents(eventendlist &eventsInProgress,
                                     const Segment::iterator &j,
                                     std::ofstream &str)
{
    eventendlist::iterator k = eventsInProgress.begin();

    while (k != eventsInProgress.end()) {

        eventendlist::iterator l(k);
        ++l;

        // Handle and remove all the relevant events in progress
        // This assumes all deferred events are indications

        try {
            Indication i(**k);

            timeT indicationEnd =
                (*k)->getNotationAbsoluteTime() + i.getIndicationDuration();
            timeT eventEnd =
                (*j)->getNotationAbsoluteTime() + (*j)->getNotationDuration();

            if (indicationEnd < eventEnd ||
                    ((i.getIndicationType() == Indication::Slur ||
                      i.getIndicationType() == Indication::PhrasingSlur) &&
                     indicationEnd == eventEnd)) {

                if (i.getIndicationType() == Indication::Slur) {
                    str << ") ";
                } else if (i.getIndicationType() == Indication::PhrasingSlur) {
                    str << "\\) ";
                } else if (i.getIndicationType() == Indication::Crescendo ||
                           i.getIndicationType() == Indication::Decrescendo) {
                    str << "\\! ";
                }

                eventsInProgress.erase(k);
            }

        } catch (Event::BadType) {
            // not an indication

        } catch (Event::NoData e) {
            std::cerr << "Bad indication: " << e.getMessage() << std::endl;
        }

        k = l;
    }
}

std::string
LilyPondExporter::convertPitchToLilyNote(int pitch, Accidental accidental,
        const Rosegarden::Key &key)
{
    Pitch p(pitch, accidental);
    std::string lilyNote = "";

    lilyNote += (char)tolower(p.getNoteName(key));
    //    std::cout << "lilyNote: " << lilyNote << std::endl;
    Accidental acc = p.getAccidental(key);
    if (acc == Accidentals::DoubleFlat)
        lilyNote += "eses";
    else if (acc == Accidentals::Flat)
        lilyNote += "es";
    else if (acc == Accidentals::Sharp)
        lilyNote += "is";
    else if (acc == Accidentals::DoubleSharp)
        lilyNote += "isis";

    return lilyNote;
}

std::string
LilyPondExporter::composeLilyMark(std::string eventMark, bool stemUp)
{

    std::string inStr = "", outStr = "";
    std::string prefix = (stemUp) ? "_" : "^";

    // shoot text mark straight through unless it's sf or rf
    if (Marks::isTextMark(eventMark)) {
        inStr = protectIllegalChars(Marks::getTextFromMark(eventMark));

        if (inStr == "sf") {
            inStr = "\\sf";
        } else if (inStr == "rf") {
            inStr = "\\rfz";
        } else {
            inStr = "\\markup { \\italic " + inStr + " } ";
        }

        outStr = prefix + inStr;

    } else if (Marks::isFingeringMark(eventMark)) {

        // fingering marks: use markup syntax only for non-trivial fingerings

        inStr = protectIllegalChars(Marks::getFingeringFromMark(eventMark));

        if (inStr != "0" && inStr != "1" && inStr != "2" && inStr != "3" && inStr != "4" && inStr != "5" && inStr != "+" ) {
            inStr = "\\markup { \\finger \"" + inStr + "\" } ";
        }

        outStr = prefix + inStr;

    } else {
        outStr = "-";

        // use full \accent format for everything, even though some shortcuts
        // exist, for the sake of consistency
        if (eventMark == Marks::Accent) {
            outStr += "\\accent";
        } else if (eventMark == Marks::Tenuto) {
            outStr += "\\tenuto";
        } else if (eventMark == Marks::Staccato) {
            outStr += "\\staccato";
        } else if (eventMark == Marks::Staccatissimo) {
            outStr += "\\staccatissimo";
        } else if (eventMark == Marks::Marcato) {
            outStr += "\\marcato";
        } else if (eventMark == Marks::Trill) {
            outStr += "\\trill";
        } else if (eventMark == Marks::LongTrill) {
            // span trill up to the next note:
            // tweak the beginning of the next note using an invisible rest having zero length
            outStr += "\\startTrillSpan s4*0 \\stopTrillSpan";
        } else if (eventMark == Marks::Turn) {
            outStr += "\\turn";
        } else if (eventMark == Marks::Pause) {
            outStr += "\\fermata";
        } else if (eventMark == Marks::UpBow) {
            outStr += "\\upbow";
        } else if (eventMark == Marks::DownBow) {
            outStr += "\\downbow";
        } else {
            outStr = "";
            std::cerr << "LilyPondExporter::composeLilyMark() - unhandled mark:  "
            << eventMark << std::endl;
        }
    }

    return outStr;
}

std::string
LilyPondExporter::indent(const int &column)
{
    std::string outStr = "";
    for (int c = 1; c <= column; c++) {
        outStr += "    ";
    }
    return outStr;
}

std::string
LilyPondExporter::protectIllegalChars(std::string inStr)
{

    TQString tmpStr = strtoqstr(inStr);

    tmpStr.replace(TQRegExp("&"), "\\&");
    tmpStr.replace(TQRegExp("\\^"), "\\^");
    tmpStr.replace(TQRegExp("%"), "\\%");
    tmpStr.replace(TQRegExp("<"), "\\<");
    tmpStr.replace(TQRegExp(">"), "\\>");
    tmpStr.replace(TQRegExp("\\["), "");
    tmpStr.replace(TQRegExp("\\]"), "");
    tmpStr.replace(TQRegExp("\\{"), "");
    tmpStr.replace(TQRegExp("\\}"), "");

    //
    // LilyPond uses utf8 encoding.
    //
    return tmpStr.utf8().data();
}

struct MarkerComp {
    // Sort Markers by time
    // Perhaps this should be made generic with a template?
    bool operator()( Marker *a, Marker *b ) { 
        return a->getTime() < b->getTime();
    }
};

bool
LilyPondExporter::write()
{
    TQString tmpName = strtoqstr(m_fileName);

    // dmm - modified to act upon the filename itself, rather than the whole
    // path; fixes bug #855349

    // split name into parts:
    TQFileInfo nfo(tmpName);
    TQString dirName = nfo.dirPath();
    TQString baseName = nfo.fileName();

    // sed LilyPond-choking chars out of the filename proper
    bool illegalFilename = (baseName.contains(' ') || baseName.contains("\\"));
    baseName.replace(TQRegExp(" "), "");
    baseName.replace(TQRegExp("\\\\"), "");
    baseName.replace(TQRegExp("'"), "");
    baseName.replace(TQRegExp("\""), "");

    // cat back together
    tmpName = dirName + '/' + baseName;

    if (illegalFilename) {
        CurrentProgressDialog::freeze();
        int reply = KMessageBox::warningContinueCancel(
                        0, i18n("LilyPond does not allow spaces or backslashes in filenames.\n\n"
                                "Would you like to use\n\n %1\n\n instead?").tqarg(baseName));
        if (reply != KMessageBox::Continue)
            return false;
    }

    std::ofstream str(qstrtostr(tmpName).c_str(), std::ios::out);
    if (!str) {
        std::cerr << "LilyPondExporter::write() - can't write file " << tmpName.ascii() << std::endl;
        return false;
    }

    str << "% This LilyPond file was generated by Rosegarden " << protectIllegalChars(VERSION) << std::endl;

    switch (m_languageLevel) {

    case LILYPOND_VERSION_2_6:
        str << "\\version \"2.6.0\"" << std::endl;
        break;

    case LILYPOND_VERSION_2_8:
        str << "\\version \"2.8.0\"" << std::endl;
        break;

    case LILYPOND_VERSION_2_10:
        str << "\\version \"2.10.0\"" << std::endl;
        break;

    case LILYPOND_VERSION_2_12:
        str << "\\version \"2.12.0\"" << std::endl;
        break;

    default:
        // force the default version if there was an error
        std::cerr << "ERROR: Unknown language level " << m_languageLevel
	    << ", using \\version \"2.6.0\" instead" << std::endl;
        str << "\\version \"2.6.0\"" << std::endl;
        m_languageLevel = LILYPOND_VERSION_2_6;
    }

    // enable "point and click" debugging via pdf to make finding the
    // unfortunately inevitable errors easier
    if (m_exportPointAndClick) {
        str << "% point and click debugging is enabled" << std::endl;
    } else {
        str << "% point and click debugging is disabled" << std::endl;
        str << "#(ly:set-option 'point-and-click #f)" << std::endl;
    }

    // LilyPond \header block

    // set indention level to make future changes to horizontal tqlayout less
    // tedious, ++col to indent a new level, --col to de-indent
    int col = 0;

    // grab user headers from metadata
    Configuration metadata = m_composition->getMetadata();
    std::vector<std::string> propertyNames = metadata.getPropertyNames();

    // open \header section if there's metadata to grab, and if the user
    // wishes it
    if (!propertyNames.empty()) {
        str << "\\header {" << std::endl;
        col++;  // indent+

        bool userTagline = false;

        for (unsigned int index = 0; index < propertyNames.size(); ++index) {
            std::string property = propertyNames [index];
            if (property == headerDedication || property == headerTitle ||
                    property == headerSubtitle || property == headerSubsubtitle ||
                    property == headerPoet || property == headerComposer ||
                    property == headerMeter || property == headerOpus ||
                    property == headerArranger || property == headerInstrument ||
                    property == headerPiece || property == headerCopyright ||
                    property == headerTagline) {
                std::string header = protectIllegalChars(metadata.get<String>(property));
                if (header != "") {
                    str << indent(col) << property << " = \"" << header << "\"" << std::endl;
                    // let users override defaults, but allow for providing
                    // defaults if they don't:
                    if (property == headerTagline)
                        userTagline = true;
                }
            }
        }

        // default tagline
        if (!userTagline) {
            str << indent(col) << "tagline = \""
            << "Created using Rosegarden " << protectIllegalChars(VERSION) << " and LilyPond"
            << "\"" << std::endl;
        }

        // close \header
        str << indent(--col) << "}" << std::endl;
    }

    // LilyPond \paper block (optional)
    if (m_raggedBottom) {
        str << indent(col) << "\\paper {" << std::endl;
        str << indent(++col) << "ragged-bottom=##t" << std::endl;
        str << indent(--col) << "}" << std::endl;
    }

    // LilyPond music data!   Mapping:
    // LilyPond Voice = Rosegarden Segment
    // LilyPond Staff = Rosegarden Track
    // (not the cleanest output but maybe the most reliable)

    // paper/font sizes
    int font;
    switch (m_fontSize) {
    case 0 :
        font = 11;
        break;
    case 1 :
        font = 13;
        break;
    case 2 :
        font = 16;
        break;
    case 3 :
        font = 19;
        break;
    case 4 :
        font = 20;
        break;
    case 5 :
        font = 23;
        break;
    case 6 :
        font = 26;
        break;
    default :
	font = 20; // if config problem
    }

    str << indent(col) << "#(set-global-staff-size " << font << ")" << std::endl;

    // write user-specified paper type as default paper size
    std::string paper = "";
    switch (m_paperSize) {
    case PAPER_A3 :
        paper += "a3";
        break;
    case PAPER_A4 :
        paper += "a4";
        break;
    case PAPER_A5 :
        paper += "a5";
        break;
    case PAPER_A6 :
        paper += "a6";
        break;
    case PAPER_LEGAL :
        paper += "legal";
        break;
    case PAPER_LETTER :
        paper += "letter";
        break;
    case PAPER_TABLOID :
        paper += "tabloid";
        break;
    case PAPER_NONE :
        paper = "";
        break; // "do not specify"
    }
    if (paper != "") {
        str << indent(col) << "#(set-default-paper-size \"" << paper << "\"" 
	    << (m_paperLandscape ? " 'landscape" : "") << ")"
	    << std::endl;
    }

    // Find out the printed length of the composition
    Composition::iterator i = m_composition->begin();
    if ((*i) == NULL) {
        str << indent(col) << "\\score {" << std::endl;
        str << indent(++col) << "% no segments found" << std::endl;
        // bind staffs with or without staff group bracket
        str << indent(col) // indent
	    << "<<" << " s4 " << ">>" << std::endl;
        str << indent(col) << "\\tqlayout { }" << std::endl;
        str << indent(--col) << "}" << std::endl;
        return true;
    }
    timeT compositionStartTime = (*i)->getStartTime();
    timeT compositionEndTime = (*i)->getEndMarkerTime();
    for (; i != m_composition->end(); ++i) {
        if (compositionStartTime > (*i)->getStartTime() && (*i)->getTrack() >= 0) {
            compositionStartTime = (*i)->getStartTime();
        }
        if (compositionEndTime < (*i)->getEndMarkerTime()) {
            compositionEndTime = (*i)->getEndMarkerTime();
        }
    }

    // define global context which is common for all staffs
    str << indent(col++) << "global = { " << std::endl;
    TimeSignature timeSignature = m_composition->
                                  getTimeSignatureAt(m_composition->getStartMarker());
    if (m_composition->getBarStart(m_composition->getBarNumber(compositionStartTime)) < compositionStartTime) {
        str << indent(col) << "\\partial ";
        // Arbitrary partial durations are handled by the following way:
        // split the partial duration to 64th notes: instead of "4" write "64*16". (hjj)
        Note partialNote = Note::getNearestNote(1, MAX_DOTS);
        int partialDuration = m_composition->getBarStart(m_composition->getBarNumber(compositionStartTime) + 1) - compositionStartTime;
        writeDuration(1, str);
        str << "*" << ((int)(partialDuration / partialNote.getDuration()))
        << std::endl;
    }
    int leftBar = 0;
    int rightBar = leftBar;
    do {
        bool isNew = false;
        m_composition->getTimeSignatureInBar(rightBar + 1, isNew);

        if (isNew || (m_composition->getBarStart(rightBar + 1) >= compositionEndTime)) {
            //  - set initial time signature; further time signature changes
            //    are defined within the segments, because they may be hidden
            str << indent(col) << (leftBar == 0 ? "" : "% ") << "\\time "
            << timeSignature.getNumerator() << "/"
            << timeSignature.getDenominator() << std::endl;
            //  - place skips upto the end of the composition;
            //    this justifies the printed staffs
            str << indent(col);
            timeT leftTime = m_composition->getBarStart(leftBar);
            timeT rightTime = m_composition->getBarStart(rightBar + 1);
            if (leftTime < compositionStartTime) {
                leftTime = compositionStartTime;
            }
            writeSkip(timeSignature, leftTime, rightTime - leftTime, false, str);
            str << " %% " << (leftBar + 1) << "-" << (rightBar + 1) << std::endl;

            timeSignature = m_composition->getTimeSignatureInBar(rightBar + 1, isNew);
            leftBar = rightBar + 1;
        }
    } while (m_composition->getBarStart(++rightBar) < compositionEndTime);
    str << indent(--col) << "}" << std::endl;

    // time signatures changes are in segments, reset initial value
    timeSignature = m_composition->
                    getTimeSignatureAt(m_composition->getStartMarker());

    // All the tempo changes are included in "globalTempo" context.
    // This context contains only skip notes between the tempo changes.
    // First tempo marking should still be include in \midi{ } block.
    // If tempo marks are printed in future, they should probably be
    // included in this context and the note duration in the tempo
    // mark should be according to the time signature. (hjj)
    int tempoCount = m_composition->getTempoChangeCount();

    if (tempoCount > 0) {

        timeT prevTempoChangeTime = m_composition->getStartMarker();
        int tempo = int(Composition::getTempoQpm(m_composition->getTempoAtTime(prevTempoChangeTime)));
        bool tempoMarksInvisible = false;

        str << indent(col++) << "globalTempo = {" << std::endl;
        if (m_exportTempoMarks == EXPORT_NONE_TEMPO_MARKS && tempoMarksInvisible == false) {
            str << indent(col) << "\\override Score.MetronomeMark #'transparent = ##t" << std::endl;
            tempoMarksInvisible = true;
        }
        str << indent(col) << "\\tempo 4 = " << tempo << "  ";
        int prevTempo = tempo;

        for (int i = 0; i < tempoCount; ++i) {

            std::pair<timeT, long> tempoChange =
                m_composition->getTempoChange(i);

            timeT tempoChangeTime = tempoChange.first;

            tempo = int(Composition::getTempoQpm(tempoChange.second));

            // First tempo change may be before the first segment.
            // Do not apply it before the first segment appears.
            if (tempoChangeTime < compositionStartTime) {
                tempoChangeTime = compositionStartTime;
            } else if (tempoChangeTime >= compositionEndTime) {
                tempoChangeTime = compositionEndTime;
            }
            if (prevTempoChangeTime < compositionStartTime) {
                prevTempoChangeTime = compositionStartTime;
            } else if (prevTempoChangeTime >= compositionEndTime) {
                prevTempoChangeTime = compositionEndTime;
            }
            writeSkip(m_composition->getTimeSignatureAt(tempoChangeTime),
                      tempoChangeTime, tempoChangeTime - prevTempoChangeTime, false, str);
            // add new \tempo only if tempo was changed
            if (tempo != prevTempo) {
                if (m_exportTempoMarks == EXPORT_FIRST_TEMPO_MARK && tempoMarksInvisible == false) {
                    str << std::endl << indent(col) << "\\override Score.MetronomeMark #'transparent = ##t";
                    tempoMarksInvisible = true;
                }
                str << std::endl << indent(col) << "\\tempo 4 = " << tempo << "  ";
            }

            prevTempo = tempo;
            prevTempoChangeTime = tempoChangeTime;
            if (prevTempoChangeTime == compositionEndTime)
                break;
        }
        // First tempo change may be before the first segment.
        // Do not apply it before the first segment appears.
        if (prevTempoChangeTime < compositionStartTime) {
            prevTempoChangeTime = compositionStartTime;
        }
        writeSkip(m_composition->getTimeSignatureAt(prevTempoChangeTime),
                  prevTempoChangeTime, compositionEndTime - prevTempoChangeTime, false, str);
        str << std::endl;
        str << indent(--col) << "}" << std::endl;
    }
    // Markers
    // Skip until marker, make sure there's only one marker per measure
    if ( m_exportMarkerMode != EXPORT_NO_MARKERS ) {
        str << indent(col++) << "markers = {" << std::endl;
        timeT prevMarkerTime = 0;

        // Need the markers sorted by time
        Composition::markercontainer markers( m_composition->getMarkers() ); // copy
        std::sort( markers.begin(), markers.end(), MarkerComp() );
        Composition::markerconstiterator i_marker = markers.begin();

        while  ( i_marker != markers.end() ) {
            timeT markerTime = m_composition->getBarStartForTime((*i_marker)->getTime());
            RG_DEBUG << "Marker: " << (*i_marker)->getTime() << " previous: " << prevMarkerTime << endl;
            // how to cope with time signature changes?
            if ( markerTime > prevMarkerTime ) {
                str << indent(col);
                writeSkip(m_composition->getTimeSignatureAt(markerTime),
                        markerTime, markerTime - prevMarkerTime, false, str);
                str << "\\mark "; 
                switch (m_exportMarkerMode) {
                    case EXPORT_DEFAULT_MARKERS:
                        // Use the marker name for text
                        str << "\\default %% " << (*i_marker)->getName() << std::endl;
                        break;
                    case EXPORT_TEXT_MARKERS:
                        // Raise the text above the staff as not to clash with the other stuff
                        str << "\\markup { \\hspace #0 \\raise #1.5 \"" << (*i_marker)->getName() << "\" }" << std::endl;
                        break;
                    default:
                        break;
                }
                prevMarkerTime = markerTime;
            }
            ++i_marker;
        }
        str << indent(--col) << "}" << std::endl;
    }

    // open \score section
    str << "\\score {" << std::endl;
    
    int lastTrackIndex = -1;
    int voiceCounter = 0;
    bool firstTrack = true;
    int staffGroupCounter = 0;
    int pianoStaffCounter = 0;
    int bracket = 0;
    int prevBracket = -1;

    // Write out all segments for each Track, in track order.
    // This involves a hell of a lot of loops through all tracks
    // and segments, but the time spent doing that should still
    // be relatively small in the greater scheme.

    Track *track = 0;

    for (int trackPos = 0;
            (track = m_composition->getTrackByPosition(trackPos)) != 0; ++trackPos) {

        for (Composition::iterator i = m_composition->begin();
                i != m_composition->end(); ++i) {

            if ((*i)->getTrack() != track->getId())
                continue;

	    // handle the bracket(s) for the first track, and if no brackets
	    // present, open with a <<
	    prevBracket = bracket;
	    bracket = track->getStaffBracket();

            //!!! how will all these indentions work out?  Probably not well,
	    // but maybe if users always provide sensible input, this will work
	    // out sensibly.  Maybe.  If not, we'll need some tracking gizmos to
	    // figure out the indention, or just skip the indention for these or
	    // something.  TBA.
	    if (firstTrack) {
	        // seems to be common to every case now
	        str << indent(col++) << "<< % common" << std::endl;
            }

            if (firstTrack && m_exportStaffGroup) {

		if (bracket == Brackets::SquareOn) {
		    str << indent(col++) << "\\context StaffGroup = \"" << staffGroupCounter++
		        << "\" << " << std::endl; //indent+
		} else if (bracket == Brackets::CurlyOn) {
		    str << indent(col++) << "\\context PianoStaff = \"" << pianoStaffCounter++
		        << "\" << " << std::endl; //indent+
		} else if (bracket == Brackets::CurlySquareOn) {
		    str << indent(col++) << "\\context StaffGroup = \"" << staffGroupCounter++
		        << "\" << " << std::endl; //indent+
		    str << indent(col++) << "\\context PianoStaff = \"" << pianoStaffCounter++
		        << "\" << " << std::endl; //indent+
		}

		// Make chords offset colliding notes by default (only write for
		// first track)
		str << indent(++col) << "% force offset of colliding notes in chords:"
		    << std::endl;
		str << indent(col)   << "\\override Score.NoteColumn #\'force-hshift = #1.0"
		    << std::endl;
	    }

            emit setProgress(int(double(trackPos) /
                                 double(m_composition->getNbTracks()) * 100.0));
            rgapp->refreshGUI(50);
            
            bool currentSegmentSelected = false;
            if ((m_exportSelection == EXPORT_SELECTED_SEGMENTS) && 
		(m_view != NULL) && (m_view->haveSelection())) {
            	//
            	// Check whether the current segment is in the list of selected segments.
            	//
            	SegmentSelection selection = m_view->getSelection();
                for (SegmentSelection::iterator it = selection.begin(); it != selection.end(); it++) {
                    if ((*it) == (*i)) currentSegmentSelected = true;
                }
            } else if ((m_exportSelection == EXPORT_SELECTED_SEGMENTS) && (m_notationView != NULL)) {
		currentSegmentSelected = m_notationView->hasSegment(*i);
	    }

	    // Check whether the track is a non-midi track.
	    InstrumentId instrumentId = track->getInstrument();
	    bool isMidiTrack = instrumentId >= MidiInstrumentBase;
    
            if (isMidiTrack && ( // Skip non-midi tracks.
		(m_exportSelection == EXPORT_ALL_TRACKS) || 
                ((m_exportSelection == EXPORT_NONMUTED_TRACKS) && (!track->isMuted())) ||
                ((m_exportSelection == EXPORT_SELECTED_TRACK) && (m_view != NULL) &&
		 (track->getId() == m_composition->getSelectedTrack())) ||
                ((m_exportSelection == EXPORT_SELECTED_TRACK) && (m_notationView != NULL) &&
		 (track->getId() == m_notationView->getCurrentSegment()->getTrack())) ||
                ((m_exportSelection == EXPORT_SELECTED_SEGMENTS) && (currentSegmentSelected)))) {
                if ((int) (*i)->getTrack() != lastTrackIndex) {
                    if (lastTrackIndex != -1) {
                        // close the old track (Staff context)
			str << indent(--col) << ">> % Staff ends" << std::endl; //indent-
                    }
                    lastTrackIndex = (*i)->getTrack();


		    // handle any necessary bracket closures with a rude
		    // hack, because bracket closures need to be handled
		    // right under staff closures, but at this point in the
		    // loop we are one track too early for closing, so we use
		    // the bracket setting for the previous track for closing
		    // purposes (I'm not quite sure why this works, but it does)			
                     if (m_exportStaffGroup) {
                          if (prevBracket == Brackets::SquareOff ||
                              prevBracket == Brackets::SquareOnOff) {
                              str << indent(--col) << ">> % StaffGroup " << staffGroupCounter
                                   << std::endl; //indent-
                          } else if (prevBracket == Brackets::CurlyOff) {
                              str << indent(--col) << ">> % PianoStaff " << pianoStaffCounter
                                   << std::endl; //indent-
                          } else if (prevBracket == Brackets::CurlySquareOff) {
                              str << indent(--col) << ">> % PianoStaff " << pianoStaffCounter
                                   << std::endl; //indent-
                              str << indent(--col) << ">> % StaffGroup " << staffGroupCounter
                                   << std::endl; //indent-
                          }
		    }

                     // handle any bracket start events (unless track staff
                     // brackets are being ignored, as when printing single parts
                     // out of a bigger score one by one)
                     if (!firstTrack && m_exportStaffGroup) {
		        if (bracket == Brackets::SquareOn ||
			    bracket == Brackets::SquareOnOff) {
			    str << indent(col++) << "\\context StaffGroup = \""
			        << ++staffGroupCounter << "\" <<" << std::endl;
			} else if (bracket == Brackets::CurlyOn) {
			    str << indent(col++) << "\\context PianoStaff = \""
			        << ++pianoStaffCounter << "\" <<" << std::endl;
			} else if (bracket == Brackets::CurlySquareOn) {
			    str << indent(col++) << "\\context StaffGroup = \""
			        << ++staffGroupCounter << "\" <<" << std::endl;
			    str << indent(col++) << "\\context PianoStaff = \""
			        << ++pianoStaffCounter << "\" <<" << std::endl;
			}
		    } 

                    // avoid problem with <untitled> tracks yielding a
                    // .ly file that jumbles all notes together on a
                    // single staff...  every Staff context has to
                    // have a unique name, even if the
                    // Staff.instrument property is the same for
                    // multiple staffs...
                    // Added an option to merge staffs with the same, non-empty
                    // name. This option makes it possible to produce staffs
                    // with polyphonic, and polyrhytmic, music. Polyrhytmic
                    // music in a single staff is typical in piano, or
                    // guitar music. (hjj)
                    // In the case of colliding note heads, user may define
                    //  - DISPLACED_X -- for a note/chord
                    //  - INVISIBLE -- for a rest
                    std::ostringstream staffName;
                    staffName << protectIllegalChars(m_composition->
                                                     getTrackById(lastTrackIndex)->getLabel());

                    if (!m_exportStaffMerge || staffName.str() == "") {
                        str << std::endl << indent(col)
                        << "\\context Staff = \"track "
                        << (trackPos + 1) << "\" ";
                    } else {
                        str << std::endl << indent(col)
                        << "\\context Staff = \"" << staffName.str()
                        << "\" ";
                    }

                    str << "<< " << std::endl;

		    // The octavation is omitted in the instrument name.
		    // HJJ: Should it be automatically added to the clef: G^8 ?
		    // What if two segments have different transpose in a track?
                    std::ostringstream staffNameWithTranspose;
		    staffNameWithTranspose << "\\markup { \\column { \"" << staffName.str() << " \"";
		    if (((*i)->getTranspose() % 12) != 0) {
			staffNameWithTranspose << " \\line { ";
			switch ((*i)->getTranspose() % 12) {
			case 1 : staffNameWithTranspose << "\"in D\" \\smaller \\flat"; break;
			case 2 : staffNameWithTranspose << "\"in D\""; break;
			case 3 : staffNameWithTranspose << "\"in E\" \\smaller \\flat"; break;
			case 4 : staffNameWithTranspose << "\"in E\""; break;
			case 5 : staffNameWithTranspose << "\"in F\""; break;
			case 6 : staffNameWithTranspose << "\"in G\" \\smaller \\flat"; break;
			case 7 : staffNameWithTranspose << "\"in G\""; break;
			case 8 : staffNameWithTranspose << "\"in A\" \\smaller \\flat"; break;
			case 9 : staffNameWithTranspose << "\"in A\""; break;
			case 10 : staffNameWithTranspose << "\"in B\" \\smaller \\flat"; break;
			case 11 : staffNameWithTranspose << "\"in B\""; break;
			}
			staffNameWithTranspose << " }";
		    }
		    staffNameWithTranspose << " } }";
		    if (m_languageLevel < LILYPOND_VERSION_2_10) {
			str << indent(++col) << "\\set Staff.instrument = " << staffNameWithTranspose.str()
			    << std::endl;
		    } else {
			str << indent(++col) << "\\set Staff.instrumentName = "
			    << staffNameWithTranspose.str() << std::endl;
		    }

                    if (m_exportMidi) {
                        // Set midi instrument for the Staff
                        std::ostringstream staffMidiName;
                        Instrument *instr = m_studio->getInstrumentById(
			        m_composition->getTrackById(lastTrackIndex)->getInstrument());
                        staffMidiName << instr->getProgramName();

                        str << indent(col) << "\\set Staff.midiInstrument = \"" << staffMidiName.str()
			    << "\"" << std::endl;
                    }

		    // multi measure rests are used by default
                    str << indent(col) << "\\set Score.skipBars = ##t" << std::endl;

                    // turn off the stupid accidental cancelling business,
                    // because we don't do that ourselves, and because my 11
                    // year old son pointed out to me that it "Looks really
                    // stupid.  Why is it cancelling out four flats and then
                    // adding five flats back?  That's brain damaged."
                    str << indent(col) << "\\set Staff.printKeyCancellation = ##f" << std::endl;
                    str << indent(col) << "\\new Voice \\global" << std::endl;
                    if (tempoCount > 0) {
                        str << indent(col) << "\\new Voice \\globalTempo" << std::endl;
                    }
                    if ( m_exportMarkerMode != EXPORT_NO_MARKERS ) {
                        str << indent(col) << "\\new Voice \\markers" << std::endl;
                    }

                }

                // Temporary storage for non-atomic events (!BOOM)
                // ex. LilyPond expects signals when a decrescendo starts
                // as well as when it ends
                eventendlist eventsInProgress;
                eventstartlist eventsToStart;

                // If the segment doesn't start at 0, add a "skip" to the start
                // No worries about overlapping segments, because Voices can overlap
                // voiceCounter is a hack because LilyPond does not by default make
                // them unique
                std::ostringstream voiceNumber;
                voiceNumber << "voice " << ++voiceCounter;

                str << std::endl << indent(col++) << "\\context Voice = \"" << voiceNumber.str()
                << "\" {"; // indent+

                str << std::endl << indent(col) << "\\override Voice.TextScript #'padding = #2.0";
                str << std::endl << indent(col) << "\\override MultiMeasureRest #'expand-limit = 1" << std::endl;

                // staff notation size
		int staffSize = track->getStaffSize();
		if (staffSize == StaffTypes::Small) str << indent(col) << "\\small" << std::endl;
		else if (staffSize == StaffTypes::Tiny) str << indent(col) << "\\tiny" << std::endl;

                SegmentNotationHelper helper(**i);
                helper.setNotationProperties();

                int firstBar = m_composition->getBarNumber((*i)->getStartTime());

                if (firstBar > 0) {
                    // Add a skip for the duration until the start of the first
                    // bar in the segment.  If the segment doesn't start on a bar
                    // line, an additional skip will be written (in the form of
                    // a series of rests) at the start of writeBar, below.
                    //!!! This doesn't cope correctly yet with time signature changes
                    // during this skipped section.
                    str << std::endl << indent(col);
                    writeSkip(timeSignature, compositionStartTime,
		              m_composition->getBarStart(firstBar) - compositionStartTime,
                              false, str);
                }

                std::string lilyText = "";      // text events
                std::string prevStyle = "";     // track note styles

                Rosegarden::Key key;

                bool haveRepeating = false;
                bool haveAlternates = false;

                bool nextBarIsAlt1 = false;
                bool nextBarIsAlt2 = false;
                bool prevBarWasAlt2 = false;

                int MultiMeasureRestCount = 0;

                bool nextBarIsDouble = false;
                bool nextBarIsEnd = false;
                bool nextBarIsDot = false;

                for (int barNo = m_composition->getBarNumber((*i)->getStartTime());
                        barNo <= m_composition->getBarNumber((*i)->getEndMarkerTime());
                        ++barNo) {

                    timeT barStart = m_composition->getBarStart(barNo);
                    timeT barEnd = m_composition->getBarEnd(barNo);
                    if (barStart < compositionStartTime) {
                        barStart = compositionStartTime;
                    }

                    // open \repeat section if this is the first bar in the
                    // repeat
                    if ((*i)->isRepeating() && !haveRepeating) {

                        haveRepeating = true;

                        //!!! calculate the number of times this segment
                        //repeats and make the following variable meaningful
                        int numRepeats = 2;

                        str << std::endl << indent(col++) << "\\repeat volta " << numRepeats << " {";
                    }

                    // open the \alternative section if this bar is alternative ending 1
                    // ending (because there was an "Alt1" flag in the
                    // previous bar to the left of where we are right now)
                    //
                    // Alt1 remains in effect until we run into Alt2, which
                    // runs to the end of the segment
                    if (nextBarIsAlt1 && haveRepeating) {
                        str << std::endl << indent(--col) << "} \% repeat close (before alternatives) ";
                        str << std::endl << indent(col++) << "\\alternative {";
                        str << std::endl << indent(col++) << "{  \% open alternative 1 ";
                        nextBarIsAlt1 = false;
                        haveAlternates = true;
                    } else if (nextBarIsAlt2 && haveRepeating) {
                        if (!prevBarWasAlt2) {
                            col--;
			    // add an extra str to the following to shut up
			    // compiler warning from --ing and ++ing it in the
			    // same statement
                            str << std::endl << indent(--col) << "} \% close alternative 1 ";
                            str << std::endl << indent(col++) << "{  \% open alternative 2";
                            col++;
                        }
                        prevBarWasAlt2 = true;
                    }

                    // write out a bar's worth of events
                    writeBar(*i, barNo, barStart, barEnd, col, key,
                             lilyText,
                             prevStyle, eventsInProgress, str,
                             MultiMeasureRestCount, 
                             nextBarIsAlt1, nextBarIsAlt2, nextBarIsDouble, nextBarIsEnd, nextBarIsDot);

                }

                // close \repeat
                if (haveRepeating) {

                    // close \alternative section if present
                    if (haveAlternates) {
                        str << std::endl << indent(--col) << " } \% close alternative 2 ";
                    }

                    // close \repeat section in either case
                    str << std::endl << indent(--col) << " } \% close "
                    << (haveAlternates ? "alternatives" : "repeat");
                }

                // closing bar
                if (((*i)->getEndMarkerTime() == compositionEndTime) && !haveRepeating) {
                    str << std::endl << indent(col) << "\\bar \"|.\"";
                }

                // close Voice context
                str << std::endl << indent(--col) << "} % Voice" << std::endl;  // indent-

		//
                // Write accumulated lyric events to the Lyric context, if desired.
                //
		// Sync the code below with LyricEditDialog::unparse() !!
		//
                if (m_exportLyrics) {
		    for (long currentVerse = 0, lastVerse = 0; 
                         currentVerse <= lastVerse; 
			 currentVerse++) {
		        bool haveLyric = false;
			bool firstNote = true;
		        TQString text = "";

		        timeT lastTime = (*i)->getStartTime();
		        for (Segment::iterator j = (*i)->begin();
		            (*i)->isBeforeEndMarker(j); ++j) {
		
		            bool isNote = (*j)->isa(Note::EventType);
		            bool isLyric = false;
		
		            if (!isNote) {
		                if ((*j)->isa(Text::EventType)) {
		                    std::string textType;
		                    if ((*j)->get
		                            <String>(Text::TextTypePropertyName, textType) &&
		                            textType == Text::Lyric) {
		                        isLyric = true;
		                    }
		                }
		            }
		
		            if (!isNote && !isLyric) continue;
		
		            timeT myTime = (*j)->getNotationAbsoluteTime();
		
			    if (isNote) {
				if ((myTime > lastTime) || firstNote) {
				    if (!haveLyric)
					text += " _";
				    lastTime = myTime;
				    haveLyric = false;
				    firstNote = false;
				}
			    }
		
		            if (isLyric) {
			        long verse;
		                (*j)->get<Int>(Text::LyricVersePropertyName, verse);

				if (verse == currentVerse) {
		                    std::string ssyllable;
		                    (*j)->get<String>(Text::TextPropertyName, ssyllable);
				    text += " ";
			    
		                    TQString syllable(strtoqstr(ssyllable));
		                    syllable.replace(TQRegExp("\\s+"), "");
		                    text += "\"" + syllable + "\"";
		                    haveLyric = true;
				} else if (verse > lastVerse) {
                                  lastVerse = verse;
				}
			    }
		        }

			text.replace( TQRegExp(" _+([^ ])") , " \\1" );
			text.replace( "\"_\"" , " " );
		
		        // Do not create empty context for lyrics.
		        // Does this save some vertical space, as was written
		        // in earlier comment?
		        TQRegExp rx( "\"" );
		        if ( rx.search( text ) != -1 ) {
		    
			    str << indent(col) << "\\lyricsto \"" << voiceNumber.str() << "\""
			        << " \\new Lyrics \\lyricmode {" << std::endl;
			    if (m_lyricsHAlignment == RIGHT_ALIGN) {
				str << indent(++col) << "\\override LyricText #'self-tqalignment-X = #RIGHT"
				    << std::endl;
			    } else if (m_lyricsHAlignment == CENTER_ALIGN) {
				str << indent(++col) << "\\override LyricText #'self-tqalignment-X = #CENTER"
				    << std::endl;
			    } else {
				str << indent(++col) << "\\override LyricText #'self-tqalignment-X = #LEFT"
				    << std::endl;
			    }
			    str << indent(col) << "\\set ignoreMelismata = ##t" << std::endl;
			    str << indent(col) << text.utf8().data() << " " << std::endl;
			    str << indent(col) << "\\unset ignoreMelismata" << std::endl;
			    str << indent(--col) << "} % Lyrics " << (currentVerse+1) << std::endl;
			    // close the Lyrics context
		        } // if ( rx.search( text....
		    } // for (long currentVerse = 0....
		} // if (m_exportLyrics....
            } // if (isMidiTrack.... 
            firstTrack = false;
        } // for (Composition::iterator i = m_composition->begin()....
    } // for (int trackPos = 0....

    // close the last track (Staff context)
    if (voiceCounter > 0) {
        str << indent(--col) << ">> % Staff (final) ends" << std::endl;  // indent-

        // handle any necessary final bracket closures (if brackets are being
        // exported)
        if (m_exportStaffGroup) {
            if (bracket == Brackets::SquareOff ||
                 bracket == Brackets::SquareOnOff) {
                 str << indent(--col) << ">> % StaffGroup " << staffGroupCounter
                     << std::endl; //indent-
            } else        if (bracket == Brackets::CurlyOff) {
                 str << indent(--col) << ">> % PianoStaff (final) " << pianoStaffCounter
                     << std::endl; //indent-
            } else if (bracket == Brackets::CurlySquareOff) {
                 str << indent(--col) << ">> % PianoStaff (final) " << pianoStaffCounter
                     << std::endl; //indent-
                 str << indent(--col) << ">> % StaffGroup (final) " << staffGroupCounter
                     << std::endl; //indent-
            }
	}
    } else {
        str << indent(--col) << "% (All staffs were muted.)" << std::endl;
    }

    // close \notes section
    str << std::endl << indent(--col) << ">> % notes" << std::endl << std::endl; // indent-
//    str << std::endl << indent(col) << ">> % global wrapper" << std::endl;

    // write \tqlayout block
    str << indent(col) << "\\tqlayout { }" << std::endl;

    // write initial tempo in Midi block, if user wishes (added per user request...
    // makes debugging the .ly file easier because fewer "noisy" errors are
    // produced during the process of rendering MIDI...)
    if (m_exportMidi) {
        int tempo = int(Composition::getTempoQpm(m_composition->getTempoAtTime(m_composition->getStartMarker())));
        // Incomplete?  Can I get away without converting tempo relative to the time
        // signature for this purpose?  we'll see...
        str << indent(col++) << "\\midi {" << std::endl;
        str << indent(col) << "\\tempo 4 = " << tempo << std::endl;
        str << indent(--col) << "} " << std::endl;
    }

    // close \score section and close out the file
    str << "} % score" << std::endl;
    str.close();
    return true;
}

timeT 
LilyPondExporter::calculateDuration(Segment *s,
		                            const Segment::iterator &i,
		                            timeT barEnd,
		                            timeT &soundingDuration,
		                            const std::pair<int, int> &tupletRatio,
		                            bool &overlong)
{
	timeT duration = (*i)->getNotationDuration();
	timeT absTime = (*i)->getNotationAbsoluteTime();

	RG_DEBUG << "LilyPondExporter::calculateDuration: first duration, absTime: "
	<< duration << ", " << absTime << endl;

	timeT durationCorrection = 0;

	if ((*i)->isa(Note::EventType) || (*i)->isa(Note::EventRestType)) {
		try {
			// tuplet compensation, etc
			Note::Type type = (*i)->get<Int>(NOTE_TYPE);
			int dots = (*i)->get<Int>(NOTE_DOTS);
			durationCorrection = Note(type, dots).getDuration() - duration;
		} catch (Exception e) { // no properties
		}
	}

	duration += durationCorrection;

	RG_DEBUG << "LilyPondExporter::calculateDuration: now duration is "
	<< duration << " after correction of " << durationCorrection << endl;

	soundingDuration = duration * tupletRatio.first/ tupletRatio.second;

	timeT toNext = barEnd - absTime;
	if (soundingDuration > toNext) {
		soundingDuration = toNext;
		duration = soundingDuration * tupletRatio.second/ tupletRatio.first;
		overlong = true;
	}

	RG_DEBUG << "LilyPondExporter::calculateDuration: time to barEnd is "
	<< toNext << endl;

	// Examine the following event, and truncate our duration
	// if we overlap it.
	Segment::iterator nextElt = s->end();
	toNext = soundingDuration;

	if ((*i)->isa(Note::EventType)) {

		Chord chord(*s, i, m_composition->getNotationQuantizer());
		Segment::iterator nextElt = chord.getFinalElement();
		++nextElt;

		if (s->isBeforeEndMarker(nextElt)) {
			// The quantizer sometimes sticks a rest at the same time
			// as this note -- don't use that one here, and mark it as
			// not to be exported -- it's just a heavy-handed way of
			// rendering counterpoint in RG
			if ((*nextElt)->isa(Note::EventRestType) &&
				(*nextElt)->getNotationAbsoluteTime() == absTime) {
				(*nextElt)->set<Bool>(SKIP_PROPERTY, true);
				++nextElt;
			}
		}

	} else {
		nextElt = i;
		++nextElt;
		while (s->isBeforeEndMarker(nextElt)) {
			if ((*nextElt)->isa(Controller::EventType) ||
				(*nextElt)->isa(ProgramChange::EventType) ||
				(*nextElt)->isa(SystemExclusive::EventType) ||
				(*nextElt)->isa(ChannelPressure::EventType) ||
				(*nextElt)->isa(KeyPressure::EventType) ||
				(*nextElt)->isa(PitchBend::EventType))
				++nextElt;
			else
				break;
		}
	}

	if (s->isBeforeEndMarker(nextElt)) {
		RG_DEBUG << "LilyPondExporter::calculateDuration: inside conditional " << endl;
		toNext = (*nextElt)->getNotationAbsoluteTime() - absTime;
		// if the note was lengthened, assume it was lengthened to the left
		// when truncating to the beginning of the next note
		if (durationCorrection > 0) {
			toNext += durationCorrection;
		}
		if (soundingDuration > toNext) {
			soundingDuration = toNext;
			duration = soundingDuration * tupletRatio.second/ tupletRatio.first;
		}
	}

	RG_DEBUG << "LilyPondExporter::calculateDuration: second toNext is "
	<< toNext << endl;

	RG_DEBUG << "LilyPondExporter::calculateDuration: final duration, soundingDuration: " << duration << ", " << soundingDuration << endl;

	return duration;
}

void
LilyPondExporter::writeBar(Segment *s,
                           int barNo, int barStart, int barEnd, int col,
                           Rosegarden::Key &key,
                           std::string &lilyText,
                           std::string &prevStyle,
                           eventendlist &eventsInProgress,
                           std::ofstream &str,
                           int &MultiMeasureRestCount,
                           bool &nextBarIsAlt1, bool &nextBarIsAlt2,
                           bool &nextBarIsDouble, bool &nextBarIsEnd, bool &nextBarIsDot)
{
    int lastStem = 0; // 0 => unset, -1 => down, 1 => up
    int isGrace = 0;

    Segment::iterator i = s->findTime(barStart);
    if (!s->isBeforeEndMarker(i))
        return ;

    if (MultiMeasureRestCount == 0) {
	str << std::endl;

	if ((barNo + 1) % 5 == 0) {
	    str << "%% " << barNo + 1 << std::endl << indent(col);
	} else {
	    str << indent(col);
	}
    }

    bool isNew = false;
    TimeSignature timeSignature = m_composition->getTimeSignatureInBar(barNo, isNew);
    if (isNew) {
	if (timeSignature.isHidden()) {
	    str << "\\once \\override Staff.TimeSignature #'break-visibility = #(vector #f #f #f) ";
	}
        str << "\\time "
        << timeSignature.getNumerator() << "/"
        << timeSignature.getDenominator()
        << std::endl << indent(col);
    }

    timeT absTime = (*i)->getNotationAbsoluteTime();
    timeT writtenDuration = 0;
    std::pair<int,int> barDurationRatio(timeSignature.getNumerator(),timeSignature.getDenominator());
    std::pair<int,int> durationRatioSum(0,1);
    static std::pair<int,int> durationRatio(0,1);

    if (absTime > barStart) {
        Note note(Note::getNearestNote(absTime - barStart, MAX_DOTS));
        writtenDuration += note.getDuration();
        durationRatio = writeSkip(timeSignature, 0, note.getDuration(), true, str);
	durationRatioSum = fractionSum(durationRatioSum,durationRatio);
        // str << qstrtostr(TQString(" %{ %1/%2 %} ").tqarg(durationRatio.first).tqarg(durationRatio.second)); // DEBUG
    }

    timeT prevDuration = -1;
    eventstartlist eventsToStart;

    long groupId = -1;
    std::string groupType = "";
    std::pair<int, int> tupletRatio(1, 1);

    bool overlong = false;
    bool newBeamedGroup = false;
    int notesInBeamedGroup = 0;

    while (s->isBeforeEndMarker(i)) {

        if ((*i)->getNotationAbsoluteTime() >= barEnd)
            break;

        // First test whether we're entering or leaving a group,
        // before we consider how to write the event itself (at least
        // for pre-2.0 LilyPond output)
	TQString startGroupBeamingsStr = "";
	TQString endGroupBeamingsStr = "";

        if ((*i)->isa(Note::EventType) || (*i)->isa(Note::EventRestType) ||
                (*i)->isa(Clef::EventType) || (*i)->isa(Rosegarden::Key::EventType)) {

            long newGroupId = -1;
            if ((*i)->get
                    <Int>(BEAMED_GROUP_ID, newGroupId)) {

                if (newGroupId != groupId) {
                    // entering a new beamed group

                    if (groupId != -1) {
                        // and leaving an old one
                        if (groupType == GROUP_TYPE_TUPLED) {
                            if (m_exportBeams && notesInBeamedGroup > 0)
                                endGroupBeamingsStr += "] ";
                            endGroupBeamingsStr += "} ";
                        } else if (groupType == GROUP_TYPE_BEAMED) {
                            if (m_exportBeams && notesInBeamedGroup > 0)
                                endGroupBeamingsStr += "] ";
                        }
                    }

                    groupId = newGroupId;
                    groupType = "";
                    (void)(*i)->get
                    <String>(BEAMED_GROUP_TYPE, groupType);

                    if (groupType == GROUP_TYPE_TUPLED) {
                        long numerator = 0;
                        long denominator = 0;
                        (*i)->get
                        <Int>(BEAMED_GROUP_TUPLED_COUNT, numerator);
                        (*i)->get
                        <Int>(BEAMED_GROUP_UNTUPLED_COUNT, denominator);
                        if (numerator == 0 || denominator == 0) {
                            std::cerr << "WARNING: LilyPondExporter::writeBar: "
                            << "tupled event without tupled/untupled counts"
                            << std::endl;
                            groupId = -1;
                            groupType = "";
                        } else {
                            startGroupBeamingsStr += TQString("\\times %1/%2 { ").tqarg(numerator).tqarg(denominator);
                            tupletRatio = std::pair<int, int>(numerator, denominator);
			    // Require explicit beamed groups,
			    // fixes bug #1683205.
			    // HJJ: Why line below was originally present?
                            // newBeamedGroup = true;
                            notesInBeamedGroup = 0;
                        }
                    } else if (groupType == GROUP_TYPE_BEAMED) {
                        newBeamedGroup = true;
                        notesInBeamedGroup = 0;
			// there can currently be only on group type, reset tuplet ratio
                        tupletRatio = std::pair<int, int>(1,1);
                    }
                }

            }
            else {

                if (groupId != -1) {
                    // leaving a beamed group
                    if (groupType == GROUP_TYPE_TUPLED) {
                        if (m_exportBeams && notesInBeamedGroup > 0)
                            endGroupBeamingsStr += "] ";
                        endGroupBeamingsStr += "} ";
                        tupletRatio = std::pair<int, int>(1, 1);
                    } else if (groupType == GROUP_TYPE_BEAMED) {
                        if (m_exportBeams && notesInBeamedGroup > 0)
                            endGroupBeamingsStr += "] ";
                    }
                    groupId = -1;
                    groupType = "";
                }
            }
        }

	// Test whether the next note is grace note or not.
	// The start or end of beamed grouping should be put in proper places.
	str << endGroupBeamingsStr.utf8().data();
	if ((*i)->has(IS_GRACE_NOTE) && (*i)->get<Bool>(IS_GRACE_NOTE)) {
	    if (isGrace == 0) { 
	        isGrace = 1;
                str << "\\grace { ";
	        // str << "%{ grace starts %} "; // DEBUG
	    }
	} else if (isGrace == 1) {
	    isGrace = 0;
	    // str << "%{ grace ends %} "; // DEBUG
            str << "} ";
	}
	str << startGroupBeamingsStr.utf8().data();

        timeT soundingDuration = -1;
        timeT duration = calculateDuration
                         (s, i, barEnd, soundingDuration, tupletRatio, overlong);

        if (soundingDuration == -1) {
            soundingDuration = duration * tupletRatio.first / tupletRatio.second;
        }

        if ((*i)->has(SKIP_PROPERTY)) {
            (*i)->unset(SKIP_PROPERTY);
            ++i;
            continue;
        }

        bool needsSlashRest = false;

        if ((*i)->isa(Note::EventType)) {

            Chord chord(*s, i, m_composition->getNotationQuantizer());
            Event *e = *chord.getInitialNote();
            bool tiedForward = false;
	    bool tiedUp = false;

            // Examine the following event, and truncate our duration
            // if we overlap it.

            if (e->has(DISPLACED_X)) {
                double xDisplacement = 1 + ((double) e->get
                                            <Int>(DISPLACED_X)) / 1000;
                str << "\\once \\override NoteColumn #'force-hshift = #"
                << xDisplacement << " ";
            }

            bool hiddenNote = false;
            if (e->has(INVISIBLE)) {
                if (e->get
                        <Bool>(INVISIBLE)) {
                    hiddenNote = true;
                }
            }
	    
	    if ( hiddenNote ) {
	        str << "\\hideNotes ";
	    }

            if (e->has(NotationProperties::STEM_UP)) {
                if (e->get
                        <Bool>(NotationProperties::STEM_UP)) {
                    if (lastStem != 1) {
                        str << "\\stemUp ";
                        lastStem = 1;
                    }
                }
                else {
                    if (lastStem != -1) {
                        str << "\\stemDown ";
                        lastStem = -1;
                    }
                }
            } else {
                if (lastStem != 0) {
                    str << "\\stemNeutral ";
                    lastStem = 0;
                }
            }

            if (chord.size() > 1)
                str << "< ";

            Segment::iterator stylei = s->end();

            for (i = chord.getInitialElement(); s->isBeforeEndMarker(i); ++i) {

                if ((*i)->isa(Text::EventType)) {
                    if (!handleDirective(*i, lilyText, nextBarIsAlt1, nextBarIsAlt2,
                                         nextBarIsDouble, nextBarIsEnd, nextBarIsDot)) {

                        handleText(*i, lilyText);
                    }

                } else if ((*i)->isa(Note::EventType)) {

                    if (m_languageLevel >= LILYPOND_VERSION_2_8) {
                        // one \tweak per each chord note
                        if (chord.size() > 1)
                            writeStyle(*i, prevStyle, col, str, true);
                        else
                            writeStyle(*i, prevStyle, col, str, false);
                    } else {
                        // only one override per chord, and that outside the <>
                        stylei = i;
                    }
                    writePitch(*i, key, str);

                    bool noteHasCautionaryAccidental = false;
                    (*i)->get
                    <Bool>(NotationProperties::USE_CAUTIONARY_ACCIDENTAL, noteHasCautionaryAccidental);
                    if (noteHasCautionaryAccidental)
                        str << "?";

                    // get TIED_FORWARD and TIE_IS_ABOVE for later
                    (*i)->get<Bool>(TIED_FORWARD, tiedForward);
		    (*i)->get<Bool>(TIE_IS_ABOVE, tiedUp);

                    str << " ";
                } else if ((*i)->isa(Indication::EventType)) {
                    eventsToStart.insert(*i);
                    eventsInProgress.insert(*i);
                }

                if (i == chord.getFinalElement())
                    break;
            }

            if (chord.size() > 1)
                str << "> ";

            if (duration != prevDuration) {
                durationRatio = writeDuration(duration, str);
                str << " ";
                prevDuration = duration;
            }

            if (m_languageLevel == LILYPOND_VERSION_2_6) {
                // only one override per chord, and that outside the <>
                if (stylei != s->end()) {
                    writeStyle(*stylei, prevStyle, col, str, false);
                    stylei = s->end();
                }
            }

            if (lilyText != "") {
                str << lilyText;
                lilyText = "";
            }
            writeSlashes(*i, str);

            writtenDuration += soundingDuration;
	    std::pair<int,int> ratio = fractionProduct(durationRatio,tupletRatio);
	    durationRatioSum = fractionSum(durationRatioSum, ratio);
	    // str << qstrtostr(TQString(" %{ %1/%2 * %3/%4 = %5/%6 %} ").tqarg(durationRatio.first).tqarg(durationRatio.second).tqarg(tupletRatio.first).tqarg(tupletRatio.second).tqarg(ratio.first).tqarg(ratio.second)); // DEBUG

            std::vector<Mark> marks(chord.getMarksForChord());
            // problem here: stem direction unavailable (it's a view-local property)
            bool stemUp = true;
            e->get
            <Bool>(NotationProperties::STEM_UP, stemUp);
            for (std::vector<Mark>::iterator j = marks.begin(); j != marks.end(); ++j) {
                str << composeLilyMark(*j, stemUp);
            }
            if (marks.size() > 0)
                str << " ";

            handleEndingEvents(eventsInProgress, i, str);
            handleStartingEvents(eventsToStart, str);

            if (tiedForward)
	        if (tiedUp) 
                    str << "^~ ";
		else
		    str << "_~ ";

	    if ( hiddenNote ) {
	        str << "\\unHideNotes ";
	    }

            if (newBeamedGroup) {
                // This is a workaround for bug #1705430:
                //   Beaming groups erroneous after merging notes
                // There will be fewer "e4. [ ]" errors in LilyPond-compiling.
                // HJJ: This should be fixed in notation engine,
                //      after which the workaround below should be removed.
                Note note(Note::getNearestNote(duration, MAX_DOTS));

                switch (note.getNoteType()) {
                case Note::SixtyFourthNote:
                case Note::ThirtySecondNote:
                case Note::SixteenthNote:
                case Note::EighthNote:
                    notesInBeamedGroup++;
                    break;
                }
            }
            // // Old version before the workaround for bug #1705430:
            // if (newBeamedGroup)
            //    notesInBeamedGroup++;
        } else if ((*i)->isa(Note::EventRestType)) {

            bool hiddenRest = false;
            if ((*i)->has(INVISIBLE)) {
                if ((*i)->get
                        <Bool>(INVISIBLE)) {
                    hiddenRest = true;
                }
            }

            bool offsetRest = false;
            int restOffset  = 0;
            if ((*i)->has(DISPLACED_Y)) {
                restOffset  = (*i)->get<Int>(DISPLACED_Y);
                offsetRest = true;
            }

            if (offsetRest) {
                 std::cout << "REST OFFSET: " << restOffset << std::endl;
            } else {
                 std::cout << "NO REST OFFSET" << std::endl;
            }

	    if (MultiMeasureRestCount == 0) {
		if (hiddenRest) {
		    str << "s";
		} else if (duration == timeSignature.getBarDuration()) {
		    // Look ahead the segment in order to detect
		    // the number of measures in the multi measure rest.
		    Segment::iterator mm_i = i;
		    while (s->isBeforeEndMarker(++mm_i)) {
			if ((*mm_i)->isa(Note::EventRestType) &&
			    (*mm_i)->getNotationDuration() == (*i)->getNotationDuration() &&
			    timeSignature == m_composition->getTimeSignatureAt((*mm_i)->getNotationAbsoluteTime())) {
			    MultiMeasureRestCount++;
			} else {
			    break;
			}
		    }
		    str << "R";
		} else {
                     if (offsetRest) {
                         // use offset height to get an approximate corresponding
                          // height on staff
                         restOffset = restOffset / 1000;
                          restOffset -= restOffset * 2;

                          // use height on staff to get a MIDI pitch
                          // get clef from whatever the last clef event was
                          Rosegarden::Key  k;
                          Accidental a;
                         Pitch helper(restOffset, m_lastClefFound, k, a);

                          // port some code from writePitch() here, rather than
                          // rewriting writePitch() to do both jobs, which
                          // somebody could conceivably clean up one day if anyone
                          // is bored

                          // use MIDI pitch to get a named note
                          int p = helper.getPerformancePitch();
                          std::string n = convertPitchToLilyNote(p, a, k);

                          // write named note
                          str << n;
    
                          // generate and write octave marks
                          std::string m = "";
                          int o = (int)(p / 12);

                          // mystery hack (it was always aiming too low)
                          o++;

                          if (o < 4) {
                              for (; o < 4; o++)
                                   m += ",";
                          } else {
                              for (; o > 4; o--)
                                   m += "\'";
                          }

                          str << m;

                          // defer the \rest until after any duration, because it
                          // can't come before a duration if a duration change is
                          // necessary, which is all determined a bit further on
                          needsSlashRest = true;


                          std::cout << "using pitch letter:"
                                    << n << m
                                     << " for offset: " 
                                     << restOffset
                                     << " for calculated octave: "
                                     << o
                                     << " in clef: "
                                     << m_lastClefFound.getClefType()
                                    << std::endl;
                     } else {
                         str << "r";
                     }
		}
	    
		if (duration != prevDuration) {
		    durationRatio = writeDuration(duration, str);
		    if (MultiMeasureRestCount > 0) {
			str << "*" << (1 + MultiMeasureRestCount);
		    }
		    prevDuration = duration;
		}

                 // have to add \rest to a fake rest note after any required
                 // duration change
                 if (needsSlashRest) {
                     str << "\\rest";
                     needsSlashRest = false;
                 }

		if (lilyText != "") {
		    str << lilyText;
		    lilyText = "";
		}

		str << " ";
	    
		handleEndingEvents(eventsInProgress, i, str);
		handleStartingEvents(eventsToStart, str);

		if (newBeamedGroup)
		    notesInBeamedGroup++;
	    } else {
		MultiMeasureRestCount--;
	    }
            writtenDuration += soundingDuration;
	    std::pair<int,int> ratio = fractionProduct(durationRatio,tupletRatio);
	    durationRatioSum = fractionSum(durationRatioSum, ratio);
	    // str << qstrtostr(TQString(" %{ %1/%2 * %3/%4 = %5/%6 %} ").tqarg(durationRatio.first).tqarg(durationRatio.second).tqarg(tupletRatio.first).tqarg(tupletRatio.second).tqarg(ratio.first).tqarg(ratio.second)); // DEBUG
        } else if ((*i)->isa(Clef::EventType)) {

            try {
                // Incomplete: Set which note the clef should center on  (DMM - why?)
                // To allow octavation of the clef, enclose the clefname always with quotes.
                str << "\\clef \"";

                Clef clef(**i);

                if (clef.getClefType() == Clef::Treble) {
                    str << "treble";
                } else if (clef.getClefType() == Clef::French) {
                    str << "french";
                } else if (clef.getClefType() == Clef::Soprano) {
                    str << "soprano";
                } else if (clef.getClefType() == Clef::Mezzosoprano) {
                    str << "mezzosoprano";
                } else if (clef.getClefType() == Clef::Alto) {
                    str << "alto";
                } else if (clef.getClefType() == Clef::Tenor) {
                    str << "tenor";
                } else if (clef.getClefType() == Clef::Baritone) {
                    str << "baritone";
                } else if (clef.getClefType() == Clef::Varbaritone) {
                    str << "varbaritone";
                } else if (clef.getClefType() == Clef::Bass) {
                    str << "bass";
                } else if (clef.getClefType() == Clef::Subbass) {
                    str << "subbass";
                }

                // save clef for later use by rests that need repositioned
                 m_lastClefFound = clef;
                 std::cout << "getting clef"
                           << std::endl
                            << "clef: "
                            << clef.getClefType()
                            << " lastClefFound: "
                            << m_lastClefFound.getClefType()
                            << std::endl;

                // Transpose the clef one or two octaves up or down, if specified.
                int octaveOffset = clef.getOctaveOffset();
                if (octaveOffset > 0) {
                    str << "^" << 8*octaveOffset;
                } else if (octaveOffset < 0) {
                    str << "_" << -8*octaveOffset;
                }

                str << "\"" << std::endl << indent(col);

            } catch (Exception e) {
                std::cerr << "Bad clef: " << e.getMessage() << std::endl;
            }

        } else if ((*i)->isa(Rosegarden::Key::EventType)) {
	    // ignore hidden key signatures
	    bool hiddenKey = false;
	    if ((*i)->has(INVISIBLE)) {
		(*i)->get <Bool>(INVISIBLE, hiddenKey);
	    }

	    if (!hiddenKey) {
		try {
		    str << "\\key ";
		    key = Rosegarden::Key(**i);

		    Accidental accidental = Accidentals::NoAccidental;

		    str << convertPitchToLilyNote(key.getTonicPitch(), accidental, key);

		    if (key.isMinor()) {
			str << " \\minor";
		    } else {
			str << " \\major";
		    }
		    str << std::endl << indent(col);

		} catch (Exception e) {
		    std::cerr << "Bad key: " << e.getMessage() << std::endl;
		}
	    }

        } else if ((*i)->isa(Text::EventType)) {

            if (!handleDirective(*i, lilyText, nextBarIsAlt1, nextBarIsAlt2,
                                 nextBarIsDouble, nextBarIsEnd, nextBarIsDot)) {
                handleText(*i, lilyText);
            }

        } else if ((*i)->isa(Guitar::Chord::EventType)) {

            try {
                Guitar::Chord chord = Guitar::Chord(**i);
                const Guitar::Fingering& fingering = chord.getFingering();
                
                int barreStart = 0, barreEnd = 0, barreFret = 0;

                // 
                // Check if there is a barre.
                //
                if (fingering.hasBarre()) {
                    Guitar::Fingering::Barre barre = fingering.getBarre();
                    barreStart = barre.start;
                    barreEnd = barre.end;
                    barreFret = barre.fret;
                }

                if (barreStart == 0) {
                    str << " s4*0^\\markup \\fret-diagram #\"";
                } else {
                    str << " s4*0^\\markup \\override #'(barre-type . straight) \\fret-diagram #\"";
                }
                //
                // Check each string individually.
                // Note: LilyPond numbers strings differently.
                //
                for (int stringNum = 6; stringNum >= 1; --stringNum) {
                    if (barreStart == stringNum) {
                        str << "c:" << barreStart << "-" << barreEnd << "-" << barreFret << ";";
                    }

                    if (fingering.getStringStatus( 6-stringNum ) == Guitar::Fingering::MUTED) {
                        str << stringNum << "-x;";
                    } else if (fingering.getStringStatus( 6-stringNum ) == Guitar::Fingering::OPEN) {
                        str << stringNum << "-o;";
                    } else {
                        int stringStatus = fingering.getStringStatus(6-stringNum);
                        if ((stringNum <= barreStart) && (stringNum >= barreEnd)) {
                            str << stringNum << "-" << barreFret << ";";
                        } else {
                            str << stringNum << "-" << stringStatus << ";";
                        }
                    }
                }
                str << "\" ";

            } catch (Exception e) { // GuitarChord ctor failed
                RG_DEBUG << "Bad GuitarChord event in LilyPond export" << endl;
            }
        }

        // LilyPond 2.0 introduces required postfix syntax for beaming
        if (m_exportBeams && newBeamedGroup && notesInBeamedGroup > 0) {
            str << "[ ";
            newBeamedGroup = false;
        }

        if ((*i)->isa(Indication::EventType)) {
            eventsToStart.insert(*i);
            eventsInProgress.insert(*i);
        }

        ++i;
    }

    if (groupId != -1) {
        if (groupType == GROUP_TYPE_TUPLED) {
            if (m_exportBeams && notesInBeamedGroup > 0)
                str << "] ";
            str << "} ";
            tupletRatio = std::pair<int, int>(1, 1);
        } else if (groupType == GROUP_TYPE_BEAMED) {
            if (m_exportBeams && notesInBeamedGroup > 0)
                str << "] ";
        }
    }

    if (isGrace == 1) {
	isGrace = 0;
	// str << "%{ grace ends %} "; // DEBUG
        str << "} ";
    }

    if (lastStem != 0) {
        str << "\\stemNeutral ";
    }

    if (overlong) {
        str << std::endl << indent(col) <<
        qstrtostr(TQString("% %1").
                  arg(i18n("warning: overlong bar truncated here")));
    }

    if (fractionSmaller(durationRatioSum, barDurationRatio)) {
        str << std::endl << indent(col) <<
	    qstrtostr(TQString("% %1").
                arg(i18n("warning: bar too short, padding with rests")));
        str << std::endl << indent(col) <<
        qstrtostr(TQString("% %1/%2 < %3/%4").
                  arg(durationRatioSum.first).
                  arg(durationRatioSum.second).
                  arg(barDurationRatio.first).
                  arg(barDurationRatio.second))
	    << std::endl << indent(col);
        durationRatio = writeSkip(timeSignature, writtenDuration,
				  (barEnd - barStart) - writtenDuration, true, str);
	durationRatioSum = fractionSum(durationRatioSum,durationRatio);
    }
    //
    // Export bar and bar checks.
    //
    if (nextBarIsDouble) {
        str << "\\bar \"||\" ";
        nextBarIsDouble = false;
    } else if (nextBarIsEnd) {
        str << "\\bar \"|.\" ";
        nextBarIsEnd = false;
    } else if (nextBarIsDot) {
        str << "\\bar \":\" ";
        nextBarIsDot = false;
    } else if (MultiMeasureRestCount == 0) {
        str << " |";
    }
}

std::pair<int,int>
LilyPondExporter::writeSkip(const TimeSignature &timeSig,
                            timeT offset,
                            timeT duration,
                            bool useRests,
                            std::ofstream &str)
{
    DurationList dlist;
    timeSig.getDurationListForInterval(dlist, duration, offset);
    std::pair<int,int> durationRatioSum(0,1);
    std::pair<int,int> durationRatio(0,1);

    int t = 0, count = 0;

    for (DurationList::iterator i = dlist.begin(); ; ++i) {

        if (i == dlist.end() || (*i) != t) {

            if (count > 0) {

                if (!useRests)
                    str << "\\skip ";
                else if (t == timeSig.getBarDuration())
                    str << "R";
                else
                    str << "r";

                durationRatio = writeDuration(t, str);

                if (count > 1) {
                    str << "*" << count;
		    durationRatio = fractionProduct(durationRatio,count);
		}
                str << " ";

		durationRatioSum = fractionSum(durationRatioSum,durationRatio);
            }

            if (i != dlist.end()) {
                t = *i;
                count = 1;
            }

        } else {
            ++count;
        }

        if (i == dlist.end())
            break;
    }
    return durationRatioSum;
}

bool
LilyPondExporter::handleDirective(const Event *textEvent,
                                  std::string &lilyText,
                                  bool &nextBarIsAlt1, bool &nextBarIsAlt2,
                                  bool &nextBarIsDouble, bool &nextBarIsEnd, bool &nextBarIsDot)
{
    Text text(*textEvent);

    if (text.getTextType() == Text::LilyPondDirective) {
        std::string directive = text.getText();
        if (directive == Text::Segno) {
            lilyText += "^\\markup { \\musicglyph #\"scripts.segno\" } ";
        } else if (directive == Text::Coda) {
            lilyText += "^\\markup { \\musicglyph #\"scripts.coda\" } ";
        } else if (directive == Text::Alternate1) {
            nextBarIsAlt1 = true;
        } else if (directive == Text::Alternate2) {
            nextBarIsAlt1 = false;
            nextBarIsAlt2 = true;
        } else if (directive == Text::BarDouble) {
            nextBarIsDouble = true;
        } else if (directive == Text::BarEnd) {
            nextBarIsEnd = true;
        } else if (directive == Text::BarDot) {
            nextBarIsDot = true;
        } else {
            // pass along less special directives for handling as plain text,
            // so they can be attached to chords and whatlike without
            // redundancy
            return false;
        }
        return true;
    } else {
        return false;
    }
}

void
LilyPondExporter::handleText(const Event *textEvent,
                             std::string &lilyText)
{
    try {

        Text text(*textEvent);
        std::string s = text.getText();

        // only protect illegal chars if this is Text, rather than
        // LilyPondDirective
        if ((*textEvent).isa(Text::EventType))
            s = protectIllegalChars(s);

        if (text.getTextType() == Text::Tempo) {

            // print above staff, bold, large
            lilyText += "^\\markup { \\bold \\large \"" + s + "\" } ";

        } else if (text.getTextType() == Text::LocalTempo ||
                   text.getTextType() == Text::Chord) {

            // print above staff, bold, small
            lilyText += "^\\markup { \\bold \"" + s + "\" } ";

        } else if (text.getTextType() == Text::Dynamic) {

            // supported dynamics first
            if (s == "ppppp" || s == "pppp" || s == "ppp" ||
                    s == "pp" || s == "p" || s == "mp" ||
                    s == "mf" || s == "f" || s == "ff" ||
                    s == "fff" || s == "ffff" || s == "rfz" ||
                    s == "sf") {

                lilyText += "-\\" + s + " ";

            } else {
	        // export as a plain markup:
		// print below staff, bold italics, small
		lilyText += "_\\markup { \\bold \\italic \"" + s + "\" } ";
            }

        } else if (text.getTextType() == Text::Direction) {

            // print above staff, large
            lilyText += "^\\markup { \\large \"" + s + "\" } ";

        } else if (text.getTextType() == Text::LocalDirection) {

            // print below staff, bold italics, small
            lilyText += "_\\markup { \\bold \\italic \"" + s + "\" } ";

            // LilyPond directives that don't require special handling across
            // barlines are handled here along with ordinary text types.  These
            // can be injected wherever they happen to occur, and should get
            // attached to the right bits in due course without extra effort.
            //
        } else if (text.getText() == Text::Gliss) {
            lilyText += "\\glissando ";
        } else if (text.getText() == Text::Arpeggio) {
            lilyText += "\\arpeggio ";
        } else if (text.getText() == Text::Tiny) {
            lilyText += "\\tiny ";
        } else if (text.getText() == Text::Small) {
            lilyText += "\\small ";
        } else if (text.getText() == Text::NormalSize) {
            lilyText += "\\normalsize ";
        } else {
            textEvent->get
            <String>(Text::TextTypePropertyName, s);
            std::cerr << "LilyPondExporter::write() - unhandled text type: "
            << s << std::endl;
        }
    } catch (Exception e) {
        std::cerr << "Bad text: " << e.getMessage() << std::endl;
    }
}

void
LilyPondExporter::writePitch(const Event *note,
                             const Rosegarden::Key &key,
                             std::ofstream &str)
{
    // Note pitch (need name as well as octave)
    // It is also possible to have "relative" pitches,
    // but for simplicity we always use absolute pitch
    // 60 is middle C, one unit is a half-step

    long pitch = 60;
    note->get
    <Int>(PITCH, pitch);

    Accidental accidental = Accidentals::NoAccidental;
    note->get
    <String>(ACCIDENTAL, accidental);

    // format of LilyPond note is:
    // name + octave + (duration) + text markup

    // calculate note name and write note
    std::string lilyNote;

    lilyNote = convertPitchToLilyNote(pitch, accidental, key);

    str << lilyNote;

    // generate and write octave marks
    std::string octaveMarks = "";
    int octave = (int)(pitch / 12);

    // tweak the octave break for B# / Cb
    if ((lilyNote == "bisis") || (lilyNote == "bis")) {
        octave--;
    } else if ((lilyNote == "ceses") || (lilyNote == "ces")) {
        octave++;
    }

    if (octave < 4) {
        for (; octave < 4; octave++)
            octaveMarks += ",";
    } else {
        for (; octave > 4; octave--)
            octaveMarks += "\'";
    }

    str << octaveMarks;
}

void
LilyPondExporter::writeStyle(const Event *note, std::string &prevStyle,
                             int col, std::ofstream &str, bool isInChord)
{
    // some hard-coded styles in order to provide rudimentary style export support
    // note that this is technically bad practice, as style names are not supposed
    // to be fixed but deduced from the style files actually present on the system
    const std::string styleMensural = "Mensural";
    const std::string styleTriangle = "Triangle";
    const std::string styleCross = "Cross";
    const std::string styleClassical = "Classical";

    // handle various note styles before opening any chord
    // brackets
    std::string style = "";
    note->get
    <String>(NotationProperties::NOTE_STYLE, style);

    if (style != prevStyle) {

        if (style == styleClassical && prevStyle == "")
            return ;

        if (!isInChord)
            prevStyle = style;

        if (style == styleMensural) {
            style = "mensural";
        } else if (style == styleTriangle) {
            style = "triangle";
        } else if (style == styleCross) {
            style = "cross";
        } else {
            style = "default"; // failsafe default or explicit
        }

        if (!isInChord) {
            str << std::endl << indent(col) << "\\override Voice.NoteHead #'style = #'" << style << std::endl << indent(col);
        } else {
            str << "\\tweak #'style #'" << style << " ";
        }
    }
}

std::pair<int,int>
LilyPondExporter::writeDuration(timeT duration,
                                std::ofstream &str)
{
    Note note(Note::getNearestNote(duration, MAX_DOTS));
    std::pair<int,int> durationRatio(0,1);

    switch (note.getNoteType()) {

    case Note::SixtyFourthNote:
        str << "64"; durationRatio = std::pair<int,int>(1,64);
        break;

    case Note::ThirtySecondNote:
        str << "32"; durationRatio = std::pair<int,int>(1,32);
        break;

    case Note::SixteenthNote:
        str << "16"; durationRatio = std::pair<int,int>(1,16);
        break;

    case Note::EighthNote:
        str << "8"; durationRatio = std::pair<int,int>(1,8);
        break;

    case Note::QuarterNote:
        str << "4"; durationRatio = std::pair<int,int>(1,4);
        break;

    case Note::HalfNote:
        str << "2"; durationRatio = std::pair<int,int>(1,2);
        break;

    case Note::WholeNote:
        str << "1"; durationRatio = std::pair<int,int>(1,1);
        break;

    case Note::DoubleWholeNote:
        str << "\\breve"; durationRatio = std::pair<int,int>(2,1);
        break;
    }

    for (int numDots = 0; numDots < note.getDots(); numDots++) {
        str << ".";
    }
    durationRatio = fractionProduct(durationRatio,
	    std::pair<int,int>((1<<(note.getDots()+1))-1,1<<note.getDots()));
    return durationRatio;
}

void
LilyPondExporter::writeSlashes(const Event *note, std::ofstream &str)
{
    // write slashes after text
    // / = 8 // = 16 /// = 32, etc.
    long slashes = 0;
    note->get
    <Int>(NotationProperties::SLASHES, slashes);
    if (slashes > 0) {
        str << ":";
        int length = 4;
        for (int c = 1; c <= slashes; c++) {
            length *= 2;
        }
        str << length;
    }
}

}