[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: `window-hscroll' does not report correct value when `text-scale-mode
From: |
路客 |
Subject: |
Re: `window-hscroll' does not report correct value when `text-scale-mode' is on |
Date: |
Tue, 23 May 2017 11:37:07 +0800 |
>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