# Show file display position (rather than cursor position) in modeline?

**URL:** <https://discuss.kakoune.com/t/show-file-display-position-rather-than-cursor-position-in-modeline/2633>\
**Category:** Uncategorized\
**Created:** [January 20, 2025, 6:03pm UTC](https://discuss.kakoune.com/t/show-file-display-position-rather-than-cursor-position-in-modeline/2633 "2025-01-20T18:03:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GavinSmith0123](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/gavinsmith0123/32/1112_2.png) [@GavinSmith0123](https://discuss.kakoune.com/u/GavinSmith0123)\
**Post date:** [January 20, 2025, 6:03pm UTC](https://discuss.kakoune.com/t/show-file-display-position-rather-than-cursor-position-in-modeline/2633/1 "2025-01-20T18:03:33Z")

</div>

I currently display the percent cursor position in my modeline:

```auto
## position of the cursor in the buffer, in percent
declare-option str modeline_pos_percent
declare-option str modeline_modified

set global modelinefmt '%val{cursor_line}:%val{cursor_char_column} {{mode_info}} %opt{filetype} [%val{session}] %sh{test $kak_modified = true && echo M} %opt{modeline_pos_percent}%% {{context_info}}'

hook global WinCreate .* %{
    hook window NormalIdle .* %{ evaluate-commands %sh{
        echo "set window modeline_pos_percent '$(($kak_cursor_line * 100 / $kak_buf_line_count))'"
    } }
}

```

However, this does not update when I scroll the buffer, as the cursor does not move automatically to be visible in newer version of Kakoune. The percent indicator only updates when I e.g. click with the mouse in the window.

This is a degradation in usability as I can’t tell where I am in the file or how big the file is when scrolling up and down with the mouse wheel.

Is there any way to get the _displayed_ position rather than the cursor position? I looked at the value expansions in “`:doc expansions`” but none looked appropriate.

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [January 21, 2025, 9:28am UTC](https://discuss.kakoune.com/t/show-file-display-position-rather-than-cursor-position-in-modeline/2633/2 "2025-01-21T09:28:38Z")

</div>

The expansion you’re looking for is `%val{window_range}`. Unfortunately, it represents the window coordinates as of the _previous_ redraw, and may be inaccurate about the _next_ redraw. In addition, it contains uninitialised values before the _first_ redraw, so if you try to use it in a `WinCreate` or `WinDisplay` hook, your plugin might think it has a window trillions of lines high, or a negative height (see [bug 4975](https://github.com/mawww/kakoune/issues/4975) for details).

It should be fine in NormalIdle, though.

---

<div class="post-metadata">

**Author:** ![GavinSmith0123](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/gavinsmith0123/32/1112_2.png) [@GavinSmith0123](https://discuss.kakoune.com/u/GavinSmith0123)\
**Post date:** [January 21, 2025, 6:20pm UTC](https://discuss.kakoune.com/t/show-file-display-position-rather-than-cursor-position-in-modeline/2633/3 "2025-01-21T18:20:51Z")

</div>

Thank you, that helps!

Here’s the code I’ve come up with to extract the needed values and get the percentage value:

```auto
hook global WinCreate .* %{
    hook window NormalIdle .* %{ evaluate-commands %sh{
        percent=$(echo $kak_buf_line_count $kak_window_range \
          | perl -wanle \
'my $top = $F[1]; 
my $height = $F[3]; 
my $length = $F[0]; 
print $top >= $length - $height ? 100 : int($top / ($length - $height) * 100);' 
        ) ;
        echo "set window modeline_pos_percent '$percent'"
    } }
}

```
