Skip to content

Commit 35f4fb6

Browse files
Add insert mode enter mapping
1 parent ce958be commit 35f4fb6

File tree

7 files changed

+106
-20
lines changed

7 files changed

+106
-20
lines changed

lua/orgmode/clock/report.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function ClockReport:draw_for_agenda(start_line)
4242
headline.logbook:get_total(self.from, self.to):to_string(),
4343
})
4444
end
45-
table.insert(data, {})
45+
table.insert(data, 'hr')
4646
end
4747

4848
local clock_table = Table.from_list(data, start_line):compile()

lua/orgmode/config/defaults.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ return {
119119
org_promote_subtree = '<s',
120120
org_demote_subtree = '>s',
121121
org_meta_return = '<Leader><CR>', -- Add headling, item or row
122+
org_return = {
123+
i = '<CR>',
124+
},
122125
org_insert_heading_respect_content = '<Leader>oih', -- Add new headling after current heading block with same level
123126
org_insert_todo_heading = '<Leader>oiT', -- Add new todo headling right after current heading with same level
124127
org_insert_todo_heading_respect_content = '<Leader>oit', -- Add new todo headling after current heading block on same level

lua/orgmode/config/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Config:new(opts)
1414
opts = vim.tbl_deep_extend('force', defaults, opts or {}),
1515
todo_keywords = nil,
1616
ts_hl_enabled = nil,
17+
old_cr_mapping = nil,
1718
}
1819
setmetatable(data, self)
1920
return data
@@ -174,6 +175,9 @@ function Config:get_todo_keywords()
174175
end
175176

176177
function Config:setup_mappings(category)
178+
if not self.old_cr_mapping then
179+
self.old_cr_mapping = vim.fn.maparg('<CR>', 'i')
180+
end
177181
if self.opts.mappings.disable_all then
178182
return
179183
end
@@ -203,7 +207,7 @@ function Config:setup_mappings(category)
203207
end
204208

205209
for name, lhs in pairs(self.opts.mappings[category]) do
206-
if mappings[category] and mappings[category][name] then
210+
if mappings[category] and mappings[category][name] and lhs then
207211
local map = {}
208212
if type(mappings[category][name]) == 'table' and not vim.tbl_islist(mappings[category][name]) then
209213
-- multi-mode mapping

