From c0c538b392240e388b80ae03f12774f577732a14 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Fri, 17 Jun 2022 13:48:53 +0200 Subject: [PATCH] Speed up shr-string-pixel-width * lisp/net/eww.el (eww-update-header-line-format): Use string-pixel-width (since we don't care about shr-use-fonts here). * lisp/net/shr.el (shr-string-pixel-width): Use `string-pixel-width', which should be faster. --- lisp/net/eww.el | 8 ++++---- lisp/net/shr.el | 18 +++++------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/lisp/net/eww.el b/lisp/net/eww.el index 8f02be12ff8..3c16942e7c0 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -833,7 +833,7 @@ The renaming scheme is performed in accordance with (when url (setq url (propertize url 'face 'variable-pitch)) (let* ((parsed (url-generic-parse-url url)) - (host-length (shr-string-pixel-width + (host-length (string-pixel-width (propertize (format "%s://%s" (url-type parsed) (url-host parsed)) @@ -842,17 +842,17 @@ The renaming scheme is performed in accordance with (cond ;; The host bit is wider than the window, so nix ;; the title. - ((> (+ host-length (shr-string-pixel-width "xxxxx")) width) + ((> (+ host-length (string-pixel-width "xxxxx")) width) (setq title "")) ;; Trim the title. - ((> (+ (shr-string-pixel-width (concat title "xx")) + ((> (+ (string-pixel-width (concat title "xx")) host-length) width) (setq title (concat (eww--limit-string-pixelwise title (- width host-length - (shr-string-pixel-width + (string-pixel-width (propertize "...: " 'face 'variable-pitch)))) (propertize "..." 'face 'variable-pitch))))))) diff --git a/lisp/net/shr.el b/lisp/net/shr.el index b54144576ef..b269607e323 100644 --- a/lisp/net/shr.el +++ b/lisp/net/shr.el @@ -337,6 +337,11 @@ and other things: 0)) (pixel-fill-width))) +(defmacro shr-string-pixel-width (string) + `(if (not shr-use-fonts) + (length ,string) + (string-pixel-width ,string))) + ;;;###autoload (defun shr-insert-document (dom) "Render the parsed document DOM into the current buffer. @@ -677,19 +682,6 @@ size, and full-buffer size." (goto-char (mark)) (shr-pixel-column)))) -(defun shr-string-pixel-width (string) - (if (not shr-use-fonts) - (length string) - ;; Save and restore point across with-temp-buffer, since - ;; shr-pixel-column uses save-window-excursion, which can reset - ;; point to 1. - (let ((pt (point))) - (prog1 - (with-temp-buffer - (insert string) - (shr-pixel-column)) - (goto-char pt))))) - (defsubst shr--translate-insertion-chars () ;; Remove soft hyphens. (goto-char (point-min)) -- 2.39.2