diff 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
line wrap: on
line diff
--- a/lisp/progmodes/cc-fonts.el	Fri Apr 06 21:07:56 2007 +0000
+++ b/lisp/progmodes/cc-fonts.el	Fri Apr 06 21:21:55 2007 +0000
@@ -704,8 +704,13 @@
       ))
 
 (defun c-font-lock-complex-decl-prepare (limit)
+  ;; This function will be called from font-lock for a region bounded by POINT
+  ;; and LIMIT, as though it were to identify a keyword for
+  ;; font-lock-keyword-face.  It always returns NIL to inhibit this and
+  ;; prevent a repeat invocation.  See elisp/lispref page "Search-based
+  ;; Fontification".
+  ;;
   ;; Called before any of the matchers in `c-complex-decl-matchers'.
-  ;; Nil is always returned.
   ;;
   ;; This function does hidden buffer changes.
 
@@ -742,10 +747,15 @@
   nil)
 
 (defun c-font-lock-<>-arglists (limit)
+  ;; This function will be called from font-lock for a region bounded by POINT
+  ;; and LIMIT, as though it were to identify a keyword for
+  ;; font-lock-keyword-face.  It always returns NIL to inhibit this and
+  ;; prevent a repeat invocation.  See elisp/lispref page "Search-based
+  ;; Fontification".
+  ;;
   ;; Fontify types and references in names containing angle bracket
   ;; arglists from the point to LIMIT.  Note that
-  ;; `c-font-lock-declarations' already has handled many of them.  Nil
-  ;; is always returned.
+  ;; `c-font-lock-declarations' already has handled many of them.
   ;;
   ;; This function might do hidden buffer changes.
 
@@ -971,9 +981,14 @@
 	font-lock-keyword-face))
 
 (defun c-font-lock-declarations (limit)
+  ;; This function will be called from font-lock for a region bounded by POINT
+  ;; and LIMIT, as though it were to identify a keyword for
+  ;; font-lock-keyword-face.  It always returns NIL to inhibit this and
+  ;; prevent a repeat invocation.  See elisp/lispref page "Search-based
+  ;; Fontification".
+  ;;
   ;; Fontify all the declarations, casts and labels from the point to LIMIT.
-  ;; Assumes that strings and comments have been fontified already.  Nil is
-  ;; always returned.
+  ;; Assumes that strings and comments have been fontified already.
   ;;
   ;; This function might do hidden buffer changes.
 
@@ -1009,6 +1024,7 @@
 	  ;; `c-forward-decl-or-cast-1' and `c-forward-label' for
 	  ;; later fontification.
 	  (c-record-type-identifiers t)
+	  label-type
 	  c-record-ref-identifiers
 	  ;; Make `c-forward-type' calls mark up template arglists if
 	  ;; it finds any.  That's necessary so that we later will
@@ -1174,39 +1190,31 @@
 	      (c-fontify-recorded-types-and-refs)
 	      nil))
 
-	  ;; It was a false alarm.
+	  ;; It was a false alarm.  Check if we're in a label (or other
+	  ;; construct with `:' except bitfield) instead.
 	  (goto-char start-pos)
-	  ;; The below code attempts to fontify the case constants in
-	  ;; c-label-face-name, but it cannot catch every case [sic].
-	  ;; And do we want to fontify case constants anyway?
-	  (c-forward-label t match-pos nil)
-;;;	  (when (c-forward-label t match-pos nil)
-;;;	    ;; Can't use `c-fontify-types-and-refs' here since we
-;;;	    ;; should use the label face.
-;;;	    (save-excursion
-;;;	      (while c-record-ref-identifiers
-;;;		(let ((elem (car c-record-ref-identifiers))
-;;;		      c-record-type-identifiers)
-;;;		  (goto-char (cdr elem))
-;;;		  ;; Find the end of any label.
-;;;		  (while (and (re-search-forward "\\sw\\|:" nil t)
-;;;			      (progn (backward-char 1) t)
-;;;			      (or (re-search-forward
-;;;				   "\\=0[Xx][0-9A-Fa-f]+\\|\\([0-9]+\\)" nil t)
-;;;				  (c-forward-name)))
-;;;		    (c-backward-syntactic-ws)
-;;;		    (let ((end (point)))
-;;;		      ;; Now find the start of the bit we regard as the label.
-;;;		      (when (and (c-simple-skip-symbol-backward)
-;;;				 (not (c-get-char-property (point) 'face)))
-;;;			(c-put-font-lock-face (point) end c-label-face-name))
-;;;		      (goto-char end))))
-;;;		(setq c-record-ref-identifiers (cdr c-record-ref-identifiers))))
-;;;	    ;; `c-forward-label' probably has added a `c-decl-end'
-;;;	    ;; marker, so return t to `c-find-decl-spots' to signal
-;;;	    ;; that.
-;;;	    t)
-	  )))
+	  (when (setq label-type (c-forward-label t match-pos nil))
+	    ;; Can't use `c-fontify-types-and-refs' here since we
+	    ;; use the label face at times.
+	    (cond ((eq label-type 'goto-target)
+		   (c-put-font-lock-face (caar c-record-ref-identifiers)
+					 (cdar c-record-ref-identifiers)
+					 c-label-face-name))
+		  ((eq label-type 'qt-1kwd-colon)
+		   (c-put-font-lock-face (caar c-record-ref-identifiers)
+					 (cdar c-record-ref-identifiers)
+					 'font-lock-keyword-face))
+		  ((eq label-type 'qt-2kwds-colon)
+		   (mapc
+		    (lambda (kwd)
+		      (c-put-font-lock-face (car kwd) (cdr kwd)
+					    'font-lock-keyword-face))
+		    c-record-ref-identifiers)))
+	    (setq c-record-ref-identifiers nil)
+	    ;; `c-forward-label' has probably added a `c-decl-end'
+	    ;; marker, so return t to `c-find-decl-spots' to signal
+	    ;; that.
+	    t))))
 
       nil)))
 
@@ -1285,6 +1293,14 @@
   "Complex font lock matchers for types and declarations.  Used on level
 3 and higher."
 
+  ;; Note: This code in this form dumps a number of funtions into the
+  ;; resulting constant, `c-matchers-3'.  At run time, font lock will call
+  ;; each of them as a "FUNCTION" (see Elisp page "Search-based
+  ;; Fontification").  The font lock region is delimited by POINT and the
+  ;; single parameter, LIMIT.  Each of these functions returns NIL (thus
+  ;; inhibiting spurious font-lock-keyword-face highlighting and another
+  ;; call).
+
   t `(;; Initialize some things before the search functions below.
       c-font-lock-complex-decl-prepare
 
@@ -1397,6 +1413,8 @@
 
       ;; Fontify the type in C++ "new" expressions.
       ,@(when (c-major-mode-is 'c++-mode)
+	  ;; This pattern is a probably a "(MATCHER . ANCHORED-HIGHLIGHTER)"
+	  ;; (see Elisp page "Search-based Fontification").
 	  `(("\\<new\\>"
 	     (c-font-lock-c++-new))))
       ))