Skip to content

Undefined variable when redirecting command output into a string variable #33

@Yilin-Yang

Description

@Yilin-Yang

Running vim-markbar's test suite keeps throwing error messages like:

"~/plugin/vim-markbar/plugin/vim-markbar.vim" line 1 of 332 --0%-- col 1
Error detected while processing function markbar#helpers#GetOpenBuffers:
line    2:
E121: Undefined variable: l:to_return

That don't seem to appear during normal use on my machine. This might be related to an issue mentioned by @mwgkgk in #3 (comment)

Relevant lines of code (that also don't reference a variable named l:to_return):

" RETURNS:  (v:t_list)      A list populated with the numbers of every
"                           buffer, listed or unlisted.
function! markbar#helpers#GetOpenBuffers() abort
    let l:buffers_str = ''
    redir => l:buffers_str
    silent ls!
    redir end

Lower down in the same file:

" RETURNS:  (v:t_string)    All buffer-local marks active within the current
"                           file as a 'raw' string.
function! markbar#helpers#GetLocalMarks() abort
    let l:to_return = ''
    try
        redir => l:to_return
        silent marks abcdefghijklmnopqrstuvwxyz[]<>'`\"^.(){}
        redir end
        let l:to_return .= "\n"

When I change the above into:

" RETURNS:  (v:t_string)    All buffer-local marks active within the current
"                           file as a 'raw' string.
function! markbar#helpers#GetLocalMarks() abort
    let l:FromGetLocalMarks = ''
    try
        redir => l:FromGetLocalMarks
        silent marks abcdefghijklmnopqrstuvwxyz[]<>'`\"^.(){}
        redir end
        let l:FromGetLocalMarks .= "\n"
    catch /E283/
        let l:FromGetLocalMarks = 'mark line  col file/text\n'
    endtry
    for l:mark in ['(',')','{','}']
        try
            let l:FromGetLocalMarks .= markbar#helpers#MakeMarkString(l:mark)."\n"
        catch
        endtry
    endfor
    return l:FromGetLocalMarks
endfunction

I see error messages like:

"10lines.txt" line 9 of 10 --90%-- col 1
Error detected while processing VimEnter Autocommands for "*"..function MarkbarVimEnter[12]..markbar#helpers#GetOpenBuffers:
line    2:
E121: Undefined variable: l:FromGetLocalMarks

And actual test failures like:

     (4/8) [     DO] (X) Vim(redir):E121: Undefined variable: l:to_return
       > command line..function <SNR>41_vader[5]..vader#run[63]..<SNR>43_run[46]..vader#window#replay[2]..<SNR>28_Apostrophe[1]..<SNR>28_OpenPeekaboo[13]..markbar#MarkbarController#openMarkbar[11]..markbar#MarkbarController#refreshContents[6]..markbar#MarkbarModel#updateCurrentAndGlobal[26]..markbar#helpers#GetLocalMarks, line 3

This occurs in both vim and neovim.

This may be an upstream issue in which the redir => command isn't properly remembering which variables were declared in which stack frames.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions