When the `track-eol' feature is doing its job, the value is
`most-positive-fixnum'.")
+(defvar last--line-number-width 0
+ "Last value of width used for displaying line numbers.
+Used internally by `line-move-visual'.")
+
(defcustom line-move-ignore-invisible t
"Non-nil means commands that move by lines ignore invisible newlines.
When this option is non-nil, \\[next-line], \\[previous-line], \\[move-end-of-line], and \\[move-beginning-of-line] behave
If NOERROR, don't signal an error if we can't move that many lines."
(let ((opoint (point))
(hscroll (window-hscroll))
+ (lnum-width (line-number-display-width t))
target-hscroll)
;; Check if the previous command was a line-motion command, or if
;; we were called from some other command.
(memq last-command `(next-line previous-line ,this-command)))
;; If so, there's no need to reset `temporary-goal-column',
;; but we may need to hscroll.
- (if (or (/= (cdr temporary-goal-column) hscroll)
- (> (cdr temporary-goal-column) 0))
- (setq target-hscroll (cdr temporary-goal-column)))
+ (progn
+ (if (or (/= (cdr temporary-goal-column) hscroll)
+ (> (cdr temporary-goal-column) 0))
+ (setq target-hscroll (cdr temporary-goal-column)))
+ ;; Update the COLUMN part of temporary-goal-column if the
+ ;; line-number display changed its width since the last
+ ;; time.
+ (setq temporary-goal-column
+ (cons (+ (car temporary-goal-column)
+ (/ (float (- lnum-width last--line-number-width))
+ (frame-char-width)))
+ (cdr temporary-goal-column)))
+ (setq last--line-number-width lnum-width))
;; Otherwise, we should reset `temporary-goal-column'.
(let ((posn (posn-at-point))
x-pos)
-1, hscroll, 0, w);
}
+/* Return the width taken by line-number display in window W. */
+static void
+line_number_display_width (struct window *w, int *width, int *pixel_width)
+{
+ if (NILP (Vdisplay_line_numbers))
+ {
+ *width = 0;
+ *pixel_width = 0;
+ }
+ else
+ {
+ struct it it;
+ struct text_pos wstart;
+ bool saved_restriction = false;
+ ptrdiff_t count = SPECPDL_INDEX ();
+ SET_TEXT_POS_FROM_MARKER (wstart, w->start);
+ void *itdata = bidi_shelve_cache ();
+ /* We must start from window's start point, but it could be
+ outside the accessible region. */
+ if (wstart.charpos < BEGV || wstart.charpos > ZV)
+ {
+ record_unwind_protect (save_restriction_restore,
+ save_restriction_save ());
+ Fwiden ();
+ saved_restriction = true;
+ }
+ start_display (&it, w, wstart);
+ move_it_by_lines (&it, 1);
+ *width = it.lnum_width;
+ *pixel_width = it.lnum_pixel_width;
+ if (saved_restriction)
+ unbind_to (count, Qnil);
+ bidi_unshelve_cache (itdata, 0);
+ }
+}
+
+DEFUN ("line-number-display-width", Fline_number_display_width,
+ Sline_number_display_width, 0, 1, 0,
+ doc: /* Return the width used for displaying line numbers in the selected window.
+If optional argument PIXELWISE is non-nil, return the width in pixels,
+otherwise return the width in columns of the face used to display
+line numbers, `line-number'. */)
+ (Lisp_Object pixelwise)
+{
+ int width, pixel_width;
+ line_number_display_width (XWINDOW (selected_window), &width, &pixel_width);
+ if (!NILP (pixelwise))
+ return make_number (pixel_width);
+ /* FIXME: The "+ 2" part knows that we add a blank on each side of
+ the line number when producing glyphs for display. */
+ return make_number (width + 2);
+}
+
/* In window W (derived from WINDOW), return x coordinate for column
COL (derived from COLUMN). */
static int
that's what normal window redisplay does. Otherwise C-n/C-p
will sometimes err by one column. */
int lnum_width = 0;
+ int lnum_pixel_width = 0;
if (!NILP (Vdisplay_line_numbers)
&& !EQ (Vdisplay_line_numbers, Qvisual))
- {
- struct text_pos wstart;
- bool saved_restriction = false;
- ptrdiff_t count1 = SPECPDL_INDEX ();
- SET_TEXT_POS_FROM_MARKER (wstart, w->start);
- itdata = bidi_shelve_cache ();
- /* We must start from window's start point, but it could be
- outside the accessible region. */
- if (wstart.charpos < BEGV || wstart.charpos > ZV)
- {
- record_unwind_protect (save_restriction_restore,
- save_restriction_save ());
- Fwiden ();
- saved_restriction = true;
- }
- start_display (&it, w, wstart);
- move_it_by_lines (&it, 1);
- lnum_width = it.lnum_width;
- if (saved_restriction)
- unbind_to (count1, Qnil);
- bidi_unshelve_cache (itdata, 0);
- }
+ line_number_display_width (w, &lnum_width, &lnum_pixel_width);
SET_TEXT_POS (pt, PT, PT_BYTE);
itdata = bidi_shelve_cache ();
start_display (&it, w, pt);
an addition to the hscroll amount. */
if (lcols_given)
{
+ /* If we are displaying line numbers, we could cross the
+ line where the width of the line-number display changes,
+ in which case we need to fix up the pixel coordinate
+ accordingly. */
+ if (lnum_pixel_width > 0)
+ to_x += it.lnum_pixel_width - lnum_pixel_width;
move_it_in_display_line (&it, ZV, first_x + to_x, MOVE_TO_X);
/* If we find ourselves in the middle of an overlay string
which includes a newline after current string position,
defsubr (&Sindent_to);
defsubr (&Scurrent_column);
defsubr (&Smove_to_column);
+ defsubr (&Sline_number_display_width);
defsubr (&Svertical_motion);
defsubr (&Scompute_motion);
}