[vim/vim] Vim9: cannot set '*expr' option to autoload import (Issue #9584)

2 views
Skip to first unread message

lacygoill

unread,
Jan 21, 2022, 9:23:00 AMJan 21
to vim/vim, Subscribed

Steps to reproduce

Run this shell command:

vim -Nu NONE -S <(tee <<'EOF'
    vim9script
    var lines =<< trim END
        # first

        aaa
        aaa
        aaa

        # second

        bbb
        bbb
        bbb

        # third

        ccc
        ccc
        ccc
    END
    var file = '/tmp/file'
    lines->writefile(file)
    execute 'noswapfile edit ' .. file

    var dir = '/tmp/.vim'
    &runtimepath = dir
    dir ..= '/autoload'
    dir->mkdir('p')
    lines =<< trim END
        vim9script
        export def Expr(): string
            return getline(v:lnum) =~ '^#' ? '>1' : '1'
        enddef
    END
    lines->writefile(dir .. '/fold.vim')

    import autoload 'fold.vim'
    &foldexpr = 'fold.Expr()'
    &foldmethod = 'expr'
    &debug = 'throw'
EOF
)

E121 is given 17 times (i.e. once for each line in the buffer):

E121: Undefined variable: fold

The lines are not folded.

Expected behavior

No error is given. The lines are folded.

Version of Vim

8.2 Included patches: 1-4171

Environment

Operating system: Ubuntu 20.04.3 LTS
Terminal: xterm
Value of $TERM: xterm-256color
Shell: zsh 5.8

Additional context

If we replace this line:

&foldexpr = 'fold.Expr()'

With this line:

&foldexpr = 'fold#Expr()'

The issue disappears:

vim9script
var lines =<< trim END
    # first

    aaa
    aaa
    aaa

    # second

    bbb
    bbb
    bbb

    # third

    ccc
    ccc
    ccc
END
var file = '/tmp/file'
lines->writefile(file)
execute 'noswapfile edit ' .. file

var dir = '/tmp/.vim'
&runtimepath = dir
dir ..= '/autoload'
dir->mkdir('p')
lines =<< trim END
    vim9script
    export def Expr(): string
        return getline(v:lnum) =~ '^#' ? '>1' : '1'
    enddef
END
lines->writefile(dir .. '/fold.vim')

import autoload 'fold.vim'
&foldexpr = 'fold#Expr()'
&foldmethod = 'expr'
&debug = 'throw'


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9584@github.com>

Bram Moolenaar

unread,
Jan 21, 2022, 10:26:51 AMJan 21
to vim/vim, Subscribed

More generally, the expression cannot access anything in the script file.
A good solution appears to be that the expression is evaluated in the context of the script where the option was set.
I don't think this will have backwards compatibility problems, previously the expression could be evaluated anywhere.

This probably also applies to other "expr" options, but let's do 'foldexpr' first.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9584/1018609538@github.com>

Bram Moolenaar

unread,
Jan 21, 2022, 11:32:11 AMJan 21
to vim/vim, Subscribed

Closed #9584 via e70dd11.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/9584/issue_event/5933268745@github.com>

Reply all
Reply to author
Forward
0 new messages