summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/matlab-prettifier/matlab-prettifier.dtx
blob: cb8145299b4f939d0d762c8d412a3aafdc48c41f (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
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
% \iffalse meta-comment
%
% Copyright (C) 2014 by Julien Cretel
%                       <jubobs.matlab.prettifier at gmail.com>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%
%     http://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2011/06/27]
%<package>\ProvidesPackage{matlab-prettifier}
%<package>  [2014/06/19 v0.3 A package for prettyprinting Matlab source code]
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{lstdoc}
\usepackage[framed,numbered]{matlab-prettifier}
\usepackage{hyperref}
\usepackage{cleveref}

\lstset
{
  style                 = Matlab-editor,
  basicstyle            = \normalsize\mlttfamily,
  upquote               = true,
  mlunquotedstringdelim = {/*}{*/},
  mlmoresharedvars      = {myglobalvar},
}
\lstdefinestyle{nonbnoframe}
{
  frame   = none,
  numbers = none,
}
\lstMakeShortInline"
\newcommand\ph\mlplaceholder
\newcommand*{\pkg}[1]{\textsf{#1}}
\newcommand*{\opt}[1]{\texttt{#1}}
\newcommand\lstpkg{\pkg{listings}}
\newcommand*{\lststy}[1]{\texttt{#1}}
\newcommand*{\lstlng}[1]{\texttt{#1}}
\newcommand\matlab{\textsc{Matlab}}
\newcommand\octave{\textsc{Octave}}
\newcommand\matlabver{\matlab~(R2013a)}
\newcommand\mathworks{MathWorks}
\newcommand*\trademark[1]{#1\textsuperscript{\textregistered}}
\newcommand\mlpkg{\pkg{matlab-prettifier}}
\newcommand\mllng{\texttt{Matlab-pretty}}
\newcommand*{\mlsty}[1]{\texttt{#1}}
\newcommand\overrideEnd{\lstinline[mloverride]|end|}
\newcommand\overrideEnumeration{\lstinline[mloverride]|enumeration|}
\newcommand\overrideMethods{\lstinline[mloverride]|methods|}
\newcommand\overrideProperties{\lstinline[mloverride]|properties|}
\newcommand\overrideEvents{\lstinline[mloverride]|events|}
\newcommand\itemp{\item[\(+\)]}
\newcommand\itemm{\item[\(-\)]}

\makeindex

\begin{document}
  \DocInput{matlab-prettifier.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{671}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v0.1}{2014/04/24}{Initial release.}
% \changes{v0.2}{2014/04/25}{
%   Refactor code extensively;
%   rewrite automatic scaling of inline code;
%   implement mlscaleinline key;
%   implement mlonlyheader.
% }
% \changes{v0.3}{2014/06/19}{
%   Change default font from Bera Mono to Computer Modern typewriter
%   (for compatibility reasons);
%   fix bug (end keyword now gets reset by a semicolon);
%   rename placeholder user macro; 
%   mention support for a subset of Octave's syntax.
% }
%
% \GetFileInfo{matlab-prettifier.sty}
%
%  ^^A see http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex%2F3540
%  \begingroup
%    \def\x{\#,\$,\%,\^,\_,\~,\&,\{,\},\/}%^^A
%    \makeatletter
%    \@onelevel@sanitize\x
%  \expandafter\endgroup\expandafter\DoNotIndex\expandafter{\x}
%
% \begingroup
%    \makeatletter
%    \lccode`9=32\relax
%    \lowercase{%^^A
%      \edef\x{\noexpand\DoNotIndex{\@backslashchar9}}%^^A
%    }%^^A
%  \expandafter\endgroup\x
%
% \DoNotIndex{\@empty,\@ifpackagewith,\@lst,\@namedef,\@ne,\@tempa}
% \DoNotIndex{\advance}
% \DoNotIndex{\baselineskip,\begingroup,\bfseries,\bgroup}
% \DoNotIndex{\ClosingEndKW@mlpr@false,\ClosingEndKW@mlpr@true,^^A
%   \color,\colorlet,\csname,\CurrentOption}
% \DoNotIndex{\DeclareOption,\def,\definecolor,\dimexpr,^^A
%   \DroppingOutput@mlpr@false,\DroppingOutput@mlpr@true}
% \DoNotIndex{\edef,\egroup,\else,\endcsname,\endgroup,\expandafter}
% \DoNotIndex{\f@baselineskip,\f@shape,\f@size,\fi,\font,\fontsize,^^A
%   \fontchardp,\fontcharht,\fontfamily,\framed@mlpr@true,\fvm@Scale}
% \DoNotIndex{\gdef,\global}
% \DoNotIndex{\ifnum,\ifx,^^A
%   \InClassdef@mlpr@false,\InClassdef@mlpr@true,^^A
%   \InSecTitle@mlpr@false,\InSecTitle@mlpr@true,^^A
%   \InStr@mlpr@false,\InStr@mlpr@true,^^A
%   \itdefault,\itshape}
% \DoNotIndex{\let,\lst@basicstyle,\lst@BeginDropOutput,\lst@AddToHook,^^A
%   \lst@CalcLostSpaceAndOutput,\lst@DefSaveDef,\lst@DelimKey,^^A
%   \lst@EnterMode,\lst@ifdisplaystyle,\lst@ifLmode,\lst@ifwhitespace,^^A
%   \lst@InstallKeywords,\lst@Key,\lst@language,\lst@LeaveMode,^^A
%   \lst@lineno,\lst@linewidth,\lst@Lmodetrue,\lst@mode,\lst@modetrue,^^A
%   \lst@NormedDef,\lst@Pmode,\lst@DeleteKeywords,\lst@InstallFamily@,^^A
%   \lst@MakeKeywords,\lst@MakeMoreKeywords,\lst@UseFamily,^^A
%   \lstdefinelanguage,\lstdefinestyle,\lstKV@SetIf}
% \DoNotIndex{\m@ne,\makebox}
% \DoNotIndex{\newcommand,\newcount,\newif,\newlength,\newtoks,\normalsize,^^A
%   \numbered@mlpr@true,\noexpand}
% \DoNotIndex{\OptionNotUsed}
% \DoNotIndex{\PackageError,\PackageWarning,\ProcessOptions}
% \DoNotIndex{\raisebox,\relax,\renewcommand,\RequirePackage,\rule,\rmfamily}
% \DoNotIndex{\selectfont,\setlength,\sldefault,\textlangle,\textrangle,^^A
%   \ttfamily}
% \DoNotIndex{\the}
% \DoNotIndex{\upshape}
% \DoNotIndex{\VisCharOccured@mlpr@true,\VisCharOccured@mlpr@false}
%
% \title^^A
% {^^A
%   The \mlpkg{} package^^A
%   \thanks^^A
%   {^^A
%     This document corresponds to \mlpkg~\fileversion,
%     dated~\filedate.^^A
%   }^^A
%  }
% \author{Julien Cretel\\ \texttt{jubobs.matlab.prettifier at gmail.com}}
% \date{\filedate}
% \thispagestyle{empty}
% \maketitle
%
% \begin{abstract}
%   Built on top of the \lstpkg{} package, the \mlpkg{} package allows you to
%   effortlessly prettyprint \matlab{} source code in documents typeset with
%   \LaTeX{} \& friends. Three predefined styles, one of which closely mimics
%   that of the \matlab{} editor, are available and can be invoked by
%   \lstpkg{} macros and environments in conjunction with (most) options
%   provided by the \lstpkg{} package. The appearance of your \matlab{}
%   listings can be further tweaked via a key-value interface extending that
%   of \lstpkg{}'. Partial support for Octave syntax is provided.
% \end{abstract}
%
% \tableofcontents\newpage
%
%
% \part{Introduction}
%
% \section{Why this package?}
%
% \trademark{\matlab} is a high-level language and interactive environment for
% numerical computation, visualization, and programming.^^A
%   \footnote{Source: \url{http://www.mathworks.co.uk/products/matlab/}}
% Despite being proprietary and occasionally frustrating, \matlab{} remains a
% great tool for prototyping matrix-oriented, number-crunching programs.
% As such, it enjoys widespread popularity, especially in academia,
% where, in particular, it is often used for teaching numerical methods.
%
% Users of both \matlab{} and \LaTeX{} (and friends) often need to typeset
% \matlab{} listings in \LaTeX{} documents, usually with some syntax
% highlighting, for improved code readability;
% the relatively large number of relevant questions posted on
% \href{http://tex.stackexchange.com/search?q=matlab+code+is%3Aquestion}^^A
% {tex.stackexchange.com} attests to that need.
%
% Recent versions of \matlab{} provide a built-in function, called "publish",
%  that can generate \LaTeX{} code for typesetting \matlab{} listings, but
% that function uses a |verbatim| environment, which doesn't allow for any
% fancy formatting.
% Several \LaTeX{} packages^^A
% ---vanilla \lstpkg{}, \pkg{mcode}, and \pkg{minted}, among others---^^A
% allow for automatic syntax highlighting of \matlab{} listings in \LaTeX{}
% documents.
% However, none of those packages do a great job at replicating the very
% specific syntax-highlighting style performed on the fly by the \matlab{}
% editor.^^A
%   \footnote{see ... for a comparison.}
%  
% The lack of tools for faithfully mimicking the style of the \matlab{}
% editor is unfortunate, especially from an educational standpoint, for the
% following reason.
% Most newcomers to \matlab{} read and write code in the \matlab{} editor
% and are, therefore, continually exposed to its highlighting style.
% Visual cues^^A
% ---such as those provided by syntax highlighting---^^A
% play an important role for recognising patterns,
% and students of a programming language are more likely to quickly and
% effectively learn and recognize its syntax if they see it highlighted in a
% consistent manner, whether it be in a text editor or in some course material
% (lab handout, assignment paper, etc.).
%
% The \mlpkg{} package is intended to fill that gap.
% Built on top of the feature-rich \lstpkg{} package, \mlpkg{} allows you to
% beautifully and effortlessly typeset \matlab{} listings, as it configures
% \lstpkg{} ``behind the scenes'' to replicate, as closely as possible, the
% syntax-highlighting style of the \matlab{} editor.
%
% What about code written in \octave{} (a free alternative to \matlab{})?
% Because \octave{}'s syntax and \matlab{}'s syntax
% \href{http://wiki.octave.org/FAQ#How_is_Octave_different_from_Matlab.3F}
%   {overlap a lot},
% \mlpkg{} correctly highlights \octave{} listings that strictly adhere to
% the subset of syntax that lies in this overlap.
% More support for \octave{} is expected to ship with a future release.
%
% Furthermore, \mlpkg{} comes with a few additional features that should make
% your life easier. Read on!
%
%
% \section{Review of alternatives to \mlpkg{}}
%
% Here is a review of the different alternatives^^A
% ---other than the \mlpkg{} package and \matlab{}'s "publish" function---^^A
% available for typesetting \matlab{} listings in \LaTeX{} documents.
%
% \paragraph{\lstpkg{}' \lstlng{Matlab} language} 
%^^A
% \begin{itemize}
%   \itemp A starting point!
%   \itemp \lstpkg{}' rich features are available.
%   \itemp Settings for \matlab{} listings are bundled into a \lstpkg{}
%     language, which can be invoked \emph{locally}.
%   \itemm Some \matlab{} keywords (e.g.\ "parfor") are not listed.
%   \itemm Built-in \matlab{} function names (e.g.\ "sum") get highlighted
%     like \matlab{} keywords do, which is very distracting.
%   \itemm Highlighting of keywords is not context-aware;
%     in particular, the |end| keyword gets typeset in the same style,
%     regardless of the context (closing keyword or last-element keyword)
%     in which it occurs.
%   \itemm No highlighting of block comments
%   \itemm No highlighting of line-continuation token and associated comment
%   \itemm Section titles are not highlighted in a style distinct from that of
%     comments.
%   \itemm No highlighting of unquoted strings
% \end{itemize}
%
% \paragraph{\pkg{mcode}}
%^^A
% \begin{itemize}
%   \itemp An honest attempt at improving \lstpkg{}' \lstlng{Matlab} language
%   \itemp Package options for quickly effecting global changes to the look
%     of \matlab{} listings
%   \itemp Block comments are highlighted as such.
%   \itemp A line-continuation token activates comment style\ldots
%   \itemm \ldots but also gets highlighted in comment style.
%   \itemm Settings for \matlab{} listings are defined \emph{globally}
%     (using |\lstset|) rather than locally,
%     which means those settings can easily be overwritten/lost.
%   \itemm The \overrideEnumeration{} keyword is not listed.
%   \itemm Highlighting of the last-element keyword is handled by a series of
%     literate replacements;
%     this approach works well only in a limited number of cases in which
%     that keyword occurs.
%   \itemm Highlighting of the four context-sensitive class-definition
%     keywords is not context-aware;
%     in particular, |properties| gets typeset in the same style,
%     regardless of the context (function or class-definition keyword)
%     in which it occurs.
%   \itemm Undesirable literate replacements
%     ("<=" by~\(\leq\), "delta" by~\(\Delta\)) are forced upon the users,
%     and cannot be easily prevented without breaking other literate
%     replacements put in place for highlighting the last-element keyword.
%   \itemm Section titles are not highlighted in a style distinct from that of
%     comments.
%   \itemm No highlighting of unquoted strings
%   \itemm The implementation of \pkg{mcode} lacks ``namespacing'',
%     which increases the risk of conflict with other packages.
%   \itemm \pkg{mcode} is currently not available on
%     \href{http://ftp.heanet.ie/pub/CTAN/tex/support/ctanify/ctanify.pdf}
%       {CTAN}.
% \end{itemize}
%
% \paragraph{Pygments-based packages
%   (\pkg{minted}, \pkg{verbments}, \pkg{pythontex})}
%^^A
% \begin{itemize}
%   \itemp Python!
%   \itemp Pygments!
%   \itemp Slick look
%   \itemp Block comments are highlighted as such.
%   \itemp A line-continuation token activates comment style\ldots
%   \itemm \ldots but also gets highlighted in comment style.
%   \itemm \lstpkg{}' features are not available.
%   \itemm Highlighting of keywords is not context-aware;
%     in particular, the last-element keyword gets highlighted like the
%     closing keyword does, which is very distracting.
%   \itemm \matlab{}'s transpose operator (".'") and "}'" are incorrectly
%     interpreted as starting a string literal.
%   \itemm No highlighting of unquoted strings
%   \itemm Escape to \LaTeX{} is only allowed in comments.
%   \itemm Slow compared to \lstpkg{}
%   \itemm Requires |-shell-escape|
% \end{itemize}
%
%
% \section{Syntactic elements automatically highlighted by \mlpkg{}}
% 
% The \mlpkg{} package defines a \pkg{listings} language called
% \mllng{}, which is designed to keep track of the context behind the scenes
% and, therefore, facilitates context-sensitive highlighting of various
% elements of \matlab{} syntax.
% That language is used as a basis for three \lstpkg{} styles,
% one of which, called \mlsty{Matlab-editor}, is showcased below.
% 
% \paragraph{Context-insensitive keywords}
% "while", "for", "break", etc.
%
% \paragraph{Context-sensitive keywords}
% "end",
% \overrideEvents{},
% \overrideProperties{},
% etc.
% 
% \paragraph{Quoted strings}
% \mbox{}
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[numbers=none]
'The sleeper must awaken.'
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \paragraph{To-end-of-line and block comments}
% \mbox{}
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[numbers=none]
% Now let's assign the value of pi to variable a
a = pi
%{
 Now that a holds the value of pi,
 here is what we're going to do...
 blah blah blah
%}
\end{lstlisting}
% \iffalse
%</example>
% \fi
% 
% \paragraph{Line-continuation token (and associated to-end-of-line comment)}
% \mbox{}
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[numbers=none]
A = [ 1, 2, 3,... (second row defined on next line)
      4, 5, 6];
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \paragraph{Section titles}
% \mbox{}
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[numbers=none]
%% Variable initialization
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \paragraph{System commands}
% \mbox{}
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[numbers=none]
! gzip sample.m
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
%
% \section{Styles provided by \mlpkg{}}
%
% The package defines three \lstpkg{} \emph{styles} for \matlab{} code:
% \mlsty{Matlab-editor}, \mlsty{Matlab-bw}, and
% \mlsty{Matlab-Pyglike}.
% Those styles differ in terms of color scheme but, for convenience,
% all three activate automatic line breaking;
% for more defails about automatic line breaking, see subsection~4.10 in
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}.
% 
% Here is a comparison of the three styles defined by \mlpkg{}.
%
% \paragraph{\mlsty{Matlab-editor}}
% This style mimics the default style of the \matlab{} editor.
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=Matlab-editor,basicstyle=\mlttfamily]
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \paragraph{\mlsty{Matlab-bw}}
% This style is mainly for black \& white printing.
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=Matlab-bw,basicstyle=\mlttfamily]
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
% \paragraph{\mlsty{Matlab-Pyglike}}
% The \pkg{minted}, \pkg{verbments}, and \pkg{pythontex} packages all use
% \href{http://pygments.org}{Pygments} lexers for syntax highlighting of
% listings.
% This \mlpkg{} style closely mimics the default style associated with
% Pygments' `MatlabLexer'.
% \iffalse
%<*example>
% \fi
\begin{lstlisting}[style=Matlab-Pyglike,basicstyle=\mlttfamily]
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
     4, 5, 6];
s = 'abcd';
for k = 1:4
  disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
% \iffalse
%</example>
% \fi
%
%
% \section{Other features}
%
% Additional features include
%
% \begin{itemize}
%   \item a key-value interface extending that of the \lstpkg{} package,
%   \item manual highlighting of variables with shared scope
%     (e.g.\ "myglobalvar"),
%   \item manual highlighting of unquoted strings
%     (e.g.\ ``on'' in ``"hold /*on*/"''),
%   \item a macro for easily typesetting placeholders
%     (e.g.\ \ph{initial-value}),
%   \item automatic scaling of inline code according to its surroundings,
%   \item an option to only print the header of a \matlab{} function.
% \end{itemize}
% 
%
% \part{User's guide}
%
% \section{Installation}
%
% \subsection{Package dependencies}
%
% \mlpkg{} requires relatively up-to-date versions of packages \pkg{textcomp},
% \pkg{xcolor}, and \lstpkg{}, all three of which ship with popular \TeX{}
% distributions. It loads those three packages without any options.
%
%
% \subsection{Installing \mlpkg{}}
%
% Since the package has been officially released on
% \href{http://www.ctan.org/pkg/matlab-prettifier}{CTAN},
% you should be able to install it directly through your package manager.
%
% However, if you need to install \mlpkg{} manually, you should run
%^^A
% \begin{verbatim}
%   latex matlab-prettifier.ins\end{verbatim}
%^^A
% and copy the file called |matlab-prettifier.sty| to a path
% where \LaTeX{} (or your preferred typesetting engine) can find it.
% To generate the documentation, run
%^^A
% \begin{verbatim}
%   pdflatex matlab-prettifier.dtx
%   makeindex -s gglo.ist -o matlab-prettifier.gls matlab-prettifier.glo
%   makeindex -s gind.ist -o matlab-prettifier.ind matlab-prettifier.idx
%   pdflatex matlab-prettifier.dtx
%   pdflatex matlab-prettifier.dtx\end{verbatim}
%^^A
%
%
% \section{Getting started}
%
% As stated above, the \mlpkg{} package is built on top of the \lstpkg{}
% package.
% If you already are a seasoned \lstpkg{} user, you should feel right at home.
% If you're not, be aware that this user's guide makes use of some \lstpkg{}
% functionalities (such as key-value options) without describing their usage.
% For more details on those functionalities, you should consult the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}.
%
%
% \subsection{Loading \texorpdfstring{\mlpkg{}}{matlab-prettifier}}
%
% Simply write
%^^A
% \begin{verbatim}
%   \usepackage{matlab-prettifier}\end{verbatim}
%^^A
% somewhere in your preamble.
%
% You may want to load the \lstpkg{} and
% \pkg{xcolor} packages with some options; in that case, make sure those
% options are passed to those two packages \emph{before} loading the \mlpkg{}
% package.
%
% The \mlpkg{} package currently offers four options.
% The first two are inspired from the \pkg{mcode} package.
% The last two are simply \lstpkg{} options that \mlpkg{} passes to \lstpkg{}
% behind the scenes;
% I chose to define those two options as \mlpkg{} options to save you
% the hassle of loading them with \lstpkg{} separately,
% should you wish to use them.
%^^A
% \begin{description}
% \item[\opt{framed}]\leavevmode
%
%       Draws (by default) a dark gray frame around each listing that uses
%       one of the three styles defined by \mlpkg{}.
%
% \item[\opt{numbered}]\leavevmode
%
%       Prints (by default) line numbers in light gray to the left of each
%       listing that uses one of the three styles defined by \mlpkg{}.
%
% \item[\opt{draft}]\leavevmode
%
%       This is simply \lstpkg{}' \opt{draft} option.
%       For more details, see subsection~2.2 of the
%       \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}.
%
% \item[\opt{final}]\leavevmode
%
%       This is simply \lstpkg{}' \opt{final} option.
%       For more details, see subsection~2.2 of the
%       \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}.
% \end{description}
%
%
% \subsection{Displayed listings}
%
% To typeset a \matlab{} listing embedded in your |tex| file, simply enclose
% it in an |lstlisting| environment, and load some style in the environment's
% optional argument, using \lstpkg{}' \keyname{style} key.
%^^A
% \begin{verbatim}
%   \begin{lstlisting}[style=Matlab-editor]
%     ...
%   \end{lstlisting}\end{verbatim}
%
%
% \subsection{Standalone listings}
%
% In practice, though, keeping your \matlab{} listings in external files^^A
% ---rather than embedding them in a |tex|~file---^^A
% is preferable, for maintainability reasons.
% To typeset a \matlab{} listing residing in an m-file, simply invoke the
% |\lstinputlisting| macro; load some style in the environment's
% optional argument, and specify the path to the m-file in question in the
% mandatory argument.
%^^A
% \begin{verbatim}
%   \lstinputlisting[style=Matlab-editor]{sample.m}\end{verbatim}
%^^A
%
%
% \subsection{Inline listings}
%
% You may want to typeset fragments of \matlab{} code within the main text of
% your document. For instance, you may want to typeset the "break" keyword in
% a sentence, in order to explain its usage.
% The |\lstinline| macro can be used for typesetting such inline code.
%^^A
% \begin{verbatim}
%   \lstinline[style=Matlab-style]!break!\end{verbatim}
%^^A
% Well, that's quite a mouthful for such a simple \matlab{} keyword!
% Writing |\lstinline| for each instance of inline \matlab{} code in your
% document can rapidly become tedious.
% Fortunately, \lstpkg{} allows its users to define a character as a shorthand
% for inline code via the |\lstMakeShortInline| macro.
% For instance, you could define the double-quote character~(|"|) as a
% shorthand for inline \matlab{} code with
%^^A
% \begin{verbatim}
%   \lstMakeShortInline[style=Matlab-editor]"\end{verbatim}
%^^A
% and you would then be able to typeset this "break" keyword simply by writing
%^^A
% \begin{verbatim}
%   "break"\end{verbatim}
%^^A
% in your |tex| file (but outside displayed listings, of course).
% 
% You should choose a character that does not otherwise occur in your
% |tex| file, especially in the inline \matlab{} code itself,
% or you run the risk of confusing \TeX{}.
% I find that, in general, the double-quote character~(|"|) offers a good
% compromise.
% If necessary, you can undefine a character as a shorthand for inline code,
% via \lstpkg{}' |\lstDeleteShortInline| macro.
% For more details, see subsection~4.17 in the \lstpkg{} manual.
%
%
% \subsection{Placeholders}
%
% Code-snippet placeholders, such as \ph{initial-value}, are particularly
% useful for educational purposes, e.g.\ to describe the syntax of a
% programming language to students.
% The following macro allows you to typeset such placeholders, both
% inside and outside listings:
%^^A
% \begin{syntax}
%
%   \item[0.1] \rcmdname\mlplaceholder|{|\meta{placeholder content}|}|
%
%     typesets a code-snippet placeholder.
%     You can use this macro both inside and outside listings.
%     When used inside listings, it must be invoked within an
%     \emph{escape to \LaTeX{}}; see subsection~4.14 of the \lstpkg{} manual.
%
% \end{syntax}
%
% If you choose to define a single character for escaping to \LaTeX{} (via
% \lstpkg{}' \keyname{escapechar} key), I recommend you define
% either the double-quote character~(|"|)
% or the backtick character~(\texttt{\`{}})
% as escape character,
% because neither is allowed in \matlab{} statements and expressions^^A
% ---although they may occur in \matlab{} string literals.
% Note that using~|"| both as shorthand for inline code and as an
% escape-to-\LaTeX{} character inside listings is perfectly allowed.
% 
% The following example illustrates how placeholders may be used to describe
% the syntax of the \matlab{} while loop.
%^^A
% \begin{lstsample}{}{\lstset{style=nonbnoframe}}
%    \begin{lstlisting}[
%      style=Matlab-editor,
%      basicstyle=\mlttfamily,
%      escapechar=`,
%    ]
%    while `\mlplaceholder{condition}`
%      if `\mlplaceholder{something-bad-happens}`
%        break
%      else
%        % do something useful
%      end
%    end
%    \end{lstlisting}
% \end{lstsample}
%
% For convenience, you can of course define a custom macro with a shorter
% name for typesetting placeholders, e.g.~|\ph|:
%^^A
% \begin{verbatim}
%   \newcommand\ph\mlplaceholder\end{verbatim}
%
%
% \section{Advanced customization}
%
% The \lstpkg{} package provides a large number of options accessible via a
% nifty key-value interface, which is described in its excellent
% \href{http://www.ctan.org/pkg/listings}{documentation}.
% The \mlpkg{} package extends \lstpkg{}' key-value interface interface by
% defining several additional keys that allow you to customize the style of
% your \matlab{} listings, should you wish to do so.
% All the keys provided by \mlpkg{} are prefixed by ``|ml|'',
% to help you distinguish them from native \lstpkg{} keys.
% 
%
% \subsection{Keys from the \lstpkg{} that you should not use}
%
% The great majority of keys provided by \lstpkg{} can be used in conjunction
% with keys provided by \mlpkg{} without any detrimental side effects,
% but there are a few exceptions that you should keep in mind.
% 
% Some \mlpkg{} keys rely on \lstpkg{} keys ``under the hood'',
% and using those \mlpkg{} and \lstpkg{} keys in conjunction is
% \emph{strongly discouraged},
% because doing so has the potential to wreak havok on the syntax highlighting
% of \matlab{} listings.
% It would be like \emph{crossing the streams}: it would be \emph{bad}!
%
% For instance, if you want to change the way \matlab{} keywords are typeset,
% you should use the dedicated \mlpkg{} key called \rkeyname{mlkeywordstyle}
% and eschew the \lstpkg{} key called \keyname{keywordstyle}.
% More generally, if \lstpkg{} provides a key called \meta{something} and
% \mlpkg{} provides a key called |ml|\meta{something},
% customization of your \matlab{} listings should be done with the latter, not
% the former.
%
%
% \subsection{Changing the font of Matlab listings}
%
% For compatibility reasons, the \mlpkg{} package uses the Computer Modern
% typewriter font by default.
% However, this font is far from ideal,
% because it doesn't come with a boldface version,
% and the \matlab{} editor does display some elements of \matlab{} syntax
% (section titles) in boldface.
% Therefore, I encourage you to switch to your preferred ``programmer font''
% instead; how to do that depends on which typesetting engine you use.
%
% For |pdflatex| users,
% \mlpkg{} conveniently provides a macro for easily selecting the Bera Mono
% font---which is a popular monospaced font for listings,
% and the one I used for all listings in this manual.
%^^A
% \begin{syntax}
%
%   \item[0.1] \rcmdname\mlttfamily
%
%     selects the Bera Mono font (somewhat scaled down).
%
% \end{syntax}
%^^A
% To use Bera Mono in your \matlab{} listings, you must pass |\mltttfamily| to
% \lstpkg{}' \keyname{basicstyle} key (\emph{after} loading one of the three
% styles defined by \mlpkg{}) and also---this is important---^^A
% load the \pkg{fontenc} package with option \opt{T1}:
%^^A
% \begin{verbatim}
%   \usepackage[T1]{fontenc}\end{verbatim}
%^^A
%
%
% \subsection{\mlpkg{}'s key-value interface}
%
% For each of the \mlpkg{} keys described below,
% the value assigned to it in the \mlsty{Matlab-editor} style is indicated
% on the right-hand side.
%^^A
% \begin{syntax}
%
%   \item[0.1,\color{blue}]
%     \rkeyname{mlkeywordstyle}|=|\meta{style}
%
%     This key determines the style applied to \matlab{} keywords.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,\color{black}]
%     \rkeyname{mllastelementstyle}|=|\meta{style}
%
%     The |end| keyword has different meanings depending on the context in
%     which it occurs:
%     it may be used to close a code block (e.g.\ a while loop),
%     or it may stand for the last element of an array.
%     In the first case, it gets highlighted in the same style as the other
%     \matlab{} keywords, like so: "end".
%     In the other case, it gets highlighted like ``normal text'', like so:
%     \overrideEnd.
%     This key determines the style of this keyword in cases where it means
%     ``last element''.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,false]
%     \rkeyname{mloverride}|=|\meta{\alternative{true,false}}
%     \syntaxor\rkeyname{mloverride}
%
%     By default, in inline code, \mlpkg{} highlights the |end| keyword as
%     the closing keyword (\emph{not} as the last-element keyword)
%     and highlights the four class-definition identifiers as \matlab{}
%     functions (\emph{not} as keywords), like so:
%     "end", "events", "enumeration", "methods", and "properties".
%     This key allows you to override the current context, so that those
%     five context-sensitive keywords be typeset in the style of the
%     alternative context, like so:
%     \overrideEnd{},
%     \overrideEvents{}, 
%     \overrideEnumeration{},
%     \overrideMethods{},
%     \overrideProperties{}.
%
%   \item[0.1,{\color[RGB]{160,32,240}}]
%     \rkeyname{mlstringstyle}|=|\meta{style}
%
%     This key determines the style applied to \matlab{} quoted and unquoted
%     strings.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,{\color[RGB]{34,139,34}}]
%     \rkeyname{mlcommentstyle}|=|\meta{style}
%
%     This key determines the style applied to \matlab{} to-end-of-line and
%     block comments.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1,{\bfseries\color[RGB]{34,139,34}}]
%     \rkeyname{mlsectiontitlestyle}|=|\meta{style}
%   
%     This key determines the style applied to \matlab{} section titles.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%     
%   \item[0.1,false]
%     \rkeyname{mlshowsectionrules}|=|\meta{\alternative{true,false}}
%     \syntaxor\rkeyname{mlshowsectionrules}
%
%     This key determines whether an horizontal rule gets printed above each
%     \matlab{} section title.
%
%   \item[0.1,.05]
%     \rkeyname{mlsectionrulethickness}|=|\meta{number}
%     
%     This key determines the thickness of the horizontal rule above each
%     \matlab{} section title.
%     The resulting thickness corresponds to the product of the value passed
%     to this key and the length value of |\baselineskip|.
%
%   \item[0.1,black!15]
%     \rkeyname{mlsectionrulecolor}|=|\meta{color}
%
%     This key determines the color of the horizontal rule shown above each
%     \matlab{} section title.
%
%   \item[0.1,{\color[RGB]{178,140,0}}]
%     \rkeyname{mlsyscomstyle}|=|\meta{style}
%
%     This key determines the style applied to \matlab{} system commands.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1]
%     \rkeyname{mlsharedvars}|=|\meta{list of variables}
%   \item[0.1]
%     \rkeyname{mlmoresharedvars}|=|\meta{list of variables}
%   \item[0.1]
%     \rkeyname{mldeletesharedvars}|=|\meta{list of variables}
%   \item[0.1,{\color[RGB]{0,163,163}}]
%     \rkeyname{mlsharedvarstyle}|=|\meta{style}
%
%     The first three of these four keys allow you to define, add, or remove
%     (respectively) \matlab{} variables with shared scope.
%     The last one determines the style applied to such variables;
%     the last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.1]
%     \rkeyname{mlunquotedstringdelim}|={|^^A
%       \meta{opening delimiter}|}{|\meta{closing delimiter}|}|
%
%     This key allows you to define delimiters
%     (possibly composed of multiple characters)
%     for highlighting unquoted strings;
%     the delimiters themselves do not get printed in the output.
%     Be aware that the special characters |{}#%\| must be escaped with a
%     backslash (see item~5 in subsection~4.1 of the
%     \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}).
%     Note that this key is only a tentative solution;
%     automatic highlighting of unquoted strings is a planned feature for the
%     next release of \mlpkg{}, which should make this key obsolete.
%
%   \item[0.1,{\rmfamily\itshape\color[RGB]{209,0,86}}]
%     \rkeyname{mlplaceholderstyle}|=|\meta{style}
%
%     This key determines the style applied to placeholders in code snippets.
%     The last token can be a one-parameter command,
%     such as |\textbf| or |\underbar|.
%
%   \item[0.2,true]
%     \rkeyname{mlscaleinline}|=|\meta{\alternative{true,false}}
%     \syntaxor\rkeyname{mlscaleinline}
%
%     If this key is set, any font-size specification in the basic style is
%     overriden, and inline \matlab{} code is scaled to it surroundings;
%     in other words, the font size of inline \matlab{} code is made to match
%     the local font size.
% 
%   \item[0.2,false]
%     \rkeyname{mlonlyheader}|=|\meta{\alternative{true,false}}
%     \syntaxor\rkeyname{mlonlyheader}
%
%     If this key is set, output is dropped after the first block of
%     contiguous line comments, which normally corresponds to the function's
%     header, if any.
%
% \end{syntax}
%
%
% \section{Tips and tricks}
%
% Here is a list of recommendations^^A
% ---some more opinionated than others.
% \paragraph{Stick with the \mllng{} language.}
% Defining a \lstpkg{} language based on \mllng{} is discouraged,
% for the following reason:
% \mlpkg{} performs some necessary housekeeping tasks at the beginning and
% end of each listing, but only under the condition that the name of the
% language used by the listing be \mllng{};
% therefore, \matlab{} listings are unlikely to get correctly highlighted if
% the language name differs from \mllng{}.
%
% \paragraph{Define your own style.}
% For maintainability reasons, if you're not completely satisfied with any of
% the predefined styles, you should define your own \lstpkg{} style.
% You can even base your custom style on one of the predefined styles and
% tweak it (see subsection~4.5 in the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}).
%
% \paragraph{Load the base language/style first; customize later.}
% If you want to customize the appearance of your \matlab{} listings, 
% you should use \lstpkg{}' \keyname{language} key or \keyname{style} key
% before using any other (\lstpkg{} or \mlpkg{}) key,
% because loading a language or a style ``too late'' has the potential to
% wipe out most of the current settings.
%
% \paragraph{Define macros for recurring placeholders.}
% For maintainability reasons, you should define macros for oft-used
% placeholders, e.g.
%^^A
% \begin{verbatim}
%   \newcommand\phcond{\mlplaceholder{condition}}\end{verbatim}
%
% \paragraph{For more highlights, use \lstpkg{}' \keyname{emph} key}
% If you want to highlight some identifiers in \matlab{} listings, use
% \lstpkg{}' \keyname{emph} key. Do \emph{not} use \lstpkg{}'
% \keyname{keywords} or \keyname{morekeywords} keys.
%
% \paragraph{Don't copy \& paste!}
% Do not encourage your readers to copy listings from their PDF viewer and
% then paste them in the \matlab{} editor.
% Unfortunately, it simply is \emph{not} a reliable way of distributing code,
% for at least three reasons:
%^^A
% \begin{itemize}
%   \item copying listings than span multiple pages of a PDF document is
%     tedious and error-prone;
%   \item the results of copying content from a PDF for
%     subsequent pasting vary widely from one PDF viewer to another;
%   \item line breaks introduced by \lstpkg{} for typesetting a \matlab{}
%     listing may translate to invalid \matlab{} syntax, if copied and pasted
%     \emph{verbatim}.
% \end{itemize}
%
% \paragraph{Typesetting a vertically centered tilde}
% Unfortunately, not all fonts typeset the tilde character ("~") vertically
% centered---as it is in the \matlab{} editor.
% Be aware that, if you set a font for your \matlab{} listings
% (via \lstpkg{}' \keyname{basicstyle} key) that is different from \mlpkg{}'s
% default (a scaled-down version of Bera Mono), tilde characters occuring in
% your listings may get typeset vertically off-center.
% Because a good, font-independent workaround seems out of reach,
% I refer you to \url{http://tex.stackexchange.com/q/312/21891},
% where you will find a list of ad-hoc solutions.
%
% \paragraph{Avoid literate replacements like the plague!}
% The \pkg{mcode} package predefines so-called ``literate replacements''
% (see subsection~5.4 in the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation}),
% e.g.\ for printing~``\(\leq\)'' in place of each instance of~``"<="''.
% I deliberately chose not to define any such literate replacements in
% \mlpkg{} because I think that, rather than improving code readability,
% they have a potential to confuse and mislead your readers.
% In particular, newcomers to the programming language may not immediately
% realize that those symbols are not part of the language's syntax;
% they may ascribe literal meaning to them and attempt to reproduce them in
% their editor or IDE.
% How counterproductive!
% Of course, if you insist, you can still define your own literate
% replacements.
%
%
% \part{Miscellaneous}
% 
% \section{To-do list}
%
% \paragraph{Automatic highlighting of unquoted strings}
% In the current version of \mlpkg{}, unquoted strings will only be
% highlighted as strings if you delimit them with custom delimiters
% (defined via the \rkeyname{mlunquotedstringdelim} key).
% However, I have plans to implement an automatic approach in a future
% release.
% Note that this feature will make the \rkeyname{mlunquotedstringdelim} key
% obsolete.
%
% \paragraph{Increased support for \octave{}'s syntax}
% Support for \octave{}'s idiosyncratic syntax---^^A
% e.g. |endif| and |endwhile| keywords---^^A
% will be added in a future release of \mlpkg{}.
%
%
% \section{Missing features and known issues}
%
% Although \mlpkg{} does a reasonably good job at replicating the syntax
% highlighting performed by the \matlab{} editor, some problems remain.
% Here is a list of known, currently unresolved problems.
% 
% \paragraph{No automatic highlighting of variables with shared scope}
% Unfortunately, automatic highlighting of variables with shared scope
% would require multiple passes, which the \lstpkg{} package cannot do.
% However, I believe that the number of variables in your \matlab{} code
% should be small enough^^A
% ---otherwise, your \matlab{} code is probably not sound!---^^A
% that you can afford to highlight those variables manually, if you insist on
% highlighting them at all.
% 
% \paragraph{No highlighting of unterminated strings}
% Because \lstpkg{} cannot look very far ahead, I haven't found an easy way of
% checking whether an opening string delimiter is missing a matching (closing)
% string delimiter on the same line.
%
% \paragraph{Illegal syntax tends to yield incorrect syntax highlighting}
% For example, the \matlab{} editor would highlight the |end| keyword in the
% listing below, not as closing keyword ("end"), but as last-element keyword
% (\overrideEnd{}).
%^^A
% \begin{lstsample}{}{\lstset{style=nonbnoframe}}
%    \begin{lstlisting}[
%      style=Matlab-editor,
%      basicstyle=\mlttfamily,
%      numbers=none]
%    if=end
%    \end{lstlisting}
% \end{lstsample}
%
% \paragraph{Some section titles fail to be highlighted as such}
% In \matlab{}, a line containing only ``\texttt{\%\%}'' and blank
% characters is a section title.
% \mlpkg{} incorrectly highlights such a line in comment style.
%^^A
% \begin{lstsample}{}{\lstset{style=nonbnoframe}}
%    \begin{lstlisting}[
%      style=Matlab-editor,
%      numbers=none,
%      basicstyle=\mlttfamily,
%      numbers=none]
%    %% This is a section title
%    %  and so is the next line
%    %%
%    % but it gets highlighted
%    % like a comment.
%    \end{lstlisting}
% \end{lstsample}
%
% \paragraph{\lstpkg{}' \keyname{keespaces} key messes up section-title rules}
% If both \lstpkg{}' \keyname{keepspaces} and \mlpkg{}'s
% \rkeyname{mlshowsectionrules} are set, section titles that start by some
% white space get pushed to the right.
%^^A
% \begin{lstsample}{}{\lstset{style=nonbnoframe}}
%    \begin{lstlisting}[
%      style=Matlab-editor,
%      basicstyle=\mlttfamily,
%      numbers=none,
%      keepspaces,
%      mlshowsectionrules]
%        %% the rule gets pushed to the right...
%    \end{lstlisting}
% \end{lstsample}
% 
% \paragraph{``Runaway'' block comments end prematurely (in some cases)} 
% \matlab{} requires opening and closing delimiters of block comments to each
% be on a line on its own, without any visible character, but \mlpkg{}
% incorrectly considers block comments closed even in some cases where this
% rule is infringed.
% For example, in the listing below, the \matlab{} editor would typeset
% "a = 1" in comment style.
%^^A
% \begin{lstsample}{}{\lstset{style=nonbnoframe}}
%    \begin{lstlisting}[
%      style=Matlab-editor,
%      basicstyle=\mlttfamily,
%      numbers=none]
%    %{
%      "runaway"
%     block
%     comment %}
%    a = 1
%    \end{lstlisting}
% \end{lstsample}
%
%
% \section{Bug reports and feature suggestions}
%
% The development version of \mlpkg{} is currently hosted on Bitbucket at
% \href{http://bitbucket.org/Jubobs/matlab-prettifier/}
%   {Jubobs/matlab-prettifier}.
% If you find an issue in \mlpkg{} that this manual does not mention,
% if you would like to see a feature implemented in the package,
% or if you can think of ways in which the \mlpkg{} documentation could be
% improved, please open a ticket in the Bitbucket repository's issue tracker;
% alternatively, you can send me an email at
% \href{mailto:jubobs.matlab.prettifier@gmail.com}
%   {jubobs.matlab.prettifier@gmail.com}
%
%
% \section{Acknowledgments}
% Thanks to the developers of the \lstpkg{} package,
% without which \mlpkg{} would never have existed.
% I'm also in debt to many
% \href{http://tex.stackexchange.com}{TeX.SX} users for their help,
% encouragements, and suggestions.
% Thanks in particular to David Carlisle, Marco Daniel,
% Enrico Gregorio (egreg), Harish Kumar, Heiko Oberdiek, and Robert Schlicht.
% Thanks also to the good people at \href{http://www.ctan.org}{CTAN}
% for hosting the package.
%
%
% \makeatletter
% \def\index@prologue{\part{Index}\markboth{Index}{Index}}
% \makeatother
% \StopEventually{^^A
%   \clearpage^^A
%   \PrintChanges^^A
%   \setcounter{IndexColumns}{2}^^A
%   \PrintIndex^^A
% }
%
%
% \part{Implementation}
%
% Be aware that, for ``namespacing'', the \mlpkg{} package uses, not a prefix,
% but the ``|mlpr|''~suffix (preceded by an |@| character) throughout.
%
%
% \section{Preliminary checks}
%
% \begin{macro}{\lstoptcheck@mlpr}
%   Because the \lstpkg{} options \opt{noaspects}, \opt{0.21}, and
%   \opt{savemem} are incompatible with \pkg{matlab-prettifier}, checking
%   whether the \lstpkg{} package has been loaded with any of those options is
%   a good idea; if so, we should issue an error.
%   This macro checks whether \lstpkg{} was loaded with a given option
%   and, if so, throws an error.
%    \begin{macrocode}
\newcommand\lstoptcheck@mlpr[1]
{%
  \@ifpackagewith{listings}{#1}%
  {
    \PackageError{matlab-prettifier}%
      {incompatible listings' option #1}%
      {%
        Make sure the `listings' package
        doesn't get loaded with option `#1'%
      }
  }
  {}
}
%    \end{macrocode}
% \end{macro}
% We now use this macro to make sure that none of the problematic \lstpkg{}
% options has been passed to \lstpkg{} during an earlier loading of that
% package.
%    \begin{macrocode}
\lstoptcheck@mlpr{noaspects}
\lstoptcheck@mlpr{0.21}
\lstoptcheck@mlpr{savemem}
%    \end{macrocode}
%
%
% \section{Package options}
%
% \paragraph{Framed listings}
% \begin{macro}{\ifframed@mlpr@}
% This option draws a frame around each listing by default.
%    \begin{macrocode}
\newif\ifframed@mlpr@
\DeclareOption{framed}{\framed@mlpr@true}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Numbered lines}
% \begin{macro}{\ifnumbered@mlpr@}
% This option prints line numbers to the left of each listing by default.
%    \begin{macrocode}
\newif\ifnumbered@mlpr@
\DeclareOption{numbered}{\numbered@mlpr@true}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Draft}
% This option is simply passed to \lstpkg{}.
%    \begin{macrocode}
\DeclareOption{draft}{\PassOptionsToPackage{\CurrentOption}{listings}}
%    \end{macrocode}
%
% \paragraph{Final}
% This option is simply passed to \lstpkg{}.
%    \begin{macrocode}
\DeclareOption{final}{\PassOptionsToPackage{\CurrentOption}{listings}}
%    \end{macrocode}
%
% \paragraph{Discard undefined options}
% We discard any other option passed to \mlpkg{} by the user
% and issue a warning.
%    \begin{macrocode}
\DeclareOption*%
{%
  \OptionNotUsed
  \PackageWarning{matlab-prettifier}{Unknown `\CurrentOption' option}
}
%    \end{macrocode}
% \paragraph{Process options}
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
%
% \section{Required packages}
% 
% The \mlpkg{} package require three packages without any package option:
% the \pkg{textcomp} package, in order to use \lstpkg{}' \keyname{upquote}
% key;
% the \pkg{xcolor} package, in order to color our \matlab{} code;
% and, of course, the \lstpkg{} package.
%    \begin{macrocode}
\RequirePackage{textcomp}[2005/09/27]
\RequirePackage{xcolor}[2007/01/21]
\RequirePackage{listings}[2013/08/26]
%    \end{macrocode}
%
%
% \section{Definition of the \texorpdfstring{\mllng}{Matlab-pretty} language}
%
% \paragraph{Language name}
% \begin{macro}{\language@mlpr}
%   To avoid code duplication in this package file, we define a macro that
% expands to the name of our new language, \mllng{}.
%    \begin{macrocode}
\newcommand\language@mlpr{Matlab-pretty}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\languageNormedDefd@mlpr}
%   However, because \lstpkg{} ``normalizes'' language names internally,
%   we will also need to define a macro that expands to the normalized name of
%   the new language.
%    \begin{macrocode}
\expandafter\lst@NormedDef\expandafter\languageNormedDefd@mlpr%
  \expandafter{\language@mlpr}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Language definition}
% We can now define our new \lstpkg{} language, using some |\expandafter|
% trickery on |\lstdefinelanguage|.
%    \begin{macrocode}
\expandafter\expandafter\expandafter\lstdefinelanguage\expandafter%
{\language@mlpr}
{%
%    \end{macrocode}
%
% \paragraph{Case sensitivity}
% \matlab{} is a case-sensitive language.
%    \begin{macrocode}
  sensitive=true,
%    \end{macrocode}
%
% \paragraph{Forbidden characters in identifiers}
% By default, \lstpkg{} allows ``\$'' and ``@'' to occur in identifiers,
% but those characters are not valid \matlab{} identifiers.
%    \begin{macrocode}
  alsoother={\$@},
%    \end{macrocode}
%
% \paragraph{Character-table adjustments}
% In order to keep track of the context, we need to modify the character
% table a bit.
%    \begin{macrocode}
  MoreSelectCharTable=\MoreSelectCharTable@mlpr,
%    \end{macrocode}
%
% \paragraph{Keywords}
%
% The keywords defined below are based on the list returned by the
% \matlabver{} "iskeyword" function and the four class-definition keywords^^A
% ---which are omitted by the "iskeyword" function.
% Because different \matlab{} keywords affect the context in different ways,
% we use several classes of \pkg{listings} keywords to handle them.
%
% The following keywords open a block unrelated to class definition. 
%    \begin{macrocode}
  morekeywords=[1]%
  {% 
    for,
    if,
    otherwise,
    parfor,
    spmd,
    switch,
    try,
    while,
  },
  keywordstyle=[1]\processOpRegKW@mlpr,
%    \end{macrocode}
% Most of the following keywords (nicknamed ``middle'' keywords herein) can
% only occur within a block opened by the keywords listed above^^A
% ---"function" and "return" are exceptions, but, as far as I can tell,
% seem to have the same effects on syntax highlighting as the others---^^A
% and are unrelated to class definition.
%    \begin{macrocode}
  morekeywords=[2]%
  {%
    break,
    case,
    catch,
    continue,
    else,
    elseif,
    function,
    return,
  }, 
  keywordstyle=[2]\processMidKW@mlpr,
%    \end{macrocode}
% The following two keywords are ``standalone'';
% they don't open or close any block.
%    \begin{macrocode}
  morekeywords=[3]%
  {%
    global,
    persistent,
  }, 
  keywordstyle=[3]\processStdaKW@mlpr,
%    \end{macrocode}
% The "classdef" keyword interacts with other keywords in a unique fashion;
% therefore, we dedicate a whole class of \lstpkg{} keywords to it. 
%    \begin{macrocode}
  morekeywords=[4]{classdef},  
  keywordstyle=[4]\processClassdefKW@mlpr,
%    \end{macrocode}
% We dedicate a class of \lstpkg{} keywords to the four \matlab{} keywords
% that only occur within a class-definition block,
% namely \overrideEvents{}, \overrideEnumeration{}, \overrideMethods{},
% and \overrideProperties{}.
%    \begin{macrocode}
  morekeywords=[5]%
  {%
    enumeration,
    events,
    methods,
    properties,
  },
  keywordstyle=[5]\processMidClassdefKW@mlpr,
%    \end{macrocode}
% The |end| keyword has a very peculiar behavior and deserves its own keyword
% class.
%    \begin{macrocode}
  morekeywords=[6]{end},
  keywordstyle=[6]\processEndKW@mlpr,
%    \end{macrocode}
%
% \paragraph{Strings}
% We simply use \lstpkg{}' built-in mechanism for highlighting \matlab{}
% quoted string\ldots with a twist; more details follow.
%    \begin{macrocode}
  morestring=[m]',
  stringstyle=\processString@mlpr,
%    \end{macrocode}
%
% \paragraph{Comments \& section titles}
% Delimiters for to-end-of-line and block comments are defined below.
%    \begin{macrocode}
  morecomment=[l]\%,
  morecomment=[n]{\%\{\^^M}{\%\}\^^M},
  commentstyle=\commentStyle@mlpr,
%    \end{macrocode}
% The line-continuation token ("..."), which starts a to-end-of-line comment,
% is treated separately.
%    \begin{macrocode}
  moredelim=**[il][\processDotDotDot@mlpr]{...},
%    \end{macrocode}
% Section titles, as special comments that get highlighted in a style
% different to that of regular comments, must also be treated separately.
%    \begin{macrocode}
  moredelim=[l][\processSectionTitle@mlpr]{\%\%\ },
%    \end{macrocode}
%
% \paragraph{System commands}
% System commands are handled in a straightforward manner by an |l|-type
% delimiter.
%    \begin{macrocode}
  moredelim=[l][\syscomStyle@mlpr]!,
%    \end{macrocode}
%
% \paragraph{Required \lstpkg{} aspects} 
% We now only need to specify the required \lstpkg{} ``aspects''.
%    \begin{macrocode}
}[
  keywords,
  strings,
  comments,
]
%    \end{macrocode}
%
%
% \section{State variables}
% We define a number of \TeX{} counters and switches that will be used as
% ``state variables'', to keep track of the context.
%
% \paragraph{Counters}
% \begin{macro}{\netBracketCount@mlpr}
% This counter is used to keep a net running count of opening and closing
% brackets---roughly speaking.
% When an opening bracket---be it round, square or curly---is encountered,
% the counter is incremented;
% conversely, when a closing bracket is encountered, the counter is
% decremented.
% I write ``roughly speaking'', because that counter gets reset on some
% occasions; more details follow.
%    \begin{macrocode}
\newcount\netBracketCount@mlpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\blkLvl@mlpr}
% This counter counter is used to keep track of the block nesting level.
%    \begin{macrocode}
\newcount\blkLvl@mlpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\blkLvlAtClassdef@mlpr}
% This counter is used to keep track of the block nesting level at which the
% last "classdef" keyword occured.
%    \begin{macrocode}
\newcount\blkLvlAtClassdef@mlpr
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Switches}
% \begin{macro}{\ifClosingEndKW@mlpr@}
% This switch determines whether the |end| keyword acts as a closing keyword
% or as last-element keyword in the current context.
%    \begin{macrocode}
\newif\ifClosingEndKW@mlpr@    \ClosingEndKW@mlpr@true
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ifInClassdef@mlpr@}
% This switch determines whether we're within a class-definition block or not.
%    \begin{macrocode}
\newif\ifInClassdef@mlpr@        \InClassdef@mlpr@false
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ifInStr@mlpr@}
% This switch determines whether we're inside a string or not.
%    \begin{macrocode}
\newif\ifInStr@mlpr@                  \InStr@mlpr@false
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ifVisCharOccured@mlpr@}
% This switch is used to keep track of whether visible characters have
% occured on the current line.
%    \begin{macrocode}
\newif\ifVisCharOccured@mlpr@\VisCharOccured@mlpr@false
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ifInSecTitle@mlpr@}
% This switch determines whether we're inside a section title or not.
%    \begin{macrocode}
\newif\ifInSecTitle@mlpr@        \InSecTitle@mlpr@false
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ifDroppingOutput@mlpr@}
% This switch determines whether we're passed the first contiguous block of
% line comments (function header).
%    \begin{macrocode}
\newif\ifDroppingOutput@mlpr@\DroppingOutput@mlpr@false
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Helper macros for resetting state variables}
% The following macros are used to reset counters and switches.
% \begin{macro}{\resetEndKW@mlpr}
% This macro restores the |end| keyword as a closing keyword.
%    \begin{macrocode}
\newcommand\resetEndKW@mlpr
{%
  \global\ClosingEndKW@mlpr@true%
  \global\netBracketCount@mlpr=0%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\resetClassdefKW@mlpr}
% This macro reinitializes state variables related to class definition.
%    \begin{macrocode}
\newcommand\resetClassdefKW@mlpr
{%
  \global\InClassdef@mlpr@false%
  \global\blkLvl@mlpr=0%
  \global\blkLvlAtClassdef@mlpr=0%
}
%    \end{macrocode}
% \end{macro}
%
%
% \section{Processing of syntactic elements}
%
% (The overarching algorithm is not documented here;
% in a future release, perhaps.)
%
% \paragraph{Processing of brackets}
% An opening and or a closing brackets occuring in a \matlab{} listing affects
% the context;
% for instance, an |end| keyword is always interpreted as a closing keyword if
% it is immediately preceded by a closing bracket, no matter what comes
% before that.
% To keep track of the context, we must update our state variables every time
% a bracket is encountered.
%
% \begin{macro}{\MoreSelectCharTable@mlpr}
% This macro, which is passed to \lstpkg{}' \keyname{MoreSelectCharTable} key
% in the definition of \mllng{}, allows us to dictate what happens when a
% bracket or a semicolon is encountered.
%    \begin{macrocode}
\newcommand\MoreSelectCharTable@mlpr
{%
%    \end{macrocode}
%
% \begin{macro}{\roundBktOp@mlpr}
% We store the original definition of ``|(|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processOpenBracket@mlpr{`(}{\roundBktOp@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\squareBktOp@mlpr}
% We store the original definition of ``|[|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processOpenBracket@mlpr{`[}{\squareBktOp@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\curlyBktOp@mlpr}
% We store the original definition of ``|{|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processOpenBracket@mlpr{`\{}{\curlyBktOp@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\roundBktCl@mlpr}
% We store the original definition of ``|)|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processCloseBracket@mlpr{`)}{\roundBktCl@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\squareBktCl@mlpr}
% We store the original definition of ``|]|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processCloseBracket@mlpr{`]}{\squareBktCl@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\curlyBktCl@mlpr}
% We store the original definition of ``|}|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processCloseBracket@mlpr{`\}}{\curlyBktCl@mlpr}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\semicolon@mlpr}
% We store the original definition of ``|;|'' from the default character
% table in a dedicated macro and modify the behavior of that character.
%    \begin{macrocode}
  \processSemicolon@mlpr{`;}{\semicolon@mlpr}%
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\processOpenBracket@mlpr}
% This macro is used to ``hook into'' opening-bracket characters and update
% state variables every time such a character is encountered in \lstpkg{}'
% ``processing mode''.
%    \begin{macrocode}
\newcommand\processOpenBracket@mlpr[2]
{%
  \lst@DefSaveDef{#1}#2%
  {%
    #2%
    \ifnum\lst@mode=\lst@Pmode\relax%
      \global\ClosingEndKW@mlpr@false%
      \global\advance\netBracketCount@mlpr by \@ne%
    \fi
  }%  
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\processCloseBracket@mlpr}
% This macro is used to ``hook into'' closing-bracket characters and update
% state variables every time such a character is encountered in \lstpkg{}'
% ``processing mode''.
%    \begin{macrocode}
\newcommand\processCloseBracket@mlpr[2]
{%
  \lst@DefSaveDef{#1}#2%
  {%
    #2%
    \ifnum\lst@mode=\lst@Pmode\relax%
      \ifClosingEndKW@mlpr@%
        \netBracketCount@mlpr=0%
      \else
        \global\advance\netBracketCount@mlpr by \m@ne%
        \ifnum\netBracketCount@mlpr>0%
        \else
          \global\ClosingEndKW@mlpr@true%
        \fi
      \fi
    \fi
  }%  
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\processSemicolon@mlpr}
% This macro is used to ``hook into'' the semicolon character and update
% state variables every time such a character is encountered in \lstpkg{}'
% ``processing mode''.
%    \begin{macrocode}
\newcommand\processSemicolon@mlpr[2]
{%
  \lst@DefSaveDef{#1}#2%
  {%
    #2%
    \ifnum\lst@mode=\lst@Pmode\relax%
      \resetEndKW@mlpr%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Processing of keywords}
% The following macros are used for updating state variables every time a
% keyword is encountered in \lstpkg{} ``processing mode''.
%
% \begin{macro}{\processOpRegKW@mlpr}
% This macro updates state variables every time an opening keyword is
% processed, and applies keyword style.
%    \begin{macrocode}
\newcommand\processOpRegKW@mlpr
{%
  \resetEndKW@mlpr%
  \global\advance\blkLvl@mlpr\@ne%
  \keywordStyle@mlpr%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\processMidKW@mlpr}
% This macro updates state variables every time a ``middle'' keyword is
% processed, and applies keyword style.
%    \begin{macrocode}
\newcommand\processMidKW@mlpr
{%
  \resetEndKW@mlpr%
  \keywordStyle@mlpr%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\processStdaKW@mlpr}
% As far as I can tell, ``standalone'' keywords and ``middle'' keywords
% affect the context in the same way;
% therefore, we simply reuse |\processMidKW@mlpr| here.
%    \begin{macrocode}
\newcommand\processStdaKW@mlpr\processMidKW@mlpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\processClassdefKW@mlpr}
% This macro updates state variables every time the "classdef" keyword is
% processed, and applies keyword style.
%    \begin{macrocode}
\newcommand\processClassdefKW@mlpr
{%
  \resetEndKW@mlpr%
  \global\InClassdef@mlpr@true%
  \global\blkLvlAtClassdef@mlpr=\blkLvl@mlpr%
  \global\advance\blkLvl@mlpr\@ne%
  \keywordStyle@mlpr%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\processMidClassdefKW@mlpr}
% This macro updates state variables every time one of the four keywords
% that only occur within a class-definition block is processed, and applies
% the appropriate style.
%    \begin{macrocode}
\newcommand\processMidClassdefKW@mlpr
{%
  \ifOverridecontext@mlpr@%
    \keywordStyle@mlpr%
  \else
    \ifInClassdef@mlpr@%
      \resetEndKW@mlpr%
      \global\advance\blkLvl@mlpr\@ne%
      \keywordStyle@mlpr%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\processEndKW@mlpr}
% This macro updates state variables every time the |end| keyword is
% processed, and applies the appropriate style.
%    \begin{macrocode}
\newcommand\processEndKW@mlpr
{%
  \ifOverridecontext@mlpr@%
    \lastElemStyle@mlpr%
  \else
    \ifClosingEndKW@mlpr@%
      \ifnum\blkLvl@mlpr>0%
        \global\advance\blkLvl@mlpr\m@ne%
      \fi
      \ifnum\blkLvl@mlpr=\blkLvlAtClassdef@mlpr%
        \global\InClassdef@mlpr@false%
      \fi
      \keywordStyle@mlpr%
    \else
      \lastElemStyle@mlpr%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Processing of strings}
% \begin{macro}{\processString@mlpr}
% This macro records that a string has just started by setting the appropriate
% switch, and applies string style.
%    \begin{macrocode}
\newcommand\processString@mlpr
{%
    \global\InStr@mlpr@true%
    \stringStyle@mlpr%
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Processing of line-continuation tokens}
% \begin{macro}{\processDotDotDot@mlpr}
% This macro typesets the line-continuation token in the style of our
% \matlab{} keywords,
% prohibits any mode changes on the rest of the current line,
% and applies comment style to the rest of the current line.
%    \begin{macrocode}
\newcommand\processDotDotDot@mlpr
{%
  \lst@CalcLostSpaceAndOutput%
  {\keywordStyle@mlpr...}%
  \lst@modetrue%
  \lst@Lmodetrue%
  \commentStyle@mlpr%
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Processing of section titles}
% First, we need to define a few length macros in order to draw the horizontal
% rule that \matlab{} shows (by default) above each section title.
% \begin{macro}{\emHeight@mlpr}
% We will use this length to store the height of the ``M'' character in the
% current font.
%    \begin{macrocode}
\newlength\emHeight@mlpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\jayDepth@mlpr}
% We will use this length to store the depth of letter ``j'' in the current
% font.
%    \begin{macrocode}
\newlength\jayDepth@mlpr
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\sectionRuleOffset@mlpr}
% We will use this length to store the result of our calculations for the
% vertical offset required.
%    \begin{macrocode}
\newlength\sectionRuleOffset@mlpr
%    \end{macrocode}
% \end{macro}
% Let's proceed\ldots
% \begin{macro}{\processSectionTitle@mlpr}
% This macro is invoked when a |%%| delimiter is encountered.
%    \begin{macrocode}
\newcommand\processSectionTitle@mlpr
{%
  \ifInSecTitle@mlpr@%
    \sectionTitleStyle@mlpr%
  \else
%    \end{macrocode}
% If visible characters have already been encountered before the |%%| on the
% current line, this line is simply typeset as a to-end-of-line comment.
%    \begin{macrocode}
    \ifVisCharOccured@mlpr@%
      \commentStyle@mlpr%
%    \end{macrocode}
% Otherwise, a section title starts here;
% we update the relevant state variables and, if the
% \rkeyname{mlshowsectionrules} key has been set, we draw a horizontal rule.
%    \begin{macrocode}
    \else % a section title is starting here
      \global\InSecTitle@mlpr@true%
      \resetEndKW@mlpr%
      \ifShowSectRules@mlpr@%
        \drawSectionRule@mlpr%
      \fi
      \sectionTitleStyle@mlpr%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\drawSectionRule@mlpr}
% This helper macro is used for drawing a horizontal rule just above the
% current line. 
%    \begin{macrocode}
\newcommand\drawSectionRule@mlpr
{%
%    \end{macrocode}
% We measure the height of the ``M'' character and the depth of the ``j''
% character, which we then use to calculate the required vertical offset.
%    \begin{macrocode}
  \setlength\emHeight@mlpr{\fontcharht\font`M}%
  \setlength\jayDepth@mlpr{\fontchardp\font`j}%
  \setlength\sectionRuleOffset@mlpr%
  {%
    \dimexpr.5\emHeight@mlpr%
           +.5\baselineskip%
           -.5\jayDepth@mlpr\relax%
  }%
%    \end{macrocode}
% We now draw a rule as required (color and dimensions).
%    \begin{macrocode}
  \bgroup%
  \color{\sectionRuleColor@mlpr}%
    \makebox[0em][l]%
    {%
      \raisebox{\sectionRuleOffset@mlpr}[0pt][0pt]%
        {\rule{\lst@linewidth}{\sectionRuleRT@mlpr\baselineskip}}%
    }%
  \egroup%
}
%    \end{macrocode}
% \end{macro}
%
% \section{Hooking into \lstpkg{}' hooks}
%
% We apply some necessary patches in a number of \lstpkg{}' hooks;
% but first, we define a few helper macros.
%
% \paragraph{Helper macros related to hooks}
% \begin{macro}{\localFontSize@mlpr}
% This macro will be used to save the current font size.
%    \begin{macrocode}
\newcommand\localFontSize@mlpr{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\localBaselineskip@mlpr}
% This macro will be used to save the current value of |\baselineskip|.
%    \begin{macrocode}
\newcommand\localBaselineskip@mlpr{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\scaleInlineCode@mlpr}
% This helper macro is for setting the font size of inline code to the local
% font size (only if the \rkeyname{mlscaleinline} key is set).
%    \begin{macrocode}
\newcommand\scaleInlineCode@mlpr
{%
  \lst@ifdisplaystyle%
  \else
    \ifScaleInline@mlpr@%
%    \end{macrocode}
% We save the values of the current font size and of |\baselineskip| into our
% dedicated macros\ldots
%    \begin{macrocode}
      \let\localFontSize@mlpr\f@size%
      \let\localBaselineskip@mlpr\f@baselineskip%
%    \end{macrocode}
% \ldots and we use the basic style but we update the font size. 
%    \begin{macrocode}
      \expandafter\def\expandafter\lst@basicstyle\expandafter%
      {%
        \lst@basicstyle%
        \fontsize{\localFontSize@mlpr}{\localBaselineskip@mlpr}%
        \selectfont%
      }%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\dropOutputAfterHeader@mlpr}
% This macro detects when the first block (if any) of contiguous of line
% comments (function header) ends and drops output thereafter.
%    \begin{macrocode}
\newcommand\dropOutputAfterHeader@mlpr
{%
  \ifonlyheader@mlpr@%
    \ifnum\lst@lineno>1%  
      \lst@ifLmode%
      \else
%    \end{macrocode}
% At this stage, the header has definitely ended.
% If we've already begun dropping output, we don't do anything.
%    \begin{macrocode}
        \ifDroppingOutput@mlpr@%
%    \end{macrocode}
% Otherwise, we begin dropping output now and we set the switch accordingly.
%    \begin{macrocode}
        \else
          \lst@EnterMode\lst@Pmode{}%
          \lst@BeginDropOutput\lst@Pmode%
        \fi
        \global\DroppingOutput@mlpr@true%
%    \end{macrocode}
%    \begin{macrocode}
      \fi
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{InitVarsEOL}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToInitVarsEOL@mlpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{InitVarsEOL} hook.
%    \begin{macrocode}
\newcommand\addedToInitVarsEOL@mlpr{}
\lst@AddToHook{InitVarsEOL}{\addedToInitVarsEOL@mlpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToInitVarsEOL@mlpr}
% The |\addedToInitVarsEOL@mlpr| macro is let to this one under certain
% conditions (more details follow).
%    \begin{macrocode}
\newcommand\@ddedToInitVarsEOL@mlpr
{%
%    \end{macrocode}
% \lstpkg{}' built-in mechanism for handling \matlab{} string does not cover
% the illegal case in which an opening string delimiter is not followed by
% any matching (closing) string delimiter on the same line.
% More specifically, \lstpkg{} incorrectly highlights such a broken string
% literal as a bona-fide \matlab{} string.
% We improve the situation somewhat, by only highlighting as a string the line
% containing the unmatched opening delimiter, not the lines that follow it:
%    \begin{macrocode}
  \ifInStr@mlpr@%
    \global\InStr@mlpr@false%
    \lst@LeaveMode%
  \fi
%    \end{macrocode}
% Clearly, at the very beginning of a line, we're not (not yet, anyway)
% within a section title, and no visible character has yet occured on that
% line.
%    \begin{macrocode}
  \global\InSecTitle@mlpr@false%
  \global\VisCharOccured@mlpr@false%
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{EndGroup}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToEndGroup@mlpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{EndGroup} hook.
%    \begin{macrocode}
\newcommand\addedToEndGroup@mlpr{}
\lst@AddToHook{EndGroup}{\addedToEndGroup@mlpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToEndGroup@mlpr}
% The |\addedToEndGroup@mlpr| macro is let to this one under certain
% conditions (more details follow).
% If we were in a string before when \hookname{EndGroup} hook was called,
% we're now exiting it;
% therefore, the relevant switch must be reset.
%    \begin{macrocode}
\newcommand\@ddedToEndGroup@mlpr{\global\InStr@mlpr@false}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{PostOutput}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToPostOutput@mlpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{PostOutput} hook.
%    \begin{macrocode}
\newcommand\addedToPostOutput@mlpr{}
\lst@AddToHook{PostOutput}{\addedToPostOutput@mlpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToPostOutput@mlpr}
% The |\addedToPostOutput@mlpr| macro is let to this one under certain
% conditions (more details follow).
% If the last processed character was not white space
% (this check is necessary if \lstpkg{}' \keyname{keepspaces} key is set),
% we set the relevant switch.
%    \begin{macrocode}
\newcommand\@ddedToPostOutput@mlpr
{%
  \lst@ifwhitespace%
  \else
    \global\VisCharOccured@mlpr@true%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{Output}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToOutput@mlpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{Output} hook.
%    \begin{macrocode}
\newcommand\addedToOutput@mlpr{}
\lst@AddToHook{Output}{\addedToOutput@mlpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToOutput@mlpr}
% The |\addedToOutput@mlpr| macro is let to this one under certain
% conditions (more details follow).
% If the \rkeyname{mlonlyheader} is set, we begin dropping output as soon as
% we detect that the first contiguous block of line comments has been passed.
%    \begin{macrocode}
\newcommand\@ddedToOutput@mlpr{\dropOutputAfterHeader@mlpr}
%    \end{macrocode}
% \end{macro}
% 
% \paragraph{\hookname{OutputOther}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% \begin{macro}{\addedToOutputOther@mlpr}
% We add this macro (initially empty) to \lstpkg{}'
% \hookname{OutputOther} hook.
%    \begin{macrocode}
\newcommand\addedToOutputOther@mlpr{}
\lst@AddToHook{OutputOther}{\addedToOutputOther@mlpr}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ddedToOutputOther@mlpr}
% The |\addedToOutputOther@mlpr| macro is let to this one under certain
% conditions (more details follow).
% If the \rkeyname{mlonlyheader} is set, we begin dropping output as soon as
% we detect that the first contiguous block of line comments has been passed.
%    \begin{macrocode}
\newcommand\@ddedToOutputOther@mlpr{\dropOutputAfterHeader@mlpr}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{PreInit}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% Because the |\lst@AddToHook| affects hooks globally
% (i.e.\ for all listings),
% we must apply our patches only when required, i.e.\ 
% in listings that use \mllng{}, and not in others.
% The \hookname{PreInit}, which is called at the very beginning of each
% listing, is where we do that.
% \begin{macro}{\addedToPreInitHook@mlpr}
% In this macro, which we add to \lstpkg{}' \hookname{PreInit} hook, we check
% whether |\lst@language| and |\languageNormedDefd@mlpr| expand (once) to the
% same replacement text and only apply our patches under that condition.
%    \begin{macrocode}
\newcommand\addedToPreInitHook@mlpr
{%
  \ifx\lst@language\languageNormedDefd@mlpr%
    \scaleInlineCode@mlpr%
    \renewcommand\addedToInitVarsEOL@mlpr\@ddedToInitVarsEOL@mlpr%
    \renewcommand\addedToEndGroup@mlpr\@ddedToEndGroup@mlpr%
    \renewcommand\addedToPostOutput@mlpr\@ddedToPostOutput@mlpr%
    \renewcommand\addedToOutput@mlpr\@ddedToOutput@mlpr%
    \renewcommand\addedToOutputOther@mlpr\@ddedToOutputOther@mlpr%
    \DroppingOutput@mlpr@false%
\fi
}
\lst@AddToHook{PreInit}{\addedToPreInitHook@mlpr}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{\hookname{DeInit}}
% (See the
% \href{http://www.ctan.org/pkg/listings}{\lstpkg{} documentation} 
% for more details on this hook.)
% In the \hookname{DeInit} hook, which is called at the very end of each
% listing, we carry out some housekeeping tasks if the current listing uses
% \mllng{}.
% \begin{macro}{\addedToDeInitHook@mlpr}
% In this macro, which we add to \lstpkg{}' \hookname{DeInit} hook, we check
% whether |\lst@language| and |\languageNormedDefd@mlpr| expand (once) to the
% same replacement text and, under that condition, we reset all state
% variables.
%    \begin{macrocode}
\newcommand\addedToDeInitHook@mlpr
{%
  \ifx\lst@language\languageNormedDefd@mlpr%
    \resetEndKW@mlpr%
    \resetClassdefKW@mlpr%
    \global\InStr@mlpr@false%
    \global\VisCharOccured@mlpr@false%
    \global\InSecTitle@mlpr@false%
    \global\DroppingOutput@mlpr@false%
  \fi
}
\lst@AddToHook{DeInit}{\addedToDeInitHook@mlpr}
%    \end{macrocode}
% \end{macro}
%
%
% \section{Key-value interface}
%
% We extend \lstpkg{}' key-value interface by defining several additional
% keys, which we will use to define three \lstpkg{} styles, and which will
% allow the user to customize the style of their \matlab{} listings,
% should they which to do so.
% All \mlpkg{} keys are prefixed by~``|ml|'', so that the user can easily
% distinguish them from ``native'' \lstpkg{} keys.
%
% \paragraph{Keywords}
% \begin{lstkey}{mlkeywordstyle}
% \begin{macro}{\keywordStyle@mlpr}
%   In the definition of \mllng{}, we used several classes of \lstpkg{}
%   keywords to handle the different \matlab{} keywords;
%   here is a style key to ``rule them all''.
%    \begin{macrocode}
\newcommand\keywordStyle@mlpr{}
\lst@Key{mlkeywordstyle}\relax%
  {\renewcommand\keywordStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \begin{lstkey}{mllastelementstyle}
% \begin{macro}{\lastElemStyle@mlpr}
%   This key determines the style applied to the last-element keyword.
%    \begin{macrocode}
\newcommand\lastElemStyle@mlpr{}
\lst@Key{mllastelementstyle}\relax%
  {\renewcommand\lastElemStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \begin{lstkey}{mloverride}
% \begin{macro}{\ifOverridecontext@mlpr@}
%   This key overrides the current context, so that context-sensitive
%   keywords be typeset in the style associated with the alternative context.
%    \begin{macrocode}
\lst@Key{mloverride}{false}[t]%
  {\lstKV@SetIf{#1}\ifOverridecontext@mlpr@}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Strings}
% \begin{lstkey}{mlstringstyle}
% \begin{macro}{\stringStyle@mlpr}
%   This key determines the style applied to \matlab{} (quoted and unquoted)
%   strings.
%    \begin{macrocode}
\newcommand\stringStyle@mlpr{}
\lst@Key{mlstringstyle}\relax%
  {\renewcommand\stringStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Comments}
% \begin{lstkey}{mlcommentstyle}
% \begin{macro}{\commentStyle@mlpr}
%   This key determines the style applied to \matlab{}
%   (to-end-of-line and block) comments.
%    \begin{macrocode}
\newcommand\commentStyle@mlpr{}
\lst@Key{mlcommentstyle}\relax%
  {\renewcommand\commentStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Section titles}
% \begin{lstkey}{mlsectiontitlestyle}
% \begin{macro}{\sectionTitleStyle@mlpr}
%   This key determines the style applied to \matlab{} section titles.
%    \begin{macrocode}
\newcommand\sectionTitleStyle@mlpr{}
\lst@Key{mlsectiontitlestyle}\relax
  {\renewcommand\sectionTitleStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \begin{lstkey}{mlshowsectionrules}
% \begin{macro}{\ifShowSectRules@mlpr@}
%   This key determines whether an horizontal rule gets printed above each
%   section title.
%    \begin{macrocode}
\lst@Key{mlshowsectionrules}{false}[t]%
  {\lstKV@SetIf{#1}\ifShowSectRules@mlpr@}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \begin{lstkey}{mlsectionrulethickness}
% \begin{macro}{\sectionRuleRT@mlpr}
%   This key determines the relative thickness of the horizontal rule that
%   gets printed above each section title.
%    \begin{macrocode}
\newcommand\sectionRuleRT@mlpr{.05}
\lst@Key{mlsectionrulethickness}\relax%
  {\renewcommand\sectionRuleRT@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \begin{lstkey}{mlsectionrulecolor}
% \begin{macro}{\sectionRuleColor@mlpr}
%   This key determines the color of the horizontal rule that gets printed
%   above each section title.
%    \begin{macrocode}
\newcommand\sectionRuleColor@mlpr{black!15}
\lst@Key{mlsectionrulecolor}\relax%
  {\renewcommand\sectionRuleColor@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{System commands}
% \begin{lstkey}{mlsyscomstyle}
% \begin{macro}{\syscomStyle@mlpr}
%   This key determines the style applied to system commands.
%    \begin{macrocode}
\newcommand\syscomStyle@mlpr{}
\lst@Key{mlsyscomstyle}\relax%
  {\renewcommand\syscomStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Variables with shared scope}
% For convenience, we create a brand new class of \lstpkg{} keywords for
% allowing the user to define \matlab{} variables with shared scope.
%
% \begin{macro}{\InstallKeywords@mlpr}
%   This helper macro (which is based on \lstpkg{}' |\lst@InstallKeywords|),
%   will let us defines four keys in one go, all prefixed by |ml|.
%    \begin{macrocode}
\gdef\InstallKeywords@mlpr#1#2#3#4#5%
{%
  \lst@Key{ml#2}\relax
    {\lst@UseFamily{#2}[\@ne]##1\relax\lst@MakeKeywords}%
  \lst@Key{mlmore#2}\relax
    {\lst@UseFamily{#2}[\@ne]##1\relax\lst@MakeMoreKeywords}%
  \lst@Key{mldelete#2}\relax
    {\lst@UseFamily{#2}[\@ne]##1\relax\lst@DeleteKeywords}%
  \ifx\@empty#3\@empty\else
     \lst@Key{#3}{#4}{\@namedef{lst@#3}{##1}}%
  \fi
  \expandafter\lst@InstallFamily@
     \csname\@lst @#2@data\expandafter\endcsname
     \csname\@lst @#5\endcsname {#1}{#2}{#3}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{lstkey}{mlsharedvars}
% \begin{lstkey}{mlmoresharedvars}
% \begin{lstkey}{mldeletesharedvars}
% \begin{lstkey}{mlsharedvarstyle}
%   We now use |\InstallKeywords@mlpr| to define the four keys in question:
%   \rkeyname{mlsharedvars}, which can be used to define a list of \matlab{}
%   variables with shared scope;
%   \rkeyname{mlmoresharedvars}, which can be used to add elements to the
%   current list of such variables;
%   \rkeyname{mldeletesharedvars}, which can be used to remove elements
%   from that list;
%   and \rkeyname{mlsharedvarstyle}, which determines the style applied to
%   variables with shared scope.
%    \begin{macrocode}
\InstallKeywords@mlpr k{sharedvars}{mlsharedvarstyle}\relax%
  {mlsharedvarstyle}{}ld
%    \end{macrocode}
% \end{lstkey}
% \end{lstkey}
% \end{lstkey}
% \end{lstkey}
%
% \paragraph{Delimiters for unquoted strings}
% \begin{lstkey}{mlunquotedstringdelim}
%   This key allows the user to define custom delimiters^^A
%   ---which do not get printed in the output---^^A
%   for unquoted strings.
%    \begin{macrocode}
\lst@Key{mlunquotedstringdelim}\relax%
  {\lst@DelimKey\relax{[is][\stringStyle@mlpr]{#1}}}
%    \end{macrocode}
% \end{lstkey}
%
% \paragraph{Placeholders}
% \begin{lstkey}{mlplaceholderstyle}
% \begin{macro}{\phStyle@mlpr}
%   This key determines the style applied to placeholder content;
%   the color of placeholder delimiters is designed to match that of
%   placeholder content.
%    \begin{macrocode}
\newcommand\phStyle@mlpr{}
\lst@Key{mlplaceholderstyle}\relax%
  {\renewcommand\phStyle@mlpr{#1}}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \mlpkg{} currently does not offer a nice interface for customizing
% code-snippet placeholder delimiters; in a future release, perhaps.
%
% \paragraph{Automatic scaling of inline code}
% \begin{lstkey}{mlscaleinline}
% \begin{macro}{\ifScaleInline@mlpr@}
%   This key determines whether the font size of inline \matlab{} code should
%   match the local font size.
%    \begin{macrocode}
\lst@Key{mlscaleinline}{true}[t]%
  {\lstKV@SetIf{#1}\ifScaleInline@mlpr@}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
% \paragraph{Printing only a function's signature and header}
% \begin{lstkey}{mlonlyheader}
% \begin{macro}{\ifonlyheader@mlpr@}
%   This key determines whether output is dropped after the first block of
%   contiguous line comments.
%    \begin{macrocode}
\lst@Key{mlonlyheader}{false}[t]%
  {\lstKV@SetIf{#1}\ifonlyheader@mlpr@}
%    \end{macrocode}
% \end{macro}
% \end{lstkey}
%
%
% \section{Two user-level macros}
%
% \begin{macro}{\mlttfamily}
% This user-level macro can be used for selecting a scaled version of the
% Bera Mono font, a typewriter font family which, contrary to typewriter
% \TeX{} fonts, conveniently comes with a boldface version.
%    \begin{macrocode}
\newcommand\mlttfamily
{%
  \def\fvm@Scale{.85}%
  \fontfamily{fvm}\selectfont%
}
%    \end{macrocode}
% \end{macro}
%
% \paragraph{Code-snippet placeholders}
% \begin{macro}{\mlplaceholder}
% This user-level macro can be used to typeset placeholders in code snippets.
%    \begin{macrocode}
\newcommand\mlplaceholder[1]
{%
  \bgroup%
    \phStyle@mlpr%
    \bgroup%
      \phDelimStyle@mlpr%
      \phOpDelim@mlpr%
    \egroup%
    #1\itcorr@mlpr%
    \bgroup%
      \phDelimStyle@mlpr%
      \phClDelim@mlpr%
    \egroup%
  \egroup%
}
%    \end{macrocode}
% \end{macro}
%
% 
% \section{Other helper macros}
%
%    \begin{macrocode}
\newcommand\phDelimStyle@mlpr{\rmfamily\upshape}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand\phOpDelim@mlpr{\textlangle}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand\phClDelim@mlpr{\textrangle}
%    \end{macrocode}
%
% \begin{macro}{\itcorr@mlpr}
% This macro is used for applying italic correction in case the current font
% shape is either italic or slanted.
%    \begin{macrocode}
\newcommand\itcorr@mlpr
{%
%    \end{macrocode}
% We define a (long) macro that expands (once) to the current font shape,
% for comparison purposes.
%    \begin{macrocode}
  \expandafter\newcommand\expandafter\long@f@shape@mlpr%
    \expandafter{\f@shape}%
%    \end{macrocode}
% If the current font shape is either italic or slanted, we apply italic
% correction.
%    \begin{macrocode}
  \ifx\long@f@shape@mlpr\itdefault%
    \/%
  \else
    \ifx\long@f@shape@mlpr\sldefault%
      \/%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
%
% \section{\texorpdfstring{\mlpkg{}}{matlab-prettifier} styles}
%
% We now define three \lstpkg{} styles for \matlab{} listings.
%
% \paragraph{Base style}
%
% This style is used internally to define the three user-level styles.
% It's not meant to be used outside this package file.
% \begin{macro}{\toks@mlpr}
% We allocate a token list register in which we store settings that we'll use
% to define the style.
%    \begin{macrocode}
\newtoks\toks@mlpr
\toks@mlpr=%
{
  language               = \languageNormedDefd@mlpr,
  basicstyle             = \color{black}\ttfamily\normalsize,
  breaklines             = true,
  showspaces             = false,
  showstringspaces       = false,
  upquote                = true,
  rulecolor              = \color{black!67},
  numberstyle            = \color{black!33},
  mlscaleinline          = true,
  mlonlyheader           = false,
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\ifframed@mlpr@
  \toks@mlpr=\expandafter{\the\toks@mlpr frame=single,}
\fi
\ifnumbered@mlpr@
  \toks@mlpr=\expandafter{\the\toks@mlpr numbers=left,}
\fi
\begingroup\edef\@tempa{\endgroup
  \noexpand\lstdefinestyle{MatlabBaseStyle@mlpr}{\the\toks@mlpr}
}\@tempa
%    \end{macrocode}
%
% \paragraph{Standard style}
% Standard style of the \matlab{} editor.
% \begin{macro}{\mlbwphstyle}^^A FIXME: to be documented
% This user macro holds the placeholder style used in the
% \mlsty{Matlab-editor} style.
%    \begin{macrocode}
\newcommand\mleditorphstyle{\color[RGB]{209,000,086}\rmfamily\itshape}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\lstdefinestyle{Matlab-editor}
{
  style               = MatlabBaseStyle@mlpr,
  mllastelementstyle  = \color{black}                    ,
  mlkeywordstyle      = \color[RGB]{000,000,255}         ,
  mlcommentstyle      = \color[RGB]{034,139,034}         ,
  mlstringstyle       = \color[RGB]{160,032,240}         ,
  mlsyscomstyle       = \color[RGB]{178,140,000}         ,
  mlsectiontitlestyle = \commentStyle@mlpr      \bfseries,
  mlsharedvarstyle    = \color[RGB]{000,163,163}         ,
  mlplaceholderstyle  = \mleditorphstyle,
}
%    \end{macrocode}
%
% \paragraph{Black \& white style}
% Black \& white, printer-friendly style.
% \begin{macro}{\mlbwphstyle}^^A FIXME: to be documented
% This user macro holds the placeholder style used in the
% \mlsty{Matlab-bw} style.
%    \begin{macrocode}
\newcommand\mlbwphstyle{\color[gray]{0}\rmfamily\itshape}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\lstdefinestyle{Matlab-bw}
{
  style               = MatlabBaseStyle@mlpr,
  mlkeywordstyle      = \color[gray]{0}  \bfseries        ,
  mlcommentstyle      = \color[gray]{.75}         \itshape,
  mlstringstyle       = \color[gray]{.5}                  ,
  mlsyscomstyle       = \color[gray]{.25}                 ,
  mlsectiontitlestyle = \color[gray]{.75}\bfseries\itshape,
  mlsharedvarstyle    = \color[gray]{0}                   ,
  mlplaceholderstyle  = \mlbwphstyle,
}
%    \end{macrocode}
%
% \paragraph{Style of Pygments' MatlabLexer}
% Style that closely mimics that of Pygments' `MatlabLexer'.
% \begin{macro}{\mlpyglikephstyle}^^A FIXME: to be documented
% This user macro holds the placeholder style used in the
% \mlsty{Matlab-Pyglike} style.
%    \begin{macrocode}
\newcommand\mlpyglikephstyle{\color[RGB]{127,063,127}\rmfamily\itshape}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
\lstdefinestyle{Matlab-Pyglike}
{
  style               = MatlabBaseStyle@mlpr,
  mllastelementstyle  = \color[RGB]{127,000,000}                 ,
  mlkeywordstyle      = \color[RGB]{000,127,000}\bfseries        ,
  mlcommentstyle      = \color[RGB]{063,127,127}         \itshape,
  mlstringstyle       = \color[RGB]{186,034,034}                 ,
  mlsyscomstyle       = \color[RGB]{000,127,000}                 ,
  mlsectiontitlestyle = \color[RGB]{063,127,127}         \itshape,
  mlsharedvarstyle    = \color[RGB]{034,034,186}                 ,
  mlplaceholderstyle  = \mlpyglikephstyle,
}
%    \end{macrocode}
%
% \Finale
\endinput