Skip to content

Commit d9f62fc

Browse files
author
Alan Mackenzie
committed
Fix fontification of first item in CC Mode macro without parentheses
* lisp/progmodes/cc-engine.el (c-find-decl-prefix-search): Handle the new matching possibility (of a #define construct) in the new c-decl-prefix-or-start-re. (c-find-decl-spots): Allow the initial search for an in-macro starting point settle on the # of #define, to facilitate the regexp matching in c-find-decl-prefix-search. * lisp/progmodes/cc-langs.el (c-anchored-hash-define-no-parens): New lang const. (c-literal-start-regexp): Correct what was always supposed to be a "generic string" regexp element. (c-decl-prefix-or-start-re): Enhance also to match "#define <identifier>". (c-dposr-cpp-macro-depth): New lang variable and lang constant.
1 parent 17a7229 commit d9f62fc

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

lisp/progmodes/cc-engine.el

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,7 +5692,10 @@ comment at the start of cc-engine.el for more info."
56925692
(setq cfd-re-match cfd-limit)
56935693
nil)
56945694
((c-got-face-at
5695-
(if (setq cfd-re-match (match-end 1))
5695+
(if (setq cfd-re-match
5696+
(or (match-end 1)
5697+
(and c-dposr-cpp-macro-depth
5698+
(match-end (1+ c-dposr-cpp-macro-depth)))))
56965699
;; Matched the end of a token preceding a decl spot.
56975700
(progn
56985701
(goto-char cfd-re-match)
@@ -5703,15 +5706,19 @@ comment at the start of cc-engine.el for more info."
57035706
c-literal-faces)
57045707
;; Pseudo match inside a comment or string literal. Skip out
57055708
;; of comments and string literals.
5706-
(while (progn
5707-
(unless
5708-
(and (match-end 1)
5709-
(c-got-face-at (1- (point)) c-literal-faces)
5710-
(not (c-got-face-at (point) c-literal-faces)))
5711-
(goto-char (c-next-single-property-change
5712-
(point) 'face nil cfd-limit)))
5713-
(and (< (point) cfd-limit)
5714-
(c-got-face-at (point) c-literal-faces))))
5709+
(while
5710+
(progn
5711+
(unless
5712+
(and
5713+
(or (match-end 1)
5714+
(and c-dposr-cpp-macro-depth
5715+
(match-end (1+ c-dposr-cpp-macro-depth))))
5716+
(c-got-face-at (1- (point)) c-literal-faces)
5717+
(not (c-got-face-at (point) c-literal-faces)))
5718+
(goto-char (c-next-single-property-change
5719+
(point) 'face nil cfd-limit)))
5720+
(and (< (point) cfd-limit)
5721+
(c-got-face-at (point) c-literal-faces))))
57155722
t) ; Continue the loop over pseudo matches.
57165723
((and c-opt-identifier-concat-key
57175724
(match-string 1)
@@ -5863,7 +5870,7 @@ comment at the start of cc-engine.el for more info."
58635870
;; before the point, and do the first `c-decl-prefix-or-start-re'
58645871
;; search unless we're at bob.
58655872

5866-
(let (start-in-literal start-in-macro syntactic-pos)
5873+
(let (start-in-literal start-in-macro syntactic-pos hash-define-pos)
58675874
;; Must back up a bit since we look for the end of the previous
58685875
;; statement or declaration, which is earlier than the first
58695876
;; returned match.
@@ -6018,7 +6025,21 @@ comment at the start of cc-engine.el for more info."
60186025
;; The only syntactic ws in macros are comments.
60196026
(c-backward-comments)
60206027
(or (bobp) (backward-char))
6021-
(c-beginning-of-current-token))
6028+
(c-beginning-of-current-token)
6029+
;; If we're in a macro without argument parentheses, we could have
6030+
;; now ended up at the macro's identifier. We need to be at #define
6031+
;; for `c-find-decl-prefix-search' to find the first token of the
6032+
;; macro's expansion.
6033+
(when (and (c-on-identifier)
6034+
(setq hash-define-pos
6035+
(save-excursion
6036+
(and
6037+
(zerop (c-backward-token-2 2)) ; over define, #
6038+
(save-excursion
6039+
(beginning-of-line)
6040+
(looking-at c-opt-cpp-macro-define-id))
6041+
(point)))))
6042+
(goto-char hash-define-pos)))
60226043

60236044
(start-in-literal
60246045
;; If we're in a comment it can only be the closest

lisp/progmodes/cc-langs.el

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,14 @@ definition, or nil if the language doesn't have any."
979979
(c-lang-defvar c-opt-cpp-macro-define-id
980980
(c-lang-const c-opt-cpp-macro-define-id))
981981

982+
(c-lang-defconst c-anchored-hash-define-no-parens
983+
;; Regexp matching everything up to the end of a cpp define which has no
984+
;; argument parentheses. Or nil in languages which don't have them.
985+
t (if (c-lang-const c-opt-cpp-macro-define)
986+
(concat (c-lang-const c-anchored-cpp-prefix)
987+
(c-lang-const c-opt-cpp-macro-define)
988+
"[ \t]+\\(\\sw\\|_\\)+\\([^(a-zA-Z0-9_]\\|$\\)")))
989+
982990
(c-lang-defconst c-cpp-expr-directives
983991
"List of cpp directives (without the prefix) that are followed by an
984992
expression."
@@ -1614,7 +1622,7 @@ starter."
16141622
t (concat (c-lang-const c-comment-start-regexp)
16151623
"\\|"
16161624
(if (memq 'gen-string-delim c-emacs-features)
1617-
"\"|"
1625+
"\"\\|\\s|"
16181626
"\"")))
16191627
(c-lang-defvar c-literal-start-regexp (c-lang-const c-literal-start-regexp))
16201628

@@ -3183,24 +3191,40 @@ constructs."
31833191
;; token that might precede such a construct, e.g. ';', '}' or '{'.
31843192
;; It's built from `c-decl-prefix-re'.
31853193
;;
3186-
;; If the first submatch did not match, the match of the whole
3187-
;; regexp is taken to be at the first token in the declaration.
3188-
;; `c-decl-start-re' is not checked in this case.
3194+
;; If the first submatch did not match, we have either a #define construct
3195+
;; without parentheses or the match of the whole regexp is taken to be at
3196+
;; the first token in the declaration. `c-decl-start-re' is not checked in
3197+
;; these cases.
31893198
;;
31903199
;; Design note: The reason the same regexp is used to match both
31913200
;; tokens that precede declarations and start them is to avoid an
31923201
;; extra regexp search from the previous declaration spot in
31933202
;; `c-find-decl-spots'. Users of `c-find-decl-spots' also count on
3194-
;; that it finds all declaration/cast/label starts in approximately
3203+
;; it finding all declaration/cast/label starts in approximately
31953204
;; linear order, so we can't do the searches in two separate passes.
3196-
t (if (c-lang-const c-decl-start-kwds)
3197-
(concat (c-lang-const c-decl-prefix-re)
3198-
"\\|"
3199-
(c-make-keywords-re t (c-lang-const c-decl-start-kwds)))
3200-
(c-lang-const c-decl-prefix-re)))
3205+
t (cond
3206+
((and (c-lang-const c-decl-start-kwds)
3207+
(c-lang-const c-anchored-hash-define-no-parens))
3208+
(concat (c-lang-const c-decl-prefix-re)
3209+
"\\|" (c-lang-const c-anchored-hash-define-no-parens)
3210+
"\\|" (c-make-keywords-re t (c-lang-const c-decl-start-kwds))))
3211+
((c-lang-const c-decl-start-kwds)
3212+
(concat (c-lang-const c-decl-prefix-re)
3213+
"\\|" (c-make-keywords-re t (c-lang-const c-decl-start-kwds))))
3214+
((c-lang-const c-anchored-hash-define-no-parens)
3215+
(concat (c-lang-const c-decl-prefix-re)
3216+
"\\|" (c-lang-const c-anchored-hash-define-no-parens)))
3217+
(t (c-lang-const c-decl-prefix-re))))
32013218
(c-lang-defvar c-decl-prefix-or-start-re
32023219
(c-lang-const c-decl-prefix-or-start-re))
32033220

3221+
(c-lang-defconst c-dposr-cpp-macro-depth
3222+
;; The match number of `c-anchored-hash-define-no-parens''s first match
3223+
;; within `c-decl-prefix-or-start-re', or nil if there is no such component.
3224+
t (if (c-lang-const c-anchored-hash-define-no-parens)
3225+
(1+ (regexp-opt-depth (c-lang-const c-decl-prefix-re)))))
3226+
(c-lang-defvar c-dposr-cpp-macro-depth (c-lang-const c-dposr-cpp-macro-depth))
3227+
32043228
(c-lang-defconst c-cast-parens
32053229
;; List containing the paren characters that can open a cast, or nil in
32063230
;; languages without casts.

0 commit comments

Comments
 (0)