lua/orgmode/config/mappings.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ return {
6767
org_move_subtree_up = { 'org_mappings.move_subtree_up' },
6868
org_move_subtree_down = { 'org_mappings.move_subtree_down' },
6969
org_export = { 'org_mappings.export' },
70+
org_return = {
71+
i = { 'org_mappings.org_return' },
72+
},
7073
org_next_visible_heading = {
7174
n = { 'org_mappings.next_visible_heading' },
7275
x = { 'org_mappings.next_visible_heading' },

lua/orgmode/org/mappings.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local constants = require('orgmode.utils.constants')
1111
local ts_utils = require('nvim-treesitter.ts_utils')
1212
local utils = require('orgmode.utils')
1313
local ts_org = require('orgmode.treesitter')
14+
local ts_table = require('orgmode.treesitter.table')
1415

1516
---@class OrgMappings
1617
---@field capture Capture
@@ -430,6 +431,21 @@ function OrgMappings:do_demote(whole_subtree)
430431
end
431432
end
432433

434+
function OrgMappings:org_return()
435+
local actions = {
436+
ts_table.handle_cr,
437+
}
438+
439+
for _, action in ipairs(actions) do
440+
local handled = action()
441+
if handled then
442+
return
443+
end
444+
end
445+
446+
return vim.api.nvim_feedkeys(utils.esc(config.old_cr_mapping or '<CR>'), 'n', true)
447+
end
448+
433449
function OrgMappings:handle_return(suffix)
434450
suffix = suffix or ''
435451
local current_file = Files.get_current_file()

lua/orgmode/parser/table/init.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,28 @@ function Table:new(opts)
2727
end
2828

2929
---@param row TableRow
30+
---@param at_position number?
3031
---@return Table
31-
function Table:add_row(row)
32-
table.insert(self.rows, row)
32+
function Table:add_row(row, at_position)
33+
if at_position then
34+
table.insert(self.rows, at_position, row)
35+
else
36+
table.insert(self.rows, row)
37+
end
3338
for col_nr, cell in ipairs(row.cells) do
3439
if not self.cols_width[col_nr] or self.cols_width[col_nr] < cell.display_len then
3540
self.cols_width[col_nr] = cell.display_len
3641
end
3742
end
38-
self.range.end_line = self.range.end_line + 1
43+
self.range.end_line = self.range.start_line + #self.rows - 1
3944
return self
4045
end
4146

4247
function Table:populate_missing_cells()
4348
for _, row in ipairs(self.rows) do
4449
row:populate_missing_cells()
4550
end
51+
return self
4652
end
4753

4854
---@return Table

lua/orgmode/treesitter/table.lua

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@ local query = vim.treesitter.query
77
---@class TsTable
88
---@field node userdata
99
---@field data table[]
10+
---@field tbl Table
1011
local TsTable = {}
1112

13+
function TsTable.from_current_node()
14+
vim.treesitter.get_parser(0, 'org', {}):parse()
15+
local view = vim.fn.winsaveview()
16+
-- Go to first non blank char
17+
vim.cmd([[norm! _]])
18+
local node = ts_utils.get_node_at_cursor()
19+
vim.fn.winrestview(view)
20+
if not node then
21+
return false
22+
end
23+
local table_node = utils.get_closest_parent_of_type(node, 'table', true)
24+
if not table_node then
25+
return false
26+
end
27+
return TsTable:new({ node = table_node })
28+
end
29+
1230
function TsTable:new(opts)
1331
local data = {}
1432
data.node = opts.node
@@ -43,43 +61,79 @@ function TsTable:_parse_data()
4361
end
4462

4563
self.data = rows
64+
local start_row, start_col = self.node:range()
65+
self.tbl = Table.from_list(self.data, start_row + 1, start_col + 1)
4666
end
4767

48-
function TsTable:rerender()
49-
local start_row, start_col = self.node:range()
50-
local tbl = Table.from_list(self.data, start_row + 1, start_col + 1)
68+
function TsTable:reformat()
69+
local view = vim.fn.winsaveview()
70+
vim.api.nvim_buf_set_lines(0, self.tbl.range.start_line - 1, self.tbl.range.end_line, false, self:_get_content())
71+
vim.fn.winrestview(view)
72+
end
73+
74+
---@private
75+
function TsTable:_get_content()
76+
local start_row = self.node:range()
5177
local first_line = vim.api.nvim_buf_get_lines(0, start_row, start_row + 1, true)
5278
local indent = first_line and first_line[1]:match('^%s*') or ''
5379
indent = config:get_indent(indent:len())
5480

55-
local contents = tbl:draw()
81+
local contents = self.tbl:draw()
5682
local indented = {}
5783
for _, content in ipairs(contents) do
5884
table.insert(indented, string.format('%s%s', indent, content))
5985
end
60-
local view = vim.fn.winsaveview()
61-
vim.api.nvim_buf_set_lines(0, tbl.range.start_line - 1, tbl.range.end_line - 1, false, indented)
62-
vim.fn.winrestview(view)
63-
end
6486

65-
local function format()
66-
local view = vim.fn.winsaveview()
67-
-- Go to first non blank char
68-
vim.cmd([[norm! _]])
69-
local node = ts_utils.get_node_at_cursor()
70-
vim.fn.winrestview(view)
87+
return indented
88+
end
7189

90+
local function get_table_from_node(node)
7291
if not node then
7392
return false
7493
end
7594
local table_node = utils.get_closest_parent_of_type(node, 'table', true)
7695
if not table_node then
7796
return false
7897
end
79-
TsTable:new({ node = table_node }):rerender()
98+
return TsTable:new({ node = table_node })
99+
end
100+
101+
function TsTable:add_row()
102+
local line = vim.fn.line('.')
103+
vim.api.nvim_buf_set_lines(0, line, line, true, { '|' })
104+
return TsTable.from_current_node():reformat()
105+
end
106+
107+
local function format()
108+
local tbl = TsTable.from_current_node()
109+
110+
if not tbl then
111+
return false
112+
end
113+
114+
tbl:reformat()
115+
return true
116+
end
117+
118+
local function handle_cr()
119+
if vim.fn.col('.') == vim.fn.col('$') then
120+
return false
121+
end
122+
local tbl = TsTable.from_current_node()
123+
if not tbl then
124+
return false
125+
end
126+
127+
tbl:add_row()
128+
vim.api.nvim_feedkeys(utils.esc('<Down>'), 'n', true)
129+
vim.schedule(function()
130+
vim.cmd([[norm! F|]])
131+
vim.api.nvim_feedkeys(utils.esc('<Right><Right>'), 'n', true)
132+
end)
80133
return true
81134
end
82135

83136
return {
84137
format = format,
138+
handle_cr = handle_cr,
85139
}

0 commit comments

Comments
 (0)