GNU bug report logs - #47712
27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`

Previous Next

Package: emacs;

Reported by: Daniel Mendler <mail <at> daniel-mendler.de>

Date: Sun, 11 Apr 2021 21:17:02 UTC

Severity: normal

Found in version 27.1

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


Message #62 received at 47712 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Eli Zaretskii <eliz <at> gnu.org>, Daniel Mendler <mail <at> daniel-mendler.de>
Cc: 47712 <at> debbugs.gnu.org
Subject: Re: bug#47712: 27.1; Provide `string-display-width` function, which
 takes properties into account, `substring-width`
Date: Tue, 13 Apr 2021 09:06:06 +0200
>    (defun string-pixel-width (string)
>      (with-current-buffer (get-buffer-create "foo")
>        (erase-buffer)
>        (insert string)
>        (window-text-pixel-size nil (point-min) (point-max))))

[...]

> (There's indeed something strange with the results, I think the
> with-current-buffer thing is not enough (because if I manually switch
> to buffer "foo" and run the function, it returns correct results).  I
> will take a closer look when I have time, unless martin beats me to
> it.)

For `window-text-pixel-size' to work correctly, the buffer in question
must be the buffer of the first argument of `window-text-pixel-size'
(that's what the display engine expects).  OTOH Elisp code doesn't have
to care about whether that buffer is current (but it can't harm either).

So one way to write `string-pixel-width' should be

(defun string-pixel-width (string)
  (let ((buffer (get-buffer-create "*foo*"))
	(old-buffer (window-buffer)))
    (with-current-buffer buffer
      (erase-buffer)
      (insert string)
      (set-window-buffer (selected-window) buffer)
      (prog1
	  (window-text-pixel-size nil (point-min) (point-max))
	(set-window-buffer (selected-window) old-buffer)))))

martin




This bug report was last modified 4 years and 37 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.