unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* `window-hscroll' does not report correct value when `text-scale-mode' is on
@ 2017-05-18  7:38 路客
  2017-05-18 15:10 ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: 路客 @ 2017-05-18  7:38 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 1310 bytes --]

Hi all,

I found `window-hscroll' function does not report a correct value
if `text-scale-mode' is on. A simple test is to enable `ruler-mode'
and edit something like the following "text-ruler":

.........1.........2.........3.........4.........5.........6...
123456789012345678901234567890123456789012345678901234567890...

Now we add a next line with 500 characters and put the cursor at
the end of it; then we use <shift-left_mouse> to decrease the font
size *twice* (with text wrapping disabled and ruler-mode enabled
to observe current `window-hscroll' value). We can now easily see
the `window-hscroll' value is incorrect, by comparing against the
above "text-ruler".

After some experiments I derived the following function that help a
bit but still not a complete answer:

(defun my-hscroll ()
  (round (/ (window-hscroll)
            (or (and (boundp 'text-scale-mode-remapping)
                     (caddr text-scale-mode-remapping))
                1))))

`my-hscroll' function can report a value that is closer to the correct
hscroll value. But the error of `my-hscroll' becomes larger if we again
decrease the font size, or purposely edit a line with 2000 characters.

Is there any way to get the "exact" hscroll value? Say, with pixel-wise
measurement?

Thanks a lot.

-- 
Best regards,
Luke Lee

[-- Attachment #2: Type: text/html, Size: 3172 bytes --]

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

* Re: `window-hscroll' does not report correct value when `text-scale-mode' is on
  2017-05-18  7:38 `window-hscroll' does not report correct value when `text-scale-mode' is on 路客
@ 2017-05-18 15:10 ` Eli Zaretskii
  2017-05-19  1:10   ` 路客
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2017-05-18 15:10 UTC (permalink / raw)
  To: 路客; +Cc: emacs-devel

> From: 路客 <luke.yx.lee@gmail.com>
> Date: Thu, 18 May 2017 15:38:47 +0800
> 
> I found `window-hscroll' function does not report a correct value
> if `text-scale-mode' is on. A simple test is to enable `ruler-mode' 
> and edit something like the following "text-ruler":
> 
> .........1.........2.........3.........4.........5.........6...
> 123456789012345678901234567890123456789012345678901234567890...
> 
> Now we add a next line with 500 characters and put the cursor at 
> the end of it; then we use <shift-left_mouse> to decrease the font 
> size *twice* (with text wrapping disabled and ruler-mode enabled 
> to observe current `window-hscroll' value). We can now easily see 
> the `window-hscroll' value is incorrect, by comparing against the
> above "text-ruler".

This is not a bug: window-hscroll reports its value in units of
canonical frame columns, so the value should indeed change when you
change the font size.

> Is there any way to get the "exact" hscroll value? Say, with pixel-wise
> measurement?

The value you get now is already exact, and is exactly equivalent to
the corresponding number of pixels.  If you really need that in pixel
units, simply multiply by the value returned by frame-char-width.



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

* Re: `window-hscroll' does not report correct value when `text-scale-mode' is on
  2017-05-18 15:10 ` Eli Zaretskii
@ 2017-05-19  1:10   ` 路客
  2017-05-19  6:39     ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: 路客 @ 2017-05-19  1:10 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 2229 bytes --]

You are right, convert it to pixel units won't fix it. Maybe
I should change my question to either of the following:

1. How should we fix `ruler-mode' to make it correct when
   `text-scale-mode' is ON?
2. When `text-scale-mode' is ON, how can I get the correct
    `window-hscroll' value respect to current text scaling?

My earlier `my-hscroll' try to calculate the scaled
`window-hscroll' but it's not always correct:

(defun my-hscroll ()
  (round (/ (window-hscroll)
            (or (and (boundp 'text-scale-mode-remapping)
                     (caddr text-scale-mode-remapping))
                1))))
How should I use the `text-scale-mode-remapping' to do this?
Anyone who is expert in `text-scale-mode' please help. I will
later try to fix ruler-mode accordingly.

Thanks!

Best regards,
Luke Lee

2017-05-18 23:10 GMT+08:00 Eli Zaretskii <eliz@gnu.org>:

> > From: 路客 <luke.yx.lee@gmail.com>
> > Date: Thu, 18 May 2017 15:38:47 +0800
> >
> > I found `window-hscroll' function does not report a correct value
> > if `text-scale-mode' is on. A simple test is to enable `ruler-mode'
> > and edit something like the following "text-ruler":
> >
> > .........1.........2.........3.........4.........5.........6...
> > 123456789012345678901234567890123456789012345678901234567890...
> >
> > Now we add a next line with 500 characters and put the cursor at
> > the end of it; then we use <shift-left_mouse> to decrease the font
> > size *twice* (with text wrapping disabled and ruler-mode enabled
> > to observe current `window-hscroll' value). We can now easily see
> > the `window-hscroll' value is incorrect, by comparing against the
> > above "text-ruler".
>
> This is not a bug: window-hscroll reports its value in units of
> canonical frame columns, so the value should indeed change when you
> change the font size.
>
> > Is there any way to get the "exact" hscroll value? Say, with pixel-wise
> > measurement?
>
> The value you get now is already exact, and is exactly equivalent to
> the corresponding number of pixels.  If you really need that in pixel
> units, simply multiply by the value returned by frame-char-width.
>



-- 
Best regards,
Luke Lee

[-- Attachment #2: Type: text/html, Size: 4310 bytes --]

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

* Re: `window-hscroll' does not report correct value when `text-scale-mode' is on
  2017-05-19  1:10   ` 路客
@ 2017-05-19  6:39     ` Eli Zaretskii
  2017-05-23  3:37       ` 路客
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2017-05-19  6:39 UTC (permalink / raw)
  To: 路客; +Cc: emacs-devel

> From: 路客 <luke.yx.lee@gmail.com>
> Date: Fri, 19 May 2017 09:10:23 +0800
> Cc: Emacs developers <emacs-devel@gnu.org>
> 
> 1. How should we fix `ruler-mode' to make it correct when 
> `text-scale-mode' is ON?

I don't think I understand what needs fixing.  Is this question
different from the second one, i.e. if you can compute a suitable
value requested in your second question, is there something else that
needs fixing in ruler-mode?  If so, what else needs fixing?

> 2. When `text-scale-mode' is ON, how can I get the correct 
> `window-hscroll' value respect to current text scaling?

I think you can do this:

  (* (/ (window-hscroll) (frame-char-width)) (default-font-width))

I should add that default-font-width can sometimes be expensive, so
don't put the above into some tight loop.



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

* Re: `window-hscroll' does not report correct value when `text-scale-mode' is on
  2017-05-19  6:39     ` Eli Zaretskii
@ 2017-05-23  3:37       ` 路客
  0 siblings, 0 replies; 5+ messages in thread
From: 路客 @ 2017-05-23  3:37 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Emacs developers

>I don't think I understand what needs fixing.  Is this question
>different from the second one, i.e. if you can compute a suitable
>value requested in your second question, is there something else that
>needs fixing in ruler-mode?  If so, what else needs fixing?

There are three problems result from the same reason:

First, when you use text-scale-mode (S-leftmouse) to decrease font
size, you will first see the ruler becomes smaller than the
`window-width'.

Second, when you move the cursor rightwards, you will see the
"indicator" of the cursor not at the correct x-coordinate as our cursor.

Third, when you edit a long line with line-wrap mode off, the
`window-hscroll' value it used is not scaled so the ruler will start
showing incorrect positions. It get more worse as the line grows longer.


>  (* (/ (window-hscroll) (frame-char-width)) (default-font-width))

Cool! After some experiment I think it should be

(/ (* (window-hscroll) (frame-char-width)) (default-font-width))

This can give the the *exact* value of scaled window-hscroll. I
tried to round it up to the closest integer but it seems not
required (I am not 100% sure).

Here is my patch to ruler-mode, please review my changes. I don't
think my function names are good. Thanks!
--------------

diff --git a/lisp/ruler-mode.el b/lisp/ruler-mode.el
index 4f09a1887f5..36d399c7c91 100644
--- a/lisp/ruler-mode.el
+++ b/lisp/ruler-mode.el
@@ -360,6 +360,21 @@ ruler-mode-dragged-symbol
 That is `fill-column', `comment-column', `goal-column', or nil when
 nothing is dragged.")

+(defun ruler-mode-text-scaled-width (width)
+  "Compute scaled text width according to current font scaling.
+Convert a width of char units into a text-scaled char width units,
+Ex. `window-hscroll'. The return value is rounded up to the closest
+integer."
+  (/ (* width (frame-char-width)) (default-font-width)))
+
+(defun ruler-mode-window-hscroll ()
+  "Text scaled `window-hscroll'."
+  (ruler-mode-text-scaled-width (window-hscroll)))
+
+(defun ruler-mode-window-width ()
+  "Text scaled `window-width'."
+  (ruler-mode-text-scaled-width (window-width)))
+
 (defun ruler-mode-mouse-grab-any-column (start-event)
   "Drag a column symbol on the ruler.
 Start dragging on mouse down event START-EVENT, and update the column
@@ -372,9 +387,9 @@ ruler-mode-mouse-grab-any-column
     (save-selected-window
       (select-window (posn-window start))
       (setq col  (ruler-mode-window-col (car (posn-col-row start)))
-            newc (+ col (window-hscroll)))
+            newc (+ col (ruler-mode-window-hscroll)))
       (and
-       (>= col 0) (< col (window-width))
+       (>= col 0) (< col (ruler-mode-window-width))
        (cond

         ;; Handle the fill column.
@@ -457,8 +472,8 @@ ruler-mode-mouse-drag-any-column
     (save-selected-window
       (select-window (posn-window start))
       (setq col  (ruler-mode-window-col (car (posn-col-row end)))
-            newc (+ col (window-hscroll)))
-      (when (and (>= col 0) (< col (window-width)))
+            newc (+ col (ruler-mode-window-hscroll)))
+      (when (and (>= col 0) (< col (ruler-mode-window-width)))
         (set ruler-mode-dragged-symbol newc)))))

 (defun ruler-mode-mouse-add-tab-stop (start-event)
@@ -473,8 +488,8 @@ ruler-mode-mouse-add-tab-stop
         (save-selected-window
           (select-window (posn-window start))
           (setq col (ruler-mode-window-col (car (posn-col-row start)))
-                ts  (+ col (window-hscroll)))
-          (and (>= col 0) (< col (window-width))
+                ts  (+ col (ruler-mode-window-hscroll)))
+          (and (>= col 0) (< col (ruler-mode-window-width))
                (not (member ts tab-stop-list))
                (progn
                  (message "Tab stop set to %d" ts)
@@ -494,8 +509,8 @@ ruler-mode-mouse-del-tab-stop
         (save-selected-window
           (select-window (posn-window start))
           (setq col (ruler-mode-window-col (car (posn-col-row start)))
-                ts  (+ col (window-hscroll)))
-          (and (>= col 0) (< col (window-width))
+                ts  (+ col (ruler-mode-window-hscroll)))
+          (and (>= col 0) (< col (ruler-mode-window-width))
                (member ts tab-stop-list)
                (progn
                  (message "Tab stop at %d deleted" ts)
@@ -648,11 +663,11 @@ ruler-mode-space

 (defun ruler-mode-ruler ()
   "Compute and return a header line ruler."
-  (let* ((w (window-width))
+  (let* ((w (ruler-mode-window-width))
          (m (window-margins))
          (f (window-fringes))
          (i 0)
-         (j (window-hscroll))
+         (j (ruler-mode-window-hscroll))
          ;; Setup the scrollbar, fringes, and margins areas.
          (lf (ruler-mode-space
               'left-fringe


Best regards,
Luke Lee



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

end of thread, other threads:[~2017-05-23  3:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-18  7:38 `window-hscroll' does not report correct value when `text-scale-mode' is on 路客
2017-05-18 15:10 ` Eli Zaretskii
2017-05-19  1:10   ` 路客
2017-05-19  6:39     ` Eli Zaretskii
2017-05-23  3:37       ` 路客

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).