comparison lisp/progmodes/cc-fonts.el @ 76986:469570420bff

Fix fontification of labels, and other things with ":". * progmodes/cc-engine.el (c-forward-label): The function now returns 'goto-target, 'qt-2kwds-colon, 'qt-1kwd-colon, as well as the former t. * progmodes/cc-fonts.el (c-font-lock-declarations): Interpret the new return code from c-forward-label, fontifying tokens properly. Add some general comments throughout the file.
author Alan Mackenzie <acm@muc.de>
date Fri, 06 Apr 2007 21:21:55 +0000
parents e3694f1cb928
children c1ec1c8a8d2e 4ef881a120fe
comparison
equal deleted inserted replaced
76985:2f0b1b082f4a 76986:469570420bff
702 702
703 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name)) 703 (eval . (list "\\(!\\)[^=]" 1 c-negation-char-face-name))
704 )) 704 ))
705 705
706 (defun c-font-lock-complex-decl-prepare (limit) 706 (defun c-font-lock-complex-decl-prepare (limit)
707 ;; This function will be called from font-lock for a region bounded by POINT
708 ;; and LIMIT, as though it were to identify a keyword for
709 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
710 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
711 ;; Fontification".
712 ;;
707 ;; Called before any of the matchers in `c-complex-decl-matchers'. 713 ;; Called before any of the matchers in `c-complex-decl-matchers'.
708 ;; Nil is always returned.
709 ;; 714 ;;
710 ;; This function does hidden buffer changes. 715 ;; This function does hidden buffer changes.
711 716
712 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit) 717 ;;(message "c-font-lock-complex-decl-prepare %s %s" (point) limit)
713 718
740 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start)))) 745 (c-font-lock-declarators limit t (eq prop 'c-decl-type-start))))
741 746
742 nil) 747 nil)
743 748
744 (defun c-font-lock-<>-arglists (limit) 749 (defun c-font-lock-<>-arglists (limit)
750 ;; This function will be called from font-lock for a region bounded by POINT
751 ;; and LIMIT, as though it were to identify a keyword for
752 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
753 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
754 ;; Fontification".
755 ;;
745 ;; Fontify types and references in names containing angle bracket 756 ;; Fontify types and references in names containing angle bracket
746 ;; arglists from the point to LIMIT. Note that 757 ;; arglists from the point to LIMIT. Note that
747 ;; `c-font-lock-declarations' already has handled many of them. Nil 758 ;; `c-font-lock-declarations' already has handled many of them.
748 ;; is always returned.
749 ;; 759 ;;
750 ;; This function might do hidden buffer changes. 760 ;; This function might do hidden buffer changes.
751 761
752 (let (;; The font-lock package in Emacs is known to clobber 762 (let (;; The font-lock package in Emacs is known to clobber
753 ;; `parse-sexp-lookup-properties' (when it exists). 763 ;; `parse-sexp-lookup-properties' (when it exists).
969 font-lock-type-face 979 font-lock-type-face
970 c-reference-face-name 980 c-reference-face-name
971 font-lock-keyword-face)) 981 font-lock-keyword-face))
972 982
973 (defun c-font-lock-declarations (limit) 983 (defun c-font-lock-declarations (limit)
984 ;; This function will be called from font-lock for a region bounded by POINT
985 ;; and LIMIT, as though it were to identify a keyword for
986 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
987 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
988 ;; Fontification".
989 ;;
974 ;; Fontify all the declarations, casts and labels from the point to LIMIT. 990 ;; Fontify all the declarations, casts and labels from the point to LIMIT.
975 ;; Assumes that strings and comments have been fontified already. Nil is 991 ;; Assumes that strings and comments have been fontified already.
976 ;; always returned.
977 ;; 992 ;;
978 ;; This function might do hidden buffer changes. 993 ;; This function might do hidden buffer changes.
979 994
980 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit) 995 ;;(message "c-font-lock-declarations search from %s to %s" (point) limit)
981 996
1007 c-restricted-<>-arglists 1022 c-restricted-<>-arglists
1008 ;; Turn on recording of identifier ranges in 1023 ;; Turn on recording of identifier ranges in
1009 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for 1024 ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
1010 ;; later fontification. 1025 ;; later fontification.
1011 (c-record-type-identifiers t) 1026 (c-record-type-identifiers t)
1027 label-type
1012 c-record-ref-identifiers 1028 c-record-ref-identifiers
1013 ;; Make `c-forward-type' calls mark up template arglists if 1029 ;; Make `c-forward-type' calls mark up template arglists if
1014 ;; it finds any. That's necessary so that we later will 1030 ;; it finds any. That's necessary so that we later will
1015 ;; stop inside them to fontify types there. 1031 ;; stop inside them to fontify types there.
1016 (c-parse-and-markup-<>-arglists t) 1032 (c-parse-and-markup-<>-arglists t)
1172 ;; A cast or declaration has been successfully identified, so do 1188 ;; A cast or declaration has been successfully identified, so do
1173 ;; all the fontification of types and refs that's been recorded. 1189 ;; all the fontification of types and refs that's been recorded.
1174 (c-fontify-recorded-types-and-refs) 1190 (c-fontify-recorded-types-and-refs)
1175 nil)) 1191 nil))
1176 1192
1177 ;; It was a false alarm. 1193 ;; It was a false alarm. Check if we're in a label (or other
1194 ;; construct with `:' except bitfield) instead.
1178 (goto-char start-pos) 1195 (goto-char start-pos)
1179 ;; The below code attempts to fontify the case constants in 1196 (when (setq label-type (c-forward-label t match-pos nil))
1180 ;; c-label-face-name, but it cannot catch every case [sic]. 1197 ;; Can't use `c-fontify-types-and-refs' here since we
1181 ;; And do we want to fontify case constants anyway? 1198 ;; use the label face at times.
1182 (c-forward-label t match-pos nil) 1199 (cond ((eq label-type 'goto-target)
1183 ;;; (when (c-forward-label t match-pos nil) 1200 (c-put-font-lock-face (caar c-record-ref-identifiers)
1184 ;;; ;; Can't use `c-fontify-types-and-refs' here since we 1201 (cdar c-record-ref-identifiers)
1185 ;;; ;; should use the label face. 1202 c-label-face-name))
1186 ;;; (save-excursion 1203 ((eq label-type 'qt-1kwd-colon)
1187 ;;; (while c-record-ref-identifiers 1204 (c-put-font-lock-face (caar c-record-ref-identifiers)
1188 ;;; (let ((elem (car c-record-ref-identifiers)) 1205 (cdar c-record-ref-identifiers)
1189 ;;; c-record-type-identifiers) 1206 'font-lock-keyword-face))
1190 ;;; (goto-char (cdr elem)) 1207 ((eq label-type 'qt-2kwds-colon)
1191 ;;; ;; Find the end of any label. 1208 (mapc
1192 ;;; (while (and (re-search-forward "\\sw\\|:" nil t) 1209 (lambda (kwd)
1193 ;;; (progn (backward-char 1) t) 1210 (c-put-font-lock-face (car kwd) (cdr kwd)
1194 ;;; (or (re-search-forward 1211 'font-lock-keyword-face))
1195 ;;; "\\=0[Xx][0-9A-Fa-f]+\\|\\([0-9]+\\)" nil t) 1212 c-record-ref-identifiers)))
1196 ;;; (c-forward-name))) 1213 (setq c-record-ref-identifiers nil)
1197 ;;; (c-backward-syntactic-ws) 1214 ;; `c-forward-label' has probably added a `c-decl-end'
1198 ;;; (let ((end (point))) 1215 ;; marker, so return t to `c-find-decl-spots' to signal
1199 ;;; ;; Now find the start of the bit we regard as the label. 1216 ;; that.
1200 ;;; (when (and (c-simple-skip-symbol-backward) 1217 t))))
1201 ;;; (not (c-get-char-property (point) 'face)))
1202 ;;; (c-put-font-lock-face (point) end c-label-face-name))
1203 ;;; (goto-char end))))
1204 ;;; (setq c-record-ref-identifiers (cdr c-record-ref-identifiers))))
1205 ;;; ;; `c-forward-label' probably has added a `c-decl-end'
1206 ;;; ;; marker, so return t to `c-find-decl-spots' to signal
1207 ;;; ;; that.
1208 ;;; t)
1209 )))
1210 1218
1211 nil))) 1219 nil)))
1212 1220
1213 (c-lang-defconst c-simple-decl-matchers 1221 (c-lang-defconst c-simple-decl-matchers
1214 "Simple font lock matchers for types and declarations. These are used 1222 "Simple font lock matchers for types and declarations. These are used
1282 `(c-font-lock-labels)))) 1290 `(c-font-lock-labels))))
1283 1291
1284 (c-lang-defconst c-complex-decl-matchers 1292 (c-lang-defconst c-complex-decl-matchers
1285 "Complex font lock matchers for types and declarations. Used on level 1293 "Complex font lock matchers for types and declarations. Used on level
1286 3 and higher." 1294 3 and higher."
1295
1296 ;; Note: This code in this form dumps a number of funtions into the
1297 ;; resulting constant, `c-matchers-3'. At run time, font lock will call
1298 ;; each of them as a "FUNCTION" (see Elisp page "Search-based
1299 ;; Fontification"). The font lock region is delimited by POINT and the
1300 ;; single parameter, LIMIT. Each of these functions returns NIL (thus
1301 ;; inhibiting spurious font-lock-keyword-face highlighting and another
1302 ;; call).
1287 1303
1288 t `(;; Initialize some things before the search functions below. 1304 t `(;; Initialize some things before the search functions below.
1289 c-font-lock-complex-decl-prepare 1305 c-font-lock-complex-decl-prepare
1290 1306
1291 ,@(if (c-major-mode-is 'objc-mode) 1307 ,@(if (c-major-mode-is 'objc-mode)
1395 c-single-line-syntactic-ws-depth))))) 1411 c-single-line-syntactic-ws-depth)))))
1396 (goto-char (match-end 0))))))) 1412 (goto-char (match-end 0)))))))
1397 1413
1398 ;; Fontify the type in C++ "new" expressions. 1414 ;; Fontify the type in C++ "new" expressions.
1399 ,@(when (c-major-mode-is 'c++-mode) 1415 ,@(when (c-major-mode-is 'c++-mode)
1416 ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
1417 ;; (see Elisp page "Search-based Fontification").
1400 `(("\\<new\\>" 1418 `(("\\<new\\>"
1401 (c-font-lock-c++-new)))) 1419 (c-font-lock-c++-new))))
1402 )) 1420 ))
1403 1421
1404 (defun c-font-lock-labels (limit) 1422 (defun c-font-lock-labels (limit)