emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Smie-auto-fill doesn’t respect comment-auto-fill-only-comments


From: Stefan Monnier
Subject: Re: Smie-auto-fill doesn’t respect comment-auto-fill-only-comments
Date: Sun, 30 Apr 2017 13:52:44 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

>> This won't fix the SMIE issue though.  That would require a similar fix
>> in smie-auto-fill.
> We should try and find a solution which doesn't require changing every
> normal-auto-fill-function.

I think the patch below "does the trick" but it's rather ugly.


        Stefan


diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 4b261c34c6..6a2bf35788 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1316,13 +1316,6 @@ comment-dwim
               (insert (comment-padleft comment-end add)))
             (indent-according-to-mode)))))))
 
-;;;###autoload
-(defcustom comment-auto-fill-only-comments nil
-  "Non-nil means to only auto-fill inside comments.
-This has no effect in modes that do not define a comment syntax."
-  :type 'boolean
-  :group 'comment)
-
 (defun comment-valid-prefix-p (prefix compos)
     "Check that the adaptive fill prefix is consistent with the context.
 PREFIX is the prefix (presumably guessed by `adaptive-fill-mode').
@@ -1384,7 +1377,6 @@ comment-indent-new-line
     ;; If we are not inside a comment and we only auto-fill comments,
     ;; don't do anything (unless no comment syntax is defined).
     (unless (and comment-start
-                comment-auto-fill-only-comments
                 (not (called-interactively-p 'interactive))
                 (not (save-excursion
                        (prog1 (setq compos (comment-beginning))
diff --git a/lisp/simple.el b/lisp/simple.el
index edc822eb51..72f265d544 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -7175,6 +7175,12 @@ normal-auto-fill-function
 ;; can be useful to prevent auto-filling.
 (put 'auto-fill-function 'safe-local-variable 'null)
 
+(defcustom comment-auto-fill-only-comments nil
+  "Non-nil means to only auto-fill inside comments.
+This has no effect in modes that do not define a comment syntax."
+  :type 'boolean
+  :group 'comment)
+
 (define-minor-mode auto-fill-mode
   "Toggle automatic line breaking (Auto Fill mode).
 With a prefix argument ARG, enable Auto Fill mode if ARG is
@@ -7191,8 +7197,19 @@ auto-fill-mode
 The value of `normal-auto-fill-function' specifies the function to use
 for `auto-fill-function' when turning Auto Fill mode on."
   :variable (auto-fill-function
-             . (lambda (v) (setq auto-fill-function
-                            (if v normal-auto-fill-function)))))
+             . (lambda (v)
+                 (if (not v)
+                     (setq auto-fill-function nil)
+                   (setq auto-fill-function normal-auto-fill-function)
+                   (add-function :around normal-auto-fill-function
+                                 (lambda (orig-fun &rest args)
+                                   (if (and comment-start
+                                           comment-auto-fill-only-comments
+                                            (not (nth 4 (syntax-ppss))))
+                                       ;; Comments exist and we only want to
+                                       ;; auto-fill them but we're not in one!
+                                       nil
+                                     (apply orig-fun args))))))))
 
 ;; This holds a document string used to document auto-fill-mode.
 (defun auto-fill-function ()




reply via email to

[Prev in Thread] Current Thread [Next in Thread]