From e80d19142bdb7b2856ef304f3a5ae05d22ba6532 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Fri, 19 Sep 2025 10:31:34 -0700 Subject: feat(api): add nvim_open_tabpage Problem: no API function for opening a new tab page and returning its handle, or to open without entering. Solution: add nvim_open_tabpage. --- runtime/lua/vim/_meta/api.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'runtime/lua/vim/_meta/api.lua') diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index 0510e2bef9..6be9305d48 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1673,6 +1673,17 @@ function vim.api.nvim_load_context(dict) end --- @return any function vim.api.nvim_notify(msg, log_level, opts) end +--- Opens a new tabpage +--- +--- @param buffer integer Buffer to open in the first window of the new tabpage. +--- Use 0 for current buffer. +--- @param config vim.api.keyset.tabpage_config Configuration for the new tabpage. Keys: +--- - enter: Whether to enter the new tabpage (default: true) +--- - after: Position to insert tabpage (default: 0). +--- 0 = after current, 1 = first, N = before Nth. +--- @return integer # Tabpage handle of the created tabpage +function vim.api.nvim_open_tabpage(buffer, config) end + --- Open a terminal instance in a buffer --- --- By default (and currently the only option) the terminal will not be -- cgit From cd4c98fded9490378bdb33966d14773242941905 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Sun, 15 Mar 2026 10:18:31 +0000 Subject: fix: nvim_open_tabpage cleanup, fixes, more tests - Cleanup, remove redundant comments, add more tests. - Enhance win_new_tabpage rather than create a new function for !enter, and use a different approach that minimizes side-effects. Return the tabpage_T * and first win_T * it allocated. - Disallow during textlock, like other APIs that open windows. - Remove existing win_alloc_firstwin error handling from win_new_tabpage; it's not needed, and looks incorrect. (enter_tabpage is called for curtab, which is not the old tabpage! Plus newtp is not freed) - Fix checks after creating the tabpage: - Don't fail if buf wasn't set successfully; the tab page may still be valid regardless. Set buffer like nvim_open_win, possibly blocking Enter/Leave events. (except BufWinEnter) - tp_curwin may not be the initial window opened by win_new_tabpage. Use the win_T * it returns instead, which is the real first window it allocated, regardless of autocmd shenanigans. - Properly check whether tab page was freed; it may have also been freed before win_set_buf. Plus, it may not be safe to read its handle! --- runtime/lua/vim/_meta/api.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/_meta/api.lua') diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index 6be9305d48..b49a547d63 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1673,7 +1673,7 @@ function vim.api.nvim_load_context(dict) end --- @return any function vim.api.nvim_notify(msg, log_level, opts) end ---- Opens a new tabpage +--- Opens a new tabpage. --- --- @param buffer integer Buffer to open in the first window of the new tabpage. --- Use 0 for current buffer. -- cgit From 0c1ed63c05fe0b2f6d7590e0368eccb9133082d6 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Sun, 15 Mar 2026 19:00:06 +0000 Subject: fix(api): nvim_open_tabpage "after" like :[count]tab Problem: "after" in nvim_open_tabpage is inconsistent with how a count works with :tab, :tabnew, etc. Plus, the name "after" implies it's inserted after that number. Solution: internally offset by 1. Allow negative numbers to mean after current. Hmm, should we even reserve sentinels for after current? Callers can probably just use nil... --- runtime/lua/vim/_meta/api.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/_meta/api.lua') diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index b49a547d63..b96f977c96 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1679,8 +1679,8 @@ function vim.api.nvim_notify(msg, log_level, opts) end --- Use 0 for current buffer. --- @param config vim.api.keyset.tabpage_config Configuration for the new tabpage. Keys: --- - enter: Whether to enter the new tabpage (default: true) ---- - after: Position to insert tabpage (default: 0). ---- 0 = after current, 1 = first, N = before Nth. +--- - after: Position to insert tabpage (default: -1; after current). +--- 0 = first, N = after Nth. --- @return integer # Tabpage handle of the created tabpage function vim.api.nvim_open_tabpage(buffer, config) end -- cgit From 97b064c9ac84bdaac65a4c4db81d11daa8f02719 Mon Sep 17 00:00:00 2001 From: Sean Dewar <6256228+seandewar@users.noreply.github.com> Date: Mon, 16 Mar 2026 13:15:33 +0000 Subject: fix(api): nvim_open_tabpage positional "enter" Problem: nvim_open_tabpage's "enter" argument is optional, which is inconsistent with nvim_open_win. Solution: make it a (non-optional) positional argument, like nvim_open_win. Also change "enter"'s description to be more like nvim_open_win's doc. --- runtime/lua/vim/_meta/api.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/_meta/api.lua') diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index b96f977c96..5c2e54f3ae 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1677,12 +1677,12 @@ function vim.api.nvim_notify(msg, log_level, opts) end --- --- @param buffer integer Buffer to open in the first window of the new tabpage. --- Use 0 for current buffer. +--- @param enter boolean Enter the tabpage (make it the current tabpage). --- @param config vim.api.keyset.tabpage_config Configuration for the new tabpage. Keys: ---- - enter: Whether to enter the new tabpage (default: true) --- - after: Position to insert tabpage (default: -1; after current). --- 0 = first, N = after Nth. --- @return integer # Tabpage handle of the created tabpage -function vim.api.nvim_open_tabpage(buffer, config) end +function vim.api.nvim_open_tabpage(buffer, enter, config) end --- Open a terminal instance in a buffer --- -- cgit