all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Toby Cubitt <toby-dated-1593024851.318b83@dr-qubit.org>
To: 41793@debbugs.gnu.org
Subject: bug#41793: `comment-only-p' erroneously flags blank lines as comments
Date: Wed, 10 Jun 2020 19:52:51 +0100	[thread overview]
Message-ID: <20200610185251.GA25375@bb84> (raw)
In-Reply-To: <20200610175331.GA23889@bb84>

On Wed, Jun 10, 2020 at 06:53:31PM +0100, Toby Cubitt wrote:
> In GNU Emacs 26.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.16)
>  of 2020-04-28
> 
> On the following text, with point at |
> (comment-only-p (point) (1+ (point)))
> returns t when it should probably return nil:
> 
> "
> |
> 
> test"
> 
> This impacts commands like `comment-or-uncomment-region'.
> 
> This is because the current implementation of `comment-only-p' fails to check the return value of (comment-forward):
> 
> The correct implementation should probably be:
[snip]

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)))))

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.

Best,
Toby
-- 
Dr T. S. Cubitt
Reader (Associate Professor) in Quantum Information
Royal Society University Research Fellow
Department of Computer Science
University College London

email: tsc25@cantab.net
web:   www.dr-qubit.org





  reply	other threads:[~2020-06-10 18:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-10 17:53 bug#41793: `comment-only-p' erroneously flags blank lines as comments Toby Cubitt
2020-06-10 18:52 ` Toby Cubitt [this message]
2021-01-28  6:11   ` Lars Ingebrigtsen
2022-05-09 11:14     ` Lars Ingebrigtsen
2022-05-09 15:07       ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200610185251.GA25375@bb84 \
    --to=toby-dated-1593024851.318b83@dr-qubit.org \
    --cc=41793@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.