GNU bug report logs - #41793
`comment-only-p' erroneously flags blank lines as comments

Previous Next

Package: emacs;

Reported by: Toby Cubitt <toby-dated-1593021285.3f7973 <at> dr-qubit.org>

Date: Wed, 10 Jun 2020 18:02:02 UTC

Severity: normal

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


Message #11 received at 41793 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Toby Cubitt <toby-dated-1593024851.318b83 <at> dr-qubit.org>
Cc: 41793 <at> debbugs.gnu.org
Subject: Re: bug#41793: `comment-only-p' erroneously flags blank lines as
 comments
Date: Thu, 28 Jan 2021 07:11:07 +0100
Toby Cubitt <toby-dated-1593024851.318b83 <at> 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




This bug report was last modified 3 years and 14 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.