Hooks

You can execute custom logic when a preview window is opened or closed using post_open_hook and post_close_hook in the setup configuration.

post_open_hook

This function is called immediately after the floating window is created and the buffer is set. It receives two arguments: buffer (id) and window (id).

Common Use Case: Setting buffer-local keymaps for the preview window.

require('goto-preview').setup {
  post_open_hook = function(buf, win)
    -- Map 'q' to close the window immediately
    vim.keymap.set("n", "q", function()
      vim.api.nvim_win_close(win, true)
    end, { buffer = buf })
  end
}

post_close_hook

This function is called right before the preview window is closed. It also receives buffer and window arguments.

Common Use Case: Cleaning up resources or undoing temporary keybindings (though buffer-local keymaps usually clean themselves up).

require('goto-preview').setup {
  post_close_hook = function(buf, win)
    print("Closing preview window: " .. win)
  end
}