[vim/vim] getbufinfo({expr}) doesn't return correct 'lnum' when {expr} is not currently focused (#6612)

7 views
Skip to first unread message

bfrg

unread,
Aug 3, 2020, 3:20:33 PM8/3/20
to vim/vim, Subscribed

Describe the bug
getbufinfo({expr})[0].lnum doesn't return the correct line number when {expr} is not the current buffer.

To Reproduce

  1. Run vim --clean test.vim -S test.vim with
    new foo
    call setline(1, ['1', '2', '3'])
    $
    wincmd p
    echomsg getbufinfo('foo')[0].lnum
  2. Result: Vim will print 0, but it should be 3.

Expected behavior
getbufinfo({expr})[0].lnum should always return the current cursor position in buffer {expr} even if the buffer isn't currently focused.

Environment
Vim version 8.2.1351


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Bram Moolenaar

unread,
Aug 4, 2020, 3:01:10 PM8/4/20
to vim/vim, Subscribed


> **Describe the bug**
> `getbufinfo({expr})[0].lnum` doesn't return the correct line number

> when `{expr}` is not the current buffer.
>
> **To Reproduce**
> 1. Run `vim --clean test.vim -S test.vim` with
> ```vim

> new foo
> call setline(1, ['1', '2', '3'])
> $
> wincmd p
> echomsg getbufinfo('foo')[0].lnum
> ```
> 2. Result: Vim will print `0`, but it should be `3`.

You mention "3" because the buffer is visible in the other window, where
the cursor position is line 3. But that's not the line number of the
buffer, that's the cursor position of the window.

The number you get with getbufinfo() is the line number for the current
window, if you would use ":buf foo" that's the line number being used.

I'll make the help clearer.

--
hundred-and-one symptoms of being an internet addict:
119. You are reading a book and look for the scroll bar to get to
the next page.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

bfrg

unread,
Aug 4, 2020, 3:48:23 PM8/4/20
to vim/vim, Subscribed

Okay I misunderstood then. I thought it returns the last cursor position in the given buffer.

bfrg

unread,
Aug 4, 2020, 3:48:24 PM8/4/20
to vim/vim, Subscribed

Closed #6612.

bfrg

unread,
Aug 4, 2020, 3:51:55 PM8/4/20
to vim/vim, Subscribed

But how is getbufinfo({expr})[0].lnum userful? I honestly don't understand the purpose of it since the buffer needs to be focused. Isn't it the same as line('.')?

Nick Jensen

unread,
Aug 4, 2020, 5:10:38 PM8/4/20
to vim/vim, Subscribed

@bfrg I think what Bram means is that the cursor position is related to the buffer when it is opened in the current window. You can see this with this modification to your example, which opens foo in the same window, instead of in a split:

enew
file foo
call setline(1, ['1', '2', '3'])
$
set hidden
edit #
echomsg getbufinfo('foo')[0].lnum

Bram Moolenaar

unread,
Aug 4, 2020, 5:25:06 PM8/4/20
to vim...@googlegroups.com, bfrg

> But how is `getbufinfo({expr})[0].lnum` userful? I honestly don't
> understand the purpose of it since the buffer needs to be focused.
> Isn't it the same as `line('.')`?

Try editing the buffer in the current window and then switching to
another buffer, then it works as you expect.

If the buffer is open in another window (or several windows), you
probably want to get the cursor position of the window, not the buffer.


--
hundred-and-one symptoms of being an internet addict:
121. You ask for e-mail adresses instead of telephone numbers.
Reply all
Reply to author
Forward
0 new messages