Subject: bug#22369: 24.5; comment-style 'extra-line' doesn't correctly indent with tabs

From: Alan Mackenzie <[email-masked]>
Date: 15 Jan 2016 11:56:28 -0000

Hello, Geyslan.

In article <[email-masked]> you wrote:
> (setq c-basic-offset 8
>       tab-width 8
>       indent-tabs-mode t
>       comment-style 'extra-line)
> (c-set-style "linux")

> Using the above settings the comment-dwim (with region active) indent
> the second and last lines with spaces instead of tabs.

>         /*
>          * void main()
>          * {
>          *  int i;
>          *  int b;
>          *  printf("format string");
>          * }
>          */

Yes.  Thanks for taking the trouble to report this.  The following patch
should fix this bug.  After applying the patch (in .../emacs-24.5/lisp),
byte-compile the file with:

    $ emacs -Q -batch -f batch-byte-compile newcomment.el

on the command line.  If you then load the file (with M-x load-file) it
should then work.

However, the complication is that newcomment.el is a built-in part of
Emacs rather than being a file loaded at runtime.  So you then have the
choice of either putting "(load newcomment.elc)" into your .emacs, or
rebuilding Emacs entirely (which isn't that time-consuming or difficult).
To do this, in directory .../emacs-24.5, do:

    $ make

.

If there are still problems with the fix, please report these to the bug
mailing list at [email-masked].

> For better comprehension check out this
> http://stackoverflow.com/questions/34710840/c-comment-in-emacs-linux-kernel-style-v2

> May I suggest the addition of a new comment-style option that does like this?

>         /* void main()
>          * {
>          *  int i;
>          *  int b;
>          *  printf("format string");
>          * }
>          */

OK, because of this feature request, I'll leave the bug open.  This would
indeed not be difficult to implement, but it'll have to be discussed on
the developers' mailing list.

> In GNU Emacs 24.5.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.16.6)
>  of 2015-09-09 on foutrelis
> Windowing system distributor `The X.Org Foundation', version 11.0.11800000
> System Description:    Arch Linux

[ .... ]

Here's the patch:

--- newcomment.el~	2015-04-02 07:23:06.000000000 +0000
+++ newcomment.el	2016-01-15 11:41:24.912588709 +0000
@@ -969,6 +969,14 @@
 	  (goto-char (point-max))))))
   (set-marker end nil))
 
+(defun comment-make-bol-ws (len)
+  "Make a white-space string of width LEN for use at BOL.
+When `indent-tabs-mode' is non-nil, tab characters will be used."
+  (if (and indent-tabs-mode (> tab-width 0))
+      (concat (make-string (/ len tab-width) ?\t)
+	      (make-string (% len tab-width) ? ))
+    (make-string len ? )))
+
 (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block)
   "Make the leading and trailing extra lines.
 This is used for `extra-line' style (or `box' style if BLOCK is specified)."
@@ -1004,8 +1012,8 @@
 	  (setq cs (replace-match fill t t s)))
 	(string-match re e)
 	(setq ce (replace-match fill t t e))))
-    (cons (concat cs "\n" (make-string min-indent ? ) ccs)
-	  (concat cce "\n" (make-string (+ min-indent eindent) ? ) ce))))
+    (cons (concat cs "\n" (comment-make-bol-ws min-indent) ccs)
+	  (concat cce "\n" (comment-make-bol-ws (+ min-indent eindent)) ce))))
 
 (defmacro comment-with-narrowing (beg end &rest body)
   "Execute BODY with BEG..END narrowing.

-- 
Alan Mackenzie (Nuremberg, Germany).