1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-24 06:20:43 -08:00

Use 'with-work-macro' in 'string-pixel-width'

Tweak the implementation of 'string-pixel-width' to run
faster and use less memory.  Also cater for the case where
this function is called in parallel (bug#72689).
* lisp/emacs-lisp/subr-x.el (string-pixel-width): Use
`with-work-macro'.  Prefer `remove-text-properties' to
`propertize' to avoid creating a new string on each call.
This commit is contained in:
David Ponce 2024-08-22 16:56:11 +02:00 committed by Eli Zaretskii
parent b930a698f2
commit dffdbc1f1f

View file

@ -393,20 +393,24 @@ determining the width."
0
;; Keeping a work buffer around is more efficient than creating a
;; new temporary buffer.
(with-current-buffer (get-buffer-create " *string-pixel-width*")
;; If `display-line-numbers' is enabled in internal buffers
;; (e.g. globally), it breaks width calculation (bug#59311)
(setq-local display-line-numbers nil)
(delete-region (point-min) (point-max))
;; Disable line-prefix and wrap-prefix, for the same reason.
(setq line-prefix nil
wrap-prefix nil)
(with-work-buffer
;; If `display-line-numbers' is enabled in internal
;; buffers (e.g. globally), it breaks width calculation
;; (bug#59311). Disable `line-prefix' and `wrap-prefix',
;; for the same reason.
(setq display-line-numbers nil
line-prefix nil wrap-prefix nil)
(if buffer
(setq-local face-remapping-alist
(with-current-buffer buffer
face-remapping-alist))
(kill-local-variable 'face-remapping-alist))
(insert (propertize string 'line-prefix nil 'wrap-prefix nil))
(erase-buffer)
(insert string)
;; Prefer `remove-text-properties' to `propertize' to avoid
;; creating a new string on each call.
(remove-text-properties
(point-min) (point-max) '(line-prefix nil wrap-prefix nil))
(car (buffer-text-pixel-size nil nil t)))))
;;;###autoload