unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "J.P." <jp@neverwas.me>
To: Rostislav Svoboda <rostislav.svoboda@gmail.com>
Cc: 51841@debbugs.gnu.org, Lars Ingebrigtsen <larsi@gnus.org>,
	emacs-erc@gnu.org
Subject: bug#51841: 27.2; erc-insert-marker has no value
Date: Thu, 18 Nov 2021 06:49:28 -0800	[thread overview]
Message-ID: <87v90pzfgn.fsf__41738.4281916142$1637247311$gmane$org@neverwas.me> (raw)
In-Reply-To: <CAEtmmezk406mKS9VU4pdU5g4+7_O5HLzTeSsZ=E2ArC57WPyHg@mail.gmail.com> (Rostislav Svoboda's message of "Mon, 15 Nov 2021 13:12:24 +0100")

[-- Attachment #1: Type: text/plain, Size: 732 bytes --]

Rostislav Svoboda <rostislav.svoboda@gmail.com> writes:

>> But by "activate" (and also from your other comments), I'm starting
>> to think you mean not only for subsequent output but for all prior
>> output as well. What I mean is, are you expecting the buffer to be
>> refilled according to the updated value? If so, ERC doesn't support
>> that (though I think Circe might). If that's what you're after, that
>> would make a good feature request. We could even change the title of
>> this to something like "Support dynamic refilling of ERC buffers".
>> (Or just open a new bug.)
>
> Yes that would be the ultimate goal.

Bost, can you try this when you get a chance? Thanks.

(P.S. Likely fails with more exotic option values.)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-interactive-command-to-refill-ERC-buffers.patch --]
[-- Type: text/x-patch, Size: 16923 bytes --]

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "F. Jason Park" <jp@neverwas.me>
Date: Tue, 16 Nov 2021 06:28:25 -0800
Subject: [PATCH 01/29] Add interactive 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 interactive 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                          | 100 +++++++++++
 .../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               | 164 ++++++++++++++++++
 6 files changed, 334 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..0a1184b156 100644
--- a/lisp/erc/erc-fill.el
+++ b/lisp/erc/erc-fill.el
@@ -112,6 +112,106 @@ 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.
+  (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)))))
+
+;; For now, don't clean this up or present errors.  Just leave it for
+;; enterprising users to fiddle with if need be.
+(defvar-local erc-fill--refill-thread nil)
+
+(defun erc-fill-buffer ()
+  "Refill an ERC buffer... eventually."
+  (interactive)
+  (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..cf1b3ba78f
--- /dev/null
+++ b/test/lisp/erc/erc-fill-tests.el
@@ -0,0 +1,164 @@
+;;; 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--populate ()
+  (let* ((ts (+ (* 2 60 60 24) (* 60 60 24 365 10)))
+         (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@rz2v467q4rwhy.irc 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@rz2v467q4rwhy.irc 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@rz2v467q4rwhy.irc 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.
+  (kill-buffer "variable-60.buffer")
+  (kill-buffer "variable-72.buffer")
+  (kill-buffer "static-60.buffer")
+  (kill-buffer "static-72.buffer")
+  (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 ()
+  (advice-add 'format-time-string :filter-args
+              (lambda (args) (list (car args) (cadr args) 0)) '((name . ts)))
+  (let* (erc-insert-pre-hook
+         erc-insert-modify-hook
+         erc-send-modify-hook
+         erc-mode-hook
+         erc-stamp-mode
+         erc-fill--refill-thread)
+
+    (erc-stamp-mode +1)
+
+    (erc-fill-tests--setup-server-buffer)
+    (erc-fill-tests--setup-channel-buffer)
+    (erc-fill-tests--populate)
+
+    (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)))
+
+;;; erc-fill-tests.el ends here
-- 
2.31.1


  parent reply	other threads:[~2021-11-18 14:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14 13:19 bug#51841: 27.2; erc-insert-marker has no value Rostislav Svoboda
2021-11-14 22:53 ` J.P.
     [not found] ` <87r1bixsb4.fsf@neverwas.me>
2021-11-14 23:29   ` Rostislav Svoboda
     [not found]   ` <CAEtmmey6kqfNNKtX6+QDwMEGTtS=eOmCYa1HRY1KivXRfwQT5Q@mail.gmail.com>
2021-11-15  9:08     ` Lars Ingebrigtsen
     [not found]     ` <87ilwtzszy.fsf@gnus.org>
2021-11-15 10:23       ` J.P.
     [not found]       ` <87v90tra36.fsf@neverwas.me>
2021-11-15 12:12         ` Rostislav Svoboda
     [not found]         ` <CAEtmmezk406mKS9VU4pdU5g4+7_O5HLzTeSsZ=E2ArC57WPyHg@mail.gmail.com>
2021-11-15 15:12           ` J.P.
2021-11-18 14:49           ` J.P. [this message]
     [not found]           ` <87v90pzfgn.fsf@neverwas.me>
2021-11-19  3:13             ` J.P.
     [not found]             ` <87o86gn8gs.fsf@neverwas.me>
2021-11-19  5:28               ` Lars Ingebrigtsen
     [not found]               ` <87tug8yaqz.fsf@gnus.org>
2021-11-19 10:45                 ` J.P.

Reply instructions:

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

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

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to='87v90pzfgn.fsf__41738.4281916142$1637247311$gmane$org@neverwas.me' \
    --to=jp@neverwas.me \
    --cc=51841@debbugs.gnu.org \
    --cc=emacs-erc@gnu.org \
    --cc=larsi@gnus.org \
    --cc=rostislav.svoboda@gmail.com \
    /path/to/YOUR_REPLY

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

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

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).