Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b92e84

Browse files
feat: Publish event when heading is toggled with <prefix>*
1 parent 42a4ccf commit 8b92e84

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---@class OrgHeadingToggledEvent: OrgEvent
2+
---@field headline? OrgHeadline
3+
---@field line? number
4+
---@field action 'line_to_headline' | 'headline_to_line' | 'line_to_child_headline'
5+
local HeadingToggledEvent = {
6+
type = 'orgmode.heading_toggled',
7+
}
8+
HeadingToggledEvent.__index = HeadingToggledEvent
9+
10+
---@param line number
11+
---@param action 'line_to_headline' | 'headline_to_line' | 'line_to_child_headline'
12+
---@param headline? OrgHeadline
13+
function HeadingToggledEvent:new(line, action, headline)
14+
return setmetatable({
15+
line = line,
16+
headline = headline,
17+
action = action,
18+
}, self)
19+
end
20+
21+
return HeadingToggledEvent

lua/orgmode/events/types/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ return {
55
TodoChanged = require('orgmode.events.types.todo_changed_event'),
66
HeadlinePromoted = require('orgmode.events.types.headline_promoted_event'),
77
HeadlineDemoted = require('orgmode.events.types.headline_demoted_event'),
8+
HeadingToggled = require('orgmode.events.types.heading_toggled'),
89
}

lua/orgmode/org/mappings.lua

+28-20
Original file line numberDiff line numberDiff line change
@@ -348,35 +348,43 @@ function OrgMappings:todo_prev_state()
348348
end
349349

350350
function OrgMappings:toggle_heading()
351-
local line = vim.fn.getline('.')
352-
-- TODO: allow nil
351+
local line_number = vim.fn.line('.')
352+
local line = vim.fn.getline(line_number)
353353
local parent = self.files:get_closest_headline_or_nil()
354+
355+
local set_line_and_dispatch_event = function(line_content, action)
356+
vim.fn.setline(line_number, line_content)
357+
EventManager.dispatch(
358+
events.HeadingToggled:new(line_number, action, self.files:get_closest_headline_or_nil({ line_number, 0 }))
359+
)
360+
end
361+
-- Convert to headline
354362
if not parent then
355-
line = '* ' .. line
356-
vim.fn.setline('.', line)
357-
return
363+
return set_line_and_dispatch_event('* ' .. line, 'line_to_headline')
358364
end
359365

366+
-- Convert headline to plain text
360367
if parent:get_range().start_line == vim.api.nvim_win_get_cursor(0)[1] then
361368
line = line:gsub('^%*+%s', '')
362-
else
363-
line = line:gsub('^(%s*)', '')
364-
if line:match('^[%*-]%s') then -- handle lists
365-
line = line:gsub('^[%*-]%s', '') -- strip bullet
366-
local todo_keywords = config:get_todo_keywords()
367-
line = line:gsub('^%[([X%s])%]%s', function(checkbox_state)
368-
if checkbox_state == 'X' then
369-
return todo_keywords:first_by_type('DONE').value .. ' '
370-
else
371-
return todo_keywords:first_by_type('TODO').value .. ' '
372-
end
373-
end)
374-
end
369+
return set_line_and_dispatch_event(line, 'headline_to_line')
370+
end
375371

376-
line = string.rep('*', parent:get_level() + 1) .. ' ' .. line
372+
line = line:gsub('^(%s*)', '')
373+
if line:match('^[%*-]%s') then -- handle lists
374+
line = line:gsub('^[%*-]%s', '') -- strip bullet
375+
local todo_keywords = config:get_todo_keywords()
376+
line = line:gsub('^%[([X%s])%]%s', function(checkbox_state)
377+
if checkbox_state == 'X' then
378+
return todo_keywords:first_by_type('DONE').value .. ' '
379+
else
380+
return todo_keywords:first_by_type('TODO').value .. ' '
381+
end
382+
end)
377383
end
378384

379-
vim.fn.setline('.', line)
385+
line = string.rep('*', parent:get_level() + 1) .. ' ' .. line
386+
387+
return set_line_and_dispatch_event(line, 'line_to_child_headline')
380388
end
381389

382390
---Prompt for a note

0 commit comments

Comments
 (0)