Subject: bug#20256: 25.0.50; css-mode: filling multi-line comments

From: Simen Heggestøyl <[email-masked]>
Date: Thu, 09 Apr 2015 20:51:06 +0200

Thanks for the hints, Stefan!

The following patch seems to work well for me, both with css-mode and
scss-mode:

 From 16b46e34bb3e0e69a039e4ed0737013aa9a06f86 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Thu, 9 Apr 2015 19:09:04 +0200
Subject: [PATCH] css-mode.el: Support multi-line comment filling

Fixes: debbugs:20256

* css-mode.el (css-fill-paragraph): Support multi-line comment
filling.
---
 lisp/textmodes/css-mode.el | 57 
++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)

diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index 851618c..f452e17 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -381,7 +381,7 @@ pseudo-classes, and at-rules."
   (setq-local comment-start-skip "/\\*+[ \t]*")
   (setq-local comment-end "*/")
   (setq-local comment-end-skip "[ \t]*\\*+/")
-  (setq-local fill-paragraph-function 'css-fill-paragraph)
+  (setq-local fill-paragraph-function #'css-fill-paragraph)
   (setq-local add-log-current-defun-function #'css-current-defun-name)
   (smie-setup css-smie-grammar #'css-smie-rules
               :forward-token #'css-smie--forward-token
@@ -395,6 +395,10 @@ pseudo-classes, and at-rules."

 (defun css-fill-paragraph (&optional justify)
   (save-excursion
+    ;; Fill succeeding comment when invoked at the beginning of a
+    ;; multi-line comment.
+    (when (save-excursion (back-to-indentation) (looking-at "/\\*"))
+      (skip-chars-forward " \t/*"))
     (let ((ppss (syntax-ppss))
           (eol (line-end-position)))
       (cond
@@ -408,24 +412,39 @@ pseudo-classes, and at-rules."
         ;; This code is meant to be generic, so that it works not only 
for
         ;; css-mode but for all modes.
         (save-restriction
-          (narrow-to-region (nth 8 ppss) eol)
-          (comment-normalize-vars)      ;Will define comment-continue.
-          (let ((fill-paragraph-function nil)
-                (paragraph-separate
-                 (if (and comment-continue
-                          (string-match "[^ \t]" comment-continue))
-                     (concat "\\(?:[ \t]*" (regexp-quote 
comment-continue)
-                             "\\)?\\(?:" paragraph-separate "\\)")
-                   paragraph-separate))
-                (paragraph-start
-                 (if (and comment-continue
-                          (string-match "[^ \t]" comment-continue))
-                     (concat "\\(?:[ \t]*" (regexp-quote 
comment-continue)
-                             "\\)?\\(?:" paragraph-start "\\)")
-                   paragraph-start)))
-            (fill-paragraph justify)
-            ;; Don't try filling again.
-            t)))
+          ;; Ensure that multi-line variants of `comment-start' and
+          ;; `comment-end' are in use, in order to support multi-line
+          ;; comment filling in SCSS mode as well.
+          (let ((comment-start "/*")
+                (comment-end "*/"))
+            (narrow-to-region (nth 8 ppss) eol)
+            (comment-normalize-vars)    ;Will define comment-continue.
+            (let ((fill-paragraph-function nil)
+                  (paragraph-separate
+                   (if (and comment-continue
+                            (string-match "[^ \t]" comment-continue))
+                       (concat "\\(?:[ \t]*"
+                               (regexp-opt
+                                (list comment-continue comment-start
+                                      comment-end))
+                               "\\)?\\(?:" paragraph-separate "\\)")
+                     paragraph-separate))
+                  (paragraph-start
+                   (if (and comment-continue
+                            (string-match "[^ \t]" comment-continue))
+                       (concat "\\(?:[ \t]*"
+                               (regexp-quote comment-continue)
+                               "\\)?\\(?:" paragraph-start "\\)")
+                     paragraph-start))
+                  (adaptive-fill-function
+                   (lambda ()
+                     (when (looking-at "[ \t]*/\\*[ \t]*")
+                       (let ((str (match-string 0)))
+                         (and (string-match "/\\*" str)
+                              (replace-match " *" t t str)))))))
+              (fill-paragraph justify)
+              ;; Don't try filling again.
+              t))))

        ((and (null (nth 8 ppss))
              (or (nth 1 ppss)
-- 
2.1.4