unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
@ 2023-08-01  7:47 Ihor Radchenko
  2023-08-01 12:07 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2023-08-01  7:47 UTC (permalink / raw)
  To: 64986

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>





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
  2023-08-01  7:47 bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer Ihor Radchenko
@ 2023-08-01 12:07 ` Eli Zaretskii
  2023-08-01 13:13   ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2023-08-01 12:07 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: 64986

> From: Ihor Radchenko <yantar92@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.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
  2023-08-01 12:07 ` Eli Zaretskii
@ 2023-08-01 13:13   ` Ihor Radchenko
  2023-08-03  8:55     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2023-08-01 13:13 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 64986

Eli Zaretskii <eliz@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>





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
  2023-08-01 13:13   ` Ihor Radchenko
@ 2023-08-03  8:55     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2023-08-03  8:55 UTC (permalink / raw)
  To: Ihor Radchenko, Lars Ingebrigtsen; +Cc: 64986

> From: Ihor Radchenko <yantar92@posteo.net>
> Cc: 64986@debbugs.gnu.org
> Date: Tue, 01 Aug 2023 13:13:08 +0000
> 
> Eli Zaretskii <eliz@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.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-03  8:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-01  7:47 bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer Ihor Radchenko
2023-08-01 12:07 ` Eli Zaretskii
2023-08-01 13:13   ` Ihor Radchenko
2023-08-03  8:55     ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).