Subject: Re: 30.0.50; CC Mode: New `c-for-clauses-as-arglist' style variable

From: Olivier Certner <[email-masked]>
Date: Fri, 05 May 2023 00:22:12 +0200

Proposed change.

-- 
Olivier Certner

>From 3a84abb4cb06f9673c851aa44f2f1b27752560ef Mon Sep 17 00:00:00 2001
From: Olivier Certner <[email-masked]>
Date: Wed, 3 May 2023 18:12:27 +0200
Subject: [PATCH] CC Mode: New `c-for-clauses-as-arglist' style variable

This new style variable allows to disable special handling of "for"
statements' clauses, which is that there are indented as statements,
or continuations of such, when on separate lines instead of argument
lists or continuations of such.

* lisp/progmodes/cc-engine.el (c-guess-basic-syntax): Skip case 7D,
which specifically handles the above-mentioned case.  While here,
rewrite the case's comment to be more explicit about what it does.

* lisp/progmodes/cc-vars.el (c-for-clauses-as-arglist): The new style
variable.
(c-style-variables): Include the new style variable.
(c-style-variables-are-local-p): Update documentation following
addition of the variable.

* doc/misc/cc-mode.texi (Style Variables): List the new variable.
(Syntactic Symbols): Indicate precisely which syntactic symbol can
appear in a syntactic element when analyzing `for' clauses depending
on the style variable value.

(Bug#63286)
---
 doc/misc/cc-mode.texi       | 19 ++++++++++++++-----
 lisp/progmodes/cc-engine.el | 13 +++++++------
 lisp/progmodes/cc-vars.el   | 14 +++++++++++++-
 3 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi
index 71bf3fcee4a..0e4ba4c6a91 100644
--- a/doc/misc/cc-mode.texi
+++ b/doc/misc/cc-mode.texi
@@ -2624,6 +2624,7 @@ Style Variables
 Commas});@*
 @code{c-cleanup-list} (@pxref{Clean-ups});@*
 @code{c-basic-offset} (@pxref{Customizing Indentation});@*
+@code{c-for-clauses-as-arglist} (@pxref{Syntactic Symbols});@*
 @code{c-offsets-alist} (@pxref{c-offsets-alist});@*
 @code{c-comment-only-line-offset} (@pxref{Comment Line-Up});@*
 @code{c-special-indent-hook}, @code{c-label-minimum-indentation}
@@ -4267,7 +4268,8 @@ Syntactic Symbols
 Subsequent lines in an enum or static array list where the line begins
 with an open brace.  @ref{Brace List Symbols}.
 @item statement
-A statement.  @ref{Function Symbols}.
+A statement, including `for' clauses except if
+@code{c-for-clauses-as-arglist} is true.  @ref{Function Symbols}.
 @item statement-cont
 A continuation of a statement.  @ref{Function Symbols}.
 @item annotation-var-cont
@@ -4309,15 +4311,22 @@ Syntactic Symbols
 @item comment-intro
 A line containing only a comment introduction.  @ref{Literal Symbols}.
 @item arglist-intro
-The first line in an argument list.  @ref{Paren List Symbols}.
+The first line in an argument list or a parenthesized expression.
+Note that @code{for} clauses are rather considered statements (or
+their continuation) except if @code{c-for-clauses-as-arglist} is true.
+@ref{Paren List Symbols}.
 @item arglist-cont
 Subsequent argument list lines when no arguments follow on the same
-line as the arglist opening paren.  @ref{Paren List Symbols}.
+line as the arglist opening paren.  Same remark concerning @code{for}
+clauses as for @code{arglist-intro} above.  @ref{Paren List Symbols}.
 @item arglist-cont-nonempty
 Subsequent argument list lines when at least one argument follows on
-the same line as the arglist opening paren.  @ref{Paren List Symbols}.
+the same line as the arglist opening paren.  Same remark concerning
+@code{for} clauses as for @code{arglist-intro} above.  @ref{Paren List
+Symbols}.
 @item arglist-close
-The solo close paren of an argument list.  @ref{Paren List Symbols}.
+The solo close paren of an argument list or a @code{for} clause.
+@ref{Paren List Symbols}.
 @item stream-op
 Lines continuing a stream operator (C++ only).  @ref{Literal
 Symbols}. @c @emph{FIXME!!!  Can this not be moved somewhere better?}
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 27740b4903c..409cbc59ab5 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -15246,12 +15246,13 @@ c-guess-basic-syntax
 			     (c-most-enclosing-brace paren-state (point))
 			     paren-state))
 
-	 ;; CASE 7D: we are inside a conditional test clause. treat
-	 ;; these things as statements
-	 ((progn
-	    (goto-char containing-sexp)
-	    (and (c-safe (c-forward-sexp -1) t)
-		 (looking-at "\\<for\\>[^_]")))
+	 ;; CASE 7D: We are inside a for clause.  Treat these clauses
+	 ;; as statements unless `c-for-clauses-as-arglist' is
+	 ;; non-nil.
+	 ((and (not c-for-clauses-as-arglist)
+	       (goto-char containing-sexp)
+	       (c-safe (c-forward-sexp -1) t)
+	       (looking-at "\\<for\\>[^_]"))
 	  (goto-char (1+ containing-sexp))
 	  (c-forward-syntactic-ws indent-point)
 	  (if (eq char-before-ip ?\;)
diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el
index 72d4b93ee59..304ebca52fe 100644
--- a/lisp/progmodes/cc-vars.el
+++ b/lisp/progmodes/cc-vars.el
@@ -158,7 +158,8 @@ c-style-variables
     c-comment-prefix-regexp c-doc-comment-style c-cleanup-list
     c-hanging-braces-alist c-hanging-colons-alist
     c-hanging-semi&comma-criteria c-backslash-column c-backslash-max-column
-    c-special-indent-hook c-label-minimum-indentation c-offsets-alist)
+    c-special-indent-hook c-label-minimum-indentation
+    c-for-clauses-as-arglist c-offsets-alist)
   "List of the style variables.")
 
 (defvar c-fallback-style nil)
@@ -960,6 +961,16 @@ c-label-minimum-indentation
   :type 'integer
   :group 'c)
 
+(defcustom-c-stylevar c-for-clauses-as-arglist nil
+  "Whether to consider for clauses as part of an argument list.
+The clauses of the for statement are normally considered by CC
+mode as separate statements when at start of a line \(and
+statement continuations when split).  Setting this variable to
+non-nil indicates that they should be treated as any other
+argument lists."
+  :type 'boolean
+  :group 'c)
+
 (defcustom c-progress-interval 5
   "Interval used to update progress status during long re-indentation.
 If a number, percentage complete gets updated after each interval of
@@ -1449,6 +1460,7 @@ c-style-variables-are-local-p
     `c-backslash-column'
     `c-backslash-max-column'
     `c-label-minimum-indentation'
+    `c-for-clauses-as-arglist'
     `c-offsets-alist'
     `c-special-indent-hook'
     `c-indentation-style'"
-- 
2.39.2