You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nvim-libmodal/doc/libmodal-lua.txt

1516 lines
42 KiB
Plaintext

4 years ago
*libmodal-lua.txt* Create modes for Neovim - Lua Reference
*libmodal-lua*
*libmodal-dev*
This is a document specifying the lower-level implementation of |libmodal|. If
one wishes to make use of it to more finely control their |libmodal-mode| or
|libmodal-prompt|, this document should be the primary reference.
Any material not covered here is covered in |libmodal-usage|.
See: |libmodal-usage|, |lua|, |lua-require-example|.
4 years ago
================================================================================
0. Table of Contents *libmodal-lua-toc*
1. `libmodal` ............................. |libmodal-lua-libmodal|
2. `libmodal.classes` ..................... |libmodal-lua-classes|
3. `libmodal.collections` ................. |libmodal-lua-collections|
3.1. `libmodal.collections.ParseTable` ...... |libmodal-lua-ParseTable|
3.2. `libmodal.collections.Popup` ........... |libmodal-lua-Popup|
3.3. `libmodal.collections.Stack` ........... |libmodal-lua-Stack|
4. `libmodal.globals` ..................... |libmodal-lua-globals|
5. `libmodal.Indicator` ................... |libmodal-lua-Indicator|
5.1. `libmodal.Indicator.HighlightSegment` .. |libmodal-lua-HighlightSegment|
6. `libmodal.Layer` ....................... |libmodal-lua-Layer|
7. `libmodal.Mode` ........................ |libmodal-lua-Mode|
8. `libmodal.Prompt` ...................... |libmodal-lua-Prompt|
9. `libmodal.utils` ....................... |libmodal-lua-utils|
9.1. `libmodal.utils.api` ................... |libmodal-lua-api|
9.2. `libmodal.utils.Help` .................. |libmodal-lua-Help|
9.3. `libmodal.utils.WindowState` ........... |libmodal-lua-Windowstate|
10. `libmodal.Vars` ........................ |libmodal-lua-Vars|
4 years ago
================================================================================
1. `libmodal` *libmodal-lua-libmodal*
This is the base of |libmodal|. It can be imported using: >
local libmodal = require 'libmodal'
<if |libmodal| is in your 'runtimepath'.
4 years ago
--------------------------------------------------------------------------------
MODULES *libmodal-lua-modules*
 `libmodal`
