feat(lua)!: execute Lua with "nvim -l"

Problem:
Nvim has Lua but the "nvim" CLI can't easily be used to execute Lua
scripts, especially scripts that take arguments or produce output.

Solution:
- support "nvim -l [args...]" for running scripts. closes #15749
- exit without +q
- remove lua2dox_filter
- remove Doxyfile. This wasn't used anyway, because the doxygen config
  is inlined in gen_vimdoc.py (`Doxyfile` variable).
- use "nvim -l" in docs-gen CI job

Examples:

    $ nvim -l scripts/lua2dox.lua --help
    Lua2DoX (0.2 20130128)
    ...

    $ echo "print(vim.inspect(_G.arg))" | nvim -l - --arg1 --arg2
    $ echo 'print(vim.inspect(vim.api.nvim_buf_get_text(1,0,0,-1,-1,{})))' | nvim +"put ='text'" -l -

TODO?
  -e executes Lua code
  -l loads a module
  -i enters REPL _after running the other arguments_.
This commit is contained in:
Justin M. Keyes
2021-09-20 19:00:50 -07:00
parent 39d70fcafd
commit 7c94bcd2d7
16 changed files with 311 additions and 2784 deletions

View File

@@ -21,15 +21,19 @@ Nvim includes a "standard library" |lua-stdlib| for Lua. It complements the
which can be used from Lua code (|lua-vimscript| |vim.api|). Together these
"namespaces" form the Nvim programming interface.
See the |lua-guide| for an introduction to using Lua in Neovim.
Lua plugins and user config are automatically discovered and loaded, just like
Vimscript. See |lua-guide| for practical guidance.
You can also run Lua scripts from your shell using the |-l| argument: >
nvim -l foo.lua [args...]
<
*lua-compat*
Lua 5.1 is the permanent interface for Nvim Lua. Plugins need only consider
Lua 5.1, not worry about forward-compatibility with future Lua versions. If
Nvim ever ships with Lua 5.4+, a Lua 5.1 compatibility shim will be provided
so that old plugins continue to work transparently.
------------------------------------------------------------------------------
==============================================================================
LUA CONCEPTS AND IDIOMS *lua-concepts*
Lua is very simple: this means that, while there are some quirks, once you
@@ -73,25 +77,24 @@ In Lua, any missing arguments are passed as `nil`. Example: >lua
Furthermore it is not an error if extra parameters are passed, they are just
discarded.
It is also allowed to omit the parentheses (only) if the function takes
exactly one string (`"foo"`) or table literal (`{1,2,3}`). The latter is often
used to approximate the "named parameters" feature of languages like Python
("kwargs" or "keyword args"). Example: >lua
*kwargs*
When calling a function, you can omit the parentheses if the function takes
exactly one string literal (`"foo"`) or table literal (`{1,2,3}`). The latter
is often used to approximate "named parameters" ("kwargs" or "keyword args")
as in languages like Python and C#. Example: >lua
local func_with_opts = function(opts)
local will_do_foo = opts.foo
local filename = opts.filename
...
end
func_with_opts { foo = true, filename = "hello.world" }
<
There is nothing special going on here except that parentheses are treated as
There's nothing special going on here except that parentheses are treated as
whitespace. But visually, this small bit of sugar gets reasonably close to
a "keyword args" interface.
It is of course also valid to call the function with parentheses: >lua
func_with_opts({ foo = true, filename = "hello.world" })
<
Nvim tends to prefer the keyword args style.
@@ -100,25 +103,20 @@ Nvim tends to prefer the keyword args style.
LUA PATTERNS *lua-patterns*
Lua intentionally does not support regular expressions, instead it has limited
"patterns" which avoid the performance pitfalls of extended regex.
|luaref-patterns|
"patterns" |luaref-patterns| which avoid the performance pitfalls of extended
regex. Lua scripts can also use Vim regex via |vim.regex()|.
Examples using |string.match()|: >lua
These examples use |string.match()| to demonstrate Lua patterns: >lua
print(string.match("foo123bar123", "%d+"))
-- 123
print(string.match("foo123bar123", "[^%d]+"))
-- foo
print(string.match("foo123bar123", "[abc]+"))
-- ba
print(string.match("foo.bar", "%.bar"))
-- .bar
For more complex matching you can use Vim regex from Lua via |vim.regex()|.
==============================================================================
IMPORTING LUA MODULES *require()* *lua-require*
@@ -389,7 +387,7 @@ For example consider the following Lua omnifunc handler: >lua
vim.api.nvim_buf_set_option(0, 'omnifunc', 'v:lua.mymod.omnifunc')
Note: The module ("mymod" in the above example) must either be a Lua global,
or use the require syntax as specified above to access it from a package.
or use require() as shown above to access it from a package.
Note: `v:lua` without a call is not allowed in a Vimscript expression:
|Funcref|s cannot represent Lua functions. The following are errors: >vim

