unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line
@ 2023-10-21 20:08 Spencer Baugh
  2023-10-21 21:23 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 4+ messages in thread
From: Spencer Baugh @ 2023-10-21 20:08 UTC (permalink / raw)
  To: 66670; +Cc: Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 673 bytes --]

Tags: patch


This fixes a remaining issue with whitespace values of comment-continue,
as described in https://github.com/ocaml/tuareg/issues/216

Namely, the fixes done so far don't cover comment-indent-new-line.

In GNU Emacs 29.1.50 (build 12, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2023-10-19 built on
 igm-qws-u22796a
Repository revision: 9163e634e296435aa7a78bc6b77b4ee90666d2ac
Repository branch: emacs-29
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Rocky Linux 8.8 (Green Obsidian)

Configured using:
 'configure --config-cache --with-x-toolkit=lucid
 --with-gif=ifavailable'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-buffer-local-comment-continue-in-comment-indent-.patch --]
[-- Type: text/patch, Size: 2766 bytes --]

From 9fab95f193eb7da9e6787122b885d1de29b5b657 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Sat, 21 Oct 2023 16:05:05 -0400
Subject: [PATCH] Use buffer-local comment-continue in comment-indent-new-line

As the FIXME said, comment-indent-new-line was wiping out the value of
comment-continue even if it had been set explicitly.  Checking
local-variable-p should be sufficient to fix this.

This makes comment-indent-new-line work properly if comment-continue
can't be recalculated from comment-start, such as if comment-continue
is all whitespace.

* lisp/newcomment.el (comment-indent-new-line): Don't set
comment-continue to nil if it's local-variable-p.
* test/lisp/newcomment-tests.el
(local-comment-continue-in-comment-indent-new-line): Add a test.
---
 lisp/newcomment.el            |  5 +++--
 test/lisp/newcomment-tests.el | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lisp/newcomment.el b/lisp/newcomment.el
index 9ae7de70a0e..b217f700f22 100644
--- a/lisp/newcomment.el
+++ b/lisp/newcomment.el
@@ -1541,9 +1541,10 @@ comment-indent-new-line
                                     (cadr (assoc comment-style
                                                  comment-styles))))
                      ;; Recreate comment-continue from comment-start.
-                     ;; FIXME: wrong if comment-continue was set explicitly!
                      ;; FIXME: use prev line's continuation if available.
-                     (comment-continue nil))
+                     (comment-continue (if (local-variable-p 'comment-continue)
+                                           comment-continue
+                                         nil)))
                 (comment-indent continuep))
 	      (save-excursion
 		(let ((pt (point)))
diff --git a/test/lisp/newcomment-tests.el b/test/lisp/newcomment-tests.el
index cb0f304c463..9851b4c2c45 100644
--- a/test/lisp/newcomment-tests.el
+++ b/test/lisp/newcomment-tests.el
@@ -36,4 +36,21 @@ test-uncomment-space-comment-continue
               (uncomment-region (point-min) (point-max))
               (buffer-string))))))
 