├──  `.collections`
│ ├──  `.ParseTable`
│ └──  `.Stack`
├──  `.globals`
├──  init.lua
│ ├── `.mode.enter()`
│ └── `.prompt.enter()`
├──  `.Indicator`
│ └──  `.HighlightSegment`
├──  `.Mode`
│ └──  `.Popup`
├──  `.Prompt`
├──  `.utils`
│ ├──  `.api`
│ ├──  `.Help`
│ └──  `.WindowState`
└──  `.Vars`
4 years ago
================================================================================
2. `libmodal.classes` *libmodal-lua-classes*
`libmodal.classes` is a small library to help shorten the amount of
boilerplate code that it takes to set up a `metatable` and subsequent
subtables.
There aren't an astounding amount of functions: the intent is not to implement
object orientation into Lua, rather it is to help facilitate the emulation of
object orientation through `metatable`s.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-classes-functions*
4 years ago
`classes`.new({name}, {base}) *libmodal-lua-classes.new()*
Define a class-metatable.
Parameters: ~
{name} The name of the class.
Note: this value is used for `classes.type()`.
{base} Optional. The base class to use.
Return: ~
A table set up to for use as a `metatable`.
4 years ago
`classes`.type({value}) *libmodal-lua-classes.type()*
Get the type of some {value}.
Parameters: ~
{value} The value to get the type of.
Return: ~
* If {value} is a `table` and it has a `__type` attribute, return
`__type`.
* Else, return `type(`{value}`)`
4 years ago
================================================================================
3. `libmodal.collections` *libmodal-lua-collections*
4 years ago
The `libmodal.collections` module consists of a few data structures that
support the rest of `libmodal`.
4 years ago
================================================================================
3.1. `libmodal.collections.ParseTable` *libmodal-lua-ParseTable*
4 years ago
A `ParseTable` is a pseudo-parse tree of a given user collection of
keybinding:expression pairs.
See: |libmodal-mode| for more information.
4 years ago
--------------------------------------------------------------------------------
VARIABLES *libmodal-lua-ParseTable-variables*
4 years ago
4 years ago
`ParseTable`.CR *libmodal-lua-ParseTable.CR*
4 years ago
The character number for <CR>.
Type: ~
`number`
Value: ~
13
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-ParseTable-functions*
4 years ago
4 years ago
`ParseTable`.stringSplit({str}, {regex}) *libmodal-lua-ParseTable.stringSplit()*
Split some {str} into a `table` with a {regex} expression.
Parameters: ~
{str} The `string` to split.
{regex} The regex expression to split {str} with.
Return: ~
* The split {str} as a `table`.
Example: ~
>
local ParseTable = require('libmodal').collections.ParseTable
print(vim.inspect(ParseTable.stringSplit('testing split', ' ')))
<
4 years ago
`ParseTable`.parse({key}) *libmodal-lua-ParseTable.parse()*
Parse some {key} so that it can be stored by a `ParseTable` instance.
Note: this method can be locally overridden in order to change the
`ParseTable`'s behavior. Example:
>
local ParseTable = require('libmodal').collections.ParseTable
ParseTable.parse = function(key)
return ParseTable.splitString(key, ' ')
end
<
Parameters: ~
{key} The key of the `table` to parse.
Return: ~
* The parsed {key}. Should be a `table`.
Example: ~
>
local ParseTable = require('libmodal').collections.ParseTable
print(vim.inspect(ParseTable.parse('testkey')))
<
4 years ago
`ParseTable`.new({userTable}) *libmodal-lua-ParseTable.new()*
4 years ago
Create a new `ParseTable` from a user-defined `table` of combos.
All keys of a `ParseTable` are numbers of characters.
Parameters: ~
{userTable} the table of combos defined by the user (see
`libmodal.mode.enter()`)
Return: ~
A new `ParseTable`.
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
-- Create a mock set of user mappings.
local userTable = {
zf = "echo 'Hello!'",
zfo = "tabnew"
4 years ago
}
-- Create a new `ParseTable`
local parseTable = libmodal.mode.ParseTable.new(userTable)
-- Print the `parseTable`.
print(vim.inspect(parseTable))
<
See Also: ~
|char2nr|, |nr2char| For character to number conversion and vice
versa.
|libmodal-mode| For information about {userTable}.
4 years ago
`self`:get({keyDict}) *libmodal-lua-ParseTable.get()*
4 years ago
Get a value from an instance of `ParseTable`.
Parameters: ~
{keyDict} a string of key characters as bytes.
Return: ~
A `function` {keyDict} is a full match.
A `table` the {keyDict} partially matches.
* `nil` {keyDict} is not ANYWHERE.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
-- Simulate user input.
local userInput = {122, 102} -- {'z', 'f'}
-- Create a dummy `ParseTable`.
local parseTable = libmodal.mode.ParseTable.new({
zfo = 'echo "Hello!"'
4 years ago
})
-- Inspect it.
print(vim.inspect(parseTable))
-- this will return a `table`
local tbl = parseTable:get(userInput)
4 years ago
-- Inspect it to show the difference.
print(vim.inspect(tbl))
<
4 years ago
`self`:parsePut({key}, {value}) *libmodal-lua-ParseTable.parsePut()*
4 years ago
Put `value` into the parse tree as `key`.
Parameters: ~
{key} the key that {value} is reffered to by. A `char`, not a
`byte`.
{value} the value to store as {key}. A `string` to |execute|.
Example: ~
>
-- Create a dummy `ParseTable`.
local parseTable = libmodal.mode.ParseTable.new({
zfo = 'echo "Hello!"'
4 years ago
})
-- Inspect it.
print(vim.inspect(parseTable))
-- this will return a `table`
parseTable:parsePut('zfc', 'split')
-- Inspect it to show the difference.
print(vim.inspect(parseTable))
<
See also: ~
|libmodal-lua-parsetable-parseputall| for how to put multiple {key}s
and {value}s at a time.
4 years ago
`self`:parsePutAll({tableToUnite}) *libmodal-lua-ParseTable.parsePutAll()*
4 years ago
Create the union of `self` and `tableToUnite`
Interally calls |libmodal-lua-parsetable-parseput| on every key:value pair
in {tableToUnite}.
Parameters: ~
{tableToUnite} the table to unite with `self.`
Example: ~
>
-- Create an empty `ParseTable`.
local parseTable = libmodal.mode.ParseTable.new({})
-- Create some dummy keybindings.
local unparsedUserKeybinds = {
zfo = 'echo "Hello!"',
zfc = 'split'
4 years ago
}
-- Add the dummy keybindings.
parseTable:parsePutAll(unparsedUserKeybinds)
-- Inspect it to show the difference.
print(vim.inspect(parseTable))
<
See also: ~
|libmodal-lua-parsetable-parseput| For more information on
{tableToUnite}.
4 years ago
================================================================================
3.2. `libmodal.collections.Popup` *libmodal-lua-Popup*
When `:enter()`ing a `Mode`, an |api-floatwin| is displayed at the bottom
right-hand corner of the screen. `libmodal.Mode.Popup` is responsible for
opening it and keeping updated.
Whenever a `Popup` is created, it is immediately opened. Additionally, it is
opened with the following options: >
local _winOpenOpts = {
anchor = 'SW',
col = vim.go.columns - 1,
focusable = false,
height = 1,
relative = 'editor',
row = vim.go.lines - vim.go.cmdheight - 1,
style = 'minimal',
width = 25,
}
<
4 years ago
--------------------------------------------------------------------------------
VARIABLES *libmodal-lua-Popup-variables*
`Popup`.config *libmodal-lua-Popup.config*
The options used when opening a `Popup`.
Note: this can be overwritten to change the default behavior of `Popup`.
Type: ~
|nvim_open_win| {config} `table`.
Value: ~
>
{
anchor = 'SW',
col = vim.go.columns - 1,
focusable = false,
height = 1,
relative = 'editor',
row = vim.go.lines - vim.go.cmdheight - 1,
style = 'minimal',
width = 1
}
<
4 years ago
`self`.buffer *libmodal-lua-Popup.buffer*
The scratch buffer used by `Popup` to display text.
Type: ~
|nvim_create_buf| handle.
Value: ~
`vim.api.nvim_create_buf(false, true)`
4 years ago
`self`.window *libmodal-lua-Popup.window*
The window used to display the contents of `Popup.buffer`.
Type: ~
|nvim_open_win| handle.
Value: ~
`vim.api.nvim_open_win(self.buffer, false, Popup.config)`
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Popup-functions*
4 years ago
`self`:close({keepBuffer}) *libmodal-lua-Popup.close()*
4 years ago
Close the |Popup.window|.
4 years ago
Note: Variables of |Popup| are de-initialized after this method.
Parameters: ~
{keepBuffer} A `boolean` that (when `true`) indicates the underlying
              |scratch-buffer| should be kept
