Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ You can send commands to a terminal without opening its window by using the `ope

see `:h expand()` for more details

### TermNew

This command allows you to open a new terminal at the next available count.
It's helpful in combination with `TermSelect` (see below) to work with
terminals without needing to remember numbers. The `size`, `dir`, `direction`
and `name` arguments work the same as in `ToggleTerm`.

### TermSelect

This command uses `vim.ui.select` to allow a user to select a terminal to open
Expand Down
6 changes: 6 additions & 0 deletions doc/toggleterm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ You can send commands to a terminal without opening its window by using the

see |expand()| for more details

TERMNEW ~

This command allows you to open a new terminal at the next available count.
It's helpful in combination with `TermSelect` (see below) to work with
terminals without needing to remember numbers. The `size`, `dir`, `direction`
and `name` arguments work the same as in `ToggleTerm`.

TERMSELECT ~

Expand Down
27 changes: 27 additions & 0 deletions lua/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ function M.send_lines_to_terminal(selection_type, trim_spaces, cmd_data)
api.nvim_win_set_cursor(current_window, { start_line, start_col - 1 })
end

function M.new_command(args)
local parsed = commandline.parse(args)
vim.validate({
size = { parsed.size, "number", true },
dir = { parsed.dir, "string", true },
direction = { parsed.direction, "string", true },
name = { parsed.name, "string", true },
})
if parsed.size then parsed.size = tonumber(parsed.size) end
M.new(parsed.size, parsed.dir, parsed.direction, parsed.name)
end

function M.toggle_command(args, count)
local parsed = commandline.parse(args)
vim.validate({
Expand All @@ -278,6 +290,15 @@ function _G.___toggleterm_winbar_click(id)
end
end

--- Creates new terminal at the first available id
--- @param size number?
--- @param dir string?
--- @param direction string?
--- @param name string?
function M.new(size, dir, direction, name)
toggle_nth_term(terms.next_id(), size, dir, direction, name)
end

--- If a count is provided we operate on the specific terminal buffer
--- i.e. 2ToggleTerm => open or close Term 2
--- if the count is 1 we use a heuristic which is as follows
Expand Down Expand Up @@ -420,6 +441,12 @@ local function setup_commands()
{ count = true, complete = commandline.term_exec_complete, nargs = "*" }
)

command(
"TermNew",
function(opts) M.new_command(opts.args) end,
{ count = true, complete = commandline.toggle_term_complete, nargs = "*" }
)

command(
"ToggleTerm",
function(opts) M.toggle_command(opts.args, opts.count) end,
Expand Down
8 changes: 4 additions & 4 deletions lua/toggleterm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ local Terminal = {}
--- hasn't already been allocated e.g. in a list of {1,2,5,6} the next id should
--- be 3 then 4 then 7
---@return integer
local function next_id()
function M.next_id()
local all = M.get_all(true)
for index, term in pairs(all) do
if index ~= term.id then return index end
Expand Down Expand Up @@ -205,7 +205,7 @@ function Terminal:new(term)
self.__index = self
term.newline_chr = term.newline_chr or get_newline_chr()
term.direction = term.direction or conf.direction
term.id = id or next_id()
term.id = id or M.next_id()
term.display_name = term.display_name
term.float_opts = vim.tbl_deep_extend("keep", term.float_opts or {}, conf.float_opts)
term.clear_env = vim.F.if_nil(term.clear_env, conf.clear_env)
Expand All @@ -228,7 +228,7 @@ end
---@package
---Add a terminal to the list of terminals
function Terminal:__add()
if terminals[self.id] and terminals[self.id] ~= self then self.id = next_id() end
if terminals[self.id] and terminals[self.id] ~= self then self.id = M.next_id() end
if not terminals[self.id] then terminals[self.id] = self end
return self
end
Expand Down Expand Up @@ -586,7 +586,7 @@ if _G.IS_TEST then
end
end

M.__next_id = next_id
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think this isn't necessary anymore. Since I wasn't exporting next_id before this was a way of trying to do it privately so I could use it in tests but now it's public this line can be removed and the corresponding usages of M.__next_id can just be M.next_id

M.__next_id = M.next_id
end

M.Terminal = Terminal
Expand Down