bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#41793: `comment-only-p' erroneously flags blank lines as comments


From: Lars Ingebrigtsen
Subject: bug#41793: `comment-only-p' erroneously flags blank lines as comments
Date: Thu, 28 Jan 2021 07:11:07 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Toby Cubitt <toby-dated-1593024851.318b83@dr-qubit.org> writes:

> Gah. That attempt breaks flagging of comments separated by whitespace-only 
> lines. Maybe this?
>
> (defun comment-only-p (beg end)
>   "Return non-nil if the text between BEG and END is all comments."
>   (if (string-blank-p (buffer-substring beg end)) nil
>     (save-excursion
>       (goto-char beg)
>       (comment-forward (point-max))
>       (<= end (point)))))

I don't think that can be correct...  for instance, it'll return nil on
this C comment:

/*
|

*/

> This fix successfully makes `comment-or-uncomment-region' call
> `comment-region' when fed empty lines, instead of calling
> `uncomment-region' (which seems wrong).
>
> It doesn't fix the main issue I was trying to address, namely that
> `comment-or-uncomment-region' fails to comment out a region consisting
> only of blank lines, even when `comment-empty-lines' is t. Without the
> above fix, it calls `uncomment-region' which does nothing as there's
> nothing to comment. With the above fix, it calls `comment-region'. But
> the latter refuses to comment out the lines, throwing a "Nothing to
> comment" error.
>
> Fixing that (assuming it's considered a bug) would require more
> changes to `comment-region-default' and `comment-region-internal'.
>
> The use-case for this was editing a LaTeX document, where empty lines
> are semantically significant (they demark paragraph breaks). It's
> fairly common (especially when editing co-authored documents) to
> comment out the empty lines in order to run two paragraphs together,
> whilst keeping the commented-out empty lines in the source so it's
> easy to revert.

Right -- `M-x comment-region' on a blank region will just say "Nothing
to comment" here.  Which makes sense in most modes, but not in LaTeX
mode, I think.

I've now fixed `M-x comment-region' for blank lines in `latex-mode' in
Emacs 28, I think, but `M-x comment-or-uncomment-region' still doesn't
work because `comment-only-p' is, as you say, wrong.  Your first
attempt looked like it had promise, but then you said:

> Gah. That attempt breaks flagging of comments separated by
> whitespace-only lines. Maybe this?

Do you have an example of when that fails?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





reply via email to

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