@@ -348,35 +348,43 @@ function OrgMappings:todo_prev_state()
348348end
349349
350350function 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' )
380388end
381389
382390--- Prompt for a note
0 commit comments