I wanted to get feedback on this lazy-loading idea before attempting to implementing it.
This model handles lazy-loading by adding two new commands, provides and
requires. The provides command specificies a module that is being provided,
and the commands for that module. The requires command looks up the modules
that have been specified by provides, runs the commands for the given module,
and marks it as loaded. If the module has already been loaded, it returns
immediately. It would fail if the requested module has not been provided. This
method would allow for overriding default modules with a -override switch for
the provides command, which would fail if the module has already been loaded.
requires are guaranteed to work## sh.kak
hook global BufCreate .*\.(z|ba|c|k|mk)?sh(rc|_profile)? %{
requires sh
set-option buffer filetype sh
}
provides sh %{
# highlighters, etc for sh
}
## kak.kak
hook global BufCreate (.*/)?(kakrc|.*.kak) %{
requires sh
requires kak
set-option buffer filetype kak
}
provides kak %{
# highlighters, etc for kak
}
## kakrc
provides -override sh %{
# Overriding sh
}
I think this is a promising direction, its simple, fast, and very flexible, in particular I like the fact that it opens the door towards vim-like autoloading with:
provides sh %{ source "%val{runtime}/rc/sh.kak" }
(I dont think we should do that right now, but in case some scripts become so big that reading and keeping them in memory is deemed too slow, we can go this way).
It shares the nice property @maximbaz's solution had that its just a few lines to add to each .kak files (just wrap the specific parts into provides).
Another thing I was wondering is if we can provide fallback: Say we have kakrc.kak in our autoload but not sh.kak, it would be nice to have that work out of the box. That means kakrc.kak should be able to specify that sh can be provided by sourcing %val{runtime}/rc/core/sh.kak.
Something that will work without extra features would be:
try %{ requires sh } catch %{ source "%val{runtime}/rc/core/sh.kak; requires sh }
If this pattern proves really usefull, it could be implemented as a switch to requires:
requires sh -fallback-source "%val{runtime}/rc/core/sh.kak"
Also, we can bikeshed a bit on the provides and requires command names.
Perhaps this can be closed now?
Most helpful comment
I think this is a promising direction, its simple, fast, and very flexible, in particular I like the fact that it opens the door towards vim-like autoloading with:
(I dont think we should do that right now, but in case some scripts become so big that reading and keeping them in memory is deemed too slow, we can go this way).
It shares the nice property @maximbaz's solution had that its just a few lines to add to each .kak files (just wrap the specific parts into
provides).Another thing I was wondering is if we can provide fallback: Say we have
kakrc.kakin our autoload but notsh.kak, it would be nice to have that work out of the box. That meanskakrc.kakshould be able to specify thatshcan be provided by sourcing%val{runtime}/rc/core/sh.kak.Something that will work without extra features would be:
If this pattern proves really usefull, it could be implemented as a switch to
requires:Also, we can bikeshed a bit on the
providesandrequirescommand names.