From 0ef908e34e7a75e84dcadb4b09d583faacbd2bba Mon Sep 17 00:00:00 2001 From: Jim Porter 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 | 15 ++++++++++----- lisp/visual-wrap.el | 12 ++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lisp/net/shr.el b/lisp/net/shr.el index b9ac9f0c8c0..1cbd9a0eaf5 100644 --- a/lisp/net/shr.el +++ b/lisp/net/shr.el @@ -1057,11 +1057,16 @@ 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)) + (avg-width (if (/= (aref info 7) 0) + (aref info 7) + (aref info 6)))) + (/ (float shr-indentation) avg-width) + 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..d0c87fd6e9e 100644 --- a/lisp/visual-wrap.el +++ b/lisp/visual-wrap.el @@ -164,11 +164,15 @@ 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)) - (info (query-font font))) + (if-let ((text (buffer-substring position (1+ position))) + (font (font-at 0 nil text)) + (info (query-font font)) + (avg-width (if (/= (aref info 7) 0) + (aref info 7) + (aref info 6)))) (max (string-width prefix) (ceiling (string-pixel-width prefix (current-buffer)) - (aref info 7))) + avg-width)) ;; We couldn't get the font, so we're in a terminal and ;; `string-pixel-width' is really returning the number of columns. ;; (This is different from `string-width', since that doesn't @@ -189,7 +193,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