aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2007-04-06 21:21:55 +0000
committerAlan Mackenzie2007-04-06 21:21:55 +0000
commit1379f2c583e480c3812516c37c735bde5f189a2a (patch)
tree8865aac90ee9189b2fdfc0f4ea8ad3cd6b8b6725
parent1bf1feb50b8304f4696cc07a27a29c2bac77043a (diff)
downloademacs-1379f2c583e480c3812516c37c735bde5f189a2a.tar.gz
emacs-1379f2c583e480c3812516c37c735bde5f189a2a.zip
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.
-rw-r--r--lisp/progmodes/cc-engine.el129
-rw-r--r--lisp/progmodes/cc-fonts.el92
2 files changed, 135 insertions, 86 deletions
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index a9641b8eda0..717016af7ea 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -5371,7 +5371,7 @@ comment at the start of cc-engine.el for more info."
5371 ;; True if there's a prefix match outside the outermost 5371 ;; True if there's a prefix match outside the outermost
5372 ;; paren pair that surrounds the declarator. 5372 ;; paren pair that surrounds the declarator.
5373 got-prefix-before-parens 5373 got-prefix-before-parens
5374y ;; True if there's a suffix match outside the outermost 5374 ;; True if there's a suffix match outside the outermost
5375 ;; paren pair that surrounds the declarator. The value is 5375 ;; paren pair that surrounds the declarator. The value is
5376 ;; the position of the first suffix match. 5376 ;; the position of the first suffix match.
5377 got-suffix-after-parens 5377 got-suffix-after-parens
@@ -5877,19 +5877,23 @@ y ;; True if there's a suffix match outside the outermost
5877 5877
5878(defun c-forward-label (&optional assume-markup preceding-token-end limit) 5878(defun c-forward-label (&optional assume-markup preceding-token-end limit)
5879 ;; Assuming that point is at the beginning of a token, check if it starts a 5879 ;; Assuming that point is at the beginning of a token, check if it starts a
5880 ;; label and if so move over it and return t, otherwise don't move and 5880 ;; label and if so move over it and return non-nil (t in default situations,
5881 ;; return nil. "Label" here means "most things with a colon". 5881 ;; specific symbols (see below) for interesting situations), otherwise don't
5882 ;; move and return nil. "Label" here means "most things with a colon".
5882 ;; 5883 ;;
5883 ;; More precisely, a "label" is regarded as one of: 5884 ;; More precisely, a "label" is regarded as one of:
5884 ;; (i) a goto target like "foo:"; 5885 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
5885 ;; (ii) A case label - either the entire construct "case FOO:" or just the 5886 ;; (ii) A case label - either the entire construct "case FOO:", or just the
5886 ;; bare "case", should the colon be missing; 5887 ;; bare "case", should the colon be missing. We return t;
5887 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; 5888 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
5889 ;; return t;
5888 ;; (iv) One of QT's "extended" C++ variants of 5890 ;; (iv) One of QT's "extended" C++ variants of
5889 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:". 5891 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
5892 ;; Returns the symbol `qt-2kwds-colon'.
5893 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
5890 ;; (v) One of the keywords matched by `c-opt-extra-label-key' (without any 5894 ;; (v) One of the keywords matched by `c-opt-extra-label-key' (without any
5891 ;; colon). Currently (2006-03), this applies only to Objective C's 5895 ;; colon). Currently (2006-03), this applies only to Objective C's
5892 ;; keywords "@private", "@protected", and "@public". 5896 ;; keywords "@private", "@protected", and "@public". Returns t.
5893 ;; 5897 ;;
5894 ;; One of the things which will NOT be recognised as a label is a bit-field 5898 ;; One of the things which will NOT be recognised as a label is a bit-field
5895 ;; element of a struct, something like "int foo:5". 5899 ;; element of a struct, something like "int foo:5".
@@ -5918,8 +5922,10 @@ y ;; True if there's a suffix match outside the outermost
5918 ;; This function might do hidden buffer changes. 5922 ;; This function might do hidden buffer changes.
5919 5923
5920 (let ((start (point)) 5924 (let ((start (point))
5925 label-end
5921 qt-symbol-idx 5926 qt-symbol-idx
5922 macro-start) ; if we're in one. 5927 macro-start ; if we're in one.
5928 label-type)
5923 (cond 5929 (cond
5924 ;; "case" or "default" (Doesn't apply to AWK). 5930 ;; "case" or "default" (Doesn't apply to AWK).
5925 ((looking-at c-label-kwds-regexp) 5931 ((looking-at c-label-kwds-regexp)
@@ -5932,25 +5938,26 @@ y ;; True if there's a suffix match outside the outermost
5932 5938
5933 ;; Find the label end. 5939 ;; Find the label end.
5934 (goto-char kwd-end) 5940 (goto-char kwd-end)
5935 (if (and (c-syntactic-re-search-forward 5941 (setq label-type
5936 ;; Stop on chars that aren't allowed in expressions, 5942 (if (and (c-syntactic-re-search-forward
5937 ;; and on operator chars that would be meaningless 5943 ;; Stop on chars that aren't allowed in expressions,
5938 ;; there. FIXME: This doesn't cope with ?: operators. 5944 ;; and on operator chars that would be meaningless
5939 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)" 5945 ;; there. FIXME: This doesn't cope with ?: operators.
5940 limit t t nil 1) 5946 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
5941 (match-beginning 2)) 5947 limit t t nil 1)
5942 5948 (match-beginning 2))
5943 (progn 5949
5944 (goto-char (match-beginning 2)) ; just after the : 5950 (progn ; there's a proper :
5945 (c-put-c-type-property (1- (point)) 'c-decl-end) 5951 (goto-char (match-beginning 2)) ; just after the :
5946 t) 5952 (c-put-c-type-property (1- (point)) 'c-decl-end)
5947 5953 t)
5948 ;; It's an unfinished label. We consider the keyword enough 5954
5949 ;; to recognize it as a label, so that it gets fontified. 5955 ;; It's an unfinished label. We consider the keyword enough
5950 ;; Leave the point at the end of it, but don't put any 5956 ;; to recognize it as a label, so that it gets fontified.
5951 ;; `c-decl-end' marker. 5957 ;; Leave the point at the end of it, but don't put any
5952 (goto-char kwd-end) 5958 ;; `c-decl-end' marker.
5953 t))) 5959 (goto-char kwd-end)
5960 t))))
5954 5961
5955 ;; @private, @protected, @public, in Objective C, or similar. 5962 ;; @private, @protected, @public, in Objective C, or similar.
5956 ((and c-opt-extra-label-key 5963 ((and c-opt-extra-label-key
@@ -5962,7 +5969,7 @@ y ;; True if there's a suffix match outside the outermost
5962 (when c-record-type-identifiers 5969 (when c-record-type-identifiers
5963 (c-record-ref-id (cons (match-beginning 1) (point)))) 5970 (c-record-ref-id (cons (match-beginning 1) (point))))
5964 (c-put-c-type-property (1- (point)) 'c-decl-end) 5971 (c-put-c-type-property (1- (point)) 'c-decl-end)
5965 t) 5972 (setq label-type t))
5966 5973
5967 ;; All other cases of labels. 5974 ;; All other cases of labels.
5968 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t. 5975 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
@@ -6038,26 +6045,49 @@ y ;; True if there's a suffix match outside the outermost
6038 (c-forward-syntactic-ws) 6045 (c-forward-syntactic-ws)
6039 (c-forward-label nil pte start)))))))))) 6046 (c-forward-label nil pte start))))))))))
6040 6047
6048 ;; Point is still at the beginning of the possible label construct.
6049 ;;
6041 ;; Check that the next nonsymbol token is ":", or that we're in one 6050 ;; Check that the next nonsymbol token is ":", or that we're in one
6042 ;; of QT's "slots" declarations. Allow '(' for the sake of macro 6051 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
6043 ;; arguments. FIXME: Should build this regexp from the language 6052 ;; arguments. FIXME: Should build this regexp from the language
6044 ;; constants. 6053 ;; constants.
6045 (when (c-syntactic-re-search-forward 6054 (cond
6046 "[ \t[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB 6055 ;; public: protected: private:
6047 (backward-char) 6056 ((and
6048 (setq qt-symbol-idx 6057 (c-major-mode-is 'c++-mode)
6049 (and (c-major-mode-is 'c++-mode) 6058 (search-forward-regexp
6050 (string-match 6059 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
6051 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>" 6060 (progn (backward-char)
6052 (buffer-substring start (point))))) 6061 (c-forward-syntactic-ws limit)
6053 (c-forward-syntactic-ws limit) 6062 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
6054 (when (or (looking-at ":\\([^:]\\|\\'\\)") ; A single colon. 6063 (forward-char)
6055 (and qt-symbol-idx 6064 (setq label-type t))
6056 (search-forward-regexp "\\=slots\\>" limit t) 6065 ;; QT double keyword like "protected slots:" or goto target.
6057 (progn (c-forward-syntactic-ws limit) 6066 ((progn (goto-char start) nil))
6058 (looking-at ":\\([^:]\\|\\'\\)")))) ; A single colon 6067 ((when (c-syntactic-re-search-forward
6059 (forward-char) ; to after the colon. 6068 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
6060 t))) 6069 (backward-char)
6070 (setq label-end (point))
6071 (setq qt-symbol-idx
6072 (and (c-major-mode-is 'c++-mode)
6073 (string-match
6074 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
6075 (buffer-substring start (point)))))
6076 (c-forward-syntactic-ws limit)
6077 (cond
6078 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
6079 (forward-char)
6080 (setq label-type
6081 (if (string= "signals" ; Special QT macro
6082 (buffer-substring-no-properties start label-end))
6083 'qt-1kwd-colon
6084 'goto-target)))
6085 ((and qt-symbol-idx
6086 (search-forward-regexp "\\=slots\\>" limit t)
6087 (progn (c-forward-syntactic-ws limit)
6088 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
6089 (forward-char)
6090 (setq label-type 'qt-2kwds-colon)))))))
6061 6091
6062 (save-restriction 6092 (save-restriction
6063 (narrow-to-region start (point)) 6093 (narrow-to-region start (point))
@@ -6068,6 +6098,7 @@ y ;; True if there's a suffix match outside the outermost
6068 (while (progn 6098 (while (progn
6069 (when (looking-at c-nonlabel-token-key) 6099 (when (looking-at c-nonlabel-token-key)
6070 (goto-char start) 6100 (goto-char start)
6101 (setq label-type nil)
6071 (throw 'check-label nil)) 6102 (throw 'check-label nil))
6072 (and (c-safe (c-forward-sexp) 6103 (and (c-safe (c-forward-sexp)
6073 (c-forward-syntactic-ws) 6104 (c-forward-syntactic-ws)
@@ -6087,12 +6118,12 @@ y ;; True if there's a suffix match outside the outermost
6087 6118
6088 (c-put-c-type-property (1- (point-max)) 'c-decl-end) 6119 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
6089 (goto-char (point-max)) 6120 (goto-char (point-max))
6090 t))) 6121 )))
6091 6122
6092 (t 6123 (t
6093 ;; Not a label. 6124 ;; Not a label.
6094 (goto-char start) 6125 (goto-char start)))
6095 nil)))) 6126 label-type))
6096 6127
6097(defun c-forward-objc-directive () 6128(defun c-forward-objc-directive ()
6098 ;; Assuming the point is at the beginning of a token, try to move 6129 ;; Assuming the point is at the beginning of a token, try to move
diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el
index a880ae9a1fe..0df09eda481 100644
--- a/lisp/progmodes/cc-fonts.el
+++ b/lisp/progmodes/cc-fonts.el
@@ -704,8 +704,13 @@ casts and declarations are fontified. Used on level 2 and higher."
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
@@ -742,10 +747,15 @@ casts and declarations are fontified. Used on level 2 and higher."
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
@@ -971,9 +981,14 @@ casts and declarations are fontified. Used on level 2 and higher."
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
@@ -1009,6 +1024,7 @@ casts and declarations are fontified. Used on level 2 and higher."
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
@@ -1174,39 +1190,31 @@ casts and declarations are fontified. Used on level 2 and higher."
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
@@ -1285,6 +1293,14 @@ on level 2 only and so aren't combined with `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
12863 and higher." 12943 and higher."
1287 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).
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
@@ -1397,6 +1413,8 @@ on level 2 only and so aren't combined with `c-complex-decl-matchers'."
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 ))