GNU bug report logs - #51969
29.0.50; Add command for refilling ERC buffers

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Severity: wishlist; Reported by: "J.P." <jp@HIDDEN>; Keywords: patch; dated Fri, 19 Nov 2021 10:41:01 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.
Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefan@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 51969 <at> debbugs.gnu.org:


Received: (at 51969) by debbugs.gnu.org; 29 Nov 2021 13:09:25 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Nov 29 08:09:25 2021
Received: from localhost ([127.0.0.1]:37390 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1mrgPL-0001w5-Rg
	for submit <at> debbugs.gnu.org; Mon, 29 Nov 2021 08:09:25 -0500
Received: from mail-108-mta175.mxroute.com ([136.175.108.175]:37611)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jp@HIDDEN>) id 1mrgPJ-0001vq-1H
 for 51969 <at> debbugs.gnu.org; Mon, 29 Nov 2021 08:09:22 -0500
Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com)
 (Authenticated sender: mN4UYu2MZsgR)
 by mail-108-mta175.mxroute.com (ZoneMTA) with ESMTPSA id
 17d6bd04f7e000177f.001 for <51969 <at> debbugs.gnu.org>
 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256);
 Mon, 29 Nov 2021 13:09:13 +0000
X-Zone-Loop: 78347433bbcae0c0db3c8b21bc718093091fa0cf705b
X-Originating-IP: [149.28.56.236]
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=neverwas.me
 ; s=x;
 h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References:
 Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID:
 Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc
 :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
 List-Post:List-Owner:List-Archive;
 bh=6lWEQRe8LCPgKIpjhSCo+yDWy9DDYLrYLLi0nOAlSx0=; b=DqTkB9mpBhgqECBDhc8wmqeu84
 tTcuYdMWCNsLbWj4GayG4otAXgYoAJLPnpkrN+FwTA+mD39zpOLvrWonNKNWy6k5/DcchGGbj0CJG
 3Dw60Bt2lGogwchEm8sLMI++G5ntmHkB/5j6YW+sRqmdcCY5uEAi0j7Y84fW9ugTaBA/SRLwubC2S
 cXfc92+aHPlVUGEWkOINi+g/tVEeDj9Hkz7bq4H+ir5nwE7Redpvx0iUxvAl+6nuZJveeuanndfMp
 dOnw6HM/9FF3LuPlOe4qosPVBo9FWWPgeBvWfbDoA9I837sS2dWQ8X8pLlTaBGSgwqlh2wp5Q+Vtv
 REsPeXEw==;
From: "J.P." <jp@HIDDEN>
To: 51969 <at> debbugs.gnu.org
Subject: Re: bug#51969: 29.0.50; Add command for refilling ERC buffers
References: <87bl2gjuo9.fsf@HIDDEN>
Date: Mon, 29 Nov 2021 05:09:10 -0800
In-Reply-To: <87bl2gjuo9.fsf@HIDDEN> (J. P.'s message of "Fri, 19 Nov
 2021 02:39:50 -0800")
Message-ID: <871r2zt8g9.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-AuthUser: masked@HIDDEN
X-Zone-Spam-Resolution: no action
X-Zone-Spam-Status: No, score=-0.1, required=15, tests=[ARC_NA=0,
 RCPT_COUNT_TWO=0, FROM_HAS_DN=0, NEURAL_HAM=0, HAS_ATTACHMENT=0,
 FROM_EQ_ENVFROM=0, MIME_TRACE=0, MIME_GOOD=-0.1, TO_DN_NONE=0,
 MID_RHS_MATCH_FROM=0, RCVD_COUNT_ZERO=0]
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 51969
Cc: emacs-erc@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain

v5. Fixed a few oversights, but others undoubtedly remain.

If we're serious about preserving a message's original white space, then
various details related to filling and indenting still need hammering
out. And if that ultimately involves tampering with the two existing
fill functions (and such a prospect proves sufficiently unpopular), we
could always try adding dedicated variants that preserve original
spacing as their thing. It's also possible that such additions may end
up needing companions to unfill in their particular style.

But progress on these and other fronts will have to wait (unless someone
else wants to have a go) because this feature remains among ERC's lowest
priorities, ATM (IMO).

(Also, the undo situation is yet unexplored.)


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=0000-v4-v5.patch

From d5e69f8ec65105d19bf46490611b0b6becefbd85 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Sun, 28 Nov 2021 23:59:45 -0800
Subject: NOT A PATCH

F. Jason Park (3):
  Remove timestamp from erc-stamp sensor function
  Make some erc-stamp functions more limber
  Add command to refill ERC buffers

 lisp/erc/erc-fill.el                          | 126 ++++++++++-
 lisp/erc/erc-stamp.el                         |  41 ++--
 .../erc/erc-fill-resources/static-60.buffer   |  24 +++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 198 ++++++++++++++++++
 7 files changed, 430 insertions(+), 15 deletions(-)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

Interdiff:
diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index f9f8f8ad5d..b3f650bc92 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,15 +112,10 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
 
-;; If there's a chance of a job's cancellation leaving things in a bad
-;; state (like with stamps removed and yet to be replaced), this
-;; function should be protected by a condition-case so the narrowed
-;; buffer's contents can be restored and the signal repropagated.
-(defun erc-fill--refill-message (beg end)
-  "Refill but don't re-stamp region between BEG and END.
+(defun erc-fill--refill-message ()
+  "Refill but don't re-stamp accessible portion of current buffer.
 Return non-nil if timestamps were removed."
   (let (left-changed right-changed)
-    (narrow-to-region beg end)
     ;; Remove at most one left timestamp, if any.
     (goto-char (point-min))
     (setq left-changed
@@ -138,7 +133,7 @@ erc-fill--refill-message
     ;; note below re ASCII art).
     (let ((fill-column (string-width (buffer-string))))
       (fill-region (point-min) (point-max)))
-    ;; Remove any stamps from right-hand side.
+    ;; Remove all right stamps, if any.
     (goto-char (point-min))
     (setq right-changed
           (when-let* ((nextf (next-single-property-change (point) 'field)))
@@ -158,6 +153,15 @@ erc-fill--refill-message
         (setq erc-timestamp-last-inserted-right nil))
       t)))
 
+(defvar erc-fill--refilling nil
+  "Non-nil when refilling.") ; Otherwise nil during normal response handling
+
+(defvar-local erc-fill--refill-thread nil
+  "A thread running a buffer-refill job.")
+
+(cl-defmethod erc-stamp--current-time (&context (erc-fill--refilling cons))
+  erc-fill--refilling)
+
 ;; TODO make `erc-fill-mode' respect preformatted text.  Currently, diagrams
 ;; and art (like figlets) meant to span multiple messages get ruined.
 (defun erc-fill--refill ()
@@ -165,47 +169,52 @@ erc-fill--refill
         (reporter (unless noninteractive
                     (make-progress-reporter "filling" 0 (point-max))))
         (inhibit-read-only t)
-        (inhibit-point-motion-hooks t)
-        ;;
-        ct) ; cached current time
-    (cl-letf (((symbol-function #'erc-restore-text-properties) #'ignore)
-              ((symbol-function #'current-time) (lambda () ct)))
-      (while
-          (save-excursion
-            (goto-char (or (marker-position m) (set-marker m (point-min))))
-            (when-let*
-                ((beg (if (get-text-property (point) 'cursor-sensor-functions)
-                          (point)
-                        (when-let*
-                            ((max (min (point-max) (+ 512 (point))))
-                             (res (next-single-property-change
-                                   (point) 'cursor-sensor-functions nil max))
-                             ((/= res max))) ; otherwise, we're done.
-                          res)))
-                 (val (get-text-property beg 'cursor-sensor-functions))
-                 (ts (get-text-property beg 'erc-timestamp))
-                 (beg (progn ; remove left padding, if any.
-                        (goto-char beg)
-                        (skip-syntax-forward "-")
-                        (delete-region (min (line-beginning-position) beg)
-                                       (point))
-                        (point)))
-                 ;; Don't expect output limited to IRC message length.
-                 (end (text-property-not-all beg (point-max)
-                                             'cursor-sensor-functions val)))
-              (save-restriction
-                (when (setq ct (and (erc-fill--refill-message beg end) ts))
-                  (erc-add-timestamp))
-                (when reporter
-                  (cl-incf (aref (cdr reporter) 2) ; max += d_new - d_old
-                           (- (point-max) (point-min) end (- beg))))
-                (set-marker m (goto-char (point-max))))))
-        (when reporter
-          (progress-reporter-update reporter (point)))
-        (thread-yield)))))
-
-(defvar-local erc-fill--refill-thread nil
-  "A thread running a buffer-refill job.")
+        (buffer-undo-list t)
+        (inhibit-point-motion-hooks t))
+    (while
+        (save-excursion
+          (goto-char (or (marker-position m) (set-marker m (point-min))))
+          (when-let*
+              ((beg (if (get-text-property (point) 'cursor-sensor-functions)
+                        (point)
+                      (when-let*
+                          ((max (min (point-max) (+ 512 (point))))
+                           (res (next-single-property-change
+                                 (point) 'cursor-sensor-functions nil max))
+                           ((/= res max))) ; otherwise, we're done.
+                        res)))
+               (val (get-text-property beg 'cursor-sensor-functions))
+               (ts (get-text-property beg 'erc-timestamp))
+               (beg (progn ; remove left padding, if any.
+                      (goto-char beg)
+                      (skip-syntax-forward "-")
+                      (delete-region (min (line-beginning-position) beg)
+                                     (point))
+                      (point)))
+               ;; Don't expect output limited to IRC message length.
+               (end (text-property-not-all beg (point-max)
+                                           'cursor-sensor-functions val)))
+            (save-restriction
+              (narrow-to-region beg end)
+              (let ((bs (buffer-string))
+                    (erc-fill--refilling ts))
+                (condition-case err
+                    (when (erc-fill--refill-message)
+                      (erc-add-timestamp))
+                  (error
+                   (delete-region (point-min) (point-max))
+                   (insert bs)
+                   (signal (car err) (cdr err)))))
+              ;; FIXME sometimes off by 1 (doesn't reach 100%); probably just
+              ;; needs final report after while loop
+              (when reporter
+                (cl-incf (aref (cdr reporter) 2) ; max += d_new - d_old
+                         (- (point-max) (point-min) end (- beg))))
+              (set-marker m (goto-char (point-max))))))
+      (when reporter
+        (progress-reporter-update reporter (point)))
+      (thread-yield)))
+  (setq erc-fill--refill-thread nil))
 
 (define-error 'erc-fill-canceled "ERC refill canceled" 'error)
 
@@ -219,7 +228,9 @@ erc-fill-buffer
         (thread-signal erc-fill--refill-thread
                        'erc-fill-canceled (list (buffer-name)))
       (user-error "Already refilling.")))
-  (setq erc-fill--refill-thread (make-thread #'erc-fill--refill "erc-fill")))
+  (setq erc-fill--refill-thread
+        (make-thread #'erc-fill--refill
+                     (format "erc-fill[%f]" (erc-current-time)))))
 
 ;;;###autoload
 (defun erc-fill ()
@@ -249,7 +260,8 @@ erc-fill-static
                                          (length nick) 1))
                                32))
           (erc-fill-regarding-timestamp))
-        (erc-restore-text-properties))))
+        (unless erc-fill--refilling
+          (erc-restore-text-properties)))))
 
 (defun erc-fill-variable ()
   "Fill from `point-min' to `point-max'."
@@ -274,7 +286,8 @@ erc-fill-variable
                                                   fill-column))
                                          32)))
           (erc-fill-regarding-timestamp))))
-    (erc-restore-text-properties)))
+    (unless erc-fill--refilling
+      (erc-restore-text-properties))))
 
 (defun erc-fill-regarding-timestamp ()
   "Fills a text such that messages start at column `erc-fill-static-center'."
diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el
index 1ef791c78b..9aed20a1a9 100644
--- a/lisp/erc/erc-stamp.el
+++ b/lisp/erc/erc-stamp.el
@@ -157,17 +157,25 @@ stamp
    (remove-hook 'erc-insert-modify-hook #'erc-add-timestamp)
    (remove-hook 'erc-send-modify-hook #'erc-add-timestamp)))
 