View File

@@ -57,6 +57,11 @@ The following new APIs or features were added.
<
(or the Vimscript equivalent) to their |config| file.
• Run Lua scripts from your shell using |-l|. >
nvim -l foo.lua --arg1 --arg2
< Also works with stdin: >
echo "print(42)" | nvim -l -
• Added a |vim.lsp.codelens.clear()| function to clear codelenses.
• |vim.inspect_pos()|, |vim.show_pos()| and |:Inspect| allow a user to get or show items

View File

@@ -9,7 +9,7 @@ Starting Vim *starting*
Type |gO| to see the table of contents.
==============================================================================
Nvim arguments *vim-arguments*
Nvim arguments *cli-arguments*
Most often, Nvim is started to edit a single file with the command: >
@@ -31,8 +31,8 @@ filename One or more file names. The first one will be the current
To avoid a file name starting with a '-' being interpreted as
an option, precede the arglist with "--", e.g.: >
nvim -- -filename
< All arguments after the "--" will be interpreted as file names,
no other options or "+command" argument can follow.
< All arguments after "--" are interpreted as file names, no
other options or "+command" argument can follow.
*--*
`-` Alias for stdin (standard input).
@@ -143,15 +143,13 @@ argument.
these commands, independently from "-c" commands.
*-S*
-S {file} Vimscript or Lua (".lua") {file} will be |:source|d after the
first file has been read. Equivalent to: >
-S [file] Vimscript or Lua (".lua") [file] will be |:source|d after the
first file has been read or "Session.vim" if [file] is not
given. Equivalent to: >
-c "source {file}"
< Can be repeated like "-c", subject to the same limit of 10
"-c" arguments. {file} cannot start with a "-".
-S Works like "-S Session.vim". Only when used as the last
argument or when another "-" option follows.
-L *-L* *-r*
-r Recovery mode. Without a file name argument, a list of
existing swap files is given. With a file name, a swap file
@@ -211,10 +209,30 @@ argument.
nvim -es +":verbose echo 'foo'"
nvim -V1 -es +foo
< User |config| is skipped (unless given with |-u|).
< User |config| is skipped unless |-u| was given.
Swap file is skipped (like |-n|).
User |shada| is loaded (unless "-i NONE" is given).
*-l*
-l {script} [args]
Executes Lua {script} file and exits. All [args] (up to "--"
|---|) are treated as {script} args, not Nvim args: by Lua
convention they are set in the `_G.arg` global table. *lua-args*
On {script} error, Nvim exits with code 1.
Sets 'verbose' to 1 (like "-V1"), so Lua `print()` writes to
output.
Any |cli-arguments| before "-l" are processed before executing
{script}. For example this quits before executing "foo.lua": >
nvim +q -l foo.lua
< This loads Lua module "bar" before executing "foo.lua": >
nvim +"lua require('bar')" -l foo.lua
<
User |config| is skipped unless |-u| was given.
User |shada| is skipped unless |-i| was given.
Swap file is skipped (like |-n|).
*-b*
-b Binary mode. File I/O will only recognize <NL> to separate
lines. The 'expandtab' option will be reset. The 'textwidth'
@@ -222,9 +240,6 @@ argument.
is set. This is done after reading the |vimrc| but before
reading any file in the arglist. See also |edit-binary|.
*-l*
-l Lisp mode. Sets the 'lisp' and 'showmatch' options on.
*-A*
-A Arabic mode. Sets the 'arabic' option on.
@@ -239,10 +254,10 @@ argument.
Example: >
nvim -V8
-V[N]{filename}
Like -V and set 'verbosefile' to {filename}. Messages are not
displayed; instead they are written to the file {filename}.
{filename} must not start with a digit.
-V[N]{file}
Like -V and sets 'verbosefile' to {file} (must not start with
a digit). Messages are not displayed; instead they are
written to {file}.
Example: >
nvim -V20vimlog
<