diff --git a/README.md b/README.md index aaaf6a8..b0a12c2 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,15 @@ Open oil browser in a floating window | ----- | ------------- | ------------------------------------------------------------------------------------------- | | dir | `nil\|string` | When nil, open the parent of the current buffer, or the cwd if current buffer is not a file | +### toggle_float(dir) + +`toggle_float(dir)` \ +Open oil browser in a floating window, or close it if open + +| Param | Type | Desc | +| ----- | ------------- | ------------------------------------------------------------------------------------------- | +| dir | `nil\|string` | When nil, open the parent of the current buffer, or the cwd if current buffer is not a file | + ### open(dir) `open(dir)` \ diff --git a/doc/oil.txt b/doc/oil.txt index 8a897c0..8f22abd 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -175,6 +175,13 @@ open_float({dir}) *oil.open_floa {dir} `nil|string` When nil, open the parent of the current buffer, or the cwd if current buffer is not a file +toggle_float({dir}) *oil.toggle_float* + Open oil browser in a floating window, or close it if open + + Parameters: + {dir} `nil|string` When nil, open the parent of the current buffer, or the + cwd if current buffer is not a file + open({dir}) *oil.open* Open oil browser for a directory diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 36f16ae..074e0af 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -337,6 +337,17 @@ M.open_float = function(dir) end end +---Open oil browser in a floating window, or close it if open +---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file +M.toggle_float = function(dir) + local util = require("oil.util") + if util.is_oil_bufnr(0) and util.is_floating_win(0) then + vim.api.nvim_win_close(0, true) + else + M.open_float(dir) + end +end + ---Open oil browser for a directory ---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file M.open = function(dir)