### Basic Installation with lazy.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt This snippet shows the basic setup for installing flatten.nvim using the lazy.nvim package manager. It ensures the plugin loads early with high priority to minimize any delay when opening files from a terminal. ```lua require("lazy").setup({ { "willothy/flatten.nvim", config = true, -- Ensure it runs first to minimize delay when opening files from terminal lazy = false, priority = 1001, }, }) ``` -------------------------------- ### Install flatten.nvim with rocks.nvim Source: https://github.com/willothy/flatten.nvim/blob/main/README.md This snippet demonstrates installing flatten.nvim using rocks.nvim, which leverages LuaRocks for package management. After installation, it shows how to set up the plugin with custom configuration. ```vim :Rocks install flatten.nvim ``` ```lua require("flatten").setup({ -- your config }) ``` -------------------------------- ### CLI Usage and Environment Variables Source: https://context7.com/willothy/flatten.nvim/llms.txt Examples for opening files, passing commands, and setting environment variables like VISUAL or EDITOR to use flatten.nvim. ```bash nvim file1.lua file2.lua nvim --cmd 'let g:flatten_wait=1' file.txt nvim -b file.txt export VISUAL="nvim --cmd 'let g:flatten_wait=1'" cat file.txt | nvim - ``` -------------------------------- ### Install flatten.nvim with lazy.nvim Source: https://github.com/willothy/flatten.nvim/blob/main/README.md This snippet shows how to install the flatten.nvim plugin using the lazy.nvim plugin manager. It emphasizes setting `lazy = false` and a high `priority` to ensure the plugin loads early, minimizing delay when opening files from the terminal. ```lua require("lazy").setup({ { "willothy/flatten.nvim", config = true, -- or pass configuration with -- opts = { } -- Ensure that it runs first to minimize delay when opening file from terminal lazy = false, priority = 1001, }, --- ... }) ``` -------------------------------- ### Implement Lifecycle Hooks Source: https://github.com/willothy/flatten.nvim/blob/main/doc/flatten.nvim.txt Examples of hook definitions for managing file opening processes, blocking behavior, and data transmission between guest and host Neovim instances. ```lua require("flatten").setup({ hooks = { should_block = function(argv) return vim.tbl_contains(argv, "-w") end, post_open = function(opts) print("Opened file in window: " .. opts.winnr) end, guest_data = function() return { timestamp = os.time() } end } }) ``` -------------------------------- ### Setup with Custom Options Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua snippet demonstrates how to configure flatten.nvim with custom options. It covers blocking for specific filetypes, window opening behavior (like split, tab, or smart opening), terminal emulator integrations, and nesting control. ```lua require("flatten").setup({ -- Block for specific filetypes (default: gitcommit, gitrebase) block_for = { gitcommit = true, gitrebase = true, }, -- Window opening behavior window = { open = "alternate", -- "current", "alternate", "split", "vsplit", "tab", "smart" diff = "tab_vsplit", -- "split", "vsplit", "tab_split", "tab_vsplit" focus = "first", -- "first" or "last" }, -- Terminal emulator integrations integrations = { kitty = false, wezterm = false, }, -- Nesting behavior nest_if_no_args = false, nest_if_cmds = false, disable_cmd_passthrough = false, }) ``` -------------------------------- ### Basic Usage Examples Source: https://github.com/willothy/flatten.nvim/blob/main/doc/flatten.nvim.txt These bash commands demonstrate various ways to open files using Neovim with flatten.nvim. They cover normal file opening, forcing blocking for file edits, opening files in diff mode, and setting environment variables for visual editing and man page display. ```bash # Open files normally: nvim file1 file2 # Force blocking for a file: # with a custom block handler, you can use `nvim -b file1 file2` vim --cmd 'let g:flatten_wait=1' file1 # Open files in diff mode: vim -d file1 file2 # Enable blocking for $VISUAL # with a custom block handler, you can use `export VISUAL="nvim -b"` export VISUAL="nvim --cmd 'let g:flatten_wait=1'" # allows edit-exec # Enable manpage formatting: export MANPAGER="nvim +Man!" # Execute a command in the host instance, before opening files: vim --cmd # Execute a command in the host instance, after opening files: vim + ``` -------------------------------- ### Configure Toggleterm Integration Source: https://context7.com/willothy/flatten.nvim/llms.txt Provides a robust example of integrating with toggleterm. It includes logic to hide the terminal during blocking operations and automatically clean up git buffers. ```lua require("flatten").setup({ window = { open = "alternate", }, hooks = { should_block = function(argv) return vim.tbl_contains(argv, "-b") end, pre_open = function() local term = require("toggleterm.terminal") local termid = term.get_focused_id() vim.g._flatten_term = termid and term.get(termid) or nil end, post_open = function(opts) if opts.is_blocking and vim.g._flatten_term then vim.g._flatten_term:close() else vim.api.nvim_set_current_win(opts.winnr) end if opts.filetype == "gitcommit" or opts.filetype == "gitrebase" then vim.api.nvim_create_autocmd("BufWritePost", { buffer = opts.bufnr, once = true, callback = vim.schedule_wrap(function() vim.api.nvim_buf_delete(opts.bufnr, {}) end), }) end end, block_end = function() vim.schedule(function() if vim.g._flatten_term then vim.g._flatten_term:open() vim.g._flatten_term = nil end end) end, }, }) ``` -------------------------------- ### pre_open Hook for Pre-File Opening Actions Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua snippet configures the `pre_open` hook, which executes code immediately before files are opened in the host Neovim instance. The example shows how to close a focused toggleterm instance before opening new files, demonstrating pre-processing actions. ```lua require("flatten").setup({ hooks = { pre_open = function(opts) -- opts.data contains custom data from guest_data hook print("About to open files from guest") -- Example: close toggleterm before opening local term = require("toggleterm.terminal") local termid = term.get_focused_id() if termid then term.get(termid):close() end end, }, }) ``` -------------------------------- ### should_block Hook for Guest Blocking Logic Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua snippet configures the `should_block` hook, which determines if the guest Neovim instance should wait for the host to close a file. It provides an example of blocking when a specific command-line flag (`-b`) is used, demonstrating conditional blocking logic. ```lua require("flatten").setup({ hooks = { should_block = function(argv) -- Block if -b flag is passed: nvim -b file.txt return vim.tbl_contains(argv, "-b") -- Alternative: block for diff mode -- return vim.tbl_contains(argv, "-d") end, }, }) -- Usage from terminal: -- nvim -b file.txt # Guest blocks until file is closed -- nvim file.txt # Guest exits immediately ``` -------------------------------- ### Initialize Flatten API Source: https://context7.com/willothy/flatten.nvim/llms.txt Demonstrates how to initialize the plugin with default settings or custom configuration options. ```lua local flatten = require("flatten") flatten.setup() flatten.setup({ window = { open = "smart" }, integrations = { wezterm = true }, }) ``` -------------------------------- ### Open files with flatten.nvim Source: https://github.com/willothy/flatten.nvim/blob/main/README.md These bash snippets demonstrate various ways to open files using Neovim with flatten.nvim. They cover opening multiple files, forcing blocking behavior with a custom handler, opening files in diff mode, and enabling manpage formatting. ```bash nvim file1 file2 ``` ```bash # with a custom block handler, you can use `nvim -b file1 file2` nvim --cmd 'let g:flatten_wait=1' file1 ``` ```bash nvim -d file1 file2 ``` ```bash # with a custom block handler, you can use `export VISUAL="nvim -b"` export VISUAL="nvim --cmd 'let g:flatten_wait=1'" # allows edit-exec ``` ```bash export MANPAGER="nvim +Man!" ``` ```bash nvim --cmd ``` ```bash nvim + ``` -------------------------------- ### Configure Window Open Modes Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua configuration snippet details the various modes for opening files in the host Neovim instance. Options range from opening in the current window to using splits, tabs, or a smart mode that automatically selects the best option. It also shows how to use a custom function handler. ```lua require("flatten").setup({ window = { -- "current": Opens file in current window open = "current", -- "alternate": Opens file in alternate window (C-w p) -- open = "alternate", -- "split": Opens file in horizontal split -- open = "split", -- "vsplit": Opens file in vertical split -- open = "vsplit", -- "tab": Opens file in new tab -- open = "tab", -- "smart": Auto-chooses between alternate, other windows, or new split -- open = "smart", -- Custom function handler -- open = function(opts) -- local winnr = vim.api.nvim_get_current_win() -- vim.api.nvim_set_current_buf(opts.files[1].bufnr) -- return winnr, opts.files[1].bufnr -- end, }, }) ``` -------------------------------- ### Configure File Opening Behavior in Window Source: https://github.com/willothy/flatten.nvim/blob/main/README.md Defines how files are opened by flatten.nvim. Options include opening in the current window, alternate window, splits, or tabs. A custom handler can also be provided for more complex scenarios. ```lua window = { open = "alternate", -- "current" | "alternate" | "split" | "vsplit" | "tab" | "smart" | Flatten.OpenHandler diff = "tab_vsplit", -- "split" | "vsplit" | "tab_split" | "tab_vsplit" | Flatten.OpenHandler focus = "first" -- "first" | "last" } ``` -------------------------------- ### Configure post_open hook in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Executes logic after a file is opened in the host instance. Useful for managing window focus or cleaning up specific file types like git commits. ```lua require("flatten").setup({ hooks = { post_open = function(opts) if opts.is_blocking then print("Guest is waiting for you to close: " .. opts.filetype) else vim.api.nvim_set_current_win(opts.winnr) end if opts.filetype == "gitcommit" then vim.api.nvim_create_autocmd("BufWritePost", { buffer = opts.bufnr, once = true, callback = vim.schedule_wrap(function() vim.api.nvim_buf_delete(opts.bufnr, {}) end), }) end end, }, }) ``` -------------------------------- ### Configure Kitty Integration Source: https://context7.com/willothy/flatten.nvim/llms.txt Enables flattening across Kitty tabs and windows by setting the kitty integration flag to true. ```lua require("flatten").setup({ integrations = { kitty = true, }, }) ``` -------------------------------- ### Configure Wezterm Integration Source: https://context7.com/willothy/flatten.nvim/llms.txt Enables flattening across Wezterm tabs and panes. Uses the post_open hook to switch to the host Neovim pane via the wezterm.nvim plugin. ```lua require("flatten").setup({ integrations = { wezterm = true, }, hooks = { post_open = function(opts) if not opts.is_blocking then require("wezterm").switch_pane.id( tonumber(os.getenv("WEZTERM_PANE")) ) end end, }, }) ``` -------------------------------- ### Configure Hooks for Customizing Flatten.nvim Behavior Source: https://github.com/willothy/flatten.nvim/blob/main/README.md Allows customization of flatten.nvim's behavior through various hooks. These hooks can control blocking, nesting, pre/post file opening actions, and handling of no-file scenarios. ```lua hooks = { should_block = function(argv) -- return true if the guest should wait for the host to close the file return false end, should_nest = function(host) -- return true if the guest should not be flattened into the same Neovim instance as the host return false end, pre_open = function(opts) -- Called before opening files end, post_open = function(opts) -- Called after opening files -- opts.bufnr, opts.winnr, opts.filetype, opts.is_blocking, opts.is_diff, opts.data end, block_end = function(opts) -- Called when the host closes the file -- opts.filetype, opts.data end, no_files = function(opts) -- Called when no files are passed to a guest instance -- opts.argv -- return true | { nest: boolean, block: boolean } return false end, guest_data = function() -- Called when the guest sends data to the host -- return custom data end, pipe_path = function() -- Called to determine whether an instance is a host or guest -- return path end } ``` -------------------------------- ### Implement custom OpenHandler in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Overrides the default window opening behavior to allow custom layouts, such as opening files in floating windows. ```lua require("flatten").setup({ window = { open = function(opts) local buf = opts.stdin_buf and opts.stdin_buf.bufnr or opts.files[1].bufnr local win = vim.api.nvim_open_win(buf, true, { relative = "editor", width = 80, height = 20, style = "minimal", border = "rounded" }) return win, buf end, }, }) ``` -------------------------------- ### Configure flatten.nvim defaults Source: https://github.com/willothy/flatten.nvim/blob/main/README.md This Lua code block displays the default configuration for flatten.nvim. It includes settings for hooks, blocking behavior for specific filetypes like gitcommit and gitrebase, command passthrough, nesting behavior, window opening preferences, and integrations with Kitty and Wezterm. ```lua local flatten = require("flatten") local config = { hooks = { should_block = flatten.hooks.should_block, should_nest = flatten.hooks.should_nest, pre_open = flatten.hooks.pre_open, post_open = flatten.hooks.post_open, block_end = flatten.hooks.block_end, no_files = flatten.hooks.no_files, guest_data = flatten.hooks.guest_data, pipe_path = flatten.hooks.pipe_path, }, block_for = { gitcommit = true, gitrebase = true, }, disable_cmd_passthrough = false, nest_if_no_args = false, nest_if_cmds = false, window = { open = "current", diff = "tab_vsplit", focus = "first", }, integrations = { kitty = false, wezterm = false, }, } ``` -------------------------------- ### Configure no_files hook in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Determines whether the plugin should nest or block when no files are provided to the guest instance. ```lua require("flatten").setup({ hooks = { no_files = function(opts) return false end, }, }) ``` -------------------------------- ### Configure guest_data hook in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Allows passing custom metadata from the guest instance to the host instance, such as terminal IDs or environment variables. ```lua require("flatten").setup({ hooks = { guest_data = function() return { terminal_id = vim.env.WEZTERM_PANE, timestamp = os.time() } end, post_open = function(opts) if opts.data and opts.data.terminal_id then print("Opened from Wezterm pane: " .. opts.data.terminal_id) end end, }, }) ``` -------------------------------- ### Configure pipe_path hook in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Defines custom pipe paths for terminal emulator integration, enabling seamless communication between host and guest instances. ```lua require("flatten").setup({ hooks = { pipe_path = function() if vim.env.NVIM then return vim.env.NVIM end if vim.env.KITTY_PID then local addr = string.format("%s/kitty.nvim-%s", vim.fn.stdpath("run"), vim.env.KITTY_PID) if not vim.uv.fs_stat(addr) then vim.fn.serverstart(addr) end return addr end return nil end, }, }) ``` -------------------------------- ### Custom Pipe Path Function for Terminal Emulators Source: https://github.com/willothy/flatten.nvim/blob/main/README.md This Lua function demonstrates how to implement a custom `pipe_path` for flatten.nvim, enabling integration with different terminal emulators or multiplexers. It includes specific logic for Neovim's internal environment and Kitty terminals. ```lua local pipe_path = function() -- If running in a terminal inside Neovim: if vim.env.NVIM then return vim.env.NVIM end -- If running in a Kitty terminal, -- all tabs/windows/os-windows in the same instance of kitty -- will open in the first neovim instance if vim.env.KITTY_PID then local addr = ("%s/%s"):format( vim.fn.stdpath("run"), "kitty.nvim-" .. vim.env.KITTY_PID ) if not vim.uv.fs_stat(addr) then vim.fn.serverstart(addr) end return addr end end ``` -------------------------------- ### Configure flatten.nvim with Toggleterm Source: https://github.com/willothy/flatten.nvim/blob/main/README.md This Lua configuration sets up flatten.nvim to work with toggleterm, allowing terminal buffers to open in the 'alternate' window. It includes hooks for managing blocking files like git commits and integrating with wezterm.nvim. ```lua local flatten = { "willothy/flatten.nvim", opts = function() --@type Terminal? local saved_terminal return { window = { open = "alternate", }, hooks = { should_block = function(argv) -- Note that argv contains all the parts of the CLI command, including -- Neovim's path, commands, options and files. -- See: :help v:argv -- In this case, we would block if we find the `-b` flag -- This allows you to use `nvim -b file1` instead of -- `nvim --cmd 'let g:flatten_wait=1' file1` return vim.tbl_contains(argv, "-b") -- Alternatively, we can block if we find the diff-mode option -- return vim.tbl_contains(argv, "-d") end, pre_open = function() local term = require("toggleterm.terminal") local termid = term.get_focused_id() saved_terminal = term.get(termid) end, post_open = function(bufnr, winnr, ft, is_blocking) if is_blocking and saved_terminal then -- Hide the terminal while it's blocking saved_terminal:close() else -- If it's a normal file, just switch to its window vim.api.nvim_set_current_win(winnr) -- If we're in a different wezterm pane/tab, switch to the current one -- Requires willothy/wezterm.nvim require("wezterm").switch_pane.id( tonumber(os.getenv("WEZTERM_PANE")) ) end -- If the file is a git commit, create one-shot autocmd to delete its buffer on write -- If you just want the toggleable terminal integration, ignore this bit if ft == "gitcommit" or ft == "gitrebase" then vim.api.nvim_create_autocmd("BufWritePost", { buffer = bufnr, once = true, callback = vim.schedule_wrap(function() vim.api.nvim_buf_delete(bufnr, {}) end), }) end end, block_end = function() -- After blocking ends (for a git commit, etc), reopen the terminal vim.schedule(function() if saved_terminal then saved_terminal:open() saved_terminal = nil end end) end, }, } end, } ``` -------------------------------- ### Configure Filetypes to Block For Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua configuration snippet allows you to define specific filetypes for which the guest Neovim instance should block, waiting for the host to close the file. This is useful for operations like committing or rebasing. ```lua require("flatten").setup({ block_for = { gitcommit = true, -- Block for git commit messages gitrebase = true, -- Block for git rebase -- Add custom filetypes: -- svn = true, -- hgcommit = true, }, }) ``` -------------------------------- ### Implement custom pipe_path for terminal multiplexers Source: https://github.com/willothy/flatten.nvim/blob/main/doc/flatten.nvim.txt A utility function to determine the pipe path for Neovim instances, allowing integration with specific terminal emulators like Kitty by checking environment variables. ```lua local pipe_path = function() if vim.env.NVIM then return vim.env.NVIM end if vim.env.KITTY_PID then local addr = ("%s/%s"):format( vim.fn.stdpath("run"), "kitty.nvim-" .. vim.env.KITTY_PID ) if not vim.uv.fs_stat(addr) then vim.fn.serverstart(addr) end return addr end end ``` -------------------------------- ### Configure Diff Mode Behavior Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua configuration snippet specifies how diff mode (`nvim -d file1 file2`) should open files. It allows choosing between different split and tab configurations for displaying the diff. ```lua require("flatten").setup({ window = { -- Diff mode options: "split", "vsplit", "tab_split", "tab_vsplit" diff = "tab_vsplit", -- Opens diffs in new tab with vertical splits }, }) ``` -------------------------------- ### Check Guest Status Source: https://context7.com/willothy/flatten.nvim/llms.txt Uses the is_guest() function to determine if the current Neovim instance is a guest or the host. ```lua local flatten = require("flatten") local status = flatten.is_guest() if flatten.is_guest() == false then print("This is the host Neovim instance") end ``` -------------------------------- ### Define Custom OpenHandler Function Source: https://github.com/willothy/flatten.nvim/blob/main/doc/flatten.nvim.txt A custom function signature for handling file opening logic. It returns the window number and optionally a buffer number to be used by the host instance. ```lua ---@type fun(opts: Flatten.OpenContext): number, number? local my_open_handler = function(opts) -- Custom logic to determine window and buffer return 0, nil end ``` -------------------------------- ### Configure flatten.nvim with toggleterm integration Source: https://github.com/willothy/flatten.nvim/blob/main/doc/flatten.nvim.txt This configuration sets up flatten.nvim to work with toggleterm. It includes hooks to block terminal windows during blocking operations and automatically closes git commit buffers upon writing. ```lua local flatten = { "willothy/flatten.nvim", opts = function() ---@type Terminal? local saved_terminal return { window = { open = "alternate", }, hooks = { should_block = function(argv) return vim.tbl_contains(argv, "-b") end, pre_open = function() local term = require("toggleterm.terminal") local termid = term.get_focused_id() saved_terminal = term.get(termid) end, post_open = function(bufnr, winnr, ft, is_blocking) if is_blocking and saved_terminal then saved_terminal:close() else vim.api.nvim_set_current_win(winnr) require("wezterm").switch_pane.id( tonumber(os.getenv("WEZTERM_PANE")) ) end if ft == "gitcommit" or ft == "gitrebase" then vim.api.nvim_create_autocmd("BufWritePost", { buffer = bufnr, once = true, callback = vim.schedule_wrap(function() vim.api.nvim_buf_delete(bufnr, {}) end), }) end end, block_end = function() vim.schedule(function() if saved_terminal then saved_terminal:open() saved_terminal = nil end end) end, }, } end, } ``` -------------------------------- ### Configure block_end hook in flatten.nvim Source: https://context7.com/willothy/flatten.nvim/llms.txt Executes code once a blocking file operation is completed. Commonly used to restore terminal states or UI elements. ```lua require("flatten").setup({ hooks = { block_end = function(opts) print("Finished editing: " .. opts.filetype) vim.schedule(function() require("toggleterm").toggle() end) end, }, }) ``` -------------------------------- ### should_nest Hook for Nesting Control Source: https://context7.com/willothy/flatten.nvim/llms.txt This Lua snippet configures the `should_nest` hook, which controls when a nested Neovim session is allowed instead of flattening. It includes logic to always flatten when inside a Neovim terminal and to nest if the working directories of the guest and host differ. ```lua require("flatten").setup({ hooks = { should_nest = function(host) -- Always flatten when in Neovim terminal if vim.env.NVIM ~= nil then return false end -- Get host's working directory via RPC local ok, host_cwd = pcall( require("flatten.rpc").exec_on_host, host, function() return vim.fn.getcwd(-1) end, {}, true ) -- Nest if working directories differ if ok then return not vim.startswith(vim.fn.getcwd(-1), host_cwd) end return false end, }, }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.