Subject: bug#72771: 31.0.50; shr html renderer throwing "Specified window is not displaying the current buffer"

From: Jim Porter <[email-masked]>
Date: Fri, 23 Aug 2024 15:39:00 -0700

On 8/23/2024 10:10 AM, Kévin Le Gouguec wrote:
> Thanks for opening an issue Rob!
> 
> Eli Zaretskii <[email-masked]> writes:
> 
>> This is being discussed here:
>>
>>    https://lists.gnu.org/archive/html/emacs-devel/2024-08/msg00788.html
> 
> (Will be traveling during the coming week, with limited time &
> connectivity, so anyone should feel free to beat me to installing the
> visual-wrap patch - AFAIU from the reported backtraces though, it won't
> fix the issues that other folks are having)

Here's a patch. I've tested this in a few configurations (in the current 
window, in a buffer that's not being displayed, in a terminal Emacs) and 
it all seems to work.

One question, Eli: is there a better way than I'm using to get the font 
that would be used for a character in the buffer? When the buffer is 
being displayed in a window, '(font-at position window)' works, but that 
doesn't address this bug, where the buffer isn't displayed. (The font 
that we get back doesn't have to be 100% accurate; just a good guess 
should be fine for this case.)

From 5cd39589ebd47ccc8f53b8921fadd566df316d21 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Fri, 23 Aug 2024 15:11:24 -0700
Subject: [PATCH] Improve computation of indent depth in SHR and
 'visual-wrap-prefix-mode'

This method gets the font that would be used for the current window for
the text in question.  That way, there are no problems if the current
buffer isn't being displayed in a window.

* lisp/net/shr.el (shr-indent):
* lisp/visual-wrap.el (visual-wrap--content-prefix): Fix getting the
font when the buffer isn't displayed in a window.
(visual-wrap-fill-context-prefix): Fix indentation.
---
 lisp/net/shr.el     | 12 +++++++-----
 lisp/visual-wrap.el |  5 +++--
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index b9ac9f0c8c0..a55c97b1349 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -1057,11 +1057,13 @@ shr-indent
          ;; of the current face, like (N . width).  That way, the
          ;; indentation is calculated correctly when using
          ;; `text-scale-adjust'.
-         `(space :width (,(if-let ((font (font-at (1- (point))))
-                                   (info (query-font font)))
-                              (/ (float shr-indentation) (aref info 7))
-                            shr-indentation)
-                         . width))))
+         `(space :width
+                 (,(if-let ((text (buffer-substring (1- (point)) (point)))
+                            (font (font-at 0 nil text))
+                            (info (query-font font)))
+                       (/ (float shr-indentation) (aref info 7))
+                     shr-indentation)
+                  . width))))
       (put-text-property start (+ (point) prefix)
                          'shr-prefix-length (+ prefix (- (point) start))))))
 
diff --git a/lisp/visual-wrap.el b/lisp/visual-wrap.el
index 902a9e41c5e..52ac39513be 100644
--- a/lisp/visual-wrap.el
+++ b/lisp/visual-wrap.el
@@ -164,7 +164,8 @@ visual-wrap--content-prefix
     ;; width of the first-line prefix in canonical-width characters.
     ;; This is useful if the first-line prefix uses some very-wide
     ;; characters.
-    (if-let ((font (font-at position))
+    (if-let ((text (buffer-substring position (1+ position)))
+             (font (font-at 0 nil text))
              (info (query-font font)))
         (max (string-width prefix)
              (ceiling (string-pixel-width prefix (current-buffer))
@@ -189,7 +190,7 @@ visual-wrap-fill-context-prefix
           ;; make much sense (and is positively harmful in
           ;; taskpaper-mode where paragraph-start matches everything).
           (or (let ((paragraph-start regexp-unmatchable))
-                    (fill-context-prefix beg end))
+                (fill-context-prefix beg end))
                   ;; Note: fill-context-prefix may return nil; See:
                   ;; http://article.gmane.org/gmane.emacs.devel/156285
               ""))
-- 
2.25.1