From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "J.P." Newsgroups: gmane.emacs.bugs,gmane.emacs.erc.general Subject: bug#51969: 29.0.50; Add command for refilling ERC buffers Date: Wed, 24 Nov 2021 05:38:23 -0800 Message-ID: <878rxdad2o.fsf@neverwas.me> References: <87bl2gjuo9.fsf@neverwas.me> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17267"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux) Cc: emacs-erc@gnu.org To: 51969@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Nov 24 14:54:05 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mpsip-0004Ba-JG for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 24 Nov 2021 14:54:04 +0100 Original-Received: from localhost ([::1]:54946 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mpsio-0000QT-F9 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 24 Nov 2021 08:54:02 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:48794) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mpsUI-0002mu-6E for bug-gnu-emacs@gnu.org; Wed, 24 Nov 2021 08:39:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:41981) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mpsUH-0002Cq-Sl for bug-gnu-emacs@gnu.org; Wed, 24 Nov 2021 08:39:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mpsUH-0008AT-M1 for bug-gnu-emacs@gnu.org; Wed, 24 Nov 2021 08:39:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: "J.P." Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 24 Nov 2021 13:39:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51969 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 51969-submit@debbugs.gnu.org id=B51969.163776112231371 (code B ref 51969); Wed, 24 Nov 2021 13:39:01 +0000 Original-Received: (at 51969) by debbugs.gnu.org; 24 Nov 2021 13:38:42 +0000 Original-Received: from localhost ([127.0.0.1]:53526 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mpsTx-00089u-4A for submit@debbugs.gnu.org; Wed, 24 Nov 2021 08:38:42 -0500 Original-Received: from mail-108-mta86.mxroute.com ([136.175.108.86]:42709) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mpsTt-00089e-Dl for 51969@debbugs.gnu.org; Wed, 24 Nov 2021 08:38:38 -0500 Original-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@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==; In-Reply-To: <87bl2gjuo9.fsf@neverwas.me> (J. P.'s message of "Fri, 19 Nov 2021 02:39:50 -0800") X-AuthUser: masked@neverwas.me 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-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:220736 gmane.emacs.erc.general:1680 Archived-At: --=-=-= 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" 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" 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" 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 + lorem ipsum This buffer is for + text that is not saved, and for + Lisp evaluation. [00:01] + 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] + =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] + 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 + lorem ipsum This buffer is for text that is + not saved, and for Lisp evaluation. [00:01] + 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] + =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] + 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 + lorem ipsum This buffer is for text that is not saved, + and for Lisp evaluation. [00:01] + 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] + =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] + 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 + lorem ipsum This buffer is for text that is not saved, and for + Lisp evaluation. [00:01] + 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] + =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] + 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 . + +;;; 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@rz2v467q4rwhy.irc 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@rz2v467q4rwhy.irc 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@rz2v467q4rwhy.irc 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@rz2v467q4rwhy.irc 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 --=-=-=--