GNU bug report logs -
#64986
30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
Previous Next
To reply to this bug, email your comments to 64986 AT debbugs.gnu.org.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#64986
; Package
emacs
.
(Tue, 01 Aug 2023 07:48:01 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Ihor Radchenko <yantar92 <at> posteo.net>
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Tue, 01 Aug 2023 07:48:01 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
Hi,
I stumbled upon apparently unexpected return value of
`window-text-pixel-size'. There is no easy reproducer involving emacs
-Q, but I can reliably reproduce it with certain variant of
`org-string-width' function. I am not 100% sure if it is a real Emacs
bug or if it is my misunderstanding of `window-text-pixel-size'.
Steps to reproduce:
1. git clone --depth 1 --branch bug/window-text-pixel-size https://git.sr.ht/~yantar92/org-mode
2. cd org-mode
3. make repro REPRO_ARGS="-l ./testing/org-test.el"
4. (setq org-confirm-babel-evaluate nil)
5. M-x debug-on-entry org-string-width
6. Insert and execute the following in *scratch*
(org-test-with-temp-text-in-file "
#+NAME: example-list
- simple
- not
- nested
- list
<point>#+BEGIN_SRC emacs-lisp :var x=example-list
(print x)
#+END_SRC"
(should (equal '("simple" "list") (org-babel-execute-src-block)))
(forward-line 5)
(should (string=
"| simple | list |"
(buffer-substring
(point-at-bol)
(point-at-eol)))))
7. If we "c" in the debugger, there is arith error originating from the
second call to `window-text-pixel-size' returning (0 . X) in buffer
containing text "a".
The code of `org-string-width' is:
(defun org-string-width (string &optional pixels)
"Return width of STRING when displayed in the current buffer.
Return width in pixels when PIXELS is non-nil."
....
(with-current-buffer (get-buffer-create " *Org string width*")
(setq-local display-line-numbers nil
line-prefix nil
wrap-prefix nil)
(setq-local buffer-invisibility-spec
(if (listp current-invisibility-spec)
(mapcar (lambda (el)
;; Consider ellipsis to have 0 width.
;; It is what Emacs 28+ does, but we have
;; to force it in earlier Emacs versions.
(if (and (consp el) (cdr el))
(list (car el))
el))
current-invisibility-spec)
current-invisibility-spec))
(setq-local char-property-alias-alist
current-char-property-alias-alist)
(let (pixel-width symbol-width)
(with-silent-modifications
(erase-buffer)
(insert string)
(setq pixel-width
(car (window-text-pixel-size
nil (line-beginning-position) (point-max))))
(unless pixels
(erase-buffer)
(insert "a")
(setq symbol-width
(car (window-text-pixel-size ;; <---- unexpected return
nil (line-beginning-position) (point-max))))))
(if pixels
pixel-width
(/ pixel-width symbol-width)))))))
In GNU Emacs 30.0.50 (build 5, x86_64-pc-linux-gnu, GTK+ Version
3.24.38, cairo version 1.17.8) of 2023-07-29 built on localhost
Repository revision: ce48073f1597ceecb82800e71c89b53badc9f9d0
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101008
System Description: Gentoo Linux
Configured using:
'configure --with-native-compilation'
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#64986
; Package
emacs
.
(Tue, 01 Aug 2023 12:08:01 GMT)
Full text and
rfc822 format available.
Message #8 received at 64986 <at> debbugs.gnu.org (full text, mbox):
> From: Ihor Radchenko <yantar92 <at> posteo.net>
> Date: Tue, 01 Aug 2023 07:47:32 +0000
>
> (with-current-buffer (get-buffer-create " *Org string width*")
> (setq-local display-line-numbers nil
> line-prefix nil
> wrap-prefix nil)
> (setq-local buffer-invisibility-spec
> (if (listp current-invisibility-spec)
> (mapcar (lambda (el)
> ;; Consider ellipsis to have 0 width.
> ;; It is what Emacs 28+ does, but we have
> ;; to force it in earlier Emacs versions.
> (if (and (consp el) (cdr el))
> (list (car el))
> el))
> current-invisibility-spec)
> current-invisibility-spec))
> (setq-local char-property-alias-alist
> current-char-property-alias-alist)
> (let (pixel-width symbol-width)
> (with-silent-modifications
> (erase-buffer)
> (insert string)
> (setq pixel-width
> (car (window-text-pixel-size
> nil (line-beginning-position) (point-max))))
> (unless pixels
> (erase-buffer)
> (insert "a")
> (setq symbol-width
> (car (window-text-pixel-size ;; <---- unexpected return
> nil (line-beginning-position) (point-max))))))
> (if pixels
> pixel-width
> (/ pixel-width symbol-width)))))))
You are using window-text-pixel-size incorrectly. Its doc string
says:
Return the size of the text of WINDOW's buffer in pixels.
Note the "WINDOW's buffer" part: it is there for a reason.
So what your code is doing is measure the size of the text of the
buffer shown in WINDOW (in your case, the selected window) between
buffer positions 1 and 2. And the buffer shown in the selected
window, the one created by org-test-with-temp-text-in-file, begins
with an empty line, so the function correctly returns zero as the
horizontal dimensions of the text.
Your code seems to assume that with-current-buffer makes the named
buffer be "temporarily shown" in the selected window, but that is not
what happens: it just makes that buffer the current buffer.
This tricky part is why we now have buffer-text-pixel-size: it takes
care of making the buffer "temporarily shown" in the window. So I
believe you should use that function instead.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#64986
; Package
emacs
.
(Tue, 01 Aug 2023 13:14:01 GMT)
Full text and
rfc822 format available.
Message #11 received at 64986 <at> debbugs.gnu.org (full text, mbox):
Eli Zaretskii <eliz <at> gnu.org> writes:
> So what your code is doing is measure the size of the text of the
> buffer shown in WINDOW (in your case, the selected window) between
> buffer positions 1 and 2. And the buffer shown in the selected
> window, the one created by org-test-with-temp-text-in-file, begins
> with an empty line, so the function correctly returns zero as the
> horizontal dimensions of the text.
Ouch! Thanks for the explanation.
> This tricky part is why we now have buffer-text-pixel-size: it takes
> care of making the buffer "temporarily shown" in the window. So I
> believe you should use that function instead.
Indeed. We previously used `set-window-buffer' as in shr.el, but that
felt like an awkward workaround because it can sometimes err when
selected-window is dedicated.
I am wondering if `shr-pixel-buffer-width' and `shr-pixel-column' should
also use `buffer-text-pixel-size'. The `(set-window-dedicated-p nil
nil)` is not nice as it could override user customization.
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#64986
; Package
emacs
.
(Thu, 03 Aug 2023 08:56:02 GMT)
Full text and
rfc822 format available.
Message #14 received at 64986 <at> debbugs.gnu.org (full text, mbox):
> From: Ihor Radchenko <yantar92 <at> posteo.net>
> Cc: 64986 <at> debbugs.gnu.org
> Date: Tue, 01 Aug 2023 13:13:08 +0000
>
> Eli Zaretskii <eliz <at> gnu.org> writes:
>
> > This tricky part is why we now have buffer-text-pixel-size: it takes
> > care of making the buffer "temporarily shown" in the window. So I
> > believe you should use that function instead.
>
> Indeed. We previously used `set-window-buffer' as in shr.el, but that
> felt like an awkward workaround because it can sometimes err when
> selected-window is dedicated.
>
> I am wondering if `shr-pixel-buffer-width' and `shr-pixel-column' should
> also use `buffer-text-pixel-size'. The `(set-window-dedicated-p nil
> nil)` is not nice as it could override user customization.
Probably. Patches to that effect are welcome.
Added tag(s) confirmed.
Request was from
Stefan Kangas <stefankangas <at> gmail.com>
to
control <at> debbugs.gnu.org
.
(Fri, 01 Sep 2023 20:32:01 GMT)
Full text and
rfc822 format available.
This bug report was last modified 1 year and 287 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.