|
From: Alan M. <ac...@mu...> - 2026-01-29 12:58:20
|
Hello again, Arsen.
On Mon, Jan 26, 2026 at 17:07:17 +0100, Arsen Arsenović via CC-Mode-help wrote:
> Package: cc-mode
> <#secure method=pgpmime mode=sign>
> Package: cc-mode
> In a c-mode buffer, insert the following:
> int foo(int a,
> /* foo */
> int b);
> ... then, reindent the whole buffer (M-h <TAB>).
> In Emacs 29, this indented, correctly, as:
> int foo(int a,
> /* foo */
> int b);
> ... and in Emacs 31, this indents as:
> int foo(int a,
> /* foo */
> int b);
> Not sure how long this has been happening, but it's probably a few
> weeks, since my dev build (which is behind, at
> 3945654f05629c620b2b404a289a969d501ee285) is seeing the same.
The cause of this bug was the strategy used to indent leading comments,
so that
int foobar(int a,
/* b */ int b,
.....
would get indented as shown, rather than /* b */ being indented under
int a;. Unfortunately, this failed to take account of comment only
lines, which also got pushed to the left.
Here's a patch which should fix it. It tidies up other aspects of the
"leading comment" handling, too. Please try it out and let me know how
well it works. Thanks!
Incidentally, I am no longer part of the Emacs project, so I don't
commit changes there any more. The patch is based on the CC Mode
repository head at https://hg.savannah.nongnu.org/hgweb/cc-mode, which
can be cloned from that address with $ hg clone. It should work, more
or less, on the Emacs repository too.
> TIA, have a lovely day, and happy new year! :-)
> Emacs : GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.51, cairo version 1.18.4)
> of 2026-01-23
> Package: CC Mode 5.35.2 (C/*l)
> Buffer Style: gnu
> c-emacs-features: (pps-extended-state col-0-paren posix-char-classes gen-string-delim gen-comment-delim syntax-properties category-properties 1-bit)
diff -r ff81967283a6 cc-align.el
--- a/cc-align.el Fri Sep 19 20:30:07 2025 +0000
+++ b/cc-align.el Thu Jan 29 11:40:31 2026 +0000
@@ -182,6 +182,19 @@
(c-safe-position (or containing-sexp (point)) c-state-cache)
containing-sexp))))
+(defun c-leading-comment-length ()
+ "Return the length of any comments preceding the first token.
+This length extends from beginning of indentation to the first
+non-comment character on the line. If there are no such comments, or
+only comments on the line, return 0."
+ (save-excursion
+ (back-to-indentation)
+ (let ((ind-col (current-column)))
+ (c-forward-comments (c-point 'eol))
+ (if (eolp)
+ 0
+ (- (current-column) ind-col)))))
+
(defun c-lineup-arglist (_langelem)
"Line up the current argument line under the first argument.
@@ -203,11 +216,7 @@
Works with: arglist-cont-nonempty, arglist-close."
(save-excursion
(let ((indent-pos (point))
- (ws-length (save-excursion
- (back-to-indentation)
- (let ((ind-col (current-column)))
- (c-forward-comments (c-point 'eol))
- (- (current-column) ind-col)))))
+ (leading-comment-len (c-leading-comment-length)))
(if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element))
c-basic-offset ; DWIM case.
@@ -227,7 +236,7 @@
(c-forward-comments (c-point 'eol))
(if (eolp)
(goto-char arglist-content-start)))
- (vector (- (current-column) ws-length)))))))
+ (vector (- (current-column) leading-comment-len)))))))
(defun c-lineup-argcont-1 (elem)
;; Move to the start of the current arg and return non-nil, otherwise
@@ -321,6 +330,25 @@
(c-forward-syntactic-ws)
(vector (+ (current-column) c-basic-offset)))))
+(defun c-lineup-arglist-cont (_langelem)
+ "Indent an arglist-cont line under the previous argument.
+\"Ignore\" leading comments.
+
+int foobar (
+ /* a */ int a,
+ /* baz */ int b, <- c-lineup-arglist-cont
+ /* empty */ <- c-lineup-arglist-cont
+ int c); <- c-lineup-arglist-cont
+
+Works with: arglist-cont."
+ (let ((leading-comment-len (c-leading-comment-length))
+ anchor-comment-len anchor-col)
+ (save-excursion
+ (goto-char (c-langelem-pos c-syntactic-element))
+ (setq anchor-comment-len (c-leading-comment-length))
+ (setq anchor-col (current-column)))
+ (vector (- (+ anchor-col anchor-comment-len) leading-comment-len))))
+
(defun c-lineup-arglist-intro-after-paren (_langelem)
"Line up a line to just after the open paren of the surrounding paren
or brace block.
@@ -328,19 +356,15 @@
Works with: defun-block-intro, brace-list-intro, enum-intro
statement-block-intro, statement-case-intro, arglist-intro."
(save-excursion
- (let ((ws-length (save-excursion
- (back-to-indentation)
- (let ((ind-col (current-column)))
- (c-forward-comments (c-point 'eol))
- (- (current-column) ind-col)))))
- (beginning-of-line)
- (backward-up-list 1)
- (forward-char)
- (let ((after-paren-pos (point)))
- (c-forward-comments (c-point 'eol))
- (if (eolp)
- (goto-char after-paren-pos)))
- (vector (- (current-column) ws-length)))))
+ (let ((ws-length (c-leading-comment-length)))
+ (beginning-of-line)
+ (backward-up-list 1)
+ (forward-char)
+ (let ((after-paren-pos (point)))
+ (c-forward-comments (c-point 'eol))
+ (if (eolp)
+ (goto-char after-paren-pos)))
+ (vector (- (current-column) ws-length)))))
(defun c-lineup-item-after-paren-at-boi (_langelem)
"Line up to just after the surrounding open paren at boi.
diff -r ff81967283a6 cc-mode.texi
--- a/cc-mode.texi Fri Sep 19 20:30:07 2025 +0000
+++ b/cc-mode.texi Thu Jan 29 11:40:31 2026 +0000
@@ -37,8 +37,8 @@
version with cross references pointing to the GNU Emacs manuals,
the second with them pointing to the XEmacs manuals.
## Info output
- makeinfo cc-mode.texi
- makeinfo -DXEMACS cc-mode.texi
+ makeinfo --no-split cc-mode.texi
+ makeinfo --no-split -DXEMACS cc-mode.texi
## DVI output
## You may need to set up the environment variable TEXINPUTS so
@@ -6145,6 +6145,10 @@
@findex lineup-arglist (c-)
Line up the current argument line under the first argument.
+If there are any comments at the start of the line, this function will
+attempt to line up the argument correctly, with the comment appearing
+to the left of that argument.
+
As a special case, if an argument on the same line as the open
parenthesis starts with a brace block opener, the indentation is
@code{c-basic-offset} only. This is intended as a ``DWIM'' measure in
@@ -6169,6 +6173,29 @@
@comment ------------------------------------------------------------
+@defun c-lineup-arglist-cont
+@findex lineup-arglist-cont (c-)
+Line up an arglist-cont line under the previous argument.
+
+If there are any comments at the start of the line, this function will
+attempt to line up the argument correctly, with the comment appearing
+to the left of that argument.
+
+@example
+@group
+int foobar (
+ /* a */ int a,
+ /* baz */ int b, @hereFn{c-lineup-arglist-cont}
+ /* empty */ @hereFn{c-lineup-arglist-cont}
+ int c); @hereFn{c-lineup-arglist-cont}
+@end group
+@end example
+
+@workswith @code{arglist-cont}.
+@end defun
+
+@comment ------------------------------------------------------------
+
@defun c-lineup-arglist-intro-after-paren
@findex lineup-arglist-intro-after-paren (c-)
Line up a line to just after the open paren of the surrounding paren or
diff -r ff81967283a6 cc-vars.el
--- a/cc-vars.el Fri Sep 19 20:30:07 2025 +0000
+++ b/cc-vars.el Thu Jan 29 11:40:31 2026 +0000
@@ -1382,7 +1382,7 @@
(arglist-intro . +)
;; Anchor pos: At the containing statement(*).
;; 2nd pos: At the open paren.
- (arglist-cont . (c-lineup-gcc-asm-reg 0))
+ (arglist-cont . (c-lineup-gcc-asm-reg c-lineup-arglist-cont))
;; Anchor pos: At the first token after the open paren.
(arglist-cont-nonempty . (c-lineup-gcc-asm-reg c-lineup-arglist))
;; Anchor pos: At the containing statement(*).
[ .... ]
> --
> Arsen Arsenović
--
Alan Mackenzie (Nuremberg, Germany).
|