+(cl-defgeneric erc-stamp--current-time ()
+  "Return a lisp time object to associate with an IRC message.
+This becomes the message's `erc-timestamp' text property, which may not
+be unique."
+  (current-time))
+
 (defun erc-add-timestamp ()
   "Add timestamp and text-properties to message.
 
 This function is meant to be called from `erc-insert-modify-hook'
 or `erc-send-modify-hook'."
   (unless (get-text-property (point) 'invisible)
-    (let ((ct (current-time)))
-      (if (fboundp erc-insert-timestamp-function)
-	  (funcall erc-insert-timestamp-function
-		   (erc-format-timestamp ct erc-timestamp-format))
-	(error "Timestamp function unbound"))
+    (let ((ct (erc-stamp--current-time)))
+      (funcall erc-insert-timestamp-function
+               ;; HACK unpaint ourselves from an unfriendly corner
+               (if (eq erc-insert-timestamp-function
+                       #'erc-insert-timestamp-left-and-right)
+                   ct
+                 (erc-format-timestamp ct erc-timestamp-format)))
       (when (and (fboundp erc-insert-away-timestamp-function)
 		 erc-away-timestamp-format
 		 (erc-away-time)
@@ -316,14 +324,20 @@ erc-insert-timestamp-right
       (when erc-timestamp-intangible
 	(erc-put-text-property from (1+ (point)) 'cursor-intangible t)))))
 
-(defun erc-insert-timestamp-left-and-right (_string)
+(defun erc-insert-timestamp-left-and-right (ct)
   "This is another function that can be used with `erc-insert-timestamp-function'.
 If the date is changed, it will print a blank line, the date, and
 another blank line.  If the time is changed, it will then print
-it off to the right."
-  (let* ((ct (current-time))
-	 (ts-left (erc-format-timestamp ct erc-timestamp-format-left))
-	 (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
+it off to the right.
+
+As has always been the case, this function differs from the other
+`erc-insert-timestamp-function' variants in that it ignores its only
+argument.  For practical reasons, this may not always be true when used
+internally."
+  (unless (consp ct)
+    (setq ct (erc-stamp--current-time)))
+  (let ((ts-left (erc-format-timestamp ct erc-timestamp-format-left))
+        (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
     ;; insert left timestamp
     (unless (string-equal ts-left erc-timestamp-last-inserted-left)
       (goto-char (point-min))
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests.el
index a0b695a6c7..ecd746196c 100644
--- a/test/lisp/erc/erc-fill-tests.el
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -111,22 +111,14 @@ erc-fill-tests--compare
 
 (defun erc-fill-tests--await-fill ()
   (call-interactively #'erc-fill-buffer)
-  ;; This timeout silliness seemed a little more realistic than just:
-  ;;
-  ;;   (thread-join erc-fill--refill-thread)
-  ;;
-  ;; Probably dumb, right?.
-  (with-timeout (3 (error "Failed"))
-    (while (thread-live-p erc-fill--refill-thread)
-      (sleep-for 0.01))))
+  (thread-join erc-fill--refill-thread))
 
 (ert-deftest erc-fill-buffer ()
-  (let* (erc-insert-pre-hook
-         erc-insert-modify-hook
-         erc-send-modify-hook
-         erc-mode-hook
-         erc-stamp-mode
-         erc-fill--refill-thread)
+  (let (erc-insert-pre-hook
+        erc-insert-modify-hook
+        erc-send-modify-hook
+        erc-mode-hook
+        erc-stamp-mode)
 
     (erc-fill-tests--setup)
 
@@ -168,12 +160,11 @@ erc-fill-buffer
     (erc-fill-tests--teardown)))
 
 (ert-deftest erc-fill-buffer--interrupted ()
-  (let* (erc-insert-pre-hook
-         erc-insert-modify-hook
-         erc-send-modify-hook
-         erc-mode-hook
-         erc-stamp-mode
-         erc-fill--refill-thread)
+  (let (erc-insert-pre-hook
+        erc-insert-modify-hook
+        erc-send-modify-hook
+        erc-mode-hook
+        erc-stamp-mode)
 
     (erc-fill-tests--setup)
 
@@ -185,20 +176,21 @@ erc-fill-buffer--interrupted
       (ert-info ("Baseline")
         (should (erc-fill-tests--compare "variable-60.buffer")))
 
-      (ert-info ("Denied")
+      (ert-info ("Denied while previous job in progress")
         (setq erc-fill-column 72)
-        (call-interactively #'erc-fill-buffer)
-        (should-error (erc-fill-buffer nil))
-        (thread-join erc-fill--refill-thread)
+        (erc-fill-tests--await-fill)
         (should (erc-fill-tests--compare "variable-72.buffer")))
 
-      (ert-info ("Canceled")
+      (ert-info ("Override switch cancels ongoing job")
         (setq erc-fill-column 60)
         (call-interactively #'erc-fill-buffer)
         (sleep-for (cl-random 0.1))
         (erc-fill-buffer t)
         (thread-join erc-fill--refill-thread)
-        (should (erc-fill-tests--compare "variable-60.buffer")))))
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Thread variable cleared")
+        (should-not erc-fill--refill-thread))))
 
   (when noninteractive
     (erc-fill-tests--teardown)))
-- 
2.31.1


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Remove-timestamp-from-erc-stamp-sensor-function.patch

From ebae073445d67c0570137f8b8ba972faa4f60538 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Wed, 24 Nov 2021 03:10:20 -0800
Subject: [PATCH 1/3] Remove timestamp from erc-stamp sensor function

* lisp/erc/erc-stamp.el (erc-add-timestamp): Add new text property
called `erc-timestamp' to store lisp time object formerly ensconced in
closure.
(erc-echo-timestamp): Check text property for timestamp when not
provided as second argument, which is now optional.
---
 lisp/erc/erc-stamp.el | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el
index 7d31bc971e..1ef791c78b 100644
--- a/lisp/erc/erc-stamp.el
+++ b/lisp/erc/erc-stamp.el
@@ -179,7 +179,8 @@ erc-add-timestamp
 			   ;; be different on different entries (bug#22700).
 			   (list 'cursor-sensor-functions
 				 (list (lambda (_window _before dir)
-					 (erc-echo-timestamp dir ct))))))))
+                                        (erc-echo-timestamp dir)))
+                                  'erc-timestamp ct)))))
 
 (defvar-local erc-timestamp-last-window-width nil
   "The width of the last window that showed the current buffer.
@@ -398,10 +399,10 @@ erc-toggle-timestamps
 	    (erc-munge-invisibility-spec)))
 	(erc-buffer-list)))
 
-(defun erc-echo-timestamp (dir stamp)
+(defun erc-echo-timestamp (dir &optional stamp)
   "Print timestamp text-property of an IRC message."
   (when (and erc-echo-timestamps (eq 'entered dir))
-    (when stamp
+    (when (or stamp (setq stamp (get-text-property (point) 'erc-timestamp)))
       (message "%s" (format-time-string erc-echo-timestamp-format
 					stamp)))))
 
-- 
2.31.1


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0002-Make-some-erc-stamp-functions-more-limber.patch

From 9a49b4ef69fa34d7e877a5fb1d2523c3769434ea Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Wed, 24 Nov 2021 03:35:35 -0800
Subject: [PATCH 2/3] Make some erc-stamp functions more limber

* lisp/erc/erc-stamp.el (erc-stamp-current-time): Add new function
to return current time.  Default to calling `current-time'.
(erc-add-timestamp): Employ ugly hack to pass current time instead of
formatted timestamp to `erc-insert-timestamp-left-and-right' when it's
the value of `erc-insert-timestamp-function'.
(erc-insert-timestamp-left-and-right): Accept a lisp timestamp as
returned by `current-time' for formerly unused string param.
---
 lisp/erc/erc-stamp.el | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el
index 1ef791c78b..9aed20a1a9 100644
--- a/lisp/erc/erc-stamp.el
+++ b/lisp/erc/erc-stamp.el
@@ -157,17 +157,25 @@ stamp
    (remove-hook 'erc-insert-modify-hook #'erc-add-timestamp)
    (remove-hook 'erc-send-modify-hook #'erc-add-timestamp)))
 
+(cl-defgeneric erc-stamp--current-time ()
+  "Return a lisp time object to associate with an IRC message.
+This becomes the message's `erc-timestamp' text property, which may not
+be unique."
+  (current-time))
+
 (defun erc-add-timestamp ()
   "Add timestamp and text-properties to message.
 
 This function is meant to be called from `erc-insert-modify-hook'
 or `erc-send-modify-hook'."
   (unless (get-text-property (point) 'invisible)
-    (let ((ct (current-time)))
-      (if (fboundp erc-insert-timestamp-function)
-	  (funcall erc-insert-timestamp-function
-		   (erc-format-timestamp ct erc-timestamp-format))
-	(error "Timestamp function unbound"))
+    (let ((ct (erc-stamp--current-time)))
+      (funcall erc-insert-timestamp-function
+               ;; HACK unpaint ourselves from an unfriendly corner
+               (if (eq erc-insert-timestamp-function
+                       #'erc-insert-timestamp-left-and-right)
+                   ct
+                 (erc-format-timestamp ct erc-timestamp-format)))
       (when (and (fboundp erc-insert-away-timestamp-function)
 		 erc-away-timestamp-format
 		 (erc-away-time)
@@ -316,14 +324,20 @@ erc-insert-timestamp-right
       (when erc-timestamp-intangible
 	(erc-put-text-property from (1+ (point)) 'cursor-intangible t)))))
 
-(defun erc-insert-timestamp-left-and-right (_string)
+(defun erc-insert-timestamp-left-and-right (ct)
   "This is another function that can be used with `erc-insert-timestamp-function'.
 If the date is changed, it will print a blank line, the date, and
 another blank line.  If the time is changed, it will then print
-it off to the right."
-  (let* ((ct (current-time))
-	 (ts-left (erc-format-timestamp ct erc-timestamp-format-left))
-	 (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
+it off to the right.
+
+As has always been the case, this function differs from the other
+`erc-insert-timestamp-function' variants in that it ignores its only
+argument.  For practical reasons, this may not always be true when used
+internally."
+  (unless (consp ct)
+    (setq ct (erc-stamp--current-time)))
+  (let ((ts-left (erc-format-timestamp ct erc-timestamp-format-left))
+        (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
     ;; insert left timestamp
     (unless (string-equal ts-left erc-timestamp-last-inserted-left)
       (goto-char (point-min))
-- 
2.31.1


--=-=-=
Content-Type: text/x-patch; charset=utf-8
Content-Disposition: attachment;
 filename=0003-Add-command-to-refill-ERC-buffers.patch
Content-Transfer-Encoding: quoted-printable

From d5e69f8ec65105d19bf46490611b0b6becefbd85 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Tue, 16 Nov 2021 06:28:25 -0800
Subject: [PATCH 3/3] Add command to refill ERC buffers

* lisp/erc/erc-fill.el (erc-fill-buffer, erc-fill--refill,
erc-fill--refill-thread, erc-fill--refill-message,
erc-fill--hack-csf): Add new command and helpers to refill ERC
buffers.
(erc-fill--refilling, erc-fill-static, erc-fill-variable): Add new
variable `erc-fill-refilling' telling fill functions not to run
`erc-restore-text-properties'.

* lisp/erc/erc-fill-tests.el: Add new file containing tests for
`erc-fill-buffer'. Add some support files to test against in
lisp/erc/erc-fill-resources.
---
 lisp/erc/erc-fill.el                          | 126 ++++++++++-
 .../erc/erc-fill-resources/static-60.buffer   |  24 +++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 198 ++++++++++++++++++
 6 files changed, 402 insertions(+), 2 deletions(-)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 9f29b9dad9..b3f650bc92 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,126 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
=20
+(defun erc-fill--refill-message ()
+  "Refill but don't re-stamp accessible portion of current buffer.
+Return non-nil if timestamps were removed."
+  (let (left-changed right-changed)
+    ;; Remove at most one left timestamp, if any.
+    (goto-char (point-min))
+    (setq left-changed
+          ;; FIXME it may be a mistake to blow past leading whitespace
+          ;; without removing any intervening ws-only field intervals
+          (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (poi=
nt)))
+                      (nextf (when (eq 'erc-timestamp (field-at-pos beg))
+                               (field-beginning beg t)))
+                      ((eq 'erc-timestamp (get-text-property nextf 'field)=
)))
+            (goto-char (field-end nextf t))
+            (skip-syntax-forward "-")
+            (delete-region nextf (point))
+            t))
+    ;; Get everything on one line (if NOSQUEEZE seems warranted, see
+    ;; note below re ASCII art).
+    (let ((fill-column (string-width (buffer-string))))
+      (fill-region (point-min) (point-max)))
+    ;; Remove all right stamps, if any.
+    (goto-char (point-min))
+    (setq right-changed
+          (when-let* ((nextf (next-single-property-change (point) 'field)))
+            (delete-region nextf (1- (point-max)))
+            t))
+    (erc-fill)
+    ;; Remove trailing whitespace from last line, if any.
+    (goto-char (point-max))
+    (forward-line -1)
+    (when (re-search-forward "\\s-$" (line-end-position) t)
+      (delete-region (match-beginning 0) (match-end 0)))
+    ;; Neuter timestamp caching to force insertion.
+    (when (or left-changed right-changed)
+      (when left-changed
+        (setq erc-timestamp-last-inserted-left nil))
+      (when right-changed
+        (setq erc-timestamp-last-inserted-right nil))
+      t)))
+
+(defvar erc-fill--refilling nil
+  "Non-nil when refilling.") ; Otherwise nil during normal response handli=
ng
+
+(defvar-local erc-fill--refill-thread nil
+  "A thread running a buffer-refill job.")
+
+(cl-defmethod erc-stamp--current-time (&context (erc-fill--refilling cons))
+  erc-fill--refilling)
+
+;; TODO make `erc-fill-mode' respect preformatted text.  Currently, diagra=
ms
+;; and art (like figlets) meant to span multiple messages get ruined.
+(defun erc-fill--refill ()
+  (let ((m (make-marker))
+        (reporter (unless noninteractive
+                    (make-progress-reporter "filling" 0 (point-max))))
+        (inhibit-read-only t)
+        (buffer-undo-list t)
+        (inhibit-point-motion-hooks t))
+    (while
+        (save-excursion
+          (goto-char (or (marker-position m) (set-marker m (point-min))))
+          (when-let*
+              ((beg (if (get-text-property (point) 'cursor-sensor-function=
s)
+                        (point)
+                      (when-let*
+                          ((max (min (point-max) (+ 512 (point))))
+                           (res (next-single-property-change
+                                 (point) 'cursor-sensor-functions nil max))
+                           ((/=3D res max))) ; otherwise, we're done.
+                        res)))
+               (val (get-text-property beg 'cursor-sensor-functions))
+               (ts (get-text-property beg 'erc-timestamp))
+               (beg (progn ; remove left padding, if any.
+                      (goto-char beg)
+                      (skip-syntax-forward "-")
+                      (delete-region (min (line-beginning-position) beg)
+                                     (point))
+                      (point)))
+               ;; Don't expect output limited to IRC message length.
+               (end (text-property-not-all beg (point-max)
+                                           'cursor-sensor-functions val)))
+            (save-restriction
+              (narrow-to-region beg end)
+              (let ((bs (buffer-string))
+                    (erc-fill--refilling ts))
+                (condition-case err
+                    (when (erc-fill--refill-message)
+                      (erc-add-timestamp))
+                  (error
+                   (delete-region (point-min) (point-max))
+                   (insert bs)
+                   (signal (car err) (cdr err)))))
+              ;; FIXME sometimes off by 1 (doesn't reach 100%); probably j=
ust
+              ;; needs final report after while loop
+              (when reporter
+                (cl-incf (aref (cdr reporter) 2) ; max +=3D d_new - d_old
+                         (- (point-max) (point-min) end (- beg))))
+              (set-marker m (goto-char (point-max))))))
+      (when reporter
+        (progress-reporter-update reporter (point)))
+      (thread-yield)))
+  (setq erc-fill--refill-thread nil))
+
+(define-error 'erc-fill-canceled "ERC refill canceled" 'error)
+
+(defun erc-fill-buffer (force)
+  "Refill an ERC buffer.
+With FORCE, cancel an active refill job if one exists."
+  (interactive "P")
+  (when (and erc-fill--refill-thread
+             (thread-live-p erc-fill--refill-thread))
+    (if force
+        (thread-signal erc-fill--refill-thread
+                       'erc-fill-canceled (list (buffer-name)))
+      (user-error "Already refilling.")))
+  (setq erc-fill--refill-thread
+        (make-thread #'erc-fill--refill
+                     (format "erc-fill[%f]" (erc-current-time)))))
+
 ;;;###autoload
 (defun erc-fill ()
   "Fill a region using the function referenced in `erc-fill-function'.
@@ -140,7 +260,8 @@ erc-fill-static
                                          (length nick) 1))
                                32))
           (erc-fill-regarding-timestamp))
-        (erc-restore-text-properties))))
+        (unless erc-fill--refilling
+          (erc-restore-text-properties)))))
=20
 (defun erc-fill-variable ()
   "Fill from `point-min' to `point-max'."
@@ -165,7 +286,8 @@ erc-fill-variable
                                                   fill-column))
                                          32)))
           (erc-fill-regarding-timestamp))))
-    (erc-restore-text-properties)))
+    (unless erc-fill--refilling
+      (erc-restore-text-properties))))
=20
 (defun erc-fill-regarding-timestamp ()
   "Fills a text such that messages start at column `erc-fill-static-center=
'."
diff --git a/test/lisp/erc/erc-fill-resources/static-60.buffer b/test/lisp/=
erc/erc-fill-resources/static-60.buffer
new file mode 100644
index 0000000000..f8db4bf7f4
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-60.buffer
@@ -0,0 +1,24 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot
+                           tester                     [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04
+                           05:06:19
+                     <bob> lorem ipsum This buffer is for
+                           text that is not saved, and for
+                           Lisp evaluation.           [00:01]
+                   <alice> tester, welcome! Your name may or
+                           may not be highlighted depending
+                           on whether erc-button's been
+                           enabled by an earlier test. ERC
+                           needs help!                [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a
+                           file, visit it with ? and enter
+                           text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/static-72.buffer b/test/lisp/=
erc/erc-fill-resources/static-72.buffer
new file mode 100644
index 0000000000..6523f0887e
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-72.buffer
@@ -0,0 +1,20 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot tester
+                                                                  [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04 05:06:19
+                     <bob> lorem ipsum This buffer is for text that is
+                           not saved, and for Lisp evaluation.    [00:01]
+                   <alice> tester, welcome! Your name may or may not be
+                           highlighted depending on whether erc-button's
+                           been enabled by an earlier test. ERC needs
+                           help!                                  [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a file, visit it
+                           with ? and enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-60.buffer b/test/lis=
p/erc/erc-fill-resources/variable-60.buffer
new file mode 100644
index 0000000000..38723209bf
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-60.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester           [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved,
+      and for Lisp evaluation.                        [00:01]
+<alice> tester, welcome! Your name may or may not be
+        highlighted depending on whether erc-button's been
+        enabled by an earlier test. ERC needs help!   [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and
+      enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-72.buffer b/test/lis=
p/erc/erc-fill-resources/variable-72.buffer
new file mode 100644
index 0000000000..cc2410d7a7
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-72.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester                       [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved, and for
+      Lisp evaluation.                                            [00:01]
+<alice> tester, welcome! Your name may or may not be highlighted
+        depending on whether erc-button's been enabled by an earlier
+        test. ERC needs help!                                     [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and enter text
+      in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests=
.el
new file mode 100644
index 0000000000..ecd746196c
--- /dev/null
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -0,0 +1,198 @@
+;;; erc-fill-tests.el --- ERC message filling -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; GNU Emacs is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert-x)
+(require 'erc-fill)
+
+(defun erc-fill-tests--insert (&rest strings)
+  (let ((inhibit-read-only t))
+    (erc-parse-server-response erc-server-process (apply #'concat strings)=
)))
+
+(defun erc-fill-tests--setup-server-buffer ()
+  (with-current-buffer (get-buffer-create "foonet")
+    (erc-mode)
+    (setq erc-server-process (start-process "true" (current-buffer) "true")
+          erc-server-current-nick "tester"
+          erc-server-users (make-hash-table :test #'equal))
+    (set-process-query-on-exit-flag erc-server-process nil)))
+
+(defun erc-fill-tests--setup-channel-buffer ()
+  (with-current-buffer (get-buffer-create "#chan")
+    (erc-mode)
+    (insert "\n\n")
+    (setq erc-input-marker (make-marker)
+          ;; Kludge to get around saving display prop
+          erc-timestamp-use-align-to nil
+          ;; Kludge to make whitespace compare equal without expanding
+          indent-tabs-mode nil
+          erc-insert-marker (make-marker)
+          erc-default-recipients '("#chan")
+          erc-channel-users (make-hash-table :test #'equal)
+          erc-server-process (with-current-buffer "foonet"
+                               erc-server-process))
+    (set-marker erc-insert-marker (point-max))
+    (erc-display-prompt)))
+
+(defun erc-fill-tests--setup ()
+  (advice-add 'format-time-string :filter-args
+              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts=
)))
+
+  (erc-stamp-mode +1)
+
+  (erc-fill-tests--setup-server-buffer)
+  (erc-fill-tests--setup-channel-buffer)
+  (erc-fill-tests--populate))
+
+(defun erc-fill-tests--populate ()
+  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10))) ; Jan 1 1980
+         (ct (time-convert ts)))
+
+    (cl-letf (((symbol-function 'current-time) (lambda () ct)))
+      (with-current-buffer "foonet"
+        (erc-fill-tests--insert ":irc.foonet.org 353 tester =3D #chan :"
+                                "alice @bob robot tester")
+        (erc-fill-tests--insert ":irc.foonet.org 366 tester #chan :"
+                                "End of /NAMES list.")
+        (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
+        (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 16201047=
79")
+
+        (setq ct (time-convert (cl-incf ts 60)))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :lorem ipsum"
+         " This buffer is for text that is not saved, and for Lisp evaluat=
ion.")
+
+        (setq ct (time-convert (cl-incf ts 120)))
+        (erc-fill-tests--insert
+         ":alice!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " Your name may or may not be highlighted depending on whether"
+         " erc-button's been enabled by an earlier test. ERC needs help!")
+
+        (erc-fill-tests--insert
+         ":robot!~u@HIDDEN PRIVMSG #chan :=E3=83=BB=E3=82=9C=E3=
=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\\_o< QUACK!")
+
+        (setq ct (time-convert (cl-incf ts (* 60 60 24))))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " To create a file, visit it with ? and enter text in its buffer.=
")))))
+
+(defun erc-fill-tests--teardown ()
+  (advice-remove 'format-time-string 'ts)
+  (let (erc-kill-server-hook
+        erc-kill-channel-hook)
+    (kill-buffer "#chan")
+    (kill-buffer "foonet"))
+  (should (=3D erc-fill-column 78)))
+
+(defun erc-fill-tests--compare (name)
+  ;; Git didn't allow committing with a trailing space after the
+  ;; prompt, hence this:
+  (equal (substring-no-properties (buffer-string) 0 -1)
+         (with-temp-buffer
+           (insert-file-contents (ert-resource-file name))
+           (buffer-string))))
+
+(defun erc-fill-tests--await-fill ()
+  (call-interactively #'erc-fill-buffer)
+  (thread-join erc-fill--refill-thread))
+
+(ert-deftest erc-fill-buffer ()
+  (let (erc-insert-pre-hook
+        erc-insert-modify-hook
+        erc-send-modify-hook
+        erc-mode-hook
+        erc-stamp-mode)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      ;; These would get clobbered by the new thread if we let-bound
+      ;; them, and we can't set them globally, so best just fake it:
+      (setq-local erc-fill-mode t
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Wider")
+        (setq erc-fill-column 72)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Fancy")
+        (setq erc-fill-function #'erc-fill-static)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-72.buffer")))
+
+      (ert-info ("Fancy normal")
+        (setq erc-fill-column 60)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Again!")
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Back home")
+        (setq erc-fill-function #'erc-fill-variable)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+(ert-deftest erc-fill-buffer--interrupted ()
+  (let (erc-insert-pre-hook
+        erc-insert-modify-hook
+        erc-send-modify-hook
+        erc-mode-hook
+        erc-stamp-mode)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      (setq-local erc-fill-mode t ; see note re these in prev test
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Denied while previous job in progress")
+        (setq erc-fill-column 72)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Override switch cancels ongoing job")
+        (setq erc-fill-column 60)
+        (call-interactively #'erc-fill-buffer)
+        (sleep-for (cl-random 0.1))
+        (erc-fill-buffer t)
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Thread variable cleared")
+        (should-not erc-fill--refill-thread))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+;;; erc-fill-tests.el ends here
--=20
2.31.1


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#51969; Package emacs. Full text available.

Message received at 51969 <at> debbugs.gnu.org:


Received: (at 51969) by debbugs.gnu.org; 24 Nov 2021 13:38:42 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Nov 24 08:38:42 2021
Received: from localhost ([127.0.0.1]:53526 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1mpsTx-00089u-4A
	for submit <at> debbugs.gnu.org; Wed, 24 Nov 2021 08:38:42 -0500
Received: from mail-108-mta86.mxroute.com ([136.175.108.86]:42709)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jp@HIDDEN>) id 1mpsTt-00089e-Dl
 for 51969 <at> debbugs.gnu.org; Wed, 24 Nov 2021 08:38:38 -0500
Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com)
 (Authenticated sender: mN4UYu2MZsgR)
 by mail-108-mta86.mxroute.com (ZoneMTA) with ESMTPSA id 17d522b442b000177f.001
 for <51969 <at> debbugs.gnu.org>
 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256);
 Wed, 24 Nov 2021 13:38:26 +0000
X-Zone-Loop: 79cdb914736cbfca4c5a2b729c23519164ba986158e6
X-Originating-IP: [149.28.56.236]
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=neverwas.me
 ; s=x;
 h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References:
 Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID:
 Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc
 :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
 List-Post:List-Owner:List-Archive;
 bh=WSv5ham5tPnGaRgWRFE3tAOHA7zdCGz4MABKZY8jDBs=; b=N2wH/8X/hdVxN3l6DPck4OYYQy
 z+POSFriQXaQMCMlYR+hHJG/3yWWKrE7dbvWxVXPueydS+oxCQXt4OIVmxtUa8MkxdK8MBgXZbcJj
 5ZBLISDHcpKgI4/N8HttbRqfElJtj+J6PfGXt/HzFh7LLsXUtY2pNTfmyHJRDs5iwa/5LMiBuEnRi
 ZMDzAlCjX5SE1W3SvRBrx3G9cOHxGaysHqBx0eC4TzgDQAc/e921kZY2VCaE9W8lz7Jm0y5XbjcV2
 SOgun8e5Sv5HxYephdHj5+0IS20xvqVb6o1pl6Au4Ojup6kg5LBBXHz/AmuOj9+dAX+BFM9Z3P+2F
 oLm0cVHg==;
From: "J.P." <jp@HIDDEN>
To: 51969 <at> debbugs.gnu.org
Subject: Re: bug#51969: 29.0.50; Add command for refilling ERC buffers
References: <87bl2gjuo9.fsf@HIDDEN>
Date: Wed, 24 Nov 2021 05:38:23 -0800
In-Reply-To: <87bl2gjuo9.fsf@HIDDEN> (J. P.'s message of "Fri, 19 Nov
 2021 02:39:50 -0800")
Message-ID: <878rxdad2o.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-AuthUser: masked@HIDDEN
X-Zone-Spam-Resolution: no action
X-Zone-Spam-Status: No, score=-0.1, required=15, tests=[ARC_NA=0,
 RCPT_COUNT_TWO=0, FROM_HAS_DN=0, NEURAL_HAM=0, HAS_ATTACHMENT=0,
 FROM_EQ_ENVFROM=0, MIME_TRACE=0, MIME_GOOD=-0.1, TO_DN_NONE=0,
 MID_RHS_MATCH_FROM=0, RCVD_COUNT_ZERO=0]
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 51969
Cc: emacs-erc@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain

v4. Removed hack to get at stored timestamp.

TODO:
- preserve white space when filling (treat text as preformatted)
- ensure jobs can be canceled with impunity
- try with non-Latin alphabets, uncommon characters, etc.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=0000-v3-v4.diff

From 4929787d009733f77b087676e85b7f65490bbb96 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Wed, 24 Nov 2021 03:20:26 -0800
Subject: NOT A PATCH

F. Jason Park (2):
  Remove timestamp from erc-stamp sensor function
  Add command to refill ERC buffers

 lisp/erc/erc-fill.el                          | 109 +++++++++
 lisp/erc/erc-stamp.el                         |   7 +-
 .../erc/erc-fill-resources/static-60.buffer   |  24 ++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 206 ++++++++++++++++++
 7 files changed, 399 insertions(+), 3 deletions(-)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

Interdiff:
diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 49130b9ffc..f9f8f8ad5d 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,10 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
 
+;; If there's a chance of a job's cancellation leaving things in a bad
+;; state (like with stamps removed and yet to be replaced), this
+;; function should be protected by a condition-case so the narrowed
+;; buffer's contents can be restored and the signal repropagated.
 (defun erc-fill--refill-message (beg end)
   "Refill but don't re-stamp region between BEG and END.
 Return non-nil if timestamps were removed."
@@ -154,24 +158,8 @@ erc-fill--refill-message
         (setq erc-timestamp-last-inserted-right nil))
       t)))
 
-(defun erc-fill--hack-csf (f)
-  ;; HACK until necessary additions to erc-stamp.el arrive (possibly
-  ;; with erc-v3 in #49860), there's no civilized way of detecting the
-  ;; bounds of a displayed message after initial insertion.
-  ;;
-  ;; These callback closures are used for that purpose, but they also
-  ;; contain the timestamp we need.  An unforeseen benefit of this
-  ;; awkwardness is that it plays well with `text-property-not-all',
-  ;; which needs unique values to match against.  That wouldn't be the
-  ;; case were we to use lisp time objects instead because successive
-  ;; messages might contain the exact same one.
-  (if (byte-code-function-p f) (aref (aref f 2) 0) (alist-get 'ct (cadr f))))
-
-;; Enabling `erc-fill-mode' is ultimately destructive to preformatted
-;; text (like ASCII art and figlets), which degenerate immediately
-;; upon display.  This is permanent because we don't store original
-;; messages (though with IRCv3, it may be possible to request a
-;; replacement from the server).
+;; TODO make `erc-fill-mode' respect preformatted text.  Currently, diagrams
+;; and art (like figlets) meant to span multiple messages get ruined.
 (defun erc-fill--refill ()
   (let ((m (make-marker))
         (reporter (unless noninteractive
@@ -195,6 +183,7 @@ erc-fill--refill
                              ((/= res max))) ; otherwise, we're done.
                           res)))
                  (val (get-text-property beg 'cursor-sensor-functions))
+                 (ts (get-text-property beg 'erc-timestamp))
                  (beg (progn ; remove left padding, if any.
                         (goto-char beg)
                         (skip-syntax-forward "-")
@@ -205,8 +194,7 @@ erc-fill--refill
                  (end (text-property-not-all beg (point-max)
                                              'cursor-sensor-functions val)))
               (save-restriction
-                (when (setq ct (and (erc-fill--refill-message beg end)
-                                    (erc-fill--hack-csf (car val))))
+                (when (setq ct (and (erc-fill--refill-message beg end) ts))
                   (erc-add-timestamp))
                 (when reporter
                   (cl-incf (aref (cdr reporter) 2) ; max += d_new - d_old
diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el
index 7d31bc971e..1ef791c78b 100644
--- a/lisp/erc/erc-stamp.el
+++ b/lisp/erc/erc-stamp.el
@@ -179,7 +179,8 @@ erc-add-timestamp
 			   ;; be different on different entries (bug#22700).
 			   (list 'cursor-sensor-functions
 				 (list (lambda (_window _before dir)
-					 (erc-echo-timestamp dir ct))))))))
+                                        (erc-echo-timestamp dir)))
+                                  'erc-timestamp ct)))))
 
 (defvar-local erc-timestamp-last-window-width nil
   "The width of the last window that showed the current buffer.
@@ -398,10 +399,10 @@ erc-toggle-timestamps
 	    (erc-munge-invisibility-spec)))
 	(erc-buffer-list)))
 
-(defun erc-echo-timestamp (dir stamp)
+(defun erc-echo-timestamp (dir &optional stamp)
   "Print timestamp text-property of an IRC message."
   (when (and erc-echo-timestamps (eq 'entered dir))
-    (when stamp
+    (when (or stamp (setq stamp (get-text-property (point) 'erc-timestamp)))
       (message "%s" (format-time-string erc-echo-timestamp-format
 					stamp)))))
 
-- 
2.31.1


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Remove-timestamp-from-erc-stamp-sensor-function.patch

From 61e0af7a032aebdc930a3b3df73818506ad4eccb Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Wed, 24 Nov 2021 03:10:20 -0800
Subject: [PATCH 1/2] Remove timestamp from erc-stamp sensor function

* lisp/erc/erc-stamp.el (erc-add-timestamp): Add new text property
called `erc-timestamp' to store lisp time object formerly ensconced in
closure.
(erc-echo-timestamp): Check text property for timestamp when not
provided as second argument, which is now optional.
---
 lisp/erc/erc-stamp.el | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lisp/erc/erc-stamp.el b/lisp/erc/erc-stamp.el
index 7d31bc971e..1ef791c78b 100644
--- a/lisp/erc/erc-stamp.el
+++ b/lisp/erc/erc-stamp.el
@@ -179,7 +179,8 @@ erc-add-timestamp
 			   ;; be different on different entries (bug#22700).
 			   (list 'cursor-sensor-functions
 				 (list (lambda (_window _before dir)
-					 (erc-echo-timestamp dir ct))))))))
+                                        (erc-echo-timestamp dir)))
+                                  'erc-timestamp ct)))))
 
 (defvar-local erc-timestamp-last-window-width nil
   "The width of the last window that showed the current buffer.
@@ -398,10 +399,10 @@ erc-toggle-timestamps
 	    (erc-munge-invisibility-spec)))
 	(erc-buffer-list)))
 
-(defun erc-echo-timestamp (dir stamp)
+(defun erc-echo-timestamp (dir &optional stamp)
   "Print timestamp text-property of an IRC message."
   (when (and erc-echo-timestamps (eq 'entered dir))
-    (when stamp
+    (when (or stamp (setq stamp (get-text-property (point) 'erc-timestamp)))
       (message "%s" (format-time-string erc-echo-timestamp-format
 					stamp)))))
 
-- 
2.31.1


--=-=-=
Content-Type: text/x-patch; charset=utf-8
Content-Disposition: attachment;
 filename=0002-Add-command-to-refill-ERC-buffers.patch
Content-Transfer-Encoding: quoted-printable

From 4929787d009733f77b087676e85b7f65490bbb96 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Tue, 16 Nov 2021 06:28:25 -0800
Subject: [PATCH 2/2] Add command to refill ERC buffers

* lisp/erc/erc-fill.el (erc-fill-buffer, erc-fill--refill,
erc-fill--refill-thread, erc-fill--refill-message,
erc-fill--hack-csf): Add new command and helpers to refill ERC
buffers.

* lisp/erc/erc-fill-tests.el: Add new file containing tests for
`erc-fill-buffer'. Add some support files to test against in
lisp/erc/erc-fill-resources.
---
 lisp/erc/erc-fill.el                          | 109 +++++++++
 .../erc/erc-fill-resources/static-60.buffer   |  24 ++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 206 ++++++++++++++++++
 6 files changed, 395 insertions(+)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 9f29b9dad9..f9f8f8ad5d 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,115 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
=20
+;; If there's a chance of a job's cancellation leaving things in a bad
+;; state (like with stamps removed and yet to be replaced), this
+;; function should be protected by a condition-case so the narrowed
+;; buffer's contents can be restored and the signal repropagated.
+(defun erc-fill--refill-message (beg end)
+  "Refill but don't re-stamp region between BEG and END.
+Return non-nil if timestamps were removed."
+  (let (left-changed right-changed)
+    (narrow-to-region beg end)
+    ;; Remove at most one left timestamp, if any.
+    (goto-char (point-min))
+    (setq left-changed
+          ;; FIXME it may be a mistake to blow past leading whitespace
+          ;; without removing any intervening ws-only field intervals
+          (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (poi=
nt)))
+                      (nextf (when (eq 'erc-timestamp (field-at-pos beg))
+                               (field-beginning beg t)))
+                      ((eq 'erc-timestamp (get-text-property nextf 'field)=
)))
+            (goto-char (field-end nextf t))
+            (skip-syntax-forward "-")
+            (delete-region nextf (point))
+            t))
+    ;; Get everything on one line (if NOSQUEEZE seems warranted, see
+    ;; note below re ASCII art).
+    (let ((fill-column (string-width (buffer-string))))
+      (fill-region (point-min) (point-max)))
+    ;; Remove any stamps from right-hand side.
+    (goto-char (point-min))
+    (setq right-changed
+          (when-let* ((nextf (next-single-property-change (point) 'field)))
+            (delete-region nextf (1- (point-max)))
+            t))
+    (erc-fill)
+    ;; Remove trailing whitespace from last line, if any.
+    (goto-char (point-max))
+    (forward-line -1)
+    (when (re-search-forward "\\s-$" (line-end-position) t)
+      (delete-region (match-beginning 0) (match-end 0)))
+    ;; Neuter timestamp caching to force insertion.
+    (when (or left-changed right-changed)
+      (when left-changed
+        (setq erc-timestamp-last-inserted-left nil))
+      (when right-changed
+        (setq erc-timestamp-last-inserted-right nil))
+      t)))
+
+;; TODO make `erc-fill-mode' respect preformatted text.  Currently, diagra=
ms
+;; and art (like figlets) meant to span multiple messages get ruined.
+(defun erc-fill--refill ()
+  (let ((m (make-marker))
+        (reporter (unless noninteractive
+                    (make-progress-reporter "filling" 0 (point-max))))
+        (inhibit-read-only t)
+        (inhibit-point-motion-hooks t)
+        ;;
+        ct) ; cached current time
+    (cl-letf (((symbol-function #'erc-restore-text-properties) #'ignore)
+              ((symbol-function #'current-time) (lambda () ct)))
+      (while
+          (save-excursion
+            (goto-char (or (marker-position m) (set-marker m (point-min))))
+            (when-let*
+                ((beg (if (get-text-property (point) 'cursor-sensor-functi=
ons)
+                          (point)
+                        (when-let*
+                            ((max (min (point-max) (+ 512 (point))))
+                             (res (next-single-property-change
+                                   (point) 'cursor-sensor-functions nil ma=
x))
+                             ((/=3D res max))) ; otherwise, we're done.
+                          res)))
+                 (val (get-text-property beg 'cursor-sensor-functions))
+                 (ts (get-text-property beg 'erc-timestamp))
+                 (beg (progn ; remove left padding, if any.
+                        (goto-char beg)
+                        (skip-syntax-forward "-")
+                        (delete-region (min (line-beginning-position) beg)
+                                       (point))
+                        (point)))
+                 ;; Don't expect output limited to IRC message length.
+                 (end (text-property-not-all beg (point-max)
+                                             'cursor-sensor-functions val)=
))
+              (save-restriction
+                (when (setq ct (and (erc-fill--refill-message beg end) ts))
+                  (erc-add-timestamp))
+                (when reporter
+                  (cl-incf (aref (cdr reporter) 2) ; max +=3D d_new - d_old
+                           (- (point-max) (point-min) end (- beg))))
+                (set-marker m (goto-char (point-max))))))
+        (when reporter
+          (progress-reporter-update reporter (point)))
+        (thread-yield)))))
+
+(defvar-local erc-fill--refill-thread nil
+  "A thread running a buffer-refill job.")
+
+(define-error 'erc-fill-canceled "ERC refill canceled" 'error)
+
+(defun erc-fill-buffer (force)
+  "Refill an ERC buffer.
+With FORCE, cancel an active refill job if one exists."
+  (interactive "P")
+  (when (and erc-fill--refill-thread
+             (thread-live-p erc-fill--refill-thread))
+    (if force
+        (thread-signal erc-fill--refill-thread
+                       'erc-fill-canceled (list (buffer-name)))
+      (user-error "Already refilling.")))
+  (setq erc-fill--refill-thread (make-thread #'erc-fill--refill "erc-fill"=
)))
+
 ;;;###autoload
 (defun erc-fill ()
   "Fill a region using the function referenced in `erc-fill-function'.
diff --git a/test/lisp/erc/erc-fill-resources/static-60.buffer b/test/lisp/=
erc/erc-fill-resources/static-60.buffer
new file mode 100644
index 0000000000..f8db4bf7f4
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-60.buffer
@@ -0,0 +1,24 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot
+                           tester                     [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04
+                           05:06:19
+                     <bob> lorem ipsum This buffer is for
+                           text that is not saved, and for
+                           Lisp evaluation.           [00:01]
+                   <alice> tester, welcome! Your name may or
+                           may not be highlighted depending
+                           on whether erc-button's been
+                           enabled by an earlier test. ERC
+                           needs help!                [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a
+                           file, visit it with ? and enter
+                           text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/static-72.buffer b/test/lisp/=
erc/erc-fill-resources/static-72.buffer
new file mode 100644
index 0000000000..6523f0887e
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-72.buffer
@@ -0,0 +1,20 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot tester
+                                                                  [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04 05:06:19
+                     <bob> lorem ipsum This buffer is for text that is
+                           not saved, and for Lisp evaluation.    [00:01]
+                   <alice> tester, welcome! Your name may or may not be
+                           highlighted depending on whether erc-button's
+                           been enabled by an earlier test. ERC needs
+                           help!                                  [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a file, visit it
+                           with ? and enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-60.buffer b/test/lis=
p/erc/erc-fill-resources/variable-60.buffer
new file mode 100644
index 0000000000..38723209bf
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-60.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester           [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved,
+      and for Lisp evaluation.                        [00:01]
+<alice> tester, welcome! Your name may or may not be
+        highlighted depending on whether erc-button's been
+        enabled by an earlier test. ERC needs help!   [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and
+      enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-72.buffer b/test/lis=
p/erc/erc-fill-resources/variable-72.buffer
new file mode 100644
index 0000000000..cc2410d7a7
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-72.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester                       [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved, and for
+      Lisp evaluation.                                            [00:01]
+<alice> tester, welcome! Your name may or may not be highlighted
+        depending on whether erc-button's been enabled by an earlier
+        test. ERC needs help!                                     [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and enter text
+      in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests=
.el
new file mode 100644
index 0000000000..a0b695a6c7
--- /dev/null
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -0,0 +1,206 @@
+;;; erc-fill-tests.el --- ERC message filling -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; GNU Emacs is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert-x)
+(require 'erc-fill)
+
+(defun erc-fill-tests--insert (&rest strings)
+  (let ((inhibit-read-only t))
+    (erc-parse-server-response erc-server-process (apply #'concat strings)=
)))
+
+(defun erc-fill-tests--setup-server-buffer ()
+  (with-current-buffer (get-buffer-create "foonet")
+    (erc-mode)
+    (setq erc-server-process (start-process "true" (current-buffer) "true")
+          erc-server-current-nick "tester"
+          erc-server-users (make-hash-table :test #'equal))
+    (set-process-query-on-exit-flag erc-server-process nil)))
+
+(defun erc-fill-tests--setup-channel-buffer ()
+  (with-current-buffer (get-buffer-create "#chan")
+    (erc-mode)
+    (insert "\n\n")
+    (setq erc-input-marker (make-marker)
+          ;; Kludge to get around saving display prop
+          erc-timestamp-use-align-to nil
+          ;; Kludge to make whitespace compare equal without expanding
+          indent-tabs-mode nil
+          erc-insert-marker (make-marker)
+          erc-default-recipients '("#chan")
+          erc-channel-users (make-hash-table :test #'equal)
+          erc-server-process (with-current-buffer "foonet"
+                               erc-server-process))
+    (set-marker erc-insert-marker (point-max))
+    (erc-display-prompt)))
+
+(defun erc-fill-tests--setup ()
+  (advice-add 'format-time-string :filter-args
+              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts=
)))
+
+  (erc-stamp-mode +1)
+
+  (erc-fill-tests--setup-server-buffer)
+  (erc-fill-tests--setup-channel-buffer)
+  (erc-fill-tests--populate))
+
+(defun erc-fill-tests--populate ()
+  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10))) ; Jan 1 1980
+         (ct (time-convert ts)))
+
+    (cl-letf (((symbol-function 'current-time) (lambda () ct)))
+      (with-current-buffer "foonet"
+        (erc-fill-tests--insert ":irc.foonet.org 353 tester =3D #chan :"
+                                "alice @bob robot tester")
+        (erc-fill-tests--insert ":irc.foonet.org 366 tester #chan :"
+                                "End of /NAMES list.")
+        (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
+        (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 16201047=
79")
+
+        (setq ct (time-convert (cl-incf ts 60)))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :lorem ipsum"
+         " This buffer is for text that is not saved, and for Lisp evaluat=
ion.")
+
+        (setq ct (time-convert (cl-incf ts 120)))
+        (erc-fill-tests--insert
+         ":alice!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " Your name may or may not be highlighted depending on whether"
+         " erc-button's been enabled by an earlier test. ERC needs help!")
+
+        (erc-fill-tests--insert
+         ":robot!~u@HIDDEN PRIVMSG #chan :=E3=83=BB=E3=82=9C=E3=
=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\\_o< QUACK!")
+
+        (setq ct (time-convert (cl-incf ts (* 60 60 24))))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " To create a file, visit it with ? and enter text in its buffer.=
")))))
+
+(defun erc-fill-tests--teardown ()
+  (advice-remove 'format-time-string 'ts)
+  (let (erc-kill-server-hook
+        erc-kill-channel-hook)
+    (kill-buffer "#chan")
+    (kill-buffer "foonet"))
+  (should (=3D erc-fill-column 78)))
+
+(defun erc-fill-tests--compare (name)
+  ;; Git didn't allow committing with a trailing space after the
+  ;; prompt, hence this:
+  (equal (substring-no-properties (buffer-string) 0 -1)
+         (with-temp-buffer
+           (insert-file-contents (ert-resource-file name))
+           (buffer-string))))
+
+(defun erc-fill-tests--await-fill ()
+  (call-interactively #'erc-fill-buffer)
+  ;; This timeout silliness seemed a little more realistic than just:
+  ;;
+  ;;   (thread-join erc-fill--refill-thread)
+  ;;
+  ;; Probably dumb, right?.
+  (with-timeout (3 (error "Failed"))
+    (while (thread-live-p erc-fill--refill-thread)
+      (sleep-for 0.01))))
+
+(ert-deftest erc-fill-buffer ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      ;; These would get clobbered by the new thread if we let-bound
+      ;; them, and we can't set them globally, so best just fake it:
+      (setq-local erc-fill-mode t
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Wider")
+        (setq erc-fill-column 72)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Fancy")
+        (setq erc-fill-function #'erc-fill-static)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-72.buffer")))
+
+      (ert-info ("Fancy normal")
+        (setq erc-fill-column 60)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Again!")
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Back home")
+        (setq erc-fill-function #'erc-fill-variable)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+(ert-deftest erc-fill-buffer--interrupted ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      (setq-local erc-fill-mode t ; see note re these in prev test
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Denied")
+        (setq erc-fill-column 72)
+        (call-interactively #'erc-fill-buffer)
+        (should-error (erc-fill-buffer nil))
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Canceled")
+        (setq erc-fill-column 60)
+        (call-interactively #'erc-fill-buffer)
+        (sleep-for (cl-random 0.1))
+        (erc-fill-buffer t)
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+;;; erc-fill-tests.el ends here
--=20
2.31.1


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#51969; Package emacs. Full text available.

Message received at 51969 <at> debbugs.gnu.org:


Received: (at 51969) by debbugs.gnu.org; 20 Nov 2021 04:12:32 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Nov 19 23:12:32 2021
Received: from localhost ([127.0.0.1]:41140 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1moHjr-0007DU-BY
	for submit <at> debbugs.gnu.org; Fri, 19 Nov 2021 23:12:32 -0500
Received: from mail-108-mta185.mxroute.com ([136.175.108.185]:40297)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jp@HIDDEN>) id 1moHjn-0007DB-UO
 for 51969 <at> debbugs.gnu.org; Fri, 19 Nov 2021 23:12:29 -0500
Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com)
 (Authenticated sender: mN4UYu2MZsgR)
 by mail-108-mta185.mxroute.com (ZoneMTA) with ESMTPSA id
 17d3b8b7c68000177f.001 for <51969 <at> debbugs.gnu.org>
 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256);
 Sat, 20 Nov 2021 04:12:16 +0000
X-Zone-Loop: 658bda79c441a625b37d7073df79d0a712a78c52e164
X-Originating-IP: [149.28.56.236]
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=neverwas.me
 ; s=x;
 h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References:
 Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID:
 Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc
 :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
 List-Post:List-Owner:List-Archive;
 bh=3uN2FqIuBdjDX/yVf45I/Zioz6NwMMQNw19RV9SlDWY=; b=M181DvC/tvNrMZhvjIPijN8xmZ
 EXsdYdxqYPiX+def/CydonE0VZWSdtAFd9zJt469MmCj4ArrjSFL3sXOPRn8knCc3vb4tIwxkjFfX
 UBZ3F2umshy9+2yrEYhDhhCt/oBk6dzxDKRSpcR37tFQZTaJc9m+38aEKujEe8tRu8c9aiRoKLBqy
 rpRBb+aFTySIcqSri3f0QKp2bjUFCcML646L17WhRRcFhRzo+f6wg5usoSCxUzvdlE4GgQcIJ4qlG
 0DXc3awIfsXJqxIvlGzqHa08OI+8H7V7j1gUFEwGDbCYH3F4qviKGvj3NFIgPADLM8awyQysv9Jua
 nqPAZ95g==;
From: "J.P." <jp@HIDDEN>
To: 51969 <at> debbugs.gnu.org
Subject: Re: bug#51969: 29.0.50; Add command for refilling ERC buffers
References: <87bl2gjuo9.fsf@HIDDEN>
Date: Fri, 19 Nov 2021 20:12:13 -0800
In-Reply-To: <87bl2gjuo9.fsf@HIDDEN> (J. P.'s message of "Fri, 19 Nov
 2021 02:39:50 -0800")
Message-ID: <87fsrrqxcy.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-AuthUser: masked@HIDDEN
X-Zone-Spam-Resolution: no action
X-Zone-Spam-Status: No, score=-0.1, required=15, tests=[ARC_NA=0,
 RCPT_COUNT_TWO=0, FROM_HAS_DN=0, HAS_ATTACHMENT=0, FROM_EQ_ENVFROM=0,
 MIME_TRACE=0, MIME_GOOD=-0.1, TO_DN_NONE=0, MID_RHS_MATCH_FROM=0,
 RCVD_COUNT_ZERO=0, NEURAL_SPAM=0]
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 51969
Cc: emacs-erc@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain

Addressed some erroneous line folding involving wide chars. But bugs
likely remain.


--=-=-=
Content-Type: text/x-patch; charset=utf-8
Content-Disposition: attachment; filename=0000-v2-v3.diff
Content-Transfer-Encoding: quoted-printable

From 1058b9202f9b530062bd5268c81a111976db61f2 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Fri, 19 Nov 2021 19:07:24 -0800
Subject: NOT A PATCH

F. Jason Park (1):
  Add command to refill ERC buffers

 lisp/erc/erc-fill.el                          | 121 ++++++++++
 .../erc/erc-fill-resources/static-60.buffer   |  24 ++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 206 ++++++++++++++++++
 6 files changed, 407 insertions(+)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

Interdiff:
diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 3bf335d098..49130b9ffc 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,30 +112,47 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
=20
-(defun erc-fill--remove-stamp-right ()
-  (goto-char (point-min))
-  (let (changed)
-    (while
-        (when-let* ((nextf (next-single-property-change (point) 'field)))
-          (goto-char (field-end nextf t))
-          ;; Sweep up residual phantom field remants
-          (delete-region nextf (field-end nextf t))
-          (setq changed t)))
-    changed))
-
-(defun erc-fill--remove-stamp-left ()
-  "Remove at most one LEFT or one right timestamp, if any."
-  (goto-char (point-min))
-  ;; FIXME actually, it may be a mistake to blow past white space
-  ;; without checking for intervening intervals that need cleaning up.
-  (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (point)))
-              (nextf (when (eq 'erc-timestamp (field-at-pos beg))
-                       (field-beginning beg t)))
-              ((eq 'erc-timestamp (get-text-property nextf 'field))))
-    (goto-char (field-end nextf t))
-    (skip-syntax-forward "-")
-    (delete-region nextf (point))
-    t))
+(defun erc-fill--refill-message (beg end)
+  "Refill but don't re-stamp region between BEG and END.
+Return non-nil if timestamps were removed."
+  (let (left-changed right-changed)
+    (narrow-to-region beg end)
+    ;; Remove at most one left timestamp, if any.
+    (goto-char (point-min))
+    (setq left-changed
+          ;; FIXME it may be a mistake to blow past leading whitespace
+          ;; without removing any intervening ws-only field intervals
+          (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (poi=
nt)))
+                      (nextf (when (eq 'erc-timestamp (field-at-pos beg))
+                               (field-beginning beg t)))
+                      ((eq 'erc-timestamp (get-text-property nextf 'field)=
)))
+            (goto-char (field-end nextf t))
+            (skip-syntax-forward "-")
+            (delete-region nextf (point))
+            t))
+    ;; Get everything on one line (if NOSQUEEZE seems warranted, see
+    ;; note below re ASCII art).
+    (let ((fill-column (string-width (buffer-string))))
+      (fill-region (point-min) (point-max)))
+    ;; Remove any stamps from right-hand side.
+    (goto-char (point-min))
+    (setq right-changed
+          (when-let* ((nextf (next-single-property-change (point) 'field)))
+            (delete-region nextf (1- (point-max)))
+            t))
+    (erc-fill)
+    ;; Remove trailing whitespace from last line, if any.
+    (goto-char (point-max))
+    (forward-line -1)
+    (when (re-search-forward "\\s-$" (line-end-position) t)
+      (delete-region (match-beginning 0) (match-end 0)))
+    ;; Neuter timestamp caching to force insertion.
+    (when (or left-changed right-changed)
+      (when left-changed
+        (setq erc-timestamp-last-inserted-left nil))
+      (when right-changed
+        (setq erc-timestamp-last-inserted-right nil))
+      t)))
=20
 (defun erc-fill--hack-csf (f)
   ;; HACK until necessary additions to erc-stamp.el arrive (possibly
@@ -162,7 +179,7 @@ erc-fill--refill
         (inhibit-read-only t)
         (inhibit-point-motion-hooks t)
         ;;
-        left-changed right-changed ct) ; cached current time
+        ct) ; cached current time
     (cl-letf (((symbol-function #'erc-restore-text-properties) #'ignore)
               ((symbol-function #'current-time) (lambda () ct)))
       (while
@@ -188,19 +205,8 @@ erc-fill--refill
                  (end (text-property-not-all beg (point-max)
                                              'cursor-sensor-functions val)=
))
               (save-restriction
-                (narrow-to-region beg end)
-                (setq left-changed (erc-fill--remove-stamp-left))
-                ;; If NOSQUEEZE seems warranted, see note above.
-                (let ((fill-column (- (point-max) (point-min))))
-                  (fill-region (point-min) (point-max)))
-                (setq right-changed (erc-fill--remove-stamp-right))
-                (erc-fill)
-                (when (setq ct (when (or left-changed right-changed)
-                                 (erc-fill--hack-csf (car val))))
-                  (when left-changed
-                    (setq erc-timestamp-last-inserted-left nil))
-                  (when right-changed
-                    (setq erc-timestamp-last-inserted-right nil))
+                (when (setq ct (and (erc-fill--refill-message beg end)
+                                    (erc-fill--hack-csf (car val))))
                   (erc-add-timestamp))
                 (when reporter
                   (cl-incf (aref (cdr reporter) 2) ; max +=3D d_new - d_old
diff --git a/test/lisp/erc/erc-fill-resources/static-60.buffer b/test/lisp/=
erc/erc-fill-resources/static-60.buffer
index b33f11ae96..f8db4bf7f4 100644
--- a/test/lisp/erc/erc-fill-resources/static-60.buffer
+++ b/test/lisp/erc/erc-fill-resources/static-60.buffer
@@ -2,7 +2,9 @@
=20
=20
 [Tue Jan  1 1980]
-                       *** #chan modes: +nt           [00:00]
+                       *** Users on #chan: alice @bob robot
+                           tester                     [00:00]
+                       *** #chan modes: +nt
                        *** #chan was created on 2021-05-04
                            05:06:19
                      <bob> lorem ipsum This buffer is for
@@ -10,9 +12,10 @@
                            Lisp evaluation.           [00:01]
                    <alice> tester, welcome! Your name may or
                            may not be highlighted depending
-                           on whether button's been loaded
-                           by an earlier test. ERC needs
-                           help!                      [00:03]
+                           on whether erc-button's been
+                           enabled by an earlier test. ERC
+                           needs help!                [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
=20
 [Wed Jan  2 1980]
                      <bob> tester, welcome! To create a
diff --git a/test/lisp/erc/erc-fill-resources/static-72.buffer b/test/lisp/=
erc/erc-fill-resources/static-72.buffer
index 79ed88d112..6523f0887e 100644
--- a/test/lisp/erc/erc-fill-resources/static-72.buffer
+++ b/test/lisp/erc/erc-fill-resources/static-72.buffer
@@ -2,14 +2,17 @@
=20
=20
 [Tue Jan  1 1980]
-                       *** #chan modes: +nt                       [00:00]
+                       *** Users on #chan: alice @bob robot tester
+                                                                  [00:00]
+                       *** #chan modes: +nt
                        *** #chan was created on 2021-05-04 05:06:19
                      <bob> lorem ipsum This buffer is for text that is
                            not saved, and for Lisp evaluation.    [00:01]
                    <alice> tester, welcome! Your name may or may not be
-                           highlighted depending on whether button's
-                           been loaded by an earlier test. ERC needs
+                           highlighted depending on whether erc-button's
+                           been enabled by an earlier test. ERC needs
                            help!                                  [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
=20
 [Wed Jan  2 1980]
                      <bob> tester, welcome! To create a file, visit it
diff --git a/test/lisp/erc/erc-fill-resources/variable-60.buffer b/test/lis=
p/erc/erc-fill-resources/variable-60.buffer
index 4bf2741af0..38723209bf 100644
--- a/test/lisp/erc/erc-fill-resources/variable-60.buffer
+++ b/test/lisp/erc/erc-fill-resources/variable-60.buffer
@@ -2,13 +2,15 @@
=20
=20
 [Tue Jan  1 1980]
-*** #chan modes: +nt                                  [00:00]
+*** Users on #chan: alice @bob robot tester           [00:00]
+*** #chan modes: +nt
 *** #chan was created on 2021-05-04 05:06:19
 <bob> lorem ipsum This buffer is for text that is not saved,
       and for Lisp evaluation.                        [00:01]
 <alice> tester, welcome! Your name may or may not be
-        highlighted depending on whether button's been
-        loaded by an earlier test. ERC needs help!    [00:03]
+        highlighted depending on whether erc-button's been
+        enabled by an earlier test. ERC needs help!   [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
=20
 [Wed Jan  2 1980]
 <bob> tester, welcome! To create a file, visit it with ? and
diff --git a/test/lisp/erc/erc-fill-resources/variable-72.buffer b/test/lis=
p/erc/erc-fill-resources/variable-72.buffer
index de376cc15d..cc2410d7a7 100644
--- a/test/lisp/erc/erc-fill-resources/variable-72.buffer
+++ b/test/lisp/erc/erc-fill-resources/variable-72.buffer
@@ -2,13 +2,15 @@
=20
=20
 [Tue Jan  1 1980]
-*** #chan modes: +nt                                              [00:00]
+*** Users on #chan: alice @bob robot tester                       [00:00]
+*** #chan modes: +nt
 *** #chan was created on 2021-05-04 05:06:19
 <bob> lorem ipsum This buffer is for text that is not saved, and for
       Lisp evaluation.                                            [00:01]
 <alice> tester, welcome! Your name may or may not be highlighted
-        depending on whether button's been loaded by an earlier
+        depending on whether erc-button's been enabled by an earlier
         test. ERC needs help!                                     [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
=20
 [Wed Jan  2 1980]
 <bob> tester, welcome! To create a file, visit it with ? and enter text
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests=
.el
index a7e3d78d74..a0b695a6c7 100644
--- a/test/lisp/erc/erc-fill-tests.el
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -67,6 +67,10 @@ erc-fill-tests--populate
=20
     (cl-letf (((symbol-function 'current-time) (lambda () ct)))
       (with-current-buffer "foonet"
+        (erc-fill-tests--insert ":irc.foonet.org 353 tester =3D #chan :"
+                                "alice @bob robot tester")
+        (erc-fill-tests--insert ":irc.foonet.org 366 tester #chan :"
+                                "End of /NAMES list.")
         (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
         (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 16201047=
79")
=20
@@ -79,7 +83,10 @@ erc-fill-tests--populate
         (erc-fill-tests--insert
          ":alice!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
          " Your name may or may not be highlighted depending on whether"
-         " button's been loaded by an earlier test. ERC needs help!")
+         " erc-button's been enabled by an earlier test. ERC needs help!")
+
+        (erc-fill-tests--insert
+         ":robot!~u@HIDDEN PRIVMSG #chan :=E3=83=BB=E3=82=9C=E3=
=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\\_o< QUACK!")
=20
         (setq ct (time-convert (cl-incf ts (* 60 60 24))))
         (erc-fill-tests--insert
@@ -87,14 +94,6 @@ erc-fill-tests--populate
          " To create a file, visit it with ? and enter text in its buffer.=
")))))
=20
 (defun erc-fill-tests--teardown ()
-  ;; XXX when inspecting manually, must reactivate fill and stamp modes.
-  ;; Otherwise `erc-fill-buffer' won't work.
-  (dolist (buf '("variable-60.buffer"
-                 "variable-72.buffer"
-                 "static-60.buffer"
-                 "static-72.buffer"))
-    (when (buffer-live-p buf)
-      (kill-buffer buf)))
   (advice-remove 'format-time-string 'ts)
   (let (erc-kill-server-hook
         erc-kill-channel-hook)
@@ -106,7 +105,8 @@ erc-fill-tests--compare
   ;; Git didn't allow committing with a trailing space after the
   ;; prompt, hence this:
   (equal (substring-no-properties (buffer-string) 0 -1)
-         (with-current-buffer (find-file-literally (ert-resource-file name=
))
+         (with-temp-buffer
+           (insert-file-contents (ert-resource-file name))
            (buffer-string))))
=20
 (defun erc-fill-tests--await-fill ()
--=20
2.31.1


--=-=-=
Content-Type: text/x-patch; charset=utf-8
Content-Disposition: attachment;
 filename=0001-Add-command-to-refill-ERC-buffers.patch
Content-Transfer-Encoding: quoted-printable

From 1058b9202f9b530062bd5268c81a111976db61f2 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Tue, 16 Nov 2021 06:28:25 -0800
Subject: [PATCH 1/1] Add command to refill ERC buffers

* lisp/erc/erc-fill.el (erc-fill-buffer, erc-fill--refill,
erc-fill--refill-thread, erc-fill--refill-message,
erc-fill--hack-csf): Add new command and helpers to refill ERC
buffers.

* lisp/erc/erc-fill-tests.el: Add new file containing tests for
`erc-fill-buffer'. Add some support files to test against in
lisp/erc/erc-fill-resources.
---
 lisp/erc/erc-fill.el                          | 121 ++++++++++
 .../erc/erc-fill-resources/static-60.buffer   |  24 ++
 .../erc/erc-fill-resources/static-72.buffer   |  20 ++
 .../erc/erc-fill-resources/variable-60.buffer |  18 ++
 .../erc/erc-fill-resources/variable-72.buffer |  18 ++
 test/lisp/erc/erc-fill-tests.el               | 206 ++++++++++++++++++
 6 files changed, 407 insertions(+)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 9f29b9dad9..49130b9ffc 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,127 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
=20
+(defun erc-fill--refill-message (beg end)
+  "Refill but don't re-stamp region between BEG and END.
+Return non-nil if timestamps were removed."
+  (let (left-changed right-changed)
+    (narrow-to-region beg end)
+    ;; Remove at most one left timestamp, if any.
+    (goto-char (point-min))
+    (setq left-changed
+          ;; FIXME it may be a mistake to blow past leading whitespace
+          ;; without removing any intervening ws-only field intervals
+          (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (poi=
nt)))
+                      (nextf (when (eq 'erc-timestamp (field-at-pos beg))
+                               (field-beginning beg t)))
+                      ((eq 'erc-timestamp (get-text-property nextf 'field)=
)))
+            (goto-char (field-end nextf t))
+            (skip-syntax-forward "-")
+            (delete-region nextf (point))
+            t))
+    ;; Get everything on one line (if NOSQUEEZE seems warranted, see
+    ;; note below re ASCII art).
+    (let ((fill-column (string-width (buffer-string))))
+      (fill-region (point-min) (point-max)))
+    ;; Remove any stamps from right-hand side.
+    (goto-char (point-min))
+    (setq right-changed
+          (when-let* ((nextf (next-single-property-change (point) 'field)))
+            (delete-region nextf (1- (point-max)))
+            t))
+    (erc-fill)
+    ;; Remove trailing whitespace from last line, if any.
+    (goto-char (point-max))
+    (forward-line -1)
+    (when (re-search-forward "\\s-$" (line-end-position) t)
+      (delete-region (match-beginning 0) (match-end 0)))
+    ;; Neuter timestamp caching to force insertion.
+    (when (or left-changed right-changed)
+      (when left-changed
+        (setq erc-timestamp-last-inserted-left nil))
+      (when right-changed
+        (setq erc-timestamp-last-inserted-right nil))
+      t)))
+
+(defun erc-fill--hack-csf (f)
+  ;; HACK until necessary additions to erc-stamp.el arrive (possibly
+  ;; with erc-v3 in #49860), there's no civilized way of detecting the
+  ;; bounds of a displayed message after initial insertion.
+  ;;
+  ;; These callback closures are used for that purpose, but they also
+  ;; contain the timestamp we need.  An unforeseen benefit of this
+  ;; awkwardness is that it plays well with `text-property-not-all',
+  ;; which needs unique values to match against.  That wouldn't be the
+  ;; case were we to use lisp time objects instead because successive
+  ;; messages might contain the exact same one.
+  (if (byte-code-function-p f) (aref (aref f 2) 0) (alist-get 'ct (cadr f)=
)))
+
+;; Enabling `erc-fill-mode' is ultimately destructive to preformatted
+;; text (like ASCII art and figlets), which degenerate immediately
+;; upon display.  This is permanent because we don't store original
+;; messages (though with IRCv3, it may be possible to request a
+;; replacement from the server).
+(defun erc-fill--refill ()
+  (let ((m (make-marker))
+        (reporter (unless noninteractive
+                    (make-progress-reporter "filling" 0 (point-max))))
+        (inhibit-read-only t)
+        (inhibit-point-motion-hooks t)
+        ;;
+        ct) ; cached current time
+    (cl-letf (((symbol-function #'erc-restore-text-properties) #'ignore)
+              ((symbol-function #'current-time) (lambda () ct)))
+      (while
+          (save-excursion
+            (goto-char (or (marker-position m) (set-marker m (point-min))))
+            (when-let*
+                ((beg (if (get-text-property (point) 'cursor-sensor-functi=
ons)
+                          (point)
+                        (when-let*
+                            ((max (min (point-max) (+ 512 (point))))
+                             (res (next-single-property-change
+                                   (point) 'cursor-sensor-functions nil ma=
x))
+                             ((/=3D res max))) ; otherwise, we're done.
+                          res)))
+                 (val (get-text-property beg 'cursor-sensor-functions))
+                 (beg (progn ; remove left padding, if any.
+                        (goto-char beg)
+                        (skip-syntax-forward "-")
+                        (delete-region (min (line-beginning-position) beg)
+                                       (point))
+                        (point)))
+                 ;; Don't expect output limited to IRC message length.
+                 (end (text-property-not-all beg (point-max)
+                                             'cursor-sensor-functions val)=
))
+              (save-restriction
+                (when (setq ct (and (erc-fill--refill-message beg end)
+                                    (erc-fill--hack-csf (car val))))
+                  (erc-add-timestamp))
+                (when reporter
+                  (cl-incf (aref (cdr reporter) 2) ; max +=3D d_new - d_old
+                           (- (point-max) (point-min) end (- beg))))
+                (set-marker m (goto-char (point-max))))))
+        (when reporter
+          (progress-reporter-update reporter (point)))
+        (thread-yield)))))
+
+(defvar-local erc-fill--refill-thread nil
+  "A thread running a buffer-refill job.")
+
+(define-error 'erc-fill-canceled "ERC refill canceled" 'error)
+
+(defun erc-fill-buffer (force)
+  "Refill an ERC buffer.
+With FORCE, cancel an active refill job if one exists."
+  (interactive "P")
+  (when (and erc-fill--refill-thread
+             (thread-live-p erc-fill--refill-thread))
+    (if force
+        (thread-signal erc-fill--refill-thread
+                       'erc-fill-canceled (list (buffer-name)))
+      (user-error "Already refilling.")))
+  (setq erc-fill--refill-thread (make-thread #'erc-fill--refill "erc-fill"=
)))
+
 ;;;###autoload
 (defun erc-fill ()
   "Fill a region using the function referenced in `erc-fill-function'.
diff --git a/test/lisp/erc/erc-fill-resources/static-60.buffer b/test/lisp/=
erc/erc-fill-resources/static-60.buffer
new file mode 100644
index 0000000000..f8db4bf7f4
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-60.buffer
@@ -0,0 +1,24 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot
+                           tester                     [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04
+                           05:06:19
+                     <bob> lorem ipsum This buffer is for
+                           text that is not saved, and for
+                           Lisp evaluation.           [00:01]
+                   <alice> tester, welcome! Your name may or
+                           may not be highlighted depending
+                           on whether erc-button's been
+                           enabled by an earlier test. ERC
+                           needs help!                [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a
+                           file, visit it with ? and enter
+                           text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/static-72.buffer b/test/lisp/=
erc/erc-fill-resources/static-72.buffer
new file mode 100644
index 0000000000..6523f0887e
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-72.buffer
@@ -0,0 +1,20 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** Users on #chan: alice @bob robot tester
+                                                                  [00:00]
+                       *** #chan modes: +nt
+                       *** #chan was created on 2021-05-04 05:06:19
+                     <bob> lorem ipsum This buffer is for text that is
+                           not saved, and for Lisp evaluation.    [00:01]
+                   <alice> tester, welcome! Your name may or may not be
+                           highlighted depending on whether erc-button's
+                           been enabled by an earlier test. ERC needs
+                           help!                                  [00:03]
+                   <robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=
=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a file, visit it
+                           with ? and enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-60.buffer b/test/lis=
p/erc/erc-fill-resources/variable-60.buffer
new file mode 100644
index 0000000000..38723209bf
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-60.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester           [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved,
+      and for Lisp evaluation.                        [00:01]
+<alice> tester, welcome! Your name may or may not be
+        highlighted depending on whether erc-button's been
+        enabled by an earlier test. ERC needs help!   [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and
+      enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-72.buffer b/test/lis=
p/erc/erc-fill-resources/variable-72.buffer
new file mode 100644
index 0000000000..cc2410d7a7
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-72.buffer
@@ -0,0 +1,18 @@
+
+
+
+[Tue Jan  1 1980]
+*** Users on #chan: alice @bob robot tester                       [00:00]
+*** #chan modes: +nt
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved, and for
+      Lisp evaluation.                                            [00:01]
+<alice> tester, welcome! Your name may or may not be highlighted
+        depending on whether erc-button's been enabled by an earlier
+        test. ERC needs help!                                     [00:03]
+<robot> =E3=83=BB=E3=82=9C=E3=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=
=82=9C=E3=82=9C\_o< QUACK!
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and enter text
+      in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests=
.el
new file mode 100644
index 0000000000..a0b695a6c7
--- /dev/null
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -0,0 +1,206 @@
+;;; erc-fill-tests.el --- ERC message filling -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; GNU Emacs is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert-x)
+(require 'erc-fill)
+
+(defun erc-fill-tests--insert (&rest strings)
+  (let ((inhibit-read-only t))
+    (erc-parse-server-response erc-server-process (apply #'concat strings)=
)))
+
+(defun erc-fill-tests--setup-server-buffer ()
+  (with-current-buffer (get-buffer-create "foonet")
+    (erc-mode)
+    (setq erc-server-process (start-process "true" (current-buffer) "true")
+          erc-server-current-nick "tester"
+          erc-server-users (make-hash-table :test #'equal))
+    (set-process-query-on-exit-flag erc-server-process nil)))
+
+(defun erc-fill-tests--setup-channel-buffer ()
+  (with-current-buffer (get-buffer-create "#chan")
+    (erc-mode)
+    (insert "\n\n")
+    (setq erc-input-marker (make-marker)
+          ;; Kludge to get around saving display prop
+          erc-timestamp-use-align-to nil
+          ;; Kludge to make whitespace compare equal without expanding
+          indent-tabs-mode nil
+          erc-insert-marker (make-marker)
+          erc-default-recipients '("#chan")
+          erc-channel-users (make-hash-table :test #'equal)
+          erc-server-process (with-current-buffer "foonet"
+                               erc-server-process))
+    (set-marker erc-insert-marker (point-max))
+    (erc-display-prompt)))
+
+(defun erc-fill-tests--setup ()
+  (advice-add 'format-time-string :filter-args
+              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts=
)))
+
+  (erc-stamp-mode +1)
+
+  (erc-fill-tests--setup-server-buffer)
+  (erc-fill-tests--setup-channel-buffer)
+  (erc-fill-tests--populate))
+
+(defun erc-fill-tests--populate ()
+  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10))) ; Jan 1 1980
+         (ct (time-convert ts)))
+
+    (cl-letf (((symbol-function 'current-time) (lambda () ct)))
+      (with-current-buffer "foonet"
+        (erc-fill-tests--insert ":irc.foonet.org 353 tester =3D #chan :"
+                                "alice @bob robot tester")
+        (erc-fill-tests--insert ":irc.foonet.org 366 tester #chan :"
+                                "End of /NAMES list.")
+        (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
+        (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 16201047=
79")
+
+        (setq ct (time-convert (cl-incf ts 60)))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :lorem ipsum"
+         " This buffer is for text that is not saved, and for Lisp evaluat=
ion.")
+
+        (setq ct (time-convert (cl-incf ts 120)))
+        (erc-fill-tests--insert
+         ":alice!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " Your name may or may not be highlighted depending on whether"
+         " erc-button's been enabled by an earlier test. ERC needs help!")
+
+        (erc-fill-tests--insert
+         ":robot!~u@HIDDEN PRIVMSG #chan :=E3=83=BB=E3=82=9C=E3=
=82=9C=E3=83=BB=E3=80=82=E3=80=82=E3=83=BB=E3=82=9C=E3=82=9C\\_o< QUACK!")
+
+        (setq ct (time-convert (cl-incf ts (* 60 60 24))))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " To create a file, visit it with ? and enter text in its buffer.=
")))))
+
+(defun erc-fill-tests--teardown ()
+  (advice-remove 'format-time-string 'ts)
+  (let (erc-kill-server-hook
+        erc-kill-channel-hook)
+    (kill-buffer "#chan")
+    (kill-buffer "foonet"))
+  (should (=3D erc-fill-column 78)))
+
+(defun erc-fill-tests--compare (name)
+  ;; Git didn't allow committing with a trailing space after the
+  ;; prompt, hence this:
+  (equal (substring-no-properties (buffer-string) 0 -1)
+         (with-temp-buffer
+           (insert-file-contents (ert-resource-file name))
+           (buffer-string))))
+
+(defun erc-fill-tests--await-fill ()
+  (call-interactively #'erc-fill-buffer)
+  ;; This timeout silliness seemed a little more realistic than just:
+  ;;
+  ;;   (thread-join erc-fill--refill-thread)
+  ;;
+  ;; Probably dumb, right?.
+  (with-timeout (3 (error "Failed"))
+    (while (thread-live-p erc-fill--refill-thread)
+      (sleep-for 0.01))))
+
+(ert-deftest erc-fill-buffer ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      ;; These would get clobbered by the new thread if we let-bound
+      ;; them, and we can't set them globally, so best just fake it:
+      (setq-local erc-fill-mode t
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Wider")
+        (setq erc-fill-column 72)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Fancy")
+        (setq erc-fill-function #'erc-fill-static)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-72.buffer")))
+
+      (ert-info ("Fancy normal")
+        (setq erc-fill-column 60)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Again!")
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Back home")
+        (setq erc-fill-function #'erc-fill-variable)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+(ert-deftest erc-fill-buffer--interrupted ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      (setq-local erc-fill-mode t ; see note re these in prev test
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Denied")
+        (setq erc-fill-column 72)
+        (call-interactively #'erc-fill-buffer)
+        (should-error (erc-fill-buffer nil))
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Canceled")
+        (setq erc-fill-column 60)
+        (call-interactively #'erc-fill-buffer)
+        (sleep-for (cl-random 0.1))
+        (erc-fill-buffer t)
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+;;; erc-fill-tests.el ends here
--=20
2.31.1


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#51969; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 19 Nov 2021 10:40:07 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Nov 19 05:40:07 2021
Received: from localhost ([127.0.0.1]:38171 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1mo1JL-0004KQ-0B
	for submit <at> debbugs.gnu.org; Fri, 19 Nov 2021 05:40:07 -0500
Received: from lists.gnu.org ([209.51.188.17]:57144)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jp@HIDDEN>) id 1mo1JI-0004Ju-3o
 for submit <at> debbugs.gnu.org; Fri, 19 Nov 2021 05:40:01 -0500
Received: from eggs.gnu.org ([209.51.188.92]:34600)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <jp@HIDDEN>) id 1mo1JH-0004lD-Tb
 for bug-gnu-emacs@HIDDEN; Fri, 19 Nov 2021 05:39:59 -0500
Received: from mail-108-mta38.mxroute.com ([136.175.108.38]:43767)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <jp@HIDDEN>) id 1mo1JE-0003gY-PB
 for bug-gnu-emacs@HIDDEN; Fri, 19 Nov 2021 05:39:59 -0500
Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com)
 (Authenticated sender: mN4UYu2MZsgR)
 by mail-108-mta38.mxroute.com (ZoneMTA) with ESMTPSA id 17d37c7fc1a000177f.001
 for <bug-gnu-emacs@HIDDEN>
 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256);
 Fri, 19 Nov 2021 10:39:52 +0000
X-Zone-Loop: 804b88d2b1e7225660d1a83de27177a0f5a91760c0bb
X-Originating-IP: [149.28.56.236]
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=neverwas.me
 ; s=x;
 h=Content-Type:MIME-Version:Message-ID:Date:Subject:Cc:To:From:Sender:
 Reply-To:Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date
 :Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:
 References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post:
 List-Owner:List-Archive; bh=kjdJoIzB2vRNsiqimPxY6ekxiFb0l2/OJFgx8xqwN3w=; b=m
 89eawOutbq1AVxhYRXYmyTQESvRahSewGeQEBZC4e3BDJJwhY2UK/T3mjAugxrKJ92Oy4xleEFhWo
 qQhoFz32AVfELWZJH7yfWpb8pmeiyJhJcTpcp05x8vN9gaD+oD/upzKfF2SiVbg0g0hdIMDccrbpF
 qy4rlu8lpnLREMLQ+haO6L4fHEiW/jQWlEURpd5eRrLVm9NvkYht8XNqr9MIlf0kB+7ckSGLPxUG7
 zKUzFTn5CRGpbKYbr37Vaw5UyVGWMtJbeY8FF3IG1SzsKFterdKXu7fOwG1H+TUOT43wLM1Ra1IoB
 UkkXjSlIPykmeVbFmyGjVoHza28/8ACcg==;
From: "J.P." <jp@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: 29.0.50; Add command for refilling ERC buffers
Date: Fri, 19 Nov 2021 02:39:50 -0800
Message-ID: <87bl2gjuo9.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-AuthUser: masked@HIDDEN
X-Zone-Spam-Resolution: no action
X-Zone-Spam-Status: No, score=-0.1, required=15, tests=[ARC_NA=0,
 RCPT_COUNT_TWO=0, FROM_HAS_DN=0, HAS_ATTACHMENT=0, FROM_EQ_ENVFROM=0,
 MIME_TRACE=0, MIME_GOOD=-0.1, TO_DN_NONE=0, MID_RHS_MATCH_FROM=0,
 RCVD_COUNT_ZERO=0, NEURAL_SPAM=0]
Received-SPF: pass client-ip=136.175.108.38; envelope-from=jp@HIDDEN;
 helo=mail-108-mta38.mxroute.com
X-Spam_score_int: -16
X-Spam_score: -1.7
X-Spam_bar: -
X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1,
 DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=no autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.4 (-)
X-Debbugs-Envelope-To: submit
Cc: emacs-erc@HIDDEN
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

--=-=-=
Content-Type: text/plain

Tags: patch

A recent bug entitled

  bug#51841: 27.2; erc-insert-marker has no value

(opened by Libera user Bost) was in part brought about by confusion over
whether ERC already offered this feature and perhaps applied it during
major-mode setup. While I don't think erc-stamp and erc-fill currently
provide sufficient support to rig up automated refilling in a reliable
way (on window configuration change, for example), I agree that some
means of doing so manually might occasionally come in handy.

A related issue concerns the purpose and treatment of the option
`erc-fill-prefix'. This one's a mystery to me and seems to have little
meaningful bearing on anything. It does make an appearance in
erc-button.el, but to what end I'm not entirely certain. Like a few
other puzzlers, it was present in much the same form back when ERC was
first ported to ye old CVS some twenty years ago (almost to the day)
[1]. Anyway, I will continue to investigate this. Thanks.


[1] https://gitlab.com/jpneverwas/og-erc/-/commit/9a819366bdf8a6f5a304916baa2d6a501eb3987b#4042b2fc4c564fbd556343d1ed666da630f85951_0_97

In GNU Emacs 29.0.50 (build 1, x86_64-redhat-linux-gnu, GTK+ Version 3.24.30, cairo version 1.17.4)
 of 2021-11-18 built on localhost
Repository revision: 69f1bc43c026049ed2aab6a6368e2e9a5406b779
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Fedora 34 (Workstation Edition)

Configured using:
 'configure --enable-check-lisp-object-type --enable-checking=yes,glyphs
 --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu
 --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin
 --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share
 --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec
 --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man
 --infodir=/usr/share/info --with-dbus --with-gif --with-jpeg --with-png
 --with-rsvg --with-tiff --with-xft --with-xpm --with-x-toolkit=gtk3
 --with-gpm=no --with-xwidgets --with-modules --with-harfbuzz
 --with-cairo --with-json build_alias=x86_64-redhat-linux-gnu
 host_alias=x86_64-redhat-linux-gnu CC=gcc 'CFLAGS=-O0 -g3'
 LDFLAGS=-Wl,-z,relro
 PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
WEBP X11 XDBE XIM XPM XWIDGETS GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media rmc puny
dired dired-loaddefs rfc822 mml mml-sec epa derived epg rfc6068
epg-config gnus-util rmail rmail-loaddefs auth-source cl-seq eieio
eieio-core cl-macs eieio-loaddefs password-cache json map
text-property-search time-date seq gv subr-x byte-opt bytecomp
byte-compile cconv mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils help-mode cl-loaddefs cl-lib iso-transl
tooltip eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/x-win x-win term/common-win x-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget keymap hashtable-print-readable backquote threads
xwidget-internal dbusbind inotify lcms2 dynamic-setting
system-font-setting font-render-setting cairo move-toolbar gtk x-toolkit
x multi-tty make-network-process emacs)

Memory information:
((conses 16 53474 6237)
 (symbols 48 6794 1)
 (strings 32 19269 1625)
 (string-bytes 1 642335)
 (vectors 16 14025)
 (vector-slots 8 189932 12509)
 (floats 8 22 37)
 (intervals 56 262 0)
 (buffers 992 10))

--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Add-command-to-refill-ERC-buffers.patch

From bb9850eee9e44555a67f8e838b12e315c0085f38 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@HIDDEN>
Date: Tue, 16 Nov 2021 06:28:25 -0800
Subject: [PATCH 1/1] Add command to refill ERC buffers

* lisp/erc/erc-fill.el (erc-fill-buffer, erc-fill--refill,
erc-fill--refill-thread, erc-fill--remove-stamp-{left,right},
erc-fill--hack-csf): Add new command and helpers to refill ERC
buffers.

* lisp/erc/erc-fill-tests.el: Add new file containing tests for
`erc-fill-buffer'. Add some support files to test against in
lisp/erc/erc-fill-resources.
---
 lisp/erc/erc-fill.el                          | 115 ++++++++++
 .../erc/erc-fill-resources/static-60.buffer   |  21 ++
 .../erc/erc-fill-resources/static-72.buffer   |  17 ++
 .../erc/erc-fill-resources/variable-60.buffer |  16 ++
 .../erc/erc-fill-resources/variable-72.buffer |  16 ++
 test/lisp/erc/erc-fill-tests.el               | 206 ++++++++++++++++++
 6 files changed, 391 insertions(+)
 create mode 100644 test/lisp/erc/erc-fill-resources/static-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/static-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-60.buffer
 create mode 100644 test/lisp/erc/erc-fill-resources/variable-72.buffer
 create mode 100644 test/lisp/erc/erc-fill-tests.el

diff --git a/lisp/erc/erc-fill.el b/lisp/erc/erc-fill.el
index 9f29b9dad9..3bf335d098 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,121 @@ erc-fill-column
   "The column at which a filled paragraph is broken."
   :type 'integer)
 
+(defun erc-fill--remove-stamp-right ()
+  (goto-char (point-min))
+  (let (changed)
+    (while
+        (when-let* ((nextf (next-single-property-change (point) 'field)))
+          (goto-char (field-end nextf t))
+          ;; Sweep up residual phantom field remants
+          (delete-region nextf (field-end nextf t))
+          (setq changed t)))
+    changed))
+
+(defun erc-fill--remove-stamp-left ()
+  "Remove at most one LEFT or one right timestamp, if any."
+  (goto-char (point-min))
+  ;; FIXME actually, it may be a mistake to blow past white space
+  ;; without checking for intervening intervals that need cleaning up.
+  (when-let* ((beg (save-excursion (skip-syntax-forward ">-") (point)))
+              (nextf (when (eq 'erc-timestamp (field-at-pos beg))
+                       (field-beginning beg t)))
+              ((eq 'erc-timestamp (get-text-property nextf 'field))))
+    (goto-char (field-end nextf t))
+    (skip-syntax-forward "-")
+    (delete-region nextf (point))
+    t))
+
+(defun erc-fill--hack-csf (f)
+  ;; HACK until necessary additions to erc-stamp.el arrive (possibly
+  ;; with erc-v3 in #49860), there's no civilized way of detecting the
+  ;; bounds of a displayed message after initial insertion.
+  ;;
+  ;; These callback closures are used for that purpose, but they also
+  ;; contain the timestamp we need.  An unforeseen benefit of this
+  ;; awkwardness is that it plays well with `text-property-not-all',
+  ;; which needs unique values to match against.  That wouldn't be the
+  ;; case were we to use lisp time objects instead because successive
+  ;; messages might contain the exact same one.
+  (if (byte-code-function-p f) (aref (aref f 2) 0) (alist-get 'ct (cadr f))))
+
+;; Enabling `erc-fill-mode' is ultimately destructive to preformatted
+;; text (like ASCII art and figlets), which degenerate immediately
+;; upon display.  This is permanent because we don't store original
+;; messages (though with IRCv3, it may be possible to request a
+;; replacement from the server).
+(defun erc-fill--refill ()
+  (let ((m (make-marker))
+        (reporter (unless noninteractive
+                    (make-progress-reporter "filling" 0 (point-max))))
+        (inhibit-read-only t)
+        (inhibit-point-motion-hooks t)
+        ;;
+        left-changed right-changed ct) ; cached current time
+    (cl-letf (((symbol-function #'erc-restore-text-properties) #'ignore)
+              ((symbol-function #'current-time) (lambda () ct)))
+      (while
+          (save-excursion
+            (goto-char (or (marker-position m) (set-marker m (point-min))))
+            (when-let*
+                ((beg (if (get-text-property (point) 'cursor-sensor-functions)
+                          (point)
+                        (when-let*
+                            ((max (min (point-max) (+ 512 (point))))
+                             (res (next-single-property-change
+                                   (point) 'cursor-sensor-functions nil max))
+                             ((/= res max))) ; otherwise, we're done.
+                          res)))
+                 (val (get-text-property beg 'cursor-sensor-functions))
+                 (beg (progn ; remove left padding, if any.
+                        (goto-char beg)
+                        (skip-syntax-forward "-")
+                        (delete-region (min (line-beginning-position) beg)
+                                       (point))
+                        (point)))
+                 ;; Don't expect output limited to IRC message length.
+                 (end (text-property-not-all beg (point-max)
+                                             'cursor-sensor-functions val)))
+              (save-restriction
+                (narrow-to-region beg end)
+                (setq left-changed (erc-fill--remove-stamp-left))
+                ;; If NOSQUEEZE seems warranted, see note above.
+                (let ((fill-column (- (point-max) (point-min))))
+                  (fill-region (point-min) (point-max)))
+                (setq right-changed (erc-fill--remove-stamp-right))
+                (erc-fill)
+                (when (setq ct (when (or left-changed right-changed)
+                                 (erc-fill--hack-csf (car val))))
+                  (when left-changed
+                    (setq erc-timestamp-last-inserted-left nil))
+                  (when right-changed
+                    (setq erc-timestamp-last-inserted-right nil))
+                  (erc-add-timestamp))
+                (when reporter
+                  (cl-incf (aref (cdr reporter) 2) ; max += d_new - d_old
+                           (- (point-max) (point-min) end (- beg))))
+                (set-marker m (goto-char (point-max))))))
+        (when reporter
+          (progress-reporter-update reporter (point)))
+        (thread-yield)))))
+
+(defvar-local erc-fill--refill-thread nil
+  "A thread running a buffer-refill job.")
+
+(define-error 'erc-fill-canceled "ERC refill canceled" 'error)
+
+(defun erc-fill-buffer (force)
+  "Refill an ERC buffer.
+With FORCE, cancel an active refill job if one exists."
+  (interactive "P")
+  (when (and erc-fill--refill-thread
+             (thread-live-p erc-fill--refill-thread))
+    (if force
+        (thread-signal erc-fill--refill-thread
+                       'erc-fill-canceled (list (buffer-name)))
+      (user-error "Already refilling.")))
+  (setq erc-fill--refill-thread (make-thread #'erc-fill--refill "erc-fill")))
+
 ;;;###autoload
 (defun erc-fill ()
   "Fill a region using the function referenced in `erc-fill-function'.
diff --git a/test/lisp/erc/erc-fill-resources/static-60.buffer b/test/lisp/erc/erc-fill-resources/static-60.buffer
new file mode 100644
index 0000000000..b33f11ae96
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-60.buffer
@@ -0,0 +1,21 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** #chan modes: +nt           [00:00]
+                       *** #chan was created on 2021-05-04
+                           05:06:19
+                     <bob> lorem ipsum This buffer is for
+                           text that is not saved, and for
+                           Lisp evaluation.           [00:01]
+                   <alice> tester, welcome! Your name may or
+                           may not be highlighted depending
+                           on whether button's been loaded
+                           by an earlier test. ERC needs
+                           help!                      [00:03]
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a
+                           file, visit it with ? and enter
+                           text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/static-72.buffer b/test/lisp/erc/erc-fill-resources/static-72.buffer
new file mode 100644
index 0000000000..79ed88d112
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/static-72.buffer
@@ -0,0 +1,17 @@
+
+
+
+[Tue Jan  1 1980]
+                       *** #chan modes: +nt                       [00:00]
+                       *** #chan was created on 2021-05-04 05:06:19
+                     <bob> lorem ipsum This buffer is for text that is
+                           not saved, and for Lisp evaluation.    [00:01]
+                   <alice> tester, welcome! Your name may or may not be
+                           highlighted depending on whether button's
+                           been loaded by an earlier test. ERC needs
+                           help!                                  [00:03]
+
+[Wed Jan  2 1980]
+                     <bob> tester, welcome! To create a file, visit it
+                           with ? and enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-60.buffer b/test/lisp/erc/erc-fill-resources/variable-60.buffer
new file mode 100644
index 0000000000..4bf2741af0
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-60.buffer
@@ -0,0 +1,16 @@
+
+
+
+[Tue Jan  1 1980]
+*** #chan modes: +nt                                  [00:00]
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved,
+      and for Lisp evaluation.                        [00:01]
+<alice> tester, welcome! Your name may or may not be
+        highlighted depending on whether button's been
+        loaded by an earlier test. ERC needs help!    [00:03]
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and
+      enter text in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-resources/variable-72.buffer b/test/lisp/erc/erc-fill-resources/variable-72.buffer
new file mode 100644
index 0000000000..de376cc15d
--- /dev/null
+++ b/test/lisp/erc/erc-fill-resources/variable-72.buffer
@@ -0,0 +1,16 @@
+
+
+
+[Tue Jan  1 1980]
+*** #chan modes: +nt                                              [00:00]
+*** #chan was created on 2021-05-04 05:06:19
+<bob> lorem ipsum This buffer is for text that is not saved, and for
+      Lisp evaluation.                                            [00:01]
+<alice> tester, welcome! Your name may or may not be highlighted
+        depending on whether button's been loaded by an earlier
+        test. ERC needs help!                                     [00:03]
+
+[Wed Jan  2 1980]
+<bob> tester, welcome! To create a file, visit it with ? and enter text
+      in its buffer.
+ERC>
\ No newline at end of file
diff --git a/test/lisp/erc/erc-fill-tests.el b/test/lisp/erc/erc-fill-tests.el
new file mode 100644
index 0000000000..a7e3d78d74
--- /dev/null
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -0,0 +1,206 @@
+;;; erc-fill-tests.el --- ERC message filling -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020-2021 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+;;
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; GNU Emacs is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert-x)
+(require 'erc-fill)
+
+(defun erc-fill-tests--insert (&rest strings)
+  (let ((inhibit-read-only t))
+    (erc-parse-server-response erc-server-process (apply #'concat strings))))
+
+(defun erc-fill-tests--setup-server-buffer ()
+  (with-current-buffer (get-buffer-create "foonet")
+    (erc-mode)
+    (setq erc-server-process (start-process "true" (current-buffer) "true")
+          erc-server-current-nick "tester"
+          erc-server-users (make-hash-table :test #'equal))
+    (set-process-query-on-exit-flag erc-server-process nil)))
+
+(defun erc-fill-tests--setup-channel-buffer ()
+  (with-current-buffer (get-buffer-create "#chan")
+    (erc-mode)
+    (insert "\n\n")
+    (setq erc-input-marker (make-marker)
+          ;; Kludge to get around saving display prop
+          erc-timestamp-use-align-to nil
+          ;; Kludge to make whitespace compare equal without expanding
+          indent-tabs-mode nil
+          erc-insert-marker (make-marker)
+          erc-default-recipients '("#chan")
+          erc-channel-users (make-hash-table :test #'equal)
+          erc-server-process (with-current-buffer "foonet"
+                               erc-server-process))
+    (set-marker erc-insert-marker (point-max))
+    (erc-display-prompt)))
+
+(defun erc-fill-tests--setup ()
+  (advice-add 'format-time-string :filter-args
+              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts)))
+
+  (erc-stamp-mode +1)
+
+  (erc-fill-tests--setup-server-buffer)
+  (erc-fill-tests--setup-channel-buffer)
+  (erc-fill-tests--populate))
+
+(defun erc-fill-tests--populate ()
+  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10))) ; Jan 1 1980
+         (ct (time-convert ts)))
+
+    (cl-letf (((symbol-function 'current-time) (lambda () ct)))
+      (with-current-buffer "foonet"
+        (erc-fill-tests--insert ":irc.foonet.org 324 tester #chan +nt")
+        (erc-fill-tests--insert ":irc.foonet.org 329 tester #chan 1620104779")
+
+        (setq ct (time-convert (cl-incf ts 60)))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :lorem ipsum"
+         " This buffer is for text that is not saved, and for Lisp evaluation.")
+
+        (setq ct (time-convert (cl-incf ts 120)))
+        (erc-fill-tests--insert
+         ":alice!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " Your name may or may not be highlighted depending on whether"
+         " button's been loaded by an earlier test. ERC needs help!")
+
+        (setq ct (time-convert (cl-incf ts (* 60 60 24))))
+        (erc-fill-tests--insert
+         ":bob!~u@HIDDEN PRIVMSG #chan :tester, welcome!"
+         " To create a file, visit it with ? and enter text in its buffer.")))))
+
+(defun erc-fill-tests--teardown ()
+  ;; XXX when inspecting manually, must reactivate fill and stamp modes.
+  ;; Otherwise `erc-fill-buffer' won't work.
+  (dolist (buf '("variable-60.buffer"
+                 "variable-72.buffer"
+                 "static-60.buffer"
+                 "static-72.buffer"))
+    (when (buffer-live-p buf)
+      (kill-buffer buf)))
+  (advice-remove 'format-time-string 'ts)
+  (let (erc-kill-server-hook
+        erc-kill-channel-hook)
+    (kill-buffer "#chan")
+    (kill-buffer "foonet"))
+  (should (= erc-fill-column 78)))
+
+(defun erc-fill-tests--compare (name)
+  ;; Git didn't allow committing with a trailing space after the
+  ;; prompt, hence this:
+  (equal (substring-no-properties (buffer-string) 0 -1)
+         (with-current-buffer (find-file-literally (ert-resource-file name))
+           (buffer-string))))
+
+(defun erc-fill-tests--await-fill ()
+  (call-interactively #'erc-fill-buffer)
+  ;; This timeout silliness seemed a little more realistic than just:
+  ;;
+  ;;   (thread-join erc-fill--refill-thread)
+  ;;
+  ;; Probably dumb, right?.
+  (with-timeout (3 (error "Failed"))
+    (while (thread-live-p erc-fill--refill-thread)
+      (sleep-for 0.01))))
+
+(ert-deftest erc-fill-buffer ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      ;; These would get clobbered by the new thread if we let-bound
+      ;; them, and we can't set them globally, so best just fake it:
+      (setq-local erc-fill-mode t
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Wider")
+        (setq erc-fill-column 72)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Fancy")
+        (setq erc-fill-function #'erc-fill-static)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-72.buffer")))
+
+      (ert-info ("Fancy normal")
+        (setq erc-fill-column 60)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Again!")
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "static-60.buffer")))
+
+      (ert-info ("Back home")
+        (setq erc-fill-function #'erc-fill-variable)
+        (erc-fill-tests--await-fill)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+(ert-deftest erc-fill-buffer--interrupted ()
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-fill-tests--setup)
+
+    (with-current-buffer "#chan"
+      (setq-local erc-fill-mode t ; see note re these in prev test
+                  erc-stamp-mode t
+                  erc-fill-column 60)
+      (erc-fill-tests--await-fill)
+      (ert-info ("Baseline")
+        (should (erc-fill-tests--compare "variable-60.buffer")))
+
+      (ert-info ("Denied")
+        (setq erc-fill-column 72)
+        (call-interactively #'erc-fill-buffer)
+        (should-error (erc-fill-buffer nil))
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-72.buffer")))
+
+      (ert-info ("Canceled")
+        (setq erc-fill-column 60)
+        (call-interactively #'erc-fill-buffer)
+        (sleep-for (cl-random 0.1))
+        (erc-fill-buffer t)
+        (thread-join erc-fill--refill-thread)
+        (should (erc-fill-tests--compare "variable-60.buffer")))))
+
+  (when noninteractive
+    (erc-fill-tests--teardown)))
+
+;;; erc-fill-tests.el ends here
-- 
2.31.1


--=-=-=--




Acknowledgement sent to "J.P." <jp@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#51969; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Mon, 6 Dec 2021 01:45:02 UTC

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