= (desired_cursor_row->enabled_p
? desired_cursor_row
: current_cursor_row);
+ int row_r2l_p = cursor_row->reversed_p;
text_area_width = window_box_width (w, TEXT_AREA);
h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
- && ((XFASTINT (w->hscroll)
- && w->cursor.x <= h_margin)
- || (cursor_row->enabled_p
- && cursor_row->truncated_on_right_p
- && (w->cursor.x >= text_area_width - h_margin))))
+ /* For left-to-right rows, hscroll when cursor is either
+ (i) inside the right hscroll margin, or (ii) if it is
+ inside the left margin and the window is already
+ hscrolled. */
+ && ((!row_r2l_p
+ && ((XFASTINT (w->hscroll)
+ && w->cursor.x <= h_margin)
+ || (cursor_row->enabled_p
+ && cursor_row->truncated_on_right_p
+ && (w->cursor.x >= text_area_width - h_margin))))
+ /* For right-to-left rows, the logic is similar,
+ except that rules for scrolling to left and right
+ are reversed. E.g., if cursor.x <= h_margin, we
+ need to hscroll "to the right" unconditionally,
+ and that will scroll the screen to the left so as
+ to reveal the next portion of the row. */
+ || (row_r2l_p
+ && ((cursor_row->enabled_p
+ /* FIXME: It is confusing to set the
+ truncated_on_right_p flag when R2L rows
+ are actually truncated on the left. */
+ && cursor_row->truncated_on_right_p
+ && w->cursor.x <= h_margin)
+ || (XFASTINT (w->hscroll)
+ && (w->cursor.x >= text_area_width - h_margin))))))
{
struct it it;
int hscroll;
? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
: (text_area_width / 2))))
/ FRAME_COLUMN_WIDTH (it.f);
- else if (w->cursor.x >= text_area_width - h_margin)
+ else if ((!row_r2l_p
+ && w->cursor.x >= text_area_width - h_margin)
+ || (row_r2l_p && w->cursor.x <= h_margin))
{
if (hscroll_relative_p)
wanted_x = text_area_width * (1 - hscroll_step_rel)