From: Eli Zaretskii <eliz@gnu.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: 64986@debbugs.gnu.org
Subject: bug#64986: 30.0.50; window-text-pixel-size sometimes returns 0 width when called from temporary buffer
Date: Tue, 01 Aug 2023 15:07:52 +0300 [thread overview]
Message-ID: <83pm470xuv.fsf@gnu.org> (raw)
In-Reply-To: <87o7jrgq5n.fsf@localhost> (message from Ihor Radchenko on Tue, 01 Aug 2023 07:47:32 +0000)
> 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.
next prev parent reply other threads:[~2023-08-01 12:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2023-08-01 13:13 ` Ihor Radchenko
2023-08-03 8:55 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=83pm470xuv.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=64986@debbugs.gnu.org \
--cc=yantar92@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).