aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2022-10-29 10:04:37 +0000
committerAlan Mackenzie2022-10-29 10:04:37 +0000
commitdfde4fc82b1b11f19edaf795ce62e8d5ff25f9ff (patch)
tree0c44005812ce12e36f159b36a074cde6ee8cd69a
parentce527464440ca070e752ca18a68ebf2d369a14df (diff)
downloademacs-dfde4fc82b1b11f19edaf795ce62e8d5ff25f9ff.tar.gz
emacs-dfde4fc82b1b11f19edaf795ce62e8d5ff25f9ff.zip
c-get-fontification-context: Refine the last non-default arm of the cond
This fixes bug #58772. * lisp/progmodes/cc-fonts.el (c-get-fontification-context): Make the function return (decl . nil) when MATCH-POS is in the second paren list of something like DEFUN (..) (..). Tidy up untidy code with save-excursion's for greater accuracy. Set a c-type text property value c-decl-arg-start to speed up future calls. Reindent the entire function.
-rw-r--r--lisp/progmodes/cc-fonts.el255
1 files changed, 133 insertions, 122 deletions
diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el
index 608919d0c90..9444828a0ed 100644
--- a/lisp/progmodes/cc-fonts.el
+++ b/lisp/progmodes/cc-fonts.el
@@ -1218,134 +1218,145 @@ casts and declarations are fontified. Used on level 2 and higher."
1218 (c-backward-syntactic-ws) 1218 (c-backward-syntactic-ws)
1219 (eq (char-before) ?\())))) 1219 (eq (char-before) ?\()))))
1220 (c-get-fontification-context (point) not-front-decl toplev)) 1220 (c-get-fontification-context (point) not-front-decl toplev))
1221 ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{))) 1221 ((not (memq (char-before match-pos) '(?\( ?, ?\[ ?< ?{)))
1222 (cons (and toplev 'top) nil)) 1222 (cons (and toplev 'top) nil))
1223 ;; A control flow expression or a decltype 1223 ;; A control flow expression or a decltype
1224 ((and (eq (char-before match-pos) ?\() 1224 ((and (eq (char-before match-pos) ?\()
1225 (save-excursion 1225 (save-excursion
1226 (goto-char match-pos) 1226 (goto-char match-pos)
1227 (backward-char) 1227 (backward-char)
1228 (c-backward-token-2) 1228 (c-backward-token-2)
1229 (cond 1229 (cond
1230 ((looking-at c-paren-stmt-key) 1230 ((looking-at c-paren-stmt-key)
1231 ;; Allow comma separated <> arglists in for statements. 1231 ;; Allow comma separated <> arglists in for statements.
1232 (cons nil nil)) 1232 (cons nil nil))
1233 ((or (looking-at c-block-stmt-2-key) 1233 ((or (looking-at c-block-stmt-2-key)
1234 (looking-at c-block-stmt-1-2-key) 1234 (looking-at c-block-stmt-1-2-key)
1235 (looking-at c-typeof-key)) 1235 (looking-at c-typeof-key))
1236 (cons nil t)) 1236 (cons nil t))
1237 (t nil))))) 1237 (t nil)))))
1238 ;; Near BOB. 1238 ;; Near BOB.
1239 ((<= match-pos (point-min)) 1239 ((<= match-pos (point-min))
1240 (cons 'arglist t)) 1240 (cons 'arglist t))
1241 ;; Got a cached hit in a declaration arglist. 1241 ;; Got a cached hit in a declaration arglist.
1242 ((eq type 'c-decl-arg-start) 1242 ((eq type 'c-decl-arg-start)
1243 (cons 'decl nil)) 1243 (cons 'decl nil))
1244 ;; We're inside (probably) a brace list. 1244 ;; We're inside (probably) a brace list.
1245 ((eq type 'c-not-decl) 1245 ((eq type 'c-not-decl)
1246 (cons 'not-decl nil)) 1246 (cons 'not-decl nil))
1247 ;; Inside a C++11 lambda function arglist. 1247 ;; Inside a C++11 lambda function arglist.
1248 ((and (c-major-mode-is 'c++-mode) 1248 ((and (c-major-mode-is 'c++-mode)
1249 (eq (char-before match-pos) ?\() 1249 (eq (char-before match-pos) ?\()
1250 (save-excursion 1250 (save-excursion
1251 (goto-char match-pos) 1251 (goto-char match-pos)
1252 (c-backward-token-2) 1252 (c-backward-token-2)
1253 (and 1253 (and
1254 (c-safe (goto-char (scan-sexps (point) -1))) 1254 (c-safe (goto-char (scan-sexps (point) -1)))
1255 (c-looking-at-c++-lambda-capture-list)))) 1255 (c-looking-at-c++-lambda-capture-list))))
1256 (c-put-char-property (1- match-pos) 'c-type 1256 (c-put-char-property (1- match-pos) 'c-type
1257 'c-decl-arg-start) 1257 'c-decl-arg-start)
1258 (cons 'decl nil)) 1258 (cons 'decl nil))
1259 ;; We're inside a brace list. 1259 ;; We're inside a brace list.
1260 ((and (eq (char-before match-pos) ?{) 1260 ((and (eq (char-before match-pos) ?{)
1261 (c-inside-bracelist-p (1- match-pos) 1261 (c-inside-bracelist-p (1- match-pos)
1262 (cdr (c-parse-state)) 1262 (cdr (c-parse-state))
1263 nil)) 1263 nil))
1264 (c-put-char-property (1- match-pos) 'c-type 1264 (c-put-char-property (1- match-pos) 'c-type
1265 'c-not-decl) 1265 'c-not-decl)
1266 (cons 'not-decl nil)) 1266 (cons 'not-decl nil))
1267 ;; We're inside an "ordinary" open brace. 1267 ;; We're inside an "ordinary" open brace.
1268 ((eq (char-before match-pos) ?{) 1268 ((eq (char-before match-pos) ?{)
1269 (cons (and toplev 'top) nil)) 1269 (cons (and toplev 'top) nil))
1270 ;; Inside an angle bracket arglist. 1270 ;; Inside an angle bracket arglist.
1271 ((or (eq type 'c-<>-arg-sep) 1271 ((or (eq type 'c-<>-arg-sep)
1272 (eq (char-before match-pos) ?<)) 1272 (eq (char-before match-pos) ?<))
1273 (cons '<> nil)) 1273 (cons '<> nil))
1274 ;; Got a cached hit in some other type of arglist. 1274 ;; Got a cached hit in some other type of arglist.
1275 (type 1275 (type
1276 (cons 'arglist t)) 1276 (cons 'arglist t))
1277 ;; We're at a C++ uniform initialization. 1277 ;; We're at a C++ uniform initialization.
1278 ((and (c-major-mode-is 'c++-mode) 1278 ((and (c-major-mode-is 'c++-mode)
1279 (eq (char-before match-pos) ?\() 1279 (eq (char-before match-pos) ?\()
1280 (save-excursion 1280 (save-excursion
1281 (goto-char match-pos) 1281 (goto-char match-pos)
1282 (and 1282 (and
1283 (zerop (c-backward-token-2 2)) 1283 (zerop (c-backward-token-2 2))
1284 (looking-at c-identifier-start) 1284 (looking-at c-identifier-start)
1285 (c-got-face-at (point) 1285 (c-got-face-at (point)
1286 '(font-lock-variable-name-face))))) 1286 '(font-lock-variable-name-face)))))
1287 (cons 'not-decl nil)) 1287 (cons 'not-decl nil))
1288 ((and not-front-decl 1288 ((and not-front-decl
1289 ;; The point is within the range of a previously 1289 ;; The point is within the range of a previously
1290 ;; encountered type decl expression, so the arglist 1290 ;; encountered type decl expression, so the arglist
1291 ;; is probably one that contains declarations. 1291 ;; is probably one that contains declarations.
1292 ;; However, if `c-recognize-paren-inits' is set it 1292 ;; However, if `c-recognize-paren-inits' is set it
1293 ;; might also be an initializer arglist. 1293 ;; might also be an initializer arglist.
1294 (or (not c-recognize-paren-inits) 1294 (or (not c-recognize-paren-inits)
1295 (save-excursion 1295 (save-excursion
1296 (goto-char match-pos) 1296 (goto-char match-pos)
1297 (not (c-back-over-member-initializers))))) 1297 (not (c-back-over-member-initializers)))))
1298 ;; The result of this check is cached with a char 1298 ;; The result of this check is cached with a char
1299 ;; property on the match token, so that we can look 1299 ;; property on the match token, so that we can look
1300 ;; it up again when refontifying single lines in a 1300 ;; it up again when refontifying single lines in a
1301 ;; multiline declaration. 1301 ;; multiline declaration.
1302 (c-put-char-property (1- match-pos) 1302 (c-put-char-property (1- match-pos)
1303 'c-type 'c-decl-arg-start) 1303 'c-type 'c-decl-arg-start)
1304 (cons 'decl nil)) 1304 (cons 'decl nil))
1305 ;; Got (an) open paren(s) preceded by an arith operator. 1305 ;; Got (an) open paren(s) preceded by an arith operator.
1306 ((and (eq (char-before match-pos) ?\() 1306 ((and (eq (char-before match-pos) ?\()
1307 (save-excursion 1307 (save-excursion
1308 (goto-char match-pos)
1309 (while
1310 (and (zerop (c-backward-token-2))
1311 (eq (char-after) ?\()))
1312 (looking-at c-arithmetic-op-regexp)))
1313 (cons nil nil))
1314 ;; In a C++ member initialization list.
1315 ((and (eq (char-before match-pos) ?,)
1316 (c-major-mode-is 'c++-mode)
1317 (save-excursion
1318 (goto-char match-pos)
1319 (c-back-over-member-initializers)))
1320 (c-put-char-property (1- match-pos) 'c-type 'c-not-decl)
1321 (cons 'not-decl nil))
1322 ;; At start of a declaration inside a declaration paren.
1323 ((save-excursion
1324 (goto-char match-pos) 1308 (goto-char match-pos)
1325 (and (memq (char-before match-pos) '(?\( ?\,)) 1309 (while
1326 (c-go-up-list-backward match-pos 1310 (and (zerop (c-backward-token-2))
1327 ; c-determine-limit is too slow, here. 1311 (eq (char-after) ?\()))
1328 (max (- (point) 2000) (point-min))) 1312 (looking-at c-arithmetic-op-regexp)))
1329 (eq (char-after) ?\() 1313 (cons nil nil))
1330 (let ((type (c-get-char-property (point) 'c-type))) 1314 ;; In a C++ member initialization list.
1331 (or (memq type '(c-decl-arg-start c-decl-type-start)) 1315 ((and (eq (char-before match-pos) ?,)
1332 (and 1316 (c-major-mode-is 'c++-mode)
1333 (progn (c-backward-syntactic-ws) t) 1317 (save-excursion
1334 (or 1318 (goto-char match-pos)
1335 (and 1319 (c-back-over-member-initializers)))
1336 (c-back-over-compound-identifier) 1320 (c-put-char-property (1- match-pos) 'c-type 'c-not-decl)
1337 (progn 1321 (cons 'not-decl nil))
1338 (c-backward-syntactic-ws) 1322 ;; At start of a declaration inside a declaration paren.
1339 (or (bobp) 1323 ((save-excursion
1340 (progn 1324 (goto-char match-pos)
1341 (setq type (c-get-char-property (1- (point)) 1325 (and (memq (char-before match-pos) '(?\( ?\,))
1342 'c-type)) 1326 (c-go-up-list-backward match-pos
1343 (memq type '(c-decl-arg-start 1327 ; c-determine-limit is too slow, here.
1344 c-decl-type-start)))))) 1328 (max (- (point) 2000) (point-min)))
1345 (and (zerop (c-backward-token-2)) 1329 (eq (char-after) ?\()
1346 (looking-at c-fun-name-substitute-key)))))))) 1330 (let ((type (c-get-char-property (point) 'c-type)))
1347 (cons 'decl nil)) 1331 (or (memq type '(c-decl-arg-start c-decl-type-start))
1348 (t (cons 'arglist t))))) 1332 (progn
1333 (c-backward-syntactic-ws)
1334 (cond
1335 ((and toplev
1336 (eq (char-before) ?\)))
1337 (save-excursion
1338 (and (c-go-list-backward nil (max (- (point) 2000)
1339 (point-min)))
1340 (eq (char-after) ?\()
1341 (progn (c-backward-syntactic-ws)
1342 (c-back-over-compound-identifier)))))
1343 ((save-excursion
1344 (and
1345 (c-back-over-compound-identifier)
1346 (progn
1347 (c-backward-syntactic-ws)
1348 (or (bobp)
1349 (progn
1350 (setq type (c-get-char-property (1- (point))
1351 'c-type))
1352 (memq type '(c-decl-arg-start
1353 c-decl-type-start))))))))
1354 ((and (zerop (c-backward-token-2))
1355 (looking-at c-fun-name-substitute-key)))))))))
1356 ;; Cache the result of this test for next time around.
1357 (c-put-char-property (1- match-pos) 'c-type 'c-decl-arg-start)
1358 (cons 'decl nil))
1359 (t (cons 'arglist t)))))
1349 1360
1350(defun c-font-lock-single-decl (limit decl-or-cast match-pos context toplev) 1361(defun c-font-lock-single-decl (limit decl-or-cast match-pos context toplev)
1351 ;; Try to fontify a single declaration, together with all its declarators. 1362 ;; Try to fontify a single declaration, together with all its declarators.