Skip to content

Provide a function for converting display columns to byte indexes #10098

@bfrg

Description

@bfrg

Is your feature request about something that is currently impossible or hard to do? Please describe the problem.

Vim doesn't provide any functions to convert display (screen) columns to byte columns. Some compilers and linters print the locations in display columns. While this doesn't matter for the quickfix list (errorformat needs %v instead of %c), the byte index is nevertheless necessary when highlighting the quickfix locations with text properties.

Describe the solution you'd like

It would be nice to have a function like

display2byte({string}, {col})

that returns the byte index of a display column, i.e. the opposite of strdisplaywidth({string}). Other names I've considered were virt2byte().

Alternatively, maybe byteidx({string}, {nr}) could be extended with an optional parameter {display}, that, if set to true treats {nr} like a display column and not character index.

Describe alternatives you've considered

The following works but looks weird and is probably not very efficient:

vim9script

export def Display2byte(str: string, virtcol: number): number
    const ts_old: number = &tabstop
    &tabstop = 8
    var col: number
    try
        col = match(str, printf('\%%%dv', virtcol)) + 1
    finally
        &tabstop = ts_old
    endtry
    return col
enddef

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions