• R/O
  • SSH

vim: 仓库概述

Mirror of the Vim source from https://github.com/vim/vim


Recent Commits RSS

Rev. 时间 作者 Message
3d41e3760130 2024-04-18 05:45:04 Christian Brabandt tip Added tag v9.1.0346 for changeset 76e3b9d514a85ed21339482...
76e3b9d514a8 2024-04-18 05:45:03 Christian Brabandt v9.1.0346 patch 9.1.0346: Patch v9.1.0338 fixed sourcing a script w...
7129cffaa3bc 2024-04-18 05:45:03 Christian Brabandt Added tag v9.1.0345 for changeset 3c99483807e9c636e1c54f2...
3c99483807e9 2024-04-18 05:45:02 Christian Brabandt v9.1.0345 patch 9.1.0345: Problem: gvimrc not sourced from XDG_CONF...
5497fab886b6 2024-04-18 05:15:03 Christian Brabandt Added tag v9.1.0344 for changeset 74f789d0adb17186c2455da...
74f789d0adb1 2024-04-18 05:15:02 Christian Brabandt v9.1.0344 patch 9.1.0344: Cursor wrong after using setcellwidth() i...
f4bcdc78c589 2024-04-18 04:45:08 Christian Brabandt Added tag v9.1.0343 for changeset 52db4364a5cd948b849e924...
52db4364a5cd 2024-04-18 04:45:06 Christian Brabandt v9.1.0343 patch 9.1.0343: 'showcmd' wrong for partial mapping with ...
bc40de80d8ec 2024-04-17 06:15:04 Christian Brabandt Added tag v9.1.0342 for changeset 4ab37aa5cbcfb906e682147...
4ab37aa5cbcf 2024-04-17 06:15:03 Christian Brabandt v9.1.0342 patch 9.1.0342: tests: test_taglist fails when 'helplang'...

Recently edited Tags

名称 Rev. 时间 作者
v7.0001 3fc0f57ecb91 2004-06-14 05:20:40 vimboss
v7.0002 4102fb4ea781 2004-06-20 21:51:53 vimboss
v7.0007 631143ac4a01 2004-07-10 18:47:34 vimboss
v7.0008 3ba373b54370 2004-07-13 00:53:54 vimboss
v7.0009 9be87deaeb52 2004-07-17 05:18:37 vimboss
v7.0010 293621502c4d 2004-07-19 06:34:53 vimboss
v7.0011 a81bc802c17c 2004-07-20 05:55:54 vimboss
v7.0012 4ac1dce8dd5e 2004-07-26 21:53:41 vimboss
v7.0015 3f44e9abe4ec 2004-09-07 02:44:46 vimboss
v7.0016 8ff7fd162d3c 2004-09-14 05:26:32 vimboss

Branches

名称 Rev. 时间 作者 Message
default 3d41e3760130 2024-04-18 05:45:04 Christian Brabandt Added tag v9.1.0346 for cha...
vim 930405b6b960 2015-08-19 04:52:11 Bram Moolenaar Close invalid branch 'vim'
vim72 cb0592df6c86 2015-08-19 04:52:12 Bram Moolenaar Close unused branch 'vim72'
vim73 c26ccfcf5989 2015-08-19 04:52:12 Bram Moolenaar Close old branch 'vim73'

README_VIM9.md

Vim Logo

What is Vim9?

This is a new syntax for Vim script that was introduced with Vim 9.0. It intends making Vim script faster and better.

Why Vim9?

1. FASTER VIM SCRIPT

The third item on the poll results of 2018, after popup windows and text properties, both of which have been implemented, is faster Vim script. So how do we do that?

I have been throwing some ideas around, and soon came to the conclusion that the current way functions are called and executed, with dictionaries for the arguments and local variables, is never going to be very fast. We're lucky if we can make it twice as fast. The overhead of a function call and executing every line is just too high.

So what then? We can only make something fast by having a new way of defining a function, with similar but different properties of the old way: * Arguments are only available by name, not through the a: dictionary or the a:000 list. * Local variables are not available in an l: dictionary. * A few more things that slow us down, such as exception handling details.

I Implemented a "proof of concept" and measured the time to run a simple for loop with an addition (Justin used this example in his presentation, full code is below):

  let sum = 0
  for i in range(1, 2999999)
    let sum += i
  endfor
how time in sec
Vim old 5.018541
Python 0.369598
Lua 0.078817
LuaJit 0.004245
Vim new 0.073595

That looks very promising! It's just one example, but it shows how much we can gain, and also that Vim script can be faster than builtin interfaces.

LuaJit is much faster at Lua-only instructions. In practice the script would not do something useless counting, but change the text. For example, reindent all the lines:

  let totallen = 0
  for i in range(1, 100000)
    call setline(i, '    ' .. getline(i))
    let totallen += len(getline(i))
  endfor
how time in sec
Vim old 0.578598
Python 0.152040
Lua 0.164917
LuaJit 0.128400
Vim new 0.079692

[These times were measured on a different system by Dominique Pelle]

The differences are smaller, but Vim 9 script is clearly the fastest. Using LuaJIT is only a little bit faster than plain Lua here, clearly the call back to the Vim code is costly.

How does Vim9 script work? The function is first compiled into a sequence of instructions. Each instruction has one or two parameters and a stack is used to store intermediate results. Local variables are also on the stack, space is reserved during compilation. This is a fairly normal way of compilation into an intermediate format, specialized for Vim, e.g. each stack item is a typeval_T. And one of the instructions is "execute Ex command", for commands that are not compiled.

2. DEPRIORITIZE INTERFACES

Attempts have been made to implement functionality with built-in script languages such as Python, Perl, Lua, Tcl and Ruby. This never gained much foothold, for various reasons.

Instead of using script language support in Vim: * Encourage implementing external tools in any language and communicate with them. The job and channel support already makes this possible. Really any language can be used, also Java and Go, which are not available built-in. * No priority for the built-in language interfaces. They will have to be kept for backwards compatibility, but many users won't need a Vim build with these interfaces. * Improve the Vim script language, it is used to communicate with the external tool and implements the Vim side of the interface. Also, it can be used when an external tool is undesired.

Altogether this creates a clear situation: Vim with the +eval feature will be sufficient for most plugins, while some plugins require installing a tool that can be written in any language. No confusion about having Vim but the plugin not working because some specific language is missing. This is a good long term goal.

Rationale: Why is it better to run a tool separately from Vim than using a built-in interface and interpreter? Take for example something that is written in Python: * The built-in interface uses the embedded python interpreter. This is less well maintained than the python command. Building Vim with it requires installing developer packages. If loaded dynamically there can be a version mismatch. * When running the tool externally the standard python command can be used, which is quite often available by default or can be easily installed. * The built-in interface has an API that is unique for Vim with Python. This is an extra API to learn. * A .py file can be compiled into a .pyc file and execute much faster. * Inside Vim multi-threading can cause problems, since the Vim core is single threaded. In an external tool there are no such problems. * The Vim part is written in .vim files, the Python part is in .py files, this is nicely separated. * Disadvantage: An interface needs to be made between Vim and Python. JSON is available for this, and it's fairly easy to use. But it still requires implementing asynchronous communication.

3. BETTER VIM SCRIPT

To make Vim faster a new way of defining a function needs to be added. While we are doing that, since the lines in this function won't be fully backwards compatible anyway, we can also make Vim script easier to use. In other words: "less weird". Making it work more like modern programming languages will help. No surprises.

A good example is how in a function the arguments are prefixed with "a:". No other language I know does that, so let's drop it.

Taking this one step further is also dropping "s:" for script-local variables; everything at the script level is script-local by default. Since this is not backwards compatible it requires a new script style: Vim9 script!

To avoid having more variations, the syntax inside a compiled function is the same as in Vim9 script. Thus you have legacy syntax and Vim9 syntax.

It should be possible to convert code from other languages to Vim script. We can add functionality to make this easier. This still needs to be discussed, but we can consider adding type checking and a simple form of classes. If you look at JavaScript for example, it has gone through these stages over time, adding real class support and now TypeScript adds type checking. But we'll have to see how much of that we actually want to include in Vim script. Ideally a conversion tool can take Python, JavaScript or TypeScript code and convert it to Vim script, with only some things that cannot be converted.

