Subject: bug#71698: closed (Re: 29.3; comint--intersect-regions fontifies some output using input function)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 23 Jun 2024 07:29:01 +0000

Your bug report

#71698: 29.3; comint--intersect-regions fontifies some output using input function

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
71698: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=71698
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: 29.3; comint--intersect-regions fontifies some output using input function
From: <[email-masked]>
To: JD Smith <[email-masked]>
Cc: [email-masked]
Date: Sun, 23 Jun 2024 09:28:30 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

JD Smith <[email-masked]> writes:

> Thanks, I took a look.  commit 949bc1c72d7 I believe solves the same problem, and also fixes my issue.  In fact I think the solutions are equivalent, since the end test will never need to trigger, because for field-end the left associativity works in our favor.  But yours is more explicit.  
>
> Feel free to close.

Thanks. I'm therefore closing this bug report.

On an unrelated note, my reply to your personal e-mail

    Subject: comint-fontify-input-mode
    Date: Wed, 22 May 2024 14:50:00 -0400

might have ended up in your spam folder, so I kindly ask you to check it
if you are interested.

Subject: 29.3; comint--intersect-regions fontifies some output using input function
From: JD Smith <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Fri, 21 Jun 2024 13:11:58 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

This relates to bug#51940: 29.0.50; [PATCH] Fontification and indentation in M-x shell.

I found a tiny bug in the introduced command `comint--intersect-regions'.  It pertains to the "field-expansion" in the following section, which is responsible for narrowing to alternating regions of input and output:

(let ((beg2 beg1)
      (end2 end1))
  (when (= beg2 beg)
    (setq beg2 (field-beginning beg2))) ; <--- bug here
  (when (= end2 end)
    (setq end2 (field-end end2)))
  ;; Narrow to the whole field surrounding the region
  (narrow-to-region beg2 end2))

The nature of the bug is that `field-beginning' is left-associative, whereas `(get-text-property (point) 'field)` is right associative.  You can demonstrate this to yourself as follows:

(progn
  (insert (concat "\n" (propertize "AAA" 'field 'a) (propertize "B" 'field 'b) (propertize "CCC" 'field 'c)))
  (goto-char (- (point) 4))             ; cursor on 'B'
  (list (get-text-property (point) 'field) (field-beginning) (field-end)
        (buffer-substring-no-properties (field-beginning) (field-end))))
;; gives: (b 356 359 "AAA")

So on the line:

              (setq beg2 (field-beginning beg2))) ; <--- bug here

if beg is at the first character of its field, `field-beginning` will actually return the start of the prior field.  This leads to `comint--intersect-regions' unwittingly including `output' field text in its input fontification narrowing.  I noticed this in a new python interactive mode I'm working on: the prompt itself (i.e. output) was being partially fontified by python-mode.

I believe the correct fix is:

              (setq beg2 (field-beginning (1+ beg2))))

This takes care of the problem here, even for single-character fields (like `boundary').  We know (< beg1 end) from the loop condition, so it's safe to increment beg2.

--- /var/folders/by/19mt78cj63v5_tslqh826g7w0000gn/T/jka-comrWKsIJ	2024-06-21 13:08:59
+++ /var/folders/by/19mt78cj63v5_tslqh826g7w0000gn/T/buffer-content-Q8dmID	2024-06-21 13:08:59
@@ -4122,7 +4122,7 @@
           (let ((beg2 beg1)
                 (end2 end1))
             (when (= beg2 beg)
-              (setq beg2 (field-beginning beg2)))
+              (setq beg2 (field-beginning (1+ beg2))))
             (when (= end2 end)
               (setq end2 (field-end end2)))
             ;; Narrow to the whole field surrounding the region