Skip to content

Commit d160a95

Browse files
fix(mappings): Map capture mappings to an instance
1 parent 63e4c5a commit d160a95

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

ftplugin/org.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ vim.b.did_ftplugin = true
77
local config = require('orgmode.config')
88
local utils = require('orgmode.utils')
99

10-
config:setup_mappings('org')
11-
config:setup_mappings('text_objects')
10+
vim.b.org_bufnr = vim.api.nvim_get_current_buf()
11+
12+
config:setup_mappings('org', vim.b.org_bufnr)
13+
config:setup_mappings('text_objects', vim.b.org_bufnr)
1214
config:setup_foldlevel()
1315

1416
if config.org_startup_indented then
1517
vim.b.org_indent_mode = true
1618
end
1719
require('orgmode.org.indent').setup_virtual_indent()
1820

19-
vim.b.org_bufnr = vim.api.nvim_get_current_buf()
2021
vim.bo.modeline = false
2122
vim.opt_local.fillchars:append('fold: ')
2223
vim.opt_local.foldmethod = 'expr'
@@ -62,5 +63,5 @@ vim.b.undo_ftplugin = table.concat({
6263
'foldexpr<',
6364
'formatexpr<',
6465
'omnifunc<',
65-
'| unlet! b:org_bufnr'
66+
'| unlet! b:org_bufnr',
6667
}, ' ')

lua/orgmode/agenda/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function Agenda:open_window()
8787
vim.cmd([[setf orgagenda]])
8888
vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]])
8989
vim.w.org_window_pos = vim.fn.win_screenpos(0)
90-
config:setup_mappings('agenda')
90+
config:setup_mappings('agenda', vim.api.nvim_get_current_buf())
9191
return vim.fn.bufnr()
9292
end
9393

lua/orgmode/capture/init.lua

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,41 @@ function Capture:prompt()
3333
self:_create_prompt(self.templates:get_list())
3434
end
3535

36+
---@private
37+
function Capture:setup_mappings()
38+
local maps = config:get_mappings('capture', vim.api.nvim_get_current_buf())
39+
if not maps then
40+
return
41+
end
42+
local capture_map = maps.org_capture_finalize
43+
capture_map.map_entry
44+
:with_handler(function()
45+
return self:refile()
46+
end)
47+
:attach(capture_map.default_map, capture_map.user_map, capture_map.opts)
48+
49+
local refile_map = maps.org_capture_refile
50+
refile_map.map_entry
51+
:with_handler(function()
52+
return self:refile_to_destination()
53+
end)
54+
:attach(refile_map.default_map, refile_map.user_map, refile_map.opts)
55+
56+
local kill_map = maps.org_capture_kill
57+
kill_map.map_entry
58+
:with_handler(function()
59+
return self:kill()
60+
end)
61+
:attach(kill_map.default_map, kill_map.user_map, kill_map.opts)
62+
end
63+
3664
---@param template OrgCaptureTemplate
3765
---@return OrgPromise<OrgCaptureWindow>
3866
function Capture:open_template(template)
3967
self._window = CaptureWindow:new({
4068
template = template,
4169
on_open = function()
42-
return config:setup_mappings('capture')
70+
return self:setup_mappings()
4371
end,
4472
on_close = function()
4573
return self:on_refile_close()
@@ -484,7 +512,7 @@ function Capture:_setup_closing_note()
484512
return content
485513
end,
486514
on_open = function()
487-
config:setup_mappings('note')
515+
config:setup_mappings('note', vim.api.nvim_get_current_buf())
488516
end,
489517
})
490518
end

lua/orgmode/config/init.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ function Config:setup_mappings(category, buffer)
205205
buffer = buffer or vim.api.nvim_get_current_buf(),
206206
})
207207
end
208+
local maps = self:get_mappings(category, buffer)
209+
if not maps then
210+
return
211+
end
212+
213+
for _, map in pairs(maps) do
214+
map.map_entry:attach(map.default_map, map.user_map, map.opts)
215+
end
216+
end
217+
218+
function Config:get_mappings(category, buffer)
208219
if self.opts.mappings.disable_all then
209220
return
210221
end
@@ -221,9 +232,16 @@ function Config:setup_mappings(category, buffer)
221232
opts.prefix = self.opts.mappings.prefix
222233
end
223234

235+
local result = {}
224236
for name, map_entry in pairs(map_entries) do
225-
map_entry:attach(default_mappings[name], user_mappings[name], opts)
237+
result[name] = {
238+
map_entry = map_entry,
239+
default_map = default_mappings[name],
240+
user_map = user_mappings[name],
241+
opts = opts,
242+
}
226243
end
244+
return result
227245
end
228246

229247
--- Setup the foldlevel for a given org file

lua/orgmode/config/mappings/map_entry.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---@class OrgMapEntry
2+
---@field provided_opts table
23
---@field handler string
34
---@field handler_cmd string
45
---@field args table[]
@@ -39,6 +40,12 @@ function MapEntry.custom(handler, opts)
3940
return MapEntry:new(handler, opts)
4041
end
4142

43+
function MapEntry:with_handler(handler)
44+
local map_entry = MapEntry:new(self.handler, self.provided_opts)
45+
map_entry.handler = handler
46+
return map_entry
47+
end
48+
4249
---@param handler string|function
4350
---@param opts? table<string, any>
4451
function MapEntry:new(handler, opts)
@@ -51,6 +58,7 @@ function MapEntry:new(handler, opts)
5158
type = { opts.type, 'string', true },
5259
})
5360
local data = {}
61+
data.provided_opts = opts
5462
data.handler = handler
5563
data.opts = vim.tbl_extend('keep', opts.opts or {}, {
5664
nowait = true,
@@ -85,7 +93,9 @@ function MapEntry:attach(default_mapping, user_mapping, opts)
8593
end
8694

8795
if type(mapping) ~= 'table' then
88-
error('Invalid mapping provided for ' .. self.handler .. '. Only string and array of strings can be provided')
96+
error(
97+
'Invalid mapping provided for ' .. tostring(self.handler) .. '. Only string and array of strings can be provided'
98+
)
8999
end
90100

91101
local map_opts = vim.tbl_extend('force', self.opts, opts or {})

0 commit comments

Comments
 (0)