[vim/vim] Add support for sourcing a Vim9 script from a buffer (PR #9974)

28 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Mar 19, 2022, 4:56:38 PM3/19/22
to vim/vim, Subscribed

You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/9974

Commit Summary

  • 146c989 Add support for sourcing a Vim9 script from a buffer

File Changes

(4 files)

Patch Links:


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/pull/9974@github.com>

codecov[bot]

unread,
Mar 19, 2022, 5:04:01 PM3/19/22
to vim/vim, Subscribed

Codecov Report

Merging #9974 (146c989) into master (47c532e) will decrease coverage by 1.05%.
The diff coverage is 72.72%.

@@            Coverage Diff             @@

##           master    #9974      +/-   ##

==========================================

- Coverage   81.90%   80.85%   -1.06%     

==========================================

  Files         167      152      -15     

  Lines      187295   174465   -12830     

  Branches    42207    39552    -2655     

==========================================

- Hits       153405   141062   -12343     

+ Misses      21540    20751     -789     

- Partials    12350    12652     +302     
Flag Coverage Δ
huge-clang-none 82.31% <72.72%> (+<0.01%) ⬆️
huge-gcc-none ?
huge-gcc-testgui ?
huge-gcc-unittests 2.01% <0.00%> (-0.01%) ⬇️
linux 80.85% <72.72%> (-3.04%) ⬇️
mingw-x64-HUGE ?
mingw-x64-HUGE-gui ?
windows ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/ex_docmd.c 88.18% <66.66%> (-3.42%) ⬇️
src/scriptfile.c 82.87% <73.68%> (-0.61%) ⬇️
src/libvterm/src/rect.h 0.00% <0.00%> (-96.78%) ⬇️
src/libvterm/src/state.c 34.80% <0.00%> (-54.77%) ⬇️
src/libvterm/src/keyboard.c 40.00% <0.00%> (-47.63%) ⬇️
src/libvterm/include/vterm.h 0.00% <0.00%> (-44.45%) ⬇️
src/libvterm/src/parser.c 55.41% <0.00%> (-40.49%) ⬇️
src/libvterm/src/pen.c 44.37% <0.00%> (-39.90%) ⬇️
src/libvterm/src/encoding.c 37.37% <0.00%> (-36.16%) ⬇️
src/libvterm/src/vterm.c 39.17% <0.00%> (-27.21%) ⬇️
... and 148 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 47c532e...146c989. Read the comment docs.


Reply to this email directly, view it on GitHub, or unsubscribe.


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/pull/9974/c1073111464@github.com>

Yegappan Lakshmanan

unread,
Mar 19, 2022, 9:13:38 PM3/19/22
to vim/vim, Push

@yegappan pushed 1 commit.

  • a302b53 Simplify the code by reusing the getsourceline() function and removing the new source_getbufline() and concat_continued_line()


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/pull/9974/push/9396377931@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 1:52:02 AM3/20/22
to vim/vim, Push

@yegappan pushed 1 commit.

  • 8285f49 Simplify the code by using do_source() for reading the lines from a buffer and remove the do_sourcebuffer() and cmd_source_buffer() functions.


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/pull/9974/push/9397060627@github.com>

Maxim Kim

unread,
Mar 20, 2022, 4:19:40 AM3/20/22
to vim/vim, Subscribed

Can it source the following:

vim9script

def Hello()
   var myList = ['multi',
            'lined',
            'list'
            ]
   echo myList[0]
enddef

nnoremap <F5> <ScriptCmd>Hello()<CR>
  • :4,7source
  • :11source -- would proper script context be used here?
    ?


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/pull/9974/c1073196471@github.com>

Bram Moolenaar

unread,
Mar 20, 2022, 6:43:43 AM3/20/22
to vim/vim, Subscribed

We can keep using sourcing_a_script(), hide the details of how that is decided.

I don't think we should try to be clever and detect the kind of script. If someone sources some sequence of buffer lines we can't really know whether they are legacy or Vim9 script. They could even be some lines in a help file. We can require using "vim9cmd source". Unless "vim9script" is the first command in the range of lines.

We also need to define what happens when sourcing from the same buffer again. I suppose this would work like "vim9script noclear". That way you could, for example, source a function again after modifying it.


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/pull/9974/c1073222021@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 10:53:28 AM3/20/22
to vim/vim, Push

@yegappan pushed 1 commit.

  • fb2cafe Restore the sourcing_a_script() function


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/pull/9974/push/9398811608@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 10:54:05 AM3/20/22
to vim/vim, Push

@yegappan pushed 2 commits.

  • a7d105b Add support for sourcing a Vim9 script from a buffer
  • 823a6a1 Restore the sourcing_a_script() function


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/pull/9974/push/9398813954@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 10:59:22 AM3/20/22
to vim/vim, Push

@yegappan pushed 1 commit.

  • fbb2eaf Add a test for sourcing a Vim9 script twice


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/pull/9974/push/9398834062@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 11:00:46 AM3/20/22
to vim_dev, reply+ACY5DGCYNEHWDY3757...@reply.github.com, vim/vim, Subscribed
Hi Bram,

On Sun, Mar 20, 2022 at 3:43 AM Bram Moolenaar <vim-dev...@256bit.org> wrote:

We can keep using sourcing_a_script(), hide the details of how that is decided.

I have updated the PR to restore this function.
 

I don't think we should try to be clever and detect the kind of script. If someone sources some sequence of buffer lines we can't really know whether they are legacy or Vim9 script. They could even be some lines in a help file. We can require using "vim9cmd source". Unless "vim9script" is the first command in the range of lines.

Currently the code relies on the first sourced line to detect the script type and
doesn't try to detect the type of script.
 

We also need to define what happens when sourcing from the same buffer again. I suppose this would work like "vim9script noclear". That way you could, for example, source a function again after modifying it.


Yes. I have added a test for sourcing a Vim9 script with a function multiple times.

- Yegappan
 

vim-dev ML

unread,
Mar 20, 2022, 11:01:01 AM3/20/22
to vim/vim, vim-dev ML, Your activity

Hi Bram,

On Sun, Mar 20, 2022 at 3:43 AM Bram Moolenaar ***@***.***>


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/pull/9974/c1073269364@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 11:01:51 AM3/20/22
to vim_dev, reply+ACY5DGAY5GJGKQWQS7...@reply.github.com, vim/vim, Subscribed
Hi,

On Sun, Mar 20, 2022 at 1:19 AM Maxim Kim <vim-dev...@256bit.org> wrote:
>
> Can it source the following:
>
> vim9script
>
> def Hello()
> var myList = ['multi',
> 'lined',
> 'list'
> ]
> echo myList[0]
> enddef
>
> nnoremap <F5> <ScriptCmd>Hello()<CR>
>
> :4,7source
> :11source -- would proper script context be used here?
> ?
>

To source a Vim9 script from a buffer, it must start with "vim9script"
as the first line.
For a legacy script, you can source any range of lines.

- Yegappan

Bram Moolenaar

unread,
Mar 20, 2022, 11:13:31 AM3/20/22
to vim/vim, vim-dev ML, Comment

There is a remark in the help about using the same SID when sourcing multiple times.
It would be good to say a few things about what happens when redefining a function. I suppose that works.
But other items from the script are still there, thus it would work like "vim9script noclear".
As is explained at "vim9-reload".

When sourcing just a few lines, e.g. from a help buffer, "vim9cmd source" should work.
Please add a test for that.


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 commented.Message ID: <vim/vim/pull/9974/c1073272199@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 11:18:16 AM3/20/22
to vim/vim, vim-dev ML, Push

@yegappan pushed 1 commit.

  • 13660d3 Use the correct script line number when sourcing a range of lines


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/pull/9974/push/9398908809@github.com>

Shane-XB-Qian

unread,
Mar 20, 2022, 12:18:35 PM3/20/22
to vim/vim, vim-dev ML, Comment

#9967

// let me add a refer/remark to there to show the diff with legacy one.

--
shane.xb.qian


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 commented.Message ID: <vim/vim/pull/9974/c1073284212@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 3:19:48 PM3/20/22
to vim/vim, vim-dev ML, Push

@yegappan pushed 1 commit.

  • 6d0e1f8 Add support for sourcing a script using the vim9cmd modifier. Add additional tests and update doc


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/pull/9974/push/9399816133@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 7:59:07 PM3/20/22
to vim/vim, vim-dev ML, Push

@yegappan pushed 1 commit.


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/pull/9974/push/9400749139@github.com>

Yegappan Lakshmanan

unread,
Mar 20, 2022, 10:39:11 PM3/20/22
to vim/vim, vim-dev ML, Push

@yegappan pushed 1 commit.

  • a70a87d Use IObuff instead of a local variable


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/pull/9974/push/9401346418@github.com>

Bram Moolenaar

unread,
Mar 21, 2022, 3:45:48 PM3/21/22
to vim/vim, vim-dev ML, Comment

Closed #9974 via 85b43c6.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/9974/issue_event/6278405591@github.com>

vim-dev ML

unread,
Oct 11, 2022, 5:12:12 AM10/11/22
to vim/vim, vim-dev ML, Your activity

Hi,


On Sun, Mar 20, 2022 at 1:19 AM Maxim Kim ***@***.***> wrote:
>
> Can it source the following:
>
> vim9script
>
> def Hello()
> var myList = ['multi',
> 'lined',
> 'list'
> ]
> echo myList[0]
> enddef
>
> nnoremap <F5> <ScriptCmd>Hello()<CR>
>
> :4,7source
> :11source -- would proper script context be used here?
> ?
>

To source a Vim9 script from a buffer, it must start with "vim9script"
as the first line.
For a legacy script, you can source any range of lines.

- Yegappan


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/9974/c1274370718@github.com>

Maxim Kim

unread,
Oct 11, 2022, 6:03:13 AM10/11/22
to vim/vim, vim-dev ML, Comment

To source a Vim9 script from a buffer, it must start with "vim9script" as the first line. For a legacy script, you can source any range of lines.

I have this operator to run both vimscript and vim9script ranges:

# source vimscript (operator)
def SourceVim(...args: list<any>): any
    if len(args) == 0
        &opfunc = matchstr(expand('<stack>'), '[^. ]*\ze[')
        return 'g@'
    endif
    if getline(1) =~ '^vim9script$'
        vim9cmd :'[,']source
    else
        :'[,']source
    endif
    return ''
enddef
nnoremap <silent> <expr> <space>v SourceVim()
xnoremap <silent> <expr> <space>v SourceVim()
nnoremap <silent> <expr> <space>vv SourceVim() .. '_'


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/9974/c1274444856@github.com>

Reply all
Reply to author
Forward
0 new messages