Custom Ex Command in Yi Editor
Tags: programming.haskell, programming.yi, programming.editors
This is something I’ve mentioned before, but I see that I wasn’t entirely clear on how to achieve it.
In more detail, here are some notes on how to write a “Hello World” Ex-style
command for the Yi editor.
– The exciting thing about this is it’s easier to write such commands for Yi
than for Vim.
The following has been checked with v0.12.2 of Yi.
Compare:
Yi.Keymap.Vim.Ex.Commands
module. The ‘Reload’ command is the simplest. - This helps see how Ex commands
for Vim-style Yi are parsed, and how they can map to an ExCommand
, which is
in turn responsible for an Action to execute.
See this gist for a full, working example, but for the code itself, the highlights would be:
- The transformation itself, adding “hello world” into the buffer:
helloWorld :: YiM ()
helloWorld = withCurrentBuffer $ insertN "Hello, world!"
- The parser for the ex command, in the
HelloWorld
module, under thelib/
folder (which is used bydyre
for dependencies for the config) in~/.config/yi
:
parse :: EventString -> Maybe ExCommand
parse "helloWorld" = Just $ impureExCommand {
cmdShow = "helloWorld"
, cmdAction = YiA $ helloWorld
}
parse _ = Nothing
- The config in
yi.hs
, to make use of the aboveparse
function:
import qualified HelloWorld as HelloWorld
...
main = yi $ defaultVimConfig {
defaultKm = mkKeymapSet $ defVimConfig `override` \ super self -> super
{ vimExCommandParsers = myExCmdParsers ++ vimExCommandParsers super }
}
myExCmdParsers = [HelloWorld.parse]
This is enough to get a “Hello World” example going.
For more sophisticated efforts, I’m guessing one would want to:
Look at parsing of Ex commands, using the extant parsers for inspiration.
A more sophisticated
Action
executed; this is where an understanding ofBuffer
/Editor
/Yi
Action
/Monad
would be necessary.
Resources at yi-editor.github.io may or may not be helpful here; grokking parts of the code (e.g. in the keymaps) would be a good place to start.