Autocmds
Auto commands let you register functions that will be automatically invoked when a certain event happens within Glide.
Suppose you want to define a key mapping to navigate directly from a GitHub repository to the Issues page, first you'd define a function to update the current URL:
async function github_go_to_issues() {
const url = new URL(glide.ctx.url);
const parts = url.pathname.split("/").filter(Boolean);
assert(
parts.length > 2,
`Path does not look like github.com/$org/$repo`,
);
url.pathname = `/${parts[0]}/${parts[1]}/issues`;
await browser.tabs.update({ url: url.toString() });
}
And then define an autocmd that sets a keymap for the current buffer
glide.autocmds.create("UrlEnter", {
hostname: "github.com",
}, async () => {
glide.buf.keymaps.set(
"normal",
"<leader>gi",
github_go_to_issues,
);
});
There are many other things you can accomplish with autocmds.
Reference
Autocmds can be created with:
glide.autocmds.create(event, pattern, callback);
Some autocmds do not require a pattern and can be called with just:
glide.autocmds.create(event, callback);
ConfigLoaded
Fired when the config is first loaded and on every subsequent reload.
glide.autocmds.create("ConfigLoaded", () => {
//
});
WindowLoaded
Fired when Glide is first started.
glide.autocmds.create("WindowLoaded", () => {
//
});
UrlEnter
Fired when the focused URL changes, which can happen under the following circumstances:
- switching tabs
- navigating back/forward through history
- clicking a link or doing anything else that would change the URL in the current tab
The pattern is either a RegExp that matches against the entire URL, or an object with { hostname: string }.
The callback can also return a function that will be called when another UrlEnter event would be fired.
glide.autocmds.create("UrlEnter", {
hostname: "example.com",
}, () => {
//
});
Callback arguments:
{
tab_id: number;
url: string;
}
ModeChanged
Fired when the mode changes.
The pattern is matched against old_mode:new_mode. You can also use * as a placeholder to match any mode.
For example, to define an autocmd that will be fired every time hint mode is entered: "*:hint"
or when hint mode is left: "hint:*"
or transitioning from hint to insert: "hint:insert"
or for just any mode: "*"
glide.autocmds.create(
"ModeChanged",
"*",
({ old_mode, new_mode }) => {
//
},
);
Callback arguments:
{
readonly old_mode: GlideMode | null;
readonly new_mode: GlideMode;
}
KeyStateChanged
Fired whenever the key sequence changes, which can happen under four circumstances:
- A key is pressed that matches a key mapping.
- A key is pressed that is part of a key mapping.
- A key is pressed that cancels a previous partial key mapping sequence.
- A partial key mapping is cancelled (see glide.o.mapping_timeout)
For example, with
glide.keymaps.set("normal", "gt", "...");
Pressing g will fire with { sequence: ["g"], partial: true }, then either:
- Pressing t would fire { sequence: ["g", "t"], partial: false }
- Pressing any other key would fire { sequence: [], partial: false }
Note that this is not fired for consecutive key presses for keys that don't correspond to mappings, as the key state has not changed.
glide.autocmds.create(
"KeyStateChanged",
({ mode, sequence, partial }) => {
//
},
);
Callback arguments:
{
readonly mode: GlideMode;
readonly sequence: string[];
readonly partial: boolean;
}