Skip to content

Commit

Permalink
feat: provide keybinding prefix option (#46)
Browse files Browse the repository at this point in the history
Allows to conveniently rebind the prefix of most of the plugins
key mappings without having to change them all explicitly.
  • Loading branch information
seflue committed Jun 2, 2024
1 parent 841e099 commit cef0790
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 53 deletions.
22 changes: 20 additions & 2 deletions DOCS.org
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@

Configuration settings used to specify keybindings.

*** prefix

This sets an overall prefix, which can be reused in other keybindings with
=<prefix>=. Defaults to =<Leader>n=.

#+begin_src lua
require("org-roam").setup({
bindings = {
prefix = "<LocalLeader>n",
},
})
#+end_src

*** add alias

Adds an alias to the node under cursor.
Expand Down Expand Up @@ -795,6 +808,7 @@

| Name | Keybinding | Description |
|--------------------------+---------------+---------------------------------------------------------------------------|
| prefix | =<Leader>n= | Common prefix for org-roam bindings. |
| add_alias | =<Leader>naa= | Adds an alias to the node under cursor. |
| add_origin | =<Leader>noa= | Adds an origin to the node under cursor. |
| capture | =<Leader>nc= | Opens org-roam capture window. |
Expand Down Expand Up @@ -828,13 +842,17 @@

** Modifying bindings

Bindings can be changed during configuration by overwriting them within the =bindings= table:
Bindings can be changed during configuration by overwriting them within the
=bindings= table. We use a common [[*prefix]] in every mapping, which is by
default =<Leader>n=. It can be adjusted with ~bindings.prefix~ and reused in
other bindings with the alias ~<prefix>~:

#+begin_src lua
require("org-roam").setup({
-- ...
bindings = {
capture = "<LocalLeader>nc",
prefix = "<LocalLeader>n", -- replaces <Leader>n in every binding with <LocalLeader>n
capture = "<prefix>C" -- remaps from <Leader>nc to <LocalLeader>nC
},
})
#+end_src
Expand Down
52 changes: 27 additions & 25 deletions lua/org-roam/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,52 @@ local DEFAULT_CONFIG = {
---Bindings associated with org-roam functionality.
---@class org-roam.config.Bindings
bindings = {
---Adjusts the prefix for every keybinding. Can be used in keybindings with <prefix>.
prefix = "<Leader>n",
---Adds an alias to the node under cursor.
add_alias = "<Leader>naa",
add_alias = "<prefix>aa",

---Adds an origin to the node under cursor.
add_origin = "<Leader>noa",
add_origin = "<prefix>oa",

---Opens org-roam capture window.
capture = "<Leader>nc",
capture = "<prefix>c",

---Completes the node under cursor.
complete_at_point = "<Leader>n.",
complete_at_point = "<prefix>.",

---Finds node and moves to it.
find_node = "<Leader>nf",
find_node = "<prefix>f",

---Goes to the next node sequentially based on origin of the node under cursor.
---
---If more than one node has the node under cursor as its origin, a selection
---dialog is displayed to choose the node.
goto_next_node = "<Leader>nn",
goto_next_node = "<prefix>n",

---Goes to the previous node sequentially based on origin of the node under cursor.
goto_prev_node = "<Leader>np",
goto_prev_node = "<prefix>p",

---Inserts node at cursor position.
insert_node = "<Leader>ni",
insert_node = "<prefix>i",

---Inserts node at cursor position without opening capture buffer.
insert_node_immediate = "<Leader>nm",
insert_node_immediate = "<prefix>m",

---Opens the quickfix menu for backlinks to the current node under cursor.
quickfix_backlinks = "<Leader>nq",
quickfix_backlinks = "<prefix>q",

---Removes an alias from the node under cursor.
remove_alias = "<Leader>nar",
remove_alias = "<prefix>ar",

---Removes the origin from the node under cursor.
remove_origin = "<Leader>nor",
remove_origin = "<prefix>or",

---Toggles the org-roam node-view buffer for the node under cursor.
toggle_roam_buffer = "<Leader>nl",
toggle_roam_buffer = "<prefix>l",

---Toggles a fixed org-roam node-view buffer for a selected node.
toggle_roam_buffer_fixed = "<Leader>nb",
toggle_roam_buffer_fixed = "<prefix>b",
},

---Settings associated with org-roam capture logic.
Expand Down Expand Up @@ -108,37 +110,37 @@ local DEFAULT_CONFIG = {
---@class org-roam.config.extensions.dailies.Bindings
bindings = {
---Capture a specific date's note.
capture_date = "<Leader>ndD",
capture_date = "<prefix>dD",

---Capture today's note.
capture_today = "<Leader>ndN",
capture_today = "<prefix>dN",

---Capture tomorrow's note.
capture_tomorrow = "<Leader>ndT",
capture_tomorrow = "<prefix>dT",

---Capture yesterday's note.
capture_yesterday = "<Leader>ndY",
capture_yesterday = "<prefix>dY",

---Navigate to dailies note directory.
find_directory = "<Leader>nd.",
find_directory = "<prefix>d.",

---Navigate to specific date's note.
goto_date = "<Leader>ndd",
goto_date = "<prefix>dd",

---Navigate to the next note in date sequence.
goto_next_date = "<Leader>ndf",
goto_next_date = "<prefix>df",

---Navigate to the previous note in date sequence.
goto_prev_date = "<Leader>ndb",
goto_prev_date = "<prefix>db",

---Navigate to today's note.
goto_today = "<Leader>ndn",
goto_today = "<prefix>dn",

---Navigate to tomorrow's note.
goto_tomorrow = "<Leader>ndt",
goto_tomorrow = "<prefix>dt",

---Navigate to yesterday's note.
goto_yesterday = "<Leader>ndy",
goto_yesterday = "<prefix>dy",
},

---Settings tied to org-roam dailies capture templates.
Expand Down
64 changes: 38 additions & 26 deletions lua/org-roam/setup/keybindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ local notify = require("org-roam.core.ui.notify")
---@param lhs string|{lhs:string, modes:org-roam.config.NvimMode[]}|nil
---@param desc string
---@param cb fun()
local function assign(lhs, desc, cb)
---@param prefix string?
local function assign(lhs, desc, cb, prefix)
if type(cb) ~= "function" then
return
end
Expand All @@ -38,6 +39,10 @@ local function assign(lhs, desc, cb)
return
end

if prefix then
lhs = lhs:gsub("<prefix>", prefix)
end

for _, mode in ipairs(modes) do
vim.api.nvim_set_keymap(mode, lhs, "", {
desc = desc,
Expand Down Expand Up @@ -82,62 +87,65 @@ end
local function assign_core_keybindings(roam)
-- User can remove all bindings by setting this to nil
local bindings = roam.config.bindings or {}
local prefix = roam.config.bindings.prefix

assign(bindings.add_alias, "Adds an alias to the roam node under cursor", function()
roam.api.add_alias()
end)
end, prefix)

assign(bindings.remove_alias, "Removes an alias from the roam node under cursor", function()
roam.api.remove_alias()
end)
end, prefix)

assign(bindings.add_origin, "Adds an origin to the roam node under cursor", function()
roam.api.add_origin()
end)
end, prefix)

assign(bindings.remove_origin, "Removes the origin from the roam node under cursor", function()
roam.api.remove_origin()
end)
end, prefix)

assign(
bindings.goto_prev_node,
"Goes to the previous node sequentially based on origin of the node under cursor",
function()
roam.api.goto_prev_node()
end
end,
prefix
)

assign(
bindings.goto_next_node,
"Goes to the next node sequentially based on origin of the node under cursor",
function()
roam.api.goto_next_node()
end
end,
prefix
)

assign(bindings.quickfix_backlinks, "Open quickfix of backlinks for org-roam node under cursor", function()
roam.ui.open_quickfix_list({
backlinks = true,
show_preview = true,
})
end)
end, prefix)

