Given recent discussion around matchaddpos() and the fact that converting
virtual column to byte offset has a larger variety of use cases (I personally
needed this to get <C-v>-selected block without altering marks, registers,
cursor position, etc) I propose the new function colnr():
colnr :: (string, number, options) -> col
This function accepts number: generic index and translates it to byte offset
according to given options which may be one of the following:
1. `'col'`: returns `number` or length of the string plus one, whichever is
smaller. Alias to {'unicode': 0, 'tabstop': 1, 'fixed_width_tab': 1}
2. `'codepoint'`: returns byte offset of Unicode codepoint with index given as
`number` (here and below index starts from 1). Alias to {'tabstop': 1,
'fixed_width_tab': 1, 'ambiwidth': 'single', 'fullwidth_len': 1,
'diacritics_len': 1, 'invalid_unicode': 'error'}. Is opposite of `strchars()`.
3. `'virtcol'`: alias to {} which is the same as {'tabstop': &tabstop,
'fixed_width_tab': 0, 'ambiwidth': &ambiwidth, 'fullwidth_len': 2,
'diacritics_len': 0, 'diacritics_start_len': 1, 'unicode': 1,
'invalid_unicode': 'strtrans'}.
4. A dictionary with the following keys:
- tabstop: effective value of &tabstop. All defaults are listed in 3.
- fixed_width_tab: if non-zero, makes every tab be assumed to occupy
`tabstop` columns.
- ambiwidth: effective value of &ambiwidth.
- fullwidth_len: number of indexes fullwidth characters occupy.
- diacritics_len: number of indexes diacritics characters occupy.
- diacritics_start_len: length of the one diacritic character that is at
the start of the string.
- unicode: if non-zero, recognizes one unicode character as occupying one
index. Disables `ambiwidth`, `fullwidth_len`, `diacritics_len`,
`diacritics_start_len` and `invalid_unicode` options.
- invalid_unicode: determines how to treat bytes that are not valid
unicode. Valid values are
- strtrans: use length of the `strtrans` result.
- error: fail with error, return zero.
- single: treat as occupying one index.
Use-cases:
1. Transform screen column to byte offset:
echo colnr("\ta", 2, 'virtcol') " Returns 1 since 2 is in the middle
of the tab
2. Transform screen column reported by some compiler to byte offset:
echo colnr(string, idx, {'tabstop': 8})
3. Transform screen column reported by some compiler that only knows ASCII and
still things 1 character == 1 byte:
echo colnr(string, idx, {'tabstop': 8, 'unicode': 0})
4. Transform index of unicode() string reported by some python program:
echo colnr(string, idx + 1, 'codepoint')
5. Allow configuring transformer without :if branches: allow byte index
echo colnr(string, idx, 'col') " Will echo idx most of time
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.