Add support for variable-pitch fonts in 'visual-wrap-prefix-mode'

* lisp/emacs-lisp/subr-x.el (string-pixel-width): Allow passing BUFFER
to use the face remappings from that buffer when calculating the width.

* lisp/visual-wrap.el (visual-wrap--prefix): Rename to...
(visual-wrap--adjust-prefix): ... this, and support PREFIX as a number.
(visual-wrap-fill-context-prefix): Make obsolete in favor of...
(visual-wrap--content-prefix): ... this.
(visual-wrap-prefix-function): Extract inside of loop into...
(visual-wrap--apply-to-line): ... this.

* doc/lispref/display.texi (Size of Displayed Text): Update
documentation for 'string-pixel-width'.

* etc/NEWS: Announce this change.
This commit is contained in:
Jim Porter 2024-07-27 20:48:38 -07:00
parent 0756f3085e
commit f70a6ea0ea
4 changed files with 102 additions and 40 deletions

View file

@ -337,8 +337,10 @@ This construct can only be used with lexical binding."
. ,aargs)))
;;;###autoload
(defun string-pixel-width (string)
"Return the width of STRING in pixels."
(defun string-pixel-width (string &optional buffer)
"Return the width of STRING in pixels.
If BUFFER is non-nil, use the face remappings from that buffer when
determining the width."
(declare (important-return-value t))
(if (zerop (length string))
0
@ -352,6 +354,11 @@ This construct can only be used with lexical binding."
;; Disable line-prefix and wrap-prefix, for the same reason.
(setq 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))
(car (buffer-text-pixel-size nil nil t)))))