+(ert-deftest local-comment-continue-in-comment-indent-new-line ()
+  (with-temp-buffer
+    (setq-local comment-start "/* ")
+    (setq-local comment-end "*/")
+    (insert "foo")
+    (newline)
+    (insert "bar")
+    (forward-line -1)
+    (end-of-line)
+    (comment-region (point-min) (point-max))
+    (should (equal (thing-at-point 'line) "/* foo\n"))
+    (comment-indent-new-line)
+    (should (equal (thing-at-point 'line) " * \n"))
+    (setq-local comment-continue "   ")
+    (comment-indent-new-line)
+    (should (equal (thing-at-point 'line) "   \n"))))
+
 ;;; newcomment-tests.el ends here
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line
  2023-10-21 20:08 bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line Spencer Baugh
@ 2023-10-21 21:23 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-10-21 22:15   ` Spencer Baugh
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-10-21 21:23 UTC (permalink / raw)
  To: Spencer Baugh; +Cc: 66670, monnier

>                       ;; Recreate comment-continue from comment-start.
> -                     ;; FIXME: wrong if comment-continue was set explicitly!
>                       ;; FIXME: use prev line's continuation if available.
> -                     (comment-continue nil))
> +                     (comment-continue (if (local-variable-p 'comment-continue)
> +                                           comment-continue
> +                                         nil)))

Are you sure?  `comment-continue` is very rarely set globally.
Usually it's set buffer-locally by `comment-normalize-vars`.

> +(ert-deftest local-comment-continue-in-comment-indent-new-line ()
> +  (with-temp-buffer
> +    (setq-local comment-start "/* ")
> +    (setq-local comment-end "*/")
                               ^^
Out of symmetry, I'd have expected a SPC here.

> +    (insert "foo")
> +    (newline)
> +    (insert "bar")
> +    (forward-line -1)
> +    (end-of-line)
> +    (comment-region (point-min) (point-max))
> +    (should (equal (thing-at-point 'line) "/* foo\n"))
> +    (comment-indent-new-line)

You should also test it with that very same comment but when
`comment-start` and `comment-end` have been set to something like
"// " and "".


        Stefan






^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line
  2023-10-21 21:23 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-10-21 22:15   ` Spencer Baugh
  2023-10-22  4:11     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 4+ messages in thread
From: Spencer Baugh @ 2023-10-21 22:15 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 66670, monnier

Stefan Monnier <monnier@iro.umontreal.ca> writes:
>>                       ;; Recreate comment-continue from comment-start.
>> -                     ;; FIXME: wrong if comment-continue was set explicitly!
>>                       ;; FIXME: use prev line's continuation if available.
>> -                     (comment-continue nil))
>> +                     (comment-continue (if (local-variable-p 'comment-continue)
>> +                                           comment-continue
>> +                                         nil)))
>
> Are you sure?  `comment-continue` is very rarely set globally.
> Usually it's set buffer-locally by `comment-normalize-vars`.

Or by the major-mode!  But yes, I see your point, this patch is
effectively removing the (comment-continue nil) definition.

I guess that the (comment-continue nil) definition is there in the first
place so that comment-normalize-vars recalculates it based on
comment-start, which is the comment prefix from the previous line?  So
we therefore copy the prefix from the previous line?

Should we just recalculate comment-continue directly in
comment-indent-new-line instead, if necessary?

Although even if we did that, I don't see any clear way to know that we
should use the configured comment-continue instead of trying to copy the
previous line.  Should we maybe just not copy the comment prefix from
the previous line at all, if comment-continue is non-nil?

>> +(ert-deftest local-comment-continue-in-comment-indent-new-line ()
>> +  (with-temp-buffer
>> +    (setq-local comment-start "/* ")
>> +    (setq-local comment-end "*/")
>                                ^^
> Out of symmetry, I'd have expected a SPC here.
>
>> +    (insert "foo")
>> +    (newline)
>> +    (insert "bar")
>> +    (forward-line -1)
>> +    (end-of-line)
>> +    (comment-region (point-min) (point-max))
>> +    (should (equal (thing-at-point 'line) "/* foo\n"))
>> +    (comment-indent-new-line)
>
> You should also test it with that very same comment but when
> `comment-start` and `comment-end` have been set to something like
> "// " and "".

Will do.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line
  2023-10-21 22:15   ` Spencer Baugh
@ 2023-10-22  4:11     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-10-22  4:11 UTC (permalink / raw)
  To: Spencer Baugh; +Cc: 66670, monnier

>>>                       ;; Recreate comment-continue from comment-start.
>>> -                     ;; FIXME: wrong if comment-continue was set explicitly!
>>>                       ;; FIXME: use prev line's continuation if available.
>>> -                     (comment-continue nil))
>>> +                     (comment-continue (if (local-variable-p 'comment-continue)
>>> +                                           comment-continue
>>> +                                         nil)))
>>
>> Are you sure?  `comment-continue` is very rarely set globally.
>> Usually it's set buffer-locally by `comment-normalize-vars`.
>
> Or by the major-mode!  But yes, I see your point, this patch is
> effectively removing the (comment-continue nil) definition.

Yup.

> I guess that the (comment-continue nil) definition is there in the first
> place so that comment-normalize-vars recalculates it based on
> comment-start,

Indeed, that's what the comment tries to say.

> which is the comment prefix from the previous line?

Something like that, yes.

> So we therefore copy the prefix from the previous line?

Not necessarily.  If the previous line was the beginning of the comment
with "/*" we should use " *".

> Should we just recalculate comment-continue directly in
> comment-indent-new-line instead, if necessary?

We could.  But we still need to decide whether to do that or to just
trust the `comment-continue` value set by those major modes which set
it by hand :-(


        Stefan






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-22  4:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-21 20:08 bug#66670: [PATCH] Use buffer-local comment-continue in comment-indent-new-line Spencer Baugh
2023-10-21 21:23 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-21 22:15   ` Spencer Baugh
2023-10-22  4:11     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).