Vim script won't work the same as any specific language, but we can use mechanisms that are commonly known, ideally with the same syntax. One thing I have been thinking of is assignments without ":let". I often make that mistake (after writing JavaScript especially). I think it is possible, if we make local variables shadow commands. That should be OK, if you shadow a command you want to use, just rename the variable. Using "var" and "const" to declare a variable, like in JavaScript and TypeScript, can work:

def MyFunction(arg: number): number
   var local = 1
   var todo = arg
   const ADD = 88
   while todo > 0
      local += ADD
      todo -= 1
   endwhile
   return local
enddef

The similarity with JavaScript/TypeScript can also be used for dependencies between files. Vim currently uses the :source command, which has several disadvantages: * In the sourced script, is not clear what it provides. By default all functions are global and can be used elsewhere. * In a script that sources other scripts, it is not clear what function comes from what sourced script. Finding the implementation is a hassle. * Prevention of loading the whole script twice must be manually implemented.

We can use the :import and :export commands from the JavaScript standard to make this much better. For example, in script "myfunction.vim" define a function and export it:

vim9script  " Vim9 script syntax used here

var local = 'local variable is not exported, script-local'

export def MyFunction()  " exported function
...

def LocalFunction() " not exported, script-local
...

And in another script import the function:

vim9script  " Vim9 script syntax used here

import MyFunction from 'myfunction.vim'

This looks like JavaScript/TypeScript, thus many users will understand the syntax.

These are ideas, this will take time to design, discuss and implement. Eventually this will lead to Vim 9!

Code for sum time measurements

Vim was build with -O2.

func VimOld()
  let sum = 0
  for i in range(1, 2999999)
    let sum += i
  endfor
  return sum
endfunc

func Python()
  py3 << END
sum = 0
for i in range(1, 3000000):
  sum += i
END
  return py3eval('sum')
endfunc

func Lua()
  lua << END
    sum = 0
    for i = 1, 2999999 do
      sum = sum + i
    end
END
  return luaeval('sum')
endfunc

def VimNew(): number
  var sum = 0
  for i in range(1, 2999999)
    sum += i
  endfor
  return sum
enddef

let start = reltime()
echo VimOld()
echo 'Vim old: ' .. reltimestr(reltime(start))

let start = reltime()
echo Python()
echo 'Python: ' .. reltimestr(reltime(start))

let start = reltime()
echo Lua()
echo 'Lua: ' .. reltimestr(reltime(start))

let start = reltime()
echo VimNew()
echo 'Vim new: ' .. reltimestr(reltime(start))

Code for indent time measurements

def VimNew(): number
  var totallen = 0
  for i in range(1, 100000)
    setline(i, '    ' .. getline(i))
    totallen += len(getline(i))
  endfor
  return totallen
enddef

func VimOld()
  let totallen = 0
  for i in range(1, 100000)
    call setline(i, '    ' .. getline(i))
    let totallen += len(getline(i))
  endfor
  return totallen
endfunc

func Lua()
  lua << END
    b = vim.buffer()
    totallen = 0
    for i = 1, 100000 do
      b[i] = "    " .. b[i]
      totallen = totallen + string.len(b[i])
    end
END
  return luaeval('totallen')
endfunc

func Python()
  py3 << END
cb = vim.current.buffer
totallen = 0
for i in range(0, 100000):
  cb[i] = '    ' + cb[i]
  totallen += len(cb[i])
END
  return py3eval('totallen')
endfunc

new
call setline(1, range(100000))
let start = reltime()
echo VimOld()
echo 'Vim old: ' .. reltimestr(reltime(start))
bwipe!

new
call setline(1, range(100000))
let start = reltime()
echo Python()
echo 'Python: ' .. reltimestr(reltime(start))
bwipe!

new
call setline(1, range(100000))
let start = reltime()
echo Lua()
echo 'Lua: ' .. reltimestr(reltime(start))
bwipe!

new
call setline(1, range(100000))
let start = reltime()
echo VimNew()
echo 'Vim new: ' .. reltimestr(reltime(start))
bwipe!
Show on old repository browser