From: Aaron Jensen <aaronjensen@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: martin rudalics <rudalics@gmx.at>, 45748@debbugs.gnu.org
Subject: bug#45748: 28.0.50; fit-frame-to-buffer ignores leading spaces
Date: Sat, 9 Jan 2021 20:56:35 -0600 [thread overview]
Message-ID: <CAHyO48zBAA6b7h5iVV+zFxo-EzyQ3wLGmGn4AfpdTZPJMh7kJQ@mail.gmail.com> (raw)
In-Reply-To: <83k0smgeq8.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 1177 bytes --]
On Sat, Jan 9, 2021 at 12:14 PM Eli Zaretskii <eliz@gnu.org> wrote:
> Yes, probably.
>
> But before we do any changes here, we need a test suite. Would you
> mind adding the necessary tests to test/src/xdisp-tests.el?
Okay, I added a few basic tests for this case. I don't know if that's
the best way to test it, but it's what I could figure out.
The implementation bypasses the extra backtrack for the FROM, but the
algorithm for the TO is the same as I first submitted. In that case,
it's not really an extra backtrack since I have to dec to read the
byte. If I had access to dec_bytepos declared in syntax.c, I think I
could use that to avoid the extra backtrack (I'd fetch the dec_bytepos
(bpos)) and test that and then only move the pointers if not breaking
from the loop.
I left the fetch_char_advance in the TO algorithm since it didn't seem
necessary to replace it with separate fetch/inc_both.
This could be done w/o the backtracking entirely by keeping two
pointers, but that's more complicated, probably not much more
efficient (if at all) and this will likely never be called in a tight
loop.
Let me know how this looks and if you want me to make any tweaks.
[-- Attachment #2: 0001-Fix-window-text-pixel-size-with-leading-trailing-spa.patch --]
[-- Type: application/octet-stream, Size: 3434 bytes --]
From 595937263d77bf392a32961119e0ce7f8754525b Mon Sep 17 00:00:00 2001
From: Aaron Jensen <aaronjensen@gmail.com>
Date: Sat, 9 Jan 2021 20:43:32 -0600
Subject: [PATCH] Fix window-text-pixel-size with leading/trailing spaces
(bug#45748)
First, scan to find the first non-whitespace character and then
backtrack to find the beginning of the line. The previous algorithm
always started on the non-whitespace character during the backtrack,
causing it to stop immediately and not actually find the beginning of
the line. The same applies to the end of line calculation.
* src/xdisp.c: (Fwindow_text_pixel_size): Fix off by one
* test/src/xdisp-tests.el (xdisp-tests--window-text-pixel-size): New test
(xdisp-tests--window-text-pixel-size-leading-space): New test
(xdisp-tests--window-text-pixel-size-trailing-space): New test
---
src/xdisp.c | 8 ++++++--
test/src/xdisp-tests.el | 30 ++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/src/xdisp.c b/src/xdisp.c
index 6a4304d194..6c8bd346c0 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -10650,9 +10650,10 @@ DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_siz
bpos = BEGV_BYTE;
while (bpos < ZV_BYTE)
{
- c = fetch_char_advance (&start, &bpos);
+ c = FETCH_BYTE (bpos);
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
break;
+ inc_both (&start, &bpos);
}
while (bpos > BEGV_BYTE)
{
@@ -10681,7 +10682,10 @@ DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_siz
dec_both (&end, &bpos);
c = FETCH_BYTE (bpos);
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
- break;
+ {
+ inc_both (&end, &bpos);
+ break;
+ }
}
while (bpos < ZV_BYTE)
{
diff --git a/test/src/xdisp-tests.el b/test/src/xdisp-tests.el
index d13ce77a99..ec96d777ff 100644
--- a/test/src/xdisp-tests.el
+++ b/test/src/xdisp-tests.el
@@ -72,4 +72,34 @@ xdisp-tests--minibuffer-scroll
(should (equal (nth 0 posns) (nth 1 posns)))
(should (equal (nth 1 posns) (nth 2 posns)))))
+(ert-deftest xdisp-tests--window-text-pixel-size () ;; bug#45748
+ (with-temp-buffer
+ (insert "xxx")
+ (let* ((window
+ (display-buffer (current-buffer) '(display-buffer-in-child-frame . nil)))
+ (char-width (frame-char-width))
+ (size (window-text-pixel-size nil t t)))
+ (delete-frame (window-frame window))
+ (should (equal (/ (car size) char-width) 3)))))
+
+(ert-deftest xdisp-tests--window-text-pixel-size-leading-space () ;; bug#45748
+ (with-temp-buffer
+ (insert " xx")
+ (let* ((window
+ (display-buffer (current-buffer) '(display-buffer-in-child-frame . nil)))
+ (char-width (frame-char-width))
+ (size (window-text-pixel-size nil t t)))
+ (delete-frame (window-frame window))
+ (should (equal (/ (car size) char-width) 3)))))
+
+(ert-deftest xdisp-tests--window-text-pixel-size-trailing-space () ;; bug#45748
+ (with-temp-buffer
+ (insert "xx ")
+ (let* ((window
+ (display-buffer (current-buffer) '(display-buffer-in-child-frame . nil)))
+ (char-width (frame-char-width))
+ (size (window-text-pixel-size nil t t)))
+ (delete-frame (window-frame window))
+ (should (equal (/ (car size) char-width) 3)))))
+
;;; xdisp-tests.el ends here
--
2.28.0
next prev parent reply other threads:[~2021-01-10 2:56 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-09 15:43 bug#45748: 28.0.50; fit-frame-to-buffer ignores leading spaces Aaron Jensen
2021-01-09 15:57 ` Aaron Jensen
2021-01-09 16:27 ` Aaron Jensen
2021-01-09 17:07 ` martin rudalics
2021-01-09 17:44 ` Aaron Jensen
2021-01-09 17:55 ` Aaron Jensen
2021-01-09 18:14 ` Eli Zaretskii
2021-01-10 2:56 ` Aaron Jensen [this message]
2021-01-10 16:05 ` martin rudalics
2021-01-10 17:31 ` Aaron Jensen
2021-01-10 17:49 ` martin rudalics
2021-01-10 17:51 ` Aaron Jensen
2021-01-10 17:57 ` martin rudalics
2021-01-10 17:58 ` Aaron Jensen
2021-01-13 4:34 ` Aaron Jensen
2021-01-13 14:26 ` Eli Zaretskii
2021-01-15 12:11 ` Eli Zaretskii
2021-01-15 12:34 ` Aaron Jensen
2021-01-15 13:15 ` Eli Zaretskii
2021-01-15 14:04 ` Aaron Jensen
2021-01-15 15:37 ` Eli Zaretskii
2021-01-15 17:03 ` Aaron Jensen
2021-01-09 18:03 ` Eli Zaretskii
2021-01-09 17:44 ` Eli Zaretskii
2021-01-09 17:49 ` Aaron Jensen
2021-01-09 17:07 ` martin rudalics
2021-01-16 17:12 ` bug#45748: [PATCH] * test/src/xdisp-tests.el Fix tests to work in batch mode Aaron Jensen
2021-01-16 17:57 ` Basil L. Contovounesios
2021-01-16 18:24 ` Aaron Jensen
2021-01-16 18:24 ` Aaron Jensen
2021-01-16 18:28 ` Aaron Jensen
2021-01-16 18:28 ` Aaron Jensen
2021-01-16 18:33 ` Eli Zaretskii
2021-01-18 17:04 ` 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=CAHyO48zBAA6b7h5iVV+zFxo-EzyQ3wLGmGn4AfpdTZPJMh7kJQ@mail.gmail.com \
--to=aaronjensen@gmail.com \
--cc=45748@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=rudalics@gmx.at \
/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).