cc-fonts.el 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. ;;; cc-fonts.el --- font lock support for CC Mode
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Authors: 2003- Alan Mackenzie
  4. ;; 2002- Martin Stjernholm
  5. ;; Maintainer: bug-cc-mode@gnu.org
  6. ;; Created: 07-Jan-2002
  7. ;; Keywords: c languages
  8. ;; Package: cc-mode
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Some comments on the use of faces:
  22. ;;
  23. ;; o `c-label-face-name' is either `font-lock-constant-face' (in
  24. ;; Emacs), or `font-lock-reference-face'.
  25. ;;
  26. ;; o `c-constant-face-name', `c-reference-face-name' and
  27. ;; `c-doc-markup-face-name' are essentially set up like
  28. ;; `c-label-face-name'.
  29. ;;
  30. ;; o `c-preprocessor-face-name' is `font-lock-preprocessor-face' in
  31. ;; XEmacs and - in lack of a closer equivalent -
  32. ;; `font-lock-builtin-face' or `font-lock-reference-face' in Emacs.
  33. ;;
  34. ;; o `c-doc-face-name' is `font-lock-doc-string-face' in XEmacs,
  35. ;; `font-lock-doc-face' in Emacs 21 and later, or
  36. ;; `font-lock-comment-face' in older Emacs (that since source
  37. ;; documentation are actually comments in these languages, as opposed
  38. ;; to elisp).
  39. ;;
  40. ;; TBD: We should probably provide real faces for the above uses and
  41. ;; instead initialize them from the standard faces.
  42. ;;; Code:
  43. ;; The faces that already have been put onto the text is tested in
  44. ;; various places to direct further fontifications. For this to work,
  45. ;; the following assumptions regarding the faces must hold (apart from
  46. ;; the dependencies on the font locking order):
  47. ;;
  48. ;; o `font-lock-comment-face' and the face in `c-doc-face-name' is
  49. ;; not used in anything but comments.
  50. ;; o If any face (e.g. `c-doc-markup-face-name') but those above is
  51. ;; used in comments, it doesn't replace them.
  52. ;; o `font-lock-string-face' is not used in anything but string
  53. ;; literals (single or double quoted).
  54. ;; o `font-lock-keyword-face' and the face in `c-label-face-name' are
  55. ;; never overlaid with other faces.
  56. (eval-when-compile
  57. (let ((load-path
  58. (if (and (boundp 'byte-compile-dest-file)
  59. (stringp byte-compile-dest-file))
  60. (cons (file-name-directory byte-compile-dest-file) load-path)
  61. load-path)))
  62. (load "cc-bytecomp" nil t)))
  63. (cc-require 'cc-defs)
  64. (cc-require-when-compile 'cc-langs)
  65. (cc-require 'cc-vars)
  66. (cc-require 'cc-engine)
  67. (cc-require-when-compile 'cc-awk) ; Change from cc-require, 2003/6/18 to
  68. ;; prevent cc-awk being loaded when it's not needed. There is now a (require
  69. ;; 'cc-awk) in (defun awk-mode ..).
  70. ;; Avoid repeated loading through the eval-after-load directive in
  71. ;; cc-mode.el.
  72. (provide 'cc-fonts)
  73. (cc-external-require 'font-lock)
  74. (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs only.
  75. ;; Need to declare these local symbols during compilation since
  76. ;; they're referenced from lambdas in `byte-compile' calls that are
  77. ;; executed at compile time. They don't need to have the proper
  78. ;; definitions, though, since the generated functions aren't called
  79. ;; during compilation.
  80. (cc-bytecomp-defvar c-preprocessor-face-name)
  81. (cc-bytecomp-defvar c-reference-face-name)
  82. (cc-bytecomp-defun c-fontify-recorded-types-and-refs)
  83. (cc-bytecomp-defun c-font-lock-declarators)
  84. (cc-bytecomp-defun c-font-lock-objc-method)
  85. (cc-bytecomp-defun c-font-lock-invalid-string)
  86. ;; Note that font-lock in XEmacs doesn't expand face names as
  87. ;; variables, so we have to use the (eval . FORM) in the font lock
  88. ;; matchers wherever we use these alias variables.
  89. (defconst c-preprocessor-face-name
  90. (cond ((c-face-name-p 'font-lock-preprocessor-face)
  91. ;; XEmacs has a font-lock-preprocessor-face.
  92. 'font-lock-preprocessor-face)
  93. ((c-face-name-p 'font-lock-builtin-face)
  94. ;; In Emacs font-lock-builtin-face has traditionally been
  95. ;; used for preprocessor directives.
  96. 'font-lock-builtin-face)
  97. (t
  98. 'font-lock-reference-face)))
  99. (cc-bytecomp-defvar font-lock-constant-face)
  100. (defconst c-label-face-name
  101. (cond ((c-face-name-p 'font-lock-label-face)
  102. ;; If it happens to occur in the future. (Well, the more
  103. ;; pragmatic reason is to get unique faces for the test
  104. ;; suite.)
  105. 'font-lock-label-face)
  106. ((and (c-face-name-p 'font-lock-constant-face)
  107. (eq font-lock-constant-face 'font-lock-constant-face))
  108. ;; Test both if font-lock-constant-face exists and that it's
  109. ;; not an alias for something else. This is important since
  110. ;; we compare already set faces in various places.
  111. 'font-lock-constant-face)
  112. (t
  113. 'font-lock-reference-face)))
  114. (defconst c-constant-face-name
  115. (if (and (c-face-name-p 'font-lock-constant-face)
  116. (eq font-lock-constant-face 'font-lock-constant-face))
  117. ;; This doesn't exist in some earlier versions of XEmacs 21.
  118. 'font-lock-constant-face
  119. c-label-face-name))
  120. (defconst c-reference-face-name
  121. (with-no-warnings
  122. (if (and (c-face-name-p 'font-lock-reference-face)
  123. (eq font-lock-reference-face 'font-lock-reference-face))
  124. ;; This is considered obsolete in Emacs, but it still maps well
  125. ;; to this use. (Another reason to do this is to get unique
  126. ;; faces for the test suite.)
  127. 'font-lock-reference-face
  128. c-label-face-name)))
  129. ;; This should not mapped to a face that also is used to fontify things
  130. ;; that aren't comments or string literals.
  131. (defconst c-doc-face-name
  132. (cond ((c-face-name-p 'font-lock-doc-string-face)
  133. ;; XEmacs.
  134. 'font-lock-doc-string-face)
  135. ((c-face-name-p 'font-lock-doc-face)
  136. ;; Emacs 21 and later.
  137. 'font-lock-doc-face)
  138. (t
  139. 'font-lock-comment-face)))
  140. (defconst c-doc-markup-face-name
  141. (if (c-face-name-p 'font-lock-doc-markup-face)
  142. ;; If it happens to occur in the future. (Well, the more
  143. ;; pragmatic reason is to get unique faces for the test
  144. ;; suite.)
  145. 'font-lock-doc-markup-face
  146. c-label-face-name))
  147. (defconst c-negation-char-face-name
  148. (if (c-face-name-p 'font-lock-negation-char-face)
  149. ;; Emacs 22 has a special face for negation chars.
  150. 'font-lock-negation-char-face))
  151. (cc-bytecomp-defun face-inverse-video-p) ; Only in Emacs.
  152. (cc-bytecomp-defun face-property-instance) ; Only in XEmacs.
  153. (defun c-make-inverse-face (oldface newface)
  154. ;; Emacs and XEmacs have completely different face manipulation
  155. ;; routines. :P
  156. (copy-face oldface newface)
  157. (cond ((fboundp 'face-inverse-video-p)
  158. ;; Emacs. This only looks at the inverse flag in the current
  159. ;; frame. Other display configurations might be different,
  160. ;; but it can only show if the same Emacs has frames on
  161. ;; e.g. a color and a monochrome display simultaneously.
  162. (unless (face-inverse-video-p oldface)
  163. (invert-face newface)))
  164. ((fboundp 'face-property-instance)
  165. ;; XEmacs. Same pitfall here.
  166. (unless (face-property-instance oldface 'reverse)
  167. (invert-face newface)))))
  168. (defvar c-annotation-face 'c-annotation-face)
  169. (defface c-annotation-face
  170. '((default :inherit font-lock-constant-face))
  171. "Face for highlighting annotations in Java mode and similar modes."
  172. :version "24.1"
  173. :group 'c)
  174. (eval-and-compile
  175. ;; We need the following definitions during compilation since they're
  176. ;; used when the `c-lang-defconst' initializers are evaluated. Define
  177. ;; them at runtime too for the sake of derived modes.
  178. ;; This indicates the "font locking context", and is set just before
  179. ;; fontification is done. If non-nil, it says, e.g., point starts
  180. ;; from within a #if preprocessor construct.
  181. (defvar c-font-lock-context nil)
  182. (make-variable-buffer-local 'c-font-lock-context)
  183. (defmacro c-put-font-lock-face (from to face)
  184. ;; Put a face on a region (overriding any existing face) in the way
  185. ;; font-lock would do it. In XEmacs that means putting an
  186. ;; additional font-lock property, or else the font-lock package
  187. ;; won't recognize it as fontified and might override it
  188. ;; incorrectly.
  189. ;;
  190. ;; This function does a hidden buffer change.
  191. (if (fboundp 'font-lock-set-face)
  192. ;; Note: This function has no docstring in XEmacs so it might be
  193. ;; considered internal.
  194. `(font-lock-set-face ,from ,to ,face)
  195. `(put-text-property ,from ,to 'face ,face)))
  196. (defmacro c-remove-font-lock-face (from to)
  197. ;; This is the inverse of `c-put-font-lock-face'.
  198. ;;
  199. ;; This function does a hidden buffer change.
  200. (if (fboundp 'font-lock-remove-face)
  201. `(font-lock-remove-face ,from ,to)
  202. `(remove-text-properties ,from ,to '(face nil))))
  203. (defmacro c-put-font-lock-string-face (from to)
  204. ;; Put `font-lock-string-face' on a string. The surrounding
  205. ;; quotes are included in Emacs but not in XEmacs. The passed
  206. ;; region should include them.
  207. ;;
  208. ;; This function does a hidden buffer change.
  209. (if (featurep 'xemacs)
  210. `(c-put-font-lock-face (1+ ,from) (1- ,to) 'font-lock-string-face)
  211. `(c-put-font-lock-face ,from ,to 'font-lock-string-face)))
  212. (defmacro c-fontify-types-and-refs (varlist &rest body)
  213. ;; Like `let', but additionally activates `c-record-type-identifiers'
  214. ;; and `c-record-ref-identifiers', and fontifies the recorded ranges
  215. ;; accordingly on exit.
  216. ;;
  217. ;; This function does hidden buffer changes.
  218. `(let ((c-record-type-identifiers t)
  219. c-record-ref-identifiers
  220. ,@varlist)
  221. (prog1 (progn ,@body)
  222. (c-fontify-recorded-types-and-refs))))
  223. (put 'c-fontify-types-and-refs 'lisp-indent-function 1)
  224. (defun c-skip-comments-and-strings (limit)
  225. ;; If the point is within a region fontified as a comment or
  226. ;; string literal skip to the end of it or to LIMIT, whichever
  227. ;; comes first, and return t. Otherwise return nil. The match
  228. ;; data is not clobbered.
  229. ;;
  230. ;; This function might do hidden buffer changes.
  231. (when (c-got-face-at (point) c-literal-faces)
  232. (while (progn
  233. (goto-char (next-single-property-change
  234. (point) 'face nil limit))
  235. (and (< (point) limit)
  236. (c-got-face-at (point) c-literal-faces))))
  237. t))
  238. (defun c-make-syntactic-matcher (regexp)
  239. ;; Returns a byte compiled function suitable for use in place of a
  240. ;; regexp string in a `font-lock-keywords' matcher, except that
  241. ;; only matches outside comments and string literals count.
  242. ;;
  243. ;; This function does not do any hidden buffer changes, but the
  244. ;; generated functions will. (They are however used in places
  245. ;; covered by the font-lock context.)
  246. (byte-compile
  247. `(lambda (limit)
  248. (let (res)
  249. (while (and (setq res (re-search-forward ,regexp limit t))
  250. (progn
  251. (goto-char (match-beginning 0))
  252. (or (c-skip-comments-and-strings limit)
  253. (progn
  254. (goto-char (match-end 0))
  255. nil)))))
  256. res))))
  257. (defun c-make-font-lock-search-form (regexp highlights)
  258. ;; Return a lisp form which will fontify every occurrence of REGEXP
  259. ;; (a regular expression, NOT a function) between POINT and `limit'
  260. ;; with HIGHLIGHTS, a list of highlighters as specified on page
  261. ;; "Search-based Fontification" in the elisp manual.
  262. `(while (re-search-forward ,regexp limit t)
  263. (unless (progn
  264. (goto-char (match-beginning 0))
  265. (c-skip-comments-and-strings limit))
  266. (goto-char (match-end 0))
  267. ,@(mapcar
  268. (lambda (highlight)
  269. (if (integerp (car highlight))
  270. ;; e.g. highlight is (1 font-lock-type-face t)
  271. (progn
  272. (unless (eq (nth 2 highlight) t)
  273. (error
  274. "The override flag must currently be t in %s"
  275. highlight))
  276. (when (nth 3 highlight)
  277. (error
  278. "The laxmatch flag may currently not be set in %s"
  279. highlight))
  280. `(save-match-data
  281. (c-put-font-lock-face
  282. (match-beginning ,(car highlight))
  283. (match-end ,(car highlight))
  284. ,(elt highlight 1))))
  285. ;; highlight is an "ANCHORED HIGHLIGHTER" of the form
  286. ;; (ANCHORED-MATCHER PRE-FORM POST-FORM SUBEXP-HIGHLIGHTERS...)
  287. (when (nth 3 highlight)
  288. (error "Match highlights currently not supported in %s"
  289. highlight))
  290. `(progn
  291. ,(nth 1 highlight)
  292. (save-match-data ,(car highlight))
  293. ,(nth 2 highlight))))
  294. highlights))))
  295. (defun c-make-font-lock-search-function (regexp &rest highlights)
  296. ;; This function makes a byte compiled function that works much like
  297. ;; a matcher element in `font-lock-keywords'. It cuts out a little
  298. ;; bit of the overhead compared to a real matcher. The main reason
  299. ;; is however to pass the real search limit to the anchored
  300. ;; matcher(s), since most (if not all) font-lock implementations
  301. ;; arbitrarily limit anchored matchers to the same line, and also
  302. ;; to insulate against various other irritating differences between
  303. ;; the different (X)Emacs font-lock packages.
  304. ;;
  305. ;; REGEXP is the matcher, which must be a regexp. Only matches
  306. ;; where the beginning is outside any comment or string literal are
  307. ;; significant.
  308. ;;
  309. ;; HIGHLIGHTS is a list of highlight specs, just like in
  310. ;; `font-lock-keywords', with these limitations: The face is always
  311. ;; overridden (no big disadvantage, since hits in comments etc are
  312. ;; filtered anyway), there is no "laxmatch", and an anchored matcher
  313. ;; is always a form which must do all the fontification directly.
  314. ;; `limit' is a variable bound to the real limit in the context of
  315. ;; the anchored matcher forms.
  316. ;;
  317. ;; This function does not do any hidden buffer changes, but the
  318. ;; generated functions will. (They are however used in places
  319. ;; covered by the font-lock context.)
  320. ;; Note: Replace `byte-compile' with `eval' to debug the generated
  321. ;; lambda more easily.
  322. (byte-compile
  323. `(lambda (limit)
  324. (let ( ;; The font-lock package in Emacs is known to clobber
  325. ;; `parse-sexp-lookup-properties' (when it exists).
  326. (parse-sexp-lookup-properties
  327. (cc-eval-when-compile
  328. (boundp 'parse-sexp-lookup-properties))))
  329. ;; (while (re-search-forward ,regexp limit t)
  330. ;; (unless (progn
  331. ;; (goto-char (match-beginning 0))
  332. ;; (c-skip-comments-and-strings limit))
  333. ;; (goto-char (match-end 0))
  334. ;; ,@(mapcar
  335. ;; (lambda (highlight)
  336. ;; (if (integerp (car highlight))
  337. ;; (progn
  338. ;; (unless (eq (nth 2 highlight) t)
  339. ;; (error
  340. ;; "The override flag must currently be t in %s"
  341. ;; highlight))
  342. ;; (when (nth 3 highlight)
  343. ;; (error
  344. ;; "The laxmatch flag may currently not be set in %s"
  345. ;; highlight))
  346. ;; `(save-match-data
  347. ;; (c-put-font-lock-face
  348. ;; (match-beginning ,(car highlight))
  349. ;; (match-end ,(car highlight))
  350. ;; ,(elt highlight 1))))
  351. ;; (when (nth 3 highlight)
  352. ;; (error "Match highlights currently not supported in %s"
  353. ;; highlight))
  354. ;; `(progn
  355. ;; ,(nth 1 highlight)
  356. ;; (save-match-data ,(car highlight))
  357. ;; ,(nth 2 highlight))))
  358. ;; highlights)))
  359. ,(c-make-font-lock-search-form regexp highlights))
  360. nil)))
  361. (defun c-make-font-lock-BO-decl-search-function (regexp &rest highlights)
  362. ;; This function makes a byte compiled function that first moves back
  363. ;; to the beginning of the current declaration (if any), then searches
  364. ;; forward for matcher elements (as in `font-lock-keywords') and
  365. ;; fontifies them.
  366. ;;
  367. ;; The motivation for moving back to the declaration start is to
  368. ;; establish a context for the current text when, e.g., a character
  369. ;; is typed on a C++ inheritance continuation line, or a jit-lock
  370. ;; chunk starts there.
  371. ;;
  372. ;; The new function works much like a matcher element in
  373. ;; `font-lock-keywords'. It cuts out a little bit of the overhead
  374. ;; compared to a real matcher. The main reason is however to pass the
  375. ;; real search limit to the anchored matcher(s), since most (if not
  376. ;; all) font-lock implementations arbitrarily limit anchored matchers
  377. ;; to the same line, and also to insulate against various other
  378. ;; irritating differences between the different (X)Emacs font-lock
  379. ;; packages.
  380. ;;
  381. ;; REGEXP is the matcher, which must be a regexp. Only matches
  382. ;; where the beginning is outside any comment or string literal are
  383. ;; significant.
  384. ;;
  385. ;; HIGHLIGHTS is a list of highlight specs, just like in
  386. ;; `font-lock-keywords', with these limitations: The face is always
  387. ;; overridden (no big disadvantage, since hits in comments etc are
  388. ;; filtered anyway), there is no "laxmatch", and an anchored matcher
  389. ;; is always a form which must do all the fontification directly.
  390. ;; `limit' is a variable bound to the real limit in the context of
  391. ;; the anchored matcher forms.
  392. ;;
  393. ;; This function does not do any hidden buffer changes, but the
  394. ;; generated functions will. (They are however used in places
  395. ;; covered by the font-lock context.)
  396. ;; Note: Replace `byte-compile' with `eval' to debug the generated
  397. ;; lambda more easily.
  398. (byte-compile
  399. `(lambda (limit)
  400. (let ( ;; The font-lock package in Emacs is known to clobber
  401. ;; `parse-sexp-lookup-properties' (when it exists).
  402. (parse-sexp-lookup-properties
  403. (cc-eval-when-compile
  404. (boundp 'parse-sexp-lookup-properties)))
  405. (BOD-limit
  406. (c-determine-limit 1000)))
  407. (goto-char
  408. (let ((here (point)))
  409. (if (eq (car (c-beginning-of-decl-1 BOD-limit)) 'same)
  410. (point)
  411. here)))
  412. ,(c-make-font-lock-search-form regexp highlights))
  413. nil)))
  414. (defun c-make-font-lock-context-search-function (normal &rest state-stanzas)
  415. ;; This function makes a byte compiled function that works much like
  416. ;; a matcher element in `font-lock-keywords', with the following
  417. ;; enhancement: the generated function will test for particular "font
  418. ;; lock contexts" at the start of the region, i.e. is this point in
  419. ;; the middle of some particular construct? if so the generated
  420. ;; function will first fontify the tail of the construct, before
  421. ;; going into the main loop and fontify full constructs up to limit.
  422. ;;
  423. ;; The generated function takes one parameter called `limit', and
  424. ;; will fontify the region between POINT and LIMIT.
  425. ;;
  426. ;; NORMAL is a list of the form (REGEXP HIGHLIGHTS .....), and is
  427. ;; used to fontify the "regular" bit of the region.
  428. ;; STATE-STANZAS is list of elements of the form (STATE LIM REGEXP
  429. ;; HIGHLIGHTS), each element coding one possible font lock context.
  430. ;; o - REGEXP is a font-lock regular expression (NOT a function),
  431. ;; o - HIGHLIGHTS is a list of zero or more highlighters as defined
  432. ;; on page "Search-based Fontification" in the elisp manual. As
  433. ;; yet (2009-06), they must have OVERRIDE set, and may not have
  434. ;; LAXMATCH set.
  435. ;;
  436. ;; o - STATE is the "font lock context" (e.g. in-cpp-expr) and is
  437. ;; not quoted.
  438. ;; o - LIM is a lisp form whose evaluation will yield the limit
  439. ;; position in the buffer for fontification by this stanza.
  440. ;;
  441. ;; This function does not do any hidden buffer changes, but the
  442. ;; generated functions will. (They are however used in places
  443. ;; covered by the font-lock context.)
  444. ;;
  445. ;; Note: Replace `byte-compile' with `eval' to debug the generated
  446. ;; lambda more easily.
  447. (byte-compile
  448. `(lambda (limit)
  449. (let ( ;; The font-lock package in Emacs is known to clobber
  450. ;; `parse-sexp-lookup-properties' (when it exists).
  451. (parse-sexp-lookup-properties
  452. (cc-eval-when-compile
  453. (boundp 'parse-sexp-lookup-properties))))
  454. ,@(mapcar
  455. (lambda (stanza)
  456. (let ((state (car stanza))
  457. (lim (nth 1 stanza))
  458. (regexp (nth 2 stanza))
  459. (highlights (cdr (cddr stanza))))
  460. `(if (eq c-font-lock-context ',state)
  461. (let ((limit ,lim))
  462. ,(c-make-font-lock-search-form
  463. regexp highlights)))))
  464. state-stanzas)
  465. ,(c-make-font-lock-search-form (car normal) (cdr normal))
  466. nil))))
  467. ; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
  468. ; '(progn
  469. (def-edebug-spec c-fontify-types-and-refs let*)
  470. (def-edebug-spec c-make-syntactic-matcher t)
  471. ;; If there are literal quoted or backquoted highlight specs in
  472. ;; the call to `c-make-font-lock-search-function' then let's
  473. ;; instrument the forms in them.
  474. (def-edebug-spec c-make-font-lock-search-function
  475. (form &rest &or ("quote" (&rest form)) ("`" (&rest form)) form)));))
  476. (defun c-fontify-recorded-types-and-refs ()
  477. ;; Convert the ranges recorded on `c-record-type-identifiers' and
  478. ;; `c-record-ref-identifiers' to fontification.
  479. ;;
  480. ;; This function does hidden buffer changes.
  481. (let (elem)
  482. (while (consp c-record-type-identifiers)
  483. (setq elem (car c-record-type-identifiers)
  484. c-record-type-identifiers (cdr c-record-type-identifiers))
  485. (c-put-font-lock-face (car elem) (cdr elem)
  486. 'font-lock-type-face))
  487. (while c-record-ref-identifiers
  488. (setq elem (car c-record-ref-identifiers)
  489. c-record-ref-identifiers (cdr c-record-ref-identifiers))
  490. ;; Note that the reference face is a variable that is
  491. ;; dereferenced, since it's an alias in Emacs.
  492. (c-put-font-lock-face (car elem) (cdr elem)
  493. c-reference-face-name))))
  494. (c-lang-defconst c-cpp-matchers
  495. "Font lock matchers for preprocessor directives and purely lexical
  496. stuff. Used on level 1 and higher."
  497. ;; Note: `c-font-lock-declarations' assumes that no matcher here
  498. ;; sets `font-lock-type-face' in languages where
  499. ;; `c-recognize-<>-arglists' is set.
  500. t `(,@(when (c-lang-const c-opt-cpp-prefix)
  501. (let* ((noncontinued-line-end "\\(\\=\\|\\(\\=\\|[^\\]\\)[\n\r]\\)")
  502. (ncle-depth (regexp-opt-depth noncontinued-line-end))
  503. (sws-depth (c-lang-const c-syntactic-ws-depth))
  504. (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
  505. `(;; The stuff after #error and #warning is a message, so
  506. ;; fontify it as a string.
  507. ,@(when (c-lang-const c-cpp-message-directives)
  508. (let* ((re (c-make-keywords-re 'appendable ; nil
  509. (c-lang-const c-cpp-message-directives)))
  510. (re-depth (regexp-opt-depth re)))
  511. `((,(concat noncontinued-line-end
  512. (c-lang-const c-opt-cpp-prefix)
  513. re
  514. "\\s +\\(.*\\)$")
  515. ,(+ ncle-depth re-depth 1) font-lock-string-face t))))
  516. ;; Fontify filenames in #include <...> as strings.
  517. ,@(when (c-lang-const c-cpp-include-directives)
  518. (let* ((re (c-make-keywords-re nil
  519. (c-lang-const c-cpp-include-directives)))
  520. (re-depth (regexp-opt-depth re)))
  521. `((,(concat noncontinued-line-end
  522. (c-lang-const c-opt-cpp-prefix)
  523. re
  524. (c-lang-const c-syntactic-ws)
  525. "\\(<[^>\n\r]*>?\\)")
  526. (,(+ ncle-depth re-depth sws-depth 1)
  527. font-lock-string-face)
  528. ;; Use an anchored matcher to put paren syntax
  529. ;; on the brackets.
  530. (,(byte-compile
  531. `(lambda (limit)
  532. (let ((beg (match-beginning
  533. ,(+ ncle-depth re-depth sws-depth 1)))
  534. (end (1- (match-end ,(+ ncle-depth re-depth
  535. sws-depth 1)))))
  536. (if (eq (char-after end) ?>)
  537. (progn
  538. (c-mark-<-as-paren beg)
  539. (c-mark->-as-paren end))
  540. ;; (c-clear-char-property beg 'syntax-table)
  541. (c-clear-char-property beg 'category)))
  542. nil)))))))
  543. ;; #define.
  544. ,@(when (c-lang-const c-opt-cpp-macro-define)
  545. `((,(c-make-font-lock-search-function
  546. (concat
  547. noncontinued-line-end
  548. (c-lang-const c-opt-cpp-prefix)
  549. (c-lang-const c-opt-cpp-macro-define)
  550. (c-lang-const c-nonempty-syntactic-ws)
  551. "\\(" (c-lang-const ; 1 + ncle + nsws
  552. c-symbol-key) "\\)"
  553. (concat "\\(" ; 2 + ncle + nsws + c-sym-key
  554. ;; Macro with arguments - a "function".
  555. "\\(\(\\)" ; 3 + ncle + nsws + c-sym-key
  556. "\\|"
  557. ;; Macro without arguments - a "variable".
  558. "\\([^\(]\\|$\\)"
  559. "\\)"))
  560. `((if (match-beginning
  561. ,(+ 3 ncle-depth nsws-depth
  562. (c-lang-const c-symbol-key-depth)))
  563. ;; "Function". Fontify the name and the arguments.
  564. (save-restriction
  565. (c-put-font-lock-face
  566. (match-beginning ,(+ 1 ncle-depth nsws-depth))
  567. (match-end ,(+ 1 ncle-depth nsws-depth))
  568. 'font-lock-function-name-face)
  569. (goto-char
  570. (match-end
  571. ,(+ 3 ncle-depth nsws-depth
  572. (c-lang-const c-symbol-key-depth))))
  573. (narrow-to-region (point-min) limit)
  574. (while (and
  575. (progn
  576. (c-forward-syntactic-ws)
  577. (looking-at c-symbol-key))
  578. (progn
  579. (c-put-font-lock-face
  580. (match-beginning 0) (match-end 0)
  581. 'font-lock-variable-name-face)
  582. (goto-char (match-end 0))
  583. (c-forward-syntactic-ws)
  584. (eq (char-after) ?,)))
  585. (forward-char)))
  586. ;; "Variable".
  587. (c-put-font-lock-face
  588. (match-beginning ,(+ 1 ncle-depth nsws-depth))
  589. (match-end ,(+ 1 ncle-depth nsws-depth))
  590. 'font-lock-variable-name-face)))))))
  591. ;; Fontify cpp function names in preprocessor
  592. ;; expressions in #if and #elif.
  593. ,@(when (and (c-lang-const c-cpp-expr-directives)
  594. (c-lang-const c-cpp-expr-functions))
  595. (let ((ced-re (c-make-keywords-re t
  596. (c-lang-const c-cpp-expr-directives)))
  597. (cef-re (c-make-keywords-re t
  598. (c-lang-const c-cpp-expr-functions))))
  599. `((,(c-make-font-lock-context-search-function
  600. `(,(concat noncontinued-line-end
  601. (c-lang-const c-opt-cpp-prefix)
  602. ced-re ; 1 + ncle-depth
  603. ;; Match the whole logical line to look
  604. ;; for the functions in.
  605. "\\(\\\\\\(.\\|[\n\r]\\)\\|[^\n\r]\\)*")
  606. ((let ((limit (match-end 0)))
  607. (while (re-search-forward ,cef-re limit 'move)
  608. (c-put-font-lock-face (match-beginning 1)
  609. (match-end 1)
  610. c-preprocessor-face-name)))
  611. (goto-char (match-end ,(1+ ncle-depth)))))
  612. `(in-cpp-expr
  613. (save-excursion (c-end-of-macro) (point))
  614. ,cef-re
  615. (1 c-preprocessor-face-name t)))))))
  616. ;; Fontify the directive names.
  617. (,(c-make-font-lock-search-function
  618. (concat noncontinued-line-end
  619. "\\("
  620. (c-lang-const c-opt-cpp-prefix)
  621. "[" (c-lang-const c-symbol-chars) "]+"
  622. "\\)")
  623. `(,(1+ ncle-depth) c-preprocessor-face-name t)))
  624. (eval . (list ,(c-make-syntactic-matcher
  625. (concat noncontinued-line-end
  626. (c-lang-const c-opt-cpp-prefix)
  627. "if\\(n\\)def\\>"))
  628. ,(+ ncle-depth 1)
  629. c-negation-char-face-name
  630. 'append))
  631. )))
  632. ,@(when (c-major-mode-is 'pike-mode)
  633. ;; Recognize hashbangs in Pike.
  634. `((eval . (list "\\`#![^\n\r]*"
  635. 0 c-preprocessor-face-name))))
  636. ;; Make hard spaces visible through an inverted `font-lock-warning-face'.
  637. (eval . (list
  638. "\240"
  639. 0 (progn
  640. (unless (c-face-name-p 'c-nonbreakable-space-face)
  641. (c-make-inverse-face 'font-lock-warning-face
  642. 'c-nonbreakable-space-face))
  643. ''c-nonbreakable-space-face)))
  644. ))
  645. (defun c-font-lock-invalid-string ()
  646. ;; Assuming the point is after the opening character of a string,
  647. ;; fontify that char with `font-lock-warning-face' if the string
  648. ;; decidedly isn't terminated properly.
  649. ;;
  650. ;; This function does hidden buffer changes.
  651. (let ((start (1- (point))))
  652. (save-excursion
  653. (and (eq (elt (parse-partial-sexp start (c-point 'eol)) 8) start)
  654. (if (integerp c-multiline-string-start-char)
  655. ;; There's no multiline string start char before the
  656. ;; string, so newlines aren't allowed.
  657. (not (eq (char-before start) c-multiline-string-start-char))
  658. ;; Multiline strings are allowed anywhere if
  659. ;; c-multiline-string-start-char is t.
  660. (not c-multiline-string-start-char))
  661. (if c-string-escaped-newlines
  662. ;; There's no \ before the newline.
  663. (not (eq (char-before (point)) ?\\))
  664. ;; Escaped newlines aren't supported.
  665. t)
  666. (c-put-font-lock-face start (1+ start) 'font-lock-warning-face)))))
  667. (c-lang-defconst c-basic-matchers-before
  668. "Font lock matchers for basic keywords, labels, references and various
  669. other easily recognizable things that should be fontified before generic
  670. casts and declarations are fontified. Used on level 2 and higher."
  671. ;; Note: `c-font-lock-declarations' assumes that no matcher here
  672. ;; sets `font-lock-type-face' in languages where
  673. ;; `c-recognize-<>-arglists' is set.
  674. t `(;; Put a warning face on the opener of unclosed strings that
  675. ;; can't span lines. Later font
  676. ;; lock packages have a `font-lock-syntactic-face-function' for
  677. ;; this, but it doesn't give the control we want since any
  678. ;; fontification done inside the function will be
  679. ;; unconditionally overridden.
  680. ,(c-make-font-lock-search-function
  681. ;; Match a char before the string starter to make
  682. ;; `c-skip-comments-and-strings' work correctly.
  683. (concat ".\\(" c-string-limit-regexp "\\)")
  684. '((c-font-lock-invalid-string)))
  685. ;; Fontify keyword constants.
  686. ,@(when (c-lang-const c-constant-kwds)
  687. (let ((re (c-make-keywords-re nil (c-lang-const c-constant-kwds))))
  688. (if (c-major-mode-is 'pike-mode)
  689. ;; No symbol is a keyword after "->" in Pike.
  690. `((eval . (list ,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
  691. "\\<\\(" re "\\)\\>")
  692. 2 c-constant-face-name)))
  693. `((eval . (list ,(concat "\\<\\(" re "\\)\\>")
  694. 1 c-constant-face-name))))))
  695. ;; Fontify all keywords except the primitive types.
  696. ,(if (c-major-mode-is 'pike-mode)
  697. ;; No symbol is a keyword after "->" in Pike.
  698. `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
  699. "\\<" (c-lang-const c-regular-keywords-regexp))
  700. 2 font-lock-keyword-face)
  701. `(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
  702. 1 font-lock-keyword-face))
  703. ;; Fontify leading identifiers in fully qualified names like
  704. ;; "foo::bar" in languages that supports such things.
  705. ,@(when (c-lang-const c-opt-identifier-concat-key)
  706. (if (c-major-mode-is 'java-mode)
  707. ;; Java needs special treatment since "." is used both to
  708. ;; qualify names and in normal indexing. Here we look for
  709. ;; capital characters at the beginning of an identifier to
  710. ;; recognize the class. "*" is also recognized to cover
  711. ;; wildcard import declarations. All preceding dot separated
  712. ;; identifiers are taken as package names and therefore
  713. ;; fontified as references.
  714. `(,(c-make-font-lock-search-function
  715. ;; Search for class identifiers preceded by ".". The
  716. ;; anchored matcher takes it from there.
  717. (concat (c-lang-const c-opt-identifier-concat-key)
  718. (c-lang-const c-simple-ws) "*"
  719. (concat "\\("
  720. "[" c-upper "]"
  721. "[" (c-lang-const c-symbol-chars) "]*"
  722. "\\|"
  723. "\\*"
  724. "\\)"))
  725. `((let (id-end)
  726. (goto-char (1+ (match-beginning 0)))
  727. (while (and (eq (char-before) ?.)
  728. (progn
  729. (backward-char)
  730. (c-backward-syntactic-ws)
  731. (setq id-end (point))
  732. (< (skip-chars-backward
  733. ,(c-lang-const c-symbol-chars)) 0))
  734. (not (get-text-property (point) 'face)))
  735. (c-put-font-lock-face (point) id-end
  736. c-reference-face-name)
  737. (c-backward-syntactic-ws)))
  738. nil
  739. (goto-char (match-end 0)))))
  740. `((,(byte-compile
  741. ;; Must use a function here since we match longer than
  742. ;; we want to move before doing a new search. This is
  743. ;; not necessary for XEmacs since it restarts the
  744. ;; search from the end of the first highlighted
  745. ;; submatch (something that causes problems in other
  746. ;; places).
  747. `(lambda (limit)
  748. (while (re-search-forward
  749. ,(concat "\\(\\<" ; 1
  750. "\\(" (c-lang-const c-symbol-key) "\\)" ; 2
  751. (c-lang-const c-simple-ws) "*"
  752. (c-lang-const c-opt-identifier-concat-key)
  753. (c-lang-const c-simple-ws) "*"
  754. "\\)"
  755. "\\("
  756. (c-lang-const c-opt-after-id-concat-key)
  757. "\\)")
  758. limit t)
  759. (unless (progn
  760. (goto-char (match-beginning 0))
  761. (c-skip-comments-and-strings limit))
  762. (or (get-text-property (match-beginning 2) 'face)
  763. (c-put-font-lock-face (match-beginning 2)
  764. (match-end 2)
  765. c-reference-face-name))
  766. (goto-char (match-end 1))))))))))
  767. ;; Fontify the special declarations in Objective-C.
  768. ,@(when (c-major-mode-is 'objc-mode)
  769. `(;; Fontify class names in the beginning of message expressions.
  770. ,(c-make-font-lock-search-function
  771. "\\["
  772. '((c-fontify-types-and-refs ()
  773. (c-forward-syntactic-ws limit)
  774. (let ((start (point)))
  775. ;; In this case we accept both primitive and known types.
  776. (when (eq (c-forward-type) 'known)
  777. (goto-char start)
  778. (let ((c-promote-possible-types t))
  779. (c-forward-type))))
  780. (if (> (point) limit) (goto-char limit)))))
  781. ;; The @interface/@implementation/@protocol directives.
  782. ,(c-make-font-lock-search-function
  783. (concat "\\<"
  784. (regexp-opt
  785. '("@interface" "@implementation" "@protocol")
  786. t)
  787. "\\>")
  788. '((c-fontify-types-and-refs
  789. (;; The font-lock package in Emacs is known to clobber
  790. ;; `parse-sexp-lookup-properties' (when it exists).
  791. (parse-sexp-lookup-properties
  792. (cc-eval-when-compile
  793. (boundp 'parse-sexp-lookup-properties))))
  794. (c-forward-objc-directive)
  795. nil)
  796. (goto-char (match-beginning 0))))))
  797. (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
  798. ))
  799. (defun c-font-lock-complex-decl-prepare (limit)
  800. ;; This function will be called from font-lock for a region bounded by POINT
  801. ;; and LIMIT, as though it were to identify a keyword for
  802. ;; font-lock-keyword-face. It always returns NIL to inhibit this and
  803. ;; prevent a repeat invocation. See elisp/lispref page "Search-based
  804. ;; Fontification".
  805. ;;
  806. ;; Called before any of the matchers in `c-complex-decl-matchers'.
  807. ;;
  808. ;; This function does hidden buffer changes.
  809. ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
  810. ;; Clear the list of found types if we start from the start of the
  811. ;; buffer, to make it easier to get rid of misspelled types and
  812. ;; variables that have gotten recognized as types in malformed code.
  813. (when (bobp)
  814. (c-clear-found-types))
  815. ;; Clear the c-type char properties which mark the region, to recalculate
  816. ;; them properly. The most interesting properties are those put on the
  817. ;; closest token before the region.
  818. (save-excursion
  819. (let ((pos (point)))
  820. (c-backward-syntactic-ws)
  821. (c-clear-char-properties
  822. (if (and (not (bobp))
  823. (memq (c-get-char-property (1- (point)) 'c-type)
  824. '(c-decl-arg-start
  825. c-decl-end
  826. c-decl-id-start
  827. c-decl-type-start)))
  828. (1- (point))
  829. pos)
  830. limit 'c-type)))
  831. ;; Update `c-state-cache' to the beginning of the region. This will
  832. ;; make `c-beginning-of-syntax' go faster when it's used later on,
  833. ;; and it's near the point most of the time.
  834. (c-parse-state)
  835. ;; Check if the fontified region starts inside a declarator list so
  836. ;; that `c-font-lock-declarators' should be called at the start.
  837. ;; The declared identifiers are font-locked correctly as types, if
  838. ;; that is what they are.
  839. (let ((prop (save-excursion
  840. (c-backward-syntactic-ws)
  841. (unless (bobp)
  842. (c-get-char-property (1- (point)) 'c-type)))))
  843. (when (memq prop '(c-decl-id-start c-decl-type-start))
  844. (c-forward-syntactic-ws limit)
  845. (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
  846. (setq c-font-lock-context ;; (c-guess-font-lock-context)
  847. (save-excursion
  848. (if (and c-cpp-expr-intro-re
  849. (c-beginning-of-macro)
  850. (looking-at c-cpp-expr-intro-re))
  851. 'in-cpp-expr)))
  852. nil)
  853. (defun c-font-lock-<>-arglists (limit)
  854. ;; This function will be called from font-lock for a region bounded by POINT
  855. ;; and LIMIT, as though it were to identify a keyword for
  856. ;; font-lock-keyword-face. It always returns NIL to inhibit this and
  857. ;; prevent a repeat invocation. See elisp/lispref page "Search-based
  858. ;; Fontification".
  859. ;;
  860. ;; Fontify types and references in names containing angle bracket
  861. ;; arglists from the point to LIMIT. Note that
  862. ;; `c-font-lock-declarations' already has handled many of them.
  863. ;;
  864. ;; This function might do hidden buffer changes.
  865. (let (;; The font-lock package in Emacs is known to clobber
  866. ;; `parse-sexp-lookup-properties' (when it exists).
  867. (parse-sexp-lookup-properties
  868. (cc-eval-when-compile
  869. (boundp 'parse-sexp-lookup-properties)))
  870. (c-parse-and-markup-<>-arglists t)
  871. c-restricted-<>-arglists
  872. id-start id-end id-face pos kwd-sym)
  873. (while (and (< (point) limit)
  874. (re-search-forward c-opt-<>-arglist-start limit t))
  875. (setq id-start (match-beginning 1)
  876. id-end (match-end 1)
  877. pos (point))
  878. (goto-char id-start)
  879. (unless (c-skip-comments-and-strings limit)
  880. (setq kwd-sym nil
  881. c-restricted-<>-arglists nil
  882. id-face (get-text-property id-start 'face))
  883. (if (cond
  884. ((eq id-face 'font-lock-type-face)
  885. ;; The identifier got the type face so it has already been
  886. ;; handled in `c-font-lock-declarations'.
  887. nil)
  888. ((eq id-face 'font-lock-keyword-face)
  889. (when (looking-at c-opt-<>-sexp-key)
  890. ;; There's a special keyword before the "<" that tells
  891. ;; that it's an angle bracket arglist.
  892. (setq kwd-sym (c-keyword-sym (match-string 1)))))
  893. (t
  894. ;; There's a normal identifier before the "<". If we're not in
  895. ;; a declaration context then we set `c-restricted-<>-arglists'
  896. ;; to avoid recognizing templates in function calls like "foo (a
  897. ;; < b, c > d)".
  898. (c-backward-syntactic-ws)
  899. (when (and (memq (char-before) '(?\( ?,))
  900. (not (eq (get-text-property (1- (point)) 'c-type)
  901. 'c-decl-arg-start)))
  902. (setq c-restricted-<>-arglists t))
  903. t))
  904. (progn
  905. (goto-char (1- pos))
  906. ;; Check for comment/string both at the identifier and
  907. ;; at the "<".
  908. (unless (c-skip-comments-and-strings limit)
  909. (c-fontify-types-and-refs ()
  910. (when (c-forward-<>-arglist (c-keyword-member
  911. kwd-sym 'c-<>-type-kwds))
  912. (when (and c-opt-identifier-concat-key
  913. (not (get-text-property id-start 'face)))
  914. (c-forward-syntactic-ws)
  915. (if (looking-at c-opt-identifier-concat-key)
  916. (c-put-font-lock-face id-start id-end
  917. c-reference-face-name)
  918. (c-put-font-lock-face id-start id-end
  919. 'font-lock-type-face)))))
  920. (goto-char pos)))
  921. (goto-char pos)))))
  922. nil)
  923. (defun c-font-lock-declarators (limit list types)
  924. ;; Assuming the point is at the start of a declarator in a declaration,
  925. ;; fontify the identifier it declares. (If TYPES is set, it does this via
  926. ;; the macro `c-fontify-types-and-refs'.)
  927. ;;
  928. ;; If LIST is non-nil, also fontify the ids in any following declarators in
  929. ;; a comma separated list (e.g. "foo" and "*bar" in "int foo = 17, *bar;");
  930. ;; additionally, mark the commas with c-type property 'c-decl-id-start or
  931. ;; 'c-decl-type-start (according to TYPES). Stop at LIMIT.
  932. ;;
  933. ;; If TYPES is non-nil, fontify all identifiers as types.
  934. ;;
  935. ;; Nil is always returned. The function leaves point at the delimiter after
  936. ;; the last declarator it processes.
  937. ;;
  938. ;; This function might do hidden buffer changes.
  939. ;;(message "c-font-lock-declarators from %s to %s" (point) limit)
  940. (c-fontify-types-and-refs
  941. ((pos (point)) next-pos id-start id-end
  942. paren-depth
  943. id-face got-init
  944. c-last-identifier-range
  945. (separator-prop (if types 'c-decl-type-start 'c-decl-id-start)))
  946. ;; The following `while' fontifies a single declarator id each time round.
  947. ;; It loops only when LIST is non-nil.
  948. (while
  949. ;; Inside the following "condition form", we move forward over the
  950. ;; declarator's identifier up as far as any opening bracket (for array
  951. ;; size) or paren (for parameters of function-type) or brace (for
  952. ;; array/struct initialization) or "=" or terminating delimiter
  953. ;; (e.g. "," or ";" or "}").
  954. (and
  955. pos
  956. (< (point) limit)
  957. ;; The following form moves forward over the declarator's
  958. ;; identifier (and what precedes it), returning t. If there
  959. ;; wasn't one, it returns nil, terminating the `while'.
  960. (let (got-identifier)
  961. (setq paren-depth 0)
  962. ;; Skip over type decl prefix operators, one for each iteration
  963. ;; of the while. These are, e.g. "*" in "int *foo" or "(" and
  964. ;; "*" in "int (*foo) (void)" (Note similar code in
  965. ;; `c-forward-decl-or-cast-1'.)
  966. (while (and (looking-at c-type-decl-prefix-key)
  967. (if (and (c-major-mode-is 'c++-mode)
  968. (match-beginning 3))
  969. ;; If the third submatch matches in C++ then
  970. ;; we're looking at an identifier that's a
  971. ;; prefix only if it specifies a member pointer.
  972. (progn
  973. (setq id-start (point))
  974. (c-forward-name)
  975. (if (looking-at "\\(::\\)")
  976. ;; We only check for a trailing "::" and
  977. ;; let the "*" that should follow be
  978. ;; matched in the next round.
  979. t
  980. ;; It turned out to be the real identifier,
  981. ;; so flag that and stop.
  982. (setq got-identifier t)
  983. nil))
  984. t))
  985. (if (eq (char-after) ?\()
  986. (progn
  987. (setq paren-depth (1+ paren-depth))
  988. (forward-char))
  989. (goto-char (match-end 1)))
  990. (c-forward-syntactic-ws))
  991. ;; If we haven't passed the identifier already, do it now.
  992. (unless got-identifier
  993. (setq id-start (point))
  994. (c-forward-name))
  995. (setq id-end (point))
  996. (/= id-end pos))
  997. ;; Skip out of the parens surrounding the identifier. If closing
  998. ;; parens are missing, this form returns nil.
  999. (or (= paren-depth 0)
  1000. (c-safe (goto-char (scan-lists (point) 1 paren-depth))))
  1001. (<= (point) limit)
  1002. ;; Skip over any trailing bit, such as "__attribute__".
  1003. (progn
  1004. (when (looking-at c-decl-hangon-key)
  1005. (c-forward-keyword-clause 1))
  1006. (<= (point) limit))
  1007. ;; Search syntactically to the end of the declarator (";",
  1008. ;; ",", a closing paren, eob etc) or to the beginning of an
  1009. ;; initializer or function prototype ("=" or "\\s\(").
  1010. ;; Note that the open paren will match array specs in
  1011. ;; square brackets, and we treat them as initializers too.
  1012. (c-syntactic-re-search-forward
  1013. "[;,]\\|\\s)\\|\\'\\|\\(=\\|\\s(\\)" limit t t))
  1014. (setq next-pos (match-beginning 0)
  1015. id-face (if (and (eq (char-after next-pos) ?\()
  1016. (let (c-last-identifier-range)
  1017. (save-excursion
  1018. (goto-char next-pos)
  1019. (c-at-toplevel-p))))
  1020. 'font-lock-function-name-face
  1021. 'font-lock-variable-name-face)
  1022. got-init (and (match-beginning 1)
  1023. (char-after (match-beginning 1))))
  1024. (if types
  1025. ;; Register and fontify the identifier as a type.
  1026. (let ((c-promote-possible-types t))
  1027. (goto-char id-start)
  1028. (c-forward-type))
  1029. ;; Fontify the last symbol in the identifier if it isn't fontified
  1030. ;; already. The check is necessary only in certain cases where this
  1031. ;; function is used "sloppily", e.g. in `c-simple-decl-matchers'.
  1032. (when (and c-last-identifier-range
  1033. (not (get-text-property (car c-last-identifier-range)
  1034. 'face)))
  1035. (c-put-font-lock-face (car c-last-identifier-range)
  1036. (cdr c-last-identifier-range)
  1037. id-face)))
  1038. (goto-char next-pos)
  1039. (setq pos nil) ; So as to terminate the enclosing `while' form.
  1040. (when list
  1041. ;; Jump past any initializer or function prototype to see if
  1042. ;; there's a ',' to continue at.
  1043. (cond ((eq id-face 'font-lock-function-name-face)
  1044. ;; Skip a parenthesized initializer (C++) or a function
  1045. ;; prototype.
  1046. (if (c-safe (c-forward-sexp 1) t) ; over the parameter list.
  1047. (c-forward-syntactic-ws limit)
  1048. (goto-char limit))) ; unbalanced parens
  1049. (got-init ; "=" sign OR opening "(", "[", or "{"
  1050. ;; Skip an initializer expression. If we're at a '='
  1051. ;; then accept a brace list directly after it to cope
  1052. ;; with array initializers. Otherwise stop at braces
  1053. ;; to avoid going past full function and class blocks.
  1054. (and (if (and (eq got-init ?=)
  1055. (= (c-forward-token-2 1 nil limit) 0)
  1056. (looking-at "{"))
  1057. (c-safe (c-forward-sexp) t) ; over { .... }
  1058. t)
  1059. ;; FIXME: Should look for c-decl-end markers here;
  1060. ;; we might go far into the following declarations
  1061. ;; in e.g. ObjC mode (see e.g. methods-4.m).
  1062. (c-syntactic-re-search-forward "[;,{]" limit 'move t)
  1063. (backward-char)))
  1064. (t (c-forward-syntactic-ws limit)))
  1065. ;; If a ',' is found we set pos to the next declarator and iterate.
  1066. (when (and (< (point) limit) (looking-at ","))
  1067. (c-put-char-property (point) 'c-type separator-prop)
  1068. (forward-char)
  1069. (c-forward-syntactic-ws limit)
  1070. (setq pos (point)))))) ; acts to make the `while' form continue.
  1071. nil)
  1072. (defconst c-font-lock-maybe-decl-faces
  1073. ;; List of faces that might be put at the start of a type when
  1074. ;; `c-font-lock-declarations' runs. This needs to be evaluated to
  1075. ;; ensure that face name aliases in Emacs are resolved.
  1076. (list nil
  1077. font-lock-type-face
  1078. c-reference-face-name
  1079. font-lock-keyword-face))
  1080. (defun c-font-lock-declarations (limit)
  1081. ;; Fontify all the declarations, casts and labels from the point to LIMIT.
  1082. ;; Assumes that strings and comments have been fontified already.
  1083. ;;
  1084. ;; This function will be called from font-lock for a region bounded by POINT
  1085. ;; and LIMIT, as though it were to identify a keyword for
  1086. ;; font-lock-keyword-face. It always returns NIL to inhibit this and
  1087. ;; prevent a repeat invocation. See elisp/lispref page "Search-based
  1088. ;; Fontification".
  1089. ;;
  1090. ;; This function might do hidden buffer changes.
  1091. ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
  1092. (save-restriction
  1093. (let (;; The position where `c-find-decl-spots' last stopped.
  1094. start-pos
  1095. ;; o - 'decl if we're in an arglist containing declarations
  1096. ;; (but if `c-recognize-paren-inits' is set it might also be
  1097. ;; an initializer arglist);
  1098. ;; o - '<> if the arglist is of angle bracket type;
  1099. ;; o - 'arglist if it's some other arglist;
  1100. ;; o - nil, if not in an arglist at all. This includes the
  1101. ;; parenthesized condition which follows "if", "while", etc.
  1102. context
  1103. ;; The position of the next token after the closing paren of
  1104. ;; the last detected cast.
  1105. last-cast-end
  1106. ;; The result from `c-forward-decl-or-cast-1'.
  1107. decl-or-cast
  1108. ;; The maximum of the end positions of all the checked type
  1109. ;; decl expressions in the successfully identified
  1110. ;; declarations. The position might be either before or
  1111. ;; after the syntactic whitespace following the last token
  1112. ;; in the type decl expression.
  1113. (max-type-decl-end 0)
  1114. ;; Same as `max-type-decl-*', but used when we're before
  1115. ;; `token-pos'.
  1116. (max-type-decl-end-before-token 0)
  1117. ;; Set according to the context to direct the heuristics for
  1118. ;; recognizing C++ templates.
  1119. c-restricted-<>-arglists
  1120. ;; Turn on recording of identifier ranges in
  1121. ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
  1122. ;; later fontification.
  1123. (c-record-type-identifiers t)
  1124. label-type
  1125. c-record-ref-identifiers
  1126. ;; Make `c-forward-type' calls mark up template arglists if
  1127. ;; it finds any. That's necessary so that we later will
  1128. ;; stop inside them to fontify types there.
  1129. (c-parse-and-markup-<>-arglists t)
  1130. lbrace ; position of some {.
  1131. ;; The font-lock package in Emacs is known to clobber
  1132. ;; `parse-sexp-lookup-properties' (when it exists).
  1133. (parse-sexp-lookup-properties
  1134. (cc-eval-when-compile
  1135. (boundp 'parse-sexp-lookup-properties))))
  1136. ;; Below we fontify a whole declaration even when it crosses the limit,
  1137. ;; to avoid gaps when jit/lazy-lock fontifies the file a block at a
  1138. ;; time. That is however annoying during editing, e.g. the following is
  1139. ;; a common situation while the first line is being written:
  1140. ;;
  1141. ;; my_variable
  1142. ;; some_other_variable = 0;
  1143. ;;
  1144. ;; font-lock will put the limit at the beginning of the second line
  1145. ;; here, and if we go past it we'll fontify "my_variable" as a type and
  1146. ;; "some_other_variable" as an identifier, and the latter will not
  1147. ;; correct itself until the second line is changed. To avoid that we
  1148. ;; narrow to the limit if the region to fontify is a single line.
  1149. (if (<= limit (c-point 'bonl))
  1150. (narrow-to-region
  1151. (point-min)
  1152. (save-excursion
  1153. ;; Narrow after any operator chars following the limit though,
  1154. ;; since those characters can be useful in recognizing a
  1155. ;; declaration (in particular the '{' that opens a function body
  1156. ;; after the header).
  1157. (goto-char limit)
  1158. (skip-chars-forward c-nonsymbol-chars)
  1159. (point))))
  1160. (c-find-decl-spots
  1161. limit
  1162. c-decl-start-re
  1163. c-font-lock-maybe-decl-faces
  1164. (lambda (match-pos inside-macro)
  1165. (setq start-pos (point))
  1166. (when
  1167. ;; The result of the form below is true when we don't recognize a
  1168. ;; declaration or cast.
  1169. (if (or (and (eq (get-text-property (point) 'face)
  1170. 'font-lock-keyword-face)
  1171. (looking-at c-not-decl-init-keywords))
  1172. (and c-macro-with-semi-re
  1173. (looking-at c-macro-with-semi-re))) ; 2008-11-04
  1174. ;; Don't do anything more if we're looking at a keyword that
  1175. ;; can't start a declaration.
  1176. t
  1177. ;; Set `context' and `c-restricted-<>-arglists'. Look for
  1178. ;; "<" for the sake of C++-style template arglists.
  1179. ;; Ignore "(" when it's part of a control flow construct
  1180. ;; (e.g. "for (").
  1181. (let ((type (and (> match-pos (point-min))
  1182. (c-get-char-property (1- match-pos) 'c-type))))
  1183. (cond ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?<)))
  1184. (setq context nil
  1185. c-restricted-<>-arglists nil))
  1186. ;; A control flow expression
  1187. ((and (eq (char-before match-pos) ?\()
  1188. (save-excursion
  1189. (goto-char match-pos)
  1190. (backward-char)
  1191. (c-backward-token-2)
  1192. (looking-at c-block-stmt-2-key)))
  1193. (setq context nil
  1194. c-restricted-<>-arglists t))
  1195. ;; Near BOB.
  1196. ((<= match-pos (point-min))
  1197. (setq context 'arglist
  1198. c-restricted-<>-arglists t))
  1199. ;; Got a cached hit in a declaration arglist.
  1200. ((eq type 'c-decl-arg-start)
  1201. (setq context 'decl
  1202. c-restricted-<>-arglists nil))
  1203. ;; Inside an angle bracket arglist.
  1204. ((or (eq type 'c-<>-arg-sep)
  1205. (eq (char-before match-pos) ?<))
  1206. (setq context '<>
  1207. c-restricted-<>-arglists nil))
  1208. ;; Got a cached hit in some other type of arglist.
  1209. (type
  1210. (setq context 'arglist
  1211. c-restricted-<>-arglists t))
  1212. ((if inside-macro
  1213. (< match-pos max-type-decl-end-before-token)
  1214. (< match-pos max-type-decl-end))
  1215. ;; The point is within the range of a previously
  1216. ;; encountered type decl expression, so the arglist
  1217. ;; is probably one that contains declarations.
  1218. ;; However, if `c-recognize-paren-inits' is set it
  1219. ;; might also be an initializer arglist.
  1220. (setq context 'decl
  1221. c-restricted-<>-arglists nil)
  1222. ;; The result of this check is cached with a char
  1223. ;; property on the match token, so that we can look
  1224. ;; it up again when refontifying single lines in a
  1225. ;; multiline declaration.
  1226. (c-put-char-property (1- match-pos)
  1227. 'c-type 'c-decl-arg-start))
  1228. (t (setq context 'arglist
  1229. c-restricted-<>-arglists t))))
  1230. ;; Check we haven't missed a preceding "typedef".
  1231. (when (not (looking-at c-typedef-key))
  1232. (c-backward-syntactic-ws)
  1233. (c-backward-token-2)
  1234. (or (looking-at c-typedef-key)
  1235. (goto-char start-pos)))
  1236. ;; In QT, "more" is an irritating keyword that expands to nothing.
  1237. ;; We skip over it to prevent recognition of "more slots: <symbol>"
  1238. ;; as a bitfield declaration.
  1239. (when (and (c-major-mode-is 'c++-mode)
  1240. (looking-at
  1241. (concat "\\(more\\)\\([^" c-symbol-chars "]\\|$\\)")))
  1242. (goto-char (match-end 1))
  1243. (c-forward-syntactic-ws))
  1244. ;; Now analyze the construct.
  1245. (setq decl-or-cast (c-forward-decl-or-cast-1
  1246. match-pos context last-cast-end))
  1247. (cond
  1248. ((eq decl-or-cast 'cast)
  1249. ;; Save the position after the previous cast so we can feed
  1250. ;; it to `c-forward-decl-or-cast-1' in the next round. That
  1251. ;; helps it discover cast chains like "(a) (b) c".
  1252. (setq last-cast-end (point))
  1253. (c-fontify-recorded-types-and-refs)
  1254. nil)
  1255. (decl-or-cast
  1256. ;; We've found a declaration.
  1257. ;; Set `max-type-decl-end' or `max-type-decl-end-before-token'
  1258. ;; under the assumption that we're after the first type decl
  1259. ;; expression in the declaration now. That's not really true;
  1260. ;; we could also be after a parenthesized initializer
  1261. ;; expression in C++, but this is only used as a last resort
  1262. ;; to slant ambiguous expression/declarations, and overall
  1263. ;; it's worth the risk to occasionally fontify an expression
  1264. ;; as a declaration in an initializer expression compared to
  1265. ;; getting ambiguous things in normal function prototypes
  1266. ;; fontified as expressions.
  1267. (if inside-macro
  1268. (when (> (point) max-type-decl-end-before-token)
  1269. (setq max-type-decl-end-before-token (point)))
  1270. (when (> (point) max-type-decl-end)
  1271. (setq max-type-decl-end (point))))
  1272. ;; Back up to the type to fontify the declarator(s).
  1273. (goto-char (car decl-or-cast))
  1274. (let ((decl-list
  1275. (if context
  1276. ;; Should normally not fontify a list of
  1277. ;; declarators inside an arglist, but the first
  1278. ;; argument in the ';' separated list of a "for"
  1279. ;; statement is an exception.
  1280. (when (eq (char-before match-pos) ?\()
  1281. (save-excursion
  1282. (goto-char (1- match-pos))
  1283. (c-backward-syntactic-ws)
  1284. (and (c-simple-skip-symbol-backward)
  1285. (looking-at c-paren-stmt-key))))
  1286. t)))
  1287. ;; Fix the `c-decl-id-start' or `c-decl-type-start' property
  1288. ;; before the first declarator if it's a list.
  1289. ;; `c-font-lock-declarators' handles the rest.
  1290. (when decl-list
  1291. (save-excursion
  1292. (c-backward-syntactic-ws)
  1293. (unless (bobp)
  1294. (c-put-char-property (1- (point)) 'c-type
  1295. (if (cdr decl-or-cast)
  1296. 'c-decl-type-start
  1297. 'c-decl-id-start)))))
  1298. (c-font-lock-declarators
  1299. (point-max) decl-list (cdr decl-or-cast)))
  1300. ;; A declaration has been successfully identified, so do all the
  1301. ;; fontification of types and refs that've been recorded.
  1302. (c-fontify-recorded-types-and-refs)
  1303. nil)
  1304. ;; Restore point, since at this point in the code it has been
  1305. ;; left undefined by c-forward-decl-or-cast-1 above.
  1306. ((progn (goto-char start-pos) nil))
  1307. ;; If point is inside a bracelist, there's no point checking it
  1308. ;; being at a declarator.
  1309. ((let ((paren-state (c-parse-state)))
  1310. (setq lbrace (c-cheap-inside-bracelist-p paren-state)))
  1311. ;; Move past this bracelist to prevent an endless loop.
  1312. (goto-char lbrace)
  1313. (unless (c-safe (progn (forward-list) t))
  1314. (goto-char start-pos)
  1315. (c-forward-token-2))
  1316. nil)
  1317. ;; If point is just after a ")" which is followed by an
  1318. ;; identifier which isn't a label, or at the matching "(", we're
  1319. ;; at either a macro invocation, a cast, or a
  1320. ;; for/while/etc. statement. The cast case is handled above.
  1321. ;; None of these cases can contain a declarator.
  1322. ((or (and (eq (char-before match-pos) ?\))
  1323. (c-on-identifier)
  1324. (save-excursion (not (c-forward-label))))
  1325. (and (eq (char-after) ?\()
  1326. (save-excursion
  1327. (and
  1328. (progn (c-backward-token-2) (c-on-identifier))
  1329. (save-excursion (not (c-forward-label)))
  1330. (progn (c-backward-token-2)
  1331. (eq (char-after) ?\())))))
  1332. (c-forward-token-2) ; Must prevent looping.
  1333. nil)
  1334. ((and (not c-enums-contain-decls)
  1335. ;; An optimization quickly to eliminate scans of long enum
  1336. ;; declarations in the next cond arm.
  1337. (let ((paren-state (c-parse-state)))
  1338. (and
  1339. (numberp (car paren-state))
  1340. (save-excursion
  1341. (goto-char (car paren-state))
  1342. (c-backward-token-2)
  1343. (or (looking-at c-brace-list-key)
  1344. (progn
  1345. (c-backward-token-2)
  1346. (looking-at c-brace-list-key)))))))
  1347. (c-forward-token-2)
  1348. nil)
  1349. (t
  1350. ;; Are we at a declarator? Try to go back to the declaration
  1351. ;; to check this. If we get there, check whether a "typedef"
  1352. ;; is there, then fontify the declarators accordingly.
  1353. (let ((decl-search-lim (c-determine-limit 1000))
  1354. paren-state bod-res encl-pos is-typedef
  1355. c-recognize-knr-p) ; Strictly speaking, bogus, but it
  1356. ; speeds up lisp.h tremendously.
  1357. (save-excursion
  1358. (setq bod-res (car (c-beginning-of-decl-1 decl-search-lim)))
  1359. (if (and (eq bod-res 'same)
  1360. (progn
  1361. (c-backward-syntactic-ws)
  1362. (eq (char-before) ?\})))
  1363. (c-beginning-of-decl-1 decl-search-lim))
  1364. ;; We're now putatively at the declaration.
  1365. (setq paren-state (c-parse-state))
  1366. ;; At top level or inside a "{"?
  1367. (if (or (not (setq encl-pos
  1368. (c-most-enclosing-brace paren-state)))
  1369. (eq (char-after encl-pos) ?\{))
  1370. (progn
  1371. (when (looking-at c-typedef-key) ; "typedef"
  1372. (setq is-typedef t)
  1373. (goto-char (match-end 0))
  1374. (c-forward-syntactic-ws))
  1375. ;; At a real declaration?
  1376. (if (memq (c-forward-type t) '(t known found))
  1377. (progn
  1378. (c-font-lock-declarators limit t is-typedef)
  1379. nil)
  1380. ;; False alarm. Return t to go on to the next check.
  1381. (goto-char start-pos)
  1382. t))
  1383. t))))))
  1384. ;; It was a false alarm. Check if we're in a label (or other
  1385. ;; construct with `:' except bitfield) instead.
  1386. (goto-char start-pos)
  1387. (when (setq label-type (c-forward-label t match-pos nil))
  1388. ;; Can't use `c-fontify-types-and-refs' here since we
  1389. ;; use the label face at times.
  1390. (cond ((eq label-type 'goto-target)
  1391. (c-put-font-lock-face (caar c-record-ref-identifiers)
  1392. (cdar c-record-ref-identifiers)
  1393. c-label-face-name))
  1394. ((eq label-type 'qt-1kwd-colon)
  1395. (c-put-font-lock-face (caar c-record-ref-identifiers)
  1396. (cdar c-record-ref-identifiers)
  1397. 'font-lock-keyword-face))
  1398. ((eq label-type 'qt-2kwds-colon)
  1399. (mapc
  1400. (lambda (kwd)
  1401. (c-put-font-lock-face (car kwd) (cdr kwd)
  1402. 'font-lock-keyword-face))
  1403. c-record-ref-identifiers)))
  1404. (setq c-record-ref-identifiers nil)
  1405. ;; `c-forward-label' has probably added a `c-decl-end'
  1406. ;; marker, so return t to `c-find-decl-spots' to signal
  1407. ;; that.
  1408. t))))
  1409. nil)))
  1410. (defun c-font-lock-enum-tail (limit)
  1411. ;; Fontify an enum's identifiers when POINT is within the enum's brace
  1412. ;; block.
  1413. ;;
  1414. ;; This function will be called from font-lock for a region bounded by POINT
  1415. ;; and LIMIT, as though it were to identify a keyword for
  1416. ;; font-lock-keyword-face. It always returns NIL to inhibit this and
  1417. ;; prevent a repeat invocation. See elisp/lispref page "Search-based
  1418. ;; Fontification".
  1419. ;;
  1420. ;; Note that this function won't attempt to fontify beyond the end of the
  1421. ;; current enum block, if any.
  1422. (let* ((paren-state (c-parse-state))
  1423. (encl-pos (c-most-enclosing-brace paren-state))
  1424. (start (point))
  1425. )
  1426. (when (and
  1427. encl-pos
  1428. (eq (char-after encl-pos) ?\{)
  1429. (save-excursion
  1430. (goto-char encl-pos)
  1431. (c-backward-syntactic-ws)
  1432. (c-simple-skip-symbol-backward)
  1433. (or (looking-at c-brace-list-key) ; "enum"
  1434. (progn (c-backward-syntactic-ws)
  1435. (c-simple-skip-symbol-backward)
  1436. (looking-at c-brace-list-key)))))
  1437. (c-syntactic-skip-backward "^{," nil t)
  1438. (c-put-char-property (1- (point)) 'c-type 'c-decl-id-start)
  1439. (c-forward-syntactic-ws)
  1440. (c-font-lock-declarators limit t nil)))
  1441. nil)
  1442. (defun c-font-lock-enclosing-decls (limit)
  1443. ;; Fontify the declarators of (nested) declarations we're in the middle of.
  1444. ;; This is mainly for when a jit-lock etc. chunk starts inside the brace
  1445. ;; block of a struct/union/class, etc.
  1446. ;;
  1447. ;; This function will be called from font-lock for a region bounded by POINT
  1448. ;; and LIMIT, as though it were to identify a keyword for
  1449. ;; font-lock-keyword-face. It always returns NIL to inhibit this and
  1450. ;; prevent a repeat invocation. See elisp/lispref page "Search-based
  1451. ;; Fontification".
  1452. (let* ((paren-state (c-parse-state))
  1453. decl-context in-typedef ps-elt)
  1454. ;; Are we in any nested struct/union/class/etc. braces?
  1455. (while paren-state
  1456. (setq ps-elt (car paren-state)
  1457. paren-state (cdr paren-state))
  1458. (when (and (atom ps-elt)
  1459. (eq (char-after ps-elt) ?\{))
  1460. (goto-char ps-elt)
  1461. (setq decl-context (c-beginning-of-decl-1)
  1462. in-typedef (looking-at c-typedef-key))
  1463. (if in-typedef (c-forward-token-2))
  1464. (when (and c-opt-block-decls-with-vars-key
  1465. (looking-at c-opt-block-decls-with-vars-key))
  1466. (goto-char ps-elt)
  1467. (when (c-safe (c-forward-sexp))
  1468. (c-forward-syntactic-ws)
  1469. (c-font-lock-declarators limit t in-typedef)))))))
  1470. (c-lang-defconst c-simple-decl-matchers
  1471. "Simple font lock matchers for types and declarations. These are used
  1472. on level 2 only and so aren't combined with `c-complex-decl-matchers'."
  1473. t `(;; Objective-C methods.
  1474. ,@(when (c-major-mode-is 'objc-mode)
  1475. `((,(c-lang-const c-opt-method-key)
  1476. (,(byte-compile
  1477. (lambda (limit)
  1478. (let (;; The font-lock package in Emacs is known to clobber
  1479. ;; `parse-sexp-lookup-properties' (when it exists).
  1480. (parse-sexp-lookup-properties
  1481. (cc-eval-when-compile
  1482. (boundp 'parse-sexp-lookup-properties))))
  1483. (save-restriction
  1484. (narrow-to-region (point-min) limit)
  1485. (c-font-lock-objc-method)))
  1486. nil))
  1487. (goto-char (match-end 1))))))
  1488. ;; Fontify all type names and the identifiers in the
  1489. ;; declarations they might start. Use eval here since
  1490. ;; `c-known-type-key' gets its value from
  1491. ;; `*-font-lock-extra-types' on mode init.
  1492. (eval . (list ,(c-make-font-lock-search-function
  1493. 'c-known-type-key
  1494. '(1 'font-lock-type-face t)
  1495. '((c-font-lock-declarators limit t nil)
  1496. (save-match-data
  1497. (goto-char (match-end 1))
  1498. (c-forward-syntactic-ws))
  1499. (goto-char (match-end 1))))))
  1500. ;; Fontify types preceded by `c-type-prefix-kwds' and the
  1501. ;; identifiers in the declarations they might start.
  1502. ,@(when (c-lang-const c-type-prefix-kwds)
  1503. (let* ((prefix-re (c-make-keywords-re nil
  1504. (c-lang-const c-type-prefix-kwds)))
  1505. (type-match (+ 2
  1506. (regexp-opt-depth prefix-re)
  1507. (c-lang-const c-simple-ws-depth))))
  1508. `((,(c-make-font-lock-search-function
  1509. (concat "\\<\\(" prefix-re "\\)" ; 1
  1510. (c-lang-const c-simple-ws) "+"
  1511. (concat "\\(" ; 2 + prefix-re + c-simple-ws
  1512. (c-lang-const c-symbol-key)
  1513. "\\)"))
  1514. `(,type-match
  1515. 'font-lock-type-face t)
  1516. `((c-font-lock-declarators limit t nil)
  1517. (save-match-data
  1518. (goto-char (match-end ,type-match))
  1519. (c-forward-syntactic-ws))
  1520. (goto-char (match-end ,type-match))))))))
  1521. ;; Fontify special declarations that lacks a type.
  1522. ,@(when (c-lang-const c-typeless-decl-kwds)
  1523. `((,(c-make-font-lock-search-function
  1524. (concat "\\<\\("
  1525. (regexp-opt (c-lang-const c-typeless-decl-kwds))
  1526. "\\)\\>")
  1527. '((c-font-lock-declarators limit t nil)
  1528. (save-match-data
  1529. (goto-char (match-end 1))
  1530. (c-forward-syntactic-ws))
  1531. (goto-char (match-end 1)))))))
  1532. ;; Fontify generic colon labels in languages that support them.
  1533. ,@(when (c-lang-const c-recognize-colon-labels)
  1534. `(c-font-lock-labels))))
  1535. (c-lang-defconst c-complex-decl-matchers
  1536. "Complex font lock matchers for types and declarations. Used on level
  1537. 3 and higher."
  1538. ;; Note: This code in this form dumps a number of functions into the
  1539. ;; resulting constant, `c-matchers-3'. At run time, font lock will call
  1540. ;; each of them as a "FUNCTION" (see Elisp page "Search-based
  1541. ;; Fontification"). The font lock region is delimited by POINT and the
  1542. ;; single parameter, LIMIT. Each of these functions returns NIL (thus
  1543. ;; inhibiting spurious font-lock-keyword-face highlighting and another
  1544. ;; call).
  1545. t `(;; Initialize some things before the search functions below.
  1546. c-font-lock-complex-decl-prepare
  1547. ,@(if (c-major-mode-is 'objc-mode)
  1548. ;; Fontify method declarations in Objective-C, but first
  1549. ;; we have to put the `c-decl-end' `c-type' property on
  1550. ;; all the @-style directives that haven't been handled in
  1551. ;; `c-basic-matchers-before'.
  1552. `(,(c-make-font-lock-search-function
  1553. (c-make-keywords-re t
  1554. ;; Exclude "@class" since that directive ends with a
  1555. ;; semicolon anyway.
  1556. (delete "@class"
  1557. (append (c-lang-const c-protection-kwds)
  1558. (c-lang-const c-other-decl-kwds)
  1559. nil)))
  1560. '((c-put-char-property (1- (match-end 1))
  1561. 'c-type 'c-decl-end)))
  1562. c-font-lock-objc-methods))
  1563. ;; Fontify all declarations, casts and normal labels.
  1564. c-font-lock-declarations
  1565. ;; Fontify declarators when POINT is within their declaration.
  1566. c-font-lock-enclosing-decls
  1567. ;; Fontify angle bracket arglists like templates in C++.
  1568. ,@(when (c-lang-const c-recognize-<>-arglists)
  1569. `(c-font-lock-<>-arglists))
  1570. ;; The first two rules here mostly find occurrences that
  1571. ;; `c-font-lock-declarations' has found already, but not
  1572. ;; declarations containing blocks in the type (see note below).
  1573. ;; It's also useful to fontify these everywhere to show e.g. when
  1574. ;; a type keyword is accidentally used as an identifier.
  1575. ;; Fontify basic types.
  1576. ,(let ((re (c-make-keywords-re nil
  1577. (c-lang-const c-primitive-type-kwds))))
  1578. (if (c-major-mode-is 'pike-mode)
  1579. ;; No symbol is a keyword after "->" in Pike.
  1580. `(,(concat "\\(\\=.?\\|[^>]\\|[^-]>\\)"
  1581. "\\<\\(" re "\\)\\>")
  1582. 2 font-lock-type-face)
  1583. `(,(concat "\\<\\(" re "\\)\\>")
  1584. 1 'font-lock-type-face)))
  1585. ;; Fontify types preceded by `c-type-prefix-kwds' (e.g. "struct").
  1586. ,@(when (c-lang-const c-type-prefix-kwds)
  1587. `((,(byte-compile
  1588. `(lambda (limit)
  1589. (c-fontify-types-and-refs
  1590. ((c-promote-possible-types t)
  1591. ;; The font-lock package in Emacs is known to clobber
  1592. ;; `parse-sexp-lookup-properties' (when it exists).
  1593. (parse-sexp-lookup-properties
  1594. (cc-eval-when-compile
  1595. (boundp 'parse-sexp-lookup-properties))))
  1596. (save-restriction
  1597. ;; Narrow to avoid going past the limit in
  1598. ;; `c-forward-type'.
  1599. (narrow-to-region (point) limit)
  1600. (while (re-search-forward
  1601. ,(concat "\\<\\("
  1602. (c-make-keywords-re nil
  1603. (c-lang-const c-type-prefix-kwds))
  1604. "\\)\\>")
  1605. limit t)
  1606. (unless (c-skip-comments-and-strings limit)
  1607. (c-forward-syntactic-ws)
  1608. ;; Handle prefix declaration specifiers.
  1609. (when (or (looking-at c-prefix-spec-kwds-re)
  1610. (and (c-major-mode-is 'java-mode)
  1611. (looking-at "@[A-Za-z0-9]+")))
  1612. (c-forward-keyword-clause 1))
  1613. ,(if (c-major-mode-is 'c++-mode)
  1614. `(when (and (c-forward-type)
  1615. (eq (char-after) ?=))
  1616. ;; In C++ we additionally check for a "class
  1617. ;; X = Y" construct which is used in
  1618. ;; templates, to fontify Y as a type.
  1619. (forward-char)
  1620. (c-forward-syntactic-ws)
  1621. (c-forward-type))
  1622. `(c-forward-type))
  1623. )))))))))
  1624. ;; Fontify symbols after closing braces as declaration
  1625. ;; identifiers under the assumption that they are part of
  1626. ;; declarations like "class Foo { ... } foo;". It's too
  1627. ;; expensive to check this accurately by skipping past the
  1628. ;; brace block, so we use the heuristic that it's such a
  1629. ;; declaration if the first identifier is on the same line as
  1630. ;; the closing brace. `c-font-lock-declarations' will later
  1631. ;; override it if it turns out to be an new declaration, but
  1632. ;; it will be wrong if it's an expression (see the test
  1633. ;; decls-8.cc).
  1634. ;; ,@(when (c-lang-const c-opt-block-decls-with-vars-key)
  1635. ;; `((,(c-make-font-lock-search-function
  1636. ;; (concat "}"
  1637. ;; (c-lang-const c-single-line-syntactic-ws)
  1638. ;; "\\(" ; 1 + c-single-line-syntactic-ws-depth
  1639. ;; (c-lang-const c-type-decl-prefix-key)
  1640. ;; "\\|"
  1641. ;; (c-lang-const c-symbol-key)
  1642. ;; "\\)")
  1643. ;; `((c-font-lock-declarators limit t nil) ; That `nil' says use `font-lock-variable-name-face';
  1644. ;; ; `t' would mean `font-lock-function-name-face'.
  1645. ;; (progn
  1646. ;; (c-put-char-property (match-beginning 0) 'c-type
  1647. ;; 'c-decl-id-start)
  1648. ;; ; 'c-decl-type-start)
  1649. ;; (goto-char (match-beginning
  1650. ;; ,(1+ (c-lang-const
  1651. ;; c-single-line-syntactic-ws-depth)))))
  1652. ;; (goto-char (match-end 0)))))))
  1653. ;; Fontify the type in C++ "new" expressions.
  1654. ,@(when (c-major-mode-is 'c++-mode)
  1655. ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
  1656. ;; (see Elisp page "Search-based Fontification").
  1657. `(("\\<new\\>"
  1658. (c-font-lock-c++-new))))
  1659. ))
  1660. (defun c-font-lock-labels (limit)
  1661. ;; Fontify all statement labels from the point to LIMIT. Assumes
  1662. ;; that strings and comments have been fontified already. Nil is
  1663. ;; always returned.
  1664. ;;
  1665. ;; Note: This function is only used on decoration level 2; this is
  1666. ;; taken care of directly by the gargantuan
  1667. ;; `c-font-lock-declarations' on higher levels.
  1668. ;;
  1669. ;; This function might do hidden buffer changes.
  1670. (let (continue-pos id-start
  1671. ;; The font-lock package in Emacs is known to clobber
  1672. ;; `parse-sexp-lookup-properties' (when it exists).
  1673. (parse-sexp-lookup-properties
  1674. (cc-eval-when-compile
  1675. (boundp 'parse-sexp-lookup-properties))))
  1676. (while (re-search-forward ":[^:]" limit t)
  1677. (setq continue-pos (point))
  1678. (goto-char (match-beginning 0))
  1679. (unless (c-skip-comments-and-strings limit)
  1680. (c-backward-syntactic-ws)
  1681. (and (setq id-start (c-on-identifier))
  1682. (not (get-text-property id-start 'face))
  1683. (progn
  1684. (goto-char id-start)
  1685. (c-backward-syntactic-ws)
  1686. (or
  1687. ;; Check for a char that precedes a statement.
  1688. (memq (char-before) '(?\} ?\{ ?\;))
  1689. ;; Check for a preceding label. We exploit the font
  1690. ;; locking made earlier by this function.
  1691. (and (eq (char-before) ?:)
  1692. (progn
  1693. (backward-char)
  1694. (c-backward-syntactic-ws)
  1695. (not (bobp)))
  1696. (eq (get-text-property (1- (point)) 'face)
  1697. c-label-face-name))
  1698. ;; Check for a keyword that precedes a statement.
  1699. (c-after-conditional)))
  1700. (progn
  1701. ;; Got a label.
  1702. (goto-char id-start)
  1703. (looking-at c-symbol-key)
  1704. (c-put-font-lock-face (match-beginning 0) (match-end 0)
  1705. c-label-face-name)))
  1706. (goto-char continue-pos))))
  1707. nil)
  1708. (c-lang-defconst c-basic-matchers-after
  1709. "Font lock matchers for various things that should be fontified after
  1710. generic casts and declarations are fontified. Used on level 2 and
  1711. higher."
  1712. t `(,@(when (c-lang-const c-brace-id-list-kwds)
  1713. ;; Fontify the remaining identifiers inside an enum list when we start
  1714. ;; inside it.
  1715. `(c-font-lock-enum-tail
  1716. ;; Fontify the identifiers inside enum lists. (The enum type
  1717. ;; name is handled by `c-simple-decl-matchers' or
  1718. ;; `c-complex-decl-matchers' below.
  1719. (,(c-make-font-lock-search-function
  1720. (concat
  1721. "\\<\\("
  1722. (c-make-keywords-re nil (c-lang-const c-brace-id-list-kwds))
  1723. "\\)\\>"
  1724. ;; Disallow various common punctuation chars that can't come
  1725. ;; before the '{' of the enum list, to avoid searching too far.
  1726. "[^\]\[{}();,/#=]*"
  1727. "{")
  1728. '((c-font-lock-declarators limit t nil)
  1729. (save-match-data
  1730. (goto-char (match-end 0))
  1731. (c-put-char-property (1- (point)) 'c-type
  1732. 'c-decl-id-start)
  1733. (c-forward-syntactic-ws))
  1734. (goto-char (match-end 0)))))))
  1735. ;; Fontify labels after goto etc.
  1736. ,@(when (c-lang-const c-before-label-kwds)
  1737. `(;; (Got three different interpretation levels here,
  1738. ;; which makes it a bit complicated: 1) The backquote
  1739. ;; stuff is expanded when compiled or loaded, 2) the
  1740. ;; eval form is evaluated at font-lock setup (to
  1741. ;; substitute c-label-face-name correctly), and 3) the
  1742. ;; resulting structure is interpreted during
  1743. ;; fontification.)
  1744. (eval
  1745. . ,(let* ((c-before-label-re
  1746. (c-make-keywords-re nil
  1747. (c-lang-const c-before-label-kwds))))
  1748. `(list
  1749. ,(concat "\\<\\(" c-before-label-re "\\)\\>"
  1750. "\\s *"
  1751. "\\(" ; identifier-offset
  1752. (c-lang-const c-symbol-key)
  1753. "\\)")
  1754. (list ,(+ (regexp-opt-depth c-before-label-re) 2)
  1755. c-label-face-name nil t))))))
  1756. ;; Fontify the clauses after various keywords.
  1757. ,@(when (or (c-lang-const c-type-list-kwds)
  1758. (c-lang-const c-ref-list-kwds)
  1759. (c-lang-const c-colon-type-list-kwds))
  1760. `((,(c-make-font-lock-BO-decl-search-function
  1761. (concat "\\<\\("
  1762. (c-make-keywords-re nil
  1763. (append (c-lang-const c-type-list-kwds)
  1764. (c-lang-const c-ref-list-kwds)
  1765. (c-lang-const c-colon-type-list-kwds)))
  1766. "\\)\\>")
  1767. '((c-fontify-types-and-refs ((c-promote-possible-types t))
  1768. (c-forward-keyword-clause 1)
  1769. (if (> (point) limit) (goto-char limit))))))))
  1770. ,@(when (c-lang-const c-paren-type-kwds)
  1771. `((,(c-make-font-lock-search-function
  1772. (concat "\\<\\("
  1773. (c-make-keywords-re nil
  1774. (c-lang-const c-paren-type-kwds))
  1775. "\\)\\>")
  1776. '((c-fontify-types-and-refs ((c-promote-possible-types t))
  1777. (c-forward-keyword-clause 1)
  1778. (if (> (point) limit) (goto-char limit))))))))
  1779. ,@(when (c-major-mode-is 'java-mode)
  1780. `((eval . (list "\\<\\(@[a-zA-Z0-9]+\\)\\>" 1 c-annotation-face))))
  1781. ))
  1782. (c-lang-defconst c-matchers-1
  1783. t (c-lang-const c-cpp-matchers))
  1784. (c-lang-defconst c-matchers-2
  1785. t (append (c-lang-const c-matchers-1)
  1786. (c-lang-const c-basic-matchers-before)
  1787. (c-lang-const c-simple-decl-matchers)
  1788. (c-lang-const c-basic-matchers-after)))
  1789. (c-lang-defconst c-matchers-3
  1790. t (append (c-lang-const c-matchers-1)
  1791. (c-lang-const c-basic-matchers-before)
  1792. (c-lang-const c-complex-decl-matchers)
  1793. (c-lang-const c-basic-matchers-after)))
  1794. (defun c-compose-keywords-list (base-list)
  1795. ;; Incorporate the font lock keyword lists according to
  1796. ;; `c-doc-comment-style' on the given keyword list and return it.
  1797. ;; This is used in the function bindings of the
  1798. ;; `*-font-lock-keywords-*' symbols since we have to build the list
  1799. ;; when font-lock is initialized.
  1800. (unless (memq c-doc-face-name c-literal-faces)
  1801. (setq c-literal-faces (cons c-doc-face-name c-literal-faces)))
  1802. (let* ((doc-keywords
  1803. (if (consp (car-safe c-doc-comment-style))
  1804. (cdr-safe (or (assq c-buffer-is-cc-mode c-doc-comment-style)
  1805. (assq 'other c-doc-comment-style)))
  1806. c-doc-comment-style))
  1807. (list (nconc (apply 'nconc
  1808. (mapcar
  1809. (lambda (doc-style)
  1810. (let ((sym (intern
  1811. (concat (symbol-name doc-style)
  1812. "-font-lock-keywords"))))
  1813. (cond ((fboundp sym)
  1814. (funcall sym))
  1815. ((boundp sym)
  1816. (append (eval sym) nil)))))
  1817. (if (listp doc-keywords)
  1818. doc-keywords
  1819. (list doc-keywords))))
  1820. base-list)))
  1821. ;; Kludge: If `c-font-lock-complex-decl-prepare' is on the list we
  1822. ;; move it first since the doc comment font lockers might add
  1823. ;; `c-type' text properties, so they have to be cleared before that.
  1824. (when (memq 'c-font-lock-complex-decl-prepare list)
  1825. (setq list (cons 'c-font-lock-complex-decl-prepare
  1826. (delq 'c-font-lock-complex-decl-prepare
  1827. (append list nil)))))
  1828. list))
  1829. (defun c-override-default-keywords (def-var)
  1830. ;; This is used to override the value on a `*-font-lock-keywords'
  1831. ;; variable only if it's nil or has the same value as one of the
  1832. ;; `*-font-lock-keywords-*' variables. Older font-lock packages
  1833. ;; define a default value for `*-font-lock-keywords' which we want
  1834. ;; to override, but we should otoh avoid clobbering a user setting.
  1835. ;; This heuristic for that isn't perfect, but I can't think of any
  1836. ;; better. /mast
  1837. (when (and (boundp def-var)
  1838. (memq (symbol-value def-var)
  1839. (cons nil
  1840. (mapcar
  1841. (lambda (suffix)
  1842. (let ((sym (intern (concat (symbol-name def-var)
  1843. suffix))))
  1844. (and (boundp sym) (symbol-value sym))))
  1845. '("-1" "-2" "-3")))))
  1846. ;; The overriding is done by unbinding the variable so that the normal
  1847. ;; defvar will install its default value later on.
  1848. (makunbound def-var)))
  1849. ;;; C.
  1850. (c-override-default-keywords 'c-font-lock-keywords)
  1851. (defconst c-font-lock-keywords-1 (c-lang-const c-matchers-1 c)
  1852. "Minimal font locking for C mode.
  1853. Fontifies only preprocessor directives (in addition to the syntactic
  1854. fontification of strings and comments).")
  1855. (defconst c-font-lock-keywords-2 (c-lang-const c-matchers-2 c)
  1856. "Fast normal font locking for C mode.
  1857. In addition to `c-font-lock-keywords-1', this adds fontification of
  1858. keywords, simple types, declarations that are easy to recognize, the
  1859. user defined types on `c-font-lock-extra-types', and the doc comment
  1860. styles specified by `c-doc-comment-style'.")
  1861. (defconst c-font-lock-keywords-3 (c-lang-const c-matchers-3 c)
  1862. "Accurate normal font locking for C mode.
  1863. Like `c-font-lock-keywords-2' but detects declarations in a more
  1864. accurate way that works in most cases for arbitrary types without the
  1865. need for `c-font-lock-extra-types'.")
  1866. (defvar c-font-lock-keywords c-font-lock-keywords-3
  1867. "Default expressions to highlight in C mode.")
  1868. (defun c-font-lock-keywords-2 ()
  1869. (c-compose-keywords-list c-font-lock-keywords-2))
  1870. (defun c-font-lock-keywords-3 ()
  1871. (c-compose-keywords-list c-font-lock-keywords-3))
  1872. (defun c-font-lock-keywords ()
  1873. (c-compose-keywords-list c-font-lock-keywords))
  1874. ;;; C++.
  1875. (defun c-font-lock-c++-new (limit)
  1876. ;; FIXME!!! Put in a comment about the context of this function's
  1877. ;; invocation. I think it's called as an ANCHORED-MATCHER within an
  1878. ;; ANCHORED-HIGHLIGHTER. (2007/2/10).
  1879. ;;
  1880. ;; Assuming point is after a "new" word, check that it isn't inside
  1881. ;; a string or comment, and if so try to fontify the type in the
  1882. ;; allocation expression. Nil is always returned.
  1883. ;;
  1884. ;; As usual, C++ takes the prize in coming up with a hard to parse
  1885. ;; syntax. :P
  1886. ;;
  1887. ;; This function might do hidden buffer changes.
  1888. (unless (c-skip-comments-and-strings limit)
  1889. (save-excursion
  1890. (catch 'false-alarm
  1891. ;; A "new" keyword is followed by one to three expressions, where
  1892. ;; the type is the middle one, and the only required part.
  1893. (let (expr1-pos expr2-pos
  1894. ;; Enable recording of identifier ranges in `c-forward-type'
  1895. ;; etc for later fontification. Not using
  1896. ;; `c-fontify-types-and-refs' here since the ranges should
  1897. ;; be fontified selectively only when an allocation
  1898. ;; expression is successfully recognized.
  1899. (c-record-type-identifiers t)
  1900. c-record-ref-identifiers
  1901. ;; The font-lock package in Emacs is known to clobber
  1902. ;; `parse-sexp-lookup-properties' (when it exists).
  1903. (parse-sexp-lookup-properties
  1904. (cc-eval-when-compile
  1905. (boundp 'parse-sexp-lookup-properties))))
  1906. (c-forward-syntactic-ws)
  1907. ;; The first placement arglist is always parenthesized, if it
  1908. ;; exists.
  1909. (when (eq (char-after) ?\()
  1910. (setq expr1-pos (1+ (point)))
  1911. (condition-case nil
  1912. (c-forward-sexp)
  1913. (scan-error (throw 'false-alarm t)))
  1914. (c-forward-syntactic-ws))
  1915. ;; The second expression is either a type followed by some "*" or
  1916. ;; "[...]" or similar, or a parenthesized type followed by a full
  1917. ;; identifierless declarator.
  1918. (setq expr2-pos (1+ (point)))
  1919. (cond ((eq (char-after) ?\())
  1920. ((let ((c-promote-possible-types t))
  1921. (c-forward-type)))
  1922. (t (setq expr2-pos nil)))
  1923. (when expr1-pos
  1924. (cond
  1925. ((not expr2-pos)
  1926. ;; No second expression, so the first has to be a
  1927. ;; parenthesized type.
  1928. (goto-char expr1-pos)
  1929. (let ((c-promote-possible-types t))
  1930. (c-forward-type)))
  1931. ((eq (char-before expr2-pos) ?\()
  1932. ;; Got two parenthesized expressions, so we have to look
  1933. ;; closer at them to decide which is the type. No need to
  1934. ;; handle `c-record-ref-identifiers' since all references
  1935. ;; has already been handled by other fontification rules.
  1936. (let (expr1-res expr2-res)
  1937. (goto-char expr1-pos)
  1938. (when (setq expr1-res (c-forward-type))
  1939. (unless (looking-at
  1940. (cc-eval-when-compile
  1941. (concat (c-lang-const c-symbol-start c++)
  1942. "\\|[*:\)\[]")))
  1943. ;; There's something after the would-be type that
  1944. ;; can't be there, so this is a placement arglist.
  1945. (setq expr1-res nil)))
  1946. (goto-char expr2-pos)
  1947. (when (setq expr2-res (c-forward-type))
  1948. (unless (looking-at
  1949. (cc-eval-when-compile
  1950. (concat (c-lang-const c-symbol-start c++)
  1951. "\\|[*:\)\[]")))
  1952. ;; There's something after the would-be type that can't
  1953. ;; be there, so this is an initialization expression.
  1954. (setq expr2-res nil))
  1955. (when (and (c-go-up-list-forward)
  1956. (progn (c-forward-syntactic-ws)
  1957. (eq (char-after) ?\()))
  1958. ;; If there's a third initialization expression
  1959. ;; then the second one is the type, so demote the
  1960. ;; first match.
  1961. (setq expr1-res nil)))
  1962. ;; We fontify the most likely type, with a preference for
  1963. ;; the first argument since a placement arglist is more
  1964. ;; unusual than an initializer.
  1965. (cond ((memq expr1-res '(t known prefix)))
  1966. ((memq expr2-res '(t known prefix)))
  1967. ((eq expr1-res 'found)
  1968. (let ((c-promote-possible-types t))
  1969. (goto-char expr1-pos)
  1970. (c-forward-type)))
  1971. ((eq expr2-res 'found)
  1972. (let ((c-promote-possible-types t))
  1973. (goto-char expr2-pos)
  1974. (c-forward-type)))
  1975. ((and (eq expr1-res 'maybe) (not expr2-res))
  1976. (let ((c-promote-possible-types t))
  1977. (goto-char expr1-pos)
  1978. (c-forward-type)))
  1979. ((and (not expr1-res) (eq expr2-res 'maybe))
  1980. (let ((c-promote-possible-types t))
  1981. (goto-char expr2-pos)
  1982. (c-forward-type)))
  1983. ;; If both type matches are 'maybe then we're
  1984. ;; too uncertain to promote either of them.
  1985. )))))
  1986. ;; Fontify the type that now is recorded in
  1987. ;; `c-record-type-identifiers', if any.
  1988. (c-fontify-recorded-types-and-refs)))))
  1989. nil)
  1990. (c-override-default-keywords 'c++-font-lock-keywords)
  1991. (defconst c++-font-lock-keywords-1 (c-lang-const c-matchers-1 c++)
  1992. "Minimal font locking for C++ mode.
  1993. Fontifies only preprocessor directives (in addition to the syntactic
  1994. fontification of strings and comments).")
  1995. (defconst c++-font-lock-keywords-2 (c-lang-const c-matchers-2 c++)
  1996. "Fast normal font locking for C++ mode.
  1997. In addition to `c++-font-lock-keywords-1', this adds fontification of
  1998. keywords, simple types, declarations that are easy to recognize, the
  1999. user defined types on `c++-font-lock-extra-types', and the doc comment
  2000. styles specified by `c-doc-comment-style'.")
  2001. (defconst c++-font-lock-keywords-3 (c-lang-const c-matchers-3 c++)
  2002. "Accurate normal font locking for C++ mode.
  2003. Like `c++-font-lock-keywords-2' but detects declarations in a more
  2004. accurate way that works in most cases for arbitrary types without the
  2005. need for `c++-font-lock-extra-types'.")
  2006. (defvar c++-font-lock-keywords c++-font-lock-keywords-3
  2007. "Default expressions to highlight in C++ mode.")
  2008. (defun c++-font-lock-keywords-2 ()
  2009. (c-compose-keywords-list c++-font-lock-keywords-2))
  2010. (defun c++-font-lock-keywords-3 ()
  2011. (c-compose-keywords-list c++-font-lock-keywords-3))
  2012. (defun c++-font-lock-keywords ()
  2013. (c-compose-keywords-list c++-font-lock-keywords))
  2014. ;;; Objective-C.
  2015. (defun c-font-lock-objc-method ()
  2016. ;; Assuming the point is after the + or - that starts an Objective-C
  2017. ;; method declaration, fontify it. This must be done before normal
  2018. ;; casts, declarations and labels are fontified since they will get
  2019. ;; false matches in these things.
  2020. ;;
  2021. ;; This function might do hidden buffer changes.
  2022. (c-fontify-types-and-refs
  2023. ((first t)
  2024. (c-promote-possible-types t))
  2025. (while (and
  2026. (progn
  2027. (c-forward-syntactic-ws)
  2028. ;; An optional method type.
  2029. (if (eq (char-after) ?\()
  2030. (progn
  2031. (forward-char)
  2032. (c-forward-syntactic-ws)
  2033. (c-forward-type)
  2034. (prog1 (c-go-up-list-forward)
  2035. (c-forward-syntactic-ws)))
  2036. t))
  2037. ;; The name. The first time it's the first part of
  2038. ;; the function name, the rest of the time it's an
  2039. ;; argument name.
  2040. (looking-at c-symbol-key)
  2041. (progn
  2042. (goto-char (match-end 0))
  2043. (c-put-font-lock-face (match-beginning 0)
  2044. (point)
  2045. (if first
  2046. 'font-lock-function-name-face
  2047. 'font-lock-variable-name-face))
  2048. (c-forward-syntactic-ws)
  2049. ;; Another optional part of the function name.
  2050. (when (looking-at c-symbol-key)
  2051. (goto-char (match-end 0))
  2052. (c-put-font-lock-face (match-beginning 0)
  2053. (point)
  2054. 'font-lock-function-name-face)
  2055. (c-forward-syntactic-ws))
  2056. ;; There's another argument if a colon follows.
  2057. (eq (char-after) ?:)))
  2058. (forward-char)
  2059. (setq first nil))))
  2060. (defun c-font-lock-objc-methods (limit)
  2061. ;; Fontify method declarations in Objective-C. Nil is always
  2062. ;; returned.
  2063. ;;
  2064. ;; This function might do hidden buffer changes.
  2065. (let (;; The font-lock package in Emacs is known to clobber
  2066. ;; `parse-sexp-lookup-properties' (when it exists).
  2067. (parse-sexp-lookup-properties
  2068. (cc-eval-when-compile
  2069. (boundp 'parse-sexp-lookup-properties))))
  2070. (c-find-decl-spots
  2071. limit
  2072. "[-+]"
  2073. nil
  2074. (lambda (match-pos inside-macro)
  2075. (forward-char)
  2076. (c-font-lock-objc-method))))
  2077. nil)
  2078. (c-override-default-keywords 'objc-font-lock-keywords)
  2079. (defconst objc-font-lock-keywords-1 (c-lang-const c-matchers-1 objc)
  2080. "Minimal font locking for Objective-C mode.
  2081. Fontifies only compiler directives (in addition to the syntactic
  2082. fontification of strings and comments).")
  2083. (defconst objc-font-lock-keywords-2 (c-lang-const c-matchers-2 objc)
  2084. "Fast normal font locking for Objective-C mode.
  2085. In addition to `objc-font-lock-keywords-1', this adds fontification of
  2086. keywords, simple types, declarations that are easy to recognize, the
  2087. user defined types on `objc-font-lock-extra-types', and the doc
  2088. comment styles specified by `c-doc-comment-style'.")
  2089. (defconst objc-font-lock-keywords-3 (c-lang-const c-matchers-3 objc)
  2090. "Accurate normal font locking for Objective-C mode.
  2091. Like `objc-font-lock-keywords-2' but detects declarations in a more
  2092. accurate way that works in most cases for arbitrary types without the
  2093. need for `objc-font-lock-extra-types'.")
  2094. (defvar objc-font-lock-keywords objc-font-lock-keywords-3
  2095. "Default expressions to highlight in Objective-C mode.")
  2096. (defun objc-font-lock-keywords-2 ()
  2097. (c-compose-keywords-list objc-font-lock-keywords-2))
  2098. (defun objc-font-lock-keywords-3 ()
  2099. (c-compose-keywords-list objc-font-lock-keywords-3))
  2100. (defun objc-font-lock-keywords ()
  2101. (c-compose-keywords-list objc-font-lock-keywords))
  2102. ;; Kludge to override the default value that
  2103. ;; `objc-font-lock-extra-types' might have gotten from the font-lock
  2104. ;; package. The value replaced here isn't relevant now anyway since
  2105. ;; those types are builtin and therefore listed directly in
  2106. ;; `c-primitive-type-kwds'.
  2107. (when (equal (sort (append objc-font-lock-extra-types nil) 'string-lessp)
  2108. '("BOOL" "Class" "IMP" "SEL"))
  2109. (setq objc-font-lock-extra-types
  2110. (cc-eval-when-compile (list (concat "[" c-upper "]\\sw*")))))
  2111. ;;; Java.
  2112. (c-override-default-keywords 'java-font-lock-keywords)
  2113. (defconst java-font-lock-keywords-1 (c-lang-const c-matchers-1 java)
  2114. "Minimal font locking for Java mode.
  2115. Fontifies nothing except the syntactic fontification of strings and
  2116. comments.")
  2117. (defconst java-font-lock-keywords-2 (c-lang-const c-matchers-2 java)
  2118. "Fast normal font locking for Java mode.
  2119. In addition to `java-font-lock-keywords-1', this adds fontification of
  2120. keywords, simple types, declarations that are easy to recognize, the
  2121. user defined types on `java-font-lock-extra-types', and the doc
  2122. comment styles specified by `c-doc-comment-style'.")
  2123. (defconst java-font-lock-keywords-3 (c-lang-const c-matchers-3 java)
  2124. "Accurate normal font locking for Java mode.
  2125. Like `java-font-lock-keywords-2' but detects declarations in a more
  2126. accurate way that works in most cases for arbitrary types without the
  2127. need for `java-font-lock-extra-types'.")
  2128. (defvar java-font-lock-keywords java-font-lock-keywords-3
  2129. "Default expressions to highlight in Java mode.")
  2130. (defun java-font-lock-keywords-2 ()
  2131. (c-compose-keywords-list java-font-lock-keywords-2))
  2132. (defun java-font-lock-keywords-3 ()
  2133. (c-compose-keywords-list java-font-lock-keywords-3))
  2134. (defun java-font-lock-keywords ()
  2135. (c-compose-keywords-list java-font-lock-keywords))
  2136. ;;; CORBA IDL.
  2137. (c-override-default-keywords 'idl-font-lock-keywords)
  2138. (defconst idl-font-lock-keywords-1 (c-lang-const c-matchers-1 idl)
  2139. "Minimal font locking for CORBA IDL mode.
  2140. Fontifies nothing except the syntactic fontification of strings and
  2141. comments.")
  2142. (defconst idl-font-lock-keywords-2 (c-lang-const c-matchers-2 idl)
  2143. "Fast normal font locking for CORBA IDL mode.
  2144. In addition to `idl-font-lock-keywords-1', this adds fontification of
  2145. keywords, simple types, declarations that are easy to recognize, the
  2146. user defined types on `idl-font-lock-extra-types', and the doc comment
  2147. styles specified by `c-doc-comment-style'.")
  2148. (defconst idl-font-lock-keywords-3 (c-lang-const c-matchers-3 idl)
  2149. "Accurate normal font locking for CORBA IDL mode.
  2150. Like `idl-font-lock-keywords-2' but detects declarations in a more
  2151. accurate way that works in most cases for arbitrary types without the
  2152. need for `idl-font-lock-extra-types'.")
  2153. (defvar idl-font-lock-keywords idl-font-lock-keywords-3
  2154. "Default expressions to highlight in CORBA IDL mode.")
  2155. (defun idl-font-lock-keywords-2 ()
  2156. (c-compose-keywords-list idl-font-lock-keywords-2))
  2157. (defun idl-font-lock-keywords-3 ()
  2158. (c-compose-keywords-list idl-font-lock-keywords-3))
  2159. (defun idl-font-lock-keywords ()
  2160. (c-compose-keywords-list idl-font-lock-keywords))
  2161. ;;; Pike.
  2162. (c-override-default-keywords 'pike-font-lock-keywords)
  2163. (defconst pike-font-lock-keywords-1 (c-lang-const c-matchers-1 pike)
  2164. "Minimal font locking for Pike mode.
  2165. Fontifies only preprocessor directives (in addition to the syntactic
  2166. fontification of strings and comments).")
  2167. (defconst pike-font-lock-keywords-2 (c-lang-const c-matchers-2 pike)
  2168. "Fast normal font locking for Pike mode.
  2169. In addition to `pike-font-lock-keywords-1', this adds fontification of
  2170. keywords, simple types, declarations that are easy to recognize, the
  2171. user defined types on `pike-font-lock-extra-types', and the doc
  2172. comment styles specified by `c-doc-comment-style'.")
  2173. (defconst pike-font-lock-keywords-3 (c-lang-const c-matchers-3 pike)
  2174. "Accurate normal font locking for Pike mode.
  2175. Like `pike-font-lock-keywords-2' but detects declarations in a more
  2176. accurate way that works in most cases for arbitrary types without the
  2177. need for `pike-font-lock-extra-types'.")
  2178. (defvar pike-font-lock-keywords pike-font-lock-keywords-3
  2179. "Default expressions to highlight in Pike mode.")
  2180. (defun pike-font-lock-keywords-2 ()
  2181. (c-compose-keywords-list pike-font-lock-keywords-2))
  2182. (defun pike-font-lock-keywords-3 ()
  2183. (c-compose-keywords-list pike-font-lock-keywords-3))
  2184. (defun pike-font-lock-keywords ()
  2185. (c-compose-keywords-list pike-font-lock-keywords))
  2186. ;;; Doc comments.
  2187. (defun c-font-lock-doc-comments (prefix limit keywords)
  2188. ;; Fontify the comments between the point and LIMIT whose start
  2189. ;; matches PREFIX with `c-doc-face-name'. Assumes comments have been
  2190. ;; fontified with `font-lock-comment-face' already. nil is always
  2191. ;; returned.
  2192. ;;
  2193. ;; After the fontification of a matching comment, fontification
  2194. ;; according to KEYWORDS is applied inside it. It's a list like
  2195. ;; `font-lock-keywords' except that anchored matches and eval
  2196. ;; clauses aren't supported and that some abbreviated forms can't be
  2197. ;; used. The buffer is narrowed to the comment while KEYWORDS is
  2198. ;; applied; leading comment starters are included but trailing
  2199. ;; comment enders for block comment are not.
  2200. ;;
  2201. ;; Note that faces added through KEYWORDS should never replace the
  2202. ;; existing `c-doc-face-name' face since the existence of that face
  2203. ;; is used as a flag in other code to skip comments.
  2204. ;;
  2205. ;; This function might do hidden buffer changes.
  2206. (let (comment-beg region-beg)
  2207. (if (eq (get-text-property (point) 'face)
  2208. 'font-lock-comment-face)
  2209. ;; Handle the case when the fontified region starts inside a
  2210. ;; comment.
  2211. (let ((range (c-literal-limits)))
  2212. (setq region-beg (point))
  2213. (when range
  2214. (goto-char (car range)))
  2215. (when (looking-at prefix)
  2216. (setq comment-beg (point)))))
  2217. (while (or
  2218. comment-beg
  2219. ;; Search for the prefix until a match is found at the start
  2220. ;; of a comment.
  2221. (while (when (re-search-forward prefix limit t)
  2222. (setq comment-beg (match-beginning 0))
  2223. (or (not (c-got-face-at comment-beg
  2224. c-literal-faces))
  2225. (and (/= comment-beg (point-min))
  2226. (c-got-face-at (1- comment-beg)
  2227. c-literal-faces))))
  2228. (setq comment-beg nil))
  2229. (setq region-beg comment-beg))
  2230. (if (eq (elt (parse-partial-sexp comment-beg (+ comment-beg 2)) 7) t)
  2231. ;; Collect a sequence of doc style line comments.
  2232. (progn
  2233. (goto-char comment-beg)
  2234. (while (and (progn
  2235. (c-forward-single-comment)
  2236. (skip-syntax-forward " ")
  2237. (< (point) limit))
  2238. (looking-at prefix))))
  2239. (goto-char comment-beg)
  2240. (c-forward-single-comment))
  2241. (if (> (point) limit) (goto-char limit))
  2242. (setq comment-beg nil)
  2243. (let ((region-end (point))
  2244. (keylist keywords) keyword matcher highlights)
  2245. (c-put-font-lock-face region-beg region-end c-doc-face-name)
  2246. (save-restriction
  2247. ;; Narrow to the doc comment. Among other things, this
  2248. ;; helps by making "^" match at the start of the comment.
  2249. ;; Do not include a trailing block comment ender, though.
  2250. (and (> region-end (1+ region-beg))
  2251. (progn (goto-char region-end)
  2252. (backward-char 2)
  2253. (looking-at "\\*/"))
  2254. (setq region-end (point)))
  2255. (narrow-to-region region-beg region-end)
  2256. (while keylist
  2257. (setq keyword (car keylist)
  2258. keylist (cdr keylist)
  2259. matcher (car keyword))
  2260. (goto-char region-beg)
  2261. (while (if (stringp matcher)
  2262. (re-search-forward matcher region-end t)
  2263. (funcall matcher region-end))
  2264. (setq highlights (cdr keyword))
  2265. (if (consp (car highlights))
  2266. (while highlights
  2267. (font-lock-apply-highlight (car highlights))
  2268. (setq highlights (cdr highlights)))
  2269. (font-lock-apply-highlight highlights))))
  2270. (goto-char region-end)))))
  2271. nil)
  2272. (put 'c-font-lock-doc-comments 'lisp-indent-function 2)
  2273. (defun c-find-invalid-doc-markup (regexp limit)
  2274. ;; Used to fontify invalid markup in doc comments after the correct
  2275. ;; ones have been fontified: Find the first occurrence of REGEXP
  2276. ;; between the point and LIMIT that only is fontified with
  2277. ;; `c-doc-face-name'. If a match is found then submatch 0 surrounds
  2278. ;; the first char and t is returned, otherwise nil is returned.
  2279. ;;
  2280. ;; This function might do hidden buffer changes.
  2281. (let (start)
  2282. (while (if (re-search-forward regexp limit t)
  2283. (not (eq (get-text-property
  2284. (setq start (match-beginning 0)) 'face)
  2285. c-doc-face-name))
  2286. (setq start nil)))
  2287. (when start
  2288. (store-match-data (list (copy-marker start)
  2289. (copy-marker (1+ start))))
  2290. t)))
  2291. ;; GtkDoc patterns contributed by Masatake YAMATO <jet@gyve.org>.
  2292. (defconst gtkdoc-font-lock-doc-comments
  2293. (let ((symbol "[a-zA-Z0-9_]+")
  2294. (header "^ \\* "))
  2295. `((,(concat header "\\(" symbol "\\):[ \t]*$")
  2296. 1 ,c-doc-markup-face-name prepend nil)
  2297. (,(concat symbol "()")
  2298. 0 ,c-doc-markup-face-name prepend nil)
  2299. (,(concat header "\\(" "@" symbol "\\):")
  2300. 1 ,c-doc-markup-face-name prepend nil)
  2301. (,(concat "[#%@]" symbol)
  2302. 0 ,c-doc-markup-face-name prepend nil))
  2303. ))
  2304. (defconst gtkdoc-font-lock-doc-protection
  2305. `(("< \\(public\\|private\\|protected\\) >"
  2306. 1 ,c-doc-markup-face-name prepend nil)))
  2307. (defconst gtkdoc-font-lock-keywords
  2308. `((,(lambda (limit)
  2309. (c-font-lock-doc-comments "/\\*\\*$" limit
  2310. gtkdoc-font-lock-doc-comments)
  2311. (c-font-lock-doc-comments "/\\*< " limit
  2312. gtkdoc-font-lock-doc-protection)
  2313. ))))
  2314. ;; Javadoc.
  2315. (defconst javadoc-font-lock-doc-comments
  2316. `(("{@[a-z]+[^}\n\r]*}" ; "{@foo ...}" markup.
  2317. 0 ,c-doc-markup-face-name prepend nil)
  2318. ("^\\(/\\*\\)?\\(\\s \\|\\*\\)*\\(@[a-z]+\\)" ; "@foo ..." markup.
  2319. 3 ,c-doc-markup-face-name prepend nil)
  2320. (,(concat "</?\\sw" ; HTML tags.
  2321. "\\("
  2322. (concat "\\sw\\|\\s \\|[=\n\r*.:]\\|"
  2323. "\"[^\"]*\"\\|'[^']*'")
  2324. "\\)*>")
  2325. 0 ,c-doc-markup-face-name prepend nil)
  2326. ("&\\(\\sw\\|[.:]\\)+;" ; HTML entities.
  2327. 0 ,c-doc-markup-face-name prepend nil)
  2328. ;; Fontify remaining markup characters as invalid. Note
  2329. ;; that the Javadoc spec is hazy about when "@" is
  2330. ;; allowed in non-markup use.
  2331. (,(lambda (limit)
  2332. (c-find-invalid-doc-markup "[<>&]\\|{@" limit))
  2333. 0 'font-lock-warning-face prepend nil)))
  2334. (defconst javadoc-font-lock-keywords
  2335. `((,(lambda (limit)
  2336. (c-font-lock-doc-comments "/\\*\\*" limit
  2337. javadoc-font-lock-doc-comments)))))
  2338. ;; Pike autodoc.
  2339. (defconst autodoc-decl-keywords
  2340. ;; Adorned regexp matching the keywords that introduce declarations
  2341. ;; in Pike Autodoc.
  2342. (cc-eval-when-compile
  2343. (c-make-keywords-re t '("@decl" "@elem" "@index" "@member") 'pike-mode)))
  2344. (defconst autodoc-decl-type-keywords
  2345. ;; Adorned regexp matching the keywords that are followed by a type.
  2346. (cc-eval-when-compile
  2347. (c-make-keywords-re t '("@elem" "@member") 'pike-mode)))
  2348. (defun autodoc-font-lock-line-markup (limit)
  2349. ;; Fontify all line oriented keywords between the point and LIMIT.
  2350. ;; Nil is always returned.
  2351. ;;
  2352. ;; This function might do hidden buffer changes.
  2353. (let ((line-re (concat "^\\(\\(/\\*!\\|\\s *\\("
  2354. c-current-comment-prefix
  2355. "\\)\\)\\s *\\)@[A-Za-z_-]+\\(\\s \\|$\\)"))
  2356. (markup-faces (list c-doc-markup-face-name c-doc-face-name)))
  2357. (while (re-search-forward line-re limit t)
  2358. (goto-char (match-end 1))
  2359. (if (looking-at autodoc-decl-keywords)
  2360. (let* ((kwd-pos (point))
  2361. (start (match-end 1))
  2362. (pos start)
  2363. end)
  2364. (c-put-font-lock-face (point) pos markup-faces)
  2365. ;; Put a declaration end mark at the markup keyword and
  2366. ;; remove the faces from the rest of the line so that it
  2367. ;; gets refontified as a declaration later on by
  2368. ;; `c-font-lock-declarations'.
  2369. (c-put-char-property (1- pos) 'c-type 'c-decl-end)
  2370. (goto-char pos)
  2371. (while (progn
  2372. (end-of-line)
  2373. (setq end (point))
  2374. (and (eq (char-before) ?@)
  2375. (not (eobp))
  2376. (progn (forward-char)
  2377. (skip-syntax-forward " ")
  2378. (looking-at c-current-comment-prefix))))
  2379. (goto-char (match-end 0))
  2380. (c-remove-font-lock-face pos (1- end))
  2381. (c-put-font-lock-face (1- end) end markup-faces)
  2382. (setq pos (point)))
  2383. ;; Include the final newline in the removed area. This
  2384. ;; has no visual effect but it avoids some tricky special
  2385. ;; cases in the testsuite wrt the differences in string
  2386. ;; fontification in Emacs vs XEmacs.
  2387. (c-remove-font-lock-face pos (min (1+ (point)) (point-max)))
  2388. ;; Must handle string literals explicitly inside the declaration.
  2389. (goto-char start)
  2390. (while (re-search-forward
  2391. "\"\\([^\\\"]\\|\\\\.\\)*\"\\|'\\([^\\']\\|\\\\.\\)*'"
  2392. end 'move)
  2393. (c-put-font-lock-string-face (match-beginning 0)
  2394. (point)))
  2395. ;; Fontify types after keywords that always are followed
  2396. ;; by them.
  2397. (goto-char kwd-pos)
  2398. (when (looking-at autodoc-decl-type-keywords)
  2399. (c-fontify-types-and-refs ((c-promote-possible-types t))
  2400. (goto-char start)
  2401. (c-forward-syntactic-ws)
  2402. (c-forward-type))))
  2403. ;; Mark each whole line as markup, as long as the logical line
  2404. ;; continues.
  2405. (while (progn
  2406. (c-put-font-lock-face (point)
  2407. (progn (end-of-line) (point))
  2408. markup-faces)
  2409. (and (eq (char-before) ?@)
  2410. (not (eobp))
  2411. (progn (forward-char)
  2412. (skip-syntax-forward " ")
  2413. (looking-at c-current-comment-prefix))))
  2414. (goto-char (match-end 0))))))
  2415. nil)
  2416. (defconst autodoc-font-lock-doc-comments
  2417. `(("@\\(\\w+{\\|\\[\\([^\]@\n\r]\\|@@\\)*\\]\\|[@}]\\|$\\)"
  2418. ;; In-text markup.
  2419. 0 ,c-doc-markup-face-name prepend nil)
  2420. (autodoc-font-lock-line-markup)
  2421. ;; Fontify remaining markup characters as invalid.
  2422. (,(lambda (limit)
  2423. (c-find-invalid-doc-markup "@" limit))
  2424. 0 'font-lock-warning-face prepend nil)
  2425. ))
  2426. (defun autodoc-font-lock-keywords ()
  2427. ;; Note that we depend on that `c-current-comment-prefix' has got
  2428. ;; its proper value here.
  2429. ;;
  2430. ;; This function might do hidden buffer changes.
  2431. ;; The `c-type' text property with `c-decl-end' is used to mark the
  2432. ;; end of the `autodoc-decl-keywords' occurrences to fontify the
  2433. ;; following declarations.
  2434. (setq c-type-decl-end-used t)
  2435. `((,(lambda (limit)
  2436. (c-font-lock-doc-comments "/[*/]!" limit
  2437. autodoc-font-lock-doc-comments)))))
  2438. ;; 2006-07-10: awk-font-lock-keywords has been moved back to cc-awk.el.
  2439. (cc-provide 'cc-fonts)
  2440. ;;; cc-fonts.el ends here