Getting/setting mark/cursor position using character index instead of byte index

20 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Jan 7, 2021, 12:51:29 PM1/7/21
to vim_dev
Hi all,

The getpos(), col() and getcurpos() functions currently return the byte index
in a line. The setpos() and cursor() functions use the byte index to position
the cursor or to set a mark.

The language server protocol uses character index instead of byte index
in a line. I see that the various LSP Vim plugins have created their own
utility functions to convert between the byte index and the character index.
It will be better to extend the built-in function to use character indexes,
so that multiple plugins can use this.

What do you think about enhancing these functions like below?

getpos({expr}, [charcol])
col({expr}, [charcol])
setpos({expr}, {list}, [charcol])
cursor({list}, [charcol])
getcurpos([{winid} [, charcoal]})

In the above functions, if 'charcol' is set to true, then the returned column
number will be a character index. If 'charcol' is false or not specified
(the default), then the returned column number will be a byte index.

Thanks,
Yegappan

Bram Moolenaar

unread,
Jan 7, 2021, 4:04:37 PM1/7/21
to vim...@googlegroups.com, Yegappan Lakshmanan
Using charidx() requires the string to be passed, so that would be a
complicated workaround.

Instead of adding a 0/1/true/false argument we could add new functions:

getcharpos()
charcol()
setcharpos()
getcursorcharpos()
setcursorcharpos()

It's easier to see that "char" is used, compared to an argument that
doesn't say what it's for.

--
hundred-and-one symptoms of being an internet addict:
103. When you find yourself in the "Computer" section of Barnes & Noble
enjoying yourself.

/// 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 ///

Yegappan Lakshmanan

unread,
Jan 7, 2021, 4:12:37 PM1/7/21
to Bram Moolenaar, vim_dev
Hi Bram,

Another option is to introduce two new functions to convert between
the byte index and the character index in a buffer line:

buf_byteidx(bufnr, lnum, charidx)
buf_charidx(bufnr, lnum, byteidx)

The buf_byteidx() function returns the byte index for the given character
index in the specified line and buf_charidx() does the opposite.

What do you think about this approach?

Thanks,
Yegappan

Bram Moolenaar

unread,
Jan 7, 2021, 4:31:11 PM1/7/21
to vim...@googlegroups.com, Yegappan Lakshmanan
What is it going to look like? You need this for your plugin, so you
can write the script lines to see how it would work.

--
hundred-and-one symptoms of being an internet addict:
105. When someone asks you for your address, you tell them your URL.

Yegappan Lakshmanan

unread,
Jan 9, 2021, 3:38:49 PM1/9/21
to Bram Moolenaar, vim_dev
Hi Bram,

On Thu, Jan 7, 2021 at 1:04 PM Bram Moolenaar <Br...@moolenaar.net> wrote:
I have created PR #7648 (https://github.com/vim/vim/pull/7648) to implement
these functions.

Regards,
Yegappan

Reply all
Reply to author
Forward
0 new messages