From 6cbe9b129061e6f6e6c392ffc756e0464022937c Mon Sep 17 00:00:00 2001 From: Thales Mello Date: Fri, 15 Aug 2025 23:11:51 -0700 Subject: [PATCH] Add autocmd with arbitrary data for autocommands --- README.md | 23 +++++++++++++++++------ binary.py | 6 ++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a1c7700..bd281c2 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,25 @@ Replace `4001` with the port number you have set in the browser extension ## Custom settings according to website -When you trigger GhostText, nvim-ghost triggers an `User` autocommand. You can -listen for that autocommand and run your own commands (e.g. setting filetype) - -**NOTE:** All the autocommands should be in the `nvim_ghost_user_autocommands` -augroup - +When you trigger GhostText, nvim-ghost triggers an `User` autocommand. You +can either use `nvim_create_autocmd()` to listen for the `GhostTextAttach` +pattern and retrieve the URL from the `data.url` callback parameter (Lua), or +create an autocommand in the `nvim_ghost_user_autocommands` augroup and +pattern match against the URL directly to set the filetype. Some examples - +```lua +vim.api.nvim_create_autocmd({ 'User' }, { + group = vim.api.nvim_create_augroup('NvimGhostText', { clear = true }), + pattern = {"GhostTextAttach"}, + callback = function(ev) + if ev.data.url == "github.com" then + vim.o.filetype = "markdown" + end + end, +}) +``` + ```vim " Autocommand for a single website (i.e. stackoverflow.com) au nvim_ghost_user_autocommands User www.stackoverflow.com setfiletype markdown diff --git a/binary.py b/binary.py index 6d12314..e624428 100644 --- a/binary.py +++ b/binary.py @@ -444,6 +444,12 @@ def _neovim_handler(self, *args): def _trigger_autocmds(self, url: str): self.neovim_handle.command(f"doau nvim_ghost_user_autocommands User {url}") + self.neovim_handle.api.exec_autocmds("User", { + "pattern": "GhostTextAttach", + "data": { + "url": url + } + }) def _do_close(self): log(