assign(bindings.toggle_roam_buffer, "Toggles org-roam buffer for node under cursor", function()
roam.ui.toggle_node_buffer({
focus = roam.config.ui.node_buffer.focus_on_toggle,
})
end)
end, prefix)

assign(bindings.toggle_roam_buffer_fixed, "Toggles org-roam buffer for a specific node, not changing", function()
roam.ui.toggle_node_buffer({
fixed = true,
focus = roam.config.ui.node_buffer.focus_on_toggle,
})
end)
end, prefix)

assign(bindings.complete_at_point, "Completes link to a node based on expression under cursor", function()
roam.api.complete_node()
end)
end, prefix)

assign({ lhs = bindings.capture, modes = { "n", "v" } }, "Opens org-roam capture window", function()
local results = get_visual_selection(roam)
Expand All @@ -150,7 +158,7 @@ local function assign_core_keybindings(roam)
roam.api.capture_node({
title = title,
})
end)
end, prefix)

assign(
{ lhs = bindings.find_node, modes = { "n", "v" } },
Expand All @@ -166,7 +174,8 @@ local function assign_core_keybindings(roam)
roam.api.find_node({
title = title,
})
end
end,
prefix
)

assign(
Expand All @@ -185,7 +194,8 @@ local function assign_core_keybindings(roam)
title = title,
ranges = ranges,
})
end
end,
prefix
)

assign(
Expand All @@ -205,58 +215,60 @@ local function assign_core_keybindings(roam)
title = title,
ranges = ranges,
})
end
end,
prefix
)
end

---@param roam OrgRoam
local function assign_dailies_keybindings(roam)
-- User can remove all bindings by setting this to nil
local bindings = roam.config.extensions.dailies.bindings or {}
local prefix = roam.config.bindings.prefix

assign(bindings.capture_date, "Capture a specific date's note", function()
roam.ext.dailies.capture_date()
end)
end, prefix)

assign(bindings.capture_today, "Capture today's note", function()
roam.ext.dailies.capture_today()
end)
end, prefix)

assign(bindings.capture_tomorrow, "Capture tomorrow's note", function()
roam.ext.dailies.capture_tomorrow()
end)
end, prefix)

assign(bindings.capture_yesterday, "Capture yesterday's note", function()
roam.ext.dailies.capture_yesterday()
end)
end, prefix)

assign(bindings.find_directory, "Navigate to dailies note directory", function()
roam.ext.dailies.find_directory()
end)
end, prefix)

assign(bindings.goto_date, "Navigate to a specific date's note", function()
roam.ext.dailies.goto_date()
end)
end, prefix)

assign(bindings.goto_today, "Navigate to today's note", function()
roam.ext.dailies.goto_today()
end)
end, prefix)

assign(bindings.goto_tomorrow, "Navigate to tomorrow's note", function()
roam.ext.dailies.goto_tomorrow()
end)
end, prefix)

assign(bindings.goto_yesterday, "Navigate to yesterday's note", function()
roam.ext.dailies.goto_yesterday()
end)
end, prefix)

assign(bindings.goto_next_date, "Navigate to the next available note", function()
roam.ext.dailies.goto_next_date()
end)
end, prefix)

assign(bindings.goto_prev_date, "Navigate to the previous available note", function()
roam.ext.dailies.goto_prev_date()
end)
end, prefix)
end

---@param roam OrgRoam
Expand Down
Loading

0 comments on commit cef0790

Please sign in to comment.