From 646a41ea3d0f4168cdb710833a64237d13fec0ef Mon Sep 17 00:00:00 2001
From: Mike Smith <10135646+mikesmithgh@users.noreply.github.com>
Date: Mon, 19 Feb 2024 22:40:07 -0500
Subject: [PATCH] feat: add after_paste_window_ready callback
closes #197
---
README.md | 89 ++++++++++++-----------
lua/kitty-scrollback/autocommands.lua | 12 +++
lua/kitty-scrollback/configs/defaults.lua | 10 +++
lua/kitty-scrollback/launch.lua | 12 +--
4 files changed, 73 insertions(+), 50 deletions(-)
diff --git a/README.md b/README.md
index 15905774..7f6c07c8 100644
--- a/README.md
+++ b/README.md
@@ -520,50 +520,51 @@ The configuration precedence is `default` > `global` > `builtin` > `user` where
#### Configuration Options
-| Options | Type | Description |
-| :---------------------------------------------------------------- | :------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| callbacks | `KsbCallbacks?` | fire and forget callback functions |
-| callbacks.after_setup | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after initializing kitty-scrollback.nvim |
-| callbacks.after_launch | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after launch started to process the scrollback buffer |
-| callbacks.after_ready | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after scrollback buffer is loaded and cursor is positioned |
-| keymaps_enabled | `boolean?` | if true, enabled all default keymaps |
-| restore_options | `boolean?` | if true, restore options that were modified while processing the scrollback buffer |
-| highlight_overrides | `KsbHighlights?` | kitty-scrollback.nvim highlight overrides |
-| highlight_overrides
.KittyScrollbackNvimStatusWinNormal | `table?` | status window Normal highlight group |
-| highlight_overrides
.KittyScrollbackNvimStatusWinHeartIcon | `table?` | status window heart icon highlight group |
-| highlight_overrides
.KittyScrollbackNvimStatusWinSpinnerIcon | `table?` | status window spinner icon highlight group |
-| highlight_overrides
.KittyScrollbackNvimStatusWinReadyIcon | `table?` | status window ready icon highlight group |
-| highlight_overrides
.KittyScrollbackNvimStatusWinKittyIcon | `table?` | status window kitty icon highlight group |
-| highlight_overrides
.KittyScrollbackNvimStatusWinNvimIcon | `table?` | status window vim icon highlight group |
-| highlight_overrides
.KittyScrollbackNvimPasteWinNormal | `table?` | paste window Normal highlight group |
-| highlight_overrides
.KittyScrollbackNvimPasteWinFloatBorder | `table?` | paste window FloatBorder highlight group |
-| highlight_overrides
.KittyScrollbackNvimPasteWinFloatTitle | `table?` | paste window FloatTitle highlight group |
-| highlight_overrides
.KittyScrollbackNvimVisual | `table?` | scrollback buffer window Visual selection highlight group |
-| highlight_overrides
.KittyScrollbackNvimNormal | `table?` | scrollback buffer window Normal highlight group |
-| status_window | `KsbStatusWindowOpts?` | options for status window indicating that kitty-scrollback.nvim is ready |
-| status_window.enabled | `boolean` | If true, show status window in upper right corner of the screen |
-| status_window.style_simple | `boolean` | If true, use plaintext instead of nerd font icons |
-| status_window.autoclose | `boolean` | If true, close the status window after kitty-scrollback.nvim is ready |
-| status_window.show_timer | `boolean` | If true, show a timer in the status window while kitty-scrollback.nvim is loading |
-| status_window.icons | `KsbStatusWindowIcons?` | Icons displayed in the status window |
-| status_window.icons.kitty | `string` | kitty status window icon |
-| status_window.icons.heart | `string` | heart string heart status window icon |
-| status_window.icons.nvim | `string` | nvim status window icon |
-| paste_window | `KsbPasteWindowOpts?` | options for paste window that sends commands to Kitty |
-| paste_window.highlight_as_normal_win | `fun(): boolean?` | If function returns true, use Normal highlight group. If false, use NormalFloat |
-| paste_window.filetype | `string?` | The filetype of the paste window |
-| paste_window.hide_footer | `boolean?` | If true, hide mappings in the footer when the paste window is initially opened |
-| paste_window.winblend | `integer?` | The winblend setting of the window, see :help winblend |
-| paste_window.winopts_overrides | `fun(paste_winopts: KsbWinOpts): table?` | Paste float window overrides, see nvim_open_win() for configuration |
-| paste_window.footer_winopts_overrides | `fun(footer_winopts: KsbWinOpts, paste_winopts: KsbWinOpts): table?` | Paste footer window overrides, see nvim_open_win() for configuration |
-| paste_window.yank_register | `string?` | register used during yanks to paste window, see `:h registers` |
-| paste_window.yank_register_enabled | `boolean?` | If true, the `yank_register` copies content to the paste window. If false, disable yank to paste window |
-| kitty_get_text | `KsbKittyGetText?` | options passed to get-text when reading scrollback buffer, see `kitty @ get-text --help` |
-| kitty_get_text.ansi | `boolean` | If true, the text will include the ANSI formatting escape codes for colors, bold, italic, etc. |
-| kitty_get_text.clear_selection | `boolean` | If true, clear the selection in the matched window, if any. |
-| kitty_get_text.extent | `string` | What text to get. The default of screen means all text currently on the screen. all means all the `screen+scrollback` and selection means the currently selected text. `first_cmd_output_on_screen` means the output of the first command that was run in the window on screen. `last_cmd_output` means the output of the last command that was run in the window. `last_visited_cmd_output` means the first command output below the last scrolled position via scroll_to_prompt. `last_non_empty_output` is the output from the last command run in the window that had some non empty output. The last four require `shell_integration` to be enabled. Choices: `screen`, `all`, `first_cmd_output_on_screen`, `last_cmd_output`, `last_non_empty_output`, `last_visited_cmd_output`, `selection` |
-| checkhealth | `boolean?` | if true execute :checkhealth kitty-scrollback and skip setup |
-| visual_selection_highlight_mode | `string?` | Sets the mode for coloring the Visual highlight group in the scrollback buffer window. `darken` uses a darkened version of the Normal highlight group to improve readability. `kitty` uses the colors defined for `selection_foreground` and `selection_background` in your Kitty configuration. `nvim` uses the default colors defined in the `Visual` highlight group. `reverse` reverses the foreground and background colors of the visual selection. |
+| Options | Type | Description |
+| :---------------------------------------------------------------- | :------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| callbacks | `KsbCallbacks?` | fire and forget callback functions |
+| callbacks.after_setup | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after initializing kitty-scrollback.nvim |
+| callbacks.after_launch | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after launch started to process the scrollback buffer |
+| callbacks.after_ready | `fun(kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after scrollback buffer is loaded and cursor is positioned |
+| callbacks.after_paste_window_ready | `fun(paste_window_data: KsbPasteWindowData, kitty_data: KsbKittyData, opts: KsbOpts)?` | callback executed after the paste window is opened or resized |
+| keymaps_enabled | `boolean?` | if true, enabled all default keymaps |
+| restore_options | `boolean?` | if true, restore options that were modified while processing the scrollback buffer |
+| highlight_overrides | `KsbHighlights?` | kitty-scrollback.nvim highlight overrides |
+| highlight_overrides
.KittyScrollbackNvimStatusWinNormal | `table?` | status window Normal highlight group |
+| highlight_overrides
.KittyScrollbackNvimStatusWinHeartIcon | `table?` | status window heart icon highlight group |
+| highlight_overrides
.KittyScrollbackNvimStatusWinSpinnerIcon | `table?` | status window spinner icon highlight group |
+| highlight_overrides
.KittyScrollbackNvimStatusWinReadyIcon | `table?` | status window ready icon highlight group |
+| highlight_overrides
.KittyScrollbackNvimStatusWinKittyIcon | `table?` | status window kitty icon highlight group |
+| highlight_overrides
.KittyScrollbackNvimStatusWinNvimIcon | `table?` | status window vim icon highlight group |
+| highlight_overrides
.KittyScrollbackNvimPasteWinNormal | `table?` | paste window Normal highlight group |
+| highlight_overrides
.KittyScrollbackNvimPasteWinFloatBorder | `table?` | paste window FloatBorder highlight group |
+| highlight_overrides
.KittyScrollbackNvimPasteWinFloatTitle | `table?` | paste window FloatTitle highlight group |
+| highlight_overrides
.KittyScrollbackNvimVisual | `table?` | scrollback buffer window Visual selection highlight group |
+| highlight_overrides
.KittyScrollbackNvimNormal | `table?` | scrollback buffer window Normal highlight group |
+| status_window | `KsbStatusWindowOpts?` | options for status window indicating that kitty-scrollback.nvim is ready |
+| status_window.enabled | `boolean` | If true, show status window in upper right corner of the screen |
+| status_window.style_simple | `boolean` | If true, use plaintext instead of nerd font icons |
+| status_window.autoclose | `boolean` | If true, close the status window after kitty-scrollback.nvim is ready |
+| status_window.show_timer | `boolean` | If true, show a timer in the status window while kitty-scrollback.nvim is loading |
+| status_window.icons | `KsbStatusWindowIcons?` | Icons displayed in the status window |
+| status_window.icons.kitty | `string` | kitty status window icon |
+| status_window.icons.heart | `string` | heart string heart status window icon |
+| status_window.icons.nvim | `string` | nvim status window icon |
+| paste_window | `KsbPasteWindowOpts?` | options for paste window that sends commands to Kitty |
+| paste_window.highlight_as_normal_win | `fun(): boolean?` | If function returns true, use Normal highlight group. If false, use NormalFloat |
+| paste_window.filetype | `string?` | The filetype of the paste window |
+| paste_window.hide_footer | `boolean?` | If true, hide mappings in the footer when the paste window is initially opened |
+| paste_window.winblend | `integer?` | The winblend setting of the window, see :help winblend |
+| paste_window.winopts_overrides | `fun(paste_winopts: KsbWinOpts): table?` | Paste float window overrides, see nvim_open_win() for configuration |
+| paste_window.footer_winopts_overrides | `fun(footer_winopts: KsbWinOpts, paste_winopts: KsbWinOpts): table?` | Paste footer window overrides, see nvim_open_win() for configuration |
+| paste_window.yank_register | `string?` | register used during yanks to paste window, see `:h registers` |
+| paste_window.yank_register_enabled | `boolean?` | If true, the `yank_register` copies content to the paste window. If false, disable yank to paste window |
+| kitty_get_text | `KsbKittyGetText?` | options passed to get-text when reading scrollback buffer, see `kitty @ get-text --help` |
+| kitty_get_text.ansi | `boolean` | If true, the text will include the ANSI formatting escape codes for colors, bold, italic, etc. |
+| kitty_get_text.clear_selection | `boolean` | If true, clear the selection in the matched window, if any. |
+| kitty_get_text.extent | `string` | What text to get. The default of screen means all text currently on the screen. all means all the `screen+scrollback` and selection means the currently selected text. `first_cmd_output_on_screen` means the output of the first command that was run in the window on screen. `last_cmd_output` means the output of the last command that was run in the window. `last_visited_cmd_output` means the first command output below the last scrolled position via scroll_to_prompt. `last_non_empty_output` is the output from the last command run in the window that had some non empty output. The last four require `shell_integration` to be enabled. Choices: `screen`, `all`, `first_cmd_output_on_screen`, `last_cmd_output`, `last_non_empty_output`, `last_visited_cmd_output`, `selection` |
+| checkhealth | `boolean?` | if true execute :checkhealth kitty-scrollback and skip setup |
+| visual_selection_highlight_mode | `string?` | Sets the mode for coloring the Visual highlight group in the scrollback buffer window. `darken` uses a darkened version of the Normal highlight group to improve readability. `kitty` uses the colors defined for `selection_foreground` and `selection_background` in your Kitty configuration. `nvim` uses the default colors defined in the `Visual` highlight group. `reverse` reverses the foreground and background colors of the visual selection. |
### Nerd Fonts
diff --git a/lua/kitty-scrollback/autocommands.lua b/lua/kitty-scrollback/autocommands.lua
index ae478dac..9bd032d6 100644
--- a/lua/kitty-scrollback/autocommands.lua
+++ b/lua/kitty-scrollback/autocommands.lua
@@ -72,6 +72,18 @@ M.set_paste_window_resized_autocmd = function()
vim.schedule(function()
local cur_winopts = ksb_footer_win.footer_winopts(current_winopts)
pcall(vim.api.nvim_win_set_config, p.footer_winid, cur_winopts)
+ if
+ opts.callbacks
+ and opts.callbacks.after_paste_window_ready
+ and type(opts.callbacks.after_paste_window_ready) == 'function'
+ then
+ local paste_window_data = {
+ scrollback_buffer = { bufid = p.bufid, winid = p.winid },
+ paste_window = { bufid = p.paste_bufid, winid = p.paste_winid },
+ paste_window_footer = { bufid = p.footer_bufid, winid = p.footer_winid },
+ }
+ opts.callbacks.after_paste_window_ready(paste_window_data, p.kitty_data, opts)
+ end
ksb_footer_win.open_footer_window(cur_winopts, true)
end)
end
diff --git a/lua/kitty-scrollback/configs/defaults.lua b/lua/kitty-scrollback/configs/defaults.lua
index 105d2435..048c10e4 100644
--- a/lua/kitty-scrollback/configs/defaults.lua
+++ b/lua/kitty-scrollback/configs/defaults.lua
@@ -1,7 +1,17 @@
+---@class KsbBufWinData
+---@field bufid integer|nil
+---@field winid integer|nil
+
+---@class KsbPasteWindowData
+---@field scrollback_buffer KsbBufWinData data for the scrollback buffer
+---@field paste_window KsbBufWinData data for the paste window
+---@field paste_window_footer KsbBufWinData data for the paste window footer
+
---@class KsbCallbacks
---@field after_setup fun(kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after initializing kitty-scrollback.nvim
---@field after_launch fun(kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after launch started to process the scrollback buffer
---@field after_ready fun(kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after scrollback buffer is loaded and cursor is positioned
+---@field after_paste_window_ready fun(paste_window_data:KsbPasteWindowData, kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after the paste window is opened or resized
---@class KsbKittyGetText
---@field ansi boolean|nil If true, the text will include the ANSI formatting escape codes for colors, bold, italic, etc.
diff --git a/lua/kitty-scrollback/launch.lua b/lua/kitty-scrollback/launch.lua
index 8c5783c5..5268a455 100644
--- a/lua/kitty-scrollback/launch.lua
+++ b/lua/kitty-scrollback/launch.lua
@@ -59,15 +59,15 @@ local M = {}
---@class KsbPrivate
---@field orig_columns number
---@field orig_normal_hl table|nil
----@field bufid number|nil
+---@field bufid integer|nil the buffer ID of the scrollback buffer
---@field winid integer|nil the initial window ID of the scrollback buffer, this ID is not always guaranteed to be correct if the user has modified the window layout
----@field paste_bufid number|nil
----@field kitty_loading_winid number|nil
+---@field kitty_loading_winid number|nil the ID of the kitty overlay loading window, this is kitty window not a nvim window
---@field kitty_colors table
---@field kitty_data KsbKittyData
----@field paste_winid number|nil
----@field footer_winid number|nil
----@field footer_bufid number|nil
+---@field paste_winid integer|nil the window ID of the paste window
+---@field paste_bufid integer|nil the buffer ID of the paste window
+---@field footer_winid integer|nil the window ID of the paste window footer
+---@field footer_bufid integer|nil the buffer ID of the paste window footer
---@field pos table|nil
local p = {}