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
|
;;; cc-fonts.el --- font lock support for CC Mode
;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
;; Authors: 2003- Alan Mackenzie
;; 2002- Martin Stjernholm
;; Maintainer: bug-cc-mode@gnu.org
;; Created: 07-Jan-2002
;; Version: See cc-mode.el
;; Keywords: c languages oop
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Some comments on the use of faces:
;;
;; o `c-label-face-name' is either `font-lock-constant-face' (in
;; Emacs), or `font-lock-reference-face'.
;;
;; o `c-constant-face-name', `c-reference-face-name' and
;; `c-doc-markup-face-name' are essentially set up like
;; `c-label-face-name'.
;;
;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in
;; XEmacs and - in lack of a closer equivalent -
;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs.
;;
;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs,
;; `font-lock-doc-face' in Emacs 21 and later, or
;; `font-lock-comment-face' in older Emacs (that since source
;; documentation are actually comments in these languages, as opposed
;; to elisp).
;;
;; TBD: We should probably provide real faces for the above uses and
;; instead initialize them from the standard faces.
;;; Code:
;; The faces that already have been put onto the text is tested in
;; various places to direct further fontifications. For this to work,
;; the following assumptions regarding the faces must hold (apart from
;; the dependencies on the font locking order):
;;
;; o `font-lock-comment-face' and the face in `c-doc-face-name' is
;; not used in anything but comments.
;; o If any face (e.g. `c-doc-markup-face-name') but those above is
;; used in comments, it doesn't replace them.
;; o `font-lock-string-face' is not used in anything but string
;; literals (single or double quoted).
;; o `font-lock-keyword-face' and the face in `c-label-face-name' are
;; never overlaid with other faces.
(eval-when-compile
(let ((load-path
(if (and (boundp 'byte-compile-dest-file)
(stringp byte-compile-dest-file))
(cons (file-name-directory byte-compile-dest-file) load-path)
load-path)))
(load "cc-bytecomp" nil t)))
(cc-require 'cc-defs)
(cc-require-when-compile 'cc-langs)
(cc-require 'cc-vars)
(cc-require 'cc-engine)
(cc-require-when-compile 'cc-awk) ; Change from cc-require, 2003/6/18 to
;; prevent cc-awk being loaded when it's not needed. There is now a (require
;; 'cc-awk) in (defun awk-mode ..).
;; Avoid repeated loading through the eval-after-load directive in
;; cc-mode.el.
(provide 'cc-fonts)
(cc-external-require 'font-lock)
(cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only.
;; Need to declare these local symbols during compilation since
;; they're referenced from lambdas in `byte-compile' calls that are
;; executed at compile time. They don't need to have the proper
;; definitions, though, since the generated functions aren't called
;; during compilation.
(cc-bytecomp-defvar c-preprocessor-face-name)
(cc-bytecomp-defvar c-reference-face-name)
(cc-bytecomp-defun c-fontify-recorded-types-and-refs)
(cc-bytecomp-defun c-font-lock-declarators)
(cc-bytecomp-defun c-font-lock-objc-method)
(cc-bytecomp-defun c-font-lock-invalid-string)
;; Note that font-lock in XEmacs doesn't expand face names as
;; variables, so we have to use the (eval . FORM) in the font lock
;; matchers wherever we use these alias variables.
(defconst c-preprocessor-face-name
(cond ((c-face-name-p 'font-lock-preprocessor-face)
;; XEmacs has a font-lock-preprocessor-face.
'font-lock-preprocessor-face)
((c-face-name-p 'font-lock-builtin-face)
;; In Emacs font-lock-builtin-face has traditionally been
;; used for preprocessor directives.
'font-lock-builtin-face)
(t
'font-lock-reference-face)))
(cc-bytecomp-defvar font-lock-constant-face)
(defconst c-label-face-name
(cond ((c-face-name-p 'font-lock-label-face)
;; If it happens to occur in the future. (Well, the more
;; pragmatic reason is to get unique faces for the test
;; suite.)
'font-lock-label-face)
((and (c-face-name-p 'font-lock-constant-face)
(eq font-lock-constant-face 'font-lock-constant-face))
;; Test both if font-lock-constant-face exists and that it's
;; not an alias for something else. This is important since
;; we compare already set faces in various places.
'font-lock-constant-face)
(t
'font-lock-reference-face)))
(defconst c-constant-face-name
(if (and (c-face-name-p 'font-lock-constant-face)
(eq font-lock-constant-face 'font-lock-constant-face))
;; This doesn't exist in some earlier versions of XEmacs 21.
'font-lock-constant-face
c-label-face-name))
(defconst c-reference-face-name
(with-no-warnings
(if (and (c-face-name-p 'font-lock-reference-face)
(eq font-lock-reference-face 'font-lock-reference-face))
;; This is considered obsolete in Emacs, but it still maps well
;; to this use. (Another reason to do this is to get unique
;; faces for the test suite.)
'font-lock-reference-face
c-label-face-name)))
;; This should not mapped to a face that also is used to fontify things
;; that aren't comments or string literals.
(defconst c-doc-face-name
(cond ((c-face-name-p 'font-lock-doc-string-face)
;; XEmacs.
'font-lock-doc-string-face)
((c-face-name-p 'font-lock-doc-face)
;; Emacs 21 and later.
'font-lock-doc-face)
(t
'font-lock-comment-face)))
(defconst c-doc-markup-face-name
(if (c-face-name-p 'font-lock-doc-markup-face)
;; If it happens to occur in the future. (Well, the more
;; pragmatic reason is to get unique faces for the test
;; suite.)
'font-lock-doc-markup-face
c-label-face-name))
(defconst c-negation-char-face-name
(if (c-face-name-p 'font-lock-negation-char-face)
;; Emacs 22 has a special face for negation chars.
'font-lock-negation-char-face))
(cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
(cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
(defun c-make-inverse-face (oldface newface)
;; Emacs and XEmacs have completely different face manipulation
;; routines. :P
(copy-face oldface newface)
(cond ((fboundp 'face-inverse-video-p)
;; Emacs. This only looks at the inverse flag in the current
;; frame. Other display configurations might be different,
;; but it can only show if the same Emacs has frames on
;; e.g. a color and a monochrome display simultaneously.
(unless (face-inverse-video-p oldface)
(invert-face newface)))
((fboundp 'face-property-instance)
;; XEmacs. Same pitfall here.
(unless (face-property-instance oldface 'reverse)
(invert-face newface)))))
(eval-and-compile
;; We need the following functions during compilation since they're
;; called when the `c-lang-defconst' initializers are evaluated.
;; Define them at runtime too for the sake of derived modes.
(defmacro c-put-font-lock-face (from to face)
;; Put a face on a region (overriding any existing face) in the way
;; font-lock would do it. In XEmacs that means putting an
;; additional font-lock property, or else the font-lock package
;; won't recognize it as fontified and might override it
;; incorrectly.
;;
;; This function does a hidden buffer change.
(if (fboundp 'font-lock-set-face)
;; Note: This function has no docstring in XEmacs so it might be
;; considered internal.
`(font-lock-set-face ,from ,to ,face)
`(put-text-property ,from ,to 'face ,face)))
(defmacro c-remove-font-lock-face (from to)
;; This is the inverse of `c-put-font-lock-face'.
;;
;; This function does a hidden buffer change.
(if (fboundp 'font-lock-remove-face)
`(font-lock-remove-face ,from ,to)
`(remove-text-properties ,from ,to '(face nil))))
(defmacro c-put-font-lock-string-face (from to)
;; Put `font-lock-string-face' on a string. The surrounding
;; quotes are included in Emacs but not in XEmacs. The passed
;; region should include them.
;;
;; This function does a hidden buffer change.
(if (featurep 'xemacs)
`(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
`(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
(defmacro c-fontify-types-and-refs (varlist &rest body)
;; Like `let', but additionally activates `c-record-type-identifiers'
;; and `c-record-ref-identifiers', and fontifies the recorded ranges
;; accordingly on exit.
;;
;; This function does hidden buffer changes.
`(let ((c-record-type-identifiers t)
c-record-ref-identifiers
,@varlist)
(prog1 (progn ,@body)
(c-fontify-recorded-types-and-refs))))
(put 'c-fontify-types-and-refs 'lisp-indent-function 1)
(defun c-skip-comments-and-strings (limit)
;; If the point is within a region fontified as a comment or
;; string literal skip to the end of it or to LIMIT, whichever
;; comes first, and return t. Otherwise return nil. The match
;; data is not clobbered.
;;
;; This function might do hidden buffer changes.
(when (c-got-face-at (point) c-literal-faces)
(while (progn
(goto-char (next-single-property-change
(point) 'face nil limit))
(and (< (point) limit)
(c-got-face-at (point) c-literal-faces))))
t))
(defun c-make-syntactic-matcher (regexp)
;; Returns a byte compiled function suitable for use in place of a
;; regexp string in a `font-lock-keywords' matcher, except that
;; only matches outside comments and string literals count.
;;
;; This function does not do any hidden buffer changes, but the
;; generated functions will. (They are however used in places
;; covered by the font-lock context.)
(byte-compile
`(lambda (limit)
(let (res)
(while (and (setq res (re-search-forward ,regexp limit t))
(progn
(goto-char (match-beginning 0))
(or (c-skip-comments-and-strings limit)
(progn
(goto-char (match-end 0))
nil)))))
res))))
(defun c-make-font-lock-search-function (regexp &rest highlights)
;; This function makes a byte compiled function that works much like
;; a matcher element in `font-lock-keywords'. It cuts out a little
;; bit of the overhead compared to a real matcher. The main reason
;; is however to pass the real search limit to the anchored
;; matcher(s), since most (if not all) font-lock implementations
;; arbitrarily limits anchored matchers to the same line, and also
;; to insulate against various other irritating differences between
;; the different (X)Emacs font-lock packages.
;;
;; REGEXP is the matcher, which must be a regexp. Only matches
;; where the beginning is outside any comment or string literal are
;; significant.
;;
;; HIGHLIGHTS is a list of highlight specs, just like in
;; `font-lock-keywords', with these limitations: The face is always
;; overridden (no big disadvantage, since hits in comments etc are
;; filtered anyway), there is no "laxmatch", and an anchored matcher
;; is always a form which must do all the fontification directly.
;; `limit' is a variable bound to the real limit in the context of
;; the anchored matcher forms.
;;
;; This function does not do any hidden buffer changes, but the
;; generated functions will. (They are however used in places
;; covered by the font-lock context.)
;; Note: Replace `byte-compile' with `eval' to debug the generated
;; lambda easier.
(byte-compile
`(lambda (limit)
(let (;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(while (re-search-forward ,regexp limit t)
(unless (progn
(goto-char (match-beginning 0))
(c-skip-comments-and-strings limit))
(goto-char (match-end 0))
,@(mapcar
(lambda (highlight)
(if (integerp (car highlight))
(progn
(unless (eq (nth 2 highlight) t)
(error
"The override flag must currently be t in %s"
highlight))
(when (nth 3 highlight)
(error
"The laxmatch flag may currently not be set in %s"
highlight))
`(save-match-data
(c-put-font-lock-face
(match-beginning ,(car highlight))
(match-end ,(car highlight))
,(elt highlight 1))))
(when (nth 3 highlight)
(error "Match highlights currently not supported in %s"
highlight))
`(progn
,(nth 1 highlight)
(save-match-data ,(car highlight))
,(nth 2 highlight))))
highlights))))
nil)))
; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
; '(progn
(def-edebug-spec c-fontify-types-and-refs let*)
(def-edebug-spec c-make-syntactic-matcher t)
;; If there are literal quoted or backquoted highlight specs in
;; the call to `c-make-font-lock-search-function' then let's
;; instrument the forms in them.
(def-edebug-spec c-make-font-lock-search-function
(form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
(defun c-fontify-recorded-types-and-refs ()
;; Convert the ranges recorded on `c-record-type-identifiers' and
;; `c-record-ref-identifiers' to fontification.
;;
;; This function does hidden buffer changes.
(let (elem)
(while (consp c-record-type-identifiers)
(setq elem (car c-record-type-identifiers)
c-record-type-identifiers (cdr c-record-type-identifiers))
(c-put-font-lock-face (car elem) (cdr elem)
'font-lock-type-face))
(while c-record-ref-identifiers
(setq elem (car c-record-ref-identifiers)
c-record-ref-identifiers (cdr c-record-ref-identifiers))
;; Note that the reference face is a variable that is
;; dereferenced, since it's an alias in Emacs.
(c-put-font-lock-face (car elem) (cdr elem)
c-reference-face-name))))
(c-lang-defconst c-cpp-matchers
"Font lock matchers for preprocessor directives and purely lexical
stuff. Used on level 1 and higher."
;; Note: `c-font-lock-declarations' assumes that no matcher here
;; sets `font-lock-type-face' in languages where
;; `c-recognize-<>-arglists' is set.
t `(,@(when (c-lang-const c-opt-cpp-prefix)
(let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
(ncle-depth (regexp-opt-depth noncontinued-line-end))
(sws-depth (c-lang-const c-syntactic-ws-depth))
(nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
`(;; The stuff after #error and #warning is a message, so
;; fontify it as a string.
,@(when (c-lang-const c-cpp-message-directives)
(let* ((re (c-make-keywords-re nil
(c-lang-const c-cpp-message-directives)))
(re-depth (regexp-opt-depth re)))
`((,(concat noncontinued-line-end
(c-lang-const c-opt-cpp-prefix)
re
"\\s +\\(.*\\)$")
,(+ ncle-depth re-depth 1) font-lock-string-face))))
;; Fontify filenames in #include <...> as strings.
,@(when (c-lang-const c-cpp-include-directives)
(let* ((re (c-make-keywords-re nil
(c-lang-const c-cpp-include-directives)))
(re-depth (regexp-opt-depth re)))
`((,(concat noncontinued-line-end
(c-lang-const c-opt-cpp-prefix)
re
(c-lang-const c-syntactic-ws)
"\\(<[^>\n\r]*>?\\)")
(,(+ ncle-depth re-depth sws-depth 1)
font-lock-string-face)
;; Use an anchored matcher to put paren syntax
;; on the brackets.
(,(byte-compile
`(lambda (limit)
(let ((beg (match-beginning
,(+ ncle-depth re-depth sws-depth 1)))
(end (1- (match-end ,(+ ncle-depth re-depth
sws-depth 1)))))
(if (eq (char-after end) ?>)
(progn
(c-mark-<-as-paren beg)
(c-mark->-as-paren end))
(c-clear-char-property beg 'syntax-table)))
nil)))))))
;; #define.
,@(when (c-lang-const c-opt-cpp-macro-define)
`((,(c-make-font-lock-search-function
(concat
noncontinued-line-end
(c-lang-const c-opt-cpp-prefix)
(c-lang-const c-opt-cpp-macro-define)
(c-lang-const c-nonempty-syntactic-ws)
"\\(" (c-lang-const ; 1 + ncle + nsws
c-symbol-key) "\\)"
(concat "\\(" ; 2 + ncle + nsws + c-sym-key
;; Macro with arguments - a "function".
"\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
"\\|"
;; Macro without arguments - a "variable".
"\\([^\(]\\|$\\)"
"\\)"))
`((if (match-beginning
,(+ 3 ncle-depth nsws-depth
(c-lang-const c-symbol-key-depth)))
;; "Function". Fontify the name and the arguments.
(save-restriction
(c-put-font-lock-face
(match-beginning ,(+ 1 ncle-depth nsws-depth))
(match-end ,(+ 1 ncle-depth nsws-depth))
'font-lock-function-name-face)
(goto-char
(match-end
,(+ 3 ncle-depth nsws-depth
(c-lang-const c-symbol-key-depth))))
(narrow-to-region (point-min) limit)
(while (and
(progn
(c-forward-syntactic-ws)
(looking-at c-symbol-key))
(progn
(c-put-font-lock-face
(match-beginning 0) (match-end 0)
'font-lock-variable-name-face)
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(eq (char-after) ?,)))
(forward-char)))
;; "Variable".
(c-put-font-lock-face
(match-beginning ,(+ 1 ncle-depth nsws-depth))
(match-end ,(+ 1 ncle-depth nsws-depth))
'font-lock-variable-name-face)))))))
;; Fontify cpp function names in preprocessor
;; expressions in #if and #elif.
,@(when (and (c-lang-const c-cpp-expr-directives)
(c-lang-const c-cpp-expr-functions))
(let ((ced-re (c-make-keywords-re t
(c-lang-const c-cpp-expr-directives)))
(cef-re (c-make-keywords-re t
(c-lang-const c-cpp-expr-functions))))
`((,(c-make-font-lock-search-function
(concat noncontinued-line-end
(c-lang-const c-opt-cpp-prefix)
ced-re ; 1 + ncle-depth
;; Match the whole logical line to look
;; for the functions in.
"\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
`((let ((limit (match-end 0)))
(while (re-search-forward ,cef-re limit 'move)
(c-put-font-lock-face (match-beginning 1)
(match-end 1)
c-preprocessor-face-name)))
(goto-char (match-end ,(1+ ncle-depth)))))))))
;; Fontify the directive names.
(,(c-make-font-lock-search-function
(concat noncontinued-line-end
"\\("
(c-lang-const c-opt-cpp-prefix)
"[" (c-lang-const c-symbol-chars) "]+"
"\\)")
`(,(1+ ncle-depth) c-preprocessor-face-name t)))
(eval . (list ,(c-make-syntactic-matcher
(concat noncontinued-line-end
(c-lang-const c-opt-cpp-prefix)
"if\\(n\\)def\\>"))
,(+ ncle-depth 1)
c-negation-char-face-name
'append))
)))
,@(when (c-major-mode-is 'pike-mode)
;; Recognize hashbangs in Pike.
`((eval . (list "\\`#![^\n\r]*"
0 c-preprocessor-face-name))))
;; Make hard spaces visible through an inverted `font-lock-warning-face'.
(eval . (list
"\240"
0 (progn
(unless (c-face-name-p 'c-nonbreakable-space-face)
(c-make-inverse-face 'font-lock-warning-face
'c-nonbreakable-space-face))
''c-nonbreakable-space-face)))
))
(defun c-font-lock-invalid-string ()
;; Assuming the point is after the opening character of a string,
;; fontify that char with `font-lock-warning-face' if the string
;; decidedly isn't terminated properly.
;;
;; This function does hidden buffer changes.
(let ((start (1- (point))))
(save-excursion
(and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
(if (integerp c-multiline-string-start-char)
;; There's no multiline string start char before the
;; string, so newlines aren't allowed.
(not (eq (char-before start) c-multiline-string-start-char))
;; Multiline strings are allowed anywhere if
;; c-multiline-string-start-char is t.
(not c-multiline-string-start-char))
(if c-string-escaped-newlines
;; There's no \ before the newline.
(not (eq (char-before (point)) ?\\))
;; Escaped newlines aren't supported.
t)
(c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
(c-lang-defconst c-basic-matchers-before
"Font lock matchers for basic keywords, labels, references and various
other easily recognizable things that should be fontified before generic
casts and declarations are fontified. Used on level 2 and higher."
;; Note: `c-font-lock-declarations' assumes that no matcher here
;; sets `font-lock-type-face' in languages where
;; `c-recognize-<>-arglists' is set.
t `(;; Put a warning face on the opener of unclosed strings that
;; can't span lines. Later font
;; lock packages have a `font-lock-syntactic-face-function' for
;; this, but it doesn't give the control we want since any
;; fontification done inside the function will be
;; unconditionally overridden.
,(c-make-font-lock-search-function
;; Match a char before the string starter to make
;; `c-skip-comments-and-strings' work correctly.
(concat ".\\(" c-string-limit-regexp "\\)")
'((c-font-lock-invalid-string)))
;; Fontify keyword constants.
,@(when (c-lang-const c-constant-kwds)
(let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
(if (c-major-mode-is 'pike-mode)
;; No symbol is a keyword after "->" in Pike.
`((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
"\\<\\(" re "\\)\\>")
2 c-constant-face-name)))
`((eval . (list ,(concat "\\<\\(" re "\\)\\>")
1 c-constant-face-name))))))
;; Fontify all keywords except the primitive types.
,(if (c-major-mode-is 'pike-mode)
;; No symbol is a keyword after "->" in Pike.
`(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
"\\<" (c-lang-const c-regular-keywords-regexp))
2 font-lock-keyword-face)
`(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
1 font-lock-keyword-face))
;; Fontify leading identifiers in fully qualified names like
;; "foo::bar" in languages that supports such things.
,@(when (c-lang-const c-opt-identifier-concat-key)
(if (c-major-mode-is 'java-mode)
;; Java needs special treatment since "." is used both to
;; qualify names and in normal indexing. Here we look for
;; capital characters at the beginning of an identifier to
;; recognize the class. "*" is also recognized to cover
;; wildcard import declarations. All preceding dot separated
;; identifiers are taken as package names and therefore
;; fontified as references.
`(,(c-make-font-lock-search-function
;; Search for class identifiers preceded by ".". The
;; anchored matcher takes it from there.
(concat (c-lang-const c-opt-identifier-concat-key)
(c-lang-const c-simple-ws) "*"
(concat "\\("
"[" c-upper "]"
"[" (c-lang-const c-symbol-chars) "]*"
"\\|"
"\\*"
"\\)"))
`((let (id-end)
(goto-char (1+ (match-beginning 0)))
(while (and (eq (char-before) ?.)
(progn
(backward-char)
(c-backward-syntactic-ws)
(setq id-end (point))
(< (skip-chars-backward
,(c-lang-const c-symbol-chars)) 0))
(not (get-text-property (point) 'face)))
(c-put-font-lock-face (point) id-end
c-reference-face-name)
(c-backward-syntactic-ws)))
nil
(goto-char (match-end 0)))))
`((,(byte-compile
;; Must use a function here since we match longer than
;; we want to move before doing a new search. This is
;; not necessary for XEmacs since it restarts the
;; search from the end of the first highlighted
;; submatch (something that causes problems in other
;; places).
`(lambda (limit)
(while (re-search-forward
,(concat "\\(\\<" ; 1
"\\(" (c-lang-const c-symbol-key) "\\)" ; 2
(c-lang-const c-simple-ws) "*"
(c-lang-const c-opt-identifier-concat-key)
(c-lang-const c-simple-ws) "*"
"\\)"
"\\("
(c-lang-const c-opt-after-id-concat-key)
"\\)")
limit t)
(unless (progn
(goto-char (match-beginning 0))
(c-skip-comments-and-strings limit))
(or (get-text-property (match-beginning 2) 'face)
(c-put-font-lock-face (match-beginning 2)
(match-end 2)
c-reference-face-name))
(goto-char (match-end 1))))))))))
;; Fontify the special declarations in Objective-C.
,@(when (c-major-mode-is 'objc-mode)
`(;; Fontify class names in the beginning of message expressions.
,(c-make-font-lock-search-function
"\\["
'((c-fontify-types-and-refs ()
(c-forward-syntactic-ws limit)
(let ((start (point)))
;; In this case we accept both primitive and known types.
(when (eq (c-forward-type) 'known)
(goto-char start)
(let ((c-promote-possible-types t))
(c-forward-type))))
(if (> (point) limit) (goto-char limit)))))
;; The @interface/@implementation/@protocol directives.
,(c-make-font-lock-search-function
(concat "\\<"
(regexp-opt
'("@interface" "@implementation" "@protocol")
t)
"\\>")
'((c-fontify-types-and-refs
(;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(c-forward-objc-directive)
nil)
(goto-char (match-beginning 0))))))
(eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
))
(defun c-font-lock-complex-decl-prepare (limit)
;; This function will be called from font-lock for a region bounded by POINT
;; and LIMIT, as though it were to identify a keyword for
;; font-lock-keyword-face. It always returns NIL to inhibit this and
;; prevent a repeat invocation. See elisp/lispref page "Search-based
;; Fontification".
;;
;; Called before any of the matchers in `c-complex-decl-matchers'.
;;
;; This function does hidden buffer changes.
;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
;; Clear the list of found types if we start from the start of the
;; buffer, to make it easier to get rid of misspelled types and
;; variables that has gotten recognized as types in malformed code.
(when (bobp)
(c-clear-found-types))
;; Clear the c-type char properties in the region to recalculate
;; them properly. This is necessary e.g. to handle constructs that
;; might been required as declarations temporarily during editing.
;; The interesting properties are anyway those put on the closest
;; token before the region.
(c-clear-char-properties (point) limit 'c-type)
;; Update `c-state-cache' to the beginning of the region. This will
;; make `c-beginning-of-syntax' go faster when it's used later on,
;; and it's near the point most of the time.
(c-parse-state)
;; Check if the fontified region starts inside a declarator list so
;; that `c-font-lock-declarators' should be called at the start.
(let ((prop (save-excursion
(c-backward-syntactic-ws)
(unless (bobp)
(c-get-char-property (1- (point)) 'c-type)))))
(when (memq prop '(c-decl-id-start c-decl-type-start))
(c-forward-syntactic-ws limit)
(c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
nil)
(defun c-font-lock-<>-arglists (limit)
;; This function will be called from font-lock for a region bounded by POINT
;; and LIMIT, as though it were to identify a keyword for
;; font-lock-keyword-face. It always returns NIL to inhibit this and
;; prevent a repeat invocation. See elisp/lispref page "Search-based
;; Fontification".
;;
;; Fontify types and references in names containing angle bracket
;; arglists from the point to LIMIT. Note that
;; `c-font-lock-declarations' already has handled many of them.
;;
;; This function might do hidden buffer changes.
(let (;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties)))
(c-parse-and-markup-<>-arglists t)
c-restricted-<>-arglists
id-start id-end id-face pos kwd-sym)
(while (and (< (point) limit)
(re-search-forward c-opt-<>-arglist-start limit t))
(setq id-start (match-beginning 1)
id-end (match-end 1)
pos (point))
(goto-char id-start)
(unless (c-skip-comments-and-strings limit)
(setq kwd-sym nil
c-restricted-<>-arglists nil
id-face (get-text-property id-start 'face))
(if (cond
((eq id-face 'font-lock-type-face)
;; The identifier got the type face so it has already been
;; handled in `c-font-lock-declarations'.
nil)
((eq id-face 'font-lock-keyword-face)
(when (looking-at c-opt-<>-sexp-key)
;; There's a special keyword before the "<" that tells
;; that it's an angle bracket arglist.
(setq kwd-sym (c-keyword-sym (match-string 1)))))
(t
;; There's a normal identifier before the "<". If we're not in
;; a declaration context then we set `c-restricted-<>-arglists'
;; to avoid recognizing templates in function calls like "foo (a
;; < b, c > d)".
(c-backward-syntactic-ws)
(when (and (memq (char-before) '(?\( ?,))
(not (eq (get-text-property (1- (point)) 'c-type)
'c-decl-arg-start)))
(setq c-restricted-<>-arglists t))
t))
(progn
(goto-char (1- pos))
;; Check for comment/string both at the identifier and
;; at the "<".
(unless (c-skip-comments-and-strings limit)
(c-fontify-types-and-refs ()
(when (c-forward-<>-arglist (c-keyword-member
kwd-sym 'c-<>-type-kwds))
(when (and c-opt-identifier-concat-key
(not (get-text-property id-start 'face)))
(c-forward-syntactic-ws)
(if (looking-at c-opt-identifier-concat-key)
(c-put-font-lock-face id-start id-end
c-reference-face-name)
(c-put-font-lock-face id-start id-end
'font-lock-type-face)))))
(goto-char pos)))
(goto-char pos)))))
nil)
(defun c-font-lock-declarators (limit list types)
;; Assuming the point is at the start of a declarator in a
;; declaration, fontify it. If LIST is non-nil, fontify also all
;; following declarators in a comma separated list (e.g. "foo" and
;; "bar" in "int foo = 17, bar;"). Stop at LIMIT. If TYPES is
;; non-nil, fontify all identifiers as types. Nil is always
;; returned.
;;
;; This function might do hidden buffer changes.
;;(message "c-font-lock-declarators from %s to %s" (point) limit)
(c-fontify-types-and-refs
((pos (point)) next-pos id-start id-end
paren-depth
id-face got-init
c-last-identifier-range
(separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
(while (and
pos
(< (point) limit)
(let (got-identifier)
(setq paren-depth 0)
;; Skip over type decl prefix operators. (Note similar
;; code in `c-forward-decl-or-cast-1'.)
(while (and (looking-at c-type-decl-prefix-key)
(if (and (c-major-mode-is 'c++-mode)
(match-beginning 2))
;; If the second submatch matches in C++ then
;; we're looking at an identifier that's a
;; prefix only if it specifies a member pointer.
(progn
(setq id-start (point))
(c-forward-name)
(if (looking-at "\\(::\\)")
;; We only check for a trailing "::" and
;; let the "*" that should follow be
;; matched in the next round.
t
;; It turned out to be the real identifier,
;; so flag that and stop.
(setq got-identifier t)
nil))
t))
(if (eq (char-after) ?\()
(progn
(setq paren-depth (1+ paren-depth))
(forward-char))
(goto-char (match-end 1)))
(c-forward-syntactic-ws))
;; If we didn't pass the identifier above already, do it now.
(unless got-identifier
(setq id-start (point))
(c-forward-name))
(setq id-end (point))
(/= id-end pos))
;; Skip out of the parens surrounding the identifier.
(or (= paren-depth 0)
(c-safe (goto-char (scan-lists (point) 1 paren-depth))))
(<= (point) limit)
(progn
(when (looking-at c-decl-hangon-key)
(c-forward-keyword-clause 1))
(<= (point) limit))
;; Search syntactically to the end of the declarator (";",
;; ",", a closen paren, eob etc) or to the beginning of an
;; initializer or function prototype ("=" or "\\s\(").
;; Note that the open paren will match array specs in
;; square brackets, and we treat them as initializers too.
(c-syntactic-re-search-forward
"[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
(setq next-pos (match-beginning 0)
id-face (if (and (eq (char-after next-pos) ?\()
(let (c-last-identifier-range)
(save-excursion
(goto-char next-pos)
(c-at-toplevel-p))))
'font-lock-function-name-face
'font-lock-variable-name-face)
got-init (and (match-beginning 1)
(char-after (match-beginning 1))))
(if types
;; Register and fontify the identifer as a type.
(let ((c-promote-possible-types t))
(goto-char id-start)
(c-forward-type))
;; Fontify the last symbol in the identifier if it isn't fontified
;; already. The check is necessary only in certain cases where this
;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
(when (and c-last-identifier-range
(not (get-text-property (car c-last-identifier-range)
'face)))
(c-put-font-lock-face (car c-last-identifier-range)
(cdr c-last-identifier-range)
id-face)))
(goto-char next-pos)
(setq pos nil)
(when list
;; Jump past any initializer or function prototype to see if
;; there's a ',' to continue at.
(cond ((eq id-face 'font-lock-function-name-face)
;; Skip a parenthesized initializer (C++) or a function
;; prototype.
(if (c-safe (c-forward-sexp 1) t)
(c-forward-syntactic-ws limit)
(goto-char limit)))
(got-init
;; Skip an initializer expression. If we're at a '='
;; then accept a brace list directly after it to cope
;; with array initializers. Otherwise stop at braces
;; to avoid going past full function and class blocks.
(and (if (and (eq got-init ?=)
(= (c-forward-token-2 1 nil limit) 0)
(looking-at "{"))
(c-safe (c-forward-sexp) t)
t)
;; FIXME: Should look for c-decl-end markers here;
;; we might go far into the following declarations
;; in e.g. ObjC mode (see e.g. methods-4.m).
(c-syntactic-re-search-forward "[;,{]" limit 'move t)
(backward-char)))
(t (c-forward-syntactic-ws limit)))
;; If a ',' is found we set pos to the next declarator and iterate.
(when (and (< (point) limit) (looking-at ","))
(c-put-char-property (point) 'c-type separator-prop)
(forward-char)
(c-forward-syntactic-ws limit)
(setq pos (point))))))
nil)
(defconst c-font-lock-maybe-decl-faces
;; List of faces that might be put at the start of a type when
;; `c-font-lock-declarations' runs. This needs to be evaluated to
;; ensure that face name aliases in Emacs are resolved.
(list nil
font-lock-type-face
c-reference-face-name
font-lock-keyword-face))
(defun c-font-lock-declarations (limit)
;; This function will be called from font-lock for a region bounded by POINT
;; and LIMIT, as though it were to identify a keyword for
;; font-lock-keyword-face. It always returns NIL to inhibit this and
;; prevent a repeat invocation. See elisp/lispref page "Search-based
;; Fontification".
;;
;; Fontify all the declarations, casts and labels from the point to LIMIT.
;; Assumes that strings and comments have been fontified already.
;;
;; This function might do hidden buffer changes.
;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
(save-restriction
(let (;; The position where `c-find-decl-spots' stopped.
start-pos
;; 'decl if we're in an arglist containing declarations (but
;; if `c-recognize-paren-inits' is set it might also be an
;; initializer arglist), '<> if the arglist is of angle
;; bracket type, 'arglist if it's some other arglist, or nil
;; if not in an arglist at all.
context
;; The position of the next token after the closing paren of
;; the last detected cast.
last-cast-end
;; The result from `c-forward-decl-or-cast-1'.
decl-or-cast
;; The maximum of the end positions of all the checked type
;; decl expressions in the successfully identified
;; declarations. The position might be either before or
;; after the syntactic whitespace following the last token
;; in the type decl expression.
(max-type-decl-end 0)
;; Same as `max-type-decl-*', but used when we're before
;; `token-pos'.
(max-type-decl-end-before-token 0)
;; Set according to the context to direct the heuristics for
;; recognizing C++ templates.
c-restricted-<>-arglists
;; Turn on recording of identifier ranges in
;; `c-forward-decl-or-cast-1' and `c-forward-label' for
;; later fontification.
(c-record-type-identifiers t)
label-type
c-record-ref-identifiers
;; Make `c-forward-type' calls mark up template arglists if
;; it finds any. That's necessary so that we later will
;; stop inside them to fontify types there.
(c-parse-and-markup-<>-arglists t)
;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
;; Below we fontify a whole declaration even when it crosses the limit,
;; to avoid gaps when lazy-lock fontifies the file a screenful at a
;; time. That is however annoying during editing, e.g. the following is
;; a common situation while the first line is being written:
;;
;; my_variable
;; some_other_variable = 0;
;;
;; font-lock will put the limit at the beginning of the second line
;; here, and if we go past it we'll fontify "my_variable" as a type and
;; "some_other_variable" as an identifier, and the latter will not
;; correct itself until the second line is changed. To avoid that we
;; narrow to the limit if the region to fontify is a single line.
(narrow-to-region
(point-min)
(if (<= limit (c-point 'bonl))
(save-excursion
;; Narrow after any operator chars following the limit though,
;; since those characters can be useful in recognizing a
;; declaration (in particular the '{' that opens a function body
;; after the header).
(goto-char limit)
(skip-chars-forward c-nonsymbol-chars)
(point))
limit))
(c-find-decl-spots
limit
c-decl-start-re
c-font-lock-maybe-decl-faces
(lambda (match-pos inside-macro)
(setq start-pos (point))
(when
;; The result of the form below is true when we don't recognize a
;; declaration or cast.
(if (and (eq (get-text-property (point) 'face)
'font-lock-keyword-face)
(looking-at c-not-decl-init-keywords))
;; Don't do anything more if we're looking at a keyword that
;; can't start a declaration.
t
;; Set `context'. Look for "<" for the sake of C++-style template
;; arglists.
(if (memq (char-before match-pos) '(?\( ?, ?\[ ?<))
;; Find out the type of the arglist.
(if (<= match-pos (point-min))
(setq context 'arglist)
(let ((type (c-get-char-property (1- match-pos) 'c-type)))
(cond ((eq type 'c-decl-arg-start)
;; Got a cached hit in a declaration arglist.
(setq context 'decl))
((or (eq type 'c-<>-arg-sep)
(eq (char-before match-pos) ?<))
;; Inside an angle bracket arglist.
(setq context '<>))
(type
;; Got a cached hit in some other type of arglist.
(setq context 'arglist))
((if inside-macro
(< match-pos max-type-decl-end-before-token)
(< match-pos max-type-decl-end))
;; The point is within the range of a previously
;; encountered type decl expression, so the arglist
;; is probably one that contains declarations.
;; However, if `c-recognize-paren-inits' is set it
;; might also be an initializer arglist.
(setq context 'decl)
;; The result of this check is cached with a char
;; property on the match token, so that we can look
;; it up again when refontifying single lines in a
;; multiline declaration.
(c-put-char-property (1- match-pos)
'c-type 'c-decl-arg-start))
(t
(setq context 'arglist)))))
(setq context nil))
;; If we're in a normal arglist context we don't want to
;; recognize commas in nested angle bracket arglists since
;; those commas could be part of our own arglist.
(setq c-restricted-<>-arglists (and c-recognize-<>-arglists
(eq context 'arglist))
;; Now analyze the construct.
decl-or-cast (c-forward-decl-or-cast-1
match-pos context last-cast-end))
(if (not decl-or-cast)
;; False alarm. Return t to go on to the next check.
t
(if (eq decl-or-cast 'cast)
;; Save the position after the previous cast so we can feed
;; it to `c-forward-decl-or-cast-1' in the next round. That
;; helps it discover cast chains like "(a) (b) c".
(setq last-cast-end (point))
;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
;; under the assumption that we're after the first type decl
;; expression in the declaration now. That's not really true;
;; we could also be after a parenthesized initializer
;; expression in C++, but this is only used as a last resort
;; to slant ambiguous expression/declarations, and overall
;; it's worth the risk to occasionally fontify an expression
;; as a declaration in an initializer expression compared to
;; getting ambiguous things in normal function prototypes
;; fontified as expressions.
(if inside-macro
(when (> (point) max-type-decl-end-before-token)
(setq max-type-decl-end-before-token (point)))
(when (> (point) max-type-decl-end)
(setq max-type-decl-end (point))))
;; Back up to the type to fontify the declarator(s).
(goto-char (car decl-or-cast))
(let ((decl-list
(if context
;; Should normally not fontify a list of
;; declarators inside an arglist, but the first
;; argument in the ';' separated list of a "for"
;; statement is an exception.
(when (eq (char-before match-pos) ?\()
(save-excursion
(goto-char (1- match-pos))
(c-backward-syntactic-ws)
(and (c-simple-skip-symbol-backward)
(looking-at c-paren-stmt-key))))
t)))
;; Fix the `c-decl-id-start' or `c-decl-type-start' property
;; before the first declarator if it's a list.
;; `c-font-lock-declarators' handles the rest.
(when decl-list
(save-excursion
(c-backward-syntactic-ws)
(unless (bobp)
(c-put-char-property (1- (point)) 'c-type
(if (cdr decl-or-cast)
'c-decl-type-start
'c-decl-id-start)))))
(c-font-lock-declarators
(point-max) decl-list (cdr decl-or-cast))))
;; A cast or declaration has been successfully identified, so do
;; all the fontification of types and refs that's been recorded.
(c-fontify-recorded-types-and-refs)
nil))
;; It was a false alarm. Check if we're in a label (or other
;; construct with `:' except bitfield) instead.
(goto-char start-pos)
(when (setq label-type (c-forward-label t match-pos nil))
;; Can't use `c-fontify-types-and-refs' here since we
;; use the label face at times.
(cond ((eq label-type 'goto-target)
(c-put-font-lock-face (caar c-record-ref-identifiers)
(cdar c-record-ref-identifiers)
c-label-face-name))
((eq label-type 'qt-1kwd-colon)
(c-put-font-lock-face (caar c-record-ref-identifiers)
(cdar c-record-ref-identifiers)
'font-lock-keyword-face))
((eq label-type 'qt-2kwds-colon)
(mapc
(lambda (kwd)
(c-put-font-lock-face (car kwd) (cdr kwd)
'font-lock-keyword-face))
c-record-ref-identifiers)))
(setq c-record-ref-identifiers nil)
;; `c-forward-label' has probably added a `c-decl-end'
;; marker, so return t to `c-find-decl-spots' to signal
;; that.
t))))
nil)))
(c-lang-defconst c-simple-decl-matchers
"Simple font lock matchers for types and declarations. These are used
on level 2 only and so aren't combined with `c-complex-decl-matchers'."
t `(;; Objective-C methods.
,@(when (c-major-mode-is 'objc-mode)
`((,(c-lang-const c-opt-method-key)
(,(byte-compile
(lambda (limit)
(let (;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(save-restriction
(narrow-to-region (point-min) limit)
(c-font-lock-objc-method)))
nil))
(goto-char (match-end 1))))))
;; Fontify all type names and the identifiers in the
;; declarations they might start. Use eval here since
;; `c-known-type-key' gets its value from
;; `*-font-lock-extra-types' on mode init.
(eval . (list ,(c-make-font-lock-search-function
'c-known-type-key
'(1 'font-lock-type-face t)
'((c-font-lock-declarators limit t nil)
(save-match-data
(goto-char (match-end 1))
(c-forward-syntactic-ws))
(goto-char (match-end 1))))))
;; Fontify types preceded by `c-type-prefix-kwds' and the
;; identifiers in the declarations they might start.
,@(when (c-lang-const c-type-prefix-kwds)
(let* ((prefix-re (c-make-keywords-re nil
(c-lang-const c-type-prefix-kwds)))
(type-match (+ 2
(regexp-opt-depth prefix-re)
(c-lang-const c-simple-ws-depth))))
`((,(c-make-font-lock-search-function
(concat "\\<\\(" prefix-re "\\)" ; 1
(c-lang-const c-simple-ws) "+"
(concat "\\(" ; 2 + prefix-re + c-simple-ws
(c-lang-const c-symbol-key)
"\\)"))
`(,type-match
'font-lock-type-face t)
`((c-font-lock-declarators limit t nil)
(save-match-data
(goto-char (match-end ,type-match))
(c-forward-syntactic-ws))
(goto-char (match-end ,type-match))))))))
;; Fontify special declarations that lacks a type.
,@(when (c-lang-const c-typeless-decl-kwds)
`((,(c-make-font-lock-search-function
(concat "\\<\\("
(regexp-opt (c-lang-const c-typeless-decl-kwds))
"\\)\\>")
'((c-font-lock-declarators limit t nil)
(save-match-data
(goto-char (match-end 1))
(c-forward-syntactic-ws))
(goto-char (match-end 1)))))))
;; Fontify generic colon labels in languages that support them.
,@(when (c-lang-const c-recognize-colon-labels)
`(c-font-lock-labels))))
(c-lang-defconst c-complex-decl-matchers
"Complex font lock matchers for types and declarations. Used on level
3 and higher."
;; Note: This code in this form dumps a number of funtions into the
;; resulting constant, `c-matchers-3'. At run time, font lock will call
;; each of them as a "FUNCTION" (see Elisp page "Search-based
;; Fontification"). The font lock region is delimited by POINT and the
;; single parameter, LIMIT. Each of these functions returns NIL (thus
;; inhibiting spurious font-lock-keyword-face highlighting and another
;; call).
t `(;; Initialize some things before the search functions below.
c-font-lock-complex-decl-prepare
,@(if (c-major-mode-is 'objc-mode)
;; Fontify method declarations in Objective-C, but first
;; we have to put the `c-decl-end' `c-type' property on
;; all the @-style directives that haven't been handled in
;; `c-basic-matchers-before'.
`(,(c-make-font-lock-search-function
(c-make-keywords-re t
;; Exclude "@class" since that directive ends with a
;; semicolon anyway.
(delete "@class"
(append (c-lang-const c-protection-kwds)
(c-lang-const c-other-decl-kwds)
nil)))
'((c-put-char-property (1- (match-end 1))
'c-type 'c-decl-end)))
c-font-lock-objc-methods))
;; Fontify all declarations, casts and normal labels.
c-font-lock-declarations
;; Fontify angle bracket arglists like templates in C++.
,@(when (c-lang-const c-recognize-<>-arglists)
`(c-font-lock-<>-arglists))
;; The first two rules here mostly find occurences that
;; `c-font-lock-declarations' has found already, but not
;; declarations containing blocks in the type (see note below).
;; It's also useful to fontify these everywhere to show e.g. when
;; a type keyword is accidentally used as an identifier.
;; Fontify basic types.
,(let ((re (c-make-keywords-re nil
(c-lang-const c-primitive-type-kwds))))
(if (c-major-mode-is 'pike-mode)
;; No symbol is a keyword after "->" in Pike.
`(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
"\\<\\(" re "\\)\\>")
2 font-lock-type-face)
`(,(concat "\\<\\(" re "\\)\\>")
1 'font-lock-type-face)))
;; Fontify types preceded by `c-type-prefix-kwds'.
,@(when (c-lang-const c-type-prefix-kwds)
`((,(byte-compile
`(lambda (limit)
(c-fontify-types-and-refs
((c-promote-possible-types t)
;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(save-restriction
;; Narrow to avoid going past the limit in
;; `c-forward-type'.
(narrow-to-region (point) limit)
(while (re-search-forward
,(concat "\\<\\("
(c-make-keywords-re nil
(c-lang-const c-type-prefix-kwds))
"\\)\\>")
limit t)
(unless (c-skip-comments-and-strings limit)
(c-forward-syntactic-ws)
;; Handle prefix declaration specifiers.
(when (looking-at c-prefix-spec-kwds-re)
(c-forward-keyword-clause 1))
,(if (c-major-mode-is 'c++-mode)
`(when (and (c-forward-type)
(eq (char-after) ?=))
;; In C++ we additionally check for a "class
;; X = Y" construct which is used in
;; templates, to fontify Y as a type.
(forward-char)
(c-forward-syntactic-ws)
(c-forward-type))
`(c-forward-type))
)))))))))
;; Fontify symbols after closing braces as declaration
;; identifiers under the assumption that they are part of
;; declarations like "class Foo { ... } foo;". It's too
;; expensive to check this accurately by skipping past the
;; brace block, so we use the heuristic that it's such a
;; declaration if the first identifier is on the same line as
;; the closing brace. `c-font-lock-declarations' will later
;; override it if it turns out to be an new declaration, but
;; it will be wrong if it's an expression (see the test
;; decls-8.cc).
,@(when (c-lang-const c-opt-block-decls-with-vars-key)
`((,(c-make-font-lock-search-function
(concat "}"
(c-lang-const c-single-line-syntactic-ws)
"\\(" ; 1 + c-single-line-syntactic-ws-depth
(c-lang-const c-type-decl-prefix-key)
"\\|"
(c-lang-const c-symbol-key)
"\\)")
`((c-font-lock-declarators limit t nil)
(progn
(c-put-char-property (match-beginning 0) 'c-type
'c-decl-id-start)
(goto-char (match-beginning
,(1+ (c-lang-const
c-single-line-syntactic-ws-depth)))))
(goto-char (match-end 0)))))))
;; Fontify the type in C++ "new" expressions.
,@(when (c-major-mode-is 'c++-mode)
;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
;; (see Elisp page "Search-based Fontification").
`(("\\<new\\>"
(c-font-lock-c++-new))))
))
(defun c-font-lock-labels (limit)
;; Fontify all statement labels from the point to LIMIT. Assumes
;; that strings and comments have been fontified already. Nil is
;; always returned.
;;
;; Note: This function is only used on decoration level 2; this is
;; taken care of directly by the gargantuan
;; `c-font-lock-declarations' on higher levels.
;;
;; This function might do hidden buffer changes.
(let (continue-pos id-start
;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(while (re-search-forward ":[^:]" limit t)
(setq continue-pos (point))
(goto-char (match-beginning 0))
(unless (c-skip-comments-and-strings limit)
(c-backward-syntactic-ws)
(and (setq id-start (c-on-identifier))
(not (get-text-property id-start 'face))
(progn
(goto-char id-start)
(c-backward-syntactic-ws)
(or
;; Check for a char that precedes a statement.
(memq (char-before) '(?\} ?\{ ?\;))
;; Check for a preceding label. We exploit the font
;; locking made earlier by this function.
(and (eq (char-before) ?:)
(progn
(backward-char)
(c-backward-syntactic-ws)
(not (bobp)))
(eq (get-text-property (1- (point)) 'face)
c-label-face-name))
;; Check for a keyword that precedes a statement.
(c-after-conditional)))
(progn
;; Got a label.
(goto-char id-start)
(looking-at c-symbol-key)
(c-put-font-lock-face (match-beginning 0) (match-end 0)
c-label-face-name)))
(goto-char continue-pos))))
nil)
(c-lang-defconst c-basic-matchers-after
"Font lock matchers for various things that should be fontified after
generic casts and declarations are fontified. Used on level 2 and
higher."
t `(;; Fontify the identifiers inside enum lists. (The enum type
;; name is handled by `c-simple-decl-matchers' or
;; `c-complex-decl-matchers' below.
,@(when (c-lang-const c-brace-id-list-kwds)
`((,(c-make-font-lock-search-function
(concat
"\\<\\("
(c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
"\\)\\>"
;; Disallow various common punctuation chars that can't come
;; before the '{' of the enum list, to avoid searching too far.
"[^\]\[{}();,/#=]*"
"{")
'((c-font-lock-declarators limit t nil)
(save-match-data
(goto-char (match-end 0))
(c-put-char-property (1- (point)) 'c-type
'c-decl-id-start)
(c-forward-syntactic-ws))
(goto-char (match-end 0)))))))
;; Fontify labels after goto etc.
,@(when (c-lang-const c-before-label-kwds)
`(;; (Got three different interpretation levels here,
;; which makes it a bit complicated: 1) The backquote
;; stuff is expanded when compiled or loaded, 2) the
;; eval form is evaluated at font-lock setup (to
;; substitute c-label-face-name correctly), and 3) the
;; resulting structure is interpreted during
;; fontification.)
(eval
. ,(let* ((c-before-label-re
(c-make-keywords-re nil
(c-lang-const c-before-label-kwds))))
`(list
,(concat "\\<\\(" c-before-label-re "\\)\\>"
"\\s *"
"\\(" ; identifier-offset
(c-lang-const c-symbol-key)
"\\)")
(list ,(+ (regexp-opt-depth c-before-label-re) 2)
c-label-face-name nil t))))))
;; Fontify the clauses after various keywords.
,@(when (or (c-lang-const c-type-list-kwds)
(c-lang-const c-ref-list-kwds)
(c-lang-const c-colon-type-list-kwds)
(c-lang-const c-paren-type-kwds))
`((,(c-make-font-lock-search-function
(concat "\\<\\("
(c-make-keywords-re nil
(append (c-lang-const c-type-list-kwds)
(c-lang-const c-ref-list-kwds)
(c-lang-const c-colon-type-list-kwds)
(c-lang-const c-paren-type-kwds)))
"\\)\\>")
'((c-fontify-types-and-refs ((c-promote-possible-types t))
(c-forward-keyword-clause 1)
(if (> (point) limit) (goto-char limit))))))))
))
(c-lang-defconst c-matchers-1
t (c-lang-const c-cpp-matchers))
(c-lang-defconst c-matchers-2
t (append (c-lang-const c-matchers-1)
(c-lang-const c-basic-matchers-before)
(c-lang-const c-simple-decl-matchers)
(c-lang-const c-basic-matchers-after)))
(c-lang-defconst c-matchers-3
t (append (c-lang-const c-matchers-1)
(c-lang-const c-basic-matchers-before)
(c-lang-const c-complex-decl-matchers)
(c-lang-const c-basic-matchers-after)))
(defun c-compose-keywords-list (base-list)
;; Incorporate the font lock keyword lists according to
;; `c-doc-comment-style' on the given keyword list and return it.
;; This is used in the function bindings of the
;; `*-font-lock-keywords-*' symbols since we have to build the list
;; when font-lock is initialized.
(unless (memq c-doc-face-name c-literal-faces)
(setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
(let* ((doc-keywords
(if (consp (car-safe c-doc-comment-style))
(cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
(assq 'other c-doc-comment-style)))
c-doc-comment-style))
(list (nconc (apply 'nconc
(mapcar
(lambda (doc-style)
(let ((sym (intern
(concat (symbol-name doc-style)
"-font-lock-keywords"))))
(cond ((fboundp sym)
(funcall sym))
((boundp sym)
(append (eval sym) nil)))))
(if (listp doc-keywords)
doc-keywords
(list doc-keywords))))
base-list)))
;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
;; move it first since the doc comment font lockers might add
;; `c-type' text properties, so they have to be cleared before that.
(when (memq 'c-font-lock-complex-decl-prepare list)
(setq list (cons 'c-font-lock-complex-decl-prepare
(delq 'c-font-lock-complex-decl-prepare
(append list nil)))))
list))
(defun c-override-default-keywords (def-var)
;; This is used to override the value on a `*-font-lock-keywords'
;; variable only if it's nil or has the same value as one of the
;; `*-font-lock-keywords-*' variables. Older font-lock packages
;; define a default value for `*-font-lock-keywords' which we want
;; to override, but we should otoh avoid clobbering a user setting.
;; This heuristic for that isn't perfect, but I can't think of any
;; better. /mast
(when (and (boundp def-var)
(memq (symbol-value def-var)
(cons nil
(mapcar
(lambda (suffix)
(let ((sym (intern (concat (symbol-name def-var)
suffix))))
(and (boundp sym) (symbol-value sym))))
'("-1" "-2" "-3")))))
;; The overriding is done by unbinding the variable so that the normal
;; defvar will install its default value later on.
(makunbound def-var)))
;;; C.
(c-override-default-keywords 'c-font-lock-keywords)
(defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
"Minimal font locking for C mode.
Fontifies only preprocessor directives (in addition to the syntactic
fontification of strings and comments).")
(defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
"Fast normal font locking for C mode.
In addition to `c-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `c-font-lock-extra-types', and the doc comment
styles specified by `c-doc-comment-style'.")
(defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
"Accurate normal font locking for C mode.
Like `c-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `c-font-lock-extra-types'.")
(defvar c-font-lock-keywords c-font-lock-keywords-3
"Default expressions to highlight in C mode.")
(defun c-font-lock-keywords-2 ()
(c-compose-keywords-list c-font-lock-keywords-2))
(defun c-font-lock-keywords-3 ()
(c-compose-keywords-list c-font-lock-keywords-3))
(defun c-font-lock-keywords ()
(c-compose-keywords-list c-font-lock-keywords))
;;; C++.
(defun c-font-lock-c++-new (limit)
;; Assuming point is after a "new" word, check that it isn't inside
;; a string or comment, and if so try to fontify the type in the
;; allocation expression. Nil is always returned.
;;
;; As usual, C++ takes the prize in coming up with a hard to parse
;; syntax. :P
;;
;; This function might do hidden buffer changes.
(unless (c-skip-comments-and-strings limit)
(save-excursion
(catch 'false-alarm
;; A "new" keyword is followed by one to three expressions, where
;; the type is the middle one, and the only required part.
(let (expr1-pos expr2-pos
;; Enable recording of identifier ranges in `c-forward-type'
;; etc for later fontification. Not using
;; `c-fontify-types-and-refs' here since the ranges should
;; be fontified selectively only when an allocation
;; expression is successfully recognized.
(c-record-type-identifiers t)
c-record-ref-identifiers
;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(c-forward-syntactic-ws)
;; The first placement arglist is always parenthesized, if it
;; exists.
(when (eq (char-after) ?\()
(setq expr1-pos (1+ (point)))
(condition-case nil
(c-forward-sexp)
(scan-error (throw 'false-alarm t)))
(c-forward-syntactic-ws))
;; The second expression is either a type followed by some "*" or
;; "[...]" or similar, or a parenthesized type followed by a full
;; identifierless declarator.
(setq expr2-pos (1+ (point)))
(cond ((eq (char-after) ?\())
((let ((c-promote-possible-types t))
(c-forward-type)))
(t (setq expr2-pos nil)))
(when expr1-pos
(cond
((not expr2-pos)
;; No second expression, so the first has to be a
;; parenthesized type.
(goto-char expr1-pos)
(let ((c-promote-possible-types t))
(c-forward-type)))
((eq (char-before expr2-pos) ?\()
;; Got two parenthesized expressions, so we have to look
;; closer at them to decide which is the type. No need to
;; handle `c-record-ref-identifiers' since all references
;; has already been handled by other fontification rules.
(let (expr1-res expr2-res)
(goto-char expr1-pos)
(when (setq expr1-res (c-forward-type))
(unless (looking-at
(cc-eval-when-compile
(concat (c-lang-const c-symbol-start c++)
"\\|[*:\)\[]")))
;; There's something after the would-be type that
;; can't be there, so this is a placement arglist.
(setq expr1-res nil)))
(goto-char expr2-pos)
(when (setq expr2-res (c-forward-type))
(unless (looking-at
(cc-eval-when-compile
(concat (c-lang-const c-symbol-start c++)
"\\|[*:\)\[]")))
;; There's something after the would-be type that can't
;; be there, so this is an initialization expression.
(setq expr2-res nil))
(when (and (c-go-up-list-forward)
(progn (c-forward-syntactic-ws)
(eq (char-after) ?\()))
;; If there's a third initialization expression
;; then the second one is the type, so demote the
;; first match.
(setq expr1-res nil)))
;; We fontify the most likely type, with a preference for
;; the first argument since a placement arglist is more
;; unusual than an initializer.
(cond ((memq expr1-res '(t known prefix)))
((memq expr2-res '(t known prefix)))
((eq expr1-res 'found)
(let ((c-promote-possible-types t))
(goto-char expr1-pos)
(c-forward-type)))
((eq expr2-res 'found)
(let ((c-promote-possible-types t))
(goto-char expr2-pos)
(c-forward-type)))
((and (eq expr1-res 'maybe) (not expr2-res))
(let ((c-promote-possible-types t))
(goto-char expr1-pos)
(c-forward-type)))
((and (not expr1-res) (eq expr2-res 'maybe))
(let ((c-promote-possible-types t))
(goto-char expr2-pos)
(c-forward-type)))
;; If both type matches are 'maybe then we're
;; too uncertain to promote either of them.
)))))
;; Fontify the type that now is recorded in
;; `c-record-type-identifiers', if any.
(c-fontify-recorded-types-and-refs)))))
nil)
(c-override-default-keywords 'c++-font-lock-keywords)
(defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
"Minimal font locking for C++ mode.
Fontifies only preprocessor directives (in addition to the syntactic
fontification of strings and comments).")
(defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
"Fast normal font locking for C++ mode.
In addition to `c++-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `c++-font-lock-extra-types', and the doc comment
styles specified by `c-doc-comment-style'.")
(defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
"Accurate normal font locking for C++ mode.
Like `c++-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `c++-font-lock-extra-types'.")
(defvar c++-font-lock-keywords c++-font-lock-keywords-3
"Default expressions to highlight in C++ mode.")
(defun c++-font-lock-keywords-2 ()
(c-compose-keywords-list c++-font-lock-keywords-2))
(defun c++-font-lock-keywords-3 ()
(c-compose-keywords-list c++-font-lock-keywords-3))
(defun c++-font-lock-keywords ()
(c-compose-keywords-list c++-font-lock-keywords))
;;; Objective-C.
(defun c-font-lock-objc-method ()
;; Assuming the point is after the + or - that starts an Objective-C
;; method declaration, fontify it. This must be done before normal
;; casts, declarations and labels are fontified since they will get
;; false matches in these things.
;;
;; This function might do hidden buffer changes.
(c-fontify-types-and-refs
((first t)
(c-promote-possible-types t))
(while (and
(progn
(c-forward-syntactic-ws)
;; An optional method type.
(if (eq (char-after) ?\()
(progn
(forward-char)
(c-forward-syntactic-ws)
(c-forward-type)
(prog1 (c-go-up-list-forward)
(c-forward-syntactic-ws)))
t))
;; The name. The first time it's the first part of
;; the function name, the rest of the time it's an
;; argument name.
(looking-at c-symbol-key)
(progn
(goto-char (match-end 0))
(c-put-font-lock-face (match-beginning 0)
(point)
(if first
'font-lock-function-name-face
'font-lock-variable-name-face))
(c-forward-syntactic-ws)
;; Another optional part of the function name.
(when (looking-at c-symbol-key)
(goto-char (match-end 0))
(c-put-font-lock-face (match-beginning 0)
(point)
'font-lock-function-name-face)
(c-forward-syntactic-ws))
;; There's another argument if a colon follows.
(eq (char-after) ?:)))
(forward-char)
(setq first nil))))
(defun c-font-lock-objc-methods (limit)
;; Fontify method declarations in Objective-C. Nil is always
;; returned.
;;
;; This function might do hidden buffer changes.
(let (;; The font-lock package in Emacs is known to clobber
;; `parse-sexp-lookup-properties' (when it exists).
(parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(c-find-decl-spots
limit
"[-+]"
nil
(lambda (match-pos inside-macro)
(forward-char)
(c-font-lock-objc-method))))
nil)
(c-override-default-keywords 'objc-font-lock-keywords)
(defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
"Minimal font locking for Objective-C mode.
Fontifies only compiler directives (in addition to the syntactic
fontification of strings and comments).")
(defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
"Fast normal font locking for Objective-C mode.
In addition to `objc-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `objc-font-lock-extra-types', and the doc
comment styles specified by `c-doc-comment-style'.")
(defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
"Accurate normal font locking for Objective-C mode.
Like `objc-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `objc-font-lock-extra-types'.")
(defvar objc-font-lock-keywords objc-font-lock-keywords-3
"Default expressions to highlight in Objective-C mode.")
(defun objc-font-lock-keywords-2 ()
(c-compose-keywords-list objc-font-lock-keywords-2))
(defun objc-font-lock-keywords-3 ()
(c-compose-keywords-list objc-font-lock-keywords-3))
(defun objc-font-lock-keywords ()
(c-compose-keywords-list objc-font-lock-keywords))
;; Kludge to override the default value that
;; `objc-font-lock-extra-types' might have gotten from the font-lock
;; package. The value replaced here isn't relevant now anyway since
;; those types are builtin and therefore listed directly in
;; `c-primitive-type-kwds'.
(when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
'("BOOL" "Class" "IMP" "SEL"))
(setq objc-font-lock-extra-types
(cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
;;; Java.
(c-override-default-keywords 'java-font-lock-keywords)
(defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
"Minimal font locking for Java mode.
Fontifies nothing except the syntactic fontification of strings and
comments.")
(defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
"Fast normal font locking for Java mode.
In addition to `java-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `java-font-lock-extra-types', and the doc
comment styles specified by `c-doc-comment-style'.")
(defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
"Accurate normal font locking for Java mode.
Like `java-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `java-font-lock-extra-types'.")
(defvar java-font-lock-keywords java-font-lock-keywords-3
"Default expressions to highlight in Java mode.")
(defun java-font-lock-keywords-2 ()
(c-compose-keywords-list java-font-lock-keywords-2))
(defun java-font-lock-keywords-3 ()
(c-compose-keywords-list java-font-lock-keywords-3))
(defun java-font-lock-keywords ()
(c-compose-keywords-list java-font-lock-keywords))
;;; CORBA IDL.
(c-override-default-keywords 'idl-font-lock-keywords)
(defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
"Minimal font locking for CORBA IDL mode.
Fontifies nothing except the syntactic fontification of strings and
comments.")
(defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
"Fast normal font locking for CORBA IDL mode.
In addition to `idl-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `idl-font-lock-extra-types', and the doc comment
styles specified by `c-doc-comment-style'.")
(defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
"Accurate normal font locking for CORBA IDL mode.
Like `idl-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `idl-font-lock-extra-types'.")
(defvar idl-font-lock-keywords idl-font-lock-keywords-3
"Default expressions to highlight in CORBA IDL mode.")
(defun idl-font-lock-keywords-2 ()
(c-compose-keywords-list idl-font-lock-keywords-2))
(defun idl-font-lock-keywords-3 ()
(c-compose-keywords-list idl-font-lock-keywords-3))
(defun idl-font-lock-keywords ()
(c-compose-keywords-list idl-font-lock-keywords))
;;; Pike.
(c-override-default-keywords 'pike-font-lock-keywords)
(defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
"Minimal font locking for Pike mode.
Fontifies only preprocessor directives (in addition to the syntactic
fontification of strings and comments).")
(defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
"Fast normal font locking for Pike mode.
In addition to `pike-font-lock-keywords-1', this adds fontification of
keywords, simple types, declarations that are easy to recognize, the
user defined types on `pike-font-lock-extra-types', and the doc
comment styles specified by `c-doc-comment-style'.")
(defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
"Accurate normal font locking for Pike mode.
Like `pike-font-lock-keywords-2' but detects declarations in a more
accurate way that works in most cases for arbitrary types without the
need for `pike-font-lock-extra-types'.")
(defvar pike-font-lock-keywords pike-font-lock-keywords-3
"Default expressions to highlight in Pike mode.")
(defun pike-font-lock-keywords-2 ()
(c-compose-keywords-list pike-font-lock-keywords-2))
(defun pike-font-lock-keywords-3 ()
(c-compose-keywords-list pike-font-lock-keywords-3))
(defun pike-font-lock-keywords ()
(c-compose-keywords-list pike-font-lock-keywords))
;;; Doc comments.
(defun c-font-lock-doc-comments (prefix limit keywords)
;; Fontify the comments between the point and LIMIT whose start
;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
;; fontified with `font-lock-comment-face' already. nil is always
;; returned.
;;
;; After the fontification of a matching comment, fontification
;; according to KEYWORDS is applied inside it. It's a list like
;; `font-lock-keywords' except that anchored matches and eval
;; clauses aren't supported and that some abbreviated forms can't be
;; used. The buffer is narrowed to the comment while KEYWORDS is
;; applied; leading comment starters are included but trailing
;; comment enders for block comment are not.
;;
;; Note that faces added through KEYWORDS should never replace the
;; existing `c-doc-face-name' face since the existence of that face
;; is used as a flag in other code to skip comments.
;;
;; This function might do hidden buffer changes.
(let (comment-beg region-beg)
(if (eq (get-text-property (point) 'face)
'font-lock-comment-face)
;; Handle the case when the fontified region starts inside a
;; comment.
(let ((range (c-literal-limits)))
(setq region-beg (point))
(when range
(goto-char (car range)))
(when (looking-at prefix)
(setq comment-beg (point)))))
(while (or
comment-beg
;; Search for the prefix until a match is found at the start
;; of a comment.
(while (when (re-search-forward prefix limit t)
(setq comment-beg (match-beginning 0))
(or (not (c-got-face-at comment-beg
c-literal-faces))
(and (/= comment-beg (point-min))
(c-got-face-at (1- comment-beg)
c-literal-faces))))
(setq comment-beg nil))
(setq region-beg comment-beg))
(if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
;; Collect a sequence of doc style line comments.
(progn
(goto-char comment-beg)
(while (and (progn
(c-forward-single-comment)
(skip-syntax-forward " ")
(< (point) limit))
(looking-at prefix))))
(goto-char comment-beg)
(c-forward-single-comment))
(if (> (point) limit) (goto-char limit))
(setq comment-beg nil)
(let ((region-end (point))
(keylist keywords) keyword matcher highlights)
(c-put-font-lock-face region-beg region-end c-doc-face-name)
(save-restriction
;; Narrow to the doc comment. Among other things, this
;; helps by making "^" match at the start of the comment.
;; Do not include a trailing block comment ender, though.
(and (> region-end (1+ region-beg))
(progn (goto-char region-end)
(backward-char 2)
(looking-at "\\*/"))
(setq region-end (point)))
(narrow-to-region region-beg region-end)
(while keylist
(setq keyword (car keylist)
keylist (cdr keylist)
matcher (car keyword))
(goto-char region-beg)
(while (if (stringp matcher)
(re-search-forward matcher region-end t)
(funcall matcher region-end))
(setq highlights (cdr keyword))
(if (consp (car highlights))
(while highlights
(font-lock-apply-highlight (car highlights))
(setq highlights (cdr highlights)))
(font-lock-apply-highlight highlights))))
(goto-char region-end)))))
nil)
(put 'c-font-lock-doc-comments 'lisp-indent-function 2)
(defun c-find-invalid-doc-markup (regexp limit)
;; Used to fontify invalid markup in doc comments after the correct
;; ones have been fontified: Find the first occurence of REGEXP
;; between the point and LIMIT that only is fontified with
;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
;; the first char and t is returned, otherwise nil is returned.
;;
;; This function might do hidden buffer changes.
(let (start)
(while (if (re-search-forward regexp limit t)
(not (eq (get-text-property
(setq start (match-beginning 0)) 'face)
c-doc-face-name))
(setq start nil)))
(when start
(store-match-data (list (copy-marker start)
(copy-marker (1+ start))))
t)))
;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
(defconst gtkdoc-font-lock-doc-comments
(let ((symbol "[a-zA-Z0-9_]+")
(header "^ \\* "))
`((,(concat header "\\(" symbol "\\):[ \t]*$")
1 ,c-doc-markup-face-name prepend nil)
(,(concat symbol "()")
0 ,c-doc-markup-face-name prepend nil)
(,(concat header "\\(" "@" symbol "\\):")
1 ,c-doc-markup-face-name prepend nil)
(,(concat "[#%@]" symbol)
0 ,c-doc-markup-face-name prepend nil))
))
(defconst gtkdoc-font-lock-doc-protection
`(("< \\(public\\|private\\|protected\\) >"
1 ,c-doc-markup-face-name prepend nil)))
(defconst gtkdoc-font-lock-keywords
`((,(lambda (limit)
(c-font-lock-doc-comments "/\\*\\*$" limit
gtkdoc-font-lock-doc-comments)
(c-font-lock-doc-comments "/\\*< " limit
gtkdoc-font-lock-doc-protection)
))))
;; Javadoc.
(defconst javadoc-font-lock-doc-comments
`(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
0 ,c-doc-markup-face-name prepend nil)
("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
3 ,c-doc-markup-face-name prepend nil)
(,(concat "</?\\sw" ; HTML tags.
"\\("
(concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
"\"[^\"]*\"\\|'[^']*'")
"\\)*>")
0 ,c-doc-markup-face-name prepend nil)
("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
0 ,c-doc-markup-face-name prepend nil)
;; Fontify remaining markup characters as invalid. Note
;; that the Javadoc spec is hazy about when "@" is
;; allowed in non-markup use.
(,(lambda (limit)
(c-find-invalid-doc-markup "[<>&]\\|{@" limit))
0 'font-lock-warning-face prepend nil)))
(defconst javadoc-font-lock-keywords
`((,(lambda (limit)
(c-font-lock-doc-comments "/\\*\\*" limit
javadoc-font-lock-doc-comments)))))
;; Pike autodoc.
(defconst autodoc-decl-keywords
;; Adorned regexp matching the keywords that introduce declarations
;; in Pike Autodoc.
(cc-eval-when-compile
(c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
(defconst autodoc-decl-type-keywords
;; Adorned regexp matching the keywords that are followed by a type.
(cc-eval-when-compile
(c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
(defun autodoc-font-lock-line-markup (limit)
;; Fontify all line oriented keywords between the point and LIMIT.
;; Nil is always returned.
;;
;; This function might do hidden buffer changes.
(let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
c-current-comment-prefix
"\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
(markup-faces (list c-doc-markup-face-name c-doc-face-name)))
(while (re-search-forward line-re limit t)
(goto-char (match-end 1))
(if (looking-at autodoc-decl-keywords)
(let* ((kwd-pos (point))
(start (match-end 1))
(pos start)
end)
(c-put-font-lock-face (point) pos markup-faces)
;; Put a declaration end mark at the markup keyword and
;; remove the faces from the rest of the line so that it
;; gets refontified as a declaration later on by
;; `c-font-lock-declarations'.
(c-put-char-property (1- pos) 'c-type 'c-decl-end)
(goto-char pos)
(while (progn
(end-of-line)
(setq end (point))
(and (eq (char-before) ?@)
(not (eobp))
(progn (forward-char)
(skip-syntax-forward " ")
(looking-at c-current-comment-prefix))))
(goto-char (match-end 0))
(c-remove-font-lock-face pos (1- end))
(c-put-font-lock-face (1- end) end markup-faces)
(setq pos (point)))
;; Include the final newline in the removed area. This
;; has no visual effect but it avoids some tricky special
;; cases in the testsuite wrt the differences in string
;; fontification in Emacs vs XEmacs.
(c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
;; Must handle string literals explicitly inside the declaration.
(goto-char start)
(while (re-search-forward
"\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
end 'move)
(c-put-font-lock-string-face (match-beginning 0)
(point)))
;; Fontify types after keywords that always are followed
;; by them.
(goto-char kwd-pos)
(when (looking-at autodoc-decl-type-keywords)
(c-fontify-types-and-refs ((c-promote-possible-types t))
(goto-char start)
(c-forward-syntactic-ws)
(c-forward-type))))
;; Mark each whole line as markup, as long as the logical line
;; continues.
(while (progn
(c-put-font-lock-face (point)
(progn (end-of-line) (point))
markup-faces)
(and (eq (char-before) ?@)
(not (eobp))
(progn (forward-char)
(skip-syntax-forward " ")
(looking-at c-current-comment-prefix))))
(goto-char (match-end 0))))))
nil)
(defconst autodoc-font-lock-doc-comments
`(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
;; In-text markup.
0 ,c-doc-markup-face-name prepend nil)
(autodoc-font-lock-line-markup)
;; Fontify remaining markup characters as invalid.
(,(lambda (limit)
(c-find-invalid-doc-markup "@" limit))
0 'font-lock-warning-face prepend nil)
))
(defun autodoc-font-lock-keywords ()
;; Note that we depend on that `c-current-comment-prefix' has got
;; its proper value here.
;;
;; This function might do hidden buffer changes.
;; The `c-type' text property with `c-decl-end' is used to mark the
;; end of the `autodoc-decl-keywords' occurrences to fontify the
;; following declarations.
(setq c-type-decl-end-used t)
`((,(lambda (limit)
(c-font-lock-doc-comments "/[*/]!" limit
autodoc-font-lock-doc-comments)))))
;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
(cc-provide 'cc-fonts)
;;; arch-tag: 2f65f405-735f-4da5-8d4b-b957844c5203
;;; cc-fonts.el ends here
|