Skip to content

Commit 5169ac3

Browse files
feat: allow configuring keymap descriptions (#789)
* Fix test for Mac OS X, which automatic symlinks /var/ to /private/var/ * add test for when keymap is set as string * confirm existing support for tables in keymapping configs with new unit test * allow user config of the desc field * update readme showing how to change the value of 'desc' * linting fixes -- commas where required * linting fixes -- spaces and indentation * linting fixes -- more spacing and indentation * linting fixes -- more indentation * linting fixes -- remove trailing whitespace * Update lua/orgmode/config/mappings/map_entry.lua Co-authored-by: Kristijan Husak <[email protected]> --------- Co-authored-by: Kristijan Husak <[email protected]>
1 parent e365b85 commit 5169ac3

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

DOCS.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,42 @@ require('orgmode').setup({
847847
org_agenda = false,
848848
org_capture = 'gC'
849849
},
850-
agenda = {
851-
org_agenda_later = false
850+
}
851+
})
852+
```
853+
854+
To change a key mapping's `lhs` but not its `desc`, provide a string or a table:
855+
856+
```lua
857+
require('orgmode').setup({
858+
org_agenda_files = {'~/Dropbox/org/*', '~/my-orgs/**/*'},
859+
org_default_notes_file = '~/Dropbox/org/refile.org',
860+
mappings = {
861+
global = {
862+
-- providing a string
863+
org_agenda = '<D-a>',
864+
-- providing a table
865+
org_capture = { '<D-c>' }
866+
},
867+
}
868+
})
869+
870+
To change a key mapping's `lhs` and its `desc`, provide a table:
871+
872+
```lua
873+
require('orgmode').setup({
874+
org_agenda_files = {'~/Dropbox/org/*', '~/my-orgs/**/*'},
875+
org_default_notes_file = '~/Dropbox/org/refile.org',
876+
mappings = {
877+
global = {
878+
org_capture = { '<D-c>', desc = 'Open Capture Prompt' }
852879
}
853880
}
854881
})
855882
```
856883

884+
(The `desc` value is displayed in tools like WhichKey.)
885+
857886
You can find the configuration file that holds all default mappings [here](./lua/orgmode/config/mappings/init.lua)
858887

859888
**NOTE**: All mappings are normal mode mappings (`nnoremap`) with exception of `org_return`

lua/orgmode/config/mappings/map_entry.lua

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ function MapEntry:attach(default_mapping, user_mapping, opts)
106106
map_opts.prefix = nil
107107
end
108108

109+
if type(user_mapping) == 'table' and user_mapping.desc then
110+
map_opts.desc = user_mapping.desc
111+
end
112+
109113
for _, map in ipairs(mapping) do
110114
if prefix ~= '' then
111115
map = map:gsub('<prefix>', prefix)

tests/plenary/config/config_spec.lua

+76-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
local orgmode = require('orgmode')
2+
local config = require('orgmode.config')
3+
4+
local get_normal_mode_mapping_in_org_buffer = function(lhs)
5+
local current_buffer = vim.api.nvim_buf_get_name(0)
6+
7+
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
8+
vim.cmd('edit ' .. refile_file)
9+
10+
local normal_mode_mappings_in_org_buffer = vim.api.nvim_buf_get_keymap(0, 'n')
11+
12+
for _, keymap in ipairs(normal_mode_mappings_in_org_buffer) do
13+
if keymap then
14+
if keymap['lhs'] then
15+
if keymap['lhs'] == lhs then
16+
vim.cmd('edit ' .. current_buffer)
17+
return keymap
18+
end
19+
end
20+
end
21+
end
22+
23+
vim.cmd('edit ' .. current_buffer)
24+
return nil
25+
end
226

327
describe('Config', function()
428
local refile_file = vim.fn.getcwd() .. '/tests/plenary/fixtures/refile.org'
@@ -9,7 +33,6 @@ describe('Config', function()
933
org_default_notes_file = refile_file,
1034
org_archive_location = vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/%s_archive::',
1135
})
12-
local config = require('orgmode.config')
1336
assert.are.same(
1437
config:parse_archive_location(refile_file),
1538
vim.fn.getcwd() .. '/tests/plenary/fixtures/archive/refile.org_archive'
@@ -22,10 +45,61 @@ describe('Config', function()
2245
org_default_notes_file = refile_file,
2346
org_archive_location = 'archives_relative/%s_archive::',
2447
})
25-
local config = require('orgmode.config')
2648
assert.are.same(
2749
config:parse_archive_location(refile_file),
2850
vim.fn.getcwd() .. '/tests/plenary/fixtures/archives_relative/refile.org_archive'
2951
)
3052
end)
53+
54+
---@diagnostic disable: need-check-nil
55+
it('should use the default key mapping when no override is provided', function()
56+
local org = orgmode.setup({})
57+
58+
local mapping = get_normal_mode_mapping_in_org_buffer('g{')
59+
assert.are.same('<Cmd>lua require("orgmode").action("org_mappings.outline_up_heading")<CR>', mapping['rhs'])
60+
assert.are.same('org goto parent headline', mapping['desc'])
61+
end)
62+
63+
it('should use the provided key mapping when the override is provided as a string', function()
64+
local org = orgmode.setup({
65+
mappings = {
66+
org = {
67+
outline_up_heading = { 'gouh' },
68+
},
69+
},
70+
})
71+
72+
local mapping = get_normal_mode_mapping_in_org_buffer('gouh')
73+
assert.are.same('<Cmd>lua require("orgmode").action("org_mappings.outline_up_heading")<CR>', mapping['rhs'])
74+
assert.are.same('org goto parent headline', mapping['desc'])
75+
end)
76+
77+
it('should use the provided key mapping when the override is provided as a table', function()
78+
local org = orgmode.setup({
79+
mappings = {
80+
org = {
81+
outline_up_heading = { 'gouh' },
82+
},
83+
},
84+
})
85+
86+
local mapping = get_normal_mode_mapping_in_org_buffer('gouh')
87+
assert.are.same('<Cmd>lua require("orgmode").action("org_mappings.outline_up_heading")<CR>', mapping['rhs'])
88+
assert.are.same('org goto parent headline', mapping['desc'])
89+
end)
90+
91+
it('should use the provided key mapping when the override is provided as a table including a new desc', function()
92+
local org = orgmode.setup({
93+
mappings = {
94+
org = {
95+
outline_up_heading = { 'gouh', desc = 'Go To Parent Headline' },
96+
},
97+
},
98+
})
99+
100+
local mapping = get_normal_mode_mapping_in_org_buffer('gouh')
101+
assert.are.same('<Cmd>lua require("orgmode").action("org_mappings.outline_up_heading")<CR>', mapping['rhs'])
102+
assert.are.same('Go To Parent Headline', mapping['desc'])
103+
end)
104+
---@diagnostic enable: need-check-nil
31105
end)

tests/plenary/init_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Init', function()
3737
end)
3838

3939
it('should append files to paths', function()
40-
local fname = vim.fn.tempname() .. '.org'
40+
local fname = vim.fn.resolve(vim.fn.tempname() .. '.org')
4141
vim.fn.writefile({ '* Appended' }, fname)
4242

4343
assert.is.Nil(org.files.files[fname])

0 commit comments

Comments
 (0)