Skip to content

Commit 36a380a

Browse files
feat: custom keymaps (#64)
1 parent 98c8774 commit 36a380a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ Enable `show_builtin_git_pickers` to additionally show builtin git pickers.
146146
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
147147
show_builtin_git_pickers = false,
148148
entry_default_author_or_date = "author", -- one of "author" or "date"
149+
keymaps = {
150+
-- following keymaps can be overridden
151+
toggle_date_author = "<C-w>",
152+
open_commit_in_browser = "<C-o>",
153+
copy_commit_hash = "<C-y>",
154+
}
149155

150156
-- Telescope layout setup
151157
telescope_theme = {

lua/advanced_git_search/telescope/mappings/init.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local global_actions = require("advanced_git_search.actions")
55

66
local file_utils = require("advanced_git_search.utils.file")
77
local git_utils = require("advanced_git_search.utils.git")
8+
local config = require("advanced_git_search.utils.config")
89

910
-- Map a key to both insert and normal modes
1011
local function omnimap(map_func, key, handler)
@@ -24,7 +25,7 @@ local toggle_date_author = function(prompt_bufnr)
2425
end
2526

2627
M.toggle_entry_value = function(map)
27-
omnimap(map, "<C-w>", toggle_date_author)
28+
omnimap(map, config.get_keymap("toggle_date_author"), toggle_date_author)
2829
end
2930

3031
-------------------------------------------------------------------------------
@@ -38,7 +39,11 @@ end
3839

3940
--- Open browser at commmit (from entry) with <C-o>
4041
M.open_selected_commit_in_browser = function(map)
41-
omnimap(map, "<C-o>", open_commit_in_browser)
42+
omnimap(
43+
map,
44+
config.get_keymap("open_commit_in_browser"),
45+
open_commit_in_browser
46+
)
4247
end
4348

4449
-------------------------------------------------------------------------------
@@ -97,7 +102,7 @@ end
97102

98103
--- copy commit hash to clipboard with <C-y>
99104
M.copy_commit_hash_to_clipboard = function(map)
100-
omnimap(map, "<C-y>", copy_commit_hash)
105+
omnimap(map, config.get_keymap("copy_commit_hash"), copy_commit_hash)
101106
end
102107

103108
-------------------------------------------------------------------------------

lua/advanced_git_search/utils/config.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,48 @@ M.git_diff_flags = function()
3232
return git_diff_flags
3333
end
3434

35+
local function get_keymaps()
36+
local keymaps = {
37+
toggle_date_author = "<C-w>",
38+
open_commit_in_browser = "<C-o>",
39+
copy_commit_hash = "<C-y>",
40+
}
41+
keymaps = vim.tbl_extend("force", keymaps, config["keymaps"] or {})
42+
43+
if type(keymaps) ~= "table" then
44+
vim.notify(
45+
"keymaps config must be a table",
46+
vim.log.levels.ERROR,
47+
{ title = "Advanced Git Search" }
48+
)
49+
return nil
50+
end
51+
52+
return keymaps
53+
end
54+
55+
M.get_keymap = function(entry)
56+
if get_keymaps()[entry] == nil then
57+
vim.notify(
58+
"No keymap defined for " .. entry,
59+
vim.log.levels.ERROR,
60+
{ title = "Advanced Git Search" }
61+
)
62+
return ""
63+
end
64+
65+
if type(get_keymaps()[entry]) ~= "string" then
66+
vim.notify(
67+
"Keymap for " .. entry .. " must be a string",
68+
vim.log.levels.ERROR,
69+
{ title = "Advanced Git Search" }
70+
)
71+
return ""
72+
end
73+
74+
return get_keymaps()[entry]
75+
end
76+
3577
M.entry_default_author_or_date = function()
3678
return config["entry_default_author_or_date"] or "author"
3779
end

0 commit comments

Comments
 (0)