Subject: Re: 28.0.50; Support GNU style of multiline comments in C source code

From: Alan Mackenzie <[email-masked]>
Date: Wed, 29 Sep 2021 17:30:37 +0000

Hello, Stefan.

On Wed, Sep 29, 2021 at 08:08:43 -0400, Stefan Monnier wrote:
> > OK.  Here's a provisional patch to lisp/newcomment.el that tries to fix
> > the two spaces at the end of a sentence issue.  I haven't got anywhere
> > with the main bug, yet.

> Thanks, Alan.  This looks good.
> Only one request: could you put the bulk of the new code into a separate function?
> I'll see about the "main bug" ASAP,

How about the patch below?  It's not perfect - the move-to-column might
insert a tab at EOL, which will then be the Wrong Thing after the
function inserts the comment opener at the left margin.  Or something
like that.  Maybe we should bind indent-tabs-mode to nil around  the
move-to-column.

I'll leave the "main bug" to you, then.

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index b458f0356d..5e8691890d 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1124,6 +1124,27 @@ comment-add
       (* comment-add 1)
     (1- (prefix-numeric-value arg))))
 
+(defun comment--sentence-spaces (c-end)
+  "Add spaces at EOL in a comment if needed for an end of sentence.
+C-END, a string, is the comment ender for the current line.  When
+called, point must be at the end of the line, and will be left
+at the possibly changed EOL at the end of this function."
+  (let (extra-spaces eos-col)
+    (setq extra-spaces
+          (and (save-excursion
+                 (skip-chars-backward "  \t")
+                 (setq eos-col (current-column))
+                 (or (bolp) (backward-char))
+                 (looking-at (sentence-end)))
+               (max (- (if sentence-end-double-space 2 1)
+                       (- (current-column) eos-col)
+                       (progn
+                         (string-match "^[  \t]*" c-end)
+                         (match-end 0))) ; # spaces in ce/cce.
+                    0)))
+  (when (and extra-spaces (> extra-spaces 0))
+    (move-to-column (+ (current-column) extra-spaces) t))))
+
 (defun comment-region-internal (beg end cs ce
                                 &optional ccs cce block lines indent)
   "Comment region BEG .. END.
@@ -1174,6 +1195,8 @@ comment-region-internal
 		(unless (looking-at "[ \t]*$")
 		  (setq min-indent (min min-indent (current-indentation))))
 		(end-of-line)
+                (let ((c-end (if (eobp) ce cce)))
+                  (if c-end (comment--sentence-spaces c-end)))
 		(setq max-indent (max max-indent (current-column)))
 		(not (or (eobp) (progn (forward-line) nil)))))

>         Stefan

-- 
Alan Mackenzie (Nuremberg, Germany).