`self`:open({config}) *libmodal-lua-Popup.open()*
4 years ago
Open the |Popup.window|.
Note: if the |Popup.window| is already open, then it will be reopened in
      case the |current_tabpage| has changed.
      In this case, it will also override the default options with the
      options in the window so there is no need to pass in {config} a
      second time.
Parameters: ~
{config} Same as |nvim_open_win| {config}.
4 years ago
`self`:refresh({inputBytes}) *libmodal-lua-Popup.refresh()*
Update the content of the `Popup` using an array-like `table` of
|char2nr|s.
Note: This is normally used to add what the user is typing. If you have a
`function` `Mode` {instruction}, then you can use `Popup` and this
function to manually show user input as it is typed.
Parameters: ~
{inputBytes} An array-like `table` of |char2nr|s to write to the
`Popup`.
`Popup`.new({config}) *libmodal-lua-Popup.new()*
Create a new `Popup` and immediately open it.
Parameters: ~
{config} Same as |nvim_open_win| {config}.
          Note: defaults to |Popup.config|.
Return: ~
4 years ago
* A new `Popup`.
See also: ~
|libmodal-lua-Popup| For the options used to spawn the window.
4 years ago
================================================================================
4.2. `libmodal.collections.Stack` *libmodal-lua-Stack*
4 years ago
The `libmodal.collections.Stack` is a simple implementation of a Stack data
structure. It is designed with reuse of resources in mind, as
commonly-accessed pieces of data are locally stored for faster reference
times.
The `#` operator in |Lua| will work on this structure.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Stack-functions*
4 years ago
4 years ago
`self`:peek() *libmodal-lua-Stack.peek()*
4 years ago
Access the value at the top of the stack without removing it.
Return: ~
4 years ago
* The value at the top of the stack.
4 years ago
Example: ~
>
local Stack = require('libmodal').collections.Stack
local stk = Stack.new()
stk:push('foo')
stk:push('bar')
print(stk:peek())
<
4 years ago
`self`:pop() *libmodal-lua-Stack.pop()*
4 years ago
Access the value at the top of the stack by removing it.
Return: ~
4 years ago
* The value at the top of the stack.
4 years ago
Example: ~
>
local Stack = require('libmodal').collections.Stack
local stk = Stack.new()
stk:push('foo')
stk:push('bar')
print(stk:peek())
local _ = stk:pop()
print(stk:peek())
<
4 years ago
`self`:push({value}) *libmodal-lua-Stack.push()*
4 years ago
Push some {value} onto the top of the stack.
Parameters: ~
{value} The value to put on top of the stack.
Example: ~
>
local Stack = require('libmodal').collections.Stack
local stk = Stack.new()
stk:push('foo')
stk:push('bar')
print(vim.inspect(stk))
<
4 years ago
`Stack`.new() *libmodal-lua-Stack.new()*
4 years ago
Create a new `libmodal.collections.Stack` and return it.
Return: ~
4 years ago
* A new `libmodal.collections.Stack`.
4 years ago
Example: ~
>
local Stack = require('libmodal').collections.Stack
local stk = Stack.new()
print(vim.inspect(stk))
<
4 years ago
================================================================================
4. `libmodal.globals` *libmodal-lua-globals*
4 years ago
These are global functions used throughout the project. They are never
modified and never meant TO be modified.
4 years ago
--------------------------------------------------------------------------------
VARIABLES *libmodal-lua-globals-variables*
4 years ago
`globals`.DEFAULT_ERROR_TITLE *libmodal-lua-globals.DEFAULT_ERROR_TITLE*
The default error message header for |libmodal| errors.
Type: ~
`string`
Value: ~
"vim-libmodal error"
4 years ago
`globals`.ESC_NR *libmodal-lua-globals.ESC_NR*
The byte of the <Esc> character.
Type: ~
`number`
Value: ~
27
4 years ago
`globals`.TYPE_FUNC *libmodal-lua-globals.TYPE_FUNC*
The `string` yielded by `lua type(x)` when `x` is a `function`.
Type: ~
`string`
Value: ~
"function"
4 years ago
`globals`.TYPE_NUM *libmodal-lua-globals.TYPE_NUM*
The `string` yielded by `lua type(x)` when `x` is a `number`.
Type: ~
`string`
Value: ~
"number"
4 years ago
`globals`.TYPE_STR *libmodal-lua-globals.TYPE_STR*
The `string` yielded by `lua type(x)` when `x` is a `string`.
Type: ~
`string`
Value: ~
"string"
4 years ago
`globals`.TYPE_TBL *libmodal-lua-globals.TYPE_TBL*
The `string` yielded by `lua type(x)` when `x` is a `table`.
Type: ~
`string`
Value: ~
"table"
4 years ago
`globals`.VIM_FALSE *libmodal-lua-globals.VIM_FALSE*
The value Vimscript uses to return `false` (besides |v:false|).
Type: ~
`number`
Value: ~
0
4 years ago
`globals`.VIM_TRUE *libmodal-lua-globals.VIM_TRUE*
Type: ~
`number`
Value: ~
1
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-globals-functions*
4 years ago
`globals`.is_false({val}) *libmodal-lua-globals.is_false()*
Determine whether or not some {val} is equal to `globals.VIM_FALSE`,
|v:false|, or `false`.
Parameters: ~
{val} The value to check. Should be a `boolean` or a `boolean`
expression.
Return: ~
4 years ago
- `true` if {val} is equal to `globals.VIM_FALSE`, |v:false|, or
`false`.
4 years ago
- `false` otherwise.
Example: ~
>
local libmodal = require 'libmodal'
-- Get Lua's truth values.
local falseValue = false
local trueValue = true
-- Get Vimscript's truth values.
local vim_falseValue = libmodal.globals.VIM_FALSE
local vim_trueValue = libmodal.globals.VIM_TRUE
--[[ Test the function. ]]
print(libmodal.globals.is_false(falseValue))
print(libmodal.globals.is_false(v_falseValue))
print(libmodal.globals.is_false(vim_falseValue))
print(libmodal.globals.is_false(trueValue))
print(libmodal.globals.is_false(v_trueValue))
print(libmodal.globals.is_false(vim_trueValue))
<
4 years ago
`globals`.is_true({val}) *libmodal-lua-globals.is_true()*
Determine whether or not some {val} is equal to `globals.VIM_TRUE`,
|v:true|, or `true`.
Parameters: ~
{val} The value to check. Should be a `boolean` or a `boolean`
expression.
Return: ~
4 years ago
- `true` if {val} is equal to `globals.VIM_TRUE`, |v:true|, or `true`.
- `false` otherwise.
Example: ~
>
local libmodal = require 'libmodal'
-- Get Lua's truth values.
local falseValue = false
local trueValue = true
-- Get Vimscript's truth values.
local vim_falseValue = libmodal.globals.VIM_FALSE
local vim_trueValue = libmodal.globals.VIM_TRUE
4 years ago
--[[ Test the function. ]]
print(libmodal.globals.is_true(falseValue))
print(libmodal.globals.is_true(v_falseValue))
print(libmodal.globals.is_true(vim_falseValue))
print(libmodal.globals.is_true(trueValue))
print(libmodal.globals.is_true(v_trueValue))
print(libmodal.globals.is_true(vim_trueValue))
<
4 years ago
================================================================================
5. `libmodal.utils.Indicator` *libmodal-lua-Indicator*
4 years ago
Provides creation sources for mode and prompt |echo| / |echohl| `string`s.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-indicator-functions*
4 years ago
4 years ago
`Indicator`.mode({modeName}) *libmodal-lua-Indicator.mode()*
4 years ago
Create a new `Indicator` for a mode.
Parameters: ~
{modeName} The name of the mode that this `Indicator` is for.
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local indicator = libmodal.utils.Indicator.mode('FOO')
libmodal.utils.api.nvim_lecho(indicator)
<
See also: ~
|libmodal-mode| For this function's use.
|libmodal-lua-api.nvim_lecho()| For effective |echo|ing of this
function.
4 years ago
`Indicator`.prompt({modeName}) *libmodal-lua-Indicator.prompt()*
4 years ago
Create a new `Indicator` for a prompt.
Parameters: ~
{modeName} The name of the mode that this `Indicator` is for.
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local indicator = libmodal.utils.Indicator.prompt('FOO')
print(indicator) -- you can't use `nvim_lecho` with this one.
<
See also: ~
|libmodal-prompt| For this function's use.
4 years ago
================================================================================
5.1. `libmodal.Indicator.HighlightSegment` *libmodal-lua-HighlightSegment*
4 years ago
The `HighlightSegment` is a map describing how a particular string should be
highlighted by Vim.
These `HighlightSegment`s can be put into a list and interpreted by
`libmodal.utils.api.nvim_lecho()`.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-HighlightSegment-functions*
4 years ago
4 years ago
`self`.hl *libmodal-lua-HighlightSegment.hl*
4 years ago
Which |highlight-group| to use when |highlight|ing `HighlightSegment.str`.
Type: ~
|highlight-group| `string`.
4 years ago
`self`.str *libmodal-lua-HighlightSegment.str*
4 years ago
What this `HighlightSegment`'s string value is.
Type: ~
`string`
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-HighlightSegment-Functions*
4 years ago
4 years ago
`HighlightSegment`.new({hlgroup}, {str}) *libmodal-lua-HighlightSegment.new()*
--
4 years ago
Create a new `HighlightSegment`.
Parameters: ~
{hlgroup} The |highlight-group| to use when |highlight|ing {str}.
{str} The {str} to |highlight|.
Return: ~
4 years ago
- A new `HighlightSegment`.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local api = libmodal.utils.api
local HighlightSegment = libmodal.Indicator.HighlightSegment
api.nvim_lecho({
HighlightSegment.new('ModeMsg', '-- '),
HighlightSegment.new('None', 'FOO')
HighlightSegment.new('ModeMsg', ' --'),
})
<
4 years ago
================================================================================
6. `libmodal.Layer` *libmodal-lua-Layer*
The libmodal `Layer` class provides many additional features to the base
`libmodal.layer.enter()`.
As there is no default mapping to leave a `Layer`, `libmodal.layer.enter()`
returns an anonymous `Layer`'s `:exit()` `function`. By directly having a
reference to a `Layer`, one can use the other `function`s that are provided,
and extend was is possible.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Layer-functions*
4 years ago
`self`:enter() *libmodal-lua-Layer.enter()*
Enter the `Layer`.
Note: mappings only get replaced for the current buffer.
*Error when the Layer has been entered previously but hasn't been exited.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local layer = libmodal.Layer.new({
n = {
gg = {
rhs = G
4 years ago
}
}
})
-- use `layer:exit()` or close the buffer to exit.
layer:enter()
<
See also: ~
|libmodal-lua-Layer.exit()| How to enter the `Layer`.
4 years ago
`self`:map({mode}, {lhs}, {rhs}, {options}) *libmodal-lua-Layer.map()*
Add a mapping to the `Layer`.
Note: if the layer has been `:enter()`ed, then the current buffer's
mappings will be updated.
Parameters: ~
{mode} The |mode| that this mapping for.
Note: |libmodal-mode| support is coming.
{lhs} The left hand side of the mapping.
{rhs} The right hand side of the mapping.
{options} Options for the mapping.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local layer = libmodal.Layer.new({
n = {
gg = {
rhs = G
4 years ago
}
}
})
-- this adds a binding for `G` to `gg` in normal mode.
layer:map('n', 'G', 'gg', {noremap = true})
4 years ago
-- use `layer:exit()` or close the buffer to exit.
layer:enter()
-- you can also call `:map()` after entering.
-- this adds a binding for `o` to `gg` in visual mode.
layer:map('v', 'o', 'gg', {noremap = true})
4 years ago
<
See also: ~
|nvim_buf_set_keymap()| For more information about the parameters.
4 years ago
`self`:unmap({mode}, {lhs}) *libmodal-lua-Layer.unmap()*
Remove a mapping from the `Layer`.
Parameters: ~
{mode} The |mode| that this mapping for.
Note: |libmodal-mode| support is coming.
{lhs} The left hand side of the mapping.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local layer = libmodal.Layer.new({
n = {
gg = {
rhs = G
4 years ago
}
}
})
-- This adds a binding for `G` to `gg` in normal mode.
layer:map('n', 'G', 'gg', {noremap = true})
4 years ago
-- Unmap the initial `gg`.
layer:unmap('n', 'gg')
-- Use `layer:exit()` or close the buffer to exit.
layer:enter()
-- Start a timer for five seconds.
vim.loop.new_timer():start(5000, 0, vim.schedule_wrap(
-- You can also call `:unmap()` after entering.
function() layer:unmap('n', 'G') end
-- No bindings should be active now.
))
<
See also: ~
|nvim_buf_del_keymap()| For more information about the parameters.
4 years ago
`self`:exit() *libmodal-lua-Layer.exit()*
Exit the `Layer`.
*Error when the Layer has not been entered yet.
4 years ago
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
local layer = libmodal.Layer.new({
n = {
gg = {
rhs = G
4 years ago
}
}
})
-- Enter the layer.
layer:enter()
-- Start a timer for five seconds.
vim.loop.new_timer():start(5000, 0,
4 years ago
-- Exit the layer. `gg` should return to normal.
vim.schedule_wrap(layer:exit())
)
4 years ago
<
See also: ~
|libmodal-lua-Layer.enter()| How to enter the `Layer`.
4 years ago
`Layer`.new({keymap}) *libmodal-lua-Layer.new()*
Create a new `Layer` for `mappings`.
Parameters: ~
4 years ago
{keymap} The list of |map|pings to replace.
4 years ago
Returns: ~
4 years ago
- A new `Layer`.
4 years ago
See also: ~
|libmodal-layer| For more information about the parameters.
4 years ago
================================================================================
7. `libmodal.Mode` *libmodal-lua-Mode*
4 years ago
While `libmodal.mode.enter()` may enter a mode, it silently creates a `Mode`
underneath: >
function mode.enter(...)
Mode.new(...):enter()
end
<
If you wish to have more control over a mode, such as the text of an
`Indicator`, one may manually create a `Mode` with `Mode.new()` and then
change some of the properties in order to make the mode do what you want.
Additionally, one may alter the actual `function`s that are used by a `Mode`
without forking |libmodal|. By using `setmetatable`, and `vim.inspect()`, one
can grab code from the |nvim-libmodal| repository and change whatever they
want to for their mode specifically. >
local libmodal = require 'libmodal'
4 years ago
local Mode = libmodal.Mode
local HighlightSegment = libmodal.Indicator.HighlightSegment
local fooMode = Mode.new(…)
fooMode.indicator = {
HighlightSegment.new('Error', '*'),
HighlightSegment.new('None', ' Something a duck walks on '),
HighlightSegment.new('Error', '>'),
HighlightSegment.new('None', ' ')
}
fooMode
fooMode:enter()
< (The specifications for these functions can be found at other places in this
document.)
4 years ago
--------------------------------------------------------------------------------
VARIABLES *libmodal-lua-Mode-variables*
4 years ago
4 years ago
`self`.exit *libmodal-lua-Mode.exit*
A liason to the `g:`{name}`ModeExit` variable.
Type: ~
`libmodal.Vars`
Value: ~
`libmodal.Vars.new('exit', `{name}`)`
4 years ago
`self`.indicator *libmodal-lua-Mode.indicator*
4 years ago
The message that is shown in the bottom-left corner of the screen while the
mode is active.
Note: this value may be replaced with any `table` of `HighlightSegment`s
(as shown in |libmodal-lua-Mode|). That will allow you to change the
message that a mode has from the vim-default "-- {name} --" to
something else.
Type: ~
`libmodal.Indicator`/array-like `table` of `HighlightSegment`s.
Value: ~
`libmodal.Indicator.mode(`{name}`)`
4 years ago
`self`.input *libmodal-lua-Mode.input*
4 years ago
A liason to `g:`{name}`ModeInput`.
Note: you may use {ModeInstance}`.input:nvimGet()` and `:nvimSet()` to
consistently read and write to the mode's unique variable.
Type: ~
`libmodal.Vars`
Value: ~
`libmodal.Vars.new('input', `{name}`)`
4 years ago
`self`.inputBytes *libmodal-lua-Mode.inputBytes*
4 years ago
The history of user input, stored as |char2nr|s.
4 years ago
Note: This variable is inert when `libmodal.Mode.new()`'s {instruction}
parameter is a `function`. Feel free to use it rather than creating
your own, to make your code more familiar to others.
4 years ago
Type: ~
array-like `table` of |char2nr|s.
4 years ago
Value: ~
4 years ago
- `nil` => {instruction} is a `function`.
- `{}` => {instruction} is a `table`.
4 years ago
`self`.mappings *libmodal-lua-Mode.mappings*
4 years ago
The mappings that have been processed by a `Mode` when
`libmodal.Mode.new()`'s {instruction} parameter is a `table`.
4 years ago
Users may add or remove mappings at any time, and they will be reflected
in the next keypress.
Type: ~
4 years ago
`libmodal.collections.ParseTable`
Value: ~
4 years ago
`libmodal.collections.ParseTable.new(`{instruction}`)`
See also: ~
|libmodal-lua-ParseTable| For information about how to use this
variable.
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Mode-functions*
4 years ago
`self`:enter() *libmodal-lua-Mode.enter()*
4 years ago
Enter the `Mode` that was created.
4 years ago
Example: ~
>
local Mode = require('libmodal').Mode
4 years ago
local fooMode = Mode.new('FOO', {zz = 'tabnew', gg = 'tabclose'})
fooMode:enter()
<
4 years ago
`Mode`.new({name}, {instruction} [, {supressExit}]) *libmodal-lua-Mode.new()*
4 years ago
Create a new `Mode` with a given {name}, using an {instruction} to perform
whenever a key is pressed, and whether or not to {supressExit} handling.
4 years ago
Return: ~
A new `Mode`.
4 years ago
See also: ~
|libmodal-mode| For more information, as all of the parameters are
the same.
4 years ago
================================================================================
8. `libmodal.Prompt` *libmodal-lua-Prompt*
4 years ago
While `libmodal.prompt.enter()` may enter a |libmodal-prompt|, itilently
creates a `Mode` underneath: >
function prompt.enter(...)
Prompt.new(...):enter()
end
<
4 years ago
See |libmodal-lua-Mode| for more information about the possibilities that are
enabled by using such a "class".
4 years ago
--------------------------------------------------------------------------------
VARIABLES *libmodal-lua-Prompt-variables*
4 years ago
`self`.indicator *libmodal-lua-Prompt.indicator*
4 years ago
The message that is shown in the bottom-left corner of the screen while the
mode is active.
4 years ago
Note: this value may be replaced with any `HighlightSegment`. That will
allow you to change the message to something else.
4 years ago
Type: ~
`HighlightSegment`
4 years ago
Value: ~
`libmodal.Indicator.prompt(`{name}`)`
4 years ago
`self`.input *libmodal-lua-Prompt.input*
4 years ago
A liason to `g:`{name}`ModeInput`.
4 years ago
Note: you may use {ModeInstance}`.input:nvimGet()` and `:nvimSet()` to
consistently read and write to the mode's unique variable.
4 years ago
Type: ~
`libmodal.Vars`
4 years ago
Value: ~
`libmodal.Vars.new('input', `{name}`)`
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Prompt-functions*
4 years ago
4 years ago
`self`:enter() *libmodal-lua-Prompt.enter()*
4 years ago
Enter the `Prompt` that was created.
Example: ~
>
4 years ago
local Prompt = require('libmodal').Prompt
4 years ago
local fooMode = Prompt.new('FOO', {new = 'tabnew', close = 'tabclose'})
fooMode:enter()
<
4 years ago
*libmodal-lua-Prompt.new()*
`Prompt`.new({name}, {instruction} [, {completions}, {supressExit}])
4 years ago
User input is taken using |input()|. It is passed through a |g:var|
determined by the {name} of the mode. For example, if {name} is "FOO"
then the |g:var| is `g:fooModeInput`.
Return: ~
A new `Mode`.
See also: ~
4 years ago
|libmodal-prompt| For more information, as all of the parameters are
the same.
4 years ago
================================================================================
9. `libmodal.utils` *libmodal-lua-utils*
Provides extra utilities to the |libmodal| library.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-utils-functions*
4 years ago
`utils`.show_error({pcall_err}) *libmodal-lua-utils.show_error()*
Show an error from `pcall()`.
Parameters: ~
4 years ago
{pcall_err} the error generated by `pcall()`.
Example: ~
>
local libmodal = require 'libmodal'
-- Run `pcall` on an anonymous function.
4 years ago
local noErrors, pcall_err = pcall(function()
-- This will always produce an error.
return "" .. true
end)
-- If there were errors…
if not noErrors then
-- Show the error.
4 years ago
libmodal.utils.show_error(pcall_err)
end
<
=============================================================================
4 years ago
9.1. `libmodal.utils.api` *libmodal-lua-api*
Provides extensions to the `vim.api` |Lua| library.
See: |API|.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-api-functions*
`api`.mode_exit({exit_char}) *libmodal-lua-api.mode_exit()*
Use |feedkeys()| to send an {exit_char} which signals a |libmodal-mode| or
|libmodal-prompt| to stop listening for input.
It is not usually necessary to specify {exit_char}. It is only necessary
to do so when |libmodal-mode|'s {supressExit} flag is active, because
it is possible that the user has set up some non-default character which
should be used to exit the mode (see |libmodal-examples-supress-exit|).
Parameters: ~
{exit_char} (Optional) The character used to exit the mode, or
|libmodal-lua-globals.ESC_NR| by default.
Can be a character number (see |char2nr()|) or a string.
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
-- This function is called every time the user presses a key.
local function _instruction()
-- Exit the mode after pressing any key.
libmodal.utils.api.mode_exit()
end
libmodal.mode.enter('Press any key to exit.', _instruction)
<
See also: ~
|char2nr()| For more information about character numbers.
|libmodal-mode| For more information about {supressExit}.
|feedkeys()| For information about how this method is implemented.
4 years ago
`api`.nvim_bell() *libmodal-lua-api.nvim_bell()*
Make vim ring the visual/audio bell, if it is enabled.
Example: ~
>
local libmodal = require 'libmodal'
4 years ago
libmodal.utils.api.nvim_bell()
<
See also: ~
'belloff' For bell settings.
'errorbells' For bell settings.
'visualbell' For bell settings.
4 years ago
`api`.nvim_input() *libmodal-lua-api.nvim_input()*
Gets one character of user input, as a number.
Uses |getchar()|.
Return: ~
One character of user input, as a number (byte).
Example: ~
>
local libmodal = require 'libmodal'
-- Get one character of user input.
local char = string.char(libmodal.utils.api.nvim_input())
-- … wait for user to press a character …
-- Print the character.
print(char)
<
4 years ago
`api`.nvim_lecho({hlTables}) *libmodal-lua-api.nvim_lecho()*
Echo an `Indicator`.
Meant to be read as "nvim list echo".
Parameters: ~
{hlTables} The `tables` to |echo| with |highlights|.
Example: ~
>
local libmodal = require 'libmodal'
-- Create an indicator.
local indicator = libmodal.utils.Indicator.mode('FOO')
-- Show the indicator.
libmodal.utils.api.nvim_lecho(indicator)
<
4 years ago
`api`.nvim_redraw() *libmodal-lua-api.nvim_redraw()*
Run |mode| to refresh the screen.
The function was not named `nvim_mode` because that would be really
confusing given the name of this plugin.
Example: ~
>
local libmodal = require 'libmodal'
-- Echo hello and prompt for input before continuing.
vim.api.nvim_command "echo 'Hello!'"
vim.fn.getchar()
-- Clear the screen.
libmodal.utils.api.nvim_redraw()
<
4 years ago
`api`.nvim_show_err({title}, {msg}) *libmodal-lua-api.nvim_show_err()*
Show a {title}-error with a given {msg}.
Parameters: ~
{title} The title of the error.
{msg} The message of the error.
Example: ~
>
local libmodal = require 'libmodal'
-- Show an error.
libmodal.utils.api.nvim_show_err(
-- Use the default error title.
libmodal.globals.DEFAULT_ERROR_TITLE,
-- Use a custom error message.
'This is a test error!'
)
<
4 years ago
================================================================================
9.2. `libmodal.utils.Help` *libmodal-lua-Help*
Allows for the creation of a default "Help" table.
By default, this "Help" is shown by pressing `?` in a |libmodal-mode| or by
entering "help" into a |libmodal-prompt|.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Help-functions*
4 years ago
`Help`.new({commandsOrMaps}, {title}) *libmodal-lua-Help.new()*
Create a default help table with `commandsOrMaps` and vim expressions.
Parameters: ~
{commandsOrMaps} The `table` of |command|s or |map|pings to Vim |expression|s.
{title} The leftmost table column's title.
Return: ~
A new `Help`.
Example: ~
>
local libmodal = require 'libmodal'
-- Create a table of mock user commands.
local commands = {
close = 'tabclose',
new = 'tabnew'
}
-- Create a table of mock user maps.
local maps = {
zf = 'split',
zfo = 'echo "Hello!"'
}
local commandHelp = libmodal.utils.Help.new(commands, 'COMMANDS')
local mapsHelp = libmodal.utils.Help.new(maps, 'MAPS')
<
4 years ago
`self`:show() *libmodal-lua-Help.show()*
Show the contents of this `Help`.
Example: ~
>
local libmodal = require 'libmodal'
-- Create a table of mock user commands.
local commands = {
close = 'tabclose',
new = 'tabnew'
}
-- Create a table of mock user maps.
local maps = {
zf = 'split',
zfo = 'echo "Hello!"'
}
local commandHelp = libmodal.utils.Help.new(commands, 'COMMANDS')
local mapsHelp = libmodal.utils.Help.new(maps, 'MAPS')
commandHelp:show()
mapsHelp:show()
<
4 years ago
================================================================================
9.3. `libmodal.utils.WindowState` *libmodal-lua-WindowState*
Tracks 'winheight' and 'winwidth' when created, so that it can be modified
freely and then restored later.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-WindowState-functions*
4 years ago
`WindowState`.new() *libmodal-lua-WindowState.new()*
Create a table representing the size of the current window.
Return: ~
The new `WindowState`.
Example: ~
>
local libmodal = require 'libmodal'
local windowState = libmodal.utils.WindowState.new()
print(vim.inspect(windowState))
<
See also: ~
'winheight' The `height` property of a `WindowState`.
'winwidth' The `width` property of a `WindowState`.
4 years ago
`self`:restore() *libmodal-lua-WindowState.restore()*
Restore the state of this `WindowState`.
Example: ~
>
local libmodal = require 'libmodal'
-- Create a new `WindowState`.
local windowState = libmodal.utils.WindowState.new()
-- Set the 'winheight' to a new value.
vim.go.winheight = 100
-- Define a function to print the 'winheight' value.
local print_height = function()
vim.api.nvim_command 'echo &winheight'
end
-- Print the 'winheight' prior to `restore()`.
print_height()
-- Restore the `windowState`.
windowState:restore()
-- Print the 'winheight' after `restore()`ing.
print_height()
<
4 years ago
================================================================================
10. `libmodal.Vars` *libmodal-lua-Vars*
4 years ago
A `Var`'s purpose is to act as an intermediary between |global-variables| and
the modes that use them.
4 years ago
--------------------------------------------------------------------------------
FUNCTIONS *libmodal-lua-Vars.functions*
4 years ago
`self`:name() *libmodal-lua-Vars.name()*
Get the name of `modeName`s global setting.
Parameters: ~
{modeName} The name of the mode.
Return: ~
4 years ago
- The name of the vimscript variable that this `Var` corresponds to.
Example: ~
>
local libmodal = require 'libmodal'
local input = libmodal.Vars('input', 'FOO')
print(input:name()) -- 'fooModeInput'
<
4 years ago
`self`:nvimGet() *libmodal-lua-Vars.nvimGet()*
Retrieve a variable value.
Parameters: ~
{modeName} The mode name this value is being retrieved for.
Return: ~
* The |global-variable| value that this `Var` represents
Example: ~
>
local libmodal = require 'libmodal'
local input = libmodal.Vars('input', 'FOO')
vim.g[input:name()] = 'test'
print(input:nvimGet())
<
4 years ago
`self`:nvimSet({val}) *libmodal-lua-Vars.nvimSet()*
Set a |variable| value.
Parameters: ~
{modeName} The mode name this value is being retrieved for.
{val} The value to set `self`'s Vimscript var to.
Example: ~
>
local libmodal = require 'libmodal'
local input = libmodal.Vars('input', 'FOO')
input:nvimSet('test')
print(input:nvimGet())
<
4 years ago
`Vars`.new(keyName, modeName) *libmodal-lua-Vars.new()*
Create a new `Var`.
Parameters: ~
{keyName} The name of the key used to refer to this variable in `Vars`.
{modeName} The name of the mode that this `Variable` is for.
Return: ~
* A new `Var`.
4 years ago
4 years ago
================================================================================
vim:tw=80:ts=4:ft=help:norl: