Skip to content

Commit

Permalink
feat: add string array support to open_mapping setting. (#557)
Browse files Browse the repository at this point in the history
* Add an array type for open_mapping.

* Fix typos and minor fixes.

* Add keymap function to utils.
  • Loading branch information
sbkkalex authored Apr 22, 2024
1 parent 17b4f1e commit 5ec59c3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ I'm also going to be pretty conservative about what I add.

This plugin must be explicitly enabled by using `require("toggleterm").setup{}`

Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for _normal_ mode.
Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for _normal_ mode. The `open_mapping` can be a key string or an array of key strings.
If you prefix the mapping with a number that particular terminal will be opened. Otherwise if a prefix is not set, then the last toggled terminal will be opened. In case there are multiple terminals opened they'll all be closed, and on the next mapping key they'll be restored.

If you set the `insert_mappings` key to `true`, the mapping will also take effect in insert mode; similarly setting `terminal_mappings` to `true` will have the mappings take effect in the opened terminal.
Expand Down Expand Up @@ -161,7 +161,7 @@ require("toggleterm").setup{
return vim.o.columns * 0.4
end
end,
open_mapping = [[<c-\>]],
open_mapping = [[<c-\>]], -- or { [[<c-\>]], [[<c-¥>]] } if you also use a Japanese keyboard.
on_create = fun(t: Terminal), -- function to run when the terminal is first created
on_open = fun(t: Terminal), -- function to run when the terminal opens
on_close = fun(t: Terminal), -- function to run when the terminal closes
Expand Down
12 changes: 6 additions & 6 deletions doc/toggleterm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ SETUP ~
This plugin must be explicitly enabled by using `require("toggleterm").setup{}`

Setting the `open_mapping` key to use for toggling the terminal(s) will set up
mappings for _normal_ mode. If you prefix the mapping with a number that
particular terminal will be opened. Otherwise if a prefix is not set, then the
last toggled terminal will be opened. In case there are multiple terminals
opened they’ll all be closed, and on the next mapping key they’ll be
restored.
mappings for _normal_ mode. The `open_mapping` can be a key string or an array
of key strings. If you prefix the mapping with a number that particular
terminal will be opened. Otherwise if a prefix is not set, then the last
toggled terminal will be opened. In case there are multiple terminals opened
they’ll all be closed, and on the next mapping key they’ll be restored.

If you set the `insert_mappings` key to `true`, the mapping will also take
effect in insert mode; similarly setting `terminal_mappings` to `true` will
Expand Down Expand Up @@ -148,7 +148,7 @@ what options are available. It is not written to be used as is.
return vim.o.columns * 0.4
end
end,
open_mapping = [[<c-\>]],
open_mapping = [[<c-\>]], -- or { [[<c-\>]], [[<c-¥>]] } if you also use a Japanese keyboard.
on_create = fun(t: Terminal), -- function to run when the terminal is first created
on_open = fun(t: Terminal), -- function to run when the terminal opens
on_close = fun(t: Terminal), -- function to run when the terminal closes
Expand Down
4 changes: 2 additions & 2 deletions lua/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ local function setup_global_mappings()
local mapping = config.open_mapping
-- v:count defaults the count to 0 but if a count is passed in uses that instead
if mapping then
vim.keymap.set("n", mapping, '<Cmd>execute v:count . "ToggleTerm"<CR>', {
utils.key_map("n", mapping, '<Cmd>execute v:count . "ToggleTerm"<CR>', {
desc = "Toggle Terminal",
silent = true,
})
if config.insert_mappings then
vim.keymap.set("i", mapping, "<Esc><Cmd>ToggleTerm<CR>", {
utils.key_map("i", mapping, "<Esc><Cmd>ToggleTerm<CR>", {
desc = "Toggle Terminal",
silent = true,
})
Expand Down
2 changes: 1 addition & 1 deletion lua/toggleterm/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function shade(color, factor) return colors.shade_color(color, factor) end
--- @field size number
--- @field shade_filetypes string[]
--- @field hide_numbers boolean
--- @field open_mapping string
--- @field open_mapping string | string[]
--- @field shade_terminals boolean
--- @field insert_mappings boolean
--- @field terminal_mappings boolean
Expand Down
2 changes: 1 addition & 1 deletion lua/toggleterm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ end
local function setup_buffer_mappings(bufnr)
local mapping = config.open_mapping
if mapping and config.terminal_mappings then
vim.keymap.set("t", mapping, "<Cmd>ToggleTerm<CR>", { buffer = bufnr, silent = true })
utils.key_map("t", mapping, "<Cmd>ToggleTerm<CR>", { buffer = bufnr, silent = true })
end
end

Expand Down
15 changes: 15 additions & 0 deletions lua/toggleterm/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ end
---@param sep string
function M.concat_without_empty(tbl, sep) return table.concat(M.tbl_filter_empty(tbl), sep) end

-- Key mapping function
---@param mod string | string[]
---@param lhs string | string[]
---@param rhs string | function
---@param opts table?
function M.key_map(mod, lhs, rhs, opts)
if type(lhs) == "string" then
vim.keymap.set(mod, lhs, rhs, opts)
elseif type(lhs) == "table" then
for _, key in pairs(lhs) do
vim.keymap.set(mod, key, rhs, opts)
end
end
end

---@param mode "visual" | "motion"
---@return table
function M.get_line_selection(mode)
Expand Down

0 comments on commit 5ec59c3

Please sign in to comment.